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 "]"
47 pulsebar.colors = args.colors or pulsebar.colors
48 pulsebar.followtag = args.followtag or false
49 pulsebar.notification_preset = args.notification_preset
50 pulsebar.devicetype = args.devicetype or "sink"
51 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'"
53 if not pulsebar.notification_preset then
54 pulsebar.notification_preset = {
59 pulsebar.bar = wibox.widget {
60 color = pulsebar.colors.unmute,
61 background_color = pulsebar.colors.background,
62 forced_height = height,
67 ticks_size = ticks_size,
68 widget = wibox.widget.progressbar,
71 pulsebar.tooltip = awful.tooltip({ objects = { pulsebar.bar } })
73 function pulsebar.update(callback)
74 helpers.async({ awful.util.shell, "-c", type(pulsebar.cmd) == "string" and pulsebar.cmd or pulsebar.cmd() },
77 index = string.match(s, "index: (%S+)") or "N/A",
78 device = string.match(s, "device.string = \"(%S+)\"") or "N/A",
79 muted = string.match(s, "muted: (%S+)") or "N/A"
82 pulsebar.device = volume_now.index
85 volume_now.channel = {}
86 for v in string.gmatch(s, ":.-(%d+)%%") do
87 volume_now.channel[ch] = v
91 volume_now.left = volume_now.channel[1] or "N/A"
92 volume_now.right = volume_now.channel[2] or "N/A"
94 local volu = volume_now.left
95 local mute = volume_now.muted
97 if volu:match("N/A") or mute:match("N/A") then return end
99 if volu ~= pulsebar._current_level or mute ~= pulsebar._mute then
100 pulsebar._current_level = tonumber(volu)
101 pulsebar.bar:set_value(pulsebar._current_level / 100)
102 if pulsebar._current_level == 0 or mute == "yes" then
103 pulsebar._mute = mute
104 pulsebar.tooltip:set_text ("[muted]")
105 pulsebar.bar.color = pulsebar.colors.mute
107 pulsebar._mute = "no"
108 pulsebar.tooltip:set_text(string.format("%s %s: %s", pulsebar.devicetype, pulsebar.device, volu))
109 pulsebar.bar.color = pulsebar.colors.unmute
114 if type(callback) == "function" then callback() end
119 function pulsebar.notify()
120 pulsebar.update(function()
121 local preset = pulsebar.notification_preset
123 preset.title = string.format("%s %s - %s%%", pulsebar.devicetype, pulsebar.device, pulsebar._current_level)
125 if pulsebar._mute == "yes" then
126 preset.title = preset.title .. " muted"
129 -- tot is the maximum number of ticks to display in the notification
130 -- fallback: default horizontal wibox height
131 local wib, tot = awful.screen.focused().mywibox, 20
133 -- if we can grab mywibox, tot is defined as its height if
134 -- horizontal, or width otherwise
136 if wib.position == "left" or wib.position == "right" then
143 int = math.modf((pulsebar._current_level / 100) * tot)
144 preset.text = string.format(
147 string.rep(tick, int),
148 string.rep(" ", tot - int),
152 if pulsebar.followtag then preset.screen = awful.screen.focused() end
154 if not pulsebar.notification then
155 pulsebar.notification = naughty.notify {
157 destroy = function() pulsebar.notification = nil end
160 naughty.replace_text(pulsebar.notification, preset.title, preset.text)
165 helpers.newtimer(string.format("pulsebar-%s-%s", pulsebar.devicetype, pulsebar.device), timeout, pulsebar.update)