vicious/widgets/volume_linux.lua

70 lines
1.8 KiB
Lua
Raw Normal View History

2009-09-29 22:33:19 +02:00
---------------------------------------------------
-- Licensed under the GNU General Public License v2
2010-01-02 21:21:54 +01:00
-- * (c) 2010, Adrian C. <anrxc@sysphere.org>
2009-09-29 22:33:19 +02:00
---------------------------------------------------
-- {{{ Grab environment
local tonumber = tonumber
local io = { popen = io.popen }
local setmetatable = setmetatable
local string = { match = string.match }
local helpers = require("vicious.helpers")
local spawn = require("vicious.spawn")
-- }}}
-- Volume: provides volume levels and state of requested ALSA mixers
-- vicious.widgets.volume
2017-01-25 17:56:22 +01:00
local volume_linux = {}
local STATES = { on = '🔉', off = '🔈' }
function volume_linux.async(format, warg, callback)
if not warg then return callback{} end
end
-- {{{ Volume widget type
local function worker(format, warg)
if not warg then return end
local mixer_state = {
["on"] = "", -- "",
["off"] = "" -- "M"
}
if type(warg) ~= "table" then
warg = { warg }
end
local cmd = "amixer -M get "
for _, arg in ipairs(warg) do
cmd = cmd .. " " .. helpers.shellquote(arg)
end
-- Get mixer control contents
local f = io.popen(cmd)
local mixer = f:read("*all")
f:close()
-- Capture mixer control state: [5%] ... ... [on]
local volu, mute = string.match(mixer, "([%d]+)%%.*%[([%l]*)")
-- Handle mixers without data
if volu == nil then
return {0, mixer_state["off"]}
end
-- Handle mixers without mute
if mute == "" and volu == "0"
-- Handle mixers that are muted
or mute == "off" then
mute = mixer_state["off"]
else
mute = mixer_state["on"]
end
return {tonumber(volu), mute}
end
-- }}}
2017-01-25 17:56:22 +01:00
return setmetatable(volume_linux, { __call = function(_, ...) return worker(...) end })