]> git.decadent.org.uk Git - ion3.git/blob - ioncore/key.c
[svn-inject] Installing original source of ion3
[ion3.git] / ioncore / key.c
1 /*
2  * ion/ioncore/key.c
3  *
4  * Copyright (c) Tuomo Valkonen 1999-2006. 
5  *
6  * Ion is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or
9  * (at your option) any later version.
10  */
11
12 #include <ctype.h>
13 #include <libtu/objp.h>
14 #include "common.h"
15 #include "key.h"
16 #include "binding.h"
17 #include "global.h"
18 #include "event.h"
19 #include "cursor.h"
20 #include "grab.h"
21 #include "regbind.h"
22 #include <libextl/extl.h>
23 #include "strings.h"
24 #include "xwindow.h"
25
26
27 static void waitrelease(WRegion *reg);
28 static void submapgrab(WRegion *reg);
29
30
31 static void insstr(WWindow *wwin, XKeyEvent *ev)
32 {
33     static XComposeStatus cs={NULL, 0};
34     char buf[32]={0,};
35     Status stat;
36     int n, i;
37     KeySym ksym;
38     
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);
47     }
48     
49     if(n<=0)
50         return;
51     
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;
59     }
60     
61     window_insstr(wwin, buf, n);
62 }
63
64
65 static void send_key(XEvent *ev, WClientWin *cwin)
66 {
67     Window win=cwin->win;
68     ev->xkey.window=win;
69     ev->xkey.subwindow=None;
70     XSendEvent(ioncore_g.dpy, win, False, KeyPressMask, ev);
71 }
72
73
74 static bool quote_next_handler(WRegion *reg, XEvent *xev)
75 {
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 */
84 }
85
86
87 /*EXTL_DOC
88  * Send next key press directly to \var{cwin}.
89  */
90 EXTL_EXPORT_MEMBER
91 void clientwin_quote_next(WClientWin *cwin)
92 {
93     ioncore_grab_establish((WRegion*)cwin, quote_next_handler, NULL, 0);
94     ioncore_change_grab_cursor(IONCORE_CURSOR_WAITKEY);
95 }
96
97
98 static bool waitrelease_handler(WRegion *reg, XEvent *ev)
99 {
100     if(!ioncore_unmod(ev->xkey.state, ev->xkey.keycode))
101         return TRUE;
102     return FALSE;
103 }
104
105
106 static void waitrelease(WRegion *reg)
107 {
108     if(ioncore_modstate()==0)
109         return;
110         
111     /* We need to grab on the root window as <reg> might have been
112      * ioncore_defer_destroy:ed by the binding handler (the most common case
113      * for using this kpress_wait!). In such a case the grab may
114      * be removed before the modifiers are released.
115      */
116     ioncore_grab_establish((WRegion*)region_rootwin_of(reg), 
117                            waitrelease_handler, 
118                            NULL, 0);
119     ioncore_change_grab_cursor(IONCORE_CURSOR_WAITKEY);
120 }
121
122
123 static void free_subs(WSubmapState *p)
124 {
125     WSubmapState *next;
126     
127     while(p!=NULL){
128         next=p->next;
129         free(p);
130         p=next;
131     }
132 }
133
134
135 static void clear_subs(WRegion *reg)
136 {
137     while(reg->submapstat!=NULL){
138         WSubmapState *tmp=reg->submapstat;
139         reg->submapstat=tmp->next;
140         free(tmp);
141     }
142 }
143
144
145 static bool add_sub(WRegion *reg, uint key, uint state)
146 {
147     WSubmapState **p;
148     WSubmapState *s;
149     
150     if(reg->submapstat==NULL){
151         p=&(reg->submapstat);
152     }else{
153         s=reg->submapstat;
154         while(s->next!=NULL)
155             s=s->next;
156         p=&(s->next);
157     }
158     
159     s=ALLOC(WSubmapState);
160     
161     if(s==NULL)
162         return FALSE;
163     
164     s->key=key;
165     s->state=state;
166     
167     *p=s;
168     
169     return TRUE;
170
171 }
172
173
174 static XKeyEvent *current_key_event=NULL;
175
176
177 XKeyEvent *ioncore_current_key_event()
178 {
179     return current_key_event;
180 }
181
182
183 /* Return value TRUE = grab needed */
184 static bool do_key(WRegion *reg, XKeyEvent *ev)
185 {
186     WBinding *binding=NULL;
187     WRegion *oreg=NULL, *binding_owner=NULL, *subreg=NULL;
188     bool grabbed;
189     
190     oreg=reg;
191     grabbed=(oreg->flags&REGION_BINDINGS_ARE_GRABBED);
192     
193     if(grabbed){
194         /* Find the deepest nested active window grabbing this key. */
195         while(reg->active_sub!=NULL)
196             reg=reg->active_sub;
197         
198         do{
199             binding=region_lookup_keybinding(reg, ev, oreg->submapstat, 
200                                              &binding_owner);
201             
202             if(binding!=NULL)
203                 break;
204             if(OBJ_IS(reg, WRootWin))
205                 break;
206             
207             subreg=reg;
208             reg=REGION_PARENT_REG(reg);
209         }while(reg!=NULL);
210     }else{
211         binding=region_lookup_keybinding(oreg, ev, oreg->submapstat, 
212                                          &binding_owner);
213     }
214     
215     if(binding!=NULL){
216         if(binding->submap!=NULL){
217             if(add_sub(oreg, ev->keycode, ev->state))
218                 return grabbed;
219             else
220                 clear_subs(oreg);
221         }else if(binding_owner!=NULL){
222             WRegion *mgd=region_managed_within(binding_owner, subreg);
223             bool subs=(oreg->submapstat!=NULL);
224             
225             clear_subs(oreg);
226             
227             if(grabbed)
228                 XUngrabKeyboard(ioncore_g.dpy, CurrentTime);
229             
230             if(!subs)
231                 current_key_event=ev;
232                 
233             /* TODO: having to pass both mgd and subreg for some handlers
234              * to work is ugly and complex.
235              */
236             extl_call(binding->func, "ooo", NULL, binding_owner, mgd, subreg);
237             
238             current_key_event=NULL;
239             
240             if(ev->state!=0 && !subs && binding->wait)
241                 waitrelease(oreg);
242         }
243     }else if(oreg->submapstat!=NULL){
244         clear_subs(oreg);
245     }else if(OBJ_IS(oreg, WWindow)){
246         insstr((WWindow*)oreg, ev);
247     }
248     
249     return FALSE;
250 }
251
252
253 static bool submapgrab_handler(WRegion* reg, XEvent *xev)
254 {
255     XKeyEvent *ev=&xev->xkey;
256     if(ev->type!=KeyPress)
257         return FALSE;
258     if(ioncore_ismod(ev->keycode))
259         return FALSE;
260     return !do_key(reg, ev);
261 }
262
263
264 static void submapgrab(WRegion *reg)
265 {
266     ioncore_grab_establish(reg, submapgrab_handler, clear_subs, 0);
267     ioncore_change_grab_cursor(IONCORE_CURSOR_WAITKEY);
268 }
269
270
271 void ioncore_do_handle_keypress(XKeyEvent *ev)
272 {
273     WRegion *reg=(WRegion*)XWINDOW_REGION_OF(ev->window);
274     
275     if(reg!=NULL){
276         if(do_key(reg, ev))
277             submapgrab(reg);
278     }
279 }