2013-09-07 12:06:42 +02:00
|
|
|
|
|
|
|
--[[
|
2013-09-13 18:15:25 +02:00
|
|
|
|
|
|
|
Licensed under GNU General Public License v2
|
|
|
|
* (c) 2013, Luke Bonham
|
|
|
|
* (c) 2010, Adrian C. <anrxc@sysphere.org>
|
|
|
|
|
2013-09-07 12:06:42 +02:00
|
|
|
--]]
|
|
|
|
|
2016-03-31 12:00:47 +02:00
|
|
|
local newtimer = require("lain.helpers").newtimer
|
|
|
|
local read_pipe = require("lain.helpers").read_pipe
|
2013-09-07 12:06:42 +02:00
|
|
|
|
2016-03-31 12:00:47 +02:00
|
|
|
local wibox = require("wibox")
|
2013-09-07 12:06:42 +02:00
|
|
|
|
2016-03-31 12:00:47 +02:00
|
|
|
local string = { match = string.match,
|
|
|
|
format = string.format }
|
2013-09-07 12:06:42 +02:00
|
|
|
|
2016-03-31 12:00:47 +02:00
|
|
|
local setmetatable = setmetatable
|
2013-09-07 12:06:42 +02:00
|
|
|
|
2013-09-10 23:02:11 +02:00
|
|
|
-- ALSA volume
|
|
|
|
-- lain.widgets.alsa
|
2016-03-20 23:41:44 +01:00
|
|
|
local alsa = { last_level = "0", last_status = "" }
|
2013-09-10 23:02:11 +02:00
|
|
|
|
|
|
|
local function worker(args)
|
|
|
|
local args = args or {}
|
2015-10-20 15:17:44 +02:00
|
|
|
local timeout = args.timeout or 5
|
2013-09-10 23:02:11 +02:00
|
|
|
local settings = args.settings or function() end
|
|
|
|
|
2016-08-28 13:57:58 +02:00
|
|
|
alsa.cmd = args.cmd or "amixer"
|
|
|
|
alsa.channel = args.channel or "Master"
|
|
|
|
alsa.togglechannel = args.togglechannel
|
|
|
|
alsa.widget = wibox.widget.textbox('')
|
2013-09-10 23:02:11 +02:00
|
|
|
|
2013-09-11 19:39:14 +02:00
|
|
|
function alsa.update()
|
2016-03-03 12:40:49 +01:00
|
|
|
mixer = read_pipe(string.format("%s get %s", alsa.cmd, alsa.channel))
|
2016-03-31 12:00:47 +02:00
|
|
|
l,s = string.match(mixer, "([%d]+)%%.*%[([%l]*)")
|
2013-09-07 12:06:42 +02:00
|
|
|
|
2016-08-28 13:57:58 +02:00
|
|
|
-- HDMIs can have a channel different from Master for toggling mute
|
|
|
|
if alsa.togglechannel then
|
|
|
|
s = string.match(read_pipe(string.format("%s get %s", alsa.cmd, alsa.togglechannel)), "%[(%a+)%]")
|
|
|
|
end
|
|
|
|
|
2016-03-03 12:40:49 +01:00
|
|
|
if alsa.last_level ~= l or alsa.last_status ~= s then
|
|
|
|
volume_now = { level = l, status = s }
|
2016-03-31 12:00:47 +02:00
|
|
|
alsa.last_level = l
|
2016-03-03 12:40:49 +01:00
|
|
|
alsa.last_status = s
|
2013-09-10 23:02:11 +02:00
|
|
|
|
2016-03-03 12:40:49 +01:00
|
|
|
widget = alsa.widget
|
|
|
|
settings()
|
2013-09-07 12:06:42 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-07-09 12:52:18 +02:00
|
|
|
timer_id = string.format("alsa-%s-%s", alsa.cmd, alsa.channel)
|
|
|
|
newtimer(timer_id, timeout, alsa.update)
|
2013-09-10 23:02:11 +02:00
|
|
|
|
2013-09-11 19:39:14 +02:00
|
|
|
return setmetatable(alsa, { __index = alsa.widget })
|
2013-09-07 12:06:42 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return setmetatable(alsa, { __call = function(_, ...) return worker(...) end })
|