From: Jaroslav Kysela Date: Mon, 23 Nov 2026 14:18:12 +0000 (+0100) Subject: ucm: support optional ('-' prefixed) device references in Syntax 10 X-Git-Url: https://git.alsa-project.org/?a=commitdiff_plain;h=d370f2af4d94f5604112f6225619c4f48d14a60d;p=alsa-lib.git ucm: support optional ('-' prefixed) device references in Syntax 10 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 --- diff --git a/src/ucm/parser.c b/src/ucm/parser.c index fdd39902..68498617 100644 --- a/src/ucm/parser.c +++ b/src/ucm/parser.c @@ -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); diff --git a/src/ucm/ucm_confdoc.h b/src/ucm/ucm_confdoc.h index 7f507644..bc152ada 100644 --- a/src/ucm/ucm_confdoc.h +++ b/src/ucm/ucm_confdoc.h @@ -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. diff --git a/src/ucm/ucm_local.h b/src/ucm/ucm_local.h index c00dd6da..a1277bb7 100644 --- a/src/ucm/ucm_local.h +++ b/src/ucm/ucm_local.h @@ -35,7 +35,7 @@ #include #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