kinput.c 37.5 KB
Newer Older
1
/*
2
 * Id: kinput.c,v 1.1 1999/11/02 03:54:46 keithp Exp $
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 *
 * Copyright  1999 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.
 */
24
/* $XFree86: xc/programs/Xserver/hw/kdrive/kinput.c,v 1.29 2002/11/05 05:28:34 keithp Exp $ */
25
26
27
28

#include "kdrive.h"
#include "inputstr.h"

29
#define XK_PUBLISHING
30
#include <X11/keysym.h>
31
#include <X11/XF86keysym.h>
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "kkeymap.h"
#include <signal.h>
#include <stdio.h>

static DeviceIntPtr	pKdKeyboard, pKdPointer;

static KdMouseFuncs	*kdMouseFuncs;
static KdKeyboardFuncs	*kdKeyboardFuncs;
static unsigned long	kdEmulationTimeout;
static Bool		kdTimeoutPending;
static int		kdBellPitch;
static int		kdBellDuration;
static int		kdLeds;
45
static Bool		kdInputEnabled;
46
47
static Bool		kdOffScreen;
static unsigned long	kdOffScreenTime;
48
static KdMouseMatrix	kdMouseMatrix = {
49
50
   { { 1, 0, 0 },
     { 0, 1, 0 } }
51
};
52

53
#ifdef TOUCHSCREEN
54
static KdMouseFuncs	*kdTsFuncs;
55
56
#endif

57
int		kdMouseButtonCount;
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
int		kdMinScanCode;
int		kdMaxScanCode;
int		kdMinKeyCode;
int		kdMaxKeyCode;
int		kdKeymapWidth = KD_MAX_WIDTH;
KeySym		kdKeymap[KD_MAX_LENGTH * KD_MAX_WIDTH];
CARD8		kdModMap[MAP_LENGTH];
KeySymsRec	kdKeySyms;


void
KdResetInputMachine (void);
    
#define KD_KEY_COUNT		248

CARD8		kdKeyState[KD_KEY_COUNT/8];

#define IsKeyDown(key) ((kdKeyState[(key) >> 3] >> ((key) & 7)) & 1)

77
78
79
80
81
82
#define KD_MAX_INPUT_FDS    8

typedef struct _kdInputFd {
    int	    type;
    int	    fd;
    void    (*read) (int fd, void *closure);
83
    int	    (*enable) (int fd, void *closure);
84
    void    (*disable) (int fd, void *closure);
85
86
87
88
89
90
91
    void    *closure;
} KdInputFd;

KdInputFd    	kdInputFds[KD_MAX_INPUT_FDS];
int		kdNumInputFds;
int		kdInputTypeSequence;

92
93
94
void
KdSigio (int sig)
{
95
96
97
98
    int	i;

    for (i = 0; i < kdNumInputFds; i++)
	(*kdInputFds[i].read) (kdInputFds[i].fd, kdInputFds[i].closure);
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
}

void
KdBlockSigio (void)
{
    sigset_t	set;
    
    sigemptyset (&set);
    sigaddset (&set, SIGIO);
    sigprocmask (SIG_BLOCK, &set, 0);
}

void
KdUnblockSigio (void)
{
    sigset_t	set;
    
    sigemptyset (&set);
    sigaddset (&set, SIGIO);
    sigprocmask (SIG_UNBLOCK, &set, 0);
}

121
#undef VERIFY_SIGIO
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#ifdef VERIFY_SIGIO

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

    sigemptyset (&set);
    sigprocmask (SIG_BLOCK, &set, &old);
    if (!sigismember (&old, SIGIO))
	ErrorF ("SIGIO not blocked at %s\n", where);
}

#else

137
#define KdAssertSigioBlocked(s)
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161

#endif

static int  kdnFds;

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

void
KdAddFd (int fd)
{
    int	flags;
    struct sigaction	act;
    sigset_t		set;
    
    kdnFds++;
    fcntl (fd, F_SETOWN, getpid());
    flags = fcntl (fd, F_GETFL);
    flags |= FASYNC|NOBLOCK;
    fcntl (fd, F_SETFL, flags);
    AddEnabledDevice (fd);
162
    memset (&act, '\0', sizeof act);
163
164
    act.sa_handler = KdSigio;
    sigemptyset (&act.sa_mask);
Keith Packard's avatar
Keith Packard committed
165
166
167
    sigaddset (&act.sa_mask, SIGIO);
    sigaddset (&act.sa_mask, SIGALRM);
    sigaddset (&act.sa_mask, SIGVTALRM);
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
    sigaction (SIGIO, &act, 0);
    sigemptyset (&set);
    sigprocmask (SIG_SETMASK, &set, 0);
}

void
KdRemoveFd (int fd)
{
    struct sigaction	act;
    int			flags;
    
    kdnFds--;
    RemoveEnabledDevice (fd);
    flags = fcntl (fd, F_GETFL);
    flags &= ~(FASYNC|NOBLOCK);
    fcntl (fd, F_SETFL, flags);
    if (kdnFds == 0)
    {
186
	memset (&act, '\0', sizeof act);
187
188
189
190
191
192
	act.sa_handler = SIG_IGN;
	sigemptyset (&act.sa_mask);
	sigaction (SIGIO, &act, 0);
    }
}

193
194
195
196
197
198
199
200
201
202
203
204
205
206
int
KdAllocInputType (void)
{
    return ++kdInputTypeSequence;
}

Bool
KdRegisterFd (int type, int fd, void (*read) (int fd, void *closure), void *closure)
{
    if (kdNumInputFds == KD_MAX_INPUT_FDS)
	return FALSE;
    kdInputFds[kdNumInputFds].type = type;
    kdInputFds[kdNumInputFds].fd = fd;
    kdInputFds[kdNumInputFds].read = read;
207
208
    kdInputFds[kdNumInputFds].enable = 0;
    kdInputFds[kdNumInputFds].disable = 0;
209
210
211
212
213
214
215
    kdInputFds[kdNumInputFds].closure = closure;
    ++kdNumInputFds;
    if (kdInputEnabled)
	KdAddFd (fd);
    return TRUE;
}

216
217
void
KdRegisterFdEnableDisable (int fd, 
218
			   int (*enable) (int fd, void *closure),
219
220
221
222
223
224
225
226
227
228
229
230
231
			   void (*disable) (int fd, void *closure))
{
    int	i;
    
    for (i = 0; i < kdNumInputFds; i++)
	if (kdInputFds[i].fd == fd)
	{
	    kdInputFds[i].enable = enable;
	    kdInputFds[i].disable = disable;
	    break;
	}
}
			   
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
void
KdUnregisterFds (int type, Bool do_close)
{
    int	i;

    for (i = 0; i < kdNumInputFds;)
    {
	if (kdInputFds[i].type == type)
	{
	    if (kdInputEnabled)
		KdRemoveFd (kdInputFds[i].fd);
	    if (do_close)
		close (kdInputFds[i].fd);
	    --kdNumInputFds;
	    for (; i < kdNumInputFds; i++)
		kdInputFds[i] = kdInputFds[i+1];
	}
	else
	    i++;
    }
}

254
255
256
void
KdDisableInput (void)
{
257
258
259
    int	i;

    for (i = 0; i < kdNumInputFds; i++)
260
    {
261
	KdRemoveFd (kdInputFds[i].fd);
262
263
264
	if (kdInputFds[i].disable)
	    (*kdInputFds[i].disable) (kdInputFds[i].fd, kdInputFds[i].closure);
    }
265
    kdInputEnabled = FALSE;
266
267
268
269
270
}

void
KdEnableInput (void)
{
271
    xEvent	xE;
272
    int	i;
273
274
    
    kdInputEnabled = TRUE;
275
    for (i = 0; i < kdNumInputFds; i++)
276
277
    {
	if (kdInputFds[i].enable)
278
279
	    kdInputFds[i].fd = (*kdInputFds[i].enable) (kdInputFds[i].fd, kdInputFds[i].closure);
	KdAddFd (kdInputFds[i].fd);
280
    }
281
    
282
283
284
    /* reset screen saver */
    xE.u.keyButtonPointer.time = GetTimeInMillis ();
    NoticeEventTime (&xE);
285
286
287
288
289
}

static int
KdMouseProc(DeviceIntPtr pDevice, int onoff)
{
290
    BYTE	map[KD_MAX_BUTTON];
291
292
    DevicePtr	pDev = (DevicePtr)pDevice;
    int		i;
293
    KdMouseInfo	*mi;
294
295
296
297
298
299
300
    
    if (!pDev)
	return BadImplementation;
    
    switch (onoff)
    {
    case DEVICE_INIT:
301
	for (i = 1; i <= kdMouseButtonCount; i++)
302
	    map[i] = i;
303
	InitPointerDeviceStruct(pDev, map, kdMouseButtonCount,
304
305
306
307
308
309
310
311
	    miPointerGetMotionEvents,
	    (PtrCtrlProcPtr)NoopDDA,
	    miPointerGetMotionBufferSize());
	break;
	
    case DEVICE_ON:
	pDev->on = TRUE;
	pKdPointer = pDevice;
312
313
#ifdef TOUCHSCREEN
	if (kdTsFuncs)
314
	    (*kdTsFuncs->Init) ();
315
#endif
Keith Packard's avatar
Keith Packard committed
316
317
	if (kdMouseFuncs)
	    (*kdMouseFuncs->Init) ();
318
319
320
321
322
323
324
	break;
    case DEVICE_OFF:
    case DEVICE_CLOSE:
	if (pDev->on)
	{
	    pDev->on = FALSE;
	    pKdPointer = 0;
325
326
	    if (kdMouseFuncs)
		(*kdMouseFuncs->Fini) ();
327
#ifdef TOUCHSCREEN
328
	    if (kdTsFuncs)
329
		(*kdTsFuncs->Fini) ();
330
#endif
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
	}
	break;
    }
    return Success;
}

Bool
LegalModifier(unsigned int key, DevicePtr pDev)
{
    return TRUE;
}

static void
KdBell (int volume, DeviceIntPtr pDev, pointer ctrl, int something)
{
346
347
    if (kdInputEnabled)
	(*kdKeyboardFuncs->Bell) (volume, kdBellPitch, kdBellDuration);
348
349
350
351
352
353
}


static void
KdSetLeds (void)
{
354
355
    if (kdInputEnabled)
	(*kdKeyboardFuncs->Leds) (kdLeds);
356
357
358
359
360
361
362
363
364
365
}

void
KdSetLed (int led, Bool on)
{
    NoteLedState (pKdKeyboard, led, on);
    kdLeds = pKdKeyboard->kbdfeed->ctrl.leds;
    KdSetLeds ();
}

366
367
368
369
370
371
void
KdSetMouseMatrix (KdMouseMatrix *matrix)
{
    kdMouseMatrix = *matrix;
}

372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
void
KdComputeMouseMatrix (KdMouseMatrix *m, Rotation randr, int width, int height)
{
    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:
390
391
	m->matrix[0][0] = 0; m->matrix[0][1] = -x_dir;
	m->matrix[1][0] = y_dir; m->matrix[1][1] = 0;
392
393
394
395
396
397
	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:
398
399
	m->matrix[0][0] = 0; m->matrix[0][1] = x_dir;
	m->matrix[1][0] = -y_dir; m->matrix[1][1] = 0;
400
401
402
	break;
    }
    for (i = 0; i < 2; i++)
403
404
    {
	m->matrix[i][2] = 0;
405
406
	for (j = 0 ; j < 2; j++)
	    if (m->matrix[i][j] < 0)
407
		m->matrix[i][2] = size[j] - 1;
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
static void
KdKbdCtrl (DeviceIntPtr pDevice, KeybdCtrl *ctrl)
{
    kdLeds = ctrl->leds;
    kdBellPitch = ctrl->bell_pitch;
    kdBellDuration = ctrl->bell_duration;
    KdSetLeds ();
}

static int
KdKeybdProc(DeviceIntPtr pDevice, int onoff)
{
    Bool        ret;
    DevicePtr   pDev = (DevicePtr)pDevice;

    if (!pDev)
	return BadImplementation;

    switch (onoff)
    {
    case DEVICE_INIT:
	if (pDev != LookupKeyboardDevice())
	{
	    return !Success;
	}
	ret = InitKeyboardDeviceStruct(pDev,
				       &kdKeySyms,
				       kdModMap,
				       KdBell, KdKbdCtrl);
	if (!ret)
	    return BadImplementation;
	break;
    case DEVICE_ON:
	pDev->on = TRUE;
	pKdKeyboard = pDevice;
	if (kdKeyboardFuncs)
447
	    (*kdKeyboardFuncs->Init) ();
448
449
450
451
452
453
454
	break;
    case DEVICE_OFF:
    case DEVICE_CLOSE:
	pKdKeyboard = 0;
	if (pDev->on)
	{
	    pDev->on = FALSE;
455
456
	    if (kdKeyboardFuncs)
		(*kdKeyboardFuncs->Fini) ();
457
458
459
460
461
462
463
464
	}
	break;
    }
    return Success;
}

extern KeybdCtrl defaultKeyboardControl;

465
466
static void
KdInitAutoRepeats (void)
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
{
    int		    key_code;
    unsigned char   mask;
    int		    i;
    unsigned char   *repeats;

    repeats = defaultKeyboardControl.autoRepeats;
    memset (repeats, '\0', 32);
    for (key_code = KD_MIN_KEYCODE; key_code <= KD_MAX_KEYCODE; key_code++)
    {
	if (!kdModMap[key_code])
	{
	    i = key_code >> 3;
	    mask = 1 << (key_code & 7);
	    repeats[i] |= mask;
	}
    }
}

486
const KdKeySymModsRec kdKeySymMods[] = {
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
  {  XK_Control_L,	ControlMask },
  {  XK_Control_R, ControlMask },
  {  XK_Shift_L,	ShiftMask },
  {  XK_Shift_R,	ShiftMask },
  {  XK_Caps_Lock,	LockMask },
  {  XK_Shift_Lock, LockMask },
  {  XK_Alt_L,	Mod1Mask },
  {  XK_Alt_R,	Mod1Mask },
  {  XK_Meta_L,	Mod1Mask },
  {  XK_Meta_R,	Mod1Mask },
  {  XK_Num_Lock,	Mod2Mask },
  {  XK_Super_L,	Mod3Mask },
  {  XK_Super_R,	Mod3Mask },
  {  XK_Hyper_L,	Mod3Mask },
  {  XK_Hyper_R,	Mod3Mask },
  {  XK_Mode_switch, Mod4Mask },
503
#ifdef TOUCHSCREEN
Keith Packard's avatar
Keith Packard committed
504
  /* PDA specific hacks */
505
506
507
508
509
  {  XF86XK_Start, ControlMask },
  {  XK_Menu, ShiftMask },
  {  XK_telephone, Mod1Mask },
  {  XF86XK_AudioRecord, Mod2Mask },
  {  XF86XK_Calendar, Mod3Mask }
510
511
512
513
514
#endif
};

#define NUM_SYM_MODS (sizeof(kdKeySymMods) / sizeof(kdKeySymMods[0]))

515
516
static void
KdInitModMap (void)
517
518
519
520
521
{
    int	    key_code;
    int	    row;
    int	    width;
    KeySym  *syms;
522
    int	    i;
523
524
525
526
527
528
529
530

    width = kdKeySyms.mapWidth;
    for (key_code = kdMinKeyCode; key_code <= kdMaxKeyCode; key_code++)
    {
	kdModMap[key_code] = 0;
	syms = kdKeymap + (key_code - kdMinKeyCode) * width;
	for (row = 0; row < width; row++, syms++)
	{
531
532
533
534
	    for (i = 0; i < NUM_SYM_MODS; i++) 
	    {
		if (*syms == kdKeySymMods[i].modsym) 
		    kdModMap[key_code] |= kdKeySymMods[i].modbit;
535
536
537
538
539
540
541
542
543
544
	    }
	}
    }
}

void
KdInitInput(KdMouseFuncs    *pMouseFuncs,
	    KdKeyboardFuncs *pKeyboardFuncs)
{
    DeviceIntPtr	pKeyboard, pPointer;
545
    KdMouseInfo		*mi;
546
    
547
548
549
550
551
552
553
554
555
    if (!kdMouseInfo)
	KdParseMouse (0);
    kdMouseButtonCount = 0;
    for (mi = kdMouseInfo; mi; mi = mi->next)
    {
	if (mi->nbutton > kdMouseButtonCount)
	    kdMouseButtonCount = mi->nbutton;
    }

556
557
558
559
560
561
562
563
564
565
566
567
568
569
    kdMouseFuncs = pMouseFuncs;
    kdKeyboardFuncs = pKeyboardFuncs;
    memset (kdKeyState, '\0', sizeof (kdKeyState));
    if (kdKeyboardFuncs)
	(*kdKeyboardFuncs->Load) ();
    kdMinKeyCode = kdMinScanCode + KD_KEY_OFFSET;
    kdMaxKeyCode = kdMaxScanCode + KD_KEY_OFFSET;
    kdKeySyms.map = kdKeymap;
    kdKeySyms.minKeyCode = kdMinKeyCode;
    kdKeySyms.maxKeyCode = kdMaxKeyCode;
    kdKeySyms.mapWidth = kdKeymapWidth;
    kdLeds = 0;
    kdBellPitch = 1000;
    kdBellDuration = 200;
570
    kdInputEnabled = TRUE;
571
572
    KdInitModMap ();
    KdInitAutoRepeats ();
573
574
575
576
577
578
579
    KdResetInputMachine ();
    pPointer  = AddInputDevice(KdMouseProc, TRUE);
    pKeyboard = AddInputDevice(KdKeybdProc, TRUE);
    RegisterPointerDevice(pPointer);
    RegisterKeyboardDevice(pKeyboard);
    miRegisterPointerDevice(screenInfo.screens[0], pPointer);
    mieqInit(&pKeyboard->public, &pPointer->public);
580
581
582
583
584
585
586
#ifdef XINPUT
    {
	static long zero1, zero2;

	SetExtInputCheck (&zero1, &zero2);
    }
#endif
587
588
}

589
590
#ifdef TOUCHSCREEN
void
591
KdInitTouchScreen(KdMouseFuncs *pTsFuncs)
592
593
594
595
596
{
    kdTsFuncs = pTsFuncs;
}
#endif

597
598
599
600
601
602
603
604
605
606
/*
 * 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
607
608
 *	Button other press  vo
 *	Button other release ^o
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
 *	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
634
635
 *	vo  -> (deliver) start
 *	^o  -> (deliver) start
636
637
638
639
640
641
642
643
644
 *	<>  -> (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
645
646
 *	vo  -> (release) (deliver) button_1_down
 *	^o  -> (release) (deliver) button_1_down
647
648
649
650
651
652
653
654
655
656
657
 *	<-> -> (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
658
659
 *	vo  -> (deliver) button_1_down
 *	^o  -> (deliver) button_1_down
660
661
662
663
664
665
666
667
668
 *	<>  -> (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
669
670
 *	vo  -> (deliver) button_2_down
 *	^o  -> (deliver) button_2_down
671
672
673
674
675
676
677
678
679
 *	<>  -> (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
680
681
 *	vo  -> (release) (deliver) button_3_down
 *	^o  -> (release) (deliver) button_3_down
682
683
684
685
686
687
688
689
690
691
692
 *	<-> -> (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
693
694
 *	vo  -> (deliver) button_3_down
 *	^o  -> (deliver) button_3_down
695
696
697
698
699
700
701
702
 *	<>  -> (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
703
704
 *	vo  -> (deliver) synthetic_2_down_13
 *	^o  -> (deliver) synthetic_2_down_13
705
706
707
708
709
710
711
712
713
 *	<>  -> (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
714
715
 *	vo  -> (deliver) synthetic_2_down_3
 *	^o  -> (deliver) synthetic_2_down_3
716
717
718
719
720
721
722
723
724
 *	<>  -> (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
725
726
 *	vo  -> (deliver) synthetic_2_down_1
 *	^o  -> (deliver) synthetic_2_down_1
727
728
729
730
731
732
733
734
 *	<>  -> (deliver) synthetic_2_down_1
 *	k   -> (deliver) synthetic_2_down_1
 */
 
typedef enum _inputClass {
    down_1, up_1,
    down_2, up_2,
    down_3, up_3,
735
    down_o, up_o,
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
    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];
756
    KdMouseState   nextState;
757
758
759
760
761
762
763
764
765
766
767
} KdInputTransition;

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 */
768
769
	{ { deliver, noop },	    start },		/* vo */
	{ { deliver, noop },	    start },		/* ^o */
770
771
	{ { deliver, noop },	    start },		/* <> */
	{ { deliver, noop },	    start },		/* <-> */
772
	{ { noop, noop },	    start },		/* k */
773
774
775
776
777
778
779
780
781
782
	{ { 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 */
783
784
	{ { release, deliver },	    button_1_down },	/* vo */
	{ { release, deliver },	    button_1_down },	/* ^o */
785
786
	{ { deliver, noop },	    button_1_pend },	/* <> */
	{ { release, deliver },	    button_1_down },	/* <-> */
787
	{ { noop, noop },	    button_1_down },	/* k */
788
789
790
791
792
793
794
795
796
797
	{ { release, noop },	    button_1_down },	/* ... */
    },
    /* button_1_down */
    {
	{ { noop, noop },	    button_1_down },	/* v1 */
	{ { deliver, noop },	    start },		/* ^1 */
	{ { deliver, noop },	    button_1_down },	/* v2 */
	{ { deliver, noop },	    button_1_down },	/* ^2 */
	{ { deliver, noop },	    button_1_down },	/* v3 */
	{ { deliver, noop },	    button_1_down },	/* ^3 */
798
799
	{ { deliver, noop },	    button_1_down },	/* vo */
	{ { deliver, noop },	    button_1_down },	/* ^o */
800
801
	{ { deliver, noop },	    button_1_down },	/* <> */
	{ { deliver, noop },	    button_1_down },	/* <-> */
802
	{ { noop, noop },	    button_1_down },	/* k */
803
804
805
806
807
808
809
810
811
812
	{ { noop, noop },	    button_1_down },	/* ... */
    },
    /* button_2_down */
    {
	{ { deliver, noop },	    button_2_down },	/* v1 */
	{ { deliver, noop },	    button_2_down },	/* ^1 */
	{ { noop, noop },	    button_2_down },	/* v2 */
	{ { deliver, noop },	    start },		/* ^2 */
	{ { deliver, noop },	    button_2_down },	/* v3 */
	{ { deliver, noop },	    button_2_down },	/* ^3 */
813
814
	{ { deliver, noop },	    button_2_down },	/* vo */
	{ { deliver, noop },	    button_2_down },	/* ^o */
815
816
	{ { deliver, noop },	    button_2_down },	/* <> */
	{ { deliver, noop },	    button_2_down },	/* <-> */
817
	{ { noop, noop },	    button_2_down },	/* k */
818
819
820
821
822
823
824
825
826
827
	{ { noop, noop },	    button_2_down },	/* ... */
    },
    /* button_3_pend */
    {
	{ { clearto, gen_down_2 },  synth_2_down_13 },	/* v1 */
	{ { release, deliver },	    button_3_down },	/* ^1 */
	{ { release, deliver },	    button_3_down },	/* v2 */
	{ { release, deliver },	    button_3_down },	/* ^2 */
	{ { release, deliver },	    button_3_down },	/* v3 */
	{ { release, deliver },	    start },		/* ^3 */
828
829
	{ { release, deliver },	    button_3_down },	/* vo */
	{ { release, deliver },	    button_3_down },	/* ^o */
830
831
	{ { deliver, noop },	    button_3_pend },	/* <> */
	{ { release, deliver },	    button_3_down },	/* <-> */
832
	{ { release, noop },	    button_3_down },	/* k */
833
834
835
836
837
838
839
840
841
842
	{ { release, noop },	    button_3_down },	/* ... */
    },
    /* button_3_down */
    {
	{ { deliver, noop },	    button_3_down },	/* v1 */
	{ { deliver, noop },	    button_3_down },	/* ^1 */
	{ { deliver, noop },	    button_3_down },	/* v2 */
	{ { deliver, noop },	    button_3_down },	/* ^2 */
	{ { noop, noop },	    button_3_down },	/* v3 */
	{ { deliver, noop },	    start },		/* ^3 */
843
844
	{ { deliver, noop },	    button_3_down },	/* vo */
	{ { deliver, noop },	    button_3_down },	/* ^o */
845
846
	{ { deliver, noop },	    button_3_down },	/* <> */
	{ { deliver, noop },	    button_3_down },	/* <-> */
847
	{ { noop, noop },	    button_3_down },	/* k */
848
849
850
851
852
853
854
855
856
857
	{ { noop, noop },	    button_3_down },	/* ... */
    },
    /* synthetic_2_down_13 */
    {
	{ { noop, noop },	    synth_2_down_13 },	/* v1 */
	{ { gen_up_2, noop },	    synth_2_down_3 },	/* ^1 */
	{ { noop, noop },	    synth_2_down_13 },	/* v2 */
	{ { noop, noop },	    synth_2_down_13 },	/* ^2 */
	{ { noop, noop },	    synth_2_down_13 },	/* v3 */
	{ { gen_up_2, noop },	    synth_2_down_1 },	/* ^3 */
858
859
	{ { deliver, noop },	    synth_2_down_13 },	/* vo */
	{ { deliver, noop },	    synth_2_down_13 },	/* ^o */
860
861
	{ { deliver, noop },	    synth_2_down_13 },	/* <> */
	{ { deliver, noop },	    synth_2_down_13 },	/* <-> */
862
	{ { noop, noop },	    synth_2_down_13 },	/* k */
863
864
865
866
867
868
869
870
871
872
	{ { noop, noop },	    synth_2_down_13 },	/* ... */
    },
    /* synthetic_2_down_3 */
    {
	{ { deliver, noop },	    synth_2_down_3 },	/* v1 */
	{ { deliver, noop },	    synth_2_down_3 },	/* ^1 */
	{ { deliver, noop },	    synth_2_down_3 },	/* v2 */
	{ { deliver, noop },	    synth_2_down_3 },	/* ^2 */
	{ { noop, noop },	    synth_2_down_3 },	/* v3 */
	{ { noop, noop },	    start },		/* ^3 */
873
874
	{ { deliver, noop },	    synth_2_down_3 },	/* vo */
	{ { deliver, noop },	    synth_2_down_3 },	/* ^o */
875
876
	{ { deliver, noop },	    synth_2_down_3 },	/* <> */
	{ { deliver, noop },	    synth_2_down_3 },	/* <-> */
877
	{ { noop, noop },	    synth_2_down_3 },	/* k */
878
879
880
881
882
883
884
885
886
887
	{ { noop, noop },	    synth_2_down_3 },	/* ... */
    },
    /* synthetic_2_down_1 */
    {
	{ { noop, noop },	    synth_2_down_1 },	/* v1 */
	{ { noop, noop },	    start },		/* ^1 */
	{ { deliver, noop },	    synth_2_down_1 },	/* v2 */
	{ { deliver, noop },	    synth_2_down_1 },	/* ^2 */
	{ { deliver, noop },	    synth_2_down_1 },	/* v3 */
	{ { deliver, noop },	    synth_2_down_1 },	/* ^3 */
888
889
	{ { deliver, noop },	    synth_2_down_1 },	/* vo */
	{ { deliver, noop },	    synth_2_down_1 },	/* ^o */
890
891
	{ { deliver, noop },	    synth_2_down_1 },	/* <> */
	{ { deliver, noop },	    synth_2_down_1 },	/* <-> */
892
	{ { noop, noop },	    synth_2_down_1 },	/* k */
893
894
895
896
897
	{ { noop, noop },	    synth_2_down_1 },	/* ... */
    },
};

