fixed wav-write, xfree-4-input-hack and adrian's solo patch - alex
[terminatorX.git] / src / tX_mouse.cc
1 /*
2     terminatorX - realtime audio scratching software
3     Copyright (C) 1999, 2000  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
26 #include "tX_mouse.h"
27 #include "tX_mastergui.h"
28 #include "tX_global.h"
29 #include "tX_engine.h"
30 #include "tX_vtt.h"
31
32 #define TX_MOUSE_SPEED_NORMAL 0.05
33 #define TX_MOUSE_SPEED_WARP 250000
34
35 tx_mouse :: tx_mouse()
36 {
37         mask=PointerMotionMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask;
38         xmot=(XMotionEvent *) &xev;
39         xkey=(XKeyEvent *) &xev;
40         xbut=(XButtonEvent *) &xev;
41         
42 #ifdef USE_DGA2
43         xdgamot=(XDGAMotionEvent *) &xev;
44         xdgakey=(XDGAKeyEvent *) &xev;
45         xdgabut=(XDGAButtonEvent *) &xev;
46 #endif  
47         
48         grabbed=0;
49         warp=TX_MOUSE_SPEED_NORMAL;
50 }
51
52
53 int tx_mouse :: grab()
54 {       
55 #ifdef USE_DGA2
56         XDGAMode *mode;
57 #endif  
58         int i, num=0;
59
60         if (grabbed) return(0);
61
62         dpy=XOpenDisplay(NULL); // FIXME: use correct display
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 void tx_mouse :: set_x_pointer(char *devname)
169 {
170         char commandline[256];
171         
172         sprintf(commandline, "xsetpointer %s", devname);
173         system(commandline);
174         printf("ran: %s\n", commandline);
175 }
176
177 int tx_mouse :: set_xinput()
178 {
179         XDeviceInfo *devlist;                   
180         int listmax, i;
181         
182         if (globals.xinput_enable)
183         {       
184                 devlist=XListInputDevices(dpy, &listmax);
185         
186                 for (i=0; i<listmax; i++)
187                 {
188                         if(devlist[i].use == IsXPointer)
189                         {
190                                 OrgXPointer=devlist[i].id;
191                                 strcpy(OrgXPointerName, devlist[i].name);
192                         }
193                 }
194                 
195                 XFreeDeviceList(devlist);               
196                 
197                 set_x_pointer(globals.xinput_device);
198         }
199         return(0);
200 }
201
202
203 void tx_mouse :: reset_xinput()
204 {
205         if (globals.xinput_enable)
206         {
207                 set_x_pointer(OrgXPointerName);
208         }
209 }
210
211
212 #define vtt vtt_class::focused_vtt
213
214 int tx_mouse :: check_event()
215 {
216         if (XCheckWindowEvent(dpy, xwindow, mask, &xev))
217         {
218 #ifdef USE_DGA2
219                 puts("Got an event");
220 #endif          
221                 if (vtt)
222                 switch(xev.type)
223                 {
224                         case MotionNotify:
225                                 vtt->xy_input(((f_prec) xmot->x_root)*warp,((f_prec) xmot->y_root)*warp);
226                                 break;
227                         
228                         case ButtonPress:
229                                 switch(xbut->button)
230                                 {
231                                         case 1: if (vtt->is_playing)
232                                                         vtt->set_scratch(1); 
233                                                 else
234                                                         vtt->sp_trigger.receive_input_value(1);
235                                                 break;
236                                         case 2: vtt->sp_mute.receive_input_value(1); break;
237                                         case 3: vtt_class::focus_next(); break;
238                                 }
239                                 break;
240                         
241                         case ButtonRelease:
242                                 switch (xbut->button)
243                                 {       
244                                         case 1: vtt->set_scratch(0); break;
245                                         case 2: vtt->sp_mute.receive_input_value(0); break;
246                                 }
247                                 break;
248                         case KeyPress:
249 #ifdef USE_DGA2
250                                 puts("Yeah its a key");
251                                 XDGAKeyEventToXKeyEvent(xdgakey, (XKeyEvent *) &xev_copy);
252                                 memcpy(&xev, &xev_copy, sizeof(XEvent));
253 #endif                  
254                         {
255                                 key=XKeycodeToKeysym(dpy, xkey->keycode, 0);
256                                 
257                                 switch(key)
258                                 {
259                                         case XK_space: vtt->set_scratch(1); break;
260                                         case XK_Escape: return(1);
261                                         
262                                         case XK_Return: vtt->sp_trigger.receive_input_value(1); break;
263                                         case XK_BackSpace : vtt->sp_trigger.receive_input_value(0); break;
264                                         
265                                         case XK_Tab: vtt_class::focus_next(); break;
266                                         
267                                         case XK_s: vtt->sp_sync_client.receive_input_value(!vtt->is_sync_client); break;
268                                         
269                                         case XK_m:
270                                         case XK_Control_L:
271                                         case XK_Control_R:                                              
272                                         vtt->sp_mute.receive_input_value(1);
273                                         break;
274                                                 
275                                         case XK_Alt_L:
276                                         case XK_Alt_R:
277                                         vtt->sp_mute.receive_input_value(0);
278                                         break;
279                                         
280                                         case XK_w:
281                                         vtt->sp_mute.receive_input_value(1);
282                                         case XK_f: 
283                                         warp=((float) vtt->samples_in_buffer)/TX_MOUSE_SPEED_WARP;      
284                                         vtt->set_scratch(1);
285                                         break;
286                                                 
287                                         case XK_F1: vtt_class::focus_no(0); break;
288                                         case XK_F2: vtt_class::focus_no(1); break;
289                                         case XK_F3: vtt_class::focus_no(2); break;
290                                         case XK_F4: vtt_class::focus_no(3); break;
291                                         case XK_F5: vtt_class::focus_no(4); break;
292                                         case XK_F6: vtt_class::focus_no(5); break;
293                                         case XK_F7: vtt_class::focus_no(6); break;
294                                         case XK_F8: vtt_class::focus_no(7); break;
295                                         case XK_F9: vtt_class::focus_no(8); break;
296                                         case XK_F10: vtt_class::focus_no(9); break;
297                                         case XK_F11: vtt_class::focus_no(10); break;
298                                         case XK_F12: vtt_class::focus_no(11); break;
299                                 }
300                         } break;
301                         
302                         case KeyRelease:
303                         {
304                                 key=XKeycodeToKeysym (dpy, xkey->keycode, 0);
305                                 
306                                 switch(key)
307                                 {
308                                         case XK_space: vtt->set_scratch(0); break;
309                                         
310                                         case XK_m:
311                                         case XK_Control_L:
312                                         case XK_Control_R:                                              
313                                         vtt->sp_mute.receive_input_value(0);
314                                         break;
315                                                 
316                                         case XK_Alt_L:
317                                         case XK_Alt_R:
318                                         vtt->sp_mute.receive_input_value(1);
319                                         break;
320                                         
321                                         case XK_w:
322                                         vtt->sp_mute.receive_input_value(0);
323                                         case XK_f: warp=TX_MOUSE_SPEED_NORMAL;
324                                         vtt->set_scratch(0);
325                                         break;                                  
326                                 }
327                         }
328                 }
329                 else {  puts("no vtt"); return(1); }
330         }
331         return(0);
332 }