Imported Upstream version 1.0.28
[platform/upstream/alsa-utils.git] / alsamixer / cli.c
1 /*
2  * alsamixer - curses mixer for the ALSA project
3  * Copyright (c) 1998,1999 Tim Janik
4  *                         Jaroslav Kysela <perex@perex.cz>
5  * Copyright (c) 2009      Clemens Ladisch <clemens@ladisch.de>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "aconfig.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <locale.h>
25 #include <getopt.h>
26 #include <alsa/asoundlib.h>
27 #include "gettext_curses.h"
28 #include "mixer_widget.h"
29 #include "mainloop.h"
30
31 static int use_color = 1;
32 static struct snd_mixer_selem_regopt selem_regopt = {
33         .ver = 1,
34         .abstract = SND_MIXER_SABSTRACT_NONE,
35         .device = "default",
36 };
37
38 static void show_help(void)
39 {
40         puts(_("Usage: alsamixer [options]"));
41         puts(_("Useful options:\n"
42                "  -h, --help              this help\n"
43                "  -c, --card=NUMBER       sound card number or id\n"
44                "  -D, --device=NAME       mixer device name\n"
45                "  -V, --view=MODE         starting view mode: playback/capture/all"));
46         puts(_("Debugging options:\n"
47                "  -g, --no-color          toggle using of colors\n"
48                "  -a, --abstraction=NAME  mixer abstraction level: none/basic"));
49 }
50
51 static void parse_options(int argc, char *argv[])
52 {
53         static const char short_options[] = "hc:D:V:gsa:";
54         static const struct option long_options[] = {
55                 { .name = "help", .val = 'h' },
56                 { .name = "card", .has_arg = 1, .val = 'c' },
57                 { .name = "device", .has_arg = 1, .val = 'D' },
58                 { .name = "view", .has_arg = 1, .val = 'V' },
59                 { .name = "no-color", .val = 'g' },
60                 { .name = "abstraction", .has_arg = 1, .val = 'a' },
61                 { }
62         };
63         int option;
64         int card_index;
65         static char name_buf[16];
66
67         while ((option = getopt_long(argc, argv, short_options,
68                                      long_options, NULL)) != -1) {
69                 switch (option) {
70                 case '?':
71                 case 'h':
72                         show_help();
73                         exit(EXIT_SUCCESS);
74                 case 'c':
75                         card_index = snd_card_get_index(optarg);
76                         if (card_index < 0) {
77                                 fprintf(stderr, _("invalid card index: %s\n"), optarg);
78                                 goto fail;
79                         }
80                         sprintf(name_buf, "hw:%d", card_index);
81                         selem_regopt.device = name_buf;
82                         break;
83                 case 'D':
84                         selem_regopt.device = optarg;
85                         break;
86                 case 'V':
87                         if (*optarg == 'p' || *optarg == 'P')
88                                 view_mode = VIEW_MODE_PLAYBACK;
89                         else if (*optarg == 'c' || *optarg == 'C')
90                                 view_mode = VIEW_MODE_CAPTURE;
91                         else
92                                 view_mode = VIEW_MODE_ALL;
93                         break;
94                 case 'g':
95                         use_color = !use_color;
96                         break;
97                 case 'a':
98                         if (!strcmp(optarg, "none"))
99                                 selem_regopt.abstract = SND_MIXER_SABSTRACT_NONE;
100                         else if (!strcmp(optarg, "basic"))
101                                 selem_regopt.abstract = SND_MIXER_SABSTRACT_BASIC;
102                         else {
103                                 fprintf(stderr, _("unknown abstraction level: %s\n"), optarg);
104                                 goto fail;
105                         }
106                         break;
107                 default:
108                         fprintf(stderr, _("unknown option: %c\n"), option);
109 fail:
110                         fputs(_("try `alsamixer --help' for more information\n"), stderr);
111                         exit(EXIT_FAILURE);
112                 }
113         }
114 }
115
116 int main(int argc, char *argv[])
117 {
118         if (!isatty(fileno(stdin)))
119                 return 0;
120
121         setlocale(LC_ALL, "");
122 #ifdef ENABLE_NLS_IN_CURSES
123         textdomain(PACKAGE);
124 #endif
125
126         parse_options(argc, argv);
127
128         create_mixer_object(&selem_regopt);
129
130         initialize_curses(use_color);
131
132         create_mixer_widget();
133
134         mainloop();
135
136         app_shutdown();
137         return 0;
138 }