]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android-sdk/platform-external-tinyalsa.git/blobdiff - tinymix.c
Merge "Revert "Update 64-bit blacklist for tinyalsa""
[android-sdk/platform-external-tinyalsa.git] / tinymix.c
index 3fc2e4125dbde1ec3607178d1d6b6eab1583db78..afc2fee0f19ef1f88a6297ce549abccd19df6232 100644 (file)
--- a/tinymix.c
+++ b/tinymix.c
 #include <stdio.h>
 #include <stdlib.h>
 #include <ctype.h>
+#include <string.h>
 
 static void tinymix_list_controls(struct mixer *mixer);
-static void tinymix_detail_control(struct mixer *mixer, unsigned int id);
-static void tinymix_set_value(struct mixer *mixer, unsigned int id,
-                              char *value);
+static void tinymix_detail_control(struct mixer *mixer, const char *control,
+                                   int print_all);
+static void tinymix_set_value(struct mixer *mixer, const char *control,
+                              char **values, unsigned int num_values);
+static void tinymix_print_enum(struct mixer_ctl *ctl, int print_all);
 
 int main(int argc, char **argv)
 {
     struct mixer *mixer;
+    int card = 0;
+
+    if ((argc > 2) && (strcmp(argv[1], "-D") == 0)) {
+        argv++;
+        if (argv[1]) {
+            card = atoi(argv[1]);
+            argv++;
+            argc -= 2;
+        } else {
+            argc -= 1;
+        }
+    }
 
-    mixer = mixer_open(0);
+    mixer = mixer_open(card);
     if (!mixer) {
         fprintf(stderr, "Failed to open mixer\n");
         return EXIT_FAILURE;
     }
 
-    if (argc == 1)
+
+    if (argc == 1) {
+        printf("Mixer name: '%s'\n", mixer_get_name(mixer));
         tinymix_list_controls(mixer);
-    else if (argc == 2)
-        tinymix_detail_control(mixer, atoi(argv[1]));
-    else if (argc == 3)
-        tinymix_set_value(mixer, atoi(argv[1]), argv[2]);
-    else
-        printf("Usage: tinymix [control id] [value to set]\n");
+    } else if (argc == 2) {
+        tinymix_detail_control(mixer, argv[1], 1);
+    } else if (argc >= 3) {
+        tinymix_set_value(mixer, argv[1], &argv[2], argc - 2);
+    } else {
+        printf("Usage: tinymix [-D card] [control id] [value to set]\n");
+    }
 
     mixer_close(mixer);
 
@@ -63,63 +81,69 @@ int main(int argc, char **argv)
 static void tinymix_list_controls(struct mixer *mixer)
 {
     struct mixer_ctl *ctl;
-    const char *type;
+    const char *name, *type;
     unsigned int num_ctls, num_values;
-    char buffer[256];
     unsigned int i;
 
     num_ctls = mixer_get_num_ctls(mixer);
 
     printf("Number of controls: %d\n", num_ctls);
 
-    printf("ctl\ttype\tnum\tname\n");
+    printf("ctl\ttype\tnum\t%-40s value\n", "name");
     for (i = 0; i < num_ctls; i++) {
         ctl = mixer_get_ctl(mixer, i);
 
-        mixer_ctl_get_name(ctl, buffer, sizeof(buffer));
+        name = mixer_ctl_get_name(ctl);
         type = mixer_ctl_get_type_string(ctl);
         num_values = mixer_ctl_get_num_values(ctl);
-
-        printf("%d\t%s\t%d\t%s\n", i, type, num_values, buffer);
+        printf("%d\t%s\t%d\t%-40s", i, type, num_values, name);
+        tinymix_detail_control(mixer, name, 0);
     }
 }
 
-static void tinymix_print_enum(struct mixer_ctl *ctl)
+static void tinymix_print_enum(struct mixer_ctl *ctl, int print_all)
 {
     unsigned int num_enums;
-    char buffer[256];
     unsigned int i;
+    const char *string;
 
     num_enums = mixer_ctl_get_num_enums(ctl);
 
     for (i = 0; i < num_enums; i++) {
-        mixer_ctl_get_enum_string(ctl, i, buffer, sizeof(buffer));
-        printf("\t%s%s", mixer_ctl_get_value(ctl, 0) == (int)i ? ">" : "",
-               buffer);
+        string = mixer_ctl_get_enum_string(ctl, i);
+        if (print_all)
+            printf("\t%s%s", mixer_ctl_get_value(ctl, 0) == (int)i ? ">" : "",
+                   string);
+        else if (mixer_ctl_get_value(ctl, 0) == (int)i)
+            printf(" %-s", string);
     }
 }
 
-static void tinymix_detail_control(struct mixer *mixer, unsigned int id)
+static void tinymix_detail_control(struct mixer *mixer, const char *control,
+                                   int print_all)
 {
     struct mixer_ctl *ctl;
     enum mixer_ctl_type type;
     unsigned int num_values;
-    char buffer[256];
     unsigned int i;
     int min, max;
 
-    if (id >= mixer_get_num_ctls(mixer)) {
+    if (isdigit(control[0]))
+        ctl = mixer_get_ctl(mixer, atoi(control));
+    else
+        ctl = mixer_get_ctl_by_name(mixer, control);
+
+    if (!ctl) {
         fprintf(stderr, "Invalid mixer control\n");
         return;
     }
 
-    ctl = mixer_get_ctl(mixer, id);
-
-    mixer_ctl_get_name(ctl, buffer, sizeof(buffer));
     type = mixer_ctl_get_type(ctl);
     num_values = mixer_ctl_get_num_values(ctl);
 
-    printf("%s:", buffer);
+    if (print_all)
+        printf("%s:", mixer_ctl_get_name(ctl));
+
     for (i = 0; i < num_values; i++) {
         switch (type)
         {
@@ -130,45 +154,81 @@ static void tinymix_detail_control(struct mixer *mixer, unsigned int id)
             printf(" %s", mixer_ctl_get_value(ctl, i) ? "On" : "Off");
             break;
         case MIXER_CTL_TYPE_ENUM:
-            tinymix_print_enum(ctl);
+            tinymix_print_enum(ctl, print_all);
+            break;
+         case MIXER_CTL_TYPE_BYTE:
+            printf(" 0x%02x", mixer_ctl_get_value(ctl, i));
             break;
         default:
             printf(" unknown");
             break;
         };
     }
-    if (type == MIXER_CTL_TYPE_INT) {
-        min = mixer_ctl_get_range_min(ctl);
-        max = mixer_ctl_get_range_max(ctl);
-        printf(" (range %d->%d)", min, max);
+
+    if (print_all) {
+        if (type == MIXER_CTL_TYPE_INT) {
+            min = mixer_ctl_get_range_min(ctl);
+            max = mixer_ctl_get_range_max(ctl);
+            printf(" (range %d->%d)", min, max);
+        }
     }
     printf("\n");
 }
 
-static void tinymix_set_value(struct mixer *mixer, unsigned int id,
-                              char *string)
+static void tinymix_set_value(struct mixer *mixer, const char *control,
+                              char **values, unsigned int num_values)
 {
     struct mixer_ctl *ctl;
     enum mixer_ctl_type type;
-    unsigned int num_values;
+    unsigned int num_ctl_values;
     unsigned int i;
 
-    ctl = mixer_get_ctl(mixer, id);
-    type = mixer_ctl_get_type(ctl);
-    num_values = mixer_ctl_get_num_values(ctl);
+    if (isdigit(control[0]))
+        ctl = mixer_get_ctl(mixer, atoi(control));
+    else
+        ctl = mixer_get_ctl_by_name(mixer, control);
 
-    if (isdigit(string[0])) {
-        int value = atoi(string);
+    if (!ctl) {
+        fprintf(stderr, "Invalid mixer control\n");
+        return;
+    }
 
-        for (i = 0; i < num_values; i++) {
-            if (mixer_ctl_set_value(ctl, i, value)) {
-                fprintf(stderr, "Error: invalid value\n");
+    type = mixer_ctl_get_type(ctl);
+    num_ctl_values = mixer_ctl_get_num_values(ctl);
+
+    if (isdigit(values[0][0])) {
+        if (num_values == 1) {
+            /* Set all values the same */
+            int value = atoi(values[0]);
+
+            for (i = 0; i < num_ctl_values; i++) {
+                if (mixer_ctl_set_value(ctl, i, value)) {
+                    fprintf(stderr, "Error: invalid value\n");
+                    return;
+                }
+            }
+        } else {
+            /* Set multiple values */
+            if (num_values > num_ctl_values) {
+                fprintf(stderr,
+                        "Error: %d values given, but control only takes %d\n",
+                        num_values, num_ctl_values);
                 return;
             }
+            for (i = 0; i < num_values; i++) {
+                if (mixer_ctl_set_value(ctl, i, atoi(values[i]))) {
+                    fprintf(stderr, "Error: invalid value for index %d\n", i);
+                    return;
+                }
+            }
         }
     } else {
         if (type == MIXER_CTL_TYPE_ENUM) {
-            if (mixer_ctl_set_enum_by_string(ctl, string))
+            if (num_values != 1) {
+                fprintf(stderr, "Enclose strings in quotes and try again\n");
+                return;
+            }
+            if (mixer_ctl_set_enum_by_string(ctl, values[0]))
                 fprintf(stderr, "Error: invalid enum value\n");
         } else {
             fprintf(stderr, "Error: only enum types can be set with strings\n");