-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathconf-bindings.c
512 lines (398 loc) · 11.8 KB
/
conf-bindings.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/*
* ion/ioncore/conf-bindings.c
*
* Copyright (c) Tuomo Valkonen 1999-2007.
*
* See the included file LICENSE for details.
*/
#include <string.h>
#define XK_MISCELLANY
#include <X11/keysymdef.h>
#ifdef CF_SUN_F1X_REMAP
#include <X11/Sunkeysym.h>
#endif
#include <libtu/map.h>
#include "common.h"
#include "binding.h"
#include <libextl/readconfig.h>
#include "global.h"
#include <libextl/extl.h>
#include "conf-bindings.h"
#include "bindmaps.h"
/*{{{ parse_keybut */
#define MOD5_NDX 7
static StringIntMap state_map[]={
{"Shift", ShiftMask},
{"Lock", LockMask},
{"Control", ControlMask},
{"Mod1", Mod1Mask},
{"Mod2", Mod2Mask},
{"Mod3", Mod3Mask},
{"Mod4", Mod4Mask},
{"Mod5", Mod5Mask},
{"AnyModifier", AnyModifier},
{"NoModifier", 0},
{NULL, 0},
};
static StringIntMap button_map[]={
{"Button1", Button1},
{"Button2", Button2},
{"Button3", Button3},
{"Button4", Button4},
{"Button5", Button5},
{"Button6", 6},
{"Button7", 7},
{"AnyButton", AnyButton},
{NULL, 0},
};
bool ioncore_parse_keybut(const char *str, uint *mod_ret, uint *ksb_ret,
bool button, bool init_any)
{
char *str2, *p, *p2;
int keysym=NoSymbol, i;
bool ret=FALSE;
*ksb_ret=NoSymbol;
*mod_ret=(init_any && !button ? AnyModifier : 0);
str2=scopy(str);
if(str2==NULL)
return FALSE;
p=str2;
while(*p!='\0'){
p2=strchr(p, '+');
if(p2!=NULL)
*p2='\0';
if(!button){
keysym=XStringToKeysym(p);
#ifdef CF_SUN_F1X_REMAP
if(keysym==XK_F11)
keysym=SunXK_F36;
else if(keysym==XK_F12)
keysym=SunXK_F37;
#endif
}
if(!button && keysym!=NoSymbol){
if(*ksb_ret!=NoSymbol){
warn_obj(str, TR("Multiple key symbols found. If you want to define a key combination, use a modifier and a key. Use xmodmap to see which keys are mapped to which modifiers."));
break;
}
if(XKeysymToKeycode(ioncore_g.dpy, keysym)==0){
warn_obj(str, TR("Could not convert keysym to keycode."));
break;
}
*ksb_ret=keysym;
}else{
i=stringintmap_ndx(state_map, p);
if(i<0){
i=stringintmap_ndx(button_map, p);
if(i<0){
warn(TR("Unknown button \"%s\"."), p);
break;
}
if(!button || *ksb_ret!=NoSymbol){
warn_obj(str, TR("Both a key and a mouse button found. If you want to define a combination of key and button, use a modifier and a button. Use xmodmap to see which keys are mapped to which modifiers."));
break;
}
*ksb_ret=button_map[i].value;
}else{
if(*mod_ret==AnyModifier){
if(!init_any){
warn_obj(str, TR("Insane modifier combination."));
break;
}else{
*mod_ret=state_map[i].value;
}
}else{
if(*mod_ret!=0 && state_map[i].value==AnyModifier){
warn_obj(str, TR("Insane modifier combination."));
break;
}else{
*mod_ret|=state_map[i].value;
}
}
}
}
if(p2==NULL){
ret=TRUE;
break;
}
p=p2+1;
}
free(str2);
return ret;
}
#undef BUTTON1_NDX
/*}}}*/
/*{{{ bindmap_defbindings */
static bool do_action(WBindmap *bindmap, const char *str,
ExtlFn func, uint act, uint mod, uint ksb,
int area, bool wr,
const char* doc, const char* label)
{
WBinding binding;
if(wr && mod==0){
warn(TR("Can not wait on modifiers when no modifiers set in \"%s\". Perhaps you have incorrectly set ALTMETA to the empty string?"),
str);
wr=FALSE;
}
binding.wait=wr;
binding.act=act;
binding.state=mod;
binding.ksb=ksb;
binding.kcb=(act==BINDING_KEYPRESS ? XKeysymToKeycode(ioncore_g.dpy, ksb) : ksb);
binding.area=area;
binding.submap=NULL;
binding.doc=doc?strdup(doc):NULL;
binding.label=label?strdup(label):NULL;
if(func!=extl_fn_none()){
binding.func=extl_ref_fn(func);
if(bindmap_add_binding(bindmap, &binding))
return TRUE;
extl_unref_fn(binding.func);
warn(TR("Unable to add binding %s."), str);
}else{
binding.func=func;
if(bindmap_remove_binding(bindmap, &binding))
return TRUE;
warn(TR("Unable to remove binding %s."), str);
}
return FALSE;
}
static bool do_submap(WBindmap *bindmap, const char *str,
ExtlTab subtab, uint mod, uint ksb)
{
WBinding binding, *bnd;
uint kcb;
kcb=XKeysymToKeycode(ioncore_g.dpy, ksb);
bnd=bindmap_lookup_binding(bindmap, BINDING_SUBMAP, mod, kcb);
if(bnd!=NULL && bnd->submap!=NULL && bnd->state==mod)
return bindmap_defbindings(bnd->submap, subtab, TRUE);
binding.wait=FALSE;
binding.act=BINDING_SUBMAP;
binding.state=mod;
binding.ksb=ksb;
binding.kcb=kcb;
binding.area=0;
binding.func=extl_fn_none();
binding.submap=create_bindmap();
binding.doc=NULL;
binding.label=NULL;
if(binding.submap==NULL)
return FALSE;
if(bindmap_add_binding(bindmap, &binding))
return bindmap_defbindings(binding.submap, subtab, TRUE);
binding_deinit(&binding);
warn(TR("Unable to add submap for binding %s."), str);
return FALSE;
}
static StringIntMap action_map[]={
{"kpress", BINDING_KEYPRESS},
{"mpress", BINDING_BUTTONPRESS},
{"mclick", BINDING_BUTTONCLICK},
{"mdblclick", BINDING_BUTTONDBLCLICK},
{"mdrag", BINDING_BUTTONMOTION},
{"submap", BINDING_SUBMAP},
{NULL, 0}
};
static bool do_entry(WBindmap *bindmap, ExtlTab tab,
const StringIntMap *areamap, bool init_any,
char **doc, char **label)
{
bool ret=FALSE;
char *action_str=NULL, *ksb_str=NULL, *area_str=NULL;
int action=0;
uint ksb=0, mod=0;
ExtlFn func;
bool wr=FALSE;
int area=0;
if(!extl_table_gets_s(tab, "action", &action_str)){
warn(TR("Binding type not set."));
goto fail;
}
if(strcmp(action_str, "kpress_wait")==0){
action=BINDING_KEYPRESS;
wr=TRUE;
}else if(strcmp(action_str, "doc")==0){
extl_table_gets_s(tab, "text", doc);
if (!extl_table_gets_s(tab, "label", label))
*label=NULL;
return TRUE;
}else{
action=stringintmap_value(action_map, action_str, -1);
if(action<0){
warn(TR("Unknown binding type \"%s\"."), action_str);
goto fail;
}
}
if(!extl_table_gets_s(tab, "kcb", &ksb_str))
goto fail;
if(!ioncore_parse_keybut(ksb_str, &mod, &ksb,
(action!=BINDING_KEYPRESS && action!=BINDING_SUBMAP && action!=-1),
init_any)){
goto fail;
}
if(action==BINDING_SUBMAP){
ExtlTab subtab;
extl_table_gets_t(tab, "submap", &subtab);
ret=do_submap(bindmap, ksb_str, subtab, mod, ksb);
extl_unref_table(subtab);
}else{
if(areamap!=NULL){
if(extl_table_gets_s(tab, "area", &area_str)){
area=stringintmap_value(areamap, area_str, -1);
if(area<0){
warn(TR("Unknown area \"%s\" for binding %s."),
area_str, ksb_str);
area=0;
}
}
}
if(!extl_table_gets_f(tab, "func", &func)){
func=extl_fn_none();
}
ret=do_action(bindmap, ksb_str, func, action, mod, ksb, area, wr, *doc, *label);
if(!ret)
extl_unref_fn(func);
}
fail:
if(action_str!=NULL)
free(action_str);
if(ksb_str!=NULL)
free(ksb_str);
if(area_str!=NULL)
free(area_str);
return ret;
}
bool bindmap_defbindings(WBindmap *bindmap, ExtlTab tab, bool submap)
{
int i, n, nok=0;
ExtlTab ent;
char *current_doc = NULL;
char *current_label = NULL;
n=extl_table_get_n(tab);
for(i=1; i<=n; i++){
if(extl_table_geti_t(tab, i, &ent)){
nok+=do_entry(bindmap, ent, bindmap->areamap, submap,
¤t_doc, ¤t_label);
extl_unref_table(ent);
continue;
}
warn(TR("Unable to get bindmap entry %d."), i);
}
return (nok!=0);
}
/*}}}*/
/*{{{ bindmap_getbindings */
static char *get_mods(uint state)
{
char *ret=NULL;
int i;
if(state==AnyModifier){
ret=scopy("AnyModifier+");
}else{
ret=scopy("");
for(i=0; i<=MOD5_NDX; i++){
if(ret==NULL)
break;
if((int)(state&state_map[i].value)==state_map[i].value){
char *ret2=ret;
ret=scat3(ret, state_map[i].string, "+");
free(ret2);
}
}
}
return ret;
}
static char *get_key(char *mods, uint ksb)
{
const char *s=XKeysymToString(ksb);
if(s==NULL){
warn(TR("Unable to convert keysym to string."));
return NULL;
}
return scat(mods, s);
}
static char *get_button(char *mods, uint ksb)
{
const char *s=stringintmap_key(button_map, ksb, NULL);
if(s==NULL){
warn(TR("Unable to convert button to string: [%d]."), ksb);
return NULL;
}
return scat(mods, s);
}
static bool get_kpress(WBindmap *UNUSED(bindmap), WBinding *b, ExtlTab t)
{
char *mods;
char *key;
if(b->wait)
extl_table_sets_s(t, "action", "kpress_wait");
else
extl_table_sets_s(t, "action", "kpress");
mods=get_mods(b->state);
if(mods==NULL)
return FALSE;
key=get_key(mods, b->ksb);
free(mods);
if(key==NULL)
return FALSE;
extl_table_sets_s(t, "kcb", key);
extl_table_sets_s(t, "doc", b->doc);
extl_table_sets_s(t, "label", b->label);
free(key);
if(b->submap!=NULL){
ExtlTab stab=bindmap_getbindings(b->submap);
extl_table_sets_t(t, "submap", stab);
}else{
extl_table_sets_f(t, "func", b->func);
}
return TRUE;
}
static bool get_mact(WBindmap *bindmap, WBinding *b, ExtlTab t)
{
char *mods;
char *button;
extl_table_sets_s(t, "action", stringintmap_key(action_map, b->act, NULL));
mods=get_mods(b->state);
if(mods==NULL)
return FALSE;
button=get_button(mods, b->ksb);
free(mods);
if(button==NULL)
return FALSE;
extl_table_sets_s(t, "kcb", button);
free(button);
if(b->area!=0 && bindmap->areamap!=NULL)
extl_table_sets_s(t, "area",
stringintmap_key(bindmap->areamap, b->area, NULL));
extl_table_sets_f(t, "func", b->func);
return TRUE;
}
static ExtlTab getbinding(WBindmap *bindmap, WBinding *b)
{
ExtlTab t=extl_create_table();
if(b->act==BINDING_KEYPRESS){
if(get_kpress(bindmap, b, t))
return t;
}else if(b->act!=BINDING_SUBMAP){
if(get_mact(bindmap, b, t))
return t;
}
return extl_unref_table(t);
}
ExtlTab bindmap_getbindings(WBindmap *bindmap)
{
Rb_node node;
WBinding *b;
ExtlTab tab;
ExtlTab btab;
int n=0;
tab=extl_create_table();
FOR_ALL_BINDINGS(b, node, bindmap->bindings){
btab=getbinding(bindmap, b);
extl_table_seti_t(tab, n+1, btab);
extl_unref_table(btab);
n++;
}
return tab;
}
/*}}}*/