wmclock: Bump to version 1.0.15.
[dockapps.git] / wmix / config.c
blob70342718549305a2d069d3317be549bf2605889d
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 VERSION_TEXT \
36 "WMixer " VERSION " by timecop@japan.co.jp + skunk@mit.edu\n"
38 #define HELP_TEXT \
39 "usage:\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;
68 config.mmkeys = 1;
69 config.wheel_button_up = 4;
70 config.wheel_button_down = 5;
71 config.scrollstep = 0.03;
72 config.osd = 1;
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)
84 int i;
86 if (config.file)
87 free(config.file);
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]);
101 else
102 break;
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)
114 int opt;
115 int count_exclude = 0;
116 bool error_found;
118 opterr = 0; /* We take charge of printing the error message */
119 config.verbose = false;
120 error_found = false;
121 for (;;) {
122 opt = getopt(argc, argv, ":d:e:f:hkm:v");
123 if (opt == -1)
124 break;
126 switch (opt) {
127 case '?':
128 fprintf(stderr, "wmix:error: unknow option '-%c'\n", optopt);
129 error_found = true;
130 break;
132 case ':':
133 fprintf(stderr, "wmix:error: missing argument for option '-%c'\n", optopt);
134 error_found = true;
135 break;
137 case 'd':
138 if (config.display_name)
139 free(config.display_name);
140 config.display_name = strdup(optarg);
141 break;
143 case 'e':
144 if (count_exclude < SOUND_MIXER_NRDEVICES) {
145 config.exclude_channel[count_exclude] = strdup(optarg);
146 count_exclude++;
147 } else
148 fprintf(stderr, "Warning: You can't exclude this many channels\n");
149 break;
151 case 'f':
152 if (config.file != NULL)
153 free(config.file);
154 config.file = strdup(optarg);
155 break;
157 case 'h':
158 fputs(VERSION_TEXT, stdout);
159 fputs(HELP_TEXT, stdout);
160 exit(0);
161 break;
163 case 'k':
164 config.mmkeys = false;
165 break;
167 case 'm':
168 if (config.mixer_device != default_mixer_device)
169 free(config.mixer_device);
170 config.mixer_device = strdup(optarg);
171 break;
173 case 'v':
174 config.verbose = true;
175 break;
177 default:
178 break;
181 config.exclude_channel[count_exclude] = NULL;
183 if (optind < argc) {
184 fprintf(stderr, "wmix:error: argument '%s' not understood\n", argv[optind]);
185 error_found = true;
188 if (error_found)
189 exit(EXIT_FAILURE);
191 if (config.verbose)
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
199 * a default name.
201 void config_read(void)
203 const char *filename;
204 char buffer_fname[512];
205 FILE *fp;
206 int line;
207 char buf[512];
209 if (config.file != NULL) {
210 filename = config.file;
211 } else {
212 const char *home;
214 home = getenv("HOME");
215 if (home == NULL) {
216 fprintf(stderr, "wmix: warning, could not get $HOME, can't load configuration file\n");
217 return;
219 snprintf(buffer_fname, sizeof(buffer_fname), "%s/.wmixrc", home);
220 filename = buffer_fname;
223 fp = fopen(filename, "r");
224 if (fp == NULL) {
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);
228 exit(EXIT_FAILURE);
230 /* Otherwise, it is acceptable if the file does not exist */
231 return;
233 if (config.verbose)
234 printf("Using configuration file: %s\n", filename);
236 line = 0;
237 while (fgets(buf, 512, fp)) {
238 char *ptr;
239 char *keyword;
240 char *value;
242 line++;
244 ptr = buf;
245 while (isspace(*ptr))
246 ptr++;
248 if ((*ptr == '\0') || (*ptr == '#'))
249 continue;
251 /* Isolate the keyword */
252 keyword = ptr;
253 if (*ptr == '=') {
254 fprintf(stderr, "wmix:warning: syntax error at line %d in \"%s\", no keyword before '='\n",
255 line, filename);
256 continue;
258 value = NULL;
259 while (*ptr) {
260 if (*ptr == '=') {
261 value = ptr + 1;
262 break;
264 if (*ptr == '#')
265 break;
266 ptr++;
268 if (value == NULL) {
269 fprintf(stderr, "wmix:warning: syntax error at line %d in \"%s\", missing '='\n",
270 line, filename);
271 continue;
273 while (isspace(ptr[-1]))
274 ptr--;
275 *ptr = '\0';
277 /* Isolate the value */
278 while (isspace(*value))
279 value++;
280 ptr = value;
281 while (*ptr) {
282 if (*ptr == '#')
283 break;
284 ptr++;
286 while (isspace(ptr[-1]))
287 ptr--;
288 *ptr = '\0';
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) {
297 int i;
299 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
300 if (config.exclude_channel[i] == NULL) {
301 config.exclude_channel[i] = strdup(value);
302 break;
305 if (strcmp(value, config.exclude_channel[i]) == 0)
306 break;
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) {
329 double val;
331 val = atof(value);
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);
335 else if (val >= 1.0)
336 config.scrollstep = val / 100.0;
337 else if (val > 0.0)
338 config.scrollstep = val;
339 else
340 fprintf(stderr, "wmix:error: value '%s' not understood for wheelstep in %s at line %d\n",
341 value, filename, line);
342 } else {
343 fprintf(stderr, "wmix:warning: unknow keyword '%s' at line %d of \"%s\", ignored\n",
344 keyword, line, filename);
347 fclose(fp);