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
)
127 memset(&config
, 0, sizeof(config
));
129 /* we can theoretically live without a config file */
130 home
= getenv("HOME");
132 config
.file
= calloc(1, strlen(home
) + 9);
133 sprintf(config
.file
, "%s/.wmixrc", home
);
136 /* handle writing PID file, silently ignore if we can't do it */
137 pid
= calloc(1, strlen(home
) + 10);
138 sprintf(pid
, "%s/.wmix.pid", home
);
139 fp
= fopen(pid
, "w");
141 fprintf(fp
, "%d\n", getpid());
147 config
.mousewheel
= 1;
148 config
.scrolltext
= 1;
149 config
.wheel_button_up
= 4;
150 config
.wheel_button_down
= 5;
151 config
.scrollstep
= 0.03;
153 config
.osd_color
= strdup("green");
155 parse_cli_options(argc
, argv
);
158 if (mixer_device
== NULL
)
159 mixer_device
= "/dev/mixer";
161 mixer_init(mixer_device
, verbose
, (const char **)exclude
);
162 mixer_set_channel(0);
164 if ((display
= XOpenDisplay(display_name
)) == NULL
) {
165 fprintf(stderr
, "Unable to open display \"%s\"\n", display_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 */
188 signal(SIGUSR1
, (void *) signal_catch
);
189 signal(SIGUSR2
, (void *) signal_catch
);
192 if (button_pressed
|| slider_pressed
|| (XPending(display
) > 0)) {
193 XNextEvent(display
, &event
);
194 switch (event
.type
) {
199 button_press_event(&event
.xbutton
);
203 button_release_event(&event
.xbutton
);
207 /* process cursor change, or drag events */
208 motion_event(&event
.xmotion
);
212 /* go back to standard cursor */
213 if ((!button_pressed
) && (!slider_pressed
))
214 set_cursor(NORMAL_CURSOR
);
217 XCloseDisplay(display
);
224 scroll_text(3, 4, 57, false);
225 /* rescroll message after some delay */
226 if (idle_loop
++ > 256) {
227 scroll_text(3, 4, 57, true);
230 /* get rid of OSD after a few seconds of idle */
231 if ((idle_loop
> 15) && osd_mapped() && !button_pressed
) {
235 if (mixer_is_changed())
242 static void signal_catch(int sig
)
246 mixer_set_volume_rel(config
.scrollstep
);
250 update_osd(mixer_get_volume(), false);
255 mixer_set_volume_rel(-config
.scrollstep
);
259 update_osd(mixer_get_volume(), false);
266 static void button_press_event(XButtonEvent
*event
)
268 double button_press_time
= get_current_time();
271 bool double_click
= false;
273 /* handle wheel scrolling to adjust volume */
274 if (config
.mousewheel
) {
275 if (event
->button
== config
.wheel_button_up
) {
276 mixer_set_volume_rel(config
.scrollstep
);
280 update_osd(mixer_get_volume(), false);
284 if (event
->button
== config
.wheel_button_down
) {
285 mixer_set_volume_rel(-config
.scrollstep
);
289 update_osd(mixer_get_volume(), false);
295 if ((button_press_time
- prev_button_press_time
) <= 0.5) {
297 prev_button_press_time
= 0.0;
299 prev_button_press_time
= button_press_time
;
301 switch (check_region(x
, y
)) {
302 case 1: /* on knob */
303 button_pressed
= true;
304 slider_pressed
= false;
305 mouse_drag_home_x
= x
;
306 mouse_drag_home_y
= y
;
312 case 2: /* on slider */
313 button_pressed
= false;
314 slider_pressed
= true;
315 mouse_drag_home_x
= x
;
316 mouse_drag_home_y
= y
;
318 mixer_set_balance(0.0);
322 case 3: /* previous channel */
323 mixer_set_channel_rel(-1);
324 blit_string(config
.scrolltext
? mixer_get_channel_name() : mixer_get_short_name());
325 scroll_text(3, 4, 57, true);
330 case 4: /* next channel */
331 mixer_set_channel_rel(1);
332 blit_string(config
.scrolltext
? mixer_get_channel_name() : mixer_get_short_name());
333 scroll_text(3, 4, 57, true);
338 case 5: /* toggle mute */
342 case 6: /* toggle rec */
347 scroll_text(3, 4, 57, true);
350 printf("unknown region pressed\n");
355 static void button_release_event(XButtonEvent
*event
)
361 region
= check_region(x
, y
);
364 set_cursor(HAND_CURSOR
);
366 button_pressed
= false;
367 slider_pressed
= false;
370 static void motion_event(XMotionEvent
*event
)
376 if ((x
== mouse_drag_home_x
) && (y
== mouse_drag_home_y
))
379 region
= check_region(x
, y
);
381 if (button_pressed
) {
382 if (y
!= mouse_drag_home_y
) {
385 set_cursor(NULL_CURSOR
);
387 delta
= (float)(mouse_drag_home_y
- y
) / display_height
;
392 update_osd(mixer_get_volume(), false);
394 XWarpPointer(display
, None
, event
->window
, x
, y
, 0, 0,
395 mouse_drag_home_x
, mouse_drag_home_y
);
399 if (slider_pressed
) {
400 if (x
!= mouse_drag_home_x
) {
403 set_cursor(NULL_CURSOR
);
405 delta
= (float)(x
- mouse_drag_home_x
) / display_width
;
408 XWarpPointer(display
, None
, event
->window
, x
, y
, 0, 0,
409 mouse_drag_home_x
, mouse_drag_home_y
);
414 set_cursor(HAND_CURSOR
);
415 else if (region
== 2)
416 set_cursor(BAR_CURSOR
);
418 set_cursor(NORMAL_CURSOR
);