kinput.c 56.8 KB
Newer Older
1
/*
Matěj Cepl's avatar
Matěj Cepl committed
2
3
 * Copyright © 1999 Keith Packard
 * Copyright © 2006 Nokia Corporation
4
5
6
7
8
 *
 * 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
Daniel Stone's avatar
Daniel Stone committed
9
 * documentation, and that the name of the authors not be used in
10
 * advertising or publicity pertaining to distribution of the software without
Daniel Stone's avatar
Daniel Stone committed
11
 * specific, written prior permission.  The authors make no
12
13
14
 * representations about the suitability of this software for any purpose.  It
 * is provided "as is" without express or implied warranty.
 *
Daniel Stone's avatar
Daniel Stone committed
15
 * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
Daniel Stone's avatar
Daniel Stone committed
17
 * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18
19
20
21
22
23
 * 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.
 */

24
#ifdef HAVE_CONFIG_H
25
#include <kdrive-config.h>
26
#endif
27
28
29
#include "kdrive.h"
#include "inputstr.h"

30
#define XK_PUBLISHING
31
#include <X11/keysym.h>
32
#if HAVE_X11_XF86KEYSYM_H
33
#include <X11/XF86keysym.h>
34
#endif
35
36
#include <signal.h>
#include <stdio.h>
37
38
39
#ifdef sun
#include <sys/file.h> /* needed for FNONBLOCK & FASYNC */
#endif
40

Daniel Stone's avatar
Daniel Stone committed
41
#include "xkbsrv.h"
42

Daniel Stone's avatar
Daniel Stone committed
43
44
#include <X11/extensions/XI.h>
#include <X11/extensions/XIproto.h>
Daniel Stone's avatar
Daniel Stone committed
45
#include "XIstubs.h" /* even though we don't use stubs.  cute, no? */
Daniel Stone's avatar
Daniel Stone committed
46
47
48
#include "exevents.h"
#include "extinit.h"
#include "exglobals.h"
49
#include "eventstr.h"
50
#include "xserver-properties.h"
51
#include "inpututils.h"
Daniel Stone's avatar
Daniel Stone committed
52
53
54
55
56
57
58
59
60
61
62
63
64

#define AtomFromName(x) MakeAtom(x, strlen(x), 1)

struct KdConfigDevice {
    char *line;
    struct KdConfigDevice *next;
};

/* kdKeyboards and kdPointers hold all the real devices. */
static KdKeyboardInfo *kdKeyboards         = NULL;
static KdPointerInfo  *kdPointers          = NULL;
static struct KdConfigDevice *kdConfigKeyboards   = NULL;
static struct KdConfigDevice *kdConfigPointers    = NULL;
65

Daniel Stone's avatar
Daniel Stone committed
66
67
static KdKeyboardDriver *kdKeyboardDrivers = NULL;
static KdPointerDriver  *kdPointerDrivers  = NULL;
68

69
static Bool		kdInputEnabled;
70
71
static Bool		kdOffScreen;
static unsigned long	kdOffScreenTime;
Daniel Stone's avatar
Daniel Stone committed
72
static KdPointerMatrix	kdPointerMatrix = {
73
74
   { { 1, 0, 0 },
     { 0, 1, 0 } }
75
};
76

Daniel Stone's avatar
Daniel Stone committed
77
void KdResetInputMachine (void);
Misha Gusarov's avatar
Misha Gusarov committed
78

79
80
81
#define KD_MAX_INPUT_FDS    8

typedef struct _kdInputFd {
Daniel Stone's avatar
Daniel Stone committed
82
83
84
85
86
    int	        fd;
    void        (*read) (int fd, void *closure);
    int	        (*enable) (int fd, void *closure);
    void        (*disable) (int fd, void *closure);
    void        *closure;
87
88
} KdInputFd;

Daniel Stone's avatar
Daniel Stone committed
89
90
91
92
static KdInputFd kdInputFds[KD_MAX_INPUT_FDS];
static int	 kdNumInputFds;

extern Bool      kdRawPointerCoordinates;
93

94
static void
95
96
KdSigio (int sig)
{
97
98
99
100
    int	i;

    for (i = 0; i < kdNumInputFds; i++)
	(*kdInputFds[i].read) (kdInputFds[i].fd, kdInputFds[i].closure);
101
102
}

103
static void
104
105
106
KdBlockSigio (void)
{
    sigset_t	set;
Misha Gusarov's avatar
Misha Gusarov committed
107

108
109
110
111
112
    sigemptyset (&set);
    sigaddset (&set, SIGIO);
    sigprocmask (SIG_BLOCK, &set, 0);
}

113
static void
114
115
116
KdUnblockSigio (void)
{
    sigset_t	set;
Misha Gusarov's avatar
Misha Gusarov committed
117

118
119
120
121
122
    sigemptyset (&set);
    sigaddset (&set, SIGIO);
    sigprocmask (SIG_UNBLOCK, &set, 0);
}

Daniel Stone's avatar
Daniel Stone committed
123
#ifdef DEBUG_SIGIO
124
125
126
127
128
129
130
131

void
KdAssertSigioBlocked (char *where)
{
    sigset_t	set, old;

    sigemptyset (&set);
    sigprocmask (SIG_BLOCK, &set, &old);
Daniel Stone's avatar
Daniel Stone committed
132
    if (!sigismember (&old, SIGIO)) {
133
	ErrorF ("SIGIO not blocked at %s\n", where);
134
        KdBacktrace(0);
Daniel Stone's avatar
Daniel Stone committed
135
    }
136
137
138
139
}

#else

140
#define KdAssertSigioBlocked(s)
141
142
143
144
145
146
147
148
149
150
151

#endif

static int  kdnFds;

#ifdef FNONBLOCK
#define NOBLOCK FNONBLOCK
#else
#define NOBLOCK FNDELAY
#endif

Daniel Stone's avatar
Daniel Stone committed
152
153
154
155
156
157
158
159
160
161
162
void
KdResetInputMachine (void)
{
    KdPointerInfo *pi;

    for (pi = kdPointers; pi; pi = pi->next) {
        pi->mouseState = start;
        pi->eventHeld = FALSE;
    }
}

163
164
165
166
167
168
169
170
171
static void
KdNonBlockFd (int fd)
{
    int	flags;
    flags = fcntl (fd, F_GETFL);
    flags |= FASYNC|NOBLOCK;
    fcntl (fd, F_SETFL, flags);
}

172
static void
173
174
175
176
KdAddFd (int fd)
{
    struct sigaction	act;
    sigset_t		set;
Misha Gusarov's avatar
Misha Gusarov committed
177

178
179
    kdnFds++;
    fcntl (fd, F_SETOWN, getpid());
180
    KdNonBlockFd (fd);
181
    AddEnabledDevice (fd);
182
    memset (&act, '\0', sizeof act);
183
184
    act.sa_handler = KdSigio;
    sigemptyset (&act.sa_mask);
Keith Packard's avatar
Keith Packard committed
185
186
187
    sigaddset (&act.sa_mask, SIGIO);
    sigaddset (&act.sa_mask, SIGALRM);
    sigaddset (&act.sa_mask, SIGVTALRM);
188
189
190
191
192
    sigaction (SIGIO, &act, 0);
    sigemptyset (&set);
    sigprocmask (SIG_SETMASK, &set, 0);
}

