summaryrefslogtreecommitdiff
path: root/Xext/xevie.c
blob: dfec68fe6eaa8e1cc9c26dd2dbd24ad1f31d1dcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
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
737
738
739
/************************************************************

Copyright 2003-2005 Sun Microsystems, Inc.

All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, and/or sell copies of the Software, and to permit persons
to whom the Software is furnished to do so, provided that the above
copyright notice(s) and this permission notice appear in all copies of
the Software and that both the above copyright notice(s) and this
permission notice appear in supporting documentation.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR 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.

Except as contained in this notice, the name of a copyright holder
shall not be used in advertising or otherwise to promote the sale, use
or other dealings in this Software without prior written authorization
of the copyright holder.

************************************************************/

#define NEED_REPLIES
#define NEED_EVENTS
#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif

#include <X11/X.h>
#include <X11/Xproto.h>
#include "misc.h"
#include "dixstruct.h"
#include "extnsionst.h"
#include "colormapst.h"
#include "scrnintstr.h"
#include "servermd.h"
#define  _XEVIE_SERVER_
#include <X11/extensions/Xeviestr.h>
#include <X11/Xfuncproto.h>
#include "input.h"
#include "inputstr.h"
#include "windowstr.h"
#include "cursorstr.h"
#include <xkbsrv.h>

#include "../os/osdep.h"
#include "modinit.h"

#define NoSuchEvent 0x80000000

#ifdef XKB
extern Bool noXkbExtension;
#endif
extern int    xeviegrabState;

static DISPATCH_PROC(ProcXevieDispatch);
static DISPATCH_PROC(SProcXevieDispatch);

static unsigned char	XevieReqCode = 0;
static int		XevieErrorBase;

int			xevieFlag = 0;
int	 		xevieClientIndex = 0;
DeviceIntPtr		xeviekb = NULL;
DeviceIntPtr		xeviemouse = NULL;
Mask			xevieMask = 0;
int       		xevieEventSent = 0;
int			xevieKBEventSent = 0;
static Bool             xevieModifiersOn = FALSE;

static int xevieDevicePrivateKeyIndex;
static DevPrivateKey xevieDevicePrivateKey = &xevieDevicePrivateKeyIndex;

#define XEVIEINFO(dev)  ((xevieDeviceInfoPtr) \
    dixLookupPrivate(&(dev)->devPrivates, xevieDevicePrivateKey))

Mask xevieFilters[128] = 
{
        NoSuchEvent,                   /* 0 */
        NoSuchEvent,                   /* 1 */
        KeyPressMask,                  /* KeyPress */
        KeyReleaseMask,                /* KeyRelease */
        ButtonPressMask,               /* ButtonPress */
        ButtonReleaseMask,             /* ButtonRelease */
        PointerMotionMask              /* MotionNotify (initial state) */
};

typedef struct {
    ProcessInputProc processInputProc;
    ProcessInputProc realInputProc;
    DeviceUnwrapProc unwrapProc;
} xevieDeviceInfoRec, *xevieDeviceInfoPtr;

typedef struct {
    CARD32 time;
    KeyClassPtr keyc;
} xevieKeycQueueRec, *xevieKeycQueuePtr;

#define KEYC_QUEUE_SIZE	    100
static xevieKeycQueueRec keycq[KEYC_QUEUE_SIZE] = {{0, NULL}};
static int keycqHead = 0, keycqTail = 0;

static Bool XevieStart(void);
static void XevieEnd(int clientIndex);
static void XevieClientStateCallback(CallbackListPtr *pcbl, pointer nulldata,
                                    pointer calldata);
static void XevieServerGrabStateCallback(CallbackListPtr *pcbl,
                                         pointer nulldata,
                                         pointer calldata);

static Bool XevieAdd(DeviceIntPtr device, pointer data);
static void XevieWrap(DeviceIntPtr device, ProcessInputProc proc);
static Bool XevieRemove(DeviceIntPtr device, pointer data);
static void doSendEvent(xEvent *xE, DeviceIntPtr device);
static void XeviePointerProcessInputProc(xEvent *xE, DeviceIntPtr dev,
                                         int count);
