Skip to content

Latest commit

 

History

History
134 lines (107 loc) · 2.95 KB

keyhook.c

File metadata and controls

134 lines (107 loc) · 2.95 KB
 
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
// !!! FIXME: this is X11 specific. :(
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xlibint.h>
#include <X11/extensions/record.h>
#include "keyhook.h"
static volatile int keyPressFlags = 0;
static volatile int sawKeyCombo = 0;
static void keyhookCallback(XPointer priv, XRecordInterceptData *data)
{
const xEvent *xev = (const xEvent *) data->data;
if (data->category == XRecordFromServer)
{
const BYTE keycode = xev->u.u.detail;
if (xev->u.u.type == KeyPress)
{
// !!! FIXME: don't hardcode these keycodes.
if ((keycode == 64) && (keyPressFlags == 0))
keyPressFlags++;
else if ((keycode == 133) && (keyPressFlags == 1))
keyPressFlags++;
else if ((keycode == 51) && (keyPressFlags == 2))
{
sawKeyCombo = 1;
keyPressFlags = 0;
} // else if
else
keyPressFlags = 0;
} // if
else if (xev->u.u.type == KeyRelease)
{
keyPressFlags = 0;
} // else if
} // if
XRecordFreeData(data);
} // keyhookCallback
// every example I've seen needs two Display connections...one for the
// keyhook, and one to control it.
static Display *ctrldpy = NULL;
static Display *datadpy = NULL;
static XRecordContext xrc = 0;
int initKeyHook(void)
{
int major = 0;
int minor = 0;
XRecordRange *xrr = NULL;
XRecordClientSpec xrcs = XRecordAllClients;
if (ctrldpy)
return 0; // already initialized.
ctrldpy = XOpenDisplay(NULL);
if (!ctrldpy)
goto failed;
XSynchronize(ctrldpy, True);
datadpy = XOpenDisplay(NULL);
if (!datadpy)
goto failed;
else if (!XRecordQueryVersion(ctrldpy, &major, &minor))
goto failed;
else if ((xrr = XRecordAllocRange()) == NULL)
goto failed;
memset(xrr, '\0', sizeof (*xrr));
xrr->device_events.first = KeyPress;
xrr->device_events.last = KeyPress;
if ((xrc = XRecordCreateContext(ctrldpy, 0, &xrcs, 1, &xrr, 1)) == 0)
goto failed;
else if (!XRecordEnableContextAsync(datadpy, xrc, keyhookCallback, NULL))
goto failed;
XFree(xrr);
xrr = NULL;
return 1;
failed:
deinitKeyHook();
if (xrr) XFree(xrr);
return 0;
} // initKeyHook
void deinitKeyHook(void)
{
if (ctrldpy)
{
if (xrc)
{
XRecordDisableContext(ctrldpy, xrc);
XRecordFreeContext(ctrldpy, xrc);
} // if
XCloseDisplay(ctrldpy);
} // if
if (datadpy)
XCloseDisplay(datadpy);
ctrldpy = NULL;
datadpy = NULL;
xrc = 0;
sawKeyCombo = 0;
keyPressFlags = 0;
} // deinitKeyHook
int pumpKeyHook(void)
{
if (!datadpy)
return 0;
sawKeyCombo = 0;
XRecordProcessReplies(datadpy);
return sawKeyCombo;
} // pumpKeyHook
// end of keyhook.c ...