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 grabing 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: unknow 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
))
167 fprintf(stderr
, "Warning: Incorrect sound api specified, defaulting to alsa\n");
170 if (config
.display_name
)
171 free(config
.display_name
);
172 config
.display_name
= strdup(optarg
);
176 if (count_exclude
< EXCLUDE_MAX_COUNT
) {
177 config
.exclude_channel
[count_exclude
] = strdup(optarg
);
180 fprintf(stderr
, "Warning: You can't exclude this many channels\n");
184 if (config
.file
!= NULL
)
186 config
.file
= strdup(optarg
);
190 fputs(VERSION_TEXT
, stdout
);
191 fputs(HELP_TEXT
, stdout
);
196 config
.mmkeys
= false;
200 if (config
.mixer_device
!= default_mixer_device
)
201 free(config
.mixer_device
);
202 config
.mixer_device
= strdup(optarg
);
206 if (!parse_monitor_value(optarg
))
207 fprintf(stderr
, "wmix:warning: unreasonable monitor number provided on command line, ignoring\n");
211 config
.verbose
= true;
218 config
.exclude_channel
[count_exclude
] = NULL
;
221 fprintf(stderr
, "wmix:error: argument '%s' not understood\n", argv
[optind
]);
229 fputs(VERSION_TEXT
, stdout
);
233 * Read configuration from a file
235 * The file name is taken from CLI if available, of falls back to
238 void config_read(void)
240 const char *filename
;
241 char buffer_fname
[512];
246 if (config
.file
!= NULL
) {
247 filename
= config
.file
;
251 home
= getenv("HOME");
253 fprintf(stderr
, "wmix: warning, could not get $HOME, can't load configuration file\n");
256 snprintf(buffer_fname
, sizeof(buffer_fname
), "%s/.wmixrc", home
);
257 filename
= buffer_fname
;
260 fp
= fopen(filename
, "r");
262 if (config
.file
!= NULL
) {
263 /* The config file was explicitely specified by user, tell him there's a problem */
264 fprintf(stderr
, "wmix: error, could not load configuration file \"%s\"\n", filename
);
267 /* Otherwise, it is acceptable if the file does not exist */
271 printf("Using configuration file: %s\n", filename
);
274 while (fgets(buf
, 512, fp
)) {
282 while (isspace(*ptr
))
285 if ((*ptr
== '\0') || (*ptr
== '#'))
288 /* Isolate the keyword */
291 fprintf(stderr
, "wmix:warning: syntax error at line %d in \"%s\", no keyword before '='\n",
306 fprintf(stderr
, "wmix:warning: syntax error at line %d in \"%s\", missing '='\n",
310 while (isspace(ptr
[-1]))
314 /* Isolate the value */
315 while (isspace(*value
))
323 while (isspace(ptr
[-1]))
327 /* Check what keyword we have */
328 if (strcmp(keyword
, "device") == 0) {
329 if (config
.mixer_device
== default_mixer_device
)
330 config
.mixer_device
= strdup(value
);
331 /* If not the default, keep the previous value because it was provided in the command-line */
333 } else if (strcmp(keyword
, "exclude") == 0) {
336 for (i
= 0; i
< EXCLUDE_MAX_COUNT
; i
++) {
337 if (config
.exclude_channel
[i
] == NULL
) {
338 config
.exclude_channel
[i
] = strdup(value
);
339 config
.exclude_channel
[i
+1] = NULL
;
343 if (strcmp(value
, config
.exclude_channel
[i
]) == 0)
346 } else if (strcmp(keyword
, "mousewheel") == 0) {
347 config
.mousewheel
= atoi(value
);
349 } else if (strcmp(keyword
, "osd") == 0) {
350 config
.osd
= atoi(value
);
352 } else if (strcmp(keyword
, "osdcolor") == 0) {
353 if (config
.osd_color
!= default_osd_color
)
354 free(config
.osd_color
);
355 config
.osd_color
= strdup(value
);
357 } else if (strcmp(keyword
, "osdmonitor") == 0) {
358 if (!config
.osd_monitor_name
&&
359 config
.osd_monitor_number
== -1 &&
360 !parse_monitor_value(value
))
361 fprintf(stderr
, "wmix:warning: unreasonable monitor number in config, ignoring\n");
363 } else if (strcmp(keyword
, "scrolltext") == 0) {
364 config
.scrolltext
= atoi(value
);
366 } else if (strcmp(keyword
, "wheelbtn1") == 0) {
367 config
.wheel_button_up
= atoi(value
);
369 } else if (strcmp(keyword
, "wheelbtn2") == 0) {
370 config
.wheel_button_down
= atoi(value
);
372 } else if (strcmp(keyword
, "wheelstep") == 0) {
376 if (val
< 0.0 || val
> 100.0)
377 fprintf(stderr
, "wmix:error: value %f is out of range for wheelstep in %s at line %d\n",
378 val
, filename
, line
);
380 config
.scrollstep
= val
/ 100.0;
382 config
.scrollstep
= val
;
384 fprintf(stderr
, "wmix:error: value '%s' not understood for wheelstep in %s at line %d\n",
385 value
, filename
, line
);
387 fprintf(stderr
, "wmix:warning: unknow keyword '%s' at line %d of \"%s\", ignored\n",
388 keyword
, line
, filename
);
394 void config_set_defaults()
396 if (!config
.mixer_device
) {
398 config
.mixer_device
= (char *)default_card_name
;
399 else if (config
.api
== 1)
400 config
.mixer_device
= (char *)default_mixer_device
;
403 if (!config
.osd_monitor_name
&& config
.osd_monitor_number
== -1)
404 config
.osd_monitor_number
= 0;