VirtualBox

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

Last change on this file since 10552 was 9709, checked in by vboxsync, 16 years ago

play safe.

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