-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuinput.c
More file actions
174 lines (153 loc) · 5.09 KB
/
Copy pathuinput.c
File metadata and controls
174 lines (153 loc) · 5.09 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/uinput.h>
#include "uinput.h"
#include "remote.h"
#include "utils.h"
static void
debug_event (struct input_event *ev)
{
int size;
int i;
size = sizeof(struct input_event);
printf("=>");
for (i=size - 2; i>=0; i-=2) {
printf(" 0x%04x", *((uint16_t*)((uint8_t*)ev+i)));
}
putchar('\n');
}
static int
uinput_write (int fd, struct input_event *ev, size_t size)
{
ssize_t bytes;
if ((bytes = write(fd, ev, size)) < size) {
if (bytes < 0) {
perror("write");
return -1;
}
fprintf(stderr, "uinput_write: unable to write entire struct "
"(sent %zd of %zd bytes)\n", bytes, size);
return -1;
}
return 0;
}
int
uinput_open (void)
{
int fd;
struct uinput_user_dev uidev;
if ((fd = open("/dev/uinput", O_WRONLY)) < 0) {
perror("uinput_open");
return -1;
}
if (ioctl(fd, UI_SET_EVBIT, EV_SYN) < 0) {
perror("UI_SET_EVBIT");
goto uinput_open_error;
}
if (ioctl(fd, UI_SET_EVBIT, EV_KEY) < 0) {
perror("UI_SET_EVBIT");
goto uinput_open_error;
}
/*
* KEY_OK needs to be set for XBMC to receive any keypresses. XBMC
* determines input device type from the list of enabled keys. Unless
* a key at or above KEY_OK is enabled, XBMC will mishandle the remote.
* You don't need to map anything to these keys to solve the XBMC
* problem.
*/
if (ioctl(fd, UI_SET_KEYBIT, KEY_OK) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_FASTFORWARD) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_NEXTSONG) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_PAUSE) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_PLAY) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_PREVIOUSSONG) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_REWIND) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_STOP) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_ENTER) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_UP) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_RIGHT) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_DOWN) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_LEFT) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_0) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_1) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_2) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_3) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_4) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_5) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_6) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_7) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_8) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_9) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_A) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_B) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_D) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_G) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_H) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_I) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_L) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_M) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_N) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_O) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_P) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_R) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_S) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_U) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_V) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_X) < 0) goto keybit_error;
if (ioctl(fd, UI_SET_KEYBIT, KEY_Y) < 0) goto keybit_error;
memset(&uidev, 0, sizeof uidev);
snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, UREMOTE_NAME);
uidev.id.bustype = BUS_VIRTUAL;
uidev.id.vendor = VENDOR_ID;
uidev.id.product = PRODUCT_ID;
uidev.id.version = 1;
if (write(fd, &uidev, sizeof(uidev)) < 0) {
perror("write");
goto uinput_open_error;
}
if (ioctl(fd, UI_DEV_CREATE) < 0) {
perror("UI_DEV_CREATE");
goto uinput_open_error;
}
return fd;
keybit_error:
perror("UI_SET_KEYBIT");
uinput_open_error:
close(fd);
return -1;
}
int
uinput_close (int fd)
{
if (ioctl(fd, UI_DEV_DESTROY) < 0) {
perror("UI_DEV_DESTROY");
}
return close(fd);
}
int
uinput_sendkey (int fd, uint16_t key, int32_t value)
{
struct input_event ev[2];
memset(&ev, 0, sizeof ev);
ev[0].type = EV_KEY;
ev[0].code = key;
ev[0].value = value;
ev[1].type = EV_SYN;
ev[1].code = SYN_REPORT;
debug_puts("Sending EV_KEY event to uinput");
DEBUG_FN(debug_event(&(ev[0])));
uinput_write(fd, &ev[0], sizeof ev[0]);
debug_puts("Sending EV_SYN event to uinput");
DEBUG_FN(debug_event(&(ev[1])));
uinput_write(fd, &ev[1], sizeof ev[1]);
return 0;
/*
* Unworking code, sends the EV_KEY and EV_SYN together. On RPi, only
* every eigth EV_SYN is received.
*/
uinput_write(fd, ev, sizeof ev);
return 0;
}