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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
|
/*
* Copyright © 2004 Keith Packard
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of Keith Packard not be used in
* advertising or publicity pertaining to distribution of the software without
* specific, written prior permission. Keith Packard makes no
* representations about the suitability of this software for any purpose. It
* is provided "as is" without express or implied warranty.
*
* KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#ifdef HAVE_CONFIG_H
#include <kdrive-config.h>
#endif
#include <errno.h>
#include <linux/input.h>
#include <X11/X.h>
#include <X11/Xproto.h>
#include <X11/Xpoll.h>
#include "inputstr.h"
#include "scrnintstr.h"
#include "kdrive.h"
#define NUM_EVENTS 128
#define ABS_UNSET -65535
#define BITS_PER_LONG (sizeof(long) * 8)
#define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
#define ISBITSET(x,y) ((x)[LONG(y)] & BIT(y))
#define OFF(x) ((x)%BITS_PER_LONG)
#define LONG(x) ((x)/BITS_PER_LONG)
#define BIT(x) (1 << OFF(x))
typedef struct _kevdev {
/* current device state */
int rel[REL_MAX + 1];
int abs[ABS_MAX + 1];
int prevabs[ABS_MAX + 1];
long key[NBITS(KEY_MAX + 1)];
/* supported device info */
long relbits[NBITS(REL_MAX + 1)];
long absbits[NBITS(ABS_MAX + 1)];
long keybits[NBITS(KEY_MAX + 1)];
struct input_absinfo absinfo[ABS_MAX + 1];
int max_rel;
int max_abs;
int fd;
} Kevdev;
static void
EvdevPtrBtn (KdPointerInfo *pi, struct input_event *ev)
{
int flags = KD_MOUSE_DELTA | pi->buttonState;
if (ev->code >= BTN_MOUSE && ev->code < BTN_JOYSTICK) {
switch (ev->code) {
case BTN_LEFT:
if (ev->value == 1)
flags |= KD_BUTTON_1;
else
flags &= ~KD_BUTTON_1;
break;
case BTN_MIDDLE:
if (ev->value == 1)
flags |= KD_BUTTON_2;
else
flags &= ~KD_BUTTON_2;
break;
case BTN_RIGHT:
if (ev->value == 1)
flags |= KD_BUTTON_3;
else
flags &= ~KD_BUTTON_3;
break;
default:
/* Unknow button */
break;
}
KdEnqueuePointerEvent (pi, flags, 0, 0, 0);
}
}
static void
EvdevPtrMotion (KdPointerInfo *pi, struct input_event *ev)
{
Kevdev *ke = pi->driverPrivate;
int i;
int flags = KD_MOUSE_DELTA | pi->buttonState;
for (i = 0; i <= ke->max_rel; i++)
if (ke->rel[i])
{
int a;
for (a = 0; a <= ke->max_rel; a++)
{
if (ISBITSET (ke->relbits, a))
{
if (a == 0)
KdEnqueuePointerEvent(pi, flags, ke->rel[a], 0, 0);
else if (a == 1)
KdEnqueuePointerEvent(pi, flags, 0, ke->rel[a], 0);
}
ke->rel[a] = 0;
}
break;
}
for (i = 0; i < ke->max_abs; i++)
if (ke->abs[i] != ke->prevabs[i])
{
int a;
ErrorF ("abs");
for (a = 0; a <= ke->max_abs; a++)
{
if (ISBITSET (ke->absbits, a))
ErrorF (" %d=%d", a, ke->abs[a]);
ke->prevabs[a] = ke->abs[a];
}
ErrorF ("\n");
break;
}
if (ev->code == REL_WHEEL) {
for (i = 0; i < abs (ev->value); i++)
{
if (ev->value > 0)
flags |= KD_BUTTON_4;
else
flags |= KD_BUTTON_5;
KdEnqueuePointerEvent (pi, flags, 0, 0, 0);
if (ev->value > 0)
flags &= ~KD_BUTTON_4;
else
flags &= ~KD_BUTTON_5;
KdEnqueuePointerEvent (pi, flags, 0, 0, 0);
}
}
}
static void
EvdevPtrRead (int evdevPort, void *closure)
{
KdPointerInfo *pi = closure;
Kevdev *ke = pi->driverPrivate;
int i;
struct input_event events[NUM_EVENTS];
int n;
n = read (evdevPort, &events, NUM_EVENTS * sizeof (struct input_event));
if (n <= 0) {
if (errno == ENODEV)
DeleteInputDeviceRequest(pi->dixdev);
return;
}
n /= sizeof (struct input_event);
for (i = 0; i < n; i++)
{
switch (events[i].type) {
case EV_SYN:
break;
case EV_KEY:
EvdevPtrBtn (pi, &events[i]);
break;
case EV_REL:
ke->rel[events[i].code] += events[i].value;
EvdevPtrMotion (pi, &events[i]);
break;
case EV_ABS:
ke->abs[events[i].code] = events[i].value;
EvdevPtrMotion (pi, &events[i]);
break;
}
}
}
char *kdefaultEvdev[] = {
"/dev/input/event0",
"/dev/input/event1",
"/dev/input/event2",
"/dev/input/event3",
};
#define NUM_DEFAULT_EVDEV (sizeof (kdefaultEvdev) / sizeof (kdefaultEvdev[0]))
static Status
EvdevPtrInit (KdPointerInfo *pi)
{
int i;
int fd;
if (!pi->path) {
for (i = 0; i < NUM_DEFAULT_EVDEV; i++) {
fd = open (kdefaultEvdev[i], 2);
if (fd >= 0) {
pi->path = strdup (kdefaultEvdev[i]);
break;
}
}
}
else {
fd = open (pi->path, O_RDWR);
if (fd < 0) {
ErrorF("Failed to open evdev device %s\n", pi->path);
return BadMatch;
}
}
close(fd);
pi->name = strdup("Evdev mouse");
return Success;
}
static Status
EvdevPtrEnable (KdPointerInfo *pi)
{
int fd;
unsigned long ev[NBITS(EV_MAX)];
Kevdev *ke;
if (!pi || !pi->path)
return BadImplementation;
fd = open(pi->path, 2);
if (fd < 0)
return BadMatch;
if (ioctl (fd, EVIOCGRAB, 1) < 0)
perror ("Grabbing evdev mouse device failed");
if (ioctl (fd, EVIOCGBIT(0 /*EV*/, sizeof (ev)), ev) < 0)
{
perror ("EVIOCGBIT 0");
close (fd);
return BadMatch;
}
ke = xcalloc (1, sizeof (Kevdev));
if (!ke)
{
close (fd);
return BadAlloc;
}
if (ISBITSET (ev, EV_KEY))
{
if (ioctl (fd, EVIOCGBIT (EV_KEY, sizeof (ke->keybits)),
ke->keybits) < 0)
{
perror ("EVIOCGBIT EV_KEY");
xfree (ke);
close (fd);
return BadMatch;
}
}
if (ISBITSET (ev, EV_REL))
{
if (ioctl (fd, EVIOCGBIT (EV_REL, sizeof (ke->relbits)),
ke->relbits) < 0)
{
perror ("EVIOCGBIT EV_REL");
xfree (ke);
close (fd);
return BadMatch;
}
for (ke->max_rel = REL_MAX; ke->max_rel >= 0; ke->max_rel--)
if (ISBITSET(ke->relbits, ke->max_rel))
break;
}
if (ISBITSET (ev, EV_ABS))
{
int i;
if (ioctl (fd, EVIOCGBIT (EV_ABS, sizeof (ke->absbits)),
ke->absbits) < 0)
{
perror ("EVIOCGBIT EV_ABS");
xfree (ke);
close (fd);
return BadMatch;
}
for (ke->max_abs = ABS_MAX; ke->max_abs >= 0; ke->max_abs--)
if (ISBITSET(ke->absbits, ke->max_abs))
break;
for (i = 0; i <= ke->max_abs; i++)
{
if (ISBITSET (ke->absbits, i))
if (ioctl (fd, EVIOCGABS(i), &ke->absinfo[i]) < 0)
{
perror ("EVIOCGABS");
break;
}
ke->prevabs[i] = ABS_UNSET;
}
if (i <= ke->max_abs)
{
xfree (ke);
close (fd);
return BadValue;
}
}
if (!KdRegisterFd (fd, EvdevPtrRead, pi)) {
xfree (ke);
close (fd);
return BadAlloc;
}
pi->driverPrivate = ke;
ke->fd = fd;
return Success;
}
static void
EvdevPtrDisable (KdPointerInfo *pi)
{
Kevdev *ke;
ke = pi->driverPrivate;
if (!pi || !pi->driverPrivate)
return;
KdUnregisterFd (pi, ke->fd, TRUE);
if (ioctl (ke->fd, EVIOCGRAB, 0) < 0)
perror ("Ungrabbing evdev mouse device failed");
xfree (ke);
pi->driverPrivate = 0;
}
static void
EvdevPtrFini (KdPointerInfo *pi)
{
}
/*
* Evdev keyboard functions
*/
static void
readMapping (KdKeyboardInfo *ki)
{
if (!ki)
return;
ki->minScanCode = 0;
ki->maxScanCode = 193;
}
static void
EvdevKbdRead (int evdevPort, void *closure)
{
KdKeyboardInfo *ki = closure;
struct input_event events[NUM_EVENTS];
int i, n;
n = read (evdevPort, &events, NUM_EVENTS * sizeof (struct input_event));
if (n <= 0) {
if (errno == ENODEV)
DeleteInputDeviceRequest(ki->dixdev);
return;
}
n /= sizeof (struct input_event);
for (i = 0; i < n; i++)
{
if (events[i].type == EV_KEY)
KdEnqueueKeyboardEvent (ki, events[i].code, !events[i].value);
/* FIXME: must implement other types of events
else
ErrorF("Event type (%d) not delivered\n", events[i].type);
*/
}
}
static Status
EvdevKbdInit (KdKeyboardInfo *ki)
{
int fd;
if (!ki->path) {
ErrorF("Couldn't find evdev device path\n");
return BadValue;
}
else {
fd = open (ki->path, O_RDWR);
if (fd < 0) {
ErrorF("Failed to open evdev device %s\n", ki->path);
return BadMatch;
}
}
close (fd);
ki->name = strdup("Evdev keyboard");
readMapping(ki);
return Success;
}
static Status
EvdevKbdEnable (KdKeyboardInfo *ki)
{
unsigned long ev[NBITS(EV_MAX)];
Kevdev *ke;
int fd;
if (!ki || !ki->path)
return BadImplementation;
fd = open(ki->path, O_RDWR);
if (fd < 0)
return BadMatch;
if (ioctl (fd, EVIOCGRAB, 1) < 0)
perror ("Grabbing evdev keyboard device failed");
if (ioctl (fd, EVIOCGBIT(0 /*EV*/, sizeof (ev)), ev) < 0) {
perror ("EVIOCGBIT 0");
close (fd);
return BadMatch;
}
ke = xcalloc (1, sizeof (Kevdev));
if (!ke) {
close (fd);
return BadAlloc;
}
if (!KdRegisterFd (fd, EvdevKbdRead, ki)) {
xfree (ke);
close (fd);
return BadAlloc;
}
ki->driverPrivate = ke;
ke->fd = fd;
return Success;
}
static void
EvdevKbdLeds (KdKeyboardInfo *ki, int leds)
{
/* struct input_event event;
Kevdev *ke;
ki->driverPrivate = ke;
memset(&event, 0, sizeof(event));
event.type = EV_LED;
event.code = LED_CAPSL;
event.value = leds & (1 << 0) ? 1 : 0;
write(ke->fd, (char *) &event, sizeof(event));
event.type = EV_LED;
event.code = LED_NUML;
event.value = leds & (1 << 1) ? 1 : 0;
write(ke->fd, (char *) &event, sizeof(event));
event.type = EV_LED;
event.code = LED_SCROLLL;
event.value = leds & (1 << 2) ? 1 : 0;
write(ke->fd, (char *) &event, sizeof(event));
event.type = EV_LED;
event.code = LED_COMPOSE;
event.value = leds & (1 << 3) ? 1 : 0;
write(ke->fd, (char *) &event, sizeof(event));
*/
}
static void
EvdevKbdBell (KdKeyboardInfo *ki, int volume, int frequency, int duration)
{
}
static void
EvdevKbdDisable (KdKeyboardInfo *ki)
{
Kevdev *ke;
ke = ki->driverPrivate;
if (!ki || !ki->driverPrivate)
return;
KdUnregisterFd (ki, ke->fd, TRUE);
if (ioctl (ke->fd, EVIOCGRAB, 0) < 0)
perror ("Ungrabbing evdev keyboard device failed");
xfree (ke);
ki->driverPrivate = 0;
}
static void
EvdevKbdFini (KdKeyboardInfo *ki)
{
}
KdPointerDriver LinuxEvdevMouseDriver = {
"evdev",
EvdevPtrInit,
EvdevPtrEnable,
EvdevPtrDisable,
EvdevPtrFini,
NULL,
};
KdKeyboardDriver LinuxEvdevKeyboardDriver = {
"evdev",
EvdevKbdInit,
EvdevKbdEnable,
EvdevKbdLeds,
EvdevKbdBell,
EvdevKbdDisable,
EvdevKbdFini,
NULL,
};
|