/
suinput.c
executable file
·225 lines (179 loc) · 6.19 KB
/
suinput.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
/*
suinput - Simple C-API to the Linux uinput-system.
Copyright (C) 2009 Tuomas Räsänen <tuos@codegrove.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/uinput.h>
#include <stdio.h>
#include "suinput.h"
char* UINPUT_FILEPATHS[] = {
"/android/dev/uinput",
"/dev/uinput",
"/dev/input/uinput",
"/dev/misc/uinput",
};
#define UINPUT_FILEPATHS_COUNT (sizeof(UINPUT_FILEPATHS) / sizeof(char*))
int suinput_write(int uinput_fd,
uint16_t type, uint16_t code, int32_t value)
{
struct input_event event;
memset(&event, 0, sizeof(event));
gettimeofday(&event.time, 0); /* This should not be able to fail ever.. */
event.type = type;
event.code = code;
event.value = value;
if (write(uinput_fd, &event, sizeof(event)) != sizeof(event))
return -1;
return 0;
}
int suinput_write_syn(int uinput_fd,
uint16_t type, uint16_t code, int32_t value)
{
if (suinput_write(uinput_fd, type, code, value))
return -1;
return suinput_write(uinput_fd, EV_SYN, SYN_REPORT, 0);
}
int suinput_open(const char* device_name, const struct input_id* id)
{
int original_errno = 0;
int uinput_fd = -1;
struct uinput_user_dev user_dev;
int i;
for (i = 0; i < UINPUT_FILEPATHS_COUNT; ++i) {
uinput_fd = open(UINPUT_FILEPATHS[i], O_WRONLY | O_NONBLOCK);
if (uinput_fd != -1)
break;
}
if (uinput_fd == -1)
return -1;
/* Set device to handle following types of events: */
/* Key and button events */
if (ioctl(uinput_fd, UI_SET_EVBIT, EV_KEY) == -1)
goto err;
//
// /* Key and button repetition events */
if (ioctl(uinput_fd, UI_SET_EVBIT, EV_REP) == -1)
goto err;
//
// /* Relative pointer motions */
// if (ioctl(uinput_fd, UI_SET_EVBIT, EV_REL) == -1)
// goto err;
/* Absolute pointer motions */
if (ioctl(uinput_fd, UI_SET_EVBIT, EV_ABS) == -1)
goto err;
/* Synchronization events, this is probably set implicitely too. */
if (ioctl(uinput_fd, UI_SET_EVBIT, EV_SYN) == -1)
goto err;
/* Configure device to handle relative x and y axis. */
// if (ioctl(uinput_fd, UI_SET_RELBIT, REL_X) == -1)
// goto err;
// if (ioctl(uinput_fd, UI_SET_RELBIT, REL_Y) == -1)
// goto err;
/* Configure device to handle absolute x and y axis. */
if (ioctl(uinput_fd, UI_SET_ABSBIT, ABS_X) == -1)
goto err;
if (ioctl(uinput_fd, UI_SET_ABSBIT, ABS_Y) == -1)
goto err;
/* Configure device to handle all keys, see linux/input.h. */
for (i = 0; i < KEY_MAX; i++) {
if (ioctl(uinput_fd, UI_SET_KEYBIT, i) == -1)
goto err;
}
/* Set device-specific information. */
memset(&user_dev, 0, sizeof(user_dev));
strncpy(user_dev.name, device_name, UINPUT_MAX_NAME_SIZE);
user_dev.id.bustype = id->bustype;
user_dev.id.vendor = id->vendor;
user_dev.id.product = id->product;
user_dev.id.version = id->version;
//minor tweak to support ABSolute events
user_dev.absmin[ABS_X] = -2047;
user_dev.absmax[ABS_X] = 2048;
user_dev.absfuzz[ABS_X] = 0;
user_dev.absflat[ABS_X] = 0;
user_dev.absmin[ABS_Y] = -2047;
user_dev.absmax[ABS_Y] = 2048;
user_dev.absfuzz[ABS_Y] = 0;
user_dev.absflat[ABS_Y] = 0;
if (write(uinput_fd, &user_dev, sizeof(user_dev)) != sizeof(user_dev))
goto err;
if (ioctl(uinput_fd, UI_DEV_CREATE) == -1)
goto err;
/*
The reason for generating a small delay is that creating succesfully
an uinput device does not guarantee that the device is ready to process
input events. It's probably due the asynchronous nature of the udev.
However, my experiments show that the device is not ready to process input
events even after a device creation event is received from udev.
*/
//sleep(2);
return uinput_fd;
err:
/*
At this point, errno is set for some reason. However, cleanup-actions
can also fail and reset errno, therefore we store the original one
and reset it before returning.
*/
original_errno = errno;
/* Cleanup. */
close(uinput_fd); /* Might fail, but we don't care anymore at this point. */
errno = original_errno;
return -1;
}
int suinput_close(int uinput_fd)
{
/*
Sleep before destroying the device because there still can be some
unprocessed events. This is not the right way, but I am still
looking for better ways. The question is: how to know whether there
are any unprocessed uinput events?
*/
sleep(2);
if (ioctl(uinput_fd, UI_DEV_DESTROY) == -1) {
close(uinput_fd);
return -1;
}
if (close(uinput_fd) == -1)
return -1;
return 0;
}
int suinput_move_pointer(int uinput_fd, int32_t x, int32_t y)
{
if (suinput_write(uinput_fd, EV_REL, REL_X, x))
return -1;
return suinput_write_syn(uinput_fd, EV_REL, REL_Y, y);
}
int suinput_set_pointer(int uinput_fd, int32_t x, int32_t y)
{
if (suinput_write(uinput_fd, EV_ABS, ABS_X, x))
return -1;
return suinput_write_syn(uinput_fd, EV_ABS, ABS_Y, y);
}
int suinput_press(int uinput_fd, uint16_t code)
{
return suinput_write(uinput_fd, EV_KEY, code, 1);
}
int suinput_release(int uinput_fd, uint16_t code)
{
return suinput_write(uinput_fd, EV_KEY, code, 0);
}
int suinput_click(int uinput_fd, uint16_t code)
{
if (suinput_press(uinput_fd, code))
return -1;
return suinput_release(uinput_fd, code);
}