From a7fd579fd1bc6eb02433ccd72e9f54dca2615675 Mon Sep 17 00:00:00 2001 From: Xavi Garcia Mena Date: Tue, 29 Sep 2015 14:13:45 +0200 Subject: Adding the code to change the icons and label when a bluetooth headset is connected --- src/service.vala | 93 +++++++++++++++++++++++++++++++------------ src/sound-menu.vala | 12 ++++++ src/volume-control-pulse.vala | 49 +++++++++++++++++------ src/volume-control.vala | 3 ++ 4 files changed, 119 insertions(+), 38 deletions(-) (limited to 'src') diff --git a/src/service.vala b/src/service.vala index a08edf3..6215c85 100644 --- a/src/service.vala +++ b/src/service.vala @@ -52,6 +52,7 @@ public class IndicatorSound.Service: Object { this.notify["visible"].connect ( () => this.update_root_icon () ); this.volume_control = volume; + this.volume_control.bluetooth_headset_status_changed.connect (this.update_root_icon); this.accounts_service = accounts; /* If we're on the greeter, don't export */ @@ -90,6 +91,10 @@ public class IndicatorSound.Service: Object { this.volume_control.bind_property ("high-volume", menu, "show-high-volume-warning", BindingFlags.SYNC_CREATE); }); + this.menus.@foreach ( (profile, menu) => { + this.volume_control.bluetooth_headset_status_changed.connect (menu.update_volume_slider); + }); + this.sync_preferred_players (); this.settings.changed["interested-media-players"].connect ( () => { this.sync_preferred_players (); @@ -245,17 +250,7 @@ public class IndicatorSound.Service: Object { void update_root_icon () { double volume = this.volume_control.volume.volume; - string icon; - if (this.volume_control.mute || volume <= 0.0) - icon = this.mute_blocks_sound ? "audio-volume-muted-blocking-panel" : "audio-volume-muted-panel"; - else if (this.accounts_service != null && this.accounts_service.silentMode) - icon = "audio-volume-muted-panel"; - else if (volume <= 0.3) - icon = "audio-volume-low-panel"; - else if (volume <= 0.7) - icon = "audio-volume-medium-panel"; - else - icon = "audio-volume-high-panel"; + string icon = get_volume_root_icon (volume, this.volume_control.mute, volume_control.active_bluetooth_headphone); string accessible_name; if (this.volume_control.mute) { @@ -282,6 +277,66 @@ public class IndicatorSound.Service: Object { private bool notify_server_supports_sync = false; private bool block_info_notifications = false; + private string get_volume_notification_icon (double volume, bool loud, bool is_bluetooth_headset_active) { + string icon = ""; + if (is_bluetooth_headset_active) { + if (loud) { + icon = "audio-volume-high"; + } else { + if (volume <= 0.0) + icon = "audio-volume-muted"; + else if (volume <= 0.3) + icon = "audio-volume-low"; + else if (volume <= 0.7) + icon = "audio-volume-medium"; + else + icon = "audio-volume-high"; + } + } else { + if (loud) { + icon = "audio-volume-high"; + } else { + if (volume <= 0.0) + icon = "audio-volume-muted"; + else if (volume <= 0.3) + icon = "audio-volume-low"; + else if (volume <= 0.7) + icon = "audio-volume-medium"; + else + icon = "audio-volume-high"; + } + } + return icon; + } + + private string get_volume_root_icon (double volume, bool mute, bool is_bluetooth_headset_active) { + string icon; + if (is_bluetooth_headset_active) { + if (mute || volume <= 0.0) + icon = this.mute_blocks_sound ? "audio-volume-muted-blocking-panel" : "audio-volume-muted-panel"; + else if (this.accounts_service != null && this.accounts_service.silentMode) + icon = "audio-volume-muted-panel"; + else if (volume <= 0.3) + icon = "audio-volume-low-panel"; + else if (volume <= 0.7) + icon = "audio-volume-medium-panel"; + else + icon = "audio-volume-high-panel"; + } else { + if (mute || volume <= 0.0) + icon = this.mute_blocks_sound ? "audio-volume-muted-blocking-panel" : "audio-volume-muted-panel"; + else if (this.accounts_service != null && this.accounts_service.silentMode) + icon = "audio-volume-muted-panel"; + else if (volume <= 0.3) + icon = "audio-volume-low-panel"; + else if (volume <= 0.7) + icon = "audio-volume-medium-panel"; + else + icon = "audio-volume-high-panel"; + } + return icon; + } + private void update_notification () { if (!notify_server_caps_checked) { @@ -328,21 +383,7 @@ public class IndicatorSound.Service: Object { : ""; /* Choose an icon */ - unowned string icon; - if (loud) { - icon = "audio-volume-high"; - } else { - var volume = volume_control.volume.volume; - - if (volume <= 0.0) - icon = "audio-volume-muted"; - else if (volume <= 0.3) - icon = "audio-volume-low"; - else if (volume <= 0.7) - icon = "audio-volume-medium"; - else - icon = "audio-volume-high"; - } + string icon = get_volume_notification_icon (volume_control.volume.volume, loud, volume_control.active_bluetooth_headphone); /* Reset the notification */ var n = this.info_notification; diff --git a/src/sound-menu.vala b/src/sound-menu.vala index 8718162..8f63d69 100644 --- a/src/sound-menu.vala +++ b/src/sound-menu.vala @@ -71,6 +71,7 @@ public class SoundMenu: Object this.notify_handlers = new HashTable (direct_hash, direct_equal); this.greeter_players = (flags & DisplayFlags.GREETER_PLAYERS) != 0; + } ~SoundMenu () { @@ -185,6 +186,17 @@ public class SoundMenu: Object this.notify_handlers.remove (player); } + public void update_volume_slider (bool bluetooth_headset_active) { + int index = find_action (this.volume_section, "indicator.volume"); + if (index != -1) { + string label = bluetooth_headset_active ? "Volume (Bluetooth)" : "Volume"; + this.volume_section.remove (index); + this.volume_section.insert_item (index, this.create_slider_menu_item (_(label), "indicator.volume(0)", 0.0, 1.0, 0.01, + "audio-volume-low-zero-panel", + "audio-volume-high-panel")); + } + } + public Menu root; public Menu menu; Menu volume_section; diff --git a/src/volume-control-pulse.vala b/src/volume-control-pulse.vala index 87af258..9edffb7 100644 --- a/src/volume-control-pulse.vala +++ b/src/volume-control-pulse.vala @@ -87,6 +87,7 @@ public class VolumeControlPulse : VolumeControl private bool _send_next_local_volume = false; private double _account_service_volume = 0.0; private bool _active_port_headphone = false; + private bool _active_port_headphone_bluetooth = false; /** true when connected to the pulse server */ public override bool ready { get; private set; } @@ -201,18 +202,34 @@ public class VolumeControlPulse : VolumeControl this.notify_property ("is-playing"); } - /* Check if the current active port is headset/headphone */ - /* There is not easy way to check if the port is a headset/headphone besides - * checking for the port name. On touch (with the pulseaudio droid element) - * the headset/headphone port is called 'output-headset' and 'output-headphone'. - * On the desktop this is usually called 'analog-output-headphones' */ - if (i.active_port != null && - (i.active_port.name == "output-wired_headset" || - i.active_port.name == "output-wired_headphone" || - i.active_port.name == "analog-output-headphones")) { - _active_port_headphone = true; - } else { - _active_port_headphone = false; + // store the current status of the bluetooth headset + // if it changes we'll emit a signal + bool active_port_headphone_bluetooth_before = _active_port_headphone_bluetooth; + + /* Check if the current active port is headset/headphone */ + /* There is not easy way to check if the port is a headset/headphone besides + * checking for the port name. On touch (with the pulseaudio droid element) + * the headset/headphone port is called 'output-headset' and 'output-headphone'. + * On the desktop this is usually called 'analog-output-headphones' */ + if (i.active_port != null && + (i.active_port.name.contains("headset") || + i.active_port.name.contains("headphone"))) { + _active_port_headphone = true; + // check if it's a bluetooth device + var device_bus = i.proplist.gets ("device.bus"); + if (device_bus != null && device_bus == "bluetooth") { + _active_port_headphone_bluetooth = true; + + } else { + _active_port_headphone_bluetooth = false; + } + } else { + _active_port_headphone = false; + _active_port_headphone_bluetooth = false; + } + + if (_active_port_headphone_bluetooth != active_port_headphone_bluetooth_before) { + this.bluetooth_headset_status_changed (_active_port_headphone_bluetooth); } if (_pulse_use_stream_restore == false && @@ -535,6 +552,14 @@ public class VolumeControlPulse : VolumeControl } } + public override bool active_bluetooth_headphone + { + get + { + return this._active_port_headphone_bluetooth; + } + } + /* Volume operations */ private static PulseAudio.Volume double_to_volume (double vol) { diff --git a/src/volume-control.vala b/src/volume-control.vala index 6efac35..4ef2c3e 100644 --- a/src/volume-control.vala +++ b/src/volume-control.vala @@ -39,6 +39,7 @@ public abstract class VolumeControl : Object public virtual bool high_volume { get { return false; } protected set { } } public virtual bool mute { get { return false; } } public virtual bool is_playing { get { return false; } } + public virtual bool active_bluetooth_headphone { get { return false; } } private Volume _volume; public virtual Volume volume { get { return _volume; } set { } } public virtual double mic_volume { get { return 0.0; } set { } } @@ -56,4 +57,6 @@ public abstract class VolumeControl : Object v.reason = reason; this.volume = v; } + + public signal void bluetooth_headset_status_changed (bool bluetooth_headset_active); } -- cgit v1.2.3