193
static void
194
195
196
197
KdRemoveFd (int fd)
{
    struct sigaction	act;
    int			flags;
Misha Gusarov's avatar
Misha Gusarov committed
198

199
200
201
202
203
204
205
    kdnFds--;
    RemoveEnabledDevice (fd);
    flags = fcntl (fd, F_GETFL);
    flags &= ~(FASYNC|NOBLOCK);
    fcntl (fd, F_SETFL, flags);
    if (kdnFds == 0)
    {
206
	memset (&act, '\0', sizeof act);
207
208
209
210
211
212
	act.sa_handler = SIG_IGN;
	sigemptyset (&act.sa_mask);
	sigaction (SIGIO, &act, 0);
    }
}

213
Bool
Daniel Stone's avatar
Daniel Stone committed
214
KdRegisterFd (int fd, void (*read) (int fd, void *closure), void *closure)
215
216
217
218
219
{
    if (kdNumInputFds == KD_MAX_INPUT_FDS)
	return FALSE;
    kdInputFds[kdNumInputFds].fd = fd;
    kdInputFds[kdNumInputFds].read = read;
220
221
    kdInputFds[kdNumInputFds].enable = 0;
    kdInputFds[kdNumInputFds].disable = 0;
222
    kdInputFds[kdNumInputFds].closure = closure;
Daniel Stone's avatar
Daniel Stone committed
223
    kdNumInputFds++;
224
225
226
227
228
    if (kdInputEnabled)
	KdAddFd (fd);
    return TRUE;
}

229
void
Daniel Stone's avatar
Daniel Stone committed
230
KdUnregisterFd (void *closure, int fd, Bool do_close)
231
{
232
    int	i, j;
233

Daniel Stone's avatar
Daniel Stone committed
234
235
    for (i = 0; i < kdNumInputFds; i++) {
	if (kdInputFds[i].closure == closure &&
Daniel Stone's avatar
Daniel Stone committed
236
            (fd == -1 || kdInputFds[i].fd == fd)) {
237
238
239
240
	    if (kdInputEnabled)
		KdRemoveFd (kdInputFds[i].fd);
	    if (do_close)
		close (kdInputFds[i].fd);
Daniel Stone's avatar
Daniel Stone committed
241
	    kdNumInputFds--;
242
243
	    for (j = i; j < kdNumInputFds; j++)
		kdInputFds[j] = kdInputFds[j+1];
Daniel Stone's avatar
Daniel Stone committed
244
            break;
245
246
247
248
	}
    }
}

Daniel Stone's avatar
Daniel Stone committed
249
250
251
252
253
254
void
KdUnregisterFds (void *closure, Bool do_close)
{
    KdUnregisterFd(closure, -1, do_close);
}

255
256
257
void
KdDisableInput (void)
{
Daniel Stone's avatar
Daniel Stone committed
258
259
260
    KdKeyboardInfo *ki;
    KdPointerInfo *pi;
    int found = 0, i = 0;
261

Daniel Stone's avatar
Daniel Stone committed
262
    KdBlockSigio();
263

Daniel Stone's avatar
Daniel Stone committed
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
    for (ki = kdKeyboards; ki; ki = ki->next) {
        if (ki->driver && ki->driver->Disable)
            (*ki->driver->Disable) (ki);
    }

    for (pi = kdPointers; pi; pi = pi->next) {
        if (pi->driver && pi->driver->Disable)
            (*pi->driver->Disable) (pi);
    }

    if (kdNumInputFds) {
        ErrorF("[KdDisableInput] Buggy drivers: still %d input fds left!",
               kdNumInputFds);
        i = 0;
        while (i < kdNumInputFds) {
            found = 0;
            for (ki = kdKeyboards; ki; ki = ki->next) {
                if (ki == kdInputFds[i].closure) {
                    ErrorF("    fd %d belongs to keybd driver %s\n",
                           kdInputFds[i].fd,
                           ki->driver && ki->driver->name ?
                             ki->driver->name : "(unnamed!)");
                    found = 1;
                    break;
                }
            }

            if (found) {
                i++;
                continue;
            }

            for (pi = kdPointers; pi; pi = pi->next) {
                if (pi == kdInputFds[i].closure) {
                    ErrorF("    fd %d belongs to pointer driver %s\n",
                           kdInputFds[i].fd,
                           pi->driver && pi->driver->name ?
                             pi->driver->name : "(unnamed!)");
                    break;
                }
            }

            if (found) {
                i++;
                continue;
            }

            ErrorF("    fd %d not claimed by any active device!\n",
                   kdInputFds[i].fd);
            KdUnregisterFd(kdInputFds[i].closure, kdInputFds[i].fd, TRUE);
        }
315
    }
Daniel Stone's avatar
Daniel Stone committed
316

317
    kdInputEnabled = FALSE;
318
319
320
321
322
}

void
KdEnableInput (void)
{
Daniel Stone's avatar
Daniel Stone committed
323
    InternalEvent ev;
Daniel Stone's avatar
Daniel Stone committed
324
325
    KdKeyboardInfo *ki;
    KdPointerInfo *pi;
Misha Gusarov's avatar
Misha Gusarov committed
326

327
    kdInputEnabled = TRUE;
Daniel Stone's avatar
Daniel Stone committed
328
329
330
331

    for (ki = kdKeyboards; ki; ki = ki->next) {
        if (ki->driver && ki->driver->Enable)
            (*ki->driver->Enable) (ki);
332
    }
Daniel Stone's avatar
Daniel Stone committed
333
334
335
336
337
338

    for (pi = kdPointers; pi; pi = pi->next) {
        if (pi->driver && pi->driver->Enable)
            (*pi->driver->Enable) (pi);
    }

339
    /* reset screen saver */
Daniel Stone's avatar
Daniel Stone committed
340
341
    ev.any.time = GetTimeInMillis ();
    NoticeEventTime (&ev);
342
343

    KdUnblockSigio ();
344
345
}

Daniel Stone's avatar
Daniel Stone committed
346
static KdKeyboardDriver *
Daniel Stone's avatar
Daniel Stone committed
347
KdFindKeyboardDriver (char *name)
348
{
Daniel Stone's avatar
Daniel Stone committed
349
350
351
352
353
    KdKeyboardDriver *ret;

    /* ask a stupid question ... */
    if (!name)
        return NULL;
Misha Gusarov's avatar
Misha Gusarov committed
354

Daniel Stone's avatar
Daniel Stone committed
355
356
357
358
359
360
361
362
    for (ret = kdKeyboardDrivers; ret; ret = ret->next) {
        if (strcmp(ret->name, name) == 0)
            return ret;
    }

    return NULL;
}

Daniel Stone's avatar
Daniel Stone committed
363
static KdPointerDriver *
Daniel Stone's avatar
Daniel Stone committed
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
KdFindPointerDriver (char *name)
{
    KdPointerDriver *ret;

    /* ask a stupid question ... */
    if (!name)
        return NULL;

    for (ret = kdPointerDrivers; ret; ret = ret->next) {
        if (strcmp(ret->name, name) == 0)
            return ret;
    }

    return NULL;
}

static int
KdPointerProc(DeviceIntPtr pDevice, int onoff)
{
    DevicePtr       pDev = (DevicePtr)pDevice;
    KdPointerInfo   *pi;
    Atom            xiclass;
386
387
    Atom            *btn_labels;
    Atom            *axes_labels;
Daniel Stone's avatar
Daniel Stone committed
388

389
390
    if (!pDev)
	return BadImplementation;
Daniel Stone's avatar
Daniel Stone committed
391
392
393
394
395
396
397
398
399
400
401
402

    for (pi = kdPointers; pi; pi = pi->next) {
        if (pi->dixdev && pi->dixdev->id == pDevice->id)
            break;
    }

    if (!pi || !pi->dixdev || pi->dixdev->id != pDevice->id) {
        ErrorF("[KdPointerProc] Failed to find pointer for device %d!\n",
               pDevice->id);
        return BadImplementation;
    }

403
404
405
    switch (onoff)
    {
    case DEVICE_INIT:
Daniel Stone's avatar
Daniel Stone committed
406
#ifdef DEBUG
Daniel Stone's avatar
Daniel Stone committed
407
        ErrorF("initialising pointer %s ...\n", pi->name);
Daniel Stone's avatar
Daniel Stone committed
408
#endif
Daniel Stone's avatar
Daniel Stone committed
409
410
411
412
413
414
415
416
417
418
419
420
421
        if (!pi->driver) {
            if (!pi->driverPrivate) {
                ErrorF("no driver specified for %s\n", pi->name);
                return BadImplementation;
            }

            pi->driver = KdFindPointerDriver(pi->driverPrivate);
            if (!pi->driver) {
                ErrorF("Couldn't find pointer driver %s\n",
                       pi->driverPrivate ? (char *) pi->driverPrivate :
                       "(unnamed)");
                return !Success;
            }
422
            free(pi->driverPrivate);
Daniel Stone's avatar
Daniel Stone committed
423
424
425
426
427
428
429
430
431
432
433
434
            pi->driverPrivate = NULL;
        }

        if (!pi->driver->Init) {
            ErrorF("no init function\n");
            return BadImplementation;
        }

        if ((*pi->driver->Init) (pi) != Success) {
            return !Success;
        }

435
	btn_labels = calloc(pi->nButtons, sizeof(Atom));
436
437
	if (!btn_labels)
	    return BadAlloc;
438
	axes_labels = calloc(pi->nAxes, sizeof(Atom));
439
	if (!axes_labels) {
440
	    free(btn_labels);
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
	    return BadAlloc;
	}

	switch(pi->nAxes)
	{
	    default:
	    case 7:
		btn_labels[6] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_HWHEEL_RIGHT);
	    case 6:
		btn_labels[5] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_HWHEEL_LEFT);
	    case 5:
		btn_labels[4] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_WHEEL_DOWN);
	    case 4:
		btn_labels[3] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_WHEEL_UP);
	    case 3:
		btn_labels[2] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_RIGHT);
	    case 2:
		btn_labels[1] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_MIDDLE);
	    case 1:
		btn_labels[0] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_LEFT);
	    case 0:
		break;
	}

	if (pi->nAxes >= 2) {
	    axes_labels[0] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_X);
	    axes_labels[1] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_Y);
	}

	InitPointerDeviceStruct(pDev, pi->map, pi->nButtons, btn_labels,
471
	    (PtrCtrlProcPtr)NoopDDA,
472
473
	    GetMotionHistorySize(), pi->nAxes, axes_labels);

474
475
        free(btn_labels);
        free(axes_labels);
Daniel Stone's avatar
Daniel Stone committed
476
477
478
479
480
481
482
483
484
485
486
487

        if (pi->inputClass == KD_TOUCHSCREEN) {
            xiclass = AtomFromName(XI_TOUCHSCREEN);
        }
        else {
            xiclass = AtomFromName(XI_MOUSE);
        }

        AssignTypeAndName(pi->dixdev, xiclass,
                          pi->name ? pi->name : "Generic KDrive Pointer");

	return Success;
Misha Gusarov's avatar
Misha Gusarov committed
488

489
    case DEVICE_ON:
Daniel Stone's avatar
Daniel Stone committed
490
491
        if (pDev->on == TRUE)
            return Success;
Misha Gusarov's avatar
Misha Gusarov committed
492

Daniel Stone's avatar
Daniel Stone committed
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
        if (!pi->driver->Enable) {
            ErrorF("no enable function\n");
            return BadImplementation;
        }

        if ((*pi->driver->Enable) (pi) == Success) {
            pDev->on = TRUE;
            return Success;
        }
        else {
            return BadImplementation;
        }

	return Success;

508
    case DEVICE_OFF:
Daniel Stone's avatar
Daniel Stone committed
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
        if (pDev->on == FALSE) {
            return Success;
        }

        if (!pi->driver->Disable) {
            return BadImplementation;
        }
        else {
            (*pi->driver->Disable) (pi);
            pDev->on = FALSE;
            return Success;
        }

        return Success;

524
    case DEVICE_CLOSE:
Daniel Stone's avatar
Daniel Stone committed
525
526
527
528
529
530
531
532
533
534
535
536
537
538
	if (pDev->on) {
            if (!pi->driver->Disable) {
                return BadImplementation;
            }
            (*pi->driver->Disable) (pi);
            pDev->on = FALSE;
        }

        if (!pi->driver->Fini)
            return BadImplementation;

        (*pi->driver->Fini) (pi);

        KdRemovePointer(pi);
Misha Gusarov's avatar
Misha Gusarov committed
539

Daniel Stone's avatar
Daniel Stone committed
540
        return Success;
541
    }
Daniel Stone's avatar
Daniel Stone committed
542
543
544

    /* NOTREACHED */
    return BadImplementation;
545
546
547
}

Bool
548
LegalModifier(unsigned int key, DeviceIntPtr pDev)
549
550
551
552
553
{
    return TRUE;
}

static void
554
KdBell (int volume, DeviceIntPtr pDev, pointer arg, int something)
555
{
556
    KeybdCtrl *ctrl = arg;
Daniel Stone's avatar
Daniel Stone committed
557
    KdKeyboardInfo *ki = NULL;
Misha Gusarov's avatar
Misha Gusarov committed
558

Daniel Stone's avatar
Daniel Stone committed
559
    for (ki = kdKeyboards; ki; ki = ki->next) {
560
        if (ki->dixdev && ki->dixdev->id == pDev->id)
Daniel Stone's avatar
Daniel Stone committed
561
562
563
564
565
            break;
    }

    if (!ki || !ki->dixdev || ki->dixdev->id != pDev->id || !ki->driver)
        return;
Misha Gusarov's avatar
Misha Gusarov committed
566

567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
    KdRingBell(ki, volume, ctrl->bell_pitch, ctrl->bell_duration);
}

void
DDXRingBell(int volume, int pitch, int duration)
{
    KdKeyboardInfo *ki = NULL;

    if (kdOsFuncs->Bell) {
        (*kdOsFuncs->Bell)(volume, pitch, duration);
    }
    else {
        for (ki = kdKeyboards; ki; ki = ki->next) {
            if (ki->dixdev->coreEvents)
                KdRingBell(ki, volume, pitch, duration);
        }
Daniel Stone's avatar
Daniel Stone committed
583
    }
584
585
}

586
587
588
589
590
void
KdRingBell(KdKeyboardInfo *ki, int volume, int pitch, int duration)
{
    if (!ki || !ki->driver || !ki->driver->Bell)
        return;
Misha Gusarov's avatar
Misha Gusarov committed
591

592
593
594
595
    if (kdInputEnabled)
        (*ki->driver->Bell) (ki, volume, pitch, duration);
}

596
597

static void
Daniel Stone's avatar
Daniel Stone committed
598
KdSetLeds (KdKeyboardInfo *ki, int leds)
599
{
Daniel Stone's avatar
Daniel Stone committed
600
601
602
603
604
605
606
    if (!ki || !ki->driver)
        return;

    if (kdInputEnabled) {
        if (ki->driver->Leds)
            (*ki->driver->Leds) (ki, leds);
    }
607
608
609
}

void
Daniel Stone's avatar
Daniel Stone committed
610
KdSetLed (KdKeyboardInfo *ki, int led, Bool on)
611
{
Daniel Stone's avatar
Daniel Stone committed
612
613
614
615
616
    if (!ki || !ki->dixdev || !ki->dixdev->kbdfeed)
        return;

    NoteLedState (ki->dixdev, led, on);
    KdSetLeds (ki, ki->dixdev->kbdfeed->ctrl.leds);
617
618
}

619
void
Daniel Stone's avatar
Daniel Stone committed
620
KdSetPointerMatrix (KdPointerMatrix *matrix)
621
{
Daniel Stone's avatar
Daniel Stone committed
622
    kdPointerMatrix = *matrix;
623
624
}

625
void
Daniel Stone's avatar
Daniel Stone committed
626
627
KdComputePointerMatrix (KdPointerMatrix *m, Rotation randr, int width,
                        int height)
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
{
    int		    x_dir = 1, y_dir = 1;
    int		    i, j;
    int		    size[2];

    size[0] = width; size[1] = height;
    if (randr & RR_Reflect_X)
	x_dir = -1;
    if (randr & RR_Reflect_Y)
	y_dir = -1;
    switch (randr & (RR_Rotate_All)) {
    case RR_Rotate_0:
	m->matrix[0][0] = x_dir; m->matrix[0][1] = 0;
	m->matrix[1][0] = 0; m->matrix[1][1] = y_dir;
	break;
    case RR_Rotate_90:
644
645
	m->matrix[0][0] = 0; m->matrix[0][1] = -x_dir;
	m->matrix[1][0] = y_dir; m->matrix[1][1] = 0;
646
647
648
649
650
651
	break;
    case RR_Rotate_180:
	m->matrix[0][0] = -x_dir; m->matrix[0][1] = 0;
	m->matrix[1][0] = 0; m->matrix[1][1] = -y_dir;
	break;
    case RR_Rotate_270:
652
653
	m->matrix[0][0] = 0; m->matrix[0][1] = x_dir;
	m->matrix[1][0] = -y_dir; m->matrix[1][1] = 0;
654
655
656
	break;
    }
    for (i = 0; i < 2; i++)
657
658
    {
	m->matrix[i][2] = 0;
659
660
	for (j = 0 ; j < 2; j++)
	    if (m->matrix[i][j] < 0)
661
		m->matrix[i][2] = size[j] - 1;
662
    }
663
664
}

665
666
667
668
669
670
671
672
673
674
675
676
void
KdScreenToPointerCoords (int *x, int *y)
{
    int	(*m)[3] = kdPointerMatrix.matrix;
    int div = m[0][1] * m[1][0] - m[1][1] * m[0][0];
    int sx = *x;
    int sy = *y;

    *x = (m[0][1] * sy - m[0][1] * m[1][2] + m[1][1] * m[0][2] - m[1][1] * sx) / div;
    *y = (m[1][0] * sx + m[0][0] * m[1][2] - m[1][0] * m[0][2] - m[0][0] * sy) / div;
}

677
678
679
static void
KdKbdCtrl (DeviceIntPtr pDevice, KeybdCtrl *ctrl)
{
Daniel Stone's avatar
Daniel Stone committed
680
    KdKeyboardInfo *ki;
681

Daniel Stone's avatar
Daniel Stone committed
682
683
684
685
    for (ki = kdKeyboards; ki; ki = ki->next) {
        if (ki->dixdev && ki->dixdev->id == pDevice->id)
            break;
    }
686

Daniel Stone's avatar
Daniel Stone committed
687
688
    if (!ki || !ki->dixdev || ki->dixdev->id != pDevice->id || !ki->driver)
        return;
689

Daniel Stone's avatar
Daniel Stone committed
690
691
    KdSetLeds(ki, ctrl->leds);
    ki->bellPitch = ctrl->bell_pitch;
Misha Gusarov's avatar
Misha Gusarov committed
692
    ki->bellDuration = ctrl->bell_duration;
693
694
695
696
}

extern KeybdCtrl defaultKeyboardControl;

