VirtualBox

source: vbox/trunk/src/VBox/Additions/linux/xmouse/xorg70/mouse.c@ 1260

Last change on this file since 1260 was 1260, checked in by vboxsync, 18 years ago

display VBOX_VERSION_STRING in Xorg.log.0 for better bug reports from end-users

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