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.
3 Licensed under GNU General Public License v2
4 * (c) 2015, Dario Gjorgjevski
8 local helpers = require("lain.helpers")
9 local awful = require("awful")
10 local wibox = require("wibox")
11 local string = { format = string.format,
12 match = string.match }
13 local execute = os.execute
14 local setmetatable = setmetatable
16 -- Keyboard layout switcher
17 -- lain.widget.contrib.kblayout
19 local function factory(args)
20 local kbdlayout = { widget = wibox.widget.textbox() }
21 local args = args or {}
22 local layouts = args.layouts or {}
23 local settings = args.settings or function () end
24 local add_us_secondary = true
25 local timeout = args.timeout or 5
28 if args.add_us_secondary == false then add_us_secondary = false end
30 local function kbd_run_settings(layout, variant)
32 layout = string.match(layout, "[^,]+"), -- Make sure to match the primary layout only.
35 widget = kbdlayout.widget
39 function kbdlayout.update()
40 helpers.async("setxkbmap -query", function(status)
41 kbd_run_settings(string.match(status, "layout:%s*([^\n]*)"),
42 string.match(status, "variant:%s*([^\n]*)"))
46 function kbdlayout.set(i)
47 if #layouts == 0 then return end
48 idx = ((i - 1) % #layouts) + 1 -- Make sure to wrap around as needed.
49 local to_execute = "setxkbmap " .. layouts[idx].layout
51 if add_us_secondary and not string.match(layouts[idx].layout, ",?us,?") then
52 to_execute = to_execute .. ",us"
55 if layouts[idx].variant then
56 to_execute = to_execute .. " " .. layouts[idx].variant
59 if execute(to_execute) then
60 kbd_run_settings(layouts[idx].layout, layouts[idx].variant)
64 function kbdlayout.next() kbdlayout.set(idx + 1) end
65 function kbdlayout.prev() kbdlayout.set(idx - 1) end
68 kbdlayout.widget:buttons(awful.util.table.join(
69 awful.button({ }, 1, function () kbdlayout.next() end),
70 awful.button({ }, 3, function () kbdlayout.prev() end)))
72 helpers.newtimer("kbdlayout", timeout, kbdlayout.update)