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 #include "configparser.h"
31
32 static int use_color = 1;
33 static int use_mouse = 1;
34 static const char* config_file = CONFIG_DEFAULT;
35 static struct snd_mixer_selem_regopt selem_regopt = {
36 .ver = 1,
37 .abstract = SND_MIXER_SABSTRACT_NONE,
38 .device = "default",
39 };
40
show_help(void)41 static void show_help(void)
42 {
43 puts(_("Usage: alsamixer [options]"));
44 puts(_("Useful options:\n"
45 " -h, --help this help\n"
46 " -c, --card=NUMBER sound card number or id\n"
47 " -D, --device=NAME mixer device name\n"
48 " -m, --mouse enable mouse\n"
49 " -M, --no-mouse disable mouse\n"
50 " -f, --config=FILE configuration file\n"
51 " -F, --no-config do not load configuration file\n"
52 " -V, --view=MODE starting view mode: playback/capture/all"));
53 puts(_("Debugging options:\n"
54 " -g, --no-color toggle using of colors\n"
55 " -a, --abstraction=NAME mixer abstraction level: none/basic"));
56 }
57
parse_options(int argc,char * argv[])58 static void parse_options(int argc, char *argv[])
59 {
60 static const char short_options[] = "hc:D:f:FmMV:gsa:";
61 static const struct option long_options[] = {
62 { .name = "help", .val = 'h' },
63 { .name = "card", .has_arg = 1, .val = 'c' },
64 { .name = "config", .has_arg = 1, .val = 'f' },
65 { .name = "no-config", .val = 'F' },
66 { .name = "device", .has_arg = 1, .val = 'D' },
67 { .name = "mouse", .val = 'm' },
68 { .name = "no-mouse", .val = 'M' },
69 { .name = "view", .has_arg = 1, .val = 'V' },
70 { .name = "no-color", .val = 'g' },
71 { .name = "abstraction", .has_arg = 1, .val = 'a' },
72 { 0 }
73 };
74 int option;
75 int card_index;
76 static char name_buf[24];
77
78 while ((option = getopt_long(argc, argv, short_options,
79 long_options, NULL)) != -1) {
80 switch (option) {
81 case '?':
82 case 'h':
83 show_help();
84 exit(EXIT_SUCCESS);
85 case 'c':
86 card_index = snd_card_get_index(optarg);
87 if (card_index < 0) {
88 fprintf(stderr, _("invalid card index: %s\n"), optarg);
89 goto fail;
90 }
91 #if defined(SND_LIB_VER) && SND_LIB_VER(1, 2, 5) <= SND_LIB_VERSION
92 sprintf(name_buf, "sysdefault:%d", card_index);
93 #else
94 sprintf(name_buf, "hw:%d", card_index);
95 #endif
96 selem_regopt.device = name_buf;
97 break;
98 case 'D':
99 selem_regopt.device = optarg;
100 break;
101 case 'f':
102 config_file = optarg;
103 break;
104 case 'F':
105 config_file = NULL;
106 break;
107 case 'm':
108 use_mouse = 1;
109 break;
110 case 'M':
111 use_mouse = 0;
112 break;
113 case 'V':
114 if (*optarg == 'p' || *optarg == 'P')
115 view_mode = VIEW_MODE_PLAYBACK;
116 else if (*optarg == 'c' || *optarg == 'C')
117 view_mode = VIEW_MODE_CAPTURE;
118 else
119 view_mode = VIEW_MODE_ALL;
120 break;
121 case 'g':
122 use_color = !use_color;
123 break;
124 case 'a':
125 if (!strcmp(optarg, "none"))
126 selem_regopt.abstract = SND_MIXER_SABSTRACT_NONE;
127 else if (!strcmp(optarg, "basic"))
128 selem_regopt.abstract = SND_MIXER_SABSTRACT_BASIC;
129 else {
130 fprintf(stderr, _("unknown abstraction level: %s\n"), optarg);
131 goto fail;
132 }
133 break;
134 default:
135 fprintf(stderr, _("unknown option: %c\n"), option);
136 fail:
137 fputs(_("try `alsamixer --help' for more information\n"), stderr);
138 exit(EXIT_FAILURE);
139 }
140 }
141 }
142
main(int argc,char * argv[])143 int main(int argc, char *argv[])
144 {
145 if (!isatty(fileno(stdin)))
146 return 0;
147
148 setlocale(LC_ALL, "");
149 #ifdef ENABLE_NLS_IN_CURSES
150 textdomain(PACKAGE);
151 #endif
152
153 parse_options(argc, argv);
154
155 if (config_file == CONFIG_DEFAULT)
156 parse_default_config_file();
157 else if (config_file)
158 parse_config_file(config_file);
159
160 create_mixer_object(&selem_regopt);
161
162 initialize_curses(use_color, use_mouse);
163
164 create_mixer_widget();
165
166 mainloop();
167
168 app_shutdown();
169 return 0;
170 }
171