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

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