]> code.delx.au - pulseaudio/blobdiff - src/pulsecore/namereg.c
Merge branch 'master' of git://0pointer.de/pulseaudio into dbus-work
[pulseaudio] / src / pulsecore / namereg.c
index 5ab3036eaeaeaafd65fe5bba453915888b6f9338..d7d83c5e8db233b2ecb579525f6158aeeece8a6d 100644 (file)
@@ -5,7 +5,7 @@
 
   PulseAudio is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published
-  by the Free Software Foundation; either version 2 of the License,
+  by the Free Software Foundation; either version 2.1 of the License,
   or (at your option) any later version.
 
   PulseAudio is distributed in the hope that it will be useful, but
@@ -149,21 +149,55 @@ const char *pa_namereg_register(pa_core *c, const char *name, pa_namereg_type_t
 
     pa_assert_se(pa_hashmap_put(c->namereg, e->name, e) >= 0);
 
+    if (type == PA_NAMEREG_SINK && !c->default_sink)
+        pa_namereg_set_default_sink(c, data);
+    else if (type == PA_NAMEREG_SOURCE && !c->default_source)
+        pa_namereg_set_default_source(c, data);
+
     return e->name;
 }
 
 void pa_namereg_unregister(pa_core *c, const char *name) {
     struct namereg_entry *e;
+    uint32_t idx;
 
     pa_assert(c);
     pa_assert(name);
 
     pa_assert_se(e = pa_hashmap_remove(c->namereg, name));
 
-    if (c->default_sink == e->data)
-        pa_namereg_set_default_sink(c, NULL);
-    else if (c->default_source == e->data)
-        pa_namereg_set_default_source(c, NULL);
+    if (c->default_sink == e->data) {
+        pa_sink *new_default = NULL;
+
+        /* FIXME: the selection here should be based priority values on
+         * the sinks */
+
+        PA_IDXSET_FOREACH(new_default, c->sinks, idx) {
+            if (new_default != e->data && PA_SINK_IS_LINKED(pa_sink_get_state(new_default)))
+                break;
+        }
+
+        pa_namereg_set_default_sink(c, new_default);
+
+    } else if (c->default_source == e->data) {
+        pa_source *new_default = NULL;
+
+        /* First, try to find one that isn't a monitor */
+        PA_IDXSET_FOREACH(new_default, c->sources, idx) {
+            if (new_default != e->data && !new_default->monitor_of && PA_SOURCE_IS_LINKED(pa_source_get_state(new_default)))
+                break;
+        }
+
+        if (!new_default) {
+            /* Then, fallback to a monitor */
+            PA_IDXSET_FOREACH(new_default, c->sources, idx) {
+                if (new_default != e->data && PA_SOURCE_IS_LINKED(pa_source_get_state(new_default)))
+                    break;
+            }
+        }
+
+        pa_namereg_set_default_source(c, new_default);
+    }
 
     pa_xfree(e->name);
     pa_xfree(e);
@@ -191,7 +225,6 @@ void* pa_namereg_get(pa_core *c, const char *name, pa_namereg_type_t type) {
 
         if ((s = pa_namereg_get(c, NULL, PA_NAMEREG_SINK)))
             return s->monitor_source;
-
     }
 
     if (!name)
@@ -223,6 +256,9 @@ void* pa_namereg_get(pa_core *c, const char *name, pa_namereg_type_t type) {
 pa_sink* pa_namereg_set_default_sink(pa_core*c, pa_sink *s) {
     pa_assert(c);
 
+    if (s && !PA_SINK_IS_LINKED(pa_sink_get_state(s)))
+        return NULL;
+
     if (c->default_sink != s) {
         c->default_sink = s;
         pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SERVER|PA_SUBSCRIPTION_EVENT_CHANGE, PA_INVALID_INDEX);
@@ -234,6 +270,9 @@ pa_sink* pa_namereg_set_default_sink(pa_core*c, pa_sink *s) {
 pa_source* pa_namereg_set_default_source(pa_core*c, pa_source *s) {
     pa_assert(c);
 
+    if (s && !PA_SOURCE_IS_LINKED(pa_source_get_state(s)))
+        return NULL;
+
     if (c->default_source != s) {
         c->default_source = s;
         pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SERVER|PA_SUBSCRIPTION_EVENT_CHANGE, PA_INVALID_INDEX);
@@ -242,35 +281,49 @@ pa_source* pa_namereg_set_default_source(pa_core*c, pa_source *s) {
     return s;
 }
 
+/* XXX: After removing old functionality, has this function become useless? */
 pa_sink *pa_namereg_get_default_sink(pa_core *c) {
     pa_sink *s;
+    uint32_t idx;
 
     pa_assert(c);
 
-    if (c->default_sink)
+    if (!c->default_sink || PA_SINK_IS_LINKED(pa_sink_get_state(c->default_sink)))
         return c->default_sink;
 
-    if ((s = pa_idxset_first(c->sinks, NULL)))
-        return pa_namereg_set_default_sink(c, s);
+    /* The old default sink has become unlinked, set a new one. */
 
-    return NULL;
+    /* FIXME: the selection here should be based priority values on
+     * the sinks */
+
+    PA_IDXSET_FOREACH(s, c->sinks, idx)
+        if (PA_SINK_IS_LINKED(pa_sink_get_state(s)))
+            return pa_namereg_set_default_sink(c, s);
+
+    return pa_namereg_set_default_sink(c, NULL);
 }
 
+/* XXX: After removing old functionality, has this function become useless? */
 pa_source *pa_namereg_get_default_source(pa_core *c) {
     pa_source *s;
     uint32_t idx;
 
     pa_assert(c);
 
-    if (c->default_source)
+    if (!c->default_source || PA_SOURCE_IS_LINKED(pa_source_get_state(c->default_source)))
         return c->default_source;
 
-    for (s = PA_SOURCE(pa_idxset_first(c->sources, &idx)); s; s = PA_SOURCE(pa_idxset_next(c->sources, &idx)))
-        if (!s->monitor_of)
+    /* The old default source has become unlinked, set a new one. */
+
+    /* First, try to find one that isn't a monitor */
+    PA_IDXSET_FOREACH(s, c->sources, idx)
+        if (!s->monitor_of && PA_SOURCE_IS_LINKED(pa_source_get_state(s)))
             return pa_namereg_set_default_source(c, s);
 
-    if ((s = pa_idxset_first(c->sources, NULL)))
-        return pa_namereg_set_default_source(c, s);
+    /* Then, fallback to a monitor */
+    PA_IDXSET_FOREACH(s, c->sources, idx)
+        if (PA_SOURCE_IS_LINKED(pa_source_get_state(s)))
+            return pa_namereg_set_default_source(c, s);
 
-    return NULL;
+    return pa_namereg_set_default_source(c, NULL);
 }