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