static void XevieKbdProcessInputProc(xEvent *xE, DeviceIntPtr dev, int count);

void
XevieExtensionInit (void)
{
    ExtensionEntry* extEntry;

    if (!AddCallback(&ServerGrabCallback,XevieServerGrabStateCallback,NULL))
       return;

    if ((extEntry = AddExtension (XEVIENAME,
				0,
				XevieNumberErrors,
				ProcXevieDispatch,
				SProcXevieDispatch,
				NULL,
				StandardMinorOpcode))) {
	XevieReqCode = (unsigned char)extEntry->base;
	XevieErrorBase = extEntry->errorBase;
    }
}

static 
int ProcXevieQueryVersion (ClientPtr client)
{
    xXevieQueryVersionReply rep;
    int n;

    REQUEST_SIZE_MATCH (xXevieQueryVersionReq);
    rep.type = X_Reply;
    rep.length = 0;
    rep.sequence_number = client->sequence;
    rep.server_major_version = XEVIE_MAJOR_VERSION;
    rep.server_minor_version = XEVIE_MINOR_VERSION;
    if (client->swapped) {
	swaps(&rep.sequence_number, n);
	swapl(&rep.length, n);
	swaps(&rep.server_major_version, n);
	swaps(&rep.server_minor_version, n);
    }
    WriteToClient (client, sizeof (xXevieQueryVersionReply), (char *)&rep);
    return client->noClientException;
}

static
int ProcXevieStart (ClientPtr client)
{
    xXevieStartReply rep;
    int n;

    REQUEST_SIZE_MATCH (xXevieStartReq);
    rep.pad1 = 0;

    if(!xevieFlag){
        if (AddCallback(&ClientStateCallback,XevieClientStateCallback,NULL)) {
           xevieFlag = 1;
           rep.pad1 = 1;
           xevieClientIndex = client->index;
	   if(!keycq[0].time ) {
		int i;
		for(i=0; i<KEYC_QUEUE_SIZE; i++) {
		    keycq[i].keyc = xalloc(sizeof(KeyClassRec));	
		    keycq[i].keyc->xkbInfo = xalloc(sizeof(XkbSrvInfoRec));
		}
	   }
        } else
           return BadAlloc;
    } else
        return BadAccess;
#ifdef XKB
    if (!noXkbExtension) {
	if (!XevieStart()) {
            DeleteCallback(&ClientStateCallback,XevieClientStateCallback,NULL);
            return BadAlloc;
        }
    }
#endif
    
    xevieModifiersOn = FALSE;

    rep.length = 0;
    rep.type = X_Reply;
    rep.sequence_number = client->sequence;
    if (client->swapped) {
	swaps(&rep.sequence_number, n);
	swapl(&rep.length, n);
    }
    WriteToClient (client, sizeof (xXevieStartReply), (char *)&rep);
    return client->noClientException;
}

static
int ProcXevieEnd (ClientPtr client)
{
    xXevieEndReply rep;
    int n;

    REQUEST_SIZE_MATCH (xXevieEndReq);
    
    if (xevieFlag) {
        if (client->index != xevieClientIndex)
            return BadAccess;

        DeleteCallback(&ClientStateCallback,XevieClientStateCallback,NULL);
        XevieEnd(xevieClientIndex);
    }

    rep.length = 0;
    rep.type = X_Reply;
    rep.sequence_number = client->sequence;
    if (client->swapped) {
	swaps(&rep.sequence_number, n);
	swapl(&rep.length, n);
    }
    WriteToClient (client, sizeof (xXevieEndReply), (char *)&rep);
    return client->noClientException;
}

