| 1 |
|
| 2 |
|
| 3 |
> |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
local type = type |
| 23 |
local tonumber = tonumber |
| 24 |
local io = { popen = io.popen } |
| 25 |
local setmetatable = setmetatable |
| 26 |
local os = { execute = os.execute } |
| 27 |
local table = { insert = table.insert } |
| 28 |
local string = { |
| 29 |
find = string.find, |
| 30 |
match = string.match, |
| 31 |
format = string.format, |
| 32 |
gmatch = string.gmatch |
| 33 |
} |
| 34 |
local math = { |
| 35 |
floor = math.floor, |
| 36 |
ceil = math.ceil |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
local pulse_all = {} |
| 44 |
|
| 45 |
|
| 46 |
local function pacmd(args) |
| 47 |
local f = io.popen("pacmd "..args) |
| 48 |
if f == nil then |
| 49 |
return nil |
| 50 |
else |
| 51 |
local line = f:read("*all") |
| 52 |
f:close() |
| 53 |
return line |
| 54 |
end |
| 55 |
end |
| 56 |
|
| 57 |
local function escape(text) |
| 58 |
local special_chars = { ["."] = "%.", ["-"] = "%-" } |
| 59 |
return text:gsub("[%.%-]", special_chars) |
| 60 |
end |
| 61 |
|
| 62 |
local cached_sinks = {} |
| 63 |
local function get_sink_name(sink) |
| 64 |
if type(sink) == "string" then return sink end |
| 65 |
|
| 66 |
local key = sink or 1 |
| 67 |
|
| 68 |
if not cached_sinks[key] then |
| 69 |
local line = pacmd("list-sinks") |
| 70 |
if line == nil then return nil end |
| 71 |
for s in string.gmatch(line, "name: <(.-)>") do |
| 72 |
table.insert(cached_sinks, s) |
| 73 |
end |
| 74 |
end |
| 75 |
|
| 76 |
return cached_sinks[key] |
| 77 |
end |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
local function worker(format, sink) |
| 84 |
sink = get_sink_name(sink) |
| 85 |
if sink == nil then return {0, "unknown"} end |
| 86 |
|
| 87 |
|
| 88 |
local data = pacmd("dump") |
| 89 |
if sink == nil then return {0, "unknown"} end |
| 90 |
|
| 91 |
|
| 92 |
if string.find(data,"set%-sink%-mute "..escape(sink).." yes") then |
| 93 |
return {0, "off"} |
| 94 |
end |
| 95 |
|
| 96 |
local vol = tonumber(string.match(data, "set%-sink%-volume "..escape(sink).." (0x[%x]+)")) |
| 97 |
if vol == nil then vol = 0 end |
| 98 |
volpercent = vol/0x10000*100 |
| 99 |
|
| 100 |
return { volpercent % 1 >= 0.5 and math.ceil(volpercent) or math.floor(volpercent), "on"} |
| 101 |
end |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
function pulse_all.add(percent, sink) |
| 106 |
sink = get_sink_name(sink) |
| 107 |
if sink == nil then return end |
| 108 |
|
| 109 |
local data = pacmd("dump") |
| 110 |
|
| 111 |
local pattern = "set%-sink%-volume "..escape(sink).." (0x[%x]+)" |
| 112 |
local initial_vol = tonumber(string.match(data, pattern)) |
| 113 |
|
| 114 |
local vol = initial_vol + percent/100*0x10000 |
| 115 |
if vol > 0x10000 then vol = 0x10000 end |
| 116 |
if vol < 0 then vol = 0 end |
| 117 |
|
| 118 |
vol = math.ceil(vol) |
| 119 |
|
| 120 |
local cmd = string.format("pacmd set-sink-volume %s 0x%x >/dev/null", sink, vol) |
| 121 |
return os.execute(cmd) |
| 122 |
end |
| 123 |
|
| 124 |
function pulse_all.toggle(sink) |
| 125 |
sink = get_sink_name(sink) |
| 126 |
if sink == nil then return end |
| 127 |
|
| 128 |
local data = pacmd("dump") |
| 129 |
local pattern = "set%-sink%-mute "..escape(sink).." (%a%a%a?)" |
| 130 |
local mute = string.match(data, pattern) |
| 131 |
|
| 132 |
|
| 133 |
local state = { yes = 0, no = 1} |
| 134 |
local cmd = string.format("pacmd set-sink-mute %s %d", sink, state[mute]) |
| 135 |
return os.execute(cmd) |
| 136 |
end |
| 137 |
|
| 138 |
|
| 139 |
return setmetatable(pulse_all, { __call = function(_, ...) return worker(...) end }) |