audiofile support + improved sample rate support + results of a -Wall -Werr
[terminatorX.git] / src / tX_mouse.cc
1 /*
2     terminatorX - realtime audio scratching software
3     Copyright (C) 1999-2002  Alexander K├Ânig
4  
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.
9  
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.
14  
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.
18  
19     File: tX_mouse.cc
20  
21     Description: This implements the mouse AND keyboard Input handling in
22                  Grab-Mode.
23 */
24
25 #include <sys/wait.h>
26 #include <X11/Xlib.h>
27 #include <X11/extensions/xf86dga.h>
28
29 #include "tX_mouse.h"
30 #include "tX_mastergui.h"
31 #include "tX_global.h"
32 #include "tX_engine.h"
33 #include "tX_vtt.h"
34
35 #define TX_MOUSE_SPEED_NORMAL 0.05
36 #define TX_MOUSE_SPEED_WARP 250000
37
38 tx_mouse :: tx_mouse() {
39         mask=PointerMotionMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask;
40         xmot=(XMotionEvent *) &xev;
41         xkey=(XKeyEvent *) &xev;
42         xbut=(XButtonEvent *) &xev;
43         
44 #ifdef USE_DGA2
45         xdgamot=(XDGAMotionEvent *) &xev;
46         xdgakey=(XDGAKeyEvent *) &xev;
47         xdgabut=(XDGAButtonEvent *) &xev;
48 #endif  
49         
50         grabbed=0;
51         warp=TX_MOUSE_SPEED_NORMAL;
52 }
53
54
55 int tx_mouse :: grab() {        
56 #ifdef USE_DGA2
57         XDGAMode *mode;
58 #endif  
59
60         if (grabbed) return(0);
61
62         dpy=XOpenDisplay(NULL);
63         if (!dpy)
64         {
65                 fputs("GrabMode Error: couldn't connect to XDisplay.", stderr);
66                 return(ENG_ERR_XOPEN);
67         }
68
69
70 #ifdef USE_DGA2
71         mode=XDGAQueryModes(dpy,DefaultScreen(dpy), &num);
72         
73         printf("Found %i DGA2-Modes:\n", num);
74         
75         for(i=0; i<num; i++)
76         {
77                 printf("%2i: %s\n", i, mode[i].name);
78         }
79         XFree(mode);
80 #endif  
81                                 
82         XSelectInput(dpy, xwindow, mask);       
83
84         XSetInputFocus(dpy, xwindow, None, CurrentTime);
85
86         if (globals.xinput_enable)
87         {
88                 if (set_xinput())
89                 {
90                         XCloseDisplay(dpy);
91                         fputs("GrabMode Error: failed to setup XInput.", stderr);
92                         return(ENG_ERR_XINPUT);
93                 }
94         }
95
96         if (GrabSuccess != XGrabPointer(dpy, xwindow, False, ButtonPressMask|ButtonReleaseMask|PointerMotionMask, GrabModeAsync,GrabModeAsync,None,None,CurrentTime))
97         {
98                 reset_xinput();
99                 XCloseDisplay(dpy);
100                 fputs("GrabMode Error: XGrabPointer failed.", stderr);
101                 return(ENG_ERR_GRABMOUSE);
102         }       
103         
104         if (GrabSuccess != XGrabKeyboard(dpy, xwindow, False, GrabModeAsync,GrabModeAsync,CurrentTime))
105         {
106                 XUngrabPointer (dpy, CurrentTime);
107                 reset_xinput();         
108                 XCloseDisplay(dpy);
109                 fputs("GrabMode Error: XGrabKeyboard failed.", stderr);
110                 return(ENG_ERR_GRABKEY);
111         }
112         
113
114 #ifdef USE_DGA2
115         if (!XDGASetMode(dpy, DefaultScreen(dpy), 1))
116 #else   
117         if (!XF86DGADirectVideo(dpy,DefaultScreen(dpy),XF86DGADirectMouse))
118 #endif
119         {
120                 XUngrabKeyboard(dpy, CurrentTime);                              
121                 XUngrabPointer (dpy, CurrentTime);
122                 reset_xinput();         
123                 XCloseDisplay(dpy);
124                 fputs("GrabMode Error: Failed to enable XF86DGA.", stderr);             
125                 return(ENG_ERR_DGA);
126         }
127
128 #ifdef USE_DGA2
129         XDGASelectInput(dpy, DefaultScreen(dpy), mask);
130 #endif  
131
132         XAutoRepeatOff(dpy);    
133         otime=CurrentTime;
134         
135         grabbed=1;
136         vtt_class::focus_no(0);
137         warp=TX_MOUSE_SPEED_NORMAL;
138         
139         return(0);
140 }
141
142 void tx_mouse :: ungrab()
143 {
144         if (!grabbed) return;
145         
146 #ifdef USE_DGA2 
147         XDGASetMode(dpy, DefaultScreen(dpy), 0);
148 #else
149         XF86DGADirectVideo(dpy,DefaultScreen(dpy),0);
150 #endif  
151
152         XUngrabKeyboard(dpy, CurrentTime);              
153         XUngrabPointer (dpy, CurrentTime);
154         XAutoRepeatOn(dpy);
155         
156         XCloseDisplay(dpy);
157
158         if (globals.xinput_enable)
159         {
160                 reset_xinput(); 
161         }
162
163         vtt_class::unfocus();
164
165         grabbed=0;
166 }
167
168 #ifdef USE_XSETPOINTER
169
170 void tx_mouse :: set_x_pointer(char *devname)
171 {
172         pid_t pid;
173         int status;
174                 
175         pid = fork();
176         
177         if (pid==-1) 
178         { 
179                 /* OOPS. fork failed */
180                 perror("tX: Error: Couldn't fork process!");
181                 return; 
182         }
183         
184         if (pid==0) 
185         {
186                 /* The child execlps xsetpointer */
187                 execlp("xsetpointer", "xsetpointer", devname, NULL);
188                 perror("tX: Error: Failed to execute xpointer!");
189                 exit(0);
190         }
191         
192         /* parent waits for xsetpointer to finish */
193         waitpid(pid,  &status, WUNTRACED);
194 }
195
196 #else
197
198 void tx_mouse :: set_x_pointer(char *devname)
199 {
200         XDeviceInfo *devlist;                   
201         XDevice *device;
202         int listmax, i;
203         
204         devlist=XListInputDevices(dpy, &listmax);
205         
206         for (i=0; i<listmax; i++)
207         {
208                 if(strcmp(devlist[i].name, devname)==0)
209                 {
210                         device=XOpenDevice(dpy, devlist[i].id);
211                         if (device)
212                         {
213                                 if (XChangePointerDevice(dpy, device, 0, 1))
214                                 {
215                                         printf("tX: Error: failed to set pointer device.");                     
216                                 }
217                                 XCloseDevice(dpy, device);
218                         }
219                         else
220                         {
221                                 printf("tX: Error: failed to open XInput device.");
222                         }               
223                 }
224         }
225                 
226         XFreeDeviceList(devlist);               
227 }
228
229 #endif
230
231 int tx_mouse :: set_xinput()
232 {
233         XDeviceInfo *devlist;                   
234         int listmax, i;
235         
236         OrgXPointer=0;
237         
238         if (globals.xinput_enable)
239         {       
240                 devlist=XListInputDevices(dpy, &listmax);
241         
242                 for (i=0; i<listmax; i++)
243                 {
244                         if(devlist[i].use == IsXPointer)
245                         {
246                                 OrgXPointer=devlist[i].id;
247                                 strcpy(OrgXPointerName, devlist[i].name);
248                         }
249                 }
250                 
251                 XFreeDeviceList(devlist);               
252                 
253                 set_x_pointer(globals.xinput_device);
254         }
255         
256         if (OrgXPointer==0) printf("tX: Error failed to detect core pointer.");
257         return(0);
258 }
259
260
261 void tx_mouse :: reset_xinput()
262 {
263         if (globals.xinput_enable)
264         {
265                 if (OrgXPointer) set_x_pointer(OrgXPointerName);
266         }
267 }
268
269
270 #define vtt vtt_class::focused_vtt
271
272 int tx_mouse :: check_event()
273 {
274         if (XCheckWindowEvent(dpy, xwindow, mask, &xev))
275         {
276 #ifdef USE_DGA2
277                 puts("Got an event");
278 #endif          
279                 if (vtt)
280                 switch(xev.type)
281                 {
282                         case MotionNotify:
283                                 vtt->xy_input(((f_prec) xmot->x_root)*warp,((f_prec) xmot->y_root)*warp);
284                                 break;
285                         
286                         case ButtonPress:
287                                 switch(xbut->button)
288                                 {
289                                         case 1: if (vtt->is_playing)
290                                                         vtt->set_scratch(1); 
291                                                 else
292                                                         vtt->sp_trigger.receive_input_value(1);
293                                                 break;
294                                         case 2: vtt->sp_mute.receive_input_value(1); break;
295                                         case 3: vtt_class::focus_next(); break;
296                                 }
297                                 break;
298                         
299                         case ButtonRelease:
300                                 switch (xbut->button)
301                                 {       
302                                         case 1: vtt->set_scratch(0); break;
303                                         case 2: vtt->sp_mute.receive_input_value(0); break;
304                                 }
305                                 break;
306                         case KeyPress:
307 #ifdef USE_DGA2
308                                 puts("Yeah its a key");
309                                 XDGAKeyEventToXKeyEvent(xdgakey, (XKeyEvent *) &xev_copy);
310                                 memcpy(&xev, &xev_copy, sizeof(XEvent));
311 #endif                  
312                         {
313                                 key=XKeycodeToKeysym(dpy, xkey->keycode, 0);
314                                 
315                                 switch(key)
316                                 {
317                                         case XK_space: vtt->set_scratch(1); break;
318                                         case XK_Escape: return(1);
319                                         
320                                         case XK_Return: vtt->sp_trigger.receive_input_value(1); break;
321                                         case XK_BackSpace : vtt->sp_trigger.receive_input_value(0); break;
322                                         
323                                         case XK_Tab: vtt_class::focus_next(); break;
324                                         
325                                         case XK_s: vtt->sp_sync_client.receive_input_value(!vtt->is_sync_client); break;
326                                         
327                                         case XK_m:
328                                         case XK_Control_L:
329                                         case XK_Control_R:                                              
330                                         vtt->sp_mute.receive_input_value(1);
331                                         break;
332                                                 
333                                         case XK_Alt_L:
334                                         case XK_Alt_R:
335                                         vtt->sp_mute.receive_input_value(0);
336                                         break;
337                                         
338                                         case XK_w:
339                                         vtt->sp_mute.receive_input_value(1);
340                                         case XK_f: 
341                                         warp=((float) vtt->samples_in_buffer)/TX_MOUSE_SPEED_WARP;      
342                                         vtt->set_scratch(1);
343                                         break;
344                                                 
345                                         case XK_F1: vtt_class::focus_no(0); break;
346                                         case XK_F2: vtt_class::focus_no(1); break;
347                                         case XK_F3: vtt_class::focus_no(2); break;
348                                         case XK_F4: vtt_class::focus_no(3); break;
349                                         case XK_F5: vtt_class::focus_no(4); break;
350                                         case XK_F6: vtt_class::focus_no(5); break;
351                                         case XK_F7: vtt_class::focus_no(6); break;
352                                         case XK_F8: vtt_class::focus_no(7); break;
353                                         case XK_F9: vtt_class::focus_no(8); break;
354                                         case XK_F10: vtt_class::focus_no(9); break;
355                                         case XK_F11: vtt_class::focus_no(10); break;
356                                         case XK_F12: vtt_class::focus_no(11); break;
357                                 }
358                         } break;
359                         
360                         case KeyRelease:
361                         {
362                                 key=XKeycodeToKeysym (dpy, xkey->keycode, 0);
363                                 
364                                 switch(key)
365                                 {
366                                         case XK_space: vtt->set_scratch(0); break;
367                                         
368                                         case XK_m:
369                                         case XK_Control_L:
370                                         case XK_Control_R:                                              
371                                         vtt->sp_mute.receive_input_value(0);
372                                         break;
373                                                 
374                                         case XK_Alt_L:
375                                         case XK_Alt_R:
376                                         vtt->sp_mute.receive_input_value(1);
377                                         break;
378                                         
379                                         case XK_w:
380                                         vtt->sp_mute.receive_input_value(0);
381                                         case XK_f: warp=TX_MOUSE_SPEED_NORMAL;
382                                         vtt->set_scratch(0);
383                                         break;                                  
384                                 }
385                         }
386                 }
387                 else {  puts("no vtt"); return(1); }
388         }
389         return(0);
390 }