summaryrefslogtreecommitdiff
path: root/src/sun_mouse.c
blob: 1f01f1766d7e9d892527b1d8ec17d804d13fcb58 (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
740
741
742
743
744
/*
 * Copyright 1999-2001 The XFree86 Project, 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, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * 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.  IN NO EVENT SHALL THE
 * XFREE86 PROJECT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the name of the XFree86 Project shall
 * not be used in advertising or otherwise to promote the sale, use or other
 * dealings in this Software without prior written authorization from the
 * XFree86 Project.
 */
/* Copyright 2004-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.
 */

#ifdef HAVE_XORG_CONFIG_H
#include <xorg-config.h>
#endif

#include "xorg-server.h"
#include "xf86.h"
#include "xf86_OSlib.h"
#include "xf86OSmouse.h"
#include "xisb.h"
#include "mipointer.h"
#include <sys/stropts.h>
#include <sys/vuid_event.h>
#include <sys/msio.h>

/* Wheel mouse support in VUID drivers in Solaris 9 updates & Solaris 10 */
#ifdef WHEEL_DEVID /* Defined in vuid_event.h if VUID wheel support present */
# define HAVE_VUID_WHEEL
#endif
#ifdef HAVE_VUID_WHEEL
# include <sys/vuid_wheel.h>
#endif

/* Support for scaling absolute coordinates to screen size in 
 * Solaris 10 updates and beyond */
#if !defined(HAVE_ABSOLUTE_MOUSE_SCALING)
# ifdef MSIOSRESOLUTION /* Defined in msio.h if scaling support present */
#  define HAVE_ABSOLUTE_MOUSE_SCALING
# endif
#endif

/* Names of protocols that are handled internally here. */

static const char *internalNames[] = {
	"VUID",
	NULL
};

static const char *solarisMouseDevs[] = {
    /* Device file:	Protocol: 			*/
    "/dev/mouse",	"VUID",		/* USB or SPARC */
#if defined(__i386) || defined(__x86)
    "/dev/kdmouse",	"PS/2",		/* PS/2 */
#endif
    NULL
};

typedef struct _VuidMseRec {
    struct _VuidMseRec *next;    
    InputInfoPtr	pInfo;
    Firm_event 		event;
    unsigned char *	buffer;
    char *		strmod;
    Bool(*wrapped_device_control)(DeviceIntPtr device, int what);
#ifdef HAVE_ABSOLUTE_MOUSE_SCALING
    Ms_screen_resolution	 absres;
#endif
} VuidMseRec, *VuidMsePtr;

static VuidMsePtr	vuidMouseList = NULL;

static int  vuidMouseProc(DeviceIntPtr pPointer, int what);
static void vuidReadInput(InputInfoPtr pInfo);

#ifdef HAVE_ABSOLUTE_MOUSE_SCALING
static void vuidMouseSendScreenSize(ScreenPtr pScreen, VuidMsePtr pVuidMse);
static void vuidMouseAdjustFrame(int index, int x, int y, int flags);

static int vuidMouseGeneration = 0;
static int vuidMouseScreenIndex;
#define vuidMouseGetScreenPrivate(s) ( \
    dixLookupPrivate(&(s)->devPrivates, &vuidMouseScreenIndex))
#define vuidMouseSetScreenPrivate(s,p) \
    dixSetPrivate(&(s)->devPrivates, &vuidMouseScreenIndex, (void *) p)
#endif /* HAVE_ABSOLUTE_MOUSE_SCALING */

static inline
VuidMsePtr getVuidMsePriv(InputInfoPtr pInfo)
{
    VuidMsePtr m = vuidMouseList;

    while ((m != NULL) && (m->pInfo != pInfo)) {
	m = m->next;
    }

    return m;
}


/*
 * Initialize and enable the mouse wheel, if present.
 *
 * Returns 1 if mouse wheel was successfully enabled.
 * Returns 0 if an error occurred or if there is no mouse wheel.
 */
