-
Notifications
You must be signed in to change notification settings - Fork 0
/
uinputdev.cpp
107 lines (86 loc) · 2.39 KB
/
uinputdev.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
#include <fcntl.h>
#include <errno.h>
#include <sys/time.h>
#include <unistd.h>
#include <cstring>
#include <linux/uinput.h>
#include <linux/input.h>
#include "uinputdev.h"
UinputDevice::UinputDevice()
{
}
bool UinputDevice::openDev(const std::string& uinputPath, const std::string& name, uint16_t vendor, uint16_t product)
{
fd = open(uinputPath.c_str(), O_RDWR);
if(fd < 0) return false;
struct uinput_setup dev = {0};
if (name.length() <= UINPUT_MAX_NAME_SIZE) strcpy(dev.name, name.c_str());
else strcpy(dev.name, name.substr(0, UINPUT_MAX_NAME_SIZE).c_str());
dev.id.bustype = BUS_VIRTUAL;
dev.id.vendor = vendor;
dev.id.product = product;
ioctl(fd, UI_SET_EVBIT, EV_ABS);
ioctl(fd, UI_SET_EVBIT, EV_KEY);
ioctl(fd, UI_SET_ABSBIT, ABS_X);
ioctl(fd, UI_SET_ABSBIT, ABS_Y);
ioctl(fd, UI_SET_ABSBIT, ABS_Z);
ioctl(fd, UI_SET_KEYBIT, BTN_TRIGGER);
ioctl(fd, UI_SET_KEYBIT, BTN_THUMB);
struct uinput_abs_setup devAbsX = {0};
devAbsX.code = ABS_X;
devAbsX.absinfo.minimum = -512;
devAbsX.absinfo.maximum = 512;
devAbsX.absinfo.flat = 5;
devAbsX.absinfo.fuzz = 2;
if(ioctl(fd, UI_ABS_SETUP, &devAbsX) < 0) return false;
struct uinput_abs_setup devAbsY = {0};
devAbsY.code = ABS_Y;
devAbsY.absinfo.minimum = -512;
devAbsY.absinfo.maximum = 512;
devAbsY.absinfo.flat = 5;
devAbsY.absinfo.fuzz = 0;
if(ioctl(fd, UI_ABS_SETUP, &devAbsY) < 0) return false;
struct uinput_abs_setup devAbsZ = {0};
devAbsZ.code = ABS_Z;
devAbsZ.absinfo.minimum = -512;
devAbsZ.absinfo.maximum = 512;
devAbsZ.absinfo.flat = 5;
devAbsZ.absinfo.fuzz = 0;
if(ioctl(fd, UI_ABS_SETUP, &devAbsZ) < 0) return false;
if(ioctl(fd, UI_DEV_SETUP, &dev) < 0) return false;
if(ioctl(fd, UI_DEV_CREATE) < 0) return false;
return true;
}
bool UinputDevice::sendAbs(int x, int y, int z)
{
if(fd < 0) return false;
struct input_event ev = {0};
ev.type = EV_ABS;
ev.code = ABS_X;
ev.value = x;
if(write(fd, &ev, sizeof(ev)) != sizeof(ev))
return false;
ev.code = ABS_Y;
ev.value = y;
if(write(fd, &ev, sizeof(ev)) != sizeof(ev))
return false;
ev.code = ABS_Z;
ev.value = z;
if(write(fd, &ev, sizeof(ev)) != sizeof(ev))
return false;
gettimeofday(&ev.time, NULL);
ev.type = EV_SYN;
ev.code = SYN_REPORT;
ev.value = 0;
if(write(fd, &ev, sizeof(ev)) != sizeof(ev))
return false;
return true;
}
UinputDevice::~UinputDevice()
{
if(fd > 0)
{
ioctl(fd, UI_DEV_DESTROY);
close(fd);
}
}