Rewrite pulse module
* replace grep with lua string matching * caching sinks * add toggle function
This commit is contained in:
parent
9bd84ff229
commit
1439de86b8
|
@ -30,56 +30,58 @@ local string = {
|
||||||
-- }}}
|
-- }}}
|
||||||
|
|
||||||
|
|
||||||
-- Pulse: provides volume levels of requested pulseaudio sinks
|
-- Pulse: provides volume levels of requested pulseaudio sinks and methods to change them
|
||||||
module("vicious.contrib.pulse")
|
module("vicious.contrib.pulse")
|
||||||
|
|
||||||
|
|
||||||
-- {{{ Helper function
|
-- {{{ Helper function
|
||||||
local function get_sink_name(sink)
|
local function pacmd(args)
|
||||||
-- If no sink is specified take the first one
|
local f = io.popen("pacmd "..args)
|
||||||
if sink == nil then
|
|
||||||
local f = io.popen("pacmd list-sinks | grep name:")
|
|
||||||
local line = f:read("*all")
|
local line = f:read("*all")
|
||||||
f:close()
|
f:close()
|
||||||
|
return line
|
||||||
sink = string.match(line, "<(.*)>")
|
|
||||||
-- If sink is an index, retrieve its name
|
|
||||||
elseif type(sink) == "number" then
|
|
||||||
local f = io.popen("pacmd list-sinks | grep name:")
|
|
||||||
local line = f:read("*all")
|
|
||||||
f:close()
|
|
||||||
|
|
||||||
local sinks = {}
|
|
||||||
for s in string.gmatch(line, "<(.*)>") do
|
|
||||||
table.insert(sinks, s)
|
|
||||||
end
|
|
||||||
|
|
||||||
sink = sinks[sink]
|
|
||||||
end
|
|
||||||
|
|
||||||
return sink
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function escape(text)
|
||||||
|
local special_chars = { ["."] = "%.", ["-"] = "%-" }
|
||||||
|
return text:gsub("[%.%-]", special_chars)
|
||||||
|
end
|
||||||
|
|
||||||
|
local cached_sinks = {}
|
||||||
|
local function get_sink_name(sink)
|
||||||
|
if type(sink) == "string" then return sink end
|
||||||
|
-- avoid nil keys
|
||||||
|
local key = sink or 1
|
||||||
|
-- Cache requests
|
||||||
|
if not cached_sinks[key] then
|
||||||
|
local line = pacmd("list-sinks")
|
||||||
|
for s in string.gmatch(line, "name: <(.-)>") do
|
||||||
|
table.insert(cached_sinks, s)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return cached_sinks[key]
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
-- }}}
|
-- }}}
|
||||||
|
|
||||||
-- {{{ Pulseaudio widget type
|
-- {{{ Pulseaudio widget type
|
||||||
local function worker(format, sink)
|
local function worker(format, sink)
|
||||||
sink = get_sink_name(sink)
|
sink = get_sink_name(sink)
|
||||||
if sink == nil then return {0} end
|
if sink == nil then return {0, "unknown"} end
|
||||||
|
|
||||||
-- Get sink data
|
-- Get sink data
|
||||||
local f = io.popen("pacmd dump | grep '\\(set-sink-volume " .. sink.."\\)\\|\\(set-sink-mute "..sink.."\\)'")
|
local data = pacmd("dump")
|
||||||
local data = f:read("*all")
|
|
||||||
f:close()
|
|
||||||
|
|
||||||
-- If mute return 0 (not "Mute") so we don't break progressbars
|
-- If mute return 0 (not "Mute") so we don't break progressbars
|
||||||
if string.match(data," (yes)\n$") then
|
if string.find(data,"set%-sink%-mute "..escape(sink).." yes") then
|
||||||
return {0}
|
return {0, "off"}
|
||||||
end
|
end
|
||||||
|
|
||||||
local vol = tonumber(string.match(data, "(0x[%x]+)"))
|
local vol = tonumber(string.match(data, "set%-sink%-volume "..escape(sink).." (0x[%x]+)"))
|
||||||
if vol == nil then vol = 0 end
|
if vol == nil then vol = 0 end
|
||||||
|
|
||||||
return { vol/0x10000*100 }
|
return { vol/0x10000*100, "on"}
|
||||||
end
|
end
|
||||||
-- }}}
|
-- }}}
|
||||||
|
|
||||||
|
@ -88,17 +90,31 @@ function add(percent, sink)
|
||||||
sink = get_sink_name(sink)
|
sink = get_sink_name(sink)
|
||||||
if sink == nil then return end
|
if sink == nil then return end
|
||||||
|
|
||||||
local f = io.popen("pacmd dump | grep 'set-sink-volume " .. sink.."'")
|
local data = pacmd("dump")
|
||||||
local data = f:read("*all")
|
|
||||||
f:close()
|
local pattern = "set%-sink%-volume "..escape(sink).." (0x[%x]+)"
|
||||||
|
local initial_vol = tonumber(string.match(data, pattern))
|
||||||
|
|
||||||
local initial_vol = tonumber(string.match(data, "(0x[%x]+)"))
|
|
||||||
local vol = initial_vol + percent/100*0x10000
|
local vol = initial_vol + percent/100*0x10000
|
||||||
if vol > 0x10000 then vol = 0x10000 end
|
if vol > 0x10000 then vol = 0x10000 end
|
||||||
if vol < 0 then vol = 0 end
|
if vol < 0 then vol = 0 end
|
||||||
|
|
||||||
local cmd = "pacmd set-sink-volume "..sink..string.format(" 0x%x", vol).." >/dev/null"
|
local cmd = string.format("pacmd set-sink-volume %s 0x%x >/dev/null", sink, vol)
|
||||||
os.execute(cmd)
|
return os.execute(cmd)
|
||||||
|
end
|
||||||
|
|
||||||
|
function toggle(sink)
|
||||||
|
sink = get_sink_name(sink)
|
||||||
|
if sink == nil then return end
|
||||||
|
|
||||||
|
local data = pacmd("dump")
|
||||||
|
local pattern = "set%-sink%-mute "..escape(sink).." (%a%a%a?)"
|
||||||
|
local mute = string.match(data, pattern)
|
||||||
|
|
||||||
|
-- 0 to enable a sink or 1 to mute it.
|
||||||
|
local state = { yes = 0, no = 1}
|
||||||
|
local cmd = string.format("pacmd set-sink-mute %s %d", sink, state[mute])
|
||||||
|
return os.execute(cmd)
|
||||||
end
|
end
|
||||||
-- }}}
|
-- }}}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue