VirtualBox

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

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

X11 additions: branding

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