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
29 #include <sys/soundcard.h>
31 #include "include/common.h"
32 #include "include/config.h"
35 #define VERSION_TEXT \
36 "WMixer " VERSION " by timecop@japan.co.jp + skunk@mit.edu\n"
40 " -d <dsp> connect to remote X display\n" \
41 " -e <name> exclude channel, can be used many times\n" \
42 " -f <file> parse this config [~/.wmixrc]\n" \
43 " -h print this help\n" \
44 " -k disable grabing volume control keys\n" \
45 " -m <dev> mixer device [/dev/mixer]\n" \
46 " -v verbose -> id, long name, name\n" \
48 /* The global configuration */
49 struct _Config config
;
51 /* The default device used for Mixer control */
52 static const char default_mixer_device
[] = "/dev/mixer";
54 /* Default color for OSD */
55 const char default_osd_color
[] = "green";
59 * Sets the default values in configuration
61 void config_init(void)
63 memset(&config
, 0, sizeof(config
));
65 config
.mixer_device
= (char *) default_mixer_device
;
66 config
.mousewheel
= 1;
67 config
.scrolltext
= 1;
69 config
.wheel_button_up
= 4;
70 config
.wheel_button_down
= 5;
71 config
.scrollstep
= 0.03;
73 config
.osd_color
= (char *) default_osd_color
;
77 * Release memory associated with configuration
79 * This does not concern the complete configuration, only the parameters
80 * that are needed during startup but are not useful during run-time
82 void config_release(void)
89 if (config
.display_name
)
90 free(config
.display_name
);
92 if (config
.mixer_device
!= default_mixer_device
)
93 free(config
.mixer_device
);
95 if (config
.osd_color
!= default_osd_color
)
96 free(config
.osd_color
);
98 for (i
= 0; i
< SOUND_MIXER_NRDEVICES
; i
++) {
99 if (config
.exclude_channel
[i
])
100 free(config
.exclude_channel
[i
]);
107 * Parse Command-Line options
109 * Supposed to be called before reading config file, as there's an
110 * option to change its name
112 void parse_cli_options(int argc
, char **argv
)
115 int count_exclude
= 0;
118 opterr
= 0; /* We take charge of printing the error message */
119 config
.verbose
= false;
122 opt
= getopt(argc
, argv
, ":d:e:f:hkm:v");
128 fprintf(stderr
, "wmix:error: unknow option '-%c'\n", optopt
);
133 fprintf(stderr
, "wmix:error: missing argument for option '-%c'\n", optopt
);
138 if (config
.display_name
)
139 free(config
.display_name
);
140 config
.display_name
= strdup(optarg
);
144 if (count_exclude
< SOUND_MIXER_NRDEVICES
) {
145 config
.exclude_channel
[count_exclude
] = strdup(optarg
);
148 fprintf(stderr
, "Warning: You can't exclude this many channels\n");
152 if (config
.file
!= NULL
)
154 config
.file
= strdup(optarg
);
158 fputs(VERSION_TEXT
, stdout
);
159 fputs(HELP_TEXT
, stdout
);
164 config
.mmkeys
= false;
168 if (config
.mixer_device
!= default_mixer_device
)
169 free(config
.mixer_device
);
170 config
.mixer_device
= strdup(optarg
);
174 config
.verbose
= true;
181 config
.exclude_channel
[count_exclude
] = NULL
;
184 fprintf(stderr
, "wmix:error: argument '%s' not understood\n", argv
[optind
]);
192 fputs(VERSION_TEXT
, stdout
);
196 * Read configuration from a file
198 * The file name is taken from CLI if available, of falls back to
201 void config_read(void)
203 const char *filename
;
204 char buffer_fname
[512];
209 if (config
.file
!= NULL
) {
210 filename
= config
.file
;
214 home
= getenv("HOME");
216 fprintf(stderr
, "wmix: warning, could not get $HOME, can't load configuration file\n");
219 snprintf(buffer_fname
, sizeof(buffer_fname
), "%s/.wmixrc", home
);
220 filename
= buffer_fname
;
223 fp
= fopen(filename
, "r");
225 if (config
.file
!= NULL
) {
226 /* The config file was explicitely specified by user, tell him there's a problem */
227 fprintf(stderr
, "wmix: error, could not load configuration file \"%s\"\n", filename
);
230 /* Otherwise, it is acceptable if the file does not exist */
234 printf("Using configuration file: %s\n", filename
);
237 while (fgets(buf
, 512, fp
)) {
245 while (isspace(*ptr
))
248 if ((*ptr
== '\0') || (*ptr
== '#'))
251 /* Isolate the keyword */
254 fprintf(stderr
, "wmix:warning: syntax error at line %d in \"%s\", no keyword before '='\n",
269 fprintf(stderr
, "wmix:warning: syntax error at line %d in \"%s\", missing '='\n",
273 while (isspace(ptr
[-1]))
277 /* Isolate the value */
278 while (isspace(*value
))
286 while (isspace(ptr
[-1]))
290 /* Check what keyword we have */
291 if (strcmp(keyword
, "device") == 0) {
292 if (config
.mixer_device
== default_mixer_device
)
293 config
.mixer_device
= strdup(value
);
294 /* If not the default, keep the previous value because it was provided in the command-line */
296 } else if (strcmp(keyword
, "exclude") == 0) {
299 for (i
= 0; i
< SOUND_MIXER_NRDEVICES
; i
++) {
300 if (config
.exclude_channel
[i
] == NULL
) {
301 config
.exclude_channel
[i
] = strdup(value
);
305 if (strcmp(value
, config
.exclude_channel
[i
]) == 0)
308 } else if (strcmp(keyword
, "mousewheel") == 0) {
309 config
.mousewheel
= atoi(value
);
311 } else if (strcmp(keyword
, "osd") == 0) {
312 config
.osd
= atoi(value
);
314 } else if (strcmp(keyword
, "osdcolor") == 0) {
315 if (config
.osd_color
!= default_osd_color
)
316 free(config
.osd_color
);
317 config
.osd_color
= strdup(value
);
319 } else if (strcmp(keyword
, "scrolltext") == 0) {
320 config
.scrolltext
= atoi(value
);
322 } else if (strcmp(keyword
, "wheelbtn1") == 0) {
323 config
.wheel_button_up
= atoi(value
);
325 } else if (strcmp(keyword
, "wheelbtn2") == 0) {
326 config
.wheel_button_down
= atoi(value
);
328 } else if (strcmp(keyword
, "wheelstep") == 0) {
332 if (val
< 0.0 || val
> 100.0)
333 fprintf(stderr
, "wmix:error: value %f is out of range for wheelstep in %s at line %d\n",
334 val
, filename
, line
);
336 config
.scrollstep
= val
/ 100.0;
338 config
.scrollstep
= val
;
340 fprintf(stderr
, "wmix:error: value '%s' not understood for wheelstep in %s at line %d\n",
341 value
, filename
, line
);
343 fprintf(stderr
, "wmix:warning: unknow keyword '%s' at line %d of \"%s\", ignored\n",
344 keyword
, line
, filename
);