Released version 3-2015061300
[notion.git] / ioncore / key.c
blob3db5a800c51b136f1717e1fc4a08f94bceb93dfe
1 /*
2 * ion/ioncore/key.c
4 * Copyright (c) Tuomo Valkonen 1999-2009.
6 * See the included file LICENSE for details.
7 */
9 #include <ctype.h>
11 #include <libtu/objp.h>
12 #include <libextl/extl.h>
13 #include <libmainloop/defer.h>
15 #include "common.h"
16 #include "key.h"
17 #include "binding.h"
18 #include "global.h"
19 #include "event.h"
20 #include "cursor.h"
21 #include "grab.h"
22 #include "regbind.h"
23 #include "strings.h"
24 #include "xwindow.h"
27 static void waitrelease(WRegion *reg);
28 static void submapgrab(WRegion *reg);
31 static void insstr(WWindow *wwin, XKeyEvent *ev)
33 static XComposeStatus cs={NULL, 0};
34 char buf[32]={0,};
35 Status stat;
36 int n;
37 KeySym ksym;
39 if(wwin->xic!=NULL){
40 if(XFilterEvent((XEvent*)ev, ev->window))
41 return;
42 n=XmbLookupString(wwin->xic, ev, buf, 16, &ksym, &stat);
43 if(stat!=XLookupChars && stat!=XLookupBoth)
44 return;
45 }else{
46 n=XLookupString(ev, buf, 32, &ksym, &cs);
49 if(n<=0)
50 return;
52 /* Won't catch bad strings, but should filter out most crap. */
53 if(ioncore_g.use_mb){
54 if(!iswprint(str_wchar_at(buf, 32)))
55 return;
56 }else{
57 if(iscntrl(*buf))
58 return;
61 window_insstr(wwin, buf, n);
65 static void send_key(XEvent *ev, WClientWin *cwin)
67 Window win=cwin->win;
68 ev->xkey.window=win;
69 ev->xkey.subwindow=None;
70 XSendEvent(ioncore_g.dpy, win, False, KeyPressMask, ev);
74 static bool quote_next_handler(WRegion *reg, XEvent *xev)
76 XKeyEvent *ev=&xev->xkey;
77 if(ev->type!=KeyPress)
78 return FALSE;
79 if(ioncore_ismod(ev->keycode))
80 return FALSE;
81 assert(OBJ_IS(reg, WClientWin));
82 send_key(xev, (WClientWin*)reg);
83 return TRUE; /* remove the grab */
87 /*EXTL_DOC
88 * Send next key press directly to \var{cwin}.
90 EXTL_EXPORT_MEMBER
91 void clientwin_quote_next(WClientWin *cwin)
93 ioncore_grab_establish((WRegion*)cwin, quote_next_handler, NULL, 0);
94 ioncore_change_grab_cursor(IONCORE_CURSOR_WAITKEY);
98 static bool waitrelease_handler(WRegion *UNUSED(reg), XEvent *ev)
100 return (ioncore_unmod(ev->xkey.state, ev->xkey.keycode)==0);
104 static void waitrelease(WRegion *reg)
106 if(ioncore_modstate()==0)
107 return;
109 /* We need to grab on the root window as <reg> might have been
110 * ioncore_defer_destroy:ed by the binding handler (the most common case
111 * for using this kpress_wait!). In such a case the grab may
112 * be removed before the modifiers are released.
114 ioncore_grab_establish((WRegion*)region_rootwin_of(reg),
115 waitrelease_handler,
116 NULL, 0);
117 ioncore_change_grab_cursor(IONCORE_CURSOR_WAITKEY);
121 static void free_sub(WSubmapState *p)
123 /*extl_unref_fn(p->leave);
124 watch_reset(&p->leave_reg);
127 free(p);
131 void region_free_submapstat(WRegion *reg)
133 while(reg->submapstat!=NULL){
134 WSubmapState *p=reg->submapstat;
135 reg->submapstat=p->next;
136 free_sub(p);
141 WHook *ioncore_submap_ungrab_hook=NULL;
144 static void call_submap_ungrab_hook()
146 hook_call_v(ioncore_submap_ungrab_hook);
150 static void clear_subs(WRegion *reg)
152 region_free_submapstat(reg);
153 mainloop_defer_action(NULL, (WDeferredAction*)call_submap_ungrab_hook);
155 while(reg!=NULL && reg->submapstat!=NULL){
156 WSubmapState *p=reg->submapstat;
157 reg->submapstat=p->next;
159 if(p->leave!=extl_fn_none() && p->leave_reg.obj!=NULL){
160 Watch regw=WATCH_INIT;
162 watch_setup(&regw, (Obj*)reg, NULL);
164 extl_call(p->leave, "o", NULL, p->leave_reg.obj);
166 reg=(WRegion*)regw.obj;
168 watch_reset(&regw);
171 free_sub(p);
177 static WSubmapState *add_sub(WRegion *reg, uint key, uint state)
179 WSubmapState **p;
180 WSubmapState *s;
182 if(reg->submapstat==NULL){
183 p=&(reg->submapstat);
184 }else{
185 s=reg->submapstat;
186 while(s->next!=NULL)
187 s=s->next;
188 p=&(s->next);
191 s=ALLOC(WSubmapState);
193 if(s==NULL)
194 return NULL;
196 s->key=key;
197 s->state=state;
198 /*s->leave=extl_fn_none();
199 watch_init(&s->leave_reg);*/
201 *p=s;
203 return s;
208 static uint current_kcb, current_state;
209 static bool current_submap;
211 /* Note: state set to AnyModifier for submaps */
212 bool ioncore_current_key(uint *kcb, uint *state, bool *sub)
214 if(current_kcb==0)
215 return FALSE;
217 *kcb=current_kcb;
218 *state=current_state;
219 *sub=current_submap;
221 return TRUE;
225 enum{GRAB_NONE, GRAB_NONE_SUBMAP, GRAB_SUBMAP, GRAB_WAITRELEASE};
228 static WBinding *lookup_binding_(WRegion *reg,
229 int act, uint state, uint kcb,
230 WSubmapState *st,
231 WRegion **binding_owner, WRegion **subreg)
233 WBinding *binding;
235 *subreg=NULL;
238 binding=region_lookup_keybinding(reg, act, state, kcb, st,
239 binding_owner);
241 if(binding!=NULL)
242 break;
244 if(OBJ_IS(reg, WRootWin))
245 break;
247 *subreg=reg;
248 reg=REGION_PARENT_REG(reg);
249 }while(reg!=NULL);
251 return binding;
254 static WBinding *lookup_binding(WRegion *oreg,
255 int act, uint state, uint kcb,
256 WRegion **binding_owner, WRegion **subreg)
258 WRegion *reg=oreg;
260 /* Find the deepest nested active window grabbing this key. */
261 while(reg->active_sub!=NULL)
262 reg=reg->active_sub;
264 return lookup_binding_(reg, act, state, kcb, oreg->submapstat,
265 binding_owner, subreg);
269 static void do_call_binding(WBinding *binding, WRegion *reg, WRegion *subreg)
271 WRegion *mgd=region_managed_within(reg, subreg);
273 /* TODO: having to pass both mgd and subreg for some handlers
274 * to work is ugly and complex.
276 extl_call(binding->func, "ooo", NULL, reg, mgd, subreg);
280 static int do_key(WRegion *oreg, XKeyEvent *ev)
282 WBinding *binding=NULL;
283 WRegion *binding_owner=NULL, *subreg=NULL;
284 bool grabbed=(oreg->flags&REGION_BINDINGS_ARE_GRABBED);
285 int ret=GRAB_NONE;
287 if(grabbed){
288 binding=lookup_binding(oreg, BINDING_KEYPRESS, ev->state, ev->keycode,
289 &binding_owner, &subreg);
290 }else{
291 binding=region_lookup_keybinding(oreg, BINDING_KEYPRESS,
292 ev->state, ev->keycode,
293 oreg->submapstat,
294 &binding_owner);
297 if(binding!=NULL){
298 bool subs=(oreg->submapstat!=NULL);
299 WBinding *call=NULL;
301 if(binding->submap!=NULL){
302 WSubmapState *s=add_sub(oreg, ev->keycode, ev->state);
303 if(s!=NULL){
304 /*WRegion *own2, *subreg2;
306 call=lookup_binding(binding_owner, BINDING_SUBMAP_LEAVE, 0, 0,
307 oreg->submapstat, &own2, &subreg2);
309 if(call!=NULL){
310 s->leave=extl_ref_fn(call->func);
311 watch_setup(&s->leave_reg, (Obj*)own2, NULL);
314 call=lookup_binding_(binding_owner, BINDING_SUBMAP_ENTER, 0, 0,
315 oreg->submapstat,
316 &binding_owner, &subreg);
318 ret=(grabbed ? GRAB_SUBMAP : GRAB_NONE_SUBMAP);
320 }else{
321 call=binding;
323 if(grabbed)
324 XUngrabKeyboard(ioncore_g.dpy, CurrentTime);
326 if(ev->state!=0 && !subs && binding->wait)
327 ret=GRAB_WAITRELEASE;
330 if(call!=NULL){
331 current_kcb=ev->keycode;
332 current_state=ev->state;
333 current_submap=subs;
335 do_call_binding(call, binding_owner, subreg);
337 current_kcb=0;
339 }else if(oreg->submapstat==NULL && OBJ_IS(oreg, WWindow)){
340 insstr((WWindow*)oreg, ev);
343 return ret;
347 static bool submapgrab_handler(WRegion* reg, XEvent *xev)
349 XKeyEvent *ev=&xev->xkey;
350 if(ev->type!=KeyPress){
351 if(ioncore_unmod(ev->state, ev->keycode)==0){
352 WBinding *binding;
353 WRegion *binding_owner, *subreg;
355 binding=lookup_binding(reg,
356 BINDING_SUBMAP_RELEASEMOD, 0, 0,
357 &binding_owner, &subreg);
359 if(binding!=NULL)
360 do_call_binding(binding, binding_owner, subreg);
362 return FALSE;
365 if(ioncore_ismod(ev->keycode))
366 return FALSE;
367 if(do_key(reg, ev)!=GRAB_SUBMAP){
368 clear_subs(reg);
369 return TRUE;
370 }else{
371 return FALSE;
378 static void submapgrab(WRegion *reg)
380 ioncore_grab_establish(reg, submapgrab_handler, clear_subs, 0);
381 ioncore_change_grab_cursor(IONCORE_CURSOR_WAITKEY);
385 void ioncore_do_handle_keypress(XKeyEvent *ev)
387 WRegion *reg=(WRegion*)XWINDOW_REGION_OF(ev->window);
389 if(reg!=NULL){
390 Watch w=WATCH_INIT;
391 int grab;
393 /* reg might be destroyed by binding handlers */
394 watch_setup(&w, (Obj*)reg, NULL);
396 grab=do_key(reg, ev);
398 reg=(WRegion*)w.obj;
400 if(reg!=NULL){
401 if(grab==GRAB_SUBMAP)
402 submapgrab(reg);
403 else if(grab==GRAB_WAITRELEASE)
404 waitrelease(reg);
405 else if(grab==GRAB_NONE_SUBMAP)
406 /* nothing */;
407 else if(grab==GRAB_NONE && reg->submapstat!=NULL)
408 clear_subs(reg);
411 watch_reset(&w);