Daniel Stone's avatar
Daniel Stone committed
697
698
699
700
701
702
703
static int
KdKeyboardProc(DeviceIntPtr pDevice, int onoff)
{
    Bool        ret;
    DevicePtr   pDev = (DevicePtr)pDevice;
    KdKeyboardInfo *ki;
    Atom xiclass;
704
    XkbRMLVOSet rmlvo;
Daniel Stone's avatar
Daniel Stone committed
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736

    if (!pDev)
	return BadImplementation;

    for (ki = kdKeyboards; ki; ki = ki->next) {
        if (ki->dixdev && ki->dixdev->id == pDevice->id)
            break;
    }

    if (!ki || !ki->dixdev || ki->dixdev->id != pDevice->id) {
        return BadImplementation;
    }

    switch (onoff)
    {
    case DEVICE_INIT:
#ifdef DEBUG
        ErrorF("initialising keyboard %s\n", ki->name);
#endif
        if (!ki->driver) {
            if (!ki->driverPrivate) {
                ErrorF("no driver specified!\n");
                return BadImplementation;
            }

            ki->driver = KdFindKeyboardDriver(ki->driverPrivate);
            if (!ki->driver) {
                ErrorF("Couldn't find keyboard driver %s\n",
                       ki->driverPrivate ? (char *) ki->driverPrivate :
                       "(unnamed)");
                return !Success;
            }
737
            free(ki->driverPrivate);
Daniel Stone's avatar
Daniel Stone committed
738
739
740
741
742
743
744
745
746
747
748
749
            ki->driverPrivate = NULL;
        }

        if (!ki->driver->Init) {
            ErrorF("Keyboard %s: no init function\n", ki->name);
            return BadImplementation;
        }

        if ((*ki->driver->Init) (ki) != Success) {
            return !Success;
        }

750
751
752
753
754
755
756
        memset(&rmlvo, 0, sizeof(rmlvo));
        rmlvo.rules = ki->xkbRules;
        rmlvo.model = ki->xkbModel;
        rmlvo.layout = ki->xkbLayout;
        rmlvo.variant = ki->xkbVariant;
        rmlvo.options = ki->xkbOptions;
        ret = InitKeyboardDeviceStruct (pDevice, &rmlvo, KdBell, KdKbdCtrl);
Daniel Stone's avatar
Daniel Stone committed
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
	if (!ret) {
            ErrorF("Couldn't initialise keyboard %s\n", ki->name);
	    return BadImplementation;
        }

        xiclass = AtomFromName(XI_KEYBOARD);
        AssignTypeAndName(pDevice, xiclass,
                          ki->name ? ki->name : "Generic KDrive Keyboard");

        KdResetInputMachine();

        return Success;

    case DEVICE_ON:
        if (pDev->on == TRUE)
            return Success;

        if (!ki->driver->Enable)
            return BadImplementation;

        if ((*ki->driver->Enable) (ki) != Success) {
            return BadMatch;
        }

        pDev->on = TRUE;
        return Success;

    case DEVICE_OFF:
        if (pDev->on == FALSE)
            return Success;

        if (!ki->driver->Disable)
            return BadImplementation;

        (*ki->driver->Disable) (ki);
        pDev->on = FALSE;

        return Success;
Misha Gusarov's avatar
Misha Gusarov committed
795

Daniel Stone's avatar
Daniel Stone committed
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
        break;

    case DEVICE_CLOSE:
	if (pDev->on) {
            if (!ki->driver->Disable)
                return BadImplementation;

            (*ki->driver->Disable) (ki);
            pDev->on = FALSE;
	}

        if (!ki->driver->Fini)
            return BadImplementation;

        (*ki->driver->Fini) (ki);

        KdRemoveKeyboard(ki);

        return Success;
    }

    /* NOTREACHED */
    return BadImplementation;
}

void
KdAddPointerDriver (KdPointerDriver *driver)
{
    KdPointerDriver **prev;

    if (!driver)
        return;

    for (prev = &kdPointerDrivers; *prev; prev = &(*prev)->next) {
        if (*prev == driver)
            return;
    }
    *prev = driver;
}

836
void
Daniel Stone's avatar
Daniel Stone committed
837
KdRemovePointerDriver (KdPointerDriver *driver)
838
{
Daniel Stone's avatar
Daniel Stone committed
839
840
841
842
843
844
845
846
847
848
849
850
    KdPointerDriver *tmp;

    if (!driver)
        return;

    /* FIXME remove all pointers using this driver */
    for (tmp = kdPointerDrivers; tmp; tmp = tmp->next) {
        if (tmp->next == driver)
            tmp->next = driver->next;
    }
    if (tmp == driver)
        tmp = NULL;
851
852
}

853
void
Daniel Stone's avatar
Daniel Stone committed
854
KdAddKeyboardDriver (KdKeyboardDriver *driver)
855
{
Daniel Stone's avatar
Daniel Stone committed
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
    KdKeyboardDriver **prev;

    if (!driver)
        return;

    for (prev = &kdKeyboardDrivers; *prev; prev = &(*prev)->next) {
        if (*prev == driver)
            return;
    }
    *prev = driver;
}

void
KdRemoveKeyboardDriver (KdKeyboardDriver *driver)
{
    KdKeyboardDriver *tmp;

    if (!driver)
        return;

    /* FIXME remove all keyboards using this driver */
    for (tmp = kdKeyboardDrivers; tmp; tmp = tmp->next) {
        if (tmp->next == driver)
            tmp->next = driver->next;
    }
    if (tmp == driver)
        tmp = NULL;
}

KdKeyboardInfo *
KdNewKeyboard (void)
{
888
    KdKeyboardInfo *ki = calloc(sizeof(KdKeyboardInfo), 1);
Daniel Stone's avatar
Daniel Stone committed
889
890
891
892
893
894
895
896
897
898
    if (!ki)
        return NULL;

    ki->minScanCode = 0;
    ki->maxScanCode = 0;
    ki->leds = 0;
    ki->bellPitch = 1000;
    ki->bellDuration = 200;
    ki->next = NULL;
    ki->options = NULL;
899
900
901
902
903
    ki->xkbRules = strdup(XKB_DFLT_RULES);
    ki->xkbModel = strdup(XKB_DFLT_MODEL);
    ki->xkbLayout = strdup(XKB_DFLT_LAYOUT);
    ki->xkbVariant = strdup(XKB_DFLT_VARIANT);
    ki->xkbOptions = strdup(XKB_DFLT_OPTIONS);
Daniel Stone's avatar
Daniel Stone committed
904
905
906
907
908
909
910
911
912
913
914
915

    return ki;
}

int
KdAddConfigKeyboard (char *keyboard)
{
    struct KdConfigDevice **prev, *new;

    if (!keyboard)
        return Success;

916
    new = (struct KdConfigDevice *) calloc(sizeof(struct KdConfigDevice), 1);
Daniel Stone's avatar
Daniel Stone committed
917
918
919
    if (!new)
        return BadAlloc;

920
    new->line = strdup(keyboard);
Daniel Stone's avatar
Daniel Stone committed
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
    new->next = NULL;

    for (prev = &kdConfigKeyboards; *prev; prev = &(*prev)->next);
    *prev = new;

    return Success;
}

int
KdAddKeyboard (KdKeyboardInfo *ki)
{
    KdKeyboardInfo **prev;

    if (!ki)
        return !Success;
Misha Gusarov's avatar
Misha Gusarov committed
936

937
    ki->dixdev = AddInputDevice(serverClient, KdKeyboardProc, TRUE);
Daniel Stone's avatar
Daniel Stone committed
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
    if (!ki->dixdev) {
        ErrorF("Couldn't register keyboard device %s\n",
               ki->name ? ki->name : "(unnamed)");
        return !Success;
    }

#ifdef DEBUG
    ErrorF("added keyboard %s with dix id %d\n", ki->name, ki->dixdev->id);
#endif

    for (prev = &kdKeyboards; *prev; prev = &(*prev)->next);
    *prev = ki;

    return Success;
}

