OSDN Git Service

ucm: substitute the merged tree completely
authorJaroslav Kysela <perex@perex.cz>
Mon, 6 Jul 2020 14:34:33 +0000 (16:34 +0200)
committerJaroslav Kysela <perex@perex.cz>
Mon, 6 Jul 2020 14:34:35 +0000 (16:34 +0200)
We need to define the common shared configuration like for multiple
HDMI devices or so. Substitute the whole merged configuration tree
including identifiers.

Fixes: https://github.com/alsa-project/alsa-lib/issues/67
Fixes: https://github.com/alsa-project/alsa-ucm-conf/commit/dcef48f13d4f5db79b006755074940b94730a883
Signed-off-by: Jaroslav Kysela <perex@perex.cz>
src/ucm/ucm_cond.c
src/ucm/ucm_include.c
src/ucm/ucm_local.h
src/ucm/ucm_subs.c

index cc49153..3ca3096 100644 (file)
@@ -401,7 +401,7 @@ int uc_mgr_evaluate_condition(snd_use_case_mgr_t *uc_mgr,
                err = uc_mgr_evaluate_inplace(uc_mgr, a);
                if (err < 0)
                        return err;
-               err = uc_mgr_config_tree_merge(parent, a, before, after);
+               err = uc_mgr_config_tree_merge(uc_mgr, parent, a, before, after);
                if (err < 0)
                        return err;
                snd_config_delete(a);
index c69490f..94ae944 100644 (file)
@@ -206,7 +206,8 @@ static int compound_merge(const char *id,
        return 0;
 }
 
-int uc_mgr_config_tree_merge(snd_config_t *parent, snd_config_t *new_ctx,
+int uc_mgr_config_tree_merge(snd_use_case_mgr_t *uc_mgr,
+                            snd_config_t *parent, snd_config_t *new_ctx,
                             snd_config_t *before, snd_config_t *after)
 {
        snd_config_iterator_t i, next;
@@ -214,6 +215,10 @@ int uc_mgr_config_tree_merge(snd_config_t *parent, snd_config_t *new_ctx,
        const char *id;
        int err;
 
+       err = uc_mgr_substitute_tree(uc_mgr, new_ctx);
+       if (err < 0)
+               return err;
+
        snd_config_for_each(i, next, new_ctx) {
                n = snd_config_iterator_entry(i);
                err = snd_config_remove(n);
@@ -271,7 +276,7 @@ int uc_mgr_evaluate_include(snd_use_case_mgr_t *uc_mgr,
                err = uc_mgr_evaluate_inplace(uc_mgr, a);
                if (err < 0)
                        return err;
-               err = uc_mgr_config_tree_merge(parent, a, before, after);
+               err = uc_mgr_config_tree_merge(uc_mgr, parent, a, before, after);
                if (err < 0)
                        return err;
                snd_config_delete(a);
index 709f4cb..dd72e3f 100644 (file)
@@ -307,7 +307,11 @@ int uc_mgr_get_substituted_value(snd_use_case_mgr_t *uc_mgr,
                                 char **_rvalue,
                                 const char *value);
 
-int uc_mgr_config_tree_merge(snd_config_t *parent, snd_config_t *new_ctx,
+int uc_mgr_substitute_tree(snd_use_case_mgr_t *uc_mgr,
+                          snd_config_t *node);
+
+int uc_mgr_config_tree_merge(snd_use_case_mgr_t *uc_mgr,
+                            snd_config_t *parent, snd_config_t *new_ctx,
                             snd_config_t *before, snd_config_t *after);
 
 int uc_mgr_evaluate_inplace(snd_use_case_mgr_t *uc_mgr,
index d40e547..f608bb0 100644 (file)
@@ -395,3 +395,56 @@ __error:
        free(r);
        return err;
 }
+
+static inline int uc_mgr_substitute_check(const char *s)
+{
+       return s && strstr(s, "${") != NULL;
+}
+
+int uc_mgr_substitute_tree(snd_use_case_mgr_t *uc_mgr, snd_config_t *node)
+{
+       snd_config_iterator_t i, next;
+       snd_config_t *n;
+       const char *id, *s2;
+       char *s;
+       int err;
+
+       err = snd_config_get_id(node, &id);
+       if (err < 0)
+               return err;
+       if (uc_mgr_substitute_check(id)) {
+               err = uc_mgr_get_substituted_value(uc_mgr, &s, id);
+               if (err < 0)
+                       return err;
+               err = snd_config_set_id(node, s);
+               free(s);
+               if (err < 0) {
+                       uc_error("unable to set substituted id '%s' (old id '%s')", s, id);
+                       return err;
+               }
+       }
+       if (snd_config_get_type(node) != SND_CONFIG_TYPE_COMPOUND) {
+               if (snd_config_get_type(node) == SND_CONFIG_TYPE_STRING) {
+                       err = snd_config_get_string(node, &s2);
+                       if (err < 0)
+                               return err;
+                       if (!uc_mgr_substitute_check(s2))
+                               return 0;
+                       err = uc_mgr_get_substituted_value(uc_mgr, &s, s2);
+                       if (err < 0)
+                               return err;
+                       err = snd_config_set_string(node, s);
+                       free(s);
+                       if (err < 0)
+                               return err;
+               }
+               return 0;
+       }
+       snd_config_for_each(i, next, node) {
+               n = snd_config_iterator_entry(i);
+               err = uc_mgr_substitute_tree(uc_mgr, n);
+               if (err < 0)
+                       return err;
+       }
+       return 0;
+}