]> git.madduck.net Git - etc/awesome.git/blob - widget/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:

widget.bat: add failsafe AC
[etc/awesome.git] / widget / contrib / kbdlayout.lua
1 --[[
2
3      Licensed under GNU General Public License v2
4       * (c) 2015, Dario Gjorgjevski
5
6 --]]
7
8 local helpers      = require("lain.helpers")
9 local awful        = require("awful")
10 local wibox        = require("wibox")
11 local string       = { format = string.format,
12                        match  = string.match }
13 local execute      = os.execute
14 local setmetatable = setmetatable
15
16 -- Keyboard layout switcher
17 -- lain.widget.contrib.kblayout
18
19 local function factory(args)
20     local kbdlayout        = { widget = wibox.widget.textbox() }
21     local args             = args or {}
22     local layouts          = args.layouts or {}
23     local settings         = args.settings or function () end
24     local add_us_secondary = true
25     local timeout          = args.timeout or 5
26     local idx              = 1
27
28     if args.add_us_secondary == false then add_us_secondary = false end
29
30     local function kbd_run_settings(layout, variant)
31         kbdlayout_now = {
32             layout  = string.match(layout, "[^,]+"), -- Make sure to match the primary layout only.
33             variant = variant
34         }
35         widget = kbdlayout.widget
36         settings()
37     end
38
39     function kbdlayout.update()
40         helpers.async("setxkbmap -query", function(status)
41             kbd_run_settings(string.match(status, "layout:%s*([^\n]*)"),
42             string.match(status, "variant:%s*([^\n]*)"))
43         end)
44     end
45
46     function kbdlayout.set(i)
47         if #layouts == 0 then return end
48         idx = ((i - 1) % #layouts) + 1 -- Make sure to wrap around as needed.
49         local to_execute = "setxkbmap " .. layouts[idx].layout
50
51         if add_us_secondary and not string.match(layouts[idx].layout, ",?us,?") then
52             to_execute = to_execute .. ",us"
53         end
54
55         if layouts[idx].variant then
56             to_execute = to_execute .. " " .. layouts[idx].variant
57         end
58
59         if execute(to_execute) then
60             kbd_run_settings(layouts[idx].layout, layouts[idx].variant)
61         end
62    end
63
64    function kbdlayout.next() kbdlayout.set(idx + 1) end
65    function kbdlayout.prev() kbdlayout.set(idx - 1) end
66
67    -- Mouse bindings
68    kbdlayout.widget:buttons(awful.util.table.join(
69                               awful.button({ }, 1, function () kbdlayout.next() end),
70                               awful.button({ }, 3, function () kbdlayout.prev() end)))
71
72    helpers.newtimer("kbdlayout", timeout, kbdlayout.update)
73
74    return kbdlayout
75 end
76
77 return factory