Skip to content

Latest commit

 

History

History
339 lines (276 loc) · 8.78 KB

linux_evdev.c

File metadata and controls

339 lines (276 loc) · 8.78 KB
 
1
2
3
/*
* Support for Linux evdevs...the /dev/input/event* devices.
*
Jul 28, 2008
Jul 28, 2008
4
* Please see the file LICENSE.txt in the source's root directory.
5
6
7
8
*
* This file written by Ryan C. Gordon.
*/
Mar 24, 2006
Mar 24, 2006
9
10
#include "manymouse.h"
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
#ifdef __linux__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h> /* evdev interface... */
#define test_bit(array, bit) (array[bit/8] & (1<<(bit%8)))
/* linux allows 32 evdev nodes currently. */
#define MAX_MICE 32
typedef struct
{
int fd;
int min_x;
int min_y;
int max_x;
int max_y;
char name[64];
} MouseStruct;
static MouseStruct mice[MAX_MICE];
static unsigned int available_mice = 0;
Jun 27, 2005
Jun 27, 2005
44
static int poll_mouse(MouseStruct *mouse, ManyMouseEvent *outevent)
45
46
47
48
49
50
51
52
53
{
int unhandled = 1;
while (unhandled) /* read until failure or valid event. */
{
struct input_event event;
int br = read(mouse->fd, &event, sizeof (event));
if (br == -1)
{
if (errno == EAGAIN)
Apr 26, 2011
Apr 26, 2011
54
return 0; /* just no new data at the moment. */
55
56
57
58
/* mouse was unplugged? */
close(mouse->fd); /* stop reading from this mouse. */
mouse->fd = -1;
Jun 27, 2005
Jun 27, 2005
59
outevent->type = MANYMOUSE_EVENT_DISCONNECT;
Apr 26, 2011
Apr 26, 2011
60
return 1;
61
62
63
} /* if */
if (br != sizeof (event))
Apr 26, 2011
Apr 26, 2011
64
return 0; /* oh well. */
65
66
67
68
69
unhandled = 0; /* will reset if necessary. */
outevent->value = event.value;
if (event.type == EV_REL)
{
Jun 27, 2005
Jun 27, 2005
70
outevent->type = MANYMOUSE_EVENT_RELMOTION;
71
72
73
74
75
76
77
if ((event.code == REL_X) || (event.code == REL_DIAL))
outevent->item = 0;
else if (event.code == REL_Y)
outevent->item = 1;
else if (event.code == REL_WHEEL)
{
Jun 27, 2005
Jun 27, 2005
78
outevent->type = MANYMOUSE_EVENT_SCROLL;
79
80
81
82
83
outevent->item = 0;
} /* else if */
else if (event.code == REL_HWHEEL)
{
Jun 27, 2005
Jun 27, 2005
84
outevent->type = MANYMOUSE_EVENT_SCROLL;
85
86
87
88
89
90
91
92
93
94
95
outevent->item = 1;
} /* else if */
else
{
unhandled = 1;
} /* else */
} /* if */
else if (event.type == EV_ABS)
{
Jun 27, 2005
Jun 27, 2005
96
outevent->type = MANYMOUSE_EVENT_ABSMOTION;
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
if (event.code == ABS_X)
{
outevent->item = 0;
outevent->minval = mouse->min_x;
outevent->maxval = mouse->max_x;
} /* if */
else if (event.code == ABS_Y)
{
outevent->item = 1;
outevent->minval = mouse->min_y;
outevent->maxval = mouse->max_y;
} /* if */
else
{
unhandled = 1;
} /* else */
} /* else if */
else if (event.type == EV_KEY)
{
Jun 27, 2005
Jun 27, 2005
117
outevent->type = MANYMOUSE_EVENT_BUTTON;
118
119
120
121
122
123
124
if ((event.code >= BTN_LEFT) && (event.code <= BTN_BACK))
outevent->item = event.code - BTN_MOUSE;
/* just in case some device uses this block of events instead... */
else if ((event.code >= BTN_MISC) && (event.code <= BTN_LEFT))
outevent->item = (event.code - BTN_MISC);
Jul 9, 2005
Jul 9, 2005
125
126
127
128
129
130
131
else if (event.code == BTN_TOUCH) /* tablet... */
outevent->item = 0;
else if (event.code == BTN_STYLUS) /* tablet... */
outevent->item = 1;
else if (event.code == BTN_STYLUS2) /* tablet... */
outevent->item = 2;
Jul 9, 2005
Jul 9, 2005
133
134
{
/*printf("unhandled mouse button: 0x%X\n", event.code);*/
135
unhandled = 1;
Jul 9, 2005
Jul 9, 2005
136
} /* else */
137
138
139
140
141
142
143
} /* else if */
else
{
unhandled = 1;
} /* else */
} /* while */
Apr 26, 2011
Apr 26, 2011
144
return 1; /* got a valid event */
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
} /* poll_mouse */
static int init_mouse(const char *fname, int fd)
{
MouseStruct *mouse = &mice[available_mice];
int has_absolutes = 0;
int is_mouse = 0;
unsigned char relcaps[(REL_MAX / 8) + 1];
unsigned char abscaps[(ABS_MAX / 8) + 1];
unsigned char keycaps[(KEY_MAX / 8) + 1];
memset(relcaps, '\0', sizeof (relcaps));
memset(abscaps, '\0', sizeof (abscaps));
memset(keycaps, '\0', sizeof (keycaps));
if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof (keycaps)), keycaps) == -1)
return 0; /* gotta have some buttons! :) */
if (ioctl(fd, EVIOCGBIT(EV_REL, sizeof (relcaps)), relcaps) != -1)
{
if ( (test_bit(relcaps, REL_X)) && (test_bit(relcaps, REL_Y)) )
{
if (test_bit(keycaps, BTN_MOUSE))
is_mouse = 1;
} /* if */
#if ALLOW_DIALS_TO_BE_MICE
if (test_bit(relcaps, REL_DIAL))
is_mouse = 1; // griffin powermate?
#endif
} /* if */
if (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof (abscaps)), abscaps) != -1)
{
if ( (test_bit(abscaps, ABS_X)) && (test_bit(abscaps, ABS_Y)) )
{
/* might be a touchpad... */
if (test_bit(keycaps, BTN_TOUCH))
{
is_mouse = 1; /* touchpad, touchscreen, or tablet. */
has_absolutes = 1;
} /* if */
} /* if */
} /* if */
if (!is_mouse)
return 0;
mouse->min_x = mouse->min_y = mouse->max_x = mouse->max_y = 0;
if (has_absolutes)
{
struct input_absinfo absinfo;
if (ioctl(fd, EVIOCGABS(ABS_X), &absinfo) == -1)
return 0;
mouse->min_x = absinfo.minimum;
mouse->max_x = absinfo.maximum;
if (ioctl(fd, EVIOCGABS(ABS_Y), &absinfo) == -1)
return 0;
mouse->min_y = absinfo.minimum;
mouse->max_y = absinfo.maximum;
} /* if */
if (ioctl(fd, EVIOCGNAME(sizeof (mouse->name)), mouse->name) == -1)
snprintf(mouse->name, sizeof (mouse->name), "Unknown device");
mouse->fd = fd;
return 1; /* we're golden. */
} /* init_mouse */
/* Return a file descriptor if this is really a mouse, -1 otherwise. */
static int open_if_mouse(const char *fname)
{
struct stat statbuf;
int fd;
int devmajor, devminor;
if (stat(fname, &statbuf) == -1)
return 0;
if (S_ISCHR(statbuf.st_mode) == 0)
return 0; /* not a character device... */
/* evdev node ids are major 13, minor 64-96. Is this safe to check? */
devmajor = (statbuf.st_rdev & 0xFF00) >> 8;
devminor = (statbuf.st_rdev & 0x00FF);
if ( (devmajor != 13) || (devminor < 64) || (devminor > 96) )
return 0; /* not an evdev. */
if ((fd = open(fname, O_RDONLY | O_NONBLOCK)) == -1)
return 0;
if (init_mouse(fname, fd))
return 1;
close(fd);
return 0;
} /* open_if_mouse */
static int linux_evdev_init(void)
{
DIR *dirp;
struct dirent *dent;
int i;
for (i = 0; i < MAX_MICE; i++)
mice[i].fd = -1;
dirp = opendir("/dev/input");
if (!dirp)
Mar 24, 2006
Mar 24, 2006
259
return -1;
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
while ((dent = readdir(dirp)) != NULL)
{
char fname[128];
snprintf(fname, sizeof (fname), "/dev/input/%s", dent->d_name);
if (open_if_mouse(fname))
available_mice++;
} /* while */
closedir(dirp);
return available_mice;
} /* linux_evdev_init */
static void linux_evdev_quit(void)
{
while (available_mice)
{
int fd = mice[available_mice--].fd;
if (fd != -1)
close(fd);
} /* while */
} /* linux_evdev_quit */
static const char *linux_evdev_name(unsigned int index)
{
Apr 26, 2011
Apr 26, 2011
288
return (index < available_mice) ? mice[index].name : NULL;
289
290
291
} /* linux_evdev_name */
Jun 27, 2005
Jun 27, 2005
292
static int linux_evdev_poll(ManyMouseEvent *event)
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
{
/*
* (i) is static so we iterate through all mice round-robin. This
* prevents a chatty mouse from dominating the queue.
*/
static unsigned int i = 0;
if (i >= available_mice)
i = 0; /* handle reset condition. */
if (event != NULL)
{
while (i < available_mice)
{
MouseStruct *mouse = &mice[i];
if (mouse->fd != -1)
{
if (poll_mouse(mouse, event))
{
event->device = i;
Apr 26, 2011
Apr 26, 2011
313
return 1;
314
315
316
317
318
319
} /* if */
} /* if */
i++;
} /* while */
} /* if */
Apr 26, 2011
Apr 26, 2011
320
return 0; /* no new events */
321
322
} /* linux_evdev_poll */
Jul 27, 2007
Jul 27, 2007
323
static const ManyMouseDriver ManyMouseDriver_interface =
Apr 26, 2011
Apr 26, 2011
325
"Linux /dev/input/event* interface",
326
327
328
329
330
331
linux_evdev_init,
linux_evdev_quit,
linux_evdev_name,
linux_evdev_poll
};
Jul 27, 2007
Jul 27, 2007
332
333
334
335
336
337
const ManyMouseDriver *ManyMouseDriver_evdev = &ManyMouseDriver_interface;
#else
const ManyMouseDriver *ManyMouseDriver_evdev = 0;
#endif /* ifdef Linux blocker */
338
/* end of linux_evdev.c ... */