2014-06-07 19:21:45 +00:00
|
|
|
/* WMix -- a mixer using the OSS mixer API.
|
|
|
|
* Copyright (C) 2014 Christophe CURIS for the WindowMaker Team
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
/*
|
2014-06-07 19:21:46 +00:00
|
|
|
* config.c: functions related to loading the configuration, both from
|
|
|
|
* command line options and from file
|
2014-06-07 19:21:45 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-06-07 19:21:50 +00:00
|
|
|
#include <ctype.h>
|
2014-06-07 19:21:46 +00:00
|
|
|
#include <getopt.h>
|
2015-09-17 01:19:44 +00:00
|
|
|
#include <limits.h>
|
2014-06-07 19:21:45 +00:00
|
|
|
|
|
|
|
#include <sys/soundcard.h>
|
|
|
|
|
|
|
|
#include "include/common.h"
|
|
|
|
#include "include/config.h"
|
2015-09-17 01:19:41 +00:00
|
|
|
#include "include/misc.h"
|
2014-06-07 19:21:45 +00:00
|
|
|
|
2014-06-07 19:21:53 +00:00
|
|
|
#define VERSION_TEXT \
|
|
|
|
"WMixer " VERSION " by timecop@japan.co.jp + skunk@mit.edu\n"
|
|
|
|
|
|
|
|
#define HELP_TEXT \
|
2014-06-07 19:21:46 +00:00
|
|
|
"usage:\n" \
|
2015-09-17 01:19:41 +00:00
|
|
|
" -a <api> use this sound api (oss or alsa) [alsa]\n" \
|
2014-06-07 19:21:46 +00:00
|
|
|
" -d <dsp> connect to remote X display\n" \
|
|
|
|
" -e <name> exclude channel, can be used many times\n" \
|
|
|
|
" -f <file> parse this config [~/.wmixrc]\n" \
|
|
|
|
" -h print this help\n" \
|
2014-06-07 19:21:56 +00:00
|
|
|
" -k disable grabing volume control keys\n" \
|
2015-09-17 01:19:41 +00:00
|
|
|
" -m <dev> oss mixer device [/dev/mixer]\n" \
|
|
|
|
" or alsa card name [default]\n" \
|
2015-09-17 01:19:44 +00:00
|
|
|
" -o <num> display osd on this monitor number or name [0]\n" \
|
|
|
|
" use -1 to disable osd\n" \
|
2014-06-07 19:21:46 +00:00
|
|
|
" -v verbose -> id, long name, name\n" \
|
|
|
|
|
2014-06-07 19:21:45 +00:00
|
|
|
/* The global configuration */
|
|
|
|
struct _Config config;
|
|
|
|
|
2014-06-07 19:21:52 +00:00
|
|
|
/* The default device used for Mixer control */
|
|
|
|
static const char default_mixer_device[] = "/dev/mixer";
|
2015-09-17 01:19:41 +00:00
|
|
|
static const char default_card_name[] = "default";
|
2014-06-07 19:21:51 +00:00
|
|
|
/* Default color for OSD */
|
|
|
|
const char default_osd_color[] = "green";
|
|
|
|
|
2014-06-07 19:21:45 +00:00
|
|
|
|
2014-06-07 19:21:47 +00:00
|
|
|
/*
|
|
|
|
* Sets the default values in configuration
|
|
|
|
*/
|
|
|
|
void config_init(void)
|
|
|
|
{
|
|
|
|
memset(&config, 0, sizeof(config));
|
2015-09-17 01:19:47 +00:00
|
|
|
config.api = -1;
|
2015-09-17 01:19:41 +00:00
|
|
|
config.mixer_device = NULL;
|
2014-06-07 19:21:47 +00:00
|
|
|
config.mousewheel = 1;
|
|
|
|
config.scrolltext = 1;
|
2014-06-07 19:21:56 +00:00
|
|
|
config.mmkeys = 1;
|
2014-06-07 19:21:47 +00:00
|
|
|
config.wheel_button_up = 4;
|
|
|
|
config.wheel_button_down = 5;
|
|
|
|
config.scrollstep = 0.03;
|
|
|
|
config.osd = 1;
|
2014-06-07 19:21:51 +00:00
|
|
|
config.osd_color = (char *) default_osd_color;
|
2015-09-17 01:19:46 +00:00
|
|
|
config.osd_monitor_number = -1;
|
2015-09-17 01:19:44 +00:00
|
|
|
config.osd_monitor_name = NULL;
|
2014-06-07 19:21:47 +00:00
|
|
|
}
|
|
|
|
|
2014-06-07 19:21:52 +00:00
|
|
|
/*
|
|
|
|
* Release memory associated with configuration
|
|
|
|
*
|
|
|
|
* This does not concern the complete configuration, only the parameters
|
|
|
|
* that are needed during startup but are not useful during run-time
|
|
|
|
*/
|
|
|
|
void config_release(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (config.file)
|
|
|
|
free(config.file);
|
|
|
|
|
|
|
|
if (config.display_name)
|
|
|
|
free(config.display_name);
|
|
|
|
|
2015-09-17 01:19:41 +00:00
|
|
|
if (config.mixer_device != default_mixer_device
|
|
|
|
&& config.mixer_device != default_card_name)
|
2014-06-07 19:21:52 +00:00
|
|
|
free(config.mixer_device);
|
|
|
|
|
|
|
|
if (config.osd_color != default_osd_color)
|
|
|
|
free(config.osd_color);
|
|
|
|
|
2015-09-17 01:19:41 +00:00
|
|
|
for (i = 0; i < EXCLUDE_MAX_COUNT; i++) {
|
2014-06-07 19:21:52 +00:00
|
|
|
if (config.exclude_channel[i])
|
|
|
|
free(config.exclude_channel[i]);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-17 01:19:46 +00:00
|
|
|
bool parse_monitor_value(char *value)
|
2015-09-17 01:19:45 +00:00
|
|
|
{
|
|
|
|
char *end;
|
|
|
|
long mon = strtol(value, &end, 10);
|
|
|
|
if (end == value + strlen(value)) {
|
|
|
|
if ((mon > INT_MAX) || (mon < -1)) {
|
2015-09-17 01:19:46 +00:00
|
|
|
return false;
|
2015-09-17 01:19:45 +00:00
|
|
|
} else {
|
|
|
|
if (mon == -1)
|
|
|
|
config.osd = 0;
|
|
|
|
else
|
|
|
|
config.osd_monitor_number = (int)mon;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
config.osd_monitor_name = strdup(value);
|
|
|
|
}
|
2015-09-17 01:19:46 +00:00
|
|
|
return true;
|
2015-09-17 01:19:45 +00:00
|
|
|
}
|
|
|
|
|
2014-06-07 19:21:46 +00:00
|
|
|
/*
|
|
|
|
* Parse Command-Line options
|
|
|
|
*
|
|
|
|
* Supposed to be called before reading config file, as there's an
|
|
|
|
* option to change its name
|
|
|
|
*/
|
|
|
|
void parse_cli_options(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int opt;
|
|
|
|
int count_exclude = 0;
|
2014-06-07 19:21:49 +00:00
|
|
|
bool error_found;
|
2014-06-07 19:21:46 +00:00
|
|
|
|
2014-06-07 19:21:49 +00:00
|
|
|
opterr = 0; /* We take charge of printing the error message */
|
2014-06-07 19:21:46 +00:00
|
|
|
config.verbose = false;
|
2014-06-07 19:21:49 +00:00
|
|
|
error_found = false;
|
|
|
|
for (;;) {
|
2015-09-17 01:19:44 +00:00
|
|
|
opt = getopt(argc, argv, ":a:d:e:f:hkm:o:v");
|
2014-06-07 19:21:49 +00:00
|
|
|
if (opt == -1)
|
|
|
|
break;
|
|
|
|
|
2014-06-07 19:21:46 +00:00
|
|
|
switch (opt) {
|
2014-06-07 19:21:49 +00:00
|
|
|
case '?':
|
|
|
|
fprintf(stderr, "wmix:error: unknow option '-%c'\n", optopt);
|
|
|
|
error_found = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ':':
|
|
|
|
fprintf(stderr, "wmix:error: missing argument for option '-%c'\n", optopt);
|
|
|
|
error_found = true;
|
|
|
|
break;
|
2015-09-17 01:19:41 +00:00
|
|
|
case 'a':
|
|
|
|
if(!strcmp("oss", optarg))
|
|
|
|
config.api = 1;
|
2015-09-17 01:19:47 +00:00
|
|
|
else if (!strcmp("alsa", optarg))
|
|
|
|
config.api = 0;
|
|
|
|
else
|
|
|
|
fprintf(stderr, "wmix:warning: incorrect sound api specified on command line, ignoring\n");
|
2015-09-17 01:19:41 +00:00
|
|
|
break;
|
2014-06-07 19:21:46 +00:00
|
|
|
case 'd':
|
2014-06-07 19:21:52 +00:00
|
|
|
if (config.display_name)
|
|
|
|
free(config.display_name);
|
2014-06-07 19:21:49 +00:00
|
|
|
config.display_name = strdup(optarg);
|
2014-06-07 19:21:46 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'e':
|
2015-09-17 01:19:41 +00:00
|
|
|
if (count_exclude < EXCLUDE_MAX_COUNT) {
|
2014-06-07 19:21:46 +00:00
|
|
|
config.exclude_channel[count_exclude] = strdup(optarg);
|
|
|
|
count_exclude++;
|
|
|
|
} else
|
|
|
|
fprintf(stderr, "Warning: You can't exclude this many channels\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
2014-06-07 19:21:49 +00:00
|
|
|
if (config.file != NULL)
|
|
|
|
free(config.file);
|
2014-06-07 19:21:46 +00:00
|
|
|
config.file = strdup(optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'h':
|
2014-06-07 19:21:53 +00:00
|
|
|
fputs(VERSION_TEXT, stdout);
|
2014-06-07 19:21:46 +00:00
|
|
|
fputs(HELP_TEXT, stdout);
|
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
|
2014-06-07 19:21:56 +00:00
|
|
|
case 'k':
|
|
|
|
config.mmkeys = false;
|
|
|
|
break;
|
|
|
|
|
2014-06-07 19:21:46 +00:00
|
|
|
case 'm':
|
2014-06-07 19:21:52 +00:00
|
|
|
if (config.mixer_device != default_mixer_device)
|
|
|
|
free(config.mixer_device);
|
2014-06-07 19:21:49 +00:00
|
|
|
config.mixer_device = strdup(optarg);
|
2014-06-07 19:21:46 +00:00
|
|
|
break;
|
|
|
|
|
2015-09-17 01:19:45 +00:00
|
|
|
case 'o':
|
2015-09-17 01:19:46 +00:00
|
|
|
if (!parse_monitor_value(optarg))
|
|
|
|
fprintf(stderr, "wmix:warning: unreasonable monitor number provided on command line, ignoring\n");
|
2015-09-17 01:19:44 +00:00
|
|
|
break;
|
|
|
|
|
2014-06-07 19:21:46 +00:00
|
|
|
case 'v':
|
|
|
|
config.verbose = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
config.exclude_channel[count_exclude] = NULL;
|
2014-06-07 19:21:49 +00:00
|
|
|
|
|
|
|
if (optind < argc) {
|
|
|
|
fprintf(stderr, "wmix:error: argument '%s' not understood\n", argv[optind]);
|
|
|
|
error_found = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error_found)
|
|
|
|
exit(EXIT_FAILURE);
|
2014-06-07 19:21:53 +00:00
|
|
|
|
|
|
|
if (config.verbose)
|
|
|
|
fputs(VERSION_TEXT, stdout);
|
2014-06-07 19:21:46 +00:00
|
|
|
}
|
|
|
|
|
2014-06-07 19:21:45 +00:00
|
|
|
/*
|
|
|
|
* Read configuration from a file
|
|
|
|
*
|
|
|
|
* The file name is taken from CLI if available, of falls back to
|
|
|
|
* a default name.
|
|
|
|
*/
|
|
|
|
void config_read(void)
|
|
|
|
{
|
2014-06-07 19:21:48 +00:00
|
|
|
const char *filename;
|
|
|
|
char buffer_fname[512];
|
2014-06-07 19:21:45 +00:00
|
|
|
FILE *fp;
|
2014-06-07 19:21:50 +00:00
|
|
|
int line;
|
2014-06-07 19:21:45 +00:00
|
|
|
char buf[512];
|
|
|
|
|
2014-06-07 19:21:48 +00:00
|
|
|
if (config.file != NULL) {
|
|
|
|
filename = config.file;
|
|
|
|
} else {
|
|
|
|
const char *home;
|
2014-06-07 19:21:45 +00:00
|
|
|
|
2014-06-07 19:21:48 +00:00
|
|
|
home = getenv("HOME");
|
|
|
|
if (home == NULL) {
|
|
|
|
fprintf(stderr, "wmix: warning, could not get $HOME, can't load configuration file\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
snprintf(buffer_fname, sizeof(buffer_fname), "%s/.wmixrc", home);
|
|
|
|
filename = buffer_fname;
|
|
|
|
}
|
|
|
|
|
|
|
|
fp = fopen(filename, "r");
|
|
|
|
if (fp == NULL) {
|
|
|
|
if (config.file != NULL) {
|
|
|
|
/* The config file was explicitely specified by user, tell him there's a problem */
|
|
|
|
fprintf(stderr, "wmix: error, could not load configuration file \"%s\"\n", filename);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
/* Otherwise, it is acceptable if the file does not exist */
|
2014-06-07 19:21:45 +00:00
|
|
|
return;
|
2014-06-07 19:21:48 +00:00
|
|
|
}
|
|
|
|
if (config.verbose)
|
|
|
|
printf("Using configuration file: %s\n", filename);
|
2014-06-07 19:21:45 +00:00
|
|
|
|
2014-06-07 19:21:50 +00:00
|
|
|
line = 0;
|
2014-06-07 19:21:45 +00:00
|
|
|
while (fgets(buf, 512, fp)) {
|
2014-06-07 19:21:50 +00:00
|
|
|
char *ptr;
|
|
|
|
char *keyword;
|
|
|
|
char *value;
|
|
|
|
|
|
|
|
line++;
|
|
|
|
|
|
|
|
ptr = buf;
|
|
|
|
while (isspace(*ptr))
|
|
|
|
ptr++;
|
|
|
|
|
|
|
|
if ((*ptr == '\0') || (*ptr == '#'))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Isolate the keyword */
|
|
|
|
keyword = ptr;
|
|
|
|
if (*ptr == '=') {
|
|
|
|
fprintf(stderr, "wmix:warning: syntax error at line %d in \"%s\", no keyword before '='\n",
|
|
|
|
line, filename);
|
|
|
|
continue;
|
2014-06-07 19:21:45 +00:00
|
|
|
}
|
2014-06-07 19:21:50 +00:00
|
|
|
value = NULL;
|
|
|
|
while (*ptr) {
|
|
|
|
if (*ptr == '=') {
|
|
|
|
value = ptr + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (*ptr == '#')
|
|
|
|
break;
|
|
|
|
ptr++;
|
2014-06-07 19:21:45 +00:00
|
|
|
}
|
2014-06-07 19:21:50 +00:00
|
|
|
if (value == NULL) {
|
|
|
|
fprintf(stderr, "wmix:warning: syntax error at line %d in \"%s\", missing '='\n",
|
|
|
|
line, filename);
|
|
|
|
continue;
|
2014-06-07 19:21:45 +00:00
|
|
|
}
|
2014-06-07 19:21:50 +00:00
|
|
|
while (isspace(ptr[-1]))
|
|
|
|
ptr--;
|
|
|
|
*ptr = '\0';
|
|
|
|
|
|
|
|
/* Isolate the value */
|
|
|
|
while (isspace(*value))
|
|
|
|
value++;
|
|
|
|
ptr = value;
|
|
|
|
while (*ptr) {
|
|
|
|
if (*ptr == '#')
|
|
|
|
break;
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
while (isspace(ptr[-1]))
|
|
|
|
ptr--;
|
|
|
|
*ptr = '\0';
|
|
|
|
|
|
|
|
/* Check what keyword we have */
|
2015-09-17 01:19:47 +00:00
|
|
|
if (strcmp(keyword, "api") == 0) {
|
|
|
|
if (config.api == -1) {
|
|
|
|
if(!strcmp("oss", value))
|
|
|
|
config.api = 1;
|
|
|
|
else if (!strcmp("alsa", value))
|
|
|
|
config.api = 0;
|
|
|
|
else
|
|
|
|
fprintf(stderr, "wmix:warning: incorrect sound api in config, ignoring\n");
|
|
|
|
}
|
|
|
|
} else if (strcmp(keyword, "device") == 0) {
|
2014-06-07 19:21:57 +00:00
|
|
|
if (config.mixer_device == default_mixer_device)
|
|
|
|
config.mixer_device = strdup(value);
|
|
|
|
/* If not the default, keep the previous value because it was provided in the command-line */
|
|
|
|
|
|
|
|
} else if (strcmp(keyword, "exclude") == 0) {
|
|
|
|
int i;
|
|
|
|
|
2015-09-17 01:19:41 +00:00
|
|
|
for (i = 0; i < EXCLUDE_MAX_COUNT; i++) {
|
2014-06-07 19:21:57 +00:00
|
|
|
if (config.exclude_channel[i] == NULL) {
|
|
|
|
config.exclude_channel[i] = strdup(value);
|
2015-09-17 01:19:41 +00:00
|
|
|
config.exclude_channel[i+1] = NULL;
|
2014-06-07 19:21:57 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(value, config.exclude_channel[i]) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (strcmp(keyword, "mousewheel") == 0) {
|
2014-06-07 19:21:50 +00:00
|
|
|
config.mousewheel = atoi(value);
|
|
|
|
|
|
|
|
} else if (strcmp(keyword, "osd") == 0) {
|
|
|
|
config.osd = atoi(value);
|
|
|
|
|
|
|
|
} else if (strcmp(keyword, "osdcolor") == 0) {
|
2014-06-07 19:21:51 +00:00
|
|
|
if (config.osd_color != default_osd_color)
|
2014-06-07 19:21:45 +00:00
|
|
|
free(config.osd_color);
|
2014-06-07 19:21:50 +00:00
|
|
|
config.osd_color = strdup(value);
|
|
|
|
|
2015-09-17 01:19:45 +00:00
|
|
|
} else if (strcmp(keyword, "osdmonitor") == 0) {
|
2015-09-17 01:19:46 +00:00
|
|
|
if (!config.osd_monitor_name &&
|
|
|
|
config.osd_monitor_number == -1 &&
|
|
|
|
!parse_monitor_value(value))
|
|
|
|
fprintf(stderr, "wmix:warning: unreasonable monitor number in config, ignoring\n");
|
2015-09-17 01:19:45 +00:00
|
|
|
|
2014-06-07 19:21:50 +00:00
|
|
|
} else if (strcmp(keyword, "scrolltext") == 0) {
|
|
|
|
config.scrolltext = atoi(value);
|
|
|
|
|
|
|
|
} else if (strcmp(keyword, "wheelbtn1") == 0) {
|
|
|
|
config.wheel_button_up = atoi(value);
|
|
|
|
|
|
|
|
} else if (strcmp(keyword, "wheelbtn2") == 0) {
|
|
|
|
config.wheel_button_down = atoi(value);
|
|
|
|
|
|
|
|
} else if (strcmp(keyword, "wheelstep") == 0) {
|
|
|
|
double val;
|
|
|
|
|
|
|
|
val = atof(value);
|
|
|
|
if (val < 0.0 || val > 100.0)
|
|
|
|
fprintf(stderr, "wmix:error: value %f is out of range for wheelstep in %s at line %d\n",
|
|
|
|
val, filename, line);
|
|
|
|
else if (val >= 1.0)
|
|
|
|
config.scrollstep = val / 100.0;
|
|
|
|
else if (val > 0.0)
|
|
|
|
config.scrollstep = val;
|
2014-06-07 19:21:45 +00:00
|
|
|
else
|
2014-06-07 19:21:50 +00:00
|
|
|
fprintf(stderr, "wmix:error: value '%s' not understood for wheelstep in %s at line %d\n",
|
|
|
|
value, filename, line);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "wmix:warning: unknow keyword '%s' at line %d of \"%s\", ignored\n",
|
|
|
|
keyword, line, filename);
|
2014-06-07 19:21:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
2015-09-17 01:19:46 +00:00
|
|
|
|
|
|
|
void config_set_defaults()
|
|
|
|
{
|
2015-09-17 01:19:47 +00:00
|
|
|
if (config.api == -1)
|
|
|
|
config.api = 0;
|
|
|
|
|
2015-09-17 01:19:46 +00:00
|
|
|
if (!config.mixer_device) {
|
|
|
|
if (config.api == 0)
|
|
|
|
config.mixer_device = (char *)default_card_name;
|
|
|
|
else if (config.api == 1)
|
|
|
|
config.mixer_device = (char *)default_mixer_device;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!config.osd_monitor_name && config.osd_monitor_number == -1)
|
|
|
|
config.osd_monitor_number = 0;
|
|
|
|
}
|