Remove trailing whitespace.
[dockapps.git] / wmix / wmix.c
blobb2ff99f764f2f33d3349f740eddd035a05d5f6a5
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"
40 #include "include/mmkeys.h"
41 #include "include/config.h"
44 static Display *display;
45 static bool button_pressed = false;
46 static bool slider_pressed = false;
47 static double prev_button_press_time = 0.0;
49 static float display_height;
50 static float display_width;
51 static int mouse_drag_home_x;
52 static int mouse_drag_home_y;
53 static int idle_loop;
55 /* local stuff */
56 static void signal_catch(int sig);
57 static void button_press_event(XButtonEvent *event);
58 static void button_release_event(XButtonEvent *event);
59 static int key_press_event(XKeyEvent *event);
60 static void motion_event(XMotionEvent *event);
63 int main(int argc, char **argv)
65 XEvent event;
67 config_init();
68 parse_cli_options(argc, argv);
69 config_read();
71 mixer_init(config.mixer_device, config.verbose, (const char **)config.exclude_channel);
72 mixer_set_channel(0);
74 display = XOpenDisplay(config.display_name);
75 if (display == NULL) {
76 const char *name;
78 if (config.display_name) {
79 name = config.display_name;
80 } else {
81 name = getenv("DISPLAY");
82 if (name == NULL) {
83 fprintf(stderr, "wmix:error: Unable to open display, variable $DISPLAY not set\n");
84 return EXIT_FAILURE;
87 fprintf(stderr, "wmix:error: Unable to open display \"%s\"\n", name);
88 return EXIT_FAILURE;
90 display_width = (float)DisplayWidth(display, DefaultScreen(display)) / 4.0;
91 display_height = (float)DisplayHeight(display, DefaultScreen(display)) / 2.0;
93 dockapp_init(display);
94 new_window("wmix", 64, 64);
95 new_osd(DisplayWidth(display, DefaultScreen(display)) - 200, 60);
97 if (config.mmkeys)
98 mmkey_install(display);
100 config_release();
102 blit_string("wmix " VERSION);
103 scroll_text(3, 4, 57, true);
104 ui_update();
106 /* add click regions */
107 add_region(1, 37, 36, 25, 25); /* knob */
108 add_region(2, 4, 42, 27, 15); /* balancer */
109 add_region(3, 2, 26, 7, 10); /* previous channel */
110 add_region(4, 10, 26, 7, 10); /* next channel */
111 add_region(5, 39, 14, 20, 7); /* mute toggle */
112 add_region(6, 4, 14, 13, 7); /* rec toggle */
113 add_region(10, 3, 4, 56, 7); /* re-scroll current channel name */
115 /* setup up/down signal handler */
116 create_pid_file();
117 signal(SIGUSR1, (void *) signal_catch);
118 signal(SIGUSR2, (void *) signal_catch);
120 while (true) {
121 if (button_pressed || slider_pressed || (XPending(display) > 0)) {
122 XNextEvent(display, &event);
123 switch (event.type) {
124 case KeyPress:
125 if (key_press_event(&event.xkey))
126 idle_loop = 0;
127 break;
128 case Expose:
129 redraw_window();
130 break;
131 case ButtonPress:
132 button_press_event(&event.xbutton);
133 idle_loop = 0;
134 break;
135 case ButtonRelease:
136 button_release_event(&event.xbutton);
137 idle_loop = 0;
138 break;
139 case MotionNotify:
140 /* process cursor change, or drag events */
141 motion_event(&event.xmotion);
142 idle_loop = 0;
143 break;
144 case LeaveNotify:
145 /* go back to standard cursor */
146 if ((!button_pressed) && (!slider_pressed))
147 set_cursor(NORMAL_CURSOR);
148 break;
149 case DestroyNotify:
150 XCloseDisplay(display);
151 return EXIT_SUCCESS;
152 default:
153 break;
155 } else {
156 usleep(100000);
157 scroll_text(3, 4, 57, false);
158 /* rescroll message after some delay */
159 if (idle_loop++ > 256) {
160 scroll_text(3, 4, 57, true);
161 idle_loop = 0;
163 /* get rid of OSD after a few seconds of idle */
164 if ((idle_loop > 15) && osd_mapped() && !button_pressed) {
165 unmap_osd();
166 idle_loop = 0;
168 if (mixer_is_changed())
169 ui_update();
172 return EXIT_SUCCESS;
175 static void signal_catch(int sig)
177 switch (sig) {
178 case SIGUSR1:
179 mixer_set_volume_rel(config.scrollstep);
180 if (!osd_mapped())
181 map_osd();
182 if (osd_mapped())
183 update_osd(mixer_get_volume(), false);
184 ui_update();
185 idle_loop = 0;
186 break;
187 case SIGUSR2:
188 mixer_set_volume_rel(-config.scrollstep);
189 if (!osd_mapped())
190 map_osd();
191 if (osd_mapped())
192 update_osd(mixer_get_volume(), false);
193 ui_update();
194 idle_loop = 0;
195 break;
199 static void button_press_event(XButtonEvent *event)
201 double button_press_time = get_current_time();
202 int x = event->x;
203 int y = event->y;
204 bool double_click = false;
206 /* handle wheel scrolling to adjust volume */
207 if (config.mousewheel) {
208 if (event->button == config.wheel_button_up) {
209 mixer_set_volume_rel(config.scrollstep);
210 if (!osd_mapped())
211 map_osd();
212 if (osd_mapped())
213 update_osd(mixer_get_volume(), false);
214 ui_update();
215 return;
217 if (event->button == config.wheel_button_down) {
218 mixer_set_volume_rel(-config.scrollstep);
219 if (!osd_mapped())
220 map_osd();
221 if (osd_mapped())
222 update_osd(mixer_get_volume(), false);
223 ui_update();
224 return;
228 if ((button_press_time - prev_button_press_time) <= 0.5) {
229 double_click = true;
230 prev_button_press_time = 0.0;
231 } else
232 prev_button_press_time = button_press_time;
234 switch (check_region(x, y)) {
235 case 1: /* on knob */
236 button_pressed = true;
237 slider_pressed = false;
238 mouse_drag_home_x = x;
239 mouse_drag_home_y = y;
240 if (double_click) {
241 mixer_toggle_mute();
242 ui_update();
244 break;
245 case 2: /* on slider */
246 button_pressed = false;
247 slider_pressed = true;
248 mouse_drag_home_x = x;
249 mouse_drag_home_y = y;
250 if (double_click) {
251 mixer_set_balance(0.0);
252 ui_update();
254 break;
255 case 3: /* previous channel */
256 mixer_set_channel_rel(-1);
257 blit_string(config.scrolltext ? mixer_get_channel_name() : mixer_get_short_name());
258 scroll_text(3, 4, 57, true);
259 unmap_osd();
260 map_osd();
261 ui_update();
262 break;
263 case 4: /* next channel */
264 mixer_set_channel_rel(1);
265 blit_string(config.scrolltext ? mixer_get_channel_name() : mixer_get_short_name());
266 scroll_text(3, 4, 57, true);
267 unmap_osd();
268 map_osd();
269 ui_update();
270 break;
271 case 5: /* toggle mute */
272 mixer_toggle_mute();
273 ui_update();
274 break;
275 case 6: /* toggle rec */
276 mixer_toggle_rec();
277 ui_update();
278 break;
279 case 10:
280 scroll_text(3, 4, 57, true);
281 break;
282 default:
283 printf("unknown region pressed\n");
284 break;
288 static int key_press_event(XKeyEvent *event)
290 if (event->keycode == mmkeys.raise_volume) {
291 mixer_set_volume_rel(config.scrollstep);
292 if (!osd_mapped())
293 map_osd();
294 if (osd_mapped())
295 update_osd(mixer_get_volume(), false);
296 ui_update();
297 return 1;
299 if (event->keycode == mmkeys.lower_volume) {
300 mixer_set_volume_rel(-config.scrollstep);
301 if (!osd_mapped())
302 map_osd();
303 if (osd_mapped())
304 update_osd(mixer_get_volume(), false);
305 ui_update();
306 return 1;
308 if (event->keycode == mmkeys.mute) {
309 mixer_toggle_mute();
310 ui_update();
311 return 1;
314 /* Ignore other keys */
315 return 0;
318 static void button_release_event(XButtonEvent *event)
320 int x = event->x;
321 int y = event->y;
322 int region;
324 region = check_region(x, y);
326 if (region == 1)
327 set_cursor(HAND_CURSOR);
329 button_pressed = false;
330 slider_pressed = false;
333 static void motion_event(XMotionEvent *event)
335 int x = event->x;
336 int y = event->y;
337 int region;
339 if ((x == mouse_drag_home_x) && (y == mouse_drag_home_y))
340 return;
342 region = check_region(x, y);
344 if (button_pressed) {
345 if (y != mouse_drag_home_y) {
346 float delta;
348 set_cursor(NULL_CURSOR);
350 delta = (float)(mouse_drag_home_y - y) / display_height;
351 knob_turn(delta);
352 if (!osd_mapped())
353 map_osd();
354 if (osd_mapped())
355 update_osd(mixer_get_volume(), false);
357 XWarpPointer(display, None, event->window, x, y, 0, 0,
358 mouse_drag_home_x, mouse_drag_home_y);
359 return;
362 if (slider_pressed) {
363 if (x != mouse_drag_home_x) {
364 float delta;
366 set_cursor(NULL_CURSOR);
368 delta = (float)(x - mouse_drag_home_x) / display_width;
369 slider_move(delta);
371 XWarpPointer(display, None, event->window, x, y, 0, 0,
372 mouse_drag_home_x, mouse_drag_home_y);
373 return;
376 if (region == 1)
377 set_cursor(HAND_CURSOR);
378 else if (region == 2)
379 set_cursor(BAR_CURSOR);
380 else
381 set_cursor(NORMAL_CURSOR);