]> 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:

abstract out reading full output of pipes
[etc/awesome.git] / widgets / contrib / kbdlayout.lua
1
2 --[[
3
4      Licensed under GNU General Public License v2 
5       * (c) 2015, Dario Gjorgjevski
6
7 --]]
8
9 local newtimer     = require("lain.helpers").newtimer
10 local read_pipe    = require("lain.helpers").read_pipe
11
12 local wibox        = require("wibox")
13
14 local string       = { match = string.match }
15
16 local setmetatable = setmetatable
17
18 local function worker (args)
19    local kbdlayout    = {}
20    kbdlayout.widget   = wibox.widget.textbox('')
21
22    local layouts          = args.layouts
23    local settings         = args.settings or function () end
24    local add_us_secondary = args.add_us_secondary or true
25    local idx              = 1
26    
27    local function run_settings (layout, variant)
28       widget = kbdlayout.widget
29       kbdlayout_now = { layout=string.match(layout, "[^,]+"), -- Make sure to match the primary layout only.
30                         variant=variant }
31       settings()
32    end
33    
34    function kbdlayout.update ()
35       local status = read_pipe('setxkbmap -query')
36
37       run_settings(string.match(status, "layout:%s*([^\n]*)"),
38                    string.match(status, "variant:%s*([^\n]*)"))
39    end
40
41    function kbdlayout.set (i)
42       idx = ((i - 1) % #layouts) + 1 -- Make sure to wrap around as needed.
43       local to_execute = 'setxkbmap ' .. layouts[idx].layout
44
45       if add_us_secondary then
46          to_execute = to_execute .. ",us"
47       end
48
49       if layouts[idx].variant then
50          to_execute = to_execute .. ' ' .. layouts[idx].variant
51       end
52
53       if os.execute(to_execute) then
54          run_settings(layouts[idx].layout, layouts[idx].variant)
55       end
56    end
57
58    function kbdlayout.next ()
59       kbdlayout.set(idx + 1)
60    end
61
62    function kbdlayout.prev ()
63       kbdlayout.set(idx - 1)
64    end
65
66    newtimer("kbdlayout", args.timeout or 10, kbdlayout.update)
67    return setmetatable(kbdlayout, { __index = kbdlayout.widget })
68 end
69
70 return setmetatable({}, { __call = function (_, ...) return worker(...) end })