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

8b15475a8c2c51a5cc2da1d221b9741a3e6df47a
[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 local awful        = require("awful")
14
15 local string       = { match = string.match }
16
17 local setmetatable = setmetatable
18
19 local function worker (args)
20    local kbdlayout    = {}
21    kbdlayout.widget   = wibox.widget.textbox('')
22
23    local layouts          = args.layouts
24    local settings         = args.settings or function () end
25    local add_us_secondary = args.add_us_secondary or true
26    local timeout          = args.timeout or 5
27
28    local idx              = 1
29
30    -- Mouse bindings
31    kbdlayout.widget:buttons(awful.util.table.join(
32                               awful.button({ }, 1, function () kbdlayout.next() end),
33                               awful.button({ }, 3, function () kbdlayout.prev() end)))
34
35    local function run_settings (layout, variant)
36       widget = kbdlayout.widget
37       kbdlayout_now = { layout=string.match(layout, "[^,]+"), -- Make sure to match the primary layout only.
38                         variant=variant }
39       settings()
40    end
41
42    function kbdlayout.update ()
43       local status = read_pipe('setxkbmap -query')
44
45       run_settings(string.match(status, "layout:%s*([^\n]*)"),
46                    string.match(status, "variant:%s*([^\n]*)"))
47    end
48
49    function kbdlayout.set (i)
50       idx = ((i - 1) % #layouts) + 1 -- Make sure to wrap around as needed.
51       local to_execute = 'setxkbmap ' .. layouts[idx].layout
52
53       if add_us_secondary then
54          to_execute = to_execute .. ",us"
55       end
56
57       if layouts[idx].variant then
58          to_execute = to_execute .. ' ' .. layouts[idx].variant
59       end
60
61       if os.execute(to_execute) then
62          run_settings(layouts[idx].layout, layouts[idx].variant)
63       end
64    end
65
66    function kbdlayout.next ()
67       kbdlayout.set(idx + 1)
68    end
69
70    function kbdlayout.prev ()
71       kbdlayout.set(idx - 1)
72    end
73
74    newtimer("kbdlayout", timeout, kbdlayout.update)
75    return setmetatable(kbdlayout, { __index = kbdlayout.widget })
76 end
77
78 return setmetatable({}, { __call = function (_, ...) return worker(...) end })