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 newtimer = require("lain.helpers").newtimer
11 local read_pipe = require("lain.helpers").read_pipe
13 local awful = require("awful")
14 local beautiful = require("beautiful")
15 local naughty = require("naughty")
17 local math = { modf = math.modf }
19 local string = { format = string.format,
22 local tonumber = tonumber
24 local setmetatable = setmetatable
27 -- lain.widgets.alsabar
33 background = beautiful.bg_normal,
38 terminal = terminal or "xterm",
39 mixer = terminal .. " -e alsamixer",
42 font = beautiful.font:sub(beautiful.font:find(""), beautiful.font:find(" ")),
44 color = beautiful.fg_normal,
53 function alsabar.notify()
60 screen = alsabar.notifications.screen,
61 font = alsabar.notifications.font .. " " ..
62 alsabar.notifications.font_size,
63 fg = alsabar.notifications.color
68 preset.title = alsabar.channel .. " - Muted"
70 preset.title = alsabar.channel .. " - " .. alsabar._current_level .. "%"
73 int = math.modf((alsabar._current_level / 100) * alsabar.notifications.bar_size)
75 .. string.rep("|", int)
76 .. string.rep(" ", alsabar.notifications.bar_size - int)
79 if alsabar.followmouse then
80 preset.screen = mouse.screen
83 if alsabar._notify ~= nil then
84 alsabar._notify = naughty.notify ({
85 replaces_id = alsabar._notify.id,
89 alsabar._notify = naughty.notify ({
95 local function worker(args)
96 local args = args or {}
97 local timeout = args.timeout or 5
98 local settings = args.settings or function() end
99 local width = args.width or 63
100 local height = args.heigth or 1
101 local ticks = args.ticks or false
102 local ticks_size = args.ticks_size or 7
103 local vertical = args.vertical or false
105 alsabar.cmd = args.cmd or "amixer"
106 alsabar.channel = args.channel or alsabar.channel
107 alsabar.togglechannel = args.togglechannel
108 alsabar.step = args.step or alsabar.step
109 alsabar.colors = args.colors or alsabar.colors
110 alsabar.notifications = args.notifications or alsabar.notifications
111 alsabar.followmouse = args.followmouse or false
113 alsabar.bar = awful.widget.progressbar()
115 alsabar.bar:set_background_color(alsabar.colors.background)
116 alsabar.bar:set_color(alsabar.colors.unmute)
117 alsabar.tooltip = awful.tooltip({ objects = { alsabar.bar } })
118 alsabar.bar:set_width(width)
119 alsabar.bar:set_height(height)
120 alsabar.bar:set_ticks(ticks)
121 alsabar.bar:set_ticks_size(ticks_size)
122 alsabar.bar:set_vertical(vertical)
124 function alsabar.update()
125 -- Get mixer control contents
126 local mixer = read_pipe(string.format("%s get %s", alsabar.cmd, alsabar.channel))
128 -- Capture mixer control state: [5%] ... ... [on]
129 local volu, mute = string.match(mixer, "([%d]+)%%.*%[([%l]*)")
131 -- HDMIs can have a channel different from Master for toggling mute
132 if alsabar.togglechannel then
133 mute = string.match(read_pipe(string.format("%s get %s", alsabar.cmd, alsabar.togglechannel)), "%[(%a+)%]")
136 if (volu and tonumber(volu) ~= alsabar._current_level) or (mute and string.match(mute, "on") ~= alsabar._muted)
138 alsabar._current_level = tonumber(volu)
139 alsabar.bar:set_value(alsabar._current_level / 100)
140 if not mute and tonumber(volu) == 0 or mute == "off"
142 alsabar._muted = true
143 alsabar.tooltip:set_text (" [Muted] ")
144 alsabar.bar:set_color(alsabar.colors.mute)
146 alsabar._muted = false
147 alsabar.tooltip:set_text(string.format(" %s:%s ", alsabar.channel, volu))
148 alsabar.bar:set_color(alsabar.colors.unmute)
152 volume_now.level = tonumber(volu)
153 volume_now.status = mute
158 alsabar.bar:buttons (awful.util.table.join (
159 awful.button ({}, 1, function()
160 awful.util.spawn(alsabar.mixer)
162 awful.button ({}, 3, function()
163 awful.util.spawn(string.format("%s set %s toggle", alsabar.cmd, alsabar.channel))
166 awful.button ({}, 4, function()
167 awful.util.spawn(string.format("%s set %s %s+", alsabar.cmd, alsabar.channel, alsabar.step))
170 awful.button ({}, 5, function()
171 awful.util.spawn(string.format("%s set %s %s-", alsabar.cmd, alsabar.channel, alsabar.step))
176 timer_id = string.format("alsabar-%s-%s", alsabar.cmd, alsabar.channel)
178 newtimer(timer_id, timeout, alsabar.update)
183 return setmetatable(alsabar, { __call = function(_, ...) return worker(...) end })