lain/widgets/alsa.lua

65 lines
1.6 KiB
Lua
Raw Normal View History

2013-09-07 12:06:42 +02:00
--[[
Licensed under GNU General Public License v2
2013-09-10 23:02:11 +02:00
* (c) 2013, Luke Bonham
* (c) 2010, Adrian C. <anrxc@sysphere.org>
2013-09-07 12:06:42 +02:00
--]]
2013-09-10 23:02:11 +02:00
local newtimer = require("lain.helpers").newtimer
2013-09-07 12:06:42 +02:00
local wibox = require("wibox")
2013-09-10 23:02:11 +02:00
local io = { popen = io.popen }
local string = { match = string.match }
2013-09-07 12:06:42 +02:00
local setmetatable = setmetatable
2013-09-10 23:02:11 +02:00
-- ALSA volume
-- lain.widgets.alsa
local alsa = {}
local function worker(args)
local args = args or {}
local timeout = args.timeout or 5
local channel = args.channel or "Master"
local settings = args.settings or function() end
2013-09-11 19:39:14 +02:00
alsa.widget = wibox.widget.textbox('')
2013-09-10 23:02:11 +02:00
2013-09-11 19:39:14 +02:00
function alsa.update()
2013-09-07 12:06:42 +02:00
local f = io.popen('amixer get ' .. channel)
local mixer = f:read("*all")
f:close()
2013-09-10 23:02:11 +02:00
volume = {}
2013-09-07 12:06:42 +02:00
2013-09-10 23:02:11 +02:00
volume.level, volume.status = string.match(mixer, "([%d]+)%%.*%[([%l]*)")
if volume.level == nil
2013-09-07 12:06:42 +02:00
then
2013-09-10 23:02:11 +02:00
volume.level = 0
volume.status = "off"
2013-09-07 12:06:42 +02:00
end
2013-09-10 23:02:11 +02:00
if volume.status == ""
2013-09-07 12:06:42 +02:00
then
2013-09-10 23:02:11 +02:00
if volume.level == 0
then
volume.status = "off"
else
volume.status = "on"
end
2013-09-07 12:06:42 +02:00
end
2013-09-10 23:02:11 +02:00
settings()
2013-09-07 12:06:42 +02:00
end
2013-09-11 19:39:14 +02:00
newtimer("alsa", 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 })