-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathhandlers.c
289 lines (247 loc) · 5.46 KB
/
handlers.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
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
/*
* Copyright (C) 2007 Olli Salonen <[email protected]>
* see btnx-config.c for detailed license information
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/select.h>
#include <linux/input.h>
#include <linux/uinput.h>
#include "handlers.h"
#define NUM_HANDLER_LOCATIONS 3
const char handler_locations[][15] =
{
{"/dev"},
{"/dev/input"},
{"/dev/misc"}
};
static handler **handlers=NULL;
static int num_handlers=0;
int handlers_add(char *path)
{
int fd;
unsigned short id[6];
if ((fd = open(path, O_RDONLY)) < 0)
{
fprintf(stderr, "Error: handler_add event handler open failed: %s: %s\n", path, strerror(errno));
return -1;
}
if (num_handlers == 0)
{
handlers = (struct handler **) malloc(sizeof(handler*));
}
else
{
handlers = (handler **) realloc(handlers, (num_handlers+1)*sizeof(handler*));
}
handlers[num_handlers] = (handler *) malloc(sizeof(handler));
if (handlers[num_handlers] == NULL || handlers == NULL)
{
fprintf(stderr, "Error, handler_add handler allocation failed: %s\n", strerror(errno));
close(fd);
return -1;
}
ioctl(fd, EVIOCGID, id);
handlers[num_handlers]->vendor = id[ID_VENDOR];
handlers[num_handlers]->product = id[ID_PRODUCT];
handlers[num_handlers]->fd = -1;
handlers[num_handlers]->open = 0;
handlers[num_handlers]->path = (char *) malloc((strlen(path)+1) * sizeof(char));
handlers[num_handlers]->vendor_name = NULL;
handlers[num_handlers]->product_name = NULL;
if (handlers[num_handlers]->path == NULL)
{
fprintf(stderr, "Error: handler_add path allocation failed: %s\n", strerror(errno));
close(fd);
free(handlers[num_handlers]);
return -1;
}
strcpy(handlers[num_handlers]->path, path);
close(fd);
num_handlers++;
return num_handlers-1;
}
const char *handlers_get_location(int index)
{
if (index < 0 || index > NUM_HANDLER_LOCATIONS - 1)
return NULL;
return handler_locations[index];
}
int handlers_add_by_ids(int vendor, int product)
{
const char *loc;
int x=0, i=0, fd;
char loc_buffer[128];
unsigned short id[6];
while ((loc = handlers_get_location(x++)) != NULL)
{
for (i=0; i<20; i++)
{
sprintf(loc_buffer, "%s/%s%d", loc, "event", i);
if ((fd = open(loc_buffer, O_RDONLY)) >= 0)
{
//printf("Opened handler: %s\n", loc_buffer);
ioctl(fd, EVIOCGID, id);
if (vendor == id[ID_VENDOR] && product == id[ID_PRODUCT])
handlers_add(loc_buffer);
close(fd);
}
}
}
return num_handlers;
}
void handlers_free(void)
{
int i;
if (handlers != NULL && num_handlers > 0)
{
for (i=0; i<num_handlers; i++)
{
if (handlers[i] != NULL)
{
if (handlers[i]->path != NULL)
{
free(handlers[i]->path);
}
if (handlers[i]->vendor_name != NULL)
{
free(handlers[i]->vendor_name);
}
if (handlers[i]->product_name != NULL)
{
free(handlers[i]->product_name);
}
if (handlers[i]->open == 1 && handlers[i]->fd >=0)
{
close(handlers[i]->fd);
}
free(handlers[i]);
}
}
free(handlers);
}
num_handlers = 0;
handlers = NULL;
}
int handlers_count(void)
{
return num_handlers;
}
int handlers_check_index(int index)
{
if (index < num_handlers && index >= 0)
return index;
//fprintf(stderr, "Error: invalid index(%d) passed to handlers_check_index.\n", index);
return -1;
}
int handlers_get_product(int index)
{
if (handlers_check_index(index) >= 0)
return handlers[index]->product;
return 0;
}
int handlers_get_vendor(int index)
{
if (handlers_check_index(index) >= 0)
return handlers[index]->vendor;
return 0;
}
int handlers_get_fd(int index)
{
if (handlers_check_index(index) >= 0)
return handlers[index]->fd;
return 0;
}
char *handlers_get_vendor_name(int index)
{
if (handlers_check_index(index) >= 0)
return handlers[index]->vendor_name;
return 0;
}
char *handlers_get_product_name(int index)
{
if (handlers_check_index(index) >= 0)
return handlers[index]->product_name;
return 0;
}
int handlers_open(int index)
{
if (handlers[index]->open == 1)
return handlers[index]->fd;
if ((handlers[index]->fd = open(handlers[index]->path, O_RDONLY)) < 0)
{
fprintf(stderr, "Error: handler_open event handler open failed: %s\n", strerror(errno));
return -1;
}
handlers[index]->open = 1;
return handlers[index]->fd;
}
int handlers_open_all(void)
{
int i;
for (i=0; i<num_handlers; i++)
{
if (handlers_open(i) < 0)
return -1;
}
return 0;
}
void handlers_close(int index)
{
if (handlers_check_index(index) >= 0)
{
if (handlers[index]->open == 1)
close(handlers[index]->fd);
handlers[index]->open = 0;
handlers[index]->fd = -1;
}
}
void handlers_close_all(void)
{
int i;
for (i=0; i<num_handlers; i++)
{
handlers_close(i);
}
}
int handlers_check_id_conflicts(void)
{
int product, vendor, i;
if (num_handlers < 1)
return 0;
product = handlers[0]->product;
vendor = handlers[0]->vendor;
for (i=0; i<num_handlers; i++)
{
if (handlers[i]->vendor != vendor || handlers[i]->product != product)
return -1;
}
return 0;
}
int handlers_zero_and_fill_fds(fd_set *fds)
{
int i, max_fd=-1;
FD_ZERO(fds);
for (i=0; i<num_handlers; i++)
{
if (handlers[i]->open == 1)
{
FD_SET(handlers[i]->fd, fds);
if (handlers[i]->fd > max_fd)
max_fd = handlers[i]->fd;
}
}
return max_fd;
}
int handlers_isset(fd_set *fds, int i)
{
if (handlers_check_index(i) == -1)
return -1;
if (handlers[i]->fd < 0 || handlers[i]->open == 0)
return 0;
return FD_ISSET(handlers[i]->fd, fds);
}