static
int ProcXevieSend (ClientPtr client)
{
    REQUEST (xXevieSendReq);
    xXevieSendReply rep;
    xEvent *xE;
    static unsigned char lastDetail = 0, lastType = 0;
    int n;

    REQUEST_SIZE_MATCH (xXevieSendReq);
    
    if (client->index != xevieClientIndex)
        return BadAccess;

    xE = (xEvent *)&stuff->event;
    rep.length = 0;
    rep.type = X_Reply;
    rep.sequence_number = client->sequence;
    if (client->swapped) {
	swaps(&rep.sequence_number, n);
	swapl(&rep.length, n);
    }
    WriteToClient (client, sizeof (xXevieSendReply), (char *)&rep);

    switch(xE->u.u.type) {
	case KeyPress:
        case KeyRelease:
	  xevieKBEventSent = 1;
#ifdef XKB
          if(!noXkbExtension)
	    doSendEvent(xE, inputInfo.keyboard);
	  else 
#endif
            CoreProcessKeyboardEvent (xE, xeviekb, 1);
	  break;
	case ButtonPress:
	case ButtonRelease:
	case MotionNotify:
	  xevieEventSent = 1;
#ifdef XKB
	  if(!noXkbExtension)
	    doSendEvent(xE, inputInfo.pointer);
	  else
#endif
	    CoreProcessPointerEvent(xE, xeviemouse, 1); 
	  break; 
	default:
	  break;
    }
    lastType = xE->u.u.type;
    lastDetail = xE->u.u.detail;
    return client->noClientException;
}

static
int ProcXevieSelectInput (ClientPtr client)
{
    REQUEST (xXevieSelectInputReq);
    xXevieSelectInputReply rep;
    int n;

    REQUEST_SIZE_MATCH (xXevieSelectInputReq);

    if (client->index != xevieClientIndex)
        return BadAccess;

    xevieMask = stuff->event_mask;
    rep.length = 0;
    rep.type = X_Reply;
    rep.sequence_number = client->sequence;
    if (client->swapped) {
	swaps(&rep.sequence_number, n);
	swapl(&rep.length, n);
    }
    WriteToClient (client, sizeof (xXevieSelectInputReply), (char *)&rep);
    return client->noClientException;
}

static 
int ProcXevieDispatch (ClientPtr client)
{
    REQUEST (xReq);
    switch (stuff->data)
    {
    case X_XevieQueryVersion:
	return ProcXevieQueryVersion (client);
    case X_XevieStart:
	return ProcXevieStart (client);
    case X_XevieEnd:
	return ProcXevieEnd (client);
    case X_XevieSend:
	return ProcXevieSend (client);
    case X_XevieSelectInput:
	return ProcXevieSelectInput(client);
    default:
	return BadRequest;
    }
}

static 
int SProcXevieQueryVersion (ClientPtr client)
{
    int n;

    REQUEST(xXevieQueryVersionReq);
    swaps (&stuff->length, n);
    REQUEST_SIZE_MATCH (xXevieQueryVersionReq);
    swaps (&stuff->client_major_version, n);
    swaps (&stuff->client_minor_version, n);
    return ProcXevieQueryVersion(client);
}

static 
int SProcXevieStart (ClientPtr client)
{
    int n;

    REQUEST (xXevieStartReq);
    swaps (&stuff->length, n);
    REQUEST_SIZE_MATCH (xXevieStartReq);
    swapl (&stuff->screen, n);
    return ProcXevieStart (client);
}

static 
int SProcXevieEnd (ClientPtr client)
{
    int n;

    REQUEST (xXevieEndReq);
    swaps (&stuff->length, n);
    REQUEST_SIZE_MATCH (xXevieEndReq);
    swapl (&stuff->cmap, n);
    return ProcXevieEnd (client);
}

static
int SProcXevieSend (ClientPtr client)
{
    int n;
    xEvent eventT;
    EventSwapPtr proc;

    REQUEST (xXevieSendReq);
    swaps (&stuff->length, n);
    REQUEST_SIZE_MATCH (xXevieSendReq);
    swapl (&stuff->dataType, n);

    /* Swap event */
    proc = EventSwapVector[stuff->event.u.u.type & 0177];
    if (!proc ||  proc == NotImplemented) /* no swapping proc; invalid event type? */
	return (BadValue);
    (*proc)(&stuff->event, &eventT);
    stuff->event = eventT;
    
    return ProcXevieSend (client);
}

