From bba1bfdff435db14bc18d256b3957c68e0c8cbdd Mon Sep 17 00:00:00 2001 From: charles kerr Date: Sun, 20 Dec 2015 11:52:28 -0600 Subject: start making volume-control-pulse's pulse functions public static for reuse. calculate_active_output() and create_pulse_dbus_connection() are now 'public static' so volume-warning can use them too. --- src/volume-control-pulse.vala | 29 ++++++---- src/volume-warning.vala | 122 ++++++++---------------------------------- 2 files changed, 41 insertions(+), 110 deletions(-) diff --git a/src/volume-control-pulse.vala b/src/volume-control-pulse.vala index 2ecf7d0..b1b7c73 100644 --- a/src/volume-control-pulse.vala +++ b/src/volume-control-pulse.vala @@ -121,7 +121,7 @@ public class VolumeControlPulse : VolumeControl stop_account_service_volume_timer(); } - private VolumeControl.ActiveOutput calculate_active_output (SinkInfo? sink) { + public static VolumeControl.ActiveOutput calculate_active_output (SinkInfo? sink) { VolumeControl.ActiveOutput ret_output = VolumeControl.ActiveOutput.SPEAKERS; /* Check if the current active port is headset/headphone */ @@ -693,16 +693,11 @@ public class VolumeControlPulse : VolumeControl } } - /* PulseAudio Dbus (Stream Restore) logic */ - private void reconnect_pulse_dbus () + public static DBusConnection create_pulse_dbus_connection() { unowned string pulse_dbus_server_env = Environment.get_variable ("PULSE_DBUS_SERVER"); string address; - /* In case of a reconnect */ - _pulse_use_stream_restore = false; - _pa_volume_sig_count = 0; - if (pulse_dbus_server_env != null) { address = pulse_dbus_server_env; } else { @@ -713,7 +708,7 @@ public class VolumeControlPulse : VolumeControl conn = Bus.get_sync (BusType.SESSION); } catch (GLib.IOError e) { warning ("unable to get the dbus session bus: %s", e.message); - return; + return null; } try { @@ -725,17 +720,29 @@ public class VolumeControlPulse : VolumeControl address = props.get_string (); } catch (GLib.Error e) { warning ("unable to get pulse unix socket: %s", e.message); - return; + return null; } } debug ("PulseAudio dbus unix socket: %s", address); try { - _pconn = new DBusConnection.for_address_sync (address, DBusConnectionFlags.AUTHENTICATION_CLIENT); + return new DBusConnection.for_address_sync (address, DBusConnectionFlags.AUTHENTICATION_CLIENT); } catch (GLib.Error e) { /* If it fails, it means the dbus pulse extension is not available */ - return; + return null; } + } + + /* PulseAudio Dbus (Stream Restore) logic */ + private void reconnect_pulse_dbus () + { + /* In case of a reconnect */ + _pulse_use_stream_restore = false; + _pa_volume_sig_count = 0; + + _pconn = create_pulse_dbus_connection(); + if (_pconn == null) + return; /* For pulse dbus related events */ _pconn.add_filter (pulse_dbus_filter); diff --git a/src/volume-warning.vala b/src/volume-warning.vala index 28bdbcd..f6b2340 100644 --- a/src/volume-warning.vala +++ b/src/volume-warning.vala @@ -117,54 +117,6 @@ public class VolumeWarning : Object stop_clamp_to_loud_timeout(); } - private VolumeControl.ActiveOutput calculate_active_output (SinkInfo? sink) { - - VolumeControl.ActiveOutput ret_output = VolumeControl.ActiveOutput.SPEAKERS; - /* 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' */ - - // first of all check if we are in call mode - if (sink.active_port != null && sink.active_port.name == "output-speaker+wired_headphone") { - return VolumeControl.ActiveOutput.CALL_MODE; - } - // look if it's a headset/headphones - if (sink.name == "indicator_sound_test_headphones" || - (sink.active_port != null && - (sink.active_port.name.contains("headset") || - sink.active_port.name.contains("headphone")))) { - _active_port_headphone = true; - // check if it's a bluetooth device - var device_bus = sink.proplist.gets ("device.bus"); - if (device_bus != null && device_bus == "bluetooth") { - ret_output = VolumeControl.ActiveOutput.BLUETOOTH_HEADPHONES; - } else if (device_bus != null && device_bus == "usb") { - ret_output = VolumeControl.ActiveOutput.USB_HEADPHONES; - } else if (device_bus != null && device_bus == "hdmi") { - ret_output = VolumeControl.ActiveOutput.HDMI_HEADPHONES; - } else { - ret_output = VolumeControl.ActiveOutput.HEADPHONES; - } - } else { - // speaker - _active_port_headphone = false; - var device_bus = sink.proplist.gets ("device.bus"); - if (device_bus != null && device_bus == "bluetooth") { - ret_output = VolumeControl.ActiveOutput.BLUETOOTH_SPEAKER; - } else if (device_bus != null && device_bus == "usb") { - ret_output = VolumeControl.ActiveOutput.USB_SPEAKER; - } else if (device_bus != null && device_bus == "hdmi") { - ret_output = VolumeControl.ActiveOutput.HDMI_SPEAKER; - } else { - ret_output = VolumeControl.ActiveOutput.SPEAKERS; - } - } - - return ret_output; - } - /* PulseAudio logic*/ private void context_events_cb (Context c, Context.SubscriptionEventType t, uint32 index) { @@ -205,23 +157,29 @@ public class VolumeWarning : Object if (i == null) return; - // store the current status of the active output - VolumeControl.ActiveOutput active_output_before = active_output; + var old_active_output = active_output; + var new_active_output = VolumeControlPulse.calculate_active_output(i); - // calculate the output - _active_output = calculate_active_output (i); + _active_output = new_active_output; - // check if the output has changed, if so... emit a signal - VolumeControl.ActiveOutput active_output_now = active_output; - if (active_output_now != active_output_before && - (active_output_now != VolumeControl.ActiveOutput.CALL_MODE && - active_output_before != VolumeControl.ActiveOutput.CALL_MODE)) { - if (active_output_now == VolumeControl.ActiveOutput.SPEAKERS) { - _high_volume_approved = false; - } - update_high_volume(); + switch (new_active_output) { + case VolumeControl.ActiveOutput.HEADPHONES: + case VolumeControl.ActiveOutput.USB_HEADPHONES: + case VolumeControl.ActiveOutput.HDMI_HEADPHONES: + case VolumeControl.ActiveOutput.BLUETOOTH_HEADPHONES: + _active_port_headphones = true; + break; + + default: + _active_port_headphones = false; + break; } + if ((new_active_output != old_active_output) + && (new_active_output != VolumeControl.ActiveOutput.CALL_MODE) + && (old_active_output != VolumeControl.ActiveOutput.CALL_MODE)) + update_high_volume(); + if (_pulse_use_stream_restore == false && _volume.volume != volume_to_double (i.volume.max ())) { @@ -234,9 +192,8 @@ public class VolumeWarning : Object private void server_info_cb_for_props (Context c, ServerInfo? i) { - if (i == null) - return; - context.get_sink_info_by_name (i.default_sink_name, sink_info_cb_for_props); + if (i != null) + context.get_sink_info_by_name (i.default_sink_name, sink_info_cb_for_props); } private void update_sink () @@ -675,46 +632,13 @@ public class VolumeWarning : Object /* PulseAudio Dbus (Stream Restore) logic */ private void reconnect_pulse_dbus () { - unowned string pulse_dbus_server_env = Environment.get_variable ("PULSE_DBUS_SERVER"); - string address; - /* In case of a reconnect */ _pulse_use_stream_restore = false; _pa_volume_sig_count = 0; - if (pulse_dbus_server_env != null) { - address = pulse_dbus_server_env; - } else { - DBusConnection conn; - Variant props; - - try { - conn = Bus.get_sync (BusType.SESSION); - } catch (GLib.IOError e) { - warning ("unable to get the dbus session bus: %s", e.message); - return; - } - - try { - var props_variant = conn.call_sync ("org.PulseAudio1", - "/org/pulseaudio/server_lookup1", "org.freedesktop.DBus.Properties", - "Get", new Variant ("(ss)", "org.PulseAudio.ServerLookup1", "Address"), - null, DBusCallFlags.NONE, -1); - props_variant.get ("(v)", out props); - address = props.get_string (); - } catch (GLib.Error e) { - warning ("unable to get pulse unix socket: %s", e.message); - return; - } - } - - debug ("PulseAudio dbus unix socket: %s", address); - try { - _pconn = new DBusConnection.for_address_sync (address, DBusConnectionFlags.AUTHENTICATION_CLIENT); - } catch (GLib.Error e) { - /* If it fails, it means the dbus pulse extension is not available */ + _pconn = VolumeControlPulse.create_pulse_dbus_connection(); + if (_pconn == null) return; - } /* For pulse dbus related events */ _pconn.add_filter (pulse_dbus_filter); -- cgit v1.2.3