1 /* WMix 3.0 -- a mixer using the OSS mixer API.
2  * Copyright (C) 2000, 2001 timecop@japan.co.jp
3  * Mixer code in version 3.0 based on mixer api library by
4  * Daniel Richard G. <skunk@mit.edu>, which in turn was based on
5  * the mixer code in WMix 2.x releases.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21 
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <signal.h>
26 #include <string.h>
27 #include <unistd.h>
28 
29 #include <X11/X.h>
30 #include <X11/Xlib.h>
31 #include <X11/Xutil.h>
32 
33 #include <sys/soundcard.h>
34 
35 #include "include/common.h"
36 #include "include/mixer.h"
37 #include "include/misc.h"
38 #include "include/ui_x.h"
39 
40 #define VERSION "3.0"
41 
42 static Display *display;
43 static char *display_name = NULL;
44 static char *mixer_device = NULL;
45 static bool button_pressed = false;
46 static bool slider_pressed = false;
47 static double prev_button_press_time = 0.0;
48 
49 static float display_height;
50 static float display_width;
51 Config config;
52 static int mouse_drag_home_x;
53 static int mouse_drag_home_y;
54 static int idle_loop;
55 static bool verbose;
56 static char *exclude[SOUND_MIXER_NRDEVICES];
57 
58 /* local stuff */
59 static void parse_cli_options(int argc, char **argv);
60 static void signal_catch(int sig);
61 static void button_press_event(XButtonEvent *event);
62 static void button_release_event(XButtonEvent *event);
63 static void motion_event(XMotionEvent *event);
64 
65 #define HELP_TEXT \
66 	"WMixer " VERSION " by timecop@japan.co.jp + skunk@mit.edu\n" \
67 	"usage:\n" \
68 	"  -d <dsp>  connect to remote X display\n" \
69 	"  -f <file> parse this config [~/.wmixrc]\n" \
70 	"  -m <dev>  mixer device [/dev/mixer]\n" \
71 	"  -h        print this help\n" \
72 	"  -v        verbose -> id, long name, name\n" \
73 	"  -e <name> exclude channel, can be used many times\n" \
74 
parse_cli_options(int argc,char ** argv)75 static void parse_cli_options(int argc, char **argv)
76 {
77     int opt;
78     int count_exclude = 0 ;
79     verbose = false ;
80 
81     while ((opt = getopt(argc, argv, "d:f:hm:ve:")) != EOF) {
82 	switch (opt) {
83 	    case 'd':
84 		if (optarg != NULL)
85 		    display_name = strdup(optarg);
86 		break;
87 	    case 'm':
88 		if (optarg != NULL)
89 		    mixer_device = strdup(optarg);
90 		break;
91 	    case 'f':
92 		if (optarg != NULL)
93 		    if (config.file != NULL)
94 			free(config.file);
95 		    config.file = strdup(optarg);
96 		break;
97 	    case 'h':
98 		fputs(HELP_TEXT, stdout);
99 		exit(0);
100 		break;
101    	    case 'v':
102 	        verbose = true;
103 		break;
104    	    case 'e':
105 		if (count_exclude < SOUND_MIXER_NRDEVICES) {
106 		    exclude[count_exclude] = strdup(optarg);
107 		    /* printf("exclude : %s\n", exclude[count_exclude]); */
108 		    count_exclude++;
109 		} else
110 		    fprintf(stderr, "Warning: You can't exclude this many channels\n");
111 		break;
112 	    default:
113 		break;
114 	}
115     }
116     exclude[count_exclude] = NULL ;
117 }
118 
main(int argc,char ** argv)119 int main(int argc, char **argv)
120 {
121     XEvent event;
122     char *home;
123     char *pid;
124     FILE *fp;
125 
126     memset(&config, 0, sizeof(config));
127 
128     /* we can theoretically live without a config file */
129     home = getenv("HOME");
130     if (home) {
131 	config.file = calloc(1, strlen(home) + 9);
132 	sprintf(config.file, "%s/.wmixrc", home);
133     }
134 
135     /* handle writing PID file, silently ignore if we can't do it */
136     pid = calloc(1, strlen(home) + 11);
137     sprintf(pid, "%s/.wmix.pid", home);
138     fp = fopen(pid, "w");
139     if (fp) {
140 	fprintf(fp, "%d\n", getpid());
141 	fclose(fp);
142     }
143     free(pid);
144 
145     /* default values */
146     config.mousewheel = 1;
147     config.scrolltext = 1;
148     config.wheel_button_up = 4;
149     config.wheel_button_down = 5;
150     config.scrollstep = 0.03;
151     config.osd = 1;
152     config.osd_color = strdup("green");
153 
154     parse_cli_options(argc, argv);
155     config_read();
156 
157     if (mixer_device == NULL)
158 	mixer_device = "/dev/mixer";
159 
160     mixer_init(mixer_device, verbose, (const char **)exclude);
161     mixer_set_channel(0);
162 
163     if ((display = XOpenDisplay(display_name)) == NULL) {
164 	fprintf(stderr, "Unable to open display \"%s\"\n", display_name);
165 	return EXIT_FAILURE;
166     }
167     display_width = (float)DisplayWidth(display, DefaultScreen(display)) / 4.0;
168     display_height = (float)DisplayHeight(display, DefaultScreen(display)) / 2.0;
169 
170     dockapp_init(display);
171     new_window("wmix", 64, 64);
172     new_osd(DisplayWidth(display, DefaultScreen(display)) - 200, 60);
173     blit_string("wmix 3.0");
174     scroll_text(3, 4, 57, true);
175     ui_update();
176 
177     /* add click regions */
178     add_region(1, 37, 36, 25, 25);	/* knob */
179     add_region(2, 4, 42, 27, 15);	/* balancer */
180     add_region(3, 2, 26, 7, 10);	/* previous channel */
181     add_region(4, 10, 26, 7, 10);	/* next channel */
182     add_region(5, 39, 14, 20, 7);	/* mute toggle */
183     add_region(6, 4, 14, 13, 7);	/* rec toggle */
184     add_region(10, 3, 4, 56, 7);	/* re-scroll current channel name */
185 
186     /* setup up/down signal handler */
187     signal(SIGUSR1, (void *) signal_catch);
188     signal(SIGUSR2, (void *) signal_catch);
189 
190     while (true) {
191 	if (button_pressed || slider_pressed || (XPending(display) > 0)) {
192 	    XNextEvent(display, &event);
193 	    switch (event.type) {
194 		case Expose:
195 		    redraw_window();
196 		    break;
197 		case ButtonPress:
198 		    button_press_event(&event.xbutton);
199 		    idle_loop = 0;
200 		    break;
201 		case ButtonRelease:
202 		    button_release_event(&event.xbutton);
203 		    idle_loop = 0;
204 		    break;
205 		case MotionNotify:
206 		    /* process cursor change, or drag events */
207 		    motion_event(&event.xmotion);
208 		    idle_loop = 0;
209 		    break;
210 		case LeaveNotify:
211 		    /* go back to standard cursor */
212 		    if ((!button_pressed) && (!slider_pressed))
213 			set_cursor(NORMAL_CURSOR);
214 		    break;
215 		case DestroyNotify:
216 		    XCloseDisplay(display);
217 		    return EXIT_SUCCESS;
218 		default:
219 		    break;
220 	    }
221 	} else {
222 	    usleep(100000);
223 	    scroll_text(3, 4, 57, false);
224 	    /* rescroll message after some delay */
225 	    if (idle_loop++ > 256) {
226 		scroll_text(3, 4, 57, true);
227 		idle_loop = 0;
228 	    }
229 	    /* get rid of OSD after a few seconds of idle */
230 	    if ((idle_loop > 15) && osd_mapped() && !button_pressed) {
231 		unmap_osd();
232 		idle_loop = 0;
233 	    }
234 	    if (mixer_is_changed())
235 		ui_update();
236 	}
237     }
238     return EXIT_SUCCESS;
239 }
240 
signal_catch(int sig)241 static void signal_catch(int sig)
242 {
243     switch (sig) {
244 	case SIGUSR1:
245 	    mixer_set_volume_rel(config.scrollstep);
246 	    if (!osd_mapped())
247 		map_osd();
248 	    if (osd_mapped())
249 		update_osd(mixer_get_volume(), false);
250 	    ui_update();
251 	    idle_loop = 0;
252 	    break;
253 	case SIGUSR2:
254 	    mixer_set_volume_rel(-config.scrollstep);
255 	    if (!osd_mapped())
256 		map_osd();
257 	    if (osd_mapped())
258 		update_osd(mixer_get_volume(), false);
259 	    ui_update();
260 	    idle_loop = 0;
261 	    break;
262     }
263 }
264 
button_press_event(XButtonEvent * event)265 static void button_press_event(XButtonEvent *event)
266 {
267     double button_press_time = get_current_time();
268     int x = event->x;
269     int y = event->y;
270     bool double_click = false;
271 
272     /* handle wheel scrolling to adjust volume */
273     if (config.mousewheel) {
274 	if (event->button == config.wheel_button_up) {
275 	    mixer_set_volume_rel(config.scrollstep);
276 	    if (!osd_mapped())
277 		map_osd();
278 	    if (osd_mapped())
279 		update_osd(mixer_get_volume(), false);
280 	    ui_update();
281 	    return;
282 	}
283 	if (event->button == config.wheel_button_down) {
284 	    mixer_set_volume_rel(-config.scrollstep);
285 	    if (!osd_mapped())
286 		map_osd();
287 	    if (osd_mapped())
288 		update_osd(mixer_get_volume(), false);
289 	    ui_update();
290 	    return;
291 	}
292     }
293 
294     if ((button_press_time - prev_button_press_time) <= 0.5) {
295 	double_click = true;
296 	prev_button_press_time = 0.0;
297     } else
298 	prev_button_press_time = button_press_time;
299 
300     switch (check_region(x, y)) {
301 	case 1:			/* on knob */
302 	    button_pressed = true;
303 	    slider_pressed = false;
304 	    mouse_drag_home_x = x;
305 	    mouse_drag_home_y = y;
306 	    if (double_click) {
307 		mixer_toggle_mute();
308 		ui_update();
309 	    }
310 	    break;
311 	case 2:			/* on slider */
312 	    button_pressed = false;
313 	    slider_pressed = true;
314 	    mouse_drag_home_x = x;
315 	    mouse_drag_home_y = y;
316 	    if (double_click) {
317 		mixer_set_balance(0.0);
318 		ui_update();
319 	    }
320 	    break;
321 	case 3:			/* previous channel */
322 	    mixer_set_channel_rel(-1);
323 	    blit_string(config.scrolltext ? mixer_get_channel_name() : mixer_get_short_name());
324 	    scroll_text(3, 4, 57, true);
325 	    unmap_osd();
326 	    map_osd();
327 	    ui_update();
328 	    break;
329 	case 4:			/* next channel */
330 	    mixer_set_channel_rel(1);
331 	    blit_string(config.scrolltext ? mixer_get_channel_name() : mixer_get_short_name());
332 	    scroll_text(3, 4, 57, true);
333 	    unmap_osd();
334 	    map_osd();
335 	    ui_update();
336 	    break;
337 	case 5:			/* toggle mute */
338 	    mixer_toggle_mute();
339 	    ui_update();
340 	    break;
341 	case 6:			/* toggle rec */
342 	    mixer_toggle_rec();
343 	    ui_update();
344 	    break;
345 	case 10:
346 	    scroll_text(3, 4, 57, true);
347 	    break;
348 	default:
349 	    printf("unknown region pressed\n");
350 	    break;
351     }
352 }
353 
button_release_event(XButtonEvent * event)354 static void button_release_event(XButtonEvent *event)
355 {
356     int x = event->x;
357     int y = event->y;
358     int region;
359 
360     region = check_region(x, y);
361 
362     if (region == 1)
363 	set_cursor(HAND_CURSOR);
364 
365     button_pressed = false;
366     slider_pressed = false;
367 }
368 
motion_event(XMotionEvent * event)369 static void motion_event(XMotionEvent *event)
370 {
371     int x = event->x;
372     int y = event->y;
373     int region;
374 
375     if ((x == mouse_drag_home_x) && (y == mouse_drag_home_y))
376 	return;
377 
378     region = check_region(x, y);
379 
380     if (button_pressed) {
381 	if (y != mouse_drag_home_y) {
382 	    float delta;
383 
384 	    set_cursor(NULL_CURSOR);
385 
386 	    delta = (float)(mouse_drag_home_y - y) / display_height;
387 	    knob_turn(delta);
388 	    if (!osd_mapped())
389 		map_osd();
390 	    if (osd_mapped())
391 		update_osd(mixer_get_volume(), false);
392 	}
393 	XWarpPointer(display, None, event->window, x, y, 0, 0,
394 			mouse_drag_home_x, mouse_drag_home_y);
395 	return;
396     }
397 
398     if (slider_pressed) {
399 	if (x != mouse_drag_home_x) {
400 	    float delta;
401 
402 	    set_cursor(NULL_CURSOR);
403 
404 	    delta = (float)(x - mouse_drag_home_x) / display_width;
405 	    slider_move(delta);
406 	}
407 	XWarpPointer(display, None, event->window, x, y, 0, 0,
408 			mouse_drag_home_x, mouse_drag_home_y);
409 	return;
410     }
411 
412     if (region == 1)
413 	set_cursor(HAND_CURSOR);
414     else if (region == 2)
415 	set_cursor(BAR_CURSOR);
416     else
417 	set_cursor(NORMAL_CURSOR);
418 }
419