VirtualBox

source: vbox/trunk/src/VBox/Additions/x11/vboxmouse/xorg70/mouse.c@ 35273

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

Additions/x11/vboxmouse: fix some warnings

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

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