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"
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
;
53 static int mouse_drag_home_x
;
54 static int mouse_drag_home_y
;
57 static char *exclude
[SOUND_MIXER_NRDEVICES
];
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
);
67 "WMixer " VERSION " by timecop@japan.co.jp + skunk@mit.edu\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)
79 int count_exclude
= 0 ;
82 while ((opt
= getopt(argc
, argv
, "d:f:hm:ve:")) != EOF
) {
86 display_name
= strdup(optarg
);
90 mixer_device
= strdup(optarg
);
94 if (config
.file
!= NULL
)
96 config
.file
= strdup(optarg
);
99 fputs(HELP_TEXT
, stdout
);
106 if (count_exclude
< SOUND_MIXER_NRDEVICES
) {
107 exclude
[count_exclude
] = strdup(optarg
);
108 /* printf("exclude : %s\n", exclude[count_exclude]); */
111 fprintf(stderr
, "Warning: You can't exclude this many channels\n");
117 exclude
[count_exclude
] = NULL
;
120 int main(int argc
, char **argv
)
125 memset(&config
, 0, sizeof(config
));
127 /* we can theoretically live without a config file */
128 home
= getenv("HOME");
130 config
.file
= calloc(1, strlen(home
) + 9);
131 sprintf(config
.file
, "%s/.wmixrc", home
);
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;
141 config
.osd_color
= strdup("green");
143 parse_cli_options(argc
, argv
);
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
) {
159 name
= getenv("DISPLAY");
161 fprintf(stderr
, "wmix:error: Unable to open display, variable $DISPLAY not set\n");
165 fprintf(stderr
, "wmix:error: Unable to open display \"%s\"\n", name
);
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);
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 */
189 signal(SIGUSR1
, (void *) signal_catch
);
190 signal(SIGUSR2
, (void *) signal_catch
);
193 if (button_pressed
|| slider_pressed
|| (XPending(display
) > 0)) {
194 XNextEvent(display
, &event
);
195 switch (event
.type
) {
200 button_press_event(&event
.xbutton
);
204 button_release_event(&event
.xbutton
);
208 /* process cursor change, or drag events */
209 motion_event(&event
.xmotion
);
213 /* go back to standard cursor */
214 if ((!button_pressed
) && (!slider_pressed
))
215 set_cursor(NORMAL_CURSOR
);
218 XCloseDisplay(display
);
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);
231 /* get rid of OSD after a few seconds of idle */
232 if ((idle_loop
> 15) && osd_mapped() && !button_pressed
) {
236 if (mixer_is_changed())
243 static void signal_catch(int sig
)
247 mixer_set_volume_rel(config
.scrollstep
);
251 update_osd(mixer_get_volume(), false);
256 mixer_set_volume_rel(-config
.scrollstep
);
260 update_osd(mixer_get_volume(), false);
267 static void button_press_event(XButtonEvent
*event
)
269 double button_press_time
= get_current_time();
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
);
281 update_osd(mixer_get_volume(), false);
285 if (event
->button
== config
.wheel_button_down
) {
286 mixer_set_volume_rel(-config
.scrollstep
);
290 update_osd(mixer_get_volume(), false);
296 if ((button_press_time
- prev_button_press_time
) <= 0.5) {
298 prev_button_press_time
= 0.0;
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
;
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
;
319 mixer_set_balance(0.0);
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);
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);
339 case 5: /* toggle mute */
343 case 6: /* toggle rec */
348 scroll_text(3, 4, 57, true);
351 printf("unknown region pressed\n");
356 static void button_release_event(XButtonEvent
*event
)
362 region
= check_region(x
, y
);
365 set_cursor(HAND_CURSOR
);
367 button_pressed
= false;
368 slider_pressed
= false;
371 static void motion_event(XMotionEvent
*event
)
377 if ((x
== mouse_drag_home_x
) && (y
== mouse_drag_home_y
))
380 region
= check_region(x
, y
);
382 if (button_pressed
) {
383 if (y
!= mouse_drag_home_y
) {
386 set_cursor(NULL_CURSOR
);
388 delta
= (float)(mouse_drag_home_y
- y
) / display_height
;
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
);
400 if (slider_pressed
) {
401 if (x
!= mouse_drag_home_x
) {
404 set_cursor(NULL_CURSOR
);
406 delta
= (float)(x
- mouse_drag_home_x
) / display_width
;
409 XWarpPointer(display
, None
, event
->window
, x
, y
, 0, 0,
410 mouse_drag_home_x
, mouse_drag_home_y
);
415 set_cursor(HAND_CURSOR
);
416 else if (region
== 2)
417 set_cursor(BAR_CURSOR
);
419 set_cursor(NORMAL_CURSOR
);