]> git.donarmstrong.com Git - x_base.git/blob - .config/awesome/rc.lua
a3b3b94bf54219d83010183a7ec8d51c61625f79
[x_base.git] / .config / awesome / rc.lua
1 -- Standard awesome library
2 require("awful")
3 require("awful.autofocus")
4 require("awful.rules")
5 -- Theme handling library
6 require("beautiful")
7 -- Notification library
8 require("naughty")
9
10 require("obvious.volume_alsa")
11 require("obvious.battery")
12
13 -- Load Debian menu entries
14 require("debian.menu")
15
16 -- from https://github.com/tony/awesome-config/blob/master/helpers.lua
17 function clean(string)
18    s = string.gsub(string, '^%s+', '')
19    s = string.gsub(s, '%s+$', '')
20    s = string.gsub(s, '[\n\r]+', ' ')
21    return s
22 end
23
24 function file_exists(file)
25    local cmd = "/bin/dash -c 'if [ -e " .. file .. " ]; then echo true; fi;'"
26    local fh = io.popen(cmd)
27    
28    s = clean(fh:read('*a'))
29    
30    if s == 'true' then return true else return nil end
31 end
32
33 -- {{{ Variable definitions
34 -- Themes define colours, icons, and wallpapers
35 beautiful.init("/usr/share/awesome/themes/default/theme.lua")
36
37 -- This is used later as the default terminal and editor to run.
38 terminal = "x-terminal-emulator"
39 editor = os.getenv("EDITOR") or "editor"
40 editor_cmd = terminal .. " -e " .. editor
41 home = os.getenv("HOME")
42
43 hostname = awful.util.pread("hostname -f"):gsub("\n", "")
44
45 wallpaper_dir = home .. "/media/photos/gallery.donarmstrong.com/"
46 if (not file_exists(wallpaper_dir)) then
47    wallpaper_dir = "/usr/share/awesome/themes/default/background.png"
48 end
49
50 wallpaper_cmd = "find " .. wallpaper_dir ..
51    " -xtype f \\( -iname '*.jpg' -o -iname '*.png' \\) " ..
52    " -not -name '.*' -print0 | shuf -n" .. screen.count() .. " -z | xargs -0 feh --bg-max"
53
54 -- set up the wallpaper timer
55 wallpaper_timer = timer { timeout = 0 }
56 wallpaper_timer:add_signal("timeout",
57   function()
58      -- tell awsetbg to randomly choose a wallpaper from your wallpaper directory
59      if file_exists(wallpaper_dir) and whereis_app('feh') then
60         os.execute(wallpaper_cmd)
61      end
62      -- stop the timer (we don't need multiple instances running at the same time)
63      mytimer:stop()
64      
65      -- define the interval in which the next wallpaper change should occur in seconds
66      -- (in this case anytime between 10 and 20 minutes)
67      --restart the timer
68      mytimer.timeout = math.random(600,1200)
69                       mytimer:start()
70   end
71 )
72
73 -- Default modkey.
74 -- Usually, Mod4 is the key with a logo between Control and Alt.
75 -- If you do not like this or do not have such a key,
76 -- I suggest you to remap Mod4 to another key using xmodmap or other tools.
77 -- However, you can use another modifier like Mod1, but it may interact with others.
78 modkey = "Mod4"
79
80 -- Table of layouts to cover with awful.layout.inc, order matters.
81 layouts =
82 {
83     awful.layout.suit.tile,
84     awful.layout.suit.tile.left,
85     awful.layout.suit.tile.bottom,
86     awful.layout.suit.tile.top,
87     awful.layout.suit.fair,
88     awful.layout.suit.fair.horizontal,
89     awful.layout.suit.spiral,
90     awful.layout.suit.spiral.dwindle,
91     awful.layout.suit.max,
92     awful.layout.suit.max.fullscreen,
93     awful.layout.suit.magnifier,
94     awful.layout.suit.floating
95 }
96 -- }}}
97
98 -- {{{ Tags
99 -- Define a tag table which hold all screen tags.
100 tags = {names={"mail","emacs","www","remotes",5,6,7,8,9}
101        }
102 for s = 1, screen.count() do
103     -- Each screen has its own tag table.
104    defaultlayout=layouts[9]
105    tags[s] = awful.tag(tags.names, s, defaultlayout)
106 end
107 -- }}}
108
109 -- {{{ Menu
110 -- Create a laucher widget and a main menu
111 myawesomemenu = {
112    { "manual", terminal .. " -e man awesome" },
113    { "edit config", editor_cmd .. " " .. awful.util.getdir("config") .. "/rc.lua" },
114    { "restart", awesome.restart },
115    { "quit", awesome.quit }
116 }
117
118 mymainmenu = awful.menu({ items = { { "awesome", myawesomemenu, beautiful.awesome_icon },
119                                     { "Debian", debian.menu.Debian_menu.Debian },
120                                     { "open terminal", terminal }
121                                   }
122                         })
123
124 mylauncher = awful.widget.launcher({ image = image(beautiful.awesome_icon),
125                                      menu = mymainmenu })
126 -- }}}
127
128 -- {{{ Wibox
129 -- Create a textclock widget
130 mytextclock = awful.widget.textclock({ align = "right" }, " %c ", 1)
131
132 myvolumealsa = obvious.volume_alsa()
133 mybattery = obvious.battery()
134
135 -- Create a systray
136 mysystray = widget({ type = "systray" })
137
138 -- Create a wibox for each screen and add it
139 mywibox = {}
140 mypromptbox = {}
141 mylayoutbox = {}
142 mytaglist = {}
143 mytaglist.buttons = awful.util.table.join(
144                     awful.button({ }, 1, awful.tag.viewonly),
145                     awful.button({ modkey }, 1, awful.client.movetotag),
146                     awful.button({ }, 3, awful.tag.viewtoggle),
147                     awful.button({ modkey }, 3, awful.client.toggletag),
148                     awful.button({ }, 4, awful.tag.viewnext),
149                     awful.button({ }, 5, awful.tag.viewprev)
150                     )
151 mytasklist = {}
152 mytasklist.buttons = awful.util.table.join(
153                      awful.button({ }, 1, function (c)
154                                               if not c:isvisible() then
155                                                   awful.tag.viewonly(c:tags()[1])
156                                               end
157                                               client.focus = c
158                                               c:raise()
159                                           end),
160                      awful.button({ }, 3, function ()
161                                               if instance then
162                                                   instance:hide()
163                                                   instance = nil
164                                               else
165                                                   instance = awful.menu.clients({ width=250 })
166                                               end
167                                           end),
168                      awful.button({ }, 4, function ()
169                                               awful.client.focus.byidx(1)
170                                               if client.focus then client.focus:raise() end
171                                           end),
172                      awful.button({ }, 5, function ()
173                                               awful.client.focus.byidx(-1)
174                                               if client.focus then client.focus:raise() end
175                                           end))
176
177 for s = 1, screen.count() do
178     -- Create a promptbox for each screen
179     mypromptbox[s] = awful.widget.prompt({ layout = awful.widget.layout.horizontal.leftright })
180     -- Create an imagebox widget which will contains an icon indicating which layout we're using.
181     -- We need one layoutbox per screen.
182     mylayoutbox[s] = awful.widget.layoutbox(s)
183     mylayoutbox[s]:buttons(awful.util.table.join(
184                            awful.button({ }, 1, function () awful.layout.inc(layouts, 1) end),
185                            awful.button({ }, 3, function () awful.layout.inc(layouts, -1) end),
186                            awful.button({ }, 4, function () awful.layout.inc(layouts, 1) end),
187                            awful.button({ }, 5, function () awful.layout.inc(layouts, -1) end)))
188     -- Create a taglist widget
189     mytaglist[s] = awful.widget.taglist(s, awful.widget.taglist.label.all, mytaglist.buttons)
190
191     -- Create a tasklist widget
192     mytasklist[s] = awful.widget.tasklist(function(c)
193                                               return awful.widget.tasklist.label.currenttags(c, s)
194                                           end, mytasklist.buttons)
195
196     -- Create the wibox
197     mywibox[s] = awful.wibox({ position = "top", screen = s })
198     -- Add widgets to the wibox - order matters
199     mywibox[s].widgets = {
200         {
201             mylauncher,
202             mytaglist[s],
203             mypromptbox[s],
204             layout = awful.widget.layout.horizontal.leftright
205         },
206         mylayoutbox[s],
207         mytextclock,
208         myvolumealsa,
209         mybattery,
210         s == 1 and mysystray or nil,
211         mytasklist[s],
212         layout = awful.widget.layout.horizontal.rightleft
213     }
214 end
215 -- }}}
216
217 -- {{{ Mouse bindings
218 root.buttons(awful.util.table.join(
219     awful.button({ }, 3, function () mymainmenu:toggle() end),
220     awful.button({ }, 4, awful.tag.viewnext),
221     awful.button({ }, 5, awful.tag.viewprev)
222 ))
223 -- }}}
224
225 -- {{{ Key bindings
226 globalkeys = awful.util.table.join(
227     -- awful.key({ modkey,           }, "Left",   awful.tag.viewprev       ),
228     -- awful.key({ modkey,           }, "Right",  awful.tag.viewnext       ),
229    awful.key({},"XF86AudioRaiseVolume",function () obvious.volume_alsa.raise(0, "Master", 2) end),
230    awful.key({},"XF86AudioLowerVolume",function () obvious.volume_alsa.lower(0, "Master", 2) end),
231     awful.key({ modkey,           }, "Escape", awful.tag.history.restore),
232
233     awful.key({ modkey,           }, "j",
234         function ()
235             awful.client.focus.byidx( 1)
236             if client.focus then client.focus:raise() end
237             warp_mouse()
238         end),
239     awful.key({ modkey,           }, "k",
240         function ()
241             awful.client.focus.byidx(-1)
242             if client.focus then client.focus:raise() end
243             warp_mouse()
244         end),
245     awful.key({ modkey,           }, "w", function () mymainmenu:show({keygrabber=true}) end),
246
247     -- Layout manipulation
248     awful.key({ modkey, "Shift"   }, "j", function () awful.client.swap.byidx(  1)    end),
249     awful.key({ modkey, "Shift"   }, "k", function () awful.client.swap.byidx( -1)    end),
250     awful.key({ modkey,           }, "Left", function () awful.screen.focus_relative( 1) end),
251     awful.key({ modkey,           }, "Right", function () awful.screen.focus_relative(-1) end),
252     awful.key({ modkey,           }, "u", awful.client.urgent.jumpto),
253     awful.key({ modkey,           }, "Tab",
254         function ()
255             awful.client.focus.history.previous()
256             if client.focus then
257                 client.focus:raise()
258             end
259         end),
260
261     -- Standard program
262     awful.key({ modkey,           }, "Return", function () awful.util.spawn(terminal) end),
263     awful.key({ modkey, "Control", "Shift" }, "r", awesome.restart),
264     awful.key({ modkey, "Control", "Shift"   }, "q", awesome.quit),
265
266     awful.key({ modkey,           }, "l",     function () awful.tag.incmwfact( 0.05)    end),
267     awful.key({ modkey,           }, "h",     function () awful.tag.incmwfact(-0.05)    end),
268     awful.key({ modkey, "Shift"   }, "h",     function () awful.tag.incnmaster( 1)      end),
269     awful.key({ modkey, "Shift"   }, "l",     function () awful.tag.incnmaster(-1)      end),
270     awful.key({ modkey, "Control" }, "h",     function () awful.tag.incncol( 1)         end),
271     awful.key({ modkey, "Control" }, "l",     function () awful.tag.incncol(-1)         end),
272     awful.key({ modkey,           }, "n", function () awful.layout.inc(layouts,  1) end),
273     awful.key({ modkey, "Shift"   }, "n", function () awful.layout.inc(layouts, -1) end),
274
275     -- resize floats
276     awful.key({ modkey, "Control" }, "Down",  function () awful.client.moveresize(0, 20, 0, 0) end),
277     awful.key({ modkey, "Control" }, "Up",    function () awful.client.moveresize(0, -20, 0, 0) end),
278     awful.key({ modkey, "Control" }, "Left",  function () awful.client.moveresize(-20, 0, 0, 0) end),
279     awful.key({ modkey, "Control" }, "Right", function () awful.client.moveresize(20, 0, 0, 0) end),
280     awful.key({ modkey, "Control" , "Shift" }, "Down",  function () awful.client.moveresize(0, 0, 0, -20) end),
281     awful.key({ modkey, "Control" , "Shift" }, "Up",    function () awful.client.moveresize(0, 0, 0, 20) end),
282     awful.key({ modkey, "Control" , "Shift" }, "Left",  function () awful.client.moveresize(0, 0, -20, 0) end),
283     awful.key({ modkey, "Control" , "Shift" }, "Right", function () awful.client.moveresize(0, 0, 20, 0) end),
284    -- Prompt
285     awful.key({ modkey },            "r",     function () mypromptbox[mouse.screen]:run() end),
286
287     awful.key({ modkey }, "x",
288               function ()
289                   awful.prompt.run({ prompt = "Run Lua code: " },
290                   mypromptbox[mouse.screen].widget,
291                   awful.util.eval, nil,
292                   awful.util.getdir("cache") .. "/history_eval")
293               end)
294 )
295
296 clientkeys = awful.util.table.join(
297     awful.key({ modkey,           }, "f",      function (c) c.fullscreen = not c.fullscreen  end),
298     awful.key({ modkey, "Shift"   }, "c",      function (c) c:kill()                         end),
299     awful.key({ modkey, "Control" }, "space",  awful.client.floating.toggle                     ),
300     awful.key({ modkey,           }, "space", function (c) c:swap(awful.client.getmaster()) end),
301     awful.key({ modkey,           }, "o",      awful.client.movetoscreen                        ),
302     awful.key({ modkey, "Shift"   }, "Right",  function(c) awful.client.movetoscreen(c,c.screen-1 % screen.count())      end),
303     awful.key({ modkey, "Shift"   }, "Left",   function(c) awful.client.movetoscreen(c,c.screen+1 % screen.count())     end),
304     awful.key({ modkey,           }, "t",      function (c) c.ontop = not c.ontop            end),
305     awful.key({ modkey, "Shift"   }, "r",      function (c) c:redraw()                       end),
306     awful.key({ modkey, "Shift"   }, "m",      function (c) c.minimized = not c.minimized    end),
307     awful.key({ modkey,           }, "m",
308         function (c)
309             c.maximized_horizontal = not c.maximized_horizontal
310             c.maximized_vertical   = not c.maximized_vertical
311         end)
312 )
313
314 -- Compute the maximum number of digit we need, limited to 9
315 keynumber = 0
316 for s = 1, screen.count() do
317    keynumber = math.min(9, math.max(#tags[s], keynumber));
318 end
319
320 -- Bind all key numbers to tags.
321 -- Be careful: we use keycodes to make it works on any keyboard layout.
322 -- This should map on the top row of your keyboard, usually 1 to 9.
323 for i = 1, keynumber do
324     globalkeys = awful.util.table.join(globalkeys,
325         awful.key({ modkey }, "#" .. i + 9,
326                   function ()
327                         local screen = mouse.screen
328                         if tags[screen][i] then
329                             awful.tag.viewonly(tags[screen][i])
330                         end
331                   end),
332         awful.key({ modkey, "Control" }, "#" .. i + 9,
333                   function ()
334                       local screen = mouse.screen
335                       if tags[screen][i] then
336                           awful.tag.viewtoggle(tags[screen][i])
337                       end
338                   end),
339         awful.key({ modkey, "Shift" }, "#" .. i + 9,
340                   function ()
341                       if client.focus and tags[client.focus.screen][i] then
342                           awful.client.movetotag(tags[client.focus.screen][i])
343                       end
344                   end),
345         awful.key({ modkey, "Control", "Shift" }, "#" .. i + 9,
346                   function ()
347                       if client.focus and tags[client.focus.screen][i] then
348                           awful.client.toggletag(tags[client.focus.screen][i])
349                       end
350                   end))
351 end
352
353 clientbuttons = awful.util.table.join(
354     awful.button({ }, 1, function (c) client.focus = c; c:raise() end),
355     awful.button({ modkey }, 1, awful.mouse.client.move),
356     awful.button({ modkey }, 3, awful.mouse.client.resize))
357
358 -- Set keys
359 root.keys(globalkeys)
360 -- }}}
361
362 -- {{{ Rules
363 awful.rules.rules = {
364     -- All clients will match this rule.
365     { rule = { },
366       properties = { border_width = beautiful.border_width,
367                      border_color = beautiful.border_normal,
368                      focus = true,
369                      keys = clientkeys,
370                      buttons = clientbuttons } },
371     { rule = { class = "MPlayer" },
372       properties = { floating = true , ontop=true } },
373     { rule = { class = "pinentry" },
374       properties = { floating = true , ontop=true } },
375     { rule = { class = "gimp" },
376       properties = { floating = true } },
377     -- Set Firefox to always map on tags number 2 of screen 1.
378     -- { rule = { class = "Firefox" },
379     --   properties = { tag = tags[1][2] } },
380 }
381 -- }}}
382
383 -- {{{ Signals
384 -- Signal function to execute when a new client appears.
385 client.add_signal("manage", function (c, startup)
386     -- Add a titlebar
387     -- awful.titlebar.add(c, { modkey = modkey })
388
389     -- Enable sloppy focus
390     c:add_signal("mouse::enter", function(c)
391         if awful.layout.get(c.screen) ~= awful.layout.suit.magnifier
392             and awful.client.focus.filter(c) then
393             client.focus = c
394         end
395     end)
396
397     if not startup then
398         -- Set the windows at the slave,
399         -- i.e. put it at the end of others instead of setting it master.
400         -- awful.client.setslave(c)
401
402         -- Put windows in a smart way, only if they does not set an initial position.
403         if not c.size_hints.user_position and not c.size_hints.program_position then
404             awful.placement.no_overlap(c)
405             awful.placement.no_offscreen(c)
406         end
407     end
408 end)
409
410 function warp_mouse()
411     c = client.focus
412     if c then
413         local g = c:geometry()
414         mouse.coords({ x = g.x + 1, y = g.y + 1}, true)
415     end
416 end
417
418 -- for s = 1, screen.count() do
419 --     screen[s]:add_signal("arrange", function (screen)
420 --         warp_mouse(screen)
421 --     end)
422 -- end
423
424
425 client.add_signal("focus", function(c) c.border_color = beautiful.border_focus end)
426 client.add_signal("unfocus", function(c) c.border_color = beautiful.border_normal end)
427 -- }}}