#define EMULATION_WINDOW    10
898
#define EMULATION_TIMEOUT   100
899
900
901
902

#define EventX(e)   ((e)->u.keyButtonPointer.rootX)
#define EventY(e)   ((e)->u.keyButtonPointer.rootY)

903
int
904
KdInsideEmulationWindow (KdMouseInfo *mi, xEvent *ev)
905
906
907
{
    if (ev->u.keyButtonPointer.pad1)
    {
908
909
	mi->emulationDx += EventX(ev);
	mi->emulationDy += EventY(ev);
910
911
912
    }
    else
    {
913
914
	mi->emulationDx = EventX(&mi->heldEvent) - EventX(ev);
	mi->emulationDy = EventY(&mi->heldEvent) - EventY(ev);
915
    }
916
917
    return (abs (mi->emulationDx) < EMULATION_WINDOW &&
	    abs (mi->emulationDy) < EMULATION_WINDOW);
918
919
920
}
				     
KdInputClass
921
KdClassifyInput (KdMouseInfo *mi, xEvent *ev)
922
923
924
925
926
927
928
{
    switch (ev->u.u.type) {
    case ButtonPress:
	switch (ev->u.u.detail) {
	case 1: return down_1;
	case 2: return down_2;
	case 3: return down_3;
929
	default: return down_o;
930
931
932
933
934
935
936
	}
	break;
    case ButtonRelease:
	switch (ev->u.u.detail) {
	case 1: return up_1;
	case 2: return up_2;
	case 3: return up_3;
937
	default: return up_o;
938
939
940
	}
	break;
    case MotionNotify:
941
	if (mi->eventHeld && !KdInsideEmulationWindow(mi, ev))
942
943
944
945
946
947
	    return outside_box;
	else
	    return motion;
    default:
	return keyboard;
    }
948
    return keyboard;
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
}

#ifndef NDEBUG
char	*kdStateNames[] = {
    "start",
    "button_1_pend",
    "button_1_down",
    "button_2_down",
    "button_3_pend",
    "button_3_down",
    "synth_2_down_13",
    "synth_2_down_3",
    "synthetic_2_down_1",
    "num_input_states"
};

char	*kdClassNames[] = {
    "down_1", "up_1",
    "down_2", "up_2",
    "down_3", "up_3",
    "motion", "ouside_box",
    "keyboard", "timeout",
    "num_input_class"
};

char *kdActionNames[] = {
    "noop",
    "hold",
    "setto",
    "deliver",
    "release",
    "clearto",
    "gen_down_2",
    "gen_up_2",
};
#endif

static void
KdQueueEvent (xEvent *ev)
{
    KdAssertSigioBlocked ("KdQueueEvent");
    if (ev->u.u.type == MotionNotify)
    {
	if (ev->u.keyButtonPointer.pad1)
	{
	    ev->u.keyButtonPointer.pad1 = 0;
	    miPointerDeltaCursor (ev->u.keyButtonPointer.rootX, 
				  ev->u.keyButtonPointer.rootY, 
				  ev->u.keyButtonPointer.time);
	}
	else
	{
	    miPointerAbsoluteCursor(ev->u.keyButtonPointer.rootX, 
				    ev->u.keyButtonPointer.rootY, 
				    ev->u.keyButtonPointer.time);
	}
    }
    else
    {
	mieqEnqueue (ev);
    }
}

