VirtualBox

source: vbox/trunk/src/VBox/Additions/x11/xmouse/xorg14/mouse.c@ 9617

Last change on this file since 9617 was 9617, checked in by vboxsync, 17 years ago

Additions/linux: added tracing code to the Xorg 1.4 mouse driver

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