kinput.c 36.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.
 */
Marc Aurele La France's avatar
Marc Aurele La France committed
24
/* $XFree86: xc/programs/Xserver/hw/kdrive/kinput.c,v 1.22 2001/10/12 06:33:07 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
84
    void    (*enable) (int fd, void *closure);
    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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
void
KdRegisterFdEnableDisable (int fd, 
			   void (*enable) (int fd, void *closure),
			   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
	KdAddFd (kdInputFds[i].fd);
278
279
280
	if (kdInputFds[i].enable)
	    (*kdInputFds[i].enable) (kdInputFds[i].fd, kdInputFds[i].closure);
    }
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
312
	    miPointerGetMotionEvents,
	    (PtrCtrlProcPtr)NoopDDA,
	    miPointerGetMotionBufferSize());
	break;
	
    case DEVICE_ON:
	pDev->on = TRUE;
	pKdPointer = pDevice;
	if (kdMouseFuncs)
313
	    (*kdMouseFuncs->Init) ();
314
315
#ifdef TOUCHSCREEN
	if (kdTsFuncs)
316
	    (*kdTsFuncs->Init) ();
317
#endif
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
329
	    if (kdTsFuncs >= 0)
		(*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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
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)
408
	    (*kdKeyboardFuncs->Init) ();
409
410
411
412
413
414
415
	break;
    case DEVICE_OFF:
    case DEVICE_CLOSE:
	pKdKeyboard = 0;
	if (pDev->on)
	{
	    pDev->on = FALSE;
416
417
	    if (kdKeyboardFuncs)
		(*kdKeyboardFuncs->Fini) ();
418
419
420
421
422
423
424
425
	}
	break;
    }
    return Success;
}

extern KeybdCtrl defaultKeyboardControl;

426
427
static void
KdInitAutoRepeats (void)
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
{
    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;
	}
    }
}

447
const KdKeySymModsRec kdKeySymMods[] = {
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
  {  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 },
464
#ifdef TOUCHSCREEN
Keith Packard's avatar
Keith Packard committed
465
  /* PDA specific hacks */
466
467
468
469
470
  {  XF86XK_Start, ControlMask },
  {  XK_Menu, ShiftMask },
  {  XK_telephone, Mod1Mask },
  {  XF86XK_AudioRecord, Mod2Mask },
  {  XF86XK_Calendar, Mod3Mask }
471
472
473
474
475
#endif
};

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

476
477
static void
KdInitModMap (void)
478
479
480
481
482
{
    int	    key_code;
    int	    row;
    int	    width;
    KeySym  *syms;
483
    int	    i;
484
485
486
487
488
489
490
491

    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++)
	{
492
493
494
495
	    for (i = 0; i < NUM_SYM_MODS; i++) 
	    {
		if (*syms == kdKeySymMods[i].modsym) 
		    kdModMap[key_code] |= kdKeySymMods[i].modbit;
496
497
498
499
500
501
502
503
504
505
	    }
	}
    }
}

void
KdInitInput(KdMouseFuncs    *pMouseFuncs,
	    KdKeyboardFuncs *pKeyboardFuncs)
{
    DeviceIntPtr	pKeyboard, pPointer;
506
    KdMouseInfo		*mi;
507
    
508
509
510
511
512
513
514
515
516
    if (!kdMouseInfo)
	KdParseMouse (0);
    kdMouseButtonCount = 0;
    for (mi = kdMouseInfo; mi; mi = mi->next)
    {
	if (mi->nbutton > kdMouseButtonCount)
	    kdMouseButtonCount = mi->nbutton;
    }

517
518
519
520
521
522
523
524
525
526
527
528
529
530
    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;
531
    kdInputEnabled = TRUE;
532
533
    KdInitModMap ();
    KdInitAutoRepeats ();
534
535
536
537
538
539
540
    KdResetInputMachine ();
    pPointer  = AddInputDevice(KdMouseProc, TRUE);
    pKeyboard = AddInputDevice(KdKeybdProc, TRUE);
    RegisterPointerDevice(pPointer);
    RegisterKeyboardDevice(pKeyboard);
    miRegisterPointerDevice(screenInfo.screens[0], pPointer);
    mieqInit(&pKeyboard->public, &pPointer->public);
541
542
543
544
545
546
547
#ifdef XINPUT
    {
	static long zero1, zero2;

	SetExtInputCheck (&zero1, &zero2);
    }
#endif
548
549
}

550
551
#ifdef TOUCHSCREEN
void
552
KdInitTouchScreen(KdMouseFuncs *pTsFuncs)
553
554
555
556
557
{
    kdTsFuncs = pTsFuncs;
}
#endif

558
559
560
561
562
563
564
565
566
567
/*
 * 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
568
569
 *	Button other press  vo
 *	Button other release ^o
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
 *	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
595
596
 *	vo  -> (deliver) start
 *	^o  -> (deliver) start
597
598
599
600
601
602
603
604
605
 *	<>  -> (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
606
607
 *	vo  -> (release) (deliver) button_1_down
 *	^o  -> (release) (deliver) button_1_down
608
609
610
611
612
613
614
615
616
617
618
 *	<-> -> (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
619
620
 *	vo  -> (deliver) button_1_down
 *	^o  -> (deliver) button_1_down
621
622
623
624
625
626
627
628
629
 *	<>  -> (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
630
631
 *	vo  -> (deliver) button_2_down
 *	^o  -> (deliver) button_2_down
632
633
634
635
636
637
638
639
640
 *	<>  -> (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
641
642
 *	vo  -> (release) (deliver) button_3_down
 *	^o  -> (release) (deliver) button_3_down
643
644
645
646
647
648
649
650
651
652
653
 *	<-> -> (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
654
655
 *	vo  -> (deliver) button_3_down
 *	^o  -> (deliver) button_3_down
656
657
658
659
660
661
662
663
 *	<>  -> (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
664
665
 *	vo  -> (deliver) synthetic_2_down_13
 *	^o  -> (deliver) synthetic_2_down_13
666
667
668
669
670
671
672
673
674
 *	<>  -> (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
675
676
 *	vo  -> (deliver) synthetic_2_down_3
 *	^o  -> (deliver) synthetic_2_down_3
677
678
679
680
681
682
683
684
685
 *	<>  -> (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
686
687
 *	vo  -> (deliver) synthetic_2_down_1
 *	^o  -> (deliver) synthetic_2_down_1
688
689
690
691
692
693
694
695
 *	<>  -> (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,
696
    down_o, up_o,
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
    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];
717
    KdMouseState   nextState;
718
719
720
721
722
723
724
725
726
727
728
} 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 */
729
730
	{ { deliver, noop },	    start },		/* vo */
	{ { deliver, noop },	    start },		/* ^o */
731
732
	{ { deliver, noop },	    start },		/* <> */
	{ { deliver, noop },	    start },		/* <-> */
733
	{ { noop, noop },	    start },		/* k */
