Debian Codegraph @

obvious/keymap_switch/init.lua

raw symbols SCIP
1 ---------------------------------
2 -- Author: Andrei Thorp --
3 -- Copyright 2010 Andrei Thorp --
4 ---------------------------------
5 -- Dependencies: setxkbmap (shell command)
6 -- Ideas:
7 -- * user specified rewrite table: convert us(dvorak) to "Dvorak"
8 -- * use formatting system to allow people to format their text widgets
9 -- * allow the user to override the text widget with some other widget
10 -- * filter out the current layout from the menu that appears on click
11 -- * let user configure the quick update delay (1 second) in case their
12 -- computer is unusually slow or quick or something
13
14 local setmetatable = setmetatable
15 local pairs = pairs
16 local ipairs = ipairs
17 local io = {
18 popen = io.popen
19 }
20 local awful = require("awful")
21 local wibox = require("wibox")
22 local lib = {
23 hooks = require("obvious.lib.hooks"),
24 markup = require("obvious.lib.markup")
25 }
26
27 local defaults = {}
28 defaults.layouts = {}
29 defaults.menu = nil
30 defaults.widget = wibox.widget.textbox()
31 defaults.widget:set_text("...")
32
33 local current_index = 1
34
35 -- Clone the defaults to the used settings
36 local settings = {}
37 for key, value in pairs(defaults) do
38 settings[key] = value
39 end
40
41 -- forward declaration of functions
42 local update
43 local set_layouts
44
45 -- Updates once after a short delay and then unregisters its timer
46 local function delayed_update_once(start)
47 if start == true then
48 lib.hooks.timer.register(1, 1, delayed_update_once, "One-off update for keymap widget")
49 lib.hooks.timer.start(delayed_update_once)
50 else
51 update()
52 lib.hooks.timer.unregister(delayed_update_once)
53 end
54 end
55
56 local setup_done = false
57 local function init_once()
58 if setup_done then
59 return
60 end
61 lib.hooks.timer.register(5, 60, update, "Update for the keymap widget")
62 lib.hooks.timer.start(update)
63 delayed_update_once(true)
64 setup_done = true
65 end
66
67 local function switch_keymap(layout_string)
68 awful.util.spawn("setxkbmap \"" .. layout_string .. "\"")
69 delayed_update_once(true)
70 end
71
72 local function init(widget)
73 init_once()
74
75 -- Use the default widget if not specified
76 if widget then
77 settings.widget = widget
78 end
79
80 local layouts = settings.layouts
81 settings.widget.rotate_layout = function()
82 current_index = current_index + 1
83
84 if current_index > #layouts then
85 current_index = 1
86 end
87
88 switch_keymap(layouts[current_index])
89 end
90
91 -- Reconfigure the menu immediately
92 set_layouts(settings.layouts)
93
94 -- Set up the on-click menu event
95 settings.widget:buttons(awful.util.table.join(
96 awful.button({ }, 1, function() settings.menu:toggle() end)
97 ))
98
99 return settings.widget
100 end
101
102 -- Returns the current keymap, as discovered from setxkbmap
103 local function get_current_keymap()
104 local fd = io.popen("setxkbmap -print")
105 if not fd then return end
106
107 for line in fd:lines() do
108 if line:match("xkb_symbols") then
109 local keymap = line:match('%+[a-zA-Z0-9()]*%+')
110
111 fd:close()
112 if not keymap then
113 return "unknown layout"
114 else
115 return keymap:sub(2, -2)
116 end
117 end
118 end
119
120 fd:close()
121 return "unknown layout"
122 end
123
124 --[[local]] function set_layouts(layouts_table)
125 settings.layouts = layouts_table or settings.layouts
126
127 local newitems = {}
128 for index, value in ipairs(settings.layouts) do
129 newitems[index] = { value, function()
130 current_index = index
131 switch_keymap(value)
132 end }
133 end
134
135 settings.menu = awful.menu.new({
136 id = "keymap_switch",
137 items = newitems
138 })
139 end
140
141 --[[local]] function update()
142 settings.widget:set_markup(get_current_keymap())
143 end
144
145 return setmetatable({
146 set_layouts = set_layouts,
147 update = update,
148 }, { __call = function() return init(settings.widget) end }) -- TODO let the user specify widget here
149
150 -- vim:ft=lua:ts=2:sw=2:sts=2:tw=80:et