-
Notifications
You must be signed in to change notification settings - Fork 9
/
lgInputDevice.cpp
372 lines (315 loc) · 9.48 KB
/
lgInputDevice.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
/*
* Copyright (c) 2021 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <err.h>
#include <linux/input.h>
#include <linux/uinput.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/poll.h>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <thread>
#include <errno.h>
#include "sendKey.h"
#include "lgInputDevice.h"
lgInputDevice::lgInputDevice()
{
type = 0;
ufd = 0;
mqId = 0;
virt = false;
sourceDev.count = 0;
mouseType = 0;
fd[MAX_DEV - 1] = {0};
}
void lgInputDevice::setVirtMode(bool mode)
{
virt = mode;
}
bool lgInputDevice::getVirtMode()
{
return virt;
}
lgInputDevice::~lgInputDevice()
{
if (ufd > 0)
close(ufd);
for (int i = 0; i < sourceDev.count; i++)
if (fd[i] > 0)
close(fd[i]);
if (mqId > 0) {
msgctl(mqId, IPC_RMID, NULL);
}
}
int lgInputDevice::getDevices(uint16_t keyCode, struct device *dev)
{
const ssize_t keyBitsSize = (KEY_MAX / 8) + 1;
dev->count = 0;
if((keyCode / 8) >= keyBitsSize)
return dev->count;
for (int i = 0; i < MAX_DEV; i++) {
int fd = 0, res = 0;
unsigned int k = 0;
char devPath[70] = " ";
snprintf(devPath, sizeof(devPath), "/dev/input/event%d", i);
fd = open(devPath, O_RDONLY);
if (fd < 0)
continue;
res = ioctl(fd, EVIOCGBIT(0, sizeof(k)), &k);
if (res < 0) {
close(fd);
continue;
}
if (k & (1 << EV_KEY)) {
uint8_t keyBits[keyBitsSize] = {};
res = ioctl(fd, EVIOCGBIT(EV_KEY, keyBitsSize), keyBits);
if (res < 0) {
close(fd);
continue;
}
if (keyBits[keyCode/8] & (1 << (keyCode % 8))) {
dev->path[dev->count] = "/dev/input/event" + to_string(i);
dev->count++;
}
}
close(fd);
}
return dev->count;
}
int lgInputDevice::openSourceDev()
{
for (int i = 0; i < sourceDev.count; i++) {
const char *devPath = sourceDev.path[i].c_str();
if ((fd[i] = open(devPath, O_RDONLY | O_CLOEXEC)) < 0)
return -1;
}
return 0;
}
int lgInputDevice::openUinputDev()
{
if ((ufd = open("/dev/uinput", O_WRONLY | O_SYNC)) < 0) {
cout << "Failed to open uinput device" << endl;
return -1;
}
return 0;
}
int lgInputDevice::createInputDevice(uint16_t keyCode, bool gvtdMode)
{
struct input_id uid = {};
struct uinput_setup usetup = {};
if (gvtdMode == true) {
if (!getDevices(keyCode, &sourceDev))
setVirtMode(true);
else
setVirtMode(false);
} else {
setVirtMode(true);
}
if (openUinputDev() < 0) {
cout << "Failed to open uinput device" << endl;
return -1;
}
char str[100];
sprintf(str, "input-looking-glass%d", mouseType);
uInputName = str;
struct uinput_user_dev dev;
ioctl(ufd, UI_SET_EVBIT, EV_KEY);
ioctl(ufd, UI_SET_EVBIT, EV_ABS);
ioctl(ufd, UI_SET_EVBIT, EV_SYN);
ioctl(ufd, UI_SET_ABSBIT, ABS_X);
ioctl(ufd, UI_SET_ABSBIT, ABS_Y);
ioctl(ufd, UI_SET_ABSBIT, ABS_MT_SLOT);
ioctl(ufd, UI_SET_ABSBIT, ABS_MT_TOUCH_MAJOR);
ioctl(ufd, UI_SET_ABSBIT, ABS_MT_POSITION_X);
ioctl(ufd, UI_SET_ABSBIT, ABS_MT_POSITION_Y);
ioctl(ufd, UI_SET_ABSBIT, ABS_MT_TRACKING_ID);
ioctl(ufd, UI_SET_ABSBIT, ABS_MT_TOOL_TYPE);
ioctl(ufd, UI_SET_PROPBIT, INPUT_PROP_DIRECT);
ioctl(ufd, UI_SET_KEYBIT, BTN_TOUCH);
memset(&dev, 0, sizeof(dev));
snprintf(dev.name, sizeof(dev.name), "IntelvTouch");
sprintf(dev.name, "input-looking-glass%d", mouseType);
dev.id.bustype = BUS_USB;
dev.id.vendor = 0x1;
dev.id.product = 0x1;
dev.id.version = 1;
signed int touch_xres = 540;
signed int touch_yres = 960;
/* single touch inputs */
dev.absmax[ABS_MT_SLOT] = 0;
dev.absmin[ABS_X] = 0;
dev.absmax[ABS_X] = touch_xres;
dev.absmin[ABS_Y] = 0;
dev.absmax[ABS_Y] = touch_yres;
dev.absmin[ABS_MT_POSITION_X] = 0;
dev.absmax[ABS_MT_POSITION_X] = touch_xres;
dev.absmin[ABS_MT_POSITION_Y] = 0;
dev.absmax[ABS_MT_POSITION_Y] = touch_yres;
dev.absmin[ABS_PRESSURE] = 0;
dev.absmax[ABS_PRESSURE] = 0xff;
dev.absmin[ABS_MT_TOUCH_MAJOR] = 0;
dev.absmax[ABS_MT_TOUCH_MAJOR] = 0xff;
dev.absmin[ABS_MT_PRESSURE] = 0;
dev.absmax[ABS_MT_PRESSURE] = 0xff;
write(ufd, &dev, sizeof(dev));
ioctl(ufd, UI_DEV_CREATE);
type = MOUSE;
return 0;
}
int lgInputDevice::createSymLink()
{
for (int i = 0; i < MAX_DEV; i++) {
char name[100] = " ";
string str;
ifstream infile;
snprintf(name, sizeof(name), "/sys/class/input/event%d/device/name", i);
infile.open(name);
if (!infile.is_open())
continue;
getline(infile, str);
if (!strncmp(str.c_str(), uInputName.c_str(), str.size() + 1)) {
char cmd[150];
softLinkPath = "/dev/input/by-id/";
snprintf(cmd, sizeof(cmd), "mkdir -p %s", softLinkPath.c_str());
system(cmd);
softLinkPath.append(uInputName);
std::replace(softLinkPath.begin(), softLinkPath.end(), ' ', '-');
snprintf(cmd, sizeof(cmd), "ln -sf /dev/input/event%d %s",
i, softLinkPath.c_str());
system(cmd);
return 0;
}
infile.close();
}
return -1;
}
void lgInputDevice::sendEvent(uint16_t type, uint16_t code, int32_t value)
{
struct input_event ev = {};
memset(&ev, 0, sizeof(ev));
ev.type = type;
ev.code = code;
ev.value = value;
write(ufd, &ev, sizeof(ev));
}
int lgInputDevice::getMsgQ()
{
key_t key = 0;
char path[100];
char cmd[100];
sprintf(path, "%s%d", MOUSE_BUTTON_FILE_PATH,mouseType);
sprintf(cmd, "touch %s",path);
system(cmd);
key = ftok(path, 99);
mqId = msgget(key, 0666 | IPC_CREAT);
if (mqId < 0) {
cout << "Failed to get msgq id" << endl;
sprintf(cmd, "rm %s",path);
system(cmd);
return -1;
}
/*
* Clean up message queue buffer
*/
struct input_event ev = {};
int n = 1;
while (n > 0) {
/*The msgsz argument contains the size of the message in bytes, excluding the length of the message type (4 byte long).*/
n = msgrcv(mqId, &ev, sizeof(struct input_event) - sizeof(long), 1, IPC_NOWAIT);
}
return mqId;
}
static void sendKeyThread(lgInputDevice *vDev)
{
struct mQData1 mD = {};
lgInputDevice *vD = vDev;
vD->type = MOUSE;
int mqId = vD->getMsgQ();
if (mqId < 0) {
cout << "Failed to get msgq id" << endl;
return ;
}
while (true) {
if (msgrcv(mqId, &mD, sizeof(struct mQData1) - sizeof(long), 1, 0) < 0) {
printf("error no: %s mousetype %d\n", strerror(errno), vD->mouseType);
continue;
}
vD->sendEvent(mD.ev.type, mD.ev.code, mD.ev.value);
}
}
static void realDeviceThread(lgInputDevice *vD)
{
struct pollfd pfd[MAX_DEV] = {};
struct input_event evBuf[10] = {};
int cnt = vD->sourceDev.count;
int res = 0;
for (int i = 0; i < cnt; i++) {
pfd[i].fd = vD->fd[i];
pfd[i].events = POLLIN;
}
while (true) {
if (poll(pfd, cnt, -1) < 0) {
cout << "Failed to poll in input device" << endl;
return;
}
for (int i = 0; i < cnt; i++)
if (pfd[i].revents & POLLIN) {
int j = 0;
while (true) {
res = read(pfd[i].fd, &evBuf[j], sizeof(input_event));
if (res < 0) {
cout << "could not get event" << endl;
break;
}
if ((!evBuf[j].type) && (!evBuf[j].code) &&
(!evBuf[i].value)) {
if ((evBuf[0].code == 116) && (evBuf[0].type == 1) &&
(evBuf[0].value == 1)) {
system("sudo python3 ./wakeup.py");
if (usleep(800000) < 0) {
cout << "Failed to add 800ms delay" << endl;
}
}
for (int k = 0; k <= j; k++) {
vD->sendEvent(evBuf[k].type, evBuf[k].code,
evBuf[k].value);
}
j = 0;
continue;
}
j++;
}
}
}
}
void lgInputDevice::pollAndPushEvents()
{
if (getVirtMode() == true) {
sendKeyThread(this);
} else {
thread sK(sendKeyThread, this);
thread rD(realDeviceThread, this);
sK.join();
rD.join();
}
}