tinymix.c 6.28 KB
Newer Older
Simon Wilson's avatar
Simon Wilson committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/* tinymix.c
**
** Copyright 2011, The Android Open Source Project
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are met:
**     * Redistributions of source code must retain the above copyright
**       notice, this list of conditions and the following disclaimer.
**     * Redistributions in binary form must reproduce the above copyright
**       notice, this list of conditions and the following disclaimer in the
**       documentation and/or other materials provided with the distribution.
**     * Neither the name of The Android Open Source Project nor the names of
**       its contributors may be used to endorse or promote products derived
**       from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY The Android Open Source Project ``AS IS'' AND
** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
** ARE DISCLAIMED. IN NO EVENT SHALL The Android Open Source Project BE LIABLE
** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
** DAMAGE.
*/

#include <tinyalsa/asoundlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

static void tinymix_list_controls(struct mixer *mixer);
Simon Wilson's avatar
Simon Wilson committed
35 36
static void tinymix_detail_control(struct mixer *mixer, unsigned int id,
                                   int print_all);
Simon Wilson's avatar
Simon Wilson committed
37 38
static void tinymix_set_value(struct mixer *mixer, unsigned int id,
                              char *value);
Simon Wilson's avatar
Simon Wilson committed
39
static void tinymix_print_enum(struct mixer_ctl *ctl, int print_all);
Simon Wilson's avatar
Simon Wilson committed
40 41 42 43

int main(int argc, char **argv)
{
    struct mixer *mixer;
Simon Wilson's avatar
Simon Wilson committed
44
    int card = 0;
Simon Wilson's avatar
Simon Wilson committed
45

Simon Wilson's avatar
Simon Wilson committed
46 47 48 49 50 51 52 53 54 55 56 57
    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(card);
Simon Wilson's avatar
Simon Wilson committed
58 59 60 61
    if (!mixer) {
        fprintf(stderr, "Failed to open mixer\n");
        return EXIT_FAILURE;
    }
Simon Wilson's avatar
Simon Wilson committed
62 63 64 65

    if (argc == 1)
        tinymix_list_controls(mixer);
    else if (argc == 2)
Simon Wilson's avatar
Simon Wilson committed
66
        tinymix_detail_control(mixer, atoi(argv[1]), 1);
Simon Wilson's avatar
Simon Wilson committed
67 68 69
    else if (argc == 3)
        tinymix_set_value(mixer, atoi(argv[1]), argv[2]);
    else
Simon Wilson's avatar
Simon Wilson committed
70
        printf("Usage: tinymix [-D card] [control id] [value to set]\n");
Simon Wilson's avatar
Simon Wilson committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

    mixer_close(mixer);

    return 0;
}

static void tinymix_list_controls(struct mixer *mixer)
{
    struct mixer_ctl *ctl;
    const char *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);

Simon Wilson's avatar
Simon Wilson committed
89
    printf("ctl\ttype\tnum\t%-40s value\n", "name");
Simon Wilson's avatar
Simon Wilson committed
90 91 92 93 94 95
    for (i = 0; i < num_ctls; i++) {
        ctl = mixer_get_ctl(mixer, i);

        mixer_ctl_get_name(ctl, buffer, sizeof(buffer));
        type = mixer_ctl_get_type_string(ctl);
        num_values = mixer_ctl_get_num_values(ctl);
Simon Wilson's avatar
Simon Wilson committed
96 97
        printf("%d\t%s\t%d\t%-40s", i, type, num_values, buffer);
        tinymix_detail_control(mixer, i, 0);
Simon Wilson's avatar
Simon Wilson committed
98 99 100
    }
}

Simon Wilson's avatar
Simon Wilson committed
101
static void tinymix_print_enum(struct mixer_ctl *ctl, int print_all)
Simon Wilson's avatar
Simon Wilson committed
102 103 104 105 106 107 108 109 110
{
    unsigned int num_enums;
    char buffer[256];
    unsigned int i;

    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));
Simon Wilson's avatar
Simon Wilson committed
111 112 113 114 115
        if (print_all)
            printf("\t%s%s", mixer_ctl_get_value(ctl, 0) == (int)i ? ">" : "",
                   buffer);
        else if (mixer_ctl_get_value(ctl, 0) == (int)i)
            printf(" %-s", buffer);
Simon Wilson's avatar
Simon Wilson committed
116 117 118
    }
}

Simon Wilson's avatar
Simon Wilson committed
119 120
static void tinymix_detail_control(struct mixer *mixer, unsigned int id,
                                   int print_all)
Simon Wilson's avatar
Simon Wilson committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
{
    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)) {
        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);

Simon Wilson's avatar
Simon Wilson committed
140 141 142
    if (print_all)
        printf("%s:", buffer);

Simon Wilson's avatar
Simon Wilson committed
143 144 145 146 147 148 149 150 151 152
    for (i = 0; i < num_values; i++) {
        switch (type)
        {
        case MIXER_CTL_TYPE_INT:
            printf(" %d", mixer_ctl_get_value(ctl, i));
            break;
        case MIXER_CTL_TYPE_BOOL:
            printf(" %s", mixer_ctl_get_value(ctl, i) ? "On" : "Off");
            break;
        case MIXER_CTL_TYPE_ENUM:
Simon Wilson's avatar
Simon Wilson committed
153
            tinymix_print_enum(ctl, print_all);
Simon Wilson's avatar
Simon Wilson committed
154
            break;
Simon Wilson's avatar
Simon Wilson committed
155 156 157
         case MIXER_CTL_TYPE_BYTE:
            printf(" 0x%02x", mixer_ctl_get_value(ctl, i));
            break;
Simon Wilson's avatar
Simon Wilson committed
158 159 160 161 162
        default:
            printf(" unknown");
            break;
        };
    }
Simon Wilson's avatar
Simon Wilson committed
163 164 165 166 167 168 169

    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);
        }
Simon Wilson's avatar
Simon Wilson committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
    }
    printf("\n");
}

static void tinymix_set_value(struct mixer *mixer, unsigned int id,
                              char *string)
{
    struct mixer_ctl *ctl;
    enum mixer_ctl_type type;
    unsigned int num_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(string[0])) {
        int value = atoi(string);

        for (i = 0; i < num_values; i++) {
            if (mixer_ctl_set_value(ctl, i, value)) {
                fprintf(stderr, "Error: invalid value\n");
                return;
            }
        }
    } else {
        if (type == MIXER_CTL_TYPE_ENUM) {
            if (mixer_ctl_set_enum_by_string(ctl, string))
                fprintf(stderr, "Error: invalid enum value\n");
        } else {
            fprintf(stderr, "Error: only enum types can be set with strings\n");
        }
    }
}