From 33a73fad65c52fc324ad3d35a6d1305a1489958d Mon Sep 17 00:00:00 2001 From: Xavi Garcia Mena Date: Thu, 1 Oct 2015 09:55:22 +0200 Subject: Changed to show a notification when the active output changes --- src/CMakeLists.txt | 1 + src/service.vala | 109 +++++++++++++++++++++++++++--------------- src/sound-menu.vala | 15 +++++- src/volume-control-pulse.vala | 76 ++++++++++++++++------------- src/volume-control.vala | 10 +++- 5 files changed, 135 insertions(+), 76 deletions(-) (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 194dfc9..a0f458d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -103,6 +103,7 @@ vala_add(indicator-sound-service sound-menu.vala DEPENDS media-player + volume-control ) vala_add(indicator-sound-service accounts-service-user.vala diff --git a/src/service.vala b/src/service.vala index 6215c85..a1850d0 100644 --- a/src/service.vala +++ b/src/service.vala @@ -52,7 +52,8 @@ 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.volume_control.active_output_changed.connect (this.update_root_icon); + this.volume_control.active_output_changed.connect (this.update_notification); this.accounts_service = accounts; /* If we're on the greeter, don't export */ @@ -92,7 +93,7 @@ public class IndicatorSound.Service: Object { }); this.menus.@foreach ( (profile, menu) => { - this.volume_control.bluetooth_headset_status_changed.connect (menu.update_volume_slider); + this.volume_control.active_output_changed.connect (menu.update_volume_slider); }); this.sync_preferred_players (); @@ -250,7 +251,7 @@ public class IndicatorSound.Service: Object { void update_root_icon () { double volume = this.volume_control.volume.volume; - string icon = get_volume_root_icon (volume, this.volume_control.mute, volume_control.active_bluetooth_headphone); + string icon = get_volume_root_icon (volume, this.volume_control.mute, volume_control.active_output); string accessible_name; if (this.volume_control.mute) { @@ -277,12 +278,12 @@ 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) { + private string get_volume_icon (double volume, VolumeControl.ActiveOutput active_output) + { string icon = ""; - if (is_bluetooth_headset_active) { - if (loud) { - icon = "audio-volume-high"; - } else { + switch (active_output) + { + case VolumeControl.ActiveOutput.SPEAKERS: if (volume <= 0.0) icon = "audio-volume-muted"; else if (volume <= 0.3) @@ -291,11 +292,18 @@ public class IndicatorSound.Service: Object { icon = "audio-volume-medium"; else icon = "audio-volume-high"; - } - } else { - if (loud) { - icon = "audio-volume-high"; - } else { + break; + case VolumeControl.ActiveOutput.HEADPHONES: + 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"; + break; + case VolumeControl.ActiveOutput.BLUETOOTH_HEADPHONES: if (volume <= 0.0) icon = "audio-volume-muted"; else if (volume <= 0.3) @@ -304,35 +312,60 @@ public class IndicatorSound.Service: Object { icon = "audio-volume-medium"; else icon = "audio-volume-high"; + break; + } + return icon; + } + + private string get_volume_notification_icon (double volume, bool loud, VolumeControl.ActiveOutput active_output) { + string icon = ""; + if (loud) { + switch (active_output) + { + case VolumeControl.ActiveOutput.SPEAKERS: + icon = "audio-volume-high"; + break; + case VolumeControl.ActiveOutput.HEADPHONES: + icon = "audio-volume-high"; + break; + case VolumeControl.ActiveOutput.BLUETOOTH_HEADPHONES: + icon = "audio-volume-high"; + break; } + } else { + icon = get_volume_icon (volume, active_output); } 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"; + private string get_volume_root_icon (double volume, bool mute, VolumeControl.ActiveOutput active_output) { + string icon = ""; + switch (active_output) + { + case VolumeControl.ActiveOutput.SPEAKERS: + 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 + icon = get_volume_icon (volume, active_output); + break; + case VolumeControl.ActiveOutput.HEADPHONES: + 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 + icon = get_volume_icon (volume, active_output); + break; + case VolumeControl.ActiveOutput.BLUETOOTH_HEADPHONES: + 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 + icon = get_volume_icon (volume, active_output); + break; } return icon; } @@ -383,7 +416,7 @@ public class IndicatorSound.Service: Object { : ""; /* Choose an icon */ - string icon = get_volume_notification_icon (volume_control.volume.volume, loud, volume_control.active_bluetooth_headphone); + string icon = get_volume_notification_icon (volume_control.volume.volume, loud, volume_control.active_output); /* Reset the notification */ var n = this.info_notification; diff --git a/src/sound-menu.vala b/src/sound-menu.vala index 8f63d69..b4e3e2a 100644 --- a/src/sound-menu.vala +++ b/src/sound-menu.vala @@ -186,10 +186,21 @@ public class SoundMenu: Object this.notify_handlers.remove (player); } - public void update_volume_slider (bool bluetooth_headset_active) { + public void update_volume_slider (VolumeControl.ActiveOutput active_output) { int index = find_action (this.volume_section, "indicator.volume"); if (index != -1) { - string label = bluetooth_headset_active ? "Volume (Bluetooth)" : "Volume"; + string label = "Volume"; + switch (active_output) { + case VolumeControl.ActiveOutput.SPEAKERS: + label = "Volume"; + break; + case VolumeControl.ActiveOutput.HEADPHONES: + label = "Volume (Headphones)"; + break; + case VolumeControl.ActiveOutput.BLUETOOTH_HEADPHONES: + label = "Volume (Bluetooth)"; + break; + } 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", diff --git a/src/volume-control-pulse.vala b/src/volume-control-pulse.vala index 9edffb7..54fa18a 100644 --- a/src/volume-control-pulse.vala +++ b/src/volume-control-pulse.vala @@ -202,34 +202,38 @@ public class VolumeControlPulse : VolumeControl this.notify_property ("is-playing"); } - // 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); + // store the current status of the active output + VolumeControl.ActiveOutput active_output_before = active_output; + + /* 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; + } + + VolumeControl.ActiveOutput active_output_now = active_output; + if (active_output_now != active_output_before) { + this.active_output_changed (active_output_now); + if (active_output_now == VolumeControl.ActiveOutput.SPEAKERS) { + _high_volume_approved = false; + } + update_high_volume(); } if (_pulse_use_stream_restore == false && @@ -552,12 +556,16 @@ public class VolumeControlPulse : VolumeControl } } - public override bool active_bluetooth_headphone - { - get - { - return this._active_port_headphone_bluetooth; - } + public override VolumeControl.ActiveOutput active_output + { + get + { + if (_active_port_headphone_bluetooth) + return VolumeControl.ActiveOutput.BLUETOOTH_HEADPHONES; + if (_active_port_headphone) + return VolumeControl.ActiveOutput.HEADPHONES; + return VolumeControl.ActiveOutput.SPEAKERS; + } } /* Volume operations */ diff --git a/src/volume-control.vala b/src/volume-control.vala index 4ef2c3e..738bdcd 100644 --- a/src/volume-control.vala +++ b/src/volume-control.vala @@ -28,6 +28,12 @@ public abstract class VolumeControl : Object VOLUME_STREAM_CHANGE } + public enum ActiveOutput { + SPEAKERS, + HEADPHONES, + BLUETOOTH_HEADPHONES + } + public class Volume : Object { public double volume; public VolumeReasons reason; @@ -39,7 +45,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; } } + public virtual VolumeControl.ActiveOutput active_output { get { return VolumeControl.ActiveOutput.SPEAKERS; } } private Volume _volume; public virtual Volume volume { get { return _volume; } set { } } public virtual double mic_volume { get { return 0.0; } set { } } @@ -58,5 +64,5 @@ public abstract class VolumeControl : Object this.volume = v; } - public signal void bluetooth_headset_status_changed (bool bluetooth_headset_active); + public signal void active_output_changed (VolumeControl.ActiveOutput active_output); } -- cgit v1.2.3