VirtualBox

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

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

Additions/FreeBSD: initial commit

Mostly tested on FreeBSD 7.0.

Working: the additions kernel module

VBoxService (working on FreeBSD 6.2 too)
vboxvideo

Partially working: vboxmouse - 100% CPU usage and clicking does not work yet
Not tested: xclient
TODO: shared folder module is currently a stub

Installer and startup scripts

  • Property svn:eol-style set to native
File size: 110.1 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
1060/* ImPS/2 is not supported on FreeBSD */
1061# ifdef RT_OS_FREEBSD
1062 protocol = "PS/2";
1063# else
1064 protocol = "ImPS/2";
1065# endif
1066 protocolFrom = X_CONFIG;
1067#else
1068 /* Find the protocol type. */
1069 protocol = xf86SetStrOption(dev->commonOptions, "Protocol", NULL);
1070 if (protocol) {
1071 protocolFrom = X_CONFIG;
1072 } else if (osInfo->DefaultProtocol) {
1073 protocol = osInfo->DefaultProtocol();
1074 protocolFrom = X_DEFAULT;
1075 }
1076 if (!protocol) {
1077 xf86Msg(X_ERROR, "%s: No Protocol specified\n", pInfo->name);
1078 return pInfo;
1079 }
1080#endif
1081
1082 /* Default Mapping: 1 2 3 8 9 10 11 ... */
1083 for (i = 0; i < MSE_MAXBUTTONS; i++)
1084 pMse->buttonMap[i] = 1 << (i > 2 && i < MSE_MAXBUTTONS-4 ? i+4 : i);
1085
1086 protocolID = ProtocolNameToID(protocol);
1087 do {
1088 detected = TRUE;
1089 switch (protocolID) {
1090 case PROT_AUTO:
1091 if (osInfo->SetupAuto) {
1092 if ((osProt = osInfo->SetupAuto(pInfo,NULL))) {
1093 MouseProtocolID id = ProtocolNameToID(osProt);
1094 if (id == PROT_UNKNOWN || id == PROT_UNSUP) {
1095 protocolID = id;
1096 protocol = osProt;
1097 detected = FALSE;
1098 }
1099 }
1100 }
1101 break;
1102 case PROT_UNKNOWN:
1103 /* Check for a builtin OS-specific protocol,
1104 * and call its PreInit. */
1105 if (osInfo->CheckProtocol
1106 && osInfo->CheckProtocol(protocol)) {
1107 if (!xf86CheckStrOption(dev->commonOptions, "Device", NULL) &&
1108 HAVE_FIND_DEVICE && osInfo->FindDevice) {
1109 xf86Msg(X_WARNING, "%s: No Device specified, "
1110 "looking for one...\n", pInfo->name);
1111 if (!osInfo->FindDevice(pInfo, protocol, 0)) {
1112 xf86Msg(X_ERROR, "%s: Cannot find which device "
1113 "to use.\n", pInfo->name);
1114 } else
1115 deviceFrom = X_PROBED;
1116 }
1117 if (osInfo->PreInit) {
1118 osInfo->PreInit(pInfo, protocol, 0);
1119 }
1120 return pInfo;
1121 }
1122 xf86Msg(X_ERROR, "%s: Unknown protocol \"%s\"\n",
1123 pInfo->name, protocol);
1124 return pInfo;
1125 break;
1126 case PROT_UNSUP:
1127 xf86Msg(X_ERROR,
1128 "%s: Protocol \"%s\" is not supported on this "
1129 "platform\n", pInfo->name, protocol);
1130 return pInfo;
1131 break;
1132 default:
1133 break;
1134
1135 }
1136 } while (!detected);
1137
1138 if (!xf86CheckStrOption(dev->commonOptions, "Device", NULL) &&
1139 HAVE_FIND_DEVICE && osInfo->FindDevice) {
1140 xf86Msg(X_WARNING, "%s: No Device specified, looking for one...\n",
1141 pInfo->name);
1142 if (!osInfo->FindDevice(pInfo, protocol, 0)) {
1143 xf86Msg(X_ERROR, "%s: Cannot find which device to use.\n",
1144 pInfo->name);
1145 } else {
1146 deviceFrom = X_PROBED;
1147 xf86MarkOptionUsedByName(dev->commonOptions, "Device");
1148 }
1149 }
1150
1151 device = xf86CheckStrOption(dev->commonOptions, "Device", NULL);
1152 if (device)
1153 xf86Msg(deviceFrom, "%s: Device: \"%s\"\n", pInfo->name, device);
1154
1155 xf86Msg(protocolFrom, "%s: Protocol: \"%s\"\n", pInfo->name, protocol);
1156 if (!(pProto = GetProtocol(protocolID)))
1157 return pInfo;
1158
1159 pMse->protocolID = protocolID;
1160 pMse->oldProtocolID = protocolID; /* hack */
1161
1162 pMse->autoProbe = FALSE;
1163 /* Collect the options, and process the common options. */
1164 xf86CollectInputOptions(pInfo, pProto->defaults, NULL);
1165 xf86ProcessCommonOptions(pInfo, pInfo->options);
1166
1167 /* XXX should handle this OS dependency elsewhere. */
1168#ifndef __OS2ELF__
1169 /* OS/2 has a mouse handled by the OS - it cannot fail here */
1170
1171 /* Check if the device can be opened. */
1172 pInfo->fd = xf86OpenSerial(pInfo->options);
1173 if (pInfo->fd == -1) {
1174 if (xf86GetAllowMouseOpenFail())
1175 xf86Msg(X_WARNING, "%s: cannot open input device\n", pInfo->name);
1176 else {
1177 xf86Msg(X_ERROR, "%s: cannot open input device\n", pInfo->name);
1178 if (pMse->mousePriv)
1179 xfree(pMse->mousePriv);
1180 xfree(pMse);
1181 pInfo->private = NULL;
1182 return pInfo;
1183 }
1184 }
1185 xf86CloseSerial(pInfo->fd);
1186#endif
1187 pInfo->fd = -1;
1188
1189#ifdef VBOX
1190 mPriv = NULL; /* later */
1191#else
1192 if (!(mPriv = (pointer) xcalloc(sizeof(mousePrivRec), 1)))
1193 return pInfo;
1194#endif
1195 pMse->mousePriv = mPriv;
1196 pMse->CommonOptions(pInfo);
1197 pMse->checkMovements = checkForErraticMovements;
1198 pMse->autoProbeMouse = autoProbeMouse;
1199 pMse->collectData = collectData;
1200 pMse->dataGood = autoGood;
1201
1202 MouseHWOptions(pInfo);
1203 MouseSerialOptions(pInfo);
1204
1205 pInfo->flags |= XI86_CONFIGURED;
1206 return pInfo;
1207}
1208
1209
1210static void
1211MouseReadInput(InputInfoPtr pInfo)
1212{
1213 MouseDevPtr pMse;
1214 int j, buttons, dx, dy, dz, dw, baddata;
1215 int pBufP;
1216 int c;
1217 unsigned char *pBuf, u;
1218
1219
1220 pMse = pInfo->private;
1221 pBufP = pMse->protoBufTail;
1222 pBuf = pMse->protoBuf;
1223
1224 /*
1225 * Set blocking to -1 on the first call because we know there is data to
1226 * read. Xisb automatically clears it after one successful read so that
1227 * succeeding reads are preceeded by a select with a 0 timeout to prevent
1228 * read from blocking indefinitely.
1229 */
1230 XisbBlockDuration(pMse->buffer, -1);
1231
1232 while ((c = XisbRead(pMse->buffer)) >= 0) {
1233 u = (unsigned char)c;
1234
1235#if defined (EXTMOUSEDEBUG) || defined (MOUSEDATADEBUG)
1236 ErrorF("mouse byte: %2.2x\n",u);
1237#endif
1238
1239#if 1
1240 /* if we do autoprobing collect the data */
1241 if (pMse->collectData && pMse->autoProbe)
1242 if (pMse->collectData(pMse,u))
1243 continue;
1244#endif
1245#ifdef SUPPORT_MOUSE_RESET
1246 if (mouseReset(pInfo,u)) {
1247 pBufP = 0;
1248 continue;
1249 }
1250#endif
1251 if (pBufP >= pMse->protoPara[4]) {
1252 /*
1253 * Buffer contains a full packet, which has already been processed:
1254 * Empty the buffer and check for optional 4th byte, which will be
1255 * processed directly, without being put into the buffer first.
1256 */
1257 pBufP = 0;
1258 if ((u & pMse->protoPara[0]) != pMse->protoPara[1] &&
1259 (u & pMse->protoPara[5]) == pMse->protoPara[6]) {
1260 /*
1261 * Hack for Logitech MouseMan Mouse - Middle button
1262 *
1263 * Unfortunately this mouse has variable length packets: the
1264 * standard Microsoft 3 byte packet plus an optional 4th byte
1265 * whenever the middle button status changes.
1266 *
1267 * We have already processed the standard packet with the
1268 * movement and button info. Now post an event message with
1269 * the old status of the left and right buttons and the
1270 * updated middle button.
1271 */
1272 /*
1273 * Even worse, different MouseMen and TrackMen differ in the
1274 * 4th byte: some will send 0x00/0x20, others 0x01/0x21, or
1275 * even 0x02/0x22, so I have to strip off the lower bits.
1276 * [CHRIS-211092]
1277 *
1278 * [JCH-96/01/21]
1279 * HACK for ALPS "fourth button". (It's bit 0x10 of the
1280 * "fourth byte" and it is activated by tapping the glidepad
1281 * with the finger! 8^) We map it to bit bit3, and the
1282 * reverse map in xf86Events just has to be extended so that
1283 * it is identified as Button 4. The lower half of the
1284 * reverse-map may remain unchanged.
1285 */
1286 /*
1287 * [KAZU-030897]
1288 * Receive the fourth byte only when preceeding three bytes
1289 * have been detected (pBufP >= pMse->protoPara[4]). In the
1290 * previous versions, the test was pBufP == 0; we may have
1291 * mistakingly received a byte even if we didn't see anything
1292 * preceeding the byte.
1293 */
1294#ifdef EXTMOUSEDEBUG
1295 ErrorF("mouse 4th byte %02x\n",u);
1296#endif
1297 dx = dy = dz = dw = 0;
1298 buttons = 0;
1299 switch (pMse->protocolID) {
1300
1301 /*
1302 * [KAZU-221197]
1303 * IntelliMouse, NetMouse (including NetMouse Pro) and Mie
1304 * Mouse always send the fourth byte, whereas the fourth byte
1305 * is optional for GlidePoint and ThinkingMouse. The fourth
1306 * byte is also optional for MouseMan+ and FirstMouse+ in
1307 * their native mode. It is always sent if they are in the
1308 * IntelliMouse compatible mode.
1309 */
1310 case PROT_IMSERIAL: /* IntelliMouse, NetMouse, Mie Mouse,
1311 MouseMan+ */
1312 dz = (u & 0x08) ?
1313 (u & 0x0f) - 16 : (u & 0x0f);
1314 if ((dz >= 7) || (dz <= -7))
1315 dz = 0;
1316 buttons |= ((int)(u & 0x10) >> 3)
1317 | ((int)(u & 0x20) >> 2)
1318 | (pMse->lastButtons & 0x05);
1319 break;
1320
1321 case PROT_GLIDE:
1322 case PROT_THINKING:
1323 buttons |= ((int)(u & 0x10) >> 1);
1324 /* fall through */
1325
1326 default:
1327 buttons |= ((int)(u & 0x20) >> 4) |
1328 (pMse->lastButtons & 0x05);
1329 break;
1330 }
1331 goto post_event;
1332 }
1333 }
1334 /* End of packet buffer flush and 4th byte hack. */
1335
1336 /*
1337 * Append next byte to buffer (which is empty or contains an
1338 * incomplete packet); iterate if packet (still) not complete.
1339 */
1340 pBuf[pBufP++] = u;
1341 if (pBufP != pMse->protoPara[4]) continue;
1342#ifdef EXTMOUSEDEBUG2
1343 {
1344 int i;
1345 ErrorF("received %d bytes",pBufP);
1346 for ( i=0; i < pBufP; i++)
1347 ErrorF(" %02x",pBuf[i]);
1348 ErrorF("\n");
1349 }
1350#endif
1351
1352 /*
1353 * Hack for resyncing: We check here for a package that is:
1354 * a) illegal (detected by wrong data-package header)
1355 * b) invalid (0x80 == -128 and that might be wrong for MouseSystems)
1356 * c) bad header-package
1357 *
1358 * NOTE: b) is a violation of the MouseSystems-Protocol, since values
1359 * of -128 are allowed, but since they are very seldom we can
1360 * easily use them as package-header with no button pressed.
1361 * NOTE/2: On a PS/2 mouse any byte is valid as a data byte.
1362 * Furthermore, 0x80 is not valid as a header byte. For a PS/2
1363 * mouse we skip checking data bytes. For resyncing a PS/2
1364 * mouse we require the two most significant bits in the header
1365 * byte to be 0. These are the overflow bits, and in case of
1366 * an overflow we actually lose sync. Overflows are very rare,
1367 * however, and we quickly gain sync again after an overflow
1368 * condition. This is the best we can do. (Actually, we could
1369 * use bit 0x08 in the header byte for resyncing, since that
1370 * bit is supposed to be always on, but nobody told Microsoft...)
1371 */
1372
1373 /*
1374 * [KAZU,OYVIND-120398]
1375 * The above hack is wrong! Because of b) above, we shall see
1376 * erroneous mouse events so often when the MouseSystem mouse is
1377 * moved quickly. As for the PS/2 and its variants, we don't need
1378 * to treat them as special cases, because protoPara[2] and
1379 * protoPara[3] are both 0x00 for them, thus, any data bytes will
1380 * never be discarded. 0x80 is rejected for MMSeries, Logitech
1381 * and MMHittab protocols, because protoPara[2] and protoPara[3]
1382 * are 0x80 and 0x00 respectively. The other protocols are 7-bit
1383 * protocols; there is no use checking 0x80.
1384 *
1385 * All in all we should check the condition a) only.
1386 */
1387
1388 /*
1389 * [OYVIND-120498]
1390 * Check packet for valid data:
1391 * If driver is in sync with datastream, the packet is considered
1392 * bad if any byte (header and/or data) contains an invalid value.
1393 *
1394 * If packet is bad, we discard the first byte and shift the buffer.
1395 * Next iteration will then check the new situation for validity.
1396 *
1397 * If flag MF_SAFE is set in proto[7] and the driver
1398 * is out of sync, the packet is also considered bad if
1399 * any of the data bytes contains a valid header byte value.
1400 * This situation could occur if the buffer contains
1401 * the tail of one packet and the header of the next.
1402 *
1403 * Note: The driver starts in out-of-sync mode (pMse->inSync = 0).
1404 */
1405
1406 baddata = 0;
1407
1408 /* All databytes must be valid. */
1409 for (j = 1; j < pBufP; j++ )
1410 if ((pBuf[j] & pMse->protoPara[2]) != pMse->protoPara[3])
1411 baddata = 1;
1412
1413 /* If out of sync, don't mistake a header byte for data. */
1414 if ((pMse->protoPara[7] & MPF_SAFE) && !pMse->inSync)
1415 for (j = 1; j < pBufP; j++ )
1416 if ((pBuf[j] & pMse->protoPara[0]) == pMse->protoPara[1])
1417 baddata = 1;
1418
1419 /* Accept or reject the packet ? */
1420 if ((pBuf[0] & pMse->protoPara[0]) != pMse->protoPara[1] || baddata) {
1421 if (pMse->inSync) {
1422#ifdef EXTMOUSEDEBUG
1423 ErrorF("mouse driver lost sync\n");
1424#endif
1425 }
1426#ifdef EXTMOUSEDEBUG
1427 ErrorF("skipping byte %02x\n",*pBuf);
1428#endif
1429 /* Tell auto probe that we are out of sync */
1430 if (pMse->autoProbeMouse && pMse->autoProbe)
1431 pMse->autoProbeMouse(pInfo, FALSE, pMse->inSync);
1432 pMse->protoBufTail = --pBufP;
1433 for (j = 0; j < pBufP; j++)
1434 pBuf[j] = pBuf[j+1];
1435 pMse->inSync = 0;
1436 continue;
1437 }
1438 /* Tell auto probe that we were successful */
1439 if (pMse->autoProbeMouse && pMse->autoProbe)
1440 pMse->autoProbeMouse(pInfo, TRUE, FALSE);
1441
1442 if (!pMse->inSync) {
1443#ifdef EXTMOUSEDEBUG
1444 ErrorF("mouse driver back in sync\n");
1445#endif
1446 pMse->inSync = 1;
1447 }
1448
1449 if (!pMse->dataGood(pMse))
1450 continue;
1451
1452 /*
1453 * Packet complete and verified, now process it ...
1454 */
1455 REDO_INTERPRET:
1456 dz = dw = 0;
1457 switch (pMse->protocolID) {
1458 case PROT_LOGIMAN: /* MouseMan / TrackMan [CHRIS-211092] */
1459 case PROT_MS: /* Microsoft */
1460 if (pMse->chordMiddle)
1461 buttons = (((int) pBuf[0] & 0x30) == 0x30) ? 2 :
1462 ((int)(pBuf[0] & 0x20) >> 3)
1463 | ((int)(pBuf[0] & 0x10) >> 4);
1464 else
1465 buttons = (pMse->lastButtons & 2)
1466 | ((int)(pBuf[0] & 0x20) >> 3)
1467 | ((int)(pBuf[0] & 0x10) >> 4);
1468 dx = (char)(((pBuf[0] & 0x03) << 6) | (pBuf[1] & 0x3F));
1469 dy = (char)(((pBuf[0] & 0x0C) << 4) | (pBuf[2] & 0x3F));
1470 break;
1471
1472 case PROT_GLIDE: /* ALPS GlidePoint */
1473 case PROT_THINKING: /* ThinkingMouse */
1474 case PROT_IMSERIAL: /* IntelliMouse, NetMouse, Mie Mouse, MouseMan+ */
1475 buttons = (pMse->lastButtons & (8 + 2))
1476 | ((int)(pBuf[0] & 0x20) >> 3)
1477 | ((int)(pBuf[0] & 0x10) >> 4);
1478 dx = (char)(((pBuf[0] & 0x03) << 6) | (pBuf[1] & 0x3F));
1479 dy = (char)(((pBuf[0] & 0x0C) << 4) | (pBuf[2] & 0x3F));
1480 break;
1481
1482 case PROT_MSC: /* Mouse Systems Corp */
1483 buttons = (~pBuf[0]) & 0x07;
1484 dx = (char)(pBuf[1]) + (char)(pBuf[3]);
1485 dy = - ((char)(pBuf[2]) + (char)(pBuf[4]));
1486 break;
1487
1488 case PROT_MMHIT: /* MM_HitTablet */
1489 buttons = pBuf[0] & 0x07;
1490 if (buttons != 0)
1491 buttons = 1 << (buttons - 1);
1492 dx = (pBuf[0] & 0x10) ? pBuf[1] : - pBuf[1];
1493 dy = (pBuf[0] & 0x08) ? - pBuf[2] : pBuf[2];
1494 break;
1495
1496 case PROT_ACECAD: /* ACECAD */
1497 /* ACECAD is almost exactly like MM but the buttons are different */
1498 buttons = (pBuf[0] & 0x02) | ((pBuf[0] & 0x04) >> 2) |
1499 ((pBuf[0] & 1) << 2);
1500 dx = (pBuf[0] & 0x10) ? pBuf[1] : - pBuf[1];
1501 dy = (pBuf[0] & 0x08) ? - pBuf[2] : pBuf[2];
1502 break;
1503
1504 case PROT_MM: /* MM Series */
1505 case PROT_LOGI: /* Logitech Mice */
1506 buttons = pBuf[0] & 0x07;
1507 dx = (pBuf[0] & 0x10) ? pBuf[1] : - pBuf[1];
1508 dy = (pBuf[0] & 0x08) ? - pBuf[2] : pBuf[2];
1509 break;
1510
1511 case PROT_BM: /* BusMouse */
1512 buttons = (~pBuf[0]) & 0x07;
1513 dx = (char)pBuf[1];
1514 dy = - (char)pBuf[2];
1515 break;
1516
1517 case PROT_PS2: /* PS/2 mouse */
1518 case PROT_GENPS2: /* generic PS/2 mouse */
1519 buttons = (pBuf[0] & 0x04) >> 1 | /* Middle */
1520 (pBuf[0] & 0x02) >> 1 | /* Right */
1521 (pBuf[0] & 0x01) << 2; /* Left */
1522 dx = (pBuf[0] & 0x10) ? (int)pBuf[1]-256 : (int)pBuf[1];
1523 dy = (pBuf[0] & 0x20) ? -((int)pBuf[2]-256) : -(int)pBuf[2];
1524 break;
1525
1526 /* PS/2 mouse variants */
1527 case PROT_IMPS2: /* IntelliMouse PS/2 */
1528 case PROT_NETPS2: /* NetMouse PS/2 */
1529 buttons = (pBuf[0] & 0x04) >> 1 | /* Middle */
1530 (pBuf[0] & 0x02) >> 1 | /* Right */
1531 (pBuf[0] & 0x01) << 2 | /* Left */
1532 (pBuf[0] & 0x40) >> 3 | /* button 4 */
1533 (pBuf[0] & 0x80) >> 3; /* button 5 */
1534 dx = (pBuf[0] & 0x10) ? pBuf[1]-256 : pBuf[1];
1535 dy = (pBuf[0] & 0x20) ? -(pBuf[2]-256) : -pBuf[2];
1536 /*
1537 * The next cast must be 'signed char' for platforms (like PPC)
1538 * where char defaults to unsigned.
1539 */
1540 dz = (signed char)(pBuf[3] | ((pBuf[3] & 0x08) ? 0xf8 : 0));
1541 if ((pBuf[3] & 0xf8) && ((pBuf[3] & 0xf8) != 0xf8)) {
1542 if (pMse->autoProbe) {
1543 SetMouseProto(pMse, PROT_EXPPS2);
1544 xf86Msg(X_INFO,
1545 "Mouse autoprobe: Changing protocol to %s\n",
1546 pMse->protocol);
1547
1548 goto REDO_INTERPRET;
1549 } else
1550 dz = 0;
1551 }
1552 break;
1553
1554 case PROT_EXPPS2: /* IntelliMouse Explorer PS/2 */
1555 if (pMse->autoProbe && (pBuf[3] & 0xC0)) {
1556 SetMouseProto(pMse, PROT_IMPS2);
1557 xf86Msg(X_INFO,"Mouse autoprobe: Changing protocol to %s\n",
1558 pMse->protocol);
1559 goto REDO_INTERPRET;
1560 }
1561 buttons = (pBuf[0] & 0x04) >> 1 | /* Middle */
1562 (pBuf[0] & 0x02) >> 1 | /* Right */
1563 (pBuf[0] & 0x01) << 2 | /* Left */
1564 (pBuf[3] & 0x10) >> 1 | /* button 4 */
1565 (pBuf[3] & 0x20) >> 1; /* button 5 */
1566 dx = (pBuf[0] & 0x10) ? pBuf[1]-256 : pBuf[1];
1567 dy = (pBuf[0] & 0x20) ? -(pBuf[2]-256) : -pBuf[2];
1568 dz = (pBuf[3] & 0x08) ? (pBuf[3] & 0x0f) - 16 : (pBuf[3] & 0x0f);
1569 break;
1570
1571 case PROT_MMPS2: /* MouseMan+ PS/2 */
1572 buttons = (pBuf[0] & 0x04) >> 1 | /* Middle */
1573 (pBuf[0] & 0x02) >> 1 | /* Right */
1574 (pBuf[0] & 0x01) << 2; /* Left */
1575 dx = (pBuf[0] & 0x10) ? pBuf[1] - 256 : pBuf[1];
1576 if (((pBuf[0] & 0x48) == 0x48) &&
1577 (abs(dx) > 191) &&
1578 ((((pBuf[2] & 0x03) << 2) | 0x02) == (pBuf[1] & 0x0f))) {
1579 /* extended data packet */
1580 switch ((((pBuf[0] & 0x30) >> 2) | ((pBuf[1] & 0x30) >> 4))) {
1581 case 1: /* wheel data packet */
1582 buttons |= ((pBuf[2] & 0x10) ? 0x08 : 0) | /* 4th button */
1583 ((pBuf[2] & 0x20) ? 0x10 : 0); /* 5th button */
1584 dx = dy = 0;
1585 dz = (pBuf[2] & 0x08) ? (pBuf[2] & 0x0f) - 16 :
1586 (pBuf[2] & 0x0f);
1587 break;
1588 case 2: /* Logitech reserves this packet type */
1589 /*
1590 * IBM ScrollPoint uses this packet to encode its
1591 * stick movement.
1592 */
1593 buttons |= (pMse->lastButtons & ~0x07);
1594 dx = dy = 0;
1595 dz = (pBuf[2] & 0x80) ? ((pBuf[2] >> 4) & 0x0f) - 16 :
1596 ((pBuf[2] >> 4) & 0x0f);
1597 dw = (pBuf[2] & 0x08) ? (pBuf[2] & 0x0f) - 16 :
1598 (pBuf[2] & 0x0f);
1599 break;
1600 case 0: /* device type packet - shouldn't happen */
1601 default:
1602 buttons |= (pMse->lastButtons & ~0x07);
1603 dx = dy = 0;
1604 dz = 0;
1605 break;
1606 }
1607 } else {
1608 buttons |= (pMse->lastButtons & ~0x07);
1609 dx = (pBuf[0] & 0x10) ? pBuf[1]-256 : pBuf[1];
1610 dy = (pBuf[0] & 0x20) ? -(pBuf[2]-256) : -pBuf[2];
1611 }
1612 break;
1613
1614 case PROT_GLIDEPS2: /* GlidePoint PS/2 */
1615 buttons = (pBuf[0] & 0x04) >> 1 | /* Middle */
1616 (pBuf[0] & 0x02) >> 1 | /* Right */
1617 (pBuf[0] & 0x01) << 2 | /* Left */
1618 ((pBuf[0] & 0x08) ? 0 : 0x08);/* fourth button */
1619 dx = (pBuf[0] & 0x10) ? pBuf[1]-256 : pBuf[1];
1620 dy = (pBuf[0] & 0x20) ? -(pBuf[2]-256) : -pBuf[2];
1621 break;
1622
1623 case PROT_NETSCPS2: /* NetScroll PS/2 */
1624 buttons = (pBuf[0] & 0x04) >> 1 | /* Middle */
1625 (pBuf[0] & 0x02) >> 1 | /* Right */
1626 (pBuf[0] & 0x01) << 2 | /* Left */
1627 ((pBuf[3] & 0x02) ? 0x08 : 0) | /* button 4 */
1628 ((pBuf[3] & 0x01) ? 0x10 : 0); /* button 5 */
1629 dx = (pBuf[0] & 0x10) ? pBuf[1]-256 : pBuf[1];
1630 dy = (pBuf[0] & 0x20) ? -(pBuf[2]-256) : -pBuf[2];
1631 dz = (pBuf[3] & 0x10) ? pBuf[4] - 256 : pBuf[4];
1632 break;
1633
1634 case PROT_THINKPS2: /* ThinkingMouse PS/2 */
1635 buttons = (pBuf[0] & 0x04) >> 1 | /* Middle */
1636 (pBuf[0] & 0x02) >> 1 | /* Right */
1637 (pBuf[0] & 0x01) << 2 | /* Left */
1638 ((pBuf[0] & 0x08) ? 0x08 : 0);/* fourth button */
1639 pBuf[1] |= (pBuf[0] & 0x40) ? 0x80 : 0x00;
1640 dx = (pBuf[0] & 0x10) ? pBuf[1]-256 : pBuf[1];
1641 dy = (pBuf[0] & 0x20) ? -(pBuf[2]-256) : -pBuf[2];
1642 break;
1643
1644 case PROT_SYSMOUSE: /* sysmouse */
1645 buttons = (~pBuf[0]) & 0x07;
1646 dx = (signed char)(pBuf[1]) + (signed char)(pBuf[3]);
1647 dy = - ((signed char)(pBuf[2]) + (signed char)(pBuf[4]));
1648 /* FreeBSD sysmouse sends additional data bytes */
1649 if (pMse->protoPara[4] >= 8) {
1650 /*
1651 * These casts must be 'signed char' for platforms (like PPC)
1652 * where char defaults to unsigned.
1653 */
1654 dz = ((signed char)(pBuf[5] << 1) +
1655 (signed char)(pBuf[6] << 1)) >> 1;
1656 buttons |= (int)(~pBuf[7] & 0x7f) << 3;
1657 }
1658 break;
1659
1660 case PROT_VALUMOUSESCROLL: /* Kensington ValuMouseScroll */
1661 buttons = ((int)(pBuf[0] & 0x20) >> 3)
1662 | ((int)(pBuf[0] & 0x10) >> 4)
1663 | ((int)(pBuf[3] & 0x10) >> 3);
1664 dx = (char)(((pBuf[0] & 0x03) << 6) | (pBuf[1] & 0x3F));
1665 dy = (char)(((pBuf[0] & 0x0C) << 4) | (pBuf[2] & 0x3F));
1666 dz = (pBuf[3] & 0x08) ? ((int)(pBuf[3] & 0x0F) - 0x10) :
1667 ((int)(pBuf[3] & 0x0F));
1668 break;
1669
1670 default: /* There's a table error */
1671#ifdef EXTMOUSEDEBUG
1672 ErrorF("mouse table error\n");
1673#endif
1674 continue;
1675 }
1676#ifdef EXTMOUSEDEBUG
1677 ErrorF("packet");
1678 for ( j=0; j < pBufP; j++)
1679 ErrorF(" %02x",pBuf[j]);
1680 ErrorF("\n");
1681#endif
1682
1683post_event:
1684#ifdef EXTMOUSEDEBUG
1685 ErrorF("dx=%i dy=%i dz=%i dw=%i buttons=%x\n",dx,dy,dz,dw,buttons);
1686#endif
1687 /* When auto-probing check if data makes sense */
1688 if (pMse->checkMovements && pMse->autoProbe)
1689 pMse->checkMovements(pInfo,dx,dy);
1690 /* post an event */
1691 pMse->PostEvent(pInfo, buttons, dx, dy, dz, dw);
1692
1693 /*
1694 * We don't reset pBufP here yet, as there may be an additional data
1695 * byte in some protocols. See above.
1696 */
1697 }
1698 pMse->protoBufTail = pBufP;
1699}
1700
1701/*
1702 * MouseCtrl --
1703 * Alter the control parameters for the mouse. Note that all special
1704 * protocol values are handled by dix.
1705 */
1706
1707static void
1708MouseCtrl(DeviceIntPtr device, PtrCtrl *ctrl)
1709{
1710 InputInfoPtr pInfo;
1711 MouseDevPtr pMse;
1712
1713 pInfo = device->public.devicePrivate;
1714 pMse = pInfo->private;
1715
1716#ifdef EXTMOUSEDEBUG
1717 ErrorF("MouseCtrl pMse=%p\n", pMse);
1718#endif
1719
1720 pMse->num = ctrl->num;
1721 pMse->den = ctrl->den;
1722 pMse->threshold = ctrl->threshold;
1723}
1724
1725/*
1726 ***************************************************************************
1727 *
1728 * MouseProc --
1729 *
1730 ***************************************************************************
1731 */
1732
1733static int
1734MouseProc(DeviceIntPtr device, int what)
1735{
1736 InputInfoPtr pInfo;
1737 MouseDevPtr pMse;
1738 mousePrivPtr mPriv;
1739 unsigned char map[MSE_MAXBUTTONS + 1];
1740 int i;
1741#ifdef VBOX
1742 mousePrivPtr pPriv;
1743#endif
1744
1745 pInfo = device->public.devicePrivate;
1746 pMse = pInfo->private;
1747 pMse->device = device;
1748
1749#ifdef VBOX
1750 pPriv = pMse->mousePriv;
1751#endif
1752
1753 switch (what)
1754 {
1755 case DEVICE_INIT:
1756 device->public.on = FALSE;
1757 /*
1758 * [KAZU-241097] We don't know exactly how many buttons the
1759 * device has, so setup the map with the maximum number.
1760 */
1761 for (i = 0; i < MSE_MAXBUTTONS; i++)
1762 map[i + 1] = i + 1;
1763
1764 InitPointerDeviceStruct((DevicePtr)device, map,
1765 min(pMse->buttons, MSE_MAXBUTTONS),
1766 GetMotionHistory, pMse->Ctrl,
1767 GetMotionHistorySize(), 2 /* Number of axes */);
1768
1769 /* X valuator */
1770 xf86InitValuatorAxisStruct(device, 0, 0, -1, 1, 0, 1);
1771 xf86InitValuatorDefaults(device, 0);
1772 /* Y valuator */
1773 xf86InitValuatorAxisStruct(device, 1, 0, -1, 1, 0, 1);
1774 xf86InitValuatorDefaults(device, 1);
1775 xf86MotionHistoryAllocate(pInfo);
1776
1777#ifdef EXTMOUSEDEBUG
1778 ErrorF("assigning %p atom=%d name=%s\n", device, pInfo->atom,
1779 pInfo->name);
1780#endif
1781 break;
1782
1783 case DEVICE_ON:
1784#ifdef VBOX
1785 if (!pPriv)
1786 {
1787 pPriv = (pointer)xcalloc(sizeof(mousePrivRec), 1);
1788 if (pPriv)
1789 {
1790 pMse->mousePriv = pPriv;
1791 pPriv->pScrn = 0;
1792 pPriv->screen_no = xf86SetIntOption(pInfo->options, "ScreenNo", 0);
1793 xf86Msg(X_CONFIG, "VirtualBox Mouse Integration associated with screen %d\n",
1794 pPriv->screen_no);
1795 }
1796 }
1797 if (pPriv)
1798 {
1799 if ( pPriv->screen_no >= screenInfo.numScreens
1800 || pPriv->screen_no < 0)
1801 {
1802 pPriv->screen_no = 0;
1803 }
1804 VBoxMouseInit();
1805 pPriv->pScrn = screenInfo.screens[pPriv->screen_no];
1806 }
1807#endif
1808 pInfo->fd = xf86OpenSerial(pInfo->options);
1809 if (pInfo->fd == -1)
1810 xf86Msg(X_WARNING, "%s: cannot open input device\n", pInfo->name);
1811 else {
1812 if (pMse->xisbscale)
1813 pMse->buffer = XisbNew(pInfo->fd, pMse->xisbscale * 4);
1814 else
1815 pMse->buffer = XisbNew(pInfo->fd, 64);
1816 if (!pMse->buffer) {
1817 xf86CloseSerial(pInfo->fd);
1818 pInfo->fd = -1;
1819 } else {
1820 if (!SetupMouse(pInfo)) {
1821 xf86CloseSerial(pInfo->fd);
1822 pInfo->fd = -1;
1823 XisbFree(pMse->buffer);
1824 pMse->buffer = NULL;
1825 } else {
1826 mPriv = (mousePrivPtr)pMse->mousePriv;
1827 if (mPriv != NULL) {
1828 if ( pMse->protocolID != PROT_AUTO) {
1829 pMse->inSync = TRUE; /* @@@ */
1830 if (mPriv->soft)
1831 mPriv->autoState = AUTOPROBE_GOOD;
1832 else
1833 mPriv->autoState = AUTOPROBE_H_GOOD;
1834 } else {
1835 if (mPriv->soft)
1836 mPriv->autoState = AUTOPROBE_NOPROTO;
1837 else
1838 mPriv->autoState = AUTOPROBE_H_NOPROTO;
1839 }
1840 }
1841 xf86FlushInput(pInfo->fd);
1842 xf86AddEnabledDevice(pInfo);
1843 }
1844 }
1845 }
1846 pMse->lastButtons = 0;
1847 pMse->lastMappedButtons = 0;
1848 pMse->emulateState = 0;
1849 pMse->emulate3Pending = FALSE;
1850 pMse->wheelButtonExpires = GetTimeInMillis ();
1851 device->public.on = TRUE;
1852 FlushButtons(pMse);
1853 if (pMse->emulate3Buttons || pMse->emulate3ButtonsSoft)
1854 {
1855 RegisterBlockAndWakeupHandlers (MouseBlockHandler, MouseWakeupHandler,
1856 (pointer) pInfo);
1857 }
1858 break;
1859
1860 case DEVICE_OFF:
1861 case DEVICE_CLOSE:
1862#ifdef VBOX
1863 if (VBoxMouseFini())
1864 {
1865 /** @todo what to do? */
1866 }
1867#endif
1868 if (pInfo->fd != -1) {
1869 xf86RemoveEnabledDevice(pInfo);
1870 if (pMse->buffer) {
1871 XisbFree(pMse->buffer);
1872 pMse->buffer = NULL;
1873 }
1874 xf86CloseSerial(pInfo->fd);
1875 pInfo->fd = -1;
1876 if (pMse->emulate3Buttons || pMse->emulate3ButtonsSoft)
1877 {
1878 RemoveBlockAndWakeupHandlers (MouseBlockHandler, MouseWakeupHandler,
1879 (pointer) pInfo);
1880 }
1881 }
1882 device->public.on = FALSE;
1883 usleep(300000);
1884 break;
1885 }
1886 return Success;
1887}
1888
1889/*
1890 ***************************************************************************
1891 *
1892 * MouseConvert --
1893 * Convert valuators to X and Y.
1894 *
1895 ***************************************************************************
1896 */
1897static Bool
1898MouseConvert(InputInfoPtr pInfo, int first, int num, int v0, int v1, int v2,
1899 int v3, int v4, int v5, int *x, int *y)
1900{
1901 if (first != 0 || num != 2)
1902 return FALSE;
1903
1904 *x = v0;
1905 *y = v1;
1906
1907 return TRUE;
1908}
1909
1910/**********************************************************************
1911 *
1912 * FlushButtons -- send button up events for sanity.
1913 *
1914 **********************************************************************/
1915
1916static void
1917FlushButtons(MouseDevPtr pMse)
1918{
1919
1920 /* If no button down is pending xf86PostButtonEvent()
1921 * will discard them. So we are on the safe side. */
1922
1923 int i, blocked;
1924
1925 pMse->lastButtons = 0;
1926 pMse->lastMappedButtons = 0;
1927
1928 blocked = xf86BlockSIGIO ();
1929 for (i = 1; i <= 5; i++)
1930 xf86PostButtonEvent(pMse->device,0,i,0,0,0);
1931 xf86UnblockSIGIO (blocked);
1932}
1933
1934/**********************************************************************
1935 *
1936 * Emulate3Button support code
1937 *
1938 **********************************************************************/
1939
1940
1941/*
1942 * Lets create a simple finite-state machine for 3 button emulation:
1943 *
1944 * We track buttons 1 and 3 (left and right). There are 11 states:
1945 * 0 ground - initial state
1946 * 1 delayed left - left pressed, waiting for right
1947 * 2 delayed right - right pressed, waiting for left
1948 * 3 pressed middle - right and left pressed, emulated middle sent
1949 * 4 pressed left - left pressed and sent
1950 * 5 pressed right - right pressed and sent
1951 * 6 released left - left released after emulated middle
1952 * 7 released right - right released after emulated middle
1953 * 8 repressed left - left pressed after released left
1954 * 9 repressed right - right pressed after released right
1955 * 10 pressed both - both pressed, not emulating middle
1956 *
1957 * At each state, we need handlers for the following events
1958 * 0: no buttons down
1959 * 1: left button down
1960 * 2: right button down
1961 * 3: both buttons down
1962 * 4: emulate3Timeout passed without a button change
1963 * Note that button events are not deltas, they are the set of buttons being
1964 * pressed now. It's possible (ie, mouse hardware does it) to go from (eg)
1965 * left down to right down without anything in between, so all cases must be
1966 * handled.
1967 *
1968 * a handler consists of three values:
1969 * 0: action1
1970 * 1: action2
1971 * 2: new emulation state
1972 *
1973 * action > 0: ButtonPress
1974 * action = 0: nothing
1975 * action < 0: ButtonRelease
1976 *
1977 * The comment preceeding each section is the current emulation state.
1978 * The comments to the right are of the form
1979 * <button state> (<events>) -> <new emulation state>
1980 * which should be read as
1981 * If the buttons are in <button state>, generate <events> then go to
1982 * <new emulation state>.
1983 */
1984static signed char stateTab[11][5][3] = {
1985/* 0 ground */
1986 {
1987 { 0, 0, 0 }, /* nothing -> ground (no change) */
1988 { 0, 0, 1 }, /* left -> delayed left */
1989 { 0, 0, 2 }, /* right -> delayed right */
1990 { 2, 0, 3 }, /* left & right (middle press) -> pressed middle */
1991 { 0, 0, -1 } /* timeout N/A */
1992 },
1993/* 1 delayed left */
1994 {
1995 { 1, -1, 0 }, /* nothing (left event) -> ground */
1996 { 0, 0, 1 }, /* left -> delayed left (no change) */
1997 { 1, -1, 2 }, /* right (left event) -> delayed right */
1998 { 2, 0, 3 }, /* left & right (middle press) -> pressed middle */
1999 { 1, 0, 4 }, /* timeout (left press) -> pressed left */
2000 },
2001/* 2 delayed right */
2002 {
2003 { 3, -3, 0 }, /* nothing (right event) -> ground */
2004 { 3, -3, 1 }, /* left (right event) -> delayed left (no change) */
2005 { 0, 0, 2 }, /* right -> delayed right (no change) */
2006 { 2, 0, 3 }, /* left & right (middle press) -> pressed middle */
2007 { 3, 0, 5 }, /* timeout (right press) -> pressed right */
2008 },
2009/* 3 pressed middle */
2010 {
2011 { -2, 0, 0 }, /* nothing (middle release) -> ground */
2012 { 0, 0, 7 }, /* left -> released right */
2013 { 0, 0, 6 }, /* right -> released left */
2014 { 0, 0, 3 }, /* left & right -> pressed middle (no change) */
2015 { 0, 0, -1 }, /* timeout N/A */
2016 },
2017/* 4 pressed left */
2018 {
2019 { -1, 0, 0 }, /* nothing (left release) -> ground */
2020 { 0, 0, 4 }, /* left -> pressed left (no change) */
2021 { -1, 0, 2 }, /* right (left release) -> delayed right */
2022 { 3, 0, 10 }, /* left & right (right press) -> pressed both */
2023 { 0, 0, -1 }, /* timeout N/A */
2024 },
2025/* 5 pressed right */
2026 {
2027 { -3, 0, 0 }, /* nothing (right release) -> ground */
2028 { -3, 0, 1 }, /* left (right release) -> delayed left */
2029 { 0, 0, 5 }, /* right -> pressed right (no change) */
2030 { 1, 0, 10 }, /* left & right (left press) -> pressed both */
2031 { 0, 0, -1 }, /* timeout N/A */
2032 },
2033/* 6 released left */
2034 {
2035 { -2, 0, 0 }, /* nothing (middle release) -> ground */
2036 { -2, 0, 1 }, /* left (middle release) -> delayed left */
2037 { 0, 0, 6 }, /* right -> released left (no change) */
2038 { 1, 0, 8 }, /* left & right (left press) -> repressed left */
2039 { 0, 0, -1 }, /* timeout N/A */
2040 },
2041/* 7 released right */
2042 {
2043 { -2, 0, 0 }, /* nothing (middle release) -> ground */
2044 { 0, 0, 7 }, /* left -> released right (no change) */
2045 { -2, 0, 2 }, /* right (middle release) -> delayed right */
2046 { 3, 0, 9 }, /* left & right (right press) -> repressed right */
2047 { 0, 0, -1 }, /* timeout N/A */
2048 },
2049/* 8 repressed left */
2050 {
2051 { -2, -1, 0 }, /* nothing (middle release, left release) -> ground */
2052 { -2, 0, 4 }, /* left (middle release) -> pressed left */
2053 { -1, 0, 6 }, /* right (left release) -> released left */
2054 { 0, 0, 8 }, /* left & right -> repressed left (no change) */
2055 { 0, 0, -1 }, /* timeout N/A */
2056 },
2057/* 9 repressed right */
2058 {
2059 { -2, -3, 0 }, /* nothing (middle release, right release) -> ground */
2060 { -3, 0, 7 }, /* left (right release) -> released right */
2061 { -2, 0, 5 }, /* right (middle release) -> pressed right */
2062 { 0, 0, 9 }, /* left & right -> repressed right (no change) */
2063 { 0, 0, -1 }, /* timeout N/A */
2064 },
2065/* 10 pressed both */
2066 {
2067 { -1, -3, 0 }, /* nothing (left release, right release) -> ground */
2068 { -3, 0, 4 }, /* left (right release) -> pressed left */
2069 { -1, 0, 5 }, /* right (left release) -> pressed right */
2070 { 0, 0, 10 }, /* left & right -> pressed both (no change) */
2071 { 0, 0, -1 }, /* timeout N/A */
2072 },
2073};
2074
2075/*
2076 * Table to allow quick reversal of natural button mapping to correct mapping
2077 */
2078
2079/*
2080 * [JCH-96/01/21] The ALPS GlidePoint pad extends the MS protocol
2081 * with a fourth button activated by tapping the PAD.
2082 * The 2nd line corresponds to 4th button on; the drv sends
2083 * the buttons in the following map (MSBit described first) :
2084 * 0 | 4th | 1st | 2nd | 3rd
2085 * And we remap them (MSBit described first) :
2086 * 0 | 4th | 3rd | 2nd | 1st
2087 */
2088static char reverseMap[16] = { 0, 4, 2, 6,
2089 1, 5, 3, 7,
2090 8, 12, 10, 14,
2091 9, 13, 11, 15 };
2092
2093static char hitachMap[16] = { 0, 2, 1, 3,
2094 8, 10, 9, 11,
2095 4, 6, 5, 7,
2096 12, 14, 13, 15 };
2097
2098#define reverseBits(map, b) (((b) & ~0x0f) | map[(b) & 0x0f])
2099
2100static CARD32
2101buttonTimer(InputInfoPtr pInfo)
2102{
2103 MouseDevPtr pMse;
2104 int sigstate;
2105 int id;
2106
2107 pMse = pInfo->private;
2108
2109 sigstate = xf86BlockSIGIO ();
2110
2111 pMse->emulate3Pending = FALSE;
2112 if ((id = stateTab[pMse->emulateState][4][0]) != 0) {
2113 xf86PostButtonEvent(pInfo->dev, 0, abs(id), (id >= 0), 0, 0);
2114 pMse->emulateState = stateTab[pMse->emulateState][4][2];
2115 } else {
2116 ErrorF("Got unexpected buttonTimer in state %d\n", pMse->emulateState);
2117 }
2118
2119 xf86UnblockSIGIO (sigstate);
2120 return 0;
2121}
2122
2123static Bool
2124Emulate3ButtonsSoft(InputInfoPtr pInfo)
2125{
2126 MouseDevPtr pMse = pInfo->private;
2127
2128 if (!pMse->emulate3ButtonsSoft)
2129 return TRUE;
2130
2131 pMse->emulate3Buttons = FALSE;
2132
2133 if (pMse->emulate3Pending)
2134 buttonTimer(pInfo);
2135
2136 xf86Msg(X_INFO,"3rd Button detected: disabling emulate3Button\n");
2137
2138 return FALSE;
2139}
2140
2141static void MouseBlockHandler(pointer data,
2142 struct timeval **waitTime,
2143 pointer LastSelectMask)
2144{
2145 InputInfoPtr pInfo = (InputInfoPtr) data;
2146 MouseDevPtr pMse = (MouseDevPtr) pInfo->private;
2147 int ms;
2148
2149 if (pMse->emulate3Pending)
2150 {
2151 ms = pMse->emulate3Expires - GetTimeInMillis ();
2152 if (ms <= 0)
2153 ms = 0;
2154 AdjustWaitForDelay (waitTime, ms);
2155 }
2156}
2157
2158static void MouseWakeupHandler(pointer data,
2159 int i,
2160 pointer LastSelectMask)
2161{
2162 InputInfoPtr pInfo = (InputInfoPtr) data;
2163 MouseDevPtr pMse = (MouseDevPtr) pInfo->private;
2164 int ms;
2165
2166 if (pMse->emulate3Pending)
2167 {
2168 ms = pMse->emulate3Expires - GetTimeInMillis ();
2169 if (ms <= 0)
2170 buttonTimer (pInfo);
2171 }
2172}
2173
2174/*******************************************************************
2175 *
2176 * Post mouse events
2177 *
2178 *******************************************************************/
2179
2180static void
2181MouseDoPostEvent(InputInfoPtr pInfo, int buttons, int dx, int dy)
2182{
2183 MouseDevPtr pMse;
2184 int emulateButtons;
2185 int id, change;
2186 int emuWheelDelta, emuWheelButton, emuWheelButtonMask;
2187 int wheelButtonMask;
2188 int ms;
2189
2190 pMse = pInfo->private;
2191
2192 change = buttons ^ pMse->lastMappedButtons;
2193 pMse->lastMappedButtons = buttons;
2194
2195 /* Do single button double click */
2196 if (pMse->doubleClickSourceButtonMask) {
2197 if (buttons & pMse->doubleClickSourceButtonMask) {
2198 if (!(pMse->doubleClickOldSourceState)) {
2199 /* double-click button has just been pressed. Ignore it if target button
2200 * is already down.
2201 */
2202 if (!(buttons & pMse->doubleClickTargetButtonMask)) {
2203 /* Target button isn't down, so send a double-click */
2204 xf86PostButtonEvent(pInfo->dev, 0, pMse->doubleClickTargetButton, 1, 0, 0);
2205 xf86PostButtonEvent(pInfo->dev, 0, pMse->doubleClickTargetButton, 0, 0, 0);
2206 xf86PostButtonEvent(pInfo->dev, 0, pMse->doubleClickTargetButton, 1, 0, 0);
2207 xf86PostButtonEvent(pInfo->dev, 0, pMse->doubleClickTargetButton, 0, 0, 0);
2208 }
2209 }
2210 pMse->doubleClickOldSourceState = 1;
2211 }
2212 else
2213 pMse->doubleClickOldSourceState = 0;
2214
2215 /* Whatever happened, mask the double-click button so it doesn't get
2216 * processed as a normal button as well.
2217 */
2218 buttons &= ~(pMse->doubleClickSourceButtonMask);
2219 change &= ~(pMse->doubleClickSourceButtonMask);
2220 }
2221
2222 if (pMse->emulateWheel) {
2223 /* Emulate wheel button handling */
2224 wheelButtonMask = 1 << (pMse->wheelButton - 1);
2225
2226 if (change & wheelButtonMask) {
2227 if (buttons & wheelButtonMask) {
2228 /* Start timeout handling */
2229 pMse->wheelButtonExpires = GetTimeInMillis () + pMse->wheelButtonTimeout;
2230 ms = - pMse->wheelButtonTimeout;
2231 } else {
2232 ms = pMse->wheelButtonExpires - GetTimeInMillis ();
2233
2234 if (0 < ms) {
2235 /*
2236 * If the button is released early enough emit the button
2237 * press/release events
2238 */
2239 xf86PostButtonEvent(pInfo->dev, 0, pMse->wheelButton, 1, 0, 0);
2240 xf86PostButtonEvent(pInfo->dev, 0, pMse->wheelButton, 0, 0, 0);
2241 }
2242 }
2243 } else
2244 ms = pMse->wheelButtonExpires - GetTimeInMillis ();
2245
2246 /* Intercept wheel emulation. */
2247 if (buttons & wheelButtonMask) {
2248 if (ms <= 0) {
2249 /* Y axis movement */
2250 if (pMse->negativeY != MSE_NOAXISMAP) {
2251 pMse->wheelYDistance += dy;
2252 if (pMse->wheelYDistance < 0) {
2253 emuWheelDelta = -pMse->wheelInertia;
2254 emuWheelButton = pMse->negativeY;
2255 } else {
2256 emuWheelDelta = pMse->wheelInertia;
2257 emuWheelButton = pMse->positiveY;
2258 }
2259 emuWheelButtonMask = 1 << (emuWheelButton - 1);
2260 while (abs(pMse->wheelYDistance) > pMse->wheelInertia) {
2261 pMse->wheelYDistance -= emuWheelDelta;
2262
2263 /*
2264 * Synthesize the press and release, but not when
2265 * the button to be synthesized is already pressed
2266 * "for real".
2267 */
2268 if (!(emuWheelButtonMask & buttons) ||
2269 (emuWheelButtonMask & wheelButtonMask)) {
2270 xf86PostButtonEvent(pInfo->dev, 0, emuWheelButton, 1, 0, 0);
2271 xf86PostButtonEvent(pInfo->dev, 0, emuWheelButton, 0, 0, 0);
2272 }
2273 }
2274 }
2275
2276 /* X axis movement */
2277 if (pMse->negativeX != MSE_NOAXISMAP) {
2278 pMse->wheelXDistance += dx;
2279 if (pMse->wheelXDistance < 0) {
2280 emuWheelDelta = -pMse->wheelInertia;
2281 emuWheelButton = pMse->negativeX;
2282 } else {
2283 emuWheelDelta = pMse->wheelInertia;
2284 emuWheelButton = pMse->positiveX;
2285 }
2286 emuWheelButtonMask = 1 << (emuWheelButton - 1);
2287 while (abs(pMse->wheelXDistance) > pMse->wheelInertia) {
2288 pMse->wheelXDistance -= emuWheelDelta;
2289
2290 /*
2291 * Synthesize the press and release, but not when
2292 * the button to be synthesized is already pressed
2293 * "for real".
2294 */
2295 if (!(emuWheelButtonMask & buttons) ||
2296 (emuWheelButtonMask & wheelButtonMask)) {
2297 xf86PostButtonEvent(pInfo->dev, 0, emuWheelButton, 1, 0, 0);
2298 xf86PostButtonEvent(pInfo->dev, 0, emuWheelButton, 0, 0, 0);
2299 }
2300 }
2301 }
2302 }
2303
2304 /* Absorb the mouse movement while the wheel button is pressed. */
2305 dx = 0;
2306 dy = 0;
2307 }
2308 /*
2309 * Button events for the wheel button are only emitted through
2310 * the timeout code.
2311 */
2312 buttons &= ~wheelButtonMask;
2313 change &= ~wheelButtonMask;
2314 }
2315
2316 if (pMse->emulate3ButtonsSoft && pMse->emulate3Pending && (dx || dy))
2317 buttonTimer(pInfo);
2318
2319#ifdef VBOX
2320 if (dx || dy)
2321 {
2322 mousePrivPtr pPriv = pMse->mousePriv;
2323 if (pPriv && pPriv->pScrn)
2324 {
2325 unsigned int abs_x;
2326 unsigned int abs_y;
2327 if (VBoxMouseQueryPosition(&abs_x, &abs_y) == 0)
2328 {
2329 /* convert to screen resolution */
2330 int x, y;
2331 x = (abs_x * pPriv->pScrn->width) / 65535;
2332 y = (abs_y * pPriv->pScrn->height) / 65535;
2333 /* send absolute movement */
2334 xf86PostMotionEvent(pInfo->dev, 1, 0, 2, x, y);
2335 }
2336 else
2337 {
2338 /* send relative event */
2339 xf86PostMotionEvent(pInfo->dev, 0, 0, 2, dx, dy);
2340 }
2341 }
2342 else
2343 {
2344 /* send relative event */
2345 xf86PostMotionEvent(pInfo->dev, 0, 0, 2, dx, dy);
2346 }
2347 }
2348#else
2349 if (dx || dy)
2350 xf86PostMotionEvent(pInfo->dev, 0, 0, 2, dx, dy);
2351#endif
2352
2353 if (change) {
2354
2355 /*
2356 * adjust buttons state for drag locks!
2357 * if there is drag locks
2358 */
2359 if (pMse->pDragLock) {
2360 DragLockPtr pLock;
2361 int tarOfGoingDown, tarOfDown;
2362 int realbuttons;
2363
2364 /* get drag lock block */
2365 pLock = pMse->pDragLock;
2366 /* save real buttons */
2367 realbuttons = buttons;
2368
2369 /* if drag lock used */
2370
2371 /* state of drag lock buttons not seen always up */
2372
2373 buttons &= ~pLock->lockButtonsM;
2374
2375 /*
2376 * if lock buttons being depressed changes state of
2377 * targets simulatedDown.
2378 */
2379 tarOfGoingDown = lock2targetMap(pLock,
2380 realbuttons & change & pLock->lockButtonsM);
2381 pLock->simulatedDown ^= tarOfGoingDown;
2382
2383 /* targets of drag locks down */
2384 tarOfDown = lock2targetMap(pLock,
2385 realbuttons & pLock->lockButtonsM);
2386
2387 /*
2388 * when simulatedDown set and target pressed,
2389 * simulatedDown goes false
2390 */
2391 pLock->simulatedDown &= ~(realbuttons & change);
2392
2393 /*
2394 * if master drag lock released
2395 * then master drag lock state on
2396 */
2397 pLock->masterTS |= (~realbuttons & change) & pLock->masterLockM;
2398
2399 /* if master state, buttons going down are simulatedDown */
2400 if (pLock->masterTS)
2401 pLock->simulatedDown |= (realbuttons & change);
2402
2403 /* if any button pressed, no longer in master drag lock state */
2404 if (realbuttons & change)
2405 pLock->masterTS = 0;
2406
2407 /* if simulatedDown or drag lock down, simulate down */
2408 buttons |= (pLock->simulatedDown | tarOfDown);
2409
2410 /* master button not seen */
2411 buttons &= ~(pLock->masterLockM);
2412
2413 /* buttons changed since last time */
2414 change = buttons ^ pLock->lockLastButtons;
2415
2416 /* save this time for next last time. */
2417 pLock->lockLastButtons = buttons;
2418 }
2419
2420 if (pMse->emulate3Buttons
2421 && (!(buttons & 0x02) || Emulate3ButtonsSoft(pInfo))) {
2422
2423 /* handle all but buttons 1 & 3 normally */
2424
2425 change &= ~05;
2426
2427 /* emulate the third button by the other two */
2428
2429 emulateButtons = (buttons & 01) | ((buttons &04) >> 1);
2430
2431 if ((id = stateTab[pMse->emulateState][emulateButtons][0]) != 0)
2432 xf86PostButtonEvent(pInfo->dev, 0, abs(id), (id >= 0), 0, 0);
2433 if ((id = stateTab[pMse->emulateState][emulateButtons][1]) != 0)
2434 xf86PostButtonEvent(pInfo->dev, 0, abs(id), (id >= 0), 0, 0);
2435
2436 pMse->emulateState =
2437 stateTab[pMse->emulateState][emulateButtons][2];
2438
2439 if (stateTab[pMse->emulateState][4][0] != 0) {
2440 pMse->emulate3Expires = GetTimeInMillis () + pMse->emulate3Timeout;
2441 pMse->emulate3Pending = TRUE;
2442 } else {
2443 pMse->emulate3Pending = FALSE;
2444 }
2445 }
2446
2447 while (change) {
2448 id = ffs(change);
2449 change &= ~(1 << (id - 1));
2450 xf86PostButtonEvent(pInfo->dev, 0, id,
2451 (buttons & (1 << (id - 1))), 0, 0);
2452 }
2453
2454 }
2455}
2456
2457static void
2458MousePostEvent(InputInfoPtr pInfo, int truebuttons,
2459 int dx, int dy, int dz, int dw)
2460{
2461 MouseDevPtr pMse;
2462 int zbutton = 0;
2463 int i, b, buttons = 0;
2464
2465 pMse = pInfo->private;
2466 if (pMse->protocolID == PROT_MMHIT)
2467 b = reverseBits(hitachMap, truebuttons);
2468 else
2469 b = reverseBits(reverseMap, truebuttons);
2470
2471 /* Remap mouse buttons */
2472 b &= (1<<MSE_MAXBUTTONS)-1;
2473 for (i = 0; b; i++) {
2474 if (b & 1)
2475 buttons |= pMse->buttonMap[i];
2476 b >>= 1;
2477 }
2478
2479 /* Map the Z axis movement. */
2480 /* XXX Could this go in the conversion_proc? */
2481 switch (pMse->negativeZ) {
2482 case MSE_NOZMAP: /* do nothing */
2483 break;
2484 case MSE_MAPTOX:
2485 if (dz != 0) {
2486 dx = dz;
2487 dz = 0;
2488 }
2489 break;
2490 case MSE_MAPTOY:
2491 if (dz != 0) {
2492 dy = dz;
2493 dz = 0;
2494 }
2495 break;
2496 default: /* buttons */
2497 buttons &= ~(pMse->negativeZ | pMse->positiveZ
2498 | pMse->negativeW | pMse->positiveW);
2499 if (dw < 0 || dz < -1)
2500 zbutton = pMse->negativeW;
2501 else if (dz < 0)
2502 zbutton = pMse->negativeZ;
2503 else if (dw > 0 || dz > 1)
2504 zbutton = pMse->positiveW;
2505 else if (dz > 0)
2506 zbutton = pMse->positiveZ;
2507 buttons |= zbutton;
2508 dz = 0;
2509 break;
2510 }
2511
2512 /* Apply angle offset */
2513 if (pMse->angleOffset != 0) {
2514 double rad = 3.141592653 * pMse->angleOffset / 180.0;
2515 int ndx = dx;
2516 dx = (int)((dx * cos(rad)) + (dy * sin(rad)) + 0.5);
2517 dy = (int)((dy * cos(rad)) - (ndx * sin(rad)) + 0.5);
2518 }
2519
2520 dx = pMse->invX * dx;
2521 dy = pMse->invY * dy;
2522 if (pMse->flipXY) {
2523 int tmp = dx;
2524 dx = dy;
2525 dy = tmp;
2526 }
2527 MouseDoPostEvent(pInfo, buttons, dx, dy);
2528
2529 /*
2530 * If dz has been mapped to a button `down' event, we need to cook up
2531 * a corresponding button `up' event.
2532 */
2533 if (zbutton) {
2534 buttons &= ~zbutton;
2535 MouseDoPostEvent(pInfo, buttons, 0, 0);
2536 }
2537
2538 pMse->lastButtons = truebuttons;
2539}
2540/******************************************************************
2541 *
2542 * Mouse Setup Code
2543 *
2544 ******************************************************************/
2545/*
2546 * This array is indexed by the MouseProtocolID values, so the order of the
2547 * entries must match that of the MouseProtocolID enum in xf86OSmouse.h.
2548 */
2549static unsigned char proto[PROT_NUMPROTOS][8] = {
2550 /* --header-- ---data--- packet -4th-byte- mouse */
2551 /* mask id mask id bytes mask id flags */
2552 /* Serial mice */
2553 { 0x40, 0x40, 0x40, 0x00, 3, ~0x23, 0x00, MPF_NONE }, /* MicroSoft */
2554 { 0xf8, 0x80, 0x00, 0x00, 5, 0x00, 0xff, MPF_SAFE }, /* MouseSystems */
2555 { 0xe0, 0x80, 0x80, 0x00, 3, 0x00, 0xff, MPF_NONE }, /* MMSeries */
2556 { 0xe0, 0x80, 0x80, 0x00, 3, 0x00, 0xff, MPF_NONE }, /* Logitech */
2557 { 0x40, 0x40, 0x40, 0x00, 3, ~0x23, 0x00, MPF_NONE }, /* MouseMan */
2558 { 0xe0, 0x80, 0x80, 0x00, 3, 0x00, 0xff, MPF_NONE }, /* MM_HitTablet */
2559 { 0x40, 0x40, 0x40, 0x00, 3, ~0x33, 0x00, MPF_NONE }, /* GlidePoint */
2560 { 0x40, 0x40, 0x40, 0x00, 3, ~0x3f, 0x00, MPF_NONE }, /* IntelliMouse */
2561 { 0x40, 0x40, 0x40, 0x00, 3, ~0x33, 0x00, MPF_NONE }, /* ThinkingMouse */
2562 { 0x80, 0x80, 0x80, 0x00, 3, 0x00, 0xff, MPF_NONE }, /* ACECAD */
2563 { 0x40, 0x40, 0x40, 0x00, 4, 0x00, 0xff, MPF_NONE }, /* ValuMouseScroll */
2564 /* PS/2 variants */
2565 { 0xc0, 0x00, 0x00, 0x00, 3, 0x00, 0xff, MPF_NONE }, /* PS/2 mouse */
2566 { 0xc8, 0x08, 0x00, 0x00, 3, 0x00, 0x00, MPF_NONE }, /* genericPS/2 mouse*/
2567 { 0x08, 0x08, 0x00, 0x00, 4, 0x00, 0xff, MPF_NONE }, /* IntelliMouse */
2568 { 0x08, 0x08, 0x00, 0x00, 4, 0x00, 0xff, MPF_NONE }, /* Explorer */
2569 { 0x80, 0x80, 0x00, 0x00, 3, 0x00, 0xff, MPF_NONE }, /* ThinkingMouse */
2570 { 0x08, 0x08, 0x00, 0x00, 3, 0x00, 0xff, MPF_NONE }, /* MouseMan+ */
2571 { 0xc0, 0x00, 0x00, 0x00, 3, 0x00, 0xff, MPF_NONE }, /* GlidePoint */
2572 { 0x08, 0x08, 0x00, 0x00, 4, 0x00, 0xff, MPF_NONE }, /* NetMouse */
2573 { 0xc0, 0x00, 0x00, 0x00, 6, 0x00, 0xff, MPF_NONE }, /* NetScroll */
2574 /* Bus Mouse */
2575 { 0xf8, 0x80, 0x00, 0x00, 5, 0x00, 0xff, MPF_NONE }, /* BusMouse */
2576 { 0xf8, 0x80, 0x00, 0x00, 5, 0x00, 0xff, MPF_NONE }, /* Auto (dummy) */
2577 { 0xf8, 0x80, 0x00, 0x00, 8, 0x00, 0xff, MPF_NONE }, /* SysMouse */
2578};
2579
2580
2581/*
2582 * SetupMouse --
2583 * Sets up the mouse parameters
2584 */
2585static Bool
2586SetupMouse(InputInfoPtr pInfo)
2587{
2588 MouseDevPtr pMse;
2589 int i;
2590 int protoPara[8] = {-1, -1, -1, -1, -1, -1, -1, -1};
2591 const char *name = NULL;
2592 Bool automatic = FALSE;
2593
2594 pMse = pInfo->private;
2595
2596 /* Handle the "Auto" protocol. */
2597 if (pMse->protocolID == PROT_AUTO) {
2598 /*
2599 * We come here when user specifies protocol "auto" in
2600 * the configuration file or thru the xf86misc extensions.
2601 * So we initialize autoprobing here.
2602 * Probe for PnP/OS mouse first. If unsuccessful
2603 * try to guess protocol from incoming data.
2604 */
2605 automatic = TRUE;
2606 pMse->autoProbe = TRUE;
2607 name = autoOSProtocol(pInfo,protoPara);
2608 if (name) {
2609#ifdef EXTMOUSEDEBUG
2610 ErrorF("PnP/OS Mouse detected: %s\n",name);
2611#endif
2612 }
2613 }
2614
2615 SetMouseProto(pMse, pMse->protocolID);
2616
2617 if (automatic) {
2618 if (name) {
2619 /* Possible protoPara overrides from SetupAuto. */
2620 for (i = 0; i < sizeof(pMse->protoPara); i++)
2621 if (protoPara[i] != -1)
2622 pMse->protoPara[i] = protoPara[i];
2623 /* if we come here PnP/OS mouse probing was successful */
2624 } else {
2625#if 1
2626 /* PnP/OS mouse probing wasn't successful; we look at data */
2627#else
2628 xf86Msg(X_ERROR, "%s: cannot determine the mouse protocol\n",
2629 pInfo->name);
2630 return FALSE;
2631#endif
2632 }
2633 }
2634
2635 /*
2636 * If protocol has changed fetch the default options
2637 * for the new protocol.
2638 */
2639 if (pMse->oldProtocolID != pMse->protocolID) {
2640 pointer tmp = NULL;
2641 if ((pMse->protocolID >= 0)
2642 && (pMse->protocolID < PROT_NUMPROTOS)
2643 && mouseProtocols[pMse->protocolID].defaults)
2644 tmp = xf86OptionListCreate(
2645 mouseProtocols[pMse->protocolID].defaults, -1, 0);
2646 pInfo->options = xf86OptionListMerge(pInfo->options, tmp);
2647 /*
2648 * If baudrate is set write it back to the option
2649 * list so that the serial interface code can access
2650 * the new value. Not set means default.
2651 */
2652 if (pMse->baudRate)
2653 xf86ReplaceIntOption(pInfo->options, "BaudRate", pMse->baudRate);
2654 pMse->oldProtocolID = pMse->protocolID; /* hack */
2655 }
2656
2657
2658 /* Set the port parameters. */
2659 if (!automatic)
2660 xf86SetSerial(pInfo->fd, pInfo->options);
2661
2662 if (!initMouseHW(pInfo))
2663 return FALSE;
2664
2665 pMse->protoBufTail = 0;
2666 pMse->inSync = 0;
2667
2668 return TRUE;
2669}
2670
2671/********************************************************************
2672 *
2673 * Mouse HW setup code
2674 *
2675 ********************************************************************/
2676
2677/*
2678** The following lines take care of the Logitech MouseMan protocols.
2679** The "Logitech" protocol is for the old "series 9" Logitech products.
2680** All products since then use the "MouseMan" protocol. Some models
2681** were programmable, but most (all?) of the current models are not.
2682**
2683** NOTE: There are different versions of both MouseMan and TrackMan!
2684** Hence I add another protocol PROT_LOGIMAN, which the user can
2685** specify as MouseMan in his XF86Config file. This entry was
2686** formerly handled as a special case of PROT_MS. However, people
2687** who don't have the middle button problem, can still specify
2688** Microsoft and use PROT_MS.
2689**
2690** By default, these mice should use a 3 byte Microsoft protocol
2691** plus a 4th byte for the middle button. However, the mouse might
2692** have switched to a different protocol before we use it, so I send
2693** the proper sequence just in case.
2694**
2695** NOTE: - all commands to (at least the European) MouseMan have to
2696** be sent at 1200 Baud.
2697** - each command starts with a '*'.
2698** - whenever the MouseMan receives a '*', it will switch back
2699** to 1200 Baud. Hence I have to select the desired protocol
2700** first, then select the baud rate.
2701**
2702** The protocols supported by the (European) MouseMan are:
2703** - 5 byte packed binary protocol, as with the Mouse Systems
2704** mouse. Selected by sequence "*U".
2705** - 2 button 3 byte MicroSoft compatible protocol. Selected
2706** by sequence "*V".
2707** - 3 button 3+1 byte MicroSoft compatible protocol (default).
2708** Selected by sequence "*X".
2709**
2710** The following baud rates are supported:
2711** - 1200 Baud (default). Selected by sequence "*n".
2712** - 9600 Baud. Selected by sequence "*q".
2713**
2714** Selecting a sample rate is no longer supported with the MouseMan!
2715** [CHRIS-211092]
2716*/
2717
2718/*
2719 * Do a reset wrap mode before reset.
2720 */
2721#define do_ps2Reset(x) { \
2722 int i = RETRY_COUNT;\
2723 while (i-- > 0) { \
2724 xf86FlushInput(x->fd); \
2725 if (ps2Reset(x)) break; \
2726 } \
2727 }
2728
2729
2730static Bool
2731initMouseHW(InputInfoPtr pInfo)
2732{
2733 MouseDevPtr pMse = pInfo->private;
2734 const char *s;
2735 unsigned char c;
2736 int speed;
2737 pointer options;
2738 unsigned char *param = NULL;
2739 int paramlen = 0;
2740 int count = RETRY_COUNT;
2741 Bool ps2Init = TRUE;
2742
2743 switch (pMse->protocolID) {
2744 case PROT_LOGI: /* Logitech Mice */
2745 /*
2746 * The baud rate selection command must be sent at the current
2747 * baud rate; try all likely settings.
2748 */
2749 speed = pMse->baudRate;
2750 switch (speed) {
2751 case 9600:
2752 s = "*q";
2753 break;
2754 case 4800:
2755 s = "*p";
2756 break;
2757 case 2400:
2758 s = "*o";
2759 break;
2760 case 1200:
2761 s = "*n";
2762 break;
2763 default:
2764 /* Fallback value */
2765 speed = 1200;
2766 s = "*n";
2767 }
2768 xf86SetSerialSpeed(pInfo->fd, 9600);
2769 xf86WriteSerial(pInfo->fd, s, 2);
2770 usleep(100000);
2771 xf86SetSerialSpeed(pInfo->fd, 4800);
2772 xf86WriteSerial(pInfo->fd, s, 2);
2773 usleep(100000);
2774 xf86SetSerialSpeed(pInfo->fd, 2400);
2775 xf86WriteSerial(pInfo->fd, s, 2);
2776 usleep(100000);
2777 xf86SetSerialSpeed(pInfo->fd, 1200);
2778 xf86WriteSerial(pInfo->fd, s, 2);
2779 usleep(100000);
2780 xf86SetSerialSpeed(pInfo->fd, speed);
2781
2782 /* Select MM series data format. */
2783 xf86WriteSerial(pInfo->fd, "S", 1);
2784 usleep(100000);
2785 /* Set the parameters up for the MM series protocol. */
2786 options = pInfo->options;
2787 xf86CollectInputOptions(pInfo, mmDefaults, NULL);
2788 xf86SetSerial(pInfo->fd, pInfo->options);
2789 pInfo->options = options;
2790
2791 /* Select report rate/frequency. */
2792 if (pMse->sampleRate <= 0) c = 'O'; /* 100 */
2793 else if (pMse->sampleRate <= 15) c = 'J'; /* 10 */
2794 else if (pMse->sampleRate <= 27) c = 'K'; /* 20 */
2795 else if (pMse->sampleRate <= 42) c = 'L'; /* 35 */
2796 else if (pMse->sampleRate <= 60) c = 'R'; /* 50 */
2797 else if (pMse->sampleRate <= 85) c = 'M'; /* 67 */
2798 else if (pMse->sampleRate <= 125) c = 'Q'; /* 100 */
2799 else c = 'N'; /* 150 */
2800 xf86WriteSerial(pInfo->fd, &c, 1);
2801 break;
2802
2803 case PROT_LOGIMAN:
2804 speed = pMse->baudRate;
2805 switch (speed) {
2806 case 9600:
2807 s = "*q";
2808 break;
2809 case 1200:
2810 s = "*n";
2811 break;
2812 default:
2813 /* Fallback value */
2814 speed = 1200;
2815 s = "*n";
2816 }
2817 xf86SetSerialSpeed(pInfo->fd, 1200);
2818 xf86WriteSerial(pInfo->fd, "*n", 2);
2819 xf86WriteSerial(pInfo->fd, "*X", 2);
2820 xf86WriteSerial(pInfo->fd, s, 2);
2821 usleep(100000);
2822 xf86SetSerialSpeed(pInfo->fd, speed);
2823 break;
2824
2825 case PROT_MMHIT: /* MM_HitTablet */
2826 /*
2827 * Initialize Hitachi PUMA Plus - Model 1212E to desired settings.
2828 * The tablet must be configured to be in MM mode, NO parity,
2829 * Binary Format. pMse->sampleRate controls the sensitivity
2830 * of the tablet. We only use this tablet for it's 4-button puck
2831 * so we don't run in "Absolute Mode".
2832 */
2833 xf86WriteSerial(pInfo->fd, "z8", 2); /* Set Parity = "NONE" */
2834 usleep(50000);
2835 xf86WriteSerial(pInfo->fd, "zb", 2); /* Set Format = "Binary" */
2836 usleep(50000);
2837 xf86WriteSerial(pInfo->fd, "@", 1); /* Set Report Mode = "Stream" */
2838 usleep(50000);
2839 xf86WriteSerial(pInfo->fd, "R", 1); /* Set Output Rate = "45 rps" */
2840 usleep(50000);
2841 xf86WriteSerial(pInfo->fd, "I\x20", 2); /* Set Incrememtal Mode "20" */
2842 usleep(50000);
2843 xf86WriteSerial(pInfo->fd, "E", 1); /* Set Data Type = "Relative */
2844 usleep(50000);
2845 /*
2846 * These sample rates translate to 'lines per inch' on the Hitachi
2847 * tablet.
2848 */
2849 if (pMse->sampleRate <= 40) c = 'g';
2850 else if (pMse->sampleRate <= 100) c = 'd';
2851 else if (pMse->sampleRate <= 200) c = 'e';
2852 else if (pMse->sampleRate <= 500) c = 'h';
2853 else if (pMse->sampleRate <= 1000) c = 'j';
2854 else c = 'd';
2855 xf86WriteSerial(pInfo->fd, &c, 1);
2856 usleep(50000);
2857 xf86WriteSerial(pInfo->fd, "\021", 1); /* Resume DATA output */
2858 break;
2859
2860 case PROT_THINKING: /* ThinkingMouse */
2861 /* This mouse may send a PnP ID string, ignore it. */
2862 usleep(200000);
2863 xf86FlushInput(pInfo->fd);
2864 /* Send the command to initialize the beast. */
2865 for (s = "E5E5"; *s; ++s) {
2866 xf86WriteSerial(pInfo->fd, s, 1);
2867 if ((xf86WaitForInput(pInfo->fd, 1000000) <= 0))
2868 break;
2869 xf86ReadSerial(pInfo->fd, &c, 1);
2870 if (c != *s)
2871 break;
2872 }
2873 break;
2874
2875 case PROT_MSC: /* MouseSystems Corp */
2876 usleep(100000);
2877 xf86FlushInput(pInfo->fd);
2878 break;
2879
2880 case PROT_ACECAD:
2881 /* initialize */
2882 /* A nul character resets. */
2883 xf86WriteSerial(pInfo->fd, "", 1);
2884 usleep(50000);
2885 /* Stream out relative mode high resolution increments of 1. */
2886 xf86WriteSerial(pInfo->fd, "@EeI!", 5);
2887 break;
2888
2889 case PROT_BM: /* bus/InPort mouse */
2890 if (osInfo->SetBMRes)
2891 osInfo->SetBMRes(pInfo, pMse->protocol, pMse->sampleRate,
2892 pMse->resolution);
2893 break;
2894
2895 case PROT_GENPS2:
2896 ps2Init = FALSE;
2897 break;
2898
2899 case PROT_PS2:
2900 case PROT_GLIDEPS2:
2901 break;
2902
2903 case PROT_IMPS2: /* IntelliMouse */
2904 {
2905 static unsigned char seq[] = { 243, 200, 243, 100, 243, 80 };
2906 param = seq;
2907 paramlen = sizeof(seq);
2908 }
2909 break;
2910
2911 case PROT_EXPPS2: /* IntelliMouse Explorer */
2912 {
2913 static unsigned char seq[] = { 243, 200, 243, 100, 243, 80,
2914 243, 200, 243, 200, 243, 80 };
2915
2916 param = seq;
2917 paramlen = sizeof(seq);
2918 }
2919 break;
2920
2921 case PROT_NETPS2: /* NetMouse, NetMouse Pro, Mie Mouse */
2922 case PROT_NETSCPS2: /* NetScroll */
2923 {
2924 static unsigned char seq[] = { 232, 3, 230, 230, 230, 233 };
2925
2926 param = seq;
2927 paramlen = sizeof(seq);
2928 }
2929 break;
2930
2931 case PROT_MMPS2: /* MouseMan+, FirstMouse+ */
2932 {
2933 static unsigned char seq[] = { 230, 232, 0, 232, 3, 232, 2, 232, 1,
2934 230, 232, 3, 232, 1, 232, 2, 232, 3 };
2935 param = seq;
2936 paramlen = sizeof(seq);
2937 }
2938 break;
2939
2940 case PROT_THINKPS2: /* ThinkingMouse */
2941 {
2942 static unsigned char seq[] = { 243, 10, 232, 0, 243, 20, 243, 60,
2943 243, 40, 243, 20, 243, 20, 243, 60,
2944 243, 40, 243, 20, 243, 20 };
2945 param = seq;
2946 paramlen = sizeof(seq);
2947 }
2948 break;
2949 case PROT_SYSMOUSE:
2950 if (osInfo->SetMiscRes)
2951 osInfo->SetMiscRes(pInfo, pMse->protocol, pMse->sampleRate,
2952 pMse->resolution);
2953 break;
2954
2955 default:
2956 /* Nothing to do. */
2957 break;
2958 }
2959
2960 if (pMse->class & (MSE_PS2 | MSE_XPS2)) {
2961 /*
2962 * If one part of the PS/2 mouse initialization fails
2963 * redo complete initialization. There are mice which
2964 * have occasional problems with initialization and
2965 * are in an unknown state.
2966 */
2967 if (ps2Init) {
2968 REDO:
2969 do_ps2Reset(pInfo);
2970 if (paramlen > 0) {
2971 if (!ps2SendPacket(pInfo,param,paramlen)) {
2972 usleep(30000);
2973 xf86FlushInput(pInfo->fd);
2974 if (!count--)
2975 return TRUE;
2976 goto REDO;
2977 }
2978 ps2GetDeviceID(pInfo);
2979 usleep(30000);
2980 xf86FlushInput(pInfo->fd);
2981 }
2982
2983 if (osInfo->SetPS2Res) {
2984 osInfo->SetPS2Res(pInfo, pMse->protocol, pMse->sampleRate,
2985 pMse->resolution);
2986 } else {
2987 unsigned char c2[2];
2988
2989 c = 0xE6; /*230*/ /* 1:1 scaling */
2990 if (!ps2SendPacket(pInfo,&c,1)) {
2991 if (!count--)
2992 return TRUE;
2993 goto REDO;
2994 }
2995 c2[0] = 0xF3; /*243*/ /* set sampling rate */
2996 if (pMse->sampleRate > 0) {
2997 if (pMse->sampleRate >= 200)
2998 c2[1] = 200;
2999 else if (pMse->sampleRate >= 100)
3000 c2[1] = 100;
3001 else if (pMse->sampleRate >= 80)
3002 c2[1] = 80;
3003 else if (pMse->sampleRate >= 60)
3004 c2[1] = 60;
3005 else if (pMse->sampleRate >= 40)
3006 c2[1] = 40;
3007 else
3008 c2[1] = 20;
3009 } else {
3010 c2[1] = 100;
3011 }
3012 if (!ps2SendPacket(pInfo,c2,2)) {
3013 if (!count--)
3014 return TRUE;
3015 goto REDO;
3016 }
3017 c2[0] = 0xE8; /*232*/ /* set device resolution */
3018 if (pMse->resolution > 0) {
3019 if (pMse->resolution >= 200)
3020 c2[1] = 3;
3021 else if (pMse->resolution >= 100)
3022 c2[1] = 2;
3023 else if (pMse->resolution >= 50)
3024 c2[1] = 1;
3025 else
3026 c2[1] = 0;
3027 } else {
3028 c2[1] = 3; /* used to be 2, W. uses 3 */
3029 }
3030 if (!ps2SendPacket(pInfo,c2,2)) {
3031 if (!count--)
3032 return TRUE;
3033 goto REDO;
3034 }
3035 usleep(30000);
3036 xf86FlushInput(pInfo->fd);
3037 if (!ps2EnableDataReporting(pInfo)) {
3038 xf86Msg(X_INFO, "%s: ps2EnableDataReporting: failed\n",
3039 pInfo->name);
3040 xf86FlushInput(pInfo->fd);
3041 if (!count--)
3042 return TRUE;
3043 goto REDO;
3044 } else {
3045 xf86Msg(X_INFO, "%s: ps2EnableDataReporting: succeeded\n",
3046 pInfo->name);
3047 }
3048 }
3049 /*
3050 * The PS/2 reset handling needs to be rechecked.
3051 * We need to wait until after the 4.3 release.
3052 */
3053 }
3054 } else {
3055 if (paramlen > 0) {
3056 if (xf86WriteSerial(pInfo->fd, param, paramlen) != paramlen)
3057 xf86Msg(X_ERROR, "%s: Mouse initialization failed\n",
3058 pInfo->name);
3059 usleep(30000);
3060 xf86FlushInput(pInfo->fd);
3061 }
3062 }
3063
3064 return TRUE;
3065}
3066
3067#ifdef SUPPORT_MOUSE_RESET
3068static Bool
3069mouseReset(InputInfoPtr pInfo, unsigned char val)
3070{
3071 MouseDevPtr pMse = pInfo->private;
3072 mousePrivPtr mousepriv = (mousePrivPtr)pMse->mousePriv;
3073 CARD32 prevEvent = mousepriv->lastEvent;
3074 Bool expectReset = FALSE;
3075 Bool ret = FALSE;
3076
3077 mousepriv->lastEvent = GetTimeInMillis();
3078
3079#ifdef EXTMOUSEDEBUG
3080 ErrorF("byte: 0x%x time: %li\n",val,mousepriv->lastEvent);
3081#endif
3082 /*
3083 * We believe that the following is true:
3084 * When the mouse is replugged it will send a reset package
3085 * It takes several seconds to replug a mouse: We don't see
3086 * events for several seconds before we see the replug event package.
3087 * There is no significant delay between consecutive bytes
3088 * of a replug event package.
3089 * There are no bytes sent after the replug event package until
3090 * the mouse is reset.
3091 */
3092
3093 if (mousepriv->current == 0
3094 && (mousepriv->lastEvent - prevEvent) < 4000)
3095 return FALSE;
3096
3097 if (mousepriv->current > 0
3098 && (mousepriv->lastEvent - prevEvent) >= 1000) {
3099 mousepriv->inReset = FALSE;
3100 mousepriv->current = 0;
3101 return FALSE;
3102 }
3103
3104 if (mousepriv->inReset)
3105 mousepriv->inReset = FALSE;
3106
3107#ifdef EXTMOUSEDEBUG
3108 ErrorF("Mouse Current: %i 0x%x\n",mousepriv->current, val);
3109#endif
3110
3111 /* here we put the mouse specific reset detction */
3112 /* They need to do three things: */
3113 /* Check if byte may be a reset byte */
3114 /* If so: Set expectReset TRUE */
3115 /* If convinced: Set inReset TRUE */
3116 /* Register BlockAndWakeupHandler */
3117
3118 /* PS/2 */
3119 {
3120 unsigned char seq[] = { 0xaa, 0x00 };
3121 int len = sizeof(seq);
3122
3123 if (seq[mousepriv->current] == val)
3124 expectReset = TRUE;
3125
3126 if (len == mousepriv->current + 1) {
3127 mousepriv->inReset = TRUE;
3128 mousepriv->expires = GetTimeInMillis() + 1000;
3129
3130#ifdef EXTMOUSEDEBUG
3131 ErrorF("Found PS/2 Reset string\n");
3132#endif
3133 RegisterBlockAndWakeupHandlers (ps2BlockHandler,
3134 ps2WakeupHandler, (pointer) pInfo);
3135 ret = TRUE;
3136 }
3137 }
3138
3139 if (!expectReset)
3140 mousepriv->current = 0;
3141 else
3142 mousepriv->current++;
3143 return ret;
3144}
3145
3146static void
3147ps2BlockHandler(pointer data, struct timeval **waitTime,
3148 pointer LastSelectMask)
3149{
3150 InputInfoPtr pInfo = (InputInfoPtr) data;
3151 MouseDevPtr pMse = (MouseDevPtr) pInfo->private;
3152 mousePrivPtr mousepriv = (mousePrivPtr)pMse->mousePriv;
3153 int ms;
3154
3155 if (mousepriv->inReset) {
3156 ms = mousepriv->expires - GetTimeInMillis ();
3157 if (ms <= 0)
3158 ms = 0;
3159 AdjustWaitForDelay (waitTime, ms);
3160 } else
3161 RemoveBlockAndWakeupHandlers (ps2BlockHandler, ps2WakeupHandler,
3162 (pointer) pInfo);
3163}
3164
3165static void
3166ps2WakeupHandler(pointer data, int i, pointer LastSelectMask)
3167{
3168 InputInfoPtr pInfo = (InputInfoPtr) data;
3169 MouseDevPtr pMse = (MouseDevPtr) pInfo->private;
3170 mousePrivPtr mousepriv = (mousePrivPtr)pMse->mousePriv;
3171 int ms;
3172
3173 if (mousepriv->inReset) {
3174 unsigned char val;
3175 int blocked;
3176
3177 ms = mousepriv->expires - GetTimeInMillis();
3178 if (ms > 0)
3179 return;
3180
3181 blocked = xf86BlockSIGIO ();
3182
3183 xf86MsgVerb(X_INFO,3,
3184 "Got reinsert event: reinitializing PS/2 mouse\n");
3185 val = 0xf4;
3186 if (xf86WriteSerial(pInfo->fd, &val, 1) != 1)
3187 xf86Msg(X_ERROR, "%s: Write to mouse failed\n",
3188 pInfo->name);
3189 xf86UnblockSIGIO(blocked);
3190 }
3191 RemoveBlockAndWakeupHandlers (ps2BlockHandler, ps2WakeupHandler,
3192 (pointer) pInfo);
3193}
3194#endif /* SUPPORT_MOUSE_RESET */
3195
3196/************************************************************
3197 *
3198 * Autoprobe stuff
3199 *
3200 ************************************************************/
3201#ifdef EXTMOUSEDEBUG
3202# define AP_DBG(x) { ErrorF("Autoprobe: "); ErrorF x; }
3203# define AP_DBGC(x) ErrorF x ;
3204# else
3205# define AP_DBG(x)
3206# define AP_DBGC(x)
3207#endif
3208
3209MouseProtocolID hardProtocolList[] = { PROT_MSC, PROT_MM, PROT_LOGI,
3210 PROT_LOGIMAN, PROT_MMHIT,
3211 PROT_GLIDE, PROT_IMSERIAL,
3212 PROT_THINKING, PROT_ACECAD,
3213 PROT_THINKPS2, PROT_MMPS2,
3214 PROT_GLIDEPS2,
3215 PROT_NETSCPS2, PROT_EXPPS2,PROT_IMPS2,
3216 PROT_GENPS2, PROT_NETPS2,
3217 PROT_MS,
3218 PROT_UNKNOWN
3219};
3220
3221MouseProtocolID softProtocolList[] = { PROT_MSC, PROT_MM, PROT_LOGI,
3222 PROT_LOGIMAN, PROT_MMHIT,
3223 PROT_GLIDE, PROT_IMSERIAL,
3224 PROT_THINKING, PROT_ACECAD,
3225 PROT_THINKPS2, PROT_MMPS2,
3226 PROT_GLIDEPS2,
3227 PROT_NETSCPS2 ,PROT_IMPS2,
3228 PROT_GENPS2,
3229 PROT_MS,
3230 PROT_UNKNOWN
3231};
3232
3233static const char *
3234autoOSProtocol(InputInfoPtr pInfo, int *protoPara)
3235{
3236 MouseDevPtr pMse = pInfo->private;
3237 const char *name = NULL;
3238 MouseProtocolID protocolID = PROT_UNKNOWN;
3239
3240 /* Check if the OS has a detection mechanism. */
3241 if (osInfo->SetupAuto) {
3242 name = osInfo->SetupAuto(pInfo, protoPara);
3243 if (name) {
3244 protocolID = ProtocolNameToID(name);
3245 switch (protocolID) {
3246 case PROT_UNKNOWN:
3247 /* Check for a builtin OS-specific protocol. */
3248 if (osInfo->CheckProtocol && osInfo->CheckProtocol(name)) {
3249 /* We can only come here if the protocol has been
3250 * changed to auto thru the xf86misc extension
3251 * and we have detected an OS specific builtin
3252 * protocol. Currently we cannot handle this */
3253 name = NULL;
3254 } else
3255 name = NULL;
3256 break;
3257 case PROT_UNSUP:
3258 name = NULL;
3259 break;
3260 default:
3261 break;
3262 }
3263 }
3264 }
3265 if (!name) {
3266 /* A PnP serial mouse? */
3267 protocolID = MouseGetPnpProtocol(pInfo);
3268 if (protocolID >= 0 && protocolID < PROT_NUMPROTOS) {
3269 name = ProtocolIDToName(protocolID);
3270 xf86Msg(X_PROBED, "%s: PnP-detected protocol: \"%s\"\n",
3271 pInfo->name, name);
3272 }
3273 }
3274 if (!name && HAVE_GUESS_PROTOCOL && osInfo->GuessProtocol) {
3275 name = osInfo->GuessProtocol(pInfo, 0);
3276 if (name)
3277 protocolID = ProtocolNameToID(name);
3278 }
3279
3280 if (name) {
3281 pMse->protocolID = protocolID;
3282 }
3283
3284 return name;
3285}
3286
3287/*
3288 * createProtocolList() -- create a list of protocols which may
3289 * match on the incoming data stream.
3290 */
3291static void
3292createProtoList(MouseDevPtr pMse, MouseProtocolID *protoList)
3293{
3294 int i, j, k = 0;
3295 MouseProtocolID prot;
3296 unsigned char *para;
3297 mousePrivPtr mPriv = (mousePrivPtr)pMse->mousePriv;
3298 MouseProtocolID *tmplist = NULL;
3299 int blocked;
3300
3301 AP_DBGC(("Autoprobe: "));
3302 for (i = 0; i < mPriv->count; i++)
3303 AP_DBGC(("%2.2x ", (unsigned char) mPriv->data[i]));
3304 AP_DBGC(("\n"));
3305
3306 blocked = xf86BlockSIGIO ();
3307
3308 /* create a private copy first so we can write in the old list */
3309 if ((tmplist = xalloc(sizeof(MouseProtocolID) * NUM_AUTOPROBE_PROTOS))){
3310 for (i = 0; protoList[i] != PROT_UNKNOWN; i++) {
3311 tmplist[i] = protoList[i];
3312 }
3313 tmplist[i] = PROT_UNKNOWN;
3314 protoList = tmplist;
3315 } else
3316 return;
3317
3318 for (i = 0; ((prot = protoList[i]) != PROT_UNKNOWN
3319 && (k < NUM_AUTOPROBE_PROTOS - 1)) ; i++) {
3320 Bool bad = TRUE;
3321 unsigned char byte = 0;
3322 int count = 0;
3323 int next_header_candidate = 0;
3324 int header_count = 0;
3325
3326 if (!GetProtocol(prot))
3327 continue;
3328 para = proto[prot];
3329
3330 AP_DBG(("Protocol: %s ", ProtocolIDToName(prot)));
3331
3332#ifdef EXTMOUSEDEBUG
3333 for (j = 0; j < 7; j++)
3334 AP_DBGC(("%2.2x ", (unsigned char) para[j]));
3335 AP_DBGC(("\n"));
3336#endif
3337 j = 0;
3338 while (1) {
3339 /* look for header */
3340 while (j < mPriv->count) {
3341 if (((byte = mPriv->data[j++]) & para[0]) == para[1]){
3342 AP_DBG(("found header %2.2x\n",byte));
3343 next_header_candidate = j;
3344 count = 1;
3345 break;
3346 } else {
3347 /*
3348 * Bail ot if number of bytes per package have
3349 * been tested for header.
3350 * Take bytes per package of leading garbage into
3351 * account.
3352 */
3353 if (j > para[4] && ++header_count > para[4]) {
3354 j = mPriv->count;
3355 break;
3356 }
3357 }
3358 }
3359 /* check if remaining data matches protocol */
3360 while (j < mPriv->count) {
3361 byte = mPriv->data[j++];
3362 if (count == para[4]) {
3363 count = 0;
3364 /* check and eat excess byte */
3365 if (((byte & para[0]) != para[1])
3366 && ((byte & para[5]) == para[6])) {
3367 AP_DBG(("excess byte found\n"));
3368 continue;
3369 }
3370 }
3371 if (count == 0) {
3372 /* validate next header */
3373 bad = FALSE;
3374 AP_DBG(("Complete set found\n"));
3375 if ((byte & para[0]) != para[1]) {
3376 AP_DBG(("Autoprobe: header bad\n"));
3377 bad = TRUE;
3378 break;
3379 } else {
3380 count++;
3381 continue;
3382 }
3383 }
3384 /* validate data */
3385 else if (((byte & para[2]) != para[3])
3386 || ((para[7] & MPF_SAFE)
3387 && ((byte & para[0]) == para[1]))) {
3388 AP_DBG(("data bad\n"));
3389 bad = TRUE;
3390 break;
3391 } else {
3392 count ++;
3393 continue;
3394 }
3395 }
3396 if (!bad) {
3397 /* this is a matching protocol */
3398 mPriv->protoList[k++] = prot;
3399 AP_DBG(("Autoprobe: Adding protocol %s to list (entry %i)\n",
3400 ProtocolIDToName(prot),k-1));
3401 break;
3402 }
3403 j = next_header_candidate;
3404 next_header_candidate = 0;
3405 /* we have tested number of bytes per package for header */
3406 if (j > para[4] && ++header_count > para[4])
3407 break;
3408 /* we have not found anything that looks like a header */
3409 if (!next_header_candidate)
3410 break;
3411 AP_DBG(("Looking for new header\n"));
3412 }
3413 }
3414
3415 xf86UnblockSIGIO(blocked);
3416
3417 mPriv->protoList[k] = PROT_UNKNOWN;
3418
3419 xfree(tmplist);
3420}
3421
3422
3423/* This only needs to be done once */
3424void **serialDefaultsList = NULL;
3425
3426/*
3427 * createSerialDefaultsLists() - create a list of the different default
3428 * settings for the serial interface of the known protocols.
3429 */
3430static void
3431createSerialDefaultsList(void)
3432{
3433 int i = 0, j, k;
3434
3435 serialDefaultsList = (void **)xnfalloc(sizeof(void*));
3436 serialDefaultsList[0] = NULL;
3437
3438 for (j = 0; mouseProtocols[j].name; j++) {
3439 if (!mouseProtocols[j].defaults)
3440 continue;
3441 for (k = 0; k < i; k++)
3442 if (mouseProtocols[j].defaults == serialDefaultsList[k])
3443 continue;
3444 i++;
3445 serialDefaultsList = (void**)xnfrealloc(serialDefaultsList,
3446 sizeof(void*)*(i+1));
3447 serialDefaultsList[i-1] = mouseProtocols[j].defaults;
3448 serialDefaultsList[i] = NULL;
3449 }
3450}
3451
3452typedef enum {
3453 STATE_INVALID,
3454 STATE_UNCERTAIN,
3455 STATE_VALID
3456} validState;
3457
3458/* Probing threshold values */
3459#define PROBE_UNCERTAINTY 50
3460#define BAD_CERTAINTY 6
3461#define BAD_INC_CERTAINTY 1
3462#define BAD_INC_CERTAINTY_WHEN_SYNC_LOST 2
3463
3464static validState
3465validCount(mousePrivPtr mPriv, Bool inSync, Bool lostSync)
3466{
3467 if (inSync) {
3468 if (!--mPriv->goodCount) {
3469 /* we are sure to have found the correct protocol */
3470 mPriv->badCount = 0;
3471 return STATE_VALID;
3472 }
3473 AP_DBG(("%i successful rounds to go\n",
3474 mPriv->goodCount));
3475 return STATE_UNCERTAIN;
3476 }
3477
3478
3479 /* We are out of sync again */
3480 mPriv->goodCount = PROBE_UNCERTAINTY;
3481 /* We increase uncertainty of having the correct protocol */
3482 mPriv->badCount+= lostSync ? BAD_INC_CERTAINTY_WHEN_SYNC_LOST
3483 : BAD_INC_CERTAINTY;
3484
3485 if (mPriv->badCount < BAD_CERTAINTY) {
3486 /* We are not convinced yet to have the wrong protocol */
3487 AP_DBG(("Changing protocol after: %i rounds\n",
3488 BAD_CERTAINTY - mPriv->badCount));
3489 return STATE_UNCERTAIN;
3490 }
3491 return STATE_INVALID;
3492}
3493
3494#define RESET_VALIDATION mPriv->goodCount = PROBE_UNCERTAINTY;\
3495 mPriv->badCount = 0;\
3496 mPriv->prevDx = 0;\
3497 mPriv->prevDy = 0;\
3498 mPriv->accDx = 0;\
3499 mPriv->accDy = 0;\
3500 mPriv->acc = 0;
3501
3502static void
3503autoProbeMouse(InputInfoPtr pInfo, Bool inSync, Bool lostSync)
3504{
3505 MouseDevPtr pMse = pInfo->private;
3506 mousePrivPtr mPriv = (mousePrivPtr)pMse->mousePriv;
3507
3508 MouseProtocolID *protocolList = NULL;
3509
3510 while (1) {
3511 switch (mPriv->autoState) {
3512 case AUTOPROBE_GOOD:
3513 if (inSync)
3514 return;
3515 AP_DBG(("State GOOD\n"));
3516 RESET_VALIDATION;
3517 mPriv->autoState = AUTOPROBE_VALIDATE1;
3518 return;
3519 case AUTOPROBE_H_GOOD:
3520 if (inSync)
3521 return;
3522 AP_DBG(("State H_GOOD\n"));
3523 RESET_VALIDATION;
3524 mPriv->autoState = AUTOPROBE_H_VALIDATE2;
3525 return;
3526 case AUTOPROBE_H_NOPROTO:
3527 AP_DBG(("State H_NOPROTO\n"));
3528 mPriv->protocolID = 0;
3529 mPriv->autoState = AUTOPROBE_H_SETPROTO;
3530 break;
3531 case AUTOPROBE_H_SETPROTO:
3532 AP_DBG(("State H_SETPROTO\n"));
3533 if ((pMse->protocolID = hardProtocolList[mPriv->protocolID++])
3534 == PROT_UNKNOWN) {
3535 mPriv->protocolID = 0;
3536 break;
3537 } else if (GetProtocol(pMse->protocolID) && SetupMouse(pInfo)) {
3538 FlushButtons(pMse);
3539 RESET_VALIDATION;
3540 AP_DBG(("Autoprobe: Trying Protocol: %s\n",
3541 ProtocolIDToName(pMse->protocolID)));
3542 mPriv->autoState = AUTOPROBE_H_VALIDATE1;
3543 return;
3544 }
3545 break;
3546 case AUTOPROBE_H_VALIDATE1:
3547 AP_DBG(("State H_VALIDATE1\n"));
3548 switch (validCount(mPriv,inSync,lostSync)) {
3549 case STATE_INVALID:
3550 mPriv->autoState = AUTOPROBE_H_SETPROTO;
3551 break;
3552 case STATE_VALID:
3553 xf86Msg(X_INFO,"Mouse autoprobe: selecting %s protocol\n",
3554 ProtocolIDToName(pMse->protocolID));
3555 mPriv->autoState = AUTOPROBE_H_GOOD;
3556 return;
3557 case STATE_UNCERTAIN:
3558 return;
3559 default:
3560 break;
3561 }
3562 break;
3563 case AUTOPROBE_H_VALIDATE2:
3564 AP_DBG(("State H_VALIDATE2\n"));
3565 switch (validCount(mPriv,inSync,lostSync)) {
3566 case STATE_INVALID:
3567 mPriv->autoState = AUTOPROBE_H_AUTODETECT;
3568 break;
3569 case STATE_VALID:
3570 xf86Msg(X_INFO,"Mouse autoprobe: selecting %s protocol\n",
3571 ProtocolIDToName(pMse->protocolID));
3572 mPriv->autoState = AUTOPROBE_H_GOOD;
3573 return;
3574 case STATE_UNCERTAIN:
3575 return;
3576 }
3577 break;
3578 case AUTOPROBE_H_AUTODETECT:
3579 AP_DBG(("State H_AUTODETECT\n"));
3580 pMse->protocolID = PROT_AUTO;
3581 AP_DBG(("Looking for PnP/OS mouse\n"));
3582 mPriv->count = 0;
3583 SetupMouse(pInfo);
3584 if (pMse->protocolID != PROT_AUTO)
3585 mPriv->autoState = AUTOPROBE_H_GOOD;
3586 else
3587 mPriv->autoState = AUTOPROBE_H_NOPROTO;
3588 break;
3589 case AUTOPROBE_NOPROTO:
3590 AP_DBG(("State NOPROTO\n"));
3591 mPriv->count = 0;
3592 mPriv->serialDefaultsNum = -1;
3593 mPriv->autoState = AUTOPROBE_COLLECT;
3594 break;
3595 case AUTOPROBE_COLLECT:
3596 AP_DBG(("State COLLECT\n"));
3597 if (mPriv->count <= NUM_MSE_AUTOPROBE_BYTES)
3598 return;
3599 protocolList = softProtocolList;
3600 mPriv->autoState = AUTOPROBE_CREATE_PROTOLIST;
3601 break;
3602 case AUTOPROBE_CREATE_PROTOLIST:
3603 AP_DBG(("State CREATE_PROTOLIST\n"));
3604 createProtoList(pMse, protocolList);
3605 mPriv->protocolID = 0;
3606 mPriv->autoState = AUTOPROBE_SWITCH_PROTOCOL;
3607 break;
3608 case AUTOPROBE_AUTODETECT:
3609 AP_DBG(("State AUTODETECT\n"));
3610 pMse->protocolID = PROT_AUTO;
3611 AP_DBG(("Looking for PnP/OS mouse\n"));
3612 mPriv->count = 0;
3613 SetupMouse(pInfo);
3614 if (pMse->protocolID != PROT_AUTO)
3615 mPriv->autoState = AUTOPROBE_GOOD;
3616 else
3617 mPriv->autoState = AUTOPROBE_NOPROTO;
3618 break;
3619 case AUTOPROBE_VALIDATE1:
3620 AP_DBG(("State VALIDATE1\n"));
3621 switch (validCount(mPriv,inSync,lostSync)) {
3622 case STATE_INVALID:
3623 mPriv->autoState = AUTOPROBE_AUTODETECT;
3624 break;
3625 case STATE_VALID:
3626 xf86Msg(X_INFO,"Mouse autoprobe: selecting %s protocol\n",
3627 ProtocolIDToName(pMse->protocolID));
3628 mPriv->autoState = AUTOPROBE_GOOD;
3629 break;
3630 case STATE_UNCERTAIN:
3631 return;
3632 }
3633 break;
3634 case AUTOPROBE_VALIDATE2:
3635 AP_DBG(("State VALIDATE2\n"));
3636 switch (validCount(mPriv,inSync,lostSync)) {
3637 case STATE_INVALID:
3638 protocolList = &mPriv->protoList[mPriv->protocolID];
3639 mPriv->autoState = AUTOPROBE_CREATE_PROTOLIST;
3640 break;
3641 case STATE_VALID:
3642 xf86Msg(X_INFO,"Mouse autoprobe: selecting %s protocol\n",
3643 ProtocolIDToName(pMse->protocolID));
3644 mPriv->autoState = AUTOPROBE_GOOD;
3645 break;
3646 case STATE_UNCERTAIN:
3647 return;
3648 }
3649 break;
3650 case AUTOPROBE_SWITCHSERIAL:
3651 {
3652 pointer serialDefaults;
3653 AP_DBG(("State SWITCHSERIAL\n"));
3654
3655 if (!serialDefaultsList)
3656 createSerialDefaultsList();
3657
3658 AP_DBG(("Switching serial params\n"));
3659 if ((serialDefaults =
3660 serialDefaultsList[++mPriv->serialDefaultsNum]) == NULL) {
3661 mPriv->serialDefaultsNum = 0;
3662 } else {
3663 pointer tmp = xf86OptionListCreate(serialDefaults, -1, 0);
3664 xf86SetSerial(pInfo->fd, tmp);
3665 xf86OptionListFree(tmp);
3666 mPriv->count = 0;
3667 mPriv->autoState = AUTOPROBE_COLLECT;
3668 }
3669 break;
3670 }
3671 case AUTOPROBE_SWITCH_PROTOCOL:
3672 {
3673 MouseProtocolID proto;
3674 void *defaults;
3675 AP_DBG(("State SWITCH_PROTOCOL\n"));
3676 proto = mPriv->protoList[mPriv->protocolID++];
3677 if (proto == PROT_UNKNOWN)
3678 mPriv->autoState = AUTOPROBE_SWITCHSERIAL;
3679 else if (!(defaults = GetProtocol(proto)->defaults)
3680 || (mPriv->serialDefaultsNum == -1
3681 && (defaults == msDefaults))
3682 || (mPriv->serialDefaultsNum != -1
3683 && serialDefaultsList[mPriv->serialDefaultsNum]
3684 == defaults)) {
3685 AP_DBG(("Changing Protocol to %s\n",
3686 ProtocolIDToName(proto)));
3687 SetMouseProto(pMse,proto);
3688 FlushButtons(pMse);
3689 RESET_VALIDATION;
3690 mPriv->autoState = AUTOPROBE_VALIDATE2;
3691 return;
3692 }
3693 break;
3694 }
3695 }
3696 }
3697}
3698
3699static Bool
3700autoGood(MouseDevPtr pMse)
3701{
3702 mousePrivPtr mPriv = (mousePrivPtr)pMse->mousePriv;
3703
3704 if (!pMse->autoProbe)
3705 return TRUE;
3706
3707 switch (mPriv->autoState) {
3708 case AUTOPROBE_GOOD:
3709 case AUTOPROBE_H_GOOD:
3710 return TRUE;
3711 case AUTOPROBE_VALIDATE1: /* @@@ */
3712 case AUTOPROBE_H_VALIDATE1: /* @@@ */
3713 case AUTOPROBE_VALIDATE2:
3714 case AUTOPROBE_H_VALIDATE2:
3715 if (mPriv->goodCount < PROBE_UNCERTAINTY/2)
3716 return TRUE;
3717 default:
3718 return FALSE;
3719 }
3720}
3721
3722
3723#define TOT_THRESHOLD 3000
3724#define VAL_THRESHOLD 40
3725
3726/*
3727 * checkForErraticMovements() -- check if mouse 'jumps around'.
3728 */
3729static void
3730checkForErraticMovements(InputInfoPtr pInfo, int dx, int dy)
3731{
3732 MouseDevPtr pMse = pInfo->private;
3733 mousePrivPtr mPriv = (mousePrivPtr)pMse->mousePriv;
3734#if 1
3735 if (!mPriv->goodCount)
3736 return;
3737#endif
3738#if 0
3739 if (abs(dx - mPriv->prevDx) > 300
3740 || abs(dy - mPriv->prevDy) > 300)
3741 AP_DBG(("erratic1 behaviour\n"));
3742#endif
3743 if (abs(dx) > VAL_THRESHOLD) {
3744 if (sign(dx) == sign(mPriv->prevDx)) {
3745 mPriv->accDx += dx;
3746 if (abs(mPriv->accDx) > mPriv->acc) {
3747 mPriv->acc = abs(mPriv->accDx);
3748 AP_DBG(("acc=%i\n",mPriv->acc));
3749 }
3750 else
3751 AP_DBG(("accDx=%i\n",mPriv->accDx));
3752 } else {
3753 mPriv->accDx = 0;
3754 }
3755 }
3756
3757 if (abs(dy) > VAL_THRESHOLD) {
3758 if (sign(dy) == sign(mPriv->prevDy)) {
3759 mPriv->accDy += dy;
3760 if (abs(mPriv->accDy) > mPriv->acc) {
3761 mPriv->acc = abs(mPriv->accDy);
3762 AP_DBG(("acc: %i\n",mPriv->acc));
3763 } else
3764 AP_DBG(("accDy=%i\n",mPriv->accDy));
3765 } else {
3766 mPriv->accDy = 0;
3767 }
3768 }
3769 mPriv->prevDx = dx;
3770 mPriv->prevDy = dy;
3771 if (mPriv->acc > TOT_THRESHOLD) {
3772 mPriv->goodCount = PROBE_UNCERTAINTY;
3773 mPriv->prevDx = 0;
3774 mPriv->prevDy = 0;
3775 mPriv->accDx = 0;
3776 mPriv->accDy = 0;
3777 mPriv->acc = 0;
3778 AP_DBG(("erratic2 behaviour\n"));
3779 autoProbeMouse(pInfo, FALSE,TRUE);
3780 }
3781}
3782
3783static void
3784SetMouseProto(MouseDevPtr pMse, MouseProtocolID protocolID)
3785{
3786 pMse->protocolID = protocolID;
3787 pMse->protocol = ProtocolIDToName(pMse->protocolID);
3788 pMse->class = ProtocolIDToClass(pMse->protocolID);
3789 if ((pMse->protocolID >= 0) && (pMse->protocolID < PROT_NUMPROTOS))
3790 memcpy(pMse->protoPara, proto[pMse->protocolID],
3791 sizeof(pMse->protoPara));
3792
3793 if (pMse->emulate3ButtonsSoft)
3794 pMse->emulate3Buttons = TRUE;
3795}
3796
3797/*
3798 * collectData() -- collect data bytes sent by mouse.
3799 */
3800static Bool
3801collectData(MouseDevPtr pMse, unsigned char u)
3802{
3803 mousePrivPtr mPriv = (mousePrivPtr)pMse->mousePriv;
3804 if (mPriv->count < NUM_MSE_AUTOPROBE_TOTAL) {
3805 mPriv->data[mPriv->count++] = u;
3806 if (mPriv->count <= NUM_MSE_AUTOPROBE_BYTES) {
3807 return TRUE;
3808 }
3809 }
3810 return FALSE;
3811}
3812
3813/**************** end of autoprobe stuff *****************/
3814
3815
3816
3817#ifdef XFree86LOADER
3818ModuleInfoRec MouseInfo = {
3819 1,
3820 "MOUSE",
3821 NULL,
3822 0,
3823 MouseAvailableOptions,
3824};
3825
3826static void
3827xf86MouseUnplug(pointer p)
3828{
3829}
3830static pointer
3831xf86MousePlug(pointer module,
3832 pointer options,
3833 int *errmaj,
3834 int *errmin)
3835{
3836 static Bool Initialised = FALSE;
3837
3838 if (!Initialised) {
3839 Initialised = TRUE;
3840#ifndef REMOVE_LOADER_CHECK_MODULE_INFO
3841 if (xf86LoaderCheckSymbol("xf86AddModuleInfo"))
3842#endif
3843 xf86AddModuleInfo(&MouseInfo, module);
3844 }
3845
3846 xf86AddInputDriver(&MOUSE, module, 0);
3847
3848 return module;
3849}
3850
3851static XF86ModuleVersionInfo xf86MouseVersionRec =
3852{
3853#ifdef VBOX
3854 "vboxmouse",
3855 "innotek GmbH",
3856#else
3857 "mouse",
3858 MODULEVENDORSTRING,
3859#endif
3860 MODINFOSTRING1,
3861 MODINFOSTRING2,
3862 XORG_VERSION_CURRENT,
3863 1, 1, 0,
3864 ABI_CLASS_XINPUT,
3865 ABI_XINPUT_VERSION,
3866 MOD_CLASS_XINPUT,
3867 {0, 0, 0, 0} /* signature, to be patched into the file by */
3868 /* a tool */
3869};
3870
3871#ifdef VBOX
3872_X_EXPORT XF86ModuleData vboxmouseModuleData = {
3873 &xf86MouseVersionRec,
3874 xf86MousePlug,
3875 xf86MouseUnplug
3876};
3877#else
3878_X_EXPORT XF86ModuleData mouseModuleData = {
3879 &xf86MouseVersionRec,
3880 xf86MousePlug,
3881 xf86MouseUnplug
3882};
3883#endif
3884
3885/*
3886 Look at hitachi device stuff.
3887*/
3888#endif /* XFree86LOADER */
3889
3890
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette