wmpop3: Add version 0.5.6a to repository.
[dockapps.git] / wmix / config.c
blob0b8001397f2c0932ff2877f768ef275dd5394a6b
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 = -1;
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 config.api = 0;
168 else
169 fprintf(stderr, "wmix:warning: incorrect sound api specified on command line, ignoring\n");
170 break;
171 case 'd':
172 if (config.display_name)
173 free(config.display_name);
174 config.display_name = strdup(optarg);
175 break;
177 case 'e':
178 if (count_exclude < EXCLUDE_MAX_COUNT) {
179 config.exclude_channel[count_exclude] = strdup(optarg);
180 count_exclude++;
181 } else
182 fprintf(stderr, "Warning: You can't exclude this many channels\n");
183 break;
185 case 'f':
186 if (config.file != NULL)
187 free(config.file);
188 config.file = strdup(optarg);
189 break;
191 case 'h':
192 fputs(VERSION_TEXT, stdout);
193 fputs(HELP_TEXT, stdout);
194 exit(0);
195 break;
197 case 'k':
198 config.mmkeys = false;
199 break;
201 case 'm':
202 if (config.mixer_device != default_mixer_device)
203 free(config.mixer_device);
204 config.mixer_device = strdup(optarg);
205 break;
207 case 'o':
208 if (!parse_monitor_value(optarg))
209 fprintf(stderr, "wmix:warning: unreasonable monitor number provided on command line, ignoring\n");
210 break;
212 case 'v':
213 config.verbose = true;
214 break;
216 default:
217 break;
220 config.exclude_channel[count_exclude] = NULL;
222 if (optind < argc) {
223 fprintf(stderr, "wmix:error: argument '%s' not understood\n", argv[optind]);
224 error_found = true;
227 if (error_found)
228 exit(EXIT_FAILURE);
230 if (config.verbose)
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
238 * a default name.
240 void config_read(void)
242 const char *filename;
243 char buffer_fname[512];
244 FILE *fp;
245 int line;
246 char buf[512];
248 if (config.file != NULL) {
249 filename = config.file;
250 } else {
251 const char *home;
253 home = getenv("HOME");
254 if (home == NULL) {
255 fprintf(stderr, "wmix: warning, could not get $HOME, can't load configuration file\n");
256 return;
258 snprintf(buffer_fname, sizeof(buffer_fname), "%s/.wmixrc", home);
259 filename = buffer_fname;
262 fp = fopen(filename, "r");
263 if (fp == NULL) {
264 if (config.file != NULL) {
265 /* The config file was explicitely specified by user, tell him there's a problem */
266 fprintf(stderr, "wmix: error, could not load configuration file \"%s\"\n", filename);
267 exit(EXIT_FAILURE);
269 /* Otherwise, it is acceptable if the file does not exist */
270 return;
272 if (config.verbose)
273 printf("Using configuration file: %s\n", filename);
275 line = 0;
276 while (fgets(buf, 512, fp)) {
277 char *ptr;
278 char *keyword;
279 char *value;
281 line++;
283 ptr = buf;
284 while (isspace(*ptr))
285 ptr++;
287 if ((*ptr == '\0') || (*ptr == '#'))
288 continue;
290 /* Isolate the keyword */
291 keyword = ptr;
292 if (*ptr == '=') {
293 fprintf(stderr, "wmix:warning: syntax error at line %d in \"%s\", no keyword before '='\n",
294 line, filename);
295 continue;
297 value = NULL;
298 while (*ptr) {
299 if (*ptr == '=') {
300 value = ptr + 1;
301 break;
303 if (*ptr == '#')
304 break;
305 ptr++;
307 if (value == NULL) {
308 fprintf(stderr, "wmix:warning: syntax error at line %d in \"%s\", missing '='\n",
309 line, filename);
310 continue;
312 while (isspace(ptr[-1]))
313 ptr--;
314 *ptr = '\0';
316 /* Isolate the value */
317 while (isspace(*value))
318 value++;
319 ptr = value;
320 while (*ptr) {
321 if (*ptr == '#')
322 break;
323 ptr++;
325 while (isspace(ptr[-1]))
326 ptr--;
327 *ptr = '\0';
329 /* Check what keyword we have */
330 if (strcmp(keyword, "api") == 0) {
331 if (config.api == -1) {
332 if(!strcmp("oss", value))
333 config.api = 1;
334 else if (!strcmp("alsa", value))
335 config.api = 0;
336 else
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) {
345 int i;
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;
351 break;
354 if (strcmp(value, config.exclude_channel[i]) == 0)
355 break;
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) {
384 double val;
386 val = atof(value);
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);
390 else if (val >= 1.0)
391 config.scrollstep = val / 100.0;
392 else if (val > 0.0)
393 config.scrollstep = val;
394 else
395 fprintf(stderr, "wmix:error: value '%s' not understood for wheelstep in %s at line %d\n",
396 value, filename, line);
397 } else {
398 fprintf(stderr, "wmix:warning: unknow keyword '%s' at line %d of \"%s\", ignored\n",
399 keyword, line, filename);
402 fclose(fp);
405 void config_set_defaults()
407 if (config.api == -1)
408 config.api = 0;
410 if (!config.mixer_device) {
411 if (config.api == 0)
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;