wmix: create the PID file only after successful initialisation
[dockapps.git] / wmix / wmix.c
blob84b1e42941c827d0efbfa83b04dd963853d81cee
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;
125 memset(&config, 0, sizeof(config));
127 /* we can theoretically live without a config file */
128 home = getenv("HOME");
129 if (home) {
130 config.file = calloc(1, strlen(home) + 9);
131 sprintf(config.file, "%s/.wmixrc", home);
134 /* default values */
135 config.mousewheel = 1;
136 config.scrolltext = 1;
137 config.wheel_button_up = 4;
138 config.wheel_button_down = 5;
139 config.scrollstep = 0.03;
140 config.osd = 1;
141 config.osd_color = strdup("green");
143 parse_cli_options(argc, argv);
144 config_read();
146 if (mixer_device == NULL)
147 mixer_device = "/dev/mixer";
149 mixer_init(mixer_device, verbose, (const char **)exclude);
150 mixer_set_channel(0);
152 display = XOpenDisplay(display_name);
153 if (display == NULL) {
154 const char *name;
156 if (display_name) {
157 name = display_name;
158 } else {
159 name = getenv("DISPLAY");
160 if (name == NULL) {
161 fprintf(stderr, "wmix:error: Unable to open display, variable $DISPLAY not set\n");
162 return EXIT_FAILURE;
165 fprintf(stderr, "wmix:error: Unable to open display \"%s\"\n", 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 create_pid_file();
189 signal(SIGUSR1, (void *) signal_catch);
190 signal(SIGUSR2, (void *) signal_catch);
192 while (true) {
193 if (button_pressed || slider_pressed || (XPending(display) > 0)) {
194 XNextEvent(display, &event);
195 switch (event.type) {
196 case Expose:
197 redraw_window();
198 break;
199 case ButtonPress:
200 button_press_event(&event.xbutton);
201 idle_loop = 0;
202 break;
203 case ButtonRelease:
204 button_release_event(&event.xbutton);
205 idle_loop = 0;
206 break;
207 case MotionNotify:
208 /* process cursor change, or drag events */
209 motion_event(&event.xmotion);
210 idle_loop = 0;
211 break;
212 case LeaveNotify:
213 /* go back to standard cursor */
214 if ((!button_pressed) && (!slider_pressed))
215 set_cursor(NORMAL_CURSOR);
216 break;
217 case DestroyNotify:
218 XCloseDisplay(display);
219 return EXIT_SUCCESS;
220 default:
221 break;
223 } else {
224 usleep(100000);
225 scroll_text(3, 4, 57, false);
226 /* rescroll message after some delay */
227 if (idle_loop++ > 256) {
228 scroll_text(3, 4, 57, true);
229 idle_loop = 0;
231 /* get rid of OSD after a few seconds of idle */
232 if ((idle_loop > 15) && osd_mapped() && !button_pressed) {
233 unmap_osd();
234 idle_loop = 0;
236 if (mixer_is_changed())
237 ui_update();
240 return EXIT_SUCCESS;
243 static void signal_catch(int sig)
245 switch (sig) {
246 case SIGUSR1:
247 mixer_set_volume_rel(config.scrollstep);
248 if (!osd_mapped())
249 map_osd();
250 if (osd_mapped())
251 update_osd(mixer_get_volume(), false);
252 ui_update();
253 idle_loop = 0;
254 break;
255 case SIGUSR2:
256 mixer_set_volume_rel(-config.scrollstep);
257 if (!osd_mapped())
258 map_osd();
259 if (osd_mapped())
260 update_osd(mixer_get_volume(), false);
261 ui_update();
262 idle_loop = 0;
263 break;
267 static void button_press_event(XButtonEvent *event)
269 double button_press_time = get_current_time();
270 int x = event->x;
271 int y = event->y;
272 bool double_click = false;
274 /* handle wheel scrolling to adjust volume */
275 if (config.mousewheel) {
276 if (event->button == config.wheel_button_up) {
277 mixer_set_volume_rel(config.scrollstep);
278 if (!osd_mapped())
279 map_osd();
280 if (osd_mapped())
281 update_osd(mixer_get_volume(), false);
282 ui_update();
283 return;
285 if (event->button == config.wheel_button_down) {
286 mixer_set_volume_rel(-config.scrollstep);
287 if (!osd_mapped())
288 map_osd();
289 if (osd_mapped())
290 update_osd(mixer_get_volume(), false);
291 ui_update();
292 return;
296 if ((button_press_time - prev_button_press_time) <= 0.5) {
297 double_click = true;
298 prev_button_press_time = 0.0;
299 } else
300 prev_button_press_time = button_press_time;
302 switch (check_region(x, y)) {
303 case 1: /* on knob */
304 button_pressed = true;
305 slider_pressed = false;
306 mouse_drag_home_x = x;
307 mouse_drag_home_y = y;
308 if (double_click) {
309 mixer_toggle_mute();
310 ui_update();
312 break;
313 case 2: /* on slider */
314 button_pressed = false;
315 slider_pressed = true;
316 mouse_drag_home_x = x;
317 mouse_drag_home_y = y;
318 if (double_click) {
319 mixer_set_balance(0.0);
320 ui_update();
322 break;
323 case 3: /* previous channel */
324 mixer_set_channel_rel(-1);
325 blit_string(config.scrolltext ? mixer_get_channel_name() : mixer_get_short_name());
326 scroll_text(3, 4, 57, true);
327 unmap_osd();
328 map_osd();
329 ui_update();
330 break;
331 case 4: /* next channel */
332 mixer_set_channel_rel(1);
333 blit_string(config.scrolltext ? mixer_get_channel_name() : mixer_get_short_name());
334 scroll_text(3, 4, 57, true);
335 unmap_osd();
336 map_osd();
337 ui_update();
338 break;
339 case 5: /* toggle mute */
340 mixer_toggle_mute();
341 ui_update();
342 break;
343 case 6: /* toggle rec */
344 mixer_toggle_rec();
345 ui_update();
346 break;
347 case 10:
348 scroll_text(3, 4, 57, true);
349 break;
350 default:
351 printf("unknown region pressed\n");
352 break;
356 static void button_release_event(XButtonEvent *event)
358 int x = event->x;
359 int y = event->y;
360 int region;
362 region = check_region(x, y);
364 if (region == 1)
365 set_cursor(HAND_CURSOR);
367 button_pressed = false;
368 slider_pressed = false;
371 static void motion_event(XMotionEvent *event)
373 int x = event->x;
374 int y = event->y;
375 int region;
377 if ((x == mouse_drag_home_x) && (y == mouse_drag_home_y))
378 return;
380 region = check_region(x, y);
382 if (button_pressed) {
383 if (y != mouse_drag_home_y) {
384 float delta;
386 set_cursor(NULL_CURSOR);
388 delta = (float)(mouse_drag_home_y - y) / display_height;
389 knob_turn(delta);
390 if (!osd_mapped())
391 map_osd();
392 if (osd_mapped())
393 update_osd(mixer_get_volume(), false);
395 XWarpPointer(display, None, event->window, x, y, 0, 0,
396 mouse_drag_home_x, mouse_drag_home_y);
397 return;
400 if (slider_pressed) {
401 if (x != mouse_drag_home_x) {
402 float delta;
404 set_cursor(NULL_CURSOR);
406 delta = (float)(x - mouse_drag_home_x) / display_width;
407 slider_move(delta);
409 XWarpPointer(display, None, event->window, x, y, 0, 0,
410 mouse_drag_home_x, mouse_drag_home_y);
411 return;
414 if (region == 1)
415 set_cursor(HAND_CURSOR);
416 else if (region == 2)
417 set_cursor(BAR_CURSOR);
418 else
419 set_cursor(NORMAL_CURSOR);