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 newtimer = require("lain.helpers").newtimer
10 local read_pipe = require("lain.helpers").read_pipe
12 local wibox = require("wibox")
13 local awful = require("awful")
15 local string = { match = string.match }
17 local setmetatable = setmetatable
19 -- Keyboard layout switcher
20 -- lain.widgets.contrib.kblayout
22 local function worker(args)
24 kbdlayout.widget = wibox.widget.textbox('')
26 local layouts = args.layouts
27 local settings = args.settings or function () end
28 local add_us_secondary = true
29 local timeout = args.timeout or 5
32 if args.add_us_secondary == false then add_us_secondary = false end
35 kbdlayout.widget:buttons(awful.util.table.join(
36 awful.button({ }, 1, function () kbdlayout.next() end),
37 awful.button({ }, 3, function () kbdlayout.prev() end)))
39 local function run_settings(layout, variant)
40 widget = kbdlayout.widget
41 kbdlayout_now = { layout=string.match(layout, "[^,]+"), -- Make sure to match the primary layout only.
46 function kbdlayout.update()
47 local status = read_pipe('setxkbmap -query')
49 run_settings(string.match(status, "layout:%s*([^\n]*)"),
50 string.match(status, "variant:%s*([^\n]*)"))
53 function kbdlayout.set(i)
54 idx = ((i - 1) % #layouts) + 1 -- Make sure to wrap around as needed.
55 local to_execute = 'setxkbmap ' .. layouts[idx].layout
57 if add_us_secondary and not string.match(layouts[idx].layout, ",?us,?") then
58 to_execute = to_execute .. ",us"
61 if layouts[idx].variant then
62 to_execute = to_execute .. ' ' .. layouts[idx].variant
65 if os.execute(to_execute) then
66 run_settings(layouts[idx].layout, layouts[idx].variant)
70 function kbdlayout.next()
71 kbdlayout.set(idx + 1)
74 function kbdlayout.prev()
75 kbdlayout.set(idx - 1)
78 newtimer("kbdlayout", timeout, kbdlayout.update)
79 return setmetatable(kbdlayout, { __index = kbdlayout.widget })
82 return setmetatable({}, { __call = function (_, ...) return worker(...) end })