8df6ac9652935fc69220eab758ec8599eb55923d
[~madcoder/dotfiles.git] / config / awesome / rc.lua
1 -- awesome 3 configuration file
2
3 -- Include awesome library, with lots of useful function!
4 require("awful")
5 require("awful.autofocus")
6 require("awful.rules")
7 require("vicious")
8 require("beautiful")
9
10 terminal = "x-terminal-emulator"
11 lock     = 'xscreensaver-command -lock'
12 beautiful.init(awful.util.getdir("config").."/theme")
13
14 -- {{{ Modkeys
15 modkey = "Mod4"
16 shift = "Shift"
17 alt = "Mod1"
18 control = "Control"
19
20 k_n   = {}
21 k_m   = {modkey}
22 k_ms  = {modkey, shift}
23 k_ma  = {modkey, alt}
24 k_mc  = {modkey, control}
25 k_mcs = {modkey, control, shift}
26 k_a   = {alt}
27 k_ac  = {alt, control}
28 k_c   = {control}
29 k_cs  = {control, shift}
30 k_s   = {shift}
31
32 -- }}}
33 -- {{{ Markup helper functions
34 -- Inline markup is a tad ugly, so use these functions
35 -- to dynamically create markup.
36 function fg(color, text)
37     return '<span color="'..color..'">'..text..'</span>'
38 end
39
40 -- }}}
41
42 -- {{{ Tags
43
44 tags = {}
45 for s = 1, screen.count() do
46     -- Each screen has its own tag table.
47     local p = {}
48     for i = 1, 10 do
49         table.insert(p, "work-"..i)
50     end
51     tags[s] = awful.tag(p, s, awful.layout.suit.tile)
52 end
53
54 -- Get the screen number we're on
55 local mtag = { }
56 function mtag.getn(idx, s)
57     return tags[s or mouse.screen][idx]
58 end
59 function mtag.viewonly (idx, s)
60     local t = mtag.getn(idx, s)
61     if t then awful.tag.viewonly(t) end
62 end
63 function mtag.viewtoggle (idx, s)
64     local t = mtag.getn(idx, s)
65     if t then awful.tag.viewtoggle(t) end
66 end
67 function mtag.isoccupied(s, t)
68     local clients = (t and t:clients()) or {}
69     return #clients > 0
70 end
71 function mtag.occupied(s)
72     local p = {}
73
74     if not s then s = mouse.screen end
75     for t in pairs(tags[s]) do
76         t = tags[s][t]
77         if mtag.isoccupied(s, t) then table.insert(p, t) end
78     end
79     return p
80 end
81 function mtag.getnext(s)
82     if s == nil then s = mouse.screen end
83
84     local p = mtag.occupied(s)
85     local curtag = awful.tag.selected()
86
87     local t = 0
88
89     -- Get tag #
90     if mtag.isoccupied(s, curtag) then
91         for x in pairs(p) do
92             if curtag == p[x] then t = x end
93         end
94     end
95
96     local lasto = 1
97     local o = 0
98
99     for x in pairs(tags[s]) do
100         if curtag == tags[s][x] then o = x end
101         if mtag.isoccupied(s, tags[s][x]) then lasto = x end
102     end
103
104     -- Now: t is # in non-empty, o is # in all
105     if o == table.maxn(tags[s]) then
106         -- We're the last tag, create a new one
107         if t == 0 then
108             -- We're empty, go to first
109             return tags[s][1]
110         else
111             -- We're occupied, create new
112             return newtag()
113         end
114     else
115         if t == 0 then
116             -- We're empty, check if we're last
117             if o > lasto then
118                 -- We're also later than the last non-empty
119                 -- Wrap to first
120                 return tags[s][1]
121             else
122                 -- Nevermind, get the next
123                 return tags[s][o+1]
124             end
125         else
126             -- Return next tag
127             return tags[s][o+1]
128         end
129     end
130 end
131 function mtag.next(s)
132     awful.tag.viewonly(mtag.getnext(s))
133 end
134 function mtag.movetonext(s)
135     local t = mtag.getnext(s)
136     awful.client.movetotag(t)
137     awful.tag.viewonly(t)
138 end
139 function mtag.getprev(s)
140     if s == nil then s = mouse.screen end
141
142     local p = mtag.occupied(s)
143     local curtag = awful.tag.selected()
144
145     local t = 0
146
147     -- Get tag #
148     if mtag.isoccupied(s, curtag) then
149         for x in pairs(p) do
150             if curtag == p[x] then t = x end
151         end
152     end
153
154     local lasto = 1
155     local o = 0
156
157     for x in pairs(tags[s]) do
158         if curtag == tags[s][x] then o = x end
159         if mtag.isoccupied(s, tags[s][x]) then lasto = x end
160     end
161
162     -- Now: t is # in non-empty, o is # in all
163     if o == 1 then
164         -- We're the very first tag, wrap around
165         return p[ table.maxn(p) ]
166     else
167         -- We're not first, just go prev
168         return tags[s][o-1]
169     end
170 end
171 function mtag.prev(s)
172     awful.tag.viewonly(mtag.getprev(s))
173 end
174 function mtag.movetoprev(s)
175     local t = mtag.getprev(s)
176     awful.client.movetotag(t)
177     awful.tag.viewonly(t)
178 end
179
180
181 -- }}}
182 -- {{{ Widgets
183
184 local icondir = awful.util.getdir("config").."/icons/"
185
186 function make_icon(fname, left, right, h, bg)
187     local icon = image(icondir..fname)
188     local ib   = widget { type = 'imagebox' }
189     local w    = left + icon.width + right
190     local i    = image.argb32(w, h, nil)
191
192     i:draw_rectangle(0, 0, w, h, true, beautiful.bg_normal)
193     i:insert(icon, left, math.floor((h - icon.height) / 2))
194
195     ib.image = i
196     return ib
197 end
198
199 local mywidgets = {}
200
201 -- {{{ Taglist
202
203 maintaglist = {}
204 maintaglist.buttons = awful.util.table.join(
205     awful.button(k_n, 1, awful.tag.viewonly),
206     awful.button(k_s, 1, awful.client.toggletag)
207 )
208 maintaglist.label = awful.widget.taglist.label.noempty
209
210 -- }}}
211 -- {{{ Prompt box
212
213 mypromptbox = awful.widget.prompt{
214     layout = awful.widget.layout.horizontal.leftright
215 }
216
217 -- }}}
218 -- {{{ CPU Widgets
219
220 table.insert(mywidgets, make_icon('cpu.png', 16, 4, 16))
221
222 cputextwidget = widget({ type = 'textbox' })
223
224 vicious.register(cputextwidget, vicious.widgets.cpu,
225 function (widget, args)
226     local r = tonumber(args[1])
227     local percent = args[1]..'%'
228     if r < 10 then
229         percent = '0'..percent
230     end
231     if r < 25 then
232         percent = fg(theme.fg_focus, percent)
233     elseif r < 50 then
234         percent = fg('yellow', percent)
235     elseif r < 75 then
236         percent = fg('orange', percent)
237     else
238         percent = fg('#ff4040', percent)
239     end
240     return percent..' '
241 end, 2)
242
243 table.insert(mywidgets, cputextwidget)
244
245 -- }}}
246 -- {{{ Memory Usage Widget
247
248 table.insert(mywidgets, make_icon('mem.png', 16, 4, 16))
249
250 memtextwidget = widget({ type = 'textbox' })
251
252 vicious.register(memtextwidget, vicious.widgets.mem, function (widget, args)
253     -- Add extra preceding zeroes when needed
254     local r = tonumber(args[1])
255     local percent = args[1]..'%'
256     if r < 10 then
257         percent = '0'..percent
258     end
259     if r < 50 then
260         percent = fg(theme.fg_focus, percent)
261     elseif r < 80 then
262         percent = fg('orange', percent)
263     else
264         percent = fg('red', percent)
265     end
266     return percent..' '..args[2]..'M'
267 end, 2)
268
269 table.insert(mywidgets, memtextwidget)
270
271 -- }}}
272 -- {{{ Clock
273
274 table.insert(mywidgets, make_icon('clock.png', 16, 4, 16))
275
276 clockwidget = widget({ type = "textbox" })
277 vicious.register(clockwidget, vicious.widgets.date,
278                  fg(theme.fg_focus, "%H:%M") .. " %a %d %b  ", 10)
279
280 table.insert(mywidgets, clockwidget)
281
282 -- }}}
283 -- {{{ Statusbar
284
285 for s = 1, screen.count() do
286     local tab = {
287         {
288             awful.widget.taglist(s, maintaglist.label, maintaglist.buttons),
289             mypromptbox.widget,
290             layout = awful.widget.layout.horizontal.leftright
291         },
292     }
293
294     if (s == 1) then
295         table.insert(tab, widget{ type = 'systray' })
296     end
297     for i = #mywidgets, 1, -1 do
298         table.insert(tab, mywidgets[i])
299     end
300     tab["layout"] = awful.widget.layout.horizontal.rightleft
301     awful.wibox{ position = "top", height = 16, screen = s }.widgets = tab
302 end
303
304 -- }}}
305 -- }}}
306 -- {{{ Keys
307 ---- {{{ Global keys
308
309 globalkeys = awful.util.table.join(
310     -- Mod+{A/S}: Switch to prev/next tag
311     awful.key(k_m, "Left",  mtag.prev),
312     awful.key(k_m, "Right", mtag.next),
313
314     -- Mod+Shift+{A/S}: Move window to Prev/Next tag
315     awful.key(k_ms, "Left",  mtag.movetoprev),
316     awful.key(k_ms, "Right", mtag.movetonext),
317
318     -- Mod+Shift_{E/D}: move window to next/prev screen
319     awful.key(k_mc, "Right", function()
320        client.focus.screen = awful.util.cycle(screen.count(), mouse.screen + 1)
321     end),
322     awful.key(k_mc, "Left", function()
323        client.focus.screen = awful.util.cycle(screen.count(), mouse.screen - 1)
324     end),
325
326
327     -- Focus Prev/Next window
328     awful.key(k_m, "j",
329         function ()
330             awful.client.focus.byidx(1)
331             if client.focus then client.focus:raise() end
332         end),
333     awful.key(k_m, "k",
334         function ()
335             awful.client.focus.byidx(-1)
336             if client.focus then client.focus:raise() end
337         end),
338
339     -- Swap window with the Prev/Next one
340     awful.key(k_ms, "j", function () awful.client.swap.byidx(1) end),
341     awful.key(k_ms, "k", function () awful.client.swap.byidx(-1) end),
342
343     -- Mod+{E/D}: Switch to next/previous screen
344     awful.key(k_m, "Tab",  function () awful.screen.focus_relative(1) end),
345     awful.key(k_ms, "Tab", function () awful.screen.focus_relative(-1) end),
346
347     -- Mod+Enter: Launch a new terminal
348     awful.key(k_m,  "e",      function() awful.util.spawn("firefox") end),
349     awful.key(k_m,  "Return", function() awful.util.spawn(terminal) end),
350     awful.key(k_ac, "r", awesome.restart),
351     awful.key(k_m, "F12", function() awful.util.spawn(lock) end),
352     awful.key({}, "#148", function() awful.util.spawn("kcalc") end),
353
354     -- Layout manipulation
355     awful.key(k_m,  "l", function () awful.tag.incmwfact(0.05) end),
356     awful.key(k_m,  "h", function () awful.tag.incmwfact(-0.05) end),
357     awful.key(k_ms, "h", function () awful.tag.incnmaster(1) end),
358     awful.key(k_ms, "l", function () awful.tag.incnmaster(-1) end),
359     awful.key(k_mc, "h", function () awful.tag.incncol(1) end),
360     awful.key(k_mc, "l", function () awful.tag.incncol(-1) end),
361
362     -- Menu
363     awful.key(k_m, "r", function () mypromptbox:run() end),
364     awful.key(k_m, "F4",
365         function ()
366             awful.prompt.run({ prompt = "Run Lua code: " },
367                              mypromptbox.widget,
368                              awful.util.eval, nil,
369                              awful.util.getdir("cache").."/lua_commands")
370     end),
371
372     awful.key({}, "#192", function() mtag.viewonly(1) end),
373     awful.key({}, "#193", function() mtag.viewonly(2) end),
374     awful.key({}, "#194", function() mtag.viewonly(3) end),
375     awful.key({}, "#195", function() mtag.viewonly(4) end),
376     awful.key({}, "#196", function() mtag.viewonly(5) end)
377 )
378
379 -- Mod+#: Switch to tag
380 -- Mod+Shift+#: Toggle tag display
381 -- Mod+Control+#: Move client to tag
382 -- Mod+Alt+#: Toggle client on tag
383
384 for i = 1, 10 do
385     globalkeys = awful.util.table.join(
386         globalkeys,
387         awful.key(k_m,  i % 10, function() mtag.viewonly(i)   end),
388         awful.key(k_ms, i % 10, function() mtag.viewtoggle(i) end),
389         awful.key(k_mc, i % 10,
390                   function ()
391                       if client.focus and tags[client.focus.screen][i] then
392                           awful.client.movetotag(tags[client.focus.screen][i])
393                       end
394                   end),
395         awful.key(k_mcs, i % 10,
396                   function ()
397                       if client.focus and tags[client.focus.screen][i] then
398                           awful.client.toggletag(tags[client.focus.screen][i])
399                       end
400                   end)
401     )
402 end
403
404 ---- }}}
405 ---- {{{ Client hotkeys / buttons
406
407 local clientkeys = awful.util.table.join(
408     awful.key(k_m, "i", function (c)
409         if mypromptbox.widget.text then
410             mypromptbox.widget.text = ""
411         else
412             mypromptbox.widget.text = "Class: " .. c.class .. " Instance: ".. c.instance
413         end
414     end),
415
416     -- Client manipulation
417     awful.key(k_m, "c", function (c) c:kill() end),
418     awful.key(k_m, "o", awful.client.floating.toggle),
419     awful.key(k_m, "F11", function (c) c.fullscreen = not c.fullscreen end)
420 )
421
422 local clientbuttons = awful.util.table.join(
423     awful.button({ }, 1, function (c) client.focus = c; c:raise() end),
424     awful.button(k_a, 1, awful.mouse.client.move),
425     awful.button(k_a, 3, awful.mouse.client.resize)
426 )
427
428 ---- }}}
429
430 root.keys(globalkeys)
431 -- }}}
432 -- {{{ Rules
433
434 awful.rules.rules = {
435     -- All clients will match this rule.
436     {
437         rule = { },
438         properties = {
439             border_width = beautiful.border_width,
440             border_color = beautiful.border_normal,
441             focus   = true,
442             keys    = clientkeys,
443             buttons = clientbuttons,
444             size_hints_honor = false,
445         }
446     },
447     { rule = { class = "MPlayer" },  properties = { floating = true } },
448     { rule = { class = "pinentry" }, properties = { floating = true } },
449     -- Set Firefox to always map on tags number 2 of screen 1.
450     -- { rule = { class = "Firefox" },
451     --   properties = { tag = tags[1][2] } },
452 }
453
454 -- }}}
455 -- {{{ Signals
456
457 function warp_mouse(c)
458     local o = awful.mouse.client_under_pointer()
459     if not o or o ~= c then
460         local g = c:geometry()
461         mouse.coords({ x = g.x + 5, y = g.y + 5 }, true)
462     end
463 end
464
465 client.add_signal("focus", function (c)
466     if not awful.client.ismarked(c) then
467         c.border_color = beautiful.border_focus
468     end
469     warp_mouse(c)
470 end)
471
472 client.add_signal("unfocus", function (c)
473     if not awful.client.ismarked(c) then
474         c.border_color = beautiful.border_normal
475     end
476 end)
477
478 client.add_signal("manage", function (c, startup)
479     c:add_signal("mouse::enter", function(c) client.focus = c end)
480 end)
481
482 for s = 1, screen.count() do
483     screen[s]:add_signal("arrange", function ()
484         if client.focus and client.focus.screen == s then
485             warp_mouse(client.focus)
486         end
487     end)
488 end
489
490 -- }}}
491
492 awful.util.spawn("xkbcomp -w 0 -R/usr/share/X11/xkb /home/madcoder/.Xkeyboard :0")
493 awful.util.spawn("xsetroot -cursor_name left_ptr")