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

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