void
KdRemoveKeyboard (KdKeyboardInfo *ki)
{
    KdKeyboardInfo **prev;

    if (!ki)
        return;
961

Daniel Stone's avatar
Daniel Stone committed
962
963
964
965
966
    for (prev = &kdKeyboards; *prev; prev = &(*prev)->next) {
        if (*prev == ki) {
            *prev = ki->next;
            break;
        }
967
    }
Daniel Stone's avatar
Daniel Stone committed
968
969
970
971
972
973
974
975
976
977
978
979

    KdFreeKeyboard(ki);
}

int
KdAddConfigPointer (char *pointer)
{
    struct KdConfigDevice **prev, *new;

    if (!pointer)
        return Success;

980
    new = (struct KdConfigDevice *) calloc(sizeof(struct KdConfigDevice), 1);
Daniel Stone's avatar
Daniel Stone committed
981
982
983
    if (!new)
        return BadAlloc;

984
    new->line = strdup(pointer);
Daniel Stone's avatar
Daniel Stone committed
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
    new->next = NULL;

    for (prev = &kdConfigPointers; *prev; prev = &(*prev)->next);
    *prev = new;

    return Success;
}

int
KdAddPointer (KdPointerInfo *pi)
{
    KdPointerInfo **prev;

    if (!pi)
        return Success;

    pi->mouseState = start;
    pi->eventHeld = FALSE;

1004
    pi->dixdev = AddInputDevice(serverClient, KdPointerProc, TRUE);
Daniel Stone's avatar
Daniel Stone committed
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
    if (!pi->dixdev) {
        ErrorF("Couldn't add pointer device %s\n",
               pi->name ? pi->name : "(unnamed)");
        return BadDevice;
    }

    for (prev = &kdPointers; *prev; prev = &(*prev)->next);
    *prev = pi;

    return Success;
}

void
KdRemovePointer (KdPointerInfo *pi)
{
    KdPointerInfo **prev;

    if (!pi)
        return;

    for (prev = &kdPointers; *prev; prev = &(*prev)->next) {
        if (*prev == pi) {
            *prev = pi->next;
            break;
        }
    }

    KdFreePointer(pi);
}

Misha Gusarov's avatar
Misha Gusarov committed
1035
/*
1036
1037
 * You can call your kdriver server with something like:
 * $ ./hw/kdrive/yourserver/X :1 -mouse evdev,,device=/dev/input/event4 -keybd
Misha Gusarov's avatar
Misha Gusarov committed
1038
 * evdev,,device=/dev/input/event1,xkbmodel=abnt2,xkblayout=br
1039
 */
Misha Gusarov's avatar
Misha Gusarov committed
1040
static Bool
Tiago Vignatti's avatar
Tiago Vignatti committed
1041
1042
1043
1044
1045
KdGetOptions (InputOption **options, char *string)
{
    InputOption     *newopt = NULL, **tmpo = NULL;
    int             tam_key = 0;

1046
    newopt = calloc(1, sizeof (InputOption));
Tiago Vignatti's avatar
Tiago Vignatti committed
1047
1048
1049
1050
    if (!newopt)
        return FALSE;

    for (tmpo = options; *tmpo; tmpo = &(*tmpo)->next)
Misha Gusarov's avatar
Misha Gusarov committed
1051
        ; /* Hello, I'm here */
Tiago Vignatti's avatar
Tiago Vignatti committed
1052
1053
1054
1055
1056
    *tmpo = newopt;

    if (strchr(string, '='))
    {
        tam_key = (strchr(string, '=') - string);
1057
        newopt->key = (char *)malloc(tam_key);
Tiago Vignatti's avatar
Tiago Vignatti committed
1058
1059
        strncpy(newopt->key, string, tam_key);
        newopt->key[tam_key] = '\0';
1060
        newopt->value = strdup(strchr(string, '=') + 1);
Tiago Vignatti's avatar
Tiago Vignatti committed
1061
1062
1063
    }
    else
    {
1064
        newopt->key = strdup(string);
Tiago Vignatti's avatar
Tiago Vignatti committed
1065
1066
1067
1068
1069
1070
1071
        newopt->value = NULL;
    }
    newopt->next = NULL;

    return TRUE;
}

1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
static void
KdParseKbdOptions (KdKeyboardInfo *ki)
{
    InputOption *option = NULL;

    for (option = ki->options; option; option = option->next)
    {
        if (strcasecmp(option->key, "XkbRules") == 0)
            ki->xkbRules = option->value;
        else if (strcasecmp(option->key, "XkbModel") == 0)
            ki->xkbModel = option->value;
        else if (strcasecmp(option->key, "XkbLayout") == 0)
            ki->xkbLayout = option->value;
        else if (strcasecmp(option->key, "XkbVariant") == 0)
            ki->xkbVariant = option->value;
        else if (strcasecmp(option->key, "XkbOptions") == 0)
            ki->xkbOptions = option->value;
1089
        else if (!strcasecmp (option->key, "device"))
Adam Jackson's avatar
Adam Jackson committed
1090
            ki->path = strdup(option->value);
Tiago Vignatti's avatar
Tiago Vignatti committed
1091
        else
Misha Gusarov's avatar
Misha Gusarov committed
1092
           ErrorF("Kbd option key (%s) of value (%s) not assigned!\n",
1093
1094
1095
1096
                    option->key, option->value);
    }
}

Daniel Stone's avatar
Daniel Stone committed
1097
1098
1099
1100
1101
KdKeyboardInfo *
KdParseKeyboard (char *arg)
{
    char            save[1024];
    char            delim;
Tiago Vignatti's avatar
Tiago Vignatti committed
1102
    InputOption     *options = NULL;
Daniel Stone's avatar
Daniel Stone committed
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
    KdKeyboardInfo     *ki = NULL;

    ki = KdNewKeyboard();
    if (!ki)
        return NULL;

    ki->name = strdup("Unknown KDrive Keyboard");
    ki->path = NULL;
    ki->driver = NULL;
    ki->driverPrivate = NULL;
    ki->next = NULL;

    if (!arg)
    {
        ErrorF("keybd: no arg\n");
        KdFreeKeyboard (ki);
        return NULL;
    }

    if (strlen (arg) >= sizeof (save))
    {
        ErrorF("keybd: arg too long\n");
        KdFreeKeyboard (ki);
        return NULL;
    }

    arg = KdParseFindNext (arg, ",", save, &delim);
    if (!save[0])
    {
        ErrorF("keybd: failed on save[0]\n");
        KdFreeKeyboard (ki);
        return NULL;
    }

    if (strcmp (save, "auto") == 0)
        ki->driverPrivate = NULL;
    else
1140
        ki->driverPrivate = strdup(save);
Daniel Stone's avatar
Daniel Stone committed
1141

1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
    if (delim != ',')
    {
        return ki;
    }

    arg = KdParseFindNext (arg, ",", save, &delim);

    while (delim == ',')
    {
        arg = KdParseFindNext (arg, ",", save, &delim);

Misha Gusarov's avatar
Misha Gusarov committed
1153
	if (!KdGetOptions(&options, save))
Tiago Vignatti's avatar
Tiago Vignatti committed
1154
1155
1156
	{
	    KdFreeKeyboard(ki);
	    return NULL;
Misha Gusarov's avatar
Misha Gusarov committed
1157
        }
1158
1159
1160
1161
1162
1163
1164
    }

    if (options)
    {
        ki->options = options;
        KdParseKbdOptions(ki);
    }
Daniel Stone's avatar
Daniel Stone committed
1165
1166
1167
1168

    return ki;
}

