-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyboard.c
98 lines (86 loc) · 2.69 KB
/
keyboard.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
// this code mostly comes from here: https://github.com/anko/xkbcat/blob/master/xkbcat.c
#include "keyboard.h"
#include "vangoghflow.h"
#include <X11/XKBlib.h>
#include <X11/extensions/XInput2.h>
int xiOpcode;
Display *disp;
bool initKB()
{
// Connect to X display
// this is just harded for now...
disp = XOpenDisplay(":0");
if (NULL == disp)
{
fprintf(stderr, "Cannot open X display: %s\n", ":0");
return false;
}
{ // Test for XInput 2 extension
int queryEvent, queryError;
if (!XQueryExtension(disp, "XInputExtension", &xiOpcode,
&queryEvent, &queryError))
{
fprintf(stderr, "X Input extension not available\n");
exit(2);
}
}
{ // Request XInput 2.0, to guard against changes in future versions
int major = 2, minor = 0;
int queryResult = XIQueryVersion(disp, &major, &minor);
if (queryResult == BadRequest)
{
fprintf(stderr, "Need XI 2.0 support (got %d.%d)\n", major, minor);
exit(3);
}
else if (queryResult != Success)
{
fprintf(stderr, "XIQueryVersion failed!\n");
exit(4);
}
}
{ // Register to receive XInput events
Window root = DefaultRootWindow(disp);
XIEventMask m;
m.deviceid = XIAllMasterDevices;
m.mask_len = XIMaskLen(XI_LASTEVENT);
m.mask = calloc(m.mask_len, sizeof(char));
XISetMask(m.mask, XI_RawKeyPress);
XISelectEvents(disp, root, &m, 1 /*number of masks*/);
XSync(disp, false);
free(m.mask);
}
return true;
}
void *CheckForHotKeys()
{
while (!shouldStop)
{
XEvent event;
XGenericEventCookie *cookie = (XGenericEventCookie *)&event.xcookie;
XNextEvent(disp, &event);
if (XGetEventData(disp, cookie) &&
cookie->type == GenericEvent &&
cookie->extension == xiOpcode)
{
switch (cookie->evtype)
{
case XI_RawKeyRelease:
case XI_RawKeyPress:
{
XIRawEvent *ev = cookie->data;
// Ask X what it calls that key; skip if it doesn't know
KeySym s = XkbKeycodeToKeysym(disp, ev->detail,
0 /*group*/, 0 /*shift level*/);
if (NoSymbol == s)
continue;
char *str = XKeysymToString(s);
if (NULL == str)
continue;
gdk_threads_add_idle_full(G_PRIORITY_DEFAULT_IDLE, DoUpdate, str, NULL);
break;
}
}
}
}
return NULL;
}