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,
19 local tonumber = tonumber
20 local setmetatable = setmetatable
23 -- lain.widgets.alsabar
26 background = "#000000",
35 local function worker(args)
36 local args = args or {}
37 local timeout = args.timeout or 5
38 local settings = args.settings or function() end
39 local width = args.width or 63
40 local height = args.height or 1
41 local ticks = args.ticks or false
42 local ticks_size = args.ticks_size or 7
43 local vertical = args.vertical or false
45 alsabar.cmd = args.cmd or "amixer"
46 alsabar.channel = args.channel or "Master"
47 alsabar.togglechannel = args.togglechannel
48 alsabar.colors = args.colors or alsabar.colors
49 alsabar.followtag = args.followtag or false
50 alsabar.notification_preset = args.notification_preset
52 if not alsabar.notification_preset then
53 alsabar.notification_preset = {}
54 alsabar.notification_preset.font = "Monospace 10"
57 local format_cmd = string.format("%s get %s", alsabar.cmd, alsabar.channel)
59 if alsabar.togglechannel then
60 format_cmd = { awful.util.shell, "-c", string.format("%s get %s; %s get %s",
61 alsabar.cmd, alsabar.channel, alsabar.cmd, alsabar.togglechannel) }
64 alsabar.bar = wibox.widget {
65 forced_height = height,
67 color = alsabar.colors.unmute,
68 background_color = alsabar.colors.background,
72 ticks_size = ticks_size,
73 widget = wibox.widget.progressbar,
74 layout = vertical and wibox.container.rotate
77 alsabar.tooltip = awful.tooltip({ objects = { alsabar.bar } })
79 function alsabar.update(callback)
80 helpers.async(format_cmd, function(mixer)
81 local volu,mute = string.match(mixer, "([%d]+)%%.*%[([%l]*)")
82 if (volu and tonumber(volu) ~= alsabar._current_level) or (mute and string.match(mute, "on") ~= alsabar._muted) then
83 alsabar._current_level = tonumber(volu) or alsabar._current_level
84 alsabar.bar:set_value(alsabar._current_level / 100)
85 if (not mute and tonumber(volu) == 0) or mute == "off" then
87 alsabar.tooltip:set_text ("[Muted]")
88 alsabar.bar.color = alsabar.colors.mute
90 alsabar._muted = false
91 alsabar.tooltip:set_text(string.format("%s: %s", alsabar.channel, volu))
92 alsabar.bar.color = alsabar.colors.unmute
96 volume_now.level = tonumber(volu)
97 volume_now.status = mute
101 if type(callback) == "function" then callback() end
106 function alsabar.notify()
107 alsabar.update(function()
108 local preset = alsabar.notification_preset
110 if alsabar._muted then
111 preset.title = string.format("%s - Muted", alsabar.channel)
113 preset.title = string.format("%s - %s%%", alsabar.channel, alsabar._current_level)
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))
120 if alsabar.followtag then preset.screen = awful.screen.focused() end
122 if not alsabar.notification then
123 alsabar.notification = naughty.notify {
125 destroy = function() alsabar.notification = nil end
128 naughty.replace_text(alsabar.notification, preset.title, preset.text)
133 helpers.newtimer(string.format("alsabar-%s-%s", alsabar.cmd, alsabar.channel), timeout, alsabar.update)
138 return setmetatable(alsabar, { __call = function(_, ...) return worker(...) end })