wmix: release memory for startup-only configuration stuff after startup
[dockapps.git] / wmix / config.c
blobee0a0245e554844440c1b3721e7bc371b7c4f31f
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;
48 /* The default device used for Mixer control */
49 static const char default_mixer_device[] = "/dev/mixer";
51 /* Default color for OSD */
52 const char default_osd_color[] = "green";
56 * Sets the default values in configuration
58 void config_init(void)
60 memset(&config, 0, sizeof(config));
62 config.mixer_device = (char *) default_mixer_device;
63 config.mousewheel = 1;
64 config.scrolltext = 1;
65 config.wheel_button_up = 4;
66 config.wheel_button_down = 5;
67 config.scrollstep = 0.03;
68 config.osd = 1;
69 config.osd_color = (char *) default_osd_color;
73 * Release memory associated with configuration
75 * This does not concern the complete configuration, only the parameters
76 * that are needed during startup but are not useful during run-time
78 void config_release(void)
80 int i;
82 if (config.file)
83 free(config.file);
85 if (config.display_name)
86 free(config.display_name);
88 if (config.mixer_device != default_mixer_device)
89 free(config.mixer_device);
91 if (config.osd_color != default_osd_color)
92 free(config.osd_color);
94 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
95 if (config.exclude_channel[i])
96 free(config.exclude_channel[i]);
97 else
98 break;
103 * Parse Command-Line options
105 * Supposed to be called before reading config file, as there's an
106 * option to change its name
108 void parse_cli_options(int argc, char **argv)
110 int opt;
111 int count_exclude = 0;
112 bool error_found;
114 opterr = 0; /* We take charge of printing the error message */
115 config.verbose = false;
116 error_found = false;
117 for (;;) {
118 opt = getopt(argc, argv, ":d:e:f:hm:v");
119 if (opt == -1)
120 break;
122 switch (opt) {
123 case '?':
124 fprintf(stderr, "wmix:error: unknow option '-%c'\n", optopt);
125 error_found = true;
126 break;
128 case ':':
129 fprintf(stderr, "wmix:error: missing argument for option '-%c'\n", optopt);
130 error_found = true;
131 break;
133 case 'd':
134 if (config.display_name)
135 free(config.display_name);
136 config.display_name = strdup(optarg);
137 break;
139 case 'e':
140 if (count_exclude < SOUND_MIXER_NRDEVICES) {
141 config.exclude_channel[count_exclude] = strdup(optarg);
142 count_exclude++;
143 } else
144 fprintf(stderr, "Warning: You can't exclude this many channels\n");
145 break;
147 case 'f':
148 if (config.file != NULL)
149 free(config.file);
150 config.file = strdup(optarg);
151 break;
153 case 'h':
154 fputs(HELP_TEXT, stdout);
155 exit(0);
156 break;
158 case 'm':
159 if (config.mixer_device != default_mixer_device)
160 free(config.mixer_device);
161 config.mixer_device = strdup(optarg);
162 break;
164 case 'v':
165 config.verbose = true;
166 break;
168 default:
169 break;
172 config.exclude_channel[count_exclude] = NULL;
174 if (optind < argc) {
175 fprintf(stderr, "wmix:error: argument '%s' not understood\n", argv[optind]);
176 error_found = true;
179 if (error_found)
180 exit(EXIT_FAILURE);
184 * Read configuration from a file
186 * The file name is taken from CLI if available, of falls back to
187 * a default name.
189 void config_read(void)
191 const char *filename;
192 char buffer_fname[512];
193 FILE *fp;
194 int line;
195 char buf[512];
197 if (config.file != NULL) {
198 filename = config.file;
199 } else {
200 const char *home;
202 home = getenv("HOME");
203 if (home == NULL) {
204 fprintf(stderr, "wmix: warning, could not get $HOME, can't load configuration file\n");
205 return;
207 snprintf(buffer_fname, sizeof(buffer_fname), "%s/.wmixrc", home);
208 filename = buffer_fname;
211 fp = fopen(filename, "r");
212 if (fp == NULL) {
213 if (config.file != NULL) {
214 /* The config file was explicitely specified by user, tell him there's a problem */
215 fprintf(stderr, "wmix: error, could not load configuration file \"%s\"\n", filename);
216 exit(EXIT_FAILURE);
218 /* Otherwise, it is acceptable if the file does not exist */
219 return;
221 if (config.verbose)
222 printf("Using configuration file: %s\n", filename);
224 line = 0;
225 while (fgets(buf, 512, fp)) {
226 char *ptr;
227 char *keyword;
228 char *value;
230 line++;
232 ptr = buf;
233 while (isspace(*ptr))
234 ptr++;
236 if ((*ptr == '\0') || (*ptr == '#'))
237 continue;
239 /* Isolate the keyword */
240 keyword = ptr;
241 if (*ptr == '=') {
242 fprintf(stderr, "wmix:warning: syntax error at line %d in \"%s\", no keyword before '='\n",
243 line, filename);
244 continue;
246 value = NULL;
247 while (*ptr) {
248 if (*ptr == '=') {
249 value = ptr + 1;
250 break;
252 if (*ptr == '#')
253 break;
254 ptr++;
256 if (value == NULL) {
257 fprintf(stderr, "wmix:warning: syntax error at line %d in \"%s\", missing '='\n",
258 line, filename);
259 continue;
261 while (isspace(ptr[-1]))
262 ptr--;
263 *ptr = '\0';
265 /* Isolate the value */
266 while (isspace(*value))
267 value++;
268 ptr = value;
269 while (*ptr) {
270 if (*ptr == '#')
271 break;
272 ptr++;
274 while (isspace(ptr[-1]))
275 ptr--;
276 *ptr = '\0';
278 /* Check what keyword we have */
279 if (strcmp(keyword, "mousewheel") == 0) {
280 config.mousewheel = atoi(value);
282 } else if (strcmp(keyword, "osd") == 0) {
283 config.osd = atoi(value);
285 } else if (strcmp(keyword, "osdcolor") == 0) {
286 if (config.osd_color != default_osd_color)
287 free(config.osd_color);
288 config.osd_color = strdup(value);
290 } else if (strcmp(keyword, "scrolltext") == 0) {
291 config.scrolltext = atoi(value);
293 } else if (strcmp(keyword, "wheelbtn1") == 0) {
294 config.wheel_button_up = atoi(value);
296 } else if (strcmp(keyword, "wheelbtn2") == 0) {
297 config.wheel_button_down = atoi(value);
299 } else if (strcmp(keyword, "wheelstep") == 0) {
300 double val;
302 val = atof(value);
303 if (val < 0.0 || val > 100.0)
304 fprintf(stderr, "wmix:error: value %f is out of range for wheelstep in %s at line %d\n",
305 val, filename, line);
306 else if (val >= 1.0)
307 config.scrollstep = val / 100.0;
308 else if (val > 0.0)
309 config.scrollstep = val;
310 else
311 fprintf(stderr, "wmix:error: value '%s' not understood for wheelstep in %s at line %d\n",
312 value, filename, line);
313 } else {
314 fprintf(stderr, "wmix:warning: unknow keyword '%s' at line %d of \"%s\", ignored\n",
315 keyword, line, filename);
318 fclose(fp);