-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathuinput.c
More file actions
executable file
·287 lines (240 loc) · 5.95 KB
/
uinput.c
File metadata and controls
executable file
·287 lines (240 loc) · 5.95 KB
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
/**** uinput.c *****************************/
/* M. Moller 2013-01-16 */
/* Universal RPi GPIO keyboard daemon */
/*******************************************/
/*
Copyright (C) 2013 Michael Moller.
This file is part of the Universal Raspberry Pi GPIO keyboard daemon.
This 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 2.1 of the License, or (at your option) any later version.
The software 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 the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <syslog.h>
#include <linux/input.h>
#include <linux/uinput.h>
//#include "uinput_linux.h"
//#include "config.h"
//include "daemon.h"
#include "uinput.h"
#include <time.h>
//#define DEBUG_UINPUT
#define ERROR_UINPUT
#ifdef DEBUG_UINPUT
#define FK_DEBUG(...) syslog(LOG_DEBUG, __VA_ARGS__);
#else
#define FK_DEBUG(...)
#endif
#ifdef ERROR_UINPUT
#define FK_ERROR(...) syslog(LOG_ERR, __VA_ARGS__);
#else
#define FK_DEBUG(...)
#endif
/* For compatibility with kernels having dates on 32 bits */
struct timeval_compat
{
unsigned int tv_sec;
long int tv_usec;
};
struct input_event_compat {
struct timeval_compat time;
unsigned short type;
unsigned short code;
unsigned int value;
};
#if TEST_UINPUT
static int sendRel(int dx, int dy);
#endif
static int sendSync(void);
#if TEST_UINPUT
static struct input_event uidev_ev;
#endif
static int uidev_fd;
/*static keyinfo_s lastkey;*/
#define die(str, args...) do { \
perror(str); \
return(EXIT_FAILURE); \
} while(0)
int init_uinput(void)
{
int fd;
struct uinput_user_dev uidev;
int i;
fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
if(fd < 0)
die("/dev/uinput");
if(ioctl(fd, UI_SET_EVBIT, EV_KEY) < 0)
die("error: ioctl");
if(ioctl(fd, UI_SET_EVBIT, EV_REP) < 0)
die("error: ioctl");
if(ioctl(fd, UI_SET_KEYBIT, BTN_LEFT) < 0)
die("error: ioctl");
if(ioctl(fd, UI_SET_EVBIT, EV_REL) < 0)
die("error: ioctl");
if(ioctl(fd, UI_SET_RELBIT, REL_X) < 0)
die("error: ioctl");
if(ioctl(fd, UI_SET_RELBIT, REL_Y) < 0)
die("error: ioctl");
/* don't forget to add all the keys! */
for(i=0; i<256; i++){
if(ioctl(fd, UI_SET_KEYBIT, i) < 0)
die("error: ioctl");
}
memset(&uidev, 0, sizeof(uidev));
snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "uinput-sample");
uidev.id.bustype = BUS_USB;
uidev.id.vendor = 0x1;
uidev.id.product = 0x1;
uidev.id.version = 1;
/*if(write(fd, &uidev, sizeof(uidev)) < 0)
die("error: write");*/
if(ioctl(fd, UI_DEV_SETUP, &uidev) < 0)
die("error: ioctl");
if(ioctl(fd, UI_DEV_CREATE) < 0)
die("error: ioctl");
uidev_fd = fd;
sleep(1);
return 0;
}
#if TEST_UINPUT
int test_uinput(void)
{
int dx, dy;
int i;
//srand(time(NULL));
int op = 1;
while(1) {
//switch(rand() % 4) {
switch(op) {
case 0:
dx = -10;
dy = -1;
break;
case 1:
dx = 10;
dy = 1;
break;
case 2:
dx = -1;
dy = 10;
break;
case 3:
dx = 1;
dy = -10;
break;
}
/*k = send_gpio_keys(21, 1);
sendKey(k, 0);*/
sendKey(KEY_D, 0);
for(i = 0; i < 20; i++) {
//sendKey(KEY_D, 1);
//sendKey(KEY_D, 0);
sendRel(dx, dy);
usleep(15000);
}
sleep(10);
}
}
#endif
int close_uinput(void)
{
sleep(2);
if(ioctl(uidev_fd, UI_DEV_DESTROY) < 0)
die("error: ioctl");
close(uidev_fd);
return 0;
}
int sendKey(int key, int value)
{
struct input_event_compat ie;
//memset(&uidev_ev, 0, sizeof(struct input_event));
//gettimeofday(&uidev_ev.time, NULL);
ie.type = EV_KEY;
ie.code = key;
ie.value = value;
ie.time.tv_sec = 0;
ie.time.tv_usec = 0;
FK_DEBUG("sendKey: %d = %d\n", key, value);
if(write(uidev_fd, &ie, sizeof(struct input_event_compat)) < 0)
die("error: write");
sendSync();
return 0;
}
#if TEST_UINPUT
static int sendRel(int dx, int dy)
{
memset(&uidev_ev, 0, sizeof(struct input_event));
uidev_ev.type = EV_REL;
uidev_ev.code = REL_X;
uidev_ev.value = dx;
if(write(uidev_fd, &uidev_ev, sizeof(struct input_event)) < 0)
die("error: write");
memset(&uidev_ev, 0, sizeof(struct input_event));
uidev_ev.type = EV_REL;
uidev_ev.code = REL_Y;
uidev_ev.value = dy;
if(write(uidev_fd, &uidev_ev, sizeof(struct input_event)) < 0)
die("error: write");
sendSync();
return 0;
}
#endif
static int sendSync(void)
{
FK_DEBUG("sendSync\n");
//memset(&uidev_ev, 0, sizeof(struct input_event));
struct input_event ie;
ie.type = EV_SYN;
ie.code = SYN_REPORT;
ie.value = 0;
ie.time.tv_sec = 0;
ie.time.tv_usec = 0;
if(write(uidev_fd, &ie, sizeof(struct input_event)) < 0)
die("error: sendSync - write");
return 0;
}
#if 0
int send_gpio_keys(int gpio, int value)
{
int k;
int xio;
restart_keys();
while( got_more_keys(gpio) ){
k = get_next_key(gpio);
if(is_xio(gpio) && value){ /* xio int-pin is active low */
xio = get_curr_xio_no();
poll_iic(xio);
last_iic_key(&lastkey);
}
else if(k<0x300){
sendKey(k, value);
if(value && got_more_keys(gpio)){
/* release the current key, so the next one can be pressed */
sendKey(k, 0);
}
lastkey.key = k;
lastkey.val = value;
}
}
return k;
}
void get_last_key(keyinfo_s *kp)
{
kp->key = lastkey.key;
kp->val = lastkey.val;
}
#endif