]> git.madduck.net Git - etc/awesome.git/blob - widget/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:

Merge branch 'master' of http://github.com/copycat-killer/lain into fix/pulse-sink
[etc/awesome.git] / widget / alsabar.lua
1 --[[
2
3      Licensed under GNU General Public License v2
4       * (c) 2013, Luke Bonham
5       * (c) 2013, Rman
6
7 --]]
8
9 local helpers        = require("lain.helpers")
10 local awful          = require("awful")
11 local naughty        = require("naughty")
12 local wibox          = require("wibox")
13 local math           = { modf   = math.modf }
14 local string         = { format = string.format,
15                          match  = string.match,
16                          rep    = string.rep }
17 local type, tonumber = type, tonumber
18
19 -- ALSA volume bar
20 -- lain.widget.alsabar
21
22 local function factory(args)
23     local alsabar = {
24         colors = {
25             background = "#000000",
26             mute       = "#EB8F8F",
27             unmute     = "#A4CE8A"
28         },
29
30         _current_level = 0,
31         _playback      = "off"
32     }
33
34     local args       = args or {}
35     local timeout    = args.timeout or 5
36     local settings   = args.settings or function() end
37     local width      = args.width or 63
38     local height     = args.height or 1
39     local ticks      = args.ticks or false
40     local ticks_size = args.ticks_size or 7
41
42     alsabar.cmd                 = args.cmd or "amixer"
43     alsabar.channel             = args.channel or "Master"
44     alsabar.togglechannel       = args.togglechannel
45     alsabar.colors              = args.colors or alsabar.colors
46     alsabar.followtag           = args.followtag or false
47     alsabar.notification_preset = args.notification_preset
48
49     if not alsabar.notification_preset then
50         alsabar.notification_preset      = {}
51         alsabar.notification_preset.font = "Monospace 10"
52     end
53
54     local format_cmd = string.format("%s get %s", alsabar.cmd, alsabar.channel)
55
56     if alsabar.togglechannel then
57         format_cmd = { awful.util.shell, "-c", string.format("%s get %s; %s get %s",
58         alsabar.cmd, alsabar.channel, alsabar.cmd, alsabar.togglechannel) }
59     end
60
61     alsabar.bar = wibox.widget {
62         forced_height    = height,
63         forced_width     = width,
64         color            = alsabar.colors.unmute,
65         background_color = alsabar.colors.background,
66         margins          = 1,
67         paddings         = 1,
68         ticks            = ticks,
69         ticks_size       = ticks_size,
70         widget           = wibox.widget.progressbar
71     }
72
73     alsabar.tooltip = awful.tooltip({ objects = { alsabar.bar } })
74
75     function alsabar.update(callback)
76         helpers.async(format_cmd, function(mixer)
77             local vol, playback = string.match(mixer, "([%d]+)%%.*%[([%l]*)")
78
79             if not vol or not playback then return end
80
81             if vol ~= alsabar._current_level or playback ~= alsabar._playback then
82                 alsabar._current_level = tonumber(vol)
83                 alsabar.bar:set_value(alsabar._current_level / 100)
84                 if alsabar._current_level == 0 or playback == "off" then
85                     alsabar._playback = playback
86                     alsabar.tooltip:set_text("[Muted]")
87                     alsabar.bar.color = alsabar.colors.mute
88                 else
89                     alsabar._playback = "on"
90                     alsabar.tooltip:set_text(string.format("%s: %s", alsabar.channel, vol))
91                     alsabar.bar.color = alsabar.colors.unmute
92                 end
93
94                 volume_now = {
95                     level  = alsabar._current_level,
96                     status = alsabar._playback
97                 }
98
99                 settings()
100
101                 if type(callback) == "function" then callback() end
102             end
103         end)
104     end
105
106     function alsabar.notify()
107         alsabar.update(function()
108             local preset = alsabar.notification_preset
109
110             preset.title = string.format("%s - %s%%", alsabar.channel, alsabar._current_level)
111
112             if alsabar._playback == "off" then
113                 preset.title = preset.title .. " Muted"
114             end
115
116             int = math.modf((alsabar._current_level / 100) * awful.screen.focused().mywibox.height)
117             preset.text = string.format("[%s%s]", string.rep("|", int),
118                           string.rep(" ", awful.screen.focused().mywibox.height - int))
119
120             if alsabar.followtag then preset.screen = awful.screen.focused() end
121
122             if not alsabar.notification then
123                 alsabar.notification = naughty.notify {
124                     preset  = preset,
125                     destroy = function() alsabar.notification = nil end
126                 }
127             else
128                 naughty.replace_text(alsabar.notification, preset.title, preset.text)
129             end
130         end)
131     end
132
133     helpers.newtimer(string.format("alsabar-%s-%s", alsabar.cmd, alsabar.channel), timeout, alsabar.update)
134
135     return alsabar
136 end
137
138 return factory