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