| 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 SoundInputRavenPlugin : Budgie.RavenPlugin, Peas.ExtensionBase { |
| 13 |
public Budgie.RavenWidget new_widget_instance(string uuid, GLib.Settings? settings) { |
| 14 |
return new SoundInputRavenWidget(uuid, settings); |
| 15 |
} |
| 16 |
|
| 17 |
public bool supports_settings() { |
| 18 |
return true; |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
public class SoundInputRavenWidget : Budgie.RavenWidget { |
| 23 |
private Settings? budgie_settings = null; |
| 24 |
private ulong scale_id = 0; |
| 25 |
private Gvc.MixerControl mixer = null; |
| 26 |
private HashTable<string,string?> derpers; |
| 27 |
private HashTable<uint,Gtk.ListBoxRow?> devices; |
| 28 |
private ulong primary_notify_id = 0; |
| 29 |
private Gvc.MixerStream? primary_stream = null; |
| 30 |
|
| 31 |
/** |
| 32 |
* Signals |
| 33 |
*/ |
| 34 |
public signal void devices_state_changed(); // devices_state_changed is triggered when the amount of devices has changed |
| 35 |
|
| 36 |
/** |
| 37 |
* Widgets |
| 38 |
*/ |
| 39 |
private Gtk.Box? main_box = null; |
| 40 |
private Gtk.ListBox? devices_list = null; |
| 41 |
private Gtk.Box? header = null; |
| 42 |
private Gtk.Button? header_icon = null; |
| 43 |
private Gtk.Button? header_reveal_button = null; |
| 44 |
private Gtk.Revealer? content_revealer = null; |
| 45 |
private Gtk.Box? content = null; |
| 46 |
private Gtk.Scale? volume_slider = null; |
| 47 |
|
| 48 |
public SoundInputRavenWidget(string uuid, GLib.Settings? settings) { |
| 49 |
initialize(uuid, settings); |
| 50 |
|
| 51 |
main_box = new Gtk.Box(Gtk.Orientation.VERTICAL, 0); |
| 52 |
add(main_box); |
| 53 |
|
| 54 |
header = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0); |
| 55 |
header.get_style_context().add_class("raven-header"); |
| 56 |
main_box.add(header); |
| 57 |
|
| 58 |
header_icon = new Gtk.Button.from_icon_name("microphone-sensitivity-muted-symbolic", Gtk.IconSize.MENU); |
| 59 |
header_icon.get_style_context().add_class("flat"); |
| 60 |
header_icon.valign = Gtk.Align.CENTER; |
| 61 |
header_icon.margin = 4; |
| 62 |
header_icon.margin_start = 8; |
| 63 |
header_icon.margin_end = 4; |
| 64 |
header_icon.clicked.connect(() => { |
| 65 |
if (primary_stream != null) { |
| 66 |
primary_stream.change_is_muted(!primary_stream.get_is_muted()); |
| 67 |
} |
| 68 |
}); |
| 69 |
header.add(header_icon); |
| 70 |
|
| 71 |
content = new Gtk.Box(Gtk.Orientation.VERTICAL, 0); |
| 72 |
content.get_style_context().add_class("raven-background"); |
| 73 |
|
| 74 |
content_revealer = new Gtk.Revealer(); |
| 75 |
content_revealer.add(content); |
| 76 |
main_box.add(content_revealer); |
| 77 |
|
| 78 |
get_style_context().add_class("audio-widget"); |
| 79 |
|
| 80 |
/** |
| 81 |
* Shared Logic |
| 82 |
*/ |
| 83 |
mixer = new Gvc.MixerControl("Budgie Volume Control"); |
| 84 |
|
| 85 |
mixer.card_added.connect((id) => { // When we add a card |
| 86 |
devices_state_changed(); |
| 87 |
}); |
| 88 |
|
| 89 |
mixer.card_removed.connect((id) => { // When we remove a card |
| 90 |
devices_state_changed(); |
| 91 |
}); |
| 92 |
|
| 93 |
derpers = new HashTable<string,string?>(str_hash, str_equal); // Create our GVC Stream app derpers |
| 94 |
derpers.insert("Vivaldi", "vivaldi"); // Vivaldi |
| 95 |
derpers.insert("Vivaldi Snapshot", "vivaldi-snapshot"); // Vivaldi Snapshot |
| 96 |
devices = new HashTable<uint,Gtk.ListBoxRow?>(direct_hash, direct_equal); |
| 97 |
|
| 98 |
/** |
| 99 |
* Shared Construction |
| 100 |
*/ |
| 101 |
devices_list = new Gtk.ListBox(); |
| 102 |
devices_list.get_style_context().add_class("devices-list"); |
| 103 |
devices_list.get_style_context().add_class("sound-devices"); |
| 104 |
devices_list.selection_mode = Gtk.SelectionMode.SINGLE; |
| 105 |
devices_list.row_selected.connect(on_device_selected); |
| 106 |
|
| 107 |
volume_slider = new Gtk.Scale.with_range(Gtk.Orientation.HORIZONTAL, 0, 100, 10); |
| 108 |
volume_slider.set_draw_value(false); |
| 109 |
volume_slider.value_changed.connect(on_scale_change); |
| 110 |
volume_slider.hexpand = true; |
| 111 |
header.add(volume_slider); |
| 112 |
|
| 113 |
header_reveal_button = new Gtk.Button.from_icon_name("pan-end-symbolic", Gtk.IconSize.MENU); |
| 114 |
header_reveal_button.get_style_context().add_class("flat"); |
| 115 |
header_reveal_button.get_style_context().add_class("expander-button"); |
| 116 |
header_reveal_button.margin = 4; |
| 117 |
header_reveal_button.valign = Gtk.Align.CENTER; |
| 118 |
header_reveal_button.clicked.connect(() => { |
| 119 |
content_revealer.reveal_child = !content_revealer.child_revealed; |
| 120 |
var image = (Gtk.Image?) header_reveal_button.get_image(); |
| 121 |
if (content_revealer.reveal_child) { |
| 122 |
image.set_from_icon_name("pan-down-symbolic", Gtk.IconSize.MENU); |
| 123 |
} else { |
| 124 |
image.set_from_icon_name("pan-end-symbolic", Gtk.IconSize.MENU); |
| 125 |
} |
| 126 |
}); |
| 127 |
header.pack_end(header_reveal_button, false, false, 0); |
| 128 |
|
| 129 |
budgie_settings = new Settings("com.solus-project.budgie-panel"); |
| 130 |
|
| 131 |
mixer.default_source_changed.connect(on_device_changed); |
| 132 |
mixer.state_changed.connect(on_state_changed); |
| 133 |
mixer.input_added.connect(on_device_added); |
| 134 |
mixer.input_removed.connect(on_device_removed); |
| 135 |
|
| 136 |
content.pack_start(devices_list, false, false, 0); // Add devices directly to layout |
| 137 |
|
| 138 |
// Add marks when sound slider can go beyond 100% |
| 139 |
settings.changed.connect(settings_updated); |
| 140 |
settings_updated("allow-volume-overdrive"); |
| 141 |
|
| 142 |
mixer.open(); |
| 143 |
|
| 144 |
/** |
| 145 |
* Widget Expansion |
| 146 |
*/ |
| 147 |
|
| 148 |
show_all(); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* has_devices will check if we have devices associated with this type |
| 153 |
*/ |
| 154 |
public bool has_devices() { |
| 155 |
return (devices.size() != 0) && (!mixer.get_cards().is_empty()); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* on_device_added will handle when an input or output device has been added |
| 160 |
*/ |
| 161 |
private void on_device_added(uint id) { |
| 162 |
if (devices.contains(id)) { // If we already have this device |
| 163 |
return; |
| 164 |
} |
| 165 |
|
| 166 |
var device = mixer.lookup_input_id(id); |
| 167 |
|
| 168 |
if (device == null) { |
| 169 |
return; |
| 170 |
} |
| 171 |
|
| 172 |
if (device.card == null) { |
| 173 |
return; |
| 174 |
} |
| 175 |
|
| 176 |
var card = device.card as Gvc.MixerCard; |
| 177 |
|
| 178 |
var box = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0); |
| 179 |
box.margin_start = 6; |
| 180 |
box.margin_end = 6; |
| 181 |
box.margin_top = 3; |
| 182 |
box.margin_bottom = 3; |
| 183 |
|
| 184 |
var label = new Gtk.Label("%s - %s".printf(device.description, card.name)) { |
| 185 |
valign = Gtk.Align.CENTER, |
| 186 |
xalign = 0.0f, |
| 187 |
max_width_chars = 1, |
| 188 |
ellipsize = Pango.EllipsizeMode.END, |
| 189 |
hexpand = true, |
| 190 |
}; |
| 191 |
box.pack_start(label, false, true, 0); |
| 192 |
|
| 193 |
Gtk.ListBoxRow list_item = new Gtk.ListBoxRow(); |
| 194 |
list_item.add(box); |
| 195 |
|
| 196 |
list_item.set_data("device_id", id); |
| 197 |
devices_list.insert(list_item, -1); // Append item |
| 198 |
|
| 199 |
devices.insert(id, list_item); |
| 200 |
list_item.show_all(); |
| 201 |
devices_list.queue_draw(); |
| 202 |
|
| 203 |
devices_state_changed(); |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* on_device_changed will handle when a Gvc.MixerUIDevice has been changed |
| 208 |
*/ |
| 209 |
private void on_device_changed(uint id) { |
| 210 |
Gvc.MixerStream stream = mixer.get_default_source(); // Set default_stream to the respective source or sink |
| 211 |
|
| 212 |
if (stream == null) { // Our default stream is null |
| 213 |
return; |
| 214 |
} |
| 215 |
|
| 216 |
if (stream == this.primary_stream) { // Didn't really change |
| 217 |
return; |
| 218 |
} |
| 219 |
|
| 220 |
var device = mixer.lookup_device_from_stream(stream); |
| 221 |
Gtk.ListBoxRow list_item = devices.lookup(device.get_id()); |
| 222 |
|
| 223 |
if (list_item != null) { |
| 224 |
devices_list.select_row(list_item); |
| 225 |
} |
| 226 |
|
| 227 |
if (this.primary_stream != null) { |
| 228 |
this.primary_stream.disconnect(this.primary_notify_id); |
| 229 |
primary_notify_id = 0; |
| 230 |
} |
| 231 |
|
| 232 |
primary_notify_id = stream.notify.connect((n, p) => { |
| 233 |
if (p.name == "volume" || p.name == "is-muted") { |
| 234 |
update_volume(); |
| 235 |
} |
| 236 |
}); |
| 237 |
|
| 238 |
this.primary_stream = stream; |
| 239 |
update_volume(); |
| 240 |
devices_list.queue_draw(); |
| 241 |
devices_state_changed(); |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* on_device_removed will handle when a Gvc.MixerUIDevice has been removed |
| 246 |
*/ |
| 247 |
private void on_device_removed(uint id) { |
| 248 |
Gtk.ListBoxRow? list_item = devices.lookup(id); |
| 249 |
|
| 250 |
if (list_item == null) { |
| 251 |
return; |
| 252 |
} |
| 253 |
|
| 254 |
devices.steal(id); |
| 255 |
list_item.destroy(); |
| 256 |
devices_list.queue_draw(); |
| 257 |
devices_state_changed(); |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* on_device_selected will handle when a checkbox related to an input or output device is selected |
| 262 |
*/ |
| 263 |
private void on_device_selected(Gtk.ListBoxRow? list_item) { |
| 264 |
SignalHandler.block_by_func((void*)devices_list, (void*)on_device_selected, this); |
| 265 |
uint id = list_item.get_data("device_id"); |
| 266 |
var device = mixer.lookup_input_id(id); |
| 267 |
|
| 268 |
if (device != null) { |
| 269 |
mixer.change_input(device); |
| 270 |
} |
| 271 |
SignalHandler.unblock_by_func((void*)devices_list, (void*)on_device_selected, this); |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* When our volume slider has changed |
| 276 |
*/ |
| 277 |
private void on_scale_change() { |
| 278 |
if (primary_stream == null || primary_stream.get_is_muted()) { |
| 279 |
return; |
| 280 |
} |
| 281 |
|
| 282 |
if (primary_stream.set_volume((uint32)volume_slider.get_value())) { |
| 283 |
Gvc.push_volume(primary_stream); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* on_state_changed will handle when the state of our Mixer or its streams have changed |
| 289 |
*/ |
| 290 |
private void on_state_changed(uint id) { |
| 291 |
devices_state_changed(); |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* update_volume will handle updating our volume slider and output header during device change |
| 296 |
*/ |
| 297 |
private void update_volume() { |
| 298 |
var vol = primary_stream.get_volume(); |
| 299 |
var vol_max = mixer.get_vol_max_norm(); |
| 300 |
|
| 301 |
/* Same maths as computed by volume.js in gnome-shell, carried over |
| 302 |
* from C->Vala port of budgie-panel */ |
| 303 |
int n = (int) Math.floor(3*vol/vol_max)+1; |
| 304 |
string image_name; |
| 305 |
|
| 306 |
// Work out an icon |
| 307 |
string icon_prefix = "microphone-sensitivity"; |
| 308 |
|
| 309 |
if (primary_stream.get_is_muted() || vol <= 0) { |
| 310 |
image_name = "muted"; |
| 311 |
} else { |
| 312 |
switch (n) { |
| 313 |
case 1: |
| 314 |
image_name = "low"; |
| 315 |
break; |
| 316 |
case 2: |
| 317 |
image_name = "medium"; |
| 318 |
break; |
| 319 |
default: |
| 320 |
image_name = "high"; |
| 321 |
break; |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
var header_image = (Gtk.Image?) header_icon.get_image(); |
| 326 |
header_image.set_from_icon_name("%s-%s-symbolic".printf(icon_prefix, image_name), Gtk.IconSize.MENU); |
| 327 |
|
| 328 |
if (scale_id > 0) { |
| 329 |
SignalHandler.block(volume_slider, scale_id); |
| 330 |
} |
| 331 |
|
| 332 |
if (scale_id > 0) { |
| 333 |
SignalHandler.unblock(volume_slider, scale_id); |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
/* |
| 338 |
* set_slider_range_on_max will set the slider range based on whether or not we are allowing overdrive |
| 339 |
*/ |
| 340 |
private void set_slider_range_on_max(bool allow_overdrive) { |
| 341 |
var current_volume = volume_slider.get_value(); |
| 342 |
var vol_max = mixer.get_vol_max_norm(); |
| 343 |
var vol_max_above = mixer.get_vol_max_amplified(); |
| 344 |
var step_size = vol_max / 20; |
| 345 |
|
| 346 |
int slider_start = 0; |
| 347 |
int slider_end = 0; |
| 348 |
volume_slider.get_slider_range(out slider_start, out slider_end); |
| 349 |
|
| 350 |
if (allow_overdrive && (slider_end != vol_max_above)) { // If we're allowing higher than max and currently slider is not a max of 150 |
| 351 |
volume_slider.set_increments(step_size, step_size); |
| 352 |
volume_slider.set_range(0, vol_max_above); |
| 353 |
volume_slider.set_value(current_volume); |
| 354 |
} else if (!allow_overdrive && (slider_end != vol_max)) { // If we're not allowing higher than max and slider is at max |
| 355 |
volume_slider.set_increments(step_size, step_size); |
| 356 |
volume_slider.set_range(0, vol_max); |
| 357 |
volume_slider.set_value(current_volume); |
| 358 |
} |
| 359 |
|
| 360 |
update_input_draw_markers(); |
| 361 |
} |
| 362 |
|
| 363 |
/** |
| 364 |
* update_input_draw_markers will update our draw markers |
| 365 |
*/ |
| 366 |
private void update_input_draw_markers() { |
| 367 |
bool allow_higher_than_max = get_instance_settings().get_boolean("allow-volume-overdrive"); |
| 368 |
|
| 369 |
if (allow_higher_than_max) { // If overdrive is enabled and thus should show mark |
| 370 |
var vol_max = mixer.get_vol_max_norm(); |
| 371 |
volume_slider.add_mark(vol_max, Gtk.PositionType.BOTTOM, ""); |
| 372 |
} else { // If we should not show markets |
| 373 |
volume_slider.clear_marks(); |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
private void settings_updated(string key) { |
| 378 |
if (key == "allow-volume-overdrive") { |
| 379 |
set_slider_range_on_max(get_instance_settings().get_boolean(key)); |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
public override Gtk.Widget build_settings_ui() { |
| 384 |
return new SoundInputRavenWidgetSettings(get_instance_settings()); |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
[GtkTemplate (ui="/org/buddiesofbudgie/budgie-desktop/raven/widget/SoundInput/settings.ui")] |
| 389 |
public class SoundInputRavenWidgetSettings : Gtk.Grid { |
| 390 |
[GtkChild] |
| 391 |
private unowned Gtk.Switch? switch_allow_volume_overdrive; |
| 392 |
|
| 393 |
public SoundInputRavenWidgetSettings(Settings? settings) { |
| 394 |
settings.bind("allow-volume-overdrive", switch_allow_volume_overdrive, "active", SettingsBindFlags.DEFAULT); |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
[ModuleInit] |
| 399 |
public void peas_register_types(TypeModule module) { |
| 400 |
// boilerplate - all modules need this |
| 401 |
var objmodule = module as Peas.ObjectModule; |
| 402 |
objmodule.register_extension_type(typeof(Budgie.RavenPlugin), typeof(SoundInputRavenPlugin)); |
| 403 |
} |