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.
4 Licensed under GNU General Public License v2
5 * (c) 2013, Luke Bonham
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,
18 local type, tonumber = type, tonumber
21 -- lain.widget.alsabar
23 local function factory(args)
26 background = "#000000",
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
43 alsabar.cmd = args.cmd or "amixer"
44 alsabar.channel = args.channel or "Master"
45 alsabar.togglechannel = args.togglechannel
46 alsabar.colors = args.colors or alsabar.colors
47 alsabar.followtag = args.followtag or false
48 alsabar.notification_preset = args.notification_preset
50 if not alsabar.notification_preset then
51 alsabar.notification_preset = {}
52 alsabar.notification_preset.font = "Monospace 10"
55 local format_cmd = string.format("%s get %s", alsabar.cmd, alsabar.channel)
57 if alsabar.togglechannel then
58 format_cmd = { awful.util.shell, "-c", string.format("%s get %s; %s get %s",
59 alsabar.cmd, alsabar.channel, alsabar.cmd, alsabar.togglechannel) }
62 alsabar.bar = wibox.widget {
63 forced_height = height,
65 color = alsabar.colors.unmute,
66 background_color = alsabar.colors.background,
70 ticks_size = ticks_size,
71 widget = wibox.widget.progressbar
74 alsabar.tooltip = awful.tooltip({ objects = { alsabar.bar } })
76 function alsabar.update(callback)
77 helpers.async(format_cmd, function(mixer)
78 local vol, playback = string.match(mixer, "([%d]+)%%.*%[([%l]*)")
80 if not vol or not playback then return end
82 if vol ~= alsabar._current_level or playback ~= alsabar._playback then
83 alsabar._current_level = tonumber(vol)
84 alsabar.bar:set_value(alsabar._current_level / 100)
85 if alsabar._current_level == 0 or playback == "off" then
86 alsabar._playback = playback
87 alsabar.tooltip:set_text("[Muted]")
88 alsabar.bar.color = alsabar.colors.mute
90 alsabar._playback = "on"
91 alsabar.tooltip:set_text(string.format("%s: %s", alsabar.channel, vol))
92 alsabar.bar.color = alsabar.colors.unmute
96 level = alsabar._current_level,
97 status = alsabar._playback
102 if type(callback) == "function" then callback() end
107 function alsabar.notify()
108 alsabar.update(function()
109 local preset = alsabar.notification_preset
111 if alsabar._playback == "off" then
112 preset.title = string.format("%s - Muted", alsabar.channel)
114 preset.title = string.format("%s - %s%%", alsabar.channel, alsabar._current_level)
117 int = math.modf((alsabar._current_level / 100) * awful.screen.focused().mywibox.height)
118 preset.text = string.format("[%s%s]", string.rep("|", int),
119 string.rep(" ", awful.screen.focused().mywibox.height - int))
121 if alsabar.followtag then preset.screen = awful.screen.focused() end
123 if not alsabar.notification then
124 alsabar.notification = naughty.notify {
126 destroy = function() alsabar.notification = nil end
129 naughty.replace_text(alsabar.notification, preset.title, preset.text)
134 helpers.newtimer(string.format("alsabar-%s-%s", alsabar.cmd, alsabar.channel), timeout, alsabar.update)