forked from fybmain/ONScripter-Jh
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuinput.cpp
126 lines (106 loc) · 3.2 KB
/
uinput.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
#include <sys/ioctl.h>
#include <linux/uinput.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include "uinput.h"
#define ALOGE(...) \
printf(__VA_ARGS__); \
printf("\n")
#define UINPUT_NAME "/dev/uinput"
#define VIR_MOUSE_NAME "DFC Virtual Mouse"
#include "ONScripter.h"
extern ONScripter ons;
static int device_handler = -1;
static struct uinput_user_dev vir_mouse;
static int install_uinput_mouse_device()
{
if (device_handler > 0)
{
ALOGE("virtual mouse has been installed already.");
return device_handler;
}
//open uinput device
if ((device_handler = open(UINPUT_NAME, O_WRONLY | O_NDELAY)) <= 0)
{
ALOGE("open uinput(%s) failed.", UINPUT_NAME);
device_handler = -1;
return device_handler;
}
//setup our uinput device of virtual mouse
memset(&vir_mouse, 0, sizeof(struct uinput_user_dev));
strncpy(vir_mouse.name, VIR_MOUSE_NAME, UINPUT_MAX_NAME_SIZE);
vir_mouse.id.version = 4;
vir_mouse.id.bustype = BUS_USB;
//if we don't do these register, it seems like not to work
ioctl(device_handler, UI_SET_EVBIT, EV_KEY);
for (int i = 0; i < 256; i++)
{
ioctl(device_handler, UI_SET_KEYBIT, i);
}
//setup mouse coordinate event
ioctl(device_handler, UI_SET_EVBIT, EV_REL);
ioctl(device_handler, UI_SET_RELBIT, REL_X);
ioctl(device_handler, UI_SET_RELBIT, REL_Y);
//setup mouse button event
ioctl(device_handler, UI_SET_KEYBIT, BTN_MOUSE);
ioctl(device_handler, UI_SET_KEYBIT, BTN_TOUCH);
ioctl(device_handler, UI_SET_KEYBIT, BTN_LEFT);
ioctl(device_handler, UI_SET_KEYBIT, BTN_RIGHT);
ioctl(device_handler, UI_SET_KEYBIT, BTN_MIDDLE);
//create input device into input subsystem
write(device_handler, &vir_mouse, sizeof(vir_mouse));
if (ioctl(device_handler, UI_DEV_CREATE))
{
ALOGE("install mouse uinput device failed with error(%s).", strerror(errno));
close(device_handler);
device_handler = -1;
}
return device_handler;
}
void uninstall_uinput_mouse_device()
{
if (device_handler > 0)
{
ioctl(device_handler, UI_DEV_DESTROY);
close(device_handler);
device_handler = ERROR_DEVICE_HANDLER;
}
}
void mouse_move(struct deltaData* data)
{
if (device_handler <= 0)
{
install_uinput_mouse_device();
}
if (device_handler > 0)
{
struct input_event event;
memset(&event, 0, sizeof(event));
//x coordinate
gettimeofday(&event.time, 0);
event.type = EV_REL;
event.code = REL_X;
event.value = data->x;
//lastX+=data->x;
write(device_handler, &event, sizeof(event));
//y coordinate
event.type = EV_REL;
event.code = REL_Y;
event.value = data->y;
//lastY+=data->y;
write(device_handler, &event, sizeof(event));
//execute move event
event.type = EV_SYN;
event.code = SYN_REPORT;
event.value = 0;
write(device_handler, &event, sizeof(event));
}
else
{
ALOGE("invalid device file handler.");
}
}