All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
4 Licensed under GNU General Public License v2
5 * (c) 2015, Dario Gjorgjevski
9 local helpers = require("lain.helpers")
10 local awful = require("awful")
11 local wibox = require("wibox")
12 local string = { format = string.format,
13 match = string.match }
14 local execute = os.execute
15 local setmetatable = setmetatable
17 -- Keyboard layout switcher
18 -- lain.widgets.contrib.kblayout
21 local function worker(args)
22 local args = args or {}
23 local layouts = args.layouts or {}
24 local settings = args.settings or function () end
25 local add_us_secondary = true
26 local timeout = args.timeout or 5
29 if args.add_us_secondary == false then add_us_secondary = false end
31 kbdlayout.widget = wibox.widget.textbox()
33 local function kbd_run_settings(layout, variant)
35 layout = string.match(layout, "[^,]+"), -- Make sure to match the primary layout only.
38 widget = kbdlayout.widget
42 function kbdlayout.update()
43 helpers.async("setxkbmap -query", function(status)
44 kbd_run_settings(string.match(status, "layout:%s*([^\n]*)"),
45 string.match(status, "variant:%s*([^\n]*)"))
49 function kbdlayout.set(i)
50 if #layouts == 0 then return end
51 idx = ((i - 1) % #layouts) + 1 -- Make sure to wrap around as needed.
52 local to_execute = "setxkbmap " .. layouts[idx].layout
54 if add_us_secondary and not string.match(layouts[idx].layout, ",?us,?") then
55 to_execute = to_execute .. ",us"
58 if layouts[idx].variant then
59 to_execute = to_execute .. " " .. layouts[idx].variant
62 if execute(to_execute) then
63 kbd_run_settings(layouts[idx].layout, layouts[idx].variant)
67 function kbdlayout.next() kbdlayout.set(idx + 1) end
68 function kbdlayout.prev() kbdlayout.set(idx - 1) end
71 kbdlayout.widget:buttons(awful.util.table.join(
72 awful.button({ }, 1, function () kbdlayout.next() end),
73 awful.button({ }, 3, function () kbdlayout.prev() end)))
75 helpers.newtimer("kbdlayout", timeout, kbdlayout.update)
80 return setmetatable({}, { __call = function (_, ...) return worker(...) end })