4 * Copyright (c) Lukas Schroeder 2002,
5 * Tuomo Valkonen 2003-2007.
7 * Ion is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
18 #include <X11/keysymdef.h>
30 typedef struct _grab_status{
33 GrabKilledHandler *killedhandler;
38 bool remove; /* TRUE, if entry marked for removal by do_grab_remove() */
45 static GrabStatus grabs[MAX_GRABS];
46 static GrabStatus *current_grab;
47 static int idx_grab=0;
48 static int last_sqid=0;
54 /*{{{ do_grab/ungrab */
57 static void grab_kb_ptr(Window win, Window confine_to, int cursor,
60 ioncore_g.input_mode=IONCORE_INPUTMODE_GRAB;
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);
73 static void ungrab_kb_ptr()
75 XUngrabKeyboard(ioncore_g.dpy, CurrentTime);
76 XUngrabPointer(ioncore_g.dpy, CurrentTime);
78 ioncore_g.input_mode=IONCORE_INPUTMODE_NORMAL;
85 /*{{{ Functions for installing grabs */
88 static void do_holder_remove(WRegion *holder, bool killed);
91 static void grab_watch_handler(Watch *w, Obj *obj)
93 do_holder_remove((WRegion*)obj, TRUE);
97 static void do_grab_install(GrabStatus *grab)
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);
106 void ioncore_grab_establish(WRegion *reg, GrabHandler *func,
107 GrabKilledHandler *kh,
110 assert((~eventmask)&(KeyPressMask|KeyReleaseMask));
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 do_grab_install(current_grab);
130 /*{{{ Grab removal functions */
133 static void do_grab_remove()
138 while(idx_grab>0 && grabs[idx_grab-1].remove==TRUE){
139 watch_reset(&grabs[idx_grab-1].watch);
146 current_grab=&grabs[idx_grab-1];
147 do_grab_install(current_grab);
152 static void mark_for_removal(GrabStatus *grab, bool killed)
156 if(killed && grab->killedhandler!=NULL && grab->holder!=NULL)
157 grab->killedhandler(grab->holder);
160 if(grabs[idx_grab-1].remove)
165 static void do_holder_remove(WRegion *holder, bool killed)
169 for(i=idx_grab-1; i>=0; i--){
170 if(grabs[i].holder==holder)
171 mark_for_removal(grabs+i, killed);
176 void ioncore_grab_holder_remove(WRegion *holder)
178 do_holder_remove(holder, FALSE);
182 void ioncore_grab_remove(GrabHandler *func)
185 for(i=idx_grab-1; i>=0; i--){
186 if(grabs[i].handler==func){
187 mark_for_removal(grabs+i, FALSE);
197 /*{{{ Grab handler calling */
200 bool ioncore_handle_grabs(XEvent *ev)
205 while(current_grab && current_grab->remove)
208 if(current_grab==NULL || current_grab->holder==NULL ||
209 current_grab->handler==NULL){
213 /* Escape key is harcoded to always kill active grab. */
214 if(ev->type==KeyPress && XLookupKeysym(&(ev->xkey), 0)==XK_Escape){
215 mark_for_removal(current_grab, TRUE);
219 if(ev->type!=KeyRelease && ev->type!=KeyPress)
222 /* We must check that the grab pointed to by current_grab still
223 * is the same grab and not already released or replaced by
228 if(gr->handler(gr->holder, ev) && gr->sqid==gr_sqid)
229 mark_for_removal(gr, FALSE);
241 bool ioncore_grab_held()
247 void ioncore_change_grab_cursor(int cursor)
249 if(current_grab!=NULL){
250 current_grab->cursor=cursor;
251 XChangeActivePointerGrab(ioncore_g.dpy, IONCORE_EVENTMASK_PTRGRAB,
252 ioncore_xcursor(cursor), CurrentTime);
257 void ioncore_grab_confine_to(Window confine_to)
259 if(current_grab!=NULL){
260 current_grab->confine_to=confine_to;
261 XGrabPointer(ioncore_g.dpy, region_root_of(current_grab->holder),
262 True, IONCORE_EVENTMASK_PTRGRAB, GrabModeAsync,
263 GrabModeAsync, confine_to,
264 ioncore_xcursor(IONCORE_CURSOR_DEFAULT),
270 WRegion *ioncore_grab_get_holder()
272 if (ioncore_grab_held())
273 return grabs[idx_grab-1].holder;
278 WRegion *ioncore_grab_get_my_holder(GrabHandler *func)
281 for(i=idx_grab-1; i>=0; i--)
282 if(grabs[i].handler==func)
283 return grabs[i].holder;