Skip to content

Commit d727af5

Browse files
committed
pactl: Do not call pactl get-* commands
Older versions of pactl, like the one shipped by Debian Bullseye, do not provide the get-* commands, like get-sink-volume or get-sink-mute. Work around them by relying on `pactl list` and `pactl info` only. This commit increases code complexity significantly and reimplementing pactl-logic in lua impairs performance compared to a modern pactl, where this workaround would not be needed. Fixes issue streetturtle#390.
1 parent ef70d16 commit d727af5

File tree

1 file changed

+50
-11
lines changed

1 file changed

+50
-11
lines changed

pactl-widget/pactl.lua

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,40 @@ function pactl.mute_toggle(device)
1616
spawn('pactl set-sink-mute ' .. device .. ' toggle', false)
1717
end
1818

19+
function pactl.get_default_sink()
20+
local line = utils.popen_and_return('pactl info'):match('Default Sink: [^\n]*')
21+
if line == nil then
22+
return "none"
23+
end
24+
25+
local t = utils.split(line, ':')
26+
return utils.trim(t[2])
27+
end
28+
1929
function pactl.get_volume(device)
20-
local stdout = utils.popen_and_return('pactl get-sink-volume ' .. device)
30+
if device == '@DEFAULT_SINK@' then
31+
device = pactl.get_default_sink()
32+
end
2133

2234
local volsum, volcnt = 0, 0
23-
for vol in string.gmatch(stdout, "(%d?%d?%d)%%") do
24-
vol = tonumber(vol)
25-
if vol ~= nil then
26-
volsum = volsum + vol
27-
volcnt = volcnt + 1
35+
local matched_sink = false
36+
for line in utils.popen_and_return('pactl list sinks'):gmatch('([^\r\n]*)[\r\n]') do
37+
local linetrim = utils.trim(line)
38+
39+
if linetrim == "Name: " .. device then
40+
matched_sink = true
41+
end
42+
43+
if matched_sink and linetrim:match("^Volume:") then
44+
45+
for vol in string.gmatch(linetrim, "(%d?%d?%d)%%") do
46+
vol = tonumber(vol)
47+
if vol ~= nil then
48+
volsum = volsum + vol
49+
volcnt = volcnt + 1
50+
end
51+
end
52+
break
2853
end
2954
end
3055

@@ -36,12 +61,26 @@ function pactl.get_volume(device)
3661
end
3762

3863
function pactl.get_mute(device)
39-
local stdout = utils.popen_and_return('pactl get-sink-mute ' .. device)
40-
if string.find(stdout, "yes") then
41-
return true
42-
else
43-
return false
64+
65+
if device == '@DEFAULT_SINK@' then
66+
device = pactl.get_default_sink()
4467
end
68+
69+
local volsum, volcnt = 0, 0
70+
local matched_sink = false
71+
for line in utils.popen_and_return('pactl list sinks'):gmatch('([^\r\n]*)[\r\n]') do
72+
local linetrim = utils.trim(line)
73+
74+
if linetrim == "Name: " .. device then
75+
matched_sink = true
76+
end
77+
78+
if matched_sink and linetrim == "Mute: yes" then
79+
return true
80+
end
81+
end
82+
83+
return false
4584
end
4685

4786
function pactl.get_sinks_and_sources()

0 commit comments

Comments
 (0)