2 terminatorX - realtime audio scratching software
3 Copyright (C) 1999-2003 Alexander König
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 Description: This implements the mouse AND keyboard Input handling in
27 #include <X11/extensions/xf86dga.h>
30 #include "tX_mastergui.h"
31 #include "tX_global.h"
32 #include "tX_engine.h"
35 #define TX_MOUSE_SPEED_NORMAL 0.05
36 #define TX_MOUSE_SPEED_WARP 250000
38 tx_mouse :: tx_mouse() {
39 mask=PointerMotionMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask;
40 xmot=(XMotionEvent *) &xev;
41 xkey=(XKeyEvent *) &xev;
42 xbut=(XButtonEvent *) &xev;
45 xdgamot=(XDGAMotionEvent *) &xev;
46 xdgakey=(XDGAKeyEvent *) &xev;
47 xdgabut=(XDGAButtonEvent *) &xev;
51 warp=TX_MOUSE_SPEED_NORMAL;
55 int tx_mouse :: grab() {
60 if (grabbed) return(0);
62 dpy=XOpenDisplay(NULL);
65 fputs("GrabMode Error: couldn't connect to XDisplay.", stderr);
66 return(ENG_ERR_XOPEN);
71 mode=XDGAQueryModes(dpy,DefaultScreen(dpy), &num);
73 printf("Found %i DGA2-Modes:\n", num);
77 printf("%2i: %s\n", i, mode[i].name);
82 XSelectInput(dpy, xwindow, mask);
84 XSetInputFocus(dpy, xwindow, None, CurrentTime);
86 if (globals.xinput_enable)
91 fputs("GrabMode Error: failed to setup XInput.", stderr);
92 return(ENG_ERR_XINPUT);
96 if (GrabSuccess != XGrabPointer(dpy, xwindow, False, ButtonPressMask|ButtonReleaseMask|PointerMotionMask, GrabModeAsync,GrabModeAsync,None,None,CurrentTime))
100 fputs("GrabMode Error: XGrabPointer failed.", stderr);
101 return(ENG_ERR_GRABMOUSE);
104 if (GrabSuccess != XGrabKeyboard(dpy, xwindow, False, GrabModeAsync,GrabModeAsync,CurrentTime))
106 XUngrabPointer (dpy, CurrentTime);
109 fputs("GrabMode Error: XGrabKeyboard failed.", stderr);
110 return(ENG_ERR_GRABKEY);
115 if (!XDGASetMode(dpy, DefaultScreen(dpy), 1))
117 if (!XF86DGADirectVideo(dpy,DefaultScreen(dpy),XF86DGADirectMouse))
120 XUngrabKeyboard(dpy, CurrentTime);
121 XUngrabPointer (dpy, CurrentTime);
124 fputs("GrabMode Error: Failed to enable XF86DGA.", stderr);
129 XDGASelectInput(dpy, DefaultScreen(dpy), mask);
137 std::list<vtt_class *> :: iterator v;
140 for (v=vtt_class::main_list.begin(); v!=vtt_class::main_list.end(); v++) {
141 if (!(*v)->audio_hidden) {
142 vtt_class::focus_no(c);
146 //vtt_class::focus_no(0);
150 warp=TX_MOUSE_SPEED_NORMAL;
152 tX_debug("tX_mouse::grab(): this: %08x, dpy: %08x", (int) this, (int) dpy);
157 void tx_mouse :: ungrab()
159 if (!grabbed) return;
161 tX_debug("tX_mouse::ungrab(): this: %08x, dpy: %08x", (int) this, (int) dpy);
164 XDGASetMode(dpy, DefaultScreen(dpy), 0);
166 XF86DGADirectVideo(dpy,DefaultScreen(dpy),0);
169 XUngrabKeyboard(dpy, CurrentTime);
170 XUngrabPointer (dpy, CurrentTime);
175 if (globals.xinput_enable)
180 vtt_class::unfocus();
182 tX_debug("tX_mouse::ungrab(): done");
187 #ifdef USE_XSETPOINTER
189 void tx_mouse :: set_x_pointer(char *devname)
198 /* OOPS. fork failed */
199 perror("tX: Error: Couldn't fork process!");
205 /* The child execlps xsetpointer */
206 execlp("xsetpointer", "xsetpointer", devname, NULL);
207 perror("tX: Error: Failed to execute xpointer!");
211 /* parent waits for xsetpointer to finish */
212 waitpid(pid, &status, WUNTRACED);
217 void tx_mouse :: set_x_pointer(char *devname)
219 XDeviceInfo *devlist;
223 devlist=XListInputDevices(dpy, &listmax);
225 for (i=0; i<listmax; i++)
227 if(strcmp(devlist[i].name, devname)==0)
229 device=XOpenDevice(dpy, devlist[i].id);
232 if (XChangePointerDevice(dpy, device, 0, 1))
234 printf("tX: Error: failed to set pointer device.");
236 XCloseDevice(dpy, device);
240 printf("tX: Error: failed to open XInput device.");
245 XFreeDeviceList(devlist);
250 int tx_mouse :: set_xinput()
252 XDeviceInfo *devlist;
257 if (globals.xinput_enable)
259 devlist=XListInputDevices(dpy, &listmax);
261 for (i=0; i<listmax; i++)
263 if(devlist[i].use == IsXPointer)
265 OrgXPointer=devlist[i].id;
266 strcpy(OrgXPointerName, devlist[i].name);
270 XFreeDeviceList(devlist);
272 set_x_pointer(globals.xinput_device);
275 if (OrgXPointer==0) printf("tX: Error failed to detect core pointer.");
280 void tx_mouse :: reset_xinput()
282 if (globals.xinput_enable)
284 if (OrgXPointer) set_x_pointer(OrgXPointerName);
289 #define vtt vtt_class::focused_vtt
291 int tx_mouse :: check_event()
293 if (XCheckWindowEvent(dpy, xwindow, mask, &xev))
296 puts("Got an event");
302 vtt->xy_input(((f_prec) xmot->x_root)*warp,((f_prec) xmot->y_root)*warp);
308 case 1: if (vtt->is_playing)
311 vtt->sp_trigger.receive_input_value(1);
313 case 2: vtt->sp_mute.receive_input_value(1); break;
314 case 3: vtt_class::focus_next(); break;
319 switch (xbut->button)
321 case 1: vtt->set_scratch(0); break;
322 case 2: vtt->sp_mute.receive_input_value(0); break;
327 puts("Yeah its a key");
328 XDGAKeyEventToXKeyEvent(xdgakey, (XKeyEvent *) &xev_copy);
329 memcpy(&xev, &xev_copy, sizeof(XEvent));
332 key=XKeycodeToKeysym(dpy, xkey->keycode, 0);
336 case XK_space: vtt->set_scratch(1); break;
337 case XK_Escape: return(1);
339 case XK_Return: vtt->sp_trigger.receive_input_value(1); break;
340 case XK_BackSpace : vtt->sp_trigger.receive_input_value(0); break;
342 case XK_Tab: vtt_class::focus_next(); break;
344 case XK_s: vtt->sp_sync_client.receive_input_value(!vtt->is_sync_client); break;
349 vtt->sp_mute.receive_input_value(1);
354 vtt->sp_mute.receive_input_value(0);
358 vtt->sp_mute.receive_input_value(1);
360 warp=((float) vtt->samples_in_buffer)/TX_MOUSE_SPEED_WARP;
364 case XK_F1: vtt_class::focus_no(0); break;
365 case XK_F2: vtt_class::focus_no(1); break;
366 case XK_F3: vtt_class::focus_no(2); break;
367 case XK_F4: vtt_class::focus_no(3); break;
368 case XK_F5: vtt_class::focus_no(4); break;
369 case XK_F6: vtt_class::focus_no(5); break;
370 case XK_F7: vtt_class::focus_no(6); break;
371 case XK_F8: vtt_class::focus_no(7); break;
372 case XK_F9: vtt_class::focus_no(8); break;
373 case XK_F10: vtt_class::focus_no(9); break;
374 case XK_F11: vtt_class::focus_no(10); break;
375 case XK_F12: vtt_class::focus_no(11); break;
381 key=XKeycodeToKeysym (dpy, xkey->keycode, 0);
385 case XK_space: vtt->set_scratch(0); break;
390 vtt->sp_mute.receive_input_value(0);
395 vtt->sp_mute.receive_input_value(1);
399 vtt->sp_mute.receive_input_value(0);
400 case XK_f: warp=TX_MOUSE_SPEED_NORMAL;
406 else { puts("no vtt"); return(1); }