revamp theme a lot
[~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 -- {{{ Taglist
200
201 maintaglist = {}
202 maintaglist.buttons = awful.util.table.join(
203     awful.button(k_n, 1, awful.tag.viewonly),
204     awful.button(k_s, 1, awful.client.toggletag)
205 )
206 maintaglist.label = awful.widget.taglist.label.noempty
207
208 -- }}}
209 -- {{{ Prompt box
210
211 mypromptbox = awful.widget.prompt{
212     layout = awful.widget.layout.horizontal.leftright
213 }
214
215 -- }}}
216 -- {{{ CPU Widgets
217
218 cpuicon = make_icon('cpu.png', 16, 4, 16)
219
220 cputextwidget = widget({ type = 'textbox' })
221
222 vicious.register(cputextwidget, vicious.widgets.cpu,
223 function (widget, args)
224     local r = tonumber(args[1])
225     local percent = args[1]..'%'
226     if r < 10 then
227         percent = '0'..percent
228     end
229     if r < 25 then
230         percent = fg(theme.fg_focus, percent)
231     elseif r < 50 then
232         percent = fg('yellow', percent)
233     elseif r < 75 then
234         percent = fg('orange', percent)
235     else
236         percent = fg('#ff4040', percent)
237     end
238     return percent..' '
239 end, 2)
240
241 -- }}}
242 -- {{{ Memory Usage Widget
243
244 memicon = make_icon('mem.png', 16, 4, 16)
245
246 memtextwidget = widget({ type = 'textbox' })
247
248 vicious.register(memtextwidget, vicious.widgets.mem, function (widget, args)
249     -- Add extra preceding zeroes when needed
250     local r = tonumber(args[1])
251     local percent = args[1]..'%'
252     if r < 10 then
253         percent = '0'..percent
254     end
255     if r < 50 then
256         percent = fg(theme.fg_focus, percent)
257     elseif r < 80 then
258         percent = fg('orange', percent)
259     else
260         percent = fg('red', percent)
261     end
262     return percent..' '..args[2]..'M'
263 end, 2)
264
265 -- }}}
266 -- {{{ Clock
267
268 clockicon = make_icon('clock.png', 16, 4, 16)
269
270 clockwidget = widget({ type = "textbox" })
271 vicious.register(clockwidget, vicious.widgets.date,
272                  fg(theme.fg_focus, "%H:%M") .. " %a %d %b  ", 10)
273
274 -- }}}
275 -- {{{ Statusbar
276
277 mainstatusbar = {}
278 for s = 1, screen.count() do
279     local lr_layout = awful.widget.layout.horizontal.leftright
280     local rl_layout = awful.widget.layout.horizontal.rightleft
281
282     mainstatusbar[s] = awful.wibox{ position = "top", height = 16, screen = s }
283
284     mainstatusbar[s].widgets = {
285         {
286             awful.widget.taglist(s, maintaglist.label, maintaglist.buttons),
287             mypromptbox.widget,
288             layout = lr_layout
289         },
290         s == 1 and widget{ type = 'systray' } or nil,
291         clockwidget, clockicon,
292         memtextwidget, memicon,
293         cputextwidget, cpuicon,
294         layout = awful.widget.layout.horizontal.rightleft
295     }
296 end
297
298 -- }}}
299 -- }}}
300 -- {{{ Keys
301 ---- {{{ Global keys
302
303 globalkeys = awful.util.table.join(
304     -- Mod+{A/S}: Switch to prev/next tag
305     awful.key(k_m, "Left",  mtag.prev),
306     awful.key(k_m, "Right", mtag.next),
307
308     -- Mod+Shift+{A/S}: Move window to Prev/Next tag
309     awful.key(k_ms, "Left",  mtag.movetoprev),
310     awful.key(k_ms, "Right", mtag.movetonext),
311
312     -- Mod+Shift_{E/D}: move window to next/prev screen
313     awful.key(k_mc, "Right", function()
314        local s = mouse.screen + 1
315        while s > screen.count() do s = s - screen.count() end
316        client.focus.screen = s
317     end),
318     awful.key(k_mc, "Left", function()
319        local s = mouse.screen - 1
320        while s < 1 do s = s + screen.count() end
321        client.focus.screen = s
322     end),
323
324
325     -- Focus Prev/Next window
326     awful.key(k_m, "j",
327         function ()
328             awful.client.focus.byidx(1)
329             if client.focus then client.focus:raise() end
330         end),
331     awful.key(k_m, "k",
332         function ()
333             awful.client.focus.byidx(-1)
334             if client.focus then client.focus:raise() end
335         end),
336
337     -- Swap window with the Prev/Next one
338     awful.key(k_ms, "j", function () awful.client.swap.byidx(1) end),
339     awful.key(k_ms, "k", function () awful.client.swap.byidx(-1) end),
340
341     -- Mod+{E/D}: Switch to next/previous screen
342     awful.key(k_m, "Tab",  function () awful.screen.focus_relative(1) end),
343     awful.key(k_ms, "Tab", function () awful.screen.focus_relative(-1) end),
344
345     -- Mod+Enter: Launch a new terminal
346     awful.key(k_m,  "e",      function() awful.util.spawn("firefox") end),
347     awful.key(k_m,  "Return", function() awful.util.spawn(terminal) end),
348     awful.key(k_ac, "r", awesome.restart),
349     awful.key(k_m, "F12", function() awful.util.spawn(lock) end),
350     awful.key({}, "#148", function() awful.util.spawn("kcalc") end),
351
352     -- Layout manipulation
353     awful.key(k_m,  "l", function () awful.tag.incmwfact(0.05) end),
354     awful.key(k_m,  "h", function () awful.tag.incmwfact(-0.05) end),
355     awful.key(k_ms, "h", function () awful.tag.incnmaster(1) end),
356     awful.key(k_ms, "l", function () awful.tag.incnmaster(-1) end),
357     awful.key(k_mc, "h", function () awful.tag.incncol(1) end),
358     awful.key(k_mc, "l", function () awful.tag.incncol(-1) end),
359
360     -- Menu
361     awful.key(k_m, "r", function () mypromptbox:run() end),
362     awful.key(k_m, "F4",
363         function ()
364             awful.prompt.run({ prompt = "Run Lua code: " },
365                              mypromptbox.widget,
366                              awful.util.eval, nil,
367                              awful.util.getdir("cache").."/lua_commands")
368     end),
369
370     awful.key({}, "#192", function() mtag.viewonly(1) end),
371     awful.key({}, "#193", function() mtag.viewonly(2) end),
372     awful.key({}, "#194", function() mtag.viewonly(3) end),
373     awful.key({}, "#195", function() mtag.viewonly(4) end),
374     awful.key({}, "#196", function() mtag.viewonly(5) end)
375 )
376
377 -- Mod+#: Switch to tag
378 -- Mod+Shift+#: Toggle tag display
379 -- Mod+Control+#: Move client to tag
380 -- Mod+Alt+#: Toggle client on tag
381
382 for i = 1, 10 do
383     globalkeys = awful.util.table.join(
384         globalkeys,
385         awful.key(k_m,  i % 10, function() mtag.viewonly(i)   end),
386         awful.key(k_ms, i % 10, function() mtag.viewtoggle(i) end),
387         awful.key(k_mc, i % 10,
388                   function ()
389                       if client.focus and tags[client.focus.screen][i] then
390                           awful.client.movetotag(tags[client.focus.screen][i])
391                       end
392                   end),
393         awful.key(k_mcs, i % 10,
394                   function ()
395                       if client.focus and tags[client.focus.screen][i] then
396                           awful.client.toggletag(tags[client.focus.screen][i])
397                       end
398                   end)
399     )
400 end
401
402 ---- }}}
403 ---- {{{ Client hotkeys / buttons
404
405 local clientkeys = awful.util.table.join(
406     awful.key(k_m, "i", function (c)
407         if mypromptbox.widget.text then
408             mypromptbox.widget.text = ""
409         else
410             mypromptbox.widget.text = "Class: " .. c.class .. " Instance: ".. c.instance
411         end
412     end),
413
414     -- Client manipulation
415     awful.key(k_m, "c", function (c) c:kill() end),
416     awful.key(k_m, "o", awful.client.floating.toggle),
417     awful.key(k_m, "F11", function (c) c.fullscreen = not c.fullscreen end)
418 )
419
420 local clientbuttons = awful.util.table.join(
421     awful.button({ }, 1, function (c) client.focus = c; c:raise() end),
422     awful.button(k_a, 1, awful.mouse.client.move),
423     awful.button(k_a, 3, awful.mouse.client.resize)
424 )
425
426 ---- }}}
427
428 root.keys(globalkeys)
429 -- }}}
430 -- {{{ Rules
431
432 awful.rules.rules = {
433     -- All clients will match this rule.
434     {
435         rule = { },
436         properties = {
437             border_width = beautiful.border_width,
438             border_color = beautiful.border_normal,
439             focus   = true,
440             keys    = clientkeys,
441             buttons = clientbuttons,
442             size_hints_honor = false,
443         }
444     },
445     { rule = { class = "MPlayer" },  properties = { floating = true } },
446     { rule = { class = "pinentry" }, properties = { floating = true } },
447     -- Set Firefox to always map on tags number 2 of screen 1.
448     -- { rule = { class = "Firefox" },
449     --   properties = { tag = tags[1][2] } },
450 }
451
452 -- }}}
453 -- {{{ Signals
454
455 function warp_mouse(c)
456     local o = awful.mouse.client_under_pointer()
457     if not o or o ~= c then
458         local g = c:geometry()
459         mouse.coords({ x = g.x + 5, y = g.y + 5 }, true)
460     end
461 end
462
463 client.add_signal("focus", function (c)
464     if not awful.client.ismarked(c) then
465         c.border_color = beautiful.border_focus
466     end
467     warp_mouse(c)
468 end)
469
470 client.add_signal("unfocus", function (c)
471     if not awful.client.ismarked(c) then
472         c.border_color = beautiful.border_normal
473     end
474 end)
475
476 client.add_signal("manage", function (c, startup)
477     c:add_signal("mouse::enter", function(c) client.focus = c end)
478 end)
479
480 for s = 1, screen.count() do
481     screen[s]:add_signal("arrange", function ()
482         if client.focus and client.focus.screen == s then
483             warp_mouse(client.focus)
484         end
485     end)
486 end
487
488 -- }}}
489
490 awful.util.spawn("xkbcomp -w 0 -R/usr/share/X11/xkb /home/madcoder/.Xkeyboard :0")
491 awful.util.spawn("xsetroot -cursor_name left_ptr")