Better handling of configuration defaults for the new settings
[dockapps.git] / wmix / config.c
blob8fdb293d583bcdb2027ffa3255c52e5922fa7211
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
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <getopt.h>
28 #include <limits.h>
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"
39 #define HELP_TEXT \
40 "usage:\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));
69 config.api = 0;
70 config.mixer_device = NULL;
71 config.mousewheel = 1;
72 config.scrolltext = 1;
73 config.mmkeys = 1;
74 config.wheel_button_up = 4;
75 config.wheel_button_down = 5;
76 config.scrollstep = 0.03;
77 config.osd = 1;
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)
91 int i;
93 if (config.file)
94 free(config.file);
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]);
109 else
110 break;
114 bool parse_monitor_value(char *value)
116 char *end;
117 long mon = strtol(value, &end, 10);
118 if (end == value + strlen(value)) {
119 if ((mon > INT_MAX) || (mon < -1)) {
120 return false;
121 } else {
122 if (mon == -1)
123 config.osd = 0;
124 else
125 config.osd_monitor_number = (int)mon;
127 } else {
128 config.osd_monitor_name = strdup(value);
130 return true;
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)
141 int opt;
142 int count_exclude = 0;
143 bool error_found;
145 opterr = 0; /* We take charge of printing the error message */
146 config.verbose = false;
147 error_found = false;
148 for (;;) {
149 opt = getopt(argc, argv, ":a:d:e:f:hkm:o:v");
150 if (opt == -1)
151 break;
153 switch (opt) {
154 case '?':
155 fprintf(stderr, "wmix:error: unknow option '-%c'\n", optopt);
156 error_found = true;
157 break;
159 case ':':
160 fprintf(stderr, "wmix:error: missing argument for option '-%c'\n", optopt);
161 error_found = true;
162 break;
163 case 'a':
164 if(!strcmp("oss", optarg))
165 config.api = 1;
166 else if (strcmp("alsa", optarg))
167 fprintf(stderr, "Warning: Incorrect sound api specified, defaulting to alsa\n");
168 break;
169 case 'd':
170 if (config.display_name)
171 free(config.display_name);
172 config.display_name = strdup(optarg);
173 break;
175 case 'e':
176 if (count_exclude < EXCLUDE_MAX_COUNT) {
177 config.exclude_channel[count_exclude] = strdup(optarg);
178 count_exclude++;
179 } else
180 fprintf(stderr, "Warning: You can't exclude this many channels\n");
181 break;
183 case 'f':
184 if (config.file != NULL)
185 free(config.file);
186 config.file = strdup(optarg);
187 break;
189 case 'h':
190 fputs(VERSION_TEXT, stdout);
191 fputs(HELP_TEXT, stdout);
192 exit(0);
193 break;
195 case 'k':
196 config.mmkeys = false;
197 break;
199 case 'm':
200 if (config.mixer_device != default_mixer_device)
201 free(config.mixer_device);
202 config.mixer_device = strdup(optarg);
203 break;
205 case 'o':
206 if (!parse_monitor_value(optarg))
207 fprintf(stderr, "wmix:warning: unreasonable monitor number provided on command line, ignoring\n");
208 break;
210 case 'v':
211 config.verbose = true;
212 break;
214 default:
215 break;
218 config.exclude_channel[count_exclude] = NULL;
220 if (optind < argc) {
221 fprintf(stderr, "wmix:error: argument '%s' not understood\n", argv[optind]);
222 error_found = true;
225 if (error_found)
226 exit(EXIT_FAILURE);
228 if (config.verbose)
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
236 * a default name.
238 void config_read(void)
240 const char *filename;
241 char buffer_fname[512];
242 FILE *fp;
243 int line;
244 char buf[512];
246 if (config.file != NULL) {
247 filename = config.file;
248 } else {
249 const char *home;
251 home = getenv("HOME");
252 if (home == NULL) {
253 fprintf(stderr, "wmix: warning, could not get $HOME, can't load configuration file\n");
254 return;
256 snprintf(buffer_fname, sizeof(buffer_fname), "%s/.wmixrc", home);
257 filename = buffer_fname;
260 fp = fopen(filename, "r");
261 if (fp == NULL) {
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);
265 exit(EXIT_FAILURE);
267 /* Otherwise, it is acceptable if the file does not exist */
268 return;
270 if (config.verbose)
271 printf("Using configuration file: %s\n", filename);
273 line = 0;
274 while (fgets(buf, 512, fp)) {
275 char *ptr;
276 char *keyword;
277 char *value;
279 line++;
281 ptr = buf;
282 while (isspace(*ptr))
283 ptr++;
285 if ((*ptr == '\0') || (*ptr == '#'))
286 continue;
288 /* Isolate the keyword */
289 keyword = ptr;
290 if (*ptr == '=') {
291 fprintf(stderr, "wmix:warning: syntax error at line %d in \"%s\", no keyword before '='\n",
292 line, filename);
293 continue;
295 value = NULL;
296 while (*ptr) {
297 if (*ptr == '=') {
298 value = ptr + 1;
299 break;
301 if (*ptr == '#')
302 break;
303 ptr++;
305 if (value == NULL) {
306 fprintf(stderr, "wmix:warning: syntax error at line %d in \"%s\", missing '='\n",
307 line, filename);
308 continue;
310 while (isspace(ptr[-1]))
311 ptr--;
312 *ptr = '\0';
314 /* Isolate the value */
315 while (isspace(*value))
316 value++;
317 ptr = value;
318 while (*ptr) {
319 if (*ptr == '#')
320 break;
321 ptr++;
323 while (isspace(ptr[-1]))
324 ptr--;
325 *ptr = '\0';
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) {
334 int i;
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;
340 break;
343 if (strcmp(value, config.exclude_channel[i]) == 0)
344 break;
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) {
373 double val;
375 val = atof(value);
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);
379 else if (val >= 1.0)
380 config.scrollstep = val / 100.0;
381 else if (val > 0.0)
382 config.scrollstep = val;
383 else
384 fprintf(stderr, "wmix:error: value '%s' not understood for wheelstep in %s at line %d\n",
385 value, filename, line);
386 } else {
387 fprintf(stderr, "wmix:warning: unknow keyword '%s' at line %d of \"%s\", ignored\n",
388 keyword, line, filename);
391 fclose(fp);
394 void config_set_defaults()
396 if (!config.mixer_device) {
397 if (config.api == 0)
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;