-
Notifications
You must be signed in to change notification settings - Fork 2
/
mouseEventThread.cpp
135 lines (123 loc) · 4.21 KB
/
mouseEventThread.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
#include "mouseEventThread.h"
MouseEventThread::MouseEventThread(QRect screenRect, int winID, double sensibility, QObject *parent) : QThread(parent) {
connect(this, &QThread::finished, this, &MouseEventThread::cleanup);
controlDisplay = XOpenDisplay(NULL);
rootWindow = XRootWindow(controlDisplay, 0);
appWindow = winID;
dataDisplay = XOpenDisplay(NULL);
if (!controlDisplay || !dataDisplay) {
qFatal("Cannot open display!");
}
XSynchronize(controlDisplay, 1);
int major, minor;
if (!XRecordQueryVersion (controlDisplay, &major, &minor)) {
qFatal("RECORD extension not supported on this X server!");
}
qInfo("Version of record extension: %d.%d", major, minor);
this->sensibility = sensibility;
}
void MouseEventThread::run() {
XRecordClientSpec rcs;
rr = XRecordAllocRange();
if (!rr) {
qFatal("Could not alloc record range object!");
}
rr->device_events.first = KeyPress;
rr->device_events.last = MotionNotify;
rcs = XRecordAllClients;
ctx = XRecordCreateContext(controlDisplay, 0, &rcs, 1, &rr, 1);
if (!ctx) {
qFatal("Could not create a record context!");
}
XRecordEnableContext(dataDisplay, ctx, callback, (XPointer) this);
while (true) {
XRecordProcessReplies(dataDisplay);
}
}
MouseEventThread::~MouseEventThread() {
}
void MouseEventThread::callback(XPointer closure, XRecordInterceptData *hook) {
((MouseEventThread *) closure)->processEvent(hook);
}
void MouseEventThread::processEvent(XRecordInterceptData *hook) {
if (hook->category != XRecordFromServer) {
XRecordFreeData (hook);
return;
}
XRecordDatum *data = (XRecordDatum*) hook->data;
unsigned char buttonCode = data->event.u.u.detail;
int absX, absY, relX, relY;
int eventType = data->type;
switch (eventType) {
case ButtonPress:
if (buttonCode == 1) {
if (queryCursor(relX, relY, absX, absY)) {
break;
}
emit mousePress(QPoint(relX * sensibility, relY * sensibility), QPoint(relX, relY));
}
break;
case ButtonRelease:
if (buttonCode == 1) {
if (queryCursor(relX, relY, absX, absY)) {
break;
}
emit mouseRelease(QPoint(relX * sensibility, relY * sensibility), QPoint(relX, relY));
}
break;
case MotionNotify:
if (queryCursor(relX, relY, absX, absY)) {
break;
}
// dx = lastx - relX;
// dy = lasty - relY;
// lastx = relX, lasty = relY;
// if (abs(dx) < 10 || abs(dy) < 10) {
// qDebug() << "motion too small, ignoring...";
// break;
// }
emit mouseEvent(QPoint(relX * sensibility, relY * sensibility), QPoint(relX, relY));
break;
}
XRecordFreeData (hook);
}
int MouseEventThread::queryCursor(int &relX, int &relY, int &absX, int &absY) {
Window root_return, child_return;
int root_x_return, root_y_return;
int win_x_return, win_y_return;
unsigned int mask_return;
int retval = XQueryPointer(controlDisplay, rootWindow, &root_return, &child_return,
&root_x_return, &root_y_return,
&win_x_return, &win_y_return,
&mask_return);
if (retval != 1) {
return 1;
}
int local_x, local_y;
XTranslateCoordinates(controlDisplay, rootWindow, appWindow,
root_x_return, root_y_return,
&local_x, &local_y, &child_return);
relX = local_x;
relY = local_y;
absX = root_x_return;
absY = root_y_return;
return 0;
}
void MouseEventThread::cleanup() {
if (ctx) {
XRecordDisableContext(controlDisplay, ctx);
XRecordFreeContext(controlDisplay, ctx);
}
if (rr) {
XFree(rr);
}
if (controlDisplay) {
XCloseDisplay(controlDisplay);
}
if (dataDisplay) {
XCloseDisplay(dataDisplay);
}
}
void MouseEventThread::setMouseSensibility(double mouseSensibility) {
this->sensibility = sensibility;
}