static
int SProcXevieSelectInput (ClientPtr client)
{
    int n;

    REQUEST (xXevieSelectInputReq);
    swaps (&stuff->length, n);
    REQUEST_SIZE_MATCH (xXevieSelectInputReq);
    swapl (&stuff->event_mask, n);
    return ProcXevieSelectInput (client);
}


static 
int SProcXevieDispatch (ClientPtr client)
{
    REQUEST(xReq);
    switch (stuff->data)
    {
    case X_XevieQueryVersion:
	return SProcXevieQueryVersion (client);
    case X_XevieStart:
	return SProcXevieStart (client);
    case X_XevieEnd:
	return SProcXevieEnd (client);
    case X_XevieSend:
	return SProcXevieSend (client);
    case X_XevieSelectInput:
	return SProcXevieSelectInput(client);
    default:
	return BadRequest;
    }
}
/*======================================================*/

#define WRAP_INPUTPROC(dev,store,inputProc) \
   store->processInputProc = dev->public.processInputProc; \
   dev->public.processInputProc = inputProc; \
   store->realInputProc = dev->public.realInputProc; \
   dev->public.realInputProc = inputProc;

#define COND_WRAP_INPUTPROC(dev,store,inputProc) \
   if (dev->public.processInputProc == dev->public.realInputProc) \
          dev->public.processInputProc = inputProc; \
   store->processInputProc =  \
   store->realInputProc = dev->public.realInputProc; \
   dev->public.realInputProc = inputProc;

#define UNWRAP_INPUTPROC(dev,restore) \
   dev->public.processInputProc = restore->processInputProc; \
   dev->public.realInputProc = restore->realInputProc;

#define UNWRAP_INPUTPROC(dev,restore) \
   dev->public.processInputProc = restore->processInputProc; \
   dev->public.realInputProc = restore->realInputProc;

#define XEVIE_EVENT(xE) \
      (xevieFlag \
       && !xeviegrabState \
       && clients[xevieClientIndex] \
       && (xevieMask & xevieFilters[xE->u.u.type]))


static void
sendEvent(ClientPtr pClient, xEvent *xE)
{
    if(pClient->swapped) {
        xEvent    eventTo;

        /* Remember to strip off the leading bit of type in case
           this event was sent with "SendEvent." */
        (*EventSwapVector[xE->u.u.type & 0177]) (xE, &eventTo);
        (void)WriteToClient(pClient, sizeof(xEvent), (char *)&eventTo);
    } else {
        (void)WriteToClient(pClient, sizeof(xEvent), (char *) xE);
    }
}

static void
XevieKbdProcessInputProc(xEvent *xE, DeviceIntPtr dev, int count)
{
    int             key, bit;
    BYTE   *kptr;
    ProcessInputProc tmp;
    KeyClassPtr keyc = dev->key;
    xevieDeviceInfoPtr xeviep = XEVIEINFO(dev);

    if(XEVIE_EVENT(xE)) {
        key = xE->u.u.detail;
        kptr = &keyc->down[key >> 3];
        bit = 1 << (key & 7);

	if (dev->key->modifierMap[xE->u.u.detail])
            xevieModifiersOn = TRUE;

        xE->u.keyButtonPointer.event = xeviewin->drawable.id;
        xE->u.keyButtonPointer.root = GetCurrentRootWindow(dev)->drawable.id;
        xE->u.keyButtonPointer.child = (xeviewin->firstChild)
            ? xeviewin->firstChild->drawable.id:0;
        xE->u.keyButtonPointer.rootX = xeviehot.x;
        xE->u.keyButtonPointer.rootY = xeviehot.y;
        xE->u.keyButtonPointer.state = keyc->state | inputInfo.pointer->button->state;
        /* fix bug: sequence lost in Xlib */
        xE->u.u.sequenceNumber = clients[xevieClientIndex]->sequence;
#ifdef XKB
	/* fix for bug5092586 */
	if(!noXkbExtension) {
          switch(xE->u.u.type) {
	    case KeyPress: *kptr |= bit; break;
	    case KeyRelease: *kptr &= ~bit; break;
	  }
	}
#endif
	keycq[keycqHead].time = xE->u.keyButtonPointer.time;
	memcpy(keycq[keycqHead].keyc, keyc, sizeof(KeyClassRec) - sizeof(KeyClassPtr));
	memcpy(keycq[keycqHead].keyc->xkbInfo, keyc->xkbInfo, sizeof(XkbSrvInfoRec));
	if(++keycqHead >=KEYC_QUEUE_SIZE)
	    keycqHead = 0;
        sendEvent(clients[xevieClientIndex], xE);
        return;
    }

    tmp = dev->public.realInputProc;
    UNWRAP_INPUTPROC(dev,xeviep);
    dev->public.processInputProc(xE,dev,count);
    COND_WRAP_INPUTPROC(dev,xeviep,tmp);
}

