Alex: Added the new sources for terminatorX 3.5
[terminatorX.git] / src / tX_mouse.cc
1 /*
2     terminatorX - realtime audio scratching software
3     Copyright (C) 1999  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 "tX_mouse.h"
26 #include "tX_mastergui.h"
27 #include "tX_global.h"
28 #include "tX_engine.h"
29 #include "tX_vtt.h"
30
31 tx_mouse :: tx_mouse()
32 {
33         mask=PointerMotionMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask;
34         xmot=(XMotionEvent *) &xev;
35         xkey=(XKeyEvent *) &xev;
36         xbut=(XButtonEvent *) &xev;
37         grabbed=0;
38 }
39
40 int tx_mouse :: grab()
41 {       
42         if (grabbed) return(0);
43
44         dpy=XOpenDisplay(NULL); // FIXME: use correct display
45         if (!dpy)
46         {
47                 return(ENG_ERR_XOPEN);
48         }
49
50         if (globals.xinput_enable)
51         {
52                 if (set_xinput())
53                 {
54                         XCloseDisplay(dpy);
55                         return(ENG_ERR_XINPUT);
56                 }
57         }
58                                 
59         XSelectInput(dpy, xwindow, mask);       
60
61         XSetInputFocus(dpy, xwindow, None, CurrentTime);
62
63         if (GrabSuccess != XGrabPointer(dpy, xwindow, False, ButtonPressMask|ButtonReleaseMask|PointerMotionMask, GrabModeAsync,GrabModeAsync,None,None,CurrentTime))
64         {
65                 reset_xinput();
66                 XCloseDisplay(dpy);
67                 return(ENG_ERR_GRABMOUSE);
68         }       
69         
70         if (GrabSuccess != XGrabKeyboard(dpy, xwindow, False, GrabModeAsync,GrabModeAsync,CurrentTime))
71         {
72                 XUngrabPointer (dpy, CurrentTime);
73                 reset_xinput();         
74                 XCloseDisplay(dpy);
75
76                 return(ENG_ERR_GRABKEY);
77         }
78         
79         
80         if (!XF86DGADirectVideo(dpy,DefaultScreen(dpy),XF86DGADirectMouse))
81         {
82                 XUngrabKeyboard(dpy, CurrentTime);                              
83                 XUngrabPointer (dpy, CurrentTime);
84                 reset_xinput();         
85                 XCloseDisplay(dpy);
86                 return(ENG_ERR_DGA);
87         }
88
89         XAutoRepeatOff(dpy);    
90         otime=CurrentTime;
91         
92         grabbed=1;
93         vtt_class::focus_no(0);
94         
95         return(0);
96 }
97
98 void tx_mouse :: ungrab()
99 {
100         if (!grabbed) return;
101
102         XF86DGADirectVideo(dpy,DefaultScreen(dpy),0);
103
104         XUngrabKeyboard(dpy, CurrentTime);              
105         XUngrabPointer (dpy, CurrentTime);
106         XAutoRepeatOn(dpy);
107
108         reset_xinput(); 
109         
110         XCloseDisplay(dpy);
111
112         vtt_class::unfocus();
113
114         grabbed=0;
115 }
116
117
118
119 int tx_mouse :: set_xinput()
120 {
121         XDeviceInfo *devlist;                   
122         int listmax, i;
123         int match=-1;
124         
125         if (globals.xinput_enable)
126         {       
127                 devlist=XListInputDevices(dpy, &listmax);
128         
129                 for (i=0; i<listmax; i++)
130                 {
131                         if(!strcmp(globals.xinput_device,devlist[i].name))
132                         {
133                                 match=i;
134                         }
135                 
136                         if(devlist[i].use == IsXPointer)
137                         {
138                                 OrgXPointer=devlist[i].id;
139                         }
140                 }
141                 
142                 if (match>=0)
143                 {
144                         input_device=NULL;
145                         input_device=XOpenDevice(dpy,devlist[match].id);
146 /*                      if (XChangePointerDevice(dpy,input_device, 0, 1)!=Success)
147                         {
148                                 match=-1;
149                         }*/
150                         XCloseDevice(dpy, input_device);
151                 }
152                 
153                 XFreeDeviceList(devlist);               
154         
155                 if (match>=0) return(0); 
156                 else return(1);
157         }
158         
159         return(0);
160 }
161
162 #define vtt vtt_class::focused_vtt
163
164 int tx_mouse :: check_event()
165 {
166         if (XCheckWindowEvent(dpy, xwindow, mask, &xev))
167         {
168                 if (vtt)
169                 switch(xev.type)
170                 {
171                         case MotionNotify:
172                                 ntime=xmot->time;
173                                 dtime=(f_prec) ntime-otime;
174                                 if (dtime<=0) dtime=1.0;
175                                 otime=ntime;
176                                 
177                                 vtt->xy_input(((f_prec) xmot->x_root)/dtime,((f_prec) xmot->y_root)/dtime);
178                                 break;
179                         
180                         case ButtonPress:
181                                 switch(xbut->button)
182                                 {
183                                         case 1: if (vtt->is_playing)
184                                                         vtt->set_scratch(1); 
185                                                 else
186                                                         vtt->trigger();
187                                                 break;
188                                         case 2: vtt->set_mute(1); break;
189                                         case 3: vtt_class::focus_next(); break;
190                                 }
191                                 break;
192                         
193                         case ButtonRelease:
194                                 switch (xbut->button)
195                                 {       
196                                         case 1: vtt->set_scratch(0); break;
197                                         case 2: vtt->set_mute(0); break;
198                                 }
199                                 break;
200                         
201                         case KeyPress:
202                         {
203                                 key=XKeycodeToKeysym(dpy, xkey->keycode, 0);
204                                 
205                                 switch(key)
206                                 {
207                                         case XK_space: vtt->set_scratch(1); break;
208                                         case XK_Escape: return(1);
209                                         
210                                         case XK_Return: vtt->trigger(); break;
211                                         case XK_BackSpace : vtt->stop(); break;
212                                         
213                                         case XK_Tab: vtt_class::focus_next(); break;
214                                         
215                                         case XK_s: vtt->set_sync_client(!vtt->is_sync_client, vtt->sync_cycles); break;
216                                         
217                                         case XK_m:
218                                         case XK_Control_L:
219                                         case XK_Control_R:                                              
220                                         vtt->set_mute(1);
221                                         break;
222                                                 
223                                         case XK_Alt_L:
224                                         case XK_Alt_R:
225                                         vtt->set_mute(0);
226                                         break;
227                                                 
228                                         case XK_F1: vtt_class::focus_no(0); break;
229                                         case XK_F2: vtt_class::focus_no(1); break;
230                                         case XK_F3: vtt_class::focus_no(2); break;
231                                         case XK_F4: vtt_class::focus_no(3); break;
232                                         case XK_F5: vtt_class::focus_no(4); break;
233                                         case XK_F6: vtt_class::focus_no(5); break;
234                                         case XK_F7: vtt_class::focus_no(6); break;
235                                         case XK_F8: vtt_class::focus_no(7); break;
236                                         case XK_F9: vtt_class::focus_no(8); break;
237                                         case XK_F10: vtt_class::focus_no(9); break;
238                                         case XK_F11: vtt_class::focus_no(10); break;
239                                         case XK_F12: vtt_class::focus_no(11); break;
240                                         /*
241                                         case XK_f:
242                                         warp=((float) globals.scratch_size)/50000.0;                                            
243                                         */
244                                 }
245                         } break;
246                         
247                         case KeyRelease:
248                         {
249                                 key=XKeycodeToKeysym (dpy, xkey->keycode, 0);
250                                 
251                                 switch(key)
252                                 {
253                                         case XK_space: vtt->set_scratch(0); break;
254                                         
255                                         case XK_m:
256                                         case XK_Control_L:
257                                         case XK_Control_R:                                              
258                                         vtt->set_mute(0);
259                                         break;
260                                                 
261                                         case XK_Alt_L:
262                                         case XK_Alt_R:
263                                         vtt->set_mute(1);
264                                         break;                                  
265                                 }
266                         }
267                 }
268                 else {  puts("no vtt"); return(1); }
269         }
270         return(0);
271 }
272
273 void tx_mouse :: reset_xinput()
274 {
275         
276 /*      if (globals.xinput_enable)
277         {
278                 input_device=XOpenDevice(dpy, OrgXPointer);
279                 XChangePointerDevice(dpy, input_device, 0, 1);
280                 XCloseDevice(dpy,input_device); 
281         }*/
282 }