Blame SOURCES/0004-core-device-port-check-availability-when-choosing-th.patch

43a070
From fa883e254c28a36657b0f28443bd69ee5a1821e0 Mon Sep 17 00:00:00 2001
43a070
From: Tanu Kaskinen <tanuk@iki.fi>
43a070
Date: Thu, 16 Feb 2017 12:09:39 +0200
43a070
Subject: [PATCH 4/4] core, device-port: check availability when choosing the
43a070
 default device
43a070
43a070
It doesn't make sense to use a sink or source whose active port is
43a070
unavailable, so let's take this into account when choosing the default
43a070
sink and source.
43a070
---
43a070
 src/pulsecore/core.c        | 16 ++++++++++++++++
43a070
 src/pulsecore/device-port.c |  8 ++++++++
43a070
 2 files changed, 24 insertions(+)
43a070
43a070
diff --git a/src/pulsecore/core.c b/src/pulsecore/core.c
43a070
index da1abb3ba..340b41ff3 100644
43a070
--- a/src/pulsecore/core.c
43a070
+++ b/src/pulsecore/core.c
43a070
@@ -265,6 +265,14 @@ static int compare_sinks(pa_sink *a, pa_sink *b) {
43a070
 
43a070
     core = a->core;
43a070
 
43a070
+    /* Available sinks always beat unavailable sinks. */
43a070
+    if (a->active_port && a->active_port->available == PA_AVAILABLE_NO
43a070
+            && (!b->active_port || b->active_port->available != PA_AVAILABLE_NO))
43a070
+        return -1;
43a070
+    if (b->active_port && b->active_port->available == PA_AVAILABLE_NO
43a070
+            && (!a->active_port || a->active_port->available != PA_AVAILABLE_NO))
43a070
+        return 1;
43a070
+
43a070
     /* The configured default sink is preferred over any other sink. */
43a070
     if (b == core->configured_default_sink)
43a070
         return -1;
43a070
@@ -331,6 +339,14 @@ static int compare_sources(pa_source *a, pa_source *b) {
43a070
 
43a070
     core = a->core;
43a070
 
43a070
+    /* Available sources always beat unavailable sources. */
43a070
+    if (a->active_port && a->active_port->available == PA_AVAILABLE_NO
43a070
+            && (!b->active_port || b->active_port->available != PA_AVAILABLE_NO))
43a070
+        return -1;
43a070
+    if (b->active_port && b->active_port->available == PA_AVAILABLE_NO
43a070
+            && (!a->active_port || a->active_port->available != PA_AVAILABLE_NO))
43a070
+        return 1;
43a070
+
43a070
     /* The configured default source is preferred over any other source. */
43a070
     if (b == core->configured_default_source)
43a070
         return -1;
43a070
diff --git a/src/pulsecore/device-port.c b/src/pulsecore/device-port.c
43a070
index 7c9ddf325..76a7e80a1 100644
43a070
--- a/src/pulsecore/device-port.c
43a070
+++ b/src/pulsecore/device-port.c
43a070
@@ -93,6 +93,14 @@ void pa_device_port_set_available(pa_device_port *p, pa_available_t status) {
43a070
      * be created before port objects, and then p->card could be non-NULL for
43a070
      * the whole lifecycle of pa_device_port. */
43a070
     if (p->card) {
43a070
+        /* A sink or source whose active port is unavailable can't be the
43a070
+         * default sink/source, so port availability changes may affect the
43a070
+         * default sink/source choice. */
43a070
+        if (p->direction == PA_DIRECTION_OUTPUT)
43a070
+            pa_core_update_default_sink(p->core);
43a070
+        else
43a070
+            pa_core_update_default_source(p->core);
43a070
+
43a070
         pa_subscription_post(p->core, PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_CHANGE, p->card->index);
43a070
         pa_hook_fire(&p->core->hooks[PA_CORE_HOOK_PORT_AVAILABLE_CHANGED], p);
43a070
     }
43a070
-- 
43a070
2.14.3
43a070