1 /* WMix -- a mixer using the OSS mixer API.
2 * Copyright (C) 2014 Christophe CURIS for the WindowMaker Team
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 * config.c: functions related to loading the configuration, both from
20 * command line options and from file
30 #include <sys/soundcard.h>
32 #include "include/common.h"
33 #include "include/config.h"
34 #include "include/misc.h"
36 #define VERSION_TEXT \
37 "WMixer " VERSION " by timecop@japan.co.jp + skunk@mit.edu\n"
41 " -a <api> use this sound api (oss or alsa) [alsa]\n" \
42 " -d <dsp> connect to remote X display\n" \
43 " -e <name> exclude channel, can be used many times\n" \
44 " -f <file> parse this config [~/.wmixrc]\n" \
45 " -h print this help\n" \
46 " -k disable grabbing volume control keys\n" \
47 " -m <dev> oss mixer device [/dev/mixer]\n" \
48 " or alsa card name [default]\n" \
49 " -o <num> display osd on this monitor number or name [0]\n" \
50 " use -1 to disable osd\n" \
51 " -v verbose -> id, long name, name\n" \
53 /* The global configuration */
54 struct _Config config
;
56 /* The default device used for Mixer control */
57 static const char default_mixer_device
[] = "/dev/mixer";
58 static const char default_card_name
[] = "default";
59 /* Default color for OSD */
60 const char default_osd_color
[] = "green";
64 * Sets the default values in configuration
66 void config_init(void)
68 memset(&config
, 0, sizeof(config
));
70 config
.mixer_device
= NULL
;
71 config
.mousewheel
= 1;
72 config
.scrolltext
= 1;
74 config
.wheel_button_up
= 4;
75 config
.wheel_button_down
= 5;
76 config
.scrollstep
= 0.03;
78 config
.osd_color
= (char *) default_osd_color
;
79 config
.osd_monitor_number
= -1;
80 config
.osd_monitor_name
= NULL
;
84 * Release memory associated with configuration
86 * This does not concern the complete configuration, only the parameters
87 * that are needed during startup but are not useful during run-time
89 void config_release(void)
96 if (config
.display_name
)
97 free(config
.display_name
);
99 if (config
.mixer_device
!= default_mixer_device
100 && config
.mixer_device
!= default_card_name
)
101 free(config
.mixer_device
);
103 if (config
.osd_color
!= default_osd_color
)
104 free(config
.osd_color
);
106 for (i
= 0; i
< EXCLUDE_MAX_COUNT
; i
++) {
107 if (config
.exclude_channel
[i
])
108 free(config
.exclude_channel
[i
]);
114 bool parse_monitor_value(char *value
)
117 long mon
= strtol(value
, &end
, 10);
118 if (end
== value
+ strlen(value
)) {
119 if ((mon
> INT_MAX
) || (mon
< -1)) {
125 config
.osd_monitor_number
= (int)mon
;
128 config
.osd_monitor_name
= strdup(value
);
134 * Parse Command-Line options
136 * Supposed to be called before reading config file, as there's an
137 * option to change its name
139 void parse_cli_options(int argc
, char **argv
)
142 int count_exclude
= 0;
145 opterr
= 0; /* We take charge of printing the error message */
146 config
.verbose
= false;
149 opt
= getopt(argc
, argv
, ":a:d:e:f:hkm:o:v");
155 fprintf(stderr
, "wmix:error: unknown option '-%c'\n", optopt
);
160 fprintf(stderr
, "wmix:error: missing argument for option '-%c'\n", optopt
);
164 if(!strcmp("oss", optarg
))
166 else if (!strcmp("alsa", optarg
))
169 fprintf(stderr
, "wmix:warning: incorrect sound api specified on command line, ignoring\n");
172 if (config
.display_name
)
173 free(config
.display_name
);
174 config
.display_name
= strdup(optarg
);
178 if (count_exclude
< EXCLUDE_MAX_COUNT
) {
179 config
.exclude_channel
[count_exclude
] = strdup(optarg
);
182 fprintf(stderr
, "Warning: You can't exclude this many channels\n");
186 if (config
.file
!= NULL
)
188 config
.file
= strdup(optarg
);
192 fputs(VERSION_TEXT
, stdout
);
193 fputs(HELP_TEXT
, stdout
);
198 config
.mmkeys
= false;
202 if (config
.mixer_device
!= default_mixer_device
)
203 free(config
.mixer_device
);
204 config
.mixer_device
= strdup(optarg
);
208 if (!parse_monitor_value(optarg
))
209 fprintf(stderr
, "wmix:warning: unreasonable monitor number provided on command line, ignoring\n");
213 config
.verbose
= true;
220 config
.exclude_channel
[count_exclude
] = NULL
;
223 fprintf(stderr
, "wmix:error: argument '%s' not understood\n", argv
[optind
]);
231 fputs(VERSION_TEXT
, stdout
);
235 * Read configuration from a file
237 * The file name is taken from CLI if available, of falls back to
240 void config_read(void)
242 const char *filename
;
243 char buffer_fname
[512];
248 if (config
.file
!= NULL
) {
249 filename
= config
.file
;
253 home
= getenv("HOME");
255 fprintf(stderr
, "wmix: warning, could not get $HOME, can't load configuration file\n");
258 snprintf(buffer_fname
, sizeof(buffer_fname
), "%s/.wmixrc", home
);
259 filename
= buffer_fname
;
262 fp
= fopen(filename
, "r");
264 if (config
.file
!= NULL
) {
265 /* The config file was explicitly specified by user, tell him there's a problem */
266 fprintf(stderr
, "wmix: error, could not load configuration file \"%s\"\n", filename
);
269 /* Otherwise, it is acceptable if the file does not exist */
273 printf("Using configuration file: %s\n", filename
);
276 while (fgets(buf
, 512, fp
)) {
284 while (isspace(*ptr
))
287 if ((*ptr
== '\0') || (*ptr
== '#'))
290 /* Isolate the keyword */
293 fprintf(stderr
, "wmix:warning: syntax error at line %d in \"%s\", no keyword before '='\n",
308 fprintf(stderr
, "wmix:warning: syntax error at line %d in \"%s\", missing '='\n",
312 while (isspace(ptr
[-1]))
316 /* Isolate the value */
317 while (isspace(*value
))
325 while (isspace(ptr
[-1]))
329 /* Check what keyword we have */
330 if (strcmp(keyword
, "api") == 0) {
331 if (config
.api
== -1) {
332 if(!strcmp("oss", value
))
334 else if (!strcmp("alsa", value
))
337 fprintf(stderr
, "wmix:warning: incorrect sound api in config, ignoring\n");
339 } else if (strcmp(keyword
, "device") == 0) {
340 if (config
.mixer_device
== default_mixer_device
)
341 config
.mixer_device
= strdup(value
);
342 /* If not the default, keep the previous value because it was provided in the command-line */
344 } else if (strcmp(keyword
, "exclude") == 0) {
347 for (i
= 0; i
< EXCLUDE_MAX_COUNT
; i
++) {
348 if (config
.exclude_channel
[i
] == NULL
) {
349 config
.exclude_channel
[i
] = strdup(value
);
350 config
.exclude_channel
[i
+1] = NULL
;
354 if (strcmp(value
, config
.exclude_channel
[i
]) == 0)
357 } else if (strcmp(keyword
, "mousewheel") == 0) {
358 config
.mousewheel
= atoi(value
);
360 } else if (strcmp(keyword
, "osd") == 0) {
361 config
.osd
= atoi(value
);
363 } else if (strcmp(keyword
, "osdcolor") == 0) {
364 if (config
.osd_color
!= default_osd_color
)
365 free(config
.osd_color
);
366 config
.osd_color
= strdup(value
);
368 } else if (strcmp(keyword
, "osdmonitor") == 0) {
369 if (!config
.osd_monitor_name
&&
370 config
.osd_monitor_number
== -1 &&
371 !parse_monitor_value(value
))
372 fprintf(stderr
, "wmix:warning: unreasonable monitor number in config, ignoring\n");
374 } else if (strcmp(keyword
, "scrolltext") == 0) {
375 config
.scrolltext
= atoi(value
);
377 } else if (strcmp(keyword
, "wheelbtn1") == 0) {
378 config
.wheel_button_up
= atoi(value
);
380 } else if (strcmp(keyword
, "wheelbtn2") == 0) {
381 config
.wheel_button_down
= atoi(value
);
383 } else if (strcmp(keyword
, "wheelstep") == 0) {
387 if (val
< 0.0 || val
> 100.0)
388 fprintf(stderr
, "wmix:error: value %f is out of range for wheelstep in %s at line %d\n",
389 val
, filename
, line
);
391 config
.scrollstep
= val
/ 100.0;
393 config
.scrollstep
= val
;
395 fprintf(stderr
, "wmix:error: value '%s' not understood for wheelstep in %s at line %d\n",
396 value
, filename
, line
);
398 fprintf(stderr
, "wmix:warning: unknown keyword '%s' at line %d of \"%s\", ignored\n",
399 keyword
, line
, filename
);
405 void config_set_defaults()
407 if (config
.api
== -1)
410 if (!config
.mixer_device
) {
412 config
.mixer_device
= (char *)default_card_name
;
413 else if (config
.api
== 1)
414 config
.mixer_device
= (char *)default_mixer_device
;
417 if (!config
.osd_monitor_name
&& config
.osd_monitor_number
== -1)
418 config
.osd_monitor_number
= 0;