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.
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
;
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
)
68 parse_cli_options(argc
, argv
);
71 mixer_init(config
.mixer_device
, config
.verbose
, (const char **)config
.exclude_channel
);
74 display
= XOpenDisplay(config
.display_name
);
75 if (display
== NULL
) {
78 if (config
.display_name
) {
79 name
= config
.display_name
;
81 name
= getenv("DISPLAY");
83 fprintf(stderr
, "wmix:error: Unable to open display, variable $DISPLAY not set\n");
87 fprintf(stderr
, "wmix:error: Unable to open display \"%s\"\n", name
);
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);
96 mmkey_install(display
);
100 blit_string("wmix 3.0");
101 scroll_text(3, 4, 57, true);
104 /* add click regions */
105 add_region(1, 37, 36, 25, 25); /* knob */
106 add_region(2, 4, 42, 27, 15); /* balancer */
107 add_region(3, 2, 26, 7, 10); /* previous channel */
108 add_region(4, 10, 26, 7, 10); /* next channel */
109 add_region(5, 39, 14, 20, 7); /* mute toggle */
110 add_region(6, 4, 14, 13, 7); /* rec toggle */
111 add_region(10, 3, 4, 56, 7); /* re-scroll current channel name */
113 /* setup up/down signal handler */
115 signal(SIGUSR1
, (void *) signal_catch
);
116 signal(SIGUSR2
, (void *) signal_catch
);
119 if (button_pressed
|| slider_pressed
|| (XPending(display
) > 0)) {
120 XNextEvent(display
, &event
);
121 switch (event
.type
) {
123 if (key_press_event(&event
.xkey
))
130 button_press_event(&event
.xbutton
);
134 button_release_event(&event
.xbutton
);
138 /* process cursor change, or drag events */
139 motion_event(&event
.xmotion
);
143 /* go back to standard cursor */
144 if ((!button_pressed
) && (!slider_pressed
))
145 set_cursor(NORMAL_CURSOR
);
148 XCloseDisplay(display
);
155 scroll_text(3, 4, 57, false);
156 /* rescroll message after some delay */
157 if (idle_loop
++ > 256) {
158 scroll_text(3, 4, 57, true);
161 /* get rid of OSD after a few seconds of idle */
162 if ((idle_loop
> 15) && osd_mapped() && !button_pressed
) {
166 if (mixer_is_changed())
173 static void signal_catch(int sig
)
177 mixer_set_volume_rel(config
.scrollstep
);
181 update_osd(mixer_get_volume(), false);
186 mixer_set_volume_rel(-config
.scrollstep
);
190 update_osd(mixer_get_volume(), false);
197 static void button_press_event(XButtonEvent
*event
)
199 double button_press_time
= get_current_time();
202 bool double_click
= false;
204 /* handle wheel scrolling to adjust volume */
205 if (config
.mousewheel
) {
206 if (event
->button
== config
.wheel_button_up
) {
207 mixer_set_volume_rel(config
.scrollstep
);
211 update_osd(mixer_get_volume(), false);
215 if (event
->button
== config
.wheel_button_down
) {
216 mixer_set_volume_rel(-config
.scrollstep
);
220 update_osd(mixer_get_volume(), false);
226 if ((button_press_time
- prev_button_press_time
) <= 0.5) {
228 prev_button_press_time
= 0.0;
230 prev_button_press_time
= button_press_time
;
232 switch (check_region(x
, y
)) {
233 case 1: /* on knob */
234 button_pressed
= true;
235 slider_pressed
= false;
236 mouse_drag_home_x
= x
;
237 mouse_drag_home_y
= y
;
243 case 2: /* on slider */
244 button_pressed
= false;
245 slider_pressed
= true;
246 mouse_drag_home_x
= x
;
247 mouse_drag_home_y
= y
;
249 mixer_set_balance(0.0);
253 case 3: /* previous channel */
254 mixer_set_channel_rel(-1);
255 blit_string(config
.scrolltext
? mixer_get_channel_name() : mixer_get_short_name());
256 scroll_text(3, 4, 57, true);
261 case 4: /* next channel */
262 mixer_set_channel_rel(1);
263 blit_string(config
.scrolltext
? mixer_get_channel_name() : mixer_get_short_name());
264 scroll_text(3, 4, 57, true);
269 case 5: /* toggle mute */
273 case 6: /* toggle rec */
278 scroll_text(3, 4, 57, true);
281 printf("unknown region pressed\n");
286 static int key_press_event(XKeyEvent
*event
)
288 if (event
->keycode
== mmkeys
.raise_volume
) {
289 mixer_set_volume_rel(config
.scrollstep
);
293 update_osd(mixer_get_volume(), false);
297 if (event
->keycode
== mmkeys
.lower_volume
) {
298 mixer_set_volume_rel(-config
.scrollstep
);
302 update_osd(mixer_get_volume(), false);
306 if (event
->keycode
== mmkeys
.mute
) {
312 /* Ignore other keys */
316 static void button_release_event(XButtonEvent
*event
)
322 region
= check_region(x
, y
);
325 set_cursor(HAND_CURSOR
);
327 button_pressed
= false;
328 slider_pressed
= false;
331 static void motion_event(XMotionEvent
*event
)
337 if ((x
== mouse_drag_home_x
) && (y
== mouse_drag_home_y
))
340 region
= check_region(x
, y
);
342 if (button_pressed
) {
343 if (y
!= mouse_drag_home_y
) {
346 set_cursor(NULL_CURSOR
);
348 delta
= (float)(mouse_drag_home_y
- y
) / display_height
;
353 update_osd(mixer_get_volume(), false);
355 XWarpPointer(display
, None
, event
->window
, x
, y
, 0, 0,
356 mouse_drag_home_x
, mouse_drag_home_y
);
360 if (slider_pressed
) {
361 if (x
!= mouse_drag_home_x
) {
364 set_cursor(NULL_CURSOR
);
366 delta
= (float)(x
- mouse_drag_home_x
) / display_width
;
369 XWarpPointer(display
, None
, event
->window
, x
, y
, 0, 0,
370 mouse_drag_home_x
, mouse_drag_home_y
);
375 set_cursor(HAND_CURSOR
);
376 else if (region
== 2)
377 set_cursor(BAR_CURSOR
);
379 set_cursor(NORMAL_CURSOR
);