author | Fabien Ninoles <fabien@tzone.org> |
Sat, 03 Jan 2015 11:21:36 -0500 | |
changeset 9 | 56e80afa3e1c |
parent 8 | 0eae48be1d62 |
child 11 | 7226709dc4c4 |
permissions | -rw-r--r-- |
7 | 1 |
require("naughty") |
2 |
require("awful") |
|
3 |
require("debug") |
|
4 |
require("vicious") |
|
5 |
require("wicked") |
|
6 |
require("beautiful") |
|
8 | 7 |
require("timer") |
7 | 8 |
|
9 |
local ipairs = ipairs |
|
10 |
local pairs = pairs |
|
11 |
local naughty = naughty |
|
12 |
local awful = awful |
|
13 |
local debug = debug |
|
14 |
local vicious = vicious |
|
15 |
local wicked = wicked |
|
16 |
local beautiful = beautiful |
|
8 | 17 |
local timer = timer |
7 | 18 |
|
19 |
local capi = { |
|
20 |
keygrabber = keygrabber, |
|
21 |
client = client, |
|
22 |
string = string, |
|
23 |
widget = widget, |
|
24 |
} |
|
25 |
||
26 |
module("my") |
|
27 |
||
28 |
local in_error = false |
|
29 |
function notify_error(err) |
|
30 |
-- Make sure we don't go into an endless error loop |
|
31 |
if in_error then return end |
|
32 |
in_error = true |
|
33 |
||
34 |
naughty.notify({ preset = naughty.config.presets.critical, |
|
35 |
title = "Oops, an error happened!", |
|
36 |
text = err }) |
|
37 |
in_error = false |
|
38 |
end |
|
39 |
||
40 |
function mkspawn(p) |
|
41 |
return function () awful.util.spawn(p) end |
|
42 |
end |
|
43 |
||
44 |
function setxkbmap(kb) |
|
45 |
XKBDIR="$HOME/.xkb" |
|
46 |
-- Strange bug on xkbcomp: pushing directly the output to the display |
|
47 |
-- lead to an error; We need to convert it first to .xkb and then feed |
|
48 |
-- xkbcomp again for setting the x display |
|
49 |
p = 'setxkbmap "' .. kb .. " -print | xkbcomp -xkb -a -I" .. XKBDIR .. " - - | kxbcomp - $DISPLAY" |
|
50 |
awful.util.spawn_with_shell(p) |
|
51 |
end |
|
52 |
||
53 |
function kill_all(rule) |
|
54 |
for c in awful.client.iterate(rule) do |
|
55 |
c:kill() |
|
56 |
end |
|
57 |
end |
|
58 |
||
59 |
function quit() |
|
60 |
kill_all(awful.rules.any) |
|
61 |
awesome.quit() |
|
62 |
end |
|
63 |
||
64 |
function show_clients() |
|
65 |
if instance then |
|
66 |
instance:hide() |
|
67 |
instance = nil |
|
68 |
else |
|
69 |
instance = awful.menu.clients({width=250}) |
|
70 |
end |
|
71 |
end |
|
72 |
||
73 |
local function widgets_cpu() |
|
74 |
local w = awful.widget.graph() |
|
75 |
-- Graph properties |
|
76 |
w:set_width(30) |
|
77 |
w:set_background_color("#729fcf") |
|
78 |
w:set_gradient_colors({ "#729fcf", "LightBlue", "white" }) |
|
79 |
-- Register widget |
|
80 |
vicious.register(w, vicious.widgets.cpu, "$1", 1) |
|
81 |
return w |
|
82 |
end |
|
83 |
||
84 |
local function widgets_mdp() |
|
85 |
-- Initialize widget |
|
86 |
local w = capi.widget({ type = "textbox" }) |
|
87 |
-- Register widget |
|
88 |
vicious.register(w, vicious.widgets.mpd, |
|
89 |
function (widget, args) |
|
90 |
if args["{state}"] == "Stop" then |
|
91 |
return " - " |
|
92 |
else |
|
93 |
return args["{Artist}"]..' - '.. args["{Title}"] |
|
94 |
end |
|
95 |
end, 10) |
|
96 |
return w |
|
97 |
end |
|
98 |
||
99 |
local function widgets_mem(monitor) |
|
100 |
-- Initialize widget |
|
101 |
local w = awful.widget.progressbar() |
|
102 |
-- Progressbar properties |
|
103 |
w:set_width(10) |
|
104 |
w:set_vertical(true) |
|
105 |
w:set_background_color("#729fcf") |
|
106 |
w:set_gradient_colors({ "#729fcf", "LightBlue", "white" }) |
|
107 |
-- Register widget |
|
108 |
vicious.register(w, vicious.widgets.mem, "$1", 5) |
|
109 |
return w |
|
110 |
end |
|
111 |
||
112 |
local function widgets_net(device) |
|
113 |
local w = capi.widget({ type = "textbox"}) |
|
114 |
-- w:set_vertical(true) |
|
115 |
w.width = 150 |
|
116 |
w.align = 'center' |
|
117 |
vicious.register(w, wicked.widgets.net, |
|
118 |
'${' .. device .. ' up} / ${' .. device .. ' down}', |
|
119 |
1) |
|
120 |
return w |
|
121 |
end |
|
122 |
||
123 |
local function widgets_mode() |
|
124 |
local w = capi.widget({ type = "textbox"}) |
|
125 |
widgets.mode_widget = w |
|
126 |
return w |
|
127 |
end |
|
128 |
||
129 |
local function widgets_keyboard() |
|
130 |
local w = capi.widget({ type = "textbox"}) |
|
131 |
widgets.keyboard_widget = w |
|
132 |
return w |
|
133 |
end |
|
134 |
||
135 |
local function widgets_decorated(w, onclick) |
|
136 |
local wg = w |
|
137 |
if w.widget then |
|
138 |
wg = w.widget |
|
139 |
end |
|
140 |
wg:buttons(awful.util.table.join( |
|
141 |
awful.button({}, 1, onclick))) |
|
142 |
return w |
|
143 |
end |
|
144 |
||
145 |
widgets = { |
|
146 |
cpu = widgets_cpu, |
|
147 |
mem = widgets_mem, |
|
148 |
net = widgets_net, |
|
149 |
mdp = widgets_mdp, |
|
150 |
mode = widgets_mode, |
|
151 |
keyboard = widgets_keyboard, |
|
152 |
decorated = widgets_decorated, |
|
153 |
} |
|
154 |
||
155 |
function make_key(k, n, f) |
|
156 |
return { key = k, name = n, func = f} |
|
157 |
end |
|
158 |
||
159 |
function show_kt(keytable, title) |
|
160 |
-- todo: replace with a menu ? |
|
161 |
text = "" |
|
162 |
for _, k in ipairs(keytable) do |
|
163 |
text = text .. "\'" .. k.key .. "\'\t" .. k.name .. "\n" |
|
164 |
end |
|
165 |
naughty.notify({text = text, |
|
166 |
title = title, |
|
167 |
position = "top_left", |
|
168 |
}) |
|
169 |
end |
|
170 |
||
171 |
local keynames = { |
|
172 |
[" "] = "space", |
|
173 |
["\t"] = "tab", |
|
174 |
} |
|
175 |
||
8 | 176 |
local ignored_mods = { |
177 |
["Mod2"] = true, |
|
178 |
} |
|
179 |
||
7 | 180 |
local function translate_key(mod, key) |
181 |
local skey = "" |
|
182 |
for m,v in pairs(mod) do |
|
8 | 183 |
if not ignored_mods[v] then |
7 | 184 |
skey = skey .. v .. "+" |
185 |
end |
|
186 |
end |
|
187 |
skey = skey .. (keynames[key] or key) |
|
188 |
return skey |
|
189 |
end |
|
190 |
||
191 |
local function kt_handler(keytable, mod, key, event) |
|
192 |
if event == "release" then |
|
193 |
return true |
|
194 |
end |
|
195 |
local theme = beautiful.get() |
|
196 |
widgets.mode_widget.bg = theme.bg_focus |
|
197 |
if key == "Escape" then |
|
198 |
return false |
|
199 |
end |
|
200 |
local skey = capi.string.lower(translate_key(mod, key)) |
|
201 |
if skey == "control+h" or skey == "shift+?" then |
|
202 |
show_kt(keytable, "Current binding") |
|
203 |
return true |
|
204 |
end |
|
205 |
for _,k in ipairs(keytable) do |
|
206 |
if skey == capi.string.lower(k.key) then |
|
207 |
return k.func(c) |
|
208 |
end |
|
209 |
end |
|
210 |
-- notify_error("Nothing for " .. skey) |
|
211 |
widgets.mode_widget.bg = theme.bg_urgent |
|
212 |
return true |
|
213 |
end |
|
214 |
||
215 |
local function stop_kt() |
|
216 |
capi.keygrabber.stop() |
|
217 |
widgets.mode_widget.visible = false |
|
218 |
end |
|
219 |
||
220 |
function mkinteractive(f) |
|
221 |
return function (c) |
|
222 |
stop_kt() |
|
223 |
return f(c) |
|
224 |
end |
|
225 |
end |
|
226 |
||
227 |
||
228 |
-- local is_in_run_kt = false |
|
229 |
function run_kt(c, keytable, title) |
|
230 |
widgets.mode_widget.text = "Mode: " .. title |
|
231 |
widgets.mode_widget.visible = true |
|
232 |
widgets.mode_widget.bg = "LightBlue" |
|
233 |
widgets.mode_widget:buttons( |
|
234 |
awful.util.table.join( |
|
235 |
awful.button({}, 1, function () show_kt(keytable, "Binding") end), |
|
236 |
awful.button({}, 3, stop_kt))) |
|
237 |
capi.keygrabber.run(function (mod, key, event) |
|
238 |
if not kt_handler(keytable, mod, key, event) then |
|
239 |
stop_kt() |
|
240 |
return false |
|
241 |
end |
|
242 |
return true |
|
243 |
end) |
|
244 |
end |
|
245 |
||
246 |
function make_kt(keytable, title) |
|
247 |
return function (c) |
|
248 |
-- if is_in_run_kt then |
|
249 |
-- -- notify_error("Already in run_kt:\n" .. debug.traceback()) |
|
250 |
-- is_in_run_kt = false -- reset |
|
251 |
-- return false |
|
252 |
-- end |
|
253 |
return run_kt(c, keytable, title) |
|
254 |
end |
|
255 |
end |
|
256 |
||
257 |
function make_focus_bydirection(direction) |
|
258 |
return function () |
|
259 |
awful.client.focus.bydirection(direction) |
|
260 |
if capi.client.focus then capi.client.focus:raise() end |
|
261 |
return true |
|
262 |
end |
|
263 |
end |
|
264 |
||
265 |
function make_globalkeys(modifiers, keytable) |
|
266 |
local t = {} |
|
267 |
for _,k in ipairs(keytable) do |
|
268 |
t = awful.util.table.join(t, awful.key(modifiers, k.key, k.func)) |
|
269 |
end |
|
270 |
return t |
|
271 |
end |
|
272 |
||
8 | 273 |
-- Install a sloppy-like focus with a timeout. |
274 |
||
275 |
function sloppy_toggle() |
|
276 |
sloppy = not sloppy |
|
277 |
end |
|
278 |
||
279 |
function sloppy_mouse_enter(c) |
|
280 |
if sloppy and |
|
281 |
c ~= sloppy_last_client and |
|
282 |
awful.client.focus.filter(c) and |
|
283 |
awful.layout.get(c.screen) ~= awful.layout.suit.magnifier |
|
284 |
then |
|
9
56e80afa3e1c
Only start sloppy timer on a mouse_enter.
Fabien Ninoles <fabien@tzone.org>
parents:
8
diff
changeset
|
285 |
if sloppy_timer.started |
56e80afa3e1c
Only start sloppy timer on a mouse_enter.
Fabien Ninoles <fabien@tzone.org>
parents:
8
diff
changeset
|
286 |
then |
56e80afa3e1c
Only start sloppy timer on a mouse_enter.
Fabien Ninoles <fabien@tzone.org>
parents:
8
diff
changeset
|
287 |
sloppy_timer:again() |
56e80afa3e1c
Only start sloppy timer on a mouse_enter.
Fabien Ninoles <fabien@tzone.org>
parents:
8
diff
changeset
|
288 |
else |
56e80afa3e1c
Only start sloppy timer on a mouse_enter.
Fabien Ninoles <fabien@tzone.org>
parents:
8
diff
changeset
|
289 |
sloppy_timer:start() |
56e80afa3e1c
Only start sloppy timer on a mouse_enter.
Fabien Ninoles <fabien@tzone.org>
parents:
8
diff
changeset
|
290 |
end |
8 | 291 |
sloppy_last_client = c |
292 |
end |
|
293 |
end |
|
294 |
||
295 |
function sloppy_on_timeout() |
|
296 |
if sloppy |
|
297 |
then |
|
298 |
local c = awful.mouse.client_under_pointer() |
|
299 |
if c and |
|
300 |
c == sloppy_last_client |
|
301 |
then |
|
302 |
capi.client.focus = c |
|
9
56e80afa3e1c
Only start sloppy timer on a mouse_enter.
Fabien Ninoles <fabien@tzone.org>
parents:
8
diff
changeset
|
303 |
sloppy_timer:stop() |
8 | 304 |
end |
305 |
end |
|
306 |
end |
|
307 |
||
308 |
sloppy = true |
|
309 |
sloppy_last_client = nil |
|
310 |
sloppy_timer = nil |
|
311 |
||
312 |
function sloppy_install(delay) |
|
313 |
sloppy_timer = timer({ timeout = delay }) |
|
314 |
sloppy_timer:add_signal("timeout", sloppy_on_timeout) |
|
315 |
sloppy_timer:start() |
|
316 |
capi.client.add_signal("manage", function (c) |
|
317 |
c:add_signal("mouse::enter", sloppy_mouse_enter) |
|
318 |
end) |
|
9
56e80afa3e1c
Only start sloppy timer on a mouse_enter.
Fabien Ninoles <fabien@tzone.org>
parents:
8
diff
changeset
|
319 |
capi.client.add_signal("focus", function () sloppy_timer:stop() end) |
8 | 320 |
end |