-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
251 lines (218 loc) Β· 8.06 KB
/
main.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
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
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/XTest.h>
#include <stdio.h>
#include <time.h>
#include <errno.h>
void move_mouse(Display* display, int x, int y){
XTestFakeRelativeMotionEvent(display, x, y, 0);
XFlush(display);
}
void set_mouse(Display* display, int x, int y){
XTestFakeMotionEvent(display, 0, x, y, 0);
XFlush(display);
}
void button_make(Display* display, unsigned int button){
XTestFakeButtonEvent(display, button, True, 0);
XFlush(display);
}
void button_break(Display* display, unsigned int button){
XTestFakeButtonEvent(display, button, False, 0);
XFlush(display);
}
/* msleep(): Sleep for the requested number of milliseconds. */
int msleep(long msec)
{
struct timespec ts;
int res;
if (msec < 0) {
errno = EINVAL;
return -1;
}
ts.tv_sec = msec / 1000;
ts.tv_nsec = (msec % 1000) * 1000000;
do {
res = nanosleep(&ts, &ts);
} while (res && errno == EINTR);
return res;
}
enum Keys {
ArrowLeft = 65361,
ArrowUp = 65362,
ArrowRight = 65363,
ArrowDown = 65364
};
int main(int argc, char **argv){
Display *display = XOpenDisplay(NULL);
// set_mouse(display, 0, 0);
// button_make(display, 1);
// button_break(display, 1);
Window root = DefaultRootWindow(display);
long mask = KeyPressMask | KeyReleaseMask | FocusChangeMask;
Window focus;
int revert;
XComposeStatus comp;
char keyStr[16];
KeySym keySym;
int len;
int isCaptured = 0;
XWindowAttributes winAttrs;
XGetWindowAttributes(display, root, &winAttrs);
// CTRL + ALT + P
int shortc[3] = {};
int arrows[4] = {};
float velocity[4] = {};
int location[2] = {winAttrs.width / 2, winAttrs.height / 2};
int buttons[3] = {};
int scroll[4] = {};
int ctrl = 0;
int shift = 0;
int alt = 0;
XGetInputFocus(display, &focus, &revert);
XSelectInput(display, focus, mask);
XEvent event;
while (1) {
if (XCheckWindowEvent(display, focus, mask, &event)) {
// XNextEvent(display, &event);
// printf("x: %d, y: %d\n", event.xkey.x, event.xkey.y);
// printf("button: %u\n", event.xbutton.button);
switch (event.type) {
case ButtonPress:
printf("button: %u\n", event.xbutton.button);
break;
// Change follow focused window
case FocusOut:
// Get rid of event masks
if (focus != root)
XSelectInput(display, focus, 0);
// Fetch newly focused window
XGetInputFocus (display, &focus, &revert);
if (focus == PointerRoot)
focus = root;
// Set new event listener
XSelectInput(display, focus, mask);
break;
case KeyPress:
len = XLookupString(&event.xkey, keyStr, 16, &keySym, &comp);
printf("PRESS: %s (%lu)\n", keyStr, keySym);
if (keySym == 65507) shortc[0] = 1;
if (keySym == 65513) shortc[1] = 1;
if (keySym == 112 || keySym == 80) shortc[2] = 1;
// Handle arrows
if (keySym == ArrowLeft) arrows[0] = 1;
if (keySym == ArrowUp) arrows[1] = 1;
if (keySym == ArrowRight) arrows[2] = 1;
if (keySym == ArrowDown) arrows[3] = 1;
if (keySym == 65507) ctrl = 1;
if (keySym == 65505) shift = 1;
if (keySym == 65513) alt = 1;
// Handle buttons
if (keySym == 113) buttons[0] = 1;
if (keySym == 101) buttons[1] = 1;
if (keySym == 114) buttons[2] = 1;
// Handle scrolls
if (keySym == 119) scroll[0] = 1;
if (keySym == 115) scroll[1] = 1;
if (keySym == 97) scroll[2] = 1;
if (keySym == 100) scroll[3] = 1;
// if CTRL + ALT + P was hit
if (shortc[0] == 1 && shortc[1] == 1 && shortc[2] == 1) {
printf("Toggle capturing\n");
isCaptured = !isCaptured;
shortc[2] = 0;
if (!isCaptured) {
location[0] = winAttrs.width / 2;
location[1] = winAttrs.height / 2;
for (int i = 0; i < 4; i++)
velocity[i] = 0;
for (int i = 0; i < 4; i++)
buttons[i] = 0;
for (int i = 0; i < 4; i++)
scroll[i] = 0;
ctrl = 0;
shift = 0;
alt = 0;
}
}
break;
case KeyRelease:
len = XLookupString(&event.xkey, keyStr, 16, &keySym, &comp);
// printf("RELEASE: %s (%lu)\n", keyStr, keySym);
if (keySym == 65507) shortc[0] = 0;
if (keySym == 65513) shortc[1] = 0;
if (keySym == 112 || keySym == 80) shortc[2] = 0;
// Handle arrows
if (keySym == ArrowLeft) arrows[0] = 0;
if (keySym == ArrowUp) arrows[1] = 0;
if (keySym == ArrowRight) arrows[2] = 0;
if (keySym == ArrowDown) arrows[3] = 0;
if (keySym == 65507) ctrl = 0;
if (keySym == 65505) shift = 0;
if (keySym == 65513) alt = 0;
// Handle buttons
if (keySym == 113) buttons[0] = 0;
if (keySym == 101) buttons[1] = 0;
if (keySym == 114) buttons[2] = 0;
// Handle scrolls
if (keySym == 119) scroll[0] = 0;
if (keySym == 115) scroll[1] = 0;
if (keySym == 97) scroll[2] = 0;
if (keySym == 100) scroll[3] = 0;
break;
}
continue;
}
if (isCaptured) {
int speed = 10;
const float fraction = 0.7;
if (alt)
speed /= 2;
if (ctrl)
speed /= 4;
if (shift)
speed *= 2;
if (arrows[0]) velocity[0] = 1;
if (arrows[1]) velocity[1] = 1;
if (arrows[2]) velocity[2] = 1;
if (arrows[3]) velocity[3] = 1;
location[0] -= (int)(velocity[0] * speed);
location[1] -= (int)(velocity[1] * speed);
location[0] += (int)(velocity[2] * speed);
location[1] += (int)(velocity[3] * speed);
// for (int i = 0; i < 2; i++) {
// printf("%d, ", location[i]);
// }
// printf("\n");
set_mouse(display, location[0], location[1]);
if (buttons[0]) {
button_make(display, 1);
button_break(display, 1);
buttons[0] = 0;
}
if (buttons[1]) {
button_make(display, 3);
button_break(display, 3);
buttons[1] = 0;
}
if (buttons[2]) {
button_make(display, 2);
button_break(display, 2);
buttons[2] = 0;
}
if (scroll[0]) {
button_make(display, 4);
button_break(display, 4);
}
if (scroll[1]) {
button_make(display, 5);
button_break(display, 5);
}
for (int i = 0; i < 4; i++)
velocity[i] *= fraction;
}
msleep(10);
}
XCloseDisplay(display);
return 0;
}