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

taskwarrior: asynchronous; #213 enhanced
[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 helpers      = require("lain.helpers")
10 local awful        = require("awful")
11 local wibox        = require("wibox")
12 local string       = { format = string.format,
13                        match  = string.match }
14 local execute      = os.execute
15 local setmetatable = setmetatable
16
17 -- Keyboard layout switcher
18 -- lain.widgets.contrib.kblayout
19 local kbdlayout = helpers.make_widget_textbox()
20
21 local function worker(args)
22    local args             = args or {}
23    local layouts          = args.layouts or {}
24    local settings         = args.settings or function () end
25    local add_us_secondary = true
26    local timeout          = args.timeout or 5
27    local idx              = 1
28
29    if args.add_us_secondary == false then add_us_secondary = false end
30
31    local function kbd_run_settings(layout, variant)
32       kbdlayout_now = {
33           layout  = string.match(layout, "[^,]+"), -- Make sure to match the primary layout only.
34           variant = variant
35       }
36       widget = kbdlayout.widget
37       settings()
38    end
39
40    function kbdlayout.update()
41       helpers.async(string.format("%s -c 'setxkbmap -query'", awful.util.shell),
42       function(status)
43           kbd_run_settings(string.match(status, "layout:%s*([^\n]*)"),
44           string.match(status, "variant:%s*([^\n]*)"))
45       end)
46    end
47
48    function kbdlayout.set(i)
49       if #layouts == 0 then return end
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 and not string.match(layouts[idx].layout, ",?us,?") 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 execute(to_execute) then
62          kbd_run_settings(layouts[idx].layout, layouts[idx].variant)
63       end
64    end
65
66    function kbdlayout.next() kbdlayout.set(idx + 1) end
67    function kbdlayout.prev() kbdlayout.set(idx - 1) end
68
69    -- Mouse bindings
70    kbdlayout.widget:buttons(awful.util.table.join(
71                               awful.button({ }, 1, function () kbdlayout.next() end),
72                               awful.button({ }, 3, function () kbdlayout.prev() end)))
73
74    helpers.newtimer("kbdlayout", timeout, kbdlayout.update)
75
76    return setmetatable(kbdlayout, { __index = kbdlayout.widget })
77 end
78
79 return setmetatable({}, { __call = function (_, ...) return worker(...) end })