Debian Codegraph @

src/raven/widgets/sound-output/sound_output.vala

raw
1 /*
2 * This file is part of budgie-desktop
3 *
4 * Copyright Budgie Desktop Developers
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12 public class SoundOutputRavenPlugin : Budgie.RavenPlugin, Peas.ExtensionBase {
13 public Budgie.RavenWidget new_widget_instance(string uuid, GLib.Settings? settings) {
14 return new SoundOutputRavenWidget(uuid, settings);
15 }
16
17 public bool supports_settings() {
18 return true;
19 }
20 }
21
22 public class SoundOutputRavenWidget : Budgie.RavenWidget {
23 private Settings? gnome_sound_settings = null;
24 private Settings? budgie_settings = null;
25 private Settings? gnome_desktop_settings = null;
26 private ulong scale_id = 0;
27 private Gvc.MixerControl mixer = null;
28 private HashTable<uint,Gtk.ListBoxRow?> apps;
29 private HashTable<string,string?> derpers;
30 private HashTable<uint,Gtk.ListBoxRow?> devices;
31 private ulong primary_notify_id = 0;
32 private Gvc.MixerStream? primary_stream = null;
33
34 /**
35 * Signals
36 */
37 public signal void devices_state_changed(); // devices_state_changed is triggered when the amount of devices has changed
38
39 /**
40 * Widgets
41 */
42 private Gtk.Box? main_box = null;
43 private Gtk.Box? apps_area = null;
44 private Gtk.ListBox? apps_listbox = null;
45 private Gtk.Label? apps_placeholder_label = null;
46 private Gtk.ListBox? devices_list = null;
47 private Gtk.Box? header = null;
48 private Gtk.Button? header_icon = null;
49 private Gtk.Button? header_reveal_button = null;
50 private Gtk.Revealer? content_revealer = null;
51 private Gtk.Box? content = null;
52 private Gtk.Stack? widget_area = null;
53 private Gtk.StackSwitcher? widget_area_switch = null;
54 private Gtk.Scale? volume_slider = null;
55
56 public SoundOutputRavenWidget(string uuid, GLib.Settings? settings) {
57 initialize(uuid, settings);
58
59 main_box = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
60 add(main_box);
61
62 header = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0);
63 header.get_style_context().add_class("raven-header");
64 main_box.add(header);
65
66 header_icon = new Gtk.Button.from_icon_name("audio-volume-high-symbolic", Gtk.IconSize.MENU);
67 header_icon.get_style_context().add_class("flat");
68 header_icon.valign = Gtk.Align.CENTER;
69 header_icon.margin = 4;
70 header_icon.margin_start = 8;
71 header_icon.margin_end = 4;
72 header_icon.clicked.connect(() => {
73 if (primary_stream != null) {
74 primary_stream.change_is_muted(!primary_stream.get_is_muted());
75 }
76 });
77 header.add(header_icon);
78
79 content = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
80 content.get_style_context().add_class("raven-background");
81
82 content_revealer = new Gtk.Revealer();
83 content_revealer.add(content);
84 content_revealer.set_reveal_child(true);
85 main_box.add(content_revealer);
86
87 get_style_context().add_class("audio-widget");
88
89 /**
90 * Shared Logic
91 */
92 mixer = new Gvc.MixerControl("Budgie Volume Control");
93
94 mixer.card_added.connect((id) => { // When we add a card
95 devices_state_changed();
96 });
97
98 mixer.card_removed.connect((id) => { // When we remove a card
99 devices_state_changed();
100 });
101
102 derpers = new HashTable<string,string?>(str_hash, str_equal); // Create our GVC Stream app derpers
103 derpers.insert("Vivaldi", "vivaldi"); // Vivaldi
104 derpers.insert("Vivaldi Snapshot", "vivaldi-snapshot"); // Vivaldi Snapshot
105 devices = new HashTable<uint,Gtk.ListBoxRow?>(direct_hash, direct_equal);
106
107 /**
108 * Shared Construction
109 */
110 devices_list = new Gtk.ListBox();
111 devices_list.get_style_context().add_class("devices-list");
112 devices_list.get_style_context().add_class("sound-devices");
113 devices_list.selection_mode = Gtk.SelectionMode.SINGLE;
114 devices_list.row_selected.connect(on_device_selected);
115
116 volume_slider = new Gtk.Scale.with_range(Gtk.Orientation.HORIZONTAL, 0, 100, 10);
117 volume_slider.set_draw_value(false);
118 volume_slider.value_changed.connect(on_scale_change);
119 volume_slider.hexpand = true;
120 header.add(volume_slider);
121
122 header_reveal_button = new Gtk.Button.from_icon_name("pan-down-symbolic", Gtk.IconSize.MENU);
123 header_reveal_button.get_style_context().add_class("flat");
124 header_reveal_button.get_style_context().add_class("expander-button");
125 header_reveal_button.margin = 4;
126 header_reveal_button.margin_start = 2;
127 header_reveal_button.valign = Gtk.Align.CENTER;
128 header_reveal_button.clicked.connect(() => {
129 content_revealer.reveal_child = !content_revealer.child_revealed;
130 var image = (Gtk.Image?) header_reveal_button.get_image();
131 if (content_revealer.reveal_child) {
132 image.set_from_icon_name("pan-down-symbolic", Gtk.IconSize.MENU);
133 } else {
134 image.set_from_icon_name("pan-end-symbolic", Gtk.IconSize.MENU);
135 }
136 });
137 header.pack_end(header_reveal_button, false, false, 0);
138
139 gnome_sound_settings = new Settings("org.gnome.desktop.sound");
140 apps = new HashTable<uint,Gtk.ListBoxRow?>(direct_hash, direct_equal);
141 budgie_settings = new Settings("com.solus-project.budgie-panel");
142 gnome_desktop_settings = new Settings("org.gnome.desktop.interface");
143
144 mixer.default_sink_changed.connect(on_device_changed);
145 mixer.output_added.connect(on_device_added);
146 mixer.output_removed.connect(on_device_removed);
147 mixer.state_changed.connect(on_state_changed);
148 mixer.stream_added.connect(on_stream_added);
149 mixer.stream_removed.connect(on_stream_removed);
150
151 budgie_settings.changed["builtin-theme"].connect(this.update_input_draw_markers);
152 gnome_desktop_settings.changed["gtk-theme"].connect(this.update_input_draw_markers);
153
154 /**
155 * Create our designated areas, our stack, and switcher
156 * Proceed to add those items to our content
157 */
158 apps_area = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
159
160 apps_listbox = new Gtk.ListBox();
161 apps_listbox.get_style_context().add_class("apps-list");
162 apps_listbox.get_style_context().remove_class(Gtk.STYLE_CLASS_LIST); // Remove List styling
163 apps_listbox.selection_mode = Gtk.SelectionMode.NONE;
164 apps_listbox.margin_top = 10;
165 apps_listbox.margin_bottom = 10;
166 apps_listbox.set_sort_func((row1, row2) => { // Alphabetize items
167 var app_1 = ((Budgie.AppSoundControl) row1.get_child()).app_name;
168 var app_2 = ((Budgie.AppSoundControl) row2.get_child()).app_name;
169 return (strcmp(app_1, app_2) <= 0) ? -1 : 1;
170 });
171 apps_area.add(apps_listbox);
172
173 apps_placeholder_label = new Gtk.Label(_("No apps are playing audio.")) {
174 wrap = true,
175 wrap_mode = Pango.WrapMode.WORD_CHAR,
176 max_width_chars = 1,
177 justify = Gtk.Justification.CENTER,
178 margin_top = 8,
179 margin_bottom = 8,
180 hexpand = true,
181 };
182 apps_area.add(apps_placeholder_label);
183
184 widget_area = new Gtk.Stack();
185 widget_area.set_transition_duration(125); // 125ms
186 widget_area.set_transition_type(Gtk.StackTransitionType.SLIDE_LEFT_RIGHT);
187 widget_area.vhomogeneous = false;
188
189 widget_area.add_titled(apps_area, "apps", _("Apps"));
190 widget_area.add_titled(devices_list, "devices", _("Devices"));
191
192 widget_area_switch = new Gtk.StackSwitcher();
193 widget_area_switch.set_stack(widget_area);
194 widget_area_switch.set_homogeneous(true);
195
196 // Add marks when sound slider can go beyond 100%
197 settings.changed.connect(settings_updated);
198 settings_updated("allow-volume-overdrive");
199
200 content.pack_start(widget_area, false, false, 0);
201 content.pack_start(widget_area_switch, true, false, 0);
202
203 mixer.open();
204
205 /**
206 * Widget Expansion
207 */
208
209 show_all();
210 apps_listbox.hide();
211 }
212
213 /**
214 * has_devices will check if we have devices associated with this type
215 */
216 public bool has_devices() {
217 return (devices.size() != 0) && (!mixer.get_cards().is_empty());
218 }
219
220 /**
221 * on_device_added will handle when an input or output device has been added
222 */
223 private void on_device_added(uint id) {
224 if (devices.contains(id)) { // If we already have this device
225 return;
226 }
227
228 var device = mixer.lookup_output_id(id);
229
230 if (device == null) {
231 return;
232 }
233
234 if (device.card == null) {
235 return;
236 }
237
238 var card = device.card as Gvc.MixerCard;
239
240 if ("Digital Output" in device.description) return; // Digital Output switching is really jank with Gvc. Don't support it.
241
242 var box = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0);
243 box.margin_start = 6;
244 box.margin_end = 6;
245 box.margin_top = 3;
246 box.margin_bottom = 3;
247
248 var label = new Gtk.Label(null) {
249 valign = Gtk.Align.CENTER,
250 xalign = 0.0f,
251 max_width_chars = 1,
252 ellipsize = Pango.EllipsizeMode.END,
253 hexpand = true,
254 };
255 label.set_markup("%s - %s".printf(device.description, card.name));
256 box.pack_start(label, false, true, 0);
257
258 Gtk.ListBoxRow list_item = new Gtk.ListBoxRow();
259 list_item.add(box);
260
261 list_item.set_data("device_id", id);
262 devices_list.insert(list_item, -1); // Append item
263
264 devices.insert(id, list_item);
265 list_item.show_all();
266 devices_list.queue_draw();
267
268 devices_state_changed();
269 }
270
271 /**
272 * on_device_changed will handle when a Gvc.MixerUIDevice has been changed
273 */
274 private void on_device_changed(uint id) {
275 Gvc.MixerStream stream = mixer.get_default_sink(); // Set default_stream to the respective source or sink
276
277 if (stream == null) { // Our default stream is null
278 return;
279 }
280
281 if (stream == this.primary_stream) { // Didn't really change
282 return;
283 }
284
285 var device = mixer.lookup_device_from_stream(stream);
286 Gtk.ListBoxRow list_item = devices.lookup(device.get_id());
287
288 if (list_item != null) {
289 devices_list.select_row(list_item);
290 }
291
292 if (this.primary_stream != null) {
293 this.primary_stream.disconnect(this.primary_notify_id);
294 primary_notify_id = 0;
295 }
296
297 primary_notify_id = stream.notify.connect((n, p) => {
298 if (p.name == "volume" || p.name == "is-muted") {
299 update_volume();
300 }
301 });
302
303 this.primary_stream = stream;
304 update_volume();
305 devices_list.queue_draw();
306 devices_state_changed();
307 }
308
309 /**
310 * on_device_removed will handle when a Gvc.MixerUIDevice has been removed
311 */
312 private void on_device_removed(uint id) {
313 Gtk.ListBoxRow? list_item = devices.lookup(id);
314
315 if (list_item == null) {
316 return;
317 }
318
319 devices.steal(id);
320 list_item.destroy();
321 devices_list.queue_draw();
322 devices_state_changed();
323 }
324
325 /**
326 * get_control_for_app will get the respective inner AppSoundControl of a ListBoxRow associated with the id
327 */
328 private Budgie.AppSoundControl get_control_for_app(uint id) {
329 Budgie.AppSoundControl? control = null;
330
331 if (apps.contains(id)) { // Has id
332 Gtk.ListBoxRow row = apps.get(id); // Get the ListBoxRow
333
334 if (row != null) { // Row is valid
335 control = (Budgie.AppSoundControl) row.get_child();
336 }
337 }
338
339 return control;
340 }
341
342 /**
343 * on_device_selected will handle when a checkbox related to an input or output device is selected
344 */
345 private void on_device_selected(Gtk.ListBoxRow? list_item) {
346 SignalHandler.block_by_func((void*)devices_list, (void*)on_device_selected, this);
347 uint id = list_item.get_data("device_id");
348 var device = mixer.lookup_output_id(id);
349
350 if (device != null) {
351 mixer.change_output(device);
352 }
353 SignalHandler.unblock_by_func((void*)devices_list, (void*)on_device_selected, this);
354 }
355
356 /**
357 * When our volume slider has changed
358 */
359 private void on_scale_change() {
360 if (primary_stream == null) return;
361
362 if (primary_stream.set_volume((uint32) volume_slider.get_value())) {
363 Gvc.push_volume(primary_stream);
364 }
365 }
366
367 /**
368 * on_state_changed will handle when the state of our Mixer or its streams have changed
369 */
370 private void on_state_changed(uint id) {
371 var stream = mixer.lookup_stream_id(id);
372
373 if ((stream != null) && (stream.get_card_index() == -1)) { // If this is a stream (and not a card)
374 if (apps.contains(id)) { // If our apps contains this stream
375 Budgie.AppSoundControl? control = get_control_for_app(id);
376
377 if (control != null) {
378 if (stream.is_running()) { // If running
379 control.refresh(); // Update our control
380 } else { // If not running
381 control.destroy();
382 apps.steal(id);
383 }
384 }
385 }
386 }
387
388 devices_state_changed();
389 }
390
391 /**
392 * on_stream_added will handle when a stream (like an application) has been added
393 */
394 private void on_stream_added(uint id) {
395 Gvc.MixerStream stream = mixer.lookup_stream_id(id); // Get our stream
396
397 if ((stream != null) && (stream.get_card_index() == -1)) { // If this isn't a card
398 string name = stream.get_name();
399 string icon = stream.get_icon_name();
400
401 if (name == null) { // If this does not have a stream name (unlike bell-window-system, for example)
402 return;
403 }
404
405 if (stream.is_event_stream) { // If this is an event stream, such as volume change sounds
406 return;
407 }
408
409 if (stream.get_volume() == 100) { // If volume doesn't match with mixer volume
410 return;
411 }
412
413 if ((icon != "") && icon.contains("audio-input-")) { // If this is a microphone (for instances when WebRTC engine returns as input)
414 return;
415 }
416
417 if (name == "System Sounds") { // If this is System Sounds
418 return;
419 }
420
421 Gvc.MixerUIDevice device = mixer.lookup_device_from_stream(stream); // Get the associated device for this
422
423 if (device != null && !device.is_output()) { // If this is an input device
424 return;
425 }
426
427 if (derpers.contains(name)) { // If our Gvc Stream derpers contains this application
428 icon = derpers.get(name); // Use its designated icon instead
429 }
430
431 if (name == "AudioIPC Server") { // Firefox reports as AudioIPC Server
432 icon = "firefox";
433 name = "Firefox";
434 } else if (name == "WEBRTC VoiceEngine") { // Discord reports as WEBRTC VoiceEngine
435 icon = "discord";
436 name = "Discord";
437 }
438
439 Budgie.AppSoundControl control = new Budgie.AppSoundControl(mixer, primary_stream, stream, icon, name); // Pass our Mixer, Stream, correct Icon and Name
440
441 if (control != null) {
442 var list_row = new Gtk.ListBoxRow();
443 list_row.activatable = false;
444 list_row.add(control); // Add our control
445
446 apps_listbox.insert(list_row, -1); // Add our control
447 apps.insert(id, list_row); // Add to apps
448 apps_placeholder_label.hide();
449 apps_listbox.show();
450 apps_listbox.show_all();
451
452 Gvc.ChannelMap channel_map = stream.get_channel_map(); // Get the channel map for this stream
453
454 if (channel_map != null) { // Valid channel map
455 channel_map.volume_changed.connect(() => { // On volume change on channel map
456 control.refresh_volume(); // Refresh the volume
457 });
458 }
459 }
460 }
461 }
462
463 /**
464 * on_stream_removed will handle when a stream (like an application) has been removed
465 */
466 private void on_stream_removed(uint id) {
467 if (apps.contains(id)) { // If this stream exists in apps
468 Gtk.ListBoxRow row = apps.get(id);
469
470 if (row != null) { // If this row exists
471 apps_listbox.remove(row); // Remove row from listbox
472 }
473
474 apps.steal(id); // Remove the apps
475
476 if (apps_listbox.get_children().is_empty()) {
477 apps_listbox.hide();
478 apps_area.show();
479 }
480 }
481 }
482
483 /*
484 * set_slider_range_on_max will set the slider range based on whether or not we are allowing overdrive
485 */
486 private void set_slider_range_on_max(bool allow_overdrive) {
487 var current_volume = volume_slider.get_value();
488 var vol_max = mixer.get_vol_max_norm();
489 var vol_max_above = mixer.get_vol_max_amplified();
490 var step_size = (allow_overdrive) ? vol_max_above / 20 : vol_max / 20;
491
492 int slider_start = 0;
493 int slider_end = 0;
494 volume_slider.get_slider_range(out slider_start, out slider_end);
495
496 if (allow_overdrive && (slider_end != vol_max_above)) { // If we're allowing higher than max and currently slider is not a max of 150
497 volume_slider.set_increments(step_size, step_size);
498 volume_slider.set_range(0, vol_max_above);
499 volume_slider.set_value(current_volume);
500 } else if (!allow_overdrive && (slider_end != vol_max)) { // If we're not allowing higher than max and slider is at max
501 volume_slider.set_increments(step_size, step_size);
502 volume_slider.set_range(0, vol_max);
503 volume_slider.set_value(current_volume);
504 }
505
506 this.update_input_draw_markers();
507 }
508
509 /**
510 * update_input_draw_markers will update our draw markers
511 */
512 private void update_input_draw_markers() {
513 bool builtin_enabled = budgie_settings.get_boolean("builtin-theme");
514 string current_theme = gnome_desktop_settings.get_string("gtk-theme");
515 bool supported_theme = (current_theme.index_of("Arc") == -1);
516
517 if (!builtin_enabled && supported_theme) { // If built-in theme is disabled
518 bool allow_higher_than_max = get_instance_settings().get_boolean("allow-volume-overdrive");
519
520 if (allow_higher_than_max) { // If overdrive is enabled and thus should show mark
521 var vol_max = mixer.get_vol_max_norm();
522 volume_slider.add_mark(vol_max, Gtk.PositionType.BOTTOM, "");
523 } else { // If we should not show markets
524 volume_slider.clear_marks();
525 }
526 } else {
527 volume_slider.clear_marks(); // Ensure we have no marks
528 }
529 }
530
531 /**
532 * update_volume will handle updating our volume slider and output header during device change
533 */
534 private void update_volume() {
535 var vol = primary_stream.get_volume();
536 var vol_max = mixer.get_vol_max_norm();
537
538 if (get_instance_settings().get_boolean("allow-volume-overdrive")) { // Allowing max
539 vol_max = mixer.get_vol_max_amplified();
540 }
541
542 /* Same maths as computed by volume.js in gnome-shell, carried over
543 * from C->Vala port of budgie-panel */
544 int n = (int) Math.floor(3*vol/vol_max)+1;
545 string image_name;
546
547 // Work out an icon
548 string icon_prefix = "audio-volume";
549
550 if (primary_stream.get_is_muted() || vol <= 0) {
551 image_name = "muted";
552 } else {
553 switch (n) {
554 case 1:
555 image_name = "low";
556 break;
557 case 2:
558 image_name = "medium";
559 break;
560 default:
561 image_name = "high";
562 break;
563 }
564 }
565
566 var header_image = (Gtk.Image?) header_icon.get_image();
567 header_image.set_from_icon_name("%s-%s-symbolic".printf(icon_prefix, image_name), Gtk.IconSize.MENU);
568
569 /* Each scroll increments by 5%, much better than units..*/
570 var step_size = vol_max / 20;
571
572 if (scale_id > 0) {
573 SignalHandler.block(volume_slider, scale_id);
574 }
575
576 volume_slider.set_increments(step_size, step_size);
577 volume_slider.set_range(0, vol_max);
578 volume_slider.set_value(vol);
579
580 if (scale_id > 0) {
581 SignalHandler.unblock(volume_slider, scale_id);
582 }
583 }
584
585 private void settings_updated(string key) {
586 if (key == "allow-volume-overdrive") {
587 set_slider_range_on_max(get_instance_settings().get_boolean(key));
588 }
589 }
590
591 public override Gtk.Widget build_settings_ui() {
592 return new SoundOutputRavenWidgetSettings(get_instance_settings());
593 }
594 }
595
596 [GtkTemplate (ui="/org/buddiesofbudgie/budgie-desktop/raven/widget/SoundOutput/settings.ui")]
597 public class SoundOutputRavenWidgetSettings : Gtk.Grid {
598 [GtkChild]
599 private unowned Gtk.Switch? switch_allow_volume_overdrive;
600
601 public SoundOutputRavenWidgetSettings(Settings? settings) {
602 settings.bind("allow-volume-overdrive", switch_allow_volume_overdrive, "active", SettingsBindFlags.DEFAULT);
603 }
604 }
605
606 [ModuleInit]
607 public void peas_register_types(TypeModule module) {
608 // boilerplate - all modules need this
609 var objmodule = module as Peas.ObjectModule;
610 objmodule.register_extension_type(typeof(Budgie.RavenPlugin), typeof(SoundOutputRavenPlugin));
611 }