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")
11 local awful = require("awful")
12 local wibox = require("wibox")
14 local string = { format = string.format,
15 match = string.match }
16 local execute = os.execute
18 local setmetatable = setmetatable
20 -- Keyboard layout switcher
21 -- lain.widgets.contrib.kblayout
23 local function worker(args)
25 local layouts = args.layouts
26 local settings = args.settings or function () end
27 local add_us_secondary = true
28 local timeout = args.timeout or 5
31 if args.add_us_secondary == false then add_us_secondary = false end
33 kbdlayout.widget = wibox.widget.textbox()
36 kbdlayout.widget:buttons(awful.util.table.join(
37 awful.button({ }, 1, function () kbdlayout.next() end),
38 awful.button({ }, 3, function () kbdlayout.prev() end)))
40 local function run_settings(layout, variant)
42 layout = string.match(layout, "[^,]+"), -- Make sure to match the primary layout only.
45 widget = kbdlayout.widget
49 function kbdlayout.update()
50 helpers.async(string.format("%s -c 'setxkbmap -query'", awful.util.shell),
52 run_settings(string.match(status, "layout:%s*([^\n]*)"),
53 string.match(status, "variant:%s*([^\n]*)"))
57 function kbdlayout.set(i)
58 idx = ((i - 1) % #layouts) + 1 -- Make sure to wrap around as needed.
59 local to_execute = "setxkbmap " .. layouts[idx].layout
61 if add_us_secondary and not string.match(layouts[idx].layout, ",?us,?") then
62 to_execute = to_execute .. ",us"
65 if layouts[idx].variant then
66 to_execute = to_execute .. " " .. layouts[idx].variant
69 if execute(to_execute) then
70 run_settings(layouts[idx].layout, layouts[idx].variant)
74 function kbdlayout.next()
75 kbdlayout.set(idx + 1)
78 function kbdlayout.prev()
79 kbdlayout.set(idx - 1)
82 helpers.newtimer("kbdlayout", timeout, kbdlayout.update)
84 return setmetatable(kbdlayout, { __index = kbdlayout.widget })
87 return setmetatable({}, { __call = function (_, ...) return worker(...) end })