This repository has been archived by the owner on Aug 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxbindkeys.c
408 lines (331 loc) · 11.3 KB
/
xbindkeys.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
/***************************************************************************
xbindkeys : a program to bind keys to commands under X11.
-------------------
begin : Sat Oct 13 14:11:34 CEST 2001
copyright : (C) 2001 by Philippe Brochard
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
//#include <config.h>
#include <X11/Xlib.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
//#include <fcntl.h>
#include "keys.h"
#include "get_key.h"
#include "grab_key.h"
#include "options.h"
#include <libguile.h>
#include "util.h"
#include <X11/XKBlib.h>
#include <popt.h>
static void inner_main(void * /*passed_data*/, int /*argc*/, char ** /*argv*/);
Display *current_display; // The current display
static int got_hup;
char *geom;
void catch_hup_signal(int sig) {
got_hup = 1;
#ifdef AVOID_KNOWN_HARMLESS_WARNINGS
sig = sig;
#endif
}
void catch_chld_signal(int sig) {
pid_t child;
/* If more than one child exits at approximately the same time, the signals
*/
/* may get merged. Handle this case correctly. */
while ((child = waitpid(-1, NULL, WNOHANG)) > 0) {
#ifdef DEBUG
printf("Catch CHLD signal -> pid %i terminated\n", child);
#endif
}
#ifdef AVOID_KNOWN_HARMLESS_WARNINGS
sig = sig;
#endif
}
int main(const int ARGC, const char **argv) {
// guile shouldn't steal our arguments! we already parse them!
// so we put them in temporary variables.
got_hup = 0;
char c;
char *home;
char default_file[513];
home = getenv("HOME");
strncpy(default_file, home, sizeof(default_file) - 24);
strncat(default_file, "/.config/xbindkeysrc.scm", sizeof(default_file) - strlen(default_file) - 1);
// Option definitions
int verbose = 0;
char *rc_guile_file = NULL;
int have_to_show_binding = 0;
int have_to_get_binding = 0;
int have_to_start_as_daemon = 1;
int detectable_ar = 0;
int poll_rc;
char *display_name = NULL;
char *geom = NULL;
Display *d;
struct poptOption options_table[] = {
{"version", 'V', 0, NULL, 'V', "prints version and exit", NULL},
{"display", 'X', POPT_ARG_STRING, &display_name, 0,
"Set X display to use", NULL},
{"file", 'f', POPT_ARG_STRING, &rc_guile_file, 0,
"Use an alternative rc file", NULL},
{"geometry", 'g', POPT_ARG_STRING, &geom, 0,
"size and position of window open with -k or-m option", NULL},
{"verbose", 'v', 0, &verbose, 'v', "be verbose for debugging purposes",
NULL},
{"poll_rc", 'p', 0, &poll_rc, 0, "Poll the config for updates", NULL},
{"show", 's', 0, &have_to_show_binding, 0, "Show the actual keybindings",
NULL},
{"key", 'k', 0, &have_to_get_binding, 0, "Identify one keypress", NULL},
{"multikey", 'm', 0, NULL, 'm', "Identify multiple keypresses", NULL},
{"nodaemon", 'n', 0, NULL, 'n', "don't start as daemon", NULL},
{"detectable-ar", 'a', 0, &detectable_ar, 'a',
"something something autorepeat", NULL},
POPT_AUTOHELP{NULL, 0, 0, NULL, 0, NULL, NULL}};
poptContext opt_con = poptGetContext(NULL, ARGC, argv, options_table, 0);
poptSetOtherOptionHelp(opt_con, "[OPTIONS]*");
while ((c = poptGetNextOpt(opt_con)) >= 0) {
switch (c) {
case 'V':
fprintf(stderr, "xbindkeys %s by Philippe Brochard & @Hudmont\n",
PACKAGE_VERSION);
exit(1);
break;
case 'v':
case 'n':
have_to_start_as_daemon = 0;
break;
case 'm':
have_to_get_binding = 2;
break;
}
}
if (rc_guile_file == NULL) {
rc_guile_file = default_file;
}
if (!rc_file_exist(rc_guile_file)) {
exit(-1);
}
if (have_to_start_as_daemon && !have_to_show_binding && !have_to_get_binding) {
start_as_daemon();
}
if (!display_name) {
display_name = XDisplayName(NULL);
}
if (verbose) {
printf("displayName = %s\n", display_name);
printf("rc guile file = %s\n", rc_guile_file);
}
d = start(display_name);
// current_display = d;
if (detectable_ar) {
Bool supported_rtrn;
XkbSetDetectableAutoRepeat(d, True, &supported_rtrn);
if (!supported_rtrn) {
fprintf(stderr, "Could not set detectable autorepeat\n");
}
}
#ifdef DEBUG
printf("Autorepeat brach resolved!\n");
#endif
get_offending_modifiers(d);
#ifdef DEBUG
printf("Offending modifiers retrieved!\n");
#endif
if (have_to_get_binding) {
get_key_binding(d, have_to_get_binding, geom);
end_it_all(d);
exit(0);
}
struct passed_data to_pass = {.rc_guile_file = rc_guile_file,
.have_to_show_binding = have_to_show_binding,
.poll_rc = poll_rc,
.d = d,
.verbose = verbose};
//free(home);
#ifdef DEBUG
printf("Starting in guile mode...\n");
#endif
scm_boot_guile(0, (char **)NULL, inner_main, (void *)(&to_pass));
return 0; /* not reached ...*/
}
/*void handle(Display *d, XEvent *e, int verbose)
{
print_key (d, &keys[i], verbose);
adjust_display(&e.xany);
start_command_key (&keys[i]);
}
*/
void init_finalize(Display *d, char *rc_guile_file, int have_to_show_binding,
int verbose) {
// Config loading needs to be done in guilemode, therefore it's here
if (get_rc_guile_file(d, rc_guile_file, verbose) != 0) {
exit(-1);
}
// this option requires the preloaded conf file
if (have_to_show_binding) {
show_key_binding(d, verbose);
end_it_all(d);
exit(0);
}
grab_keys(d, verbose);
/* This: for restarting reading the RC file if got a HUP signal */
signal(SIGHUP, catch_hup_signal);
/* and for reaping dead children */
signal(SIGCHLD, catch_chld_signal);
}
void inner_main(void *passed_data, int argc, char **argv) {
struct passed_data *params = (struct passed_data *)passed_data;
struct passed_data p = *params;
init_finalize(p.d, p.rc_guile_file, p.have_to_show_binding, p.verbose);
#ifdef DEBUG
printf("starting loop...\n");
#endif
XEvent e;
time_t rc_guile_file_changed = 0;
struct stat rc_guile_file_info;
XSetErrorHandler((XErrorHandler)&null_x_error);
if (p.poll_rc) {
stat(p.rc_guile_file, &rc_guile_file_info);
rc_guile_file_changed = rc_guile_file_info.st_mtime;
}
while (True) {
while ((got_hup || p.poll_rc) && !XPending(p.d)) {
// if the rc guile file has been modified, reload it
stat(p.rc_guile_file, &rc_guile_file_info);
if (rc_guile_file_info.st_mtime != rc_guile_file_changed) {
reload_rc_file(p.d, p.rc_guile_file, p.verbose);
#ifdef DEBUG
printf("The configuration file has been modified, reload it\n");
#endif
rc_guile_file_changed = rc_guile_file_info.st_mtime;
got_hup = 0;
}
usleep(SLEEP_TIME * 1000);
}
XNextEvent(p.d, &e);
switch (e.type) {
case KeyPress:
#ifdef DEBUG
printf("Key press !\n");
printf("e.xkey.keycode=%d\n", e.xkey.keycode);
printf("e.xkey.state=%d\n", e.xkey.state);
#endif
e.xkey.state &= ~(numlock_mask | capslock_mask | scrolllock_mask);
for (int i = 0; i < nb_keys; i++) {
if (keys[i].type == SYM && keys[i].event_type == PRESS) {
if (e.xkey.keycode == XKeysymToKeycode(p.d, keys[i].key.sym) &&
e.xkey.state == keys[i].modifier) {
print_key(p.d, &keys[i], p.verbose);
adjust_display(&e.xany);
start_command_key(&keys[i]);
}
} else if (keys[i].type == CODE && keys[i].event_type == PRESS) {
if (e.xkey.keycode == keys[i].key.code &&
e.xkey.state == keys[i].modifier) {
print_key(p.d, &keys[i], p.verbose);
adjust_display(&e.xany);
start_command_key(&keys[i]);
}
}
}
break;
case KeyRelease:
#ifdef DEBUG
printf("Key release !\n");
printf("e.xkey.keycode=%d\n", e.xkey.keycode);
printf("e.xkey.state=%d\n", e.xkey.state);
#endif
e.xkey.state &= ~(numlock_mask | capslock_mask | scrolllock_mask);
for (int i = 0; i < nb_keys; i++) {
if (keys[i].type == SYM && keys[i].event_type == RELEASE) {
if (e.xkey.keycode == XKeysymToKeycode(p.d, keys[i].key.sym) &&
e.xkey.state == keys[i].modifier) {
print_key(p.d, &keys[i], p.verbose);
adjust_display(&e.xany);
start_command_key(&keys[i]);
}
} else if (keys[i].type == CODE && keys[i].event_type == RELEASE) {
if (e.xkey.keycode == keys[i].key.code &&
e.xkey.state == keys[i].modifier) {
print_key(p.d, &keys[i], p.verbose);
adjust_display(&e.xany);
start_command_key(&keys[i]);
}
}
}
break;
case ButtonPress:
#ifdef DEBUG
printf("Button press !\n");
printf("e.xbutton.button=%d\n", e.xbutton.button);
printf("e.xbutton.state=%d\n", e.xbutton.state);
#endif
e.xbutton.state &=
0x1FFF &
~(numlock_mask | capslock_mask | scrolllock_mask | Button1Mask |
Button2Mask | Button3Mask | Button4Mask | Button5Mask);
for (int i = 0; i < nb_keys; i++) {
if (keys[i].type == BUTTON && keys[i].event_type == PRESS) {
if (e.xbutton.button == keys[i].key.button &&
e.xbutton.state == keys[i].modifier) {
// printf("Replay!!!\n");
// ungrab_all_keys(p.d);
// XPutBackEvent(p.d, &e);
// sleep(1);
// grab_keys(p.d, p.verbose);
print_key(p.d, &keys[i], p.verbose);
adjust_display(&e.xany);
start_command_key(&keys[i]);
}
}
}
break;
case ButtonRelease:
#ifdef DEBUG
printf("Button release !\n");
printf("e.xbutton.button=%d\n", e.xbutton.button);
printf("e.xbutton.state=%d\n", e.xbutton.state);
#endif
e.xbutton.state &=
0x1FFF &
~(numlock_mask | capslock_mask | scrolllock_mask | Button1Mask |
Button2Mask | Button3Mask | Button4Mask | Button5Mask);
for (int i = 0; i < nb_keys; i++) {
if (keys[i].type == BUTTON && keys[i].event_type == RELEASE) {
if (e.xbutton.button == keys[i].key.button &&
e.xbutton.state == keys[i].modifier) {
print_key(p.d, &keys[i], p.verbose);
adjust_display(&e.xany);
start_command_key(&keys[i]);
}
}
}
break;
default:
break;
}
} /* infinite loop */
#ifdef DEBUG
printf("ending...\n");
#endif
end_it_all(p.d);
#ifdef AVOID_KNOWN_HARMLESS_WARNINGS
argc = argc;
argv = argv;
#endif
}