VirtualBox

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

Last change on this file since 1176 was 1, checked in by vboxsync, 55 years ago

import

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