-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
executable file
·513 lines (431 loc) · 11.1 KB
/
main.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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
#include <err.h>
#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/uinput.h>
#include <string.h>
#include <dirent.h>
#include <vector>
#include <string>
#include <iostream>
#include <poll.h>
#include <signal.h>
#include <pthread.h>
#include <atomic>
#define STR(X) STR2(X)
#define STR2(X) #X
#define ENSURE(CONDITION, ...) if (!(CONDITION)) { err(EXIT_FAILURE, __FILE__ ":" STR(__LINE__) " " __VA_ARGS__); }
static const char *EVENT_DEV_NAME = "event";
static const char *EVENT_DEV_PREFIX = "/dev/input/";
static const char *UINPUT_NAME = "/dev/uinput";
static const int BITS_PER_LONG = sizeof(long) * 8;
inline int nbytes(int x)
{
return (x - 1) / BITS_PER_LONG + 1;
}
inline bool testBits(int bit, const unsigned long array[])
{
unsigned long result = array[bit / BITS_PER_LONG] >> bit % BITS_PER_LONG & 1;
return (result == 1);
}
static int isEventDevice(const struct dirent *dir)
{
return strncmp(EVENT_DEV_NAME, dir->d_name, strlen(EVENT_DEV_NAME)) == 0;
}
static void createVirtualMouse(int fd)
{
// enable mouse button left and relative events
ioctl(fd, UI_SET_EVBIT, EV_KEY);
ioctl(fd, UI_SET_KEYBIT, BTN_LEFT);
ioctl(fd, UI_SET_EVBIT, EV_REL);
ioctl(fd, UI_SET_RELBIT, REL_X);
ioctl(fd, UI_SET_RELBIT, REL_Y);
struct uinput_user_dev uud;
memset(&uud, 0, sizeof(uud));
snprintf(uud.name, UINPUT_MAX_NAME_SIZE, "Virtual Mouse");
int ret = write(fd, &uud, sizeof(uud));
ENSURE(ret == sizeof(uud));
ioctl(fd, UI_DEV_CREATE);
}
static std::vector<std::string> getAllEventDevicePath()
{
struct dirent **direntList;
int ndev = scandir(EVENT_DEV_PREFIX, &direntList, isEventDevice, versionsort);
ENSURE(ndev > 0, "No event device found");
std::vector<std::string> result(ndev);
for (int i = 0; i < ndev; i++)
{
result[i] = std::string(EVENT_DEV_PREFIX) + std::string(direntList[i]->d_name);
}
return std::move(result);
}
// A simple RAII class
class FileOpener
{
public:
explicit FileOpener(const char *pathname, int flags)
{
fd = open(pathname, flags);
}
explicit FileOpener(const char *pathname, int flags, mode_t mode)
{
fd = open(pathname, flags, mode);
}
~FileOpener()
{
if (fd >= 0)
{
close(fd);
}
}
int getFd()
{
return fd;
}
private:
int fd;
};
static bool isSupportType(const std::string &path, int type)
{
FileOpener opener(path.c_str(), O_RDONLY);
int fd = opener.getFd();
if (fd < 0)
{
return false;
}
unsigned long supportType[nbytes(EV_MAX)];
ioctl(fd, EVIOCGBIT(0, EV_MAX), supportType);
if (!testBits(type, supportType))
{
return false;
}
return true;
}
static bool isSupportTypeCode(const std::string &path, int type, int code)
{
if (!isSupportType(path, type))
{
return false;
}
FileOpener opener(path.c_str(), O_RDONLY);
int fd = opener.getFd();
if (fd < 0)
{
return false;
}
unsigned long supportCode[nbytes(KEY_MAX)];
ioctl(fd, EVIOCGBIT(type, KEY_MAX), supportCode);
if (!testBits(code, supportCode))
{
return false;
}
return true;
}
static const int NEEDED_KEY[] = { KEY_KP8, KEY_KP2, KEY_KP4, KEY_KP6 };
// device reports KEY_KP8 KEY_KP2 KEY_KP4 KEY_KP6 is valid keyboard
static bool isValidKeyboard(const std::string &path)
{
for (unsigned int i = 0; i < (sizeof(NEEDED_KEY) / sizeof(int)); i++)
{
if (!isSupportTypeCode(path, EV_KEY, NEEDED_KEY[i]))
{
return false;
}
}
return true;
}
static std::vector<std::string> getAllValidKeyboard()
{
std::vector<std::string> validDevice;
std::vector<std::string> allDevice = getAllEventDevicePath();
for (unsigned int i = 0; i < allDevice.size(); i++)
{
if (isValidKeyboard(allDevice[i]))
{
validDevice.push_back(allDevice[i]);
}
}
return validDevice;
}
static std::string getOneNumLockDevice()
{
std::vector<std::string> allDevice = getAllEventDevicePath();
for (unsigned int i = 0; i < allDevice.size(); i++)
{
if (isSupportTypeCode(allDevice[i], EV_LED, LED_CAPSL))
{
return allDevice[i];
}
}
return std::string("");
}
static int uinputFd = 0;
// program should exit
static volatile bool shouldStop = false;
// if numlock is on, we don't move mouse
static bool numlockOn = false;
const static int UP = 0;
const static int DOWN = 1;
const static int LEFT = 2;
const static int RIGHT = 3;
const static int DIRECTION_NUM = 4;
// move status
// change to true/false when corresponding key is pressed/released
static bool isMoving[DIRECTION_NUM] = { false };
// move times from the point that corresponding key is pressed
// reset to 0 when key is released
static std::atomic_int moveTimes[DIRECTION_NUM];
inline void resetMoveTimes()
{
for (int i = 0; i < DIRECTION_NUM; i++)
{
moveTimes[i] = 0;
}
}
// move step is affected by move times
// so we can apply some acceleration strategy
static int moveStep(int times)
{
const int MIN_STEP = 1;
const int MAX_STEP = 10;
const int MIN_POINT = 50;
const int MAX_POINT = 200;
if (times <= MIN_POINT)
{
return MIN_STEP;
}
else if (times <= MAX_POINT)
{
return MIN_STEP + (times - MIN_POINT) * 1.0 / (MAX_POINT - MIN_POINT) * (MAX_STEP - MIN_STEP);
}
else
{
return MAX_STEP;
}
}
// write mouse move event every timeInterval ms
static int timeInterval = 10;
static void setIsMoving(int index, int value)
{
isMoving[index] = value;
if (!value)
{
moveTimes[index] = 0;
}
}
static void keyAction(int index, int value)
{
ENSURE(index >= 0 && index < DIRECTION_NUM);
if (value == 1)
{
setIsMoving(index, true);
}
else if (value == 0)
{
setIsMoving(index, false);
}
else
{
// do nothing
}
}
static void handleKeyEvent(const struct input_event &ev)
{
if (numlockOn)
{
return;
}
int index = -1;
switch (ev.code)
{
case KEY_KP8: index = UP; break;
case KEY_KP2: index = DOWN; break;
case KEY_KP4: index = LEFT; break;
case KEY_KP6: index = RIGHT; break;
default: index = -1; break;
}
if (index != -1)
{
keyAction(index, ev.value);
}
}
static void handleLEDEvent(const struct input_event &ev)
{
if (ev.code == LED_NUML)
{
if (ev.value == 0)
{
numlockOn = false;
}
else if (ev.value == 1)
{
numlockOn = true;
for (int i = 0; i < DIRECTION_NUM; i++)
{
setIsMoving(i, false);
}
}
}
}
static void handleEvent(const struct input_event &ev)
{
if (ev.type == EV_KEY)
{
handleKeyEvent(ev);
}
else if (ev.type == EV_LED)
{
handleLEDEvent(ev);
}
}
static void handleDevice(int fd)
{
struct input_event ev[64];
int rd = read(fd, ev, sizeof(ev));
ENSURE(rd % sizeof(struct input_event) == 0, "expected %d bytes, got %d\n", (int) sizeof(struct input_event), rd % (int) sizeof(struct input_event));
int n = rd / sizeof(struct input_event);
for (int j = 0; j < n; j++)
{
handleEvent(ev[j]);
}
}
static void interrupt_handler(int sig)
{
(void) sig;
shouldStop = true;
}
static bool queryNumlock()
{
std::string ledDevice = getOneNumLockDevice();
// if there is no led device, we suppose that numlock is always off
if (ledDevice == std::string(""))
{
return false;
}
FileOpener opener(ledDevice.c_str(), O_RDONLY);
int fd = opener.getFd();
ENSURE(fd >= 0);
unsigned long supportLED[nbytes(LED_MAX)];
ioctl(fd, EVIOCGLED(sizeof(supportLED)), supportLED);
if (testBits(LED_NUML, supportLED))
{
return true;
}
else
{
return false;
}
}
static void incMoveTimes()
{
for (int i = 0; i < DIRECTION_NUM; i++)
{
if (isMoving[i])
{
moveTimes[i]++;
}
}
}
static void calcMoveSteps(int *steps)
{
for (int i = 0; i < DIRECTION_NUM; i++)
{
if (isMoving[i])
{
steps[i] = moveStep(moveTimes[i]);
}
}
// if opposite key is pressed, we set the steps all to 0
if (isMoving[UP] && isMoving[DOWN])
{
steps[UP] = 0;
steps[DOWN] = 0;
}
if (isMoving[LEFT] && isMoving[RIGHT])
{
steps[LEFT] = 0;
steps[RIGHT] = 0;
}
}
// ok, let's write device to move the mouse
static void writeDevice(int *steps)
{
int x = steps[RIGHT] - steps[LEFT];
int y = steps[DOWN] - steps[UP];
struct input_event ev[3];
memset(ev, 0, sizeof(ev));
ev[0].type = EV_REL;
ev[0].code = REL_X;
ev[0].value = x;
ev[1].type = EV_REL;
ev[1].code = REL_Y;
ev[1].value = y;
ev[2].type = EV_SYN;
ev[2].code = SYN_REPORT;
int ret = write(uinputFd, ev, sizeof(ev));
ENSURE(ret == sizeof(ev));
}
static void moveMouse()
{
incMoveTimes();
int steps[DIRECTION_NUM] = { 0 };
calcMoveSteps(steps);
writeDevice(steps);
}
static void *moveMouseThread(void *arg)
{
(void) arg;
while(true)
{
moveMouse();
usleep(timeInterval * 1000);
}
return nullptr;
}
int main()
{
uinputFd = open(UINPUT_NAME, O_WRONLY);
ENSURE(uinputFd >= 0);
createVirtualMouse(uinputFd);
numlockOn = queryNumlock();
std::vector<std::string> kbds = getAllValidKeyboard();
ENSURE(kbds.size() >= 1, "There should be at least one keyboard");
struct pollfd fds[kbds.size()];
memset(fds, 0, sizeof(fds));
for (unsigned int i = 0; i < kbds.size(); i++)
{
fds[i].fd = open(kbds[i].c_str(), O_RDONLY);
ENSURE(fds[i].fd >= 0, "Open keyboard %s failed", kbds[i].c_str());
fds[i].events = POLLIN;
}
signal(SIGINT, interrupt_handler);
signal(SIGTERM, interrupt_handler);
pthread_t tid;
int ret = pthread_create(&tid, nullptr, moveMouseThread, nullptr);
ENSURE(ret == 0);
resetMoveTimes();
while (!shouldStop)
{
for (unsigned int i = 0; i < kbds.size(); i++)
{
fds[i].revents = 0;
}
poll(fds, kbds.size(), -1);
for (unsigned int i = 0; i < kbds.size(); i++)
{
if (fds[i].revents & POLLIN)
{
handleDevice(fds[i].fd);
}
}
}
for (unsigned int i = 0; i < kbds.size(); i++)
{
close(fds[i].fd);
}
return 0;
}