wmpager: add missing Makefile.am
[dockapps.git] / wmix / wmix.c
blob5e31b5d741f4d09ba83603f924a45e5b742b2ff3
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.
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <signal.h>
26 #include <string.h>
27 #include <getopt.h>
28 #include <unistd.h>
30 #include <X11/X.h>
31 #include <X11/Xlib.h>
32 #include <X11/Xutil.h>
34 #include <sys/soundcard.h>
36 #include "include/common.h"
37 #include "include/mixer.h"
38 #include "include/misc.h"
39 #include "include/ui_x.h"
41 #define VERSION "3.0"
43 static Display *display;
44 static char *display_name = NULL;
45 static char *mixer_device = NULL;
46 static bool button_pressed = false;
47 static bool slider_pressed = false;
48 static double prev_button_press_time = 0.0;
50 static float display_height;
51 static float display_width;
52 Config config;
53 static int mouse_drag_home_x;
54 static int mouse_drag_home_y;
55 static int idle_loop;
56 static bool verbose;
57 static char *exclude[SOUND_MIXER_NRDEVICES];
59 /* local stuff */
60 static void parse_cli_options(int argc, char **argv);
61 static void signal_catch(int sig);
62 static void button_press_event(XButtonEvent *event);
63 static void button_release_event(XButtonEvent *event);
64 static void motion_event(XMotionEvent *event);
66 #define HELP_TEXT \
67 "WMixer " VERSION " by timecop@japan.co.jp + skunk@mit.edu\n" \
68 "usage:\n" \
69 " -d <dsp> connect to remote X display\n" \
70 " -f <file> parse this config [~/.wmixrc]\n" \
71 " -m <dev> mixer device [/dev/mixer]\n" \
72 " -h print this help\n" \
73 " -v verbose -> id, long name, name\n" \
74 " -e <name> exclude channel, can be used many times\n" \
76 static void parse_cli_options(int argc, char **argv)
78 int opt;
79 int count_exclude = 0 ;
80 verbose = false ;
82 while ((opt = getopt(argc, argv, "d:f:hm:ve:")) != EOF) {
83 switch (opt) {
84 case 'd':
85 if (optarg != NULL)
86 display_name = strdup(optarg);
87 break;
88 case 'm':
89 if (optarg != NULL)
90 mixer_device = strdup(optarg);
91 break;
92 case 'f':
93 if (optarg != NULL)
94 if (config.file != NULL)
95 free(config.file);
96 config.file = strdup(optarg);
97 break;
98 case 'h':
99 fputs(HELP_TEXT, stdout);
100 exit(0);
101 break;
102 case 'v':
103 verbose = true;
104 break;
105 case 'e':
106 if (count_exclude < SOUND_MIXER_NRDEVICES) {
107 exclude[count_exclude] = strdup(optarg);
108 /* printf("exclude : %s\n", exclude[count_exclude]); */
109 count_exclude++;
110 } else
111 fprintf(stderr, "Warning: You can't exclude this many channels\n");
112 break;
113 default:
114 break;
117 exclude[count_exclude] = NULL ;
120 int main(int argc, char **argv)
122 XEvent event;
123 char *home;
124 char *pid;
125 FILE *fp;
127 memset(&config, 0, sizeof(config));
129 /* we can theoretically live without a config file */
130 home = getenv("HOME");
131 if (home) {
132 config.file = calloc(1, strlen(home) + 9);
133 sprintf(config.file, "%s/.wmixrc", home);
136 /* handle writing PID file, silently ignore if we can't do it */
137 pid = calloc(1, strlen(home) + 10);
138 sprintf(pid, "%s/.wmix.pid", home);
139 fp = fopen(pid, "w");
140 if (fp) {
141 fprintf(fp, "%d\n", getpid());
142 fclose(fp);
144 free(pid);
146 /* default values */
147 config.mousewheel = 1;
148 config.scrolltext = 1;
149 config.wheel_button_up = 4;
150 config.wheel_button_down = 5;
151 config.scrollstep = 0.03;
152 config.osd = 1;
153 config.osd_color = strdup("green");
155 parse_cli_options(argc, argv);
156 config_read();
158 if (mixer_device == NULL)
159 mixer_device = "/dev/mixer";
161 mixer_init(mixer_device, verbose, (const char **)exclude);
162 mixer_set_channel(0);
164 if ((display = XOpenDisplay(display_name)) == NULL) {
165 fprintf(stderr, "Unable to open display \"%s\"\n", display_name);
166 return EXIT_FAILURE;
168 display_width = (float)DisplayWidth(display, DefaultScreen(display)) / 4.0;
169 display_height = (float)DisplayHeight(display, DefaultScreen(display)) / 2.0;
171 dockapp_init(display);
172 new_window("wmix", 64, 64);
173 new_osd(DisplayWidth(display, DefaultScreen(display)) - 200, 60);
174 blit_string("wmix 3.0");
175 scroll_text(3, 4, 57, true);
176 ui_update();
178 /* add click regions */
179 add_region(1, 37, 36, 25, 25); /* knob */
180 add_region(2, 4, 42, 27, 15); /* balancer */
181 add_region(3, 2, 26, 7, 10); /* previous channel */
182 add_region(4, 10, 26, 7, 10); /* next channel */
183 add_region(5, 39, 14, 20, 7); /* mute toggle */
184 add_region(6, 4, 14, 13, 7); /* rec toggle */
185 add_region(10, 3, 4, 56, 7); /* re-scroll current channel name */
187 /* setup up/down signal handler */
188 signal(SIGUSR1, (void *) signal_catch);
189 signal(SIGUSR2, (void *) signal_catch);
191 while (true) {
192 if (button_pressed || slider_pressed || (XPending(display) > 0)) {
193 XNextEvent(display, &event);
194 switch (event.type) {
195 case Expose:
196 redraw_window();
197 break;
198 case ButtonPress:
199 button_press_event(&event.xbutton);
200 idle_loop = 0;
201 break;
202 case ButtonRelease:
203 button_release_event(&event.xbutton);
204 idle_loop = 0;
205 break;
206 case MotionNotify:
207 /* process cursor change, or drag events */
208 motion_event(&event.xmotion);
209 idle_loop = 0;
210 break;
211 case LeaveNotify:
212 /* go back to standard cursor */
213 if ((!button_pressed) && (!slider_pressed))
214 set_cursor(NORMAL_CURSOR);
215 break;
216 case DestroyNotify:
217 XCloseDisplay(display);
218 return EXIT_SUCCESS;
219 default:
220 break;
222 } else {
223 usleep(100000);
224 scroll_text(3, 4, 57, false);
225 /* rescroll message after some delay */
226 if (idle_loop++ > 256) {
227 scroll_text(3, 4, 57, true);
228 idle_loop = 0;
230 /* get rid of OSD after a few seconds of idle */
231 if ((idle_loop > 15) && osd_mapped() && !button_pressed) {
232 unmap_osd();
233 idle_loop = 0;
235 if (mixer_is_changed())
236 ui_update();
239 return EXIT_SUCCESS;
242 static void signal_catch(int sig)
244 switch (sig) {
245 case SIGUSR1:
246 mixer_set_volume_rel(config.scrollstep);
247 if (!osd_mapped())
248 map_osd();
249 if (osd_mapped())
250 update_osd(mixer_get_volume(), false);
251 ui_update();
252 idle_loop = 0;
253 break;
254 case SIGUSR2:
255 mixer_set_volume_rel(-config.scrollstep);
256 if (!osd_mapped())
257 map_osd();
258 if (osd_mapped())
259 update_osd(mixer_get_volume(), false);
260 ui_update();
261 idle_loop = 0;
262 break;
266 static void button_press_event(XButtonEvent *event)
268 double button_press_time = get_current_time();
269 int x = event->x;
270 int y = event->y;
271 bool double_click = false;
273 /* handle wheel scrolling to adjust volume */
274 if (config.mousewheel) {
275 if (event->button == config.wheel_button_up) {
276 mixer_set_volume_rel(config.scrollstep);
277 if (!osd_mapped())
278 map_osd();
279 if (osd_mapped())
280 update_osd(mixer_get_volume(), false);
281 ui_update();
282 return;
284 if (event->button == config.wheel_button_down) {
285 mixer_set_volume_rel(-config.scrollstep);
286 if (!osd_mapped())
287 map_osd();
288 if (osd_mapped())
289 update_osd(mixer_get_volume(), false);
290 ui_update();
291 return;
295 if ((button_press_time - prev_button_press_time) <= 0.5) {
296 double_click = true;
297 prev_button_press_time = 0.0;
298 } else
299 prev_button_press_time = button_press_time;
301 switch (check_region(x, y)) {
302 case 1: /* on knob */
303 button_pressed = true;
304 slider_pressed = false;
305 mouse_drag_home_x = x;
306 mouse_drag_home_y = y;
307 if (double_click) {
308 mixer_toggle_mute();
309 ui_update();
311 break;
312 case 2: /* on slider */
313 button_pressed = false;
314 slider_pressed = true;
315 mouse_drag_home_x = x;
316 mouse_drag_home_y = y;
317 if (double_click) {
318 mixer_set_balance(0.0);
319 ui_update();
321 break;
322 case 3: /* previous channel */
323 mixer_set_channel_rel(-1);
324 blit_string(config.scrolltext ? mixer_get_channel_name() : mixer_get_short_name());
325 scroll_text(3, 4, 57, true);
326 unmap_osd();
327 map_osd();
328 ui_update();
329 break;
330 case 4: /* next channel */
331 mixer_set_channel_rel(1);
332 blit_string(config.scrolltext ? mixer_get_channel_name() : mixer_get_short_name());
333 scroll_text(3, 4, 57, true);
334 unmap_osd();
335 map_osd();
336 ui_update();
337 break;
338 case 5: /* toggle mute */
339 mixer_toggle_mute();
340 ui_update();
341 break;
342 case 6: /* toggle rec */
343 mixer_toggle_rec();
344 ui_update();
345 break;
346 case 10:
347 scroll_text(3, 4, 57, true);
348 break;
349 default:
350 printf("unknown region pressed\n");
351 break;
355 static void button_release_event(XButtonEvent *event)
357 int x = event->x;
358 int y = event->y;
359 int region;
361 region = check_region(x, y);
363 if (region == 1)
364 set_cursor(HAND_CURSOR);
366 button_pressed = false;
367 slider_pressed = false;
370 static void motion_event(XMotionEvent *event)
372 int x = event->x;
373 int y = event->y;
374 int region;
376 if ((x == mouse_drag_home_x) && (y == mouse_drag_home_y))
377 return;
379 region = check_region(x, y);
381 if (button_pressed) {
382 if (y != mouse_drag_home_y) {
383 float delta;
385 set_cursor(NULL_CURSOR);
387 delta = (float)(mouse_drag_home_y - y) / display_height;
388 knob_turn(delta);
389 if (!osd_mapped())
390 map_osd();
391 if (osd_mapped())
392 update_osd(mixer_get_volume(), false);
394 XWarpPointer(display, None, event->window, x, y, 0, 0,
395 mouse_drag_home_x, mouse_drag_home_y);
396 return;
399 if (slider_pressed) {
400 if (x != mouse_drag_home_x) {
401 float delta;
403 set_cursor(NULL_CURSOR);
405 delta = (float)(x - mouse_drag_home_x) / display_width;
406 slider_move(delta);
408 XWarpPointer(display, None, event->window, x, y, 0, 0,
409 mouse_drag_home_x, mouse_drag_home_y);
410 return;
413 if (region == 1)
414 set_cursor(HAND_CURSOR);
415 else if (region == 2)
416 set_cursor(BAR_CURSOR);
417 else
418 set_cursor(NORMAL_CURSOR);