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

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