1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
static void
KdParsePointerOptions (KdPointerInfo *pi)
{
    InputOption *option = NULL;

    for (option = pi->options; option; option = option->next)
    {
        if (!strcmp (option->key, "emulatemiddle"))
            pi->emulateMiddleButton = TRUE;
        else if (!strcmp (option->key, "noemulatemiddle"))
            pi->emulateMiddleButton = FALSE;
        else if (!strcmp (option->key, "transformcoord"))
            pi->transformCoordinates = TRUE;
        else if (!strcmp (option->key, "rawcoord"))
            pi->transformCoordinates = FALSE;
1184
        else if (!strcasecmp (option->key, "device"))
Adam Jackson's avatar
Adam Jackson committed
1185
            pi->path = strdup(option->value);
1186
1187
        else if (!strcasecmp (option->key, "protocol"))
            pi->protocol = strdup(option->value);
1188
        else
Misha Gusarov's avatar
Misha Gusarov committed
1189
            ErrorF("Pointer option key (%s) of value (%s) not assigned!\n",
1190
1191
1192
1193
                    option->key, option->value);
    }
}

Daniel Stone's avatar
Daniel Stone committed
1194
1195
1196
1197
1198
1199
KdPointerInfo *
KdParsePointer (char *arg)
{
    char            save[1024];
    char            delim;
    KdPointerInfo   *pi = NULL;
Tiago Vignatti's avatar
Tiago Vignatti committed
1200
    InputOption     *options = NULL;
Daniel Stone's avatar
Daniel Stone committed
1201
1202
1203
1204
1205
1206
1207
    int             i = 0;

    pi = KdNewPointer();
    if (!pi)
        return NULL;
    pi->emulateMiddleButton = kdEmulateMiddleButton;
    pi->transformCoordinates = !kdRawPointerCoordinates;
1208
    pi->protocol = NULL;
1209
    pi->nButtons = 5; /* XXX should not be hardcoded */
Daniel Stone's avatar
Daniel Stone committed
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
    pi->inputClass = KD_MOUSE;

    if (!arg)
    {
        ErrorF("mouse: no arg\n");
        KdFreePointer (pi);
        return NULL;
    }

    if (strlen (arg) >= sizeof (save))
    {
        ErrorF("mouse: arg too long\n");
        KdFreePointer (pi);
        return NULL;
    }
    arg = KdParseFindNext (arg, ",", save, &delim);
    if (!save[0])
    {
        ErrorF("failed on save[0]\n");
        KdFreePointer (pi);
        return NULL;
    }

    if (strcmp(save, "auto") == 0)
        pi->driverPrivate = NULL;
    else
1236
        pi->driverPrivate = strdup(save);
Daniel Stone's avatar
Daniel Stone committed
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262

    if (delim != ',')
    {
        return pi;
    }

    arg = KdParseFindNext (arg, ",", save, &delim);

    while (delim == ',')
    {
        arg = KdParseFindNext (arg, ",", save, &delim);
        if (save[0] == '{')
        {
            char *s = save + 1;
             i = 0;
             while (*s && *s != '}')
             {
                if ('1' <= *s && *s <= '0' + pi->nButtons)
                    pi->map[i] = *s - '0';
                else
                    UseMsg ();
                s++;
             }
        }
        else
        {
Tiago Vignatti's avatar
Tiago Vignatti committed
1263
            if (!KdGetOptions(&options, save))
Daniel Stone's avatar
Daniel Stone committed
1264
1265
1266
1267
1268
1269
1270
1271
            {
                KdFreePointer(pi);
                return NULL;
            }
        }
    }

    if (options)
1272
    {
Daniel Stone's avatar
Daniel Stone committed
1273
        pi->options = options;
1274
1275
        KdParsePointerOptions(pi);
    }
Daniel Stone's avatar
Daniel Stone committed
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304

    return pi;
}


void
KdInitInput (void)
{
    KdPointerInfo *pi;
    KdKeyboardInfo *ki;
    struct KdConfigDevice *dev;

    kdInputEnabled = TRUE;

    for (dev = kdConfigPointers; dev; dev = dev->next) {
        pi = KdParsePointer(dev->line);
        if (!pi)
            ErrorF("Failed to parse pointer\n");
        if (KdAddPointer(pi) != Success)
            ErrorF("Failed to add pointer!\n");
    }
    for (dev = kdConfigKeyboards; dev; dev = dev->next) {
        ki = KdParseKeyboard(dev->line);
        if (!ki)
            ErrorF("Failed to parse keyboard\n");
        if (KdAddKeyboard(ki) != Success)
            ErrorF("Failed to add keyboard!\n");
    }

Daniel Stone's avatar
Daniel Stone committed
1305
    mieqInit();
1306
1307
}

1308
1309
1310
1311
1312
1313
void
KdCloseInput (void)
{
    mieqFini();
}

