-
Notifications
You must be signed in to change notification settings - Fork 3
/
pactl.js
164 lines (137 loc) · 3.66 KB
/
pactl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// :TRICKY: any way to determine this is main/top module, Gjs devs?
if (
new Error().stack.split(/\r\n|\r|\n/g).filter(line => line.length > 0)
.length == 1
) {
imports.searchPath.unshift(".");
}
const GLib = imports.gi.GLib;
const sprintf = imports.sprintf.sprintf;
function execCmd(cmd) {
print("execCmd:", cmd);
let resList = GLib.spawn_command_line_sync(cmd);
let [res, out, err, status] = resList;
if (res != true || status != 0) {
print(
"spawn_sync failed:",
cmd,
JSON.stringify({
res,
out: out.toString(),
err: err.toString(),
status,
}),
);
return;
}
return resList;
}
function getDefaultSink() {
let failRes = [null, false];
let resList = execCmd("pactl info");
if (!resList) {
return failRes;
}
let [res, out, err, status] = resList;
// $ grep -R -C 2 --include='*.c*' "Default Sink: " .
// ./src/utils/pactl.c- "Default Sample Specification: %s\n"
// ./src/utils/pactl.c- "Default Channel Map: %s\n"
// ./src/utils/pactl.c: "Default Sink: %s\n"
// ./src/utils/pactl.c- "Default Source: %s\n"
// ./src/utils/pactl.c- "Cookie: %04x:%04x\n"),
let pattern = /^Default Sink: (.*)$/m;
let m = pattern.exec(out.toString());
if (!m || m.length < 2) {
print("Can't find default sink (sound output) via pactl");
return failRes;
}
return [m[1], true];
}
function doMute(defaultSink, isMute) {
let resList = execCmd(
sprintf("pactl set-sink-mute %s %s", defaultSink, isMute),
);
if (!resList) {
return;
}
}
function muteAudio() {
let nullUnmute = null;
let [defaultSink, ok] = getDefaultSink();
if (!ok) {
return nullUnmute;
}
let [muted, ok2] = isMuted(defaultSink);
if (!ok2) {
return nullUnmute;
}
if (muted) {
return nullUnmute;
}
doMute(defaultSink, true);
return function() {
let [defaultSink, ok] = getDefaultSink();
if (!ok) {
return;
}
doMute(defaultSink, false);
};
}
function isMuted(defaultSink) {
let failRes = [null, false];
let resList = execCmd("pactl list sinks");
if (!resList) {
return failRes;
}
let [res, out, err, status] = resList;
// $ grep -R -B 10 --include='*.c*' 'Mute: ' .
// ./src/utils/pactl.c- }
// ./src/utils/pactl.c-
// ./src/utils/pactl.c- printf(_("Sink #%u\n"
// ./src/utils/pactl.c- "\tState: %s\n"
// ./src/utils/pactl.c- "\tName: %s\n"
// ./src/utils/pactl.c- "\tDescription: %s\n"
// ./src/utils/pactl.c- "\tDriver: %s\n"
// ./src/utils/pactl.c- "\tSample Specification: %s\n"
// ./src/utils/pactl.c- "\tChannel Map: %s\n"
// ./src/utils/pactl.c- "\tOwner Module: %u\n"
// ./src/utils/pactl.c: "\tMute: %s\n"
// [\\s\\S] = /[\s\S]/ - ". + \n"
// *? - non-greedy search
let pattern = new RegExp(
sprintf("Name: %s[\\s\\S]*?\tMute: (yes|no)", defaultSink),
"m",
);
let m = pattern.exec(out.toString());
if (!m || m.length < 2) {
print("Can't find muteness via pactl");
return failRes;
}
return [m[1] == "yes", true];
}
function main() {
if (false) {
var [defaultSink, ok] = getDefaultSink();
if (!ok) {
return;
}
print(isMuted(defaultSink));
}
if (true) {
let unMuteAudio = muteAudio();
const Mainloop = imports.mainloop;
Mainloop.timeout_add(2 * 1000, function() {
if (unMuteAudio) {
unMuteAudio();
}
return GLib.SOURCE_REMOVE;
});
Mainloop.run();
}
}
if (
new Error().stack.split(/\r\n|\r|\n/g).filter(line => line.length > 0)
.length == 1
) {
main();
}