mouse.c revision c508c3da
1659607e0Smrg/*
2659607e0Smrg *
3659607e0Smrg * Copyright 1990,91 by Thomas Roell, Dinkelscherben, Germany.
4659607e0Smrg * Copyright 1993 by David Dawes <dawes@xfree86.org>
5659607e0Smrg * Copyright 2002 by SuSE Linux AG, Author: Egbert Eich
6659607e0Smrg * Copyright 1994-2002 by The XFree86 Project, Inc.
7659607e0Smrg * Copyright 2002 by Paul Elliott
8659607e0Smrg *
9659607e0Smrg * Permission to use, copy, modify, distribute, and sell this software and its
10659607e0Smrg * documentation for any purpose is hereby granted without fee, provided that
11659607e0Smrg * the above copyright notice appear in all copies and that both that
12659607e0Smrg * copyright notice and this permission notice appear in supporting
13659607e0Smrg * documentation, and that the names of copyright holders not be
14659607e0Smrg * used in advertising or publicity pertaining to distribution of the
15659607e0Smrg * software without specific, written prior permission.  The copyright holders
16659607e0Smrg * make no representations about the suitability of this
17659607e0Smrg * software for any purpose.  It is provided "as is" without express or
18659607e0Smrg * implied warranty.
19659607e0Smrg *
20659607e0Smrg * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
21659607e0Smrg * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
22659607e0Smrg * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
23659607e0Smrg * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
24659607e0Smrg * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
25659607e0Smrg * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
26659607e0Smrg * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27659607e0Smrg *
28659607e0Smrg */
29659607e0Smrg/* Patch for PS/2 Intellimouse - Tim Goodwin 1997-11-06. */
30659607e0Smrg
31659607e0Smrg/*
32659607e0Smrg * [JCH-96/01/21] Added fourth button support for PROT_GLIDEPOINT mouse
33659607e0Smrg * protocol.
34659607e0Smrg */
35659607e0Smrg
36659607e0Smrg/*
37659607e0Smrg * [TVO-97/03/05] Added microsoft IntelliMouse support
38659607e0Smrg */
39659607e0Smrg
40659607e0Smrg/*
41659607e0Smrg * [PME-02/08/11] Added suport for drag lock buttons
42659607e0Smrg * for use with 4 button trackballs for convenience
43659607e0Smrg * and to help limited dexterity persons
44659607e0Smrg */
45659607e0Smrg
46659607e0Smrg#ifdef HAVE_CONFIG_H
47659607e0Smrg#include "config.h"
48659607e0Smrg#endif
49659607e0Smrg
50f5c3fc63Smrg#include <xorg-server.h>
51659607e0Smrg#include <math.h>
52659607e0Smrg#include <string.h>
53659607e0Smrg#include <stdio.h>
54659607e0Smrg#include <stdlib.h>
55659607e0Smrg#include <X11/X.h>
56659607e0Smrg
57659607e0Smrg#include "xf86.h"
58659607e0Smrg
59659607e0Smrg#include <X11/extensions/XI.h>
60659607e0Smrg#include "extnsionst.h"
61659607e0Smrg#include "extinit.h"
62659607e0Smrg
63659607e0Smrg#include "xf86Xinput.h"
64659607e0Smrg#include "xf86_OSproc.h"
651450c749Smrg#include "exevents.h"
661450c749Smrg#include <X11/Xatom.h>
671450c749Smrg#include "xserver-properties.h"
681450c749Smrg#include "xf86-mouse-properties.h"
69659607e0Smrg
705ddc3750Smrg#ifdef __NetBSD__
715ddc3750Smrg#include <time.h>
725ddc3750Smrg#include <dev/wscons/wsconsio.h>
733e41e457Schristos#include <sys/ioctl.h>
745ddc3750Smrg#endif
755ddc3750Smrg
76659607e0Smrg#include "compiler.h"
77659607e0Smrg
78659607e0Smrg#include "xisb.h"
79659607e0Smrg#include "mouse.h"
80659607e0Smrg#include "mousePriv.h"
81659607e0Smrg#include "mipointer.h"
82659607e0Smrg
83659607e0Smrgenum {
84659607e0Smrg    /* number of bits in mapped nibble */
85659607e0Smrg    NIB_BITS=4,
86659607e0Smrg    /* size of map of nibbles to bitmask */
87659607e0Smrg    NIB_SIZE= (1 << NIB_BITS),
88659607e0Smrg    /* mask for map */
89659607e0Smrg    NIB_MASK= (NIB_SIZE -1),
90659607e0Smrg    /* number of maps to map all the buttons */
91659607e0Smrg    NIB_COUNT = ((MSE_MAXBUTTONS+NIB_BITS-1)/NIB_BITS)
92659607e0Smrg};
93659607e0Smrg
94659607e0Smrg/*data to be used in implementing trackball drag locks.*/
95659607e0Smrgtypedef struct _DragLockRec {
96659607e0Smrg
97659607e0Smrg    /* Fields used to implement trackball drag locks. */
98659607e0Smrg    /* mask for those buttons that are ordinary drag lock buttons */
99659607e0Smrg    int lockButtonsM;
100659607e0Smrg
101659607e0Smrg    /* mask for the master drag lock button if any */
102659607e0Smrg    int masterLockM;
103659607e0Smrg
104659607e0Smrg    /* button state up/down from last time adjusted for drag locks */
105659607e0Smrg    int lockLastButtons;
106659607e0Smrg
107659607e0Smrg    /*
108659607e0Smrg     * true if master lock state i.e. master drag lock
109659607e0Smrg     * button has just been pressed
110659607e0Smrg     */
111659607e0Smrg    int masterTS;
112659607e0Smrg
113659607e0Smrg    /* simulate these buttons being down although they are not */
114659607e0Smrg    int simulatedDown;
115659607e0Smrg
116659607e0Smrg    /*
117659607e0Smrg     * data to map bits for drag lock buttons to corresponding
118659607e0Smrg     * bits for the target buttons
119659607e0Smrg     */
120659607e0Smrg    int nib_table[NIB_COUNT][NIB_SIZE];
121659607e0Smrg
122659607e0Smrg} DragLockRec, *DragLockPtr;
123659607e0Smrg
124659607e0Smrg
125dd4cbfe8Smrg#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) < 12
126659607e0Smrgstatic InputInfoPtr MousePreInit(InputDriverPtr drv, IDevPtr dev, int flags);
127dd4cbfe8Smrg#else
128dd4cbfe8Smrgstatic int MousePreInit(InputDriverPtr drv, InputInfoPtr pInfo, int flags);
129659607e0Smrg#endif
130659607e0Smrg
131659607e0Smrgstatic int MouseProc(DeviceIntPtr device, int what);
132659607e0Smrgstatic void MouseCtrl(DeviceIntPtr device, PtrCtrl *ctrl);
133659607e0Smrgstatic void MousePostEvent(InputInfoPtr pInfo, int buttons,
1341450c749Smrg                           int dx, int dy, int dz, int dw);
135659607e0Smrgstatic void MouseReadInput(InputInfoPtr pInfo);
136659607e0Smrgstatic void MouseBlockHandler(pointer data, struct timeval **waitTime,
1371450c749Smrg                              pointer LastSelectMask);
138659607e0Smrgstatic void MouseWakeupHandler(pointer data, int i, pointer LastSelectMask);
139659607e0Smrgstatic void FlushButtons(MouseDevPtr pMse);
140659607e0Smrg
141659607e0Smrgstatic Bool SetupMouse(InputInfoPtr pInfo);
142659607e0Smrgstatic Bool initMouseHW(InputInfoPtr pInfo);
143659607e0Smrg#ifdef SUPPORT_MOUSE_RESET
144659607e0Smrgstatic Bool mouseReset(InputInfoPtr pInfo, unsigned char val);
145659607e0Smrgstatic void ps2WakeupHandler(pointer data, int i, pointer LastSelectMask);
146659607e0Smrgstatic void ps2BlockHandler(pointer data, struct timeval **waitTime,
1471450c749Smrg                            pointer LastSelectMask);
148659607e0Smrg#endif
1491450c749Smrgstatic void Emulate3ButtonsSetEnabled(InputInfoPtr pInfo, Bool enable);
150659607e0Smrg
151659607e0Smrg/* mouse autoprobe stuff */
152659607e0Smrgstatic const char *autoOSProtocol(InputInfoPtr pInfo, int *protoPara);
153659607e0Smrgstatic void autoProbeMouse(InputInfoPtr pInfo, Bool inSync, Bool lostSync);
154659607e0Smrgstatic void checkForErraticMovements(InputInfoPtr pInfo, int dx, int dy);
155659607e0Smrgstatic Bool collectData(MouseDevPtr pMse, unsigned char u);
156659607e0Smrgstatic void SetMouseProto(MouseDevPtr pMse, MouseProtocolID protocolID);
157659607e0Smrgstatic Bool autoGood(MouseDevPtr pMse);
158659607e0Smrg
159659607e0Smrg#undef MOUSE
160659607e0Smrg_X_EXPORT InputDriverRec MOUSE = {
1611450c749Smrg        1,
1621450c749Smrg        "mouse",
1631450c749Smrg        NULL,
1641450c749Smrg        MousePreInit,
1651450c749Smrg        NULL,
1661450c749Smrg        NULL,
167659607e0Smrg};
168659607e0Smrg
169659607e0Smrg#define RETRY_COUNT 4
170659607e0Smrg
1711450c749Smrg/* Properties that can be set at runtime via xinput */
1721450c749Smrgstatic Atom prop_mbemu     = 0; /* Middle button emulation on/off property */
1731450c749Smrgstatic Atom prop_mbtimeout = 0; /* Middle button timeout property */
1741450c749Smrg
175659607e0Smrg/*
176659607e0Smrg * Microsoft (all serial models), Logitech MouseMan, First Mouse, etc,
177659607e0Smrg * ALPS GlidePoint, Thinking Mouse.
178659607e0Smrg */
179659607e0Smrgstatic const char *msDefaults[] = {
1801450c749Smrg        "BaudRate",     "1200",
1811450c749Smrg        "DataBits",     "7",
1821450c749Smrg        "StopBits",     "1",
1831450c749Smrg        "Parity",       "None",
1841450c749Smrg        "FlowControl",  "None",
1851450c749Smrg        "VTime",        "0",
1861450c749Smrg        "VMin",         "1",
1871450c749Smrg        NULL
188659607e0Smrg};
189659607e0Smrg/* MouseSystems */
190659607e0Smrgstatic const char *mlDefaults[] = {
1911450c749Smrg        "BaudRate",     "1200",
1921450c749Smrg        "DataBits",     "8",
1931450c749Smrg        "StopBits",     "2",
1941450c749Smrg        "Parity",       "None",
1951450c749Smrg        "FlowControl",  "None",
1961450c749Smrg        "VTime",        "0",
1971450c749Smrg        "VMin",         "1",
1981450c749Smrg        NULL
199659607e0Smrg};
200659607e0Smrg/* MMSeries */
201659607e0Smrgstatic const char *mmDefaults[] = {
2021450c749Smrg        "BaudRate",     "1200",
2031450c749Smrg        "DataBits",     "8",
2041450c749Smrg        "StopBits",     "1",
2051450c749Smrg        "Parity",       "Odd",
2061450c749Smrg        "FlowControl",  "None",
2071450c749Smrg        "VTime",        "0",
2081450c749Smrg        "VMin",         "1",
2091450c749Smrg        NULL
210659607e0Smrg};
211659607e0Smrg/* Hitachi Tablet */
212659607e0Smrgstatic const char *mmhitDefaults[] = {
2131450c749Smrg        "BaudRate",     "1200",
2141450c749Smrg        "DataBits",     "8",
2151450c749Smrg        "StopBits",     "1",
2161450c749Smrg        "Parity",       "None",
2171450c749Smrg        "FlowControl",  "None",
2181450c749Smrg        "VTime",        "0",
2191450c749Smrg        "VMin",         "1",
2201450c749Smrg        NULL
221659607e0Smrg};
222659607e0Smrg/* AceCad Tablet */
223659607e0Smrgstatic const char *acecadDefaults[] = {
2241450c749Smrg        "BaudRate",     "9600",
2251450c749Smrg        "DataBits",     "8",
2261450c749Smrg        "StopBits",     "1",
2271450c749Smrg        "Parity",       "Odd",
2281450c749Smrg        "FlowControl",  "None",
2291450c749Smrg        "VTime",        "0",
2301450c749Smrg        "VMin",         "1",
2311450c749Smrg        NULL
232659607e0Smrg};
233659607e0Smrg
234659607e0Smrgstatic MouseProtocolRec mouseProtocols[] = {
235659607e0Smrg
236659607e0Smrg    /* Serial protocols */
2371450c749Smrg    { "Microsoft",              MSE_SERIAL,     msDefaults,     PROT_MS },
2381450c749Smrg    { "MouseSystems",           MSE_SERIAL,     mlDefaults,     PROT_MSC },
2391450c749Smrg    { "MMSeries",               MSE_SERIAL,     mmDefaults,     PROT_MM },
2401450c749Smrg    { "Logitech",               MSE_SERIAL,     mlDefaults,     PROT_LOGI },
2411450c749Smrg    { "MouseMan",               MSE_SERIAL,     msDefaults,     PROT_LOGIMAN },
2421450c749Smrg    { "MMHitTab",               MSE_SERIAL,     mmhitDefaults,  PROT_MMHIT },
2431450c749Smrg    { "GlidePoint",             MSE_SERIAL,     msDefaults,     PROT_GLIDE },
2441450c749Smrg    { "IntelliMouse",           MSE_SERIAL,     msDefaults,     PROT_IMSERIAL },
2451450c749Smrg    { "ThinkingMouse",          MSE_SERIAL,     msDefaults,     PROT_THINKING },
2461450c749Smrg    { "AceCad",                 MSE_SERIAL,     acecadDefaults, PROT_ACECAD },
2471450c749Smrg    { "ValuMouseScroll",        MSE_SERIAL,     msDefaults,     PROT_VALUMOUSESCROLL },
248659607e0Smrg
249659607e0Smrg    /* Standard PS/2 */
2501450c749Smrg    { "PS/2",                   MSE_PS2,        NULL,           PROT_PS2 },
2511450c749Smrg    { "GenericPS/2",            MSE_PS2,        NULL,           PROT_GENPS2 },
252659607e0Smrg
253659607e0Smrg    /* Extended PS/2 */
2541450c749Smrg    { "ImPS/2",                 MSE_XPS2,       NULL,           PROT_IMPS2 },
2551450c749Smrg    { "ExplorerPS/2",           MSE_XPS2,       NULL,           PROT_EXPPS2 },
2561450c749Smrg    { "ThinkingMousePS/2",      MSE_XPS2,       NULL,           PROT_THINKPS2 },
2571450c749Smrg    { "MouseManPlusPS/2",       MSE_XPS2,       NULL,           PROT_MMPS2 },
2581450c749Smrg    { "GlidePointPS/2",         MSE_XPS2,       NULL,           PROT_GLIDEPS2 },
2591450c749Smrg    { "NetMousePS/2",           MSE_XPS2,       NULL,           PROT_NETPS2 },
2601450c749Smrg    { "NetScrollPS/2",          MSE_XPS2,       NULL,           PROT_NETSCPS2 },
261659607e0Smrg
262659607e0Smrg    /* Bus Mouse */
2631450c749Smrg    { "BusMouse",               MSE_BUS,        NULL,           PROT_BM },
264659607e0Smrg
265659607e0Smrg    /* Auto-detect (PnP) */
2661450c749Smrg    { "Auto",                   MSE_AUTO,       NULL,           PROT_AUTO },
267659607e0Smrg
268659607e0Smrg    /* Misc (usually OS-specific) */
2691450c749Smrg    { "SysMouse",               MSE_MISC,       mlDefaults,     PROT_SYSMOUSE },
2701450c749Smrg    { "WSMouse",                MSE_MISC,       NULL,           PROT_WSMOUSE },
2711450c749Smrg    { "VUID",                   MSE_MISC,       NULL,           PROT_VUID },
272659607e0Smrg
273659607e0Smrg    /* end of list */
2741450c749Smrg    { NULL,                     MSE_NONE,       NULL,           PROT_UNKNOWN }
275659607e0Smrg};
276659607e0Smrg
277659607e0Smrg/* Process options common to all mouse types. */
278659607e0Smrgstatic void
279659607e0SmrgMouseCommonOptions(InputInfoPtr pInfo)
280659607e0Smrg{
281659607e0Smrg    MouseDevPtr pMse;
282659607e0Smrg    MessageType buttons_from = X_CONFIG;
283659607e0Smrg    char *s;
284659607e0Smrg    int origButtons;
285c508c3daSmrg    int i;
286659607e0Smrg
287659607e0Smrg    pMse = pInfo->private;
288659607e0Smrg
289659607e0Smrg    pMse->buttons = xf86SetIntOption(pInfo->options, "Buttons", 0);
290659607e0Smrg    if (!pMse->buttons) {
2911450c749Smrg        pMse->buttons = MSE_DFLTBUTTONS;
2921450c749Smrg        buttons_from = X_DEFAULT;
293659607e0Smrg    }
294659607e0Smrg    origButtons = pMse->buttons;
295659607e0Smrg
296659607e0Smrg    pMse->emulate3Buttons = xf86SetBoolOption(pInfo->options,
2971450c749Smrg                                              "Emulate3Buttons", FALSE);
298659607e0Smrg    if (!xf86FindOptionValue(pInfo->options,"Emulate3Buttons")) {
2991450c749Smrg        pMse->emulate3ButtonsSoft = TRUE;
3001450c749Smrg        pMse->emulate3Buttons = TRUE;
301659607e0Smrg    }
3021450c749Smrg
303659607e0Smrg    pMse->emulate3Timeout = xf86SetIntOption(pInfo->options,
3041450c749Smrg                                             "Emulate3Timeout", 50);
305659607e0Smrg    if (pMse->emulate3Buttons || pMse->emulate3ButtonsSoft) {
3061450c749Smrg        MessageType from = X_CONFIG;
3071450c749Smrg        if (pMse->emulate3ButtonsSoft)
3081450c749Smrg            from = X_DEFAULT;
3091450c749Smrg        xf86Msg(from, "%s: Emulate3Buttons, Emulate3Timeout: %d\n",
3101450c749Smrg                pInfo->name, pMse->emulate3Timeout);
311659607e0Smrg    }
312659607e0Smrg
313659607e0Smrg    pMse->chordMiddle = xf86SetBoolOption(pInfo->options, "ChordMiddle", FALSE);
314659607e0Smrg    pMse->flipXY = xf86SetBoolOption(pInfo->options, "FlipXY", FALSE);
315659607e0Smrg    if (xf86SetBoolOption(pInfo->options, "InvX", FALSE)) {
3161450c749Smrg        pMse->invX = -1;
317659607e0Smrg    } else
3181450c749Smrg        pMse->invX = 1;
319659607e0Smrg    if (xf86SetBoolOption(pInfo->options, "InvY", FALSE)) {
3201450c749Smrg        pMse->invY = -1;
321659607e0Smrg    } else
3221450c749Smrg        pMse->invY = 1;
323659607e0Smrg    pMse->angleOffset = xf86SetIntOption(pInfo->options, "AngleOffset", 0);
3241450c749Smrg
325659607e0Smrg
326659607e0Smrg    if (pMse->pDragLock)
3271450c749Smrg        free(pMse->pDragLock);
328659607e0Smrg    pMse->pDragLock = NULL;
3291450c749Smrg
330659607e0Smrg    s = xf86SetStrOption(pInfo->options, "DragLockButtons", NULL);
331659607e0Smrg
332659607e0Smrg    if (s) {
3331450c749Smrg        int lock;             /* lock button */
3341450c749Smrg        int target;           /* target button */
3351450c749Smrg        int lockM,targetM;    /* bitmasks for drag lock, target */
3361450c749Smrg        int j;                /* indexes */
3371450c749Smrg        char *s1;             /* parse input string */
3381450c749Smrg        DragLockPtr pLock;
3391450c749Smrg
3401450c749Smrg        pLock = pMse->pDragLock = calloc(1, sizeof(DragLockRec));
3411450c749Smrg        /* init code */
3421450c749Smrg
3431450c749Smrg        /* initial string to be taken apart */
3441450c749Smrg        s1 = s;
3451450c749Smrg
3461450c749Smrg        /* keep getting numbers which are buttons */
3471450c749Smrg        while ((s1 != NULL) && (lock = strtol(s1, &s1, 10)) != 0) {
3481450c749Smrg
3491450c749Smrg            /* check sanity for a button */
3501450c749Smrg            if ((lock < 0) || (lock > MSE_MAXBUTTONS)) {
3511450c749Smrg                xf86Msg(X_WARNING, "DragLock: Invalid button number = %d\n",
3521450c749Smrg                        lock);
3531450c749Smrg                break;
3541450c749Smrg            };
3551450c749Smrg            /* turn into a button mask */
3561450c749Smrg            lockM = 1 << (lock - 1);
3571450c749Smrg
3581450c749Smrg            /* try to get drag lock button */
3591450c749Smrg            if ((s1 == NULL) || ((target=strtol(s1, &s1, 10)) == 0)) {
3601450c749Smrg                /*if no target, must be a master drag lock button */
3611450c749Smrg                /* save master drag lock mask */
3621450c749Smrg                pLock->masterLockM = lockM;
3631450c749Smrg                xf86Msg(X_CONFIG,
3641450c749Smrg                        "DragLock button %d is master drag lock",
3651450c749Smrg                        lock);
3661450c749Smrg            } else {
3671450c749Smrg                /* have target button number*/
3681450c749Smrg                /* check target button number for sanity */
3691450c749Smrg                if ((target < 0) || (target > MSE_MAXBUTTONS)) {
3701450c749Smrg                    xf86Msg(X_WARNING,
3711450c749Smrg                            "DragLock: Invalid button number for target=%d\n",
3721450c749Smrg                            target);
3731450c749Smrg                    break;
3741450c749Smrg                }
375659607e0Smrg
3761450c749Smrg                /* target button mask */
3771450c749Smrg                targetM = 1 << (target - 1);
378659607e0Smrg
3791450c749Smrg                xf86Msg(X_CONFIG,
3801450c749Smrg                        "DragLock: button %d is drag lock for button %d\n",
3811450c749Smrg                        lock,target);
3821450c749Smrg                lock--;
383659607e0Smrg
3841450c749Smrg                /* initialize table that maps drag lock mask to target mask */
3851450c749Smrg                pLock->nib_table[lock / NIB_BITS][1 << (lock % NIB_BITS)] =
3861450c749Smrg                        targetM;
387659607e0Smrg
3881450c749Smrg                /* add new drag lock to mask of drag locks */
3891450c749Smrg                pLock->lockButtonsM |= lockM;
3901450c749Smrg            }
391659607e0Smrg
3921450c749Smrg        }
393659607e0Smrg
3941450c749Smrg        /*
3951450c749Smrg         * fill out rest of map that maps sets of drag lock buttons
3961450c749Smrg         * to sets of target buttons, in the form of masks
3971450c749Smrg         */
3981450c749Smrg
3991450c749Smrg        /* for each nibble */
4001450c749Smrg        for (i = 0; i < NIB_COUNT; i++) {
4011450c749Smrg            /* for each possible set of bits for that nibble */
4021450c749Smrg            for (j = 0; j < NIB_SIZE; j++) {
4031450c749Smrg                int ff, fM, otherbits;
4041450c749Smrg
4051450c749Smrg                /* get first bit set in j*/
4061450c749Smrg                ff = ffs(j) - 1;
4071450c749Smrg                /* if 0 bits set nothing to do */
4081450c749Smrg                if (ff >= 0) {
4091450c749Smrg                    /* form mask for fist bit set */
4101450c749Smrg                    fM = 1 << ff;
4111450c749Smrg                    /* mask off first bit set to get remaining bits set*/
4121450c749Smrg                    otherbits = j & ~fM;
4131450c749Smrg                    /*
4141450c749Smrg                     * if otherbits =0 then only 1 bit set
4151450c749Smrg                     * so j=fM
4161450c749Smrg                     * nib_table[i][fM] already calculated if fM has
4171450c749Smrg                     * only 1 bit set.
4181450c749Smrg                     * nib_table[i][j] has already been filled in
4191450c749Smrg                     * by previous loop. otherwise
4201450c749Smrg                     * otherbits < j so nibtable[i][otherbits]
4211450c749Smrg                     * has already been calculated.
4221450c749Smrg                     */
4231450c749Smrg                    if (otherbits)
4241450c749Smrg                        pLock->nib_table[i][j] =
4251450c749Smrg                                     pLock->nib_table[i][fM] |
4261450c749Smrg                                     pLock->nib_table[i][otherbits];
4271450c749Smrg
4281450c749Smrg                }
4291450c749Smrg            }
4301450c749Smrg        }
4311450c749Smrg        free(s);
432659607e0Smrg    }
433659607e0Smrg
434659607e0Smrg    s = xf86SetStrOption(pInfo->options, "ZAxisMapping", "4 5");
435659607e0Smrg    if (s) {
4361450c749Smrg        int b1 = 0, b2 = 0, b3 = 0, b4 = 0;
4371450c749Smrg        char *msg = NULL;
4381450c749Smrg
4391450c749Smrg        pMse->negativeZ = pMse->positiveZ = MSE_NOAXISMAP;
4401450c749Smrg        pMse->negativeW = pMse->positiveW = MSE_NOAXISMAP;
4411450c749Smrg        if (!xf86NameCmp(s, "x")) {
4421450c749Smrg            pMse->negativeZ = pMse->positiveZ = MSE_MAPTOX;
4431450c749Smrg            msg = xstrdup("X axis");
4441450c749Smrg        } else if (!xf86NameCmp(s, "y")) {
4451450c749Smrg            pMse->negativeZ = pMse->positiveZ = MSE_MAPTOY;
4461450c749Smrg            msg = xstrdup("Y axis");
4471450c749Smrg        } else if (sscanf(s, "%d %d %d %d", &b1, &b2, &b3, &b4) >= 2 &&
4481450c749Smrg                 b1 > 0 && b1 <= MSE_MAXBUTTONS &&
4491450c749Smrg                 b2 > 0 && b2 <= MSE_MAXBUTTONS) {
4501450c749Smrg            msg = xstrdup("buttons XX and YY");
4511450c749Smrg            if (msg)
4521450c749Smrg                sprintf(msg, "buttons %d and %d", b1, b2);
4531450c749Smrg            pMse->negativeZ = 1 << (b1-1);
4541450c749Smrg            pMse->positiveZ = 1 << (b2-1);
4551450c749Smrg            if (b3 > 0 && b3 <= MSE_MAXBUTTONS &&
4561450c749Smrg                b4 > 0 && b4 <= MSE_MAXBUTTONS) {
4571450c749Smrg                if (msg)
4581450c749Smrg                    free(msg);
4591450c749Smrg                msg = xstrdup("buttons XX, YY, ZZ and WW");
4601450c749Smrg                if (msg)
4611450c749Smrg                    sprintf(msg, "buttons %d, %d, %d and %d", b1, b2, b3, b4);
4621450c749Smrg                pMse->negativeW = 1 << (b3-1);
4631450c749Smrg                pMse->positiveW = 1 << (b4-1);
4641450c749Smrg            }
4651450c749Smrg            if (b1 > pMse->buttons) pMse->buttons = b1;
4661450c749Smrg            if (b2 > pMse->buttons) pMse->buttons = b2;
4671450c749Smrg            if (b3 > pMse->buttons) pMse->buttons = b3;
4681450c749Smrg            if (b4 > pMse->buttons) pMse->buttons = b4;
4691450c749Smrg        }
4701450c749Smrg        if (msg) {
4711450c749Smrg            xf86Msg(X_CONFIG, "%s: ZAxisMapping: %s\n", pInfo->name, msg);
4721450c749Smrg            free(msg);
4731450c749Smrg        } else {
4741450c749Smrg            xf86Msg(X_WARNING, "%s: Invalid ZAxisMapping value: \"%s\"\n",
4751450c749Smrg                    pInfo->name, s);
4761450c749Smrg        }
4771450c749Smrg        free(s);
478659607e0Smrg    }
479659607e0Smrg    if (xf86SetBoolOption(pInfo->options, "EmulateWheel", FALSE)) {
4801450c749Smrg        Bool yFromConfig = FALSE;
4811450c749Smrg        int wheelButton;
4821450c749Smrg
4831450c749Smrg        pMse->emulateWheel = TRUE;
4841450c749Smrg        wheelButton = xf86SetIntOption(pInfo->options,
4851450c749Smrg                                        "EmulateWheelButton", 4);
4861450c749Smrg        if (wheelButton < 0 || wheelButton > MSE_MAXBUTTONS) {
4871450c749Smrg            xf86Msg(X_WARNING, "%s: Invalid EmulateWheelButton value: %d\n",
4881450c749Smrg                        pInfo->name, wheelButton);
4891450c749Smrg            wheelButton = 4;
4901450c749Smrg        }
4911450c749Smrg        pMse->wheelButton = wheelButton;
4921450c749Smrg
4931450c749Smrg        pMse->wheelInertia = xf86SetIntOption(pInfo->options,
4941450c749Smrg                                        "EmulateWheelInertia", 10);
4951450c749Smrg        if (pMse->wheelInertia <= 0) {
4961450c749Smrg            xf86Msg(X_WARNING, "%s: Invalid EmulateWheelInertia value: %d\n",
4971450c749Smrg                        pInfo->name, pMse->wheelInertia);
4981450c749Smrg            pMse->wheelInertia = 10;
4991450c749Smrg        }
5001450c749Smrg        pMse->wheelButtonTimeout = xf86SetIntOption(pInfo->options,
5011450c749Smrg                                        "EmulateWheelTimeout", 200);
5021450c749Smrg        if (pMse->wheelButtonTimeout <= 0) {
5031450c749Smrg            xf86Msg(X_WARNING, "%s: Invalid EmulateWheelTimeout value: %d\n",
5041450c749Smrg                        pInfo->name, pMse->wheelButtonTimeout);
5051450c749Smrg            pMse->wheelButtonTimeout = 200;
5061450c749Smrg        }
507659607e0Smrg
5081450c749Smrg        pMse->negativeX = MSE_NOAXISMAP;
5091450c749Smrg        pMse->positiveX = MSE_NOAXISMAP;
5101450c749Smrg        s = xf86SetStrOption(pInfo->options, "XAxisMapping", NULL);
5111450c749Smrg        if (s) {
5121450c749Smrg            int b1 = 0, b2 = 0;
5131450c749Smrg            char *msg = NULL;
5141450c749Smrg
5151450c749Smrg            if ((sscanf(s, "%d %d", &b1, &b2) == 2) &&
5161450c749Smrg                 b1 > 0 && b1 <= MSE_MAXBUTTONS &&
5171450c749Smrg                 b2 > 0 && b2 <= MSE_MAXBUTTONS) {
5181450c749Smrg                msg = xstrdup("buttons XX and YY");
5191450c749Smrg                if (msg)
5201450c749Smrg                    sprintf(msg, "buttons %d and %d", b1, b2);
5211450c749Smrg                pMse->negativeX = b1;
5221450c749Smrg                pMse->positiveX = b2;
5231450c749Smrg                if (b1 > pMse->buttons) pMse->buttons = b1;
5241450c749Smrg                if (b2 > pMse->buttons) pMse->buttons = b2;
5251450c749Smrg            } else {
5261450c749Smrg                xf86Msg(X_WARNING, "%s: Invalid XAxisMapping value: \"%s\"\n",
5271450c749Smrg                        pInfo->name, s);
5281450c749Smrg            }
5291450c749Smrg            if (msg) {
5301450c749Smrg                xf86Msg(X_CONFIG, "%s: XAxisMapping: %s\n", pInfo->name, msg);
5311450c749Smrg                free(msg);
5321450c749Smrg            }
5331450c749Smrg            free(s);
5341450c749Smrg        }
5351450c749Smrg        s = xf86SetStrOption(pInfo->options, "YAxisMapping", NULL);
5361450c749Smrg        if (s) {
5371450c749Smrg            int b1 = 0, b2 = 0;
5381450c749Smrg            char *msg = NULL;
5391450c749Smrg
5401450c749Smrg            if ((sscanf(s, "%d %d", &b1, &b2) == 2) &&
5411450c749Smrg                 b1 > 0 && b1 <= MSE_MAXBUTTONS &&
5421450c749Smrg                 b2 > 0 && b2 <= MSE_MAXBUTTONS) {
5431450c749Smrg                msg = xstrdup("buttons XX and YY");
5441450c749Smrg                if (msg)
5451450c749Smrg                    sprintf(msg, "buttons %d and %d", b1, b2);
5461450c749Smrg                pMse->negativeY = b1;
5471450c749Smrg                pMse->positiveY = b2;
5481450c749Smrg                if (b1 > pMse->buttons) pMse->buttons = b1;
5491450c749Smrg                if (b2 > pMse->buttons) pMse->buttons = b2;
5501450c749Smrg                yFromConfig = TRUE;
5511450c749Smrg            } else {
5521450c749Smrg                xf86Msg(X_WARNING, "%s: Invalid YAxisMapping value: \"%s\"\n",
5531450c749Smrg                        pInfo->name, s);
5541450c749Smrg            }
5551450c749Smrg            if (msg) {
5561450c749Smrg                xf86Msg(X_CONFIG, "%s: YAxisMapping: %s\n", pInfo->name, msg);
5571450c749Smrg                free(msg);
5581450c749Smrg            }
5591450c749Smrg            free(s);
5601450c749Smrg        }
5611450c749Smrg        if (!yFromConfig) {
5621450c749Smrg            pMse->negativeY = 4;
5631450c749Smrg            pMse->positiveY = 5;
5641450c749Smrg            if (pMse->negativeY > pMse->buttons)
5651450c749Smrg                pMse->buttons = pMse->negativeY;
5661450c749Smrg            if (pMse->positiveY > pMse->buttons)
5671450c749Smrg                pMse->buttons = pMse->positiveY;
5681450c749Smrg            xf86Msg(X_DEFAULT, "%s: YAxisMapping: buttons %d and %d\n",
5691450c749Smrg                    pInfo->name, pMse->negativeY, pMse->positiveY);
5701450c749Smrg        }
5711450c749Smrg        xf86Msg(X_CONFIG, "%s: EmulateWheel, EmulateWheelButton: %d, "
5721450c749Smrg                          "EmulateWheelInertia: %d, "
5731450c749Smrg                          "EmulateWheelTimeout: %d\n",
5741450c749Smrg                pInfo->name, wheelButton, pMse->wheelInertia,
5751450c749Smrg                pMse->wheelButtonTimeout);
576659607e0Smrg    }
577659607e0Smrg    s = xf86SetStrOption(pInfo->options, "ButtonMapping", NULL);
578659607e0Smrg    if (s) {
579659607e0Smrg       int b, n = 0;
580659607e0Smrg       char *s1 = s;
581659607e0Smrg       /* keep getting numbers which are buttons */
582659607e0Smrg       while (s1 && n < MSE_MAXBUTTONS && (b = strtol(s1, &s1, 10)) != 0) {
5831450c749Smrg           /* check sanity for a button */
5841450c749Smrg           if (b < 0 || b > MSE_MAXBUTTONS) {
5851450c749Smrg               xf86Msg(X_WARNING,
5861450c749Smrg                       "ButtonMapping: Invalid button number = %d\n", b);
5871450c749Smrg               break;
5881450c749Smrg           };
5891450c749Smrg           pMse->buttonMap[n++] = 1 << (b-1);
5901450c749Smrg           if (b > pMse->buttons) pMse->buttons = b;
591659607e0Smrg       }
592dd4cbfe8Smrg       free(s);
593659607e0Smrg    }
594659607e0Smrg    /* get maximum of mapped buttons */
5951450c749Smrg    for (i = pMse->buttons-1; i >= 0; i--) {
5961450c749Smrg        int f = ffs (pMse->buttonMap[i]);
5971450c749Smrg        if (f > pMse->buttons)
5981450c749Smrg            pMse->buttons = f;
599659607e0Smrg    }
600659607e0Smrg    if (origButtons != pMse->buttons)
6011450c749Smrg        buttons_from = X_CONFIG;
602659607e0Smrg    xf86Msg(buttons_from, "%s: Buttons: %d\n", pInfo->name, pMse->buttons);
603659607e0Smrg
604659607e0Smrg    pMse->doubleClickSourceButtonMask = 0;
605659607e0Smrg    pMse->doubleClickTargetButtonMask = 0;
606659607e0Smrg    pMse->doubleClickTargetButton = 0;
607659607e0Smrg    s = xf86SetStrOption(pInfo->options, "DoubleClickButtons", NULL);
608659607e0Smrg    if (s) {
609659607e0Smrg        int b1 = 0, b2 = 0;
610659607e0Smrg        char *msg = NULL;
611659607e0Smrg
612659607e0Smrg        if ((sscanf(s, "%d %d", &b1, &b2) == 2) &&
613659607e0Smrg        (b1 > 0) && (b1 <= MSE_MAXBUTTONS) && (b2 > 0) && (b2 <= MSE_MAXBUTTONS)) {
614659607e0Smrg            msg = xstrdup("buttons XX and YY");
615659607e0Smrg            if (msg)
616659607e0Smrg                sprintf(msg, "buttons %d and %d", b1, b2);
617659607e0Smrg            pMse->doubleClickTargetButton = b1;
618659607e0Smrg            pMse->doubleClickTargetButtonMask = 1 << (b1 - 1);
619659607e0Smrg            pMse->doubleClickSourceButtonMask = 1 << (b2 - 1);
620659607e0Smrg            if (b1 > pMse->buttons) pMse->buttons = b1;
621659607e0Smrg            if (b2 > pMse->buttons) pMse->buttons = b2;
622659607e0Smrg        } else {
623659607e0Smrg            xf86Msg(X_WARNING, "%s: Invalid DoubleClickButtons value: \"%s\"\n",
624659607e0Smrg                    pInfo->name, s);
625659607e0Smrg        }
626659607e0Smrg        if (msg) {
627659607e0Smrg            xf86Msg(X_CONFIG, "%s: DoubleClickButtons: %s\n", pInfo->name, msg);
628dd4cbfe8Smrg            free(msg);
629659607e0Smrg        }
6301450c749Smrg        free(s);
631659607e0Smrg    }
632659607e0Smrg}
633659607e0Smrg/*
634659607e0Smrg * map bits corresponding to lock buttons.
635659607e0Smrg * for each bit for a lock button,
636659607e0Smrg * turn on bit corresponding to button button that the lock
637659607e0Smrg * button services.
638659607e0Smrg */
639659607e0Smrg
640659607e0Smrgstatic int
641659607e0Smrglock2targetMap(DragLockPtr pLock, int lockMask)
642659607e0Smrg{
643659607e0Smrg    int result,i;
644659607e0Smrg    result = 0;
645659607e0Smrg
646659607e0Smrg    /*
647659607e0Smrg     * for each nibble group of bits, use
648659607e0Smrg     * map for that group to get corresponding
649659607e0Smrg     * bits, turn them on.
650659607e0Smrg     * if 4 or less buttons only first map will
651659607e0Smrg     * need to be used.
652659607e0Smrg     */
653659607e0Smrg    for (i = 0; (i < NIB_COUNT) && lockMask; i++) {
6541450c749Smrg        result |= pLock->nib_table[i][lockMask& NIB_MASK];
655659607e0Smrg
6561450c749Smrg        lockMask &= ~NIB_MASK;
6571450c749Smrg        lockMask >>= NIB_BITS;
658659607e0Smrg    }
659659607e0Smrg    return result;
660659607e0Smrg}
661659607e0Smrg
662659607e0Smrgstatic void
663659607e0SmrgMouseHWOptions(InputInfoPtr pInfo)
664659607e0Smrg{
665659607e0Smrg    MouseDevPtr  pMse = pInfo->private;
666659607e0Smrg    mousePrivPtr mPriv = (mousePrivPtr)pMse->mousePriv;
6671450c749Smrg
6681450c749Smrg    if (mPriv == NULL)
6691450c749Smrg            return;
670659607e0Smrg
671659607e0Smrg    if ((mPriv->soft
6721450c749Smrg         = xf86SetBoolOption(pInfo->options, "AutoSoft", FALSE))) {
6731450c749Smrg        xf86Msg(X_CONFIG, "Don't initialize mouse when auto-probing\n");
674659607e0Smrg    }
675659607e0Smrg    pMse->sampleRate = xf86SetIntOption(pInfo->options, "SampleRate", 0);
676659607e0Smrg    pMse->resolution = xf86SetIntOption(pInfo->options, "Resolution", 0);
677dd4cbfe8Smrg    mPriv->sensitivity = xf86SetRealOption(pInfo->options, "Sensitivity", 1.0);
678659607e0Smrg}
679659607e0Smrg
680659607e0Smrgstatic void
681659607e0SmrgMouseSerialOptions(InputInfoPtr pInfo)
682659607e0Smrg{
683659607e0Smrg    MouseDevPtr  pMse = pInfo->private;
684659607e0Smrg
685dd4cbfe8Smrg    pMse->baudRate = xf86SetIntOption(pInfo->options, "BaudRate", 0);
686659607e0Smrg}
687659607e0Smrg
688659607e0Smrgstatic MouseProtocolID
689659607e0SmrgProtocolNameToID(const char *name)
690659607e0Smrg{
691659607e0Smrg    int i;
692659607e0Smrg
693659607e0Smrg    for (i = 0; mouseProtocols[i].name; i++)
6941450c749Smrg        if (xf86NameCmp(name, mouseProtocols[i].name) == 0)
6951450c749Smrg            return mouseProtocols[i].id;
696659607e0Smrg    return PROT_UNKNOWN;
697659607e0Smrg}
698659607e0Smrg
699659607e0Smrgstatic const char *
700659607e0SmrgProtocolIDToName(MouseProtocolID id)
701659607e0Smrg{
702659607e0Smrg    int i;
703659607e0Smrg
704659607e0Smrg    switch (id) {
705659607e0Smrg    case PROT_UNKNOWN:
7061450c749Smrg        return "Unknown";
7071450c749Smrg        break;
708659607e0Smrg    case PROT_UNSUP:
7091450c749Smrg        return "Unsupported";
7101450c749Smrg        break;
711659607e0Smrg    default:
7121450c749Smrg        for (i = 0; mouseProtocols[i].name; i++)
7131450c749Smrg            if (id == mouseProtocols[i].id)
7141450c749Smrg                return mouseProtocols[i].name;
7151450c749Smrg        return "Invalid";
716659607e0Smrg    }
717659607e0Smrg}
718659607e0Smrg
719659607e0Smrgstatic int
720659607e0SmrgProtocolIDToClass(MouseProtocolID id)
721659607e0Smrg{
722659607e0Smrg    int i;
723659607e0Smrg
724659607e0Smrg    switch (id) {
725659607e0Smrg    case PROT_UNKNOWN:
726659607e0Smrg    case PROT_UNSUP:
7271450c749Smrg        return MSE_NONE;
7281450c749Smrg        break;
729659607e0Smrg    default:
7301450c749Smrg        for (i = 0; mouseProtocols[i].name; i++)
7311450c749Smrg            if (id == mouseProtocols[i].id)
7321450c749Smrg                return mouseProtocols[i].class;
7331450c749Smrg        return MSE_NONE;
734659607e0Smrg    }
735659607e0Smrg}
736659607e0Smrg
737659607e0Smrgstatic MouseProtocolPtr
738659607e0SmrgGetProtocol(MouseProtocolID id) {
739659607e0Smrg    int i;
740659607e0Smrg
741659607e0Smrg    switch (id) {
742659607e0Smrg    case PROT_UNKNOWN:
743659607e0Smrg    case PROT_UNSUP:
7441450c749Smrg        return NULL;
7451450c749Smrg        break;
746659607e0Smrg    default:
7471450c749Smrg        for (i = 0; mouseProtocols[i].name; i++)
7481450c749Smrg            if (id == mouseProtocols[i].id) {
7491450c749Smrg                return &mouseProtocols[i];
7501450c749Smrg            }
7511450c749Smrg        return NULL;
752659607e0Smrg    }
753659607e0Smrg}
754659607e0Smrg
755659607e0Smrgstatic OSMouseInfoPtr osInfo = NULL;
756659607e0Smrg
757659607e0Smrgstatic Bool
758659607e0SmrgInitProtocols(void)
759659607e0Smrg{
760659607e0Smrg    int classes;
761659607e0Smrg    int i;
762659607e0Smrg    const char *osname = NULL;
763659607e0Smrg
764659607e0Smrg    if (osInfo)
7651450c749Smrg        return TRUE;
766659607e0Smrg
767dd4cbfe8Smrg    osInfo = OSMouseInit(0);
768659607e0Smrg    if (!osInfo)
7691450c749Smrg        return FALSE;
770659607e0Smrg    if (!osInfo->SupportedInterfaces)
7711450c749Smrg        return FALSE;
772659607e0Smrg
773659607e0Smrg    classes = osInfo->SupportedInterfaces();
774659607e0Smrg    if (!classes)
7751450c749Smrg        return FALSE;
7761450c749Smrg
777659607e0Smrg    /* Mark unsupported interface classes. */
778659607e0Smrg    for (i = 0; mouseProtocols[i].name; i++)
7791450c749Smrg        if (!(mouseProtocols[i].class & classes))
7801450c749Smrg            mouseProtocols[i].id = PROT_UNSUP;
781659607e0Smrg
782659607e0Smrg    for (i = 0; mouseProtocols[i].name; i++)
7831450c749Smrg        if (mouseProtocols[i].class & MSE_MISC)
7841450c749Smrg            if (!osInfo->CheckProtocol ||
7851450c749Smrg                !osInfo->CheckProtocol(mouseProtocols[i].name))
7861450c749Smrg                mouseProtocols[i].id = PROT_UNSUP;
787659607e0Smrg
788659607e0Smrg    /* NetBSD uses PROT_BM for "PS/2". */
789659607e0Smrg    xf86GetOS(&osname, NULL, NULL, NULL);
790659607e0Smrg    if (osname && xf86NameCmp(osname, "netbsd") == 0)
7911450c749Smrg        for (i = 0; mouseProtocols[i].name; i++)
7921450c749Smrg            if (mouseProtocols[i].id == PROT_PS2)
7931450c749Smrg                mouseProtocols[i].id = PROT_BM;
794659607e0Smrg
795659607e0Smrg    return TRUE;
796659607e0Smrg}
797659607e0Smrg
798dd4cbfe8Smrgstatic const char*
799dd4cbfe8SmrgMouseFindDevice(InputInfoPtr pInfo, const char* protocol)
800dd4cbfe8Smrg{
801dd4cbfe8Smrg    const char *device;
802dd4cbfe8Smrg
803dd4cbfe8Smrg    if (!osInfo->FindDevice)
804dd4cbfe8Smrg        return NULL;
805dd4cbfe8Smrg
806dd4cbfe8Smrg    xf86Msg(X_WARNING, "%s: No Device specified, looking for one...\n", pInfo->name);
807dd4cbfe8Smrg    device = osInfo->FindDevice(pInfo, protocol, 0);
808dd4cbfe8Smrg    if (!device)
8091450c749Smrg        xf86Msg(X_ERROR, "%s: Cannot find which device to use.\n", pInfo->name);
810dd4cbfe8Smrg    else
8111450c749Smrg        xf86Msg(X_PROBED, "%s: Device: \"%s\"\n", pInfo->name, device);
812dd4cbfe8Smrg
813dd4cbfe8Smrg    return device;
814dd4cbfe8Smrg}
815dd4cbfe8Smrg
816dd4cbfe8Smrgstatic const char*
817dd4cbfe8SmrgMousePickProtocol(InputInfoPtr pInfo, const char* device,
8181450c749Smrg                  const char *protocol, MouseProtocolID *protocolID_out)
819dd4cbfe8Smrg{
820dd4cbfe8Smrg    MouseProtocolID protocolID = *protocolID_out;
821dd4cbfe8Smrg
822dd4cbfe8Smrg    protocolID = ProtocolNameToID(protocol);
823dd4cbfe8Smrg
824dd4cbfe8Smrg    if (protocolID == PROT_AUTO)
825dd4cbfe8Smrg    {
8261450c749Smrg        const char *osProt;
8271450c749Smrg        if (osInfo->SetupAuto && (osProt = osInfo->SetupAuto(pInfo,NULL))) {
8281450c749Smrg            MouseProtocolID id = ProtocolNameToID(osProt);
8291450c749Smrg            if (id == PROT_UNKNOWN || id == PROT_UNSUP) {
8301450c749Smrg                protocolID = id;
8311450c749Smrg                protocol = osProt;
8321450c749Smrg            }
8331450c749Smrg        }
834dd4cbfe8Smrg    }
835dd4cbfe8Smrg
836dd4cbfe8Smrg    switch (protocolID) {
837dd4cbfe8Smrg        case PROT_WSMOUSE:
838dd4cbfe8Smrg        case PROT_VUID:
839dd4cbfe8Smrg            if (osInfo->PreInit)
840dd4cbfe8Smrg                osInfo->PreInit(pInfo, protocol, 0);
841dd4cbfe8Smrg            break;
8421450c749Smrg        case PROT_UNKNOWN:
8431450c749Smrg            /* Check for a builtin OS-specific protocol,
8441450c749Smrg             * and call its PreInit. */
8451450c749Smrg            if (osInfo->CheckProtocol
8461450c749Smrg                    && osInfo->CheckProtocol(protocol)) {
8471450c749Smrg                if (!device)
8481450c749Smrg                    MouseFindDevice(pInfo, protocol);
8491450c749Smrg                if (osInfo->PreInit) {
8501450c749Smrg                    osInfo->PreInit(pInfo, protocol, 0);
8511450c749Smrg                }
8521450c749Smrg                break;
8531450c749Smrg            }
8541450c749Smrg            xf86Msg(X_ERROR, "%s: Unknown protocol \"%s\"\n",
8551450c749Smrg                    pInfo->name, protocol);
8561450c749Smrg            break;
8571450c749Smrg        case PROT_UNSUP:
8581450c749Smrg            xf86Msg(X_ERROR,
8591450c749Smrg                    "%s: Protocol \"%s\" is not supported on this "
8601450c749Smrg                    "platform\n", pInfo->name, protocol);
8611450c749Smrg            break;
8621450c749Smrg        default:
8631450c749Smrg            break;
864dd4cbfe8Smrg    }
865dd4cbfe8Smrg
866dd4cbfe8Smrg    *protocolID_out = protocolID;
867dd4cbfe8Smrg
868dd4cbfe8Smrg    return protocol;
869dd4cbfe8Smrg}
870dd4cbfe8Smrg
871dd4cbfe8Smrg#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) < 12
872dd4cbfe8Smrgstatic int NewMousePreInit(InputDriverPtr drv, InputInfoPtr pInfo,
873dd4cbfe8Smrg                           int flags);
874dd4cbfe8Smrg
875659607e0Smrgstatic InputInfoPtr
876659607e0SmrgMousePreInit(InputDriverPtr drv, IDevPtr dev, int flags)
877659607e0Smrg{
878659607e0Smrg    InputInfoPtr pInfo;
879dd4cbfe8Smrg
880dd4cbfe8Smrg    if (!(pInfo = xf86AllocateInput(drv, 0)))
8811450c749Smrg        return NULL;
882dd4cbfe8Smrg
883dd4cbfe8Smrg    pInfo->name = dev->identifier;
884dd4cbfe8Smrg    pInfo->flags = XI86_SEND_DRAG_EVENTS;
885dd4cbfe8Smrg    pInfo->conf_idev = dev;
886dd4cbfe8Smrg    pInfo->close_proc = NULL;
887dd4cbfe8Smrg    pInfo->private_flags = 0;
888dd4cbfe8Smrg    pInfo->always_core_feedback = NULL;
889dd4cbfe8Smrg
8901450c749Smrg    COLLECT_INPUT_OPTIONS(pInfo, NULL);
8911450c749Smrg
892dd4cbfe8Smrg    if (NewMousePreInit(drv, pInfo, flags) == Success)
893dd4cbfe8Smrg    {
894dd4cbfe8Smrg        /* Check if SendDragEvents has been disabled. */
895dd4cbfe8Smrg        if (!xf86SetBoolOption(dev->commonOptions, "SendDragEvents", TRUE))
896dd4cbfe8Smrg            pInfo->flags &= ~XI86_SEND_DRAG_EVENTS;
897dd4cbfe8Smrg
898dd4cbfe8Smrg        pInfo->flags |= XI86_CONFIGURED;
899dd4cbfe8Smrg
900dd4cbfe8Smrg        return pInfo;
901dd4cbfe8Smrg    }
902dd4cbfe8Smrg
903dd4cbfe8Smrg    xf86DeleteInput(pInfo, 0);
904dd4cbfe8Smrg
905dd4cbfe8Smrg    return NULL;
906dd4cbfe8Smrg}
907dd4cbfe8Smrg
908dd4cbfe8Smrgstatic int
909dd4cbfe8SmrgNewMousePreInit(InputDriverPtr drv, InputInfoPtr pInfo, int flags)
910dd4cbfe8Smrg#else
911dd4cbfe8Smrgstatic int
912dd4cbfe8SmrgMousePreInit(InputDriverPtr drv, InputInfoPtr pInfo, int flags)
913dd4cbfe8Smrg#endif
914dd4cbfe8Smrg{
915659607e0Smrg    MouseDevPtr pMse;
916659607e0Smrg    mousePrivPtr mPriv;
917dd4cbfe8Smrg    MessageType protocolFrom = X_DEFAULT;
918dd4cbfe8Smrg    const char *protocol;
919659607e0Smrg    const char *device;
920659607e0Smrg    MouseProtocolID protocolID;
921659607e0Smrg    MouseProtocolPtr pProto;
922659607e0Smrg    int i;
923dd4cbfe8Smrg    int rc = Success;
924659607e0Smrg
925dd4cbfe8Smrg    if (!InitProtocols())
9261450c749Smrg        return BadAlloc;
927659607e0Smrg
928659607e0Smrg    /* Initialise the InputInfoRec. */
929659607e0Smrg    pInfo->type_name = XI_MOUSE;
930659607e0Smrg    pInfo->device_control = MouseProc;
931659607e0Smrg    pInfo->read_input = MouseReadInput;
932659607e0Smrg    pInfo->control_proc = NULL;
933659607e0Smrg    pInfo->switch_mode = NULL;
934659607e0Smrg    pInfo->fd = -1;
935659607e0Smrg    pInfo->dev = NULL;
936659607e0Smrg
937dd4cbfe8Smrg    /* Allocate the MouseDevRec and initialise it. */
938dd4cbfe8Smrg    if (!(pMse = calloc(sizeof(MouseDevRec), 1)))
939dd4cbfe8Smrg    {
9401450c749Smrg        rc = BadAlloc;
9411450c749Smrg        goto out;
942659607e0Smrg    }
943659607e0Smrg
944659607e0Smrg    pInfo->private = pMse;
945659607e0Smrg    pMse->Ctrl = MouseCtrl;
946659607e0Smrg    pMse->PostEvent = MousePostEvent;
947659607e0Smrg    pMse->CommonOptions = MouseCommonOptions;
948dd4cbfe8Smrg
949659607e0Smrg    /* Find the protocol type. */
950dd4cbfe8Smrg    protocol = xf86SetStrOption(pInfo->options, "Protocol", NULL);
951659607e0Smrg    if (protocol) {
9521450c749Smrg        protocolFrom = X_CONFIG;
953659607e0Smrg    } else if (osInfo->DefaultProtocol) {
9541450c749Smrg        protocol = osInfo->DefaultProtocol();
9551450c749Smrg        protocolFrom = X_DEFAULT;
956659607e0Smrg    }
957659607e0Smrg    if (!protocol) {
9581450c749Smrg        xf86Msg(X_ERROR, "%s: No Protocol specified\n", pInfo->name);
9591450c749Smrg        rc = BadValue;
9601450c749Smrg        goto out;
961659607e0Smrg    }
962659607e0Smrg
963dd4cbfe8Smrg    device = xf86SetStrOption(pInfo->options, "Device", NULL);
964dd4cbfe8Smrg
965659607e0Smrg    /* Default Mapping: 1 2 3 8 9 10 11 ... */
966659607e0Smrg    for (i = 0; i < MSE_MAXBUTTONS; i++)
9671450c749Smrg        pMse->buttonMap[i] = 1 << (i > 2 && i < MSE_MAXBUTTONS-4 ? i+4 : i);
968659607e0Smrg
969dd4cbfe8Smrg    protocol = MousePickProtocol(pInfo, device, protocol, &protocolID);
970dd4cbfe8Smrg
971dd4cbfe8Smrg    if (!device)
972dd4cbfe8Smrg        MouseFindDevice(pInfo, protocol);
973659607e0Smrg
974659607e0Smrg    xf86Msg(protocolFrom, "%s: Protocol: \"%s\"\n", pInfo->name, protocol);
975dd4cbfe8Smrg    if (protocolID == PROT_UNKNOWN)
976dd4cbfe8Smrg        goto out;
977659607e0Smrg    if (!(pProto = GetProtocol(protocolID)))
978dd4cbfe8Smrg    {
9791450c749Smrg        rc = BadValue;
9801450c749Smrg        goto out;
981dd4cbfe8Smrg    }
982659607e0Smrg
983659607e0Smrg    pMse->protocolID = protocolID;
984659607e0Smrg    pMse->oldProtocolID = protocolID;  /* hack */
985659607e0Smrg
986659607e0Smrg    pMse->autoProbe = FALSE;
987659607e0Smrg    /* Collect the options, and process the common options. */
988dd4cbfe8Smrg#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) < 12
989dd4cbfe8Smrg    /* need some special handling here. xf86CollectInputOptions will reset
990dd4cbfe8Smrg     * pInfo->options if the second argument is not-null. To re-merge the
991dd4cbfe8Smrg     * previously set arguments, pass the original pInfo->options in.
992dd4cbfe8Smrg     */
993dd4cbfe8Smrg    xf86CollectInputOptions(pInfo, pProto->defaults, pInfo->options);
994dd4cbfe8Smrg#else
995dd4cbfe8Smrg    COLLECT_INPUT_OPTIONS(pInfo, pProto->defaults);
996dd4cbfe8Smrg#endif
997659607e0Smrg    xf86ProcessCommonOptions(pInfo, pInfo->options);
998659607e0Smrg
999659607e0Smrg    /* Check if the device can be opened. */
1000659607e0Smrg    pInfo->fd = xf86OpenSerial(pInfo->options);
1001659607e0Smrg    if (pInfo->fd == -1) {
10021450c749Smrg        if (xf86GetAllowMouseOpenFail())
10031450c749Smrg            xf86Msg(X_WARNING, "%s: cannot open input device\n", pInfo->name);
10041450c749Smrg        else {
10051450c749Smrg            xf86Msg(X_ERROR, "%s: cannot open input device\n", pInfo->name);
10061450c749Smrg            if (pMse->mousePriv)
10071450c749Smrg                free(pMse->mousePriv);
10081450c749Smrg            free(pMse);
10091450c749Smrg            pInfo->private = NULL;
10101450c749Smrg            rc = BadValue;
10111450c749Smrg            goto out;
10121450c749Smrg        }
1013659607e0Smrg    }
1014659607e0Smrg    xf86CloseSerial(pInfo->fd);
1015659607e0Smrg    pInfo->fd = -1;
1016659607e0Smrg
1017dd4cbfe8Smrg    if (!(mPriv = (pointer) calloc(sizeof(mousePrivRec), 1)))
1018dd4cbfe8Smrg    {
10191450c749Smrg        rc = BadAlloc;
10201450c749Smrg        goto out;
1021dd4cbfe8Smrg    }
1022dd4cbfe8Smrg
1023659607e0Smrg    pMse->mousePriv = mPriv;
1024659607e0Smrg    pMse->CommonOptions(pInfo);
1025659607e0Smrg    pMse->checkMovements = checkForErraticMovements;
1026659607e0Smrg    pMse->autoProbeMouse = autoProbeMouse;
1027659607e0Smrg    pMse->collectData = collectData;
1028659607e0Smrg    pMse->dataGood = autoGood;
10291450c749Smrg
1030659607e0Smrg    MouseHWOptions(pInfo);
1031659607e0Smrg    MouseSerialOptions(pInfo);
1032dd4cbfe8Smrg
1033dd4cbfe8Smrgout:
1034dd4cbfe8Smrg    return rc;
1035659607e0Smrg}
1036659607e0Smrg
10371450c749Smrgstatic void MouseInitButtonLabels(Atom *btn_labels)
10381450c749Smrg{
10391450c749Smrg    int i;
10401450c749Smrg    Atom unknown_btn;
10411450c749Smrg
10421450c749Smrg    btn_labels[0] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_LEFT);
10431450c749Smrg    btn_labels[1] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_MIDDLE);
10441450c749Smrg    btn_labels[2] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_RIGHT);
10451450c749Smrg    btn_labels[3] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_WHEEL_UP);
10461450c749Smrg    btn_labels[4] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_WHEEL_DOWN);
10471450c749Smrg    btn_labels[5] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_HWHEEL_LEFT);
10481450c749Smrg    btn_labels[6] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_HWHEEL_RIGHT);
10491450c749Smrg
10501450c749Smrg    unknown_btn = XIGetKnownProperty(BTN_LABEL_PROP_BTN_UNKNOWN);
10511450c749Smrg    for (i = 7; i < MSE_MAXBUTTONS; i++)
10521450c749Smrg        btn_labels[i] = unknown_btn;
10531450c749Smrg}
10541450c749Smrg
10551450c749Smrgstatic int
10561450c749SmrgMouseSetProperty(DeviceIntPtr device, Atom atom,
10571450c749Smrg                                XIPropertyValuePtr val, BOOL checkonly)
10581450c749Smrg{
10591450c749Smrg    InputInfoPtr pInfo = device->public.devicePrivate;
10601450c749Smrg    MouseDevPtr pMse = pInfo->private;
10611450c749Smrg
10621450c749Smrg    if (atom == prop_mbemu)
10631450c749Smrg    {
10641450c749Smrg        if (val->format != 8 || val->size != 1 || val->type != XA_INTEGER)
10651450c749Smrg            return BadMatch;
10661450c749Smrg
10671450c749Smrg        if (!checkonly)
10681450c749Smrg            Emulate3ButtonsSetEnabled(pInfo, *((BOOL*)val->data));
10691450c749Smrg    }
10701450c749Smrg    else if (atom == prop_mbtimeout)
10711450c749Smrg    {
10721450c749Smrg        if (val->format != 32 || val->size != 1 || val->type != XA_INTEGER)
10731450c749Smrg            return BadMatch;
10741450c749Smrg
10751450c749Smrg        if (!checkonly)
10761450c749Smrg            pMse->emulate3Timeout = *((CARD32*)val->data);
10771450c749Smrg    }
10781450c749Smrg
10791450c749Smrg    return Success;
10801450c749Smrg}
10811450c749Smrg
10821450c749Smrgstatic void
10831450c749SmrgMouseInitProperties(DeviceIntPtr device)
10841450c749Smrg{
10851450c749Smrg    InputInfoPtr pInfo = device->public.devicePrivate;
10861450c749Smrg    MouseDevPtr pMse = pInfo->private;
10871450c749Smrg    int rc;
10881450c749Smrg
10891450c749Smrg#ifdef XI_PROP_DEVICE_NODE
10901450c749Smrg    const char *device_node =
10911450c749Smrg        xf86CheckStrOption(pInfo->options, "Device", NULL);
10921450c749Smrg
10931450c749Smrg    if (device_node)
10941450c749Smrg    {
10951450c749Smrg        Atom prop_device = MakeAtom(XI_PROP_DEVICE_NODE,
10961450c749Smrg                                    strlen(XI_PROP_DEVICE_NODE), TRUE);
10971450c749Smrg        XIChangeDeviceProperty(device, prop_device, XA_STRING, 8,
10981450c749Smrg                               PropModeReplace,
10991450c749Smrg                               strlen(device_node), device_node, FALSE);
11001450c749Smrg    }
11011450c749Smrg#endif /* XI_PROP_DEVICE_NODE */
11021450c749Smrg
11031450c749Smrg    /* Button labels */
11041450c749Smrg    if (pMse->buttons > 0)
11051450c749Smrg    {
11061450c749Smrg        Atom prop_btn_label = XIGetKnownProperty(BTN_LABEL_PROP);
11071450c749Smrg
11081450c749Smrg        if (prop_btn_label)
11091450c749Smrg        {
11101450c749Smrg            Atom btn_labels[MSE_MAXBUTTONS];
11111450c749Smrg            MouseInitButtonLabels(btn_labels);
11121450c749Smrg
11131450c749Smrg            XIChangeDeviceProperty(device, prop_btn_label, XA_ATOM, 32,
11141450c749Smrg                                   PropModeReplace, pMse->buttons,
11151450c749Smrg                                   btn_labels, FALSE);
11161450c749Smrg            XISetDevicePropertyDeletable(device, prop_btn_label, FALSE);
11171450c749Smrg        }
11181450c749Smrg    }
11191450c749Smrg
11201450c749Smrg    /* Middle button emulation - which this driver calls 3rd button emulation,
11211450c749Smrg     * but evdev's properties considers that to be simulating right button
11221450c749Smrg     * clicks from a one button mouse, which this driver does not currently
11231450c749Smrg     * support, so we use this name for better consistency.
11241450c749Smrg     */
11251450c749Smrg    prop_mbemu = MakeAtom(MOUSE_PROP_MIDBUTTON, strlen(MOUSE_PROP_MIDBUTTON),
11261450c749Smrg                          TRUE);
11271450c749Smrg    rc = XIChangeDeviceProperty(device, prop_mbemu, XA_INTEGER, 8,
11281450c749Smrg                                PropModeReplace, 1,
11291450c749Smrg                                &pMse->emulate3Buttons, FALSE);
11301450c749Smrg    if (rc != Success)
11311450c749Smrg        return;
11321450c749Smrg    XISetDevicePropertyDeletable(device, prop_mbemu, FALSE);
11331450c749Smrg
11341450c749Smrg    prop_mbtimeout = MakeAtom(MOUSE_PROP_MIDBUTTON_TIMEOUT,
11351450c749Smrg                              strlen(MOUSE_PROP_MIDBUTTON_TIMEOUT), TRUE);
11361450c749Smrg    rc = XIChangeDeviceProperty(device, prop_mbtimeout, XA_INTEGER, 32,
11371450c749Smrg                                PropModeReplace, 1,
11381450c749Smrg                                &pMse->emulate3Timeout, FALSE);
11391450c749Smrg
11401450c749Smrg    if (rc != Success)
11411450c749Smrg        return;
11421450c749Smrg    XISetDevicePropertyDeletable(device, prop_mbtimeout, FALSE);
11431450c749Smrg
11441450c749Smrg    XIRegisterPropertyHandler(device, MouseSetProperty, NULL, NULL);
11451450c749Smrg}
1146659607e0Smrg
1147659607e0Smrgstatic void
1148659607e0SmrgMouseReadInput(InputInfoPtr pInfo)
1149659607e0Smrg{
1150659607e0Smrg    MouseDevPtr pMse;
1151659607e0Smrg    int j, buttons, dx, dy, dz, dw, baddata;
1152659607e0Smrg    int pBufP;
1153659607e0Smrg    int c;
1154659607e0Smrg    unsigned char *pBuf, u;
1155659607e0Smrg
1156659607e0Smrg
1157659607e0Smrg    pMse = pInfo->private;
1158659607e0Smrg    pBufP = pMse->protoBufTail;
1159659607e0Smrg    pBuf = pMse->protoBuf;
1160659607e0Smrg
1161659607e0Smrg    if (pInfo->fd == -1)
11621450c749Smrg        return;
1163659607e0Smrg
1164659607e0Smrg    /*
1165659607e0Smrg     * Set blocking to -1 on the first call because we know there is data to
1166659607e0Smrg     * read. Xisb automatically clears it after one successful read so that
1167659607e0Smrg     * succeeding reads are preceeded by a select with a 0 timeout to prevent
1168659607e0Smrg     * read from blocking indefinitely.
1169659607e0Smrg     */
1170659607e0Smrg    XisbBlockDuration(pMse->buffer, -1);
1171659607e0Smrg
1172659607e0Smrg    while ((c = XisbRead(pMse->buffer)) >= 0) {
11731450c749Smrg        u = (unsigned char)c;
1174659607e0Smrg
1175659607e0Smrg#if defined (EXTMOUSEDEBUG) || defined (MOUSEDATADEBUG)
11761450c749Smrg        LogMessageVerbSigSafe(X_INFO, -1, "mouse byte: %x\n",u);
1177659607e0Smrg#endif
1178659607e0Smrg
11791450c749Smrg        /* if we do autoprobing collect the data */
11801450c749Smrg        if (pMse->collectData && pMse->autoProbe)
11811450c749Smrg            if (pMse->collectData(pMse,u))
11821450c749Smrg                continue;
1183eeaac534Smrg
1184659607e0Smrg#ifdef SUPPORT_MOUSE_RESET
11851450c749Smrg        if (mouseReset(pInfo,u)) {
11861450c749Smrg            pBufP = 0;
11871450c749Smrg            continue;
11881450c749Smrg        }
1189659607e0Smrg#endif
11901450c749Smrg        if (pBufP >= pMse->protoPara[4]) {
11911450c749Smrg            /*
11921450c749Smrg             * Buffer contains a full packet, which has already been processed:
11931450c749Smrg             * Empty the buffer and check for optional 4th byte, which will be
11941450c749Smrg             * processed directly, without being put into the buffer first.
11951450c749Smrg             */
11961450c749Smrg            pBufP = 0;
11971450c749Smrg            if ((u & pMse->protoPara[0]) != pMse->protoPara[1] &&
11981450c749Smrg                (u & pMse->protoPara[5]) == pMse->protoPara[6]) {
11991450c749Smrg                /*
12001450c749Smrg                 * Hack for Logitech MouseMan Mouse - Middle button
12011450c749Smrg                 *
12021450c749Smrg                 * Unfortunately this mouse has variable length packets: the
12031450c749Smrg                 * standard Microsoft 3 byte packet plus an optional 4th byte
12041450c749Smrg                 * whenever the middle button status changes.
12051450c749Smrg                 *
12061450c749Smrg                 * We have already processed the standard packet with the
12071450c749Smrg                 * movement and button info.  Now post an event message with
12081450c749Smrg                 * the old status of the left and right buttons and the
12091450c749Smrg                 * updated middle button.
12101450c749Smrg                 */
12111450c749Smrg                /*
12121450c749Smrg                 * Even worse, different MouseMen and TrackMen differ in the
12131450c749Smrg                 * 4th byte: some will send 0x00/0x20, others 0x01/0x21, or
12141450c749Smrg                 * even 0x02/0x22, so I have to strip off the lower bits.
12151450c749Smrg                 * [CHRIS-211092]
12161450c749Smrg                 *
12171450c749Smrg                 * [JCH-96/01/21]
12181450c749Smrg                 * HACK for ALPS "fourth button".  (It's bit 0x10 of the
12191450c749Smrg                 * "fourth byte" and it is activated by tapping the glidepad
12201450c749Smrg                 * with the finger! 8^) We map it to bit bit3, and the
12211450c749Smrg                 * reverse map in xf86Events just has to be extended so that
12221450c749Smrg                 * it is identified as Button 4.  The lower half of the
12231450c749Smrg                 * reverse-map may remain unchanged.
12241450c749Smrg                 */
12251450c749Smrg                /*
12261450c749Smrg                 * [KAZU-030897]
12271450c749Smrg                 * Receive the fourth byte only when preceeding three bytes
12281450c749Smrg                 * have been detected (pBufP >= pMse->protoPara[4]).  In the
12291450c749Smrg                 * previous versions, the test was pBufP == 0; we may have
12301450c749Smrg                 * mistakingly received a byte even if we didn't see anything
12311450c749Smrg                 * preceeding the byte.
12321450c749Smrg                 */
1233659607e0Smrg#ifdef EXTMOUSEDEBUG
12341450c749Smrg                LogMessageVerbSigSafe(X_INFO, -1, "mouse 4th byte %x\n",u);
1235659607e0Smrg#endif
12361450c749Smrg                dx = dy = dz = dw = 0;
12371450c749Smrg                buttons = 0;
12381450c749Smrg                switch (pMse->protocolID) {
12391450c749Smrg
12401450c749Smrg                /*
12411450c749Smrg                 * [KAZU-221197]
12421450c749Smrg                 * IntelliMouse, NetMouse (including NetMouse Pro) and Mie
12431450c749Smrg                 * Mouse always send the fourth byte, whereas the fourth byte
12441450c749Smrg                 * is optional for GlidePoint and ThinkingMouse.  The fourth
12451450c749Smrg                 * byte is also optional for MouseMan+ and FirstMouse+ in
12461450c749Smrg                 * their native mode.  It is always sent if they are in the
12471450c749Smrg                 * IntelliMouse compatible mode.
12481450c749Smrg                 */
12491450c749Smrg                case PROT_IMSERIAL:     /* IntelliMouse, NetMouse, Mie Mouse,
12501450c749Smrg                                           MouseMan+ */
12511450c749Smrg                    dz = (u & 0x08) ?
12521450c749Smrg                                (u & 0x0f) - 16 : (u & 0x0f);
12531450c749Smrg                    if ((dz >= 7) || (dz <= -7))
12541450c749Smrg                        dz = 0;
12551450c749Smrg                    buttons |=  ((int)(u & 0x10) >> 3)
12561450c749Smrg                              | ((int)(u & 0x20) >> 2)
12571450c749Smrg                              | (pMse->lastButtons & 0x05);
12581450c749Smrg                    break;
12591450c749Smrg
12601450c749Smrg                case PROT_GLIDE:
12611450c749Smrg                case PROT_THINKING:
12621450c749Smrg                    buttons |= ((int)(u & 0x10) >> 1);
12631450c749Smrg                    /* fall through */
12641450c749Smrg
12651450c749Smrg                default:
12661450c749Smrg                    buttons |= ((int)(u & 0x20) >> 4) |
12671450c749Smrg                               (pMse->lastButtons & 0x05);
12681450c749Smrg                    break;
12691450c749Smrg                }
12701450c749Smrg                goto post_event;
12711450c749Smrg            }
12721450c749Smrg        }
12731450c749Smrg        /* End of packet buffer flush and 4th byte hack. */
12741450c749Smrg
12751450c749Smrg        /*
12761450c749Smrg         * Append next byte to buffer (which is empty or contains an
12771450c749Smrg         * incomplete packet); iterate if packet (still) not complete.
12781450c749Smrg         */
12791450c749Smrg        pBuf[pBufP++] = u;
12801450c749Smrg        if (pBufP != pMse->protoPara[4]) continue;
1281659607e0Smrg#ifdef EXTMOUSEDEBUG2
12821450c749Smrg        {
12831450c749Smrg            int i;
12841450c749Smrg            LogMessageVerbSigSafe(X_INFO, -1, "received %d bytes",pBufP);
12851450c749Smrg            for ( i=0; i < pBufP; i++)
12861450c749Smrg                LogMessageVerbSigSafe(X_INFO, -1, " %x",pBuf[i]);
12871450c749Smrg            LogMessageVerbSigSafe(X_INFO, -1, "\n");
12881450c749Smrg        }
1289659607e0Smrg#endif
1290659607e0Smrg
12911450c749Smrg        /*
12921450c749Smrg         * Hack for resyncing: We check here for a package that is:
12931450c749Smrg         *  a) illegal (detected by wrong data-package header)
12941450c749Smrg         *  b) invalid (0x80 == -128 and that might be wrong for MouseSystems)
12951450c749Smrg         *  c) bad header-package
12961450c749Smrg         *
12971450c749Smrg         * NOTE: b) is a violation of the MouseSystems-Protocol, since values
12981450c749Smrg         *       of -128 are allowed, but since they are very seldom we can
12991450c749Smrg         *       easily  use them as package-header with no button pressed.
13001450c749Smrg         * NOTE/2: On a PS/2 mouse any byte is valid as a data byte.
13011450c749Smrg         *       Furthermore, 0x80 is not valid as a header byte. For a PS/2
13021450c749Smrg         *       mouse we skip checking data bytes.  For resyncing a PS/2
13031450c749Smrg         *       mouse we require the two most significant bits in the header
13041450c749Smrg         *       byte to be 0. These are the overflow bits, and in case of
13051450c749Smrg         *       an overflow we actually lose sync. Overflows are very rare,
13061450c749Smrg         *       however, and we quickly gain sync again after an overflow
13071450c749Smrg         *       condition. This is the best we can do. (Actually, we could
13081450c749Smrg         *       use bit 0x08 in the header byte for resyncing, since that
13091450c749Smrg         *       bit is supposed to be always on, but nobody told Microsoft...)
13101450c749Smrg         */
13111450c749Smrg
13121450c749Smrg        /*
13131450c749Smrg         * [KAZU,OYVIND-120398]
13141450c749Smrg         * The above hack is wrong!  Because of b) above, we shall see
13151450c749Smrg         * erroneous mouse events so often when the MouseSystem mouse is
13161450c749Smrg         * moved quickly.  As for the PS/2 and its variants, we don't need
13171450c749Smrg         * to treat them as special cases, because protoPara[2] and
13181450c749Smrg         * protoPara[3] are both 0x00 for them, thus, any data bytes will
13191450c749Smrg         * never be discarded.  0x80 is rejected for MMSeries, Logitech
13201450c749Smrg         * and MMHittab protocols, because protoPara[2] and protoPara[3]
13211450c749Smrg         * are 0x80 and 0x00 respectively.  The other protocols are 7-bit
13221450c749Smrg         * protocols; there is no use checking 0x80.
13231450c749Smrg         *
13241450c749Smrg         * All in all we should check the condition a) only.
13251450c749Smrg         */
13261450c749Smrg
13271450c749Smrg        /*
13281450c749Smrg         * [OYVIND-120498]
13291450c749Smrg         * Check packet for valid data:
13301450c749Smrg         * If driver is in sync with datastream, the packet is considered
13311450c749Smrg         * bad if any byte (header and/or data) contains an invalid value.
13321450c749Smrg         *
13331450c749Smrg         * If packet is bad, we discard the first byte and shift the buffer.
13341450c749Smrg         * Next iteration will then check the new situation for validity.
13351450c749Smrg         *
13361450c749Smrg         * If flag MF_SAFE is set in proto[7] and the driver
13371450c749Smrg         * is out of sync, the packet is also considered bad if
13381450c749Smrg         * any of the data bytes contains a valid header byte value.
13391450c749Smrg         * This situation could occur if the buffer contains
13401450c749Smrg         * the tail of one packet and the header of the next.
13411450c749Smrg         *
13421450c749Smrg         * Note: The driver starts in out-of-sync mode (pMse->inSync = 0).
13431450c749Smrg         */
13441450c749Smrg
13451450c749Smrg        baddata = 0;
13461450c749Smrg
13471450c749Smrg        /* All databytes must be valid. */
13481450c749Smrg        for (j = 1; j < pBufP; j++ )
13491450c749Smrg            if ((pBuf[j] & pMse->protoPara[2]) != pMse->protoPara[3])
13501450c749Smrg                baddata = 1;
13511450c749Smrg
13521450c749Smrg        /* If out of sync, don't mistake a header byte for data. */
13531450c749Smrg        if ((pMse->protoPara[7] & MPF_SAFE) && !pMse->inSync)
13541450c749Smrg            for (j = 1; j < pBufP; j++ )
13551450c749Smrg                if ((pBuf[j] & pMse->protoPara[0]) == pMse->protoPara[1])
13561450c749Smrg                    baddata = 1;
13571450c749Smrg
13581450c749Smrg        /* Accept or reject the packet ? */
13591450c749Smrg        if ((pBuf[0] & pMse->protoPara[0]) != pMse->protoPara[1] || baddata) {
13601450c749Smrg            if (pMse->inSync) {
1361659607e0Smrg#ifdef EXTMOUSEDEBUG
13621450c749Smrg                LogMessageVerbSigSafe(X_INFO, -1, "mouse driver lost sync\n");
1363659607e0Smrg#endif
13641450c749Smrg            }
1365659607e0Smrg#ifdef EXTMOUSEDEBUG
13661450c749Smrg            LogMessageVerbSigSafe(X_INFO, -1, "skipping byte %x\n",*pBuf);
1367659607e0Smrg#endif
13681450c749Smrg            /* Tell auto probe that we are out of sync */
13691450c749Smrg            if (pMse->autoProbeMouse && pMse->autoProbe)
13701450c749Smrg                pMse->autoProbeMouse(pInfo, FALSE, pMse->inSync);
13711450c749Smrg            pMse->protoBufTail = --pBufP;
13721450c749Smrg            for (j = 0; j < pBufP; j++)
13731450c749Smrg                pBuf[j] = pBuf[j+1];
13741450c749Smrg            pMse->inSync = 0;
13751450c749Smrg            continue;
13761450c749Smrg        }
13771450c749Smrg        /* Tell auto probe that we were successful */
13781450c749Smrg        if (pMse->autoProbeMouse && pMse->autoProbe)
13791450c749Smrg            pMse->autoProbeMouse(pInfo, TRUE, FALSE);
13801450c749Smrg
13811450c749Smrg        if (!pMse->inSync) {
1382659607e0Smrg#ifdef EXTMOUSEDEBUG
13831450c749Smrg            LogMessageVerbSigSafe(X_INFO, -1, "mouse driver back in sync\n");
1384659607e0Smrg#endif
13851450c749Smrg            pMse->inSync = 1;
13861450c749Smrg        }
1387659607e0Smrg
13881450c749Smrg        if (!pMse->dataGood(pMse))
13891450c749Smrg            continue;
13901450c749Smrg
13911450c749Smrg        /*
13921450c749Smrg         * Packet complete and verified, now process it ...
13931450c749Smrg         */
1394659607e0Smrg    REDO_INTERPRET:
13951450c749Smrg        dz = dw = 0;
13961450c749Smrg        switch (pMse->protocolID) {
13971450c749Smrg        case PROT_LOGIMAN:      /* MouseMan / TrackMan   [CHRIS-211092] */
13981450c749Smrg        case PROT_MS:           /* Microsoft */
13991450c749Smrg            if (pMse->chordMiddle)
14001450c749Smrg                buttons = (((int) pBuf[0] & 0x30) == 0x30) ? 2 :
14011450c749Smrg                                  ((int)(pBuf[0] & 0x20) >> 3)
14021450c749Smrg                                | ((int)(pBuf[0] & 0x10) >> 4);
14031450c749Smrg            else
14041450c749Smrg                buttons = (pMse->lastButtons & 2)
14051450c749Smrg                        | ((int)(pBuf[0] & 0x20) >> 3)
14061450c749Smrg                        | ((int)(pBuf[0] & 0x10) >> 4);
14071450c749Smrg            dx = (signed char)(((pBuf[0] & 0x03) << 6) | (pBuf[1] & 0x3F));
14081450c749Smrg            dy = (signed char)(((pBuf[0] & 0x0C) << 4) | (pBuf[2] & 0x3F));
14091450c749Smrg            break;
14101450c749Smrg
14111450c749Smrg        case PROT_GLIDE:        /* ALPS GlidePoint */
14121450c749Smrg        case PROT_THINKING:     /* ThinkingMouse */
14131450c749Smrg        case PROT_IMSERIAL:     /* IntelliMouse, NetMouse, Mie Mouse, MouseMan+ */
14141450c749Smrg            buttons =  (pMse->lastButtons & (8 + 2))
14151450c749Smrg                     | ((int)(pBuf[0] & 0x20) >> 3)
14161450c749Smrg                     | ((int)(pBuf[0] & 0x10) >> 4);
14171450c749Smrg            dx = (signed char)(((pBuf[0] & 0x03) << 6) | (pBuf[1] & 0x3F));
14181450c749Smrg            dy = (signed char)(((pBuf[0] & 0x0C) << 4) | (pBuf[2] & 0x3F));
14191450c749Smrg            break;
14201450c749Smrg
14211450c749Smrg        case PROT_MSC:          /* Mouse Systems Corp */
14221450c749Smrg            buttons = (~pBuf[0]) & 0x07;
14231450c749Smrg            dx =    (signed char)(pBuf[1]) + (char)(pBuf[3]);
14241450c749Smrg            dy = - ((signed char)(pBuf[2]) + (char)(pBuf[4]));
14251450c749Smrg            break;
14261450c749Smrg
14271450c749Smrg        case PROT_MMHIT:        /* MM_HitTablet */
14281450c749Smrg            buttons = pBuf[0] & 0x07;
14291450c749Smrg            if (buttons != 0)
14301450c749Smrg                buttons = 1 << (buttons - 1);
14311450c749Smrg            dx = (pBuf[0] & 0x10) ?   pBuf[1] : - pBuf[1];
14321450c749Smrg            dy = (pBuf[0] & 0x08) ? - pBuf[2] :   pBuf[2];
14331450c749Smrg            break;
14341450c749Smrg
14351450c749Smrg        case PROT_ACECAD:       /* ACECAD */
14361450c749Smrg            /* ACECAD is almost exactly like MM but the buttons are different */
14371450c749Smrg            buttons = (pBuf[0] & 0x02) | ((pBuf[0] & 0x04) >> 2) |
14381450c749Smrg                      ((pBuf[0] & 1) << 2);
14391450c749Smrg            dx = (pBuf[0] & 0x10) ?   pBuf[1] : - pBuf[1];
14401450c749Smrg            dy = (pBuf[0] & 0x08) ? - pBuf[2] :   pBuf[2];
14411450c749Smrg            break;
14421450c749Smrg
14431450c749Smrg        case PROT_MM:           /* MM Series */
14441450c749Smrg        case PROT_LOGI:         /* Logitech Mice */
14451450c749Smrg            buttons = pBuf[0] & 0x07;
14461450c749Smrg            dx = (pBuf[0] & 0x10) ?   pBuf[1] : - pBuf[1];
14471450c749Smrg            dy = (pBuf[0] & 0x08) ? - pBuf[2] :   pBuf[2];
14481450c749Smrg            break;
14491450c749Smrg
14501450c749Smrg        case PROT_BM:           /* BusMouse */
14511450c749Smrg            buttons = (~pBuf[0]) & 0x07;
14521450c749Smrg            dx =   (signed char)pBuf[1];
14531450c749Smrg            dy = - (signed char)pBuf[2];
14541450c749Smrg            break;
14551450c749Smrg
14561450c749Smrg        case PROT_PS2:          /* PS/2 mouse */
14571450c749Smrg        case PROT_GENPS2:       /* generic PS/2 mouse */
14581450c749Smrg            buttons = (pBuf[0] & 0x04) >> 1 |       /* Middle */
14591450c749Smrg                      (pBuf[0] & 0x02) >> 1 |       /* Right */
14601450c749Smrg                      (pBuf[0] & 0x01) << 2;        /* Left */
14611450c749Smrg            dx = (pBuf[0] & 0x10) ?    (int)pBuf[1]-256  :  (int)pBuf[1];
14621450c749Smrg            dy = (pBuf[0] & 0x20) ?  -((int)pBuf[2]-256) : -(int)pBuf[2];
14631450c749Smrg            break;
14641450c749Smrg
14651450c749Smrg        /* PS/2 mouse variants */
14661450c749Smrg        case PROT_IMPS2:        /* IntelliMouse PS/2 */
14671450c749Smrg        case PROT_NETPS2:       /* NetMouse PS/2 */
14681450c749Smrg            buttons = (pBuf[0] & 0x04) >> 1 |       /* Middle */
14691450c749Smrg                      (pBuf[0] & 0x02) >> 1 |       /* Right */
14701450c749Smrg                      (pBuf[0] & 0x01) << 2 |       /* Left */
14711450c749Smrg                      (pBuf[0] & 0x40) >> 3 |       /* button 4 */
14721450c749Smrg                      (pBuf[0] & 0x80) >> 3;        /* button 5 */
14731450c749Smrg            dx = (pBuf[0] & 0x10) ?    pBuf[1]-256  :  pBuf[1];
14741450c749Smrg            dy = (pBuf[0] & 0x20) ?  -(pBuf[2]-256) : -pBuf[2];
14751450c749Smrg            /*
14761450c749Smrg             * The next cast must be 'signed char' for platforms (like PPC)
14771450c749Smrg             * where char defaults to unsigned.
14781450c749Smrg             */
14791450c749Smrg            dz = (signed char)(pBuf[3] | ((pBuf[3] & 0x08) ? 0xf8 : 0));
14801450c749Smrg            if ((pBuf[3] & 0xf8) && ((pBuf[3] & 0xf8) != 0xf8)) {
14811450c749Smrg                if (pMse->autoProbe) {
14821450c749Smrg                    SetMouseProto(pMse, PROT_EXPPS2);
14831450c749Smrg                    xf86Msg(X_INFO,
14841450c749Smrg                            "Mouse autoprobe: Changing protocol to %s\n",
14851450c749Smrg                            pMse->protocol);
14861450c749Smrg
14871450c749Smrg                    goto REDO_INTERPRET;
14881450c749Smrg                } else
14891450c749Smrg                    dz = 0;
14901450c749Smrg            }
14911450c749Smrg            break;
14921450c749Smrg
14931450c749Smrg        case PROT_EXPPS2:       /* IntelliMouse Explorer PS/2 */
14941450c749Smrg            if (pMse->autoProbe && (pBuf[3] & 0xC0)) {
14951450c749Smrg                SetMouseProto(pMse, PROT_IMPS2);
14961450c749Smrg                xf86Msg(X_INFO,"Mouse autoprobe: Changing protocol to %s\n",
14971450c749Smrg                        pMse->protocol);
14981450c749Smrg                goto REDO_INTERPRET;
14991450c749Smrg            }
15001450c749Smrg            buttons = (pBuf[0] & 0x04) >> 1 |       /* Middle */
15011450c749Smrg                      (pBuf[0] & 0x02) >> 1 |       /* Right */
15021450c749Smrg                      (pBuf[0] & 0x01) << 2 |       /* Left */
15031450c749Smrg                      (pBuf[3] & 0x10) >> 1 |       /* button 4 */
15041450c749Smrg                      (pBuf[3] & 0x20) >> 1;        /* button 5 */
15051450c749Smrg            dx = (pBuf[0] & 0x10) ?    pBuf[1]-256  :  pBuf[1];
15061450c749Smrg            dy = (pBuf[0] & 0x20) ?  -(pBuf[2]-256) : -pBuf[2];
15071450c749Smrg            if (pMse->negativeW != MSE_NOAXISMAP) {
15081450c749Smrg                switch (pBuf[3] & 0x0f) {
15091450c749Smrg                case 0x00:          break;
15101450c749Smrg                case 0x01: dz =  1; break;
15111450c749Smrg                case 0x02: dw =  1; break;
15121450c749Smrg                case 0x0e: dw = -1; break;
15131450c749Smrg                case 0x0f: dz = -1; break;
15141450c749Smrg                default:
15151450c749Smrg                    xf86Msg(X_INFO,
15161450c749Smrg                            "Mouse autoprobe: Disabling secondary wheel\n");
15171450c749Smrg                    pMse->negativeW = pMse->positiveW = MSE_NOAXISMAP;
15181450c749Smrg                }
15191450c749Smrg            }
15201450c749Smrg            if (pMse->negativeW == MSE_NOAXISMAP)
15211450c749Smrg                dz = (pBuf[3]&0x08) ? (pBuf[3]&0x0f) - 16 : (pBuf[3]&0x0f);
15221450c749Smrg            break;
15231450c749Smrg
15241450c749Smrg        case PROT_MMPS2:        /* MouseMan+ PS/2 */
15251450c749Smrg            buttons = (pBuf[0] & 0x04) >> 1 |       /* Middle */
15261450c749Smrg                      (pBuf[0] & 0x02) >> 1 |       /* Right */
15271450c749Smrg                      (pBuf[0] & 0x01) << 2;        /* Left */
15281450c749Smrg            dx = (pBuf[0] & 0x10) ? pBuf[1] - 256 : pBuf[1];
15291450c749Smrg            if (((pBuf[0] & 0x48) == 0x48) &&
15301450c749Smrg                (abs(dx) > 191) &&
15311450c749Smrg                ((((pBuf[2] & 0x03) << 2) | 0x02) == (pBuf[1] & 0x0f))) {
15321450c749Smrg                /* extended data packet */
15331450c749Smrg                switch ((((pBuf[0] & 0x30) >> 2) | ((pBuf[1] & 0x30) >> 4))) {
15341450c749Smrg                case 1:         /* wheel data packet */
15351450c749Smrg                    buttons |= ((pBuf[2] & 0x10) ? 0x08 : 0) | /* 4th button */
15361450c749Smrg                               ((pBuf[2] & 0x20) ? 0x10 : 0);  /* 5th button */
15371450c749Smrg                    dx = dy = 0;
15381450c749Smrg                    dz = (pBuf[2] & 0x08) ? (pBuf[2] & 0x0f) - 16 :
15391450c749Smrg                                            (pBuf[2] & 0x0f);
15401450c749Smrg                    break;
15411450c749Smrg                case 2:         /* Logitech reserves this packet type */
15421450c749Smrg                    /*
15431450c749Smrg                     * IBM ScrollPoint uses this packet to encode its
15441450c749Smrg                     * stick movement.
15451450c749Smrg                     */
15461450c749Smrg                    buttons |= (pMse->lastButtons & ~0x07);
15471450c749Smrg                    dx = dy = 0;
15481450c749Smrg                    dz = (pBuf[2] & 0x80) ? ((pBuf[2] >> 4) & 0x0f) - 16 :
15491450c749Smrg                                            ((pBuf[2] >> 4) & 0x0f);
15501450c749Smrg                    dw = (pBuf[2] & 0x08) ? (pBuf[2] & 0x0f) - 16 :
15511450c749Smrg                                            (pBuf[2] & 0x0f);
15521450c749Smrg                    break;
15531450c749Smrg                case 0:         /* device type packet - shouldn't happen */
15541450c749Smrg                default:
15551450c749Smrg                    buttons |= (pMse->lastButtons & ~0x07);
15561450c749Smrg                    dx = dy = 0;
15571450c749Smrg                    dz = 0;
15581450c749Smrg                    break;
15591450c749Smrg                }
15601450c749Smrg            } else {
15611450c749Smrg                buttons |= (pMse->lastButtons & ~0x07);
15621450c749Smrg                dx = (pBuf[0] & 0x10) ?    pBuf[1]-256  :  pBuf[1];
15631450c749Smrg                dy = (pBuf[0] & 0x20) ?  -(pBuf[2]-256) : -pBuf[2];
15641450c749Smrg            }
15651450c749Smrg            break;
15661450c749Smrg
15671450c749Smrg        case PROT_GLIDEPS2:     /* GlidePoint PS/2 */
15681450c749Smrg            buttons = (pBuf[0] & 0x04) >> 1 |       /* Middle */
15691450c749Smrg                      (pBuf[0] & 0x02) >> 1 |       /* Right */
15701450c749Smrg                      (pBuf[0] & 0x01) << 2 |       /* Left */
15711450c749Smrg                      ((pBuf[0] & 0x08) ? 0 : 0x08);/* fourth button */
15721450c749Smrg            dx = (pBuf[0] & 0x10) ?    pBuf[1]-256  :  pBuf[1];
15731450c749Smrg            dy = (pBuf[0] & 0x20) ?  -(pBuf[2]-256) : -pBuf[2];
15741450c749Smrg            break;
15751450c749Smrg
15761450c749Smrg        case PROT_NETSCPS2:     /* NetScroll PS/2 */
15771450c749Smrg            buttons = (pBuf[0] & 0x04) >> 1 |       /* Middle */
15781450c749Smrg                      (pBuf[0] & 0x02) >> 1 |       /* Right */
15791450c749Smrg                      (pBuf[0] & 0x01) << 2 |       /* Left */
15801450c749Smrg                      ((pBuf[3] & 0x02) ? 0x08 : 0) | /* button 4 */
15811450c749Smrg                      ((pBuf[3] & 0x01) ? 0x10 : 0);  /* button 5 */
15821450c749Smrg            dx = (pBuf[0] & 0x10) ?    pBuf[1]-256  :  pBuf[1];
15831450c749Smrg            dy = (pBuf[0] & 0x20) ?  -(pBuf[2]-256) : -pBuf[2];
15841450c749Smrg            dz = (pBuf[3] & 0x10) ? pBuf[4] - 256 : pBuf[4];
15851450c749Smrg            break;
15861450c749Smrg
15871450c749Smrg        case PROT_THINKPS2:     /* ThinkingMouse PS/2 */
15881450c749Smrg            buttons = (pBuf[0] & 0x04) >> 1 |       /* Middle */
15891450c749Smrg                      (pBuf[0] & 0x02) >> 1 |       /* Right */
15901450c749Smrg                      (pBuf[0] & 0x01) << 2 |       /* Left */
15911450c749Smrg                      ((pBuf[0] & 0x08) ? 0x08 : 0);/* fourth button */
15921450c749Smrg            pBuf[1] |= (pBuf[0] & 0x40) ? 0x80 : 0x00;
15931450c749Smrg            dx = (pBuf[0] & 0x10) ?    pBuf[1]-256  :  pBuf[1];
15941450c749Smrg            dy = (pBuf[0] & 0x20) ?  -(pBuf[2]-256) : -pBuf[2];
15951450c749Smrg            break;
15961450c749Smrg
15971450c749Smrg        case PROT_SYSMOUSE:     /* sysmouse */
15981450c749Smrg            buttons = (~pBuf[0]) & 0x07;
15991450c749Smrg            dx =    (signed char)(pBuf[1]) + (signed char)(pBuf[3]);
16001450c749Smrg            dy = - ((signed char)(pBuf[2]) + (signed char)(pBuf[4]));
16011450c749Smrg            /* FreeBSD sysmouse sends additional data bytes */
16021450c749Smrg            if (pMse->protoPara[4] >= 8) {
16031450c749Smrg                /*
16041450c749Smrg                 * These casts must be 'signed char' for platforms (like PPC)
16051450c749Smrg                 * where char defaults to unsigned.
16061450c749Smrg                 */
16071450c749Smrg                dz = ((signed char)(pBuf[5] << 1) +
16081450c749Smrg                      (signed char)(pBuf[6] << 1)) >> 1;
16091450c749Smrg                buttons |= (int)(~pBuf[7] & 0x7f) << 3;
16101450c749Smrg            }
16111450c749Smrg            break;
1612659607e0Smrg
16131450c749Smrg        case PROT_VALUMOUSESCROLL:      /* Kensington ValuMouseScroll */
1614659607e0Smrg            buttons = ((int)(pBuf[0] & 0x20) >> 3)
1615659607e0Smrg                      | ((int)(pBuf[0] & 0x10) >> 4)
1616659607e0Smrg                      | ((int)(pBuf[3] & 0x10) >> 3);
1617fecac299Smrg            dx = (signed char)(((pBuf[0] & 0x03) << 6) | (pBuf[1] &  0x3F));
1618fecac299Smrg            dy = (signed char)(((pBuf[0] & 0x0C) << 4) | (pBuf[2] &  0x3F));
16191450c749Smrg            dz = (pBuf[3] & 0x08) ? ((int)(pBuf[3] & 0x0F) - 0x10) :
1620659607e0Smrg                                    ((int)(pBuf[3] & 0x0F));
16211450c749Smrg            break;
1622659607e0Smrg
16231450c749Smrg        default: /* There's a table error */
1624659607e0Smrg#ifdef EXTMOUSEDEBUG
16251450c749Smrg            LogMessageVerbSigSafe(X_INFO, -1, "mouse table error\n");
1626659607e0Smrg#endif
16271450c749Smrg            continue;
16281450c749Smrg        }
1629659607e0Smrg#ifdef EXTMOUSEDEBUG
16301450c749Smrg        LogMessageVerbSigSafe(X_INFO, -1, "packet");
16311450c749Smrg        for ( j=0; j < pBufP; j++)
16321450c749Smrg            LogMessageVerbSigSafe(X_INFO, -1, " %x",pBuf[j]);
16331450c749Smrg        LogMessageVerbSigSafe(X_INFO, -1, "\n");
1634659607e0Smrg#endif
1635659607e0Smrg
1636659607e0Smrgpost_event:
1637659607e0Smrg#ifdef EXTMOUSEDEBUG
16381450c749Smrg        LogMessageVerbSigSafe(X_INFO, -1, "dx=%i dy=%i dz=%i dw=%i buttons=%x\n",dx,dy,dz,dw,buttons);
1639659607e0Smrg#endif
16401450c749Smrg        /* When auto-probing check if data makes sense */
16411450c749Smrg        if (pMse->checkMovements && pMse->autoProbe)
16421450c749Smrg            pMse->checkMovements(pInfo,dx,dy);
16431450c749Smrg        /* post an event */
16441450c749Smrg        pMse->PostEvent(pInfo, buttons, dx, dy, dz, dw);
16451450c749Smrg
16461450c749Smrg        /*
16471450c749Smrg         * We don't reset pBufP here yet, as there may be an additional data
16481450c749Smrg         * byte in some protocols. See above.
16491450c749Smrg         */
1650659607e0Smrg    }
1651659607e0Smrg    pMse->protoBufTail = pBufP;
1652659607e0Smrg}
1653659607e0Smrg
1654659607e0Smrg/*
1655659607e0Smrg * MouseCtrl --
1656f5c3fc63Smrg *      Alter the control parameters for the mouse. Note that all
1657f5c3fc63Smrg *      settings are now handled by dix.
1658659607e0Smrg */
1659659607e0Smrg
1660659607e0Smrgstatic void
1661659607e0SmrgMouseCtrl(DeviceIntPtr device, PtrCtrl *ctrl)
1662659607e0Smrg{
1663f5c3fc63Smrg    /* This function intentionally left blank */
1664659607e0Smrg}
1665659607e0Smrg
1666659607e0Smrg/*
1667659607e0Smrg ***************************************************************************
1668659607e0Smrg *
1669659607e0Smrg * MouseProc --
1670659607e0Smrg *
1671659607e0Smrg ***************************************************************************
1672659607e0Smrg */
1673659607e0Smrg
1674659607e0Smrgstatic int
1675659607e0SmrgMouseProc(DeviceIntPtr device, int what)
1676659607e0Smrg{
1677659607e0Smrg    InputInfoPtr pInfo;
1678659607e0Smrg    MouseDevPtr pMse;
1679659607e0Smrg    mousePrivPtr mPriv;
1680659607e0Smrg    unsigned char map[MSE_MAXBUTTONS + 1];
1681659607e0Smrg    int i;
1682fecac299Smrg    Atom btn_labels[MSE_MAXBUTTONS] = {0};
1683fecac299Smrg    Atom axes_labels[2] = { 0, 0 };
1684fecac299Smrg
1685659607e0Smrg    pInfo = device->public.devicePrivate;
1686659607e0Smrg    pMse = pInfo->private;
1687659607e0Smrg    pMse->device = device;
1688659607e0Smrg
1689659607e0Smrg    switch (what)
1690659607e0Smrg    {
1691659607e0Smrg    case DEVICE_INIT:
16921450c749Smrg        device->public.on = FALSE;
16931450c749Smrg        /*
16941450c749Smrg         * [KAZU-241097] We don't know exactly how many buttons the
16951450c749Smrg         * device has, so setup the map with the maximum number.
16961450c749Smrg         */
16971450c749Smrg        for (i = 0; i < MSE_MAXBUTTONS; i++)
16981450c749Smrg            map[i + 1] = i + 1;
16991450c749Smrg
17001450c749Smrg        MouseInitButtonLabels(btn_labels);
17011450c749Smrg        axes_labels[0] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_X);
17021450c749Smrg        axes_labels[1] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_Y);
17031450c749Smrg
17041450c749Smrg        InitPointerDeviceStruct((DevicePtr)device, map,
17051450c749Smrg                                min(pMse->buttons, MSE_MAXBUTTONS),
1706fecac299Smrg                                btn_labels,
1707659607e0Smrg                                pMse->Ctrl,
17081450c749Smrg                                GetMotionHistorySize(), 2,
17091450c749Smrg                                axes_labels
1710659607e0Smrg                                );
1711659607e0Smrg
17121450c749Smrg        /* X valuator */
17131450c749Smrg        xf86InitValuatorAxisStruct(device, 0,
1714fecac299Smrg                axes_labels[0],
1715dd4cbfe8Smrg                -1, -1, 1, 0, 1
1716dd4cbfe8Smrg#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 12
1717dd4cbfe8Smrg                , Relative
1718dd4cbfe8Smrg#endif
1719dd4cbfe8Smrg                );
17201450c749Smrg        xf86InitValuatorDefaults(device, 0);
17211450c749Smrg        /* Y valuator */
17221450c749Smrg        xf86InitValuatorAxisStruct(device, 1,
1723fecac299Smrg                axes_labels[1],
1724dd4cbfe8Smrg                -1, -1, 1, 0, 1
1725dd4cbfe8Smrg#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 12
1726dd4cbfe8Smrg                , Relative
1727659607e0Smrg#endif
1728dd4cbfe8Smrg                );
17291450c749Smrg        xf86InitValuatorDefaults(device, 1);
1730659607e0Smrg
1731659607e0Smrg#ifdef EXTMOUSEDEBUG
17321450c749Smrg        ErrorF("assigning %p name=%s\n", device, pInfo->name);
1733659607e0Smrg#endif
17341450c749Smrg        MouseInitProperties(device);
17351450c749Smrg        break;
1736659607e0Smrg
1737659607e0Smrg    case DEVICE_ON:
17381450c749Smrg
1739659607e0Smrg	pInfo->fd = xf86OpenSerial(pInfo->options);
1740659607e0Smrg	if (pInfo->fd == -1)
1741659607e0Smrg	    xf86Msg(X_WARNING, "%s: cannot open input device\n", pInfo->name);
1742659607e0Smrg	else {
17435ddc3750Smrg#if defined(__NetBSD__) && defined(WSCONS_SUPPORT) && defined(WSMOUSEIO_SETVERSION)
1744afecbbc8Smrg	     int version = WSMOUSE_EVENT_VERSION;
1745afecbbc8Smrg	     if (ioctl(pInfo->fd, WSMOUSEIO_SETVERSION, &version) == -1)
1746afecbbc8Smrg	         xf86Msg(X_WARNING, "%s: cannot set version\n", pInfo->name);
17475ddc3750Smrg#endif
1748659607e0Smrg	    if (pMse->xisbscale)
1749659607e0Smrg		pMse->buffer = XisbNew(pInfo->fd, pMse->xisbscale * 4);
1750659607e0Smrg	    else
1751659607e0Smrg		pMse->buffer = XisbNew(pInfo->fd, 64);
1752659607e0Smrg	    if (!pMse->buffer) {
1753659607e0Smrg		xf86CloseSerial(pInfo->fd);
1754659607e0Smrg		pInfo->fd = -1;
1755659607e0Smrg	    } else {
1756659607e0Smrg		if (!SetupMouse(pInfo)) {
1757659607e0Smrg		    xf86CloseSerial(pInfo->fd);
1758659607e0Smrg		    pInfo->fd = -1;
1759659607e0Smrg		    XisbFree(pMse->buffer);
1760659607e0Smrg		    pMse->buffer = NULL;
1761659607e0Smrg		} else {
1762659607e0Smrg		    mPriv = (mousePrivPtr)pMse->mousePriv;
1763659607e0Smrg		    if (mPriv != NULL) {
1764659607e0Smrg			if ( pMse->protocolID != PROT_AUTO) {
1765659607e0Smrg			    pMse->inSync = TRUE; /* @@@ */
1766659607e0Smrg			    if (mPriv->soft)
1767659607e0Smrg				mPriv->autoState = AUTOPROBE_GOOD;
1768659607e0Smrg			    else
1769659607e0Smrg				mPriv->autoState = AUTOPROBE_H_GOOD;
1770659607e0Smrg			} else {
1771659607e0Smrg			    if (mPriv->soft)
1772659607e0Smrg				mPriv->autoState = AUTOPROBE_NOPROTO;
1773659607e0Smrg			    else
1774659607e0Smrg				mPriv->autoState = AUTOPROBE_H_NOPROTO;
1775659607e0Smrg			}
1776659607e0Smrg		    }
1777659607e0Smrg		    xf86FlushInput(pInfo->fd);
1778659607e0Smrg		    xf86AddEnabledDevice(pInfo);
1779fecac299Smrg		    if (pMse->emulate3Buttons || pMse->emulate3ButtonsSoft) {
1780fecac299Smrg			RegisterBlockAndWakeupHandlers (MouseBlockHandler,
1781fecac299Smrg							MouseWakeupHandler,
1782fecac299Smrg							(pointer) pInfo);
1783fecac299Smrg		    }
1784659607e0Smrg		}
1785659607e0Smrg	    }
1786659607e0Smrg	}
1787659607e0Smrg	pMse->lastButtons = 0;
1788659607e0Smrg	pMse->lastMappedButtons = 0;
1789659607e0Smrg	pMse->emulateState = 0;
1790659607e0Smrg	pMse->emulate3Pending = FALSE;
1791659607e0Smrg	pMse->wheelButtonExpires = GetTimeInMillis ();
1792659607e0Smrg	device->public.on = TRUE;
1793659607e0Smrg	FlushButtons(pMse);
1794659607e0Smrg	break;
17951450c749Smrg
1796659607e0Smrg    case DEVICE_OFF:
17971450c749Smrg        if (pInfo->fd != -1) {
17981450c749Smrg            xf86RemoveEnabledDevice(pInfo);
17991450c749Smrg            if (pMse->buffer) {
18001450c749Smrg                XisbFree(pMse->buffer);
18011450c749Smrg                pMse->buffer = NULL;
18021450c749Smrg            }
18031450c749Smrg            xf86CloseSerial(pInfo->fd);
18041450c749Smrg            pInfo->fd = -1;
18051450c749Smrg            if (pMse->emulate3Buttons || pMse->emulate3ButtonsSoft)
18061450c749Smrg            {
18071450c749Smrg                RemoveBlockAndWakeupHandlers (MouseBlockHandler,
18081450c749Smrg                                              MouseWakeupHandler,
18091450c749Smrg                                              (pointer) pInfo);
18101450c749Smrg            }
18111450c749Smrg        }
18121450c749Smrg        device->public.on = FALSE;
18131450c749Smrg        break;
1814f5c3fc63Smrg    case DEVICE_CLOSE:
18151450c749Smrg        free(pMse->mousePriv);
18161450c749Smrg        pMse->mousePriv = NULL;
18171450c749Smrg        break;
18181450c749Smrg
18191450c749Smrg    default:
18201450c749Smrg        return BadValue;
1821659607e0Smrg    }
1822659607e0Smrg    return Success;
1823659607e0Smrg}
1824659607e0Smrg
1825659607e0Smrg/**********************************************************************
1826659607e0Smrg *
1827659607e0Smrg * FlushButtons -- reset button states.
1828659607e0Smrg *
1829659607e0Smrg **********************************************************************/
1830659607e0Smrg
1831659607e0Smrgstatic void
1832659607e0SmrgFlushButtons(MouseDevPtr pMse)
1833659607e0Smrg{
1834659607e0Smrg    pMse->lastButtons = 0;
1835659607e0Smrg    pMse->lastMappedButtons = 0;
1836659607e0Smrg}
1837659607e0Smrg
1838659607e0Smrg/**********************************************************************
1839659607e0Smrg *
1840659607e0Smrg *  Emulate3Button support code
1841659607e0Smrg *
1842659607e0Smrg **********************************************************************/
1843659607e0Smrg
1844659607e0Smrg
1845659607e0Smrg/*
1846659607e0Smrg * Lets create a simple finite-state machine for 3 button emulation:
1847659607e0Smrg *
1848659607e0Smrg * We track buttons 1 and 3 (left and right).  There are 11 states:
1849659607e0Smrg *   0 ground           - initial state
1850659607e0Smrg *   1 delayed left     - left pressed, waiting for right
1851659607e0Smrg *   2 delayed right    - right pressed, waiting for left
1852659607e0Smrg *   3 pressed middle   - right and left pressed, emulated middle sent
1853659607e0Smrg *   4 pressed left     - left pressed and sent
1854659607e0Smrg *   5 pressed right    - right pressed and sent
1855659607e0Smrg *   6 released left    - left released after emulated middle
1856659607e0Smrg *   7 released right   - right released after emulated middle
1857659607e0Smrg *   8 repressed left   - left pressed after released left
1858659607e0Smrg *   9 repressed right  - right pressed after released right
1859659607e0Smrg *  10 pressed both     - both pressed, not emulating middle
1860659607e0Smrg *
1861659607e0Smrg * At each state, we need handlers for the following events
1862659607e0Smrg *   0: no buttons down
1863659607e0Smrg *   1: left button down
1864659607e0Smrg *   2: right button down
1865659607e0Smrg *   3: both buttons down
1866659607e0Smrg *   4: emulate3Timeout passed without a button change
1867659607e0Smrg * Note that button events are not deltas, they are the set of buttons being
1868659607e0Smrg * pressed now.  It's possible (ie, mouse hardware does it) to go from (eg)
1869659607e0Smrg * left down to right down without anything in between, so all cases must be
1870659607e0Smrg * handled.
1871659607e0Smrg *
1872659607e0Smrg * a handler consists of three values:
1873659607e0Smrg *   0: action1
1874659607e0Smrg *   1: action2
1875659607e0Smrg *   2: new emulation state
1876659607e0Smrg *
1877659607e0Smrg * action > 0: ButtonPress
1878659607e0Smrg * action = 0: nothing
1879659607e0Smrg * action < 0: ButtonRelease
1880659607e0Smrg *
1881659607e0Smrg * The comment preceeding each section is the current emulation state.
1882659607e0Smrg * The comments to the right are of the form
1883659607e0Smrg *      <button state> (<events>) -> <new emulation state>
1884659607e0Smrg * which should be read as
1885659607e0Smrg *      If the buttons are in <button state>, generate <events> then go to
1886659607e0Smrg *      <new emulation state>.
1887659607e0Smrg */
1888659607e0Smrgstatic signed char stateTab[11][5][3] = {
1889659607e0Smrg/* 0 ground */
1890659607e0Smrg  {
1891659607e0Smrg    {  0,  0,  0 },   /* nothing -> ground (no change) */
1892659607e0Smrg    {  0,  0,  1 },   /* left -> delayed left */
1893659607e0Smrg    {  0,  0,  2 },   /* right -> delayed right */
1894659607e0Smrg    {  2,  0,  3 },   /* left & right (middle press) -> pressed middle */
1895659607e0Smrg    {  0,  0, -1 }    /* timeout N/A */
1896659607e0Smrg  },
1897659607e0Smrg/* 1 delayed left */
1898659607e0Smrg  {
1899659607e0Smrg    {  1, -1,  0 },   /* nothing (left event) -> ground */
1900659607e0Smrg    {  0,  0,  1 },   /* left -> delayed left (no change) */
1901659607e0Smrg    {  1, -1,  2 },   /* right (left event) -> delayed right */
1902659607e0Smrg    {  2,  0,  3 },   /* left & right (middle press) -> pressed middle */
1903659607e0Smrg    {  1,  0,  4 },   /* timeout (left press) -> pressed left */
1904659607e0Smrg  },
1905659607e0Smrg/* 2 delayed right */
1906659607e0Smrg  {
1907659607e0Smrg    {  3, -3,  0 },   /* nothing (right event) -> ground */
1908659607e0Smrg    {  3, -3,  1 },   /* left (right event) -> delayed left (no change) */
1909659607e0Smrg    {  0,  0,  2 },   /* right -> delayed right (no change) */
1910659607e0Smrg    {  2,  0,  3 },   /* left & right (middle press) -> pressed middle */
1911659607e0Smrg    {  3,  0,  5 },   /* timeout (right press) -> pressed right */
1912659607e0Smrg  },
1913659607e0Smrg/* 3 pressed middle */
1914659607e0Smrg  {
1915659607e0Smrg    { -2,  0,  0 },   /* nothing (middle release) -> ground */
1916659607e0Smrg    {  0,  0,  7 },   /* left -> released right */
1917659607e0Smrg    {  0,  0,  6 },   /* right -> released left */
1918659607e0Smrg    {  0,  0,  3 },   /* left & right -> pressed middle (no change) */
1919659607e0Smrg    {  0,  0, -1 },   /* timeout N/A */
1920659607e0Smrg  },
1921659607e0Smrg/* 4 pressed left */
1922659607e0Smrg  {
1923659607e0Smrg    { -1,  0,  0 },   /* nothing (left release) -> ground */
1924659607e0Smrg    {  0,  0,  4 },   /* left -> pressed left (no change) */
1925659607e0Smrg    { -1,  0,  2 },   /* right (left release) -> delayed right */
1926659607e0Smrg    {  3,  0, 10 },   /* left & right (right press) -> pressed both */
1927659607e0Smrg    {  0,  0, -1 },   /* timeout N/A */
1928659607e0Smrg  },
1929659607e0Smrg/* 5 pressed right */
1930659607e0Smrg  {
1931659607e0Smrg    { -3,  0,  0 },   /* nothing (right release) -> ground */
1932659607e0Smrg    { -3,  0,  1 },   /* left (right release) -> delayed left */
1933659607e0Smrg    {  0,  0,  5 },   /* right -> pressed right (no change) */
1934659607e0Smrg    {  1,  0, 10 },   /* left & right (left press) -> pressed both */
1935659607e0Smrg    {  0,  0, -1 },   /* timeout N/A */
1936659607e0Smrg  },
1937659607e0Smrg/* 6 released left */
1938659607e0Smrg  {
1939659607e0Smrg    { -2,  0,  0 },   /* nothing (middle release) -> ground */
1940659607e0Smrg    { -2,  0,  1 },   /* left (middle release) -> delayed left */
1941659607e0Smrg    {  0,  0,  6 },   /* right -> released left (no change) */
1942659607e0Smrg    {  1,  0,  8 },   /* left & right (left press) -> repressed left */
1943659607e0Smrg    {  0,  0, -1 },   /* timeout N/A */
1944659607e0Smrg  },
1945659607e0Smrg/* 7 released right */
1946659607e0Smrg  {
1947659607e0Smrg    { -2,  0,  0 },   /* nothing (middle release) -> ground */
1948659607e0Smrg    {  0,  0,  7 },   /* left -> released right (no change) */
1949659607e0Smrg    { -2,  0,  2 },   /* right (middle release) -> delayed right */
1950659607e0Smrg    {  3,  0,  9 },   /* left & right (right press) -> repressed right */
1951659607e0Smrg    {  0,  0, -1 },   /* timeout N/A */
1952659607e0Smrg  },
1953659607e0Smrg/* 8 repressed left */
1954659607e0Smrg  {
1955659607e0Smrg    { -2, -1,  0 },   /* nothing (middle release, left release) -> ground */
1956659607e0Smrg    { -2,  0,  4 },   /* left (middle release) -> pressed left */
1957659607e0Smrg    { -1,  0,  6 },   /* right (left release) -> released left */
1958659607e0Smrg    {  0,  0,  8 },   /* left & right -> repressed left (no change) */
1959659607e0Smrg    {  0,  0, -1 },   /* timeout N/A */
1960659607e0Smrg  },
1961659607e0Smrg/* 9 repressed right */
1962659607e0Smrg  {
1963659607e0Smrg    { -2, -3,  0 },   /* nothing (middle release, right release) -> ground */
1964659607e0Smrg    { -3,  0,  7 },   /* left (right release) -> released right */
1965659607e0Smrg    { -2,  0,  5 },   /* right (middle release) -> pressed right */
1966659607e0Smrg    {  0,  0,  9 },   /* left & right -> repressed right (no change) */
1967659607e0Smrg    {  0,  0, -1 },   /* timeout N/A */
1968659607e0Smrg  },
1969659607e0Smrg/* 10 pressed both */
1970659607e0Smrg  {
1971659607e0Smrg    { -1, -3,  0 },   /* nothing (left release, right release) -> ground */
1972659607e0Smrg    { -3,  0,  4 },   /* left (right release) -> pressed left */
1973659607e0Smrg    { -1,  0,  5 },   /* right (left release) -> pressed right */
1974659607e0Smrg    {  0,  0, 10 },   /* left & right -> pressed both (no change) */
1975659607e0Smrg    {  0,  0, -1 },   /* timeout N/A */
1976659607e0Smrg  },
1977659607e0Smrg};
1978659607e0Smrg
1979659607e0Smrg/*
1980659607e0Smrg * Table to allow quick reversal of natural button mapping to correct mapping
1981659607e0Smrg */
1982659607e0Smrg
1983659607e0Smrg/*
1984659607e0Smrg * [JCH-96/01/21] The ALPS GlidePoint pad extends the MS protocol
1985659607e0Smrg * with a fourth button activated by tapping the PAD.
1986659607e0Smrg * The 2nd line corresponds to 4th button on; the drv sends
1987659607e0Smrg * the buttons in the following map (MSBit described first) :
1988659607e0Smrg * 0 | 4th | 1st | 2nd | 3rd
1989659607e0Smrg * And we remap them (MSBit described first) :
1990659607e0Smrg * 0 | 4th | 3rd | 2nd | 1st
1991659607e0Smrg */
1992659607e0Smrgstatic char reverseMap[16] = { 0,  4,  2,  6,
19931450c749Smrg                               1,  5,  3,  7,
19941450c749Smrg                               8, 12, 10, 14,
19951450c749Smrg                               9, 13, 11, 15 };
1996659607e0Smrg
19971450c749Smrgstatic char hitachMap[16] = {  0,  2,  1,  3,
19981450c749Smrg                               8, 10,  9, 11,
19991450c749Smrg                               4,  6,  5,  7,
20001450c749Smrg                              12, 14, 13, 15 };
2001659607e0Smrg
20021450c749Smrg#define reverseBits(map, b)     (((b) & ~0x0f) | map[(b) & 0x0f])
2003659607e0Smrg
2004659607e0Smrgstatic CARD32
2005659607e0SmrgbuttonTimer(InputInfoPtr pInfo)
2006659607e0Smrg{
2007659607e0Smrg    MouseDevPtr pMse;
20081450c749Smrg    int sigstate;
2009659607e0Smrg    int id;
2010659607e0Smrg
2011659607e0Smrg    pMse = pInfo->private;
2012659607e0Smrg
2013659607e0Smrg    sigstate = xf86BlockSIGIO ();
2014659607e0Smrg
2015659607e0Smrg    pMse->emulate3Pending = FALSE;
2016659607e0Smrg    if ((id = stateTab[pMse->emulateState][4][0]) != 0) {
2017659607e0Smrg        xf86PostButtonEvent(pInfo->dev, 0, abs(id), (id >= 0), 0, 0);
2018659607e0Smrg        pMse->emulateState = stateTab[pMse->emulateState][4][2];
2019659607e0Smrg    } else {
20201450c749Smrg        LogMessageVerbSigSafe(X_WARNING, -1, "Got unexpected buttonTimer in state %d\n", pMse->emulateState);
2021659607e0Smrg    }
2022659607e0Smrg
2023659607e0Smrg    xf86UnblockSIGIO (sigstate);
2024659607e0Smrg    return 0;
2025659607e0Smrg}
2026659607e0Smrg
20271450c749Smrgstatic void
20281450c749SmrgEmulate3ButtonsSetEnabled(InputInfoPtr pInfo, Bool enable)
20291450c749Smrg{
20301450c749Smrg    MouseDevPtr pMse = pInfo->private;
20311450c749Smrg
20321450c749Smrg    if (pMse->emulate3Buttons == enable)
20331450c749Smrg        return;
20341450c749Smrg
20351450c749Smrg    pMse->emulate3Buttons = enable;
20361450c749Smrg
20371450c749Smrg    if (enable) {
20381450c749Smrg        pMse->emulateState = 0;
20391450c749Smrg        pMse->emulate3Pending = FALSE;
20401450c749Smrg        pMse->emulate3ButtonsSoft = FALSE; /* specifically requested now */
20411450c749Smrg
20421450c749Smrg        RegisterBlockAndWakeupHandlers (MouseBlockHandler, MouseWakeupHandler,
20431450c749Smrg                                        (pointer) pInfo);
20441450c749Smrg    } else {
20451450c749Smrg        if (pMse->emulate3Pending)
20461450c749Smrg            buttonTimer(pInfo);
20471450c749Smrg
20481450c749Smrg        RemoveBlockAndWakeupHandlers (MouseBlockHandler, MouseWakeupHandler,
20491450c749Smrg                                      (pointer) pInfo);
20501450c749Smrg    }
20511450c749Smrg}
20521450c749Smrg
2053659607e0Smrgstatic Bool
2054659607e0SmrgEmulate3ButtonsSoft(InputInfoPtr pInfo)
2055659607e0Smrg{
2056659607e0Smrg    MouseDevPtr pMse = pInfo->private;
2057659607e0Smrg
2058659607e0Smrg    if (!pMse->emulate3ButtonsSoft)
20591450c749Smrg        return TRUE;
2060659607e0Smrg
20617022890bSmartin#if defined(__NetBSD__) && defined(WSCONS_SUPPORT)
20627022890bSmartin   /*
20637022890bSmartin    * XXXX - check for pMse->protocolID being wsmouse? Why doesn't it
20647022890bSmartin    * have it's own ID?
20657022890bSmartin    * On NetBSD a wsmouse is a multiplexed device. Imagine a notebook
20667022890bSmartin    * with two-button mousepad, and an external USB mouse plugged in
20677022890bSmartin    * temporarily. After using button 3 on the external mouse and
20687022890bSmartin    * unplugging it again, the mousepad will still need to emulate
20697022890bSmartin    * 3 buttons.
20707022890bSmartin    */
20717022890bSmartin   return TRUE;
20727022890bSmartin#endif
2073659607e0Smrg
2074659607e0Smrg    if (pMse->emulate3Pending)
2075659607e0Smrg	buttonTimer(pInfo);
2076659607e0Smrg
20771450c749Smrg    LogMessageVerbSigSafe(X_INFO, 4, "mouse: 3rd Button detected: disabling emulate3Button\n");
2078fecac299Smrg
20791450c749Smrg    Emulate3ButtonsSetEnabled(pInfo, FALSE);
2080fecac299Smrg
2081659607e0Smrg    return FALSE;
2082659607e0Smrg}
2083659607e0Smrg
2084659607e0Smrgstatic void MouseBlockHandler(pointer data,
20851450c749Smrg                              struct timeval **waitTime,
20861450c749Smrg                              pointer LastSelectMask)
2087659607e0Smrg{
2088659607e0Smrg    InputInfoPtr    pInfo = (InputInfoPtr) data;
20891450c749Smrg    MouseDevPtr     pMse = (MouseDevPtr) pInfo->private;
20901450c749Smrg    int             ms;
2091659607e0Smrg
2092659607e0Smrg    if (pMse->emulate3Pending)
2093659607e0Smrg    {
20941450c749Smrg        ms = pMse->emulate3Expires - GetTimeInMillis ();
20951450c749Smrg        if (ms <= 0)
20961450c749Smrg            ms = 0;
20971450c749Smrg        AdjustWaitForDelay (waitTime, ms);
2098659607e0Smrg    }
2099659607e0Smrg}
2100659607e0Smrg
2101659607e0Smrgstatic void MouseWakeupHandler(pointer data,
21021450c749Smrg                               int i,
21031450c749Smrg                               pointer LastSelectMask)
2104659607e0Smrg{
2105659607e0Smrg    InputInfoPtr    pInfo = (InputInfoPtr) data;
21061450c749Smrg    MouseDevPtr     pMse = (MouseDevPtr) pInfo->private;
21071450c749Smrg    int             ms;
21081450c749Smrg
2109659607e0Smrg    if (pMse->emulate3Pending)
2110659607e0Smrg    {
21111450c749Smrg        ms = pMse->emulate3Expires - GetTimeInMillis ();
21121450c749Smrg        if (ms <= 0)
21131450c749Smrg            buttonTimer (pInfo);
2114659607e0Smrg    }
2115659607e0Smrg}
2116659607e0Smrg
2117659607e0Smrg/*******************************************************************
2118659607e0Smrg *
2119659607e0Smrg * Post mouse events
2120659607e0Smrg *
2121659607e0Smrg *******************************************************************/
2122659607e0Smrg
2123659607e0Smrgstatic void
2124659607e0SmrgMouseDoPostEvent(InputInfoPtr pInfo, int buttons, int dx, int dy)
2125659607e0Smrg{
2126659607e0Smrg    MouseDevPtr pMse;
2127659607e0Smrg    int emulateButtons;
2128659607e0Smrg    int id, change;
2129659607e0Smrg    int emuWheelDelta, emuWheelButton, emuWheelButtonMask;
2130659607e0Smrg    int wheelButtonMask;
2131659607e0Smrg    int ms;
2132659607e0Smrg
2133659607e0Smrg    pMse = pInfo->private;
2134659607e0Smrg
2135659607e0Smrg    change = buttons ^ pMse->lastMappedButtons;
2136659607e0Smrg    pMse->lastMappedButtons = buttons;
2137659607e0Smrg
2138659607e0Smrg    /* Do single button double click */
2139659607e0Smrg    if (pMse->doubleClickSourceButtonMask) {
2140659607e0Smrg        if (buttons & pMse->doubleClickSourceButtonMask) {
2141659607e0Smrg            if (!(pMse->doubleClickOldSourceState)) {
2142659607e0Smrg                /* double-click button has just been pressed. Ignore it if target button
2143659607e0Smrg                 * is already down.
2144659607e0Smrg                 */
2145659607e0Smrg                if (!(buttons & pMse->doubleClickTargetButtonMask)) {
2146659607e0Smrg                    /* Target button isn't down, so send a double-click */
2147659607e0Smrg                    xf86PostButtonEvent(pInfo->dev, 0, pMse->doubleClickTargetButton, 1, 0, 0);
2148659607e0Smrg                    xf86PostButtonEvent(pInfo->dev, 0, pMse->doubleClickTargetButton, 0, 0, 0);
2149659607e0Smrg                    xf86PostButtonEvent(pInfo->dev, 0, pMse->doubleClickTargetButton, 1, 0, 0);
2150659607e0Smrg                    xf86PostButtonEvent(pInfo->dev, 0, pMse->doubleClickTargetButton, 0, 0, 0);
2151659607e0Smrg                }
2152659607e0Smrg            }
2153659607e0Smrg            pMse->doubleClickOldSourceState = 1;
2154659607e0Smrg        }
2155659607e0Smrg        else
2156659607e0Smrg            pMse->doubleClickOldSourceState = 0;
2157659607e0Smrg
2158659607e0Smrg        /* Whatever happened, mask the double-click button so it doesn't get
2159659607e0Smrg         * processed as a normal button as well.
2160659607e0Smrg         */
2161659607e0Smrg        buttons &= ~(pMse->doubleClickSourceButtonMask);
2162659607e0Smrg        change  &= ~(pMse->doubleClickSourceButtonMask);
2163659607e0Smrg    }
2164659607e0Smrg
2165659607e0Smrg    if (pMse->emulateWheel) {
21661450c749Smrg        /* Emulate wheel button handling */
21671450c749Smrg        if(pMse->wheelButton == 0)
21681450c749Smrg            wheelButtonMask = 0;
21691450c749Smrg        else
21701450c749Smrg            wheelButtonMask = 1 << (pMse->wheelButton - 1);
2171659607e0Smrg
21721450c749Smrg        if (change & wheelButtonMask) {
21731450c749Smrg            if (buttons & wheelButtonMask) {
21741450c749Smrg                /* Start timeout handling */
21751450c749Smrg                pMse->wheelButtonExpires = GetTimeInMillis () + pMse->wheelButtonTimeout;
21761450c749Smrg                ms = - pMse->wheelButtonTimeout;
21771450c749Smrg            } else {
21781450c749Smrg                ms = pMse->wheelButtonExpires - GetTimeInMillis ();
21791450c749Smrg
21801450c749Smrg                if (0 < ms) {
21811450c749Smrg                    /*
21821450c749Smrg                     * If the button is released early enough emit the button
21831450c749Smrg                     * press/release events
21841450c749Smrg                     */
21851450c749Smrg                    xf86PostButtonEvent(pInfo->dev, 0, pMse->wheelButton, 1, 0, 0);
21861450c749Smrg                    xf86PostButtonEvent(pInfo->dev, 0, pMse->wheelButton, 0, 0, 0);
21871450c749Smrg                }
21881450c749Smrg            }
21891450c749Smrg        } else
21901450c749Smrg            ms = pMse->wheelButtonExpires - GetTimeInMillis ();
21911450c749Smrg
21921450c749Smrg        /* Intercept wheel emulation if the necessary button is depressed or
2193fecac299Smrg           if no button is necessary */
21941450c749Smrg        if ((buttons & wheelButtonMask) || wheelButtonMask==0) {
21951450c749Smrg            if (ms <= 0 || wheelButtonMask==0) {
21961450c749Smrg                /* Y axis movement */
21971450c749Smrg                if (pMse->negativeY != MSE_NOAXISMAP) {
21981450c749Smrg                    pMse->wheelYDistance += dy;
21991450c749Smrg                    if (pMse->wheelYDistance < 0) {
22001450c749Smrg                        emuWheelDelta = -pMse->wheelInertia;
22011450c749Smrg                        emuWheelButton = pMse->negativeY;
22021450c749Smrg                    } else {
22031450c749Smrg                        emuWheelDelta = pMse->wheelInertia;
22041450c749Smrg                        emuWheelButton = pMse->positiveY;
22051450c749Smrg                    }
22061450c749Smrg                    emuWheelButtonMask = 1 << (emuWheelButton - 1);
22071450c749Smrg                    while (abs(pMse->wheelYDistance) > pMse->wheelInertia) {
22081450c749Smrg                        pMse->wheelYDistance -= emuWheelDelta;
22091450c749Smrg
22101450c749Smrg                        pMse->wheelXDistance = 0;
22111450c749Smrg                        /*
22121450c749Smrg                         * Synthesize the press and release, but not when
22131450c749Smrg                         * the button to be synthesized is already pressed
22141450c749Smrg                         * "for real".
22151450c749Smrg                         */
22161450c749Smrg                        if (!(emuWheelButtonMask & buttons) ||
22171450c749Smrg                            (emuWheelButtonMask & wheelButtonMask)) {
22181450c749Smrg                            xf86PostButtonEvent(pInfo->dev, 0, emuWheelButton, 1, 0, 0);
22191450c749Smrg                            xf86PostButtonEvent(pInfo->dev, 0, emuWheelButton, 0, 0, 0);
22201450c749Smrg                        }
22211450c749Smrg                    }
22221450c749Smrg                }
2223659607e0Smrg
22241450c749Smrg                /* X axis movement */
22251450c749Smrg                if (pMse->negativeX != MSE_NOAXISMAP) {
22261450c749Smrg                    pMse->wheelXDistance += dx;
22271450c749Smrg                    if (pMse->wheelXDistance < 0) {
22281450c749Smrg                        emuWheelDelta = -pMse->wheelInertia;
22291450c749Smrg                        emuWheelButton = pMse->negativeX;
22301450c749Smrg                    } else {
22311450c749Smrg                        emuWheelDelta = pMse->wheelInertia;
22321450c749Smrg                        emuWheelButton = pMse->positiveX;
22331450c749Smrg                    }
22341450c749Smrg                    emuWheelButtonMask = 1 << (emuWheelButton - 1);
22351450c749Smrg                    while (abs(pMse->wheelXDistance) > pMse->wheelInertia) {
22361450c749Smrg                        pMse->wheelXDistance -= emuWheelDelta;
22371450c749Smrg
22381450c749Smrg                        pMse->wheelYDistance = 0;
22391450c749Smrg                        /*
22401450c749Smrg                         * Synthesize the press and release, but not when
22411450c749Smrg                         * the button to be synthesized is already pressed
22421450c749Smrg                         * "for real".
22431450c749Smrg                         */
22441450c749Smrg                        if (!(emuWheelButtonMask & buttons) ||
22451450c749Smrg                            (emuWheelButtonMask & wheelButtonMask)) {
22461450c749Smrg                            xf86PostButtonEvent(pInfo->dev, 0, emuWheelButton, 1, 0, 0);
22471450c749Smrg                            xf86PostButtonEvent(pInfo->dev, 0, emuWheelButton, 0, 0, 0);
22481450c749Smrg                        }
22491450c749Smrg                    }
22501450c749Smrg                }
22511450c749Smrg            }
2252659607e0Smrg
22531450c749Smrg            /* Absorb the mouse movement while the wheel button is pressed. */
22541450c749Smrg            dx = 0;
22551450c749Smrg            dy = 0;
22561450c749Smrg        }
22571450c749Smrg        /*
22581450c749Smrg         * Button events for the wheel button are only emitted through
22591450c749Smrg         * the timeout code.
22601450c749Smrg         */
22611450c749Smrg        buttons &= ~wheelButtonMask;
22621450c749Smrg        change  &= ~wheelButtonMask;
2263659607e0Smrg    }
2264659607e0Smrg
2265659607e0Smrg    if (pMse->emulate3ButtonsSoft && pMse->emulate3Pending && (dx || dy))
22661450c749Smrg        buttonTimer(pInfo);
2267659607e0Smrg
2268659607e0Smrg    if (dx || dy)
22691450c749Smrg        xf86PostMotionEvent(pInfo->dev, 0, 0, 2, dx, dy);
2270659607e0Smrg
2271659607e0Smrg    if (change) {
2272659607e0Smrg
22731450c749Smrg        /*
22741450c749Smrg         * adjust buttons state for drag locks!
22751450c749Smrg         * if there is drag locks
22761450c749Smrg         */
22771450c749Smrg        if (pMse->pDragLock) {
22781450c749Smrg            DragLockPtr   pLock;
22791450c749Smrg            int tarOfGoingDown, tarOfDown;
22801450c749Smrg            int realbuttons;
22811450c749Smrg
22821450c749Smrg            /* get drag lock block */
22831450c749Smrg            pLock = pMse->pDragLock;
22841450c749Smrg            /* save real buttons */
22851450c749Smrg            realbuttons = buttons;
22861450c749Smrg
22871450c749Smrg            /* if drag lock used */
22881450c749Smrg
22891450c749Smrg            /* state of drag lock buttons not seen always up */
22901450c749Smrg
22911450c749Smrg            buttons &= ~pLock->lockButtonsM;
22921450c749Smrg
22931450c749Smrg            /*
22941450c749Smrg             * if lock buttons being depressed changes state of
22951450c749Smrg             * targets simulatedDown.
22961450c749Smrg             */
22971450c749Smrg            tarOfGoingDown = lock2targetMap(pLock,
22981450c749Smrg                                realbuttons & change & pLock->lockButtonsM);
22991450c749Smrg            pLock->simulatedDown ^= tarOfGoingDown;
23001450c749Smrg
23011450c749Smrg            /* targets of drag locks down */
23021450c749Smrg            tarOfDown = lock2targetMap(pLock,
23031450c749Smrg                                realbuttons & pLock->lockButtonsM);
23041450c749Smrg
23051450c749Smrg            /*
23061450c749Smrg             * when simulatedDown set and target pressed,
23071450c749Smrg             * simulatedDown goes false
23081450c749Smrg             */
23091450c749Smrg            pLock->simulatedDown &= ~(realbuttons & change);
23101450c749Smrg
23111450c749Smrg            /*
23121450c749Smrg             * if master drag lock released
23131450c749Smrg             * then master drag lock state on
23141450c749Smrg             */
23151450c749Smrg            pLock->masterTS |= (~realbuttons & change) & pLock->masterLockM;
23161450c749Smrg
23171450c749Smrg            /* if master state, buttons going down are simulatedDown */
23181450c749Smrg            if (pLock->masterTS)
23191450c749Smrg                pLock->simulatedDown |= (realbuttons & change);
23201450c749Smrg
23211450c749Smrg            /* if any button pressed, no longer in master drag lock state */
23221450c749Smrg            if (realbuttons & change)
23231450c749Smrg                pLock->masterTS = 0;
23241450c749Smrg
23251450c749Smrg            /* if simulatedDown or drag lock down, simulate down */
23261450c749Smrg            buttons |= (pLock->simulatedDown | tarOfDown);
23271450c749Smrg
23281450c749Smrg            /* master button not seen */
23291450c749Smrg            buttons &= ~(pLock->masterLockM);
23301450c749Smrg
23311450c749Smrg            /* buttons changed since last time */
23321450c749Smrg            change = buttons ^ pLock->lockLastButtons;
23331450c749Smrg
23341450c749Smrg            /* save this time for next last time. */
23351450c749Smrg            pLock->lockLastButtons = buttons;
23361450c749Smrg        }
2337659607e0Smrg
2338659607e0Smrg        if (pMse->emulate3Buttons
23391450c749Smrg            && (!(buttons & 0x02) || Emulate3ButtonsSoft(pInfo))) {
2340659607e0Smrg
2341659607e0Smrg            /* handle all but buttons 1 & 3 normally */
2342659607e0Smrg
2343659607e0Smrg            change &= ~05;
2344659607e0Smrg
2345659607e0Smrg            /* emulate the third button by the other two */
2346659607e0Smrg
2347659607e0Smrg            emulateButtons = (buttons & 01) | ((buttons &04) >> 1);
2348659607e0Smrg
2349659607e0Smrg            if ((id = stateTab[pMse->emulateState][emulateButtons][0]) != 0)
2350659607e0Smrg                xf86PostButtonEvent(pInfo->dev, 0, abs(id), (id >= 0), 0, 0);
2351659607e0Smrg            if ((id = stateTab[pMse->emulateState][emulateButtons][1]) != 0)
2352659607e0Smrg                xf86PostButtonEvent(pInfo->dev, 0, abs(id), (id >= 0), 0, 0);
2353659607e0Smrg
2354659607e0Smrg            pMse->emulateState =
2355659607e0Smrg                stateTab[pMse->emulateState][emulateButtons][2];
2356659607e0Smrg
2357659607e0Smrg            if (stateTab[pMse->emulateState][4][0] != 0) {
23581450c749Smrg                pMse->emulate3Expires = GetTimeInMillis () + pMse->emulate3Timeout;
23591450c749Smrg                pMse->emulate3Pending = TRUE;
2360659607e0Smrg            } else {
23611450c749Smrg                pMse->emulate3Pending = FALSE;
2362659607e0Smrg            }
2363659607e0Smrg        }
2364659607e0Smrg
23651450c749Smrg        while (change) {
23661450c749Smrg            id = ffs(change);
23671450c749Smrg            change &= ~(1 << (id - 1));
23681450c749Smrg            xf86PostButtonEvent(pInfo->dev, 0, id,
23691450c749Smrg                                (buttons & (1 << (id - 1))), 0, 0);
23701450c749Smrg        }
2371659607e0Smrg
2372659607e0Smrg    }
2373659607e0Smrg}
2374659607e0Smrg
2375659607e0Smrgstatic void
2376659607e0SmrgMousePostEvent(InputInfoPtr pInfo, int truebuttons,
23771450c749Smrg               int dx, int dy, int dz, int dw)
2378659607e0Smrg{
2379659607e0Smrg    MouseDevPtr pMse;
2380659607e0Smrg    mousePrivPtr mousepriv;
2381659607e0Smrg    int zbutton = 0, wbutton = 0, zbuttoncount = 0, wbuttoncount = 0;
2382659607e0Smrg    int i, b, buttons = 0;
2383659607e0Smrg
2384659607e0Smrg    pMse = pInfo->private;
2385659607e0Smrg    mousepriv = (mousePrivPtr)pMse->mousePriv;
23861450c749Smrg
2387659607e0Smrg    if (pMse->protocolID == PROT_MMHIT)
23881450c749Smrg        b = reverseBits(hitachMap, truebuttons);
2389659607e0Smrg    else
23901450c749Smrg        b = reverseBits(reverseMap, truebuttons);
2391659607e0Smrg
2392659607e0Smrg    /* Remap mouse buttons */
2393659607e0Smrg    b &= (1<<MSE_MAXBUTTONS)-1;
2394659607e0Smrg    for (i = 0; b; i++) {
2395659607e0Smrg       if (b & 1)
23961450c749Smrg           buttons |= pMse->buttonMap[i];
2397659607e0Smrg       b >>= 1;
2398659607e0Smrg    }
2399659607e0Smrg
2400659607e0Smrg    /* Map the Z axis movement. */
2401659607e0Smrg    /* XXX Could this go in the conversion_proc? */
2402659607e0Smrg    switch (pMse->negativeZ) {
24031450c749Smrg    case MSE_NOZMAP:    /* do nothing */
24041450c749Smrg        dz = 0;
24051450c749Smrg        break;
2406659607e0Smrg    case MSE_MAPTOX:
24071450c749Smrg        if (dz != 0) {
24081450c749Smrg            dx = dz;
24091450c749Smrg            dz = 0;
24101450c749Smrg        }
24111450c749Smrg        break;
2412659607e0Smrg    case MSE_MAPTOY:
24131450c749Smrg        if (dz != 0) {
24141450c749Smrg            dy = dz;
24151450c749Smrg            dz = 0;
24161450c749Smrg        }
24171450c749Smrg        break;
24181450c749Smrg    default:    /* buttons */
24191450c749Smrg        buttons &= ~(pMse->negativeZ | pMse->positiveZ);
24201450c749Smrg        if (dz < 0) {
24211450c749Smrg            zbutton = pMse->negativeZ;
24221450c749Smrg            zbuttoncount = -dz;
24231450c749Smrg        } else if (dz > 0) {
24241450c749Smrg            zbutton = pMse->positiveZ;
24251450c749Smrg            zbuttoncount = dz;
24261450c749Smrg        }
24271450c749Smrg        dz = 0;
24281450c749Smrg        break;
2429659607e0Smrg    }
2430659607e0Smrg    switch (pMse->negativeW) {
24311450c749Smrg    case MSE_NOZMAP:    /* do nothing */
24321450c749Smrg        dw = 0;
24331450c749Smrg        break;
2434659607e0Smrg    case MSE_MAPTOX:
24351450c749Smrg        if (dw != 0) {
24361450c749Smrg            dx = dw;
24371450c749Smrg            dw = 0;
24381450c749Smrg        }
24391450c749Smrg        break;
2440659607e0Smrg    case MSE_MAPTOY:
24411450c749Smrg        if (dw != 0) {
24421450c749Smrg            dy = dw;
24431450c749Smrg            dw = 0;
24441450c749Smrg        }
24451450c749Smrg        break;
24461450c749Smrg    default:    /* buttons */
24471450c749Smrg        buttons &= ~(pMse->negativeW | pMse->positiveW);
24481450c749Smrg        if (dw < 0) {
24491450c749Smrg            wbutton = pMse->negativeW;
24501450c749Smrg            wbuttoncount = -dw;
24511450c749Smrg        } else if (dw > 0) {
24521450c749Smrg            wbutton = pMse->positiveW;
24531450c749Smrg            wbuttoncount = dw;
24541450c749Smrg        }
24551450c749Smrg        dw = 0;
24561450c749Smrg        break;
2457659607e0Smrg    }
2458659607e0Smrg
2459659607e0Smrg
2460659607e0Smrg    /* Apply angle offset */
2461659607e0Smrg    if (pMse->angleOffset != 0) {
24621450c749Smrg        double rad = 3.141592653 * pMse->angleOffset / 180.0;
24631450c749Smrg        int ndx = dx;
24641450c749Smrg        dx = (int)((dx * cos(rad)) + (dy * sin(rad)) + 0.5);
24651450c749Smrg        dy = (int)((dy * cos(rad)) - (ndx * sin(rad)) + 0.5);
2466659607e0Smrg    }
2467659607e0Smrg
2468659607e0Smrg    dx = pMse->invX * dx;
2469659607e0Smrg    dy = pMse->invY * dy;
2470659607e0Smrg    if (pMse->flipXY) {
24711450c749Smrg        int tmp = dx;
24721450c749Smrg        dx = dy;
24731450c749Smrg        dy = tmp;
2474659607e0Smrg    }
2475659607e0Smrg
24761450c749Smrg    /* Accumulate the scaled dx, dy in the private variables
2477659607e0Smrg       fracdx,fracdy and return the integer number part */
2478659607e0Smrg    if (mousepriv) {
24791450c749Smrg        mousepriv->fracdx += mousepriv->sensitivity*dx;
24801450c749Smrg        mousepriv->fracdy += mousepriv->sensitivity*dy;
24811450c749Smrg        mousepriv->fracdx -= ( dx=(int)(mousepriv->fracdx) );
24821450c749Smrg        mousepriv->fracdy -= ( dy=(int)(mousepriv->fracdy) );
2483659607e0Smrg    }
24841450c749Smrg
2485659607e0Smrg    /* If mouse wheel movement has to be mapped on a button, we need to
2486659607e0Smrg     * loop for button press and release events. */
2487659607e0Smrg    do {
2488659607e0Smrg        MouseDoPostEvent(pInfo, buttons | zbutton | wbutton, dx, dy);
24891450c749Smrg        dx = dy = 0;
24901450c749Smrg        if (zbutton || wbutton)
24911450c749Smrg            MouseDoPostEvent(pInfo, buttons, 0, 0);
24921450c749Smrg        if (--zbuttoncount <= 0)
24931450c749Smrg            zbutton = 0;
24941450c749Smrg        if (--wbuttoncount <= 0)
24951450c749Smrg            wbutton = 0;
2496659607e0Smrg    } while (zbutton || wbutton);
2497659607e0Smrg
2498659607e0Smrg    pMse->lastButtons = truebuttons;
2499659607e0Smrg}
2500659607e0Smrg/******************************************************************
2501659607e0Smrg *
2502659607e0Smrg * Mouse Setup Code
2503659607e0Smrg *
2504659607e0Smrg ******************************************************************/
2505659607e0Smrg/*
2506659607e0Smrg * This array is indexed by the MouseProtocolID values, so the order of the
2507dd4cbfe8Smrg * entries must match that of the MouseProtocolID enum in mouse.h.
2508659607e0Smrg */
2509659607e0Smrgstatic unsigned char proto[PROT_NUMPROTOS][8] = {
2510659607e0Smrg  /* --header--  ---data--- packet -4th-byte-  mouse   */
2511659607e0Smrg  /* mask   id   mask   id  bytes  mask   id   flags   */
25121450c749Smrg                                                            /* Serial mice */
2513659607e0Smrg  {  0x40, 0x40, 0x40, 0x00,  3,  ~0x23, 0x00, MPF_NONE },  /* MicroSoft */
2514659607e0Smrg  {  0xf8, 0x80, 0x00, 0x00,  5,   0x00, 0xff, MPF_SAFE },  /* MouseSystems */
2515659607e0Smrg  {  0xe0, 0x80, 0x80, 0x00,  3,   0x00, 0xff, MPF_NONE },  /* MMSeries */
2516659607e0Smrg  {  0xe0, 0x80, 0x80, 0x00,  3,   0x00, 0xff, MPF_NONE },  /* Logitech */
2517659607e0Smrg  {  0x40, 0x40, 0x40, 0x00,  3,  ~0x23, 0x00, MPF_NONE },  /* MouseMan */
2518659607e0Smrg  {  0xe0, 0x80, 0x80, 0x00,  3,   0x00, 0xff, MPF_NONE },  /* MM_HitTablet */
2519659607e0Smrg  {  0x40, 0x40, 0x40, 0x00,  3,  ~0x33, 0x00, MPF_NONE },  /* GlidePoint */
2520659607e0Smrg  {  0x40, 0x40, 0x40, 0x00,  3,  ~0x3f, 0x00, MPF_NONE },  /* IntelliMouse */
2521659607e0Smrg  {  0x40, 0x40, 0x40, 0x00,  3,  ~0x33, 0x00, MPF_NONE },  /* ThinkingMouse */
2522659607e0Smrg  {  0x80, 0x80, 0x80, 0x00,  3,   0x00, 0xff, MPF_NONE },  /* ACECAD */
2523659607e0Smrg  {  0x40, 0x40, 0x40, 0x00,  4,   0x00, 0xff, MPF_NONE },  /* ValuMouseScroll */
25241450c749Smrg                                                            /* PS/2 variants */
2525659607e0Smrg  {  0xc0, 0x00, 0x00, 0x00,  3,   0x00, 0xff, MPF_NONE },  /* PS/2 mouse */
2526659607e0Smrg  {  0xc8, 0x08, 0x00, 0x00,  3,   0x00, 0x00, MPF_NONE },  /* genericPS/2 mouse*/
2527659607e0Smrg  {  0x08, 0x08, 0x00, 0x00,  4,   0x00, 0xff, MPF_NONE },  /* IntelliMouse */
2528659607e0Smrg  {  0x08, 0x08, 0x00, 0x00,  4,   0x00, 0xff, MPF_NONE },  /* Explorer */
2529659607e0Smrg  {  0x80, 0x80, 0x00, 0x00,  3,   0x00, 0xff, MPF_NONE },  /* ThinkingMouse */
2530659607e0Smrg  {  0x08, 0x08, 0x00, 0x00,  3,   0x00, 0xff, MPF_NONE },  /* MouseMan+ */
2531659607e0Smrg  {  0xc0, 0x00, 0x00, 0x00,  3,   0x00, 0xff, MPF_NONE },  /* GlidePoint */
2532659607e0Smrg  {  0x08, 0x08, 0x00, 0x00,  4,   0x00, 0xff, MPF_NONE },  /* NetMouse */
2533659607e0Smrg  {  0xc0, 0x00, 0x00, 0x00,  6,   0x00, 0xff, MPF_NONE },  /* NetScroll */
25341450c749Smrg                                                            /* Bus Mouse */
2535659607e0Smrg  {  0xf8, 0x80, 0x00, 0x00,  5,   0x00, 0xff, MPF_NONE },  /* BusMouse */
2536659607e0Smrg  {  0xf8, 0x80, 0x00, 0x00,  5,   0x00, 0xff, MPF_NONE },  /* Auto (dummy) */
2537659607e0Smrg  {  0xf8, 0x80, 0x00, 0x00,  8,   0x00, 0xff, MPF_NONE },  /* SysMouse */
2538659607e0Smrg};
2539659607e0Smrg
2540659607e0Smrg
2541659607e0Smrg/*
2542659607e0Smrg * SetupMouse --
25431450c749Smrg *      Sets up the mouse parameters
2544659607e0Smrg */
2545659607e0Smrgstatic Bool
2546659607e0SmrgSetupMouse(InputInfoPtr pInfo)
2547659607e0Smrg{
2548659607e0Smrg    MouseDevPtr pMse;
2549659607e0Smrg    int i;
2550659607e0Smrg    int protoPara[8] = {-1, -1, -1, -1, -1, -1, -1, -1};
2551659607e0Smrg    const char *name = NULL;
2552659607e0Smrg    Bool automatic = FALSE;
2553659607e0Smrg
2554659607e0Smrg    pMse = pInfo->private;
25551450c749Smrg
2556659607e0Smrg    /* Handle the "Auto" protocol. */
2557659607e0Smrg    if (pMse->protocolID == PROT_AUTO) {
25581450c749Smrg        /*
25591450c749Smrg         * We come here when user specifies protocol "auto" in
25601450c749Smrg         * the configuration file or thru the xf86misc extensions.
25611450c749Smrg         * So we initialize autoprobing here.
25621450c749Smrg         * Probe for PnP/OS mouse first. If unsuccessful
25631450c749Smrg         * try to guess protocol from incoming data.
25641450c749Smrg         */
25651450c749Smrg        automatic = TRUE;
25661450c749Smrg        pMse->autoProbe = TRUE;
25671450c749Smrg        name = autoOSProtocol(pInfo,protoPara);
25681450c749Smrg        if (name)  {
2569659607e0Smrg#ifdef EXTMOUSEDEBUG
25701450c749Smrg            ErrorF("PnP/OS Mouse detected: %s\n",name);
25711450c749Smrg#endif
25721450c749Smrg        }
2573659607e0Smrg    }
2574659607e0Smrg
2575659607e0Smrg    SetMouseProto(pMse, pMse->protocolID);
2576659607e0Smrg
2577659607e0Smrg    if (automatic) {
25781450c749Smrg        if (name) {
25791450c749Smrg            /* Possible protoPara overrides from SetupAuto. */
25801450c749Smrg            for (i = 0; i < sizeof(pMse->protoPara); i++)
25811450c749Smrg                if (protoPara[i] != -1)
25821450c749Smrg                    pMse->protoPara[i] = protoPara[i];
25831450c749Smrg            /* if we come here PnP/OS mouse probing was successful */
25841450c749Smrg        } else {
25851450c749Smrg            /* PnP/OS mouse probing wasn't successful; we look at data */
25861450c749Smrg        }
2587659607e0Smrg    }
2588659607e0Smrg
2589659607e0Smrg    /*
2590659607e0Smrg     * If protocol has changed fetch the default options
2591659607e0Smrg     * for the new protocol.
2592659607e0Smrg     */
2593659607e0Smrg    if (pMse->oldProtocolID != pMse->protocolID) {
25941450c749Smrg        if ((pMse->protocolID >= 0)
25951450c749Smrg            && (pMse->protocolID < PROT_NUMPROTOS)
25961450c749Smrg            && mouseProtocols[pMse->protocolID].defaults) {
25971450c749Smrg            pointer tmp = xf86OptionListCreate(
25981450c749Smrg                mouseProtocols[pMse->protocolID].defaults, -1, 0);
25991450c749Smrg            pInfo->options = xf86OptionListMerge(pInfo->options, tmp);
26001450c749Smrg        }
26011450c749Smrg        /*
26021450c749Smrg         * If baudrate is set write it back to the option
26031450c749Smrg         * list so that the serial interface code can access
26041450c749Smrg         * the new value. Not set means default.
26051450c749Smrg         */
26061450c749Smrg        if (pMse->baudRate)
26071450c749Smrg            xf86ReplaceIntOption(pInfo->options, "BaudRate", pMse->baudRate);
26081450c749Smrg        pMse->oldProtocolID = pMse->protocolID; /* hack */
2609659607e0Smrg    }
2610659607e0Smrg
2611659607e0Smrg
2612659607e0Smrg    /* Set the port parameters. */
2613659607e0Smrg    if (!automatic)
26141450c749Smrg        xf86SetSerial(pInfo->fd, pInfo->options);
2615659607e0Smrg
2616659607e0Smrg    if (!initMouseHW(pInfo))
26171450c749Smrg        return FALSE;
2618659607e0Smrg
2619659607e0Smrg    pMse->protoBufTail = 0;
2620659607e0Smrg    pMse->inSync = 0;
2621659607e0Smrg
2622659607e0Smrg    return TRUE;
2623659607e0Smrg}
2624659607e0Smrg
2625659607e0Smrg/********************************************************************
2626659607e0Smrg *
2627659607e0Smrg * Mouse HW setup code
2628659607e0Smrg *
2629659607e0Smrg ********************************************************************/
2630659607e0Smrg
2631659607e0Smrg/*
2632659607e0Smrg** The following lines take care of the Logitech MouseMan protocols.
2633659607e0Smrg** The "Logitech" protocol is for the old "series 9" Logitech products.
2634659607e0Smrg** All products since then use the "MouseMan" protocol.  Some models
2635659607e0Smrg** were programmable, but most (all?) of the current models are not.
2636659607e0Smrg**
2637659607e0Smrg** NOTE: There are different versions of both MouseMan and TrackMan!
2638659607e0Smrg**       Hence I add another protocol PROT_LOGIMAN, which the user can
2639659607e0Smrg**       specify as MouseMan in his XF86Config file. This entry was
2640659607e0Smrg**       formerly handled as a special case of PROT_MS. However, people
2641659607e0Smrg**       who don't have the middle button problem, can still specify
2642659607e0Smrg**       Microsoft and use PROT_MS.
2643659607e0Smrg**
2644659607e0Smrg** By default, these mice should use a 3 byte Microsoft protocol
2645659607e0Smrg** plus a 4th byte for the middle button. However, the mouse might
2646659607e0Smrg** have switched to a different protocol before we use it, so I send
2647659607e0Smrg** the proper sequence just in case.
2648659607e0Smrg**
2649659607e0Smrg** NOTE: - all commands to (at least the European) MouseMan have to
2650659607e0Smrg**         be sent at 1200 Baud.
2651659607e0Smrg**       - each command starts with a '*'.
2652659607e0Smrg**       - whenever the MouseMan receives a '*', it will switch back
26531450c749Smrg**       to 1200 Baud. Hence I have to select the desired protocol
26541450c749Smrg**       first, then select the baud rate.
2655659607e0Smrg**
2656659607e0Smrg** The protocols supported by the (European) MouseMan are:
2657659607e0Smrg**   -  5 byte packed binary protocol, as with the Mouse Systems
2658659607e0Smrg**      mouse. Selected by sequence "*U".
2659659607e0Smrg**   -  2 button 3 byte MicroSoft compatible protocol. Selected
2660659607e0Smrg**      by sequence "*V".
2661659607e0Smrg**   -  3 button 3+1 byte MicroSoft compatible protocol (default).
2662659607e0Smrg**      Selected by sequence "*X".
2663659607e0Smrg**
2664659607e0Smrg** The following baud rates are supported:
2665659607e0Smrg**   -  1200 Baud (default). Selected by sequence "*n".
2666659607e0Smrg**   -  9600 Baud. Selected by sequence "*q".
2667659607e0Smrg**
2668659607e0Smrg** Selecting a sample rate is no longer supported with the MouseMan!
2669659607e0Smrg**               [CHRIS-211092]
2670659607e0Smrg*/
2671659607e0Smrg
2672659607e0Smrg/*
2673659607e0Smrg * Do a reset wrap mode before reset.
2674659607e0Smrg */
2675659607e0Smrg#define do_ps2Reset(x)  { \
2676659607e0Smrg    int i = RETRY_COUNT;\
2677659607e0Smrg     while (i-- > 0) { \
2678659607e0Smrg       xf86FlushInput(x->fd); \
2679659607e0Smrg       if (ps2Reset(x)) break; \
2680659607e0Smrg    } \
2681659607e0Smrg  }
2682659607e0Smrg
26831450c749Smrg
2684659607e0Smrgstatic Bool
2685659607e0SmrginitMouseHW(InputInfoPtr pInfo)
2686659607e0Smrg{
2687659607e0Smrg    MouseDevPtr pMse = pInfo->private;
2688659607e0Smrg    const char *s;
2689659607e0Smrg    unsigned char c;
2690659607e0Smrg    int speed;
2691659607e0Smrg    pointer options;
2692659607e0Smrg    unsigned char *param = NULL;
2693659607e0Smrg    int paramlen = 0;
2694659607e0Smrg    int count = RETRY_COUNT;
2695659607e0Smrg    Bool ps2Init = TRUE;
26961450c749Smrg
2697659607e0Smrg    switch (pMse->protocolID) {
26981450c749Smrg        case PROT_LOGI:         /* Logitech Mice */
26991450c749Smrg            /*
27001450c749Smrg             * The baud rate selection command must be sent at the current
27011450c749Smrg             * baud rate; try all likely settings.
27021450c749Smrg             */
27031450c749Smrg            speed = pMse->baudRate;
27041450c749Smrg            switch (speed) {
27051450c749Smrg                case 9600:
27061450c749Smrg                    s = "*q";
27071450c749Smrg                    break;
27081450c749Smrg                case 4800:
27091450c749Smrg                    s = "*p";
27101450c749Smrg                    break;
27111450c749Smrg                case 2400:
27121450c749Smrg                    s = "*o";
27131450c749Smrg                    break;
27141450c749Smrg                case 1200:
27151450c749Smrg                    s = "*n";
27161450c749Smrg                    break;
27171450c749Smrg                default:
27181450c749Smrg                    /* Fallback value */
27191450c749Smrg                    speed = 1200;
27201450c749Smrg                    s = "*n";
27211450c749Smrg            }
27221450c749Smrg            xf86SetSerialSpeed(pInfo->fd, 9600);
27231450c749Smrg            xf86WriteSerial(pInfo->fd, s, 2);
27241450c749Smrg            usleep(100000);
27251450c749Smrg            xf86SetSerialSpeed(pInfo->fd, 4800);
27261450c749Smrg            xf86WriteSerial(pInfo->fd, s, 2);
27271450c749Smrg            usleep(100000);
27281450c749Smrg            xf86SetSerialSpeed(pInfo->fd, 2400);
27291450c749Smrg            xf86WriteSerial(pInfo->fd, s, 2);
27301450c749Smrg            usleep(100000);
27311450c749Smrg            xf86SetSerialSpeed(pInfo->fd, 1200);
27321450c749Smrg            xf86WriteSerial(pInfo->fd, s, 2);
27331450c749Smrg            usleep(100000);
27341450c749Smrg            xf86SetSerialSpeed(pInfo->fd, speed);
27351450c749Smrg
27361450c749Smrg            /* Select MM series data format. */
27371450c749Smrg            xf86WriteSerial(pInfo->fd, "S", 1);
27381450c749Smrg            usleep(100000);
27391450c749Smrg            /* Set the parameters up for the MM series protocol. */
27401450c749Smrg            options = pInfo->options;
27411450c749Smrg            COLLECT_INPUT_OPTIONS(pInfo, mmDefaults);
27421450c749Smrg            xf86SetSerial(pInfo->fd, pInfo->options);
27431450c749Smrg            pInfo->options = options;
27441450c749Smrg
27451450c749Smrg            /* Select report rate/frequency. */
27461450c749Smrg            if      (pMse->sampleRate <=   0)  c = 'O';  /* 100 */
27471450c749Smrg            else if (pMse->sampleRate <=  15)  c = 'J';  /*  10 */
27481450c749Smrg            else if (pMse->sampleRate <=  27)  c = 'K';  /*  20 */
27491450c749Smrg            else if (pMse->sampleRate <=  42)  c = 'L';  /*  35 */
27501450c749Smrg            else if (pMse->sampleRate <=  60)  c = 'R';  /*  50 */
27511450c749Smrg            else if (pMse->sampleRate <=  85)  c = 'M';  /*  67 */
27521450c749Smrg            else if (pMse->sampleRate <= 125)  c = 'Q';  /* 100 */
27531450c749Smrg            else                               c = 'N';  /* 150 */
27541450c749Smrg            xf86WriteSerial(pInfo->fd, &c, 1);
27551450c749Smrg            break;
2756659607e0Smrg
27571450c749Smrg        case PROT_LOGIMAN:
27581450c749Smrg            speed = pMse->baudRate;
27591450c749Smrg            switch (speed) {
27601450c749Smrg                case 9600:
27611450c749Smrg                    s = "*q";
27621450c749Smrg                    break;
27631450c749Smrg                case 1200:
27641450c749Smrg                    s = "*n";
27651450c749Smrg                    break;
27661450c749Smrg                default:
27671450c749Smrg                    /* Fallback value */
27681450c749Smrg                    speed = 1200;
27691450c749Smrg                    s = "*n";
27701450c749Smrg            }
27711450c749Smrg            xf86SetSerialSpeed(pInfo->fd, 1200);
27721450c749Smrg            xf86WriteSerial(pInfo->fd, "*n", 2);
27731450c749Smrg            xf86WriteSerial(pInfo->fd, "*X", 2);
27741450c749Smrg            xf86WriteSerial(pInfo->fd, s, 2);
27751450c749Smrg            usleep(100000);
27761450c749Smrg            xf86SetSerialSpeed(pInfo->fd, speed);
27771450c749Smrg            break;
2778659607e0Smrg
27791450c749Smrg        case PROT_MMHIT:                /* MM_HitTablet */
27801450c749Smrg            /*
27811450c749Smrg             * Initialize Hitachi PUMA Plus - Model 1212E to desired settings.
27821450c749Smrg             * The tablet must be configured to be in MM mode, NO parity,
27831450c749Smrg             * Binary Format.  pMse->sampleRate controls the sensitivity
27841450c749Smrg             * of the tablet.  We only use this tablet for it's 4-button puck
27851450c749Smrg             * so we don't run in "Absolute Mode".
27861450c749Smrg             */
27871450c749Smrg            xf86WriteSerial(pInfo->fd, "z8", 2);        /* Set Parity = "NONE" */
27881450c749Smrg            usleep(50000);
27891450c749Smrg            xf86WriteSerial(pInfo->fd, "zb", 2);        /* Set Format = "Binary" */
27901450c749Smrg            usleep(50000);
27911450c749Smrg            xf86WriteSerial(pInfo->fd, "@", 1); /* Set Report Mode = "Stream" */
27921450c749Smrg            usleep(50000);
27931450c749Smrg            xf86WriteSerial(pInfo->fd, "R", 1); /* Set Output Rate = "45 rps" */
27941450c749Smrg            usleep(50000);
27951450c749Smrg            xf86WriteSerial(pInfo->fd, "I\x20", 2);     /* Set Incrememtal Mode "20" */
27961450c749Smrg            usleep(50000);
27971450c749Smrg            xf86WriteSerial(pInfo->fd, "E", 1); /* Set Data Type = "Relative */
27981450c749Smrg            usleep(50000);
27991450c749Smrg            /*
28001450c749Smrg             * These sample rates translate to 'lines per inch' on the Hitachi
28011450c749Smrg             * tablet.
28021450c749Smrg             */
28031450c749Smrg            if      (pMse->sampleRate <=   40) c = 'g';
28041450c749Smrg            else if (pMse->sampleRate <=  100) c = 'd';
28051450c749Smrg            else if (pMse->sampleRate <=  200) c = 'e';
28061450c749Smrg            else if (pMse->sampleRate <=  500) c = 'h';
28071450c749Smrg            else if (pMse->sampleRate <= 1000) c = 'j';
28081450c749Smrg            else                               c = 'd';
28091450c749Smrg            xf86WriteSerial(pInfo->fd, &c, 1);
28101450c749Smrg            usleep(50000);
28111450c749Smrg            xf86WriteSerial(pInfo->fd, "\021", 1);      /* Resume DATA output */
28121450c749Smrg            break;
28131450c749Smrg
28141450c749Smrg        case PROT_THINKING:             /* ThinkingMouse */
28151450c749Smrg            /* This mouse may send a PnP ID string, ignore it. */
28161450c749Smrg            usleep(200000);
28171450c749Smrg            xf86FlushInput(pInfo->fd);
28181450c749Smrg            /* Send the command to initialize the beast. */
28191450c749Smrg            for (s = "E5E5"; *s; ++s) {
28201450c749Smrg                xf86WriteSerial(pInfo->fd, s, 1);
28211450c749Smrg                if ((xf86WaitForInput(pInfo->fd, 1000000) <= 0))
28221450c749Smrg                    break;
28231450c749Smrg                xf86ReadSerial(pInfo->fd, &c, 1);
28241450c749Smrg                if (c != *s)
28251450c749Smrg                    break;
28261450c749Smrg            }
28271450c749Smrg            break;
28281450c749Smrg
28291450c749Smrg        case PROT_MSC:          /* MouseSystems Corp */
28301450c749Smrg            usleep(100000);
28311450c749Smrg            xf86FlushInput(pInfo->fd);
28321450c749Smrg            break;
28331450c749Smrg
28341450c749Smrg        case PROT_ACECAD:
28351450c749Smrg            /* initialize */
28361450c749Smrg            /* A nul character resets. */
28371450c749Smrg            xf86WriteSerial(pInfo->fd, "", 1);
28381450c749Smrg            usleep(50000);
28391450c749Smrg            /* Stream out relative mode high resolution increments of 1. */
28401450c749Smrg            xf86WriteSerial(pInfo->fd, "@EeI!", 5);
28411450c749Smrg            break;
28421450c749Smrg
28431450c749Smrg        case PROT_BM:           /* bus/InPort mouse */
28441450c749Smrg            if (osInfo->SetBMRes)
28451450c749Smrg                osInfo->SetBMRes(pInfo, pMse->protocol, pMse->sampleRate,
28461450c749Smrg                                 pMse->resolution);
28471450c749Smrg            break;
28481450c749Smrg
28491450c749Smrg        case PROT_GENPS2:
28501450c749Smrg            ps2Init = FALSE;
28511450c749Smrg            break;
28521450c749Smrg
28531450c749Smrg        case PROT_PS2:
28541450c749Smrg        case PROT_GLIDEPS2:
28551450c749Smrg            break;
28561450c749Smrg
28571450c749Smrg        case PROT_IMPS2:                /* IntelliMouse */
28581450c749Smrg        {
28591450c749Smrg            static unsigned char seq[] = { 243, 200, 243, 100, 243, 80 };
28601450c749Smrg            param = seq;
28611450c749Smrg            paramlen = sizeof(seq);
28621450c749Smrg        }
28631450c749Smrg        break;
28641450c749Smrg
28651450c749Smrg        case PROT_EXPPS2:               /* IntelliMouse Explorer */
28661450c749Smrg        {
28671450c749Smrg            static unsigned char seq[] = { 243, 200, 243, 100, 243, 80,
28681450c749Smrg                                           243, 200, 243, 200, 243, 80 };
28691450c749Smrg
28701450c749Smrg            param = seq;
28711450c749Smrg            paramlen = sizeof(seq);
28721450c749Smrg        }
28731450c749Smrg        break;
28741450c749Smrg
28751450c749Smrg        case PROT_NETPS2:               /* NetMouse, NetMouse Pro, Mie Mouse */
28761450c749Smrg        case PROT_NETSCPS2:             /* NetScroll */
28771450c749Smrg        {
28781450c749Smrg            static unsigned char seq[] = { 232, 3, 230, 230, 230, 233 };
28791450c749Smrg
28801450c749Smrg            param = seq;
28811450c749Smrg            paramlen = sizeof(seq);
28821450c749Smrg        }
28831450c749Smrg        break;
28841450c749Smrg
28851450c749Smrg        case PROT_MMPS2:                /* MouseMan+, FirstMouse+ */
28861450c749Smrg        {
28871450c749Smrg            static unsigned char seq[] = { 230, 232, 0, 232, 3, 232, 2, 232, 1,
28881450c749Smrg                                           230, 232, 3, 232, 1, 232, 2, 232, 3 };
28891450c749Smrg            param = seq;
28901450c749Smrg            paramlen = sizeof(seq);
28911450c749Smrg        }
28921450c749Smrg        break;
28931450c749Smrg
28941450c749Smrg        case PROT_THINKPS2:             /* ThinkingMouse */
28951450c749Smrg        {
28961450c749Smrg            static unsigned char seq[] = { 243, 10, 232,  0, 243, 20, 243, 60,
28971450c749Smrg                                           243, 40, 243, 20, 243, 20, 243, 60,
28981450c749Smrg                                           243, 40, 243, 20, 243, 20 };
28991450c749Smrg            param = seq;
29001450c749Smrg            paramlen = sizeof(seq);
29011450c749Smrg        }
29021450c749Smrg        break;
29031450c749Smrg        case PROT_SYSMOUSE:
29041450c749Smrg            if (osInfo->SetMiscRes)
29051450c749Smrg                osInfo->SetMiscRes(pInfo, pMse->protocol, pMse->sampleRate,
29061450c749Smrg                                   pMse->resolution);
29071450c749Smrg            break;
29081450c749Smrg
29091450c749Smrg        default:
29101450c749Smrg            /* Nothing to do. */
29111450c749Smrg            break;
2912659607e0Smrg    }
2913659607e0Smrg
2914659607e0Smrg    if (pMse->class & (MSE_PS2 | MSE_XPS2)) {
29151450c749Smrg        /*
29161450c749Smrg         * If one part of the PS/2 mouse initialization fails
29171450c749Smrg         * redo complete initialization. There are mice which
29181450c749Smrg         * have occasional problems with initialization and
29191450c749Smrg         * are in an unknown state.
29201450c749Smrg         */
29211450c749Smrg        if (ps2Init) {
29221450c749Smrg        REDO:
29231450c749Smrg            do_ps2Reset(pInfo);
29241450c749Smrg            if (paramlen > 0) {
29251450c749Smrg                if (!ps2SendPacket(pInfo,param,paramlen)) {
29261450c749Smrg                    usleep(30000);
29271450c749Smrg                    xf86FlushInput(pInfo->fd);
29281450c749Smrg                    if (!count--)
29291450c749Smrg                        return TRUE;
29301450c749Smrg                    goto REDO;
29311450c749Smrg                }
29321450c749Smrg                ps2GetDeviceID(pInfo);
29331450c749Smrg                usleep(30000);
29341450c749Smrg                xf86FlushInput(pInfo->fd);
29351450c749Smrg            }
29361450c749Smrg
29371450c749Smrg            if (osInfo->SetPS2Res) {
29381450c749Smrg                osInfo->SetPS2Res(pInfo, pMse->protocol, pMse->sampleRate,
29391450c749Smrg                                  pMse->resolution);
29401450c749Smrg            } else {
29411450c749Smrg                unsigned char c2[2];
29421450c749Smrg
29431450c749Smrg                c = 0xE6;       /*230*/ /* 1:1 scaling */
29441450c749Smrg                if (!ps2SendPacket(pInfo,&c,1)) {
29451450c749Smrg                    if (!count--)
29461450c749Smrg                        return TRUE;
29471450c749Smrg                    goto REDO;
29481450c749Smrg                }
29491450c749Smrg                c2[0] = 0xF3; /*243*/ /* set sampling rate */
29501450c749Smrg                if (pMse->sampleRate > 0) {
29511450c749Smrg                    if (pMse->sampleRate >= 200)
29521450c749Smrg                        c2[1] = 200;
29531450c749Smrg                    else if (pMse->sampleRate >= 100)
29541450c749Smrg                        c2[1] = 100;
29551450c749Smrg                    else if (pMse->sampleRate >= 80)
29561450c749Smrg                        c2[1] = 80;
29571450c749Smrg                    else if (pMse->sampleRate >= 60)
29581450c749Smrg                        c2[1] = 60;
29591450c749Smrg                    else if (pMse->sampleRate >= 40)
29601450c749Smrg                        c2[1] = 40;
29611450c749Smrg                    else
29621450c749Smrg                        c2[1] = 20;
29631450c749Smrg                } else {
29641450c749Smrg                    c2[1] = 100;
29651450c749Smrg                }
29661450c749Smrg                if (!ps2SendPacket(pInfo,c2,2)) {
29671450c749Smrg                    if (!count--)
29681450c749Smrg                        return TRUE;
29691450c749Smrg                    goto REDO;
29701450c749Smrg                }
29711450c749Smrg                c2[0] = 0xE8; /*232*/   /* set device resolution */
29721450c749Smrg                if (pMse->resolution > 0) {
29731450c749Smrg                    if (pMse->resolution >= 200)
29741450c749Smrg                        c2[1] = 3;
29751450c749Smrg                    else if (pMse->resolution >= 100)
29761450c749Smrg                        c2[1] = 2;
29771450c749Smrg                    else if (pMse->resolution >= 50)
29781450c749Smrg                        c2[1] = 1;
29791450c749Smrg                    else
29801450c749Smrg                        c2[1] = 0;
29811450c749Smrg                } else {
29821450c749Smrg                    c2[1] = 3; /* used to be 2, W. uses 3 */
29831450c749Smrg                }
29841450c749Smrg                if (!ps2SendPacket(pInfo,c2,2)) {
29851450c749Smrg                    if (!count--)
29861450c749Smrg                        return TRUE;
29871450c749Smrg                    goto REDO;
29881450c749Smrg                }
29891450c749Smrg                usleep(30000);
29901450c749Smrg                xf86FlushInput(pInfo->fd);
29911450c749Smrg                if (!ps2EnableDataReporting(pInfo)) {
29921450c749Smrg                    xf86Msg(X_INFO, "%s: ps2EnableDataReporting: failed\n",
29931450c749Smrg                            pInfo->name);
29941450c749Smrg                    xf86FlushInput(pInfo->fd);
29951450c749Smrg                    if (!count--)
29961450c749Smrg                        return TRUE;
29971450c749Smrg                    goto REDO;
29981450c749Smrg                } else {
29991450c749Smrg                    xf86Msg(X_INFO, "%s: ps2EnableDataReporting: succeeded\n",
30001450c749Smrg                            pInfo->name);
30011450c749Smrg                }
30021450c749Smrg            }
30031450c749Smrg            /*
30041450c749Smrg             * The PS/2 reset handling needs to be rechecked.
30051450c749Smrg             * We need to wait until after the 4.3 release.
30061450c749Smrg             */
30071450c749Smrg        }
3008659607e0Smrg    } else {
30091450c749Smrg        if (paramlen > 0) {
30101450c749Smrg            if (xf86WriteSerial(pInfo->fd, param, paramlen) != paramlen)
30111450c749Smrg                xf86Msg(X_ERROR, "%s: Mouse initialization failed\n",
30121450c749Smrg                        pInfo->name);
30131450c749Smrg            usleep(30000);
30141450c749Smrg            xf86FlushInput(pInfo->fd);
30151450c749Smrg        }
3016659607e0Smrg    }
3017659607e0Smrg
3018659607e0Smrg    return TRUE;
3019659607e0Smrg}
3020659607e0Smrg
3021659607e0Smrg#ifdef SUPPORT_MOUSE_RESET
3022659607e0Smrgstatic Bool
30231450c749SmrgmouseReset(InputInfoPtr pInfo, unsigned char val)
3024659607e0Smrg{
3025659607e0Smrg    MouseDevPtr pMse = pInfo->private;
3026659607e0Smrg    mousePrivPtr mousepriv = (mousePrivPtr)pMse->mousePriv;
3027659607e0Smrg    CARD32 prevEvent = mousepriv->lastEvent;
3028659607e0Smrg    Bool expectReset = FALSE;
3029659607e0Smrg    Bool ret = FALSE;
3030659607e0Smrg
3031659607e0Smrg    mousepriv->lastEvent = GetTimeInMillis();
3032659607e0Smrg
3033659607e0Smrg#ifdef EXTMOUSEDEBUG
30341450c749Smrg    LogMessageVerbSigSafe(X_INFO, -1, "byte: 0x%x time: %li\n",val,mousepriv->lastEvent);
3035659607e0Smrg#endif
3036659607e0Smrg    /*
3037659607e0Smrg     * We believe that the following is true:
3038659607e0Smrg     * When the mouse is replugged it will send a reset package
3039659607e0Smrg     * It takes several seconds to replug a mouse: We don't see
3040659607e0Smrg     * events for several seconds before we see the replug event package.
3041659607e0Smrg     * There is no significant delay between consecutive bytes
3042659607e0Smrg     * of a replug event package.
3043659607e0Smrg     * There are no bytes sent after the replug event package until
3044659607e0Smrg     * the mouse is reset.
3045659607e0Smrg     */
30461450c749Smrg
3047659607e0Smrg    if (mousepriv->current == 0
30481450c749Smrg        && (mousepriv->lastEvent - prevEvent) < 4000)
30491450c749Smrg        return FALSE;
3050659607e0Smrg
3051659607e0Smrg    if (mousepriv->current > 0
30521450c749Smrg        && (mousepriv->lastEvent - prevEvent) >= 1000) {
30531450c749Smrg        mousepriv->inReset = FALSE;
30541450c749Smrg        mousepriv->current = 0;
30551450c749Smrg        return FALSE;
3056659607e0Smrg    }
3057659607e0Smrg
3058659607e0Smrg    if (mousepriv->inReset)
30591450c749Smrg        mousepriv->inReset = FALSE;
3060659607e0Smrg
3061659607e0Smrg#ifdef EXTMOUSEDEBUG
30621450c749Smrg    LogMessageVerbSigSafe(X_INFO, -1, "Mouse Current: %i 0x%x\n",mousepriv->current, val);
3063659607e0Smrg#endif
30641450c749Smrg
3065eeaac534Smrg    /* here we put the mouse specific reset detection */
3066659607e0Smrg    /* They need to do three things:                 */
3067659607e0Smrg    /*  Check if byte may be a reset byte            */
3068659607e0Smrg    /*  If so: Set expectReset TRUE                  */
3069659607e0Smrg    /*  If convinced: Set inReset TRUE               */
3070659607e0Smrg    /*                Register BlockAndWakeupHandler */
3071659607e0Smrg
3072659607e0Smrg    /* PS/2 */
3073659607e0Smrg    {
30741450c749Smrg        unsigned char seq[] = { 0xaa, 0x00 };
30751450c749Smrg        int len = sizeof(seq);
3076659607e0Smrg
30771450c749Smrg        if (seq[mousepriv->current] == val)
30781450c749Smrg            expectReset = TRUE;
3079659607e0Smrg
30801450c749Smrg        if (len == mousepriv->current + 1) {
30811450c749Smrg            mousepriv->inReset = TRUE;
30821450c749Smrg            mousepriv->expires = GetTimeInMillis() + 1000;
3083659607e0Smrg
3084659607e0Smrg#ifdef EXTMOUSEDEBUG
30851450c749Smrg            LogMessageVerbSigSafe(X_INFO, -1, "Found PS/2 Reset string\n");
3086659607e0Smrg#endif
30871450c749Smrg            RegisterBlockAndWakeupHandlers (ps2BlockHandler,
30881450c749Smrg                                            ps2WakeupHandler, (pointer) pInfo);
30891450c749Smrg            ret = TRUE;
30901450c749Smrg        }
3091659607e0Smrg    }
30921450c749Smrg
30931450c749Smrg        if (!expectReset)
30941450c749Smrg            mousepriv->current = 0;
30951450c749Smrg        else
30961450c749Smrg            mousepriv->current++;
30971450c749Smrg        return ret;
3098659607e0Smrg}
3099659607e0Smrg
3100659607e0Smrgstatic void
3101659607e0Smrgps2BlockHandler(pointer data, struct timeval **waitTime,
31021450c749Smrg                pointer LastSelectMask)
3103659607e0Smrg{
3104659607e0Smrg    InputInfoPtr    pInfo = (InputInfoPtr) data;
31051450c749Smrg    MouseDevPtr     pMse = (MouseDevPtr) pInfo->private;
3106659607e0Smrg    mousePrivPtr    mousepriv = (mousePrivPtr)pMse->mousePriv;
31071450c749Smrg    int             ms;
3108659607e0Smrg
3109659607e0Smrg    if (mousepriv->inReset) {
31101450c749Smrg        ms = mousepriv->expires - GetTimeInMillis ();
31111450c749Smrg        if (ms <= 0)
31121450c749Smrg            ms = 0;
31131450c749Smrg        AdjustWaitForDelay (waitTime, ms);
3114659607e0Smrg    } else
31151450c749Smrg        RemoveBlockAndWakeupHandlers (ps2BlockHandler, ps2WakeupHandler,
31161450c749Smrg                                      (pointer) pInfo);
3117659607e0Smrg}
3118659607e0Smrg
3119659607e0Smrgstatic void
3120659607e0Smrgps2WakeupHandler(pointer data, int i, pointer LastSelectMask)
3121659607e0Smrg{
3122659607e0Smrg    InputInfoPtr    pInfo = (InputInfoPtr) data;
31231450c749Smrg    MouseDevPtr     pMse = (MouseDevPtr) pInfo->private;
3124659607e0Smrg    mousePrivPtr mousepriv = (mousePrivPtr)pMse->mousePriv;
31251450c749Smrg    int             ms;
3126659607e0Smrg
31271450c749Smrg    if (mousepriv->inReset) {
31281450c749Smrg        unsigned char val;
31291450c749Smrg        int blocked;
31301450c749Smrg
31311450c749Smrg        ms = mousepriv->expires - GetTimeInMillis();
31321450c749Smrg        if (ms > 0)
31331450c749Smrg            return;
31341450c749Smrg
31351450c749Smrg        blocked = xf86BlockSIGIO ();
31361450c749Smrg
31371450c749Smrg        xf86MsgVerb(X_INFO,3,
31381450c749Smrg                    "Got reinsert event: reinitializing PS/2 mouse\n");
31391450c749Smrg        val = 0xf4;
31401450c749Smrg        if (xf86WriteSerial(pInfo->fd, &val, 1) != 1)
31411450c749Smrg            xf86Msg(X_ERROR, "%s: Write to mouse failed\n",
31421450c749Smrg                    pInfo->name);
31431450c749Smrg        xf86UnblockSIGIO(blocked);
3144659607e0Smrg    }
3145659607e0Smrg    RemoveBlockAndWakeupHandlers (ps2BlockHandler, ps2WakeupHandler,
31461450c749Smrg                                  (pointer) pInfo);
3147659607e0Smrg}
3148659607e0Smrg#endif /* SUPPORT_MOUSE_RESET */
3149659607e0Smrg
3150659607e0Smrg/************************************************************
3151659607e0Smrg *
3152659607e0Smrg * Autoprobe stuff
3153659607e0Smrg *
3154659607e0Smrg ************************************************************/
3155659607e0Smrg#ifdef EXTMOUSEDEBUG
3156659607e0Smrg#  define AP_DBG(x) { ErrorF("Autoprobe: "); ErrorF x; }
3157659607e0Smrg#  define AP_DBGC(x) ErrorF x ;
3158659607e0Smrg# else
3159659607e0Smrg#  define AP_DBG(x)
3160659607e0Smrg#  define AP_DBGC(x)
3161659607e0Smrg#endif
3162659607e0Smrg
3163eeaac534Smrgstatic
31641450c749SmrgMouseProtocolID hardProtocolList[] = {  PROT_MSC, PROT_MM, PROT_LOGI,
31651450c749Smrg                                        PROT_LOGIMAN, PROT_MMHIT,
31661450c749Smrg                                        PROT_GLIDE, PROT_IMSERIAL,
31671450c749Smrg                                        PROT_THINKING, PROT_ACECAD,
31681450c749Smrg                                        PROT_THINKPS2, PROT_MMPS2,
31691450c749Smrg                                        PROT_GLIDEPS2,
31701450c749Smrg                                        PROT_NETSCPS2, PROT_EXPPS2,PROT_IMPS2,
31711450c749Smrg                                        PROT_GENPS2, PROT_NETPS2,
31721450c749Smrg                                        PROT_MS,
31731450c749Smrg                                        PROT_UNKNOWN
3174659607e0Smrg};
3175659607e0Smrg
3176eeaac534Smrgstatic
31771450c749SmrgMouseProtocolID softProtocolList[] = {  PROT_MSC, PROT_MM, PROT_LOGI,
31781450c749Smrg                                        PROT_LOGIMAN, PROT_MMHIT,
31791450c749Smrg                                        PROT_GLIDE, PROT_IMSERIAL,
31801450c749Smrg                                        PROT_THINKING, PROT_ACECAD,
31811450c749Smrg                                        PROT_THINKPS2, PROT_MMPS2,
31821450c749Smrg                                        PROT_GLIDEPS2,
31831450c749Smrg                                        PROT_NETSCPS2 ,PROT_IMPS2,
31841450c749Smrg                                        PROT_GENPS2,
31851450c749Smrg                                        PROT_MS,
31861450c749Smrg                                        PROT_UNKNOWN
3187659607e0Smrg};
3188659607e0Smrg
3189659607e0Smrgstatic const char *
3190659607e0SmrgautoOSProtocol(InputInfoPtr pInfo, int *protoPara)
3191659607e0Smrg{
3192659607e0Smrg    MouseDevPtr pMse = pInfo->private;
3193659607e0Smrg    const char *name = NULL;
3194659607e0Smrg    MouseProtocolID protocolID = PROT_UNKNOWN;
3195659607e0Smrg
3196659607e0Smrg    /* Check if the OS has a detection mechanism. */
3197659607e0Smrg    if (osInfo->SetupAuto) {
31981450c749Smrg        name = osInfo->SetupAuto(pInfo, protoPara);
31991450c749Smrg        if (name) {
32001450c749Smrg            protocolID = ProtocolNameToID(name);
32011450c749Smrg            switch (protocolID) {
32021450c749Smrg                case PROT_UNKNOWN:
32031450c749Smrg                    /* Check for a builtin OS-specific protocol. */
32041450c749Smrg                    if (osInfo->CheckProtocol && osInfo->CheckProtocol(name)) {
32051450c749Smrg                        /* We can only come here if the protocol has been
32061450c749Smrg                         * changed to auto thru the xf86misc extension
32071450c749Smrg                         * and we have detected an OS specific builtin
32081450c749Smrg                         * protocol. Currently we cannot handle this */
32091450c749Smrg                        name = NULL;
32101450c749Smrg                    } else
32111450c749Smrg                        name = NULL;
32121450c749Smrg                    break;
32131450c749Smrg                case PROT_UNSUP:
32141450c749Smrg                    name = NULL;
32151450c749Smrg                    break;
32161450c749Smrg                default:
32171450c749Smrg                    break;
32181450c749Smrg            }
32191450c749Smrg        }
3220659607e0Smrg    }
3221659607e0Smrg    if (!name) {
32221450c749Smrg        /* A PnP serial mouse? */
32231450c749Smrg        protocolID = MouseGetPnpProtocol(pInfo);
32241450c749Smrg        if (protocolID >= 0 && protocolID < PROT_NUMPROTOS) {
32251450c749Smrg            name = ProtocolIDToName(protocolID);
32261450c749Smrg            xf86Msg(X_PROBED, "%s: PnP-detected protocol: \"%s\"\n",
32271450c749Smrg                    pInfo->name, name);
32281450c749Smrg        }
3229659607e0Smrg    }
3230eeaac534Smrg    if (!name && osInfo->GuessProtocol) {
32311450c749Smrg        name = osInfo->GuessProtocol(pInfo, 0);
32321450c749Smrg        if (name)
32331450c749Smrg            protocolID = ProtocolNameToID(name);
3234659607e0Smrg    }
3235659607e0Smrg
3236659607e0Smrg    if (name) {
32371450c749Smrg        pMse->protocolID = protocolID;
3238659607e0Smrg    }
32391450c749Smrg
3240659607e0Smrg    return name;
3241659607e0Smrg}
3242659607e0Smrg
3243659607e0Smrg/*
3244659607e0Smrg * createProtocolList() -- create a list of protocols which may
3245659607e0Smrg * match on the incoming data stream.
3246659607e0Smrg */
3247659607e0Smrgstatic void
3248659607e0SmrgcreateProtoList(MouseDevPtr pMse, MouseProtocolID *protoList)
3249659607e0Smrg{
3250659607e0Smrg    int i, j, k  = 0;
3251659607e0Smrg    MouseProtocolID prot;
3252659607e0Smrg    unsigned char *para;
3253659607e0Smrg    mousePrivPtr mPriv = (mousePrivPtr)pMse->mousePriv;
3254659607e0Smrg    MouseProtocolID *tmplist = NULL;
3255659607e0Smrg    int blocked;
32561450c749Smrg
3257659607e0Smrg    AP_DBGC(("Autoprobe: "));
3258659607e0Smrg    for (i = 0; i < mPriv->count; i++)
32591450c749Smrg        AP_DBGC(("%2.2x ", (unsigned char) mPriv->data[i]));
3260659607e0Smrg    AP_DBGC(("\n"));
3261659607e0Smrg
3262659607e0Smrg    blocked = xf86BlockSIGIO ();
3263659607e0Smrg
3264659607e0Smrg    /* create a private copy first so we can write in the old list */
3265dd4cbfe8Smrg    if ((tmplist = malloc(sizeof(MouseProtocolID) * NUM_AUTOPROBE_PROTOS))){
32661450c749Smrg        for (i = 0; protoList[i] != PROT_UNKNOWN; i++) {
32671450c749Smrg            tmplist[i] = protoList[i];
32681450c749Smrg        }
32691450c749Smrg        tmplist[i] = PROT_UNKNOWN;
32701450c749Smrg        protoList = tmplist;
3271659607e0Smrg    } else
32721450c749Smrg        return;
32731450c749Smrg
32741450c749Smrg    for (i = 0; ((prot = protoList[i]) != PROT_UNKNOWN
32751450c749Smrg                 && (k < NUM_AUTOPROBE_PROTOS - 1)) ; i++) {
32761450c749Smrg        Bool bad = TRUE;
32771450c749Smrg        unsigned char byte = 0;
32781450c749Smrg        int count = 0;
32791450c749Smrg        int next_header_candidate = 0;
32801450c749Smrg        int header_count = 0;
32811450c749Smrg
32821450c749Smrg        if (!GetProtocol(prot))
32831450c749Smrg            continue;
32841450c749Smrg        para = proto[prot];
32851450c749Smrg
32861450c749Smrg        AP_DBG(("Protocol: %s ", ProtocolIDToName(prot)));
3287659607e0Smrg
3288659607e0Smrg#ifdef EXTMOUSEDEBUG
32891450c749Smrg        for (j = 0; j < 7; j++)
32901450c749Smrg            AP_DBGC(("%2.2x ", (unsigned char) para[j]));
32911450c749Smrg        AP_DBGC(("\n"));
32921450c749Smrg#endif
32931450c749Smrg        j = 0;
32941450c749Smrg        while (1) {
32951450c749Smrg            /* look for header */
32961450c749Smrg            while (j < mPriv->count) {
32971450c749Smrg                if (((byte = mPriv->data[j++]) & para[0]) == para[1]){
32981450c749Smrg                    AP_DBG(("found header %2.2x\n",byte));
32991450c749Smrg                    next_header_candidate = j;
33001450c749Smrg                    count = 1;
33011450c749Smrg                    break;
33021450c749Smrg                } else {
33031450c749Smrg                    /*
33041450c749Smrg                     * Bail out if number of bytes per package have
33051450c749Smrg                     * been tested for header.
33061450c749Smrg                     * Take bytes per package of leading garbage into
33071450c749Smrg                     * account.
33081450c749Smrg                     */
33091450c749Smrg                    if (j > para[4] && ++header_count > para[4]) {
33101450c749Smrg                        j = mPriv->count;
33111450c749Smrg                        break;
33121450c749Smrg                    }
33131450c749Smrg                }
33141450c749Smrg            }
33151450c749Smrg            /* check if remaining data matches protocol */
33161450c749Smrg            while (j < mPriv->count) {
33171450c749Smrg                byte = mPriv->data[j++];
33181450c749Smrg                if (count == para[4]) {
33191450c749Smrg                    count = 0;
33201450c749Smrg                    /* check and eat excess byte */
33211450c749Smrg                    if (((byte & para[0]) != para[1])
33221450c749Smrg                        && ((byte & para[5]) == para[6])) {
33231450c749Smrg                        AP_DBG(("excess byte found\n"));
33241450c749Smrg                        continue;
33251450c749Smrg                    }
33261450c749Smrg                }
33271450c749Smrg                if (count == 0) {
33281450c749Smrg                    /* validate next header */
33291450c749Smrg                    bad = FALSE;
33301450c749Smrg                    AP_DBG(("Complete set found\n"));
33311450c749Smrg                    if ((byte & para[0]) != para[1]) {
33321450c749Smrg                        AP_DBG(("Autoprobe: header bad\n"));
33331450c749Smrg                        bad = TRUE;
33341450c749Smrg                        break;
33351450c749Smrg                    } else {
33361450c749Smrg                        count++;
33371450c749Smrg                        continue;
33381450c749Smrg                    }
33391450c749Smrg                }
33401450c749Smrg                /* validate data */
33411450c749Smrg                else if (((byte & para[2]) != para[3])
33421450c749Smrg                         || ((para[7] & MPF_SAFE)
33431450c749Smrg                             && ((byte & para[0]) == para[1]))) {
33441450c749Smrg                    AP_DBG(("data bad\n"));
33451450c749Smrg                    bad = TRUE;
33461450c749Smrg                    break;
33471450c749Smrg                } else {
33481450c749Smrg                    count ++;
33491450c749Smrg                    continue;
33501450c749Smrg                }
33511450c749Smrg            }
33521450c749Smrg            if (!bad) {
33531450c749Smrg                /* this is a matching protocol */
33541450c749Smrg                mPriv->protoList[k++] = prot;
33551450c749Smrg                AP_DBG(("Autoprobe: Adding protocol %s to list (entry %i)\n",
33561450c749Smrg                        ProtocolIDToName(prot),k-1));
33571450c749Smrg                break;
33581450c749Smrg            }
33591450c749Smrg            j = next_header_candidate;
33601450c749Smrg            next_header_candidate = 0;
33611450c749Smrg            /* we have tested number of bytes per package for header */
33621450c749Smrg            if (j > para[4] && ++header_count > para[4])
33631450c749Smrg                break;
33641450c749Smrg            /* we have not found anything that looks like a header */
33651450c749Smrg            if (!next_header_candidate)
33661450c749Smrg                break;
33671450c749Smrg            AP_DBG(("Looking for new header\n"));
33681450c749Smrg        }
3369659607e0Smrg    }
3370659607e0Smrg
3371659607e0Smrg    xf86UnblockSIGIO(blocked);
33721450c749Smrg
3373659607e0Smrg    mPriv->protoList[k] = PROT_UNKNOWN;
3374659607e0Smrg
3375dd4cbfe8Smrg    free(tmplist);
3376659607e0Smrg}
3377659607e0Smrg
3378659607e0Smrg
3379659607e0Smrg/* This only needs to be done once */
3380eeaac534Smrgstatic void **serialDefaultsList = NULL;
3381659607e0Smrg
3382659607e0Smrg/*
3383659607e0Smrg * createSerialDefaultsLists() - create a list of the different default
3384659607e0Smrg * settings for the serial interface of the known protocols.
3385659607e0Smrg */
3386659607e0Smrgstatic void
3387659607e0SmrgcreateSerialDefaultsList(void)
3388659607e0Smrg{
3389659607e0Smrg    int i = 0, j, k;
3390659607e0Smrg
3391659607e0Smrg    serialDefaultsList = (void **)xnfalloc(sizeof(void*));
3392659607e0Smrg    serialDefaultsList[0] = NULL;
3393659607e0Smrg
3394659607e0Smrg    for (j = 0; mouseProtocols[j].name; j++) {
33951450c749Smrg        if (!mouseProtocols[j].defaults)
33961450c749Smrg            continue;
33971450c749Smrg        for (k = 0; k < i; k++)
33981450c749Smrg            if (mouseProtocols[j].defaults == serialDefaultsList[k])
33991450c749Smrg                continue;
34001450c749Smrg        i++;
34011450c749Smrg        serialDefaultsList = (void**)xnfrealloc(serialDefaultsList,
34021450c749Smrg                                                sizeof(void*)*(i+1));
34031450c749Smrg        serialDefaultsList[i-1] = mouseProtocols[j].defaults;
34041450c749Smrg        serialDefaultsList[i] = NULL;
3405659607e0Smrg    }
3406659607e0Smrg}
3407659607e0Smrg
3408659607e0Smrgtypedef enum {
3409659607e0Smrg    STATE_INVALID,
3410659607e0Smrg    STATE_UNCERTAIN,
3411659607e0Smrg    STATE_VALID
3412659607e0Smrg} validState;
3413659607e0Smrg
3414659607e0Smrg/* Probing threshold values */
3415659607e0Smrg#define PROBE_UNCERTAINTY 50
3416659607e0Smrg#define BAD_CERTAINTY 6
3417659607e0Smrg#define BAD_INC_CERTAINTY 1
3418659607e0Smrg#define BAD_INC_CERTAINTY_WHEN_SYNC_LOST 2
3419659607e0Smrg
3420659607e0Smrgstatic validState
34211450c749SmrgvalidCount(mousePrivPtr mPriv, Bool inSync, Bool lostSync)
3422659607e0Smrg{
3423659607e0Smrg    if (inSync) {
34241450c749Smrg        if (!--mPriv->goodCount) {
34251450c749Smrg            /* we are sure to have found the correct protocol */
34261450c749Smrg            mPriv->badCount = 0;
34271450c749Smrg            return STATE_VALID;
34281450c749Smrg        }
34291450c749Smrg        AP_DBG(("%i successful rounds to go\n",
34301450c749Smrg                mPriv->goodCount));
34311450c749Smrg        return STATE_UNCERTAIN;
3432659607e0Smrg    }
3433659607e0Smrg
3434659607e0Smrg
3435659607e0Smrg    /* We are out of sync again */
3436659607e0Smrg    mPriv->goodCount = PROBE_UNCERTAINTY;
3437659607e0Smrg    /* We increase uncertainty of having the correct protocol */
34381450c749Smrg    mPriv->badCount+= lostSync ? BAD_INC_CERTAINTY_WHEN_SYNC_LOST
34391450c749Smrg        : BAD_INC_CERTAINTY;
3440659607e0Smrg
3441659607e0Smrg    if (mPriv->badCount < BAD_CERTAINTY) {
34421450c749Smrg        /* We are not convinced yet to have the wrong protocol */
34431450c749Smrg        AP_DBG(("Changing protocol after: %i rounds\n",
34441450c749Smrg                BAD_CERTAINTY - mPriv->badCount));
34451450c749Smrg        return STATE_UNCERTAIN;
3446659607e0Smrg    }
3447659607e0Smrg    return STATE_INVALID;
3448659607e0Smrg}
3449659607e0Smrg
34501450c749Smrg#define RESET_VALIDATION        mPriv->goodCount = PROBE_UNCERTAINTY;\
34511450c749Smrg                                mPriv->badCount = 0;\
34521450c749Smrg                                mPriv->prevDx = 0;\
34531450c749Smrg                                mPriv->prevDy = 0;\
34541450c749Smrg                                mPriv->accDx = 0;\
34551450c749Smrg                                mPriv->accDy = 0;\
34561450c749Smrg                                mPriv->acc = 0;
3457659607e0Smrg
3458659607e0Smrgstatic void
34591450c749SmrgautoProbeMouse(InputInfoPtr pInfo, Bool inSync, Bool lostSync)
3460659607e0Smrg{
3461659607e0Smrg    MouseDevPtr pMse = pInfo->private;
3462659607e0Smrg    mousePrivPtr mPriv = (mousePrivPtr)pMse->mousePriv;
3463659607e0Smrg
3464659607e0Smrg    MouseProtocolID *protocolList = NULL;
34651450c749Smrg
3466659607e0Smrg    while (1) {
34671450c749Smrg        switch (mPriv->autoState) {
34681450c749Smrg        case AUTOPROBE_GOOD:
34691450c749Smrg            if (inSync)
34701450c749Smrg                return;
34711450c749Smrg            AP_DBG(("State GOOD\n"));
34721450c749Smrg            RESET_VALIDATION;
34731450c749Smrg            mPriv->autoState = AUTOPROBE_VALIDATE1;
34741450c749Smrg            return;
34751450c749Smrg        case AUTOPROBE_H_GOOD:
34761450c749Smrg            if (inSync)
34771450c749Smrg                return;
34781450c749Smrg            AP_DBG(("State H_GOOD\n"));
34791450c749Smrg            RESET_VALIDATION;
34801450c749Smrg            mPriv->autoState = AUTOPROBE_H_VALIDATE2;
34811450c749Smrg            return;
34821450c749Smrg        case AUTOPROBE_H_NOPROTO:
34831450c749Smrg            AP_DBG(("State H_NOPROTO\n"));
34841450c749Smrg            mPriv->protocolID = 0;
34851450c749Smrg            mPriv->autoState = AUTOPROBE_H_SETPROTO;
34861450c749Smrg            break;
34871450c749Smrg        case AUTOPROBE_H_SETPROTO:
34881450c749Smrg            AP_DBG(("State H_SETPROTO\n"));
34891450c749Smrg            if ((pMse->protocolID = hardProtocolList[mPriv->protocolID++])
34901450c749Smrg                == PROT_UNKNOWN) {
34911450c749Smrg                mPriv->protocolID = 0;
34921450c749Smrg                break;
34931450c749Smrg            } else if (GetProtocol(pMse->protocolID) &&  SetupMouse(pInfo)) {
34941450c749Smrg                FlushButtons(pMse);
34951450c749Smrg                RESET_VALIDATION;
34961450c749Smrg                AP_DBG(("Autoprobe: Trying Protocol: %s\n",
34971450c749Smrg                        ProtocolIDToName(pMse->protocolID)));
34981450c749Smrg                mPriv->autoState = AUTOPROBE_H_VALIDATE1;
34991450c749Smrg                return;
35001450c749Smrg            }
35011450c749Smrg            break;
35021450c749Smrg        case AUTOPROBE_H_VALIDATE1:
35031450c749Smrg            AP_DBG(("State H_VALIDATE1\n"));
35041450c749Smrg            switch (validCount(mPriv,inSync,lostSync)) {
35051450c749Smrg            case STATE_INVALID:
35061450c749Smrg                mPriv->autoState = AUTOPROBE_H_SETPROTO;
35071450c749Smrg                break;
35081450c749Smrg            case STATE_VALID:
35091450c749Smrg                    xf86Msg(X_INFO,"Mouse autoprobe: selecting %s protocol\n",
35101450c749Smrg                            ProtocolIDToName(pMse->protocolID));
35111450c749Smrg                    mPriv->autoState = AUTOPROBE_H_GOOD;
35121450c749Smrg                    return;
35131450c749Smrg            case STATE_UNCERTAIN:
35141450c749Smrg                return;
35151450c749Smrg            default:
35161450c749Smrg                break;
35171450c749Smrg            }
35181450c749Smrg            break;
35191450c749Smrg        case AUTOPROBE_H_VALIDATE2:
35201450c749Smrg            AP_DBG(("State H_VALIDATE2\n"));
35211450c749Smrg            switch (validCount(mPriv,inSync,lostSync)) {
35221450c749Smrg            case STATE_INVALID:
35231450c749Smrg                mPriv->autoState = AUTOPROBE_H_AUTODETECT;
35241450c749Smrg                break;
35251450c749Smrg            case STATE_VALID:
35261450c749Smrg                xf86Msg(X_INFO,"Mouse autoprobe: selecting %s protocol\n",
35271450c749Smrg                        ProtocolIDToName(pMse->protocolID));
35281450c749Smrg                mPriv->autoState = AUTOPROBE_H_GOOD;
35291450c749Smrg                return;
35301450c749Smrg            case STATE_UNCERTAIN:
35311450c749Smrg                return;
35321450c749Smrg            }
35331450c749Smrg            break;
35341450c749Smrg        case AUTOPROBE_H_AUTODETECT:
35351450c749Smrg            AP_DBG(("State H_AUTODETECT\n"));
35361450c749Smrg            pMse->protocolID = PROT_AUTO;
35371450c749Smrg            AP_DBG(("Looking for PnP/OS mouse\n"));
35381450c749Smrg            mPriv->count = 0;
35391450c749Smrg            SetupMouse(pInfo);
35401450c749Smrg            if (pMse->protocolID != PROT_AUTO)
35411450c749Smrg                mPriv->autoState = AUTOPROBE_H_GOOD;
35421450c749Smrg            else
35431450c749Smrg                mPriv->autoState = AUTOPROBE_H_NOPROTO;
35441450c749Smrg            break;
35451450c749Smrg        case AUTOPROBE_NOPROTO:
35461450c749Smrg            AP_DBG(("State NOPROTO\n"));
35471450c749Smrg            mPriv->count = 0;
35481450c749Smrg            mPriv->serialDefaultsNum = -1;
35491450c749Smrg            mPriv->autoState = AUTOPROBE_COLLECT;
35501450c749Smrg            break;
35511450c749Smrg        case AUTOPROBE_COLLECT:
35521450c749Smrg            AP_DBG(("State COLLECT\n"));
35531450c749Smrg            if (mPriv->count <= NUM_MSE_AUTOPROBE_BYTES)
35541450c749Smrg                return;
35551450c749Smrg            protocolList = softProtocolList;
35561450c749Smrg            mPriv->autoState = AUTOPROBE_CREATE_PROTOLIST;
35571450c749Smrg            break;
35581450c749Smrg        case AUTOPROBE_CREATE_PROTOLIST:
35591450c749Smrg            AP_DBG(("State CREATE_PROTOLIST\n"));
35601450c749Smrg            createProtoList(pMse, protocolList);
35611450c749Smrg            mPriv->protocolID = 0;
35621450c749Smrg            mPriv->autoState = AUTOPROBE_SWITCH_PROTOCOL;
35631450c749Smrg            break;
35641450c749Smrg        case AUTOPROBE_AUTODETECT:
35651450c749Smrg            AP_DBG(("State AUTODETECT\n"));
35661450c749Smrg            pMse->protocolID = PROT_AUTO;
35671450c749Smrg            AP_DBG(("Looking for PnP/OS mouse\n"));
35681450c749Smrg            mPriv->count = 0;
35691450c749Smrg            SetupMouse(pInfo);
35701450c749Smrg            if (pMse->protocolID != PROT_AUTO)
35711450c749Smrg                mPriv->autoState = AUTOPROBE_GOOD;
35721450c749Smrg            else
35731450c749Smrg                mPriv->autoState = AUTOPROBE_NOPROTO;
35741450c749Smrg            break;
35751450c749Smrg        case AUTOPROBE_VALIDATE1:
35761450c749Smrg            AP_DBG(("State VALIDATE1\n"));
35771450c749Smrg            switch (validCount(mPriv,inSync,lostSync)) {
35781450c749Smrg            case STATE_INVALID:
35791450c749Smrg                mPriv->autoState = AUTOPROBE_AUTODETECT;
35801450c749Smrg                break;
35811450c749Smrg            case STATE_VALID:
35821450c749Smrg                xf86Msg(X_INFO,"Mouse autoprobe: selecting %s protocol\n",
35831450c749Smrg                        ProtocolIDToName(pMse->protocolID));
35841450c749Smrg                mPriv->autoState = AUTOPROBE_GOOD;
35851450c749Smrg                break;
35861450c749Smrg            case STATE_UNCERTAIN:
35871450c749Smrg                return;
35881450c749Smrg            }
35891450c749Smrg            break;
35901450c749Smrg        case AUTOPROBE_VALIDATE2:
35911450c749Smrg            AP_DBG(("State VALIDATE2\n"));
35921450c749Smrg            switch (validCount(mPriv,inSync,lostSync)) {
35931450c749Smrg            case STATE_INVALID:
35941450c749Smrg                protocolList = &mPriv->protoList[mPriv->protocolID];
35951450c749Smrg                mPriv->autoState = AUTOPROBE_CREATE_PROTOLIST;
35961450c749Smrg                break;
35971450c749Smrg            case STATE_VALID:
35981450c749Smrg                xf86Msg(X_INFO,"Mouse autoprobe: selecting %s protocol\n",
35991450c749Smrg                        ProtocolIDToName(pMse->protocolID));
36001450c749Smrg                mPriv->autoState = AUTOPROBE_GOOD;
36011450c749Smrg                break;
36021450c749Smrg            case STATE_UNCERTAIN:
36031450c749Smrg                return;
36041450c749Smrg            }
36051450c749Smrg            break;
36061450c749Smrg        case AUTOPROBE_SWITCHSERIAL:
36071450c749Smrg        {
36081450c749Smrg            pointer serialDefaults;
36091450c749Smrg            AP_DBG(("State SWITCHSERIAL\n"));
36101450c749Smrg
36111450c749Smrg            if (!serialDefaultsList)
36121450c749Smrg                createSerialDefaultsList();
36131450c749Smrg
36141450c749Smrg            AP_DBG(("Switching serial params\n"));
36151450c749Smrg            if ((serialDefaults =
36161450c749Smrg                 serialDefaultsList[++mPriv->serialDefaultsNum]) == NULL) {
36171450c749Smrg                mPriv->serialDefaultsNum = 0;
36181450c749Smrg            } else {
36191450c749Smrg                pointer tmp = xf86OptionListCreate(serialDefaults, -1, 0);
36201450c749Smrg                xf86SetSerial(pInfo->fd, tmp);
36211450c749Smrg                xf86OptionListFree(tmp);
36221450c749Smrg                mPriv->count = 0;
36231450c749Smrg                mPriv->autoState = AUTOPROBE_COLLECT;
36241450c749Smrg            }
36251450c749Smrg            break;
36261450c749Smrg        }
36271450c749Smrg        case AUTOPROBE_SWITCH_PROTOCOL:
36281450c749Smrg        {
36291450c749Smrg            MouseProtocolID prot;
36301450c749Smrg            MouseProtocolPtr pProto;
36311450c749Smrg            void *defaults;
36321450c749Smrg            AP_DBG(("State SWITCH_PROTOCOL\n"));
36331450c749Smrg            prot = mPriv->protoList[mPriv->protocolID++];
36341450c749Smrg            if (prot == PROT_UNKNOWN)
36351450c749Smrg                mPriv->autoState = AUTOPROBE_SWITCHSERIAL;
36361450c749Smrg            else if (!((pProto = GetProtocol(prot)) &&
36371450c749Smrg                       ((defaults = pProto->defaults)))
36381450c749Smrg                       || (mPriv->serialDefaultsNum == -1
36391450c749Smrg                           && (defaults == msDefaults))
36401450c749Smrg                       || (mPriv->serialDefaultsNum != -1
36411450c749Smrg                           && serialDefaultsList[mPriv->serialDefaultsNum]
36421450c749Smrg                           == defaults)) {
36431450c749Smrg                AP_DBG(("Changing Protocol to %s\n",
36441450c749Smrg                        ProtocolIDToName(prot)));
36451450c749Smrg                SetMouseProto(pMse,prot);
36461450c749Smrg                FlushButtons(pMse);
36471450c749Smrg                RESET_VALIDATION;
36481450c749Smrg                mPriv->autoState = AUTOPROBE_VALIDATE2;
36491450c749Smrg                return;
36501450c749Smrg            }
36511450c749Smrg            break;
36521450c749Smrg        }
36531450c749Smrg        }
3654659607e0Smrg    }
3655659607e0Smrg}
3656659607e0Smrg
3657659607e0Smrgstatic Bool
3658659607e0SmrgautoGood(MouseDevPtr pMse)
3659659607e0Smrg{
3660659607e0Smrg    mousePrivPtr mPriv = (mousePrivPtr)pMse->mousePriv;
36611450c749Smrg
3662659607e0Smrg    if (!pMse->autoProbe)
36631450c749Smrg        return TRUE;
3664659607e0Smrg
3665659607e0Smrg    switch (mPriv->autoState) {
3666659607e0Smrg    case AUTOPROBE_GOOD:
3667659607e0Smrg    case AUTOPROBE_H_GOOD:
36681450c749Smrg        return TRUE;
3669659607e0Smrg    case AUTOPROBE_VALIDATE1: /* @@@ */
3670659607e0Smrg    case AUTOPROBE_H_VALIDATE1: /* @@@ */
3671659607e0Smrg    case AUTOPROBE_VALIDATE2:
3672659607e0Smrg    case AUTOPROBE_H_VALIDATE2:
36731450c749Smrg        if (mPriv->goodCount < PROBE_UNCERTAINTY/2)
36741450c749Smrg            return TRUE;
3675659607e0Smrg    default:
36761450c749Smrg        return FALSE;
3677659607e0Smrg    }
3678659607e0Smrg}
3679659607e0Smrg
3680659607e0Smrg
3681659607e0Smrg#define TOT_THRESHOLD 3000
3682659607e0Smrg#define VAL_THRESHOLD 40
3683659607e0Smrg
3684659607e0Smrg/*
3685659607e0Smrg * checkForErraticMovements() -- check if mouse 'jumps around'.
3686659607e0Smrg */
3687659607e0Smrgstatic void
3688659607e0SmrgcheckForErraticMovements(InputInfoPtr pInfo, int dx, int dy)
3689659607e0Smrg{
3690659607e0Smrg    MouseDevPtr pMse = pInfo->private;
3691659607e0Smrg    mousePrivPtr mPriv = (mousePrivPtr)pMse->mousePriv;
3692eeaac534Smrg
3693659607e0Smrg    if (!mPriv->goodCount)
36941450c749Smrg        return;
3695eeaac534Smrg
3696659607e0Smrg#if 0
36971450c749Smrg    if (abs(dx - mPriv->prevDx) > 300
36981450c749Smrg        || abs(dy - mPriv->prevDy) > 300)
36991450c749Smrg        AP_DBG(("erratic1 behaviour\n"));
3700659607e0Smrg#endif
3701659607e0Smrg    if (abs(dx) > VAL_THRESHOLD) {
37021450c749Smrg        if (sign(dx) == sign(mPriv->prevDx)) {
37031450c749Smrg            mPriv->accDx += dx;
37041450c749Smrg            if (abs(mPriv->accDx) > mPriv->acc) {
37051450c749Smrg                mPriv->acc = abs(mPriv->accDx);
37061450c749Smrg                AP_DBG(("acc=%i\n",mPriv->acc));
37071450c749Smrg            }
37081450c749Smrg            else
37091450c749Smrg                AP_DBG(("accDx=%i\n",mPriv->accDx));
37101450c749Smrg        } else {
37111450c749Smrg            mPriv->accDx = 0;
37121450c749Smrg        }
3713659607e0Smrg    }
3714659607e0Smrg
3715659607e0Smrg    if (abs(dy) > VAL_THRESHOLD) {
37161450c749Smrg        if (sign(dy) == sign(mPriv->prevDy)) {
37171450c749Smrg            mPriv->accDy += dy;
37181450c749Smrg            if (abs(mPriv->accDy) > mPriv->acc) {
37191450c749Smrg                mPriv->acc = abs(mPriv->accDy);
37201450c749Smrg                AP_DBG(("acc: %i\n",mPriv->acc));
37211450c749Smrg            } else
37221450c749Smrg                AP_DBG(("accDy=%i\n",mPriv->accDy));
37231450c749Smrg        } else {
37241450c749Smrg            mPriv->accDy = 0;
37251450c749Smrg        }
3726659607e0Smrg    }
3727659607e0Smrg    mPriv->prevDx = dx;
3728659607e0Smrg    mPriv->prevDy = dy;
3729659607e0Smrg    if (mPriv->acc > TOT_THRESHOLD) {
37301450c749Smrg        mPriv->goodCount = PROBE_UNCERTAINTY;
37311450c749Smrg        mPriv->prevDx = 0;
37321450c749Smrg        mPriv->prevDy = 0;
37331450c749Smrg        mPriv->accDx = 0;
37341450c749Smrg        mPriv->accDy = 0;
37351450c749Smrg        mPriv->acc = 0;
37361450c749Smrg        AP_DBG(("erratic2 behaviour\n"));
37371450c749Smrg        autoProbeMouse(pInfo, FALSE,TRUE);
3738659607e0Smrg    }
3739659607e0Smrg}
3740659607e0Smrg
3741659607e0Smrgstatic void
3742659607e0SmrgSetMouseProto(MouseDevPtr pMse, MouseProtocolID protocolID)
3743659607e0Smrg{
3744659607e0Smrg    pMse->protocolID = protocolID;
3745659607e0Smrg    pMse->protocol = ProtocolIDToName(pMse->protocolID);
3746659607e0Smrg    pMse->class = ProtocolIDToClass(pMse->protocolID);
3747659607e0Smrg    if ((pMse->protocolID >= 0) && (pMse->protocolID < PROT_NUMPROTOS))
37481450c749Smrg        memcpy(pMse->protoPara, proto[pMse->protocolID],
37491450c749Smrg               sizeof(pMse->protoPara));
37501450c749Smrg
3751659607e0Smrg    if (pMse->emulate3ButtonsSoft)
37521450c749Smrg        pMse->emulate3Buttons = TRUE;
3753659607e0Smrg}
3754659607e0Smrg
3755659607e0Smrg/*
3756659607e0Smrg * collectData() -- collect data bytes sent by mouse.
3757659607e0Smrg */
3758659607e0Smrgstatic Bool
3759659607e0SmrgcollectData(MouseDevPtr pMse, unsigned char u)
3760659607e0Smrg{
3761659607e0Smrg    mousePrivPtr mPriv = (mousePrivPtr)pMse->mousePriv;
3762659607e0Smrg    if (mPriv->count < NUM_MSE_AUTOPROBE_TOTAL) {
37631450c749Smrg        mPriv->data[mPriv->count++] = u;
37641450c749Smrg        if (mPriv->count <= NUM_MSE_AUTOPROBE_BYTES) {
37651450c749Smrg                return TRUE;
37661450c749Smrg        }
3767659607e0Smrg    }
3768659607e0Smrg    return FALSE;
3769659607e0Smrg}
3770659607e0Smrg
3771659607e0Smrg/**************** end of autoprobe stuff *****************/
3772659607e0Smrg
3773659607e0Smrg
3774659607e0Smrgstatic void
37751450c749Smrgxf86MouseUnplug(pointer p)
3776659607e0Smrg{
3777659607e0Smrg}
3778659607e0Smrgstatic pointer
37791450c749Smrgxf86MousePlug(pointer   module,
37801450c749Smrg            pointer     options,
37811450c749Smrg            int         *errmaj,
37821450c749Smrg            int         *errmin)
3783659607e0Smrg{
3784659607e0Smrg    static Bool Initialised = FALSE;
3785659607e0Smrg
3786eeaac534Smrg    if (!Initialised)
37871450c749Smrg        Initialised = TRUE;
3788659607e0Smrg
3789659607e0Smrg    xf86AddInputDriver(&MOUSE, module, 0);
3790659607e0Smrg
3791659607e0Smrg    return module;
3792659607e0Smrg}
3793659607e0Smrg
3794659607e0Smrgstatic XF86ModuleVersionInfo xf86MouseVersionRec =
3795659607e0Smrg{
3796659607e0Smrg    "mouse",
3797659607e0Smrg    MODULEVENDORSTRING,
3798659607e0Smrg    MODINFOSTRING1,
3799659607e0Smrg    MODINFOSTRING2,
3800659607e0Smrg    XORG_VERSION_CURRENT,
3801659607e0Smrg    PACKAGE_VERSION_MAJOR, PACKAGE_VERSION_MINOR, PACKAGE_VERSION_PATCHLEVEL,
3802659607e0Smrg    ABI_CLASS_XINPUT,
3803659607e0Smrg    ABI_XINPUT_VERSION,
3804659607e0Smrg    MOD_CLASS_XINPUT,
38051450c749Smrg    {0, 0, 0, 0}                /* signature, to be patched into the file by */
38061450c749Smrg                                /* a tool */
3807659607e0Smrg};
3808659607e0Smrg
3809659607e0Smrg_X_EXPORT XF86ModuleData mouseModuleData = {
3810659607e0Smrg    &xf86MouseVersionRec,
3811659607e0Smrg    xf86MousePlug,
3812659607e0Smrg    xf86MouseUnplug
3813659607e0Smrg};
3814659607e0Smrg
3815659607e0Smrg/*
3816659607e0Smrg  Look at hitachi device stuff.
3817659607e0Smrg*/
3818