wmix: rewrote parser of config file to report problems to user
[dockapps.git] / wmix / config.c
blob317033a341714530c7c9a621ae3c61bf22f9a5aa
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>
29 #include <sys/soundcard.h>
31 #include "include/common.h"
32 #include "include/config.h"
35 #define HELP_TEXT \
36 "WMixer " VERSION " by timecop@japan.co.jp + skunk@mit.edu\n" \
37 "usage:\n" \
38 " -d <dsp> connect to remote X display\n" \
39 " -e <name> exclude channel, can be used many times\n" \
40 " -f <file> parse this config [~/.wmixrc]\n" \
41 " -h print this help\n" \
42 " -m <dev> mixer device [/dev/mixer]\n" \
43 " -v verbose -> id, long name, name\n" \
45 /* The global configuration */
46 struct _Config config;
50 * Sets the default values in configuration
52 void config_init(void)
54 memset(&config, 0, sizeof(config));
56 config.mousewheel = 1;
57 config.scrolltext = 1;
58 config.wheel_button_up = 4;
59 config.wheel_button_down = 5;
60 config.scrollstep = 0.03;
61 config.osd = 1;
62 config.osd_color = strdup("green");
66 * Parse Command-Line options
68 * Supposed to be called before reading config file, as there's an
69 * option to change its name
71 void parse_cli_options(int argc, char **argv)
73 int opt;
74 int count_exclude = 0;
75 bool error_found;
77 opterr = 0; /* We take charge of printing the error message */
78 config.verbose = false;
79 error_found = false;
80 for (;;) {
81 opt = getopt(argc, argv, ":d:e:f:hm:v");
82 if (opt == -1)
83 break;
85 switch (opt) {
86 case '?':
87 fprintf(stderr, "wmix:error: unknow option '-%c'\n", optopt);
88 error_found = true;
89 break;
91 case ':':
92 fprintf(stderr, "wmix:error: missing argument for option '-%c'\n", optopt);
93 error_found = true;
94 break;
96 case 'd':
97 config.display_name = strdup(optarg);
98 break;
100 case 'e':
101 if (count_exclude < SOUND_MIXER_NRDEVICES) {
102 config.exclude_channel[count_exclude] = strdup(optarg);
103 count_exclude++;
104 } else
105 fprintf(stderr, "Warning: You can't exclude this many channels\n");
106 break;
108 case 'f':
109 if (config.file != NULL)
110 free(config.file);
111 config.file = strdup(optarg);
112 break;
114 case 'h':
115 fputs(HELP_TEXT, stdout);
116 exit(0);
117 break;
119 case 'm':
120 config.mixer_device = strdup(optarg);
121 break;
123 case 'v':
124 config.verbose = true;
125 break;
127 default:
128 break;
131 config.exclude_channel[count_exclude] = NULL;
133 if (optind < argc) {
134 fprintf(stderr, "wmix:error: argument '%s' not understood\n", argv[optind]);
135 error_found = true;
138 if (error_found)
139 exit(EXIT_FAILURE);
143 * Read configuration from a file
145 * The file name is taken from CLI if available, of falls back to
146 * a default name.
148 void config_read(void)
150 const char *filename;
151 char buffer_fname[512];
152 FILE *fp;
153 int line;
154 char buf[512];
156 if (config.file != NULL) {
157 filename = config.file;
158 } else {
159 const char *home;
161 home = getenv("HOME");
162 if (home == NULL) {
163 fprintf(stderr, "wmix: warning, could not get $HOME, can't load configuration file\n");
164 return;
166 snprintf(buffer_fname, sizeof(buffer_fname), "%s/.wmixrc", home);
167 filename = buffer_fname;
170 fp = fopen(filename, "r");
171 if (fp == NULL) {
172 if (config.file != NULL) {
173 /* The config file was explicitely specified by user, tell him there's a problem */
174 fprintf(stderr, "wmix: error, could not load configuration file \"%s\"\n", filename);
175 exit(EXIT_FAILURE);
177 /* Otherwise, it is acceptable if the file does not exist */
178 return;
180 if (config.verbose)
181 printf("Using configuration file: %s\n", filename);
183 line = 0;
184 while (fgets(buf, 512, fp)) {
185 char *ptr;
186 char *keyword;
187 char *value;
189 line++;
191 ptr = buf;
192 while (isspace(*ptr))
193 ptr++;
195 if ((*ptr == '\0') || (*ptr == '#'))
196 continue;
198 /* Isolate the keyword */
199 keyword = ptr;
200 if (*ptr == '=') {
201 fprintf(stderr, "wmix:warning: syntax error at line %d in \"%s\", no keyword before '='\n",
202 line, filename);
203 continue;
205 value = NULL;
206 while (*ptr) {
207 if (*ptr == '=') {
208 value = ptr + 1;
209 break;
211 if (*ptr == '#')
212 break;
213 ptr++;
215 if (value == NULL) {
216 fprintf(stderr, "wmix:warning: syntax error at line %d in \"%s\", missing '='\n",
217 line, filename);
218 continue;
220 while (isspace(ptr[-1]))
221 ptr--;
222 *ptr = '\0';
224 /* Isolate the value */
225 while (isspace(*value))
226 value++;
227 ptr = value;
228 while (*ptr) {
229 if (*ptr == '#')
230 break;
231 ptr++;
233 while (isspace(ptr[-1]))
234 ptr--;
235 *ptr = '\0';
237 /* Check what keyword we have */
238 if (strcmp(keyword, "mousewheel") == 0) {
239 config.mousewheel = atoi(value);
241 } else if (strcmp(keyword, "osd") == 0) {
242 config.osd = atoi(value);
244 } else if (strcmp(keyword, "osdcolor") == 0) {
245 if (config.osd_color)
246 free(config.osd_color);
247 config.osd_color = strdup(value);
249 } else if (strcmp(keyword, "scrolltext") == 0) {
250 config.scrolltext = atoi(value);
252 } else if (strcmp(keyword, "wheelbtn1") == 0) {
253 config.wheel_button_up = atoi(value);
255 } else if (strcmp(keyword, "wheelbtn2") == 0) {
256 config.wheel_button_down = atoi(value);
258 } else if (strcmp(keyword, "wheelstep") == 0) {
259 double val;
261 val = atof(value);
262 if (val < 0.0 || val > 100.0)
263 fprintf(stderr, "wmix:error: value %f is out of range for wheelstep in %s at line %d\n",
264 val, filename, line);
265 else if (val >= 1.0)
266 config.scrollstep = val / 100.0;
267 else if (val > 0.0)
268 config.scrollstep = val;
269 else
270 fprintf(stderr, "wmix:error: value '%s' not understood for wheelstep in %s at line %d\n",
271 value, filename, line);
272 } else {
273 fprintf(stderr, "wmix:warning: unknow keyword '%s' at line %d of \"%s\", ignored\n",
274 keyword, line, filename);
277 fclose(fp);