VirtualBox

source: vbox/trunk/src/VBox/Additions/x11/vboxmouse/xorg71/mouse.c@ 29593

Last change on this file since 29593 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 110.0 KB
Line 
1/** @file
2 *
3 */
4
5/*
6 * Copyright (C) 2006-2007 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 * --------------------------------------------------------------------
16 *
17 * This code is based on:
18 *
19 * Copyright 1990,91 by Thomas Roell, Dinkelscherben, Germany.
20 * Copyright 1993 by David Dawes <[email protected]>
21 * Copyright 2002 by SuSE Linux AG, Author: Egbert Eich
22 * Copyright 1994-2002 by The XFree86 Project, Inc.
23 * Copyright 2002 by Paul Elliott
24 *
25 * Permission to use, copy, modify, distribute, and sell this software and its
26 * documentation for any purpose is hereby granted without fee, provided that
27 * the above copyright notice appear in all copies and that both that
28 * copyright notice and this permission notice appear in supporting
29 * documentation, and that the names of copyright holders not be
30 * used in advertising or publicity pertaining to distribution of the
31 * software without specific, written prior permission. The copyright holders
32 * make no representations about the suitability of this
33 * software for any purpose. It is provided "as is" without express or
34 * implied warranty.
35 *
36 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
37 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
38 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
39 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
40 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
41 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
42 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
43 *
44 */
45/* Patch for PS/2 Intellimouse - Tim Goodwin 1997-11-06. */
46
47/*
48 * [JCH-96/01/21] Added fourth button support for PROT_GLIDEPOINT mouse
49 * protocol.
50 */
51
52/*
53 * [TVO-97/03/05] Added microsoft IntelliMouse support
54 */
55
56/*
57 * [PME-02/08/11] Added suport for drag lock buttons
58 * for use with 4 button trackballs for convenience
59 * and to help limited dexterity persons
60 */
61
62#ifdef VBOX
63/* this is necessary to prevent redirecting sscanf to isoc99_sscanf which is
64 * glibc 2.7++ only */
65#define _GNU_SOURCE
66#endif
67
68#ifdef XFree86LOADER
69# include "xorg-server.h"
70#else
71# ifdef HAVE_CONFIG_H
72# include "config.h"
73# endif
74#endif
75
76#include <math.h>
77#include <string.h>
78#include <stdio.h>
79#define NEED_EVENTS
80#include <X11/X.h>
81#include <X11/Xproto.h>
82
83#include "xf86.h"
84
85#ifdef XINPUT
86#include <X11/extensions/XI.h>
87#include <X11/extensions/XIproto.h>
88#include "extnsionst.h"
89#include "extinit.h"
90#else
91#include "inputstr.h"
92#endif
93
94#include "xf86Xinput.h"
95#include "xf86_OSproc.h"
96#include "xf86OSmouse.h"
97#ifndef NEED_XF86_TYPES
98#define NEED_XF86_TYPES /* for xisb.h when !XFree86LOADER */
99#endif
100#include "compiler.h"
101
102#include "xisb.h"
103#include "mouse.h"
104#include "mousePriv.h"
105#include "mipointer.h"
106
107#ifdef VBOX
108#include "VBoxUtils.h"
109#include "version-generated.h"
110#include "product-generated.h"
111/* Xorg 7.1 does not include xf86_ansic.h anymore. Don't reinclude this
112 * file as it renamed ANSI C functions to xf86*. */
113extern int abs(int);
114extern long strtol(const char*,char**,int);
115#endif
116
117enum {
118 /* number of bits in mapped nibble */
119 NIB_BITS=4,
120 /* size of map of nibbles to bitmask */
121 NIB_SIZE= (1 << NIB_BITS),
122 /* mask for map */
123 NIB_MASK= (NIB_SIZE -1),
124 /* number of maps to map all the buttons */
125 NIB_COUNT = ((MSE_MAXBUTTONS+NIB_BITS-1)/NIB_BITS)
126};
127
128/*data to be used in implementing trackball drag locks.*/
129typedef struct _DragLockRec {
130
131 /* Fields used to implement trackball drag locks. */
132 /* mask for those buttons that are ordinary drag lock buttons */
133 int lockButtonsM;
134
135 /* mask for the master drag lock button if any */
136 int masterLockM;
137
138 /* button state up/down from last time adjusted for drag locks */
139 int lockLastButtons;
140
141 /*
142 * true if master lock state i.e. master drag lock
143 * button has just been pressed
144 */
145 int masterTS;
146
147 /* simulate these buttons being down although they are not */
148 int simulatedDown;
149
150 /*
151 * data to map bits for drag lock buttons to corresponding
152 * bits for the target buttons
153 */
154 int nib_table[NIB_COUNT][NIB_SIZE];
155
156} DragLockRec, *DragLockPtr;
157
158
159
160#ifdef XFree86LOADER
161static const OptionInfoRec *MouseAvailableOptions(void *unused);
162#endif
163static InputInfoPtr MousePreInit(InputDriverPtr drv, IDevPtr dev, int flags);
164#if 0
165static void MouseUnInit(InputDriverPtr drv, InputInfoPtr pInfo, int flags);
166#endif
167
168static int MouseProc(DeviceIntPtr device, int what);
169static Bool MouseConvert(LocalDevicePtr local, int first, int num, int v0,
170 int v1, int v2, int v3, int v4, int v5, int *x,
171 int *y);
172
173static void MouseCtrl(DeviceIntPtr device, PtrCtrl *ctrl);
174static void MousePostEvent(InputInfoPtr pInfo, int buttons,
175 int dx, int dy, int dz, int dw);
176static void MouseReadInput(InputInfoPtr pInfo);
177static void MouseBlockHandler(pointer data, struct timeval **waitTime,
178 pointer LastSelectMask);
179static void MouseWakeupHandler(pointer data, int i, pointer LastSelectMask);
180static void FlushButtons(MouseDevPtr pMse);
181
182static Bool SetupMouse(InputInfoPtr pInfo);
183static Bool initMouseHW(InputInfoPtr pInfo);
184#ifdef SUPPORT_MOUSE_RESET
185static Bool mouseReset(InputInfoPtr pInfo, unsigned char val);
186static void ps2WakeupHandler(pointer data, int i, pointer LastSelectMask);
187static void ps2BlockHandler(pointer data, struct timeval **waitTime,
188 pointer LastSelectMask);
189#endif
190
191/* mouse autoprobe stuff */
192static const char *autoOSProtocol(InputInfoPtr pInfo, int *protoPara);
193static void autoProbeMouse(InputInfoPtr pInfo, Bool inSync, Bool lostSync);
194static void checkForErraticMovements(InputInfoPtr pInfo, int dx, int dy);
195static Bool collectData(MouseDevPtr pMse, unsigned char u);
196static void SetMouseProto(MouseDevPtr pMse, MouseProtocolID protocolID);
197static Bool autoGood(MouseDevPtr pMse);
198
199#undef MOUSE
200_X_EXPORT InputDriverRec MOUSE = {
201 1,
202#ifdef VBOX
203 "vboxmouse",
204#else
205 "mouse",
206#endif
207 NULL,
208 MousePreInit,
209 /*MouseUnInit,*/NULL,
210 NULL,
211 0
212};
213
214typedef enum {
215 OPTION_ALWAYS_CORE,
216 OPTION_SEND_CORE_EVENTS,
217 OPTION_CORE_POINTER,
218 OPTION_SEND_DRAG_EVENTS,
219 OPTION_HISTORY_SIZE,
220 OPTION_DEVICE,
221 OPTION_PROTOCOL,
222 OPTION_BUTTONS,
223 OPTION_EMULATE_3_BUTTONS,
224 OPTION_EMULATE_3_TIMEOUT,
225 OPTION_CHORD_MIDDLE,
226 OPTION_FLIP_XY,
227 OPTION_INV_X,
228 OPTION_INV_Y,
229 OPTION_ANGLE_OFFSET,
230 OPTION_Z_AXIS_MAPPING,
231 OPTION_SAMPLE_RATE,
232 OPTION_RESOLUTION,
233 OPTION_EMULATE_WHEEL,
234 OPTION_EMU_WHEEL_BUTTON,
235 OPTION_EMU_WHEEL_INERTIA,
236 OPTION_EMU_WHEEL_TIMEOUT,
237 OPTION_X_AXIS_MAPPING,
238 OPTION_Y_AXIS_MAPPING,
239 OPTION_AUTO_SOFT,
240 OPTION_CLEAR_DTR,
241 OPTION_CLEAR_RTS,
242 OPTION_BAUD_RATE,
243 OPTION_DATA_BITS,
244 OPTION_STOP_BITS,
245 OPTION_PARITY,
246 OPTION_FLOW_CONTROL,
247 OPTION_VTIME,
248 OPTION_VMIN,
249 OPTION_DRAGLOCKBUTTONS,
250 OPTION_DOUBLECLICK_BUTTONS,
251 OPTION_BUTTON_MAPPING
252} MouseOpts;
253
254#ifdef XFree86LOADER
255static const OptionInfoRec mouseOptions[] = {
256 { OPTION_ALWAYS_CORE, "AlwaysCore", OPTV_BOOLEAN, {0}, FALSE },
257 { OPTION_SEND_CORE_EVENTS, "SendCoreEvents", OPTV_BOOLEAN, {0}, FALSE },
258 { OPTION_CORE_POINTER, "CorePointer", OPTV_BOOLEAN, {0}, FALSE },
259 { OPTION_SEND_DRAG_EVENTS, "SendDragEvents", OPTV_BOOLEAN, {0}, FALSE },
260 { OPTION_HISTORY_SIZE, "HistorySize", OPTV_INTEGER, {0}, FALSE },
261 { OPTION_DEVICE, "Device", OPTV_STRING, {0}, FALSE },
262 { OPTION_PROTOCOL, "Protocol", OPTV_STRING, {0}, FALSE },
263 { OPTION_BUTTONS, "Buttons", OPTV_INTEGER, {0}, FALSE },
264 { OPTION_EMULATE_3_BUTTONS, "Emulate3Buttons",OPTV_BOOLEAN, {0}, FALSE },
265 { OPTION_EMULATE_3_TIMEOUT, "Emulate3Timeout",OPTV_INTEGER, {0}, FALSE },
266 { OPTION_CHORD_MIDDLE, "ChordMiddle", OPTV_BOOLEAN, {0}, FALSE },
267 { OPTION_FLIP_XY, "FlipXY", OPTV_BOOLEAN, {0}, FALSE },
268 { OPTION_INV_X, "InvX", OPTV_BOOLEAN, {0}, FALSE },
269 { OPTION_INV_Y, "InvY", OPTV_BOOLEAN, {0}, FALSE },
270 { OPTION_ANGLE_OFFSET, "AngleOffset", OPTV_INTEGER, {0}, FALSE },
271 { OPTION_Z_AXIS_MAPPING, "ZAxisMapping", OPTV_STRING, {0}, FALSE },
272 { OPTION_SAMPLE_RATE, "SampleRate", OPTV_INTEGER, {0}, FALSE },
273 { OPTION_RESOLUTION, "Resolution", OPTV_INTEGER, {0}, FALSE },
274 { OPTION_EMULATE_WHEEL, "EmulateWheel", OPTV_BOOLEAN, {0}, FALSE },
275 { OPTION_EMU_WHEEL_BUTTON, "EmulateWheelButton", OPTV_INTEGER, {0}, FALSE },
276 { OPTION_EMU_WHEEL_INERTIA, "EmulateWheelInertia", OPTV_INTEGER, {0}, FALSE },
277 { OPTION_EMU_WHEEL_TIMEOUT, "EmulateWheelTimeout", OPTV_INTEGER, {0}, FALSE },
278 { OPTION_X_AXIS_MAPPING, "XAxisMapping", OPTV_STRING, {0}, FALSE },
279 { OPTION_Y_AXIS_MAPPING, "YAxisMapping", OPTV_STRING, {0}, FALSE },
280 { OPTION_AUTO_SOFT, "AutoSoft", OPTV_BOOLEAN, {0}, FALSE },
281 /* serial options */
282 { OPTION_CLEAR_DTR, "ClearDTR", OPTV_BOOLEAN, {0}, FALSE },
283 { OPTION_CLEAR_RTS, "ClearRTS", OPTV_BOOLEAN, {0}, FALSE },
284 { OPTION_BAUD_RATE, "BaudRate", OPTV_INTEGER, {0}, FALSE },
285 { OPTION_DATA_BITS, "DataBits", OPTV_INTEGER, {0}, FALSE },
286 { OPTION_STOP_BITS, "StopBits", OPTV_INTEGER, {0}, FALSE },
287 { OPTION_PARITY, "Parity", OPTV_STRING, {0}, FALSE },
288 { OPTION_FLOW_CONTROL, "FlowControl", OPTV_STRING, {0}, FALSE },
289 { OPTION_VTIME, "VTime", OPTV_INTEGER, {0}, FALSE },
290 { OPTION_VMIN, "VMin", OPTV_INTEGER, {0}, FALSE },
291 /* end serial options */
292 { OPTION_DRAGLOCKBUTTONS, "DragLockButtons",OPTV_STRING, {0}, FALSE },
293 { OPTION_DOUBLECLICK_BUTTONS,"DoubleClickButtons", OPTV_STRING, {0}, FALSE },
294 { OPTION_BUTTON_MAPPING, "ButtonMapping", OPTV_STRING, {0}, FALSE },
295 { -1, NULL, OPTV_NONE, {0}, FALSE }
296};
297#endif
298
299#define RETRY_COUNT 4
300
301/*
302 * Microsoft (all serial models), Logitech MouseMan, First Mouse, etc,
303 * ALPS GlidePoint, Thinking Mouse.
304 */
305static const char *msDefaults[] = {
306 "BaudRate", "1200",
307 "DataBits", "7",
308 "StopBits", "1",
309 "Parity", "None",
310 "FlowControl", "None",
311 "VTime", "0",
312 "VMin", "1",
313 NULL
314};
315/* MouseSystems */
316static const char *mlDefaults[] = {
317 "BaudRate", "1200",
318 "DataBits", "8",
319 "StopBits", "2",
320 "Parity", "None",
321 "FlowControl", "None",
322 "VTime", "0",
323 "VMin", "1",
324 NULL
325};
326/* MMSeries */
327static const char *mmDefaults[] = {
328 "BaudRate", "1200",
329 "DataBits", "8",
330 "StopBits", "1",
331 "Parity", "Odd",
332 "FlowControl", "None",
333 "VTime", "0",
334 "VMin", "1",
335 NULL
336};
337#if 0
338/* Logitech series 9 *//* same as msc: now mlDefaults */
339static const char *logiDefaults[] = {
340 "BaudRate", "1200",
341 "DataBits", "8",
342 "StopBits", "2",
343 "Parity", "None",
344 "FlowControl", "None",
345 "VTime", "0",
346 "VMin", "1",
347 NULL
348};
349#endif
350/* Hitachi Tablet */
351static const char *mmhitDefaults[] = {
352 "BaudRate", "1200",
353 "DataBits", "8",
354 "StopBits", "1",
355 "Parity", "None",
356 "FlowControl", "None",
357 "VTime", "0",
358 "VMin", "1",
359 NULL
360};
361/* AceCad Tablet */
362static const char *acecadDefaults[] = {
363 "BaudRate", "9600",
364 "DataBits", "8",
365 "StopBits", "1",
366 "Parity", "Odd",
367 "FlowControl", "None",
368 "VTime", "0",
369 "VMin", "1",
370 NULL
371};
372
373static MouseProtocolRec mouseProtocols[] = {
374
375 /* Serial protocols */
376 { "Microsoft", MSE_SERIAL, msDefaults, PROT_MS },
377 { "MouseSystems", MSE_SERIAL, mlDefaults, PROT_MSC },
378 { "MMSeries", MSE_SERIAL, mmDefaults, PROT_MM },
379 { "Logitech", MSE_SERIAL, mlDefaults, PROT_LOGI },
380 { "MouseMan", MSE_SERIAL, msDefaults, PROT_LOGIMAN },
381 { "MMHitTab", MSE_SERIAL, mmhitDefaults, PROT_MMHIT },
382 { "GlidePoint", MSE_SERIAL, msDefaults, PROT_GLIDE },
383 { "IntelliMouse", MSE_SERIAL, msDefaults, PROT_IMSERIAL },
384 { "ThinkingMouse", MSE_SERIAL, msDefaults, PROT_THINKING },
385 { "AceCad", MSE_SERIAL, acecadDefaults, PROT_ACECAD },
386 { "ValuMouseScroll", MSE_SERIAL, msDefaults, PROT_VALUMOUSESCROLL },
387
388 /* Standard PS/2 */
389 { "PS/2", MSE_PS2, NULL, PROT_PS2 },
390 { "GenericPS/2", MSE_PS2, NULL, PROT_GENPS2 },
391
392 /* Extended PS/2 */
393 { "ImPS/2", MSE_XPS2, NULL, PROT_IMPS2 },
394 { "ExplorerPS/2", MSE_XPS2, NULL, PROT_EXPPS2 },
395 { "ThinkingMousePS/2", MSE_XPS2, NULL, PROT_THINKPS2 },
396 { "MouseManPlusPS/2", MSE_XPS2, NULL, PROT_MMPS2 },
397 { "GlidePointPS/2", MSE_XPS2, NULL, PROT_GLIDEPS2 },
398 { "NetMousePS/2", MSE_XPS2, NULL, PROT_NETPS2 },
399 { "NetScrollPS/2", MSE_XPS2, NULL, PROT_NETSCPS2 },
400
401 /* Bus Mouse */
402 { "BusMouse", MSE_BUS, NULL, PROT_BM },
403
404 /* Auto-detect (PnP) */
405 { "Auto", MSE_AUTO, NULL, PROT_AUTO },
406
407 /* Misc (usually OS-specific) */
408 { "SysMouse", MSE_MISC, mlDefaults, PROT_SYSMOUSE },
409
410 /* end of list */
411 { NULL, MSE_NONE, NULL, PROT_UNKNOWN }
412};
413
414#ifdef XFree86LOADER
415/*ARGSUSED*/
416static const OptionInfoRec *
417MouseAvailableOptions(void *unused)
418{
419 return (mouseOptions);
420}
421#endif
422
423/* Process options common to all mouse types. */
424static void
425MouseCommonOptions(InputInfoPtr pInfo)
426{
427 MouseDevPtr pMse;
428 MessageType buttons_from = X_CONFIG;
429 char *s;
430 int origButtons;
431 int i;
432
433 pMse = pInfo->private;
434
435 pMse->buttons = xf86SetIntOption(pInfo->options, "Buttons", 0);
436 if (!pMse->buttons) {
437 pMse->buttons = MSE_DFLTBUTTONS;
438 buttons_from = X_DEFAULT;
439 }
440 origButtons = pMse->buttons;
441
442 pMse->emulate3Buttons = xf86SetBoolOption(pInfo->options,
443 "Emulate3Buttons", FALSE);
444 if (!xf86FindOptionValue(pInfo->options,"Emulate3Buttons")) {
445 pMse->emulate3ButtonsSoft = TRUE;
446 pMse->emulate3Buttons = TRUE;
447 }
448
449 pMse->emulate3Timeout = xf86SetIntOption(pInfo->options,
450 "Emulate3Timeout", 50);
451 if (pMse->emulate3Buttons || pMse->emulate3ButtonsSoft) {
452 MessageType from = X_CONFIG;
453 if (pMse->emulate3ButtonsSoft)
454 from = X_DEFAULT;
455 xf86Msg(from, "%s: Emulate3Buttons, Emulate3Timeout: %d\n",
456 pInfo->name, pMse->emulate3Timeout);
457 }
458
459 pMse->chordMiddle = xf86SetBoolOption(pInfo->options, "ChordMiddle", FALSE);
460 if (pMse->chordMiddle)
461 xf86Msg(X_CONFIG, "%s: ChordMiddle\n", pInfo->name);
462 pMse->flipXY = xf86SetBoolOption(pInfo->options, "FlipXY", FALSE);
463 if (pMse->flipXY)
464 xf86Msg(X_CONFIG, "%s: FlipXY\n", pInfo->name);
465 if (xf86SetBoolOption(pInfo->options, "InvX", FALSE)) {
466 pMse->invX = -1;
467 xf86Msg(X_CONFIG, "%s: InvX\n", pInfo->name);
468 } else
469 pMse->invX = 1;
470 if (xf86SetBoolOption(pInfo->options, "InvY", FALSE)) {
471 pMse->invY = -1;
472 xf86Msg(X_CONFIG, "%s: InvY\n", pInfo->name);
473 } else
474 pMse->invY = 1;
475 pMse->angleOffset = xf86SetIntOption(pInfo->options, "AngleOffset", 0);
476
477
478 if (pMse->pDragLock)
479 xfree(pMse->pDragLock);
480 pMse->pDragLock = NULL;
481
482 s = xf86SetStrOption(pInfo->options, "DragLockButtons", NULL);
483
484 if (s) {
485 int lock; /* lock button */
486 int target; /* target button */
487 int lockM,targetM; /* bitmasks for drag lock, target */
488 int i, j; /* indexes */
489 char *s1; /* parse input string */
490 DragLockPtr pLock;
491
492 pLock = pMse->pDragLock = xcalloc(1, sizeof(DragLockRec));
493 /* init code */
494
495 /* initial string to be taken apart */
496 s1 = s;
497
498 /* keep getting numbers which are buttons */
499 while ((s1 != NULL) && (lock = strtol(s1, &s1, 10)) != 0) {
500
501 /* check sanity for a button */
502 if ((lock < 0) || (lock > MSE_MAXBUTTONS)) {
503 xf86Msg(X_WARNING, "DragLock: Invalid button number = %d\n",
504 lock);
505 break;
506 };
507 /* turn into a button mask */
508 lockM = 1 << (lock - 1);
509
510 /* try to get drag lock button */
511 if ((s1 == NULL) || ((target=strtol(s1, &s1, 10)) == 0)) {
512 /*if no target, must be a master drag lock button */
513 /* save master drag lock mask */
514 pLock->masterLockM = lockM;
515 xf86Msg(X_CONFIG,
516 "DragLock button %d is master drag lock",
517 lock);
518 } else {
519 /* have target button number*/
520 /* check target button number for sanity */
521 if ((target < 0) || (target > MSE_MAXBUTTONS)) {
522 xf86Msg(X_WARNING,
523 "DragLock: Invalid button number for target=%d\n",
524 target);
525 break;
526 }
527
528 /* target button mask */
529 targetM = 1 << (target - 1);
530
531 xf86Msg(X_CONFIG,
532 "DragLock: button %d is drag lock for button %d\n",
533 lock,target);
534 lock--;
535
536 /* initialize table that maps drag lock mask to target mask */
537 pLock->nib_table[lock / NIB_BITS][1 << (lock % NIB_BITS)] =
538 targetM;
539
540 /* add new drag lock to mask of drag locks */
541 pLock->lockButtonsM |= lockM;
542 }
543
544 }
545
546 /*
547 * fill out rest of map that maps sets of drag lock buttons
548 * to sets of target buttons, in the form of masks
549 */
550
551 /* for each nibble */
552 for (i = 0; i < NIB_COUNT; i++) {
553 /* for each possible set of bits for that nibble */
554 for (j = 0; j < NIB_SIZE; j++) {
555 int ff, fM, otherbits;
556
557 /* get first bit set in j*/
558 ff = ffs(j) - 1;
559 /* if 0 bits set nothing to do */
560 if (ff >= 0) {
561 /* form mask for fist bit set */
562 fM = 1 << ff;
563 /* mask off first bit set to get remaining bits set*/
564 otherbits = j & ~fM;
565 /*
566 * if otherbits =0 then only 1 bit set
567 * so j=fM
568 * nib_table[i][fM] already calculated if fM has
569 * only 1 bit set.
570 * nib_table[i][j] has already been filled in
571 * by previous loop. otherwise
572 * otherbits < j so nibtable[i][otherbits]
573 * has already been calculated.
574 */
575 if (otherbits)
576 pLock->nib_table[i][j] =
577 pLock->nib_table[i][fM] |
578 pLock->nib_table[i][otherbits];
579
580 }
581 }
582 }
583 xfree(s);
584 }
585
586 s = xf86SetStrOption(pInfo->options, "ZAxisMapping", "4 5 6 7");
587 if (s) {
588 int b1 = 0, b2 = 0, b3 = 0, b4 = 0;
589 char *msg = NULL;
590
591 if (!xf86NameCmp(s, "x")) {
592 pMse->negativeZ = pMse->positiveZ = MSE_MAPTOX;
593 pMse->negativeW = pMse->positiveW = MSE_MAPTOX;
594 msg = xstrdup("X axis");
595 } else if (!xf86NameCmp(s, "y")) {
596 pMse->negativeZ = pMse->positiveZ = MSE_MAPTOY;
597 pMse->negativeW = pMse->positiveW = MSE_MAPTOY;
598 msg = xstrdup("Y axis");
599 } else if (sscanf(s, "%d %d %d %d", &b1, &b2, &b3, &b4) >= 2 &&
600 b1 > 0 && b1 <= MSE_MAXBUTTONS &&
601 b2 > 0 && b2 <= MSE_MAXBUTTONS) {
602 msg = xstrdup("buttons XX and YY");
603 if (msg)
604 sprintf(msg, "buttons %d and %d", b1, b2);
605 pMse->negativeZ = pMse->negativeW = 1 << (b1-1);
606 pMse->positiveZ = pMse->positiveW = 1 << (b2-1);
607 if (b3 > 0 && b3 <= MSE_MAXBUTTONS &&
608 b4 > 0 && b4 <= MSE_MAXBUTTONS) {
609 if (msg)
610 xfree(msg);
611 msg = xstrdup("buttons XX, YY, ZZ and WW");
612 if (msg)
613 sprintf(msg, "buttons %d, %d, %d and %d", b1, b2, b3, b4);
614 pMse->negativeW = 1 << (b3-1);
615 pMse->positiveW = 1 << (b4-1);
616 }
617 if (b1 > pMse->buttons) pMse->buttons = b1;
618 if (b2 > pMse->buttons) pMse->buttons = b2;
619 if (b3 > pMse->buttons) pMse->buttons = b3;
620 if (b4 > pMse->buttons) pMse->buttons = b4;
621 } else {
622 pMse->negativeZ = pMse->positiveZ = MSE_NOZMAP;
623 pMse->negativeW = pMse->positiveW = MSE_NOZMAP;
624 }
625 if (msg) {
626 xf86Msg(X_CONFIG, "%s: ZAxisMapping: %s\n", pInfo->name, msg);
627 xfree(msg);
628 } else {
629 xf86Msg(X_WARNING, "%s: Invalid ZAxisMapping value: \"%s\"\n",
630 pInfo->name, s);
631 }
632 xfree(s);
633 }
634 if (xf86SetBoolOption(pInfo->options, "EmulateWheel", FALSE)) {
635 Bool yFromConfig = FALSE;
636 int wheelButton;
637
638 pMse->emulateWheel = TRUE;
639 wheelButton = xf86SetIntOption(pInfo->options,
640 "EmulateWheelButton", 4);
641 if (wheelButton < 0 || wheelButton > MSE_MAXBUTTONS) {
642 xf86Msg(X_WARNING, "%s: Invalid EmulateWheelButton value: %d\n",
643 pInfo->name, wheelButton);
644 wheelButton = 4;
645 }
646 pMse->wheelButton = wheelButton;
647
648 pMse->wheelInertia = xf86SetIntOption(pInfo->options,
649 "EmulateWheelInertia", 10);
650 if (pMse->wheelInertia <= 0) {
651 xf86Msg(X_WARNING, "%s: Invalid EmulateWheelInertia value: %d\n",
652 pInfo->name, pMse->wheelInertia);
653 pMse->wheelInertia = 10;
654 }
655 pMse->wheelButtonTimeout = xf86SetIntOption(pInfo->options,
656 "EmulateWheelTimeout", 200);
657 if (pMse->wheelButtonTimeout <= 0) {
658 xf86Msg(X_WARNING, "%s: Invalid EmulateWheelTimeout value: %d\n",
659 pInfo->name, pMse->wheelButtonTimeout);
660 pMse->wheelButtonTimeout = 200;
661 }
662
663 pMse->negativeX = MSE_NOAXISMAP;
664 pMse->positiveX = MSE_NOAXISMAP;
665 s = xf86SetStrOption(pInfo->options, "XAxisMapping", NULL);
666 if (s) {
667 int b1 = 0, b2 = 0;
668 char *msg = NULL;
669
670 if ((sscanf(s, "%d %d", &b1, &b2) == 2) &&
671 b1 > 0 && b1 <= MSE_MAXBUTTONS &&
672 b2 > 0 && b2 <= MSE_MAXBUTTONS) {
673 msg = xstrdup("buttons XX and YY");
674 if (msg)
675 sprintf(msg, "buttons %d and %d", b1, b2);
676 pMse->negativeX = b1;
677 pMse->positiveX = b2;
678 if (b1 > pMse->buttons) pMse->buttons = b1;
679 if (b2 > pMse->buttons) pMse->buttons = b2;
680 } else {
681 xf86Msg(X_WARNING, "%s: Invalid XAxisMapping value: \"%s\"\n",
682 pInfo->name, s);
683 }
684 if (msg) {
685 xf86Msg(X_CONFIG, "%s: XAxisMapping: %s\n", pInfo->name, msg);
686 xfree(msg);
687 }
688 xfree(s);
689 }
690 s = xf86SetStrOption(pInfo->options, "YAxisMapping", NULL);
691 if (s) {
692 int b1 = 0, b2 = 0;
693 char *msg = NULL;
694
695 if ((sscanf(s, "%d %d", &b1, &b2) == 2) &&
696 b1 > 0 && b1 <= MSE_MAXBUTTONS &&
697 b2 > 0 && b2 <= MSE_MAXBUTTONS) {
698 msg = xstrdup("buttons XX and YY");
699 if (msg)
700 sprintf(msg, "buttons %d and %d", b1, b2);
701 pMse->negativeY = b1;
702 pMse->positiveY = b2;
703 if (b1 > pMse->buttons) pMse->buttons = b1;
704 if (b2 > pMse->buttons) pMse->buttons = b2;
705 yFromConfig = TRUE;
706 } else {
707 xf86Msg(X_WARNING, "%s: Invalid YAxisMapping value: \"%s\"\n",
708 pInfo->name, s);
709 }
710 if (msg) {
711 xf86Msg(X_CONFIG, "%s: YAxisMapping: %s\n", pInfo->name, msg);
712 xfree(msg);
713 }
714 xfree(s);
715 }
716 if (!yFromConfig) {
717 pMse->negativeY = 4;
718 pMse->positiveY = 5;
719 if (pMse->negativeY > pMse->buttons)
720 pMse->buttons = pMse->negativeY;
721 if (pMse->positiveY > pMse->buttons)
722 pMse->buttons = pMse->positiveY;
723 xf86Msg(X_DEFAULT, "%s: YAxisMapping: buttons %d and %d\n",
724 pInfo->name, pMse->negativeY, pMse->positiveY);
725 }
726 xf86Msg(X_CONFIG, "%s: EmulateWheel, EmulateWheelButton: %d, "
727 "EmulateWheelInertia: %d, "
728 "EmulateWheelTimeout: %d\n",
729 pInfo->name, wheelButton, pMse->wheelInertia,
730 pMse->wheelButtonTimeout);
731 }
732 s = xf86SetStrOption(pInfo->options, "ButtonMapping", NULL);
733 if (s) {
734 int b, n = 0;
735 char *s1 = s;
736 /* keep getting numbers which are buttons */
737 while (s1 && n < MSE_MAXBUTTONS && (b = strtol(s1, &s1, 10)) != 0) {
738 /* check sanity for a button */
739 if (b < 0 || b > MSE_MAXBUTTONS) {
740 xf86Msg(X_WARNING,
741 "ButtonMapping: Invalid button number = %d\n", b);
742 break;
743 };
744 pMse->buttonMap[n++] = 1 << (b-1);
745 if (b > pMse->buttons) pMse->buttons = b;
746 }
747 xfree(s);
748 }
749 /* get maximum of mapped buttons */
750 for (i = pMse->buttons-1; i >= 0; i--) {
751 int f = ffs (pMse->buttonMap[i]);
752 if (f > pMse->buttons)
753 pMse->buttons = f;
754 }
755 if (origButtons != pMse->buttons)
756 buttons_from = X_CONFIG;
757 xf86Msg(buttons_from, "%s: Buttons: %d\n", pInfo->name, pMse->buttons);
758
759 pMse->doubleClickSourceButtonMask = 0;
760 pMse->doubleClickTargetButtonMask = 0;
761 pMse->doubleClickTargetButton = 0;
762 s = xf86SetStrOption(pInfo->options, "DoubleClickButtons", NULL);
763 if (s) {
764 int b1 = 0, b2 = 0;
765 char *msg = NULL;
766
767 if ((sscanf(s, "%d %d", &b1, &b2) == 2) &&
768 (b1 > 0) && (b1 <= MSE_MAXBUTTONS) && (b2 > 0) && (b2 <= MSE_MAXBUTTONS)) {
769 msg = xstrdup("buttons XX and YY");
770 if (msg)
771 sprintf(msg, "buttons %d and %d", b1, b2);
772 pMse->doubleClickTargetButton = b1;
773 pMse->doubleClickTargetButtonMask = 1 << (b1 - 1);
774 pMse->doubleClickSourceButtonMask = 1 << (b2 - 1);
775 if (b1 > pMse->buttons) pMse->buttons = b1;
776 if (b2 > pMse->buttons) pMse->buttons = b2;
777 } else {
778 xf86Msg(X_WARNING, "%s: Invalid DoubleClickButtons value: \"%s\"\n",
779 pInfo->name, s);
780 }
781 if (msg) {
782 xf86Msg(X_CONFIG, "%s: DoubleClickButtons: %s\n", pInfo->name, msg);
783 xfree(msg);
784 }
785 }
786}
787/*
788 * map bits corresponding to lock buttons.
789 * for each bit for a lock button,
790 * turn on bit corresponding to button button that the lock
791 * button services.
792 */
793
794static int
795lock2targetMap(DragLockPtr pLock, int lockMask)
796{
797 int result,i;
798 result = 0;
799
800 /*
801 * for each nibble group of bits, use
802 * map for that group to get corresponding
803 * bits, turn them on.
804 * if 4 or less buttons only first map will
805 * need to be used.
806 */
807 for (i = 0; (i < NIB_COUNT) && lockMask; i++) {
808 result |= pLock->nib_table[i][lockMask& NIB_MASK];
809
810 lockMask &= ~NIB_MASK;
811 lockMask >>= NIB_BITS;
812 }
813 return result;
814}
815
816static void
817MouseHWOptions(InputInfoPtr pInfo)
818{
819 MouseDevPtr pMse = pInfo->private;
820 mousePrivPtr mPriv = (mousePrivPtr)pMse->mousePriv;
821
822 if (mPriv == NULL)
823 return;
824
825 if ((mPriv->soft
826 = xf86SetBoolOption(pInfo->options, "AutoSoft", FALSE))) {
827 xf86Msg(X_CONFIG, "Don't initialize mouse when auto-probing\n");
828 }
829 pMse->sampleRate = xf86SetIntOption(pInfo->options, "SampleRate", 0);
830 if (pMse->sampleRate) {
831 xf86Msg(X_CONFIG, "%s: SampleRate: %d\n", pInfo->name,
832 pMse->sampleRate);
833 }
834 pMse->resolution = xf86SetIntOption(pInfo->options, "Resolution", 0);
835 if (pMse->resolution) {
836 xf86Msg(X_CONFIG, "%s: Resolution: %d\n", pInfo->name,
837 pMse->resolution);
838 }
839}
840
841static void
842MouseSerialOptions(InputInfoPtr pInfo)
843{
844 MouseDevPtr pMse = pInfo->private;
845 Bool clearDTR, clearRTS;
846
847
848 pMse->baudRate = xf86SetIntOption(pInfo->options, "BaudRate", 0);
849 if (pMse->baudRate) {
850 xf86Msg(X_CONFIG, "%s: BaudRate: %d\n", pInfo->name,
851 pMse->baudRate);
852 }
853
854 if ((clearDTR = xf86SetBoolOption(pInfo->options, "ClearDTR",FALSE)))
855 pMse->mouseFlags |= MF_CLEAR_DTR;
856
857
858 if ((clearRTS = xf86SetBoolOption(pInfo->options, "ClearRTS",FALSE)))
859 pMse->mouseFlags |= MF_CLEAR_RTS;
860
861 if (clearDTR || clearRTS) {
862 xf86Msg(X_CONFIG, "%s: ", pInfo->name);
863 if (clearDTR) {
864 xf86ErrorF("ClearDTR");
865 if (clearRTS)
866 xf86ErrorF(", ");
867 }
868 if (clearRTS) {
869 xf86ErrorF("ClearRTS");
870 }
871 xf86ErrorF("\n");
872 }
873}
874
875static MouseProtocolID
876ProtocolNameToID(const char *name)
877{
878 int i;
879
880 for (i = 0; mouseProtocols[i].name; i++)
881 if (xf86NameCmp(name, mouseProtocols[i].name) == 0)
882 return mouseProtocols[i].id;
883 return PROT_UNKNOWN;
884}
885
886static const char *
887ProtocolIDToName(MouseProtocolID id)
888{
889 int i;
890
891 switch (id) {
892 case PROT_UNKNOWN:
893 return "Unknown";
894 break;
895 case PROT_UNSUP:
896 return "Unsupported";
897 break;
898 default:
899 for (i = 0; mouseProtocols[i].name; i++)
900 if (id == mouseProtocols[i].id)
901 return mouseProtocols[i].name;
902 return "Invalid";
903 }
904}
905
906const char *
907xf86MouseProtocolIDToName(MouseProtocolID id)
908{
909 return ProtocolIDToName(id);
910}
911
912MouseProtocolID
913xf86MouseProtocolNameToID(const char *name)
914{
915 return ProtocolNameToID(name);
916}
917
918static int
919ProtocolIDToClass(MouseProtocolID id)
920{
921 int i;
922
923 switch (id) {
924 case PROT_UNKNOWN:
925 case PROT_UNSUP:
926 return MSE_NONE;
927 break;
928 default:
929 for (i = 0; mouseProtocols[i].name; i++)
930 if (id == mouseProtocols[i].id)
931 return mouseProtocols[i].class;
932 return MSE_NONE;
933 }
934}
935
936static MouseProtocolPtr
937GetProtocol(MouseProtocolID id) {
938 int i;
939
940 switch (id) {
941 case PROT_UNKNOWN:
942 case PROT_UNSUP:
943 return NULL;
944 break;
945 default:
946 for (i = 0; mouseProtocols[i].name; i++)
947 if (id == mouseProtocols[i].id) {
948 return &mouseProtocols[i];
949 }
950 return NULL;
951 }
952}
953
954static OSMouseInfoPtr osInfo = NULL;
955
956static Bool
957InitProtocols(void)
958{
959 int classes;
960 int i;
961 const char *osname = NULL;
962
963 if (osInfo)
964 return TRUE;
965
966 osInfo = xf86OSMouseInit(0);
967 if (!osInfo)
968 return FALSE;
969 if (!osInfo->SupportedInterfaces)
970 return FALSE;
971
972 classes = osInfo->SupportedInterfaces();
973 if (!classes)
974 return FALSE;
975
976 /* Mark unsupported interface classes. */
977 for (i = 0; mouseProtocols[i].name; i++)
978 if (!(mouseProtocols[i].class & classes))
979 mouseProtocols[i].id = PROT_UNSUP;
980
981 for (i = 0; mouseProtocols[i].name; i++)
982 if (mouseProtocols[i].class & MSE_MISC)
983 if (!osInfo->CheckProtocol ||
984 !osInfo->CheckProtocol(mouseProtocols[i].name))
985 mouseProtocols[i].id = PROT_UNSUP;
986
987 /* NetBSD uses PROT_BM for "PS/2". */
988 xf86GetOS(&osname, NULL, NULL, NULL);
989 if (osname && xf86NameCmp(osname, "netbsd") == 0)
990 for (i = 0; mouseProtocols[i].name; i++)
991 if (mouseProtocols[i].id == PROT_PS2)
992 mouseProtocols[i].id = PROT_BM;
993
994 return TRUE;
995}
996
997static InputInfoPtr
998MousePreInit(InputDriverPtr drv, IDevPtr dev, int flags)
999{
1000 InputInfoPtr pInfo;
1001 MouseDevPtr pMse;
1002 mousePrivPtr mPriv;
1003 MessageType protocolFrom = X_DEFAULT, deviceFrom = X_CONFIG;
1004 const char *protocol, *osProt = NULL;
1005 const char *device;
1006 MouseProtocolID protocolID;
1007 MouseProtocolPtr pProto;
1008 Bool detected;
1009 int i;
1010
1011#ifdef VBOX
1012 xf86Msg(X_INFO,
1013 "VirtualBox guest additions mouse driver version "
1014 VBOX_VERSION_STRING "\n");
1015#endif
1016
1017 if (!InitProtocols())
1018 return NULL;
1019
1020 if (!(pInfo = xf86AllocateInput(drv, 0)))
1021 return NULL;
1022
1023 /* Initialise the InputInfoRec. */
1024 pInfo->name = dev->identifier;
1025 pInfo->type_name = XI_MOUSE;
1026 pInfo->flags = XI86_POINTER_CAPABLE | XI86_SEND_DRAG_EVENTS;
1027 pInfo->device_control = MouseProc;
1028 pInfo->read_input = MouseReadInput;
1029 pInfo->motion_history_proc = xf86GetMotionEvents;
1030 pInfo->history_size = 0;
1031 pInfo->control_proc = NULL;
1032 pInfo->close_proc = NULL;
1033 pInfo->switch_mode = NULL;
1034 pInfo->conversion_proc = MouseConvert;
1035 pInfo->reverse_conversion_proc = NULL;
1036 pInfo->fd = -1;
1037 pInfo->dev = NULL;
1038 pInfo->private_flags = 0;
1039 pInfo->always_core_feedback = 0;
1040 pInfo->conf_idev = dev;
1041
1042 /* Check if SendDragEvents has been disabled. */
1043 if (!xf86SetBoolOption(dev->commonOptions, "SendDragEvents", TRUE)) {
1044 pInfo->flags &= ~XI86_SEND_DRAG_EVENTS;
1045 }
1046
1047 /* Allocate the MouseDevRec and initialise it. */
1048 /*
1049 * XXX This should be done by a function in the core server since the
1050 * MouseDevRec is defined in the os-support layer.
1051 */
1052 if (!(pMse = xcalloc(sizeof(MouseDevRec), 1)))
1053 return pInfo;
1054 pInfo->private = pMse;
1055 pMse->Ctrl = MouseCtrl;
1056 pMse->PostEvent = MousePostEvent;
1057 pMse->CommonOptions = MouseCommonOptions;
1058
1059#ifdef VBOX
1060 protocol = "ImPS/2";
1061 protocolFrom = X_CONFIG;
1062#else
1063 /* Find the protocol type. */
1064 protocol = xf86SetStrOption(dev->commonOptions, "Protocol", NULL);
1065 if (protocol) {
1066 protocolFrom = X_CONFIG;
1067 } else if (osInfo->DefaultProtocol) {
1068 protocol = osInfo->DefaultProtocol();
1069 protocolFrom = X_DEFAULT;
1070 }
1071 if (!protocol) {
1072 xf86Msg(X_ERROR, "%s: No Protocol specified\n", pInfo->name);
1073 return pInfo;
1074 }
1075#endif
1076
1077 /* Default Mapping: 1 2 3 8 9 10 11 ... */
1078 for (i = 0; i < MSE_MAXBUTTONS; i++)
1079 pMse->buttonMap[i] = 1 << (i > 2 && i < MSE_MAXBUTTONS-4 ? i+4 : i);
1080
1081 protocolID = ProtocolNameToID(protocol);
1082 do {
1083 detected = TRUE;
1084 switch (protocolID) {
1085 case PROT_AUTO:
1086 if (osInfo->SetupAuto) {
1087 if ((osProt = osInfo->SetupAuto(pInfo,NULL))) {
1088 MouseProtocolID id = ProtocolNameToID(osProt);
1089 if (id == PROT_UNKNOWN || id == PROT_UNSUP) {
1090 protocolID = id;
1091 protocol = osProt;
1092 detected = FALSE;
1093 }
1094 }
1095 }
1096 break;
1097 case PROT_UNKNOWN:
1098 /* Check for a builtin OS-specific protocol,
1099 * and call its PreInit. */
1100 if (osInfo->CheckProtocol
1101 && osInfo->CheckProtocol(protocol)) {
1102 if (!xf86CheckStrOption(dev->commonOptions, "Device", NULL) &&
1103 HAVE_FIND_DEVICE && osInfo->FindDevice) {
1104 xf86Msg(X_WARNING, "%s: No Device specified, "
1105 "looking for one...\n", pInfo->name);
1106 if (!osInfo->FindDevice(pInfo, protocol, 0)) {
1107 xf86Msg(X_ERROR, "%s: Cannot find which device "
1108 "to use.\n", pInfo->name);
1109 } else
1110 deviceFrom = X_PROBED;
1111 }
1112 if (osInfo->PreInit) {
1113 osInfo->PreInit(pInfo, protocol, 0);
1114 }
1115 return pInfo;
1116 }
1117 xf86Msg(X_ERROR, "%s: Unknown protocol \"%s\"\n",
1118 pInfo->name, protocol);
1119 return pInfo;
1120 break;
1121 case PROT_UNSUP:
1122 xf86Msg(X_ERROR,
1123 "%s: Protocol \"%s\" is not supported on this "
1124 "platform\n", pInfo->name, protocol);
1125 return pInfo;
1126 break;
1127 default:
1128 break;
1129
1130 }
1131 } while (!detected);
1132
1133 if (!xf86CheckStrOption(dev->commonOptions, "Device", NULL) &&
1134 HAVE_FIND_DEVICE && osInfo->FindDevice) {
1135 xf86Msg(X_WARNING, "%s: No Device specified, looking for one...\n",
1136 pInfo->name);
1137 if (!osInfo->FindDevice(pInfo, protocol, 0)) {
1138 xf86Msg(X_ERROR, "%s: Cannot find which device to use.\n",
1139 pInfo->name);
1140 } else {
1141 deviceFrom = X_PROBED;
1142 xf86MarkOptionUsedByName(dev->commonOptions, "Device");
1143 }
1144 }
1145
1146 device = xf86CheckStrOption(dev->commonOptions, "Device", NULL);
1147 if (device)
1148 xf86Msg(deviceFrom, "%s: Device: \"%s\"\n", pInfo->name, device);
1149
1150 xf86Msg(protocolFrom, "%s: Protocol: \"%s\"\n", pInfo->name, protocol);
1151 if (!(pProto = GetProtocol(protocolID)))
1152 return pInfo;
1153
1154 pMse->protocolID = protocolID;
1155 pMse->oldProtocolID = protocolID; /* hack */
1156
1157 pMse->autoProbe = FALSE;
1158 /* Collect the options, and process the common options. */
1159 xf86CollectInputOptions(pInfo, pProto->defaults, NULL);
1160 xf86ProcessCommonOptions(pInfo, pInfo->options);
1161
1162 /* XXX should handle this OS dependency elsewhere. */
1163#ifndef __OS2ELF__
1164 /* OS/2 has a mouse handled by the OS - it cannot fail here */
1165
1166 /* Check if the device can be opened. */
1167 pInfo->fd = xf86OpenSerial(pInfo->options);
1168 if (pInfo->fd == -1) {
1169 if (xf86GetAllowMouseOpenFail())
1170 xf86Msg(X_WARNING, "%s: cannot open input device\n", pInfo->name);
1171 else {
1172 xf86Msg(X_ERROR, "%s: cannot open input device\n", pInfo->name);
1173 if (pMse->mousePriv)
1174 xfree(pMse->mousePriv);
1175 xfree(pMse);
1176 pInfo->private = NULL;
1177 return pInfo;
1178 }
1179 }
1180 xf86CloseSerial(pInfo->fd);
1181#endif
1182 pInfo->fd = -1;
1183
1184#ifdef VBOX
1185 mPriv = NULL; /* later */
1186#else
1187 if (!(mPriv = (pointer) xcalloc(sizeof(mousePrivRec), 1)))
1188 return pInfo;
1189#endif
1190 pMse->mousePriv = mPriv;
1191 pMse->CommonOptions(pInfo);
1192 pMse->checkMovements = checkForErraticMovements;
1193 pMse->autoProbeMouse = autoProbeMouse;
1194 pMse->collectData = collectData;
1195 pMse->dataGood = autoGood;
1196
1197 MouseHWOptions(pInfo);
1198 MouseSerialOptions(pInfo);
1199
1200 pInfo->flags |= XI86_CONFIGURED;
1201 return pInfo;
1202}
1203
1204
1205static void
1206MouseReadInput(InputInfoPtr pInfo)
1207{
1208 MouseDevPtr pMse;
1209 int j, buttons, dx, dy, dz, dw, baddata;
1210 int pBufP;
1211 int c;
1212 unsigned char *pBuf, u;
1213
1214
1215 pMse = pInfo->private;
1216 pBufP = pMse->protoBufTail;
1217 pBuf = pMse->protoBuf;
1218
1219 /*
1220 * Set blocking to -1 on the first call because we know there is data to
1221 * read. Xisb automatically clears it after one successful read so that
1222 * succeeding reads are preceeded by a select with a 0 timeout to prevent
1223 * read from blocking indefinitely.
1224 */
1225 XisbBlockDuration(pMse->buffer, -1);
1226
1227 while ((c = XisbRead(pMse->buffer)) >= 0) {
1228 u = (unsigned char)c;
1229
1230#if defined (EXTMOUSEDEBUG) || defined (MOUSEDATADEBUG)
1231 ErrorF("mouse byte: %2.2x\n",u);
1232#endif
1233
1234#if 1
1235 /* if we do autoprobing collect the data */
1236 if (pMse->collectData && pMse->autoProbe)
1237 if (pMse->collectData(pMse,u))
1238 continue;
1239#endif
1240#ifdef SUPPORT_MOUSE_RESET
1241 if (mouseReset(pInfo,u)) {
1242 pBufP = 0;
1243 continue;
1244 }
1245#endif
1246 if (pBufP >= pMse->protoPara[4]) {
1247 /*
1248 * Buffer contains a full packet, which has already been processed:
1249 * Empty the buffer and check for optional 4th byte, which will be
1250 * processed directly, without being put into the buffer first.
1251 */
1252 pBufP = 0;
1253 if ((u & pMse->protoPara[0]) != pMse->protoPara[1] &&
1254 (u & pMse->protoPara[5]) == pMse->protoPara[6]) {
1255 /*
1256 * Hack for Logitech MouseMan Mouse - Middle button
1257 *
1258 * Unfortunately this mouse has variable length packets: the
1259 * standard Microsoft 3 byte packet plus an optional 4th byte
1260 * whenever the middle button status changes.
1261 *
1262 * We have already processed the standard packet with the
1263 * movement and button info. Now post an event message with
1264 * the old status of the left and right buttons and the
1265 * updated middle button.
1266 */
1267 /*
1268 * Even worse, different MouseMen and TrackMen differ in the
1269 * 4th byte: some will send 0x00/0x20, others 0x01/0x21, or
1270 * even 0x02/0x22, so I have to strip off the lower bits.
1271 * [CHRIS-211092]
1272 *
1273 * [JCH-96/01/21]
1274 * HACK for ALPS "fourth button". (It's bit 0x10 of the
1275 * "fourth byte" and it is activated by tapping the glidepad
1276 * with the finger! 8^) We map it to bit bit3, and the
1277 * reverse map in xf86Events just has to be extended so that
1278 * it is identified as Button 4. The lower half of the
1279 * reverse-map may remain unchanged.
1280 */
1281 /*
1282 * [KAZU-030897]
1283 * Receive the fourth byte only when preceeding three bytes
1284 * have been detected (pBufP >= pMse->protoPara[4]). In the
1285 * previous versions, the test was pBufP == 0; we may have
1286 * mistakingly received a byte even if we didn't see anything
1287 * preceeding the byte.
1288 */
1289#ifdef EXTMOUSEDEBUG
1290 ErrorF("mouse 4th byte %02x\n",u);
1291#endif
1292 dx = dy = dz = dw = 0;
1293 buttons = 0;
1294 switch (pMse->protocolID) {
1295
1296 /*
1297 * [KAZU-221197]
1298 * IntelliMouse, NetMouse (including NetMouse Pro) and Mie
1299 * Mouse always send the fourth byte, whereas the fourth byte
1300 * is optional for GlidePoint and ThinkingMouse. The fourth
1301 * byte is also optional for MouseMan+ and FirstMouse+ in
1302 * their native mode. It is always sent if they are in the
1303 * IntelliMouse compatible mode.
1304 */
1305 case PROT_IMSERIAL: /* IntelliMouse, NetMouse, Mie Mouse,
1306 MouseMan+ */
1307 dz = (u & 0x08) ?
1308 (u & 0x0f) - 16 : (u & 0x0f);
1309 if ((dz >= 7) || (dz <= -7))
1310 dz = 0;
1311 buttons |= ((int)(u & 0x10) >> 3)
1312 | ((int)(u & 0x20) >> 2)
1313 | (pMse->lastButtons & 0x05);
1314 break;
1315
1316 case PROT_GLIDE:
1317 case PROT_THINKING:
1318 buttons |= ((int)(u & 0x10) >> 1);
1319 /* fall through */
1320
1321 default:
1322 buttons |= ((int)(u & 0x20) >> 4) |
1323 (pMse->lastButtons & 0x05);
1324 break;
1325 }
1326 goto post_event;
1327 }
1328 }
1329 /* End of packet buffer flush and 4th byte hack. */
1330
1331 /*
1332 * Append next byte to buffer (which is empty or contains an
1333 * incomplete packet); iterate if packet (still) not complete.
1334 */
1335 pBuf[pBufP++] = u;
1336 if (pBufP != pMse->protoPara[4]) continue;
1337#ifdef EXTMOUSEDEBUG2
1338 {
1339 int i;
1340 ErrorF("received %d bytes",pBufP);
1341 for ( i=0; i < pBufP; i++)
1342 ErrorF(" %02x",pBuf[i]);
1343 ErrorF("\n");
1344 }
1345#endif
1346
1347 /*
1348 * Hack for resyncing: We check here for a package that is:
1349 * a) illegal (detected by wrong data-package header)
1350 * b) invalid (0x80 == -128 and that might be wrong for MouseSystems)
1351 * c) bad header-package
1352 *
1353 * NOTE: b) is a violation of the MouseSystems-Protocol, since values
1354 * of -128 are allowed, but since they are very seldom we can
1355 * easily use them as package-header with no button pressed.
1356 * NOTE/2: On a PS/2 mouse any byte is valid as a data byte.
1357 * Furthermore, 0x80 is not valid as a header byte. For a PS/2
1358 * mouse we skip checking data bytes. For resyncing a PS/2
1359 * mouse we require the two most significant bits in the header
1360 * byte to be 0. These are the overflow bits, and in case of
1361 * an overflow we actually lose sync. Overflows are very rare,
1362 * however, and we quickly gain sync again after an overflow
1363 * condition. This is the best we can do. (Actually, we could
1364 * use bit 0x08 in the header byte for resyncing, since that
1365 * bit is supposed to be always on, but nobody told Microsoft...)
1366 */
1367
1368 /*
1369 * [KAZU,OYVIND-120398]
1370 * The above hack is wrong! Because of b) above, we shall see
1371 * erroneous mouse events so often when the MouseSystem mouse is
1372 * moved quickly. As for the PS/2 and its variants, we don't need
1373 * to treat them as special cases, because protoPara[2] and
1374 * protoPara[3] are both 0x00 for them, thus, any data bytes will
1375 * never be discarded. 0x80 is rejected for MMSeries, Logitech
1376 * and MMHittab protocols, because protoPara[2] and protoPara[3]
1377 * are 0x80 and 0x00 respectively. The other protocols are 7-bit
1378 * protocols; there is no use checking 0x80.
1379 *
1380 * All in all we should check the condition a) only.
1381 */
1382
1383 /*
1384 * [OYVIND-120498]
1385 * Check packet for valid data:
1386 * If driver is in sync with datastream, the packet is considered
1387 * bad if any byte (header and/or data) contains an invalid value.
1388 *
1389 * If packet is bad, we discard the first byte and shift the buffer.
1390 * Next iteration will then check the new situation for validity.
1391 *
1392 * If flag MF_SAFE is set in proto[7] and the driver
1393 * is out of sync, the packet is also considered bad if
1394 * any of the data bytes contains a valid header byte value.
1395 * This situation could occur if the buffer contains
1396 * the tail of one packet and the header of the next.
1397 *
1398 * Note: The driver starts in out-of-sync mode (pMse->inSync = 0).
1399 */
1400
1401 baddata = 0;
1402
1403 /* All databytes must be valid. */
1404 for (j = 1; j < pBufP; j++ )
1405 if ((pBuf[j] & pMse->protoPara[2]) != pMse->protoPara[3])
1406 baddata = 1;
1407
1408 /* If out of sync, don't mistake a header byte for data. */
1409 if ((pMse->protoPara[7] & MPF_SAFE) && !pMse->inSync)
1410 for (j = 1; j < pBufP; j++ )
1411 if ((pBuf[j] & pMse->protoPara[0]) == pMse->protoPara[1])
1412 baddata = 1;
1413
1414 /* Accept or reject the packet ? */
1415 if ((pBuf[0] & pMse->protoPara[0]) != pMse->protoPara[1] || baddata) {
1416 if (pMse->inSync) {
1417#ifdef EXTMOUSEDEBUG
1418 ErrorF("mouse driver lost sync\n");
1419#endif
1420 }
1421#ifdef EXTMOUSEDEBUG
1422 ErrorF("skipping byte %02x\n",*pBuf);
1423#endif
1424 /* Tell auto probe that we are out of sync */
1425 if (pMse->autoProbeMouse && pMse->autoProbe)
1426 pMse->autoProbeMouse(pInfo, FALSE, pMse->inSync);
1427 pMse->protoBufTail = --pBufP;
1428 for (j = 0; j < pBufP; j++)
1429 pBuf[j] = pBuf[j+1];
1430 pMse->inSync = 0;
1431 continue;
1432 }
1433 /* Tell auto probe that we were successful */
1434 if (pMse->autoProbeMouse && pMse->autoProbe)
1435 pMse->autoProbeMouse(pInfo, TRUE, FALSE);
1436
1437 if (!pMse->inSync) {
1438#ifdef EXTMOUSEDEBUG
1439 ErrorF("mouse driver back in sync\n");
1440#endif
1441 pMse->inSync = 1;
1442 }
1443
1444 if (!pMse->dataGood(pMse))
1445 continue;
1446
1447 /*
1448 * Packet complete and verified, now process it ...
1449 */
1450 REDO_INTERPRET:
1451 dz = dw = 0;
1452 switch (pMse->protocolID) {
1453 case PROT_LOGIMAN: /* MouseMan / TrackMan [CHRIS-211092] */
1454 case PROT_MS: /* Microsoft */
1455 if (pMse->chordMiddle)
1456 buttons = (((int) pBuf[0] & 0x30) == 0x30) ? 2 :
1457 ((int)(pBuf[0] & 0x20) >> 3)
1458 | ((int)(pBuf[0] & 0x10) >> 4);
1459 else
1460 buttons = (pMse->lastButtons & 2)
1461 | ((int)(pBuf[0] & 0x20) >> 3)
1462 | ((int)(pBuf[0] & 0x10) >> 4);
1463 dx = (char)(((pBuf[0] & 0x03) << 6) | (pBuf[1] & 0x3F));
1464 dy = (char)(((pBuf[0] & 0x0C) << 4) | (pBuf[2] & 0x3F));
1465 break;
1466
1467 case PROT_GLIDE: /* ALPS GlidePoint */
1468 case PROT_THINKING: /* ThinkingMouse */
1469 case PROT_IMSERIAL: /* IntelliMouse, NetMouse, Mie Mouse, MouseMan+ */
1470 buttons = (pMse->lastButtons & (8 + 2))
1471 | ((int)(pBuf[0] & 0x20) >> 3)
1472 | ((int)(pBuf[0] & 0x10) >> 4);
1473 dx = (char)(((pBuf[0] & 0x03) << 6) | (pBuf[1] & 0x3F));
1474 dy = (char)(((pBuf[0] & 0x0C) << 4) | (pBuf[2] & 0x3F));
1475 break;
1476
1477 case PROT_MSC: /* Mouse Systems Corp */
1478 buttons = (~pBuf[0]) & 0x07;
1479 dx = (char)(pBuf[1]) + (char)(pBuf[3]);
1480 dy = - ((char)(pBuf[2]) + (char)(pBuf[4]));
1481 break;
1482
1483 case PROT_MMHIT: /* MM_HitTablet */
1484 buttons = pBuf[0] & 0x07;
1485 if (buttons != 0)
1486 buttons = 1 << (buttons - 1);
1487 dx = (pBuf[0] & 0x10) ? pBuf[1] : - pBuf[1];
1488 dy = (pBuf[0] & 0x08) ? - pBuf[2] : pBuf[2];
1489 break;
1490
1491 case PROT_ACECAD: /* ACECAD */
1492 /* ACECAD is almost exactly like MM but the buttons are different */
1493 buttons = (pBuf[0] & 0x02) | ((pBuf[0] & 0x04) >> 2) |
1494 ((pBuf[0] & 1) << 2);
1495 dx = (pBuf[0] & 0x10) ? pBuf[1] : - pBuf[1];
1496 dy = (pBuf[0] & 0x08) ? - pBuf[2] : pBuf[2];
1497 break;
1498
1499 case PROT_MM: /* MM Series */
1500 case PROT_LOGI: /* Logitech Mice */
1501 buttons = pBuf[0] & 0x07;
1502 dx = (pBuf[0] & 0x10) ? pBuf[1] : - pBuf[1];
1503 dy = (pBuf[0] & 0x08) ? - pBuf[2] : pBuf[2];
1504 break;
1505
1506 case PROT_BM: /* BusMouse */
1507 buttons = (~pBuf[0]) & 0x07;
1508 dx = (char)pBuf[1];
1509 dy = - (char)pBuf[2];
1510 break;
1511
1512 case PROT_PS2: /* PS/2 mouse */
1513 case PROT_GENPS2: /* generic PS/2 mouse */
1514 buttons = (pBuf[0] & 0x04) >> 1 | /* Middle */
1515 (pBuf[0] & 0x02) >> 1 | /* Right */
1516 (pBuf[0] & 0x01) << 2; /* Left */
1517 dx = (pBuf[0] & 0x10) ? (int)pBuf[1]-256 : (int)pBuf[1];
1518 dy = (pBuf[0] & 0x20) ? -((int)pBuf[2]-256) : -(int)pBuf[2];
1519 break;
1520
1521 /* PS/2 mouse variants */
1522 case PROT_IMPS2: /* IntelliMouse PS/2 */
1523 case PROT_NETPS2: /* NetMouse PS/2 */
1524 buttons = (pBuf[0] & 0x04) >> 1 | /* Middle */
1525 (pBuf[0] & 0x02) >> 1 | /* Right */
1526 (pBuf[0] & 0x01) << 2 | /* Left */
1527 (pBuf[0] & 0x40) >> 3 | /* button 4 */
1528 (pBuf[0] & 0x80) >> 3; /* button 5 */
1529 dx = (pBuf[0] & 0x10) ? pBuf[1]-256 : pBuf[1];
1530 dy = (pBuf[0] & 0x20) ? -(pBuf[2]-256) : -pBuf[2];
1531 /*
1532 * The next cast must be 'signed char' for platforms (like PPC)
1533 * where char defaults to unsigned.
1534 */
1535 dz = (signed char)(pBuf[3] | ((pBuf[3] & 0x08) ? 0xf8 : 0));
1536 if ((pBuf[3] & 0xf8) && ((pBuf[3] & 0xf8) != 0xf8)) {
1537 if (pMse->autoProbe) {
1538 SetMouseProto(pMse, PROT_EXPPS2);
1539 xf86Msg(X_INFO,
1540 "Mouse autoprobe: Changing protocol to %s\n",
1541 pMse->protocol);
1542
1543 goto REDO_INTERPRET;
1544 } else
1545 dz = 0;
1546 }
1547 break;
1548
1549 case PROT_EXPPS2: /* IntelliMouse Explorer PS/2 */
1550 if (pMse->autoProbe && (pBuf[3] & 0xC0)) {
1551 SetMouseProto(pMse, PROT_IMPS2);
1552 xf86Msg(X_INFO,"Mouse autoprobe: Changing protocol to %s\n",
1553 pMse->protocol);
1554 goto REDO_INTERPRET;
1555 }
1556 buttons = (pBuf[0] & 0x04) >> 1 | /* Middle */
1557 (pBuf[0] & 0x02) >> 1 | /* Right */
1558 (pBuf[0] & 0x01) << 2 | /* Left */
1559 (pBuf[3] & 0x10) >> 1 | /* button 4 */
1560 (pBuf[3] & 0x20) >> 1; /* button 5 */
1561 dx = (pBuf[0] & 0x10) ? pBuf[1]-256 : pBuf[1];
1562 dy = (pBuf[0] & 0x20) ? -(pBuf[2]-256) : -pBuf[2];
1563 dz = (pBuf[3] & 0x08) ? (pBuf[3] & 0x0f) - 16 : (pBuf[3] & 0x0f);
1564 break;
1565
1566 case PROT_MMPS2: /* MouseMan+ PS/2 */
1567 buttons = (pBuf[0] & 0x04) >> 1 | /* Middle */
1568 (pBuf[0] & 0x02) >> 1 | /* Right */
1569 (pBuf[0] & 0x01) << 2; /* Left */
1570 dx = (pBuf[0] & 0x10) ? pBuf[1] - 256 : pBuf[1];
1571 if (((pBuf[0] & 0x48) == 0x48) &&
1572 (abs(dx) > 191) &&
1573 ((((pBuf[2] & 0x03) << 2) | 0x02) == (pBuf[1] & 0x0f))) {
1574 /* extended data packet */
1575 switch ((((pBuf[0] & 0x30) >> 2) | ((pBuf[1] & 0x30) >> 4))) {
1576 case 1: /* wheel data packet */
1577 buttons |= ((pBuf[2] & 0x10) ? 0x08 : 0) | /* 4th button */
1578 ((pBuf[2] & 0x20) ? 0x10 : 0); /* 5th button */
1579 dx = dy = 0;
1580 dz = (pBuf[2] & 0x08) ? (pBuf[2] & 0x0f) - 16 :
1581 (pBuf[2] & 0x0f);
1582 break;
1583 case 2: /* Logitech reserves this packet type */
1584 /*
1585 * IBM ScrollPoint uses this packet to encode its
1586 * stick movement.
1587 */
1588 buttons |= (pMse->lastButtons & ~0x07);
1589 dx = dy = 0;
1590 dz = (pBuf[2] & 0x80) ? ((pBuf[2] >> 4) & 0x0f) - 16 :
1591 ((pBuf[2] >> 4) & 0x0f);
1592 dw = (pBuf[2] & 0x08) ? (pBuf[2] & 0x0f) - 16 :
1593 (pBuf[2] & 0x0f);
1594 break;
1595 case 0: /* device type packet - shouldn't happen */
1596 default:
1597 buttons |= (pMse->lastButtons & ~0x07);
1598 dx = dy = 0;
1599 dz = 0;
1600 break;
1601 }
1602 } else {
1603 buttons |= (pMse->lastButtons & ~0x07);
1604 dx = (pBuf[0] & 0x10) ? pBuf[1]-256 : pBuf[1];
1605 dy = (pBuf[0] & 0x20) ? -(pBuf[2]-256) : -pBuf[2];
1606 }
1607 break;
1608
1609 case PROT_GLIDEPS2: /* GlidePoint PS/2 */
1610 buttons = (pBuf[0] & 0x04) >> 1 | /* Middle */
1611 (pBuf[0] & 0x02) >> 1 | /* Right */
1612 (pBuf[0] & 0x01) << 2 | /* Left */
1613 ((pBuf[0] & 0x08) ? 0 : 0x08);/* fourth button */
1614 dx = (pBuf[0] & 0x10) ? pBuf[1]-256 : pBuf[1];
1615 dy = (pBuf[0] & 0x20) ? -(pBuf[2]-256) : -pBuf[2];
1616 break;
1617
1618 case PROT_NETSCPS2: /* NetScroll PS/2 */
1619 buttons = (pBuf[0] & 0x04) >> 1 | /* Middle */
1620 (pBuf[0] & 0x02) >> 1 | /* Right */
1621 (pBuf[0] & 0x01) << 2 | /* Left */
1622 ((pBuf[3] & 0x02) ? 0x08 : 0) | /* button 4 */
1623 ((pBuf[3] & 0x01) ? 0x10 : 0); /* button 5 */
1624 dx = (pBuf[0] & 0x10) ? pBuf[1]-256 : pBuf[1];
1625 dy = (pBuf[0] & 0x20) ? -(pBuf[2]-256) : -pBuf[2];
1626 dz = (pBuf[3] & 0x10) ? pBuf[4] - 256 : pBuf[4];
1627 break;
1628
1629 case PROT_THINKPS2: /* ThinkingMouse PS/2 */
1630 buttons = (pBuf[0] & 0x04) >> 1 | /* Middle */
1631 (pBuf[0] & 0x02) >> 1 | /* Right */
1632 (pBuf[0] & 0x01) << 2 | /* Left */
1633 ((pBuf[0] & 0x08) ? 0x08 : 0);/* fourth button */
1634 pBuf[1] |= (pBuf[0] & 0x40) ? 0x80 : 0x00;
1635 dx = (pBuf[0] & 0x10) ? pBuf[1]-256 : pBuf[1];
1636 dy = (pBuf[0] & 0x20) ? -(pBuf[2]-256) : -pBuf[2];
1637 break;
1638
1639 case PROT_SYSMOUSE: /* sysmouse */
1640 buttons = (~pBuf[0]) & 0x07;
1641 dx = (signed char)(pBuf[1]) + (signed char)(pBuf[3]);
1642 dy = - ((signed char)(pBuf[2]) + (signed char)(pBuf[4]));
1643 /* FreeBSD sysmouse sends additional data bytes */
1644 if (pMse->protoPara[4] >= 8) {
1645 /*
1646 * These casts must be 'signed char' for platforms (like PPC)
1647 * where char defaults to unsigned.
1648 */
1649 dz = ((signed char)(pBuf[5] << 1) +
1650 (signed char)(pBuf[6] << 1)) >> 1;
1651 buttons |= (int)(~pBuf[7] & 0x7f) << 3;
1652 }
1653 break;
1654
1655 case PROT_VALUMOUSESCROLL: /* Kensington ValuMouseScroll */
1656 buttons = ((int)(pBuf[0] & 0x20) >> 3)
1657 | ((int)(pBuf[0] & 0x10) >> 4)
1658 | ((int)(pBuf[3] & 0x10) >> 3);
1659 dx = (char)(((pBuf[0] & 0x03) << 6) | (pBuf[1] & 0x3F));
1660 dy = (char)(((pBuf[0] & 0x0C) << 4) | (pBuf[2] & 0x3F));
1661 dz = (pBuf[3] & 0x08) ? ((int)(pBuf[3] & 0x0F) - 0x10) :
1662 ((int)(pBuf[3] & 0x0F));
1663 break;
1664
1665 default: /* There's a table error */
1666#ifdef EXTMOUSEDEBUG
1667 ErrorF("mouse table error\n");
1668#endif
1669 continue;
1670 }
1671#ifdef EXTMOUSEDEBUG
1672 ErrorF("packet");
1673 for ( j=0; j < pBufP; j++)
1674 ErrorF(" %02x",pBuf[j]);
1675 ErrorF("\n");
1676#endif
1677
1678post_event:
1679#ifdef EXTMOUSEDEBUG
1680 ErrorF("dx=%i dy=%i dz=%i dw=%i buttons=%x\n",dx,dy,dz,dw,buttons);
1681#endif
1682 /* When auto-probing check if data makes sense */
1683 if (pMse->checkMovements && pMse->autoProbe)
1684 pMse->checkMovements(pInfo,dx,dy);
1685 /* post an event */
1686 pMse->PostEvent(pInfo, buttons, dx, dy, dz, dw);
1687
1688 /*
1689 * We don't reset pBufP here yet, as there may be an additional data
1690 * byte in some protocols. See above.
1691 */
1692 }
1693 pMse->protoBufTail = pBufP;
1694}
1695
1696/*
1697 * MouseCtrl --
1698 * Alter the control parameters for the mouse. Note that all special
1699 * protocol values are handled by dix.
1700 */
1701
1702static void
1703MouseCtrl(DeviceIntPtr device, PtrCtrl *ctrl)
1704{
1705 InputInfoPtr pInfo;
1706 MouseDevPtr pMse;
1707
1708 pInfo = device->public.devicePrivate;
1709 pMse = pInfo->private;
1710
1711#ifdef EXTMOUSEDEBUG
1712 ErrorF("MouseCtrl pMse=%p\n", pMse);
1713#endif
1714
1715 pMse->num = ctrl->num;
1716 pMse->den = ctrl->den;
1717 pMse->threshold = ctrl->threshold;
1718}
1719
1720/*
1721 ***************************************************************************
1722 *
1723 * MouseProc --
1724 *
1725 ***************************************************************************
1726 */
1727
1728static int
1729MouseProc(DeviceIntPtr device, int what)
1730{
1731 InputInfoPtr pInfo;
1732 MouseDevPtr pMse;
1733 mousePrivPtr mPriv;
1734 unsigned char map[MSE_MAXBUTTONS + 1];
1735 int i;
1736#ifdef VBOX
1737 mousePrivPtr pPriv;
1738#endif
1739
1740 pInfo = device->public.devicePrivate;
1741 pMse = pInfo->private;
1742 pMse->device = device;
1743
1744#ifdef VBOX
1745 pPriv = pMse->mousePriv;
1746#endif
1747
1748 switch (what)
1749 {
1750 case DEVICE_INIT:
1751 device->public.on = FALSE;
1752 /*
1753 * [KAZU-241097] We don't know exactly how many buttons the
1754 * device has, so setup the map with the maximum number.
1755 */
1756 for (i = 0; i < MSE_MAXBUTTONS; i++)
1757 map[i + 1] = i + 1;
1758
1759 InitPointerDeviceStruct((DevicePtr)device, map,
1760 min(pMse->buttons, MSE_MAXBUTTONS),
1761 miPointerGetMotionEvents, pMse->Ctrl,
1762 miPointerGetMotionBufferSize());
1763
1764 /* X valuator */
1765 xf86InitValuatorAxisStruct(device, 0, 0, -1, 1, 0, 1);
1766 xf86InitValuatorDefaults(device, 0);
1767 /* Y valuator */
1768 xf86InitValuatorAxisStruct(device, 1, 0, -1, 1, 0, 1);
1769 xf86InitValuatorDefaults(device, 1);
1770 xf86MotionHistoryAllocate(pInfo);
1771
1772#ifdef EXTMOUSEDEBUG
1773 ErrorF("assigning %p atom=%d name=%s\n", device, pInfo->atom,
1774 pInfo->name);
1775#endif
1776 break;
1777
1778 case DEVICE_ON:
1779#ifdef VBOX
1780 if (!pPriv)
1781 {
1782 pPriv = (pointer)xcalloc(sizeof(mousePrivRec), 1);
1783 if (pPriv)
1784 {
1785 pMse->mousePriv = pPriv;
1786 pPriv->pScrn = 0;
1787 pPriv->screen_no = xf86SetIntOption(pInfo->options, "ScreenNo", 0);
1788 xf86Msg(X_CONFIG, "VirtualBox Mouse Integration associated with screen %d\n",
1789 pPriv->screen_no);
1790 }
1791 }
1792 if (pPriv)
1793 {
1794 if ( pPriv->screen_no >= screenInfo.numScreens
1795 || pPriv->screen_no < 0)
1796 {
1797 pPriv->screen_no = 0;
1798 }
1799 VBoxMouseInit();
1800 pPriv->pScrn = screenInfo.screens[pPriv->screen_no];
1801 }
1802#endif
1803 pInfo->fd = xf86OpenSerial(pInfo->options);
1804 if (pInfo->fd == -1)
1805 xf86Msg(X_WARNING, "%s: cannot open input device\n", pInfo->name);
1806 else {
1807 if (pMse->xisbscale)
1808 pMse->buffer = XisbNew(pInfo->fd, pMse->xisbscale * 4);
1809 else
1810 pMse->buffer = XisbNew(pInfo->fd, 64);
1811 if (!pMse->buffer) {
1812 xf86CloseSerial(pInfo->fd);
1813 pInfo->fd = -1;
1814 } else {
1815 if (!SetupMouse(pInfo)) {
1816 xf86CloseSerial(pInfo->fd);
1817 pInfo->fd = -1;
1818 XisbFree(pMse->buffer);
1819 pMse->buffer = NULL;
1820 } else {
1821 mPriv = (mousePrivPtr)pMse->mousePriv;
1822 if (mPriv != NULL) {
1823 if ( pMse->protocolID != PROT_AUTO) {
1824 pMse->inSync = TRUE; /* @@@ */
1825 if (mPriv->soft)
1826 mPriv->autoState = AUTOPROBE_GOOD;
1827 else
1828 mPriv->autoState = AUTOPROBE_H_GOOD;
1829 } else {
1830 if (mPriv->soft)
1831 mPriv->autoState = AUTOPROBE_NOPROTO;
1832 else
1833 mPriv->autoState = AUTOPROBE_H_NOPROTO;
1834 }
1835 }
1836 xf86FlushInput(pInfo->fd);
1837 xf86AddEnabledDevice(pInfo);
1838 }
1839 }
1840 }
1841 pMse->lastButtons = 0;
1842 pMse->lastMappedButtons = 0;
1843 pMse->emulateState = 0;
1844 pMse->emulate3Pending = FALSE;
1845 pMse->wheelButtonExpires = GetTimeInMillis ();
1846 device->public.on = TRUE;
1847 FlushButtons(pMse);
1848 if (pMse->emulate3Buttons || pMse->emulate3ButtonsSoft)
1849 {
1850 RegisterBlockAndWakeupHandlers (MouseBlockHandler, MouseWakeupHandler,
1851 (pointer) pInfo);
1852 }
1853 break;
1854
1855 case DEVICE_OFF:
1856 case DEVICE_CLOSE:
1857#ifdef VBOX
1858 if (VBoxMouseFini())
1859 {
1860 /** @todo what to do? */
1861 }
1862#endif
1863 if (pInfo->fd != -1) {
1864 xf86RemoveEnabledDevice(pInfo);
1865 if (pMse->buffer) {
1866 XisbFree(pMse->buffer);
1867 pMse->buffer = NULL;
1868 }
1869 xf86CloseSerial(pInfo->fd);
1870 pInfo->fd = -1;
1871 if (pMse->emulate3Buttons || pMse->emulate3ButtonsSoft)
1872 {
1873 RemoveBlockAndWakeupHandlers (MouseBlockHandler, MouseWakeupHandler,
1874 (pointer) pInfo);
1875 }
1876 }
1877 device->public.on = FALSE;
1878 usleep(300000);
1879 break;
1880 }
1881 return Success;
1882}
1883
1884/*
1885 ***************************************************************************
1886 *
1887 * MouseConvert --
1888 * Convert valuators to X and Y.
1889 *
1890 ***************************************************************************
1891 */
1892static Bool
1893MouseConvert(InputInfoPtr pInfo, int first, int num, int v0, int v1, int v2,
1894 int v3, int v4, int v5, int *x, int *y)
1895{
1896 if (first != 0 || num != 2)
1897 return FALSE;
1898
1899 *x = v0;
1900 *y = v1;
1901
1902 return TRUE;
1903}
1904
1905/**********************************************************************
1906 *
1907 * FlushButtons -- send button up events for sanity.
1908 *
1909 **********************************************************************/
1910
1911static void
1912FlushButtons(MouseDevPtr pMse)
1913{
1914
1915 /* If no button down is pending xf86PostButtonEvent()
1916 * will discard them. So we are on the safe side. */
1917
1918 int i, blocked;
1919
1920 pMse->lastButtons = 0;
1921 pMse->lastMappedButtons = 0;
1922
1923 blocked = xf86BlockSIGIO ();
1924 for (i = 1; i <= 5; i++)
1925 xf86PostButtonEvent(pMse->device,0,i,0,0,0);
1926 xf86UnblockSIGIO (blocked);
1927}
1928
1929/**********************************************************************
1930 *
1931 * Emulate3Button support code
1932 *
1933 **********************************************************************/
1934
1935
1936/*
1937 * Lets create a simple finite-state machine for 3 button emulation:
1938 *
1939 * We track buttons 1 and 3 (left and right). There are 11 states:
1940 * 0 ground - initial state
1941 * 1 delayed left - left pressed, waiting for right
1942 * 2 delayed right - right pressed, waiting for left
1943 * 3 pressed middle - right and left pressed, emulated middle sent
1944 * 4 pressed left - left pressed and sent
1945 * 5 pressed right - right pressed and sent
1946 * 6 released left - left released after emulated middle
1947 * 7 released right - right released after emulated middle
1948 * 8 repressed left - left pressed after released left
1949 * 9 repressed right - right pressed after released right
1950 * 10 pressed both - both pressed, not emulating middle
1951 *
1952 * At each state, we need handlers for the following events
1953 * 0: no buttons down
1954 * 1: left button down
1955 * 2: right button down
1956 * 3: both buttons down
1957 * 4: emulate3Timeout passed without a button change
1958 * Note that button events are not deltas, they are the set of buttons being
1959 * pressed now. It's possible (ie, mouse hardware does it) to go from (eg)
1960 * left down to right down without anything in between, so all cases must be
1961 * handled.
1962 *
1963 * a handler consists of three values:
1964 * 0: action1
1965 * 1: action2
1966 * 2: new emulation state
1967 *
1968 * action > 0: ButtonPress
1969 * action = 0: nothing
1970 * action < 0: ButtonRelease
1971 *
1972 * The comment preceeding each section is the current emulation state.
1973 * The comments to the right are of the form
1974 * <button state> (<events>) -> <new emulation state>
1975 * which should be read as
1976 * If the buttons are in <button state>, generate <events> then go to
1977 * <new emulation state>.
1978 */
1979static signed char stateTab[11][5][3] = {
1980/* 0 ground */
1981 {
1982 { 0, 0, 0 }, /* nothing -> ground (no change) */
1983 { 0, 0, 1 }, /* left -> delayed left */
1984 { 0, 0, 2 }, /* right -> delayed right */
1985 { 2, 0, 3 }, /* left & right (middle press) -> pressed middle */
1986 { 0, 0, -1 } /* timeout N/A */
1987 },
1988/* 1 delayed left */
1989 {
1990 { 1, -1, 0 }, /* nothing (left event) -> ground */
1991 { 0, 0, 1 }, /* left -> delayed left (no change) */
1992 { 1, -1, 2 }, /* right (left event) -> delayed right */
1993 { 2, 0, 3 }, /* left & right (middle press) -> pressed middle */
1994 { 1, 0, 4 }, /* timeout (left press) -> pressed left */
1995 },
1996/* 2 delayed right */
1997 {
1998 { 3, -3, 0 }, /* nothing (right event) -> ground */
1999 { 3, -3, 1 }, /* left (right event) -> delayed left (no change) */
2000 { 0, 0, 2 }, /* right -> delayed right (no change) */
2001 { 2, 0, 3 }, /* left & right (middle press) -> pressed middle */
2002 { 3, 0, 5 }, /* timeout (right press) -> pressed right */
2003 },
2004/* 3 pressed middle */
2005 {
2006 { -2, 0, 0 }, /* nothing (middle release) -> ground */
2007 { 0, 0, 7 }, /* left -> released right */
2008 { 0, 0, 6 }, /* right -> released left */
2009 { 0, 0, 3 }, /* left & right -> pressed middle (no change) */
2010 { 0, 0, -1 }, /* timeout N/A */
2011 },
2012/* 4 pressed left */
2013 {
2014 { -1, 0, 0 }, /* nothing (left release) -> ground */
2015 { 0, 0, 4 }, /* left -> pressed left (no change) */
2016 { -1, 0, 2 }, /* right (left release) -> delayed right */
2017 { 3, 0, 10 }, /* left & right (right press) -> pressed both */
2018 { 0, 0, -1 }, /* timeout N/A */
2019 },
2020/* 5 pressed right */
2021 {
2022 { -3, 0, 0 }, /* nothing (right release) -> ground */
2023 { -3, 0, 1 }, /* left (right release) -> delayed left */
2024 { 0, 0, 5 }, /* right -> pressed right (no change) */
2025 { 1, 0, 10 }, /* left & right (left press) -> pressed both */
2026 { 0, 0, -1 }, /* timeout N/A */
2027 },
2028/* 6 released left */
2029 {
2030 { -2, 0, 0 }, /* nothing (middle release) -> ground */
2031 { -2, 0, 1 }, /* left (middle release) -> delayed left */
2032 { 0, 0, 6 }, /* right -> released left (no change) */
2033 { 1, 0, 8 }, /* left & right (left press) -> repressed left */
2034 { 0, 0, -1 }, /* timeout N/A */
2035 },
2036/* 7 released right */
2037 {
2038 { -2, 0, 0 }, /* nothing (middle release) -> ground */
2039 { 0, 0, 7 }, /* left -> released right (no change) */
2040 { -2, 0, 2 }, /* right (middle release) -> delayed right */
2041 { 3, 0, 9 }, /* left & right (right press) -> repressed right */
2042 { 0, 0, -1 }, /* timeout N/A */
2043 },
2044/* 8 repressed left */
2045 {
2046 { -2, -1, 0 }, /* nothing (middle release, left release) -> ground */
2047 { -2, 0, 4 }, /* left (middle release) -> pressed left */
2048 { -1, 0, 6 }, /* right (left release) -> released left */
2049 { 0, 0, 8 }, /* left & right -> repressed left (no change) */
2050 { 0, 0, -1 }, /* timeout N/A */
2051 },
2052/* 9 repressed right */
2053 {
2054 { -2, -3, 0 }, /* nothing (middle release, right release) -> ground */
2055 { -3, 0, 7 }, /* left (right release) -> released right */
2056 { -2, 0, 5 }, /* right (middle release) -> pressed right */
2057 { 0, 0, 9 }, /* left & right -> repressed right (no change) */
2058 { 0, 0, -1 }, /* timeout N/A */
2059 },
2060/* 10 pressed both */
2061 {
2062 { -1, -3, 0 }, /* nothing (left release, right release) -> ground */
2063 { -3, 0, 4 }, /* left (right release) -> pressed left */
2064 { -1, 0, 5 }, /* right (left release) -> pressed right */
2065 { 0, 0, 10 }, /* left & right -> pressed both (no change) */
2066 { 0, 0, -1 }, /* timeout N/A */
2067 },
2068};
2069
2070/*
2071 * Table to allow quick reversal of natural button mapping to correct mapping
2072 */
2073
2074/*
2075 * [JCH-96/01/21] The ALPS GlidePoint pad extends the MS protocol
2076 * with a fourth button activated by tapping the PAD.
2077 * The 2nd line corresponds to 4th button on; the drv sends
2078 * the buttons in the following map (MSBit described first) :
2079 * 0 | 4th | 1st | 2nd | 3rd
2080 * And we remap them (MSBit described first) :
2081 * 0 | 4th | 3rd | 2nd | 1st
2082 */
2083static char reverseMap[16] = { 0, 4, 2, 6,
2084 1, 5, 3, 7,
2085 8, 12, 10, 14,
2086 9, 13, 11, 15 };
2087
2088static char hitachMap[16] = { 0, 2, 1, 3,
2089 8, 10, 9, 11,
2090 4, 6, 5, 7,
2091 12, 14, 13, 15 };
2092
2093#define reverseBits(map, b) (((b) & ~0x0f) | map[(b) & 0x0f])
2094
2095static CARD32
2096buttonTimer(InputInfoPtr pInfo)
2097{
2098 MouseDevPtr pMse;
2099 int sigstate;
2100 int id;
2101
2102 pMse = pInfo->private;
2103
2104 sigstate = xf86BlockSIGIO ();
2105
2106 pMse->emulate3Pending = FALSE;
2107 if ((id = stateTab[pMse->emulateState][4][0]) != 0) {
2108 xf86PostButtonEvent(pInfo->dev, 0, abs(id), (id >= 0), 0, 0);
2109 pMse->emulateState = stateTab[pMse->emulateState][4][2];
2110 } else {
2111 ErrorF("Got unexpected buttonTimer in state %d\n", pMse->emulateState);
2112 }
2113
2114 xf86UnblockSIGIO (sigstate);
2115 return 0;
2116}
2117
2118static Bool
2119Emulate3ButtonsSoft(InputInfoPtr pInfo)
2120{
2121 MouseDevPtr pMse = pInfo->private;
2122
2123 if (!pMse->emulate3ButtonsSoft)
2124 return TRUE;
2125
2126 pMse->emulate3Buttons = FALSE;
2127
2128 if (pMse->emulate3Pending)
2129 buttonTimer(pInfo);
2130
2131 xf86Msg(X_INFO,"3rd Button detected: disabling emulate3Button\n");
2132
2133 return FALSE;
2134}
2135
2136static void MouseBlockHandler(pointer data,
2137 struct timeval **waitTime,
2138 pointer LastSelectMask)
2139{
2140 InputInfoPtr pInfo = (InputInfoPtr) data;
2141 MouseDevPtr pMse = (MouseDevPtr) pInfo->private;
2142 int ms;
2143
2144 if (pMse->emulate3Pending)
2145 {
2146 ms = pMse->emulate3Expires - GetTimeInMillis ();
2147 if (ms <= 0)
2148 ms = 0;
2149 AdjustWaitForDelay (waitTime, ms);
2150 }
2151}
2152
2153static void MouseWakeupHandler(pointer data,
2154 int i,
2155 pointer LastSelectMask)
2156{
2157 InputInfoPtr pInfo = (InputInfoPtr) data;
2158 MouseDevPtr pMse = (MouseDevPtr) pInfo->private;
2159 int ms;
2160
2161 if (pMse->emulate3Pending)
2162 {
2163 ms = pMse->emulate3Expires - GetTimeInMillis ();
2164 if (ms <= 0)
2165 buttonTimer (pInfo);
2166 }
2167}
2168
2169/*******************************************************************
2170 *
2171 * Post mouse events
2172 *
2173 *******************************************************************/
2174
2175static void
2176MouseDoPostEvent(InputInfoPtr pInfo, int buttons, int dx, int dy)
2177{
2178 MouseDevPtr pMse;
2179 int emulateButtons;
2180 int id, change;
2181 int emuWheelDelta, emuWheelButton, emuWheelButtonMask;
2182 int wheelButtonMask;
2183 int ms;
2184
2185 pMse = pInfo->private;
2186
2187 change = buttons ^ pMse->lastMappedButtons;
2188 pMse->lastMappedButtons = buttons;
2189
2190 /* Do single button double click */
2191 if (pMse->doubleClickSourceButtonMask) {
2192 if (buttons & pMse->doubleClickSourceButtonMask) {
2193 if (!(pMse->doubleClickOldSourceState)) {
2194 /* double-click button has just been pressed. Ignore it if target button
2195 * is already down.
2196 */
2197 if (!(buttons & pMse->doubleClickTargetButtonMask)) {
2198 /* Target button isn't down, so send a double-click */
2199 xf86PostButtonEvent(pInfo->dev, 0, pMse->doubleClickTargetButton, 1, 0, 0);
2200 xf86PostButtonEvent(pInfo->dev, 0, pMse->doubleClickTargetButton, 0, 0, 0);
2201 xf86PostButtonEvent(pInfo->dev, 0, pMse->doubleClickTargetButton, 1, 0, 0);
2202 xf86PostButtonEvent(pInfo->dev, 0, pMse->doubleClickTargetButton, 0, 0, 0);
2203 }
2204 }
2205 pMse->doubleClickOldSourceState = 1;
2206 }
2207 else
2208 pMse->doubleClickOldSourceState = 0;
2209
2210 /* Whatever happened, mask the double-click button so it doesn't get
2211 * processed as a normal button as well.
2212 */
2213 buttons &= ~(pMse->doubleClickSourceButtonMask);
2214 change &= ~(pMse->doubleClickSourceButtonMask);
2215 }
2216
2217 if (pMse->emulateWheel) {
2218 /* Emulate wheel button handling */
2219 wheelButtonMask = 1 << (pMse->wheelButton - 1);
2220
2221 if (change & wheelButtonMask) {
2222 if (buttons & wheelButtonMask) {
2223 /* Start timeout handling */
2224 pMse->wheelButtonExpires = GetTimeInMillis () + pMse->wheelButtonTimeout;
2225 ms = - pMse->wheelButtonTimeout;
2226 } else {
2227 ms = pMse->wheelButtonExpires - GetTimeInMillis ();
2228
2229 if (0 < ms) {
2230 /*
2231 * If the button is released early enough emit the button
2232 * press/release events
2233 */
2234 xf86PostButtonEvent(pInfo->dev, 0, pMse->wheelButton, 1, 0, 0);
2235 xf86PostButtonEvent(pInfo->dev, 0, pMse->wheelButton, 0, 0, 0);
2236 }
2237 }
2238 } else
2239 ms = pMse->wheelButtonExpires - GetTimeInMillis ();
2240
2241 /* Intercept wheel emulation. */
2242 if (buttons & wheelButtonMask) {
2243 if (ms <= 0) {
2244 /* Y axis movement */
2245 if (pMse->negativeY != MSE_NOAXISMAP) {
2246 pMse->wheelYDistance += dy;
2247 if (pMse->wheelYDistance < 0) {
2248 emuWheelDelta = -pMse->wheelInertia;
2249 emuWheelButton = pMse->negativeY;
2250 } else {
2251 emuWheelDelta = pMse->wheelInertia;
2252 emuWheelButton = pMse->positiveY;
2253 }
2254 emuWheelButtonMask = 1 << (emuWheelButton - 1);
2255 while (abs(pMse->wheelYDistance) > pMse->wheelInertia) {
2256 pMse->wheelYDistance -= emuWheelDelta;
2257
2258 /*
2259 * Synthesize the press and release, but not when
2260 * the button to be synthesized is already pressed
2261 * "for real".
2262 */
2263 if (!(emuWheelButtonMask & buttons) ||
2264 (emuWheelButtonMask & wheelButtonMask)) {
2265 xf86PostButtonEvent(pInfo->dev, 0, emuWheelButton, 1, 0, 0);
2266 xf86PostButtonEvent(pInfo->dev, 0, emuWheelButton, 0, 0, 0);
2267 }
2268 }
2269 }
2270
2271 /* X axis movement */
2272 if (pMse->negativeX != MSE_NOAXISMAP) {
2273 pMse->wheelXDistance += dx;
2274 if (pMse->wheelXDistance < 0) {
2275 emuWheelDelta = -pMse->wheelInertia;
2276 emuWheelButton = pMse->negativeX;
2277 } else {
2278 emuWheelDelta = pMse->wheelInertia;
2279 emuWheelButton = pMse->positiveX;
2280 }
2281 emuWheelButtonMask = 1 << (emuWheelButton - 1);
2282 while (abs(pMse->wheelXDistance) > pMse->wheelInertia) {
2283 pMse->wheelXDistance -= emuWheelDelta;
2284
2285 /*
2286 * Synthesize the press and release, but not when
2287 * the button to be synthesized is already pressed
2288 * "for real".
2289 */
2290 if (!(emuWheelButtonMask & buttons) ||
2291 (emuWheelButtonMask & wheelButtonMask)) {
2292 xf86PostButtonEvent(pInfo->dev, 0, emuWheelButton, 1, 0, 0);
2293 xf86PostButtonEvent(pInfo->dev, 0, emuWheelButton, 0, 0, 0);
2294 }
2295 }
2296 }
2297 }
2298
2299 /* Absorb the mouse movement while the wheel button is pressed. */
2300 dx = 0;
2301 dy = 0;
2302 }
2303 /*
2304 * Button events for the wheel button are only emitted through
2305 * the timeout code.
2306 */
2307 buttons &= ~wheelButtonMask;
2308 change &= ~wheelButtonMask;
2309 }
2310
2311 if (pMse->emulate3ButtonsSoft && pMse->emulate3Pending && (dx || dy))
2312 buttonTimer(pInfo);
2313
2314#ifdef VBOX
2315 if (dx || dy)
2316 {
2317 mousePrivPtr pPriv = pMse->mousePriv;
2318 if (pPriv && pPriv->pScrn)
2319 {
2320 unsigned int abs_x;
2321 unsigned int abs_y;
2322 if (VBoxMouseQueryPosition(&abs_x, &abs_y) == 0)
2323 {
2324 /* convert to screen resolution */
2325 int x, y;
2326 x = (abs_x * pPriv->pScrn->width) / 65535;
2327 y = (abs_y * pPriv->pScrn->height) / 65535;
2328 /* send absolute movement */
2329 xf86PostMotionEvent(pInfo->dev, 1, 0, 2, x, y);
2330 }
2331 else
2332 {
2333 /* send relative event */
2334 xf86PostMotionEvent(pInfo->dev, 0, 0, 2, dx, dy);
2335 }
2336 }
2337 else
2338 {
2339 /* send relative event */
2340 xf86PostMotionEvent(pInfo->dev, 0, 0, 2, dx, dy);
2341 }
2342 }
2343#else
2344 if (dx || dy)
2345 xf86PostMotionEvent(pInfo->dev, 0, 0, 2, dx, dy);
2346#endif
2347
2348 if (change) {
2349
2350 /*
2351 * adjust buttons state for drag locks!
2352 * if there is drag locks
2353 */
2354 if (pMse->pDragLock) {
2355 DragLockPtr pLock;
2356 int tarOfGoingDown, tarOfDown;
2357 int realbuttons;
2358
2359 /* get drag lock block */
2360 pLock = pMse->pDragLock;
2361 /* save real buttons */
2362 realbuttons = buttons;
2363
2364 /* if drag lock used */
2365
2366 /* state of drag lock buttons not seen always up */
2367
2368 buttons &= ~pLock->lockButtonsM;
2369
2370 /*
2371 * if lock buttons being depressed changes state of
2372 * targets simulatedDown.
2373 */
2374 tarOfGoingDown = lock2targetMap(pLock,
2375 realbuttons & change & pLock->lockButtonsM);
2376 pLock->simulatedDown ^= tarOfGoingDown;
2377
2378 /* targets of drag locks down */
2379 tarOfDown = lock2targetMap(pLock,
2380 realbuttons & pLock->lockButtonsM);
2381
2382 /*
2383 * when simulatedDown set and target pressed,
2384 * simulatedDown goes false
2385 */
2386 pLock->simulatedDown &= ~(realbuttons & change);
2387
2388 /*
2389 * if master drag lock released
2390 * then master drag lock state on
2391 */
2392 pLock->masterTS |= (~realbuttons & change) & pLock->masterLockM;
2393
2394 /* if master state, buttons going down are simulatedDown */
2395 if (pLock->masterTS)
2396 pLock->simulatedDown |= (realbuttons & change);
2397
2398 /* if any button pressed, no longer in master drag lock state */
2399 if (realbuttons & change)
2400 pLock->masterTS = 0;
2401
2402 /* if simulatedDown or drag lock down, simulate down */
2403 buttons |= (pLock->simulatedDown | tarOfDown);
2404
2405 /* master button not seen */
2406 buttons &= ~(pLock->masterLockM);
2407
2408 /* buttons changed since last time */
2409 change = buttons ^ pLock->lockLastButtons;
2410
2411 /* save this time for next last time. */
2412 pLock->lockLastButtons = buttons;
2413 }
2414
2415 if (pMse->emulate3Buttons
2416 && (!(buttons & 0x02) || Emulate3ButtonsSoft(pInfo))) {
2417
2418 /* handle all but buttons 1 & 3 normally */
2419
2420 change &= ~05;
2421
2422 /* emulate the third button by the other two */
2423
2424 emulateButtons = (buttons & 01) | ((buttons &04) >> 1);
2425
2426 if ((id = stateTab[pMse->emulateState][emulateButtons][0]) != 0)
2427 xf86PostButtonEvent(pInfo->dev, 0, abs(id), (id >= 0), 0, 0);
2428 if ((id = stateTab[pMse->emulateState][emulateButtons][1]) != 0)
2429 xf86PostButtonEvent(pInfo->dev, 0, abs(id), (id >= 0), 0, 0);
2430
2431 pMse->emulateState =
2432 stateTab[pMse->emulateState][emulateButtons][2];
2433
2434 if (stateTab[pMse->emulateState][4][0] != 0) {
2435 pMse->emulate3Expires = GetTimeInMillis () + pMse->emulate3Timeout;
2436 pMse->emulate3Pending = TRUE;
2437 } else {
2438 pMse->emulate3Pending = FALSE;
2439 }
2440 }
2441
2442 while (change) {
2443 id = ffs(change);
2444 change &= ~(1 << (id - 1));
2445 xf86PostButtonEvent(pInfo->dev, 0, id,
2446 (buttons & (1 << (id - 1))), 0, 0);
2447 }
2448
2449 }
2450}
2451
2452static void
2453MousePostEvent(InputInfoPtr pInfo, int truebuttons,
2454 int dx, int dy, int dz, int dw)
2455{
2456 MouseDevPtr pMse;
2457 int zbutton = 0;
2458 int i, b, buttons = 0;
2459
2460 pMse = pInfo->private;
2461 if (pMse->protocolID == PROT_MMHIT)
2462 b = reverseBits(hitachMap, truebuttons);
2463 else
2464 b = reverseBits(reverseMap, truebuttons);
2465
2466 /* Remap mouse buttons */
2467 b &= (1<<MSE_MAXBUTTONS)-1;
2468 for (i = 0; b; i++) {
2469 if (b & 1)
2470 buttons |= pMse->buttonMap[i];
2471 b >>= 1;
2472 }
2473
2474 /* Map the Z axis movement. */
2475 /* XXX Could this go in the conversion_proc? */
2476 switch (pMse->negativeZ) {
2477 case MSE_NOZMAP: /* do nothing */
2478 break;
2479 case MSE_MAPTOX:
2480 if (dz != 0) {
2481 dx = dz;
2482 dz = 0;
2483 }
2484 break;
2485 case MSE_MAPTOY:
2486 if (dz != 0) {
2487 dy = dz;
2488 dz = 0;
2489 }
2490 break;
2491 default: /* buttons */
2492 buttons &= ~(pMse->negativeZ | pMse->positiveZ
2493 | pMse->negativeW | pMse->positiveW);
2494 if (dw < 0 || dz < -1)
2495 zbutton = pMse->negativeW;
2496 else if (dz < 0)
2497 zbutton = pMse->negativeZ;
2498 else if (dw > 0 || dz > 1)
2499 zbutton = pMse->positiveW;
2500 else if (dz > 0)
2501 zbutton = pMse->positiveZ;
2502 buttons |= zbutton;
2503 dz = 0;
2504 break;
2505 }
2506
2507 /* Apply angle offset */
2508 if (pMse->angleOffset != 0) {
2509 double rad = 3.141592653 * pMse->angleOffset / 180.0;
2510 int ndx = dx;
2511 dx = (int)((dx * cos(rad)) + (dy * sin(rad)) + 0.5);
2512 dy = (int)((dy * cos(rad)) - (ndx * sin(rad)) + 0.5);
2513 }
2514
2515 dx = pMse->invX * dx;
2516 dy = pMse->invY * dy;
2517 if (pMse->flipXY) {
2518 int tmp = dx;
2519 dx = dy;
2520 dy = tmp;
2521 }
2522 MouseDoPostEvent(pInfo, buttons, dx, dy);
2523
2524 /*
2525 * If dz has been mapped to a button `down' event, we need to cook up
2526 * a corresponding button `up' event.
2527 */
2528 if (zbutton) {
2529 buttons &= ~zbutton;
2530 MouseDoPostEvent(pInfo, buttons, 0, 0);
2531 }
2532
2533 pMse->lastButtons = truebuttons;
2534}
2535/******************************************************************
2536 *
2537 * Mouse Setup Code
2538 *
2539 ******************************************************************/
2540/*
2541 * This array is indexed by the MouseProtocolID values, so the order of the
2542 * entries must match that of the MouseProtocolID enum in xf86OSmouse.h.
2543 */
2544static unsigned char proto[PROT_NUMPROTOS][8] = {
2545 /* --header-- ---data--- packet -4th-byte- mouse */
2546 /* mask id mask id bytes mask id flags */
2547 /* Serial mice */
2548 { 0x40, 0x40, 0x40, 0x00, 3, ~0x23, 0x00, MPF_NONE }, /* MicroSoft */
2549 { 0xf8, 0x80, 0x00, 0x00, 5, 0x00, 0xff, MPF_SAFE }, /* MouseSystems */
2550 { 0xe0, 0x80, 0x80, 0x00, 3, 0x00, 0xff, MPF_NONE }, /* MMSeries */
2551 { 0xe0, 0x80, 0x80, 0x00, 3, 0x00, 0xff, MPF_NONE }, /* Logitech */
2552 { 0x40, 0x40, 0x40, 0x00, 3, ~0x23, 0x00, MPF_NONE }, /* MouseMan */
2553 { 0xe0, 0x80, 0x80, 0x00, 3, 0x00, 0xff, MPF_NONE }, /* MM_HitTablet */
2554 { 0x40, 0x40, 0x40, 0x00, 3, ~0x33, 0x00, MPF_NONE }, /* GlidePoint */
2555 { 0x40, 0x40, 0x40, 0x00, 3, ~0x3f, 0x00, MPF_NONE }, /* IntelliMouse */
2556 { 0x40, 0x40, 0x40, 0x00, 3, ~0x33, 0x00, MPF_NONE }, /* ThinkingMouse */
2557 { 0x80, 0x80, 0x80, 0x00, 3, 0x00, 0xff, MPF_NONE }, /* ACECAD */
2558 { 0x40, 0x40, 0x40, 0x00, 4, 0x00, 0xff, MPF_NONE }, /* ValuMouseScroll */
2559 /* PS/2 variants */
2560 { 0xc0, 0x00, 0x00, 0x00, 3, 0x00, 0xff, MPF_NONE }, /* PS/2 mouse */
2561 { 0xc8, 0x08, 0x00, 0x00, 3, 0x00, 0x00, MPF_NONE }, /* genericPS/2 mouse*/
2562 { 0x08, 0x08, 0x00, 0x00, 4, 0x00, 0xff, MPF_NONE }, /* IntelliMouse */
2563 { 0x08, 0x08, 0x00, 0x00, 4, 0x00, 0xff, MPF_NONE }, /* Explorer */
2564 { 0x80, 0x80, 0x00, 0x00, 3, 0x00, 0xff, MPF_NONE }, /* ThinkingMouse */
2565 { 0x08, 0x08, 0x00, 0x00, 3, 0x00, 0xff, MPF_NONE }, /* MouseMan+ */
2566 { 0xc0, 0x00, 0x00, 0x00, 3, 0x00, 0xff, MPF_NONE }, /* GlidePoint */
2567 { 0x08, 0x08, 0x00, 0x00, 4, 0x00, 0xff, MPF_NONE }, /* NetMouse */
2568 { 0xc0, 0x00, 0x00, 0x00, 6, 0x00, 0xff, MPF_NONE }, /* NetScroll */
2569 /* Bus Mouse */
2570 { 0xf8, 0x80, 0x00, 0x00, 5, 0x00, 0xff, MPF_NONE }, /* BusMouse */
2571 { 0xf8, 0x80, 0x00, 0x00, 5, 0x00, 0xff, MPF_NONE }, /* Auto (dummy) */
2572 { 0xf8, 0x80, 0x00, 0x00, 8, 0x00, 0xff, MPF_NONE }, /* SysMouse */
2573};
2574
2575
2576/*
2577 * SetupMouse --
2578 * Sets up the mouse parameters
2579 */
2580static Bool
2581SetupMouse(InputInfoPtr pInfo)
2582{
2583 MouseDevPtr pMse;
2584 unsigned i;
2585 int protoPara[8] = {-1, -1, -1, -1, -1, -1, -1, -1};
2586 const char *name = NULL;
2587 Bool automatic = FALSE;
2588
2589 pMse = pInfo->private;
2590
2591 /* Handle the "Auto" protocol. */
2592 if (pMse->protocolID == PROT_AUTO) {
2593 /*
2594 * We come here when user specifies protocol "auto" in
2595 * the configuration file or thru the xf86misc extensions.
2596 * So we initialize autoprobing here.
2597 * Probe for PnP/OS mouse first. If unsuccessful
2598 * try to guess protocol from incoming data.
2599 */
2600 automatic = TRUE;
2601 pMse->autoProbe = TRUE;
2602 name = autoOSProtocol(pInfo,protoPara);
2603 if (name) {
2604#ifdef EXTMOUSEDEBUG
2605 ErrorF("PnP/OS Mouse detected: %s\n",name);
2606#endif
2607 }
2608 }
2609
2610 SetMouseProto(pMse, pMse->protocolID);
2611
2612 if (automatic) {
2613 if (name) {
2614 /* Possible protoPara overrides from SetupAuto. */
2615 for (i = 0; i < sizeof(pMse->protoPara); i++)
2616 if (protoPara[i] != -1)
2617 pMse->protoPara[i] = protoPara[i];
2618 /* if we come here PnP/OS mouse probing was successful */
2619 } else {
2620#if 1
2621 /* PnP/OS mouse probing wasn't successful; we look at data */
2622#else
2623 xf86Msg(X_ERROR, "%s: cannot determine the mouse protocol\n",
2624 pInfo->name);
2625 return FALSE;
2626#endif
2627 }
2628 }
2629
2630 /*
2631 * If protocol has changed fetch the default options
2632 * for the new protocol.
2633 */
2634 if (pMse->oldProtocolID != pMse->protocolID) {
2635 pointer tmp = NULL;
2636 if ((pMse->protocolID >= 0)
2637 && (pMse->protocolID < PROT_NUMPROTOS)
2638 && mouseProtocols[pMse->protocolID].defaults)
2639 tmp = xf86OptionListCreate(
2640 mouseProtocols[pMse->protocolID].defaults, -1, 0);
2641 pInfo->options = xf86OptionListMerge(pInfo->options, tmp);
2642 /*
2643 * If baudrate is set write it back to the option
2644 * list so that the serial interface code can access
2645 * the new value. Not set means default.
2646 */
2647 if (pMse->baudRate)
2648 xf86ReplaceIntOption(pInfo->options, "BaudRate", pMse->baudRate);
2649 pMse->oldProtocolID = pMse->protocolID; /* hack */
2650 }
2651
2652
2653 /* Set the port parameters. */
2654 if (!automatic)
2655 xf86SetSerial(pInfo->fd, pInfo->options);
2656
2657 if (!initMouseHW(pInfo))
2658 return FALSE;
2659
2660 pMse->protoBufTail = 0;
2661 pMse->inSync = 0;
2662
2663 return TRUE;
2664}
2665
2666/********************************************************************
2667 *
2668 * Mouse HW setup code
2669 *
2670 ********************************************************************/
2671
2672/*
2673** The following lines take care of the Logitech MouseMan protocols.
2674** The "Logitech" protocol is for the old "series 9" Logitech products.
2675** All products since then use the "MouseMan" protocol. Some models
2676** were programmable, but most (all?) of the current models are not.
2677**
2678** NOTE: There are different versions of both MouseMan and TrackMan!
2679** Hence I add another protocol PROT_LOGIMAN, which the user can
2680** specify as MouseMan in his XF86Config file. This entry was
2681** formerly handled as a special case of PROT_MS. However, people
2682** who don't have the middle button problem, can still specify
2683** Microsoft and use PROT_MS.
2684**
2685** By default, these mice should use a 3 byte Microsoft protocol
2686** plus a 4th byte for the middle button. However, the mouse might
2687** have switched to a different protocol before we use it, so I send
2688** the proper sequence just in case.
2689**
2690** NOTE: - all commands to (at least the European) MouseMan have to
2691** be sent at 1200 Baud.
2692** - each command starts with a '*'.
2693** - whenever the MouseMan receives a '*', it will switch back
2694** to 1200 Baud. Hence I have to select the desired protocol
2695** first, then select the baud rate.
2696**
2697** The protocols supported by the (European) MouseMan are:
2698** - 5 byte packed binary protocol, as with the Mouse Systems
2699** mouse. Selected by sequence "*U".
2700** - 2 button 3 byte MicroSoft compatible protocol. Selected
2701** by sequence "*V".
2702** - 3 button 3+1 byte MicroSoft compatible protocol (default).
2703** Selected by sequence "*X".
2704**
2705** The following baud rates are supported:
2706** - 1200 Baud (default). Selected by sequence "*n".
2707** - 9600 Baud. Selected by sequence "*q".
2708**
2709** Selecting a sample rate is no longer supported with the MouseMan!
2710** [CHRIS-211092]
2711*/
2712
2713/*
2714 * Do a reset wrap mode before reset.
2715 */
2716#define do_ps2Reset(x) { \
2717 int i = RETRY_COUNT;\
2718 while (i-- > 0) { \
2719 xf86FlushInput(x->fd); \
2720 if (ps2Reset(x)) break; \
2721 } \
2722 }
2723
2724
2725static Bool
2726initMouseHW(InputInfoPtr pInfo)
2727{
2728 MouseDevPtr pMse = pInfo->private;
2729 const char *s;
2730 unsigned char c;
2731 int speed;
2732 pointer options;
2733 unsigned char *param = NULL;
2734 int paramlen = 0;
2735 int count = RETRY_COUNT;
2736 Bool ps2Init = TRUE;
2737
2738 switch (pMse->protocolID) {
2739 case PROT_LOGI: /* Logitech Mice */
2740 /*
2741 * The baud rate selection command must be sent at the current
2742 * baud rate; try all likely settings.
2743 */
2744 speed = pMse->baudRate;
2745 switch (speed) {
2746 case 9600:
2747 s = "*q";
2748 break;
2749 case 4800:
2750 s = "*p";
2751 break;
2752 case 2400:
2753 s = "*o";
2754 break;
2755 case 1200:
2756 s = "*n";
2757 break;
2758 default:
2759 /* Fallback value */
2760 speed = 1200;
2761 s = "*n";
2762 }
2763 xf86SetSerialSpeed(pInfo->fd, 9600);
2764 xf86WriteSerial(pInfo->fd, s, 2);
2765 usleep(100000);
2766 xf86SetSerialSpeed(pInfo->fd, 4800);
2767 xf86WriteSerial(pInfo->fd, s, 2);
2768 usleep(100000);
2769 xf86SetSerialSpeed(pInfo->fd, 2400);
2770 xf86WriteSerial(pInfo->fd, s, 2);
2771 usleep(100000);
2772 xf86SetSerialSpeed(pInfo->fd, 1200);
2773 xf86WriteSerial(pInfo->fd, s, 2);
2774 usleep(100000);
2775 xf86SetSerialSpeed(pInfo->fd, speed);
2776
2777 /* Select MM series data format. */
2778 xf86WriteSerial(pInfo->fd, "S", 1);
2779 usleep(100000);
2780 /* Set the parameters up for the MM series protocol. */
2781 options = pInfo->options;
2782 xf86CollectInputOptions(pInfo, mmDefaults, NULL);
2783 xf86SetSerial(pInfo->fd, pInfo->options);
2784 pInfo->options = options;
2785
2786 /* Select report rate/frequency. */
2787 if (pMse->sampleRate <= 0) c = 'O'; /* 100 */
2788 else if (pMse->sampleRate <= 15) c = 'J'; /* 10 */
2789 else if (pMse->sampleRate <= 27) c = 'K'; /* 20 */
2790 else if (pMse->sampleRate <= 42) c = 'L'; /* 35 */
2791 else if (pMse->sampleRate <= 60) c = 'R'; /* 50 */
2792 else if (pMse->sampleRate <= 85) c = 'M'; /* 67 */
2793 else if (pMse->sampleRate <= 125) c = 'Q'; /* 100 */
2794 else c = 'N'; /* 150 */
2795 xf86WriteSerial(pInfo->fd, &c, 1);
2796 break;
2797
2798 case PROT_LOGIMAN:
2799 speed = pMse->baudRate;
2800 switch (speed) {
2801 case 9600:
2802 s = "*q";
2803 break;
2804 case 1200:
2805 s = "*n";
2806 break;
2807 default:
2808 /* Fallback value */
2809 speed = 1200;
2810 s = "*n";
2811 }
2812 xf86SetSerialSpeed(pInfo->fd, 1200);
2813 xf86WriteSerial(pInfo->fd, "*n", 2);
2814 xf86WriteSerial(pInfo->fd, "*X", 2);
2815 xf86WriteSerial(pInfo->fd, s, 2);
2816 usleep(100000);
2817 xf86SetSerialSpeed(pInfo->fd, speed);
2818 break;
2819
2820 case PROT_MMHIT: /* MM_HitTablet */
2821 /*
2822 * Initialize Hitachi PUMA Plus - Model 1212E to desired settings.
2823 * The tablet must be configured to be in MM mode, NO parity,
2824 * Binary Format. pMse->sampleRate controls the sensitivity
2825 * of the tablet. We only use this tablet for it's 4-button puck
2826 * so we don't run in "Absolute Mode".
2827 */
2828 xf86WriteSerial(pInfo->fd, "z8", 2); /* Set Parity = "NONE" */
2829 usleep(50000);
2830 xf86WriteSerial(pInfo->fd, "zb", 2); /* Set Format = "Binary" */
2831 usleep(50000);
2832 xf86WriteSerial(pInfo->fd, "@", 1); /* Set Report Mode = "Stream" */
2833 usleep(50000);
2834 xf86WriteSerial(pInfo->fd, "R", 1); /* Set Output Rate = "45 rps" */
2835 usleep(50000);
2836 xf86WriteSerial(pInfo->fd, "I\x20", 2); /* Set Incrememtal Mode "20" */
2837 usleep(50000);
2838 xf86WriteSerial(pInfo->fd, "E", 1); /* Set Data Type = "Relative */
2839 usleep(50000);
2840 /*
2841 * These sample rates translate to 'lines per inch' on the Hitachi
2842 * tablet.
2843 */
2844 if (pMse->sampleRate <= 40) c = 'g';
2845 else if (pMse->sampleRate <= 100) c = 'd';
2846 else if (pMse->sampleRate <= 200) c = 'e';
2847 else if (pMse->sampleRate <= 500) c = 'h';
2848 else if (pMse->sampleRate <= 1000) c = 'j';
2849 else c = 'd';
2850 xf86WriteSerial(pInfo->fd, &c, 1);
2851 usleep(50000);
2852 xf86WriteSerial(pInfo->fd, "\021", 1); /* Resume DATA output */
2853 break;
2854
2855 case PROT_THINKING: /* ThinkingMouse */
2856 /* This mouse may send a PnP ID string, ignore it. */
2857 usleep(200000);
2858 xf86FlushInput(pInfo->fd);
2859 /* Send the command to initialize the beast. */
2860 for (s = "E5E5"; *s; ++s) {
2861 xf86WriteSerial(pInfo->fd, s, 1);
2862 if ((xf86WaitForInput(pInfo->fd, 1000000) <= 0))
2863 break;
2864 xf86ReadSerial(pInfo->fd, &c, 1);
2865 if (c != *s)
2866 break;
2867 }
2868 break;
2869
2870 case PROT_MSC: /* MouseSystems Corp */
2871 usleep(100000);
2872 xf86FlushInput(pInfo->fd);
2873 break;
2874
2875 case PROT_ACECAD:
2876 /* initialize */
2877 /* A nul character resets. */
2878 xf86WriteSerial(pInfo->fd, "", 1);
2879 usleep(50000);
2880 /* Stream out relative mode high resolution increments of 1. */
2881 xf86WriteSerial(pInfo->fd, "@EeI!", 5);
2882 break;
2883
2884 case PROT_BM: /* bus/InPort mouse */
2885 if (osInfo->SetBMRes)
2886 osInfo->SetBMRes(pInfo, pMse->protocol, pMse->sampleRate,
2887 pMse->resolution);
2888 break;
2889
2890 case PROT_GENPS2:
2891 ps2Init = FALSE;
2892 break;
2893
2894 case PROT_PS2:
2895 case PROT_GLIDEPS2:
2896 break;
2897
2898 case PROT_IMPS2: /* IntelliMouse */
2899 {
2900 static unsigned char seq[] = { 243, 200, 243, 100, 243, 80 };
2901 param = seq;
2902 paramlen = sizeof(seq);
2903 }
2904 break;
2905
2906 case PROT_EXPPS2: /* IntelliMouse Explorer */
2907 {
2908 static unsigned char seq[] = { 243, 200, 243, 100, 243, 80,
2909 243, 200, 243, 200, 243, 80 };
2910
2911 param = seq;
2912 paramlen = sizeof(seq);
2913 }
2914 break;
2915
2916 case PROT_NETPS2: /* NetMouse, NetMouse Pro, Mie Mouse */
2917 case PROT_NETSCPS2: /* NetScroll */
2918 {
2919 static unsigned char seq[] = { 232, 3, 230, 230, 230, 233 };
2920
2921 param = seq;
2922 paramlen = sizeof(seq);
2923 }
2924 break;
2925
2926 case PROT_MMPS2: /* MouseMan+, FirstMouse+ */
2927 {
2928 static unsigned char seq[] = { 230, 232, 0, 232, 3, 232, 2, 232, 1,
2929 230, 232, 3, 232, 1, 232, 2, 232, 3 };
2930 param = seq;
2931 paramlen = sizeof(seq);
2932 }
2933 break;
2934
2935 case PROT_THINKPS2: /* ThinkingMouse */
2936 {
2937 static unsigned char seq[] = { 243, 10, 232, 0, 243, 20, 243, 60,
2938 243, 40, 243, 20, 243, 20, 243, 60,
2939 243, 40, 243, 20, 243, 20 };
2940 param = seq;
2941 paramlen = sizeof(seq);
2942 }
2943 break;
2944 case PROT_SYSMOUSE:
2945 if (osInfo->SetMiscRes)
2946 osInfo->SetMiscRes(pInfo, pMse->protocol, pMse->sampleRate,
2947 pMse->resolution);
2948 break;
2949
2950 default:
2951 /* Nothing to do. */
2952 break;
2953 }
2954
2955 if (pMse->class & (MSE_PS2 | MSE_XPS2)) {
2956 /*
2957 * If one part of the PS/2 mouse initialization fails
2958 * redo complete initialization. There are mice which
2959 * have occasional problems with initialization and
2960 * are in an unknown state.
2961 */
2962 if (ps2Init) {
2963 REDO:
2964 do_ps2Reset(pInfo);
2965 if (paramlen > 0) {
2966 if (!ps2SendPacket(pInfo,param,paramlen)) {
2967 usleep(30000);
2968 xf86FlushInput(pInfo->fd);
2969 if (!count--)
2970 return TRUE;
2971 goto REDO;
2972 }
2973 ps2GetDeviceID(pInfo);
2974 usleep(30000);
2975 xf86FlushInput(pInfo->fd);
2976 }
2977
2978 if (osInfo->SetPS2Res) {
2979 osInfo->SetPS2Res(pInfo, pMse->protocol, pMse->sampleRate,
2980 pMse->resolution);
2981 } else {
2982 unsigned char c2[2];
2983
2984 c = 0xE6; /*230*/ /* 1:1 scaling */
2985 if (!ps2SendPacket(pInfo,&c,1)) {
2986 if (!count--)
2987 return TRUE;
2988 goto REDO;
2989 }
2990 c2[0] = 0xF3; /*243*/ /* set sampling rate */
2991 if (pMse->sampleRate > 0) {
2992 if (pMse->sampleRate >= 200)
2993 c2[1] = 200;
2994 else if (pMse->sampleRate >= 100)
2995 c2[1] = 100;
2996 else if (pMse->sampleRate >= 80)
2997 c2[1] = 80;
2998 else if (pMse->sampleRate >= 60)
2999 c2[1] = 60;
3000 else if (pMse->sampleRate >= 40)
3001 c2[1] = 40;
3002 else
3003 c2[1] = 20;
3004 } else {
3005 c2[1] = 100;
3006 }
3007 if (!ps2SendPacket(pInfo,c2,2)) {
3008 if (!count--)
3009 return TRUE;
3010 goto REDO;
3011 }
3012 c2[0] = 0xE8; /*232*/ /* set device resolution */
3013 if (pMse->resolution > 0) {
3014 if (pMse->resolution >= 200)
3015 c2[1] = 3;
3016 else if (pMse->resolution >= 100)
3017 c2[1] = 2;
3018 else if (pMse->resolution >= 50)
3019 c2[1] = 1;
3020 else
3021 c2[1] = 0;
3022 } else {
3023 c2[1] = 3; /* used to be 2, W. uses 3 */
3024 }
3025 if (!ps2SendPacket(pInfo,c2,2)) {
3026 if (!count--)
3027 return TRUE;
3028 goto REDO;
3029 }
3030 usleep(30000);
3031 xf86FlushInput(pInfo->fd);
3032 if (!ps2EnableDataReporting(pInfo)) {
3033 xf86Msg(X_INFO, "%s: ps2EnableDataReporting: failed\n",
3034 pInfo->name);
3035 xf86FlushInput(pInfo->fd);
3036 if (!count--)
3037 return TRUE;
3038 goto REDO;
3039 } else {
3040 xf86Msg(X_INFO, "%s: ps2EnableDataReporting: succeeded\n",
3041 pInfo->name);
3042 }
3043 }
3044 /*
3045 * The PS/2 reset handling needs to be rechecked.
3046 * We need to wait until after the 4.3 release.
3047 */
3048 }
3049 } else {
3050 if (paramlen > 0) {
3051 if (xf86WriteSerial(pInfo->fd, param, paramlen) != paramlen)
3052 xf86Msg(X_ERROR, "%s: Mouse initialization failed\n",
3053 pInfo->name);
3054 usleep(30000);
3055 xf86FlushInput(pInfo->fd);
3056 }
3057 }
3058
3059 return TRUE;
3060}
3061
3062#ifdef SUPPORT_MOUSE_RESET
3063static Bool
3064mouseReset(InputInfoPtr pInfo, unsigned char val)
3065{
3066 MouseDevPtr pMse = pInfo->private;
3067 mousePrivPtr mousepriv = (mousePrivPtr)pMse->mousePriv;
3068 CARD32 prevEvent = mousepriv->lastEvent;
3069 Bool expectReset = FALSE;
3070 Bool ret = FALSE;
3071
3072 mousepriv->lastEvent = GetTimeInMillis();
3073
3074#ifdef EXTMOUSEDEBUG
3075 ErrorF("byte: 0x%x time: %li\n",val,mousepriv->lastEvent);
3076#endif
3077 /*
3078 * We believe that the following is true:
3079 * When the mouse is replugged it will send a reset package
3080 * It takes several seconds to replug a mouse: We don't see
3081 * events for several seconds before we see the replug event package.
3082 * There is no significant delay between consecutive bytes
3083 * of a replug event package.
3084 * There are no bytes sent after the replug event package until
3085 * the mouse is reset.
3086 */
3087
3088 if (mousepriv->current == 0
3089 && (mousepriv->lastEvent - prevEvent) < 4000)
3090 return FALSE;
3091
3092 if (mousepriv->current > 0
3093 && (mousepriv->lastEvent - prevEvent) >= 1000) {
3094 mousepriv->inReset = FALSE;
3095 mousepriv->current = 0;
3096 return FALSE;
3097 }
3098
3099 if (mousepriv->inReset)
3100 mousepriv->inReset = FALSE;
3101
3102#ifdef EXTMOUSEDEBUG
3103 ErrorF("Mouse Current: %i 0x%x\n",mousepriv->current, val);
3104#endif
3105
3106 /* here we put the mouse specific reset detction */
3107 /* They need to do three things: */
3108 /* Check if byte may be a reset byte */
3109 /* If so: Set expectReset TRUE */
3110 /* If convinced: Set inReset TRUE */
3111 /* Register BlockAndWakeupHandler */
3112
3113 /* PS/2 */
3114 {
3115 unsigned char seq[] = { 0xaa, 0x00 };
3116 int len = sizeof(seq);
3117
3118 if (seq[mousepriv->current] == val)
3119 expectReset = TRUE;
3120
3121 if (len == mousepriv->current + 1) {
3122 mousepriv->inReset = TRUE;
3123 mousepriv->expires = GetTimeInMillis() + 1000;
3124
3125#ifdef EXTMOUSEDEBUG
3126 ErrorF("Found PS/2 Reset string\n");
3127#endif
3128 RegisterBlockAndWakeupHandlers (ps2BlockHandler,
3129 ps2WakeupHandler, (pointer) pInfo);
3130 ret = TRUE;
3131 }
3132 }
3133
3134 if (!expectReset)
3135 mousepriv->current = 0;
3136 else
3137 mousepriv->current++;
3138 return ret;
3139}
3140
3141static void
3142ps2BlockHandler(pointer data, struct timeval **waitTime,
3143 pointer LastSelectMask)
3144{
3145 InputInfoPtr pInfo = (InputInfoPtr) data;
3146 MouseDevPtr pMse = (MouseDevPtr) pInfo->private;
3147 mousePrivPtr mousepriv = (mousePrivPtr)pMse->mousePriv;
3148 int ms;
3149
3150 if (mousepriv->inReset) {
3151 ms = mousepriv->expires - GetTimeInMillis ();
3152 if (ms <= 0)
3153 ms = 0;
3154 AdjustWaitForDelay (waitTime, ms);
3155 } else
3156 RemoveBlockAndWakeupHandlers (ps2BlockHandler, ps2WakeupHandler,
3157 (pointer) pInfo);
3158}
3159
3160static void
3161ps2WakeupHandler(pointer data, int i, pointer LastSelectMask)
3162{
3163 InputInfoPtr pInfo = (InputInfoPtr) data;
3164 MouseDevPtr pMse = (MouseDevPtr) pInfo->private;
3165 mousePrivPtr mousepriv = (mousePrivPtr)pMse->mousePriv;
3166 int ms;
3167
3168 if (mousepriv->inReset) {
3169 unsigned char val;
3170 int blocked;
3171
3172 ms = mousepriv->expires - GetTimeInMillis();
3173 if (ms > 0)
3174 return;
3175
3176 blocked = xf86BlockSIGIO ();
3177
3178 xf86MsgVerb(X_INFO,3,
3179 "Got reinsert event: reinitializing PS/2 mouse\n");
3180 val = 0xf4;
3181 if (xf86WriteSerial(pInfo->fd, &val, 1) != 1)
3182 xf86Msg(X_ERROR, "%s: Write to mouse failed\n",
3183 pInfo->name);
3184 xf86UnblockSIGIO(blocked);
3185 }
3186 RemoveBlockAndWakeupHandlers (ps2BlockHandler, ps2WakeupHandler,
3187 (pointer) pInfo);
3188}
3189#endif /* SUPPORT_MOUSE_RESET */
3190
3191/************************************************************
3192 *
3193 * Autoprobe stuff
3194 *
3195 ************************************************************/
3196#ifdef EXTMOUSEDEBUG
3197# define AP_DBG(x) { ErrorF("Autoprobe: "); ErrorF x; }
3198# define AP_DBGC(x) ErrorF x ;
3199# else
3200# define AP_DBG(x) do {} while (0)
3201# define AP_DBGC(x) do {} while (0)
3202#endif
3203
3204MouseProtocolID hardProtocolList[] = { PROT_MSC, PROT_MM, PROT_LOGI,
3205 PROT_LOGIMAN, PROT_MMHIT,
3206 PROT_GLIDE, PROT_IMSERIAL,
3207 PROT_THINKING, PROT_ACECAD,
3208 PROT_THINKPS2, PROT_MMPS2,
3209 PROT_GLIDEPS2,
3210 PROT_NETSCPS2, PROT_EXPPS2,PROT_IMPS2,
3211 PROT_GENPS2, PROT_NETPS2,
3212 PROT_MS,
3213 PROT_UNKNOWN
3214};
3215
3216MouseProtocolID softProtocolList[] = { PROT_MSC, PROT_MM, PROT_LOGI,
3217 PROT_LOGIMAN, PROT_MMHIT,
3218 PROT_GLIDE, PROT_IMSERIAL,
3219 PROT_THINKING, PROT_ACECAD,
3220 PROT_THINKPS2, PROT_MMPS2,
3221 PROT_GLIDEPS2,
3222 PROT_NETSCPS2 ,PROT_IMPS2,
3223 PROT_GENPS2,
3224 PROT_MS,
3225 PROT_UNKNOWN
3226};
3227
3228static const char *
3229autoOSProtocol(InputInfoPtr pInfo, int *protoPara)
3230{
3231 MouseDevPtr pMse = pInfo->private;
3232 const char *name = NULL;
3233 MouseProtocolID protocolID = PROT_UNKNOWN;
3234
3235 /* Check if the OS has a detection mechanism. */
3236 if (osInfo->SetupAuto) {
3237 name = osInfo->SetupAuto(pInfo, protoPara);
3238 if (name) {
3239 protocolID = ProtocolNameToID(name);
3240 switch (protocolID) {
3241 case PROT_UNKNOWN:
3242 /* Check for a builtin OS-specific protocol. */
3243 if (osInfo->CheckProtocol && osInfo->CheckProtocol(name)) {
3244 /* We can only come here if the protocol has been
3245 * changed to auto thru the xf86misc extension
3246 * and we have detected an OS specific builtin
3247 * protocol. Currently we cannot handle this */
3248 name = NULL;
3249 } else
3250 name = NULL;
3251 break;
3252 case PROT_UNSUP:
3253 name = NULL;
3254 break;
3255 default:
3256 break;
3257 }
3258 }
3259 }
3260 if (!name) {
3261 /* A PnP serial mouse? */
3262 protocolID = MouseGetPnpProtocol(pInfo);
3263 if (protocolID >= 0 && protocolID < PROT_NUMPROTOS) {
3264 name = ProtocolIDToName(protocolID);
3265 xf86Msg(X_PROBED, "%s: PnP-detected protocol: \"%s\"\n",
3266 pInfo->name, name);
3267 }
3268 }
3269 if (!name && HAVE_GUESS_PROTOCOL && osInfo->GuessProtocol) {
3270 name = osInfo->GuessProtocol(pInfo, 0);
3271 if (name)
3272 protocolID = ProtocolNameToID(name);
3273 }
3274
3275 if (name) {
3276 pMse->protocolID = protocolID;
3277 }
3278
3279 return name;
3280}
3281
3282/*
3283 * createProtocolList() -- create a list of protocols which may
3284 * match on the incoming data stream.
3285 */
3286static void
3287createProtoList(MouseDevPtr pMse, MouseProtocolID *protoList)
3288{
3289 int i, j, k = 0;
3290 MouseProtocolID prot;
3291 unsigned char *para;
3292 mousePrivPtr mPriv = (mousePrivPtr)pMse->mousePriv;
3293 MouseProtocolID *tmplist = NULL;
3294 int blocked;
3295
3296 AP_DBGC(("Autoprobe: "));
3297 for (i = 0; i < mPriv->count; i++)
3298 AP_DBGC(("%2.2x ", (unsigned char) mPriv->data[i]));
3299 AP_DBGC(("\n"));
3300
3301 blocked = xf86BlockSIGIO ();
3302
3303 /* create a private copy first so we can write in the old list */
3304 if ((tmplist = xalloc(sizeof(MouseProtocolID) * NUM_AUTOPROBE_PROTOS))){
3305 for (i = 0; protoList[i] != PROT_UNKNOWN; i++) {
3306 tmplist[i] = protoList[i];
3307 }
3308 tmplist[i] = PROT_UNKNOWN;
3309 protoList = tmplist;
3310 } else
3311 return;
3312
3313 for (i = 0; ((prot = protoList[i]) != PROT_UNKNOWN
3314 && (k < NUM_AUTOPROBE_PROTOS - 1)) ; i++) {
3315 Bool bad = TRUE;
3316 unsigned char byte = 0;
3317 int count = 0;
3318 int next_header_candidate = 0;
3319 int header_count = 0;
3320
3321 if (!GetProtocol(prot))
3322 continue;
3323 para = proto[prot];
3324
3325 AP_DBG(("Protocol: %s ", ProtocolIDToName(prot)));
3326
3327#ifdef EXTMOUSEDEBUG
3328 for (j = 0; j < 7; j++)
3329 AP_DBGC(("%2.2x ", (unsigned char) para[j]));
3330 AP_DBGC(("\n"));
3331#endif
3332 j = 0;
3333 while (1) {
3334 /* look for header */
3335 while (j < mPriv->count) {
3336 if (((byte = mPriv->data[j++]) & para[0]) == para[1]){
3337 AP_DBG(("found header %2.2x\n",byte));
3338 next_header_candidate = j;
3339 count = 1;
3340 break;
3341 } else {
3342 /*
3343 * Bail ot if number of bytes per package have
3344 * been tested for header.
3345 * Take bytes per package of leading garbage into
3346 * account.
3347 */
3348 if (j > para[4] && ++header_count > para[4]) {
3349 j = mPriv->count;
3350 break;
3351 }
3352 }
3353 }
3354 /* check if remaining data matches protocol */
3355 while (j < mPriv->count) {
3356 byte = mPriv->data[j++];
3357 if (count == para[4]) {
3358 count = 0;
3359 /* check and eat excess byte */
3360 if (((byte & para[0]) != para[1])
3361 && ((byte & para[5]) == para[6])) {
3362 AP_DBG(("excess byte found\n"));
3363 continue;
3364 }
3365 }
3366 if (count == 0) {
3367 /* validate next header */
3368 bad = FALSE;
3369 AP_DBG(("Complete set found\n"));
3370 if ((byte & para[0]) != para[1]) {
3371 AP_DBG(("Autoprobe: header bad\n"));
3372 bad = TRUE;
3373 break;
3374 } else {
3375 count++;
3376 continue;
3377 }
3378 }
3379 /* validate data */
3380 else if (((byte & para[2]) != para[3])
3381 || ((para[7] & MPF_SAFE)
3382 && ((byte & para[0]) == para[1]))) {
3383 AP_DBG(("data bad\n"));
3384 bad = TRUE;
3385 break;
3386 } else {
3387 count ++;
3388 continue;
3389 }
3390 }
3391 if (!bad) {
3392 /* this is a matching protocol */
3393 mPriv->protoList[k++] = prot;
3394 AP_DBG(("Autoprobe: Adding protocol %s to list (entry %i)\n",
3395 ProtocolIDToName(prot),k-1));
3396 break;
3397 }
3398 j = next_header_candidate;
3399 next_header_candidate = 0;
3400 /* we have tested number of bytes per package for header */
3401 if (j > para[4] && ++header_count > para[4])
3402 break;
3403 /* we have not found anything that looks like a header */
3404 if (!next_header_candidate)
3405 break;
3406 AP_DBG(("Looking for new header\n"));
3407 }
3408 }
3409
3410 xf86UnblockSIGIO(blocked);
3411
3412 mPriv->protoList[k] = PROT_UNKNOWN;
3413
3414 xfree(tmplist);
3415}
3416
3417
3418/* This only needs to be done once */
3419void **serialDefaultsList = NULL;
3420
3421/*
3422 * createSerialDefaultsLists() - create a list of the different default
3423 * settings for the serial interface of the known protocols.
3424 */
3425static void
3426createSerialDefaultsList(void)
3427{
3428 int i = 0, j, k;
3429
3430 serialDefaultsList = (void **)xnfalloc(sizeof(void*));
3431 serialDefaultsList[0] = NULL;
3432
3433 for (j = 0; mouseProtocols[j].name; j++) {
3434 if (!mouseProtocols[j].defaults)
3435 continue;
3436 for (k = 0; k < i; k++)
3437 if (mouseProtocols[j].defaults == serialDefaultsList[k])
3438 continue;
3439 i++;
3440 serialDefaultsList = (void**)xnfrealloc(serialDefaultsList,
3441 sizeof(void*)*(i+1));
3442 serialDefaultsList[i-1] = mouseProtocols[j].defaults;
3443 serialDefaultsList[i] = NULL;
3444 }
3445}
3446
3447typedef enum {
3448 STATE_INVALID,
3449 STATE_UNCERTAIN,
3450 STATE_VALID
3451} validState;
3452
3453/* Probing threshold values */
3454#define PROBE_UNCERTAINTY 50
3455#define BAD_CERTAINTY 6
3456#define BAD_INC_CERTAINTY 1
3457#define BAD_INC_CERTAINTY_WHEN_SYNC_LOST 2
3458
3459static validState
3460validCount(mousePrivPtr mPriv, Bool inSync, Bool lostSync)
3461{
3462 if (inSync) {
3463 if (!--mPriv->goodCount) {
3464 /* we are sure to have found the correct protocol */
3465 mPriv->badCount = 0;
3466 return STATE_VALID;
3467 }
3468 AP_DBG(("%i successful rounds to go\n",
3469 mPriv->goodCount));
3470 return STATE_UNCERTAIN;
3471 }
3472
3473
3474 /* We are out of sync again */
3475 mPriv->goodCount = PROBE_UNCERTAINTY;
3476 /* We increase uncertainty of having the correct protocol */
3477 mPriv->badCount+= lostSync ? BAD_INC_CERTAINTY_WHEN_SYNC_LOST
3478 : BAD_INC_CERTAINTY;
3479
3480 if (mPriv->badCount < BAD_CERTAINTY) {
3481 /* We are not convinced yet to have the wrong protocol */
3482 AP_DBG(("Changing protocol after: %i rounds\n",
3483 BAD_CERTAINTY - mPriv->badCount));
3484 return STATE_UNCERTAIN;
3485 }
3486 return STATE_INVALID;
3487}
3488
3489#define RESET_VALIDATION mPriv->goodCount = PROBE_UNCERTAINTY;\
3490 mPriv->badCount = 0;\
3491 mPriv->prevDx = 0;\
3492 mPriv->prevDy = 0;\
3493 mPriv->accDx = 0;\
3494 mPriv->accDy = 0;\
3495 mPriv->acc = 0;
3496
3497static void
3498autoProbeMouse(InputInfoPtr pInfo, Bool inSync, Bool lostSync)
3499{
3500 MouseDevPtr pMse = pInfo->private;
3501 mousePrivPtr mPriv = (mousePrivPtr)pMse->mousePriv;
3502
3503 MouseProtocolID *protocolList = NULL;
3504
3505 while (1) {
3506 switch (mPriv->autoState) {
3507 case AUTOPROBE_GOOD:
3508 if (inSync)
3509 return;
3510 AP_DBG(("State GOOD\n"));
3511 RESET_VALIDATION;
3512 mPriv->autoState = AUTOPROBE_VALIDATE1;
3513 return;
3514 case AUTOPROBE_H_GOOD:
3515 if (inSync)
3516 return;
3517 AP_DBG(("State H_GOOD\n"));
3518 RESET_VALIDATION;
3519 mPriv->autoState = AUTOPROBE_H_VALIDATE2;
3520 return;
3521 case AUTOPROBE_H_NOPROTO:
3522 AP_DBG(("State H_NOPROTO\n"));
3523 mPriv->protocolID = 0;
3524 mPriv->autoState = AUTOPROBE_H_SETPROTO;
3525 break;
3526 case AUTOPROBE_H_SETPROTO:
3527 AP_DBG(("State H_SETPROTO\n"));
3528 if ((pMse->protocolID = hardProtocolList[mPriv->protocolID++])
3529 == PROT_UNKNOWN) {
3530 mPriv->protocolID = 0;
3531 break;
3532 } else if (GetProtocol(pMse->protocolID) && SetupMouse(pInfo)) {
3533 FlushButtons(pMse);
3534 RESET_VALIDATION;
3535 AP_DBG(("Autoprobe: Trying Protocol: %s\n",
3536 ProtocolIDToName(pMse->protocolID)));
3537 mPriv->autoState = AUTOPROBE_H_VALIDATE1;
3538 return;
3539 }
3540 break;
3541 case AUTOPROBE_H_VALIDATE1:
3542 AP_DBG(("State H_VALIDATE1\n"));
3543 switch (validCount(mPriv,inSync,lostSync)) {
3544 case STATE_INVALID:
3545 mPriv->autoState = AUTOPROBE_H_SETPROTO;
3546 break;
3547 case STATE_VALID:
3548 xf86Msg(X_INFO,"Mouse autoprobe: selecting %s protocol\n",
3549 ProtocolIDToName(pMse->protocolID));
3550 mPriv->autoState = AUTOPROBE_H_GOOD;
3551 return;
3552 case STATE_UNCERTAIN:
3553 return;
3554 default:
3555 break;
3556 }
3557 break;
3558 case AUTOPROBE_H_VALIDATE2:
3559 AP_DBG(("State H_VALIDATE2\n"));
3560 switch (validCount(mPriv,inSync,lostSync)) {
3561 case STATE_INVALID:
3562 mPriv->autoState = AUTOPROBE_H_AUTODETECT;
3563 break;
3564 case STATE_VALID:
3565 xf86Msg(X_INFO,"Mouse autoprobe: selecting %s protocol\n",
3566 ProtocolIDToName(pMse->protocolID));
3567 mPriv->autoState = AUTOPROBE_H_GOOD;
3568 return;
3569 case STATE_UNCERTAIN:
3570 return;
3571 }
3572 break;
3573 case AUTOPROBE_H_AUTODETECT:
3574 AP_DBG(("State H_AUTODETECT\n"));
3575 pMse->protocolID = PROT_AUTO;
3576 AP_DBG(("Looking for PnP/OS mouse\n"));
3577 mPriv->count = 0;
3578 SetupMouse(pInfo);
3579 if (pMse->protocolID != PROT_AUTO)
3580 mPriv->autoState = AUTOPROBE_H_GOOD;
3581 else
3582 mPriv->autoState = AUTOPROBE_H_NOPROTO;
3583 break;
3584 case AUTOPROBE_NOPROTO:
3585 AP_DBG(("State NOPROTO\n"));
3586 mPriv->count = 0;
3587 mPriv->serialDefaultsNum = -1;
3588 mPriv->autoState = AUTOPROBE_COLLECT;
3589 break;
3590 case AUTOPROBE_COLLECT:
3591 AP_DBG(("State COLLECT\n"));
3592 if (mPriv->count <= NUM_MSE_AUTOPROBE_BYTES)
3593 return;
3594 protocolList = softProtocolList;
3595 mPriv->autoState = AUTOPROBE_CREATE_PROTOLIST;
3596 break;
3597 case AUTOPROBE_CREATE_PROTOLIST:
3598 AP_DBG(("State CREATE_PROTOLIST\n"));
3599 createProtoList(pMse, protocolList);
3600 mPriv->protocolID = 0;
3601 mPriv->autoState = AUTOPROBE_SWITCH_PROTOCOL;
3602 break;
3603 case AUTOPROBE_AUTODETECT:
3604 AP_DBG(("State AUTODETECT\n"));
3605 pMse->protocolID = PROT_AUTO;
3606 AP_DBG(("Looking for PnP/OS mouse\n"));
3607 mPriv->count = 0;
3608 SetupMouse(pInfo);
3609 if (pMse->protocolID != PROT_AUTO)
3610 mPriv->autoState = AUTOPROBE_GOOD;
3611 else
3612 mPriv->autoState = AUTOPROBE_NOPROTO;
3613 break;
3614 case AUTOPROBE_VALIDATE1:
3615 AP_DBG(("State VALIDATE1\n"));
3616 switch (validCount(mPriv,inSync,lostSync)) {
3617 case STATE_INVALID:
3618 mPriv->autoState = AUTOPROBE_AUTODETECT;
3619 break;
3620 case STATE_VALID:
3621 xf86Msg(X_INFO,"Mouse autoprobe: selecting %s protocol\n",
3622 ProtocolIDToName(pMse->protocolID));
3623 mPriv->autoState = AUTOPROBE_GOOD;
3624 break;
3625 case STATE_UNCERTAIN:
3626 return;
3627 }
3628 break;
3629 case AUTOPROBE_VALIDATE2:
3630 AP_DBG(("State VALIDATE2\n"));
3631 switch (validCount(mPriv,inSync,lostSync)) {
3632 case STATE_INVALID:
3633 protocolList = &mPriv->protoList[mPriv->protocolID];
3634 mPriv->autoState = AUTOPROBE_CREATE_PROTOLIST;
3635 break;
3636 case STATE_VALID:
3637 xf86Msg(X_INFO,"Mouse autoprobe: selecting %s protocol\n",
3638 ProtocolIDToName(pMse->protocolID));
3639 mPriv->autoState = AUTOPROBE_GOOD;
3640 break;
3641 case STATE_UNCERTAIN:
3642 return;
3643 }
3644 break;
3645 case AUTOPROBE_SWITCHSERIAL:
3646 {
3647 pointer serialDefaults;
3648 AP_DBG(("State SWITCHSERIAL\n"));
3649
3650 if (!serialDefaultsList)
3651 createSerialDefaultsList();
3652
3653 AP_DBG(("Switching serial params\n"));
3654 if ((serialDefaults =
3655 serialDefaultsList[++mPriv->serialDefaultsNum]) == NULL) {
3656 mPriv->serialDefaultsNum = 0;
3657 } else {
3658 pointer tmp = xf86OptionListCreate(serialDefaults, -1, 0);
3659 xf86SetSerial(pInfo->fd, tmp);
3660 xf86OptionListFree(tmp);
3661 mPriv->count = 0;
3662 mPriv->autoState = AUTOPROBE_COLLECT;
3663 }
3664 break;
3665 }
3666 case AUTOPROBE_SWITCH_PROTOCOL:
3667 {
3668 MouseProtocolID proto;
3669 void *defaults;
3670 AP_DBG(("State SWITCH_PROTOCOL\n"));
3671 proto = mPriv->protoList[mPriv->protocolID++];
3672 if (proto == PROT_UNKNOWN)
3673 mPriv->autoState = AUTOPROBE_SWITCHSERIAL;
3674 else if (!(defaults = GetProtocol(proto)->defaults)
3675 || (mPriv->serialDefaultsNum == -1
3676 && (defaults == msDefaults))
3677 || (mPriv->serialDefaultsNum != -1
3678 && serialDefaultsList[mPriv->serialDefaultsNum]
3679 == defaults)) {
3680 AP_DBG(("Changing Protocol to %s\n",
3681 ProtocolIDToName(proto)));
3682 SetMouseProto(pMse,proto);
3683 FlushButtons(pMse);
3684 RESET_VALIDATION;
3685 mPriv->autoState = AUTOPROBE_VALIDATE2;
3686 return;
3687 }
3688 break;
3689 }
3690 }
3691 }
3692}
3693
3694static Bool
3695autoGood(MouseDevPtr pMse)
3696{
3697 mousePrivPtr mPriv = (mousePrivPtr)pMse->mousePriv;
3698
3699 if (!pMse->autoProbe)
3700 return TRUE;
3701
3702 switch (mPriv->autoState) {
3703 case AUTOPROBE_GOOD:
3704 case AUTOPROBE_H_GOOD:
3705 return TRUE;
3706 case AUTOPROBE_VALIDATE1: /* @@@ */
3707 case AUTOPROBE_H_VALIDATE1: /* @@@ */
3708 case AUTOPROBE_VALIDATE2:
3709 case AUTOPROBE_H_VALIDATE2:
3710 if (mPriv->goodCount < PROBE_UNCERTAINTY/2)
3711 return TRUE;
3712 default:
3713 return FALSE;
3714 }
3715}
3716
3717
3718#define TOT_THRESHOLD 3000
3719#define VAL_THRESHOLD 40
3720
3721/*
3722 * checkForErraticMovements() -- check if mouse 'jumps around'.
3723 */
3724static void
3725checkForErraticMovements(InputInfoPtr pInfo, int dx, int dy)
3726{
3727 MouseDevPtr pMse = pInfo->private;
3728 mousePrivPtr mPriv = (mousePrivPtr)pMse->mousePriv;
3729#if 1
3730 if (!mPriv->goodCount)
3731 return;
3732#endif
3733#if 0
3734 if (abs(dx - mPriv->prevDx) > 300
3735 || abs(dy - mPriv->prevDy) > 300)
3736 AP_DBG(("erratic1 behaviour\n"));
3737#endif
3738 if (abs(dx) > VAL_THRESHOLD) {
3739 if (sign(dx) == sign(mPriv->prevDx)) {
3740 mPriv->accDx += dx;
3741 if (abs(mPriv->accDx) > mPriv->acc) {
3742 mPriv->acc = abs(mPriv->accDx);
3743 AP_DBG(("acc=%i\n",mPriv->acc));
3744 }
3745 else
3746 AP_DBG(("accDx=%i\n",mPriv->accDx));
3747 } else {
3748 mPriv->accDx = 0;
3749 }
3750 }
3751
3752 if (abs(dy) > VAL_THRESHOLD) {
3753 if (sign(dy) == sign(mPriv->prevDy)) {
3754 mPriv->accDy += dy;
3755 if (abs(mPriv->accDy) > mPriv->acc) {
3756 mPriv->acc = abs(mPriv->accDy);
3757 AP_DBG(("acc: %i\n",mPriv->acc));
3758 } else
3759 AP_DBG(("accDy=%i\n",mPriv->accDy));
3760 } else {
3761 mPriv->accDy = 0;
3762 }
3763 }
3764 mPriv->prevDx = dx;
3765 mPriv->prevDy = dy;
3766 if (mPriv->acc > TOT_THRESHOLD) {
3767 mPriv->goodCount = PROBE_UNCERTAINTY;
3768 mPriv->prevDx = 0;
3769 mPriv->prevDy = 0;
3770 mPriv->accDx = 0;
3771 mPriv->accDy = 0;
3772 mPriv->acc = 0;
3773 AP_DBG(("erratic2 behaviour\n"));
3774 autoProbeMouse(pInfo, FALSE,TRUE);
3775 }
3776}
3777
3778static void
3779SetMouseProto(MouseDevPtr pMse, MouseProtocolID protocolID)
3780{
3781 pMse->protocolID = protocolID;
3782 pMse->protocol = ProtocolIDToName(pMse->protocolID);
3783 pMse->class = ProtocolIDToClass(pMse->protocolID);
3784 if ((pMse->protocolID >= 0) && (pMse->protocolID < PROT_NUMPROTOS))
3785 memcpy(pMse->protoPara, proto[pMse->protocolID],
3786 sizeof(pMse->protoPara));
3787
3788 if (pMse->emulate3ButtonsSoft)
3789 pMse->emulate3Buttons = TRUE;
3790}
3791
3792/*
3793 * collectData() -- collect data bytes sent by mouse.
3794 */
3795static Bool
3796collectData(MouseDevPtr pMse, unsigned char u)
3797{
3798 mousePrivPtr mPriv = (mousePrivPtr)pMse->mousePriv;
3799 if (mPriv->count < NUM_MSE_AUTOPROBE_TOTAL) {
3800 mPriv->data[mPriv->count++] = u;
3801 if (mPriv->count <= NUM_MSE_AUTOPROBE_BYTES) {
3802 return TRUE;
3803 }
3804 }
3805 return FALSE;
3806}
3807
3808/**************** end of autoprobe stuff *****************/
3809
3810
3811
3812#ifdef XFree86LOADER
3813ModuleInfoRec MouseInfo = {
3814 1,
3815 "MOUSE",
3816 NULL,
3817 0,
3818 MouseAvailableOptions,
3819};
3820
3821static void
3822xf86MouseUnplug(pointer p)
3823{
3824}
3825static pointer
3826xf86MousePlug(pointer module,
3827 pointer options,
3828 int *errmaj,
3829 int *errmin)
3830{
3831 static Bool Initialised = FALSE;
3832
3833 if (!Initialised) {
3834 Initialised = TRUE;
3835#ifndef REMOVE_LOADER_CHECK_MODULE_INFO
3836 if (xf86LoaderCheckSymbol("xf86AddModuleInfo"))
3837#endif
3838 xf86AddModuleInfo(&MouseInfo, module);
3839 }
3840
3841 xf86AddInputDriver(&MOUSE, module, 0);
3842
3843 return module;
3844}
3845
3846static XF86ModuleVersionInfo xf86MouseVersionRec =
3847{
3848#ifdef VBOX
3849 "vboxmouse",
3850 VBOX_VENDOR,
3851#else
3852 "mouse",
3853 MODULEVENDORSTRING,
3854#endif
3855 MODINFOSTRING1,
3856 MODINFOSTRING2,
3857 XORG_VERSION_CURRENT,
3858 1, 1, 0,
3859 ABI_CLASS_XINPUT,
3860 ABI_XINPUT_VERSION,
3861 MOD_CLASS_XINPUT,
3862 {0, 0, 0, 0} /* signature, to be patched into the file by */
3863 /* a tool */
3864};
3865
3866#ifdef VBOX
3867_X_EXPORT XF86ModuleData vboxmouseModuleData = {
3868 &xf86MouseVersionRec,
3869 xf86MousePlug,
3870 xf86MouseUnplug
3871};
3872#else
3873_X_EXPORT XF86ModuleData mouseModuleData = {
3874 &xf86MouseVersionRec,
3875 xf86MousePlug,
3876 xf86MouseUnplug
3877};
3878#endif
3879
3880/*
3881 Look at hitachi device stuff.
3882*/
3883#endif /* XFree86LOADER */
3884
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette