]> git.alsa-project.org Git - alsa-lib.git/commitdiff
ucm: add integer comparison condition (Syntax 9)
authorJaroslav Kysela <perex@perex.cz>
Fri, 6 Feb 2026 11:57:02 +0000 (12:57 +0100)
committerJaroslav Kysela <perex@perex.cz>
Fri, 6 Feb 2026 17:27:37 +0000 (18:27 +0100)
Adds support for integer comparison operations in If.Condition blocks.
Supports operators: ==, !=, <, >, <=, >= for comparing integer values.
Both values are substituted and converted from strings to 64-bit integers.
Hexadecimal (C like) strings are also accepted (like 0x1234).

Example usage:
  If.check_channels {
    Condition {
      Type Integer
      Operation ">"
      Value1 "${var:channels}"
      Value2 "2"
    }
    True { ... }
  }

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

index b909c6b2bd396620105636d0bedeb70265c7a8fa..37118927987fe36837ef6e78a1de49c13158820b 100644 (file)
@@ -270,6 +270,80 @@ static int if_eval_control_exists(snd_use_case_mgr_t *uc_mgr, snd_config_t *eval
        return 1;
 }
 
+static int if_eval_integer(snd_use_case_mgr_t *uc_mgr, snd_config_t *eval)
+{
+       const char *value1_str = NULL, *value2_str = NULL, *operation = NULL;
+       char *s1, *s2;
+       long long val1, val2;
+       int err, err1, err2;
+
+       if (uc_mgr->conf_format < 9) {
+               snd_error(UCM, "Integer condition is supported in v9+ syntax");
+               return -EINVAL;
+       }
+
+       err = get_string(eval, "Operation", &operation);
+       if (err < 0) {
+               snd_error(UCM, "Integer error (If.Condition.Operation)");
+               return -EINVAL;
+       }
+
+       err = get_string(eval, "Value1", &value1_str);
+       if (err < 0) {
+               snd_error(UCM, "Integer error (If.Condition.Value1)");
+               return -EINVAL;
+       }
+
+       err = get_string(eval, "Value2", &value2_str);
+       if (err < 0) {
+               snd_error(UCM, "Integer error (If.Condition.Value2)");
+               return -EINVAL;
+       }
+
+       err = uc_mgr_get_substituted_value(uc_mgr, &s1, value1_str);
+       if (err < 0)
+               return err;
+
+       err = uc_mgr_get_substituted_value(uc_mgr, &s2, value2_str);
+       if (err < 0) {
+               free(s1);
+               return err;
+       }
+
+       err1 = safe_strtoll(s1, &val1);
+       err2 = safe_strtoll(s2, &val2);
+
+       if (err1 < 0 || err2 < 0) {
+               if (err1 < 0)
+                       snd_error(UCM, "Integer conversion error for Value1 '%s'", s1);
+               if (err2 < 0)
+                       snd_error(UCM, "Integer conversion error for Value2 '%s'", s2);
+               free(s2);
+               free(s1);
+               return -EINVAL;
+       }
+
+       free(s2);
+       free(s1);
+
+       if (strcmp(operation, "==") == 0) {
+               return val1 == val2;
+       } else if (strcmp(operation, "!=") == 0) {
+               return val1 != val2;
+       } else if (strcmp(operation, "<") == 0) {
+               return val1 < val2;
+       } else if (strcmp(operation, ">") == 0) {
+               return val1 > val2;
+       } else if (strcmp(operation, "<=") == 0) {
+               return val1 <= val2;
+       } else if (strcmp(operation, ">=") == 0) {
+               return val1 >= val2;
+       } else {
+               snd_error(UCM, "Integer unknown operation '%s'", operation);
+               return -EINVAL;
+       }
+}
+
 static int if_eval_path(snd_use_case_mgr_t *uc_mgr, snd_config_t *eval)
 {
        const char *path, *mode = "";
@@ -365,6 +439,9 @@ static int if_eval(snd_use_case_mgr_t *uc_mgr, snd_config_t *eval)
        if (strcmp(type, "Path") == 0)
                return if_eval_path(uc_mgr, eval);
 
+       if (strcmp(type, "Integer") == 0)
+               return if_eval_integer(uc_mgr, eval);
+
        snd_error(UCM, "unknown If.Condition.Type");
        return -EINVAL;
 }
index 5fac99e45d5a0dc7e25d27eeaefcfd5c7301924a..0a344b7ec14b12088415d19a07c8917bd53991a2 100644 (file)
@@ -878,6 +878,32 @@ If.fmic {
 }
 ~~~
 
+#### Integer comparison (Type Integer)
+
+Field                | Description
+---------------------|-----------------------
+Operation            | comparison operator (==, !=, <, >, <=, >=)
+Value1               | first integer value (string converted to long long)
+Value2               | second integer value (string converted to long long)
+
+Note: Integer condition is supported in *Syntax* version *9*+.
+
+Example:
+
+~~~{.html}
+If.check_channels {
+  Condition {
+    Type Integer
+    Operation ">"
+    Value1 "${var:channels}"
+    Value2 "2"
+  }
+  True {
+    ...
+  }
+}
+~~~
+
 ### Variants
 
 To avoid duplication of the many configuration files for the cases with