]> git.madduck.net Git - etc/awesome.git/blob - widgets/contrib/kbdlayout.lua

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

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.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

add new widget (kbdlayout.lua)
[etc/awesome.git] / widgets / contrib / kbdlayout.lua
1
2 local newtimer     = require("lain.helpers").newtimer
3 local wibox        = require("wibox")
4
5 local string       = { match = string.match }
6 local io           = { popen = io.popen }
7
8 local setmetatable = setmetatable
9
10 local function worker (args)
11    local kbdlayout    = {}
12    kbdlayout.widget   = wibox.widget.textbox('')
13
14    local settings     = args.settings or function () end
15    local layouts      = args.layouts
16    local idx          = 1
17    
18    local function run_settings (layout, variant)
19       widget = kbdlayout.widget
20       kbdlayout_now = { layout=layout, variant=variant }
21       settings()
22    end
23    
24    function kbdlayout.update ()
25       local file   = assert(io.popen('setxkbmap -query'))
26       local status = file:read('*all')
27       file:close()
28
29       run_settings(string.match(status, "layout:%s*([^\n]*)%s*"),
30                    string.match(status, "variant:%s*([^\n]*)%s*"))
31    end
32
33    function kbdlayout.set (i)
34       idx = ((i - 1) % #layouts) + 1 -- Make sure to wrap around as needed.
35       local to_execute = 'setxkbmap ' .. layouts[idx].layout
36
37       if layouts[idx].variant then
38          to_execute = to_execute .. ' ' .. layouts[idx].variant
39       end
40
41       if os.execute(to_execute) then
42          run_settings(layouts[idx].layout, layouts[idx].variant)
43       end
44    end
45
46    function kbdlayout.next ()
47       kbdlayout.set(idx + 1)
48    end
49
50    function kbdlayout.prev ()
51       kbdlayout.set(idx - 1)
52    end
53
54    newtimer("kbdlayout", args.timeout or 10, kbdlayout.update)
55    return setmetatable(kbdlayout, { __index = kbdlayout.widget })
56 end
57
58 return setmetatable({}, { __call = function (_, ...) return worker(...) end })