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.
3 Licensed under GNU General Public License v2
9 local helpers = require("lain.helpers")
10 local awful = require("awful")
11 local naughty = require("naughty")
12 local wibox = require("wibox")
14 local math, string, type, tonumber = math, string, type, tonumber
17 -- lain.widget.alsabar
19 local function factory(args)
22 background = "#000000",
31 local args = args or {}
32 local timeout = args.timeout or 5
33 local settings = args.settings or function() end
34 local width = args.width or 63
35 local height = args.height or 1
36 local margins = args.margins or 1
37 local paddings = args.paddings or 1
38 local ticks = args.ticks or false
39 local ticks_size = args.ticks_size or 7
41 alsabar.cmd = args.cmd or "amixer"
42 alsabar.channel = args.channel or "Master"
43 alsabar.togglechannel = args.togglechannel
44 alsabar.colors = args.colors or alsabar.colors
45 alsabar.followtag = args.followtag or false
46 alsabar.notification_preset = args.notification_preset
48 if not alsabar.notification_preset then
49 alsabar.notification_preset = {}
50 alsabar.notification_preset.font = "Monospace 10"
53 local format_cmd = string.format("%s get %s", alsabar.cmd, alsabar.channel)
55 if alsabar.togglechannel then
56 format_cmd = { awful.util.shell, "-c", string.format("%s get %s; %s get %s",
57 alsabar.cmd, alsabar.channel, alsabar.cmd, alsabar.togglechannel) }
60 alsabar.bar = wibox.widget {
61 color = alsabar.colors.unmute,
62 background_color = alsabar.colors.background,
63 forced_height = height,
68 ticks_size = ticks_size,
69 widget = wibox.widget.progressbar
72 alsabar.tooltip = awful.tooltip({ objects = { alsabar.bar } })
74 function alsabar.update(callback)
75 helpers.async(format_cmd, function(mixer)
76 local vol, playback = string.match(mixer, "([%d]+)%%.*%[([%l]*)")
78 if not vol or not playback then return end
80 if vol ~= alsabar._current_level or playback ~= alsabar._playback then
81 alsabar._current_level = tonumber(vol)
82 alsabar.bar:set_value(alsabar._current_level / 100)
83 if alsabar._current_level == 0 or playback == "off" then
84 alsabar._playback = playback
85 alsabar.tooltip:set_text("[Muted]")
86 alsabar.bar.color = alsabar.colors.mute
88 alsabar._playback = "on"
89 alsabar.tooltip:set_text(string.format("%s: %s", alsabar.channel, vol))
90 alsabar.bar.color = alsabar.colors.unmute
94 level = alsabar._current_level,
95 status = alsabar._playback
100 if type(callback) == "function" then callback() end
105 function alsabar.notify()
106 alsabar.update(function()
107 local preset = alsabar.notification_preset
109 preset.title = string.format("%s - %s%%", alsabar.channel, alsabar._current_level)
111 if alsabar._playback == "off" then
112 preset.title = preset.title .. " Muted"
115 -- tot is the maximum number of ticks to display in the notification
116 -- fallback: default horizontal wibox height
117 local wib, tot = awful.screen.focused().mywibox, 20
119 -- if we can grab mywibox, tot is defined as its height if
120 -- horizontal, or width otherwise
122 if wib.position == "left" or wib.position == "right" then
129 int = math.modf((alsabar._current_level / 100) * tot)
130 preset.text = string.format("[%s%s]", string.rep("|", int),
131 string.rep(" ", tot - int))
133 if alsabar.followtag then preset.screen = awful.screen.focused() end
135 if not alsabar.notification then
136 alsabar.notification = naughty.notify {
138 destroy = function() alsabar.notification = nil end
141 naughty.replace_text(alsabar.notification, preset.title, preset.text)
146 helpers.newtimer(string.format("alsabar-%s-%s", alsabar.cmd, alsabar.channel), timeout, alsabar.update)