VirtualBox

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

Last change on this file since 32915 was 31415, checked in by vboxsync, 14 years ago

Additions/x11/vboxmouse: fix some warnings

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