2010-08-29 00:49:57 +02:00
|
|
|
---------------------------------------------------
|
|
|
|
-- Licensed under the GNU General Public License v2
|
2010-10-18 16:36:07 +02:00
|
|
|
-- * (c) 2010, MrMagne <mr.magne@yahoo.fr>
|
2010-12-18 16:55:23 +01:00
|
|
|
-- * (c) 2010, Mic92 <jthalheim@gmail.com>
|
2010-08-29 00:49:57 +02:00
|
|
|
---------------------------------------------------
|
|
|
|
|
|
|
|
-- {{{ Grab environment
|
|
|
|
local type = type
|
|
|
|
local tonumber = tonumber
|
|
|
|
local io = { popen = io.popen }
|
|
|
|
local setmetatable = setmetatable
|
|
|
|
local os = { execute = os.execute }
|
|
|
|
local table = { insert = table.insert }
|
|
|
|
local string = {
|
|
|
|
find = string.find,
|
|
|
|
match = string.match,
|
|
|
|
format = string.format,
|
|
|
|
gmatch = string.gmatch
|
|
|
|
}
|
2011-09-01 16:08:02 +02:00
|
|
|
local math = {
|
|
|
|
floor = math.floor
|
|
|
|
}
|
2010-08-29 00:49:57 +02:00
|
|
|
-- }}}
|
|
|
|
|
|
|
|
|
2010-12-18 16:55:23 +01:00
|
|
|
-- Pulse: provides volume levels of requested pulseaudio sinks and methods to change them
|
2012-06-15 20:48:17 +02:00
|
|
|
-- vicious.contrib.pulse
|
|
|
|
local pulse = {}
|
2010-08-29 00:49:57 +02:00
|
|
|
|
|
|
|
-- {{{ Helper function
|
2010-12-18 16:55:23 +01:00
|
|
|
local function pacmd(args)
|
|
|
|
local f = io.popen("pacmd "..args)
|
|
|
|
local line = f:read("*all")
|
|
|
|
f:close()
|
|
|
|
return line
|
|
|
|
end
|
|
|
|
|
|
|
|
local function escape(text)
|
|
|
|
local special_chars = { ["."] = "%.", ["-"] = "%-" }
|
|
|
|
return text:gsub("[%.%-]", special_chars)
|
|
|
|
end
|
|
|
|
|
|
|
|
local cached_sinks = {}
|
2010-08-29 00:49:57 +02:00
|
|
|
local function get_sink_name(sink)
|
2010-12-18 16:55:23 +01:00
|
|
|
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
|
2010-08-29 00:49:57 +02:00
|
|
|
end
|
|
|
|
|
2010-12-18 16:55:23 +01:00
|
|
|
return cached_sinks[key]
|
2010-08-29 00:49:57 +02:00
|
|
|
end
|
2010-12-18 16:55:23 +01:00
|
|
|
|
|
|
|
|
2010-08-29 00:49:57 +02:00
|
|
|
-- }}}
|
|
|
|
|
|
|
|
-- {{{ Pulseaudio widget type
|
|
|
|
local function worker(format, sink)
|
|
|
|
sink = get_sink_name(sink)
|
2010-12-18 16:55:23 +01:00
|
|
|
if sink == nil then return {0, "unknown"} end
|
2010-08-29 00:49:57 +02:00
|
|
|
|
|
|
|
-- Get sink data
|
2010-12-18 16:55:23 +01:00
|
|
|
local data = pacmd("dump")
|
2010-08-29 00:49:57 +02:00
|
|
|
|
|
|
|
-- If mute return 0 (not "Mute") so we don't break progressbars
|
2010-12-18 16:55:23 +01:00
|
|
|
if string.find(data,"set%-sink%-mute "..escape(sink).." yes") then
|
|
|
|
return {0, "off"}
|
2010-08-29 00:49:57 +02:00
|
|
|
end
|
|
|
|
|
2010-12-18 16:55:23 +01:00
|
|
|
local vol = tonumber(string.match(data, "set%-sink%-volume "..escape(sink).." (0x[%x]+)"))
|
2010-08-29 00:49:57 +02:00
|
|
|
if vol == nil then vol = 0 end
|
|
|
|
|
2011-09-01 16:08:02 +02:00
|
|
|
return { math.floor(vol/0x10000*100), "on"}
|
2010-08-29 00:49:57 +02:00
|
|
|
end
|
|
|
|
-- }}}
|
|
|
|
|
|
|
|
-- {{{ Volume control helper
|
2012-06-15 20:48:17 +02:00
|
|
|
function pulse.add(percent, sink)
|
2010-08-29 00:49:57 +02:00
|
|
|
sink = get_sink_name(sink)
|
|
|
|
if sink == nil then return end
|
|
|
|
|
2010-12-18 16:55:23 +01:00
|
|
|
local data = pacmd("dump")
|
|
|
|
|
|
|
|
local pattern = "set%-sink%-volume "..escape(sink).." (0x[%x]+)"
|
|
|
|
local initial_vol = tonumber(string.match(data, pattern))
|
2010-08-29 00:49:57 +02:00
|
|
|
|
|
|
|
local vol = initial_vol + percent/100*0x10000
|
|
|
|
if vol > 0x10000 then vol = 0x10000 end
|
|
|
|
if vol < 0 then vol = 0 end
|
|
|
|
|
2010-12-18 16:55:23 +01:00
|
|
|
local cmd = string.format("pacmd set-sink-volume %s 0x%x >/dev/null", sink, vol)
|
|
|
|
return os.execute(cmd)
|
|
|
|
end
|
|
|
|
|
2012-06-15 20:48:17 +02:00
|
|
|
function pulse.toggle(sink)
|
2010-12-18 16:55:23 +01:00
|
|
|
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)
|
2010-08-29 00:49:57 +02:00
|
|
|
end
|
|
|
|
-- }}}
|
|
|
|
|
2012-06-15 20:48:17 +02:00
|
|
|
return setmetatable(pulse, { __call = function(_, ...) return worker(...) end })
|