]> code.delx.au - pulseaudio/blobdiff - src/modules/alsa/alsa-ucm.c
alsa: Support ALSA without a use case manager
[pulseaudio] / src / modules / alsa / alsa-ucm.c
index 87ec293486d584cc66890d8980f68d2b04baa353..be3ac74dcf10a6a18ae7f27cafb032fe0275771f 100644 (file)
@@ -66,6 +66,8 @@
     } while (0)
 #define PA_UCM_IS_MODIFIER_MAPPING(m) ((pa_proplist_gets((m)->proplist, PA_ALSA_PROP_UCM_MODIFIER)) != NULL)
 
+#ifdef HAVE_ALSA_UCM
+
 struct ucm_items {
     const char *id;
     const char *property;
@@ -247,7 +249,12 @@ static int ucm_get_device_property(
         }
     }
 
-    pa_assert(device->playback_channels || device->capture_channels);
+    if (device->playback_channels == 0 && device->capture_channels == 0) {
+        pa_log_warn("UCM file does not specify 'PlaybackChannels' or 'CaptureChannels'"
+                    "for device %s, assuming stereo duplex.", device_name);
+        device->playback_channels = 2;
+        device->capture_channels = 2;
+    }
 
     /* get priority of device */
     if (device->playback_channels) { /* sink device */
@@ -446,10 +453,10 @@ static void add_media_role(const char *name, pa_alsa_ucm_device *list, const cha
 static char *modifier_name_to_role(const char *mod_name, bool *is_sink) {
     char *sub = NULL, *tmp;
 
-    *is_sink = FALSE;
+    *is_sink = false;
 
     if (pa_startswith(mod_name, "Play")) {
-        *is_sink = TRUE;
+        *is_sink = true;
         sub = pa_xstrdup(mod_name + 4);
     } else if (pa_startswith(mod_name, "Capture"))
         sub = pa_xstrdup(mod_name + 7);
@@ -471,7 +478,7 @@ static char *modifier_name_to_role(const char *mod_name, bool *is_sink) {
 
 static void ucm_set_media_roles(pa_alsa_ucm_modifier *modifier, pa_alsa_ucm_device *list, const char *mod_name) {
     int i;
-    bool is_sink = FALSE;
+    bool is_sink = false;
     char *sub = NULL;
     const char *role_name;
 
@@ -520,8 +527,7 @@ static void append_lost_relationship(pa_alsa_ucm_device *dev) {
     }
 }
 
-int pa_alsa_ucm_query_profiles(pa_alsa_ucm_config *ucm, int card_index)
-{
+int pa_alsa_ucm_query_profiles(pa_alsa_ucm_config *ucm, int card_index) {
     char *card_name;
     const char **verb_list;
     int num_verbs, i, err = 0;
@@ -535,7 +541,7 @@ int pa_alsa_ucm_query_profiles(pa_alsa_ucm_config *ucm, int card_index)
 
     err = snd_use_case_mgr_open(&ucm->ucm_mgr, card_name);
     if (err < 0) {
-        pa_log("UCM not available for card %s", card_name);
+        pa_log_info("UCM not available for card %s", card_name);
         goto ucm_mgr_fail;
     }
 
@@ -683,7 +689,15 @@ static void ucm_add_port_combination(
 
     port = pa_hashmap_get(ports, name);
     if (!port) {
-        port = pa_device_port_new(core, pa_strna(name), desc, 0);
+        pa_device_port_new_data port_data;
+
+        pa_device_port_new_data_init(&port_data);
+        pa_device_port_new_data_set_name(&port_data, name);
+        pa_device_port_new_data_set_description(&port_data, desc);
+        pa_device_port_new_data_set_direction(&port_data, is_sink ? PA_DIRECTION_OUTPUT : PA_DIRECTION_INPUT);
+
+        port = pa_device_port_new(core, &port_data, 0);
+        pa_device_port_new_data_done(&port_data);
         pa_assert(port);
 
         pa_hashmap_put(ports, port->name, port);
@@ -692,10 +706,6 @@ static void ucm_add_port_combination(
     }
 
     port->priority = priority;
-    if (is_sink)
-        port->is_output = TRUE;
-    else
-        port->is_input = TRUE;
 
     pa_xfree(name);
     pa_xfree(desc);
@@ -721,7 +731,7 @@ static int ucm_port_contains(const char *port_name, const char *dev_name, bool i
     int len;
 
     if (!port_name || !dev_name)
-        return FALSE;
+        return false;
 
     port_name += is_sink ? strlen(PA_UCM_PRE_TAG_OUTPUT) : strlen(PA_UCM_PRE_TAG_INPUT);
 
@@ -1195,9 +1205,9 @@ static int ucm_create_mapping(
     }
 
     if (sink)
-        ret = ucm_create_mapping_direction(ucm, ps, p, device, verb_name, device_name, sink, TRUE);
+        ret = ucm_create_mapping_direction(ucm, ps, p, device, verb_name, device_name, sink, true);
     if (ret == 0 && source)
-        ret = ucm_create_mapping_direction(ucm, ps, p, device, verb_name, device_name, source, FALSE);
+        ret = ucm_create_mapping_direction(ucm, ps, p, device, verb_name, device_name, source, false);
 
     return ret;
 }
@@ -1211,8 +1221,8 @@ static pa_alsa_jack* ucm_get_jack(pa_alsa_ucm_config *ucm, const char *dev_name,
             goto out;
 
     j = pa_xnew0(pa_alsa_jack, 1);
-    j->state_unplugged = PA_PORT_AVAILABLE_NO;
-    j->state_plugged = PA_PORT_AVAILABLE_YES;
+    j->state_unplugged = PA_AVAILABLE_NO;
+    j->state_plugged = PA_AVAILABLE_YES;
     j->name = pa_xstrdup(name);
     j->alsa_name = pa_sprintf_malloc("%s Jack", dev_name);
 
@@ -1252,7 +1262,7 @@ static int ucm_create_profile(
     p->output_mappings = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
     p->input_mappings = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
 
-    p->supported = TRUE;
+    p->supported = true;
     pa_hashmap_put(ps->profiles, p->name, p);
 
     /* TODO: get profile priority from ucm info or policy management */
@@ -1297,9 +1307,9 @@ static int ucm_create_profile(
         source = pa_proplist_gets(mod->proplist, PA_ALSA_PROP_UCM_SOURCE);
 
         if (sink)
-            ucm_create_mapping_for_modifier(ucm, ps, p, mod, verb_name, name, sink, TRUE);
+            ucm_create_mapping_for_modifier(ucm, ps, p, mod, verb_name, name, sink, true);
         else if (source)
-            ucm_create_mapping_for_modifier(ucm, ps, p, mod, verb_name, name, source, FALSE);
+            ucm_create_mapping_for_modifier(ucm, ps, p, mod, verb_name, name, source, false);
     }
 
     pa_alsa_profile_dump(p);
@@ -1397,7 +1407,7 @@ static void ucm_probe_profile_set(pa_alsa_ucm_config *ucm, pa_alsa_profile_set *
 
         if ((snd_use_case_set(ucm->ucm_mgr, "_verb", p->name)) < 0) {
             pa_log("Failed to set verb %s", p->name);
-            p->supported = FALSE;
+            p->supported = false;
             continue;
         }
 
@@ -1410,7 +1420,7 @@ static void ucm_probe_profile_set(pa_alsa_ucm_config *ucm, pa_alsa_profile_set *
 
             m->output_pcm = mapping_open_pcm(ucm, m, SND_PCM_STREAM_PLAYBACK);
             if (!m->output_pcm) {
-                p->supported = FALSE;
+                p->supported = false;
                 break;
             }
         }
@@ -1425,7 +1435,7 @@ static void ucm_probe_profile_set(pa_alsa_ucm_config *ucm, pa_alsa_profile_set *
 
                 m->input_pcm = mapping_open_pcm(ucm, m, SND_PCM_STREAM_CAPTURE);
                 if (!m->input_pcm) {
-                    p->supported = FALSE;
+                    p->supported = false;
                     break;
                 }
             }
@@ -1476,11 +1486,11 @@ pa_alsa_profile_set* pa_alsa_ucm_add_profile_set(pa_alsa_ucm_config *ucm, pa_cha
             continue;
         }
 
-           ucm_create_profile(ucm, ps, verb, verb_name, verb_desc);
+        ucm_create_profile(ucm, ps, verb, verb_name, verb_desc);
     }
 
     ucm_probe_profile_set(ucm, ps);
-    ps->probed = TRUE;
+    ps->probed = true;
 
     return ps;
 }
@@ -1493,9 +1503,9 @@ static void free_verb(pa_alsa_ucm_verb *verb) {
         PA_LLIST_REMOVE(pa_alsa_ucm_device, verb->devices, di);
         pa_proplist_free(di->proplist);
         if (di->conflicting_devices)
-            pa_idxset_free(di->conflicting_devices, NULL, NULL);
+            pa_idxset_free(di->conflicting_devices, NULL);
         if (di->supported_devices)
-            pa_idxset_free(di->supported_devices, NULL, NULL);
+            pa_idxset_free(di->supported_devices, NULL);
         pa_xfree(di);
     }
 
@@ -1547,7 +1557,7 @@ void pa_alsa_ucm_mapping_context_free(pa_alsa_ucm_mapping_context *context) {
                 dev->capture_mapping = NULL;
         }
 
-        pa_idxset_free(context->ucm_devices, NULL, NULL);
+        pa_idxset_free(context->ucm_devices, NULL);
     }
 
     if (context->ucm_modifiers) {
@@ -1558,6 +1568,111 @@ void pa_alsa_ucm_mapping_context_free(pa_alsa_ucm_mapping_context *context) {
                 mod->capture_mapping = NULL;
         }
 
-        pa_idxset_free(context->ucm_modifiers, NULL, NULL);
+        pa_idxset_free(context->ucm_modifiers, NULL);
+    }
+}
+
+/* Enable the modifier when the first stream with matched role starts */
+void pa_alsa_ucm_roled_stream_begin(pa_alsa_ucm_config *ucm, const char *role, pa_direction_t dir) {
+    pa_alsa_ucm_modifier *mod;
+
+    if (!ucm->active_verb)
+        return;
+
+    PA_LLIST_FOREACH(mod, ucm->active_verb->modifiers) {
+        if ((mod->action_direction == dir) && (pa_streq(mod->media_role, role))) {
+            if (mod->enabled_counter == 0) {
+                const char *mod_name = pa_proplist_gets(mod->proplist, PA_ALSA_PROP_UCM_NAME);
+
+                pa_log_info("Enable ucm modifier %s", mod_name);
+                if (snd_use_case_set(ucm->ucm_mgr, "_enamod", mod_name) < 0) {
+                    pa_log("Failed to enable ucm modifier %s", mod_name);
+                }
+            }
+
+            mod->enabled_counter++;
+            break;
+        }
     }
 }
+
+/* Disable the modifier when the last stream with matched role ends */
+void pa_alsa_ucm_roled_stream_end(pa_alsa_ucm_config *ucm, const char *role, pa_direction_t dir) {
+    pa_alsa_ucm_modifier *mod;
+
+    if (!ucm->active_verb)
+        return;
+
+    PA_LLIST_FOREACH(mod, ucm->active_verb->modifiers) {
+        if ((mod->action_direction == dir) && (pa_streq(mod->media_role, role))) {
+
+            mod->enabled_counter--;
+            if (mod->enabled_counter == 0) {
+                const char *mod_name = pa_proplist_gets(mod->proplist, PA_ALSA_PROP_UCM_NAME);
+
+                pa_log_info("Disable ucm modifier %s", mod_name);
+                if (snd_use_case_set(ucm->ucm_mgr, "_dismod", mod_name) < 0) {
+                    pa_log("Failed to disable ucm modifier %s", mod_name);
+                }
+            }
+
+            break;
+        }
+    }
+}
+
+#else /* HAVE_ALSA_UCM */
+
+/* Dummy functions for systems without UCM support */
+
+int pa_alsa_ucm_query_profiles(pa_alsa_ucm_config *ucm, int card_index) {
+        pa_log_info("UCM not available.");
+        return -1;
+}
+
+pa_alsa_profile_set* pa_alsa_ucm_add_profile_set(pa_alsa_ucm_config *ucm, pa_channel_map *default_channel_map) {
+    return NULL;
+}
+
+int pa_alsa_ucm_set_profile(pa_alsa_ucm_config *ucm, const char *new_profile, const char *old_profile) {
+    return -1;
+}
+
+int pa_alsa_ucm_get_verb(snd_use_case_mgr_t *uc_mgr, const char *verb_name, const char *verb_desc, pa_alsa_ucm_verb **p_verb) {
+    return -1;
+}
+
+void pa_alsa_ucm_add_ports(
+        pa_hashmap **hash,
+        pa_proplist *proplist,
+        pa_alsa_ucm_mapping_context *context,
+        bool is_sink,
+        pa_card *card) {
+}
+
+void pa_alsa_ucm_add_ports_combination(
+        pa_hashmap *hash,
+        pa_alsa_ucm_mapping_context *context,
+        bool is_sink,
+        pa_hashmap *ports,
+        pa_card_profile *cp,
+        pa_core *core) {
+}
+
+int pa_alsa_ucm_set_port(pa_alsa_ucm_mapping_context *context, pa_device_port *port, bool is_sink) {
+    return -1;
+}
+
+void pa_alsa_ucm_free(pa_alsa_ucm_config *ucm) {
+}
+
+void pa_alsa_ucm_mapping_context_free(pa_alsa_ucm_mapping_context *context) {
+}
+
+void pa_alsa_ucm_roled_stream_begin(pa_alsa_ucm_config *ucm, const char *role, pa_direction_t dir) {
+}
+
+void pa_alsa_ucm_roled_stream_end(pa_alsa_ucm_config *ucm, const char *role, pa_direction_t dir) {
+}
+
+#endif