static void
1013
KdRunMouseMachine (KdMouseInfo *mi, KdInputClass c, xEvent *ev)
1014
1015
1016
1017
{
    KdInputTransition	*t;
    int			a;

1018
    t = &kdInputMachine[mi->mouseState][c];
1019
1020
1021
1022
1023
1024
    for (a = 0; a < MAX_ACTIONS; a++)
    {
	switch (t->actions[a]) {
	case noop:
	    break;
	case hold:
1025
1026
1027
1028
	    mi->eventHeld = TRUE;
	    mi->emulationDx = 0;
	    mi->emulationDy = 0;
	    mi->heldEvent = *ev;
1029
1030
	    break;
	case setto:
1031
1032
	    mi->emulationTimeout = GetTimeInMillis () + EMULATION_TIMEOUT;
	    mi->timeoutPending = TRUE;
1033
1034
1035
1036
1037
	    break;
	case deliver:
	    KdQueueEvent (ev);
	    break;
	case release:
1038
1039
1040
	    mi->eventHeld = FALSE;
	    mi->timeoutPending = FALSE;
	    KdQueueEvent (&mi->heldEvent);
1041
1042
	    break;
	case clearto:
1043
	    mi->timeoutPending = FALSE;
1044
1045
1046
	    break;
	case gen_down_2:
	    ev->u.u.detail = 2;
1047
	    mi->eventHeld = FALSE;
1048
1049
1050
1051
1052
1053
1054
1055
	    KdQueueEvent (ev);
	    break;
	case gen_up_2:
	    ev->u.u.detail = 2;
	    KdQueueEvent (ev);
	    break;
	}
    }
1056
    mi->mouseState = t->nextState;
1057
1058
1059
1060
1061
}

void
KdResetInputMachine (void)
{
1062
1063
1064
1065
1066
1067
1068
    KdMouseInfo	*mi;

    for (mi = kdMouseInfo; mi; mi = mi->next)
    {
	mi->mouseState = start;
	mi->eventHeld = FALSE;
    }
1069
1070
1071
}

void
1072
KdHandleMouseEvent (KdMouseInfo *mi, xEvent *ev)
1073
{
1074
1075
    if (mi->emulateMiddleButton)
	KdRunMouseMachine (mi, KdClassifyInput (mi, ev), ev);
1076
1077
1078
1079
1080
    else
	KdQueueEvent (ev);
}

void
1081
KdReceiveTimeout (KdMouseInfo *mi)
1082
{
1083
    KdRunMouseMachine (mi, timeout, 0);
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
}

#define KILL_SEQUENCE ((1L << KK_CONTROL)|(1L << KK_ALT)|(1L << KK_F8)|(1L << KK_F10))
#define SPECIAL_SEQUENCE ((1L << KK_CONTROL) | (1L << KK_ALT))
#define SETKILLKEY(b) (KdSpecialKeys |= (1L << (b)))
#define CLEARKILLKEY(b) (KdSpecialKeys &= ~(1L << (b)))
#define KEYMAP	    (pKdKeyboard->key->curKeySyms)
#define KEYCOL1(k) (KEYMAP.map[((k)-kdMinKeyCode)*KEYMAP.mapWidth])

CARD32	KdSpecialKeys = 0;

extern char dispatchException;

/*
 * kdCheckTermination
 *
 * This function checks for the key sequence that terminates the server.  When
 * detected, it sets the dispatchException flag and returns.  The key sequence
 * is:
 *	Control-Alt
 * It's assumed that the server will be waken up by the caller when this
 * function returns.
 */

extern int nClients;

void
KdCheckSpecialKeys(xEvent *xE)
{
1113
    KeySym	sym = KEYCOL1(xE->u.u.detail);
1114
1115
1116
1117
1118
1119
1120
1121
    
    if (!pKdKeyboard) return;

    /*
     * Ignore key releases
     */
    
    if (xE->u.u.type == KeyRelease) return;
1122
1123
1124
1125
1126

#ifdef XIPAQ
    /* 
     * Check for buttons 1, 2 and 3 on the iPAQ
     */
1127
1128
    if (sym == XK_Pointer_Button1 && kdMouseInfo) {
	KdEnqueueMouseEvent(kdMouseInfo, KD_MOUSE_DELTA | KD_BUTTON_1, 0, 0);
1129
1130
	return;
    }
1131
1132
    if (sym == XK_Pointer_Button2 && kdMouseInfo) {
	KdEnqueueMouseEvent(kdMouseInfo, KD_MOUSE_DELTA | KD_BUTTON_2, 0, 0);
1133
1134
	return;
    }
1135
1136
    if (sym == XK_Pointer_Button3 && kdMouseInfo) {
	KdEnqueueMouseEvent(kdMouseInfo, KD_MOUSE_DELTA | KD_BUTTON_3, 0, 0);
1137
1138
1139
1140
	return;
    }
#endif

1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
    /*
     * Check for control/alt pressed
     */
    if ((pKdKeyboard->key->state & (ControlMask|Mod1Mask)) !=
	(ControlMask|Mod1Mask))
	return;
    
    
    /*
     * Let OS function see keysym first
     */
    
    if (kdOsFuncs->SpecialKey)
	if ((*kdOsFuncs->SpecialKey) (sym))
	    return;

    /*
     * Now check for backspace or delete; these signal the
     * X server to terminate
     */
    switch (sym) {
    case XK_BackSpace:
    case XK_Delete:
    case XK_KP_Delete:
	/* 
	 * Set the dispatch exception flag so the server will terminate the
	 * next time through the dispatch loop.
	 */
	dispatchException |= DE_TERMINATE;
	break;
    }
}

/*
 * kdEnqueueKeyboardEvent
 *
 * This function converts hardware keyboard event information into an X event
 * and enqueues it using MI.  It wakes up the server before returning so that
 * the event will be processed normally.
 *
 */

void
KdHandleKeyboardEvent (xEvent *ev)
{
1186
1187
1188
1189
    int		key = ev->u.u.detail;
    int		byte;
    CARD8	bit;
    KdMouseInfo	*mi;
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
    
    byte = key >> 3;
    bit = 1 << (key & 7);
    switch (ev->u.u.type) {
    case KeyPress:
	kdKeyState[byte] |= bit;
	break;
    case KeyRelease:
	kdKeyState[byte] &= ~bit;
	break;
    }
1201
1202
1203
    for (mi = kdMouseInfo; mi; mi = mi->next)
	KdRunMouseMachine (mi, keyboard, 0);
    KdQueueEvent (ev);
1204
1205
1206
1207
1208
1209
1210
1211
}