static void
XeviePointerProcessInputProc(xEvent *xE, DeviceIntPtr dev, int count)
{
    xevieDeviceInfoPtr xeviep = XEVIEINFO(dev);
    ProcessInputProc tmp;

    if (XEVIE_EVENT(xE)) {
        /* fix bug: sequence lost in Xlib */
        xE->u.u.sequenceNumber = clients[xevieClientIndex]->sequence;
        sendEvent(clients[xevieClientIndex], xE);
        return;
    }

    tmp = dev->public.realInputProc;
    UNWRAP_INPUTPROC(dev,xeviep);
    dev->public.processInputProc(xE,dev,count);
    COND_WRAP_INPUTPROC(dev,xeviep,tmp);
}

static Bool
XevieStart(void)
{
    ProcessInputProc prp;
    prp = XevieKbdProcessInputProc;
    if (!XevieAdd(inputInfo.keyboard,&prp))
        return FALSE;
    prp = XeviePointerProcessInputProc;
    if (!XevieAdd(inputInfo.pointer,&prp))
        return FALSE;

    return TRUE;
}


static void
XevieEnd(int clientIndex)
{
    if (!clientIndex || clientIndex == xevieClientIndex) {

#ifdef XKB
       if(!noXkbExtension) {

	   XevieRemove(inputInfo.keyboard,NULL);

	   inputInfo.keyboard->public.processInputProc = CoreProcessKeyboardEvent;
           inputInfo.keyboard->public.realInputProc = CoreProcessKeyboardEvent;
           XkbSetExtension(inputInfo.keyboard,ProcessKeyboardEvent);


           XevieRemove(inputInfo.pointer,NULL);

	   inputInfo.pointer->public.processInputProc = CoreProcessPointerEvent;
           inputInfo.pointer->public.realInputProc = CoreProcessPointerEvent;
           XkbSetExtension(inputInfo.pointer,ProcessPointerEvent);
       }
#endif

       xevieFlag = 0;
       xevieClientIndex = 0;
       DeleteCallback (&ClientStateCallback, XevieClientStateCallback, NULL);
    }
}

static void
XevieClientStateCallback(CallbackListPtr *pcbl, pointer nulldata,
                        pointer calldata)
{
    NewClientInfoRec *pci = (NewClientInfoRec *)calldata;
    ClientPtr client = pci->client;
    if (client->clientState == ClientStateGone
       || client->clientState == ClientStateRetained)
       XevieEnd(client->index);
}

static void
XevieServerGrabStateCallback(CallbackListPtr *pcbl, pointer nulldata,
                            pointer calldata)
{
    ServerGrabInfoRec *grbinfo = (ServerGrabInfoRec *)calldata;
    if (grbinfo->grabstate == SERVER_GRABBED)
       xeviegrabState = TRUE;
    else
       xeviegrabState = FALSE;
}

#define UNWRAP_UNWRAPPROC(device,proc_store) \
    device->unwrapProc = proc_store;

#define WRAP_UNWRAPPROC(device,proc_store,proc) \
    proc_store = device->unwrapProc; \
    device->unwrapProc = proc;

static void
xevieUnwrapProc(DeviceIntPtr device, DeviceHandleProc proc, pointer data)
{
    xevieDeviceInfoPtr xeviep = XEVIEINFO(device);
    ProcessInputProc tmp = device->public.processInputProc;

    UNWRAP_INPUTPROC(device,xeviep);
    UNWRAP_UNWRAPPROC(device,xeviep->unwrapProc);
    proc(device,data);
    WRAP_INPUTPROC(device,xeviep,tmp);
    WRAP_UNWRAPPROC(device,xeviep->unwrapProc,xevieUnwrapProc);
}

