]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android-sdk/platform-external-tinyalsa.git/blob - tinymix.c
e7bd27678d9a5b2b7dbc9b0a89a93e2366d9bd0c
[android-sdk/platform-external-tinyalsa.git] / tinymix.c
1 /* tinymix.c
2 **
3 ** Copyright 2011, The Android Open Source Project
4 **
5 ** Redistribution and use in source and binary forms, with or without
6 ** modification, are permitted provided that the following conditions are met:
7 **     * Redistributions of source code must retain the above copyright
8 **       notice, this list of conditions and the following disclaimer.
9 **     * Redistributions in binary form must reproduce the above copyright
10 **       notice, this list of conditions and the following disclaimer in the
11 **       documentation and/or other materials provided with the distribution.
12 **     * Neither the name of The Android Open Source Project nor the names of
13 **       its contributors may be used to endorse or promote products derived
14 **       from this software without specific prior written permission.
15 **
16 ** THIS SOFTWARE IS PROVIDED BY The Android Open Source Project ``AS IS'' AND
17 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ** ARE DISCLAIMED. IN NO EVENT SHALL The Android Open Source Project BE LIABLE
20 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26 ** DAMAGE.
27 */
29 #include <tinyalsa/asoundlib.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <ctype.h>
34 static void tinymix_list_controls(struct mixer *mixer);
35 static void tinymix_detail_control(struct mixer *mixer, unsigned int id,
36                                    int print_all);
37 static void tinymix_set_value(struct mixer *mixer, unsigned int id,
38                               char *value);
39 static void tinymix_print_enum(struct mixer_ctl *ctl, int print_all);
41 int main(int argc, char **argv)
42 {
43     struct mixer *mixer;
44     int card = 0;
46     if ((argc > 2) && (strcmp(argv[1], "-D") == 0)) {
47         argv++;
48         if (argv[1]) {
49             card = atoi(argv[1]);
50             argv++;
51             argc -= 2;
52         } else {
53             argc -= 1;
54         }
55     }
57     mixer = mixer_open(card);
58     if (!mixer) {
59         fprintf(stderr, "Failed to open mixer\n");
60         return EXIT_FAILURE;
61     }
63     if (argc == 1)
64         tinymix_list_controls(mixer);
65     else if (argc == 2)
66         tinymix_detail_control(mixer, atoi(argv[1]), 1);
67     else if (argc == 3)
68         tinymix_set_value(mixer, atoi(argv[1]), argv[2]);
69     else
70         printf("Usage: tinymix [-D card] [control id] [value to set]\n");
72     mixer_close(mixer);
74     return 0;
75 }
77 static void tinymix_list_controls(struct mixer *mixer)
78 {
79     struct mixer_ctl *ctl;
80     const char *type;
81     unsigned int num_ctls, num_values;
82     char buffer[256];
83     unsigned int i;
85     num_ctls = mixer_get_num_ctls(mixer);
87     printf("Number of controls: %d\n", num_ctls);
89     printf("ctl\ttype\tnum\t%-40s value\n", "name");
90     for (i = 0; i < num_ctls; i++) {
91         ctl = mixer_get_ctl(mixer, i);
93         mixer_ctl_get_name(ctl, buffer, sizeof(buffer));
94         type = mixer_ctl_get_type_string(ctl);
95         num_values = mixer_ctl_get_num_values(ctl);
96         printf("%d\t%s\t%d\t%-40s", i, type, num_values, buffer);
97         tinymix_detail_control(mixer, i, 0);
98     }
99 }
101 static void tinymix_print_enum(struct mixer_ctl *ctl, int print_all)
103     unsigned int num_enums;
104     char buffer[256];
105     unsigned int i;
107     num_enums = mixer_ctl_get_num_enums(ctl);
109     for (i = 0; i < num_enums; i++) {
110         mixer_ctl_get_enum_string(ctl, i, buffer, sizeof(buffer));
111         if (print_all)
112             printf("\t%s%s", mixer_ctl_get_value(ctl, 0) == (int)i ? ">" : "",
113                    buffer);
114         else if (mixer_ctl_get_value(ctl, 0) == (int)i)
115             printf(" %-s", buffer);
116     }
119 static void tinymix_detail_control(struct mixer *mixer, unsigned int id,
120                                    int print_all)
122     struct mixer_ctl *ctl;
123     enum mixer_ctl_type type;
124     unsigned int num_values;
125     char buffer[256];
126     unsigned int i;
127     int min, max;
129     if (id >= mixer_get_num_ctls(mixer)) {
130         fprintf(stderr, "Invalid mixer control\n");
131         return;
132     }
134     ctl = mixer_get_ctl(mixer, id);
136     mixer_ctl_get_name(ctl, buffer, sizeof(buffer));
137     type = mixer_ctl_get_type(ctl);
138     num_values = mixer_ctl_get_num_values(ctl);
140     if (print_all)
141         printf("%s:", buffer);
143     for (i = 0; i < num_values; i++) {
144         switch (type)
145         {
146         case MIXER_CTL_TYPE_INT:
147             printf(" %d", mixer_ctl_get_value(ctl, i));
148             break;
149         case MIXER_CTL_TYPE_BOOL:
150             printf(" %s", mixer_ctl_get_value(ctl, i) ? "On" : "Off");
151             break;
152         case MIXER_CTL_TYPE_ENUM:
153             tinymix_print_enum(ctl, print_all);
154             break;
155          case MIXER_CTL_TYPE_BYTE:
156             printf(" 0x%02x", mixer_ctl_get_value(ctl, i));
157             break;
158         default:
159             printf(" unknown");
160             break;
161         };
162     }
164     if (print_all) {
165         if (type == MIXER_CTL_TYPE_INT) {
166             min = mixer_ctl_get_range_min(ctl);
167             max = mixer_ctl_get_range_max(ctl);
168             printf(" (range %d->%d)", min, max);
169         }
170     }
171     printf("\n");
174 static void tinymix_set_value(struct mixer *mixer, unsigned int id,
175                               char *string)
177     struct mixer_ctl *ctl;
178     enum mixer_ctl_type type;
179     unsigned int num_values;
180     unsigned int i;
182     ctl = mixer_get_ctl(mixer, id);
183     type = mixer_ctl_get_type(ctl);
184     num_values = mixer_ctl_get_num_values(ctl);
186     if (isdigit(string[0])) {
187         int value = atoi(string);
189         for (i = 0; i < num_values; i++) {
190             if (mixer_ctl_set_value(ctl, i, value)) {
191                 fprintf(stderr, "Error: invalid value\n");
192                 return;
193             }
194         }
195     } else {
196         if (type == MIXER_CTL_TYPE_ENUM) {
197             if (mixer_ctl_set_enum_by_string(ctl, string))
198                 fprintf(stderr, "Error: invalid enum value\n");
199         } else {
200             fprintf(stderr, "Error: only enum types can be set with strings\n");
201         }
202     }