]> git.madduck.net Git - etc/awesome.git/blob - widgets/alsabar.lua

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

6a03a1079c55c3b66a32e2eadb4f11d5cc3b6f07
[etc/awesome.git] / widgets / alsabar.lua
1
2 --[[
3                                                   
4      Licensed under GNU General Public License v2 
5       * (c) 2013, Luke Bonham                     
6       * (c) 2013, Rman                            
7                                                   
8 --]]
9
10 local helpers      = require("lain.helpers")
11 local awful        = require("awful")
12 local naughty      = require("naughty")
13 local wibox        = require("wibox")
14 local math         = { modf   = math.modf }
15 local string       = { format = string.format,
16                        match  = string.match,
17                        rep    = string.rep }
18 local tonumber     = tonumber
19 local setmetatable = setmetatable
20
21 -- ALSA volume bar
22 -- lain.widgets.alsabar
23 local alsabar = {
24     colors = {
25         background = "#000000",
26         mute       = "#EB8F8F",
27         unmute     = "#A4CE8A"
28     },
29
30     _current_level = 0,
31     _muted         = false
32 }
33
34 local function worker(args)
35     local args       = args or {}
36     local timeout    = args.timeout or 5
37     local settings   = args.settings or function() end
38     local width      = args.width or 63
39     local height     = args.height or 1
40     local ticks      = args.ticks or false
41     local ticks_size = args.ticks_size or 7
42     local vertical   = args.vertical or false
43
44     alsabar.cmd                 = args.cmd or "amixer"
45     alsabar.channel             = args.channel or "Master"
46     alsabar.colors              = args.colors or alsabar.colors
47     alsabar.followtag           = args.followtag or false
48     alsabar._notify             = args.notify or "on"
49     alsabar.notification_preset = args.notification_preset
50
51     if not alsabar.notification_preset then
52         alsabar.notification_preset      = naughty.config.defaults
53         alsabar.notification_preset.font = "Monospace 11"
54     end
55
56     alsabar.bar = wibox.widget {
57         forced_height    = height,
58         forced_width     = width,
59         color            = alsabar.colors.unmute,
60         background_color = alsabar.colors.background,
61         margins          = 1,
62         paddings         = 1,
63         ticks            = ticks,
64         ticks_size       = ticks_size,
65         widget           = wibox.widget.progressbar,
66         layout           = vertical and wibox.container.rotate
67     }
68
69     alsabar.tooltip = awful.tooltip({ objects = { alsabar.bar } })
70
71     function alsabar.update()
72         helpers.async(alsabar.cmd, function(mixer)
73             local volu,mute = string.match(mixer, "([%d]+)%%.*%[([%l]*)")
74             if (volu and tonumber(volu) ~= alsabar._current_level) or (mute and string.match(mute, "on") ~= alsabar._muted) then
75                 alsabar._current_level = tonumber(volu) or alsabar._current_level
76                 alsabar.bar:set_value(alsabar._current_level / 100)
77                 if (not mute and tonumber(volu) == 0) or mute == "off" then
78                     alsabar._muted = true
79                     alsabar.tooltip:set_text ("[Muted]")
80                     alsabar.bar.color = alsabar.colors.mute
81                 else
82                     alsabar._muted = false
83                     alsabar.tooltip:set_text(string.format("%s: %s", alsabar.channel, volu))
84                     alsabar.bar.color = alsabar.colors.unmute
85                 end
86
87                 volume_now = {}
88                 volume_now.level = tonumber(volu)
89                 volume_now.status = mute
90
91                 settings()
92             end
93         end)
94     end
95
96     function alsabar.notify()
97         alsabar.update(function()
98             local preset = alsabar.notification_preset
99
100             if alsabar._muted then
101                 preset.title = string.format("%s - Muted", alsabar.channel)
102             else
103                 preset.title = string.format("%s - %s%%", alsabar.channel, alsabar._current_level)
104             end
105
106             int = math.modf((alsabar._current_level / 100) * awful.screen.focused().mywibox.height)
107             preset.text = string.format("[%s%s]", string.rep("|", int),
108                           string.rep(" ", awful.screen.focused().mywibox.height - int))
109
110             if alsabar.followtag then preset.screen = awful.screen.focused() end
111
112             alsabar.id = naughty.notify ({
113                 replaces_id = alsabar.id,
114                 preset      = preset
115             }).id
116         end)
117     end
118
119     timer_id = string.format("alsabar-%s-%s", alsabar.cmd, alsabar.channel)
120
121     helpers.newtimer(timer_id, timeout, alsabar.update)
122
123     return alsabar
124 end
125
126 return setmetatable(alsabar, { __call = function(_, ...) return worker(...) end })