-
Notifications
You must be signed in to change notification settings - Fork 0
/
xkeylayers.cpp
391 lines (332 loc) · 10.6 KB
/
xkeylayers.cpp
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
/******************************************************************************
* xkeylayers
*****************************************************************************/
#include <stdarg.h>
#include <sys/time.h>
#include <signal.h>
#include <unistd.h>
#include <pthread.h>
#include <X11/XKBlib.h>
#include <X11/keysym.h>
#include <X11/extensions/record.h>
#include <X11/extensions/XTest.h>
#include <X11/Xlib.h>
#include <X11/Xproto.h>
#include <iostream>
#include <vector>
#include "config.hpp"
class TApplication
{
public: // system related
// todo: wrap these away so that this module contains only the application
// logic
Display* _data_conn;
Display* _ctrl_conn;
XRecordContext _record_ctx;
pthread_t _sigwait_thread;
sigset_t _sigset;
void trigger_key(TModifierKey *mod);
public: // app. logic related
bool debug;
TModifierKeys modifiers;
vector<pair<KeySym, timeval>> last_presses; // a log of last N presses
// for heuristics
TKey* generated;
timeval timeout;
bool timeout_valid;
void connect_x11();
void hook_x11();
};
TApplication app;
/******************************************************************************
* utilities
*****************************************************************************/
void debug(const char *fmt, ...) {
if (app.debug) {
va_list args;
va_start(args, fmt);
vfprintf(stdout, fmt, args);
va_end(args);
}
}
inline KeySym sym_from_code(KeyCode code) {
return XkbKeycodeToKeysym(app._ctrl_conn, code, 0, 0);
}
/******************************************************************************
* x11 setup
*****************************************************************************/
void intercept(XPointer user_data, XRecordInterceptData* data);
void TApplication::trigger_key(TModifierKey* mod)
{
for (auto keycode : mod->fake_keys)
{
::debug("Generating %s!\n", XKeysymToString(sym_from_code(keycode)));
XTestFakeKeyEvent(_ctrl_conn, keycode, True, 0);
generated = key_add_key(generated, keycode);
}
for (auto keycode : mod->fake_keys)
{
XTestFakeKeyEvent(_ctrl_conn, keycode, False, 0);
generated = key_add_key(generated, keycode);
}
XFlush(_ctrl_conn);
}
void TApplication::connect_x11() {
int dummy;
_data_conn = XOpenDisplay(NULL);
_ctrl_conn = XOpenDisplay(NULL);
if (!_data_conn || !_ctrl_conn)
{
cerr << "Unable to connect to X11 display. Is $DISPLAY set?\n";
exit(EXIT_FAILURE);
}
if (!XQueryExtension(_ctrl_conn,
"XTEST", &dummy, &dummy, &dummy))
{
cerr << "Xtst extension missing\n";
exit(EXIT_FAILURE);
}
if (!XRecordQueryVersion(_ctrl_conn, &dummy, &dummy))
{
cerr << "Failed to obtain xrecord version\n";
exit(EXIT_FAILURE);
}
if (!XkbQueryExtension(_ctrl_conn, &dummy, &dummy,
&dummy, &dummy, &dummy))
{
cerr << "Failed to obtain xkb version\n";
exit(EXIT_FAILURE);
}
}
void TApplication::hook_x11() {
XRecordRange *rec_range = XRecordAllocRange();
rec_range->device_events.first = KeyPress;
rec_range->device_events.last = ButtonRelease;
XRecordClientSpec client_spec = XRecordAllClients;
_record_ctx = XRecordCreateContext(_ctrl_conn,
0, &client_spec, 1, &rec_range, 1);
if (_record_ctx == 0)
{
cerr << "Failed to create xrecord context\n";
exit(EXIT_FAILURE);
}
XSync(_ctrl_conn, False);
if (!XRecordEnableContext(_data_conn,
_record_ctx, intercept, (XPointer)&app))
{
cerr << "Failed to enable xrecord context\n";
exit(EXIT_FAILURE);
}
if (!XRecordFreeContext(_ctrl_conn, _record_ctx))
{
cerr << "Failed to free xrecord context\n";
}
}
/******************************************************************************
* layering logic
*****************************************************************************/
void handle_modifier(TModifierKey *mod, bool mouse_pressed, int key_event);
// this is the main hook to the X11. every input event is processed in this func.
void intercept(XPointer user_data, XRecordInterceptData* data)
{
static bool mouse_pressed = False;
if (data->category == XRecordFromServer)
{
xEvent* event = reinterpret_cast<xEvent*>(data->data);
int key_event = event->u.u.type;
KeyCode key_code = event->u.u.detail;
debug("Intercepted key event %d, key code %d\n", key_event, key_code);
TKey *g, *g_prev = NULL;
for (g = app.generated; g != NULL; g = g->next)
{
if (g->key == key_code)
{
debug("Ignoring generated event.\n");
if (g_prev != NULL)
{
g_prev->next = g->next;
}
else
{
app.generated = g->next;
}
free (g);
goto exit;
}
g_prev = g;
}
// log last N keys for heuristics
// todo: make this a circular buffer or something
if (key_event == KeyRelease) {
timeval now;
gettimeofday(&now, NULL);
auto p = make_pair(sym_from_code(key_code), now);
app.last_presses.push_back(p);
}
if (key_event == ButtonPress)
{
mouse_pressed = True;
}
else if (key_event == ButtonRelease)
{
mouse_pressed = False;
}
else
{
for (TModifierKey* mod : app.modifiers) {
if ((mod->use_keycode == False && sym_from_code(key_code) == mod->keysym)
||
(mod->use_keycode == True && key_code == mod->keycode))
{
handle_modifier(mod, mouse_pressed, key_event);
}
else if (mod->is_down && key_event == KeyPress)
{
mod->used = True;
}
}
}
}
exit:
XRecordFreeData(data);
}
bool is_any_mod_down() {
return std::any_of(app.modifiers.begin(),
app.modifiers.end(),
[](TModifierKey* m){
return m->is_down;
});
}
// testing heuristics ideas...
// this one looks if the last 3 immediate keypresses are alpha or not.
// if the last 3 immediate key events are alphanumeric, most likely user wants
// the space key.
bool heuristic_test1(TModifierKey* mod) {
auto& lst = app.last_presses;
bool last_3_keys_alpha =
std::all_of(lst.rbegin() + 1, // the very last event was for the
// modifier itself, so skip that.
lst.rbegin() + 4,
[](pair<KeySym, timeval> p){
KeySym k = p.first;
return ((k >= XK_A && k <= XK_Z) ||
(k >= XK_a && k <= XK_z));
}
);
// take a look if the space was pressed very recently... (1 sec)
timeval now;
gettimeofday(&now, NULL);
bool is_immediate = abs(diff_ms(now, lst[lst.size()-1].second)) < 1000;
return last_3_keys_alpha && is_immediate;
}
// handle_modifier is called everytime a modifier key press/release occurs.
void handle_modifier(TModifierKey* mod, bool mouse_pressed, int key_event)
{
if (key_event == KeyPress)
{
debug("Key pressed!\n");
mod->is_down = True;
if (app.timeout_valid)
gettimeofday(&mod->down_at, NULL);
if (mouse_pressed)
{
mod->used = True;
}
// testing heuristic 1
if (heuristic_test1(mod)) {
// if heuristic func returns true this means user probably wants
// the key itself (space) not the modifier.
app.trigger_key(mod);
mod->used = true;
}
}
else
{
debug("Key released!\n");
if (!mod->used)
{
timeval down_time = app.timeout;
if (app.timeout_valid)
{
gettimeofday(&down_time, NULL);
timersub(&down_time, &mod->down_at, &down_time);
}
if (!app.timeout_valid ||
timercmp(&down_time, &app.timeout, <))
{
// so a modifier key was pressed shortly.
// generate fake keys (actually... press and release events).
app.trigger_key(mod);
}
}
mod->used = False;
mod->is_down = False;
}
}
/******************************************************************************
* main
*****************************************************************************/
void* sig_handler(void *user_data)
{
TApplication *self = (TApplication*)user_data;
int sig;
debug("sig_handler running...\n");
sigwait(&self->_sigset, &sig);
debug("Caught signal %d!\n", sig);
if (!XRecordDisableContext(self->_ctrl_conn, self->_record_ctx))
{
fprintf(stderr, "Failed to disable xrecord context\n");
exit(EXIT_FAILURE);
}
XSync(self->_ctrl_conn, False);
debug("sig_handler exiting...\n");
return NULL;
}
int main(int argc, char **argv)
{
int ch;
app.debug = False;
app.timeout.tv_sec = 0;
app.timeout.tv_usec = 500000;
app.timeout_valid = True;
app.generated = NULL;
while ((ch = getopt (argc, argv, "d")) != -1)
{
switch (ch)
{
case 'd':
app.debug = True;
break;
default:
printf("Usage: %s [-d]\n", argv[0]);
printf("Runs as a daemon unless -d flag is set\n");
return EXIT_SUCCESS;
}
}
app.connect_x11();
try {
app.modifiers = load_config(".xkeylayers");
}
catch (...) {
debug("configuration failure. exiting!");
exit(EXIT_FAILURE);
}
if (app.modifiers.size() == 0) {
debug("no modifiers declared. exiting!");
exit(EXIT_FAILURE);
}
if (!app.debug)
daemon (0, 0);
// setup signal handler for proper exits.
sigemptyset(&app._sigset);
sigaddset(&app._sigset, SIGINT);
sigaddset(&app._sigset, SIGTERM);
pthread_sigmask(SIG_BLOCK, &app._sigset, NULL);
pthread_create(&app._sigwait_thread, NULL, sig_handler, &app);
// hook 11 and start processing input events. this call will block.
app.hook_x11();
XCloseDisplay(app._ctrl_conn);
XCloseDisplay(app._data_conn);
debug("main exiting\n");
return EXIT_SUCCESS;
}