]> git.decadent.org.uk Git - ion3.git/blob - ioncore/grab.c
c65f2887eb1b602cc413b220060f4a41973798e9
[ion3.git] / ioncore / grab.c
1 /*
2  * ion/ioncore/grab.c
3  * 
4  * Copyright (c) Lukas Schroeder 2002,
5  *               Tuomo Valkonen 2003-2008.
6  *
7  * See the included file LICENSE for details.
8  * 
9  * Alternatively, you may apply the Clarified Artistic License to this file,
10  * since Lukas' contributions were originally under that.
11  */
12
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <sys/time.h>
16
17 #define XK_MISCELLANY
18 #include <X11/keysymdef.h>
19
20 #include "common.h"
21 #include "global.h"
22 #include "event.h"
23 #include "cursor.h"
24 #include "grab.h"
25
26
27 /*{{{ Definitions */
28
29
30 typedef struct _grab_status{
31     WRegion *holder;
32     GrabHandler *handler;
33     GrabKilledHandler *killedhandler;
34     Watch watch;
35     long eventmask;
36     long flags;
37     
38     bool remove;    /* TRUE, if entry marked for removal by do_grab_remove() */
39     int cursor;
40     Window confine_to;
41     int sqid;
42 }GrabStatus;
43
44 #define MAX_GRABS 4
45 static GrabStatus grabs[MAX_GRABS];
46 static GrabStatus *current_grab;
47 static int idx_grab=0;
48 static int last_sqid=0;
49
50
51 /*}}}*/
52
53
54 /*{{{ do_grab/ungrab */
55
56
57 static void grab_kb_ptr(Window win, Window confine_to, int cursor, 
58                         long eventmask)
59 {
60     ioncore_g.input_mode=IONCORE_INPUTMODE_GRAB;
61     
62     XSelectInput(ioncore_g.dpy, win, IONCORE_EVENTMASK_ROOT&~eventmask);
63     XGrabPointer(ioncore_g.dpy, win, True, IONCORE_EVENTMASK_PTRGRAB,
64                  GrabModeAsync, GrabModeAsync, confine_to,
65                  ioncore_xcursor(cursor), CurrentTime);
66     XGrabKeyboard(ioncore_g.dpy, win, False, GrabModeAsync,
67                   GrabModeAsync, CurrentTime);
68     XSync(ioncore_g.dpy, False);
69     XSelectInput(ioncore_g.dpy, win, IONCORE_EVENTMASK_ROOT);
70 }
71
72
73 static void ungrab_kb_ptr()
74 {
75     XUngrabKeyboard(ioncore_g.dpy, CurrentTime);
76     XUngrabPointer(ioncore_g.dpy, CurrentTime);
77     
78     ioncore_g.input_mode=IONCORE_INPUTMODE_NORMAL;
79 }
80
81
82 /*}}}*/
83
84
85 /*{{{ Functions for installing grabs */
86
87
88 static void do_holder_remove(WRegion *holder, bool killed);
89
90
91 static void grab_watch_handler(Watch *w, Obj *obj)
92 {
93     do_holder_remove((WRegion*)obj, TRUE);
94 }
95
96
97 static void do_grab_install(GrabStatus *grab)
98 {
99     watch_setup(&grab->watch, (Obj*)grab->holder, grab_watch_handler);
100     grab_kb_ptr(region_root_of(grab->holder), grab->confine_to, 
101                 grab->cursor, grab->eventmask);
102     current_grab=grab;
103 }
104
105
106 void ioncore_grab_establish(WRegion *reg, GrabHandler *func, 
107                             GrabKilledHandler *kh,
108                             long eventmask)
109 {
110     assert((~eventmask)&(KeyPressMask|KeyReleaseMask));
111     
112     if(idx_grab<MAX_GRABS){
113         current_grab=&grabs[idx_grab++];
114         current_grab->holder=reg;
115         current_grab->handler=func;
116         current_grab->killedhandler=kh;
117         current_grab->eventmask=eventmask;
118         current_grab->remove=FALSE;
119         current_grab->cursor=IONCORE_CURSOR_DEFAULT;
120         current_grab->confine_to=None; /*region_root_of(reg);*/
121         current_grab->sqid=last_sqid++;
122         watch_init(&current_grab->watch);
123         do_grab_install(current_grab);
124     }
125 }
126
127
128 /*}}}*/
129
130
131 /*{{{ Grab removal functions */
132
133
134 static void do_grab_remove()
135 {
136     current_grab=NULL;
137     ungrab_kb_ptr();
138     
139     while(idx_grab>0 && grabs[idx_grab-1].remove==TRUE){
140         watch_reset(&grabs[idx_grab-1].watch);
141         idx_grab--;
142     }
143     
144     assert(idx_grab>=0);
145     
146     if(idx_grab>0){
147         current_grab=&grabs[idx_grab-1];
148         do_grab_install(current_grab);
149     }
150 }
151
152
153 static void mark_for_removal(GrabStatus *grab, bool killed)
154 {
155     if(!grab->remove){
156         grab->remove=TRUE;
157         if(killed && grab->killedhandler!=NULL && grab->holder!=NULL)
158             grab->killedhandler(grab->holder);
159     }
160     
161     if(grabs[idx_grab-1].remove)
162         do_grab_remove();
163 }
164
165
166 static void do_holder_remove(WRegion *holder, bool killed)
167 {
168     int i;
169     
170     for(i=idx_grab-1; i>=0; i--){
171         if(grabs[i].holder==holder)
172             mark_for_removal(grabs+i, killed);
173     }
174 }
175
176
177 void ioncore_grab_holder_remove(WRegion *holder)
178 {
179     do_holder_remove(holder, FALSE);
180 }
181
182
183 void ioncore_grab_remove(GrabHandler *func)
184 {
185     int i;
186     for(i=idx_grab-1; i>=0; i--){
187         if(grabs[i].handler==func){
188             mark_for_removal(grabs+i, FALSE);
189             break;
190         }
191     }
192 }
193
194
195 /*}}}*/
196
197
198 /*{{{ Grab handler calling */
199
200
201 bool ioncore_handle_grabs(XEvent *ev)
202 {
203     GrabStatus *gr;
204     int gr_sqid;
205     
206     while(current_grab && current_grab->remove)
207         do_grab_remove();
208     
209     if(current_grab==NULL || current_grab->holder==NULL ||
210        current_grab->handler==NULL){
211         return FALSE;
212     }
213     
214     /* Escape key is harcoded to always kill active grab. */
215     if(ev->type==KeyPress && XLookupKeysym(&(ev->xkey), 0)==XK_Escape){
216         mark_for_removal(current_grab, TRUE);
217         return TRUE;
218     }
219     
220     if(ev->type!=KeyRelease && ev->type!=KeyPress)
221         return FALSE;
222     
223     /* We must check that the grab pointed to by current_grab still
224      * is the same grab and not already released or replaced by
225      * another grab.
226      */
227     gr=current_grab;
228     gr_sqid=gr->sqid;
229     if(gr->handler(gr->holder, ev) && gr->sqid==gr_sqid)
230         mark_for_removal(gr, FALSE);
231     
232     return TRUE;
233 }
234
235
236 /*}}}*/
237
238
239 /*{{{ Misc. */
240
241
242 bool ioncore_grab_held()
243 {
244     return idx_grab>0;
245 }
246
247
248 void ioncore_change_grab_cursor(int cursor)
249 {
250     if(current_grab!=NULL){
251         current_grab->cursor=cursor;
252         XChangeActivePointerGrab(ioncore_g.dpy, IONCORE_EVENTMASK_PTRGRAB,
253                                  ioncore_xcursor(cursor), CurrentTime);
254     }
255 }
256
257
258 void ioncore_grab_confine_to(Window confine_to)
259 {
260     if(current_grab!=NULL){
261         current_grab->confine_to=confine_to;
262         XGrabPointer(ioncore_g.dpy, region_root_of(current_grab->holder),
263                      True, IONCORE_EVENTMASK_PTRGRAB, GrabModeAsync, 
264                      GrabModeAsync, confine_to, 
265                      ioncore_xcursor(IONCORE_CURSOR_DEFAULT), 
266                      CurrentTime);
267     }
268 }
269
270
271 WRegion *ioncore_grab_get_holder()
272 {
273     if (ioncore_grab_held())
274         return grabs[idx_grab-1].holder;
275     return NULL;
276 }
277
278
279 WRegion *ioncore_grab_get_my_holder(GrabHandler *func)
280 {
281     int i;
282     for(i=idx_grab-1; i>=0; i--)
283         if(grabs[i].handler==func)
284             return grabs[i].holder;
285     return NULL;
286 }
287
288
289 /*}}}*/
290