VirtualBox

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

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

re-export x11

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