void
KdReleaseAllKeys (void)
{
    xEvent  xE;
    int	    key;

1212
    KdBlockSigio ();
1213
1214
1215
1216
1217
1218
1219
1220
    for (key = 0; key < KD_KEY_COUNT; key++)
	if (IsKeyDown(key))
	{
	    xE.u.keyButtonPointer.time = GetTimeInMillis();
	    xE.u.u.type = KeyRelease;
	    xE.u.u.detail = key;
	    KdHandleKeyboardEvent (&xE);
	}
1221
    KdUnblockSigio ();
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
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
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
}

void
KdCheckLock (void)
{
    KeyClassPtr	    keyc = pKdKeyboard->key;
    Bool	    isSet, shouldBeSet;

    if (kdKeyboardFuncs->LockLed)
    {
	isSet = (kdLeds & (1 << (kdKeyboardFuncs->LockLed-1))) != 0;
	shouldBeSet = (keyc->state & LockMask) != 0;
	if (isSet != shouldBeSet)
	{
	    KdSetLed (kdKeyboardFuncs->LockLed, shouldBeSet);
	}
    }
}

void
KdEnqueueKeyboardEvent(unsigned char	scan_code,
		       unsigned char	is_up)
{
    unsigned char   key_code;
    xEvent	    xE;
    int		    e;
    KeyClassPtr	    keyc;

    if (!pKdKeyboard)
	return;
    keyc = pKdKeyboard->key;

    xE.u.keyButtonPointer.time = GetTimeInMillis();

    if (kdMinScanCode <= scan_code && scan_code <= kdMaxScanCode)
    {
	key_code = scan_code + KD_MIN_KEYCODE - kdMinScanCode;
	
	/*
	 * Set up this event -- the type may be modified below
	 */
	if (is_up)
	    xE.u.u.type = KeyRelease;
	else
	    xE.u.u.type = KeyPress;
	xE.u.u.detail = key_code;
	
	switch (KEYCOL1(key_code)) 
	{
	case XK_Num_Lock:
	case XK_Scroll_Lock:
	case XK_Shift_Lock:
	case XK_Caps_Lock:
	    if (xE.u.u.type == KeyRelease)
		return;
	    if (IsKeyDown (key_code))
		xE.u.u.type = KeyRelease;
	    else
		xE.u.u.type = KeyPress;
	}
	
	/*
	 * Check pressed keys which are already down
	 */
	if (IsKeyDown (key_code) && xE.u.u.type == KeyPress)
	{
	    KeybdCtrl	*ctrl = &pKdKeyboard->kbdfeed->ctrl;
	    
	    /*
	     * Check auto repeat
	     */
	    if (!ctrl->autoRepeat || keyc->modifierMap[key_code] ||
		!(ctrl->autoRepeats[key_code >> 3] & (1 << (key_code & 7))))
	    {
		return;
	    }
1298
1299
1300
1301
1302
1303
	    /*
	     * X delivers press/release even for autorepeat
	     */
	    xE.u.u.type = KeyRelease;
	    KdHandleKeyboardEvent (&xE);
	    xE.u.u.type = KeyPress;
1304
	}
1305
1306
1307
1308
	/*
	 * Check released keys which are already up
	 */
	else if (!IsKeyDown (key_code) && xE.u.u.type == KeyRelease)
1309
	{
1310
	    return;
1311
1312
1313
1314
1315
1316
	}
	KdCheckSpecialKeys (&xE);
	KdHandleKeyboardEvent (&xE);
    }
}

1317
#define SetButton(mi, b, v, s) \
1318
{\
1319
    xE.u.u.detail = mi->map[b]; \
1320
    xE.u.u.type = v; \
1321
    KdHandleMouseEvent (mi, &xE); \
1322
1323
}

1324
1325
#define Press(mi, b)         SetButton(mi, b, ButtonPress, "Down")
#define Release(mi, b)       SetButton(mi, b, ButtonRelease, "Up")
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346

/*
 * kdEnqueueMouseEvent
 *
 * This function converts hardware mouse event information into X event
 * information.  A mouse movement event is passed off to MI to generate
 * a MotionNotify event, if appropriate.  Button events are created and
 * passed off to MI for enqueueing.
 */

static int
KdMouseAccelerate (DeviceIntPtr	device, int delta)
{
    PtrCtrl *pCtrl = &device->ptrfeed->ctrl;

    if (abs(delta) > pCtrl->threshold)
	delta = (delta * pCtrl->num) / pCtrl->den;
    return delta;
}

void
1347
KdEnqueueMouseEvent(KdMouseInfo *mi, unsigned long flags, int rx, int ry)
1348
{
1349
1350
1351
1352
1353
1354
1355
    CARD32	    ms;
    xEvent	    xE;
    unsigned char   buttons;
    int		    x, y;
    int		    (*matrix)[3] = kdMouseMatrix.matrix;
    unsigned long   button;
    int		    n;
1356
1357
1358
1359
1360
1361
1362
1363

    if (!pKdPointer)
	return;
    
    ms = GetTimeInMillis();
    
    if (flags & KD_MOUSE_DELTA)
    {
1364
1365
	x = matrix[0][0] * rx + matrix[0][1] * ry;
	y = matrix[1][0] * rx + matrix[1][1] * ry;
1366
1367
1368
1369
1370
	x = KdMouseAccelerate (pKdPointer, x);
	y = KdMouseAccelerate (pKdPointer, y);
	xE.u.keyButtonPointer.pad1 = 1;
    }
    else
1371
1372
1373
    {
	x = matrix[0][0] * rx + matrix[0][1] * ry + matrix[0][2];
	y = matrix[1][0] * rx + matrix[1][1] * ry + matrix[1][2];
1374
	xE.u.keyButtonPointer.pad1 = 0;
1375
    }
1376
1377
1378
1379
1380
1381
    xE.u.keyButtonPointer.time = ms;
    xE.u.keyButtonPointer.rootX = x;
    xE.u.keyButtonPointer.rootY = y;

    xE.u.u.type = MotionNotify;
    xE.u.u.detail = 0;
1382
    KdHandleMouseEvent (mi, &xE);
1383
1384
1385

    buttons = flags;

1386
    for (button = KD_BUTTON_1, n = 0; button <= KD_BUTTON_5; button <<= 1, n++)
1387
    {
1388
	if ((mi->buttonState & button) ^ (buttons & button))
1389
	{
1390
1391
1392
1393
1394
1395
1396
1397
	    if (buttons & button)
	    {
		Press(mi, n);
	    }
	    else
	    {
		Release(mi, n);
	    }
1398
1399
	}
    }
1400
    mi->buttonState = buttons;
1401
1402
1403
}

