-
Notifications
You must be signed in to change notification settings - Fork 23
/
extension.js
269 lines (244 loc) · 7.72 KB
/
extension.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
"use strict";
import GLib from "gi://GLib";
import Gio from "gi://Gio";
import St from "gi://St";
import Meta from "gi://Meta";
import Shell from "gi://Shell";
import Gvc from "gi://Gvc";
import GObject from "gi://GObject";
import Gst from "gi://Gst";
import GstAudio from "gi://GstAudio";
import * as Main from "resource:///org/gnome/shell/ui/main.js";
import * as PanelMenu from "resource:///org/gnome/shell/ui/panelMenu.js";
import { Extension } from "resource:///org/gnome/shell/extensions/extension.js";
import * as Signals from "resource:///org/gnome/shell/misc/signals.js";
const isPlayingSoundSupported = Gst != null && GstAudio != null;
const EXCLUDED_APPLICATION_IDS = [
"org.gnome.VolumeControl",
"org.PulseAudio.pavucontrol",
];
const KEYBINDING_KEY_NAME = "keybinding-toggle-mute";
const MICROPHONE_ACTIVE_STYLE_CLASS = "screencast-indicator";
let initialised = false; // flag to avoid notifications on startup
let settings = null;
let microphone;
let panel_button;
class Microphone extends Signals.EventEmitter {
constructor(dir) {
super();
this.active = null;
this.stream = null;
this.muted_changed_id = 0;
this.mixer_control = new Gvc.MixerControl({ name: "Nothing to say" });
this.mixer_control.open();
const refresh_cb = () => {
this.refresh();
};
this.mixer_control.connect("default-source-changed", refresh_cb);
this.mixer_control.connect("stream-added", refresh_cb);
this.mixer_control.connect("stream-removed", refresh_cb);
if (isPlayingSoundSupported) {
Gst.init(null);
this.on_sound = init_sound(dir, "on");
this.off_sound = init_sound(dir, "off");
}
this.refresh();
}
refresh() {
// based on gnome-shell volume control
if (this.stream && this.muted_changed_id) {
this.stream.disconnect(this.muted_changed_id);
}
let was_active = this.active;
this.active = false;
this.stream = this.mixer_control.get_default_source();
if (this.stream) {
this.muted_changed_id = this.stream.connect("notify::is-muted", () => {
this.notify_muted();
});
let recording_apps = this.mixer_control.get_source_outputs();
for (let i = 0; i < recording_apps.length; i++) {
let output_stream = recording_apps[i];
let application_id = output_stream.get_application_id();
if (EXCLUDED_APPLICATION_IDS.includes(application_id)) continue;
this.active = true;
}
}
this.notify_muted();
if (this.active != was_active) {
this.emit("notify::active");
}
}
destroy() {
this.mixer_control.close();
}
notify_muted() {
this.emit("notify::muted");
}
get muted() {
if (!this.stream) return true;
return this.stream.is_muted;
}
set muted(muted) {
if (!this.stream) return;
this.stream.change_is_muted(muted);
}
get level() {
if (!this.stream) return 0;
return this.stream.get_volume() / this.mixer_control.get_vol_max_norm();
}
}
const MicrophonePanelButton = GObject.registerClass(
{ GTypeName: "MicrophonePanelButton" },
class extends PanelMenu.Button {
_init(metadata) {
super._init(0.0, `${metadata.name} panel indicator`, false);
this.icon = new St.Icon({
icon_name: get_icon_name(false),
style_class: "system-status-icon",
});
this.add_child(this.icon);
this.connect("button-press-event", () => {
on_activate({ give_feedback: false });
});
}
},
);
function init_sound(dir, name) {
const playbin = Gst.ElementFactory.make("playbin", null);
const path = dir.get_child(`sounds/${name}.ogg`).get_path();
const uri = Gst.filename_to_uri(path);
playbin.set_property("uri", uri);
const sink = Gst.ElementFactory.make("pulsesink", "sink");
playbin.set_property("audio-sink", sink);
playbin.set_volume(GstAudio.StreamVolumeFormat.LINEAR, 0.5);
// Fix audio node suspend-on-idle; stop playback at end-of-stream
const bus = playbin.get_bus();
bus.add_signal_watch();
bus.connect('message', (_bus, msg) => {
if (msg.type === Gst.MessageType.EOS)
playbin.set_state(Gst.State.NULL);
});
return playbin;
}
function get_icon_name(muted) {
// TODO: use -low and -medium icons based on .level
return muted
? "microphone-sensitivity-muted-symbolic"
: "microphone-sensitivity-high-symbolic";
}
function icon_should_be_visible(microphone_active) {
let setting = settings.get_value("icon-visibility").unpack();
switch (setting) {
case "always":
return true;
case "never":
return false;
default:
return microphone.active; // when-recording
}
}
function show_osd(text, muted, level) {
const monitor = -1;
const icon = Gio.Icon.new_for_string(get_icon_name(muted));
Main.osdWindowManager.show(monitor, icon, text, level);
}
let mute_timeout_id = null;
function on_activate({ give_feedback }) {
if (microphone.muted) {
microphone.muted = false;
if (give_feedback) {
show_osd(null, false, microphone.level);
}
if (
isPlayingSoundSupported &&
settings.get_boolean("play-feedback-sounds")
) {
play_sound(microphone.on_sound);
}
} else {
// use a delay before muting; this makes push-to-talk work
if (mute_timeout_id) {
GLib.Source.remove(mute_timeout_id);
if (give_feedback) {
// keep osd visible
show_osd(null, false, microphone.level);
}
}
mute_timeout_id = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 100, () => {
mute_timeout_id = null;
microphone.muted = true;
if (give_feedback) {
show_osd(null, true, 0);
}
if (
isPlayingSoundSupported &&
settings.get_boolean("play-feedback-sounds")
) {
play_sound(microphone.off_sound);
}
});
}
}
function play_sound(sound) {
// Rewind in case the sound has played already.
sound.set_state(Gst.State.NULL);
sound.set_state(Gst.State.PLAYING);
}
export default class extends Extension {
enable() {
settings = this.getSettings();
microphone = new Microphone(this.dir);
panel_button = new MicrophonePanelButton(this.metadata);
panel_button.visible = icon_should_be_visible(microphone.active);
const indicatorName = `${this.metadata.name} indicator`;
Main.panel.addToStatusArea(indicatorName, panel_button, 0, "right");
microphone.connect("notify::active", () => {
if (microphone.active) {
panel_button.icon.add_style_class_name(MICROPHONE_ACTIVE_STYLE_CLASS);
} else {
panel_button.icon.remove_style_class_name(
MICROPHONE_ACTIVE_STYLE_CLASS,
);
}
panel_button.visible = icon_should_be_visible(microphone.active);
if (
settings.get_boolean("show-osd") &&
(initialised || microphone.active)
)
show_osd(
microphone.active ? "Microphone activated" : "Microphone deactivated",
microphone.muted,
);
initialised = true;
});
microphone.connect("notify::muted", () => {
panel_button.icon.icon_name = get_icon_name(microphone.muted);
});
Main.wm.addKeybinding(
KEYBINDING_KEY_NAME,
settings,
Meta.KeyBindingFlags.NONE,
Shell.ActionMode.NORMAL | Shell.ActionMode.OVERVIEW,
() => {
on_activate({ give_feedback: settings.get_boolean("show-osd") });
},
);
settings.connect("changed::icon-visibility", () => {
panel_button.visible = icon_should_be_visible(microphone.active);
});
}
disable() {
Main.wm.removeKeybinding(KEYBINDING_KEY_NAME);
Main.panel._rightBox.remove_child(panel_button);
settings = null;
microphone.destroy();
microphone = null;
panel_button.destroy();
panel_button = null;
if (mute_timeout_id) {
GLib.Source.remove(mute_timeout_id);
mute_timeout_id = null;
}
}
}