wmix: added error catch for XGrabKey on multimedia keys
[dockapps.git] / wmix / config.c
blob6a3c82294aad81d3835cfae2dbef6f63a876fd0d
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 " -m <dev> mixer device [/dev/mixer]\n" \
45 " -v verbose -> id, long name, name\n" \
47 /* The global configuration */
48 struct _Config config;
50 /* The default device used for Mixer control */
51 static const char default_mixer_device[] = "/dev/mixer";
53 /* Default color for OSD */
54 const char default_osd_color[] = "green";
58 * Sets the default values in configuration
60 void config_init(void)
62 memset(&config, 0, sizeof(config));
64 config.mixer_device = (char *) default_mixer_device;
65 config.mousewheel = 1;
66 config.scrolltext = 1;
67 config.wheel_button_up = 4;
68 config.wheel_button_down = 5;
69 config.scrollstep = 0.03;
70 config.osd = 1;
71 config.osd_color = (char *) default_osd_color;
75 * Release memory associated with configuration
77 * This does not concern the complete configuration, only the parameters
78 * that are needed during startup but are not useful during run-time
80 void config_release(void)
82 int i;
84 if (config.file)
85 free(config.file);
87 if (config.display_name)
88 free(config.display_name);
90 if (config.mixer_device != default_mixer_device)
91 free(config.mixer_device);
93 if (config.osd_color != default_osd_color)
94 free(config.osd_color);
96 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
97 if (config.exclude_channel[i])
98 free(config.exclude_channel[i]);
99 else
100 break;
105 * Parse Command-Line options
107 * Supposed to be called before reading config file, as there's an
108 * option to change its name
110 void parse_cli_options(int argc, char **argv)
112 int opt;
113 int count_exclude = 0;
114 bool error_found;
116 opterr = 0; /* We take charge of printing the error message */
117 config.verbose = false;
118 error_found = false;
119 for (;;) {
120 opt = getopt(argc, argv, ":d:e:f:hm:v");
121 if (opt == -1)
122 break;
124 switch (opt) {
125 case '?':
126 fprintf(stderr, "wmix:error: unknow option '-%c'\n", optopt);
127 error_found = true;
128 break;
130 case ':':
131 fprintf(stderr, "wmix:error: missing argument for option '-%c'\n", optopt);
132 error_found = true;
133 break;
135 case 'd':
136 if (config.display_name)
137 free(config.display_name);
138 config.display_name = strdup(optarg);
139 break;
141 case 'e':
142 if (count_exclude < SOUND_MIXER_NRDEVICES) {
143 config.exclude_channel[count_exclude] = strdup(optarg);
144 count_exclude++;
145 } else
146 fprintf(stderr, "Warning: You can't exclude this many channels\n");
147 break;
149 case 'f':
150 if (config.file != NULL)
151 free(config.file);
152 config.file = strdup(optarg);
153 break;
155 case 'h':
156 fputs(VERSION_TEXT, stdout);
157 fputs(HELP_TEXT, stdout);
158 exit(0);
159 break;
161 case 'm':
162 if (config.mixer_device != default_mixer_device)
163 free(config.mixer_device);
164 config.mixer_device = strdup(optarg);
165 break;
167 case 'v':
168 config.verbose = true;
169 break;
171 default:
172 break;
175 config.exclude_channel[count_exclude] = NULL;
177 if (optind < argc) {
178 fprintf(stderr, "wmix:error: argument '%s' not understood\n", argv[optind]);
179 error_found = true;
182 if (error_found)
183 exit(EXIT_FAILURE);
185 if (config.verbose)
186 fputs(VERSION_TEXT, stdout);
190 * Read configuration from a file
192 * The file name is taken from CLI if available, of falls back to
193 * a default name.
195 void config_read(void)
197 const char *filename;
198 char buffer_fname[512];
199 FILE *fp;
200 int line;
201 char buf[512];
203 if (config.file != NULL) {
204 filename = config.file;
205 } else {
206 const char *home;
208 home = getenv("HOME");
209 if (home == NULL) {
210 fprintf(stderr, "wmix: warning, could not get $HOME, can't load configuration file\n");
211 return;
213 snprintf(buffer_fname, sizeof(buffer_fname), "%s/.wmixrc", home);
214 filename = buffer_fname;
217 fp = fopen(filename, "r");
218 if (fp == NULL) {
219 if (config.file != NULL) {
220 /* The config file was explicitely specified by user, tell him there's a problem */
221 fprintf(stderr, "wmix: error, could not load configuration file \"%s\"\n", filename);
222 exit(EXIT_FAILURE);
224 /* Otherwise, it is acceptable if the file does not exist */
225 return;
227 if (config.verbose)
228 printf("Using configuration file: %s\n", filename);
230 line = 0;
231 while (fgets(buf, 512, fp)) {
232 char *ptr;
233 char *keyword;
234 char *value;
236 line++;
238 ptr = buf;
239 while (isspace(*ptr))
240 ptr++;
242 if ((*ptr == '\0') || (*ptr == '#'))
243 continue;
245 /* Isolate the keyword */
246 keyword = ptr;
247 if (*ptr == '=') {
248 fprintf(stderr, "wmix:warning: syntax error at line %d in \"%s\", no keyword before '='\n",
249 line, filename);
250 continue;
252 value = NULL;
253 while (*ptr) {
254 if (*ptr == '=') {
255 value = ptr + 1;
256 break;
258 if (*ptr == '#')
259 break;
260 ptr++;
262 if (value == NULL) {
263 fprintf(stderr, "wmix:warning: syntax error at line %d in \"%s\", missing '='\n",
264 line, filename);
265 continue;
267 while (isspace(ptr[-1]))
268 ptr--;
269 *ptr = '\0';
271 /* Isolate the value */
272 while (isspace(*value))
273 value++;
274 ptr = value;
275 while (*ptr) {
276 if (*ptr == '#')
277 break;
278 ptr++;
280 while (isspace(ptr[-1]))
281 ptr--;
282 *ptr = '\0';
284 /* Check what keyword we have */
285 if (strcmp(keyword, "mousewheel") == 0) {
286 config.mousewheel = atoi(value);
288 } else if (strcmp(keyword, "osd") == 0) {
289 config.osd = atoi(value);
291 } else if (strcmp(keyword, "osdcolor") == 0) {
292 if (config.osd_color != default_osd_color)
293 free(config.osd_color);
294 config.osd_color = strdup(value);
296 } else if (strcmp(keyword, "scrolltext") == 0) {
297 config.scrolltext = atoi(value);
299 } else if (strcmp(keyword, "wheelbtn1") == 0) {
300 config.wheel_button_up = atoi(value);
302 } else if (strcmp(keyword, "wheelbtn2") == 0) {
303 config.wheel_button_down = atoi(value);
305 } else if (strcmp(keyword, "wheelstep") == 0) {
306 double val;
308 val = atof(value);
309 if (val < 0.0 || val > 100.0)
310 fprintf(stderr, "wmix:error: value %f is out of range for wheelstep in %s at line %d\n",
311 val, filename, line);
312 else if (val >= 1.0)
313 config.scrollstep = val / 100.0;
314 else if (val > 0.0)
315 config.scrollstep = val;
316 else
317 fprintf(stderr, "wmix:error: value '%s' not understood for wheelstep in %s at line %d\n",
318 value, filename, line);
319 } else {
320 fprintf(stderr, "wmix:warning: unknow keyword '%s' at line %d of \"%s\", ignored\n",
321 keyword, line, filename);
324 fclose(fp);