void
1404
KdEnqueueMotionEvent (KdMouseInfo *mi, int x, int y)
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
{
    xEvent  xE;
    CARD32  ms;
    
    ms = GetTimeInMillis();
    
    xE.u.u.type = MotionNotify;
    xE.u.keyButtonPointer.time = ms;
    xE.u.keyButtonPointer.rootX = x;
    xE.u.keyButtonPointer.rootY = y;

1416
    KdHandleMouseEvent (mi, &xE);
1417
1418
1419
1420
1421
1422
1423
1424
}

void
KdBlockHandler (int		screen,
		pointer		blockData,
		pointer		timeout,
		pointer		readmask)
{
1425
    KdMouseInfo		    *mi;
1426
    
1427
    for (mi = kdMouseInfo; mi; mi = mi->next)
1428
    {
1429
	if (mi->timeoutPending)
1430
	{
1431
1432
1433
1434
1435
1436
	    int	ms;
    
	    ms = mi->emulationTimeout - GetTimeInMillis ();
	    if (ms < 0)
		ms = 0;
	    AdjustWaitForDelay (timeout, ms);
1437
1438
1439
1440
1441
1442
1443
	}
    }
}

void
KdWakeupHandler (int		screen, 
		 pointer    	data,
1444
		 unsigned long	lresult,
1445
1446
		 pointer	readmask)
{
1447
1448
1449
1450
    int		result = (int) lresult;
    fd_set	*pReadmask = (fd_set *) readmask;
    int		i;
    KdMouseInfo	*mi;
1451
    
1452
    if (kdInputEnabled && result > 0)
1453
    {
1454
1455
1456
1457
1458
1459
1460
	for (i = 0; i < kdNumInputFds; i++)
	    if (FD_ISSET (kdInputFds[i].fd, pReadmask))
	    {
		KdBlockSigio ();
		(*kdInputFds[i].read) (kdInputFds[i].fd, kdInputFds[i].closure);
		KdUnblockSigio ();
	    }
1461
    }
1462
    for (mi = kdMouseInfo; mi; mi = mi->next)
1463
    {
1464
	if (mi->timeoutPending)
1465
	{
1466
1467
1468
1469
1470
1471
1472
	    if ((long) (GetTimeInMillis () - mi->emulationTimeout) >= 0)
	    {
		mi->timeoutPending = FALSE;
		KdBlockSigio ();
		KdReceiveTimeout (mi);
		KdUnblockSigio ();
	    }
1473
1474
1475
1476
1477
1478
	}
    }
    if (kdSwitchPending)
	KdProcessSwitch ();
}

1479
1480
#define KdScreenOrigin(pScreen) (&(KdGetScreenPriv(pScreen)->screen->origin))

1481
1482
1483
1484
static Bool
KdCursorOffScreen(ScreenPtr *ppScreen, int *x, int *y)
{
    ScreenPtr	pScreen  = *ppScreen;
1485
    ScreenPtr	pNewScreen;
1486
    int		n;
1487
1488
1489
    int		dx, dy;
    int		best_x, best_y;
    int		n_best_x, n_best_y;
1490
    CARD32	ms;
1491
1492
1493
    
    if (kdDisableZaphod || screenInfo.numScreens <= 1)
	return FALSE;
1494
1495
1496

    if (0 <= *x && *x < pScreen->width && 0 <= *y && *y < pScreen->height)
	return FALSE;
1497
	
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
    ms = GetTimeInMillis ();
    if (kdOffScreen && (int) (ms - kdOffScreenTime) < 1000)
	return FALSE;
    kdOffScreen = TRUE;
    kdOffScreenTime = ms;
    n_best_x = -1;
    best_x = 32767;
    n_best_y = -1;
    best_y = 32767;
    for (n = 0; n < screenInfo.numScreens; n++)
    {
	pNewScreen = screenInfo.screens[n];
	if (pNewScreen == pScreen) 
	    continue;
	dx = KdScreenOrigin(pNewScreen)->x - KdScreenOrigin(pScreen)->x;
	dy = KdScreenOrigin(pNewScreen)->y - KdScreenOrigin(pScreen)->y;
1514
	if (*x < 0)
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
	{
	    if (dx <= 0 && -dx < best_x)
	    {
		best_x = -dx;
		n_best_x = n;
	    }
	}
	else if (*x >= pScreen->width)
	{
	    if (dx >= 0 && dx < best_x)
	    {
		best_x = dx;
		n_best_x = n;
	    }
	}
1530
	if (*y < 0)
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
	{
	    if (dy <= 0 && -dy < best_y)
	    {
		best_y = -dy;
		n_best_y = n;
	    }
	}
	else if (*y >= pScreen->height)
	{
	    if (dy >= 0 && dy < best_y)
	    {
		best_y = dy;
		n_best_y = n;
	    }
	}
1546
    }
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
    if (best_y < best_x)
	n_best_x = n_best_y;
    if (n_best_x == -1)
	return FALSE;
    pNewScreen = screenInfo.screens[n_best_x];
    
    if (*x < 0)
	*x += pNewScreen->width;
    if (*y < 0)
	*y += pNewScreen->height;
    
    if (*x >= pScreen->width)
	*x -= pScreen->width;
    if (*y >= pScreen->height)
	*y -= pScreen->height;
    
    *ppScreen = pNewScreen;
    return TRUE;
1565
1566
1567
1568
1569
}

static void
KdCrossScreen(ScreenPtr pScreen, Bool entering)
{
1570
#ifndef XIPAQ
1571
1572
1573
1574
    if (entering)
	KdEnableScreen (pScreen);
    else
	KdDisableScreen (pScreen);
1575
#endif
1576
1577
}