From 4c1bae53c482a3b6a7d39e6f3f43f1deb24521bf Mon Sep 17 00:00:00 2001 From: Christophe CURIS Date: Sat, 7 Jun 2014 21:21:45 +0200 Subject: [PATCH] wmix: created new file config.c to contain configuration related stuff Management of the configuration is split in many places, the goal is to regroup stuff together, starting with the loading from file stuff. Signed-off-by: Christophe CURIS --- wmix/Makefile | 2 +- wmix/config.c | 95 +++++++++++++++++++++++++++++++++++++++++++ wmix/include/common.h | 13 ------ wmix/include/config.h | 41 +++++++++++++++++++ wmix/include/misc.h | 1 - wmix/misc.c | 56 ------------------------- wmix/ui_x.c | 4 +- wmix/wmix.c | 2 +- 8 files changed, 140 insertions(+), 74 deletions(-) create mode 100644 wmix/config.c create mode 100644 wmix/include/config.h diff --git a/wmix/Makefile b/wmix/Makefile index ffc1d5f..f7a44b7 100644 --- a/wmix/Makefile +++ b/wmix/Makefile @@ -2,7 +2,7 @@ CC = gcc CFLAGS = -O3 -W -Wall LDFLAGS = -L/usr/X11R6/lib LIBS = -lXpm -lXext -lX11 -lm -OBJECTS = misc.o mixer-oss.o ui_x.o wmix.o +OBJECTS = misc.o config.o mixer-oss.o ui_x.o wmix.o # where to install this program (also for packaging stuff) PREFIX = /usr/local diff --git a/wmix/config.c b/wmix/config.c new file mode 100644 index 0000000..9823949 --- /dev/null +++ b/wmix/config.c @@ -0,0 +1,95 @@ +/* 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. + */ +/* + * config.c: functions related to loading the configuration from file + */ + +#include +#include +#include + +#include + +#include "include/common.h" +#include "include/config.h" + + +/* The global configuration */ +struct _Config config; + + +/* + * 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) +{ + FILE *fp; + char buf[512]; + char *ptr; + + if (config.file == NULL) + return; + + fp = fopen(config.file, "r"); + if (!fp) + return; + + while (fgets(buf, 512, fp)) { + if ((ptr = strstr(buf, "mousewheel="))) { + ptr += 11; + config.mousewheel = atoi(ptr); + } + if ((ptr = strstr(buf, "scrolltext="))) { + ptr += 11; + config.scrolltext = atoi(ptr); + } + if ((ptr = strstr(buf, "osd="))) { + ptr += 4; + config.osd = atoi(ptr); + } + if ((ptr = strstr(buf, "osdcolor="))) { + char *end; + ptr += 9; + end = strchr(ptr, '\n'); + ptr[end - ptr] = '\0'; + if (config.osd_color) + free(config.osd_color); + config.osd_color = strdup(ptr); + } + if ((ptr = strstr(buf, "wheelstep="))) { + ptr += 10; + /* detect old style config */ + if (atoi(ptr) > 1) + config.scrollstep = (float)atoi(ptr) / 100.0; + else + config.scrollstep = atof(ptr); + } + if ((ptr = strstr(buf, "wheelbtn1="))) { + ptr += 10; + config.wheel_button_up = atoi(ptr); + } + if ((ptr = strstr(buf, "wheelbtn2="))) { + ptr += 10; + config.wheel_button_down = atoi(ptr); + } + } + fclose(fp); +} diff --git a/wmix/include/common.h b/wmix/include/common.h index 71aa8e6..4fdc857 100644 --- a/wmix/include/common.h +++ b/wmix/include/common.h @@ -35,16 +35,3 @@ typedef unsigned int bool; #define MAX_DOUBLE_CLICK_TIME 0.5 #define BUTTON_WHEEL_UP 4 #define BUTTON_WHEEL_DOWN 5 - -typedef struct _Config Config; - -struct _Config { - char *file; /* full path to config file name */ - unsigned int osd : 1; /* show OSD? */ - unsigned int mousewheel : 1; /* mousewheel enabled? */ - unsigned int scrolltext : 1; /* scroll channel names? */ - unsigned int wheel_button_up; /* up button */ - unsigned int wheel_button_down; /* down button */ - float scrollstep; /* scroll mouse step adjustment */ - char *osd_color; /* osd color */ -}; diff --git a/wmix/include/config.h b/wmix/include/config.h new file mode 100644 index 0000000..5d84ae9 --- /dev/null +++ b/wmix/include/config.h @@ -0,0 +1,41 @@ +/* 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. + */ +/* include/config.h: functions related to setting the configuration */ + +#ifndef WMIX_CONFIG_H +#define WMIX_CONFIG_H + +/* Global Configuration */ +extern struct _Config { + char *file; /* full path to config file name */ + + unsigned int osd : 1; /* show OSD? */ + unsigned int mousewheel : 1; /* mousewheel enabled? */ + unsigned int scrolltext : 1; /* scroll channel names? */ + + unsigned int wheel_button_up; /* up button */ + unsigned int wheel_button_down; /* down button */ + + float scrollstep; /* scroll mouse step adjustment */ + char *osd_color; /* osd color */ +} config; + +/* Read configuration from file */ +void config_read(void); + +#endif /* WMIX_CONFIG_H */ diff --git a/wmix/include/misc.h b/wmix/include/misc.h index 7794111..0ad4d87 100644 --- a/wmix/include/misc.h +++ b/wmix/include/misc.h @@ -23,5 +23,4 @@ void vb_to_lr (float volume, float balance, float *left, float *right); double get_current_time(void); void add_region (int index, int x, int y, int width, int height); int check_region (int x, int y); -void config_read (void); void create_pid_file (void); diff --git a/wmix/misc.c b/wmix/misc.c index 544a6e9..9120bd9 100644 --- a/wmix/misc.c +++ b/wmix/misc.c @@ -42,7 +42,6 @@ typedef struct { } MRegion; MRegion mr[16]; -extern Config config; /* Converts separate left and right channel volumes (each in [0, 1]) to * volume and balance values. (Volume is in [0, 1], balance is in [-1, 1]) @@ -109,61 +108,6 @@ int check_region(int x, int y) return (i - 1); } -void config_read(void) -{ - FILE *fp; - char buf[512]; - char *ptr; - - if (config.file == NULL) - return; - - fp = fopen(config.file, "r"); - if (!fp) - return; - - while (fgets(buf, 512, fp)) { - if ((ptr = strstr(buf, "mousewheel="))) { - ptr += 11; - config.mousewheel = atoi(ptr); - } - if ((ptr = strstr(buf, "scrolltext="))) { - ptr += 11; - config.scrolltext = atoi(ptr); - } - if ((ptr = strstr(buf, "osd="))) { - ptr += 4; - config.osd = atoi(ptr); - } - if ((ptr = strstr(buf, "osdcolor="))) { - char *end; - ptr += 9; - end = strchr(ptr, '\n'); - ptr[end - ptr] = '\0'; - if (config.osd_color) - free(config.osd_color); - config.osd_color = strdup(ptr); - } - if ((ptr = strstr(buf, "wheelstep="))) { - ptr += 10; - /* detect old style config */ - if (atoi(ptr) > 1) - config.scrollstep = (float)atoi(ptr) / 100.0; - else - config.scrollstep = atof(ptr); - } - if ((ptr = strstr(buf, "wheelbtn1="))) { - ptr += 10; - config.wheel_button_up = atoi(ptr); - } - if ((ptr = strstr(buf, "wheelbtn2="))) { - ptr += 10; - config.wheel_button_down = atoi(ptr); - } - } - fclose(fp); -} - /* handle writing PID file, silently ignore if we can't do it */ void create_pid_file(void) { diff --git a/wmix/ui_x.c b/wmix/ui_x.c index 2b993bf..31a818a 100644 --- a/wmix/ui_x.c +++ b/wmix/ui_x.c @@ -44,6 +44,8 @@ #include "include/misc.h" #include "include/mixer.h" #include "include/ui_x.h" +#include "include/config.h" + #ifndef PI #define PI M_PI @@ -72,8 +74,6 @@ struct _Dockapp { }; -extern Config config; - static Pixmap led_on_pixmap; static Pixmap led_on_mask; static Pixmap led_off_pixmap; diff --git a/wmix/wmix.c b/wmix/wmix.c index 84b1e42..331c3b9 100644 --- a/wmix/wmix.c +++ b/wmix/wmix.c @@ -37,6 +37,7 @@ #include "include/mixer.h" #include "include/misc.h" #include "include/ui_x.h" +#include "include/config.h" #define VERSION "3.0" @@ -49,7 +50,6 @@ static double prev_button_press_time = 0.0; static float display_height; static float display_width; -Config config; static int mouse_drag_home_x; static int mouse_drag_home_y; static int idle_loop;