static Bool
XevieUnwrapAdd(DeviceIntPtr device, void* data)
{
    if (device->unwrapProc)
        device->unwrapProc(device,XevieUnwrapAdd,data);
    else {
        ProcessInputProc *ptr = data;
        XevieWrap(device,*ptr);
    }

    return TRUE;
}

static Bool
XevieAdd(DeviceIntPtr device, void* data)
{
    xevieDeviceInfoPtr xeviep;

    xeviep = xalloc (sizeof (xevieDeviceInfoRec));
    if (!xeviep)
            return FALSE;

    dixSetPrivate(&device->devPrivates, xevieDevicePrivateKey, xeviep);
    XevieUnwrapAdd(device, data);

    return TRUE;
}

static Bool
XevieRemove(DeviceIntPtr device,pointer data)
{
    xevieDeviceInfoPtr xeviep = XEVIEINFO(device);

    if (!xeviep) return TRUE;

    UNWRAP_INPUTPROC(device,xeviep);
    UNWRAP_UNWRAPPROC(device,xeviep->unwrapProc);

    xfree(xeviep);
    dixSetPrivate(&device->devPrivates, xevieDevicePrivateKey, NULL);
    return TRUE;
}

static void
XevieWrap(DeviceIntPtr device, ProcessInputProc proc)
{
    xevieDeviceInfoPtr xeviep = XEVIEINFO(device);

    WRAP_INPUTPROC(device,xeviep,proc);
    WRAP_UNWRAPPROC(device,xeviep->unwrapProc,xevieUnwrapProc);
}

static void
doSendEvent(xEvent *xE, DeviceIntPtr dev)
{
    xevieDeviceInfoPtr xeviep = XEVIEINFO(dev);
    ProcessInputProc tmp = dev->public.realInputProc;
    if (((xE->u.u.type==KeyPress)||(xE->u.u.type==KeyRelease))
        && !xevieModifiersOn) {
	KeyClassPtr keyc =  dev->key;
        CARD8 realModes = dev->key->modifierMap[xE->u.u.detail];
	int notFound = 0;
	/* if some events are consumed by client, move the queue tail pointer to the current 
           event which just comes back from Xevie client . 
	*/
        if(keycq[keycqTail].time != xE->u.keyButtonPointer.time) {
	    while(keycq[keycqTail].time != xE->u.keyButtonPointer.time) {
		if(++keycqTail >= KEYC_QUEUE_SIZE)
		    keycqTail = 0;
		if(keycqTail == keycqHead) {
		    notFound = 1;
		    break;
		}
	    }
	}
	if(!notFound) {
	    dev->key = keycq[keycqTail].keyc;
	    if(++keycqTail >= KEYC_QUEUE_SIZE)
	        keycqTail = 0;
	}
        dev->key->modifierMap[xE->u.u.detail] = 0;  

	if(dev->key->xkbInfo->repeatKey != 0 && xE->u.u.type != KeyPress)
            XkbLastRepeatEvent=     (pointer)xE;
        UNWRAP_INPUTPROC(dev,xeviep);
        dev->public.processInputProc(xE,dev,1);
        COND_WRAP_INPUTPROC(dev,xeviep,tmp);
        XkbLastRepeatEvent= NULL;

        dev->key->modifierMap[xE->u.u.detail] = realModes;
	dev->key = keyc;
	if(notFound) {
	    DeleteCallback(&ClientStateCallback,XevieClientStateCallback,NULL);
            XevieEnd(xevieClientIndex);
	    ErrorF("Error: Xevie keyc queue size is not enough, disable Xevie\n");
	}	
    } else {
        UNWRAP_INPUTPROC(dev,xeviep);
        dev->public.processInputProc(xE,dev,1);
        COND_WRAP_INPUTPROC(dev,xeviep,tmp);
    }
}