1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
/*
 * Middle button emulation state machine
 *
 *  Possible transitions:
 *	Button 1 press	    v1
 *	Button 1 release    ^1
 *	Button 2 press	    v2
 *	Button 2 release    ^2
 *	Button 3 press	    v3
 *	Button 3 release    ^3
1324
1325
 *	Button other press  vo
 *	Button other release ^o
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
 *	Mouse motion	    <>
 *	Keyboard event	    k
 *	timeout		    ...
 *	outside box	    <->
 *
 *  States:
 *	start
 *	button_1_pend
 *	button_1_down
 *	button_2_down
 *	button_3_pend
 *	button_3_down
 *	synthetic_2_down_13
 *	synthetic_2_down_3
 *	synthetic_2_down_1
 *
 *  Transition diagram
 *
 *  start
 *	v1  -> (hold) (settimeout) button_1_pend
 *	^1  -> (deliver) start
 *	v2  -> (deliver) button_2_down
 *	^2  -> (deliever) start
 *	v3  -> (hold) (settimeout) button_3_pend
 *	^3  -> (deliver) start
1351
1352
 *	vo  -> (deliver) start
 *	^o  -> (deliver) start
1353
1354
1355
1356
1357
1358
1359
1360
1361
 *	<>  -> (deliver) start
 *	k   -> (deliver) start
 *
 *  button_1_pend	(button 1 is down, timeout pending)
 *	^1  -> (release) (deliver) start
 *	v2  -> (release) (deliver) button_1_down
 *	^2  -> (release) (deliver) button_1_down
 *	v3  -> (cleartimeout) (generate v2) synthetic_2_down_13
 *	^3  -> (release) (deliver) button_1_down
1362
1363
 *	vo  -> (release) (deliver) button_1_down
 *	^o  -> (release) (deliver) button_1_down
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
 *	<-> -> (release) (deliver) button_1_down
 *	<>  -> (deliver) button_1_pend
 *	k   -> (release) (deliver) button_1_down
 *	... -> (release) button_1_down
 *
 *  button_1_down	(button 1 is down)
 *	^1  -> (deliver) start
 *	v2  -> (deliver) button_1_down
 *	^2  -> (deliver) button_1_down
 *	v3  -> (deliver) button_1_down
 *	^3  -> (deliver) button_1_down
1375
1376
 *	vo  -> (deliver) button_1_down
 *	^o  -> (deliver) button_1_down
1377
1378
1379
1380
1381
1382
1383
1384
1385
 *	<>  -> (deliver) button_1_down
 *	k   -> (deliver) button_1_down
 *
 *  button_2_down	(button 2 is down)
 *	v1  -> (deliver) button_2_down
 *	^1  -> (deliver) button_2_down
 *	^2  -> (deliver) start
 *	v3  -> (deliver) button_2_down
 *	^3  -> (deliver) button_2_down
1386
1387
 *	vo  -> (deliver) button_2_down
 *	^o  -> (deliver) button_2_down
1388
1389
1390
1391
1392
1393
1394
1395
1396
 *	<>  -> (deliver) button_2_down
 *	k   -> (deliver) button_2_down
 *
 *  button_3_pend	(button 3 is down, timeout pending)
 *	v1  -> (generate v2) synthetic_2_down
 *	^1  -> (release) (deliver) button_3_down
 *	v2  -> (release) (deliver) button_3_down
 *	^2  -> (release) (deliver) button_3_down
 *	^3  -> (release) (deliver) start
1397
1398
 *	vo  -> (release) (deliver) button_3_down
 *	^o  -> (release) (deliver) button_3_down
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
 *	<-> -> (release) (deliver) button_3_down
 *	<>  -> (deliver) button_3_pend
 *	k   -> (release) (deliver) button_3_down
 *	... -> (release) button_3_down
 *
 *  button_3_down	(button 3 is down)
 *	v1  -> (deliver) button_3_down
 *	^1  -> (deliver) button_3_down
 *	v2  -> (deliver) button_3_down
 *	^2  -> (deliver) button_3_down
 *	^3  -> (deliver) start
1410
1411
 *	vo  -> (deliver) button_3_down
 *	^o  -> (deliver) button_3_down
1412
1413
1414
1415
1416
1417
1418
1419
 *	<>  -> (deliver) button_3_down
 *	k   -> (deliver) button_3_down
 *
 *  synthetic_2_down_13	(button 1 and 3 are down)
 *	^1  -> (generate ^2) synthetic_2_down_3
 *	v2  -> synthetic_2_down_13
 *	^2  -> synthetic_2_down_13
 *	^3  -> (generate ^2) synthetic_2_down_1
1420
1421
 *	vo  -> (deliver) synthetic_2_down_13
 *	^o  -> (deliver) synthetic_2_down_13
1422
1423
1424
1425
1426
1427
1428
1429
1430
 *	<>  -> (deliver) synthetic_2_down_13
 *	k   -> (deliver) synthetic_2_down_13
 *
 *  synthetic_2_down_3 (button 3 is down)
 *	v1  -> (deliver) synthetic_2_down_3
 *	^1  -> (deliver) synthetic_2_down_3
 *	v2  -> synthetic_2_down_3
 *	^2  -> synthetic_2_down_3
 *	^3  -> start
1431
1432
 *	vo  -> (deliver) synthetic_2_down_3
 *	^o  -> (deliver) synthetic_2_down_3
1433
1434
1435
1436
1437
1438
1439
1440
1441
 *	<>  -> (deliver) synthetic_2_down_3
 *	k   -> (deliver) synthetic_2_down_3
 *
 *  synthetic_2_down_1 (button 1 is down)
 *	^1  -> start
 *	v2  -> synthetic_2_down_1
 *	^2  -> synthetic_2_down_1
 *	v3  -> (deliver) synthetic_2_down_1
 *	^3  -> (deliver) synthetic_2_down_1
1442
1443
 *	vo  -> (deliver) synthetic_2_down_1
 *	^o  -> (deliver) synthetic_2_down_1
1444
1445
1446
 *	<>  -> (deliver) synthetic_2_down_1
 *	k   -> (deliver) synthetic_2_down_1
 */
Misha Gusarov's avatar
Misha Gusarov committed
1447

1448
1449
1450
1451
typedef enum _inputClass {
    down_1, up_1,
    down_2, up_2,
    down_3, up_3,
1452
    down_o, up_o,
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
    motion, outside_box,
    keyboard, timeout,
    num_input_class
} KdInputClass;

typedef enum _inputAction {
    noop,
    hold,
    setto,
    deliver,
    release,
    clearto,
    gen_down_2,
    gen_up_2
} KdInputAction;

#define MAX_ACTIONS 2

typedef struct _inputTransition {
    KdInputAction  actions[MAX_ACTIONS];
Daniel Stone's avatar
Daniel Stone committed
1473
    KdPointerState nextState;
1474
1475
} KdInputTransition;

1476
static const
1477
1478
1479
1480
1481
1482
1483
1484
1485
KdInputTransition  kdInputMachine[num_input_states][num_input_class] = {
    /* start */
    {
	{ { hold, setto },	    button_1_pend },	/* v1 */
	{ { deliver, noop },	    start },		/* ^1 */
	{ { deliver, noop },	    button_2_down },	/* v2 */
	{ { deliver, noop },	    start },		/* ^2 */
	{ { hold, setto },	    button_3_pend },	/* v3 */
	{ { deliver, noop },	    start },		/* ^3 */
1486
1487
	{ { deliver, noop },	    start },		/* vo */
	{ { deliver, noop },	    start },		/* ^o */
1488
1489
	{ { deliver, noop },	    start },		/* <> */
	{ { deliver, noop },	    start },		/* <-> */
1490
	{ { noop, noop },	    start },		/* k */
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
	{ { noop, noop },	    start },		/* ... */
    },
    /* button_1_pend */
    {
	{ { noop, noop },	    button_1_pend },	/* v1 */
	{ { release, deliver },	    start },		/* ^1 */
	{ { release, deliver },	    button_1_down },	/* v2 */
	{ { release, deliver },	    button_1_down },	/* ^2 */
	{ { clearto, gen_down_2 },  synth_2_down_13 },	/* v3 */
	{ { release, deliver },	    button_1_down },	/* ^3 */
1501
1502
	{ { release, deliver },	    button_1_down },	/* vo */
	{ { release, deliver },	    button_1_down },	/* ^o */
1503
1504
	{ { deliver, noop },	    button_1_pend },	/* <> */
	{ { release, deliver },	    button_1_down },	/* <-> */