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")
16 local tonumber = tonumber
18 -- PulseAudio volume bar
19 -- lain.widget.pulsebar
21 local function factory(args)
24 background = "#000000",
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 margins = args.margins or 1
40 local paddings = args.paddings or 1
41 local ticks = args.ticks or false
42 local ticks_size = args.ticks_size or 7
43 local tick = args.tick or "|"
44 local tick_pre = args.tick_pre or "["
45 local tick_post = args.tick_post or "]"
46 local tick_none = args.tick_none or " "
48 pulsebar.colors = args.colors or pulsebar.colors
49 pulsebar.followtag = args.followtag or false
50 pulsebar.notification_preset = args.notification_preset
51 pulsebar.devicetype = args.devicetype or "sink"
52 pulsebar.cmd = args.cmd or "pacmd list-" .. pulsebar.devicetype .. "s | sed -n -e '/*/,$!d' -e '/index/p' -e '/base volume/d' -e '/volume:/p' -e '/muted:/p' -e '/device\\.string/p'"
54 if not pulsebar.notification_preset then
55 pulsebar.notification_preset = {
60 pulsebar.bar = wibox.widget {
61 color = pulsebar.colors.unmute,
62 background_color = pulsebar.colors.background,
63 forced_height = height,
68 ticks_size = ticks_size,
69 widget = wibox.widget.progressbar,
72 pulsebar.tooltip = awful.tooltip({ objects = { pulsebar.bar } })
74 function pulsebar.update(callback)
75 helpers.async({ awful.util.shell, "-c", type(pulsebar.cmd) == "string" and pulsebar.cmd or pulsebar.cmd() },
78 index = string.match(s, "index: (%S+)") or "N/A",
79 device = string.match(s, "device.string = \"(%S+)\"") or "N/A",
80 muted = string.match(s, "muted: (%S+)") or "N/A"
83 pulsebar.device = volume_now.index
86 volume_now.channel = {}
87 for v in string.gmatch(s, ":.-(%d+)%%") do
88 volume_now.channel[ch] = v
92 volume_now.left = volume_now.channel[1] or "N/A"
93 volume_now.right = volume_now.channel[2] or "N/A"
95 local volu = volume_now.left
96 local mute = volume_now.muted
98 if volu:match("N/A") or mute:match("N/A") then return end
100 if volu ~= pulsebar._current_level or mute ~= pulsebar._mute then
101 pulsebar._current_level = tonumber(volu)
102 pulsebar.bar:set_value(pulsebar._current_level / 100)
103 if pulsebar._current_level == 0 or mute == "yes" then
104 pulsebar._mute = mute
105 pulsebar.tooltip:set_text ("[muted]")
106 pulsebar.bar.color = pulsebar.colors.mute
108 pulsebar._mute = "no"
109 pulsebar.tooltip:set_text(string.format("%s %s: %s", pulsebar.devicetype, pulsebar.device, volu))
110 pulsebar.bar.color = pulsebar.colors.unmute
115 if type(callback) == "function" then callback() end
120 function pulsebar.notify()
121 pulsebar.update(function()
122 local preset = pulsebar.notification_preset
124 preset.title = string.format("%s %s - %s%%", pulsebar.devicetype, pulsebar.device, pulsebar._current_level)
126 if pulsebar._mute == "yes" then
127 preset.title = preset.title .. " muted"
130 -- tot is the maximum number of ticks to display in the notification
131 -- fallback: default horizontal wibox height
132 local wib, tot = awful.screen.focused().mywibox, 20
134 -- if we can grab mywibox, tot is defined as its height if
135 -- horizontal, or width otherwise
137 if wib.position == "left" or wib.position == "right" then
144 int = math.modf((pulsebar._current_level / 100) * tot)
145 preset.text = string.format(
148 string.rep(tick, int),
149 string.rep(tick_none, tot - int),
153 if pulsebar.followtag then preset.screen = awful.screen.focused() end
155 if not pulsebar.notification then
156 pulsebar.notification = naughty.notify {
158 destroy = function() pulsebar.notification = nil end
161 naughty.replace_text(pulsebar.notification, preset.title, preset.text)
166 helpers.newtimer(string.format("pulsebar-%s-%s", pulsebar.devicetype, pulsebar.device), timeout, pulsebar.update)