-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathkey.c
309 lines (242 loc) · 6.97 KB
/
key.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
* ion/ioncore/key.c
*
* Copyright (c) Tuomo Valkonen 1999-2007.
*
* See the included file LICENSE for details.
*/
#include <ctype.h>
#include <libtu/objp.h>
#include "common.h"
#include "key.h"
#include "binding.h"
#include "global.h"
#include "event.h"
#include "cursor.h"
#include "grab.h"
#include "regbind.h"
#include <libextl/extl.h>
#include "strings.h"
#include "xwindow.h"
static void waitrelease(WRegion *reg);
static void submapgrab(WRegion *reg);
static void insstr(WWindow *wwin, XKeyEvent *ev)
{
static XComposeStatus cs={NULL, 0};
char buf[32]={0,};
Status stat;
int n;
KeySym ksym;
if(wwin->xic!=NULL){
if(XFilterEvent((XEvent*)ev, ev->window))
return;
n=XmbLookupString(wwin->xic, ev, buf, 16, &ksym, &stat);
if(stat!=XLookupChars && stat!=XLookupBoth)
return;
}else{
n=XLookupString(ev, buf, 32, &ksym, &cs);
}
if(n<=0)
return;
/* Won't catch bad strings, but should filter out most crap. */
if(ioncore_g.use_mb){
if(!iswprint(str_wchar_at(buf, 32)))
return;
}else{
if(iscntrl(*buf))
return;
}
window_insstr(wwin, buf, n);
}
static void send_key(XEvent *ev, WClientWin *cwin)
{
Window win=cwin->win;
ev->xkey.window=win;
ev->xkey.subwindow=None;
XSendEvent(ioncore_g.dpy, win, False, KeyPressMask, ev);
}
static bool quote_next_handler(WRegion *reg, XEvent *xev)
{
XKeyEvent *ev=&xev->xkey;
if(ev->type!=KeyPress)
return FALSE;
if(ioncore_ismod(ev->keycode))
return FALSE;
assert(OBJ_IS(reg, WClientWin));
send_key(xev, (WClientWin*)reg);
return TRUE; /* remove the grab */
}
/*EXTL_DOC
* Send next key press directly to \var{cwin}.
*/
EXTL_EXPORT_MEMBER
void clientwin_quote_next(WClientWin *cwin)
{
ioncore_grab_establish((WRegion*)cwin,
quote_next_handler, NULL,
0, GRAB_DEFAULT_FLAGS);
ioncore_change_grab_cursor(IONCORE_CURSOR_WAITKEY);
}
static bool waitrelease_handler(WRegion *UNUSED(reg), XEvent *ev)
{
if(!ioncore_unmod(ev->xkey.state, ev->xkey.keycode))
return TRUE;
return FALSE;
}
static void waitrelease(WRegion *reg)
{
if(ioncore_modstate()==0)
return;
/* We need to grab on the root window as <reg> might have been
* ioncore_defer_destroy:ed by the binding handler (the most common case
* for using this kpress_wait!). In such a case the grab may
* be removed before the modifiers are released.
*/
ioncore_grab_establish((WRegion*)region_rootwin_of(reg),
waitrelease_handler, NULL,
0, GRAB_DEFAULT_FLAGS);
ioncore_change_grab_cursor(IONCORE_CURSOR_WAITKEY);
}
static void free_subs(WSubmapState *p)
{
WSubmapState *next;
while(p!=NULL){
next=p->next;
free(p);
p=next;
}
}
static void clear_subs(WRegion *reg)
{
while(reg->submapstat!=NULL){
WSubmapState *tmp=reg->submapstat;
reg->submapstat=tmp->next;
free(tmp);
}
}
static bool add_sub(WRegion *reg, uint key, uint state)
{
WSubmapState **p;
WSubmapState *s;
if(reg->submapstat==NULL){
p=&(reg->submapstat);
}else{
s=reg->submapstat;
while(s->next!=NULL)
s=s->next;
p=&(s->next);
}
s=ALLOC(WSubmapState);
if(s==NULL)
return FALSE;
s->key=key;
s->state=state;
*p=s;
return TRUE;
}
static uint current_kcb, current_state;
static bool current_submap;
/* Note: state set to AnyModifier for submaps */
bool ioncore_current_key(uint *kcb, uint *state, bool *sub)
{
if(current_kcb==0)
return FALSE;
*kcb=current_kcb;
*state=current_state;
*sub=current_submap;
return TRUE;
}
static bool submap_defined(WRegion *reg, XKeyEvent *ev)
{
WRegion *oreg=reg;
/* Find the deepest nested active window grabbing this key. */
while(reg->active_sub!=NULL)
reg=reg->active_sub;
do{
WRegion *binding_owner=NULL;
WBinding *binding=region_lookup_submap(reg, ev, oreg->submapstat,
&binding_owner);
if(binding!=NULL && binding->submap!=NULL)
return TRUE;
reg=REGION_PARENT_REG(reg);
}while(reg!=NULL);
return FALSE;
}
/* Return value TRUE = grab needed */
static bool do_key(WRegion *reg, XKeyEvent *ev)
{
WBinding *binding=NULL;
WRegion *oreg=NULL, *binding_owner=NULL, *subreg=NULL;
bool grabbed;
bool grab_needed = FALSE;
bool has_submap = FALSE;
oreg=reg;
grabbed=(oreg->flags®ION_BINDINGS_ARE_GRABBED);
if(grabbed){
/* Find the deepest nested active window grabbing this key. */
while(reg->active_sub!=NULL)
reg=reg->active_sub;
do{
binding=region_lookup_keybinding(reg, ev, oreg->submapstat,
&binding_owner);
if(binding!=NULL && binding->func!=extl_fn_none())
break;
if(OBJ_IS(reg, WRootWin))
break;
subreg=reg;
reg=REGION_PARENT_REG(reg);
}while(reg!=NULL);
}else{
binding=region_lookup_keybinding(oreg, ev, oreg->submapstat,
&binding_owner);
}
has_submap = submap_defined(oreg, ev);
if(has_submap && add_sub(oreg, ev->keycode, ev->state))
grab_needed = TRUE;
else
clear_subs(oreg);
if(binding!=NULL){
if(binding_owner!=NULL && binding->func!=extl_fn_none()){
WRegion *mgd=region_managed_within(binding_owner, subreg);
bool subs=(oreg->submapstat!=NULL);
if(grabbed)
XUngrabKeyboard(ioncore_g.dpy, CurrentTime);
current_kcb=ev->keycode;
current_state=ev->state;
current_submap=oreg->submapstat!=NULL;
/* TODO: having to pass both mgd and subreg for some handlers
* to work is ugly and complex.
*/
extl_call(binding->func, "ooo", NULL, binding_owner, mgd, subreg);
current_kcb=0;
if(ev->state!=0 && !subs && binding->wait)
waitrelease(oreg);
}
}else if(OBJ_IS(oreg, WWindow)){
insstr((WWindow*)oreg, ev);
}
return grab_needed;
}
static bool submapgrab_handler(WRegion* reg, XEvent *xev)
{
XKeyEvent *ev=&xev->xkey;
if(ev->type!=KeyPress)
return FALSE;
if(ioncore_ismod(ev->keycode))
return FALSE;
return !do_key(reg, ev);
}
static void submapgrab(WRegion *reg)
{
ioncore_grab_establish(reg, submapgrab_handler, clear_subs,
0, GRAB_DEFAULT_FLAGS);
ioncore_change_grab_cursor(IONCORE_CURSOR_WAITKEY);
}
void ioncore_do_handle_keypress(XKeyEvent *ev)
{
WRegion *reg=(WRegion*)XWINDOW_REGION_OF(ev->window);
if(reg!=NULL){
if(do_key(reg, ev))
submapgrab(reg);
}
}