summaryrefslogtreecommitdiff
path: root/hw/xfree86/os-support/sysv/xqueue.c
blob: 0478ee65e65bcc3e5326c35a8124263a319fcd36 (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
/* $XFree86: xc/programs/Xserver/hw/xfree86/os-support/sysv/xqueue.c,v 3.20 2001/03/06 18:20:31 dawes Exp $ */
/*
 * Copyright 1990,91 by Thomas Roell, Dinkelscherben, Germany
 * Copyright 1993-1999 by The XFree86 Project, Inc.
 *
 * 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 the copyright holders not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission.  The copyright holders make no
 * representations about the suitability of this software for any purpose.
 * It is provided "as is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL THE COPYRIGHT HOLDERS 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.
 *
 */
/* $XConsortium: xqueue.c /main/8 1996/10/19 18:08:11 kaleb $ */

#include "X.h"
#include "compiler.h"

#include "xf86.h"
#include "xf86Priv.h"
#include "xf86_OSlib.h"
#include "xf86Xinput.h"
#include "xf86OSmouse.h"
#include "xqueue.h"

#ifdef XQUEUE

static xqEventQueue      *XqueQaddr;
static int xqueFd = -1;
#ifndef XQUEUE_ASYNC
static int xquePipe[2];
#endif

#ifdef XKB
#include "inputstr.h"
#include <X11/extensions/XKB.h>
#include <X11/extensions/XKBstr.h>
#include <X11/extensions/XKBsrv.h>
extern Bool noXkbExtension;
#endif

#include "xf86Xinput.h"
#include "mipointer.h"

typedef struct {
	int		xquePending;
	int		xqueSema;
} XqInfoRec, *XqInfoPtr;

InputInfoPtr XqMouse = NULL;
InputInfoPtr XqKeyboard = NULL;

#ifndef XQUEUE_ASYNC
/*
 * xf86XqueSignal --
 *	Trap the signal from xqueue and let it be known that events are
 *	ready for collection
 */

static void
xf86XqueSignal(int signum)
{
  ((XqInfoPtr)(((MouseDevPtr)(XqMouse->private))->mousePriv))->xquePending = 1;
  /*
   * This is a hack, but it is the only reliable way I can find of letting
   * the main select() loop know that there is more input waiting.  Receiving
   * a signal will interrupt select(), but there is no way I can find of
   * dealing with events that come in between the end of processing the
   * last set and when select() gets called.
   *
   * Suggestions for better ways of dealing with this without going back to
   * asynchronous event processing are welcome.
   */
#ifdef DEBUG
  ErrorF("xf86XqueSignal\n");
#endif
  write(xquePipe[1], "X", 1);
  signal(SIGUSR2, xf86XqueSignal);
}
#endif
  

/*
 * xf86XqueKbdProc --
 *	Handle the initialization, etc. of a keyboard.
 */

int
xf86XqueKbdProc(DeviceIntPtr pKeyboard, int what)
{
  KeySymsRec  keySyms;
  CARD8       modMap[MAP_LENGTH];

  switch (what) {
      
  case DEVICE_INIT:
    
    xf86KbdGetMapping(&keySyms, modMap);
    
    /*
     * Get also the initial led settings
     */
    ioctl(xf86Info.consoleFd, KDGETLED, &xf86Info.leds);

    /*
     * Perform final initialization of the system private keyboard
     * structure and fill in various slots in the device record
     * itself which couldn't be filled in before.
     */
    pKeyboard->public.on = FALSE;

#ifdef XKB
    if (noXkbExtension) {
#endif
    InitKeyboardDeviceStruct((DevicePtr)xf86Info.pKeyboard,
			     &keySyms,
			     modMap,
			     xf86KbdBell,
			     (KbdCtrlProcPtr)xf86KbdCtrl);
#ifdef XKB
    } else {
	XkbComponentNamesRec names;
	if (XkbInitialMap) {
	    if ((xf86Info.xkbkeymap = strchr(XkbInitialMap, '/')) != NULL)
		xf86Info.xkbkeymap++;
	    else
		xf86Info.xkbkeymap = XkbInitialMap;
	}
	if (xf86Info.xkbkeymap) {
	    names.keymap = xf86Info.xkbkeymap;
	    names.keycodes = NULL;
	    names.types = NULL;
	    names.compat = NULL;
	    names.symbols = NULL;
	    names.geometry = NULL;
	} else {
	    names.keymap = NULL;
	    names.keycodes = xf86Info.xkbkeycodes;
	    names.types = xf86Info.xkbtypes;
	    names.compat = xf86Info.xkbcompat;
	    names.symbols = xf86Info.xkbsymbols;
	    names.geometry = xf86Info.xkbgeometry;
	}
	if ((xf86Info.xkbkeymap || xf86Info.xkbcomponents_specified)
	   && (xf86Info.xkbmodel == NULL || xf86Info.xkblayout == NULL)) {
		xf86Info.xkbrules = NULL;
	}
	XkbSetRulesDflts(xf86Info.xkbrules, xf86Info.xkbmodel,
			 xf86Info.xkblayout, xf86Info.xkbvariant,
			 xf86Info.xkboptions);
	XkbInitKeyboardDeviceStruct(pKeyboard, 
				    &names,
				    &keySyms, 
				    modMap, 
				    xf86KbdBell,
				    (KbdCtrlProcPtr)xf86KbdCtrl);
    }
#endif

    xf86InitKBD(TRUE);
    break;
    
  case DEVICE_ON:
    pKeyboard->public.on = TRUE;
    xf86InitKBD(FALSE);
    break;
    
  case DEVICE_CLOSE:
  case DEVICE_OFF:
    pKeyboard->public.on = FALSE;
    break;
  }
  
  return (Success);
}


/*
 * xf86XqueEvents --
 *      Get some events from our queue. Nothing to do here ...
 */

void
xf86XqueEvents()
{
}


#ifdef XQUEUE_ASYNC
static void XqDoInput(int signum);
#endif

void
XqReadInput(InputInfoPtr pInfo)
{
    MouseDevPtr pMse;
    XqInfoPtr pXq;
    xqEvent *XqueEvents;
    int XqueHead;
    char buf[100];
    signed char dx, dy;

    if (xqueFd < 0)
	return;

    pMse = pInfo->private;
    pXq = pMse->mousePriv;

    XqueEvents = XqueQaddr->xq_events;
    XqueHead = XqueQaddr->xq_head;

    while (XqueHead != XqueQaddr->xq_tail) {
	switch (XqueEvents[XqueHead].xq_type) {
	case XQ_BUTTON:
	    pMse->PostEvent(pInfo, ~(XqueEvents[XqueHead].xq_code) & 0x07,
			    0, 0, 0, 0);
#ifdef DEBUG
	    ErrorF("xqueue: buttons: %d\n", ~(XqueEvents[XqueHead].xq_code) & 0x07);
#endif
	    break;

	case XQ_MOTION:
	    dx = (signed char)XqueEvents[XqueHead].xq_x;
	    dy = (signed char)XqueEvents[XqueHead].xq_y;
	    pMse->PostEvent(pInfo, ~(XqueEvents[XqueHead].xq_code) & 0x07,
			    (int)dx, (int)dy, 0, 0);
#ifdef DEBUG
	    ErrorF("xqueue: Motion: (%d, %d) (buttons: %d)\n", dx, dy, ~(XqueEvents[XqueHead].xq_code) & 0x07);
#endif
	    break;

	case XQ_KEY:
	    /* XXX Need to deal with the keyboard part nicely. */
#ifdef DEBUG
	    ErrorF("xqueue: key: %d\n", XqueEvents[XqueHead].xq_code);
#endif
	    xf86PostKbdEvent(XqueEvents[XqueHead].xq_code);
	    break;
	default:
	    xf86Msg(X_WARNING, "Unknown Xque Event: 0x%02x\n",
		    XqueEvents[XqueHead].xq_type);
	}
      
	if ((++XqueHead) == XqueQaddr->xq_size) XqueHead = 0;
	xf86Info.inputPending = TRUE;
    }

    /* reenable the signal-processing */
#ifdef XQUEUE_ASYNC
    signal(SIGUSR2, XqDoInput);
#endif

#ifndef XQUEUE_ASYNC
    {
	int rval;

	while ((rval = read(xquePipe[0], buf, sizeof(buf))) > 0)
#ifdef DEBUG
	    ErrorF("Read %d bytes from xquePipe[0]\n", rval);
#else
	    ;
#endif
    }
#endif

#ifdef DEBUG
    ErrorF("Leaving XqReadInput()\n");
#endif
    pXq->xquePending = 0;
    XqueQaddr->xq_head = XqueQaddr->xq_tail;
    XqueQaddr->xq_sigenable = 1; /* UNLOCK */
}

#ifdef XQUEUE_ASYNC
static void
XqDoInput(int signum)
{
    if (XqMouse)
	XqReadInput(XqMouse);
}
#endif

static void
XqBlock(pointer blockData, OSTimePtr pTimeout, pointer pReadmask)
{
    InputInfoPtr pInfo;
    MouseDevPtr pMse;
    XqInfoPtr pXq;
    /*
     * On MP SVR4 boxes, a race condition exists because the XQUEUE does
     * not have anyway to lock it for exclusive access. This results in one
     * processor putting something on the queue at the same time the other
     * processor is taking it something off. The count of items in the queue
     * can get off by 1. This just goes and checks to see if an extra event
     * was put in the queue a during this period. The signal for this event
     * was ignored while processing the previous event.
     */

    pInfo = blockData;
    pMse = pInfo->private;
    pXq = pMse-> mousePriv;
    if (!pXq->xquePending) {
#ifdef DEBUG
	ErrorF("XqBlock: calling XqReadInput()\n");
#endif
	XqReadInput((InputInfoPtr)blockData);
    } else {
#ifdef DEBUG
	ErrorF("XqBlock: not calling XqReadInput()\n");
#endif
	;
    }
    /*
     * Make sure that any events that come in here are passed on without.
     * waiting for the next wakeup.
     */
    if (xf86Info.inputPending) {
#ifdef DEBUG
	ErrorF("XqBlock: calling ProcessInputEvents()\n");
#endif
	ProcessInputEvents();
    } else {
#ifdef DEBUG
	ErrorF("XqBlock: not calling ProcessInputEvents()\n");
#endif
	;
    }
}

/*
 * XqEnable --
 *      Enable the handling of the Xque
 */

static int
XqEnable(InputInfoPtr pInfo)
{
    MouseDevPtr pMse;
    XqInfoPtr pXq;
    static struct kd_quemode xqueMode;
    static Bool was_here = FALSE;

    pMse = pInfo->private;
    pXq = pMse->mousePriv;

    if (xqueFd < 0) {
	if ((xqueFd = open("/dev/mouse", O_RDONLY | O_NDELAY)) < 0) {
	    if (xf86GetAllowMouseOpenFail()) {
		xf86Msg(X_WARNING,
		    "%s: Cannot open /dev/mouse (%s) - Continuing...\n",
		    pInfo->name, strerror(errno));
		return Success;
	    } else {
		xf86Msg(X_ERROR, "%s: Cannot open /dev/mouse (%s)\n",
			pInfo->name, strerror(errno));
		return !Success;
	    }
	}
    }
#ifndef XQUEUE_ASYNC
    if (!was_here) {
	pipe(xquePipe);
	fcntl(xquePipe[0], F_SETFL, fcntl(xquePipe[0], F_GETFL, 0) | O_NDELAY);
	fcntl(xquePipe[1], F_SETFL, fcntl(xquePipe[1], F_GETFL, 0) | O_NDELAY);
	was_here = TRUE;
    }
#endif

    if (pXq->xqueSema++ == 0) {
#ifdef XQUEUE_ASYNC
	(void) signal(SIGUSR2, XqDoInput);
#else
	(void) signal(SIGUSR2, xf86XqueSignal);
#endif
	xqueMode.qsize = 64;    /* max events */
	xqueMode.signo = SIGUSR2;
	ioctl(xf86Info.consoleFd, KDQUEMODE, NULL);

	if (ioctl(xf86Info.consoleFd, KDQUEMODE, &xqueMode) < 0) {
	    xf86Msg(X_ERROR, "%s: Cannot set KDQUEMODE", pInfo->name);
	    return !Success;
	}
	XqueQaddr = (xqEventQueue *)xqueMode.qaddr;
	XqueQaddr->xq_sigenable = 1; /* UNLOCK */
    }

    return Success;
}



/*
 * xf86XqueDisable --
 *      disable the handling of the Xque
 */

static int
XqDisable(InputInfoPtr pInfo)
{
    MouseDevPtr pMse;
    XqInfoPtr pXq;

    pMse = pInfo->private;
    pXq = pMse->mousePriv;

    if (pXq->xqueSema-- == 1)
    {
	XqueQaddr->xq_sigenable = 0; /* LOCK */
      
	if (ioctl(xf86Info.consoleFd, KDQUEMODE, NULL) < 0) {
	    xf86Msg(X_ERROR, "%s: Cannot unset KDQUEMODE", pInfo->name);
	    return !Success;
	}
    }

    if (xqueFd >= 0) {
	close(xqueFd);
	xqueFd = -1;
    }

    return Success;
}

/*
 * XqMouseProc --
 *      Handle the initialization, etc. of a mouse
 */

static int
XqMouseProc(DeviceIntPtr pPointer, int what)
{
    InputInfoPtr pInfo;
    MouseDevPtr pMse;
    unchar        map[4];
    int ret;
 
    pInfo = pPointer->public.devicePrivate;
    pMse = pInfo->private;
    pMse->device = pPointer;

    switch (what) {
    case DEVICE_INIT: 
	pPointer->public.on = FALSE;

	map[1] = 1;
	map[2] = 2;
	map[3] = 3;

	InitPointerDeviceStruct((DevicePtr)pPointer, 
				map, 
				3, 
				miPointerGetMotionEvents, 
				pMse->Ctrl,
				miPointerGetMotionBufferSize());
	/* X valuator */
	xf86InitValuatorAxisStruct(pPointer, 0, 0, -1, 1, 0, 1);
	xf86InitValuatorDefaults(pPointer, 0);
	/* Y valuator */
	xf86InitValuatorAxisStruct(pPointer, 1, 0, -1, 1, 0, 1);
	xf86InitValuatorDefaults(pPointer, 1);
	xf86MotionHistoryAllocate(pInfo);
	RegisterBlockAndWakeupHandlers(XqBlock, (WakeupHandlerProcPtr)NoopDDA,
					pInfo);
	break;
      
    case DEVICE_ON:
	pMse->lastButtons = 0;
	pMse->emulateState = 0;
	pPointer->public.on = TRUE;
	ret = XqEnable(pInfo);
#ifndef XQUEUE_ASYNC
	if (xquePipe[0] != -1) {
	    pInfo->fd = xquePipe[0];
	    AddEnabledDevice(xquePipe[0]);
	}
#endif
	return ret;
      
    case DEVICE_CLOSE:
    case DEVICE_OFF:
	pPointer->public.on = FALSE;
	ret = XqDisable(pInfo);
#ifndef XQUEUE_ASYNC
	if (xquePipe[0] != -1) {
	    RemoveEnabledDevice(xquePipe[0]);
	    pInfo->fd = -1;
	}
#endif
	return ret;
    }
    return Success;
}

Bool
XqueueMousePreInit(InputInfoPtr pInfo, const char *protocol, int flags)
{
    MouseDevPtr pMse;
    XqInfoPtr pXq;

    pMse = pInfo->private;
    pMse->protocol = protocol;
    xf86Msg(X_CONFIG, "%s: Protocol: %s\n", pInfo->name, protocol);
    pXq = pMse->mousePriv = xnfcalloc(sizeof(XqInfoRec), 1);

    /* Collect the options, and process the common options. */
    xf86CollectInputOptions(pInfo, NULL, NULL);
    xf86ProcessCommonOptions(pInfo, pInfo->options);

    /* Process common mouse options (like Emulate3Buttons, etc). */
    pMse->CommonOptions(pInfo);

    /* Setup the local procs. */
    pInfo->device_control = XqMouseProc;
#ifdef XQUEUE_ASYNC
    pInfo->read_input = NULL;
#else
    pInfo->read_input = XqReadInput;
#endif
    pInfo->fd = -1;

    XqMouse = pInfo;

    pInfo->flags |= XI86_CONFIGURED;
    return TRUE;
}

#endif /* XQUEUE */