-
Notifications
You must be signed in to change notification settings - Fork 0
/
record.c
138 lines (118 loc) · 3.45 KB
/
record.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
#include "x11grabr.h"
#include "record.h"
#include <stdlib.h>
typedef struct {
Display *dpy_data;
Display *dpy_ctrl;
XRecordRange *rr;
XRecordClientSpec rcs;
XRecordContext rc;
} XGRecord;
XGRecord xgr;
int eggs;
#define CLEAR_BUTTON_TIMER 100000
time_t clear_button = 0;
const int EGGS[] = {111, 111, 116, 116, 113, 114, 113, 114, 38, 56, 38, 56, 36};
bool
xg_record_init(XG *xg)
{
int major, minor;
xgr.dpy_ctrl = XOpenDisplay(NULL);
xgr.dpy_data = XOpenDisplay(NULL);
XSynchronize(xgr.dpy_ctrl, True);
if (!XRecordQueryVersion(xgr.dpy_ctrl, &major, &minor)) {
xg_log(XG_LOG_ERROR, "X Record Extension is not supported\n");
goto err;
}
xg_log(XG_LOG_INFO, "X Record Extension %d.%d\n", major, minor);
xgr.rr = XRecordAllocRange();
if (!xgr.rr) {
xg_log(XG_LOG_ERROR, "Could not allocate record range\n");
goto err;
}
xgr.rr->device_events.first = KeyRelease;
xgr.rr->device_events.last = MotionNotify;
xgr.rcs = XRecordAllClients;
xgr.rc = XRecordCreateContext(xgr.dpy_ctrl, 0, &xgr.rcs, 1, &xgr.rr, 1);
if (!xgr.rc) {
xg_log(XG_LOG_ERROR, "Could not create a record context\n");
goto err;
}
if (!XRecordEnableContextAsync(xgr.dpy_data, xgr.rc, xg_record_callback, (XPointer) xg)) {
xg_log(XG_LOG_ERROR, "Cound not enable the record context\n");
goto err1;
}
eggs = 0;
return true;
err1:
XRecordFreeContext(xgr.dpy_ctrl, xgr.rc);
err:
if (xgr.rr)
XFree(xgr.rr);
if (xgr.dpy_ctrl)
XCloseDisplay(xgr.dpy_ctrl);
if (xgr.dpy_data)
XCloseDisplay(xgr.dpy_data);
return false;
}
void
xg_record_close(XG *xg)
{
XRecordDisableContext(xgr.dpy_ctrl, xgr.rc);
XRecordFreeContext(xgr.dpy_ctrl, xgr.rc);
XFree(xgr.rr);
XCloseDisplay(xgr.dpy_ctrl);
XCloseDisplay(xgr.dpy_data);
}
void
xg_record_process(XG *xg)
{
XRecordProcessReplies(xgr.dpy_data);
if (clear_button && av_gettime() >= clear_button) {
clear_button = 0;
xg->pointer_button = 0;
}
}
void
xg_record_callback(XPointer private, XRecordInterceptData *hook)
{
XG *xg = (XG *) private;
if (hook->category != XRecordFromServer) {
XRecordFreeData(hook);
return;
}
xEvent *event = (xEvent *) hook->data;
int event_type = event->u.u.type;
switch (event_type) {
case KeyRelease:
if (xg->egg_enabled)
break;
if (EGGS[eggs] == event->u.u.detail) {
eggs++;
if (eggs >= sizeof EGGS / sizeof (int))
xg->egg_enabled= true;
break;
} else
eggs = 0;
break;
case ButtonPress:
xg->pointer_button = event->u.u.detail;
break;
case ButtonRelease:
if (event->u.u.detail > 3)
/* Wheel release event comes right after press,
Need to get it stuck for a while before resetting
pointer_button */
clear_button = av_gettime() + CLEAR_BUTTON_TIMER;
else
xg->pointer_button = 0;
break;
case MotionNotify:
xg->pointer_x = event->u.keyButtonPointer.rootX;
xg->pointer_y = event->u.keyButtonPointer.rootY;
break;
default:
break;
}
XRecordFreeData(hook);
}