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);
98 mmkey_install(display
);
102 blit_string("wmix " VERSION
);
103 scroll_text(3, 4, 57, true);
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 */
117 signal(SIGUSR1
, (void *) signal_catch
);
118 signal(SIGUSR2
, (void *) signal_catch
);
121 if (button_pressed
|| slider_pressed
|| (XPending(display
) > 0)) {
122 XNextEvent(display
, &event
);
123 switch (event
.type
) {
125 if (key_press_event(&event
.xkey
))
132 button_press_event(&event
.xbutton
);
136 button_release_event(&event
.xbutton
);
140 /* process cursor change, or drag events */
141 motion_event(&event
.xmotion
);
145 /* go back to standard cursor */
146 if ((!button_pressed
) && (!slider_pressed
))
147 set_cursor(NORMAL_CURSOR
);
150 XCloseDisplay(display
);
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);
163 /* get rid of OSD after a few seconds of idle */
164 if ((idle_loop
> 15) && osd_mapped() && !button_pressed
) {
168 if (mixer_is_changed())
175 static void signal_catch(int sig
)
179 mixer_set_volume_rel(config
.scrollstep
);
183 update_osd(mixer_get_volume(), false);
188 mixer_set_volume_rel(-config
.scrollstep
);
192 update_osd(mixer_get_volume(), false);
199 static void button_press_event(XButtonEvent
*event
)
201 double button_press_time
= get_current_time();
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
);
213 update_osd(mixer_get_volume(), false);
217 if (event
->button
== config
.wheel_button_down
) {
218 mixer_set_volume_rel(-config
.scrollstep
);
222 update_osd(mixer_get_volume(), false);
228 if ((button_press_time
- prev_button_press_time
) <= 0.5) {
230 prev_button_press_time
= 0.0;
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
;
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
;
251 mixer_set_balance(0.0);
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);
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);
271 case 5: /* toggle mute */
275 case 6: /* toggle rec */
280 scroll_text(3, 4, 57, true);
283 printf("unknown region pressed\n");
288 static int key_press_event(XKeyEvent
*event
)
290 if (event
->keycode
== mmkeys
.raise_volume
) {
291 mixer_set_volume_rel(config
.scrollstep
);
295 update_osd(mixer_get_volume(), false);
299 if (event
->keycode
== mmkeys
.lower_volume
) {
300 mixer_set_volume_rel(-config
.scrollstep
);
304 update_osd(mixer_get_volume(), false);
308 if (event
->keycode
== mmkeys
.mute
) {
314 /* Ignore other keys */
318 static void button_release_event(XButtonEvent
*event
)
324 region
= check_region(x
, y
);
327 set_cursor(HAND_CURSOR
);
329 button_pressed
= false;
330 slider_pressed
= false;
333 static void motion_event(XMotionEvent
*event
)
339 if ((x
== mouse_drag_home_x
) && (y
== mouse_drag_home_y
))
342 region
= check_region(x
, y
);
344 if (button_pressed
) {
345 if (y
!= mouse_drag_home_y
) {
348 set_cursor(NULL_CURSOR
);
350 delta
= (float)(mouse_drag_home_y
- y
) / display_height
;
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
);
362 if (slider_pressed
) {
363 if (x
!= mouse_drag_home_x
) {
366 set_cursor(NULL_CURSOR
);
368 delta
= (float)(x
- mouse_drag_home_x
) / display_width
;
371 XWarpPointer(display
, None
, event
->window
, x
, y
, 0, 0,
372 mouse_drag_home_x
, mouse_drag_home_y
);
377 set_cursor(HAND_CURSOR
);
378 else if (region
== 2)
379 set_cursor(BAR_CURSOR
);
381 set_cursor(NORMAL_CURSOR
);