static int
vuidMouseWheelInit(InputInfoPtr pInfo)
{
#ifdef HAVE_VUID_WHEEL
    wheel_state wstate;
    int nwheel = -1;
    int i;

    wstate.vers = VUID_WHEEL_STATE_VERS;
    wstate.id = 0;
    wstate.stateflags = (uint32_t) -1;

    SYSCALL(i = ioctl(pInfo->fd, VUIDGWHEELCOUNT, &nwheel));
    if (i != 0)
	return (0);

    SYSCALL(i = ioctl(pInfo->fd, VUIDGWHEELSTATE, &wstate));
    if (i != 0) {
	xf86Msg(X_WARNING, "%s: couldn't get wheel state\n", pInfo->name);
	return (0);
    }

    wstate.stateflags |= VUID_WHEEL_STATE_ENABLED;

    SYSCALL(i = ioctl(pInfo->fd, VUIDSWHEELSTATE, &wstate));
    if (i != 0) {
	xf86Msg(X_WARNING, "%s: couldn't enable wheel\n", pInfo->name);
	return (0);
    }

    return (1);
#else
    return (0);
#endif
}


/* This function is called when the protocol is "VUID". */
static Bool
vuidPreInit(InputInfoPtr pInfo, const char *protocol, int flags)
{
    MouseDevPtr pMse = pInfo->private;
    VuidMsePtr pVuidMse;
    int buttons, i;

    pVuidMse = xcalloc(sizeof(VuidMseRec), 1);
    if (pVuidMse == NULL) {
	xf86Msg(X_ERROR, "%s: cannot allocate VuidMouseRec\n", pInfo->name);
	xfree(pMse);
	return FALSE;
    }

    pMse->protocol = protocol;
    xf86Msg(X_CONFIG, "%s: Protocol: %s\n", pInfo->name, protocol);

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

    /* Check if the device can be opened. */
    pInfo->fd = xf86OpenSerial(pInfo->options);
    if (pInfo->fd == -1) {
	if (xf86GetAllowMouseOpenFail())
	    xf86Msg(X_WARNING, "%s: cannot open input device\n", pInfo->name);
	else {
	    xf86Msg(X_ERROR, "%s: cannot open input device\n", pInfo->name);
	    xfree(pVuidMse);
	    xfree(pMse);
	    return FALSE;
	}
    }

    pVuidMse->buffer = (unsigned char *)&pVuidMse->event;

    pVuidMse->strmod = xf86SetStrOption(pInfo->options, "StreamsModule", NULL);
    if (pVuidMse->strmod) {
	SYSCALL(i = ioctl(pInfo->fd, I_PUSH, pVuidMse->strmod));
 	if (i < 0) {
	    xf86Msg(X_ERROR,
		    "%s: cannot push module '%s' onto mouse device: %s\n",
		    pInfo->name, pVuidMse->strmod, strerror(errno));
	    xf86CloseSerial(pInfo->fd);
	    pInfo->fd = -1;
	    xfree(pVuidMse->strmod);
	    xfree(pVuidMse);
	    xfree(pMse);
	    return FALSE;
	}
    }

    buttons = xf86SetIntOption(pInfo->options, "Buttons", 0);
    if (buttons == 0) {
	SYSCALL(i = ioctl(pInfo->fd, MSIOBUTTONS, &buttons));
	if (i == 0) {
	    pInfo->conf_idev->commonOptions =
		xf86ReplaceIntOption(pInfo->conf_idev->commonOptions, 
				     "Buttons", buttons);
	    xf86Msg(X_INFO, "%s: Setting Buttons option to \"%d\"\n",
		    pInfo->name, buttons);
	}
    }

    if (pVuidMse->strmod) { 
	SYSCALL(i = ioctl(pInfo->fd, I_POP, pVuidMse->strmod));
	if (i == -1) {
	    xf86Msg(X_WARNING,
		    "%s: cannot pop module '%s' off mouse device: %s\n",
		    pInfo->name, pVuidMse->strmod, strerror(errno));
	}
    }

    xf86CloseSerial(pInfo->fd);
    pInfo->fd = -1;

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

    /* Setup the local procs. */
    pVuidMse->wrapped_device_control = pInfo->device_control;
    pInfo->device_control = vuidMouseProc;
    pInfo->read_input = vuidReadInput;

    pMse->xisbscale = sizeof(Firm_event);

#ifdef HAVE_ABSOLUTE_MOUSE_SCALING    
    pVuidMse->absres.height = pVuidMse->absres.width = 0;
#endif
    pVuidMse->pInfo = pInfo;
    pVuidMse->next = vuidMouseList; 
    vuidMouseList = pVuidMse;

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

static void
vuidFlushAbsEvents(InputInfoPtr pInfo, int absX, int absY, 
		   Bool *absXset, Bool *absYset)
{
#ifdef DEBUG
    ErrorF("vuidFlushAbsEvents: %d,%d (set: %d, %d)\n", absX, absY, 
	   *absXset, *absYset);
#endif
    if ((*absXset) && (*absYset)) {
	xf86PostMotionEvent(pInfo->dev, 
			    /* is_absolute: */    TRUE,
			    /* first_valuator: */ 0,
			    /* num_valuators: */  2,
			    absX, absY);
    } else if (*absXset) {
	xf86PostMotionEvent(pInfo->dev, 
			    /* is_absolute: */    TRUE,
			    /* first_valuator: */ 0,
			    /* num_valuators: */  1,
			    absX);
    } else if (*absYset) {
	xf86PostMotionEvent(pInfo->dev, 
			    /* is_absolute: */    TRUE,
			    /* first_valuator: */ 1,
			    /* num_valuators: */  1,
			    absY);
    }

    *absXset = FALSE;
    *absYset = FALSE;
}

static void
vuidReadInput(InputInfoPtr pInfo)
{
    MouseDevPtr pMse;
    VuidMsePtr pVuidMse;
    int buttons;
    int dx = 0, dy = 0, dz = 0, dw = 0;
    unsigned int n;
    int c; 
    unsigned char *pBuf;
    int absX = 0, absY = 0;
    Bool absXset = FALSE, absYset = FALSE;

    pMse = pInfo->private;
    pVuidMse = getVuidMsePriv(pInfo);
    buttons = pMse->lastButtons;
    XisbBlockDuration(pMse->buffer, -1);
    pBuf = pVuidMse->buffer;
    n = 0;

    do {
	while (n < sizeof(Firm_event) && (c = XisbRead(pMse->buffer)) >= 0) {
	    pBuf[n++] = (unsigned char)c;
	}

	if (n == 0)
	    return;

	if (n != sizeof(Firm_event)) {
	    xf86Msg(X_WARNING, "%s: incomplete packet, size %d\n",
			pInfo->name, n);
	}

#ifdef DEBUG
	ErrorF("vuidReadInput: event type: %3d value: %5d\n",
	       pVuidMse->event.id, pVuidMse->event.value);
#endif

	if (pVuidMse->event.id >= BUT_FIRST && pVuidMse->event.id <= BUT_LAST) {
	    /* button */
	    int butnum = pVuidMse->event.id - BUT_FIRST;

	    if (butnum < 3)
		butnum = 2 - butnum;
	    if (!pVuidMse->event.value)
		buttons &= ~(1 << butnum);
	    else
		buttons |= (1 << butnum);
	} else if (pVuidMse->event.id >= VLOC_FIRST &&
		   pVuidMse->event.id <= VLOC_LAST) {
	    /* axis */
	    int delta = pVuidMse->event.value;
	    switch(pVuidMse->event.id) {
	    case LOC_X_DELTA:
		dx += delta;
		break;
	    case LOC_Y_DELTA:
		dy -= delta;
		break;
	    case LOC_X_ABSOLUTE:
		if (absXset) {
		    vuidFlushAbsEvents(pInfo, absX, absY, &absXset, &absYset);
		}
		absX = delta;
		absXset = TRUE;
		break;
	    case LOC_Y_ABSOLUTE:
		if (absYset) {
		    vuidFlushAbsEvents(pInfo, absX, absY, &absXset, &absYset);
		}
		absY = delta;
		absYset = TRUE;
		break;
	    }
	} 
#ifdef HAVE_VUID_WHEEL
	else if (vuid_in_range(VUID_WHEEL, pVuidMse->event.id)) {
	    if (vuid_id_offset(pVuidMse->event.id) == 0)
		dz -= VUID_WHEEL_GETDELTA(pVuidMse->event.value);
	    else
		dw -= VUID_WHEEL_GETDELTA(pVuidMse->event.value);
	}
#endif
#ifdef HAVE_ABSOLUTE_MOUSE_SCALING
	else if (pVuidMse->event.id == MOUSE_TYPE_ABSOLUTE) {
	    ScreenPtr 	ptrCurScreen;

	    /* force sending absolute resolution scaling ioctl */
	    pVuidMse->absres.height = pVuidMse->absres.width = 0;
#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) == 0
	    ptrCurScreen = miPointerCurrentScreen();
#else
	    ptrCurScreen = miPointerGetScreen(pInfo->dev);
#endif	    
	    vuidMouseSendScreenSize(ptrCurScreen, pVuidMse);
	}
#endif

	n = 0;
	if ((c = XisbRead(pMse->buffer)) >= 0) {
	    /* Another packet.  Handle it right away. */
	    pBuf[n++] = c;
	}
    } while (n != 0);

    if (absXset || absYset) {
	vuidFlushAbsEvents(pInfo, absX, absY, &absXset, &absYset);
    }

    pMse->PostEvent(pInfo, buttons, dx, dy, dz, dw);
    return;
}

#ifdef HAVE_ABSOLUTE_MOUSE_SCALING
static void vuidMouseSendScreenSize(ScreenPtr pScreen, VuidMsePtr pVuidMse)
{
    InputInfoPtr pInfo = pVuidMse->pInfo;
    ScrnInfoPtr pScr = XF86SCRNINFO(pScreen);
    int result;

    if ((pVuidMse->absres.width != pScr->currentMode->HDisplay) || 
	(pVuidMse->absres.height != pScr->currentMode->VDisplay))
    {
	pVuidMse->absres.width = pScr->currentMode->HDisplay;
	pVuidMse->absres.height = pScr->currentMode->VDisplay;

	do {
	    result = ioctl(pInfo->fd, MSIOSRESOLUTION, &(pVuidMse->absres));
	} while ( (result != 0) && (errno == EINTR) );

	if (result != 0) {
	    xf86Msg(X_WARNING, 
		    "%s: couldn't set absolute mouse scaling resolution: %s\n",
		    pInfo->name, strerror(errno));
#ifdef DEBUG
	} else {
	    xf86Msg(X_INFO, 
		    "%s: absolute mouse scaling resolution set to %d x %d\n", 
		    pInfo->name, 
		    pVuidMse->absres.width, pVuidMse->absres.height);
#endif
	}
    }
}

static void vuidMouseAdjustFrame(int index, int x, int y, int flags)
{
      ScrnInfoPtr	pScrn = xf86Screens[index];
      ScreenPtr		pScreen = pScrn->pScreen;
      xf86AdjustFrameProc *wrappedAdjustFrame 
	  = (xf86AdjustFrameProc *) vuidMouseGetScreenPrivate(pScreen);
      VuidMsePtr	m;
      ScreenPtr 	ptrCurScreen;

      if(wrappedAdjustFrame) {
        pScrn->AdjustFrame = wrappedAdjustFrame;
        (*pScrn->AdjustFrame)(index, x, y, flags);
        pScrn->AdjustFrame = vuidMouseAdjustFrame;
      }

#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) == 0
      ptrCurScreen = miPointerCurrentScreen();
#endif
      
      for (m = vuidMouseList; m != NULL ; m = m->next) {
#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 0
	  ptrCurScreen = miPointerGetScreen(m->pInfo->dev);
#endif
	  if (ptrCurScreen == pScreen)
	  {
	      vuidMouseSendScreenSize(pScreen, m);
	  }
      }
}
#endif /* HAVE_ABSOLUTE_MOUSE_SCALING */


static int
vuidMouseProc(DeviceIntPtr pPointer, int what)
{
    InputInfoPtr pInfo;
    MouseDevPtr pMse;
    VuidMsePtr pVuidMse;
    int ret = Success;
    int i;

    pInfo = pPointer->public.devicePrivate;
    pMse = pInfo->private;
    pMse->device = pPointer;

    pVuidMse = getVuidMsePriv(pInfo);
    if (pVuidMse == NULL) {
	return BadImplementation;
    }
    
    switch (what) {

    case DEVICE_INIT:
#ifdef HAVE_ABSOLUTE_MOUSE_SCALING
	if (vuidMouseGeneration != serverGeneration) {
		for (i = 0; i < screenInfo.numScreens; i++) {
		    ScreenPtr pScreen = screenInfo.screens[i];
		    ScrnInfoPtr pScrn = XF86SCRNINFO(pScreen);
		    vuidMouseSetScreenPrivate(pScreen, pScrn->AdjustFrame);
		    pScrn->AdjustFrame = vuidMouseAdjustFrame;
		}
	    vuidMouseGeneration = serverGeneration;
	}
#endif    	
	ret = pVuidMse->wrapped_device_control(pPointer, what);
	break;
	
    case DEVICE_ON:
	ret = pVuidMse->wrapped_device_control(pPointer, DEVICE_ON);

	if ((ret == Success) && (pInfo->fd != -1)) {
	    int fmt = VUID_FIRM_EVENT;
	    
	    if (pVuidMse->strmod) {
		SYSCALL(i = ioctl(pInfo->fd, I_PUSH, pVuidMse->strmod));
		if (i < 0) {
		    xf86Msg(X_WARNING,
			"%s: cannot push module '%s' onto mouse device: %s\n",
			pInfo->name, pVuidMse->strmod, strerror(errno));
		    xfree(pVuidMse->strmod);
		    pVuidMse->strmod = NULL;
		}
	    }
	    SYSCALL(i = ioctl(pInfo->fd, VUIDSFORMAT, &fmt));
	    if (i < 0) {
		xf86Msg(X_WARNING,
			"%s: cannot set mouse device to VUID mode: %s\n",
			pInfo->name, strerror(errno));
	    }
	    vuidMouseWheelInit(pInfo);
#ifdef HAVE_ABSOLUTE_MOUSE_SCALING	    
	    vuidMouseSendScreenSize(screenInfo.screens[0], pVuidMse);
#endif	    
	    xf86FlushInput(pInfo->fd);
	}
	break;

    case DEVICE_OFF:
    case DEVICE_CLOSE:
	if (pInfo->fd != -1) {
	    if (pVuidMse->strmod) {
		SYSCALL(i = ioctl(pInfo->fd, I_POP, pVuidMse->strmod));
		if (i == -1) {
		    xf86Msg(X_WARNING,
		      "%s: cannot pop module '%s' off mouse device: %s\n",
		      pInfo->name, pVuidMse->strmod, strerror(errno));
		}
	    }
	}
	ret = pVuidMse->wrapped_device_control(pPointer, what);
	break;

    default: /* Should never be called, but just in case */
	ret = pVuidMse->wrapped_device_control(pPointer, what);
	break;
    }
    return ret;
}

static Bool
sunMousePreInit(InputInfoPtr pInfo, const char *protocol, int flags)
{
    /* The protocol is guaranteed to be one of the internalNames[] */
    if (xf86NameCmp(protocol, "VUID") == 0) {
	return vuidPreInit(pInfo, protocol, flags);
    }
    return TRUE;
}    

static const char **
BuiltinNames(void)
{
    return internalNames;
}

static Bool
CheckProtocol(const char *protocol)
{
    int i;

    for (i = 0; internalNames[i]; i++)
	if (xf86NameCmp(protocol, internalNames[i]) == 0)
	    return TRUE;

    return FALSE;
}

static const char *
DefaultProtocol(void)
{
    return "Auto";
}

static Bool
solarisMouseAutoProbe(InputInfoPtr pInfo, const char **protocol, 
	const char **device)
{
    const char **pdev, **pproto;
    int fd = -1;
    Bool found;
    char *strmod;

    if (*device == NULL) {
	/* Check to see if xorg.conf or HAL specified a device to use */
	*device = xf86CheckStrOption(pInfo->options, "Device", NULL);
	if (*device == NULL) {
	    *device = xf86CheckStrOption(pInfo->conf_idev->commonOptions,
					 "Device", NULL);
	}
    }

    if (*device != NULL) {
	strmod = xf86CheckStrOption(pInfo->options, "StreamsModule", NULL);
	if (strmod) {
	    /* if a device name is already known, and a StreamsModule is
	       specified to convert events to VUID, then we don't need to
	       probe further */
	    *protocol = "VUID";
	    return TRUE;
	}
    }


    for (pdev = solarisMouseDevs; *pdev; pdev += 2) {
	pproto = pdev + 1;
	if ((*protocol != NULL) && (strcmp(*protocol, "Auto") != 0) &&
	  (*pproto != NULL) && (strcmp(*pproto, *protocol) != 0)) {
	    continue;
	}
	if ((*device != NULL) && (strcmp(*device, *pdev) != 0)) {
	    continue;
	}
        SYSCALL (fd = open(*pdev, O_RDWR | O_NONBLOCK));
	if (fd == -1) {
#ifdef DEBUG
	    ErrorF("Cannot open %s (%s)\n", pdev, strerror(errno));
#endif
	} else {
	    found = TRUE;
	    if ((*pproto != NULL) && (strcmp(*pproto, "VUID") == 0)) {
		int i, r;
		SYSCALL(r = ioctl(fd, VUIDGFORMAT, &i));
    		if (r < 0) {
		    found = FALSE;
		}
	    }
	    close(fd);
	    if (found == TRUE) {
		if (*pproto != NULL) {
		    *protocol = *pproto;
		}
		*device = *pdev;
		return TRUE;
	    }
	}
    }
    return FALSE;
}

static const char *
SetupAuto(InputInfoPtr pInfo, int *protoPara)
{
    const char *pdev = NULL;
    const char *pproto = NULL;
    MouseDevPtr pMse = pInfo->private;

    if (pInfo->fd == -1) {
	/* probe to find device/protocol to use */
	if (solarisMouseAutoProbe(pInfo, &pproto, &pdev) != FALSE) {
	    /* Set the Device option. */
	    pInfo->conf_idev->commonOptions =
	     xf86AddNewOption(pInfo->conf_idev->commonOptions, "Device", pdev);
	    xf86Msg(X_INFO, "%s: Setting Device option to \"%s\"\n",
	      pInfo->name, pdev);
	}
    } else if (pMse->protocolID == PROT_AUTO) {
	pdev = xf86CheckStrOption(pInfo->conf_idev->commonOptions, 
		"Device", NULL);
	solarisMouseAutoProbe(pInfo, &pproto, &pdev);
    }
    return pproto;
}

static const char *
FindDevice(InputInfoPtr pInfo, const char *protocol, int flags)
{
    const char *pdev = NULL;
    const char *pproto = protocol;

    if (solarisMouseAutoProbe(pInfo, &pproto, &pdev) != FALSE) {
	/* Set the Device option. */
	pInfo->conf_idev->commonOptions =
	  xf86AddNewOption(pInfo->conf_idev->commonOptions, "Device", pdev);
	xf86Msg(X_INFO, "%s: Setting Device option to \"%s\"\n",
	  pInfo->name, pdev);
    }
    return pdev;
}

static int
SupportedInterfaces(void)
{
    /* XXX This needs to be checked. */
    return MSE_SERIAL | MSE_BUS | MSE_PS2 | MSE_AUTO | MSE_XPS2 | MSE_MISC;
}

_X_EXPORT OSMouseInfoPtr
xf86OSMouseInit(int flags)
{
    OSMouseInfoPtr p;

    p = xcalloc(sizeof(OSMouseInfoRec), 1);
    if (!p)
	return NULL;
    p->SupportedInterfaces = SupportedInterfaces;
    p->BuiltinNames = BuiltinNames;
    p->CheckProtocol = CheckProtocol;
    p->PreInit = sunMousePreInit;
    p->DefaultProtocol = DefaultProtocol;
    p->SetupAuto = SetupAuto;
    p->FindDevice = FindDevice;

    return p;
}