734
735
736
737
738
739
740
741
742
743
	{ { 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 */
744
745
	{ { release, deliver },	    button_1_down },	/* vo */
	{ { release, deliver },	    button_1_down },	/* ^o */
746
747
	{ { deliver, noop },	    button_1_pend },	/* <> */
	{ { release, deliver },	    button_1_down },	/* <-> */
748
	{ { noop, noop },	    button_1_down },	/* k */
749
750
751
752
753
754
755
756
757
758
	{ { 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 */
759
760
	{ { deliver, noop },	    button_1_down },	/* vo */
	{ { deliver, noop },	    button_1_down },	/* ^o */
761
762
	{ { deliver, noop },	    button_1_down },	/* <> */
	{ { deliver, noop },	    button_1_down },	/* <-> */
763
	{ { noop, noop },	    button_1_down },	/* k */
764
765
766
767
768
769
770
771
772
773
	{ { 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 */
774
775
	{ { deliver, noop },	    button_2_down },	/* vo */
	{ { deliver, noop },	    button_2_down },	/* ^o */
776
777
	{ { deliver, noop },	    button_2_down },	/* <> */
	{ { deliver, noop },	    button_2_down },	/* <-> */
778
	{ { noop, noop },	    button_2_down },	/* k */
779
780
781
782
783
784
785
786
787
788
	{ { 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 */
789
790
	{ { release, deliver },	    button_3_down },	/* vo */
	{ { release, deliver },	    button_3_down },	/* ^o */
791
792
	{ { deliver, noop },	    button_3_pend },	/* <> */
	{ { release, deliver },	    button_3_down },	/* <-> */
793
	{ { release, noop },	    button_3_down },	/* k */
794
795
796
797
798
799
800
801
802
803
	{ { 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 */
804
805
	{ { deliver, noop },	    button_3_down },	/* vo */
	{ { deliver, noop },	    button_3_down },	/* ^o */
806
807
	{ { deliver, noop },	    button_3_down },	/* <> */
	{ { deliver, noop },	    button_3_down },	/* <-> */
808
	{ { noop, noop },	    button_3_down },	/* k */
809
810
811
812
813
814
815
816
817
818
	{ { 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 */
819
820
	{ { deliver, noop },	    synth_2_down_13 },	/* vo */
	{ { deliver, noop },	    synth_2_down_13 },	/* ^o */
821
822
	{ { deliver, noop },	    synth_2_down_13 },	/* <> */
	{ { deliver, noop },	    synth_2_down_13 },	/* <-> */
823
	{ { noop, noop },	    synth_2_down_13 },	/* k */
824
825
826
827
828
829
830
831
832
833
	{ { 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 */
834
835
	{ { deliver, noop },	    synth_2_down_3 },	/* vo */
	{ { deliver, noop },	    synth_2_down_3 },	/* ^o */
836
837
	{ { deliver, noop },	    synth_2_down_3 },	/* <> */
	{ { deliver, noop },	    synth_2_down_3 },	/* <-> */
838
	{ { noop, noop },	    synth_2_down_3 },	/* k */
839
840
841
842
843
844
845
846
847
848
	{ { 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 */
849
850
	{ { deliver, noop },	    synth_2_down_1 },	/* vo */
	{ { deliver, noop },	    synth_2_down_1 },	/* ^o */
851
852
	{ { deliver, noop },	    synth_2_down_1 },	/* <> */
	{ { deliver, noop },	    synth_2_down_1 },	/* <-> */
853
	{ { noop, noop },	    synth_2_down_1 },	/* k */
854
855
856
857
858
	{ { noop, noop },	    synth_2_down_1 },	/* ... */
    },
};

#define EMULATION_WINDOW    10
859
#define EMULATION_TIMEOUT   100
860
861
862
863

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

864
int
865
KdInsideEmulationWindow (KdMouseInfo *mi, xEvent *ev)
866
867
868
{
    if (ev->u.keyButtonPointer.pad1)
    {
869
870
	mi->emulationDx += EventX(ev);
	mi->emulationDy += EventY(ev);
871
872
873
    }
    else
    {
874
875
	mi->emulationDx = EventX(&mi->heldEvent) - EventX(ev);
	mi->emulationDy = EventY(&mi->heldEvent) - EventY(ev);
876
    }
877
878
    return (abs (mi->emulationDx) < EMULATION_WINDOW &&
	    abs (mi->emulationDy) < EMULATION_WINDOW);
879
880
881
}
				     
KdInputClass
882
KdClassifyInput (KdMouseInfo *mi, xEvent *ev)
883
884
885
886
887
888
889
{
    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;
890
	default: return down_o;
891
892
893
894
895
896
897
	}
	break;
    case ButtonRelease:
	switch (ev->u.u.detail) {
	case 1: return up_1;
	case 2: return up_2;
	case 3: return up_3;
898
	default: return up_o;
899
900
901
	}
	break;
    case MotionNotify:
902
	if (mi->eventHeld && !KdInsideEmulationWindow(mi, ev))
903
904
905
906
907
908
	    return outside_box;
	else
	    return motion;
    default:
	return keyboard;
    }
909
    return keyboard;
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
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
}

#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
974
KdRunMouseMachine (KdMouseInfo *mi, KdInputClass c, xEvent *ev)
975
976
977
978
{
    KdInputTransition	*t;
    int			a;

979
    t = &kdInputMachine[mi->mouseState][c];
980
981
982
983
984
985
    for (a = 0; a < MAX_ACTIONS; a++)
    {
	switch (t->actions[a]) {
	case noop:
	    break;
	case hold:
986
987
988
989
	    mi->eventHeld = TRUE;
	    mi->emulationDx = 0;
	    mi->emulationDy = 0;
	    mi->heldEvent = *ev;
990
991
	    break;
	case setto:
992
993
	    mi->emulationTimeout = GetTimeInMillis () + EMULATION_TIMEOUT;
	    mi->timeoutPending = TRUE;
994
995
996
997
998
	    break;
	case deliver:
	    KdQueueEvent (ev);
	    break;
	case release:
999
1000
1001
	    mi->eventHeld = FALSE;
	    mi->timeoutPending = FALSE;
	    KdQueueEvent (&mi->heldEvent);
1002
1003
	    break;
	case clearto:
1004
	    mi->timeoutPending = FALSE;
1005
1006
1007
	    break;
	case gen_down_2:
	    ev->u.u.detail = 2;
1008
	    mi->eventHeld = FALSE;
1009
1010
1011
1012
1013
1014
1015
1016
	    KdQueueEvent (ev);
	    break;
	case gen_up_2:
	    ev->u.u.detail = 2;
	    KdQueueEvent (ev);
	    break;
	}
    }
1017
    mi->mouseState = t->nextState;
1018
1019
1020
1021
1022
}

void
KdResetInputMachine (void)
{
1023
1024
1025
1026
1027
1028
1029
    KdMouseInfo	*mi;

    for (mi = kdMouseInfo; mi; mi = mi->next)
    {
	mi->mouseState = start;
	mi->eventHeld = FALSE;
    }
1030
1031
1032
}

void
1033
KdHandleMouseEvent (KdMouseInfo *mi, xEvent *ev)
1034
{
1035
1036
    if (mi->emulateMiddleButton)
	KdRunMouseMachine (mi, KdClassifyInput (mi, ev), ev);
1037
1038
1039
1040
1041
    else
	KdQueueEvent (ev);
}

void
1042
KdReceiveTimeout (KdMouseInfo *mi)
1043
{
1044
    KdRunMouseMachine (mi, timeout, 0);
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
}

#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)
{
1074
    KeySym	sym = KEYCOL1(xE->u.u.detail);
1075
1076
1077
1078
1079
1080
1081
1082
    
    if (!pKdKeyboard) return;

    /*
     * Ignore key releases
     */
    
    if (xE->u.u.type == KeyRelease) return;
1083
1084
1085
1086
1087

#ifdef XIPAQ
    /* 
     * Check for buttons 1, 2 and 3 on the iPAQ
     */
Marc Aurele La France's avatar
Marc Aurele La France committed
1088
1089
    if (sym == XK_Pointer_Button1 && kdMouseInfo) {
	KdEnqueueMouseEvent(kdMouseInfo, KD_MOUSE_DELTA | KD_BUTTON_1, 0, 0);
1090
1091
	return;
    }
Marc Aurele La France's avatar
Marc Aurele La France committed
1092
1093
    if (sym == XK_Pointer_Button2 && kdMouseInfo) {
	KdEnqueueMouseEvent(kdMouseInfo, KD_MOUSE_DELTA | KD_BUTTON_2, 0, 0);
1094
1095
	return;
    }
Marc Aurele La France's avatar
Marc Aurele La France committed
1096
1097
    if (sym == XK_Pointer_Button3 && kdMouseInfo) {
	KdEnqueueMouseEvent(kdMouseInfo, KD_MOUSE_DELTA | KD_BUTTON_3, 0, 0);
1098
1099
1100
1101
	return;
    }
#endif

1102
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
1140
1141
1142
1143
1144
1145
1146
    /*
     * 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)
{
1147
1148
1149
1150
    int		key = ev->u.u.detail;
    int		byte;
    CARD8	bit;
    KdMouseInfo	*mi;
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
    
    byte = key >> 3;
    bit = 1 << (key & 7);
    switch (ev->u.u.type) {
    case KeyPress:
	kdKeyState[byte] |= bit;
	break;
    case KeyRelease:
	kdKeyState[byte] &= ~bit;
	break;
    }
1162
1163
1164
    for (mi = kdMouseInfo; mi; mi = mi->next)
	KdRunMouseMachine (mi, keyboard, 0);
    KdQueueEvent (ev);
1165
1166
1167
1168
1169
1170
1171
1172
}

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

1173
    KdBlockSigio ();
1174
1175
1176
1177
1178
1179
1180
1181
    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);
	}
1182
    KdUnblockSigio ();
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
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
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
}

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;
	    }
1259
1260
1261
1262
1263
1264
	    /*
	     * X delivers press/release even for autorepeat
	     */
	    xE.u.u.type = KeyRelease;
	    KdHandleKeyboardEvent (&xE);
	    xE.u.u.type = KeyPress;
1265
	}
1266
1267
1268
1269
	/*
	 * Check released keys which are already up
	 */
	else if (!IsKeyDown (key_code) && xE.u.u.type == KeyRelease)
1270
	{
1271
	    return;
1272
1273
1274
1275
1276
1277
	}
	KdCheckSpecialKeys (&xE);
	KdHandleKeyboardEvent (&xE);
    }
}

1278
#define SetButton(mi, b, v, s) \
1279
{\
1280
    xE.u.u.detail = mi->map[b]; \
1281
    xE.u.u.type = v; \
1282
    KdHandleMouseEvent (mi, &xE); \
1283
1284
}

1285
1286
#define Press(mi, b)         SetButton(mi, b, ButtonPress, "Down")
#define Release(mi, b)       SetButton(mi, b, ButtonRelease, "Up")
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307

/*
 * 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
1308
KdEnqueueMouseEvent(KdMouseInfo *mi, unsigned long flags, int rx, int ry)
1309
{
1310
1311
1312
1313
1314
1315
1316
    CARD32	    ms;
    xEvent	    xE;
    unsigned char   buttons;
    int		    x, y;
    int		    (*matrix)[3] = kdMouseMatrix.matrix;
    unsigned long   button;
    int		    n;
1317
1318
1319
1320
1321
1322
1323
1324

    if (!pKdPointer)
	return;
    
    ms = GetTimeInMillis();
    
    if (flags & KD_MOUSE_DELTA)
    {
1325
1326
	x = matrix[0][0] * rx + matrix[0][1] * ry;
	y = matrix[1][0] * rx + matrix[1][1] * ry;
1327
1328
1329
1330
1331
	x = KdMouseAccelerate (pKdPointer, x);
	y = KdMouseAccelerate (pKdPointer, y);
	xE.u.keyButtonPointer.pad1 = 1;
    }
    else
1332
1333
1334
    {
	x = matrix[0][0] * rx + matrix[0][1] * ry + matrix[0][2];
	y = matrix[1][0] * rx + matrix[1][1] * ry + matrix[1][2];
1335
	xE.u.keyButtonPointer.pad1 = 0;
1336
    }
1337
1338
1339
1340
1341
1342
    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;
1343
    KdHandleMouseEvent (mi, &xE);
1344
1345
1346

    buttons = flags;

1347
    for (button = KD_BUTTON_1, n = 0; button <= KD_BUTTON_5; button <<= 1, n++)
1348
    {
1349
	if ((mi->buttonState & button) ^ (buttons & button))
1350
	{
1351
1352
1353
1354
1355
1356
1357
1358
	    if (buttons & button)
	    {
		Press(mi, n);
	    }
	    else
	    {
		Release(mi, n);
	    }
1359
1360
	}
    }
1361
    mi->buttonState = buttons;
1362
1363
1364
}

void
1365
KdEnqueueMotionEvent (KdMouseInfo *mi, int x, int y)
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
{
    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;

1377
    KdHandleMouseEvent (mi, &xE);
1378
1379
1380
1381
1382
1383
1384
1385
}

void
KdBlockHandler (int		screen,
		pointer		blockData,
		pointer		timeout,
		pointer		readmask)
{
1386
    KdMouseInfo		    *mi;
1387
    
1388
    for (mi = kdMouseInfo; mi; mi = mi->next)
1389
    {
1390
	if (mi->timeoutPending)
1391
	{
1392
1393
1394
1395
1396
1397
	    int	ms;
    
	    ms = mi->emulationTimeout - GetTimeInMillis ();
	    if (ms < 0)
		ms = 0;
	    AdjustWaitForDelay (timeout, ms);
1398
1399
1400
1401
1402
1403
1404
	}
    }
}

void
KdWakeupHandler (int		screen, 
		 pointer    	data,
1405
		 unsigned long	lresult,
1406
1407
		 pointer	readmask)
{
1408
1409
1410
1411
    int		result = (int) lresult;
    fd_set	*pReadmask = (fd_set *) readmask;
    int		i;
    KdMouseInfo	*mi;
1412
    
1413
    if (kdInputEnabled && result > 0)
1414
    {
1415
1416
1417
1418
1419
1420
1421
	for (i = 0; i < kdNumInputFds; i++)
	    if (FD_ISSET (kdInputFds[i].fd, pReadmask))
	    {
		KdBlockSigio ();
		(*kdInputFds[i].read) (kdInputFds[i].fd, kdInputFds[i].closure);
		KdUnblockSigio ();
	    }
1422
    }
1423
    for (mi = kdMouseInfo; mi; mi = mi->next)
1424
    {
1425
	if (mi->timeoutPending)
1426
	{
1427
1428
1429
1430
1431
1432
1433
	    if ((long) (GetTimeInMillis () - mi->emulationTimeout) >= 0)
	    {
		mi->timeoutPending = FALSE;
		KdBlockSigio ();
		KdReceiveTimeout (mi);
		KdUnblockSigio ();
	    }
1434
1435
1436
1437
1438
1439
	}
    }
    if (kdSwitchPending)
	KdProcessSwitch ();
}

1440
1441
#define KdScreenOrigin(pScreen) (&(KdGetScreenPriv(pScreen)->screen->origin))

1442
1443
1444
1445
static Bool
KdCursorOffScreen(ScreenPtr *ppScreen, int *x, int *y)
{
    ScreenPtr	pScreen  = *ppScreen;
1446
    ScreenPtr	pNewScreen;
1447
    int		n;
1448
1449
1450
    int		dx, dy;
    int		best_x, best_y;
    int		n_best_x, n_best_y;
1451
    CARD32	ms;
1452
1453
1454
    
    if (kdDisableZaphod || screenInfo.numScreens <= 1)
	return FALSE;
1455
1456
1457

    if (0 <= *x && *x < pScreen->width && 0 <= *y && *y < pScreen->height)
	return FALSE;
1458
	
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
    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;
1475
	if (*x < 0)
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
	{
	    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;
	    }
	}
1491
	if (*y < 0)
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
	{
	    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;
	    }
	}
1507
    }
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
    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;
1526
1527
1528
1529
1530
}

static void
KdCrossScreen(ScreenPtr pScreen, Bool entering)
{
1531
#ifndef XIPAQ
1532
1533
1534
1535
    if (entering)
	KdEnableScreen (pScreen);
    else
	KdDisableScreen (pScreen);
1536
#endif
1537
1538
}

1539
#ifdef TOUCHSCREEN
1540
1541
/* HACK! */
extern int TsScreen;
1542
#endif
1543

1544
1545
1546
1547
static void
KdWarpCursor (ScreenPtr pScreen, int x, int y)
{
    KdBlockSigio ();
1548
#ifdef TOUCHSCREEN
1549
    TsScreen = pScreen->myNum;
1550
#endif
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
    miPointerWarpCursor (pScreen, x, y);
    KdUnblockSigio ();
}

miPointerScreenFuncRec kdPointerScreenFuncs = 
{
    KdCursorOffScreen,
    KdCrossScreen,
    KdWarpCursor
};

void
ProcessInputEvents ()
{
1565
    mieqProcessInputEvents();
1566
1567
1568
1569
1570
    miPointerUpdate();
    if (kdSwitchPending)
	KdProcessSwitch ();
    KdCheckLock ();
}