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
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"
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;
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)
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
]);
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
)
113 int count_exclude
= 0;
116 opterr
= 0; /* We take charge of printing the error message */
117 config
.verbose
= false;
120 opt
= getopt(argc
, argv
, ":d:e:f:hm:v");
126 fprintf(stderr
, "wmix:error: unknow option '-%c'\n", optopt
);
131 fprintf(stderr
, "wmix:error: missing argument for option '-%c'\n", optopt
);
136 if (config
.display_name
)
137 free(config
.display_name
);
138 config
.display_name
= strdup(optarg
);
142 if (count_exclude
< SOUND_MIXER_NRDEVICES
) {
143 config
.exclude_channel
[count_exclude
] = strdup(optarg
);
146 fprintf(stderr
, "Warning: You can't exclude this many channels\n");
150 if (config
.file
!= NULL
)
152 config
.file
= strdup(optarg
);
156 fputs(VERSION_TEXT
, stdout
);
157 fputs(HELP_TEXT
, stdout
);
162 if (config
.mixer_device
!= default_mixer_device
)
163 free(config
.mixer_device
);
164 config
.mixer_device
= strdup(optarg
);
168 config
.verbose
= true;
175 config
.exclude_channel
[count_exclude
] = NULL
;
178 fprintf(stderr
, "wmix:error: argument '%s' not understood\n", argv
[optind
]);
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
195 void config_read(void)
197 const char *filename
;
198 char buffer_fname
[512];
203 if (config
.file
!= NULL
) {
204 filename
= config
.file
;
208 home
= getenv("HOME");
210 fprintf(stderr
, "wmix: warning, could not get $HOME, can't load configuration file\n");
213 snprintf(buffer_fname
, sizeof(buffer_fname
), "%s/.wmixrc", home
);
214 filename
= buffer_fname
;
217 fp
= fopen(filename
, "r");
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
);
224 /* Otherwise, it is acceptable if the file does not exist */
228 printf("Using configuration file: %s\n", filename
);
231 while (fgets(buf
, 512, fp
)) {
239 while (isspace(*ptr
))
242 if ((*ptr
== '\0') || (*ptr
== '#'))
245 /* Isolate the keyword */
248 fprintf(stderr
, "wmix:warning: syntax error at line %d in \"%s\", no keyword before '='\n",
263 fprintf(stderr
, "wmix:warning: syntax error at line %d in \"%s\", missing '='\n",
267 while (isspace(ptr
[-1]))
271 /* Isolate the value */
272 while (isspace(*value
))
280 while (isspace(ptr
[-1]))
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) {
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
);
313 config
.scrollstep
= val
/ 100.0;
315 config
.scrollstep
= val
;
317 fprintf(stderr
, "wmix:error: value '%s' not understood for wheelstep in %s at line %d\n",
318 value
, filename
, line
);
320 fprintf(stderr
, "wmix:warning: unknow keyword '%s' at line %d of \"%s\", ignored\n",
321 keyword
, line
, filename
);