]> git.alsa-project.org Git - alsa-lib.git/commitdiff
ucm: support optional ('-' prefixed) device references in Syntax 10
authorJaroslav Kysela <perex@perex.cz>
Mon, 23 Nov 2026 14:18:12 +0000 (15:18 +0100)
committerJaroslav Kysela <perex@perex.cz>
Tue, 24 Nov 2026 10:23:35 +0000 (11:23 +0100)
A device name in a SupportedDevice or ConflictingDevice list may be
prefixed with '-' to mark the reference as optional. A '-' prefixed name
that matches no device is silently dropped instead of aborting parsing;
when the device exists, the prefix is stripped and it behaves like a
normal reference. This lets shared configuration snippets reference
devices that may or may not be present without conditional blocks.

Gated on conf_format >= 10.

Signed-off-by: Jaroslav Kysela <perex@perex.cz>
src/ucm/parser.c
src/ucm/ucm_confdoc.h
src/ucm/ucm_local.h

index fdd39902a1e39aa14f1c2171add0566029291210..68498617eaca13d62cd3bdda97cefe6aef8ee73a 100644 (file)
@@ -1736,6 +1736,9 @@ static int uc_mgr_config_copy_eval_merge(snd_use_case_mgr_t *uc_mgr,
  *             PlaybackSwitch "name='Master Playback Switch',index=2"
  *     }
  * }
+ *
+ * SupportedDevice and ConflictingDevice cannot be specified together.
+ * Both are optional.
  */
 
 static int parse_device_by_name(snd_use_case_mgr_t *uc_mgr,
@@ -2074,6 +2077,68 @@ static int verb_dev_list_add(struct use_case_verb *verb,
        return -ENOENT;
 }
 
+static bool verb_has_device(struct use_case_verb *verb, const char *name)
+{
+       struct list_head *pos;
+       struct use_case_device *device;
+
+       list_for_each(pos, &verb->device_list) {
+               device = list_entry(pos, struct use_case_device, list);
+               if (strcmp(device->name, name) == 0)
+                       return true;
+       }
+       return false;
+}
+
+static int dev_list_resolve_optional(struct use_case_verb *verb,
+                                    struct dev_list *dev_list)
+{
+       struct list_head *pos, *npos;
+       struct dev_list_node *dlist;
+
+       list_for_each_safe(pos, npos, &dev_list->list) {
+               dlist = list_entry(pos, struct dev_list_node, list);
+               if (dlist->name[0] != '-')
+                       continue;
+               if (verb_has_device(verb, dlist->name + 1)) {
+                       memmove(dlist->name, dlist->name + 1, strlen(dlist->name));
+               } else {
+                       list_del(&dlist->list);
+                       free(dlist->name);
+                       free(dlist);
+               }
+       }
+
+       return 0;
+}
+
+static int verb_resolve_optional_devices(snd_use_case_mgr_t *uc_mgr,
+                                        struct use_case_verb *verb)
+{
+       struct list_head *pos;
+       struct use_case_device *device;
+       struct use_case_modifier *modifier;
+       int err;
+
+       if (uc_mgr->conf_format < 10)
+               return 0;
+
+       list_for_each(pos, &verb->device_list) {
+               device = list_entry(pos, struct use_case_device, list);
+               err = dev_list_resolve_optional(verb, &device->dev_list);
+               if (err < 0)
+                       return err;
+       }
+       list_for_each(pos, &verb->modifier_list) {
+               modifier = list_entry(pos, struct use_case_modifier, list);
+               err = dev_list_resolve_optional(verb, &modifier->dev_list);
+               if (err < 0)
+                       return err;
+       }
+
+       return 0;
+}
+
 static int verb_dev_list_check(struct use_case_verb *verb)
 {
        struct list_head *pos, *pos2, *pos3;
@@ -2496,6 +2561,13 @@ static int verb_device_management(snd_use_case_mgr_t *uc_mgr, struct use_case_ve
        struct ucm_dev_name *dev;
        int err;
 
+       /* resolve optional ('-' prefixed) device references before any rename
+        * or normalization so the kept references are processed like normal ones
+        */
+       err = verb_resolve_optional_devices(uc_mgr, verb);
+       if (err < 0)
+               return err;
+
        /* rename devices */
        list_for_each(pos, &verb->rename_list) {
                dev = list_entry(pos, struct ucm_dev_name, list);
index 7f50764417db82b945818cd63fd5944eb694c437..bc152ada6d55d04df6de2ae8e4b25320880d4569 100644 (file)
@@ -1092,6 +1092,47 @@ SectionDevice."Speaker" {
 }
 ~~~
 
+### Device relationships (SupportedDevice / ConflictingDevice)
+
+A device or modifier may declare its relationship to other devices using the
+*SupportedDevice* and *ConflictingDevice* lists. Only one of the two may be
+specified for a given section; both are optional. The relationships declared for
+a device are automatically made bidirectional and transitive within the verb
+(if A conflicts with B and B conflicts with C, then A, B and C conflict with
+each other).
+
+By default, every name listed in *SupportedDevice* or *ConflictingDevice* must
+refer to an existing device in the verb. A reference to an unknown device aborts
+the configuration parsing with an error.
+
+#### Optional device references (Syntax 10+)
+
+Starting with **Syntax 10**, a device name in a *SupportedDevice* or
+*ConflictingDevice* list may be prefixed with a minus sign ('-') to mark the
+reference as *optional*. If no device with the given name exists in the verb,
+the reference is removed from the list instead of raising an error. The '-'
+prefix only affects error handling; when the referenced device does exist, the
+prefix is dropped and the entry behaves exactly like a normal reference.
+
+This is useful for configuration snippets that are shared between cards or
+layouts where some devices may or may not be present, avoiding the need to
+guard every reference with conditional blocks.
+
+Example - a headset device that conflicts with a speaker that may not exist on
+all models:
+
+~~~{.html}
+SectionDevice."Headset" {
+  ConflictingDevice [
+    "Headphones"     # must exist - error if missing
+    "-Speaker"       # optional - ignored if the card has no Speaker device
+  ]
+  EnableSequence [
+    ...
+  ]
+}
+~~~
+
 ### Device Variants
 
 Starting with **Syntax 8**, devices can define variants using the *DeviceVariant* block.
index c00dd6da9262e808a0a351bcb76f53b2fd7c8f95..a1277bb7d2e8db7b7cb2fbdc713fa7f7d2b0fb83 100644 (file)
@@ -35,7 +35,7 @@
 #include <stdbool.h>
 #include "use-case.h"
 
-#define SYNTAX_VERSION_MAX     9
+#define SYNTAX_VERSION_MAX     10
 
 #define MAX_CARD_SHORT_NAME    32
 #define MAX_CARD_LONG_NAME     80