mouse.c revision 659607e0
1659607e0Smrg/* $XdotOrg: driver/xf86-input-mouse/src/mouse.c,v 1.29 2006/04/21 11:15:23 mhopf Exp $ */
2659607e0Smrg/* $XFree86: xc/programs/Xserver/hw/xfree86/input/mouse/mouse.c,v 1.79 2003/11/03 05:11:48 tsi Exp $ */
3659607e0Smrg/*
4659607e0Smrg *
5659607e0Smrg * Copyright 1990,91 by Thomas Roell, Dinkelscherben, Germany.
6659607e0Smrg * Copyright 1993 by David Dawes <dawes@xfree86.org>
7659607e0Smrg * Copyright 2002 by SuSE Linux AG, Author: Egbert Eich
8659607e0Smrg * Copyright 1994-2002 by The XFree86 Project, Inc.
9659607e0Smrg * Copyright 2002 by Paul Elliott
10659607e0Smrg *
11659607e0Smrg * Permission to use, copy, modify, distribute, and sell this software and its
12659607e0Smrg * documentation for any purpose is hereby granted without fee, provided that
13659607e0Smrg * the above copyright notice appear in all copies and that both that
14659607e0Smrg * copyright notice and this permission notice appear in supporting
15659607e0Smrg * documentation, and that the names of copyright holders not be
16659607e0Smrg * used in advertising or publicity pertaining to distribution of the
17659607e0Smrg * software without specific, written prior permission.  The copyright holders
18659607e0Smrg * make no representations about the suitability of this
19659607e0Smrg * software for any purpose.  It is provided "as is" without express or
20659607e0Smrg * implied warranty.
21659607e0Smrg *
22659607e0Smrg * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
23659607e0Smrg * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
24659607e0Smrg * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
25659607e0Smrg * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
26659607e0Smrg * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
27659607e0Smrg * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
28659607e0Smrg * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29659607e0Smrg *
30659607e0Smrg */
31659607e0Smrg/* Patch for PS/2 Intellimouse - Tim Goodwin 1997-11-06. */
32659607e0Smrg
33659607e0Smrg/*
34659607e0Smrg * [JCH-96/01/21] Added fourth button support for PROT_GLIDEPOINT mouse
35659607e0Smrg * protocol.
36659607e0Smrg */
37659607e0Smrg
38659607e0Smrg/*
39659607e0Smrg * [TVO-97/03/05] Added microsoft IntelliMouse support
40659607e0Smrg */
41659607e0Smrg
42659607e0Smrg/*
43659607e0Smrg * [PME-02/08/11] Added suport for drag lock buttons
44659607e0Smrg * for use with 4 button trackballs for convenience
45659607e0Smrg * and to help limited dexterity persons
46659607e0Smrg */
47659607e0Smrg
48659607e0Smrg#ifdef HAVE_CONFIG_H
49659607e0Smrg#include "config.h"
50659607e0Smrg#endif
51659607e0Smrg
52659607e0Smrg#include <math.h>
53659607e0Smrg#include <string.h>
54659607e0Smrg#include <stdio.h>
55659607e0Smrg#include <stdlib.h>
56659607e0Smrg#define NEED_EVENTS
57659607e0Smrg#include <X11/X.h>
58659607e0Smrg#include <X11/Xproto.h>
59659607e0Smrg
60659607e0Smrg#include "xf86.h"
61659607e0Smrg
62659607e0Smrg#ifdef XINPUT
63659607e0Smrg#include <X11/extensions/XI.h>
64659607e0Smrg#include <X11/extensions/XIproto.h>
65659607e0Smrg#include "extnsionst.h"
66659607e0Smrg#include "extinit.h"
67659607e0Smrg#else
68659607e0Smrg#include "inputstr.h"
69659607e0Smrg#endif
70659607e0Smrg
71659607e0Smrg#include "xf86Xinput.h"
72659607e0Smrg#include "xf86_OSproc.h"
73659607e0Smrg#include "xf86OSmouse.h"
74659607e0Smrg
75659607e0Smrg#ifndef NEED_XF86_TYPES
76659607e0Smrg#define NEED_XF86_TYPES	/* for xisb.h when !XFree86LOADER */
77659607e0Smrg#endif
78659607e0Smrg
79659607e0Smrg#include "compiler.h"
80659607e0Smrg
81659607e0Smrg#include "xisb.h"
82659607e0Smrg#include "mouse.h"
83659607e0Smrg#include "mousePriv.h"
84659607e0Smrg#include "mipointer.h"
85659607e0Smrg
86659607e0Smrgenum {
87659607e0Smrg    /* number of bits in mapped nibble */
88659607e0Smrg    NIB_BITS=4,
89659607e0Smrg    /* size of map of nibbles to bitmask */
90659607e0Smrg    NIB_SIZE= (1 << NIB_BITS),
91659607e0Smrg    /* mask for map */
92659607e0Smrg    NIB_MASK= (NIB_SIZE -1),
93659607e0Smrg    /* number of maps to map all the buttons */
94659607e0Smrg    NIB_COUNT = ((MSE_MAXBUTTONS+NIB_BITS-1)/NIB_BITS)
95659607e0Smrg};
96659607e0Smrg
97659607e0Smrg/*data to be used in implementing trackball drag locks.*/
98659607e0Smrgtypedef struct _DragLockRec {
99659607e0Smrg
100659607e0Smrg    /* Fields used to implement trackball drag locks. */
101659607e0Smrg    /* mask for those buttons that are ordinary drag lock buttons */
102659607e0Smrg    int lockButtonsM;
103659607e0Smrg
104659607e0Smrg    /* mask for the master drag lock button if any */
105659607e0Smrg    int masterLockM;
106659607e0Smrg
107659607e0Smrg    /* button state up/down from last time adjusted for drag locks */
108659607e0Smrg    int lockLastButtons;
109659607e0Smrg
110659607e0Smrg    /*
111659607e0Smrg     * true if master lock state i.e. master drag lock
112659607e0Smrg     * button has just been pressed
113659607e0Smrg     */
114659607e0Smrg    int masterTS;
115659607e0Smrg
116659607e0Smrg    /* simulate these buttons being down although they are not */
117659607e0Smrg    int simulatedDown;
118659607e0Smrg
119659607e0Smrg    /*
120659607e0Smrg     * data to map bits for drag lock buttons to corresponding
121659607e0Smrg     * bits for the target buttons
122659607e0Smrg     */
123659607e0Smrg    int nib_table[NIB_COUNT][NIB_SIZE];
124659607e0Smrg
125659607e0Smrg} DragLockRec, *DragLockPtr;
126659607e0Smrg
127659607e0Smrg
128659607e0Smrg
129659607e0Smrg#ifdef XFree86LOADER
130659607e0Smrgstatic const OptionInfoRec *MouseAvailableOptions(void *unused);
131659607e0Smrg#endif
132659607e0Smrgstatic InputInfoPtr MousePreInit(InputDriverPtr drv, IDevPtr dev, int flags);
133659607e0Smrg#if 0
134659607e0Smrgstatic void MouseUnInit(InputDriverPtr drv, InputInfoPtr pInfo, int flags);
135659607e0Smrg#endif
136659607e0Smrg
137659607e0Smrgstatic int MouseProc(DeviceIntPtr device, int what);
138659607e0Smrgstatic Bool MouseConvert(LocalDevicePtr local, int first, int num, int v0,
139659607e0Smrg		 	     int v1, int v2, int v3, int v4, int v5, int *x,
140659607e0Smrg		 	     int *y);
141659607e0Smrg
142659607e0Smrgstatic void MouseCtrl(DeviceIntPtr device, PtrCtrl *ctrl);
143659607e0Smrgstatic void MousePostEvent(InputInfoPtr pInfo, int buttons,
144659607e0Smrg			   int dx, int dy, int dz, int dw);
145659607e0Smrgstatic void MouseReadInput(InputInfoPtr pInfo);
146659607e0Smrgstatic void MouseBlockHandler(pointer data, struct timeval **waitTime,
147659607e0Smrg			      pointer LastSelectMask);
148659607e0Smrgstatic void MouseWakeupHandler(pointer data, int i, pointer LastSelectMask);
149659607e0Smrgstatic void FlushButtons(MouseDevPtr pMse);
150659607e0Smrg
151659607e0Smrgstatic Bool SetupMouse(InputInfoPtr pInfo);
152659607e0Smrgstatic Bool initMouseHW(InputInfoPtr pInfo);
153659607e0Smrg#ifdef SUPPORT_MOUSE_RESET
154659607e0Smrgstatic Bool mouseReset(InputInfoPtr pInfo, unsigned char val);
155659607e0Smrgstatic void ps2WakeupHandler(pointer data, int i, pointer LastSelectMask);
156659607e0Smrgstatic void ps2BlockHandler(pointer data, struct timeval **waitTime,
157659607e0Smrg			    pointer LastSelectMask);
158659607e0Smrg#endif
159659607e0Smrg
160659607e0Smrg/* mouse autoprobe stuff */
161659607e0Smrgstatic const char *autoOSProtocol(InputInfoPtr pInfo, int *protoPara);
162659607e0Smrgstatic void autoProbeMouse(InputInfoPtr pInfo, Bool inSync, Bool lostSync);
163659607e0Smrgstatic void checkForErraticMovements(InputInfoPtr pInfo, int dx, int dy);
164659607e0Smrgstatic Bool collectData(MouseDevPtr pMse, unsigned char u);
165659607e0Smrgstatic void SetMouseProto(MouseDevPtr pMse, MouseProtocolID protocolID);
166659607e0Smrgstatic Bool autoGood(MouseDevPtr pMse);
167659607e0Smrg
168659607e0Smrg#undef MOUSE
169659607e0Smrg_X_EXPORT InputDriverRec MOUSE = {
170659607e0Smrg	1,
171659607e0Smrg	"mouse",
172659607e0Smrg	NULL,
173659607e0Smrg	MousePreInit,
174659607e0Smrg	/*MouseUnInit,*/NULL,
175659607e0Smrg	NULL,
176659607e0Smrg	0
177659607e0Smrg};
178659607e0Smrg
179659607e0Smrgtypedef enum {
180659607e0Smrg    OPTION_ALWAYS_CORE,
181659607e0Smrg    OPTION_SEND_CORE_EVENTS,
182659607e0Smrg    OPTION_CORE_POINTER,
183659607e0Smrg    OPTION_SEND_DRAG_EVENTS,
184659607e0Smrg    OPTION_HISTORY_SIZE,
185659607e0Smrg    OPTION_DEVICE,
186659607e0Smrg    OPTION_PROTOCOL,
187659607e0Smrg    OPTION_BUTTONS,
188659607e0Smrg    OPTION_EMULATE_3_BUTTONS,
189659607e0Smrg    OPTION_EMULATE_3_TIMEOUT,
190659607e0Smrg    OPTION_CHORD_MIDDLE,
191659607e0Smrg    OPTION_FLIP_XY,
192659607e0Smrg    OPTION_INV_X,
193659607e0Smrg    OPTION_INV_Y,
194659607e0Smrg    OPTION_ANGLE_OFFSET,
195659607e0Smrg    OPTION_Z_AXIS_MAPPING,
196659607e0Smrg    OPTION_SAMPLE_RATE,
197659607e0Smrg    OPTION_RESOLUTION,
198659607e0Smrg    OPTION_EMULATE_WHEEL,
199659607e0Smrg    OPTION_EMU_WHEEL_BUTTON,
200659607e0Smrg    OPTION_EMU_WHEEL_INERTIA,
201659607e0Smrg    OPTION_EMU_WHEEL_TIMEOUT,
202659607e0Smrg    OPTION_X_AXIS_MAPPING,
203659607e0Smrg    OPTION_Y_AXIS_MAPPING,
204659607e0Smrg    OPTION_AUTO_SOFT,
205659607e0Smrg    OPTION_CLEAR_DTR,
206659607e0Smrg    OPTION_CLEAR_RTS,
207659607e0Smrg    OPTION_BAUD_RATE,
208659607e0Smrg    OPTION_DATA_BITS,
209659607e0Smrg    OPTION_STOP_BITS,
210659607e0Smrg    OPTION_PARITY,
211659607e0Smrg    OPTION_FLOW_CONTROL,
212659607e0Smrg    OPTION_VTIME,
213659607e0Smrg    OPTION_VMIN,
214659607e0Smrg    OPTION_DRAGLOCKBUTTONS,
215659607e0Smrg    OPTION_DOUBLECLICK_BUTTONS,
216659607e0Smrg    OPTION_BUTTON_MAPPING,
217659607e0Smrg    OPTION_SENSITIVITY
218659607e0Smrg} MouseOpts;
219659607e0Smrg
220659607e0Smrg#ifdef XFree86LOADER
221659607e0Smrgstatic const OptionInfoRec mouseOptions[] = {
222659607e0Smrg    { OPTION_ALWAYS_CORE,	"AlwaysCore",	  OPTV_BOOLEAN,	{0}, FALSE },
223659607e0Smrg    { OPTION_SEND_CORE_EVENTS,	"SendCoreEvents", OPTV_BOOLEAN,	{0}, FALSE },
224659607e0Smrg    { OPTION_CORE_POINTER,	"CorePointer",	  OPTV_BOOLEAN,	{0}, FALSE },
225659607e0Smrg    { OPTION_SEND_DRAG_EVENTS,	"SendDragEvents", OPTV_BOOLEAN,	{0}, FALSE },
226659607e0Smrg    { OPTION_HISTORY_SIZE,	"HistorySize",	  OPTV_INTEGER,	{0}, FALSE },
227659607e0Smrg    { OPTION_DEVICE,		"Device",	  OPTV_STRING,	{0}, FALSE },
228659607e0Smrg    { OPTION_PROTOCOL,		"Protocol",	  OPTV_STRING,	{0}, FALSE },
229659607e0Smrg    { OPTION_BUTTONS,		"Buttons",	  OPTV_INTEGER,	{0}, FALSE },
230659607e0Smrg    { OPTION_EMULATE_3_BUTTONS,	"Emulate3Buttons",OPTV_BOOLEAN,	{0}, FALSE },
231659607e0Smrg    { OPTION_EMULATE_3_TIMEOUT,	"Emulate3Timeout",OPTV_INTEGER,	{0}, FALSE },
232659607e0Smrg    { OPTION_CHORD_MIDDLE,	"ChordMiddle",	  OPTV_BOOLEAN,	{0}, FALSE },
233659607e0Smrg    { OPTION_FLIP_XY,		"FlipXY",	  OPTV_BOOLEAN,	{0}, FALSE },
234659607e0Smrg    { OPTION_INV_X,		"InvX",		  OPTV_BOOLEAN,	{0}, FALSE },
235659607e0Smrg    { OPTION_INV_Y,		"InvY",		  OPTV_BOOLEAN,	{0}, FALSE },
236659607e0Smrg    { OPTION_ANGLE_OFFSET,	"AngleOffset",	  OPTV_INTEGER,	{0}, FALSE },
237659607e0Smrg    { OPTION_Z_AXIS_MAPPING,	"ZAxisMapping",	  OPTV_STRING,	{0}, FALSE },
238659607e0Smrg    { OPTION_SAMPLE_RATE,	"SampleRate",	  OPTV_INTEGER,	{0}, FALSE },
239659607e0Smrg    { OPTION_RESOLUTION,	"Resolution",	  OPTV_INTEGER,	{0}, FALSE },
240659607e0Smrg    { OPTION_EMULATE_WHEEL,	"EmulateWheel",	  OPTV_BOOLEAN, {0}, FALSE },
241659607e0Smrg    { OPTION_EMU_WHEEL_BUTTON,	"EmulateWheelButton", OPTV_INTEGER, {0}, FALSE },
242659607e0Smrg    { OPTION_EMU_WHEEL_INERTIA,	"EmulateWheelInertia", OPTV_INTEGER, {0}, FALSE },
243659607e0Smrg    { OPTION_EMU_WHEEL_TIMEOUT,	"EmulateWheelTimeout", OPTV_INTEGER, {0}, FALSE },
244659607e0Smrg    { OPTION_X_AXIS_MAPPING,	"XAxisMapping",	  OPTV_STRING,	{0}, FALSE },
245659607e0Smrg    { OPTION_Y_AXIS_MAPPING,	"YAxisMapping",	  OPTV_STRING,	{0}, FALSE },
246659607e0Smrg    { OPTION_AUTO_SOFT,		"AutoSoft",	  OPTV_BOOLEAN, {0}, FALSE },
247659607e0Smrg    /* serial options */
248659607e0Smrg    { OPTION_CLEAR_DTR,		"ClearDTR",	  OPTV_BOOLEAN,	{0}, FALSE },
249659607e0Smrg    { OPTION_CLEAR_RTS,		"ClearRTS",	  OPTV_BOOLEAN,	{0}, FALSE },
250659607e0Smrg    { OPTION_BAUD_RATE,		"BaudRate",	  OPTV_INTEGER,	{0}, FALSE },
251659607e0Smrg    { OPTION_DATA_BITS,		"DataBits",	  OPTV_INTEGER,	{0}, FALSE },
252659607e0Smrg    { OPTION_STOP_BITS,		"StopBits",	  OPTV_INTEGER,	{0}, FALSE },
253659607e0Smrg    { OPTION_PARITY,		"Parity",	  OPTV_STRING,	{0}, FALSE },
254659607e0Smrg    { OPTION_FLOW_CONTROL,	"FlowControl",	  OPTV_STRING,	{0}, FALSE },
255659607e0Smrg    { OPTION_VTIME,		"VTime",	  OPTV_INTEGER,	{0}, FALSE },
256659607e0Smrg    { OPTION_VMIN,		"VMin",		  OPTV_INTEGER,	{0}, FALSE },
257659607e0Smrg    /* end serial options */
258659607e0Smrg    { OPTION_DRAGLOCKBUTTONS,	"DragLockButtons",OPTV_STRING,	{0}, FALSE },
259659607e0Smrg    { OPTION_DOUBLECLICK_BUTTONS,"DoubleClickButtons", OPTV_STRING, {0}, FALSE },
260659607e0Smrg    { OPTION_BUTTON_MAPPING,   "ButtonMapping",   OPTV_STRING,  {0}, FALSE },
261659607e0Smrg    { OPTION_SENSITIVITY,      "Sensitivity",     OPTV_REAL,    {0}, FALSE },
262659607e0Smrg    { -1,			NULL,		  OPTV_NONE,	{0}, FALSE }
263659607e0Smrg};
264659607e0Smrg#endif
265659607e0Smrg
266659607e0Smrg#define RETRY_COUNT 4
267659607e0Smrg
268659607e0Smrg/*
269659607e0Smrg * Microsoft (all serial models), Logitech MouseMan, First Mouse, etc,
270659607e0Smrg * ALPS GlidePoint, Thinking Mouse.
271659607e0Smrg */
272659607e0Smrgstatic const char *msDefaults[] = {
273659607e0Smrg	"BaudRate",	"1200",
274659607e0Smrg	"DataBits",	"7",
275659607e0Smrg	"StopBits",	"1",
276659607e0Smrg	"Parity",	"None",
277659607e0Smrg	"FlowControl",	"None",
278659607e0Smrg	"VTime",	"0",
279659607e0Smrg	"VMin",		"1",
280659607e0Smrg	NULL
281659607e0Smrg};
282659607e0Smrg/* MouseSystems */
283659607e0Smrgstatic const char *mlDefaults[] = {
284659607e0Smrg	"BaudRate",	"1200",
285659607e0Smrg	"DataBits",	"8",
286659607e0Smrg	"StopBits",	"2",
287659607e0Smrg	"Parity",	"None",
288659607e0Smrg	"FlowControl",	"None",
289659607e0Smrg	"VTime",	"0",
290659607e0Smrg	"VMin",		"1",
291659607e0Smrg	NULL
292659607e0Smrg};
293659607e0Smrg/* MMSeries */
294659607e0Smrgstatic const char *mmDefaults[] = {
295659607e0Smrg	"BaudRate",	"1200",
296659607e0Smrg	"DataBits",	"8",
297659607e0Smrg	"StopBits",	"1",
298659607e0Smrg	"Parity",	"Odd",
299659607e0Smrg	"FlowControl",	"None",
300659607e0Smrg	"VTime",	"0",
301659607e0Smrg	"VMin",		"1",
302659607e0Smrg	NULL
303659607e0Smrg};
304659607e0Smrg#if 0
305659607e0Smrg/* Logitech series 9 *//* same as msc: now mlDefaults */
306659607e0Smrgstatic const char *logiDefaults[] = {
307659607e0Smrg	"BaudRate",	"1200",
308659607e0Smrg	"DataBits",	"8",
309659607e0Smrg	"StopBits",	"2",
310659607e0Smrg	"Parity",	"None",
311659607e0Smrg	"FlowControl",	"None",
312659607e0Smrg	"VTime",	"0",
313659607e0Smrg	"VMin",		"1",
314659607e0Smrg	NULL
315659607e0Smrg};
316659607e0Smrg#endif
317659607e0Smrg/* Hitachi Tablet */
318659607e0Smrgstatic const char *mmhitDefaults[] = {
319659607e0Smrg	"BaudRate",	"1200",
320659607e0Smrg	"DataBits",	"8",
321659607e0Smrg	"StopBits",	"1",
322659607e0Smrg	"Parity",	"None",
323659607e0Smrg	"FlowControl",	"None",
324659607e0Smrg	"VTime",	"0",
325659607e0Smrg	"VMin",		"1",
326659607e0Smrg	NULL
327659607e0Smrg};
328659607e0Smrg/* AceCad Tablet */
329659607e0Smrgstatic const char *acecadDefaults[] = {
330659607e0Smrg	"BaudRate",	"9600",
331659607e0Smrg	"DataBits",	"8",
332659607e0Smrg	"StopBits",	"1",
333659607e0Smrg	"Parity",	"Odd",
334659607e0Smrg	"FlowControl",	"None",
335659607e0Smrg	"VTime",	"0",
336659607e0Smrg	"VMin",		"1",
337659607e0Smrg	NULL
338659607e0Smrg};
339659607e0Smrg
340659607e0Smrgstatic MouseProtocolRec mouseProtocols[] = {
341659607e0Smrg
342659607e0Smrg    /* Serial protocols */
343659607e0Smrg    { "Microsoft",		MSE_SERIAL,	msDefaults,	PROT_MS },
344659607e0Smrg    { "MouseSystems",		MSE_SERIAL,	mlDefaults,	PROT_MSC },
345659607e0Smrg    { "MMSeries",		MSE_SERIAL,	mmDefaults,	PROT_MM },
346659607e0Smrg    { "Logitech",		MSE_SERIAL,	mlDefaults,	PROT_LOGI },
347659607e0Smrg    { "MouseMan",		MSE_SERIAL,	msDefaults,	PROT_LOGIMAN },
348659607e0Smrg    { "MMHitTab",		MSE_SERIAL,	mmhitDefaults,	PROT_MMHIT },
349659607e0Smrg    { "GlidePoint",		MSE_SERIAL,	msDefaults,	PROT_GLIDE },
350659607e0Smrg    { "IntelliMouse",		MSE_SERIAL,	msDefaults,	PROT_IMSERIAL },
351659607e0Smrg    { "ThinkingMouse",		MSE_SERIAL,	msDefaults,	PROT_THINKING },
352659607e0Smrg    { "AceCad",			MSE_SERIAL,	acecadDefaults,	PROT_ACECAD },
353659607e0Smrg    { "ValuMouseScroll",	MSE_SERIAL,	msDefaults,	PROT_VALUMOUSESCROLL },
354659607e0Smrg
355659607e0Smrg    /* Standard PS/2 */
356659607e0Smrg    { "PS/2",			MSE_PS2,	NULL,		PROT_PS2 },
357659607e0Smrg    { "GenericPS/2",		MSE_PS2,	NULL,		PROT_GENPS2 },
358659607e0Smrg
359659607e0Smrg    /* Extended PS/2 */
360659607e0Smrg    { "ImPS/2",			MSE_XPS2,	NULL,		PROT_IMPS2 },
361659607e0Smrg    { "ExplorerPS/2",		MSE_XPS2,	NULL,		PROT_EXPPS2 },
362659607e0Smrg    { "ThinkingMousePS/2",	MSE_XPS2,	NULL,		PROT_THINKPS2 },
363659607e0Smrg    { "MouseManPlusPS/2",	MSE_XPS2,	NULL,		PROT_MMPS2 },
364659607e0Smrg    { "GlidePointPS/2",		MSE_XPS2,	NULL,		PROT_GLIDEPS2 },
365659607e0Smrg    { "NetMousePS/2",		MSE_XPS2,	NULL,		PROT_NETPS2 },
366659607e0Smrg    { "NetScrollPS/2",		MSE_XPS2,	NULL,		PROT_NETSCPS2 },
367659607e0Smrg
368659607e0Smrg    /* Bus Mouse */
369659607e0Smrg    { "BusMouse",		MSE_BUS,	NULL,		PROT_BM },
370659607e0Smrg
371659607e0Smrg    /* Auto-detect (PnP) */
372659607e0Smrg    { "Auto",			MSE_AUTO,	NULL,		PROT_AUTO },
373659607e0Smrg
374659607e0Smrg    /* Misc (usually OS-specific) */
375659607e0Smrg    { "SysMouse",		MSE_MISC,	mlDefaults,	PROT_SYSMOUSE },
376659607e0Smrg
377659607e0Smrg    /* end of list */
378659607e0Smrg    { NULL,			MSE_NONE,	NULL,		PROT_UNKNOWN }
379659607e0Smrg};
380659607e0Smrg
381659607e0Smrg#ifdef XFree86LOADER
382659607e0Smrg/*ARGSUSED*/
383659607e0Smrgstatic const OptionInfoRec *
384659607e0SmrgMouseAvailableOptions(void *unused)
385659607e0Smrg{
386659607e0Smrg    return (mouseOptions);
387659607e0Smrg}
388659607e0Smrg#endif
389659607e0Smrg
390659607e0Smrg/* Process options common to all mouse types. */
391659607e0Smrgstatic void
392659607e0SmrgMouseCommonOptions(InputInfoPtr pInfo)
393659607e0Smrg{
394659607e0Smrg    MouseDevPtr pMse;
395659607e0Smrg    MessageType buttons_from = X_CONFIG;
396659607e0Smrg    char *s;
397659607e0Smrg    int origButtons;
398659607e0Smrg    int i;
399659607e0Smrg
400659607e0Smrg    pMse = pInfo->private;
401659607e0Smrg
402659607e0Smrg    pMse->buttons = xf86SetIntOption(pInfo->options, "Buttons", 0);
403659607e0Smrg    if (!pMse->buttons) {
404659607e0Smrg	pMse->buttons = MSE_DFLTBUTTONS;
405659607e0Smrg	buttons_from = X_DEFAULT;
406659607e0Smrg    }
407659607e0Smrg    origButtons = pMse->buttons;
408659607e0Smrg
409659607e0Smrg    pMse->emulate3Buttons = xf86SetBoolOption(pInfo->options,
410659607e0Smrg					      "Emulate3Buttons", FALSE);
411659607e0Smrg    if (!xf86FindOptionValue(pInfo->options,"Emulate3Buttons")) {
412659607e0Smrg	pMse->emulate3ButtonsSoft = TRUE;
413659607e0Smrg	pMse->emulate3Buttons = TRUE;
414659607e0Smrg    }
415659607e0Smrg
416659607e0Smrg    pMse->emulate3Timeout = xf86SetIntOption(pInfo->options,
417659607e0Smrg					     "Emulate3Timeout", 50);
418659607e0Smrg    if (pMse->emulate3Buttons || pMse->emulate3ButtonsSoft) {
419659607e0Smrg	MessageType from = X_CONFIG;
420659607e0Smrg	if (pMse->emulate3ButtonsSoft)
421659607e0Smrg	    from = X_DEFAULT;
422659607e0Smrg	xf86Msg(from, "%s: Emulate3Buttons, Emulate3Timeout: %d\n",
423659607e0Smrg		pInfo->name, pMse->emulate3Timeout);
424659607e0Smrg    }
425659607e0Smrg
426659607e0Smrg    pMse->chordMiddle = xf86SetBoolOption(pInfo->options, "ChordMiddle", FALSE);
427659607e0Smrg    if (pMse->chordMiddle)
428659607e0Smrg	xf86Msg(X_CONFIG, "%s: ChordMiddle\n", pInfo->name);
429659607e0Smrg    pMse->flipXY = xf86SetBoolOption(pInfo->options, "FlipXY", FALSE);
430659607e0Smrg    if (pMse->flipXY)
431659607e0Smrg	xf86Msg(X_CONFIG, "%s: FlipXY\n", pInfo->name);
432659607e0Smrg    if (xf86SetBoolOption(pInfo->options, "InvX", FALSE)) {
433659607e0Smrg	pMse->invX = -1;
434659607e0Smrg	xf86Msg(X_CONFIG, "%s: InvX\n", pInfo->name);
435659607e0Smrg    } else
436659607e0Smrg	pMse->invX = 1;
437659607e0Smrg    if (xf86SetBoolOption(pInfo->options, "InvY", FALSE)) {
438659607e0Smrg	pMse->invY = -1;
439659607e0Smrg	xf86Msg(X_CONFIG, "%s: InvY\n", pInfo->name);
440659607e0Smrg    } else
441659607e0Smrg	pMse->invY = 1;
442659607e0Smrg    pMse->angleOffset = xf86SetIntOption(pInfo->options, "AngleOffset", 0);
443659607e0Smrg
444659607e0Smrg
445659607e0Smrg    if (pMse->pDragLock)
446659607e0Smrg	xfree(pMse->pDragLock);
447659607e0Smrg    pMse->pDragLock = NULL;
448659607e0Smrg
449659607e0Smrg    s = xf86SetStrOption(pInfo->options, "DragLockButtons", NULL);
450659607e0Smrg
451659607e0Smrg    if (s) {
452659607e0Smrg	int lock;             /* lock button */
453659607e0Smrg	int target;           /* target button */
454659607e0Smrg	int lockM,targetM;    /* bitmasks for drag lock, target */
455659607e0Smrg	int i, j;             /* indexes */
456659607e0Smrg	char *s1;             /* parse input string */
457659607e0Smrg	DragLockPtr pLock;
458659607e0Smrg
459659607e0Smrg	pLock = pMse->pDragLock = xcalloc(1, sizeof(DragLockRec));
460659607e0Smrg	/* init code */
461659607e0Smrg
462659607e0Smrg	/* initial string to be taken apart */
463659607e0Smrg	s1 = s;
464659607e0Smrg
465659607e0Smrg	/* keep getting numbers which are buttons */
466659607e0Smrg	while ((s1 != NULL) && (lock = strtol(s1, &s1, 10)) != 0) {
467659607e0Smrg
468659607e0Smrg	    /* check sanity for a button */
469659607e0Smrg	    if ((lock < 0) || (lock > MSE_MAXBUTTONS)) {
470659607e0Smrg		xf86Msg(X_WARNING, "DragLock: Invalid button number = %d\n",
471659607e0Smrg			lock);
472659607e0Smrg		break;
473659607e0Smrg	    };
474659607e0Smrg	    /* turn into a button mask */
475659607e0Smrg	    lockM = 1 << (lock - 1);
476659607e0Smrg
477659607e0Smrg	    /* try to get drag lock button */
478659607e0Smrg	    if ((s1 == NULL) || ((target=strtol(s1, &s1, 10)) == 0)) {
479659607e0Smrg		/*if no target, must be a master drag lock button */
480659607e0Smrg		/* save master drag lock mask */
481659607e0Smrg		pLock->masterLockM = lockM;
482659607e0Smrg		xf86Msg(X_CONFIG,
483659607e0Smrg			"DragLock button %d is master drag lock",
484659607e0Smrg			lock);
485659607e0Smrg	    } else {
486659607e0Smrg		/* have target button number*/
487659607e0Smrg		/* check target button number for sanity */
488659607e0Smrg		if ((target < 0) || (target > MSE_MAXBUTTONS)) {
489659607e0Smrg		    xf86Msg(X_WARNING,
490659607e0Smrg			    "DragLock: Invalid button number for target=%d\n",
491659607e0Smrg			    target);
492659607e0Smrg		    break;
493659607e0Smrg		}
494659607e0Smrg
495659607e0Smrg		/* target button mask */
496659607e0Smrg		targetM = 1 << (target - 1);
497659607e0Smrg
498659607e0Smrg		xf86Msg(X_CONFIG,
499659607e0Smrg			"DragLock: button %d is drag lock for button %d\n",
500659607e0Smrg			lock,target);
501659607e0Smrg		lock--;
502659607e0Smrg
503659607e0Smrg		/* initialize table that maps drag lock mask to target mask */
504659607e0Smrg		pLock->nib_table[lock / NIB_BITS][1 << (lock % NIB_BITS)] =
505659607e0Smrg			targetM;
506659607e0Smrg
507659607e0Smrg		/* add new drag lock to mask of drag locks */
508659607e0Smrg		pLock->lockButtonsM |= lockM;
509659607e0Smrg	    }
510659607e0Smrg
511659607e0Smrg	}
512659607e0Smrg
513659607e0Smrg	/*
514659607e0Smrg	 * fill out rest of map that maps sets of drag lock buttons
515659607e0Smrg	 * to sets of target buttons, in the form of masks
516659607e0Smrg	 */
517659607e0Smrg
518659607e0Smrg	/* for each nibble */
519659607e0Smrg	for (i = 0; i < NIB_COUNT; i++) {
520659607e0Smrg	    /* for each possible set of bits for that nibble */
521659607e0Smrg	    for (j = 0; j < NIB_SIZE; j++) {
522659607e0Smrg		int ff, fM, otherbits;
523659607e0Smrg
524659607e0Smrg		/* get first bit set in j*/
525659607e0Smrg		ff = ffs(j) - 1;
526659607e0Smrg		/* if 0 bits set nothing to do */
527659607e0Smrg		if (ff >= 0) {
528659607e0Smrg		    /* form mask for fist bit set */
529659607e0Smrg		    fM = 1 << ff;
530659607e0Smrg		    /* mask off first bit set to get remaining bits set*/
531659607e0Smrg		    otherbits = j & ~fM;
532659607e0Smrg		    /*
533659607e0Smrg		     * if otherbits =0 then only 1 bit set
534659607e0Smrg		     * so j=fM
535659607e0Smrg		     * nib_table[i][fM] already calculated if fM has
536659607e0Smrg		     * only 1 bit set.
537659607e0Smrg		     * nib_table[i][j] has already been filled in
538659607e0Smrg		     * by previous loop. otherwise
539659607e0Smrg		     * otherbits < j so nibtable[i][otherbits]
540659607e0Smrg		     * has already been calculated.
541659607e0Smrg		     */
542659607e0Smrg		    if (otherbits)
543659607e0Smrg			pLock->nib_table[i][j] =
544659607e0Smrg				     pLock->nib_table[i][fM] |
545659607e0Smrg				     pLock->nib_table[i][otherbits];
546659607e0Smrg
547659607e0Smrg		}
548659607e0Smrg	    }
549659607e0Smrg	}
550659607e0Smrg	xfree(s);
551659607e0Smrg    }
552659607e0Smrg
553659607e0Smrg    s = xf86SetStrOption(pInfo->options, "ZAxisMapping", "4 5");
554659607e0Smrg    if (s) {
555659607e0Smrg	int b1 = 0, b2 = 0, b3 = 0, b4 = 0;
556659607e0Smrg	char *msg = NULL;
557659607e0Smrg
558659607e0Smrg	pMse->negativeZ = pMse->positiveZ = MSE_NOAXISMAP;
559659607e0Smrg	pMse->negativeW = pMse->positiveW = MSE_NOAXISMAP;
560659607e0Smrg	if (!xf86NameCmp(s, "x")) {
561659607e0Smrg	    pMse->negativeZ = pMse->positiveZ = MSE_MAPTOX;
562659607e0Smrg	    msg = xstrdup("X axis");
563659607e0Smrg	} else if (!xf86NameCmp(s, "y")) {
564659607e0Smrg	    pMse->negativeZ = pMse->positiveZ = MSE_MAPTOY;
565659607e0Smrg	    msg = xstrdup("Y axis");
566659607e0Smrg	} else if (sscanf(s, "%d %d %d %d", &b1, &b2, &b3, &b4) >= 2 &&
567659607e0Smrg		 b1 > 0 && b1 <= MSE_MAXBUTTONS &&
568659607e0Smrg		 b2 > 0 && b2 <= MSE_MAXBUTTONS) {
569659607e0Smrg	    msg = xstrdup("buttons XX and YY");
570659607e0Smrg	    if (msg)
571659607e0Smrg		sprintf(msg, "buttons %d and %d", b1, b2);
572659607e0Smrg	    pMse->negativeZ = 1 << (b1-1);
573659607e0Smrg	    pMse->positiveZ = 1 << (b2-1);
574659607e0Smrg	    if (b3 > 0 && b3 <= MSE_MAXBUTTONS &&
575659607e0Smrg		b4 > 0 && b4 <= MSE_MAXBUTTONS) {
576659607e0Smrg		if (msg)
577659607e0Smrg		    xfree(msg);
578659607e0Smrg		msg = xstrdup("buttons XX, YY, ZZ and WW");
579659607e0Smrg		if (msg)
580659607e0Smrg		    sprintf(msg, "buttons %d, %d, %d and %d", b1, b2, b3, b4);
581659607e0Smrg		pMse->negativeW = 1 << (b3-1);
582659607e0Smrg		pMse->positiveW = 1 << (b4-1);
583659607e0Smrg	    }
584659607e0Smrg	    if (b1 > pMse->buttons) pMse->buttons = b1;
585659607e0Smrg	    if (b2 > pMse->buttons) pMse->buttons = b2;
586659607e0Smrg	    if (b3 > pMse->buttons) pMse->buttons = b3;
587659607e0Smrg	    if (b4 > pMse->buttons) pMse->buttons = b4;
588659607e0Smrg	}
589659607e0Smrg	if (msg) {
590659607e0Smrg	    xf86Msg(X_CONFIG, "%s: ZAxisMapping: %s\n", pInfo->name, msg);
591659607e0Smrg	    xfree(msg);
592659607e0Smrg	} else {
593659607e0Smrg	    xf86Msg(X_WARNING, "%s: Invalid ZAxisMapping value: \"%s\"\n",
594659607e0Smrg		    pInfo->name, s);
595659607e0Smrg	}
596659607e0Smrg	xfree(s);
597659607e0Smrg    }
598659607e0Smrg    if (xf86SetBoolOption(pInfo->options, "EmulateWheel", FALSE)) {
599659607e0Smrg	Bool yFromConfig = FALSE;
600659607e0Smrg	int wheelButton;
601659607e0Smrg
602659607e0Smrg	pMse->emulateWheel = TRUE;
603659607e0Smrg	wheelButton = xf86SetIntOption(pInfo->options,
604659607e0Smrg					"EmulateWheelButton", 4);
605659607e0Smrg	if (wheelButton < 0 || wheelButton > MSE_MAXBUTTONS) {
606659607e0Smrg	    xf86Msg(X_WARNING, "%s: Invalid EmulateWheelButton value: %d\n",
607659607e0Smrg			pInfo->name, wheelButton);
608659607e0Smrg	    wheelButton = 4;
609659607e0Smrg	}
610659607e0Smrg	pMse->wheelButton = wheelButton;
611659607e0Smrg
612659607e0Smrg	pMse->wheelInertia = xf86SetIntOption(pInfo->options,
613659607e0Smrg					"EmulateWheelInertia", 10);
614659607e0Smrg	if (pMse->wheelInertia <= 0) {
615659607e0Smrg	    xf86Msg(X_WARNING, "%s: Invalid EmulateWheelInertia value: %d\n",
616659607e0Smrg			pInfo->name, pMse->wheelInertia);
617659607e0Smrg	    pMse->wheelInertia = 10;
618659607e0Smrg	}
619659607e0Smrg	pMse->wheelButtonTimeout = xf86SetIntOption(pInfo->options,
620659607e0Smrg					"EmulateWheelTimeout", 200);
621659607e0Smrg	if (pMse->wheelButtonTimeout <= 0) {
622659607e0Smrg	    xf86Msg(X_WARNING, "%s: Invalid EmulateWheelTimeout value: %d\n",
623659607e0Smrg			pInfo->name, pMse->wheelButtonTimeout);
624659607e0Smrg	    pMse->wheelButtonTimeout = 200;
625659607e0Smrg	}
626659607e0Smrg
627659607e0Smrg	pMse->negativeX = MSE_NOAXISMAP;
628659607e0Smrg	pMse->positiveX = MSE_NOAXISMAP;
629659607e0Smrg	s = xf86SetStrOption(pInfo->options, "XAxisMapping", NULL);
630659607e0Smrg	if (s) {
631659607e0Smrg	    int b1 = 0, b2 = 0;
632659607e0Smrg	    char *msg = NULL;
633659607e0Smrg
634659607e0Smrg	    if ((sscanf(s, "%d %d", &b1, &b2) == 2) &&
635659607e0Smrg		 b1 > 0 && b1 <= MSE_MAXBUTTONS &&
636659607e0Smrg		 b2 > 0 && b2 <= MSE_MAXBUTTONS) {
637659607e0Smrg		msg = xstrdup("buttons XX and YY");
638659607e0Smrg		if (msg)
639659607e0Smrg		    sprintf(msg, "buttons %d and %d", b1, b2);
640659607e0Smrg		pMse->negativeX = b1;
641659607e0Smrg		pMse->positiveX = b2;
642659607e0Smrg		if (b1 > pMse->buttons) pMse->buttons = b1;
643659607e0Smrg		if (b2 > pMse->buttons) pMse->buttons = b2;
644659607e0Smrg	    } else {
645659607e0Smrg		xf86Msg(X_WARNING, "%s: Invalid XAxisMapping value: \"%s\"\n",
646659607e0Smrg			pInfo->name, s);
647659607e0Smrg	    }
648659607e0Smrg	    if (msg) {
649659607e0Smrg		xf86Msg(X_CONFIG, "%s: XAxisMapping: %s\n", pInfo->name, msg);
650659607e0Smrg		xfree(msg);
651659607e0Smrg	    }
652659607e0Smrg	    xfree(s);
653659607e0Smrg	}
654659607e0Smrg	s = xf86SetStrOption(pInfo->options, "YAxisMapping", NULL);
655659607e0Smrg	if (s) {
656659607e0Smrg	    int b1 = 0, b2 = 0;
657659607e0Smrg	    char *msg = NULL;
658659607e0Smrg
659659607e0Smrg	    if ((sscanf(s, "%d %d", &b1, &b2) == 2) &&
660659607e0Smrg		 b1 > 0 && b1 <= MSE_MAXBUTTONS &&
661659607e0Smrg		 b2 > 0 && b2 <= MSE_MAXBUTTONS) {
662659607e0Smrg		msg = xstrdup("buttons XX and YY");
663659607e0Smrg		if (msg)
664659607e0Smrg		    sprintf(msg, "buttons %d and %d", b1, b2);
665659607e0Smrg		pMse->negativeY = b1;
666659607e0Smrg		pMse->positiveY = b2;
667659607e0Smrg		if (b1 > pMse->buttons) pMse->buttons = b1;
668659607e0Smrg		if (b2 > pMse->buttons) pMse->buttons = b2;
669659607e0Smrg		yFromConfig = TRUE;
670659607e0Smrg	    } else {
671659607e0Smrg		xf86Msg(X_WARNING, "%s: Invalid YAxisMapping value: \"%s\"\n",
672659607e0Smrg			pInfo->name, s);
673659607e0Smrg	    }
674659607e0Smrg	    if (msg) {
675659607e0Smrg		xf86Msg(X_CONFIG, "%s: YAxisMapping: %s\n", pInfo->name, msg);
676659607e0Smrg		xfree(msg);
677659607e0Smrg	    }
678659607e0Smrg	    xfree(s);
679659607e0Smrg	}
680659607e0Smrg	if (!yFromConfig) {
681659607e0Smrg	    pMse->negativeY = 4;
682659607e0Smrg	    pMse->positiveY = 5;
683659607e0Smrg	    if (pMse->negativeY > pMse->buttons)
684659607e0Smrg		pMse->buttons = pMse->negativeY;
685659607e0Smrg	    if (pMse->positiveY > pMse->buttons)
686659607e0Smrg		pMse->buttons = pMse->positiveY;
687659607e0Smrg	    xf86Msg(X_DEFAULT, "%s: YAxisMapping: buttons %d and %d\n",
688659607e0Smrg		    pInfo->name, pMse->negativeY, pMse->positiveY);
689659607e0Smrg	}
690659607e0Smrg	xf86Msg(X_CONFIG, "%s: EmulateWheel, EmulateWheelButton: %d, "
691659607e0Smrg			  "EmulateWheelInertia: %d, "
692659607e0Smrg			  "EmulateWheelTimeout: %d\n",
693659607e0Smrg		pInfo->name, wheelButton, pMse->wheelInertia,
694659607e0Smrg		pMse->wheelButtonTimeout);
695659607e0Smrg    }
696659607e0Smrg    s = xf86SetStrOption(pInfo->options, "ButtonMapping", NULL);
697659607e0Smrg    if (s) {
698659607e0Smrg       int b, n = 0;
699659607e0Smrg       char *s1 = s;
700659607e0Smrg       /* keep getting numbers which are buttons */
701659607e0Smrg       while (s1 && n < MSE_MAXBUTTONS && (b = strtol(s1, &s1, 10)) != 0) {
702659607e0Smrg	   /* check sanity for a button */
703659607e0Smrg	   if (b < 0 || b > MSE_MAXBUTTONS) {
704659607e0Smrg	       xf86Msg(X_WARNING,
705659607e0Smrg		       "ButtonMapping: Invalid button number = %d\n", b);
706659607e0Smrg	       break;
707659607e0Smrg	   };
708659607e0Smrg	   pMse->buttonMap[n++] = 1 << (b-1);
709659607e0Smrg	   if (b > pMse->buttons) pMse->buttons = b;
710659607e0Smrg       }
711659607e0Smrg       xfree(s);
712659607e0Smrg    }
713659607e0Smrg    /* get maximum of mapped buttons */
714659607e0Smrg    for (i = pMse->buttons-1; i >= 0; i--) {
715659607e0Smrg	int f = ffs (pMse->buttonMap[i]);
716659607e0Smrg	if (f > pMse->buttons)
717659607e0Smrg	    pMse->buttons = f;
718659607e0Smrg    }
719659607e0Smrg    if (origButtons != pMse->buttons)
720659607e0Smrg	buttons_from = X_CONFIG;
721659607e0Smrg    xf86Msg(buttons_from, "%s: Buttons: %d\n", pInfo->name, pMse->buttons);
722659607e0Smrg
723659607e0Smrg    pMse->doubleClickSourceButtonMask = 0;
724659607e0Smrg    pMse->doubleClickTargetButtonMask = 0;
725659607e0Smrg    pMse->doubleClickTargetButton = 0;
726659607e0Smrg    s = xf86SetStrOption(pInfo->options, "DoubleClickButtons", NULL);
727659607e0Smrg    if (s) {
728659607e0Smrg        int b1 = 0, b2 = 0;
729659607e0Smrg        char *msg = NULL;
730659607e0Smrg
731659607e0Smrg        if ((sscanf(s, "%d %d", &b1, &b2) == 2) &&
732659607e0Smrg        (b1 > 0) && (b1 <= MSE_MAXBUTTONS) && (b2 > 0) && (b2 <= MSE_MAXBUTTONS)) {
733659607e0Smrg            msg = xstrdup("buttons XX and YY");
734659607e0Smrg            if (msg)
735659607e0Smrg                sprintf(msg, "buttons %d and %d", b1, b2);
736659607e0Smrg            pMse->doubleClickTargetButton = b1;
737659607e0Smrg            pMse->doubleClickTargetButtonMask = 1 << (b1 - 1);
738659607e0Smrg            pMse->doubleClickSourceButtonMask = 1 << (b2 - 1);
739659607e0Smrg            if (b1 > pMse->buttons) pMse->buttons = b1;
740659607e0Smrg            if (b2 > pMse->buttons) pMse->buttons = b2;
741659607e0Smrg        } else {
742659607e0Smrg            xf86Msg(X_WARNING, "%s: Invalid DoubleClickButtons value: \"%s\"\n",
743659607e0Smrg                    pInfo->name, s);
744659607e0Smrg        }
745659607e0Smrg        if (msg) {
746659607e0Smrg            xf86Msg(X_CONFIG, "%s: DoubleClickButtons: %s\n", pInfo->name, msg);
747659607e0Smrg            xfree(msg);
748659607e0Smrg        }
749659607e0Smrg	xfree(s);
750659607e0Smrg    }
751659607e0Smrg}
752659607e0Smrg/*
753659607e0Smrg * map bits corresponding to lock buttons.
754659607e0Smrg * for each bit for a lock button,
755659607e0Smrg * turn on bit corresponding to button button that the lock
756659607e0Smrg * button services.
757659607e0Smrg */
758659607e0Smrg
759659607e0Smrgstatic int
760659607e0Smrglock2targetMap(DragLockPtr pLock, int lockMask)
761659607e0Smrg{
762659607e0Smrg    int result,i;
763659607e0Smrg    result = 0;
764659607e0Smrg
765659607e0Smrg    /*
766659607e0Smrg     * for each nibble group of bits, use
767659607e0Smrg     * map for that group to get corresponding
768659607e0Smrg     * bits, turn them on.
769659607e0Smrg     * if 4 or less buttons only first map will
770659607e0Smrg     * need to be used.
771659607e0Smrg     */
772659607e0Smrg    for (i = 0; (i < NIB_COUNT) && lockMask; i++) {
773659607e0Smrg	result |= pLock->nib_table[i][lockMask& NIB_MASK];
774659607e0Smrg
775659607e0Smrg	lockMask &= ~NIB_MASK;
776659607e0Smrg	lockMask >>= NIB_BITS;
777659607e0Smrg    }
778659607e0Smrg    return result;
779659607e0Smrg}
780659607e0Smrg
781659607e0Smrgstatic void
782659607e0SmrgMouseHWOptions(InputInfoPtr pInfo)
783659607e0Smrg{
784659607e0Smrg    MouseDevPtr  pMse = pInfo->private;
785659607e0Smrg    mousePrivPtr mPriv = (mousePrivPtr)pMse->mousePriv;
786659607e0Smrg
787659607e0Smrg    if (mPriv == NULL)
788659607e0Smrg	    return;
789659607e0Smrg
790659607e0Smrg    if ((mPriv->soft
791659607e0Smrg	 = xf86SetBoolOption(pInfo->options, "AutoSoft", FALSE))) {
792659607e0Smrg	xf86Msg(X_CONFIG, "Don't initialize mouse when auto-probing\n");
793659607e0Smrg    }
794659607e0Smrg    pMse->sampleRate = xf86SetIntOption(pInfo->options, "SampleRate", 0);
795659607e0Smrg    if (pMse->sampleRate) {
796659607e0Smrg	xf86Msg(X_CONFIG, "%s: SampleRate: %d\n", pInfo->name,
797659607e0Smrg		pMse->sampleRate);
798659607e0Smrg    }
799659607e0Smrg    pMse->resolution = xf86SetIntOption(pInfo->options, "Resolution", 0);
800659607e0Smrg    if (pMse->resolution) {
801659607e0Smrg	xf86Msg(X_CONFIG, "%s: Resolution: %d\n", pInfo->name,
802659607e0Smrg		pMse->resolution);
803659607e0Smrg    }
804659607e0Smrg
805659607e0Smrg    if (mPriv->sensitivity
806659607e0Smrg	= xf86SetRealOption(pInfo->options, "Sensitivity", 1.0)) {
807659607e0Smrg	xf86Msg(X_CONFIG, "%s: Sensitivity: %g\n", pInfo->name,
808659607e0Smrg		mPriv->sensitivity);
809659607e0Smrg    }
810659607e0Smrg}
811659607e0Smrg
812659607e0Smrgstatic void
813659607e0SmrgMouseSerialOptions(InputInfoPtr pInfo)
814659607e0Smrg{
815659607e0Smrg    MouseDevPtr  pMse = pInfo->private;
816659607e0Smrg    Bool clearDTR, clearRTS;
817659607e0Smrg
818659607e0Smrg
819659607e0Smrg    pMse->baudRate = xf86SetIntOption(pInfo->options, "BaudRate", 0);
820659607e0Smrg    if (pMse->baudRate) {
821659607e0Smrg	xf86Msg(X_CONFIG, "%s: BaudRate: %d\n", pInfo->name,
822659607e0Smrg		pMse->baudRate);
823659607e0Smrg    }
824659607e0Smrg
825659607e0Smrg    if ((clearDTR = xf86SetBoolOption(pInfo->options, "ClearDTR",FALSE)))
826659607e0Smrg	pMse->mouseFlags |= MF_CLEAR_DTR;
827659607e0Smrg
828659607e0Smrg
829659607e0Smrg    if ((clearRTS = xf86SetBoolOption(pInfo->options, "ClearRTS",FALSE)))
830659607e0Smrg	pMse->mouseFlags |= MF_CLEAR_RTS;
831659607e0Smrg
832659607e0Smrg    if (clearDTR || clearRTS) {
833659607e0Smrg	xf86Msg(X_CONFIG, "%s: ", pInfo->name);
834659607e0Smrg	if (clearDTR) {
835659607e0Smrg	    xf86ErrorF("ClearDTR");
836659607e0Smrg	    if (clearRTS)
837659607e0Smrg		xf86ErrorF(", ");
838659607e0Smrg	}
839659607e0Smrg	if (clearRTS) {
840659607e0Smrg	    xf86ErrorF("ClearRTS");
841659607e0Smrg	}
842659607e0Smrg	xf86ErrorF("\n");
843659607e0Smrg    }
844659607e0Smrg}
845659607e0Smrg
846659607e0Smrgstatic MouseProtocolID
847659607e0SmrgProtocolNameToID(const char *name)
848659607e0Smrg{
849659607e0Smrg    int i;
850659607e0Smrg
851659607e0Smrg    for (i = 0; mouseProtocols[i].name; i++)
852659607e0Smrg	if (xf86NameCmp(name, mouseProtocols[i].name) == 0)
853659607e0Smrg	    return mouseProtocols[i].id;
854659607e0Smrg    return PROT_UNKNOWN;
855659607e0Smrg}
856659607e0Smrg
857659607e0Smrgstatic const char *
858659607e0SmrgProtocolIDToName(MouseProtocolID id)
859659607e0Smrg{
860659607e0Smrg    int i;
861659607e0Smrg
862659607e0Smrg    switch (id) {
863659607e0Smrg    case PROT_UNKNOWN:
864659607e0Smrg	return "Unknown";
865659607e0Smrg	break;
866659607e0Smrg    case PROT_UNSUP:
867659607e0Smrg	return "Unsupported";
868659607e0Smrg	break;
869659607e0Smrg    default:
870659607e0Smrg	for (i = 0; mouseProtocols[i].name; i++)
871659607e0Smrg	    if (id == mouseProtocols[i].id)
872659607e0Smrg		return mouseProtocols[i].name;
873659607e0Smrg	return "Invalid";
874659607e0Smrg    }
875659607e0Smrg}
876659607e0Smrg
877659607e0Smrgconst char *
878659607e0Smrgxf86MouseProtocolIDToName(MouseProtocolID id)
879659607e0Smrg{
880659607e0Smrg	return ProtocolIDToName(id);
881659607e0Smrg}
882659607e0Smrg
883659607e0SmrgMouseProtocolID
884659607e0Smrgxf86MouseProtocolNameToID(const char *name)
885659607e0Smrg{
886659607e0Smrg    return ProtocolNameToID(name);
887659607e0Smrg}
888659607e0Smrg
889659607e0Smrgstatic int
890659607e0SmrgProtocolIDToClass(MouseProtocolID id)
891659607e0Smrg{
892659607e0Smrg    int i;
893659607e0Smrg
894659607e0Smrg    switch (id) {
895659607e0Smrg    case PROT_UNKNOWN:
896659607e0Smrg    case PROT_UNSUP:
897659607e0Smrg	return MSE_NONE;
898659607e0Smrg	break;
899659607e0Smrg    default:
900659607e0Smrg	for (i = 0; mouseProtocols[i].name; i++)
901659607e0Smrg	    if (id == mouseProtocols[i].id)
902659607e0Smrg		return mouseProtocols[i].class;
903659607e0Smrg	return MSE_NONE;
904659607e0Smrg    }
905659607e0Smrg}
906659607e0Smrg
907659607e0Smrgstatic MouseProtocolPtr
908659607e0SmrgGetProtocol(MouseProtocolID id) {
909659607e0Smrg    int i;
910659607e0Smrg
911659607e0Smrg    switch (id) {
912659607e0Smrg    case PROT_UNKNOWN:
913659607e0Smrg    case PROT_UNSUP:
914659607e0Smrg	return NULL;
915659607e0Smrg	break;
916659607e0Smrg    default:
917659607e0Smrg	for (i = 0; mouseProtocols[i].name; i++)
918659607e0Smrg	    if (id == mouseProtocols[i].id) {
919659607e0Smrg		return &mouseProtocols[i];
920659607e0Smrg	    }
921659607e0Smrg	return NULL;
922659607e0Smrg    }
923659607e0Smrg}
924659607e0Smrg
925659607e0Smrgstatic OSMouseInfoPtr osInfo = NULL;
926659607e0Smrg
927659607e0Smrgstatic Bool
928659607e0SmrgInitProtocols(void)
929659607e0Smrg{
930659607e0Smrg    int classes;
931659607e0Smrg    int i;
932659607e0Smrg    const char *osname = NULL;
933659607e0Smrg
934659607e0Smrg    if (osInfo)
935659607e0Smrg	return TRUE;
936659607e0Smrg
937659607e0Smrg    osInfo = xf86OSMouseInit(0);
938659607e0Smrg    if (!osInfo)
939659607e0Smrg	return FALSE;
940659607e0Smrg    if (!osInfo->SupportedInterfaces)
941659607e0Smrg	return FALSE;
942659607e0Smrg
943659607e0Smrg    classes = osInfo->SupportedInterfaces();
944659607e0Smrg    if (!classes)
945659607e0Smrg	return FALSE;
946659607e0Smrg
947659607e0Smrg    /* Mark unsupported interface classes. */
948659607e0Smrg    for (i = 0; mouseProtocols[i].name; i++)
949659607e0Smrg	if (!(mouseProtocols[i].class & classes))
950659607e0Smrg	    mouseProtocols[i].id = PROT_UNSUP;
951659607e0Smrg
952659607e0Smrg    for (i = 0; mouseProtocols[i].name; i++)
953659607e0Smrg	if (mouseProtocols[i].class & MSE_MISC)
954659607e0Smrg	    if (!osInfo->CheckProtocol ||
955659607e0Smrg		!osInfo->CheckProtocol(mouseProtocols[i].name))
956659607e0Smrg		mouseProtocols[i].id = PROT_UNSUP;
957659607e0Smrg
958659607e0Smrg    /* NetBSD uses PROT_BM for "PS/2". */
959659607e0Smrg    xf86GetOS(&osname, NULL, NULL, NULL);
960659607e0Smrg    if (osname && xf86NameCmp(osname, "netbsd") == 0)
961659607e0Smrg	for (i = 0; mouseProtocols[i].name; i++)
962659607e0Smrg	    if (mouseProtocols[i].id == PROT_PS2)
963659607e0Smrg		mouseProtocols[i].id = PROT_BM;
964659607e0Smrg
965659607e0Smrg    return TRUE;
966659607e0Smrg}
967659607e0Smrg
968659607e0Smrgstatic InputInfoPtr
969659607e0SmrgMousePreInit(InputDriverPtr drv, IDevPtr dev, int flags)
970659607e0Smrg{
971659607e0Smrg    InputInfoPtr pInfo;
972659607e0Smrg    MouseDevPtr pMse;
973659607e0Smrg    mousePrivPtr mPriv;
974659607e0Smrg    MessageType protocolFrom = X_DEFAULT, deviceFrom = X_CONFIG;
975659607e0Smrg    const char *protocol, *osProt = NULL;
976659607e0Smrg    const char *device;
977659607e0Smrg    MouseProtocolID protocolID;
978659607e0Smrg    MouseProtocolPtr pProto;
979659607e0Smrg    Bool detected;
980659607e0Smrg    int i;
981659607e0Smrg
982659607e0Smrg    if (!InitProtocols())
983659607e0Smrg	return NULL;
984659607e0Smrg
985659607e0Smrg    if (!(pInfo = xf86AllocateInput(drv, 0)))
986659607e0Smrg	return NULL;
987659607e0Smrg
988659607e0Smrg    /* Initialise the InputInfoRec. */
989659607e0Smrg    pInfo->name = dev->identifier;
990659607e0Smrg    pInfo->type_name = XI_MOUSE;
991659607e0Smrg    pInfo->flags = XI86_POINTER_CAPABLE | XI86_SEND_DRAG_EVENTS;
992659607e0Smrg    pInfo->device_control = MouseProc;
993659607e0Smrg    pInfo->read_input = MouseReadInput;
994659607e0Smrg#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) == 0
995659607e0Smrg    pInfo->motion_history_proc = xf86GetMotionEvents;
996659607e0Smrg    pInfo->history_size = 0;
997659607e0Smrg#endif
998659607e0Smrg    pInfo->control_proc = NULL;
999659607e0Smrg    pInfo->close_proc = NULL;
1000659607e0Smrg    pInfo->switch_mode = NULL;
1001659607e0Smrg    pInfo->conversion_proc = MouseConvert;
1002659607e0Smrg    pInfo->reverse_conversion_proc = NULL;
1003659607e0Smrg    pInfo->fd = -1;
1004659607e0Smrg    pInfo->dev = NULL;
1005659607e0Smrg    pInfo->private_flags = 0;
1006659607e0Smrg    pInfo->always_core_feedback = 0;
1007659607e0Smrg    pInfo->conf_idev = dev;
1008659607e0Smrg
1009659607e0Smrg    /* Check if SendDragEvents has been disabled. */
1010659607e0Smrg    if (!xf86SetBoolOption(dev->commonOptions, "SendDragEvents", TRUE)) {
1011659607e0Smrg	pInfo->flags &= ~XI86_SEND_DRAG_EVENTS;
1012659607e0Smrg    }
1013659607e0Smrg
1014659607e0Smrg    /* Allocate the MouseDevRec and initialise it. */
1015659607e0Smrg    /*
1016659607e0Smrg     * XXX This should be done by a function in the core server since the
1017659607e0Smrg     * MouseDevRec is defined in the os-support layer.
1018659607e0Smrg     */
1019659607e0Smrg    if (!(pMse = xcalloc(sizeof(MouseDevRec), 1)))
1020659607e0Smrg	return pInfo;
1021659607e0Smrg    pInfo->private = pMse;
1022659607e0Smrg    pMse->Ctrl = MouseCtrl;
1023659607e0Smrg    pMse->PostEvent = MousePostEvent;
1024659607e0Smrg    pMse->CommonOptions = MouseCommonOptions;
1025659607e0Smrg
1026659607e0Smrg    /* Find the protocol type. */
1027659607e0Smrg    protocol = xf86SetStrOption(dev->commonOptions, "Protocol", NULL);
1028659607e0Smrg    if (protocol) {
1029659607e0Smrg	protocolFrom = X_CONFIG;
1030659607e0Smrg    } else if (osInfo->DefaultProtocol) {
1031659607e0Smrg	protocol = osInfo->DefaultProtocol();
1032659607e0Smrg	protocolFrom = X_DEFAULT;
1033659607e0Smrg    }
1034659607e0Smrg    if (!protocol) {
1035659607e0Smrg	xf86Msg(X_ERROR, "%s: No Protocol specified\n", pInfo->name);
1036659607e0Smrg	return pInfo;
1037659607e0Smrg    }
1038659607e0Smrg
1039659607e0Smrg    /* Default Mapping: 1 2 3 8 9 10 11 ... */
1040659607e0Smrg    for (i = 0; i < MSE_MAXBUTTONS; i++)
1041659607e0Smrg	pMse->buttonMap[i] = 1 << (i > 2 && i < MSE_MAXBUTTONS-4 ? i+4 : i);
1042659607e0Smrg
1043659607e0Smrg    protocolID = ProtocolNameToID(protocol);
1044659607e0Smrg    do {
1045659607e0Smrg	detected = TRUE;
1046659607e0Smrg	switch (protocolID) {
1047659607e0Smrg	case PROT_AUTO:
1048659607e0Smrg	    if (osInfo->SetupAuto) {
1049659607e0Smrg		if ((osProt = osInfo->SetupAuto(pInfo,NULL))) {
1050659607e0Smrg		    MouseProtocolID id = ProtocolNameToID(osProt);
1051659607e0Smrg		    if (id == PROT_UNKNOWN || id == PROT_UNSUP) {
1052659607e0Smrg			protocolID = id;
1053659607e0Smrg			protocol = osProt;
1054659607e0Smrg			detected = FALSE;
1055659607e0Smrg		    }
1056659607e0Smrg		}
1057659607e0Smrg	    }
1058659607e0Smrg	    break;
1059659607e0Smrg	case PROT_UNKNOWN:
1060659607e0Smrg	    /* Check for a builtin OS-specific protocol,
1061659607e0Smrg	     * and call its PreInit. */
1062659607e0Smrg	    if (osInfo->CheckProtocol
1063659607e0Smrg		&& osInfo->CheckProtocol(protocol)) {
1064659607e0Smrg		if (!xf86CheckStrOption(dev->commonOptions, "Device", NULL) &&
1065659607e0Smrg		    HAVE_FIND_DEVICE && osInfo->FindDevice) {
1066659607e0Smrg		    xf86Msg(X_WARNING, "%s: No Device specified, "
1067659607e0Smrg			    "looking for one...\n", pInfo->name);
1068659607e0Smrg		    if (!osInfo->FindDevice(pInfo, protocol, 0)) {
1069659607e0Smrg			xf86Msg(X_ERROR, "%s: Cannot find which device "
1070659607e0Smrg				"to use.\n", pInfo->name);
1071659607e0Smrg		    } else
1072659607e0Smrg			deviceFrom = X_PROBED;
1073659607e0Smrg		}
1074659607e0Smrg		if (osInfo->PreInit) {
1075659607e0Smrg		    osInfo->PreInit(pInfo, protocol, 0);
1076659607e0Smrg		}
1077659607e0Smrg		return pInfo;
1078659607e0Smrg	    }
1079659607e0Smrg	    xf86Msg(X_ERROR, "%s: Unknown protocol \"%s\"\n",
1080659607e0Smrg		    pInfo->name, protocol);
1081659607e0Smrg	    return pInfo;
1082659607e0Smrg	    break;
1083659607e0Smrg	case PROT_UNSUP:
1084659607e0Smrg	    xf86Msg(X_ERROR,
1085659607e0Smrg		    "%s: Protocol \"%s\" is not supported on this "
1086659607e0Smrg		    "platform\n", pInfo->name, protocol);
1087659607e0Smrg	    return pInfo;
1088659607e0Smrg	    break;
1089659607e0Smrg	default:
1090659607e0Smrg	    break;
1091659607e0Smrg
1092659607e0Smrg	}
1093659607e0Smrg    } while (!detected);
1094659607e0Smrg
1095659607e0Smrg    if (!xf86CheckStrOption(dev->commonOptions, "Device", NULL) &&
1096659607e0Smrg	HAVE_FIND_DEVICE && osInfo->FindDevice) {
1097659607e0Smrg	xf86Msg(X_WARNING, "%s: No Device specified, looking for one...\n",
1098659607e0Smrg		pInfo->name);
1099659607e0Smrg	if (!osInfo->FindDevice(pInfo, protocol, 0)) {
1100659607e0Smrg	    xf86Msg(X_ERROR, "%s: Cannot find which device to use.\n",
1101659607e0Smrg		    pInfo->name);
1102659607e0Smrg	} else {
1103659607e0Smrg	    deviceFrom = X_PROBED;
1104659607e0Smrg	    xf86MarkOptionUsedByName(dev->commonOptions, "Device");
1105659607e0Smrg	}
1106659607e0Smrg    }
1107659607e0Smrg
1108659607e0Smrg    device = xf86CheckStrOption(dev->commonOptions, "Device", NULL);
1109659607e0Smrg    if (device)
1110659607e0Smrg	xf86Msg(deviceFrom, "%s: Device: \"%s\"\n", pInfo->name, device);
1111659607e0Smrg
1112659607e0Smrg    xf86Msg(protocolFrom, "%s: Protocol: \"%s\"\n", pInfo->name, protocol);
1113659607e0Smrg    if (!(pProto = GetProtocol(protocolID)))
1114659607e0Smrg	return pInfo;
1115659607e0Smrg
1116659607e0Smrg    pMse->protocolID = protocolID;
1117659607e0Smrg    pMse->oldProtocolID = protocolID;  /* hack */
1118659607e0Smrg
1119659607e0Smrg    pMse->autoProbe = FALSE;
1120659607e0Smrg    /* Collect the options, and process the common options. */
1121659607e0Smrg    xf86CollectInputOptions(pInfo, pProto->defaults, NULL);
1122659607e0Smrg    xf86ProcessCommonOptions(pInfo, pInfo->options);
1123659607e0Smrg
1124659607e0Smrg    /* XXX should handle this OS dependency elsewhere. */
1125659607e0Smrg#ifndef __OS2ELF__
1126659607e0Smrg    /* OS/2 has a mouse handled by the OS - it cannot fail here */
1127659607e0Smrg
1128659607e0Smrg    /* Check if the device can be opened. */
1129659607e0Smrg    pInfo->fd = xf86OpenSerial(pInfo->options);
1130659607e0Smrg    if (pInfo->fd == -1) {
1131659607e0Smrg	if (xf86GetAllowMouseOpenFail())
1132659607e0Smrg	    xf86Msg(X_WARNING, "%s: cannot open input device\n", pInfo->name);
1133659607e0Smrg	else {
1134659607e0Smrg	    xf86Msg(X_ERROR, "%s: cannot open input device\n", pInfo->name);
1135659607e0Smrg	    if (pMse->mousePriv)
1136659607e0Smrg		xfree(pMse->mousePriv);
1137659607e0Smrg	    xfree(pMse);
1138659607e0Smrg	    pInfo->private = NULL;
1139659607e0Smrg	    return pInfo;
1140659607e0Smrg	}
1141659607e0Smrg    }
1142659607e0Smrg    xf86CloseSerial(pInfo->fd);
1143659607e0Smrg#endif
1144659607e0Smrg    pInfo->fd = -1;
1145659607e0Smrg
1146659607e0Smrg    if (!(mPriv = (pointer) xcalloc(sizeof(mousePrivRec), 1)))
1147659607e0Smrg	return pInfo;
1148659607e0Smrg    pMse->mousePriv = mPriv;
1149659607e0Smrg    pMse->CommonOptions(pInfo);
1150659607e0Smrg    pMse->checkMovements = checkForErraticMovements;
1151659607e0Smrg    pMse->autoProbeMouse = autoProbeMouse;
1152659607e0Smrg    pMse->collectData = collectData;
1153659607e0Smrg    pMse->dataGood = autoGood;
1154659607e0Smrg
1155659607e0Smrg    MouseHWOptions(pInfo);
1156659607e0Smrg    MouseSerialOptions(pInfo);
1157659607e0Smrg
1158659607e0Smrg    pInfo->flags |= XI86_CONFIGURED;
1159659607e0Smrg    return pInfo;
1160659607e0Smrg}
1161659607e0Smrg
1162659607e0Smrg
1163659607e0Smrgstatic void
1164659607e0SmrgMouseReadInput(InputInfoPtr pInfo)
1165659607e0Smrg{
1166659607e0Smrg    MouseDevPtr pMse;
1167659607e0Smrg    int j, buttons, dx, dy, dz, dw, baddata;
1168659607e0Smrg    int pBufP;
1169659607e0Smrg    int c;
1170659607e0Smrg    unsigned char *pBuf, u;
1171659607e0Smrg
1172659607e0Smrg
1173659607e0Smrg    pMse = pInfo->private;
1174659607e0Smrg    pBufP = pMse->protoBufTail;
1175659607e0Smrg    pBuf = pMse->protoBuf;
1176659607e0Smrg
1177659607e0Smrg    if (pInfo->fd == -1)
1178659607e0Smrg	return;
1179659607e0Smrg
1180659607e0Smrg    /*
1181659607e0Smrg     * Set blocking to -1 on the first call because we know there is data to
1182659607e0Smrg     * read. Xisb automatically clears it after one successful read so that
1183659607e0Smrg     * succeeding reads are preceeded by a select with a 0 timeout to prevent
1184659607e0Smrg     * read from blocking indefinitely.
1185659607e0Smrg     */
1186659607e0Smrg    XisbBlockDuration(pMse->buffer, -1);
1187659607e0Smrg
1188659607e0Smrg    while ((c = XisbRead(pMse->buffer)) >= 0) {
1189659607e0Smrg	u = (unsigned char)c;
1190659607e0Smrg
1191659607e0Smrg#if defined (EXTMOUSEDEBUG) || defined (MOUSEDATADEBUG)
1192659607e0Smrg	ErrorF("mouse byte: %2.2x\n",u);
1193659607e0Smrg#endif
1194659607e0Smrg
1195659607e0Smrg#if 1
1196659607e0Smrg	/* if we do autoprobing collect the data */
1197659607e0Smrg	if (pMse->collectData && pMse->autoProbe)
1198659607e0Smrg	    if (pMse->collectData(pMse,u))
1199659607e0Smrg		continue;
1200659607e0Smrg#endif
1201659607e0Smrg#ifdef SUPPORT_MOUSE_RESET
1202659607e0Smrg	if (mouseReset(pInfo,u)) {
1203659607e0Smrg	    pBufP = 0;
1204659607e0Smrg	    continue;
1205659607e0Smrg	}
1206659607e0Smrg#endif
1207659607e0Smrg	if (pBufP >= pMse->protoPara[4]) {
1208659607e0Smrg	    /*
1209659607e0Smrg	     * Buffer contains a full packet, which has already been processed:
1210659607e0Smrg	     * Empty the buffer and check for optional 4th byte, which will be
1211659607e0Smrg	     * processed directly, without being put into the buffer first.
1212659607e0Smrg	     */
1213659607e0Smrg	    pBufP = 0;
1214659607e0Smrg	    if ((u & pMse->protoPara[0]) != pMse->protoPara[1] &&
1215659607e0Smrg		(u & pMse->protoPara[5]) == pMse->protoPara[6]) {
1216659607e0Smrg		/*
1217659607e0Smrg		 * Hack for Logitech MouseMan Mouse - Middle button
1218659607e0Smrg		 *
1219659607e0Smrg		 * Unfortunately this mouse has variable length packets: the
1220659607e0Smrg		 * standard Microsoft 3 byte packet plus an optional 4th byte
1221659607e0Smrg		 * whenever the middle button status changes.
1222659607e0Smrg		 *
1223659607e0Smrg		 * We have already processed the standard packet with the
1224659607e0Smrg		 * movement and button info.  Now post an event message with
1225659607e0Smrg		 * the old status of the left and right buttons and the
1226659607e0Smrg		 * updated middle button.
1227659607e0Smrg		 */
1228659607e0Smrg		/*
1229659607e0Smrg		 * Even worse, different MouseMen and TrackMen differ in the
1230659607e0Smrg		 * 4th byte: some will send 0x00/0x20, others 0x01/0x21, or
1231659607e0Smrg		 * even 0x02/0x22, so I have to strip off the lower bits.
1232659607e0Smrg		 * [CHRIS-211092]
1233659607e0Smrg		 *
1234659607e0Smrg		 * [JCH-96/01/21]
1235659607e0Smrg		 * HACK for ALPS "fourth button".  (It's bit 0x10 of the
1236659607e0Smrg		 * "fourth byte" and it is activated by tapping the glidepad
1237659607e0Smrg		 * with the finger! 8^) We map it to bit bit3, and the
1238659607e0Smrg		 * reverse map in xf86Events just has to be extended so that
1239659607e0Smrg		 * it is identified as Button 4.  The lower half of the
1240659607e0Smrg		 * reverse-map may remain unchanged.
1241659607e0Smrg		 */
1242659607e0Smrg		/*
1243659607e0Smrg		 * [KAZU-030897]
1244659607e0Smrg		 * Receive the fourth byte only when preceeding three bytes
1245659607e0Smrg		 * have been detected (pBufP >= pMse->protoPara[4]).  In the
1246659607e0Smrg		 * previous versions, the test was pBufP == 0; we may have
1247659607e0Smrg		 * mistakingly received a byte even if we didn't see anything
1248659607e0Smrg		 * preceeding the byte.
1249659607e0Smrg		 */
1250659607e0Smrg#ifdef EXTMOUSEDEBUG
1251659607e0Smrg		ErrorF("mouse 4th byte %02x\n",u);
1252659607e0Smrg#endif
1253659607e0Smrg		dx = dy = dz = dw = 0;
1254659607e0Smrg		buttons = 0;
1255659607e0Smrg		switch (pMse->protocolID) {
1256659607e0Smrg
1257659607e0Smrg		/*
1258659607e0Smrg		 * [KAZU-221197]
1259659607e0Smrg		 * IntelliMouse, NetMouse (including NetMouse Pro) and Mie
1260659607e0Smrg		 * Mouse always send the fourth byte, whereas the fourth byte
1261659607e0Smrg		 * is optional for GlidePoint and ThinkingMouse.  The fourth
1262659607e0Smrg		 * byte is also optional for MouseMan+ and FirstMouse+ in
1263659607e0Smrg		 * their native mode.  It is always sent if they are in the
1264659607e0Smrg		 * IntelliMouse compatible mode.
1265659607e0Smrg		 */
1266659607e0Smrg		case PROT_IMSERIAL:	/* IntelliMouse, NetMouse, Mie Mouse,
1267659607e0Smrg					   MouseMan+ */
1268659607e0Smrg		    dz = (u & 0x08) ?
1269659607e0Smrg				(u & 0x0f) - 16 : (u & 0x0f);
1270659607e0Smrg		    if ((dz >= 7) || (dz <= -7))
1271659607e0Smrg			dz = 0;
1272659607e0Smrg		    buttons |=  ((int)(u & 0x10) >> 3)
1273659607e0Smrg			      | ((int)(u & 0x20) >> 2)
1274659607e0Smrg			      | (pMse->lastButtons & 0x05);
1275659607e0Smrg		    break;
1276659607e0Smrg
1277659607e0Smrg		case PROT_GLIDE:
1278659607e0Smrg		case PROT_THINKING:
1279659607e0Smrg		    buttons |= ((int)(u & 0x10) >> 1);
1280659607e0Smrg		    /* fall through */
1281659607e0Smrg
1282659607e0Smrg		default:
1283659607e0Smrg		    buttons |= ((int)(u & 0x20) >> 4) |
1284659607e0Smrg			       (pMse->lastButtons & 0x05);
1285659607e0Smrg		    break;
1286659607e0Smrg		}
1287659607e0Smrg		goto post_event;
1288659607e0Smrg	    }
1289659607e0Smrg	}
1290659607e0Smrg	/* End of packet buffer flush and 4th byte hack. */
1291659607e0Smrg
1292659607e0Smrg	/*
1293659607e0Smrg	 * Append next byte to buffer (which is empty or contains an
1294659607e0Smrg	 * incomplete packet); iterate if packet (still) not complete.
1295659607e0Smrg	 */
1296659607e0Smrg	pBuf[pBufP++] = u;
1297659607e0Smrg	if (pBufP != pMse->protoPara[4]) continue;
1298659607e0Smrg#ifdef EXTMOUSEDEBUG2
1299659607e0Smrg	{
1300659607e0Smrg	    int i;
1301659607e0Smrg	    ErrorF("received %d bytes",pBufP);
1302659607e0Smrg	    for ( i=0; i < pBufP; i++)
1303659607e0Smrg		ErrorF(" %02x",pBuf[i]);
1304659607e0Smrg	    ErrorF("\n");
1305659607e0Smrg	}
1306659607e0Smrg#endif
1307659607e0Smrg
1308659607e0Smrg	/*
1309659607e0Smrg	 * Hack for resyncing: We check here for a package that is:
1310659607e0Smrg	 *  a) illegal (detected by wrong data-package header)
1311659607e0Smrg	 *  b) invalid (0x80 == -128 and that might be wrong for MouseSystems)
1312659607e0Smrg	 *  c) bad header-package
1313659607e0Smrg	 *
1314659607e0Smrg	 * NOTE: b) is a violation of the MouseSystems-Protocol, since values
1315659607e0Smrg	 *       of -128 are allowed, but since they are very seldom we can
1316659607e0Smrg	 *       easily  use them as package-header with no button pressed.
1317659607e0Smrg	 * NOTE/2: On a PS/2 mouse any byte is valid as a data byte.
1318659607e0Smrg	 *       Furthermore, 0x80 is not valid as a header byte. For a PS/2
1319659607e0Smrg	 *       mouse we skip checking data bytes.  For resyncing a PS/2
1320659607e0Smrg	 *       mouse we require the two most significant bits in the header
1321659607e0Smrg	 *       byte to be 0. These are the overflow bits, and in case of
1322659607e0Smrg	 *       an overflow we actually lose sync. Overflows are very rare,
1323659607e0Smrg	 *       however, and we quickly gain sync again after an overflow
1324659607e0Smrg	 *       condition. This is the best we can do. (Actually, we could
1325659607e0Smrg	 *       use bit 0x08 in the header byte for resyncing, since that
1326659607e0Smrg	 *       bit is supposed to be always on, but nobody told Microsoft...)
1327659607e0Smrg	 */
1328659607e0Smrg
1329659607e0Smrg	/*
1330659607e0Smrg	 * [KAZU,OYVIND-120398]
1331659607e0Smrg	 * The above hack is wrong!  Because of b) above, we shall see
1332659607e0Smrg	 * erroneous mouse events so often when the MouseSystem mouse is
1333659607e0Smrg	 * moved quickly.  As for the PS/2 and its variants, we don't need
1334659607e0Smrg	 * to treat them as special cases, because protoPara[2] and
1335659607e0Smrg	 * protoPara[3] are both 0x00 for them, thus, any data bytes will
1336659607e0Smrg	 * never be discarded.  0x80 is rejected for MMSeries, Logitech
1337659607e0Smrg	 * and MMHittab protocols, because protoPara[2] and protoPara[3]
1338659607e0Smrg	 * are 0x80 and 0x00 respectively.  The other protocols are 7-bit
1339659607e0Smrg	 * protocols; there is no use checking 0x80.
1340659607e0Smrg	 *
1341659607e0Smrg	 * All in all we should check the condition a) only.
1342659607e0Smrg	 */
1343659607e0Smrg
1344659607e0Smrg	/*
1345659607e0Smrg	 * [OYVIND-120498]
1346659607e0Smrg	 * Check packet for valid data:
1347659607e0Smrg	 * If driver is in sync with datastream, the packet is considered
1348659607e0Smrg	 * bad if any byte (header and/or data) contains an invalid value.
1349659607e0Smrg	 *
1350659607e0Smrg	 * If packet is bad, we discard the first byte and shift the buffer.
1351659607e0Smrg	 * Next iteration will then check the new situation for validity.
1352659607e0Smrg	 *
1353659607e0Smrg	 * If flag MF_SAFE is set in proto[7] and the driver
1354659607e0Smrg	 * is out of sync, the packet is also considered bad if
1355659607e0Smrg	 * any of the data bytes contains a valid header byte value.
1356659607e0Smrg	 * This situation could occur if the buffer contains
1357659607e0Smrg	 * the tail of one packet and the header of the next.
1358659607e0Smrg	 *
1359659607e0Smrg	 * Note: The driver starts in out-of-sync mode (pMse->inSync = 0).
1360659607e0Smrg	 */
1361659607e0Smrg
1362659607e0Smrg	baddata = 0;
1363659607e0Smrg
1364659607e0Smrg	/* All databytes must be valid. */
1365659607e0Smrg	for (j = 1; j < pBufP; j++ )
1366659607e0Smrg	    if ((pBuf[j] & pMse->protoPara[2]) != pMse->protoPara[3])
1367659607e0Smrg		baddata = 1;
1368659607e0Smrg
1369659607e0Smrg	/* If out of sync, don't mistake a header byte for data. */
1370659607e0Smrg	if ((pMse->protoPara[7] & MPF_SAFE) && !pMse->inSync)
1371659607e0Smrg	    for (j = 1; j < pBufP; j++ )
1372659607e0Smrg		if ((pBuf[j] & pMse->protoPara[0]) == pMse->protoPara[1])
1373659607e0Smrg		    baddata = 1;
1374659607e0Smrg
1375659607e0Smrg	/* Accept or reject the packet ? */
1376659607e0Smrg	if ((pBuf[0] & pMse->protoPara[0]) != pMse->protoPara[1] || baddata) {
1377659607e0Smrg	    if (pMse->inSync) {
1378659607e0Smrg#ifdef EXTMOUSEDEBUG
1379659607e0Smrg		ErrorF("mouse driver lost sync\n");
1380659607e0Smrg#endif
1381659607e0Smrg	    }
1382659607e0Smrg#ifdef EXTMOUSEDEBUG
1383659607e0Smrg	    ErrorF("skipping byte %02x\n",*pBuf);
1384659607e0Smrg#endif
1385659607e0Smrg	    /* Tell auto probe that we are out of sync */
1386659607e0Smrg	    if (pMse->autoProbeMouse && pMse->autoProbe)
1387659607e0Smrg		pMse->autoProbeMouse(pInfo, FALSE, pMse->inSync);
1388659607e0Smrg	    pMse->protoBufTail = --pBufP;
1389659607e0Smrg	    for (j = 0; j < pBufP; j++)
1390659607e0Smrg		pBuf[j] = pBuf[j+1];
1391659607e0Smrg	    pMse->inSync = 0;
1392659607e0Smrg	    continue;
1393659607e0Smrg	}
1394659607e0Smrg	/* Tell auto probe that we were successful */
1395659607e0Smrg	if (pMse->autoProbeMouse && pMse->autoProbe)
1396659607e0Smrg	    pMse->autoProbeMouse(pInfo, TRUE, FALSE);
1397659607e0Smrg
1398659607e0Smrg	if (!pMse->inSync) {
1399659607e0Smrg#ifdef EXTMOUSEDEBUG
1400659607e0Smrg	    ErrorF("mouse driver back in sync\n");
1401659607e0Smrg#endif
1402659607e0Smrg	    pMse->inSync = 1;
1403659607e0Smrg	}
1404659607e0Smrg
1405659607e0Smrg  	if (!pMse->dataGood(pMse))
1406659607e0Smrg  	    continue;
1407659607e0Smrg
1408659607e0Smrg	/*
1409659607e0Smrg	 * Packet complete and verified, now process it ...
1410659607e0Smrg	 */
1411659607e0Smrg    REDO_INTERPRET:
1412659607e0Smrg	dz = dw = 0;
1413659607e0Smrg	switch (pMse->protocolID) {
1414659607e0Smrg	case PROT_LOGIMAN:	/* MouseMan / TrackMan   [CHRIS-211092] */
1415659607e0Smrg	case PROT_MS:		/* Microsoft */
1416659607e0Smrg	    if (pMse->chordMiddle)
1417659607e0Smrg		buttons = (((int) pBuf[0] & 0x30) == 0x30) ? 2 :
1418659607e0Smrg				  ((int)(pBuf[0] & 0x20) >> 3)
1419659607e0Smrg				| ((int)(pBuf[0] & 0x10) >> 4);
1420659607e0Smrg	    else
1421659607e0Smrg        	buttons = (pMse->lastButtons & 2)
1422659607e0Smrg			| ((int)(pBuf[0] & 0x20) >> 3)
1423659607e0Smrg			| ((int)(pBuf[0] & 0x10) >> 4);
1424659607e0Smrg	    dx = (char)(((pBuf[0] & 0x03) << 6) | (pBuf[1] & 0x3F));
1425659607e0Smrg	    dy = (char)(((pBuf[0] & 0x0C) << 4) | (pBuf[2] & 0x3F));
1426659607e0Smrg	    break;
1427659607e0Smrg
1428659607e0Smrg	case PROT_GLIDE:	/* ALPS GlidePoint */
1429659607e0Smrg	case PROT_THINKING:	/* ThinkingMouse */
1430659607e0Smrg	case PROT_IMSERIAL:	/* IntelliMouse, NetMouse, Mie Mouse, MouseMan+ */
1431659607e0Smrg	    buttons =  (pMse->lastButtons & (8 + 2))
1432659607e0Smrg		     | ((int)(pBuf[0] & 0x20) >> 3)
1433659607e0Smrg		     | ((int)(pBuf[0] & 0x10) >> 4);
1434659607e0Smrg	    dx = (char)(((pBuf[0] & 0x03) << 6) | (pBuf[1] & 0x3F));
1435659607e0Smrg	    dy = (char)(((pBuf[0] & 0x0C) << 4) | (pBuf[2] & 0x3F));
1436659607e0Smrg	    break;
1437659607e0Smrg
1438659607e0Smrg	case PROT_MSC:		/* Mouse Systems Corp */
1439659607e0Smrg	    buttons = (~pBuf[0]) & 0x07;
1440659607e0Smrg	    dx =    (char)(pBuf[1]) + (char)(pBuf[3]);
1441659607e0Smrg	    dy = - ((char)(pBuf[2]) + (char)(pBuf[4]));
1442659607e0Smrg	    break;
1443659607e0Smrg
1444659607e0Smrg	case PROT_MMHIT:	/* MM_HitTablet */
1445659607e0Smrg	    buttons = pBuf[0] & 0x07;
1446659607e0Smrg	    if (buttons != 0)
1447659607e0Smrg		buttons = 1 << (buttons - 1);
1448659607e0Smrg	    dx = (pBuf[0] & 0x10) ?   pBuf[1] : - pBuf[1];
1449659607e0Smrg	    dy = (pBuf[0] & 0x08) ? - pBuf[2] :   pBuf[2];
1450659607e0Smrg	    break;
1451659607e0Smrg
1452659607e0Smrg	case PROT_ACECAD:	/* ACECAD */
1453659607e0Smrg	    /* ACECAD is almost exactly like MM but the buttons are different */
1454659607e0Smrg	    buttons = (pBuf[0] & 0x02) | ((pBuf[0] & 0x04) >> 2) |
1455659607e0Smrg		      ((pBuf[0] & 1) << 2);
1456659607e0Smrg	    dx = (pBuf[0] & 0x10) ?   pBuf[1] : - pBuf[1];
1457659607e0Smrg	    dy = (pBuf[0] & 0x08) ? - pBuf[2] :   pBuf[2];
1458659607e0Smrg	    break;
1459659607e0Smrg
1460659607e0Smrg	case PROT_MM:		/* MM Series */
1461659607e0Smrg	case PROT_LOGI:		/* Logitech Mice */
1462659607e0Smrg	    buttons = pBuf[0] & 0x07;
1463659607e0Smrg	    dx = (pBuf[0] & 0x10) ?   pBuf[1] : - pBuf[1];
1464659607e0Smrg	    dy = (pBuf[0] & 0x08) ? - pBuf[2] :   pBuf[2];
1465659607e0Smrg	    break;
1466659607e0Smrg
1467659607e0Smrg	case PROT_BM:		/* BusMouse */
1468659607e0Smrg	    buttons = (~pBuf[0]) & 0x07;
1469659607e0Smrg	    dx =   (char)pBuf[1];
1470659607e0Smrg	    dy = - (char)pBuf[2];
1471659607e0Smrg	    break;
1472659607e0Smrg
1473659607e0Smrg	case PROT_PS2:		/* PS/2 mouse */
1474659607e0Smrg	case PROT_GENPS2:	/* generic PS/2 mouse */
1475659607e0Smrg	    buttons = (pBuf[0] & 0x04) >> 1 |       /* Middle */
1476659607e0Smrg		      (pBuf[0] & 0x02) >> 1 |       /* Right */
1477659607e0Smrg		      (pBuf[0] & 0x01) << 2;        /* Left */
1478659607e0Smrg	    dx = (pBuf[0] & 0x10) ?    (int)pBuf[1]-256  :  (int)pBuf[1];
1479659607e0Smrg	    dy = (pBuf[0] & 0x20) ?  -((int)pBuf[2]-256) : -(int)pBuf[2];
1480659607e0Smrg	    break;
1481659607e0Smrg
1482659607e0Smrg	/* PS/2 mouse variants */
1483659607e0Smrg	case PROT_IMPS2:	/* IntelliMouse PS/2 */
1484659607e0Smrg	case PROT_NETPS2:	/* NetMouse PS/2 */
1485659607e0Smrg	    buttons = (pBuf[0] & 0x04) >> 1 |       /* Middle */
1486659607e0Smrg		      (pBuf[0] & 0x02) >> 1 |       /* Right */
1487659607e0Smrg		      (pBuf[0] & 0x01) << 2 |       /* Left */
1488659607e0Smrg		      (pBuf[0] & 0x40) >> 3 |       /* button 4 */
1489659607e0Smrg		      (pBuf[0] & 0x80) >> 3;        /* button 5 */
1490659607e0Smrg	    dx = (pBuf[0] & 0x10) ?    pBuf[1]-256  :  pBuf[1];
1491659607e0Smrg	    dy = (pBuf[0] & 0x20) ?  -(pBuf[2]-256) : -pBuf[2];
1492659607e0Smrg	    /*
1493659607e0Smrg	     * The next cast must be 'signed char' for platforms (like PPC)
1494659607e0Smrg	     * where char defaults to unsigned.
1495659607e0Smrg	     */
1496659607e0Smrg	    dz = (signed char)(pBuf[3] | ((pBuf[3] & 0x08) ? 0xf8 : 0));
1497659607e0Smrg	    if ((pBuf[3] & 0xf8) && ((pBuf[3] & 0xf8) != 0xf8)) {
1498659607e0Smrg		if (pMse->autoProbe) {
1499659607e0Smrg		    SetMouseProto(pMse, PROT_EXPPS2);
1500659607e0Smrg		    xf86Msg(X_INFO,
1501659607e0Smrg			    "Mouse autoprobe: Changing protocol to %s\n",
1502659607e0Smrg			    pMse->protocol);
1503659607e0Smrg
1504659607e0Smrg		    goto REDO_INTERPRET;
1505659607e0Smrg		} else
1506659607e0Smrg		    dz = 0;
1507659607e0Smrg	    }
1508659607e0Smrg	    break;
1509659607e0Smrg
1510659607e0Smrg	case PROT_EXPPS2:	/* IntelliMouse Explorer PS/2 */
1511659607e0Smrg	    if (pMse->autoProbe && (pBuf[3] & 0xC0)) {
1512659607e0Smrg		SetMouseProto(pMse, PROT_IMPS2);
1513659607e0Smrg		xf86Msg(X_INFO,"Mouse autoprobe: Changing protocol to %s\n",
1514659607e0Smrg			pMse->protocol);
1515659607e0Smrg		goto REDO_INTERPRET;
1516659607e0Smrg	    }
1517659607e0Smrg	    buttons = (pBuf[0] & 0x04) >> 1 |       /* Middle */
1518659607e0Smrg		      (pBuf[0] & 0x02) >> 1 |       /* Right */
1519659607e0Smrg		      (pBuf[0] & 0x01) << 2 |       /* Left */
1520659607e0Smrg		      (pBuf[3] & 0x10) >> 1 |       /* button 4 */
1521659607e0Smrg		      (pBuf[3] & 0x20) >> 1;        /* button 5 */
1522659607e0Smrg	    dx = (pBuf[0] & 0x10) ?    pBuf[1]-256  :  pBuf[1];
1523659607e0Smrg	    dy = (pBuf[0] & 0x20) ?  -(pBuf[2]-256) : -pBuf[2];
1524659607e0Smrg	    if (pMse->negativeW != MSE_NOAXISMAP) {
1525659607e0Smrg		switch (pBuf[3] & 0x0f) {
1526659607e0Smrg		case 0x00:          break;
1527659607e0Smrg		case 0x01: dz =  1; break;
1528659607e0Smrg		case 0x02: dw =  1; break;
1529659607e0Smrg		case 0x0e: dw = -1; break;
1530659607e0Smrg		case 0x0f: dz = -1; break;
1531659607e0Smrg		default:
1532659607e0Smrg		    xf86Msg(X_INFO,
1533659607e0Smrg			    "Mouse autoprobe: Disabling secondary wheel\n");
1534659607e0Smrg		    pMse->negativeW = pMse->positiveW = MSE_NOAXISMAP;
1535659607e0Smrg		}
1536659607e0Smrg	    }
1537659607e0Smrg	    if (pMse->negativeW == MSE_NOAXISMAP)
1538659607e0Smrg	        dz = (pBuf[3]&0x08) ? (pBuf[3]&0x0f) - 16 : (pBuf[3]&0x0f);
1539659607e0Smrg	    break;
1540659607e0Smrg
1541659607e0Smrg	case PROT_MMPS2:	/* MouseMan+ PS/2 */
1542659607e0Smrg	    buttons = (pBuf[0] & 0x04) >> 1 |       /* Middle */
1543659607e0Smrg		      (pBuf[0] & 0x02) >> 1 |       /* Right */
1544659607e0Smrg		      (pBuf[0] & 0x01) << 2;        /* Left */
1545659607e0Smrg	    dx = (pBuf[0] & 0x10) ? pBuf[1] - 256 : pBuf[1];
1546659607e0Smrg	    if (((pBuf[0] & 0x48) == 0x48) &&
1547659607e0Smrg		(abs(dx) > 191) &&
1548659607e0Smrg		((((pBuf[2] & 0x03) << 2) | 0x02) == (pBuf[1] & 0x0f))) {
1549659607e0Smrg		/* extended data packet */
1550659607e0Smrg		switch ((((pBuf[0] & 0x30) >> 2) | ((pBuf[1] & 0x30) >> 4))) {
1551659607e0Smrg		case 1:		/* wheel data packet */
1552659607e0Smrg		    buttons |= ((pBuf[2] & 0x10) ? 0x08 : 0) | /* 4th button */
1553659607e0Smrg		               ((pBuf[2] & 0x20) ? 0x10 : 0);  /* 5th button */
1554659607e0Smrg		    dx = dy = 0;
1555659607e0Smrg		    dz = (pBuf[2] & 0x08) ? (pBuf[2] & 0x0f) - 16 :
1556659607e0Smrg					    (pBuf[2] & 0x0f);
1557659607e0Smrg		    break;
1558659607e0Smrg		case 2:		/* Logitech reserves this packet type */
1559659607e0Smrg		    /*
1560659607e0Smrg		     * IBM ScrollPoint uses this packet to encode its
1561659607e0Smrg		     * stick movement.
1562659607e0Smrg		     */
1563659607e0Smrg		    buttons |= (pMse->lastButtons & ~0x07);
1564659607e0Smrg		    dx = dy = 0;
1565659607e0Smrg		    dz = (pBuf[2] & 0x80) ? ((pBuf[2] >> 4) & 0x0f) - 16 :
1566659607e0Smrg					    ((pBuf[2] >> 4) & 0x0f);
1567659607e0Smrg		    dw = (pBuf[2] & 0x08) ? (pBuf[2] & 0x0f) - 16 :
1568659607e0Smrg					    (pBuf[2] & 0x0f);
1569659607e0Smrg		    break;
1570659607e0Smrg		case 0:		/* device type packet - shouldn't happen */
1571659607e0Smrg		default:
1572659607e0Smrg		    buttons |= (pMse->lastButtons & ~0x07);
1573659607e0Smrg		    dx = dy = 0;
1574659607e0Smrg		    dz = 0;
1575659607e0Smrg		    break;
1576659607e0Smrg		}
1577659607e0Smrg	    } else {
1578659607e0Smrg		buttons |= (pMse->lastButtons & ~0x07);
1579659607e0Smrg		dx = (pBuf[0] & 0x10) ?    pBuf[1]-256  :  pBuf[1];
1580659607e0Smrg		dy = (pBuf[0] & 0x20) ?  -(pBuf[2]-256) : -pBuf[2];
1581659607e0Smrg	    }
1582659607e0Smrg	    break;
1583659607e0Smrg
1584659607e0Smrg	case PROT_GLIDEPS2:	/* GlidePoint PS/2 */
1585659607e0Smrg	    buttons = (pBuf[0] & 0x04) >> 1 |       /* Middle */
1586659607e0Smrg		      (pBuf[0] & 0x02) >> 1 |       /* Right */
1587659607e0Smrg		      (pBuf[0] & 0x01) << 2 |       /* Left */
1588659607e0Smrg		      ((pBuf[0] & 0x08) ? 0 : 0x08);/* fourth button */
1589659607e0Smrg	    dx = (pBuf[0] & 0x10) ?    pBuf[1]-256  :  pBuf[1];
1590659607e0Smrg	    dy = (pBuf[0] & 0x20) ?  -(pBuf[2]-256) : -pBuf[2];
1591659607e0Smrg	    break;
1592659607e0Smrg
1593659607e0Smrg	case PROT_NETSCPS2:	/* NetScroll PS/2 */
1594659607e0Smrg	    buttons = (pBuf[0] & 0x04) >> 1 |       /* Middle */
1595659607e0Smrg		      (pBuf[0] & 0x02) >> 1 |       /* Right */
1596659607e0Smrg		      (pBuf[0] & 0x01) << 2 |       /* Left */
1597659607e0Smrg		      ((pBuf[3] & 0x02) ? 0x08 : 0) | /* button 4 */
1598659607e0Smrg		      ((pBuf[3] & 0x01) ? 0x10 : 0);  /* button 5 */
1599659607e0Smrg	    dx = (pBuf[0] & 0x10) ?    pBuf[1]-256  :  pBuf[1];
1600659607e0Smrg	    dy = (pBuf[0] & 0x20) ?  -(pBuf[2]-256) : -pBuf[2];
1601659607e0Smrg	    dz = (pBuf[3] & 0x10) ? pBuf[4] - 256 : pBuf[4];
1602659607e0Smrg	    break;
1603659607e0Smrg
1604659607e0Smrg	case PROT_THINKPS2:	/* ThinkingMouse PS/2 */
1605659607e0Smrg	    buttons = (pBuf[0] & 0x04) >> 1 |       /* Middle */
1606659607e0Smrg		      (pBuf[0] & 0x02) >> 1 |       /* Right */
1607659607e0Smrg		      (pBuf[0] & 0x01) << 2 |       /* Left */
1608659607e0Smrg		      ((pBuf[0] & 0x08) ? 0x08 : 0);/* fourth button */
1609659607e0Smrg	    pBuf[1] |= (pBuf[0] & 0x40) ? 0x80 : 0x00;
1610659607e0Smrg	    dx = (pBuf[0] & 0x10) ?    pBuf[1]-256  :  pBuf[1];
1611659607e0Smrg	    dy = (pBuf[0] & 0x20) ?  -(pBuf[2]-256) : -pBuf[2];
1612659607e0Smrg	    break;
1613659607e0Smrg
1614659607e0Smrg	case PROT_SYSMOUSE:	/* sysmouse */
1615659607e0Smrg	    buttons = (~pBuf[0]) & 0x07;
1616659607e0Smrg	    dx =    (signed char)(pBuf[1]) + (signed char)(pBuf[3]);
1617659607e0Smrg	    dy = - ((signed char)(pBuf[2]) + (signed char)(pBuf[4]));
1618659607e0Smrg	    /* FreeBSD sysmouse sends additional data bytes */
1619659607e0Smrg	    if (pMse->protoPara[4] >= 8) {
1620659607e0Smrg		/*
1621659607e0Smrg		 * These casts must be 'signed char' for platforms (like PPC)
1622659607e0Smrg		 * where char defaults to unsigned.
1623659607e0Smrg		 */
1624659607e0Smrg		dz = ((signed char)(pBuf[5] << 1) +
1625659607e0Smrg		      (signed char)(pBuf[6] << 1)) >> 1;
1626659607e0Smrg		buttons |= (int)(~pBuf[7] & 0x7f) << 3;
1627659607e0Smrg	    }
1628659607e0Smrg	    break;
1629659607e0Smrg
1630659607e0Smrg	case PROT_VALUMOUSESCROLL:	/* Kensington ValuMouseScroll */
1631659607e0Smrg            buttons = ((int)(pBuf[0] & 0x20) >> 3)
1632659607e0Smrg                      | ((int)(pBuf[0] & 0x10) >> 4)
1633659607e0Smrg                      | ((int)(pBuf[3] & 0x10) >> 3);
1634659607e0Smrg            dx = (char)(((pBuf[0] & 0x03) << 6) | (pBuf[1] & 0x3F));
1635659607e0Smrg            dy = (char)(((pBuf[0] & 0x0C) << 4) | (pBuf[2] & 0x3F));
1636659607e0Smrg	    dz = (pBuf[3] & 0x08) ? ((int)(pBuf[3] & 0x0F) - 0x10) :
1637659607e0Smrg                                    ((int)(pBuf[3] & 0x0F));
1638659607e0Smrg	    break;
1639659607e0Smrg
1640659607e0Smrg	default: /* There's a table error */
1641659607e0Smrg#ifdef EXTMOUSEDEBUG
1642659607e0Smrg	    ErrorF("mouse table error\n");
1643659607e0Smrg#endif
1644659607e0Smrg	    continue;
1645659607e0Smrg	}
1646659607e0Smrg#ifdef EXTMOUSEDEBUG
1647659607e0Smrg	ErrorF("packet");
1648659607e0Smrg	for ( j=0; j < pBufP; j++)
1649659607e0Smrg	    ErrorF(" %02x",pBuf[j]);
1650659607e0Smrg	ErrorF("\n");
1651659607e0Smrg#endif
1652659607e0Smrg
1653659607e0Smrgpost_event:
1654659607e0Smrg#ifdef EXTMOUSEDEBUG
1655659607e0Smrg	ErrorF("dx=%i dy=%i dz=%i dw=%i buttons=%x\n",dx,dy,dz,dw,buttons);
1656659607e0Smrg#endif
1657659607e0Smrg	/* When auto-probing check if data makes sense */
1658659607e0Smrg	if (pMse->checkMovements && pMse->autoProbe)
1659659607e0Smrg	    pMse->checkMovements(pInfo,dx,dy);
1660659607e0Smrg	/* post an event */
1661659607e0Smrg	pMse->PostEvent(pInfo, buttons, dx, dy, dz, dw);
1662659607e0Smrg
1663659607e0Smrg	/*
1664659607e0Smrg	 * We don't reset pBufP here yet, as there may be an additional data
1665659607e0Smrg	 * byte in some protocols. See above.
1666659607e0Smrg	 */
1667659607e0Smrg    }
1668659607e0Smrg    pMse->protoBufTail = pBufP;
1669659607e0Smrg}
1670659607e0Smrg
1671659607e0Smrg/*
1672659607e0Smrg * MouseCtrl --
1673659607e0Smrg *      Alter the control parameters for the mouse. Note that all special
1674659607e0Smrg *      protocol values are handled by dix.
1675659607e0Smrg */
1676659607e0Smrg
1677659607e0Smrgstatic void
1678659607e0SmrgMouseCtrl(DeviceIntPtr device, PtrCtrl *ctrl)
1679659607e0Smrg{
1680659607e0Smrg    InputInfoPtr pInfo;
1681659607e0Smrg    MouseDevPtr pMse;
1682659607e0Smrg
1683659607e0Smrg    pInfo = device->public.devicePrivate;
1684659607e0Smrg    pMse = pInfo->private;
1685659607e0Smrg
1686659607e0Smrg#ifdef EXTMOUSEDEBUG
1687659607e0Smrg    ErrorF("MouseCtrl pMse=%p\n", pMse);
1688659607e0Smrg#endif
1689659607e0Smrg
1690659607e0Smrg    pMse->num       = ctrl->num;
1691659607e0Smrg    pMse->den       = ctrl->den;
1692659607e0Smrg    pMse->threshold = ctrl->threshold;
1693659607e0Smrg}
1694659607e0Smrg
1695659607e0Smrg/*
1696659607e0Smrg ***************************************************************************
1697659607e0Smrg *
1698659607e0Smrg * MouseProc --
1699659607e0Smrg *
1700659607e0Smrg ***************************************************************************
1701659607e0Smrg */
1702659607e0Smrg
1703659607e0Smrgstatic int
1704659607e0SmrgMouseProc(DeviceIntPtr device, int what)
1705659607e0Smrg{
1706659607e0Smrg    InputInfoPtr pInfo;
1707659607e0Smrg    MouseDevPtr pMse;
1708659607e0Smrg    mousePrivPtr mPriv;
1709659607e0Smrg    unsigned char map[MSE_MAXBUTTONS + 1];
1710659607e0Smrg    int i;
1711659607e0Smrg
1712659607e0Smrg    pInfo = device->public.devicePrivate;
1713659607e0Smrg    pMse = pInfo->private;
1714659607e0Smrg    pMse->device = device;
1715659607e0Smrg
1716659607e0Smrg    switch (what)
1717659607e0Smrg    {
1718659607e0Smrg    case DEVICE_INIT:
1719659607e0Smrg	device->public.on = FALSE;
1720659607e0Smrg	/*
1721659607e0Smrg	 * [KAZU-241097] We don't know exactly how many buttons the
1722659607e0Smrg	 * device has, so setup the map with the maximum number.
1723659607e0Smrg	 */
1724659607e0Smrg	for (i = 0; i < MSE_MAXBUTTONS; i++)
1725659607e0Smrg	    map[i + 1] = i + 1;
1726659607e0Smrg
1727659607e0Smrg	InitPointerDeviceStruct((DevicePtr)device, map,
1728659607e0Smrg				min(pMse->buttons, MSE_MAXBUTTONS),
1729659607e0Smrg#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) == 0
1730659607e0Smrg				miPointerGetMotionEvents,
1731659607e0Smrg#else
1732659607e0Smrg                                GetMotionHistory,
1733659607e0Smrg#endif
1734659607e0Smrg                                pMse->Ctrl,
1735659607e0Smrg#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) == 0
1736659607e0Smrg				miPointerGetMotionBufferSize()
1737659607e0Smrg#else
1738659607e0Smrg                                GetMotionHistorySize(), 2
1739659607e0Smrg#endif
1740659607e0Smrg                                );
1741659607e0Smrg
1742659607e0Smrg	/* X valuator */
1743659607e0Smrg	xf86InitValuatorAxisStruct(device, 0, -1, -1, 1, 0, 1);
1744659607e0Smrg	xf86InitValuatorDefaults(device, 0);
1745659607e0Smrg	/* Y valuator */
1746659607e0Smrg	xf86InitValuatorAxisStruct(device, 1, -1, -1, 1, 0, 1);
1747659607e0Smrg	xf86InitValuatorDefaults(device, 1);
1748659607e0Smrg#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) == 0
1749659607e0Smrg	xf86MotionHistoryAllocate(pInfo);
1750659607e0Smrg#endif
1751659607e0Smrg
1752659607e0Smrg#ifdef EXTMOUSEDEBUG
1753659607e0Smrg	ErrorF("assigning %p atom=%d name=%s\n", device, pInfo->atom,
1754659607e0Smrg		pInfo->name);
1755659607e0Smrg#endif
1756659607e0Smrg	break;
1757659607e0Smrg
1758659607e0Smrg    case DEVICE_ON:
1759659607e0Smrg	pInfo->fd = xf86OpenSerial(pInfo->options);
1760659607e0Smrg	if (pInfo->fd == -1)
1761659607e0Smrg	    xf86Msg(X_WARNING, "%s: cannot open input device\n", pInfo->name);
1762659607e0Smrg	else {
1763659607e0Smrg	    if (pMse->xisbscale)
1764659607e0Smrg		pMse->buffer = XisbNew(pInfo->fd, pMse->xisbscale * 4);
1765659607e0Smrg	    else
1766659607e0Smrg		pMse->buffer = XisbNew(pInfo->fd, 64);
1767659607e0Smrg	    if (!pMse->buffer) {
1768659607e0Smrg		xf86CloseSerial(pInfo->fd);
1769659607e0Smrg		pInfo->fd = -1;
1770659607e0Smrg	    } else {
1771659607e0Smrg		if (!SetupMouse(pInfo)) {
1772659607e0Smrg		    xf86CloseSerial(pInfo->fd);
1773659607e0Smrg		    pInfo->fd = -1;
1774659607e0Smrg		    XisbFree(pMse->buffer);
1775659607e0Smrg		    pMse->buffer = NULL;
1776659607e0Smrg		} else {
1777659607e0Smrg		    mPriv = (mousePrivPtr)pMse->mousePriv;
1778659607e0Smrg		    if (mPriv != NULL) {
1779659607e0Smrg			if ( pMse->protocolID != PROT_AUTO) {
1780659607e0Smrg			    pMse->inSync = TRUE; /* @@@ */
1781659607e0Smrg			    if (mPriv->soft)
1782659607e0Smrg				mPriv->autoState = AUTOPROBE_GOOD;
1783659607e0Smrg			    else
1784659607e0Smrg				mPriv->autoState = AUTOPROBE_H_GOOD;
1785659607e0Smrg			} else {
1786659607e0Smrg			    if (mPriv->soft)
1787659607e0Smrg				mPriv->autoState = AUTOPROBE_NOPROTO;
1788659607e0Smrg			    else
1789659607e0Smrg				mPriv->autoState = AUTOPROBE_H_NOPROTO;
1790659607e0Smrg			}
1791659607e0Smrg		    }
1792659607e0Smrg		    xf86FlushInput(pInfo->fd);
1793659607e0Smrg		    xf86AddEnabledDevice(pInfo);
1794659607e0Smrg		}
1795659607e0Smrg	    }
1796659607e0Smrg	}
1797659607e0Smrg	pMse->lastButtons = 0;
1798659607e0Smrg	pMse->lastMappedButtons = 0;
1799659607e0Smrg	pMse->emulateState = 0;
1800659607e0Smrg	pMse->emulate3Pending = FALSE;
1801659607e0Smrg	pMse->wheelButtonExpires = GetTimeInMillis ();
1802659607e0Smrg	device->public.on = TRUE;
1803659607e0Smrg	FlushButtons(pMse);
1804659607e0Smrg	if (pMse->emulate3Buttons || pMse->emulate3ButtonsSoft)
1805659607e0Smrg	{
1806659607e0Smrg	    RegisterBlockAndWakeupHandlers (MouseBlockHandler, MouseWakeupHandler,
1807659607e0Smrg					    (pointer) pInfo);
1808659607e0Smrg	}
1809659607e0Smrg	break;
1810659607e0Smrg
1811659607e0Smrg    case DEVICE_OFF:
1812659607e0Smrg    case DEVICE_CLOSE:
1813659607e0Smrg	if (pInfo->fd != -1) {
1814659607e0Smrg	    xf86RemoveEnabledDevice(pInfo);
1815659607e0Smrg	    if (pMse->buffer) {
1816659607e0Smrg		XisbFree(pMse->buffer);
1817659607e0Smrg		pMse->buffer = NULL;
1818659607e0Smrg	    }
1819659607e0Smrg	    xf86CloseSerial(pInfo->fd);
1820659607e0Smrg	    pInfo->fd = -1;
1821659607e0Smrg	    if (pMse->emulate3Buttons || pMse->emulate3ButtonsSoft)
1822659607e0Smrg	    {
1823659607e0Smrg		RemoveBlockAndWakeupHandlers (MouseBlockHandler, MouseWakeupHandler,
1824659607e0Smrg					      (pointer) pInfo);
1825659607e0Smrg	    }
1826659607e0Smrg	}
1827659607e0Smrg	device->public.on = FALSE;
1828659607e0Smrg	break;
1829659607e0Smrg    }
1830659607e0Smrg    return Success;
1831659607e0Smrg}
1832659607e0Smrg
1833659607e0Smrg/*
1834659607e0Smrg ***************************************************************************
1835659607e0Smrg *
1836659607e0Smrg * MouseConvert --
1837659607e0Smrg *	Convert valuators to X and Y.
1838659607e0Smrg *
1839659607e0Smrg ***************************************************************************
1840659607e0Smrg */
1841659607e0Smrgstatic Bool
1842659607e0SmrgMouseConvert(InputInfoPtr pInfo, int first, int num, int v0, int v1, int v2,
1843659607e0Smrg	     int v3, int v4, int v5, int *x, int *y)
1844659607e0Smrg{
1845659607e0Smrg    if (first != 0 || num != 2)
1846659607e0Smrg	return FALSE;
1847659607e0Smrg
1848659607e0Smrg    *x = v0;
1849659607e0Smrg    *y = v1;
1850659607e0Smrg
1851659607e0Smrg    return TRUE;
1852659607e0Smrg}
1853659607e0Smrg
1854659607e0Smrg/**********************************************************************
1855659607e0Smrg *
1856659607e0Smrg * FlushButtons -- reset button states.
1857659607e0Smrg *
1858659607e0Smrg **********************************************************************/
1859659607e0Smrg
1860659607e0Smrgstatic void
1861659607e0SmrgFlushButtons(MouseDevPtr pMse)
1862659607e0Smrg{
1863659607e0Smrg    int i, blocked;
1864659607e0Smrg
1865659607e0Smrg    pMse->lastButtons = 0;
1866659607e0Smrg    pMse->lastMappedButtons = 0;
1867659607e0Smrg}
1868659607e0Smrg
1869659607e0Smrg/**********************************************************************
1870659607e0Smrg *
1871659607e0Smrg *  Emulate3Button support code
1872659607e0Smrg *
1873659607e0Smrg **********************************************************************/
1874659607e0Smrg
1875659607e0Smrg
1876659607e0Smrg/*
1877659607e0Smrg * Lets create a simple finite-state machine for 3 button emulation:
1878659607e0Smrg *
1879659607e0Smrg * We track buttons 1 and 3 (left and right).  There are 11 states:
1880659607e0Smrg *   0 ground           - initial state
1881659607e0Smrg *   1 delayed left     - left pressed, waiting for right
1882659607e0Smrg *   2 delayed right    - right pressed, waiting for left
1883659607e0Smrg *   3 pressed middle   - right and left pressed, emulated middle sent
1884659607e0Smrg *   4 pressed left     - left pressed and sent
1885659607e0Smrg *   5 pressed right    - right pressed and sent
1886659607e0Smrg *   6 released left    - left released after emulated middle
1887659607e0Smrg *   7 released right   - right released after emulated middle
1888659607e0Smrg *   8 repressed left   - left pressed after released left
1889659607e0Smrg *   9 repressed right  - right pressed after released right
1890659607e0Smrg *  10 pressed both     - both pressed, not emulating middle
1891659607e0Smrg *
1892659607e0Smrg * At each state, we need handlers for the following events
1893659607e0Smrg *   0: no buttons down
1894659607e0Smrg *   1: left button down
1895659607e0Smrg *   2: right button down
1896659607e0Smrg *   3: both buttons down
1897659607e0Smrg *   4: emulate3Timeout passed without a button change
1898659607e0Smrg * Note that button events are not deltas, they are the set of buttons being
1899659607e0Smrg * pressed now.  It's possible (ie, mouse hardware does it) to go from (eg)
1900659607e0Smrg * left down to right down without anything in between, so all cases must be
1901659607e0Smrg * handled.
1902659607e0Smrg *
1903659607e0Smrg * a handler consists of three values:
1904659607e0Smrg *   0: action1
1905659607e0Smrg *   1: action2
1906659607e0Smrg *   2: new emulation state
1907659607e0Smrg *
1908659607e0Smrg * action > 0: ButtonPress
1909659607e0Smrg * action = 0: nothing
1910659607e0Smrg * action < 0: ButtonRelease
1911659607e0Smrg *
1912659607e0Smrg * The comment preceeding each section is the current emulation state.
1913659607e0Smrg * The comments to the right are of the form
1914659607e0Smrg *      <button state> (<events>) -> <new emulation state>
1915659607e0Smrg * which should be read as
1916659607e0Smrg *      If the buttons are in <button state>, generate <events> then go to
1917659607e0Smrg *      <new emulation state>.
1918659607e0Smrg */
1919659607e0Smrgstatic signed char stateTab[11][5][3] = {
1920659607e0Smrg/* 0 ground */
1921659607e0Smrg  {
1922659607e0Smrg    {  0,  0,  0 },   /* nothing -> ground (no change) */
1923659607e0Smrg    {  0,  0,  1 },   /* left -> delayed left */
1924659607e0Smrg    {  0,  0,  2 },   /* right -> delayed right */
1925659607e0Smrg    {  2,  0,  3 },   /* left & right (middle press) -> pressed middle */
1926659607e0Smrg    {  0,  0, -1 }    /* timeout N/A */
1927659607e0Smrg  },
1928659607e0Smrg/* 1 delayed left */
1929659607e0Smrg  {
1930659607e0Smrg    {  1, -1,  0 },   /* nothing (left event) -> ground */
1931659607e0Smrg    {  0,  0,  1 },   /* left -> delayed left (no change) */
1932659607e0Smrg    {  1, -1,  2 },   /* right (left event) -> delayed right */
1933659607e0Smrg    {  2,  0,  3 },   /* left & right (middle press) -> pressed middle */
1934659607e0Smrg    {  1,  0,  4 },   /* timeout (left press) -> pressed left */
1935659607e0Smrg  },
1936659607e0Smrg/* 2 delayed right */
1937659607e0Smrg  {
1938659607e0Smrg    {  3, -3,  0 },   /* nothing (right event) -> ground */
1939659607e0Smrg    {  3, -3,  1 },   /* left (right event) -> delayed left (no change) */
1940659607e0Smrg    {  0,  0,  2 },   /* right -> delayed right (no change) */
1941659607e0Smrg    {  2,  0,  3 },   /* left & right (middle press) -> pressed middle */
1942659607e0Smrg    {  3,  0,  5 },   /* timeout (right press) -> pressed right */
1943659607e0Smrg  },
1944659607e0Smrg/* 3 pressed middle */
1945659607e0Smrg  {
1946659607e0Smrg    { -2,  0,  0 },   /* nothing (middle release) -> ground */
1947659607e0Smrg    {  0,  0,  7 },   /* left -> released right */
1948659607e0Smrg    {  0,  0,  6 },   /* right -> released left */
1949659607e0Smrg    {  0,  0,  3 },   /* left & right -> pressed middle (no change) */
1950659607e0Smrg    {  0,  0, -1 },   /* timeout N/A */
1951659607e0Smrg  },
1952659607e0Smrg/* 4 pressed left */
1953659607e0Smrg  {
1954659607e0Smrg    { -1,  0,  0 },   /* nothing (left release) -> ground */
1955659607e0Smrg    {  0,  0,  4 },   /* left -> pressed left (no change) */
1956659607e0Smrg    { -1,  0,  2 },   /* right (left release) -> delayed right */
1957659607e0Smrg    {  3,  0, 10 },   /* left & right (right press) -> pressed both */
1958659607e0Smrg    {  0,  0, -1 },   /* timeout N/A */
1959659607e0Smrg  },
1960659607e0Smrg/* 5 pressed right */
1961659607e0Smrg  {
1962659607e0Smrg    { -3,  0,  0 },   /* nothing (right release) -> ground */
1963659607e0Smrg    { -3,  0,  1 },   /* left (right release) -> delayed left */
1964659607e0Smrg    {  0,  0,  5 },   /* right -> pressed right (no change) */
1965659607e0Smrg    {  1,  0, 10 },   /* left & right (left press) -> pressed both */
1966659607e0Smrg    {  0,  0, -1 },   /* timeout N/A */
1967659607e0Smrg  },
1968659607e0Smrg/* 6 released left */
1969659607e0Smrg  {
1970659607e0Smrg    { -2,  0,  0 },   /* nothing (middle release) -> ground */
1971659607e0Smrg    { -2,  0,  1 },   /* left (middle release) -> delayed left */
1972659607e0Smrg    {  0,  0,  6 },   /* right -> released left (no change) */
1973659607e0Smrg    {  1,  0,  8 },   /* left & right (left press) -> repressed left */
1974659607e0Smrg    {  0,  0, -1 },   /* timeout N/A */
1975659607e0Smrg  },
1976659607e0Smrg/* 7 released right */
1977659607e0Smrg  {
1978659607e0Smrg    { -2,  0,  0 },   /* nothing (middle release) -> ground */
1979659607e0Smrg    {  0,  0,  7 },   /* left -> released right (no change) */
1980659607e0Smrg    { -2,  0,  2 },   /* right (middle release) -> delayed right */
1981659607e0Smrg    {  3,  0,  9 },   /* left & right (right press) -> repressed right */
1982659607e0Smrg    {  0,  0, -1 },   /* timeout N/A */
1983659607e0Smrg  },
1984659607e0Smrg/* 8 repressed left */
1985659607e0Smrg  {
1986659607e0Smrg    { -2, -1,  0 },   /* nothing (middle release, left release) -> ground */
1987659607e0Smrg    { -2,  0,  4 },   /* left (middle release) -> pressed left */
1988659607e0Smrg    { -1,  0,  6 },   /* right (left release) -> released left */
1989659607e0Smrg    {  0,  0,  8 },   /* left & right -> repressed left (no change) */
1990659607e0Smrg    {  0,  0, -1 },   /* timeout N/A */
1991659607e0Smrg  },
1992659607e0Smrg/* 9 repressed right */
1993659607e0Smrg  {
1994659607e0Smrg    { -2, -3,  0 },   /* nothing (middle release, right release) -> ground */
1995659607e0Smrg    { -3,  0,  7 },   /* left (right release) -> released right */
1996659607e0Smrg    { -2,  0,  5 },   /* right (middle release) -> pressed right */
1997659607e0Smrg    {  0,  0,  9 },   /* left & right -> repressed right (no change) */
1998659607e0Smrg    {  0,  0, -1 },   /* timeout N/A */
1999659607e0Smrg  },
2000659607e0Smrg/* 10 pressed both */
2001659607e0Smrg  {
2002659607e0Smrg    { -1, -3,  0 },   /* nothing (left release, right release) -> ground */
2003659607e0Smrg    { -3,  0,  4 },   /* left (right release) -> pressed left */
2004659607e0Smrg    { -1,  0,  5 },   /* right (left release) -> pressed right */
2005659607e0Smrg    {  0,  0, 10 },   /* left & right -> pressed both (no change) */
2006659607e0Smrg    {  0,  0, -1 },   /* timeout N/A */
2007659607e0Smrg  },
2008659607e0Smrg};
2009659607e0Smrg
2010659607e0Smrg/*
2011659607e0Smrg * Table to allow quick reversal of natural button mapping to correct mapping
2012659607e0Smrg */
2013659607e0Smrg
2014659607e0Smrg/*
2015659607e0Smrg * [JCH-96/01/21] The ALPS GlidePoint pad extends the MS protocol
2016659607e0Smrg * with a fourth button activated by tapping the PAD.
2017659607e0Smrg * The 2nd line corresponds to 4th button on; the drv sends
2018659607e0Smrg * the buttons in the following map (MSBit described first) :
2019659607e0Smrg * 0 | 4th | 1st | 2nd | 3rd
2020659607e0Smrg * And we remap them (MSBit described first) :
2021659607e0Smrg * 0 | 4th | 3rd | 2nd | 1st
2022659607e0Smrg */
2023659607e0Smrgstatic char reverseMap[16] = { 0,  4,  2,  6,
2024659607e0Smrg			       1,  5,  3,  7,
2025659607e0Smrg			       8, 12, 10, 14,
2026659607e0Smrg			       9, 13, 11, 15 };
2027659607e0Smrg
2028659607e0Smrgstatic char hitachMap[16] = {  0,  2,  1,  3,
2029659607e0Smrg			       8, 10,  9, 11,
2030659607e0Smrg			       4,  6,  5,  7,
2031659607e0Smrg			      12, 14, 13, 15 };
2032659607e0Smrg
2033659607e0Smrg#define reverseBits(map, b)	(((b) & ~0x0f) | map[(b) & 0x0f])
2034659607e0Smrg
2035659607e0Smrgstatic CARD32
2036659607e0SmrgbuttonTimer(InputInfoPtr pInfo)
2037659607e0Smrg{
2038659607e0Smrg    MouseDevPtr pMse;
2039659607e0Smrg    int	sigstate;
2040659607e0Smrg    int id;
2041659607e0Smrg
2042659607e0Smrg    pMse = pInfo->private;
2043659607e0Smrg
2044659607e0Smrg    sigstate = xf86BlockSIGIO ();
2045659607e0Smrg
2046659607e0Smrg    pMse->emulate3Pending = FALSE;
2047659607e0Smrg    if ((id = stateTab[pMse->emulateState][4][0]) != 0) {
2048659607e0Smrg        xf86PostButtonEvent(pInfo->dev, 0, abs(id), (id >= 0), 0, 0);
2049659607e0Smrg        pMse->emulateState = stateTab[pMse->emulateState][4][2];
2050659607e0Smrg    } else {
2051659607e0Smrg        ErrorF("Got unexpected buttonTimer in state %d\n", pMse->emulateState);
2052659607e0Smrg    }
2053659607e0Smrg
2054659607e0Smrg    xf86UnblockSIGIO (sigstate);
2055659607e0Smrg    return 0;
2056659607e0Smrg}
2057659607e0Smrg
2058659607e0Smrgstatic Bool
2059659607e0SmrgEmulate3ButtonsSoft(InputInfoPtr pInfo)
2060659607e0Smrg{
2061659607e0Smrg    MouseDevPtr pMse = pInfo->private;
2062659607e0Smrg
2063659607e0Smrg    if (!pMse->emulate3ButtonsSoft)
2064659607e0Smrg	return TRUE;
2065659607e0Smrg
2066659607e0Smrg    pMse->emulate3Buttons = FALSE;
2067659607e0Smrg
2068659607e0Smrg    if (pMse->emulate3Pending)
2069659607e0Smrg	buttonTimer(pInfo);
2070659607e0Smrg
2071659607e0Smrg    xf86Msg(X_INFO,"3rd Button detected: disabling emulate3Button\n");
2072659607e0Smrg
2073659607e0Smrg    return FALSE;
2074659607e0Smrg}
2075659607e0Smrg
2076659607e0Smrgstatic void MouseBlockHandler(pointer data,
2077659607e0Smrg			      struct timeval **waitTime,
2078659607e0Smrg			      pointer LastSelectMask)
2079659607e0Smrg{
2080659607e0Smrg    InputInfoPtr    pInfo = (InputInfoPtr) data;
2081659607e0Smrg    MouseDevPtr	    pMse = (MouseDevPtr) pInfo->private;
2082659607e0Smrg    int		    ms;
2083659607e0Smrg
2084659607e0Smrg    if (pMse->emulate3Pending)
2085659607e0Smrg    {
2086659607e0Smrg	ms = pMse->emulate3Expires - GetTimeInMillis ();
2087659607e0Smrg	if (ms <= 0)
2088659607e0Smrg	    ms = 0;
2089659607e0Smrg	AdjustWaitForDelay (waitTime, ms);
2090659607e0Smrg    }
2091659607e0Smrg}
2092659607e0Smrg
2093659607e0Smrgstatic void MouseWakeupHandler(pointer data,
2094659607e0Smrg			       int i,
2095659607e0Smrg			       pointer LastSelectMask)
2096659607e0Smrg{
2097659607e0Smrg    InputInfoPtr    pInfo = (InputInfoPtr) data;
2098659607e0Smrg    MouseDevPtr	    pMse = (MouseDevPtr) pInfo->private;
2099659607e0Smrg    int		    ms;
2100659607e0Smrg
2101659607e0Smrg    if (pMse->emulate3Pending)
2102659607e0Smrg    {
2103659607e0Smrg	ms = pMse->emulate3Expires - GetTimeInMillis ();
2104659607e0Smrg	if (ms <= 0)
2105659607e0Smrg	    buttonTimer (pInfo);
2106659607e0Smrg    }
2107659607e0Smrg}
2108659607e0Smrg
2109659607e0Smrg/*******************************************************************
2110659607e0Smrg *
2111659607e0Smrg * Post mouse events
2112659607e0Smrg *
2113659607e0Smrg *******************************************************************/
2114659607e0Smrg
2115659607e0Smrgstatic void
2116659607e0SmrgMouseDoPostEvent(InputInfoPtr pInfo, int buttons, int dx, int dy)
2117659607e0Smrg{
2118659607e0Smrg    MouseDevPtr pMse;
2119659607e0Smrg    int emulateButtons;
2120659607e0Smrg    int id, change;
2121659607e0Smrg    int emuWheelDelta, emuWheelButton, emuWheelButtonMask;
2122659607e0Smrg    int wheelButtonMask;
2123659607e0Smrg    int ms;
2124659607e0Smrg
2125659607e0Smrg    pMse = pInfo->private;
2126659607e0Smrg
2127659607e0Smrg    change = buttons ^ pMse->lastMappedButtons;
2128659607e0Smrg    pMse->lastMappedButtons = buttons;
2129659607e0Smrg
2130659607e0Smrg    /* Do single button double click */
2131659607e0Smrg    if (pMse->doubleClickSourceButtonMask) {
2132659607e0Smrg        if (buttons & pMse->doubleClickSourceButtonMask) {
2133659607e0Smrg            if (!(pMse->doubleClickOldSourceState)) {
2134659607e0Smrg                /* double-click button has just been pressed. Ignore it if target button
2135659607e0Smrg                 * is already down.
2136659607e0Smrg                 */
2137659607e0Smrg                if (!(buttons & pMse->doubleClickTargetButtonMask)) {
2138659607e0Smrg                    /* Target button isn't down, so send a double-click */
2139659607e0Smrg                    xf86PostButtonEvent(pInfo->dev, 0, pMse->doubleClickTargetButton, 1, 0, 0);
2140659607e0Smrg                    xf86PostButtonEvent(pInfo->dev, 0, pMse->doubleClickTargetButton, 0, 0, 0);
2141659607e0Smrg                    xf86PostButtonEvent(pInfo->dev, 0, pMse->doubleClickTargetButton, 1, 0, 0);
2142659607e0Smrg                    xf86PostButtonEvent(pInfo->dev, 0, pMse->doubleClickTargetButton, 0, 0, 0);
2143659607e0Smrg                }
2144659607e0Smrg            }
2145659607e0Smrg            pMse->doubleClickOldSourceState = 1;
2146659607e0Smrg        }
2147659607e0Smrg        else
2148659607e0Smrg            pMse->doubleClickOldSourceState = 0;
2149659607e0Smrg
2150659607e0Smrg        /* Whatever happened, mask the double-click button so it doesn't get
2151659607e0Smrg         * processed as a normal button as well.
2152659607e0Smrg         */
2153659607e0Smrg        buttons &= ~(pMse->doubleClickSourceButtonMask);
2154659607e0Smrg        change  &= ~(pMse->doubleClickSourceButtonMask);
2155659607e0Smrg    }
2156659607e0Smrg
2157659607e0Smrg    if (pMse->emulateWheel) {
2158659607e0Smrg	/* Emulate wheel button handling */
2159659607e0Smrg	wheelButtonMask = 1 << (pMse->wheelButton - 1);
2160659607e0Smrg
2161659607e0Smrg	if (change & wheelButtonMask) {
2162659607e0Smrg	    if (buttons & wheelButtonMask) {
2163659607e0Smrg		/* Start timeout handling */
2164659607e0Smrg		pMse->wheelButtonExpires = GetTimeInMillis () + pMse->wheelButtonTimeout;
2165659607e0Smrg		ms = - pMse->wheelButtonTimeout;
2166659607e0Smrg	    } else {
2167659607e0Smrg		ms = pMse->wheelButtonExpires - GetTimeInMillis ();
2168659607e0Smrg
2169659607e0Smrg		if (0 < ms) {
2170659607e0Smrg		    /*
2171659607e0Smrg		     * If the button is released early enough emit the button
2172659607e0Smrg		     * press/release events
2173659607e0Smrg		     */
2174659607e0Smrg		    xf86PostButtonEvent(pInfo->dev, 0, pMse->wheelButton, 1, 0, 0);
2175659607e0Smrg		    xf86PostButtonEvent(pInfo->dev, 0, pMse->wheelButton, 0, 0, 0);
2176659607e0Smrg		}
2177659607e0Smrg	    }
2178659607e0Smrg	} else
2179659607e0Smrg	    ms = pMse->wheelButtonExpires - GetTimeInMillis ();
2180659607e0Smrg
2181659607e0Smrg	/* Intercept wheel emulation. */
2182659607e0Smrg	if (buttons & wheelButtonMask) {
2183659607e0Smrg	    if (ms <= 0) {
2184659607e0Smrg		/* Y axis movement */
2185659607e0Smrg		if (pMse->negativeY != MSE_NOAXISMAP) {
2186659607e0Smrg		    pMse->wheelYDistance += dy;
2187659607e0Smrg		    if (pMse->wheelYDistance < 0) {
2188659607e0Smrg			emuWheelDelta = -pMse->wheelInertia;
2189659607e0Smrg			emuWheelButton = pMse->negativeY;
2190659607e0Smrg		    } else {
2191659607e0Smrg			emuWheelDelta = pMse->wheelInertia;
2192659607e0Smrg			emuWheelButton = pMse->positiveY;
2193659607e0Smrg		    }
2194659607e0Smrg		    emuWheelButtonMask = 1 << (emuWheelButton - 1);
2195659607e0Smrg		    while (abs(pMse->wheelYDistance) > pMse->wheelInertia) {
2196659607e0Smrg			pMse->wheelYDistance -= emuWheelDelta;
2197659607e0Smrg
2198659607e0Smrg			/*
2199659607e0Smrg			 * Synthesize the press and release, but not when
2200659607e0Smrg			 * the button to be synthesized is already pressed
2201659607e0Smrg			 * "for real".
2202659607e0Smrg			 */
2203659607e0Smrg			if (!(emuWheelButtonMask & buttons) ||
2204659607e0Smrg			    (emuWheelButtonMask & wheelButtonMask)) {
2205659607e0Smrg			    xf86PostButtonEvent(pInfo->dev, 0, emuWheelButton, 1, 0, 0);
2206659607e0Smrg			    xf86PostButtonEvent(pInfo->dev, 0, emuWheelButton, 0, 0, 0);
2207659607e0Smrg			}
2208659607e0Smrg		    }
2209659607e0Smrg		}
2210659607e0Smrg
2211659607e0Smrg		/* X axis movement */
2212659607e0Smrg		if (pMse->negativeX != MSE_NOAXISMAP) {
2213659607e0Smrg		    pMse->wheelXDistance += dx;
2214659607e0Smrg		    if (pMse->wheelXDistance < 0) {
2215659607e0Smrg			emuWheelDelta = -pMse->wheelInertia;
2216659607e0Smrg			emuWheelButton = pMse->negativeX;
2217659607e0Smrg		    } else {
2218659607e0Smrg			emuWheelDelta = pMse->wheelInertia;
2219659607e0Smrg			emuWheelButton = pMse->positiveX;
2220659607e0Smrg		    }
2221659607e0Smrg		    emuWheelButtonMask = 1 << (emuWheelButton - 1);
2222659607e0Smrg		    while (abs(pMse->wheelXDistance) > pMse->wheelInertia) {
2223659607e0Smrg			pMse->wheelXDistance -= emuWheelDelta;
2224659607e0Smrg
2225659607e0Smrg			/*
2226659607e0Smrg			 * Synthesize the press and release, but not when
2227659607e0Smrg			 * the button to be synthesized is already pressed
2228659607e0Smrg			 * "for real".
2229659607e0Smrg			 */
2230659607e0Smrg			if (!(emuWheelButtonMask & buttons) ||
2231659607e0Smrg			    (emuWheelButtonMask & wheelButtonMask)) {
2232659607e0Smrg			    xf86PostButtonEvent(pInfo->dev, 0, emuWheelButton, 1, 0, 0);
2233659607e0Smrg			    xf86PostButtonEvent(pInfo->dev, 0, emuWheelButton, 0, 0, 0);
2234659607e0Smrg			}
2235659607e0Smrg		    }
2236659607e0Smrg		}
2237659607e0Smrg	    }
2238659607e0Smrg
2239659607e0Smrg	    /* Absorb the mouse movement while the wheel button is pressed. */
2240659607e0Smrg	    dx = 0;
2241659607e0Smrg	    dy = 0;
2242659607e0Smrg	}
2243659607e0Smrg	/*
2244659607e0Smrg	 * Button events for the wheel button are only emitted through
2245659607e0Smrg	 * the timeout code.
2246659607e0Smrg	 */
2247659607e0Smrg	buttons &= ~wheelButtonMask;
2248659607e0Smrg	change  &= ~wheelButtonMask;
2249659607e0Smrg    }
2250659607e0Smrg
2251659607e0Smrg    if (pMse->emulate3ButtonsSoft && pMse->emulate3Pending && (dx || dy))
2252659607e0Smrg	buttonTimer(pInfo);
2253659607e0Smrg
2254659607e0Smrg    if (dx || dy)
2255659607e0Smrg	xf86PostMotionEvent(pInfo->dev, 0, 0, 2, dx, dy);
2256659607e0Smrg
2257659607e0Smrg    if (change) {
2258659607e0Smrg
2259659607e0Smrg	/*
2260659607e0Smrg	 * adjust buttons state for drag locks!
2261659607e0Smrg	 * if there is drag locks
2262659607e0Smrg	 */
2263659607e0Smrg        if (pMse->pDragLock) {
2264659607e0Smrg	    DragLockPtr   pLock;
2265659607e0Smrg	    int tarOfGoingDown, tarOfDown;
2266659607e0Smrg	    int realbuttons;
2267659607e0Smrg
2268659607e0Smrg	    /* get drag lock block */
2269659607e0Smrg	    pLock = pMse->pDragLock;
2270659607e0Smrg	    /* save real buttons */
2271659607e0Smrg	    realbuttons = buttons;
2272659607e0Smrg
2273659607e0Smrg	    /* if drag lock used */
2274659607e0Smrg
2275659607e0Smrg	    /* state of drag lock buttons not seen always up */
2276659607e0Smrg
2277659607e0Smrg	    buttons &= ~pLock->lockButtonsM;
2278659607e0Smrg
2279659607e0Smrg	    /*
2280659607e0Smrg	     * if lock buttons being depressed changes state of
2281659607e0Smrg	     * targets simulatedDown.
2282659607e0Smrg	     */
2283659607e0Smrg	    tarOfGoingDown = lock2targetMap(pLock,
2284659607e0Smrg				realbuttons & change & pLock->lockButtonsM);
2285659607e0Smrg	    pLock->simulatedDown ^= tarOfGoingDown;
2286659607e0Smrg
2287659607e0Smrg	    /* targets of drag locks down */
2288659607e0Smrg	    tarOfDown = lock2targetMap(pLock,
2289659607e0Smrg				realbuttons & pLock->lockButtonsM);
2290659607e0Smrg
2291659607e0Smrg	    /*
2292659607e0Smrg	     * when simulatedDown set and target pressed,
2293659607e0Smrg	     * simulatedDown goes false
2294659607e0Smrg	     */
2295659607e0Smrg	    pLock->simulatedDown &= ~(realbuttons & change);
2296659607e0Smrg
2297659607e0Smrg	    /*
2298659607e0Smrg	     * if master drag lock released
2299659607e0Smrg	     * then master drag lock state on
2300659607e0Smrg	     */
2301659607e0Smrg	    pLock->masterTS |= (~realbuttons & change) & pLock->masterLockM;
2302659607e0Smrg
2303659607e0Smrg	    /* if master state, buttons going down are simulatedDown */
2304659607e0Smrg	    if (pLock->masterTS)
2305659607e0Smrg		pLock->simulatedDown |= (realbuttons & change);
2306659607e0Smrg
2307659607e0Smrg	    /* if any button pressed, no longer in master drag lock state */
2308659607e0Smrg	    if (realbuttons & change)
2309659607e0Smrg		pLock->masterTS = 0;
2310659607e0Smrg
2311659607e0Smrg	    /* if simulatedDown or drag lock down, simulate down */
2312659607e0Smrg	    buttons |= (pLock->simulatedDown | tarOfDown);
2313659607e0Smrg
2314659607e0Smrg	    /* master button not seen */
2315659607e0Smrg	    buttons &= ~(pLock->masterLockM);
2316659607e0Smrg
2317659607e0Smrg	    /* buttons changed since last time */
2318659607e0Smrg	    change = buttons ^ pLock->lockLastButtons;
2319659607e0Smrg
2320659607e0Smrg	    /* save this time for next last time. */
2321659607e0Smrg	    pLock->lockLastButtons = buttons;
2322659607e0Smrg	}
2323659607e0Smrg
2324659607e0Smrg        if (pMse->emulate3Buttons
2325659607e0Smrg	    && (!(buttons & 0x02) || Emulate3ButtonsSoft(pInfo))) {
2326659607e0Smrg
2327659607e0Smrg            /* handle all but buttons 1 & 3 normally */
2328659607e0Smrg
2329659607e0Smrg            change &= ~05;
2330659607e0Smrg
2331659607e0Smrg            /* emulate the third button by the other two */
2332659607e0Smrg
2333659607e0Smrg            emulateButtons = (buttons & 01) | ((buttons &04) >> 1);
2334659607e0Smrg
2335659607e0Smrg            if ((id = stateTab[pMse->emulateState][emulateButtons][0]) != 0)
2336659607e0Smrg                xf86PostButtonEvent(pInfo->dev, 0, abs(id), (id >= 0), 0, 0);
2337659607e0Smrg            if ((id = stateTab[pMse->emulateState][emulateButtons][1]) != 0)
2338659607e0Smrg                xf86PostButtonEvent(pInfo->dev, 0, abs(id), (id >= 0), 0, 0);
2339659607e0Smrg
2340659607e0Smrg            pMse->emulateState =
2341659607e0Smrg                stateTab[pMse->emulateState][emulateButtons][2];
2342659607e0Smrg
2343659607e0Smrg            if (stateTab[pMse->emulateState][4][0] != 0) {
2344659607e0Smrg		pMse->emulate3Expires = GetTimeInMillis () + pMse->emulate3Timeout;
2345659607e0Smrg		pMse->emulate3Pending = TRUE;
2346659607e0Smrg            } else {
2347659607e0Smrg		pMse->emulate3Pending = FALSE;
2348659607e0Smrg            }
2349659607e0Smrg        }
2350659607e0Smrg
2351659607e0Smrg	while (change) {
2352659607e0Smrg	    id = ffs(change);
2353659607e0Smrg	    change &= ~(1 << (id - 1));
2354659607e0Smrg	    xf86PostButtonEvent(pInfo->dev, 0, id,
2355659607e0Smrg				(buttons & (1 << (id - 1))), 0, 0);
2356659607e0Smrg	}
2357659607e0Smrg
2358659607e0Smrg    }
2359659607e0Smrg}
2360659607e0Smrg
2361659607e0Smrgstatic void
2362659607e0SmrgMousePostEvent(InputInfoPtr pInfo, int truebuttons,
2363659607e0Smrg	       int dx, int dy, int dz, int dw)
2364659607e0Smrg{
2365659607e0Smrg    MouseDevPtr pMse;
2366659607e0Smrg    mousePrivPtr mousepriv;
2367659607e0Smrg    int zbutton = 0, wbutton = 0, zbuttoncount = 0, wbuttoncount = 0;
2368659607e0Smrg    int i, b, buttons = 0;
2369659607e0Smrg
2370659607e0Smrg    pMse = pInfo->private;
2371659607e0Smrg    mousepriv = (mousePrivPtr)pMse->mousePriv;
2372659607e0Smrg
2373659607e0Smrg    if (pMse->protocolID == PROT_MMHIT)
2374659607e0Smrg	b = reverseBits(hitachMap, truebuttons);
2375659607e0Smrg    else
2376659607e0Smrg	b = reverseBits(reverseMap, truebuttons);
2377659607e0Smrg
2378659607e0Smrg    /* Remap mouse buttons */
2379659607e0Smrg    b &= (1<<MSE_MAXBUTTONS)-1;
2380659607e0Smrg    for (i = 0; b; i++) {
2381659607e0Smrg       if (b & 1)
2382659607e0Smrg	   buttons |= pMse->buttonMap[i];
2383659607e0Smrg       b >>= 1;
2384659607e0Smrg    }
2385659607e0Smrg
2386659607e0Smrg    /* Map the Z axis movement. */
2387659607e0Smrg    /* XXX Could this go in the conversion_proc? */
2388659607e0Smrg    switch (pMse->negativeZ) {
2389659607e0Smrg    case MSE_NOZMAP:	/* do nothing */
2390659607e0Smrg	dz = 0;
2391659607e0Smrg	break;
2392659607e0Smrg    case MSE_MAPTOX:
2393659607e0Smrg	if (dz != 0) {
2394659607e0Smrg	    dx = dz;
2395659607e0Smrg	    dz = 0;
2396659607e0Smrg	}
2397659607e0Smrg	break;
2398659607e0Smrg    case MSE_MAPTOY:
2399659607e0Smrg	if (dz != 0) {
2400659607e0Smrg	    dy = dz;
2401659607e0Smrg	    dz = 0;
2402659607e0Smrg	}
2403659607e0Smrg	break;
2404659607e0Smrg    default:	/* buttons */
2405659607e0Smrg	buttons &= ~(pMse->negativeZ | pMse->positiveZ);
2406659607e0Smrg	if (dz < 0) {
2407659607e0Smrg	    zbutton = pMse->negativeZ;
2408659607e0Smrg	    zbuttoncount = -dz;
2409659607e0Smrg	} else if (dz > 0) {
2410659607e0Smrg	    zbutton = pMse->positiveZ;
2411659607e0Smrg	    zbuttoncount = dz;
2412659607e0Smrg	}
2413659607e0Smrg	dz = 0;
2414659607e0Smrg	break;
2415659607e0Smrg    }
2416659607e0Smrg    switch (pMse->negativeW) {
2417659607e0Smrg    case MSE_NOZMAP:	/* do nothing */
2418659607e0Smrg	dw = 0;
2419659607e0Smrg	break;
2420659607e0Smrg    case MSE_MAPTOX:
2421659607e0Smrg	if (dw != 0) {
2422659607e0Smrg	    dx = dw;
2423659607e0Smrg	    dw = 0;
2424659607e0Smrg	}
2425659607e0Smrg	break;
2426659607e0Smrg    case MSE_MAPTOY:
2427659607e0Smrg	if (dw != 0) {
2428659607e0Smrg	    dy = dw;
2429659607e0Smrg	    dw = 0;
2430659607e0Smrg	}
2431659607e0Smrg	break;
2432659607e0Smrg    default:	/* buttons */
2433659607e0Smrg	buttons &= ~(pMse->negativeW | pMse->positiveW);
2434659607e0Smrg	if (dw < 0) {
2435659607e0Smrg	    wbutton = pMse->negativeW;
2436659607e0Smrg	    wbuttoncount = -dw;
2437659607e0Smrg	} else if (dw > 0) {
2438659607e0Smrg	    wbutton = pMse->positiveW;
2439659607e0Smrg	    wbuttoncount = dw;
2440659607e0Smrg	}
2441659607e0Smrg	dw = 0;
2442659607e0Smrg	break;
2443659607e0Smrg    }
2444659607e0Smrg
2445659607e0Smrg
2446659607e0Smrg    /* Apply angle offset */
2447659607e0Smrg    if (pMse->angleOffset != 0) {
2448659607e0Smrg	double rad = 3.141592653 * pMse->angleOffset / 180.0;
2449659607e0Smrg	int ndx = dx;
2450659607e0Smrg	dx = (int)((dx * cos(rad)) + (dy * sin(rad)) + 0.5);
2451659607e0Smrg	dy = (int)((dy * cos(rad)) - (ndx * sin(rad)) + 0.5);
2452659607e0Smrg    }
2453659607e0Smrg
2454659607e0Smrg    dx = pMse->invX * dx;
2455659607e0Smrg    dy = pMse->invY * dy;
2456659607e0Smrg    if (pMse->flipXY) {
2457659607e0Smrg	int tmp = dx;
2458659607e0Smrg	dx = dy;
2459659607e0Smrg	dy = tmp;
2460659607e0Smrg    }
2461659607e0Smrg
2462659607e0Smrg    /* Accumulate the scaled dx, dy in the private variables
2463659607e0Smrg       fracdx,fracdy and return the integer number part */
2464659607e0Smrg    if (mousepriv) {
2465659607e0Smrg	mousepriv->fracdx += mousepriv->sensitivity*dx;
2466659607e0Smrg	mousepriv->fracdy += mousepriv->sensitivity*dy;
2467659607e0Smrg	mousepriv->fracdx -= ( dx=(int)(mousepriv->fracdx) );
2468659607e0Smrg	mousepriv->fracdy -= ( dy=(int)(mousepriv->fracdy) );
2469659607e0Smrg    }
2470659607e0Smrg
2471659607e0Smrg    /* If mouse wheel movement has to be mapped on a button, we need to
2472659607e0Smrg     * loop for button press and release events. */
2473659607e0Smrg    do {
2474659607e0Smrg        MouseDoPostEvent(pInfo, buttons | zbutton | wbutton, dx, dy);
2475659607e0Smrg	dx = dy = 0;
2476659607e0Smrg	if (zbutton || wbutton)
2477659607e0Smrg	    MouseDoPostEvent(pInfo, buttons, 0, 0);
2478659607e0Smrg	if (--zbuttoncount <= 0)
2479659607e0Smrg	    zbutton = 0;
2480659607e0Smrg	if (--wbuttoncount <= 0)
2481659607e0Smrg	    wbutton = 0;
2482659607e0Smrg    } while (zbutton || wbutton);
2483659607e0Smrg
2484659607e0Smrg    pMse->lastButtons = truebuttons;
2485659607e0Smrg}
2486659607e0Smrg/******************************************************************
2487659607e0Smrg *
2488659607e0Smrg * Mouse Setup Code
2489659607e0Smrg *
2490659607e0Smrg ******************************************************************/
2491659607e0Smrg/*
2492659607e0Smrg * This array is indexed by the MouseProtocolID values, so the order of the
2493659607e0Smrg * entries must match that of the MouseProtocolID enum in xf86OSmouse.h.
2494659607e0Smrg */
2495659607e0Smrgstatic unsigned char proto[PROT_NUMPROTOS][8] = {
2496659607e0Smrg  /* --header--  ---data--- packet -4th-byte-  mouse   */
2497659607e0Smrg  /* mask   id   mask   id  bytes  mask   id   flags   */
2498659607e0Smrg							    /* Serial mice */
2499659607e0Smrg  {  0x40, 0x40, 0x40, 0x00,  3,  ~0x23, 0x00, MPF_NONE },  /* MicroSoft */
2500659607e0Smrg  {  0xf8, 0x80, 0x00, 0x00,  5,   0x00, 0xff, MPF_SAFE },  /* MouseSystems */
2501659607e0Smrg  {  0xe0, 0x80, 0x80, 0x00,  3,   0x00, 0xff, MPF_NONE },  /* MMSeries */
2502659607e0Smrg  {  0xe0, 0x80, 0x80, 0x00,  3,   0x00, 0xff, MPF_NONE },  /* Logitech */
2503659607e0Smrg  {  0x40, 0x40, 0x40, 0x00,  3,  ~0x23, 0x00, MPF_NONE },  /* MouseMan */
2504659607e0Smrg  {  0xe0, 0x80, 0x80, 0x00,  3,   0x00, 0xff, MPF_NONE },  /* MM_HitTablet */
2505659607e0Smrg  {  0x40, 0x40, 0x40, 0x00,  3,  ~0x33, 0x00, MPF_NONE },  /* GlidePoint */
2506659607e0Smrg  {  0x40, 0x40, 0x40, 0x00,  3,  ~0x3f, 0x00, MPF_NONE },  /* IntelliMouse */
2507659607e0Smrg  {  0x40, 0x40, 0x40, 0x00,  3,  ~0x33, 0x00, MPF_NONE },  /* ThinkingMouse */
2508659607e0Smrg  {  0x80, 0x80, 0x80, 0x00,  3,   0x00, 0xff, MPF_NONE },  /* ACECAD */
2509659607e0Smrg  {  0x40, 0x40, 0x40, 0x00,  4,   0x00, 0xff, MPF_NONE },  /* ValuMouseScroll */
2510659607e0Smrg							    /* PS/2 variants */
2511659607e0Smrg  {  0xc0, 0x00, 0x00, 0x00,  3,   0x00, 0xff, MPF_NONE },  /* PS/2 mouse */
2512659607e0Smrg  {  0xc8, 0x08, 0x00, 0x00,  3,   0x00, 0x00, MPF_NONE },  /* genericPS/2 mouse*/
2513659607e0Smrg  {  0x08, 0x08, 0x00, 0x00,  4,   0x00, 0xff, MPF_NONE },  /* IntelliMouse */
2514659607e0Smrg  {  0x08, 0x08, 0x00, 0x00,  4,   0x00, 0xff, MPF_NONE },  /* Explorer */
2515659607e0Smrg  {  0x80, 0x80, 0x00, 0x00,  3,   0x00, 0xff, MPF_NONE },  /* ThinkingMouse */
2516659607e0Smrg  {  0x08, 0x08, 0x00, 0x00,  3,   0x00, 0xff, MPF_NONE },  /* MouseMan+ */
2517659607e0Smrg  {  0xc0, 0x00, 0x00, 0x00,  3,   0x00, 0xff, MPF_NONE },  /* GlidePoint */
2518659607e0Smrg  {  0x08, 0x08, 0x00, 0x00,  4,   0x00, 0xff, MPF_NONE },  /* NetMouse */
2519659607e0Smrg  {  0xc0, 0x00, 0x00, 0x00,  6,   0x00, 0xff, MPF_NONE },  /* NetScroll */
2520659607e0Smrg							    /* Bus Mouse */
2521659607e0Smrg  {  0xf8, 0x80, 0x00, 0x00,  5,   0x00, 0xff, MPF_NONE },  /* BusMouse */
2522659607e0Smrg  {  0xf8, 0x80, 0x00, 0x00,  5,   0x00, 0xff, MPF_NONE },  /* Auto (dummy) */
2523659607e0Smrg  {  0xf8, 0x80, 0x00, 0x00,  8,   0x00, 0xff, MPF_NONE },  /* SysMouse */
2524659607e0Smrg};
2525659607e0Smrg
2526659607e0Smrg
2527659607e0Smrg/*
2528659607e0Smrg * SetupMouse --
2529659607e0Smrg *	Sets up the mouse parameters
2530659607e0Smrg */
2531659607e0Smrgstatic Bool
2532659607e0SmrgSetupMouse(InputInfoPtr pInfo)
2533659607e0Smrg{
2534659607e0Smrg    MouseDevPtr pMse;
2535659607e0Smrg    int i;
2536659607e0Smrg    int protoPara[8] = {-1, -1, -1, -1, -1, -1, -1, -1};
2537659607e0Smrg    const char *name = NULL;
2538659607e0Smrg    Bool automatic = FALSE;
2539659607e0Smrg
2540659607e0Smrg    pMse = pInfo->private;
2541659607e0Smrg
2542659607e0Smrg    /* Handle the "Auto" protocol. */
2543659607e0Smrg    if (pMse->protocolID == PROT_AUTO) {
2544659607e0Smrg	/*
2545659607e0Smrg	 * We come here when user specifies protocol "auto" in
2546659607e0Smrg	 * the configuration file or thru the xf86misc extensions.
2547659607e0Smrg	 * So we initialize autoprobing here.
2548659607e0Smrg	 * Probe for PnP/OS mouse first. If unsuccessful
2549659607e0Smrg	 * try to guess protocol from incoming data.
2550659607e0Smrg	 */
2551659607e0Smrg	automatic = TRUE;
2552659607e0Smrg	pMse->autoProbe = TRUE;
2553659607e0Smrg	name = autoOSProtocol(pInfo,protoPara);
2554659607e0Smrg	if (name)  {
2555659607e0Smrg#ifdef EXTMOUSEDEBUG
2556659607e0Smrg	    ErrorF("PnP/OS Mouse detected: %s\n",name);
2557659607e0Smrg#endif
2558659607e0Smrg	}
2559659607e0Smrg    }
2560659607e0Smrg
2561659607e0Smrg    SetMouseProto(pMse, pMse->protocolID);
2562659607e0Smrg
2563659607e0Smrg    if (automatic) {
2564659607e0Smrg	if (name) {
2565659607e0Smrg	    /* Possible protoPara overrides from SetupAuto. */
2566659607e0Smrg	    for (i = 0; i < sizeof(pMse->protoPara); i++)
2567659607e0Smrg		if (protoPara[i] != -1)
2568659607e0Smrg		    pMse->protoPara[i] = protoPara[i];
2569659607e0Smrg	    /* if we come here PnP/OS mouse probing was successful */
2570659607e0Smrg	} else {
2571659607e0Smrg#if 1
2572659607e0Smrg	    /* PnP/OS mouse probing wasn't successful; we look at data */
2573659607e0Smrg#else
2574659607e0Smrg  	    xf86Msg(X_ERROR, "%s: cannot determine the mouse protocol\n",
2575659607e0Smrg		    pInfo->name);
2576659607e0Smrg	    return FALSE;
2577659607e0Smrg#endif
2578659607e0Smrg	}
2579659607e0Smrg    }
2580659607e0Smrg
2581659607e0Smrg    /*
2582659607e0Smrg     * If protocol has changed fetch the default options
2583659607e0Smrg     * for the new protocol.
2584659607e0Smrg     */
2585659607e0Smrg    if (pMse->oldProtocolID != pMse->protocolID) {
2586659607e0Smrg	pointer tmp = NULL;
2587659607e0Smrg	if ((pMse->protocolID >= 0)
2588659607e0Smrg	    && (pMse->protocolID < PROT_NUMPROTOS)
2589659607e0Smrg	    && mouseProtocols[pMse->protocolID].defaults)
2590659607e0Smrg	    tmp = xf86OptionListCreate(
2591659607e0Smrg		mouseProtocols[pMse->protocolID].defaults, -1, 0);
2592659607e0Smrg	pInfo->options = xf86OptionListMerge(pInfo->options, tmp);
2593659607e0Smrg	/*
2594659607e0Smrg	 * If baudrate is set write it back to the option
2595659607e0Smrg	 * list so that the serial interface code can access
2596659607e0Smrg	 * the new value. Not set means default.
2597659607e0Smrg	 */
2598659607e0Smrg	if (pMse->baudRate)
2599659607e0Smrg	    xf86ReplaceIntOption(pInfo->options, "BaudRate", pMse->baudRate);
2600659607e0Smrg	pMse->oldProtocolID = pMse->protocolID; /* hack */
2601659607e0Smrg    }
2602659607e0Smrg
2603659607e0Smrg
2604659607e0Smrg    /* Set the port parameters. */
2605659607e0Smrg    if (!automatic)
2606659607e0Smrg	xf86SetSerial(pInfo->fd, pInfo->options);
2607659607e0Smrg
2608659607e0Smrg    if (!initMouseHW(pInfo))
2609659607e0Smrg	return FALSE;
2610659607e0Smrg
2611659607e0Smrg    pMse->protoBufTail = 0;
2612659607e0Smrg    pMse->inSync = 0;
2613659607e0Smrg
2614659607e0Smrg    return TRUE;
2615659607e0Smrg}
2616659607e0Smrg
2617659607e0Smrg/********************************************************************
2618659607e0Smrg *
2619659607e0Smrg * Mouse HW setup code
2620659607e0Smrg *
2621659607e0Smrg ********************************************************************/
2622659607e0Smrg
2623659607e0Smrg/*
2624659607e0Smrg** The following lines take care of the Logitech MouseMan protocols.
2625659607e0Smrg** The "Logitech" protocol is for the old "series 9" Logitech products.
2626659607e0Smrg** All products since then use the "MouseMan" protocol.  Some models
2627659607e0Smrg** were programmable, but most (all?) of the current models are not.
2628659607e0Smrg**
2629659607e0Smrg** NOTE: There are different versions of both MouseMan and TrackMan!
2630659607e0Smrg**       Hence I add another protocol PROT_LOGIMAN, which the user can
2631659607e0Smrg**       specify as MouseMan in his XF86Config file. This entry was
2632659607e0Smrg**       formerly handled as a special case of PROT_MS. However, people
2633659607e0Smrg**       who don't have the middle button problem, can still specify
2634659607e0Smrg**       Microsoft and use PROT_MS.
2635659607e0Smrg**
2636659607e0Smrg** By default, these mice should use a 3 byte Microsoft protocol
2637659607e0Smrg** plus a 4th byte for the middle button. However, the mouse might
2638659607e0Smrg** have switched to a different protocol before we use it, so I send
2639659607e0Smrg** the proper sequence just in case.
2640659607e0Smrg**
2641659607e0Smrg** NOTE: - all commands to (at least the European) MouseMan have to
2642659607e0Smrg**         be sent at 1200 Baud.
2643659607e0Smrg**       - each command starts with a '*'.
2644659607e0Smrg**       - whenever the MouseMan receives a '*', it will switch back
2645659607e0Smrg**	 to 1200 Baud. Hence I have to select the desired protocol
2646659607e0Smrg**	 first, then select the baud rate.
2647659607e0Smrg**
2648659607e0Smrg** The protocols supported by the (European) MouseMan are:
2649659607e0Smrg**   -  5 byte packed binary protocol, as with the Mouse Systems
2650659607e0Smrg**      mouse. Selected by sequence "*U".
2651659607e0Smrg**   -  2 button 3 byte MicroSoft compatible protocol. Selected
2652659607e0Smrg**      by sequence "*V".
2653659607e0Smrg**   -  3 button 3+1 byte MicroSoft compatible protocol (default).
2654659607e0Smrg**      Selected by sequence "*X".
2655659607e0Smrg**
2656659607e0Smrg** The following baud rates are supported:
2657659607e0Smrg**   -  1200 Baud (default). Selected by sequence "*n".
2658659607e0Smrg**   -  9600 Baud. Selected by sequence "*q".
2659659607e0Smrg**
2660659607e0Smrg** Selecting a sample rate is no longer supported with the MouseMan!
2661659607e0Smrg**               [CHRIS-211092]
2662659607e0Smrg*/
2663659607e0Smrg
2664659607e0Smrg/*
2665659607e0Smrg * Do a reset wrap mode before reset.
2666659607e0Smrg */
2667659607e0Smrg#define do_ps2Reset(x)  { \
2668659607e0Smrg    int i = RETRY_COUNT;\
2669659607e0Smrg     while (i-- > 0) { \
2670659607e0Smrg       xf86FlushInput(x->fd); \
2671659607e0Smrg       if (ps2Reset(x)) break; \
2672659607e0Smrg    } \
2673659607e0Smrg  }
2674659607e0Smrg
2675659607e0Smrg
2676659607e0Smrgstatic Bool
2677659607e0SmrginitMouseHW(InputInfoPtr pInfo)
2678659607e0Smrg{
2679659607e0Smrg    MouseDevPtr pMse = pInfo->private;
2680659607e0Smrg    const char *s;
2681659607e0Smrg    unsigned char c;
2682659607e0Smrg    int speed;
2683659607e0Smrg    pointer options;
2684659607e0Smrg    unsigned char *param = NULL;
2685659607e0Smrg    int paramlen = 0;
2686659607e0Smrg    int count = RETRY_COUNT;
2687659607e0Smrg    Bool ps2Init = TRUE;
2688659607e0Smrg
2689659607e0Smrg    switch (pMse->protocolID) {
2690659607e0Smrg	case PROT_LOGI:		/* Logitech Mice */
2691659607e0Smrg	    /*
2692659607e0Smrg	     * The baud rate selection command must be sent at the current
2693659607e0Smrg	     * baud rate; try all likely settings.
2694659607e0Smrg	     */
2695659607e0Smrg	    speed = pMse->baudRate;
2696659607e0Smrg	    switch (speed) {
2697659607e0Smrg		case 9600:
2698659607e0Smrg		    s = "*q";
2699659607e0Smrg		    break;
2700659607e0Smrg		case 4800:
2701659607e0Smrg		    s = "*p";
2702659607e0Smrg		    break;
2703659607e0Smrg		case 2400:
2704659607e0Smrg		    s = "*o";
2705659607e0Smrg		    break;
2706659607e0Smrg		case 1200:
2707659607e0Smrg		    s = "*n";
2708659607e0Smrg		    break;
2709659607e0Smrg		default:
2710659607e0Smrg		    /* Fallback value */
2711659607e0Smrg		    speed = 1200;
2712659607e0Smrg		    s = "*n";
2713659607e0Smrg	    }
2714659607e0Smrg	    xf86SetSerialSpeed(pInfo->fd, 9600);
2715659607e0Smrg	    xf86WriteSerial(pInfo->fd, s, 2);
2716659607e0Smrg	    usleep(100000);
2717659607e0Smrg	    xf86SetSerialSpeed(pInfo->fd, 4800);
2718659607e0Smrg	    xf86WriteSerial(pInfo->fd, s, 2);
2719659607e0Smrg	    usleep(100000);
2720659607e0Smrg	    xf86SetSerialSpeed(pInfo->fd, 2400);
2721659607e0Smrg	    xf86WriteSerial(pInfo->fd, s, 2);
2722659607e0Smrg	    usleep(100000);
2723659607e0Smrg	    xf86SetSerialSpeed(pInfo->fd, 1200);
2724659607e0Smrg	    xf86WriteSerial(pInfo->fd, s, 2);
2725659607e0Smrg	    usleep(100000);
2726659607e0Smrg	    xf86SetSerialSpeed(pInfo->fd, speed);
2727659607e0Smrg
2728659607e0Smrg	    /* Select MM series data format. */
2729659607e0Smrg	    xf86WriteSerial(pInfo->fd, "S", 1);
2730659607e0Smrg	    usleep(100000);
2731659607e0Smrg	    /* Set the parameters up for the MM series protocol. */
2732659607e0Smrg	    options = pInfo->options;
2733659607e0Smrg	    xf86CollectInputOptions(pInfo, mmDefaults, NULL);
2734659607e0Smrg	    xf86SetSerial(pInfo->fd, pInfo->options);
2735659607e0Smrg	    pInfo->options = options;
2736659607e0Smrg
2737659607e0Smrg	    /* Select report rate/frequency. */
2738659607e0Smrg	    if      (pMse->sampleRate <=   0)  c = 'O';  /* 100 */
2739659607e0Smrg	    else if (pMse->sampleRate <=  15)  c = 'J';  /*  10 */
2740659607e0Smrg	    else if (pMse->sampleRate <=  27)  c = 'K';  /*  20 */
2741659607e0Smrg	    else if (pMse->sampleRate <=  42)  c = 'L';  /*  35 */
2742659607e0Smrg	    else if (pMse->sampleRate <=  60)  c = 'R';  /*  50 */
2743659607e0Smrg	    else if (pMse->sampleRate <=  85)  c = 'M';  /*  67 */
2744659607e0Smrg	    else if (pMse->sampleRate <= 125)  c = 'Q';  /* 100 */
2745659607e0Smrg	    else                               c = 'N';  /* 150 */
2746659607e0Smrg	    xf86WriteSerial(pInfo->fd, &c, 1);
2747659607e0Smrg	    break;
2748659607e0Smrg
2749659607e0Smrg	case PROT_LOGIMAN:
2750659607e0Smrg	    speed = pMse->baudRate;
2751659607e0Smrg	    switch (speed) {
2752659607e0Smrg		case 9600:
2753659607e0Smrg		    s = "*q";
2754659607e0Smrg		    break;
2755659607e0Smrg		case 1200:
2756659607e0Smrg		    s = "*n";
2757659607e0Smrg		    break;
2758659607e0Smrg		default:
2759659607e0Smrg		    /* Fallback value */
2760659607e0Smrg		    speed = 1200;
2761659607e0Smrg		    s = "*n";
2762659607e0Smrg	    }
2763659607e0Smrg	    xf86SetSerialSpeed(pInfo->fd, 1200);
2764659607e0Smrg	    xf86WriteSerial(pInfo->fd, "*n", 2);
2765659607e0Smrg	    xf86WriteSerial(pInfo->fd, "*X", 2);
2766659607e0Smrg	    xf86WriteSerial(pInfo->fd, s, 2);
2767659607e0Smrg	    usleep(100000);
2768659607e0Smrg	    xf86SetSerialSpeed(pInfo->fd, speed);
2769659607e0Smrg	    break;
2770659607e0Smrg
2771659607e0Smrg	case PROT_MMHIT:		/* MM_HitTablet */
2772659607e0Smrg	    /*
2773659607e0Smrg	     * Initialize Hitachi PUMA Plus - Model 1212E to desired settings.
2774659607e0Smrg	     * The tablet must be configured to be in MM mode, NO parity,
2775659607e0Smrg	     * Binary Format.  pMse->sampleRate controls the sensitivity
2776659607e0Smrg	     * of the tablet.  We only use this tablet for it's 4-button puck
2777659607e0Smrg	     * so we don't run in "Absolute Mode".
2778659607e0Smrg	     */
2779659607e0Smrg	    xf86WriteSerial(pInfo->fd, "z8", 2);	/* Set Parity = "NONE" */
2780659607e0Smrg	    usleep(50000);
2781659607e0Smrg	    xf86WriteSerial(pInfo->fd, "zb", 2);	/* Set Format = "Binary" */
2782659607e0Smrg	    usleep(50000);
2783659607e0Smrg	    xf86WriteSerial(pInfo->fd, "@", 1);	/* Set Report Mode = "Stream" */
2784659607e0Smrg	    usleep(50000);
2785659607e0Smrg	    xf86WriteSerial(pInfo->fd, "R", 1);	/* Set Output Rate = "45 rps" */
2786659607e0Smrg	    usleep(50000);
2787659607e0Smrg	    xf86WriteSerial(pInfo->fd, "I\x20", 2);	/* Set Incrememtal Mode "20" */
2788659607e0Smrg	    usleep(50000);
2789659607e0Smrg	    xf86WriteSerial(pInfo->fd, "E", 1);	/* Set Data Type = "Relative */
2790659607e0Smrg	    usleep(50000);
2791659607e0Smrg	    /*
2792659607e0Smrg	     * These sample rates translate to 'lines per inch' on the Hitachi
2793659607e0Smrg	     * tablet.
2794659607e0Smrg	     */
2795659607e0Smrg	    if      (pMse->sampleRate <=   40) c = 'g';
2796659607e0Smrg	    else if (pMse->sampleRate <=  100) c = 'd';
2797659607e0Smrg	    else if (pMse->sampleRate <=  200) c = 'e';
2798659607e0Smrg	    else if (pMse->sampleRate <=  500) c = 'h';
2799659607e0Smrg	    else if (pMse->sampleRate <= 1000) c = 'j';
2800659607e0Smrg	    else                               c = 'd';
2801659607e0Smrg	    xf86WriteSerial(pInfo->fd, &c, 1);
2802659607e0Smrg	    usleep(50000);
2803659607e0Smrg	    xf86WriteSerial(pInfo->fd, "\021", 1);	/* Resume DATA output */
2804659607e0Smrg	    break;
2805659607e0Smrg
2806659607e0Smrg	case PROT_THINKING:		/* ThinkingMouse */
2807659607e0Smrg	    /* This mouse may send a PnP ID string, ignore it. */
2808659607e0Smrg	    usleep(200000);
2809659607e0Smrg	    xf86FlushInput(pInfo->fd);
2810659607e0Smrg	    /* Send the command to initialize the beast. */
2811659607e0Smrg	    for (s = "E5E5"; *s; ++s) {
2812659607e0Smrg		xf86WriteSerial(pInfo->fd, s, 1);
2813659607e0Smrg		if ((xf86WaitForInput(pInfo->fd, 1000000) <= 0))
2814659607e0Smrg		    break;
2815659607e0Smrg		xf86ReadSerial(pInfo->fd, &c, 1);
2816659607e0Smrg		if (c != *s)
2817659607e0Smrg		    break;
2818659607e0Smrg	    }
2819659607e0Smrg	    break;
2820659607e0Smrg
2821659607e0Smrg	case PROT_MSC:		/* MouseSystems Corp */
2822659607e0Smrg	    usleep(100000);
2823659607e0Smrg	    xf86FlushInput(pInfo->fd);
2824659607e0Smrg	    break;
2825659607e0Smrg
2826659607e0Smrg	case PROT_ACECAD:
2827659607e0Smrg	    /* initialize */
2828659607e0Smrg	    /* A nul character resets. */
2829659607e0Smrg	    xf86WriteSerial(pInfo->fd, "", 1);
2830659607e0Smrg	    usleep(50000);
2831659607e0Smrg	    /* Stream out relative mode high resolution increments of 1. */
2832659607e0Smrg	    xf86WriteSerial(pInfo->fd, "@EeI!", 5);
2833659607e0Smrg	    break;
2834659607e0Smrg
2835659607e0Smrg	case PROT_BM:		/* bus/InPort mouse */
2836659607e0Smrg	    if (osInfo->SetBMRes)
2837659607e0Smrg		osInfo->SetBMRes(pInfo, pMse->protocol, pMse->sampleRate,
2838659607e0Smrg				 pMse->resolution);
2839659607e0Smrg	    break;
2840659607e0Smrg
2841659607e0Smrg	case PROT_GENPS2:
2842659607e0Smrg	    ps2Init = FALSE;
2843659607e0Smrg	    break;
2844659607e0Smrg
2845659607e0Smrg	case PROT_PS2:
2846659607e0Smrg	case PROT_GLIDEPS2:
2847659607e0Smrg	    break;
2848659607e0Smrg
2849659607e0Smrg	case PROT_IMPS2:		/* IntelliMouse */
2850659607e0Smrg	{
2851659607e0Smrg	    static unsigned char seq[] = { 243, 200, 243, 100, 243, 80 };
2852659607e0Smrg	    param = seq;
2853659607e0Smrg	    paramlen = sizeof(seq);
2854659607e0Smrg	}
2855659607e0Smrg	break;
2856659607e0Smrg
2857659607e0Smrg	case PROT_EXPPS2:		/* IntelliMouse Explorer */
2858659607e0Smrg	{
2859659607e0Smrg	    static unsigned char seq[] = { 243, 200, 243, 100, 243, 80,
2860659607e0Smrg					   243, 200, 243, 200, 243, 80 };
2861659607e0Smrg
2862659607e0Smrg	    param = seq;
2863659607e0Smrg	    paramlen = sizeof(seq);
2864659607e0Smrg	}
2865659607e0Smrg	break;
2866659607e0Smrg
2867659607e0Smrg	case PROT_NETPS2:		/* NetMouse, NetMouse Pro, Mie Mouse */
2868659607e0Smrg	case PROT_NETSCPS2:		/* NetScroll */
2869659607e0Smrg	{
2870659607e0Smrg	    static unsigned char seq[] = { 232, 3, 230, 230, 230, 233 };
2871659607e0Smrg
2872659607e0Smrg	    param = seq;
2873659607e0Smrg	    paramlen = sizeof(seq);
2874659607e0Smrg	}
2875659607e0Smrg	break;
2876659607e0Smrg
2877659607e0Smrg	case PROT_MMPS2:		/* MouseMan+, FirstMouse+ */
2878659607e0Smrg	{
2879659607e0Smrg	    static unsigned char seq[] = { 230, 232, 0, 232, 3, 232, 2, 232, 1,
2880659607e0Smrg					   230, 232, 3, 232, 1, 232, 2, 232, 3 };
2881659607e0Smrg	    param = seq;
2882659607e0Smrg	    paramlen = sizeof(seq);
2883659607e0Smrg	}
2884659607e0Smrg	break;
2885659607e0Smrg
2886659607e0Smrg	case PROT_THINKPS2:		/* ThinkingMouse */
2887659607e0Smrg	{
2888659607e0Smrg	    static unsigned char seq[] = { 243, 10, 232,  0, 243, 20, 243, 60,
2889659607e0Smrg					   243, 40, 243, 20, 243, 20, 243, 60,
2890659607e0Smrg					   243, 40, 243, 20, 243, 20 };
2891659607e0Smrg	    param = seq;
2892659607e0Smrg	    paramlen = sizeof(seq);
2893659607e0Smrg	}
2894659607e0Smrg	break;
2895659607e0Smrg	case PROT_SYSMOUSE:
2896659607e0Smrg	    if (osInfo->SetMiscRes)
2897659607e0Smrg		osInfo->SetMiscRes(pInfo, pMse->protocol, pMse->sampleRate,
2898659607e0Smrg				   pMse->resolution);
2899659607e0Smrg	    break;
2900659607e0Smrg
2901659607e0Smrg	default:
2902659607e0Smrg	    /* Nothing to do. */
2903659607e0Smrg	    break;
2904659607e0Smrg    }
2905659607e0Smrg
2906659607e0Smrg    if (pMse->class & (MSE_PS2 | MSE_XPS2)) {
2907659607e0Smrg	/*
2908659607e0Smrg	 * If one part of the PS/2 mouse initialization fails
2909659607e0Smrg	 * redo complete initialization. There are mice which
2910659607e0Smrg	 * have occasional problems with initialization and
2911659607e0Smrg	 * are in an unknown state.
2912659607e0Smrg	 */
2913659607e0Smrg	if (ps2Init) {
2914659607e0Smrg	REDO:
2915659607e0Smrg	    do_ps2Reset(pInfo);
2916659607e0Smrg	    if (paramlen > 0) {
2917659607e0Smrg		if (!ps2SendPacket(pInfo,param,paramlen)) {
2918659607e0Smrg		    usleep(30000);
2919659607e0Smrg		    xf86FlushInput(pInfo->fd);
2920659607e0Smrg		    if (!count--)
2921659607e0Smrg			return TRUE;
2922659607e0Smrg		    goto REDO;
2923659607e0Smrg		}
2924659607e0Smrg		ps2GetDeviceID(pInfo);
2925659607e0Smrg		usleep(30000);
2926659607e0Smrg		xf86FlushInput(pInfo->fd);
2927659607e0Smrg	    }
2928659607e0Smrg
2929659607e0Smrg	    if (osInfo->SetPS2Res) {
2930659607e0Smrg		osInfo->SetPS2Res(pInfo, pMse->protocol, pMse->sampleRate,
2931659607e0Smrg				  pMse->resolution);
2932659607e0Smrg	    } else {
2933659607e0Smrg		unsigned char c2[2];
2934659607e0Smrg
2935659607e0Smrg		c = 0xE6;	/*230*/	/* 1:1 scaling */
2936659607e0Smrg		if (!ps2SendPacket(pInfo,&c,1)) {
2937659607e0Smrg		    if (!count--)
2938659607e0Smrg			return TRUE;
2939659607e0Smrg		    goto REDO;
2940659607e0Smrg		}
2941659607e0Smrg		c2[0] = 0xF3; /*243*/ /* set sampling rate */
2942659607e0Smrg		if (pMse->sampleRate > 0) {
2943659607e0Smrg		    if (pMse->sampleRate >= 200)
2944659607e0Smrg			c2[1] = 200;
2945659607e0Smrg		    else if (pMse->sampleRate >= 100)
2946659607e0Smrg			c2[1] = 100;
2947659607e0Smrg		    else if (pMse->sampleRate >= 80)
2948659607e0Smrg			c2[1] = 80;
2949659607e0Smrg		    else if (pMse->sampleRate >= 60)
2950659607e0Smrg			c2[1] = 60;
2951659607e0Smrg		    else if (pMse->sampleRate >= 40)
2952659607e0Smrg			c2[1] = 40;
2953659607e0Smrg		    else
2954659607e0Smrg			c2[1] = 20;
2955659607e0Smrg		} else {
2956659607e0Smrg		    c2[1] = 100;
2957659607e0Smrg		}
2958659607e0Smrg		if (!ps2SendPacket(pInfo,c2,2)) {
2959659607e0Smrg		    if (!count--)
2960659607e0Smrg			return TRUE;
2961659607e0Smrg		    goto REDO;
2962659607e0Smrg		}
2963659607e0Smrg		c2[0] = 0xE8; /*232*/	/* set device resolution */
2964659607e0Smrg		if (pMse->resolution > 0) {
2965659607e0Smrg		    if (pMse->resolution >= 200)
2966659607e0Smrg			c2[1] = 3;
2967659607e0Smrg		    else if (pMse->resolution >= 100)
2968659607e0Smrg			c2[1] = 2;
2969659607e0Smrg		    else if (pMse->resolution >= 50)
2970659607e0Smrg			c2[1] = 1;
2971659607e0Smrg		    else
2972659607e0Smrg			c2[1] = 0;
2973659607e0Smrg		} else {
2974659607e0Smrg		    c2[1] = 3; /* used to be 2, W. uses 3 */
2975659607e0Smrg		}
2976659607e0Smrg		if (!ps2SendPacket(pInfo,c2,2)) {
2977659607e0Smrg		    if (!count--)
2978659607e0Smrg			return TRUE;
2979659607e0Smrg		    goto REDO;
2980659607e0Smrg		}
2981659607e0Smrg		usleep(30000);
2982659607e0Smrg		xf86FlushInput(pInfo->fd);
2983659607e0Smrg		if (!ps2EnableDataReporting(pInfo)) {
2984659607e0Smrg		    xf86Msg(X_INFO, "%s: ps2EnableDataReporting: failed\n",
2985659607e0Smrg			    pInfo->name);
2986659607e0Smrg		    xf86FlushInput(pInfo->fd);
2987659607e0Smrg		    if (!count--)
2988659607e0Smrg			return TRUE;
2989659607e0Smrg		    goto REDO;
2990659607e0Smrg		} else {
2991659607e0Smrg		    xf86Msg(X_INFO, "%s: ps2EnableDataReporting: succeeded\n",
2992659607e0Smrg			    pInfo->name);
2993659607e0Smrg		}
2994659607e0Smrg	    }
2995659607e0Smrg	    /*
2996659607e0Smrg	     * The PS/2 reset handling needs to be rechecked.
2997659607e0Smrg	     * We need to wait until after the 4.3 release.
2998659607e0Smrg	     */
2999659607e0Smrg	}
3000659607e0Smrg    } else {
3001659607e0Smrg	if (paramlen > 0) {
3002659607e0Smrg	    if (xf86WriteSerial(pInfo->fd, param, paramlen) != paramlen)
3003659607e0Smrg		xf86Msg(X_ERROR, "%s: Mouse initialization failed\n",
3004659607e0Smrg			pInfo->name);
3005659607e0Smrg	    usleep(30000);
3006659607e0Smrg	    xf86FlushInput(pInfo->fd);
3007659607e0Smrg	}
3008659607e0Smrg    }
3009659607e0Smrg
3010659607e0Smrg    return TRUE;
3011659607e0Smrg}
3012659607e0Smrg
3013659607e0Smrg#ifdef SUPPORT_MOUSE_RESET
3014659607e0Smrgstatic Bool
3015659607e0SmrgmouseReset(InputInfoPtr pInfo, unsigned char val)
3016659607e0Smrg{
3017659607e0Smrg    MouseDevPtr pMse = pInfo->private;
3018659607e0Smrg    mousePrivPtr mousepriv = (mousePrivPtr)pMse->mousePriv;
3019659607e0Smrg    CARD32 prevEvent = mousepriv->lastEvent;
3020659607e0Smrg    Bool expectReset = FALSE;
3021659607e0Smrg    Bool ret = FALSE;
3022659607e0Smrg
3023659607e0Smrg    mousepriv->lastEvent = GetTimeInMillis();
3024659607e0Smrg
3025659607e0Smrg#ifdef EXTMOUSEDEBUG
3026659607e0Smrg    ErrorF("byte: 0x%x time: %li\n",val,mousepriv->lastEvent);
3027659607e0Smrg#endif
3028659607e0Smrg    /*
3029659607e0Smrg     * We believe that the following is true:
3030659607e0Smrg     * When the mouse is replugged it will send a reset package
3031659607e0Smrg     * It takes several seconds to replug a mouse: We don't see
3032659607e0Smrg     * events for several seconds before we see the replug event package.
3033659607e0Smrg     * There is no significant delay between consecutive bytes
3034659607e0Smrg     * of a replug event package.
3035659607e0Smrg     * There are no bytes sent after the replug event package until
3036659607e0Smrg     * the mouse is reset.
3037659607e0Smrg     */
3038659607e0Smrg
3039659607e0Smrg    if (mousepriv->current == 0
3040659607e0Smrg	&& (mousepriv->lastEvent - prevEvent) < 4000)
3041659607e0Smrg	return FALSE;
3042659607e0Smrg
3043659607e0Smrg    if (mousepriv->current > 0
3044659607e0Smrg	&& (mousepriv->lastEvent - prevEvent) >= 1000) {
3045659607e0Smrg	mousepriv->inReset = FALSE;
3046659607e0Smrg	mousepriv->current = 0;
3047659607e0Smrg	return FALSE;
3048659607e0Smrg    }
3049659607e0Smrg
3050659607e0Smrg    if (mousepriv->inReset)
3051659607e0Smrg	mousepriv->inReset = FALSE;
3052659607e0Smrg
3053659607e0Smrg#ifdef EXTMOUSEDEBUG
3054659607e0Smrg    ErrorF("Mouse Current: %i 0x%x\n",mousepriv->current, val);
3055659607e0Smrg#endif
3056659607e0Smrg
3057659607e0Smrg    /* here we put the mouse specific reset detction */
3058659607e0Smrg    /* They need to do three things:                 */
3059659607e0Smrg    /*  Check if byte may be a reset byte            */
3060659607e0Smrg    /*  If so: Set expectReset TRUE                  */
3061659607e0Smrg    /*  If convinced: Set inReset TRUE               */
3062659607e0Smrg    /*                Register BlockAndWakeupHandler */
3063659607e0Smrg
3064659607e0Smrg    /* PS/2 */
3065659607e0Smrg    {
3066659607e0Smrg	unsigned char seq[] = { 0xaa, 0x00 };
3067659607e0Smrg	int len = sizeof(seq);
3068659607e0Smrg
3069659607e0Smrg	if (seq[mousepriv->current] == val)
3070659607e0Smrg	    expectReset = TRUE;
3071659607e0Smrg
3072659607e0Smrg	if (len == mousepriv->current + 1) {
3073659607e0Smrg	    mousepriv->inReset = TRUE;
3074659607e0Smrg	    mousepriv->expires = GetTimeInMillis() + 1000;
3075659607e0Smrg
3076659607e0Smrg#ifdef EXTMOUSEDEBUG
3077659607e0Smrg	    ErrorF("Found PS/2 Reset string\n");
3078659607e0Smrg#endif
3079659607e0Smrg	    RegisterBlockAndWakeupHandlers (ps2BlockHandler,
3080659607e0Smrg					    ps2WakeupHandler, (pointer) pInfo);
3081659607e0Smrg	    ret = TRUE;
3082659607e0Smrg	}
3083659607e0Smrg    }
3084659607e0Smrg
3085659607e0Smrg	if (!expectReset)
3086659607e0Smrg	    mousepriv->current = 0;
3087659607e0Smrg	else
3088659607e0Smrg	    mousepriv->current++;
3089659607e0Smrg	return ret;
3090659607e0Smrg}
3091659607e0Smrg
3092659607e0Smrgstatic void
3093659607e0Smrgps2BlockHandler(pointer data, struct timeval **waitTime,
3094659607e0Smrg		pointer LastSelectMask)
3095659607e0Smrg{
3096659607e0Smrg    InputInfoPtr    pInfo = (InputInfoPtr) data;
3097659607e0Smrg    MouseDevPtr	    pMse = (MouseDevPtr) pInfo->private;
3098659607e0Smrg    mousePrivPtr    mousepriv = (mousePrivPtr)pMse->mousePriv;
3099659607e0Smrg    int		    ms;
3100659607e0Smrg
3101659607e0Smrg    if (mousepriv->inReset) {
3102659607e0Smrg	ms = mousepriv->expires - GetTimeInMillis ();
3103659607e0Smrg	if (ms <= 0)
3104659607e0Smrg	    ms = 0;
3105659607e0Smrg	AdjustWaitForDelay (waitTime, ms);
3106659607e0Smrg    } else
3107659607e0Smrg	RemoveBlockAndWakeupHandlers (ps2BlockHandler, ps2WakeupHandler,
3108659607e0Smrg				      (pointer) pInfo);
3109659607e0Smrg}
3110659607e0Smrg
3111659607e0Smrgstatic void
3112659607e0Smrgps2WakeupHandler(pointer data, int i, pointer LastSelectMask)
3113659607e0Smrg{
3114659607e0Smrg    InputInfoPtr    pInfo = (InputInfoPtr) data;
3115659607e0Smrg    MouseDevPtr	    pMse = (MouseDevPtr) pInfo->private;
3116659607e0Smrg    mousePrivPtr mousepriv = (mousePrivPtr)pMse->mousePriv;
3117659607e0Smrg    int		    ms;
3118659607e0Smrg
3119659607e0Smrg    if (mousepriv->inReset) {
3120659607e0Smrg	unsigned char val;
3121659607e0Smrg	int blocked;
3122659607e0Smrg
3123659607e0Smrg	ms = mousepriv->expires - GetTimeInMillis();
3124659607e0Smrg	if (ms > 0)
3125659607e0Smrg	    return;
3126659607e0Smrg
3127659607e0Smrg	blocked = xf86BlockSIGIO ();
3128659607e0Smrg
3129659607e0Smrg	xf86MsgVerb(X_INFO,3,
3130659607e0Smrg		    "Got reinsert event: reinitializing PS/2 mouse\n");
3131659607e0Smrg	val = 0xf4;
3132659607e0Smrg	if (xf86WriteSerial(pInfo->fd, &val, 1) != 1)
3133659607e0Smrg	    xf86Msg(X_ERROR, "%s: Write to mouse failed\n",
3134659607e0Smrg		    pInfo->name);
3135659607e0Smrg	xf86UnblockSIGIO(blocked);
3136659607e0Smrg    }
3137659607e0Smrg    RemoveBlockAndWakeupHandlers (ps2BlockHandler, ps2WakeupHandler,
3138659607e0Smrg				  (pointer) pInfo);
3139659607e0Smrg}
3140659607e0Smrg#endif /* SUPPORT_MOUSE_RESET */
3141659607e0Smrg
3142659607e0Smrg/************************************************************
3143659607e0Smrg *
3144659607e0Smrg * Autoprobe stuff
3145659607e0Smrg *
3146659607e0Smrg ************************************************************/
3147659607e0Smrg#ifdef EXTMOUSEDEBUG
3148659607e0Smrg#  define AP_DBG(x) { ErrorF("Autoprobe: "); ErrorF x; }
3149659607e0Smrg#  define AP_DBGC(x) ErrorF x ;
3150659607e0Smrg# else
3151659607e0Smrg#  define AP_DBG(x)
3152659607e0Smrg#  define AP_DBGC(x)
3153659607e0Smrg#endif
3154659607e0Smrg
3155659607e0SmrgMouseProtocolID hardProtocolList[] = { 	PROT_MSC, PROT_MM, PROT_LOGI,
3156659607e0Smrg					PROT_LOGIMAN, PROT_MMHIT,
3157659607e0Smrg					PROT_GLIDE, PROT_IMSERIAL,
3158659607e0Smrg					PROT_THINKING, PROT_ACECAD,
3159659607e0Smrg					PROT_THINKPS2, PROT_MMPS2,
3160659607e0Smrg					PROT_GLIDEPS2,
3161659607e0Smrg					PROT_NETSCPS2, PROT_EXPPS2,PROT_IMPS2,
3162659607e0Smrg					PROT_GENPS2, PROT_NETPS2,
3163659607e0Smrg					PROT_MS,
3164659607e0Smrg					PROT_UNKNOWN
3165659607e0Smrg};
3166659607e0Smrg
3167659607e0SmrgMouseProtocolID softProtocolList[] = { 	PROT_MSC, PROT_MM, PROT_LOGI,
3168659607e0Smrg					PROT_LOGIMAN, PROT_MMHIT,
3169659607e0Smrg					PROT_GLIDE, PROT_IMSERIAL,
3170659607e0Smrg					PROT_THINKING, PROT_ACECAD,
3171659607e0Smrg					PROT_THINKPS2, PROT_MMPS2,
3172659607e0Smrg					PROT_GLIDEPS2,
3173659607e0Smrg					PROT_NETSCPS2 ,PROT_IMPS2,
3174659607e0Smrg					PROT_GENPS2,
3175659607e0Smrg					PROT_MS,
3176659607e0Smrg					PROT_UNKNOWN
3177659607e0Smrg};
3178659607e0Smrg
3179659607e0Smrgstatic const char *
3180659607e0SmrgautoOSProtocol(InputInfoPtr pInfo, int *protoPara)
3181659607e0Smrg{
3182659607e0Smrg    MouseDevPtr pMse = pInfo->private;
3183659607e0Smrg    const char *name = NULL;
3184659607e0Smrg    MouseProtocolID protocolID = PROT_UNKNOWN;
3185659607e0Smrg
3186659607e0Smrg    /* Check if the OS has a detection mechanism. */
3187659607e0Smrg    if (osInfo->SetupAuto) {
3188659607e0Smrg	name = osInfo->SetupAuto(pInfo, protoPara);
3189659607e0Smrg	if (name) {
3190659607e0Smrg	    protocolID = ProtocolNameToID(name);
3191659607e0Smrg	    switch (protocolID) {
3192659607e0Smrg		case PROT_UNKNOWN:
3193659607e0Smrg		    /* Check for a builtin OS-specific protocol. */
3194659607e0Smrg		    if (osInfo->CheckProtocol && osInfo->CheckProtocol(name)) {
3195659607e0Smrg			/* We can only come here if the protocol has been
3196659607e0Smrg			 * changed to auto thru the xf86misc extension
3197659607e0Smrg			 * and we have detected an OS specific builtin
3198659607e0Smrg			 * protocol. Currently we cannot handle this */
3199659607e0Smrg			name = NULL;
3200659607e0Smrg		    } else
3201659607e0Smrg			name = NULL;
3202659607e0Smrg		    break;
3203659607e0Smrg		case PROT_UNSUP:
3204659607e0Smrg		    name = NULL;
3205659607e0Smrg		    break;
3206659607e0Smrg		default:
3207659607e0Smrg		    break;
3208659607e0Smrg	    }
3209659607e0Smrg	}
3210659607e0Smrg    }
3211659607e0Smrg    if (!name) {
3212659607e0Smrg	/* A PnP serial mouse? */
3213659607e0Smrg	protocolID = MouseGetPnpProtocol(pInfo);
3214659607e0Smrg	if (protocolID >= 0 && protocolID < PROT_NUMPROTOS) {
3215659607e0Smrg	    name = ProtocolIDToName(protocolID);
3216659607e0Smrg	    xf86Msg(X_PROBED, "%s: PnP-detected protocol: \"%s\"\n",
3217659607e0Smrg		    pInfo->name, name);
3218659607e0Smrg	}
3219659607e0Smrg    }
3220659607e0Smrg    if (!name && HAVE_GUESS_PROTOCOL && osInfo->GuessProtocol) {
3221659607e0Smrg	name = osInfo->GuessProtocol(pInfo, 0);
3222659607e0Smrg	if (name)
3223659607e0Smrg	    protocolID = ProtocolNameToID(name);
3224659607e0Smrg    }
3225659607e0Smrg
3226659607e0Smrg    if (name) {
3227659607e0Smrg	pMse->protocolID = protocolID;
3228659607e0Smrg    }
3229659607e0Smrg
3230659607e0Smrg    return name;
3231659607e0Smrg}
3232659607e0Smrg
3233659607e0Smrg/*
3234659607e0Smrg * createProtocolList() -- create a list of protocols which may
3235659607e0Smrg * match on the incoming data stream.
3236659607e0Smrg */
3237659607e0Smrgstatic void
3238659607e0SmrgcreateProtoList(MouseDevPtr pMse, MouseProtocolID *protoList)
3239659607e0Smrg{
3240659607e0Smrg    int i, j, k  = 0;
3241659607e0Smrg    MouseProtocolID prot;
3242659607e0Smrg    unsigned char *para;
3243659607e0Smrg    mousePrivPtr mPriv = (mousePrivPtr)pMse->mousePriv;
3244659607e0Smrg    MouseProtocolID *tmplist = NULL;
3245659607e0Smrg    int blocked;
3246659607e0Smrg
3247659607e0Smrg    AP_DBGC(("Autoprobe: "));
3248659607e0Smrg    for (i = 0; i < mPriv->count; i++)
3249659607e0Smrg	AP_DBGC(("%2.2x ", (unsigned char) mPriv->data[i]));
3250659607e0Smrg    AP_DBGC(("\n"));
3251659607e0Smrg
3252659607e0Smrg    blocked = xf86BlockSIGIO ();
3253659607e0Smrg
3254659607e0Smrg    /* create a private copy first so we can write in the old list */
3255659607e0Smrg    if ((tmplist = xalloc(sizeof(MouseProtocolID) * NUM_AUTOPROBE_PROTOS))){
3256659607e0Smrg	for (i = 0; protoList[i] != PROT_UNKNOWN; i++) {
3257659607e0Smrg	    tmplist[i] = protoList[i];
3258659607e0Smrg	}
3259659607e0Smrg	tmplist[i] = PROT_UNKNOWN;
3260659607e0Smrg	protoList = tmplist;
3261659607e0Smrg    } else
3262659607e0Smrg	return;
3263659607e0Smrg
3264659607e0Smrg    for (i = 0; ((prot = protoList[i]) != PROT_UNKNOWN
3265659607e0Smrg		 && (k < NUM_AUTOPROBE_PROTOS - 1)) ; i++) {
3266659607e0Smrg	Bool bad = TRUE;
3267659607e0Smrg	unsigned char byte = 0;
3268659607e0Smrg	int count = 0;
3269659607e0Smrg	int next_header_candidate = 0;
3270659607e0Smrg	int header_count = 0;
3271659607e0Smrg
3272659607e0Smrg	if (!GetProtocol(prot))
3273659607e0Smrg	    continue;
3274659607e0Smrg	para = proto[prot];
3275659607e0Smrg
3276659607e0Smrg	AP_DBG(("Protocol: %s ", ProtocolIDToName(prot)));
3277659607e0Smrg
3278659607e0Smrg#ifdef EXTMOUSEDEBUG
3279659607e0Smrg	for (j = 0; j < 7; j++)
3280659607e0Smrg	    AP_DBGC(("%2.2x ", (unsigned char) para[j]));
3281659607e0Smrg	AP_DBGC(("\n"));
3282659607e0Smrg#endif
3283659607e0Smrg	j = 0;
3284659607e0Smrg	while (1) {
3285659607e0Smrg	    /* look for header */
3286659607e0Smrg	    while (j < mPriv->count) {
3287659607e0Smrg		if (((byte = mPriv->data[j++]) & para[0]) == para[1]){
3288659607e0Smrg		    AP_DBG(("found header %2.2x\n",byte));
3289659607e0Smrg		    next_header_candidate = j;
3290659607e0Smrg		    count = 1;
3291659607e0Smrg		    break;
3292659607e0Smrg		} else {
3293659607e0Smrg		    /*
3294659607e0Smrg		     * Bail ot if number of bytes per package have
3295659607e0Smrg		     * been tested for header.
3296659607e0Smrg		     * Take bytes per package of leading garbage into
3297659607e0Smrg		     * account.
3298659607e0Smrg		     */
3299659607e0Smrg		    if (j > para[4] && ++header_count > para[4]) {
3300659607e0Smrg			j = mPriv->count;
3301659607e0Smrg			break;
3302659607e0Smrg		    }
3303659607e0Smrg		}
3304659607e0Smrg	    }
3305659607e0Smrg	    /* check if remaining data matches protocol */
3306659607e0Smrg	    while (j < mPriv->count) {
3307659607e0Smrg		byte = mPriv->data[j++];
3308659607e0Smrg		if (count == para[4]) {
3309659607e0Smrg		    count = 0;
3310659607e0Smrg		    /* check and eat excess byte */
3311659607e0Smrg		    if (((byte & para[0]) != para[1])
3312659607e0Smrg			&& ((byte & para[5]) == para[6])) {
3313659607e0Smrg			AP_DBG(("excess byte found\n"));
3314659607e0Smrg			continue;
3315659607e0Smrg		    }
3316659607e0Smrg		}
3317659607e0Smrg		if (count == 0) {
3318659607e0Smrg		    /* validate next header */
3319659607e0Smrg		    bad = FALSE;
3320659607e0Smrg		    AP_DBG(("Complete set found\n"));
3321659607e0Smrg		    if ((byte & para[0]) != para[1]) {
3322659607e0Smrg			AP_DBG(("Autoprobe: header bad\n"));
3323659607e0Smrg			bad = TRUE;
3324659607e0Smrg			break;
3325659607e0Smrg		    } else {
3326659607e0Smrg			count++;
3327659607e0Smrg			continue;
3328659607e0Smrg		    }
3329659607e0Smrg		}
3330659607e0Smrg		/* validate data */
3331659607e0Smrg		else if (((byte & para[2]) != para[3])
3332659607e0Smrg			 || ((para[7] & MPF_SAFE)
3333659607e0Smrg			     && ((byte & para[0]) == para[1]))) {
3334659607e0Smrg		    AP_DBG(("data bad\n"));
3335659607e0Smrg		    bad = TRUE;
3336659607e0Smrg		    break;
3337659607e0Smrg		} else {
3338659607e0Smrg		    count ++;
3339659607e0Smrg		    continue;
3340659607e0Smrg		}
3341659607e0Smrg	    }
3342659607e0Smrg	    if (!bad) {
3343659607e0Smrg		/* this is a matching protocol */
3344659607e0Smrg		mPriv->protoList[k++] = prot;
3345659607e0Smrg		AP_DBG(("Autoprobe: Adding protocol %s to list (entry %i)\n",
3346659607e0Smrg			ProtocolIDToName(prot),k-1));
3347659607e0Smrg		break;
3348659607e0Smrg	    }
3349659607e0Smrg	    j = next_header_candidate;
3350659607e0Smrg	    next_header_candidate = 0;
3351659607e0Smrg	    /* we have tested number of bytes per package for header */
3352659607e0Smrg	    if (j > para[4] && ++header_count > para[4])
3353659607e0Smrg		break;
3354659607e0Smrg	    /* we have not found anything that looks like a header */
3355659607e0Smrg	    if (!next_header_candidate)
3356659607e0Smrg		break;
3357659607e0Smrg	    AP_DBG(("Looking for new header\n"));
3358659607e0Smrg	}
3359659607e0Smrg    }
3360659607e0Smrg
3361659607e0Smrg    xf86UnblockSIGIO(blocked);
3362659607e0Smrg
3363659607e0Smrg    mPriv->protoList[k] = PROT_UNKNOWN;
3364659607e0Smrg
3365659607e0Smrg    xfree(tmplist);
3366659607e0Smrg}
3367659607e0Smrg
3368659607e0Smrg
3369659607e0Smrg/* This only needs to be done once */
3370659607e0Smrgvoid **serialDefaultsList = NULL;
3371659607e0Smrg
3372659607e0Smrg/*
3373659607e0Smrg * createSerialDefaultsLists() - create a list of the different default
3374659607e0Smrg * settings for the serial interface of the known protocols.
3375659607e0Smrg */
3376659607e0Smrgstatic void
3377659607e0SmrgcreateSerialDefaultsList(void)
3378659607e0Smrg{
3379659607e0Smrg    int i = 0, j, k;
3380659607e0Smrg
3381659607e0Smrg    serialDefaultsList = (void **)xnfalloc(sizeof(void*));
3382659607e0Smrg    serialDefaultsList[0] = NULL;
3383659607e0Smrg
3384659607e0Smrg    for (j = 0; mouseProtocols[j].name; j++) {
3385659607e0Smrg	if (!mouseProtocols[j].defaults)
3386659607e0Smrg	    continue;
3387659607e0Smrg	for (k = 0; k < i; k++)
3388659607e0Smrg	    if (mouseProtocols[j].defaults == serialDefaultsList[k])
3389659607e0Smrg		continue;
3390659607e0Smrg	i++;
3391659607e0Smrg	serialDefaultsList = (void**)xnfrealloc(serialDefaultsList,
3392659607e0Smrg						sizeof(void*)*(i+1));
3393659607e0Smrg	serialDefaultsList[i-1] = mouseProtocols[j].defaults;
3394659607e0Smrg	serialDefaultsList[i] = NULL;
3395659607e0Smrg    }
3396659607e0Smrg}
3397659607e0Smrg
3398659607e0Smrgtypedef enum {
3399659607e0Smrg    STATE_INVALID,
3400659607e0Smrg    STATE_UNCERTAIN,
3401659607e0Smrg    STATE_VALID
3402659607e0Smrg} validState;
3403659607e0Smrg
3404659607e0Smrg/* Probing threshold values */
3405659607e0Smrg#define PROBE_UNCERTAINTY 50
3406659607e0Smrg#define BAD_CERTAINTY 6
3407659607e0Smrg#define BAD_INC_CERTAINTY 1
3408659607e0Smrg#define BAD_INC_CERTAINTY_WHEN_SYNC_LOST 2
3409659607e0Smrg
3410659607e0Smrgstatic validState
3411659607e0SmrgvalidCount(mousePrivPtr mPriv, Bool inSync, Bool lostSync)
3412659607e0Smrg{
3413659607e0Smrg    if (inSync) {
3414659607e0Smrg	if (!--mPriv->goodCount) {
3415659607e0Smrg	    /* we are sure to have found the correct protocol */
3416659607e0Smrg	    mPriv->badCount = 0;
3417659607e0Smrg	    return STATE_VALID;
3418659607e0Smrg	}
3419659607e0Smrg	AP_DBG(("%i successful rounds to go\n",
3420659607e0Smrg		mPriv->goodCount));
3421659607e0Smrg	return STATE_UNCERTAIN;
3422659607e0Smrg    }
3423659607e0Smrg
3424659607e0Smrg
3425659607e0Smrg    /* We are out of sync again */
3426659607e0Smrg    mPriv->goodCount = PROBE_UNCERTAINTY;
3427659607e0Smrg    /* We increase uncertainty of having the correct protocol */
3428659607e0Smrg    mPriv->badCount+= lostSync ? BAD_INC_CERTAINTY_WHEN_SYNC_LOST
3429659607e0Smrg	: BAD_INC_CERTAINTY;
3430659607e0Smrg
3431659607e0Smrg    if (mPriv->badCount < BAD_CERTAINTY) {
3432659607e0Smrg	/* We are not convinced yet to have the wrong protocol */
3433659607e0Smrg	AP_DBG(("Changing protocol after: %i rounds\n",
3434659607e0Smrg		BAD_CERTAINTY - mPriv->badCount));
3435659607e0Smrg	return STATE_UNCERTAIN;
3436659607e0Smrg    }
3437659607e0Smrg    return STATE_INVALID;
3438659607e0Smrg}
3439659607e0Smrg
3440659607e0Smrg#define RESET_VALIDATION	mPriv->goodCount = PROBE_UNCERTAINTY;\
3441659607e0Smrg				mPriv->badCount = 0;\
3442659607e0Smrg				mPriv->prevDx = 0;\
3443659607e0Smrg				mPriv->prevDy = 0;\
3444659607e0Smrg				mPriv->accDx = 0;\
3445659607e0Smrg				mPriv->accDy = 0;\
3446659607e0Smrg				mPriv->acc = 0;
3447659607e0Smrg
3448659607e0Smrgstatic void
3449659607e0SmrgautoProbeMouse(InputInfoPtr pInfo, Bool inSync, Bool lostSync)
3450659607e0Smrg{
3451659607e0Smrg    MouseDevPtr pMse = pInfo->private;
3452659607e0Smrg    mousePrivPtr mPriv = (mousePrivPtr)pMse->mousePriv;
3453659607e0Smrg
3454659607e0Smrg    MouseProtocolID *protocolList = NULL;
3455659607e0Smrg
3456659607e0Smrg    while (1) {
3457659607e0Smrg	switch (mPriv->autoState) {
3458659607e0Smrg	case AUTOPROBE_GOOD:
3459659607e0Smrg 	    if (inSync)
3460659607e0Smrg		return;
3461659607e0Smrg	    AP_DBG(("State GOOD\n"));
3462659607e0Smrg	    RESET_VALIDATION;
3463659607e0Smrg	    mPriv->autoState = AUTOPROBE_VALIDATE1;
3464659607e0Smrg	    return;
3465659607e0Smrg	case AUTOPROBE_H_GOOD:
3466659607e0Smrg	    if (inSync)
3467659607e0Smrg		return;
3468659607e0Smrg	    AP_DBG(("State H_GOOD\n"));
3469659607e0Smrg	    RESET_VALIDATION;
3470659607e0Smrg	    mPriv->autoState = AUTOPROBE_H_VALIDATE2;
3471659607e0Smrg	    return;
3472659607e0Smrg	case AUTOPROBE_H_NOPROTO:
3473659607e0Smrg	    AP_DBG(("State H_NOPROTO\n"));
3474659607e0Smrg	    mPriv->protocolID = 0;
3475659607e0Smrg	    mPriv->autoState = AUTOPROBE_H_SETPROTO;
3476659607e0Smrg	    break;
3477659607e0Smrg	case AUTOPROBE_H_SETPROTO:
3478659607e0Smrg	    AP_DBG(("State H_SETPROTO\n"));
3479659607e0Smrg	    if ((pMse->protocolID = hardProtocolList[mPriv->protocolID++])
3480659607e0Smrg		== PROT_UNKNOWN) {
3481659607e0Smrg		mPriv->protocolID = 0;
3482659607e0Smrg		break;
3483659607e0Smrg	    } else if (GetProtocol(pMse->protocolID) &&  SetupMouse(pInfo)) {
3484659607e0Smrg		FlushButtons(pMse);
3485659607e0Smrg		RESET_VALIDATION;
3486659607e0Smrg		AP_DBG(("Autoprobe: Trying Protocol: %s\n",
3487659607e0Smrg			ProtocolIDToName(pMse->protocolID)));
3488659607e0Smrg		mPriv->autoState = AUTOPROBE_H_VALIDATE1;
3489659607e0Smrg		return;
3490659607e0Smrg	    }
3491659607e0Smrg	    break;
3492659607e0Smrg	case AUTOPROBE_H_VALIDATE1:
3493659607e0Smrg	    AP_DBG(("State H_VALIDATE1\n"));
3494659607e0Smrg	    switch (validCount(mPriv,inSync,lostSync)) {
3495659607e0Smrg	    case STATE_INVALID:
3496659607e0Smrg		mPriv->autoState = AUTOPROBE_H_SETPROTO;
3497659607e0Smrg		break;
3498659607e0Smrg	    case STATE_VALID:
3499659607e0Smrg		    xf86Msg(X_INFO,"Mouse autoprobe: selecting %s protocol\n",
3500659607e0Smrg			    ProtocolIDToName(pMse->protocolID));
3501659607e0Smrg		    mPriv->autoState = AUTOPROBE_H_GOOD;
3502659607e0Smrg		    return;
3503659607e0Smrg	    case STATE_UNCERTAIN:
3504659607e0Smrg		return;
3505659607e0Smrg	    default:
3506659607e0Smrg		break;
3507659607e0Smrg	    }
3508659607e0Smrg	    break;
3509659607e0Smrg	case AUTOPROBE_H_VALIDATE2:
3510659607e0Smrg	    AP_DBG(("State H_VALIDATE2\n"));
3511659607e0Smrg	    switch (validCount(mPriv,inSync,lostSync)) {
3512659607e0Smrg	    case STATE_INVALID:
3513659607e0Smrg		mPriv->autoState = AUTOPROBE_H_AUTODETECT;
3514659607e0Smrg		break;
3515659607e0Smrg	    case STATE_VALID:
3516659607e0Smrg		xf86Msg(X_INFO,"Mouse autoprobe: selecting %s protocol\n",
3517659607e0Smrg			ProtocolIDToName(pMse->protocolID));
3518659607e0Smrg		mPriv->autoState = AUTOPROBE_H_GOOD;
3519659607e0Smrg		return;
3520659607e0Smrg	    case STATE_UNCERTAIN:
3521659607e0Smrg		return;
3522659607e0Smrg	    }
3523659607e0Smrg	    break;
3524659607e0Smrg	case AUTOPROBE_H_AUTODETECT:
3525659607e0Smrg	    AP_DBG(("State H_AUTODETECT\n"));
3526659607e0Smrg	    pMse->protocolID = PROT_AUTO;
3527659607e0Smrg	    AP_DBG(("Looking for PnP/OS mouse\n"));
3528659607e0Smrg	    mPriv->count = 0;
3529659607e0Smrg	    SetupMouse(pInfo);
3530659607e0Smrg	    if (pMse->protocolID != PROT_AUTO)
3531659607e0Smrg		mPriv->autoState = AUTOPROBE_H_GOOD;
3532659607e0Smrg	    else
3533659607e0Smrg		mPriv->autoState = AUTOPROBE_H_NOPROTO;
3534659607e0Smrg	    break;
3535659607e0Smrg	case AUTOPROBE_NOPROTO:
3536659607e0Smrg	    AP_DBG(("State NOPROTO\n"));
3537659607e0Smrg	    mPriv->count = 0;
3538659607e0Smrg	    mPriv->serialDefaultsNum = -1;
3539659607e0Smrg	    mPriv->autoState = AUTOPROBE_COLLECT;
3540659607e0Smrg	    break;
3541659607e0Smrg	case AUTOPROBE_COLLECT:
3542659607e0Smrg	    AP_DBG(("State COLLECT\n"));
3543659607e0Smrg	    if (mPriv->count <= NUM_MSE_AUTOPROBE_BYTES)
3544659607e0Smrg		return;
3545659607e0Smrg	    protocolList = softProtocolList;
3546659607e0Smrg	    mPriv->autoState = AUTOPROBE_CREATE_PROTOLIST;
3547659607e0Smrg	    break;
3548659607e0Smrg	case AUTOPROBE_CREATE_PROTOLIST:
3549659607e0Smrg	    AP_DBG(("State CREATE_PROTOLIST\n"));
3550659607e0Smrg	    createProtoList(pMse, protocolList);
3551659607e0Smrg	    mPriv->protocolID = 0;
3552659607e0Smrg	    mPriv->autoState = AUTOPROBE_SWITCH_PROTOCOL;
3553659607e0Smrg	    break;
3554659607e0Smrg	case AUTOPROBE_AUTODETECT:
3555659607e0Smrg	    AP_DBG(("State AUTODETECT\n"));
3556659607e0Smrg	    pMse->protocolID = PROT_AUTO;
3557659607e0Smrg	    AP_DBG(("Looking for PnP/OS mouse\n"));
3558659607e0Smrg	    mPriv->count = 0;
3559659607e0Smrg	    SetupMouse(pInfo);
3560659607e0Smrg	    if (pMse->protocolID != PROT_AUTO)
3561659607e0Smrg		mPriv->autoState = AUTOPROBE_GOOD;
3562659607e0Smrg	    else
3563659607e0Smrg		mPriv->autoState = AUTOPROBE_NOPROTO;
3564659607e0Smrg	    break;
3565659607e0Smrg	case AUTOPROBE_VALIDATE1:
3566659607e0Smrg	    AP_DBG(("State VALIDATE1\n"));
3567659607e0Smrg	    switch (validCount(mPriv,inSync,lostSync)) {
3568659607e0Smrg	    case STATE_INVALID:
3569659607e0Smrg		mPriv->autoState = AUTOPROBE_AUTODETECT;
3570659607e0Smrg		break;
3571659607e0Smrg	    case STATE_VALID:
3572659607e0Smrg		xf86Msg(X_INFO,"Mouse autoprobe: selecting %s protocol\n",
3573659607e0Smrg			ProtocolIDToName(pMse->protocolID));
3574659607e0Smrg		mPriv->autoState = AUTOPROBE_GOOD;
3575659607e0Smrg		break;
3576659607e0Smrg	    case STATE_UNCERTAIN:
3577659607e0Smrg		return;
3578659607e0Smrg	    }
3579659607e0Smrg	    break;
3580659607e0Smrg	case AUTOPROBE_VALIDATE2:
3581659607e0Smrg	    AP_DBG(("State VALIDATE2\n"));
3582659607e0Smrg	    switch (validCount(mPriv,inSync,lostSync)) {
3583659607e0Smrg	    case STATE_INVALID:
3584659607e0Smrg		protocolList = &mPriv->protoList[mPriv->protocolID];
3585659607e0Smrg		mPriv->autoState = AUTOPROBE_CREATE_PROTOLIST;
3586659607e0Smrg		break;
3587659607e0Smrg	    case STATE_VALID:
3588659607e0Smrg		xf86Msg(X_INFO,"Mouse autoprobe: selecting %s protocol\n",
3589659607e0Smrg			ProtocolIDToName(pMse->protocolID));
3590659607e0Smrg		mPriv->autoState = AUTOPROBE_GOOD;
3591659607e0Smrg		break;
3592659607e0Smrg	    case STATE_UNCERTAIN:
3593659607e0Smrg		return;
3594659607e0Smrg	    }
3595659607e0Smrg	    break;
3596659607e0Smrg	case AUTOPROBE_SWITCHSERIAL:
3597659607e0Smrg	{
3598659607e0Smrg	    pointer serialDefaults;
3599659607e0Smrg	    AP_DBG(("State SWITCHSERIAL\n"));
3600659607e0Smrg
3601659607e0Smrg	    if (!serialDefaultsList)
3602659607e0Smrg		createSerialDefaultsList();
3603659607e0Smrg
3604659607e0Smrg	    AP_DBG(("Switching serial params\n"));
3605659607e0Smrg	    if ((serialDefaults =
3606659607e0Smrg		 serialDefaultsList[++mPriv->serialDefaultsNum]) == NULL) {
3607659607e0Smrg		mPriv->serialDefaultsNum = 0;
3608659607e0Smrg	    } else {
3609659607e0Smrg		pointer tmp = xf86OptionListCreate(serialDefaults, -1, 0);
3610659607e0Smrg		xf86SetSerial(pInfo->fd, tmp);
3611659607e0Smrg		xf86OptionListFree(tmp);
3612659607e0Smrg		mPriv->count = 0;
3613659607e0Smrg		mPriv->autoState = AUTOPROBE_COLLECT;
3614659607e0Smrg	    }
3615659607e0Smrg	    break;
3616659607e0Smrg	}
3617659607e0Smrg	case AUTOPROBE_SWITCH_PROTOCOL:
3618659607e0Smrg	{
3619659607e0Smrg	    MouseProtocolID proto;
3620659607e0Smrg	    void *defaults;
3621659607e0Smrg	    AP_DBG(("State SWITCH_PROTOCOL\n"));
3622659607e0Smrg	    proto = mPriv->protoList[mPriv->protocolID++];
3623659607e0Smrg	    if (proto == PROT_UNKNOWN)
3624659607e0Smrg		mPriv->autoState = AUTOPROBE_SWITCHSERIAL;
3625659607e0Smrg	    else if (!(defaults = GetProtocol(proto)->defaults)
3626659607e0Smrg		       || (mPriv->serialDefaultsNum == -1
3627659607e0Smrg			   && (defaults == msDefaults))
3628659607e0Smrg		       || (mPriv->serialDefaultsNum != -1
3629659607e0Smrg			   && serialDefaultsList[mPriv->serialDefaultsNum]
3630659607e0Smrg			   == defaults)) {
3631659607e0Smrg		AP_DBG(("Changing Protocol to %s\n",
3632659607e0Smrg			ProtocolIDToName(proto)));
3633659607e0Smrg		SetMouseProto(pMse,proto);
3634659607e0Smrg		FlushButtons(pMse);
3635659607e0Smrg		RESET_VALIDATION;
3636659607e0Smrg		mPriv->autoState = AUTOPROBE_VALIDATE2;
3637659607e0Smrg		return;
3638659607e0Smrg	    }
3639659607e0Smrg	    break;
3640659607e0Smrg	}
3641659607e0Smrg	}
3642659607e0Smrg    }
3643659607e0Smrg}
3644659607e0Smrg
3645659607e0Smrgstatic Bool
3646659607e0SmrgautoGood(MouseDevPtr pMse)
3647659607e0Smrg{
3648659607e0Smrg    mousePrivPtr mPriv = (mousePrivPtr)pMse->mousePriv;
3649659607e0Smrg
3650659607e0Smrg    if (!pMse->autoProbe)
3651659607e0Smrg	return TRUE;
3652659607e0Smrg
3653659607e0Smrg    switch (mPriv->autoState) {
3654659607e0Smrg    case AUTOPROBE_GOOD:
3655659607e0Smrg    case AUTOPROBE_H_GOOD:
3656659607e0Smrg	return TRUE;
3657659607e0Smrg    case AUTOPROBE_VALIDATE1: /* @@@ */
3658659607e0Smrg    case AUTOPROBE_H_VALIDATE1: /* @@@ */
3659659607e0Smrg    case AUTOPROBE_VALIDATE2:
3660659607e0Smrg    case AUTOPROBE_H_VALIDATE2:
3661659607e0Smrg	if (mPriv->goodCount < PROBE_UNCERTAINTY/2)
3662659607e0Smrg	    return TRUE;
3663659607e0Smrg    default:
3664659607e0Smrg	return FALSE;
3665659607e0Smrg    }
3666659607e0Smrg}
3667659607e0Smrg
3668659607e0Smrg
3669659607e0Smrg#define TOT_THRESHOLD 3000
3670659607e0Smrg#define VAL_THRESHOLD 40
3671659607e0Smrg
3672659607e0Smrg/*
3673659607e0Smrg * checkForErraticMovements() -- check if mouse 'jumps around'.
3674659607e0Smrg */
3675659607e0Smrgstatic void
3676659607e0SmrgcheckForErraticMovements(InputInfoPtr pInfo, int dx, int dy)
3677659607e0Smrg{
3678659607e0Smrg    MouseDevPtr pMse = pInfo->private;
3679659607e0Smrg    mousePrivPtr mPriv = (mousePrivPtr)pMse->mousePriv;
3680659607e0Smrg#if 1
3681659607e0Smrg    if (!mPriv->goodCount)
3682659607e0Smrg	return;
3683659607e0Smrg#endif
3684659607e0Smrg#if 0
3685659607e0Smrg    if (abs(dx - mPriv->prevDx) > 300
3686659607e0Smrg	|| abs(dy - mPriv->prevDy) > 300)
3687659607e0Smrg	AP_DBG(("erratic1 behaviour\n"));
3688659607e0Smrg#endif
3689659607e0Smrg    if (abs(dx) > VAL_THRESHOLD) {
3690659607e0Smrg	if (sign(dx) == sign(mPriv->prevDx)) {
3691659607e0Smrg	    mPriv->accDx += dx;
3692659607e0Smrg	    if (abs(mPriv->accDx) > mPriv->acc) {
3693659607e0Smrg		mPriv->acc = abs(mPriv->accDx);
3694659607e0Smrg		AP_DBG(("acc=%i\n",mPriv->acc));
3695659607e0Smrg	    }
3696659607e0Smrg	    else
3697659607e0Smrg		AP_DBG(("accDx=%i\n",mPriv->accDx));
3698659607e0Smrg	} else {
3699659607e0Smrg	    mPriv->accDx = 0;
3700659607e0Smrg	}
3701659607e0Smrg    }
3702659607e0Smrg
3703659607e0Smrg    if (abs(dy) > VAL_THRESHOLD) {
3704659607e0Smrg	if (sign(dy) == sign(mPriv->prevDy)) {
3705659607e0Smrg	    mPriv->accDy += dy;
3706659607e0Smrg	    if (abs(mPriv->accDy) > mPriv->acc) {
3707659607e0Smrg		mPriv->acc = abs(mPriv->accDy);
3708659607e0Smrg		AP_DBG(("acc: %i\n",mPriv->acc));
3709659607e0Smrg	    } else
3710659607e0Smrg		AP_DBG(("accDy=%i\n",mPriv->accDy));
3711659607e0Smrg	} else {
3712659607e0Smrg	    mPriv->accDy = 0;
3713659607e0Smrg	}
3714659607e0Smrg    }
3715659607e0Smrg    mPriv->prevDx = dx;
3716659607e0Smrg    mPriv->prevDy = dy;
3717659607e0Smrg    if (mPriv->acc > TOT_THRESHOLD) {
3718659607e0Smrg	mPriv->goodCount = PROBE_UNCERTAINTY;
3719659607e0Smrg	mPriv->prevDx = 0;
3720659607e0Smrg	mPriv->prevDy = 0;
3721659607e0Smrg	mPriv->accDx = 0;
3722659607e0Smrg	mPriv->accDy = 0;
3723659607e0Smrg	mPriv->acc = 0;
3724659607e0Smrg	AP_DBG(("erratic2 behaviour\n"));
3725659607e0Smrg	autoProbeMouse(pInfo, FALSE,TRUE);
3726659607e0Smrg    }
3727659607e0Smrg}
3728659607e0Smrg
3729659607e0Smrgstatic void
3730659607e0SmrgSetMouseProto(MouseDevPtr pMse, MouseProtocolID protocolID)
3731659607e0Smrg{
3732659607e0Smrg    pMse->protocolID = protocolID;
3733659607e0Smrg    pMse->protocol = ProtocolIDToName(pMse->protocolID);
3734659607e0Smrg    pMse->class = ProtocolIDToClass(pMse->protocolID);
3735659607e0Smrg    if ((pMse->protocolID >= 0) && (pMse->protocolID < PROT_NUMPROTOS))
3736659607e0Smrg	memcpy(pMse->protoPara, proto[pMse->protocolID],
3737659607e0Smrg	       sizeof(pMse->protoPara));
3738659607e0Smrg
3739659607e0Smrg    if (pMse->emulate3ButtonsSoft)
3740659607e0Smrg	pMse->emulate3Buttons = TRUE;
3741659607e0Smrg}
3742659607e0Smrg
3743659607e0Smrg/*
3744659607e0Smrg * collectData() -- collect data bytes sent by mouse.
3745659607e0Smrg */
3746659607e0Smrgstatic Bool
3747659607e0SmrgcollectData(MouseDevPtr pMse, unsigned char u)
3748659607e0Smrg{
3749659607e0Smrg    mousePrivPtr mPriv = (mousePrivPtr)pMse->mousePriv;
3750659607e0Smrg    if (mPriv->count < NUM_MSE_AUTOPROBE_TOTAL) {
3751659607e0Smrg	mPriv->data[mPriv->count++] = u;
3752659607e0Smrg	if (mPriv->count <= NUM_MSE_AUTOPROBE_BYTES) {
3753659607e0Smrg		return TRUE;
3754659607e0Smrg	}
3755659607e0Smrg    }
3756659607e0Smrg    return FALSE;
3757659607e0Smrg}
3758659607e0Smrg
3759659607e0Smrg/**************** end of autoprobe stuff *****************/
3760659607e0Smrg
3761659607e0Smrg
3762659607e0Smrg
3763659607e0Smrg#ifdef XFree86LOADER
3764659607e0SmrgModuleInfoRec MouseInfo = {
3765659607e0Smrg    1,
3766659607e0Smrg    "MOUSE",
3767659607e0Smrg    NULL,
3768659607e0Smrg    0,
3769659607e0Smrg    MouseAvailableOptions,
3770659607e0Smrg};
3771659607e0Smrg
3772659607e0Smrgstatic void
3773659607e0Smrgxf86MouseUnplug(pointer	p)
3774659607e0Smrg{
3775659607e0Smrg}
3776659607e0Smrgstatic pointer
3777659607e0Smrgxf86MousePlug(pointer	module,
3778659607e0Smrg	    pointer	options,
3779659607e0Smrg	    int		*errmaj,
3780659607e0Smrg	    int		*errmin)
3781659607e0Smrg{
3782659607e0Smrg    static Bool Initialised = FALSE;
3783659607e0Smrg
3784659607e0Smrg    if (!Initialised) {
3785659607e0Smrg	Initialised = TRUE;
3786659607e0Smrg#ifndef REMOVE_LOADER_CHECK_MODULE_INFO
3787659607e0Smrg	if (xf86LoaderCheckSymbol("xf86AddModuleInfo"))
3788659607e0Smrg#endif
3789659607e0Smrg	xf86AddModuleInfo(&MouseInfo, module);
3790659607e0Smrg    }
3791659607e0Smrg
3792659607e0Smrg    xf86AddInputDriver(&MOUSE, module, 0);
3793659607e0Smrg
3794659607e0Smrg    return module;
3795659607e0Smrg}
3796659607e0Smrg
3797659607e0Smrgstatic XF86ModuleVersionInfo xf86MouseVersionRec =
3798659607e0Smrg{
3799659607e0Smrg    "mouse",
3800659607e0Smrg    MODULEVENDORSTRING,
3801659607e0Smrg    MODINFOSTRING1,
3802659607e0Smrg    MODINFOSTRING2,
3803659607e0Smrg    XORG_VERSION_CURRENT,
3804659607e0Smrg    PACKAGE_VERSION_MAJOR, PACKAGE_VERSION_MINOR, PACKAGE_VERSION_PATCHLEVEL,
3805659607e0Smrg    ABI_CLASS_XINPUT,
3806659607e0Smrg    ABI_XINPUT_VERSION,
3807659607e0Smrg    MOD_CLASS_XINPUT,
3808659607e0Smrg    {0, 0, 0, 0}		/* signature, to be patched into the file by */
3809659607e0Smrg				/* a tool */
3810659607e0Smrg};
3811659607e0Smrg
3812659607e0Smrg_X_EXPORT XF86ModuleData mouseModuleData = {
3813659607e0Smrg    &xf86MouseVersionRec,
3814659607e0Smrg    xf86MousePlug,
3815659607e0Smrg    xf86MouseUnplug
3816659607e0Smrg};
3817659607e0Smrg
3818659607e0Smrg/*
3819659607e0Smrg  Look at hitachi device stuff.
3820659607e0Smrg*/
3821659607e0Smrg#endif /* XFree86LOADER */
3822659607e0Smrg
3823659607e0Smrg
3824