moused.c revision 148161
1/** 2 ** Copyright (c) 1995 Michael Smith, All rights reserved. 3 ** 4 ** Redistribution and use in source and binary forms, with or without 5 ** modification, are permitted provided that the following conditions 6 ** are met: 7 ** 1. Redistributions of source code must retain the above copyright 8 ** notice, this list of conditions and the following disclaimer as 9 ** the first lines of this file unmodified. 10 ** 2. Redistributions in binary form must reproduce the above copyright 11 ** notice, this list of conditions and the following disclaimer in the 12 ** documentation and/or other materials provided with the distribution. 13 ** 3. All advertising materials mentioning features or use of this software 14 ** must display the following acknowledgment: 15 ** This product includes software developed by Michael Smith. 16 ** 4. The name of the author may not be used to endorse or promote products 17 ** derived from this software without specific prior written permission. 18 ** 19 ** 20 ** THIS SOFTWARE IS PROVIDED BY Michael Smith ``AS IS'' AND ANY 21 ** EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Michael Smith BE LIABLE FOR 24 ** ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 ** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 ** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 27 ** BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 28 ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 29 ** OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 30 ** EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 ** 32 **/ 33 34/** 35 ** MOUSED.C 36 ** 37 ** Mouse daemon : listens to a serial port, the bus mouse interface, or 38 ** the PS/2 mouse port for mouse data stream, interprets data and passes 39 ** ioctls off to the console driver. 40 ** 41 ** The mouse interface functions are derived closely from the mouse 42 ** handler in the XFree86 X server. Many thanks to the XFree86 people 43 ** for their great work! 44 ** 45 **/ 46 47#include <sys/cdefs.h> 48__FBSDID("$FreeBSD: head/usr.sbin/moused/moused.c 148161 2005-07-19 19:30:49Z philip $"); 49 50#include <sys/param.h> 51#include <sys/consio.h> 52#include <sys/linker.h> 53#include <sys/module.h> 54#include <sys/mouse.h> 55#include <sys/socket.h> 56#include <sys/stat.h> 57#include <sys/time.h> 58#include <sys/un.h> 59 60#include <ctype.h> 61#include <err.h> 62#include <errno.h> 63#include <fcntl.h> 64#include <limits.h> 65#include <setjmp.h> 66#include <signal.h> 67#include <stdarg.h> 68#include <stdio.h> 69#include <stdlib.h> 70#include <string.h> 71#include <syslog.h> 72#include <termios.h> 73#include <unistd.h> 74 75#define MAX_CLICKTHRESHOLD 2000 /* 2 seconds */ 76#define MAX_BUTTON2TIMEOUT 2000 /* 2 seconds */ 77#define DFLT_CLICKTHRESHOLD 500 /* 0.5 second */ 78#define DFLT_BUTTON2TIMEOUT 100 /* 0.1 second */ 79#define DFLT_SCROLLTHRESHOLD 3 /* 3 pixels */ 80 81/* Abort 3-button emulation delay after this many movement events. */ 82#define BUTTON2_MAXMOVE 3 83 84#define TRUE 1 85#define FALSE 0 86 87#define MOUSE_XAXIS (-1) 88#define MOUSE_YAXIS (-2) 89 90/* Logitech PS2++ protocol */ 91#define MOUSE_PS2PLUS_CHECKBITS(b) \ 92 ((((b[2] & 0x03) << 2) | 0x02) == (b[1] & 0x0f)) 93#define MOUSE_PS2PLUS_PACKET_TYPE(b) \ 94 (((b[0] & 0x30) >> 2) | ((b[1] & 0x30) >> 4)) 95 96#define ChordMiddle 0x0001 97#define Emulate3Button 0x0002 98#define ClearDTR 0x0004 99#define ClearRTS 0x0008 100#define NoPnP 0x0010 101#define VirtualScroll 0x0020 102#define HVirtualScroll 0x0040 103 104#define ID_NONE 0 105#define ID_PORT 1 106#define ID_IF 2 107#define ID_TYPE 4 108#define ID_MODEL 8 109#define ID_ALL (ID_PORT | ID_IF | ID_TYPE | ID_MODEL) 110 111#define debug(fmt, args...) do { \ 112 if (debug && nodaemon) \ 113 warnx(fmt, ##args); \ 114} while (0) 115 116#define logerr(e, fmt, args...) do { \ 117 log_or_warn(LOG_DAEMON | LOG_ERR, errno, fmt, ##args); \ 118 exit(e); \ 119} while (0) 120 121#define logerrx(e, fmt, args...) do { \ 122 log_or_warn(LOG_DAEMON | LOG_ERR, 0, fmt, ##args); \ 123 exit(e); \ 124} while (0) 125 126#define logwarn(fmt, args...) \ 127 log_or_warn(LOG_DAEMON | LOG_WARNING, errno, fmt, ##args) 128 129#define logwarnx(fmt, args...) \ 130 log_or_warn(LOG_DAEMON | LOG_WARNING, 0, fmt, ##args) 131 132/* structures */ 133 134/* symbol table entry */ 135typedef struct { 136 char *name; 137 int val; 138 int val2; 139} symtab_t; 140 141/* serial PnP ID string */ 142typedef struct { 143 int revision; /* PnP revision, 100 for 1.00 */ 144 char *eisaid; /* EISA ID including mfr ID and product ID */ 145 char *serial; /* serial No, optional */ 146 char *class; /* device class, optional */ 147 char *compat; /* list of compatible drivers, optional */ 148 char *description; /* product description, optional */ 149 int neisaid; /* length of the above fields... */ 150 int nserial; 151 int nclass; 152 int ncompat; 153 int ndescription; 154} pnpid_t; 155 156/* global variables */ 157 158int debug = 0; 159int nodaemon = FALSE; 160int background = FALSE; 161int identify = ID_NONE; 162int extioctl = FALSE; 163char *pidfile = "/var/run/moused.pid"; 164 165#define SCROLL_NOTSCROLLING 0 166#define SCROLL_PREPARE 1 167#define SCROLL_SCROLLING 2 168 169static int scroll_state; 170static int scroll_movement; 171static int hscroll_movement; 172 173/* local variables */ 174 175/* interface (the table must be ordered by MOUSE_IF_XXX in mouse.h) */ 176static symtab_t rifs[] = { 177 { "serial", MOUSE_IF_SERIAL }, 178 { "bus", MOUSE_IF_BUS }, 179 { "inport", MOUSE_IF_INPORT }, 180 { "ps/2", MOUSE_IF_PS2 }, 181 { "sysmouse", MOUSE_IF_SYSMOUSE }, 182 { "usb", MOUSE_IF_USB }, 183 { NULL, MOUSE_IF_UNKNOWN }, 184}; 185 186/* types (the table must be ordered by MOUSE_PROTO_XXX in mouse.h) */ 187static char *rnames[] = { 188 "microsoft", 189 "mousesystems", 190 "logitech", 191 "mmseries", 192 "mouseman", 193 "busmouse", 194 "inportmouse", 195 "ps/2", 196 "mmhitab", 197 "glidepoint", 198 "intellimouse", 199 "thinkingmouse", 200 "sysmouse", 201 "x10mouseremote", 202 "kidspad", 203 "versapad", 204 "jogdial", 205#if notyet 206 "mariqua", 207#endif 208 "gtco_digipad", 209 NULL 210}; 211 212/* models */ 213static symtab_t rmodels[] = { 214 { "NetScroll", MOUSE_MODEL_NETSCROLL }, 215 { "NetMouse/NetScroll Optical", MOUSE_MODEL_NET }, 216 { "GlidePoint", MOUSE_MODEL_GLIDEPOINT }, 217 { "ThinkingMouse", MOUSE_MODEL_THINK }, 218 { "IntelliMouse", MOUSE_MODEL_INTELLI }, 219 { "EasyScroll/SmartScroll", MOUSE_MODEL_EASYSCROLL }, 220 { "MouseMan+", MOUSE_MODEL_MOUSEMANPLUS }, 221 { "Kidspad", MOUSE_MODEL_KIDSPAD }, 222 { "VersaPad", MOUSE_MODEL_VERSAPAD }, 223 { "IntelliMouse Explorer", MOUSE_MODEL_EXPLORER }, 224 { "4D Mouse", MOUSE_MODEL_4D }, 225 { "4D+ Mouse", MOUSE_MODEL_4DPLUS }, 226 { "Synaptics Touchpad", MOUSE_MODEL_SYNAPTICS }, 227 { "generic", MOUSE_MODEL_GENERIC }, 228 { NULL, MOUSE_MODEL_UNKNOWN }, 229}; 230 231/* PnP EISA/product IDs */ 232static symtab_t pnpprod[] = { 233 /* Kensignton ThinkingMouse */ 234 { "KML0001", MOUSE_PROTO_THINK, MOUSE_MODEL_THINK }, 235 /* MS IntelliMouse */ 236 { "MSH0001", MOUSE_PROTO_INTELLI, MOUSE_MODEL_INTELLI }, 237 /* MS IntelliMouse TrackBall */ 238 { "MSH0004", MOUSE_PROTO_INTELLI, MOUSE_MODEL_INTELLI }, 239 /* Tremon Wheel Mouse MUSD */ 240 { "HTK0001", MOUSE_PROTO_INTELLI, MOUSE_MODEL_INTELLI }, 241 /* Genius PnP Mouse */ 242 { "KYE0001", MOUSE_PROTO_MS, MOUSE_MODEL_GENERIC }, 243 /* MouseSystems SmartScroll Mouse (OEM from Genius?) */ 244 { "KYE0002", MOUSE_PROTO_MS, MOUSE_MODEL_EASYSCROLL }, 245 /* Genius NetMouse */ 246 { "KYE0003", MOUSE_PROTO_INTELLI, MOUSE_MODEL_NET }, 247 /* Genius Kidspad, Easypad and other tablets */ 248 { "KYE0005", MOUSE_PROTO_KIDSPAD, MOUSE_MODEL_KIDSPAD }, 249 /* Genius EZScroll */ 250 { "KYEEZ00", MOUSE_PROTO_MS, MOUSE_MODEL_EASYSCROLL }, 251 /* Logitech Cordless MouseMan Wheel */ 252 { "LGI8033", MOUSE_PROTO_INTELLI, MOUSE_MODEL_MOUSEMANPLUS }, 253 /* Logitech MouseMan (new 4 button model) */ 254 { "LGI800C", MOUSE_PROTO_INTELLI, MOUSE_MODEL_MOUSEMANPLUS }, 255 /* Logitech MouseMan+ */ 256 { "LGI8050", MOUSE_PROTO_INTELLI, MOUSE_MODEL_MOUSEMANPLUS }, 257 /* Logitech FirstMouse+ */ 258 { "LGI8051", MOUSE_PROTO_INTELLI, MOUSE_MODEL_MOUSEMANPLUS }, 259 /* Logitech serial */ 260 { "LGI8001", MOUSE_PROTO_LOGIMOUSEMAN, MOUSE_MODEL_GENERIC }, 261 /* A4 Tech 4D/4D+ Mouse */ 262 { "A4W0005", MOUSE_PROTO_INTELLI, MOUSE_MODEL_4D }, 263 /* 8D Scroll Mouse */ 264 { "PEC9802", MOUSE_PROTO_INTELLI, MOUSE_MODEL_INTELLI }, 265 /* Mitsumi Wireless Scroll Mouse */ 266 { "MTM6401", MOUSE_PROTO_INTELLI, MOUSE_MODEL_INTELLI }, 267 268 /* MS bus */ 269 { "PNP0F00", MOUSE_PROTO_BUS, MOUSE_MODEL_GENERIC }, 270 /* MS serial */ 271 { "PNP0F01", MOUSE_PROTO_MS, MOUSE_MODEL_GENERIC }, 272 /* MS InPort */ 273 { "PNP0F02", MOUSE_PROTO_INPORT, MOUSE_MODEL_GENERIC }, 274 /* MS PS/2 */ 275 { "PNP0F03", MOUSE_PROTO_PS2, MOUSE_MODEL_GENERIC }, 276 /* 277 * EzScroll returns PNP0F04 in the compatible device field; but it 278 * doesn't look compatible... XXX 279 */ 280 /* MouseSystems */ 281 { "PNP0F04", MOUSE_PROTO_MSC, MOUSE_MODEL_GENERIC }, 282 /* MouseSystems */ 283 { "PNP0F05", MOUSE_PROTO_MSC, MOUSE_MODEL_GENERIC }, 284#if notyet 285 /* Genius Mouse */ 286 { "PNP0F06", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC }, 287 /* Genius Mouse */ 288 { "PNP0F07", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC }, 289#endif 290 /* Logitech serial */ 291 { "PNP0F08", MOUSE_PROTO_LOGIMOUSEMAN, MOUSE_MODEL_GENERIC }, 292 /* MS BallPoint serial */ 293 { "PNP0F09", MOUSE_PROTO_MS, MOUSE_MODEL_GENERIC }, 294 /* MS PnP serial */ 295 { "PNP0F0A", MOUSE_PROTO_MS, MOUSE_MODEL_GENERIC }, 296 /* MS PnP BallPoint serial */ 297 { "PNP0F0B", MOUSE_PROTO_MS, MOUSE_MODEL_GENERIC }, 298 /* MS serial comatible */ 299 { "PNP0F0C", MOUSE_PROTO_MS, MOUSE_MODEL_GENERIC }, 300 /* MS InPort comatible */ 301 { "PNP0F0D", MOUSE_PROTO_INPORT, MOUSE_MODEL_GENERIC }, 302 /* MS PS/2 comatible */ 303 { "PNP0F0E", MOUSE_PROTO_PS2, MOUSE_MODEL_GENERIC }, 304 /* MS BallPoint comatible */ 305 { "PNP0F0F", MOUSE_PROTO_MS, MOUSE_MODEL_GENERIC }, 306#if notyet 307 /* TI QuickPort */ 308 { "PNP0F10", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC }, 309#endif 310 /* MS bus comatible */ 311 { "PNP0F11", MOUSE_PROTO_BUS, MOUSE_MODEL_GENERIC }, 312 /* Logitech PS/2 */ 313 { "PNP0F12", MOUSE_PROTO_PS2, MOUSE_MODEL_GENERIC }, 314 /* PS/2 */ 315 { "PNP0F13", MOUSE_PROTO_PS2, MOUSE_MODEL_GENERIC }, 316#if notyet 317 /* MS Kids Mouse */ 318 { "PNP0F14", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC }, 319#endif 320 /* Logitech bus */ 321 { "PNP0F15", MOUSE_PROTO_BUS, MOUSE_MODEL_GENERIC }, 322#if notyet 323 /* Logitech SWIFT */ 324 { "PNP0F16", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC }, 325#endif 326 /* Logitech serial compat */ 327 { "PNP0F17", MOUSE_PROTO_LOGIMOUSEMAN, MOUSE_MODEL_GENERIC }, 328 /* Logitech bus compatible */ 329 { "PNP0F18", MOUSE_PROTO_BUS, MOUSE_MODEL_GENERIC }, 330 /* Logitech PS/2 compatible */ 331 { "PNP0F19", MOUSE_PROTO_PS2, MOUSE_MODEL_GENERIC }, 332#if notyet 333 /* Logitech SWIFT compatible */ 334 { "PNP0F1A", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC }, 335 /* HP Omnibook */ 336 { "PNP0F1B", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC }, 337 /* Compaq LTE TrackBall PS/2 */ 338 { "PNP0F1C", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC }, 339 /* Compaq LTE TrackBall serial */ 340 { "PNP0F1D", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC }, 341 /* MS Kidts Trackball */ 342 { "PNP0F1E", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC }, 343#endif 344 /* Interlink VersaPad */ 345 { "LNK0001", MOUSE_PROTO_VERSAPAD, MOUSE_MODEL_VERSAPAD }, 346 347 { NULL, MOUSE_PROTO_UNKNOWN, MOUSE_MODEL_GENERIC }, 348}; 349 350/* the table must be ordered by MOUSE_PROTO_XXX in mouse.h */ 351static unsigned short rodentcflags[] = 352{ 353 (CS7 | CREAD | CLOCAL | HUPCL), /* MicroSoft */ 354 (CS8 | CSTOPB | CREAD | CLOCAL | HUPCL), /* MouseSystems */ 355 (CS8 | CSTOPB | CREAD | CLOCAL | HUPCL), /* Logitech */ 356 (CS8 | PARENB | PARODD | CREAD | CLOCAL | HUPCL), /* MMSeries */ 357 (CS7 | CREAD | CLOCAL | HUPCL), /* MouseMan */ 358 0, /* Bus */ 359 0, /* InPort */ 360 0, /* PS/2 */ 361 (CS8 | CREAD | CLOCAL | HUPCL), /* MM HitTablet */ 362 (CS7 | CREAD | CLOCAL | HUPCL), /* GlidePoint */ 363 (CS7 | CREAD | CLOCAL | HUPCL), /* IntelliMouse */ 364 (CS7 | CREAD | CLOCAL | HUPCL), /* Thinking Mouse */ 365 (CS8 | CSTOPB | CREAD | CLOCAL | HUPCL), /* sysmouse */ 366 (CS7 | CREAD | CLOCAL | HUPCL), /* X10 MouseRemote */ 367 (CS8 | PARENB | PARODD | CREAD | CLOCAL | HUPCL), /* kidspad etc. */ 368 (CS8 | CREAD | CLOCAL | HUPCL), /* VersaPad */ 369 0, /* JogDial */ 370#if notyet 371 (CS8 | CSTOPB | CREAD | CLOCAL | HUPCL), /* Mariqua */ 372#endif 373 (CS8 | CREAD | HUPCL ), /* GTCO Digi-Pad */ 374}; 375 376static struct rodentparam { 377 int flags; 378 char *portname; /* /dev/XXX */ 379 int rtype; /* MOUSE_PROTO_XXX */ 380 int level; /* operation level: 0 or greater */ 381 int baudrate; 382 int rate; /* report rate */ 383 int resolution; /* MOUSE_RES_XXX or a positive number */ 384 int zmap[4]; /* MOUSE_{X|Y}AXIS or a button number */ 385 int wmode; /* wheel mode button number */ 386 int mfd; /* mouse file descriptor */ 387 int cfd; /* /dev/consolectl file descriptor */ 388 int mremsfd; /* mouse remote server file descriptor */ 389 int mremcfd; /* mouse remote client file descriptor */ 390 long clickthreshold; /* double click speed in msec */ 391 long button2timeout; /* 3 button emulation timeout */ 392 mousehw_t hw; /* mouse device hardware information */ 393 mousemode_t mode; /* protocol information */ 394 float accelx; /* Acceleration in the X axis */ 395 float accely; /* Acceleration in the Y axis */ 396 int scrollthreshold; /* Movement distance before virtual scrolling */ 397} rodent = { 398 .flags = 0, 399 .portname = NULL, 400 .rtype = MOUSE_PROTO_UNKNOWN, 401 .level = -1, 402 .baudrate = 1200, 403 .rate = 0, 404 .resolution = MOUSE_RES_UNKNOWN, 405 .zmap = { 0, 0, 0, 0 }, 406 .wmode = 0, 407 .mfd = -1, 408 .cfd = -1, 409 .mremsfd = -1, 410 .mremcfd = -1, 411 .clickthreshold = DFLT_CLICKTHRESHOLD, 412 .button2timeout = DFLT_BUTTON2TIMEOUT, 413 .accelx = 1.0, 414 .accely = 1.0, 415 .scrollthreshold = DFLT_SCROLLTHRESHOLD, 416}; 417 418/* button status */ 419struct button_state { 420 int count; /* 0: up, 1: single click, 2: double click,... */ 421 struct timeval tv; /* timestamp on the last button event */ 422}; 423static struct button_state bstate[MOUSE_MAXBUTTON]; /* button state */ 424static struct button_state *mstate[MOUSE_MAXBUTTON];/* mapped button st.*/ 425static struct button_state zstate[4]; /* Z/W axis state */ 426 427/* state machine for 3 button emulation */ 428 429#define S0 0 /* start */ 430#define S1 1 /* button 1 delayed down */ 431#define S2 2 /* button 3 delayed down */ 432#define S3 3 /* both buttons down -> button 2 down */ 433#define S4 4 /* button 1 delayed up */ 434#define S5 5 /* button 1 down */ 435#define S6 6 /* button 3 down */ 436#define S7 7 /* both buttons down */ 437#define S8 8 /* button 3 delayed up */ 438#define S9 9 /* button 1 or 3 up after S3 */ 439 440#define A(b1, b3) (((b1) ? 2 : 0) | ((b3) ? 1 : 0)) 441#define A_TIMEOUT 4 442#define S_DELAYED(st) (states[st].s[A_TIMEOUT] != (st)) 443 444static struct { 445 int s[A_TIMEOUT + 1]; 446 int buttons; 447 int mask; 448 int timeout; 449} states[10] = { 450 /* S0 */ 451 { { S0, S2, S1, S3, S0 }, 0, ~(MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN), FALSE }, 452 /* S1 */ 453 { { S4, S2, S1, S3, S5 }, 0, ~MOUSE_BUTTON1DOWN, FALSE }, 454 /* S2 */ 455 { { S8, S2, S1, S3, S6 }, 0, ~MOUSE_BUTTON3DOWN, FALSE }, 456 /* S3 */ 457 { { S0, S9, S9, S3, S3 }, MOUSE_BUTTON2DOWN, ~0, FALSE }, 458 /* S4 */ 459 { { S0, S2, S1, S3, S0 }, MOUSE_BUTTON1DOWN, ~0, TRUE }, 460 /* S5 */ 461 { { S0, S2, S5, S7, S5 }, MOUSE_BUTTON1DOWN, ~0, FALSE }, 462 /* S6 */ 463 { { S0, S6, S1, S7, S6 }, MOUSE_BUTTON3DOWN, ~0, FALSE }, 464 /* S7 */ 465 { { S0, S6, S5, S7, S7 }, MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, ~0, FALSE }, 466 /* S8 */ 467 { { S0, S2, S1, S3, S0 }, MOUSE_BUTTON3DOWN, ~0, TRUE }, 468 /* S9 */ 469 { { S0, S9, S9, S3, S9 }, 0, ~(MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN), FALSE }, 470}; 471static int mouse_button_state; 472static struct timeval mouse_button_state_tv; 473static int mouse_move_delayed; 474 475static jmp_buf env; 476 477/* function prototypes */ 478 479static void moused(void); 480static void hup(int sig); 481static void cleanup(int sig); 482static void usage(void); 483static void log_or_warn(int log_pri, int errnum, const char *fmt, ...) 484 __printflike(3, 4); 485 486static int r_identify(void); 487static char *r_if(int type); 488static char *r_name(int type); 489static char *r_model(int model); 490static void r_init(void); 491static int r_protocol(u_char b, mousestatus_t *act); 492static int r_statetrans(mousestatus_t *a1, mousestatus_t *a2, int trans); 493static int r_installmap(char *arg); 494static void r_map(mousestatus_t *act1, mousestatus_t *act2); 495static void r_timestamp(mousestatus_t *act); 496static int r_timeout(void); 497static void r_click(mousestatus_t *act); 498static void setmousespeed(int old, int new, unsigned cflag); 499 500static int pnpwakeup1(void); 501static int pnpwakeup2(void); 502static int pnpgets(char *buf); 503static int pnpparse(pnpid_t *id, char *buf, int len); 504static symtab_t *pnpproto(pnpid_t *id); 505 506static symtab_t *gettoken(symtab_t *tab, char *s, int len); 507static char *gettokenname(symtab_t *tab, int val); 508 509static void mremote_serversetup(); 510static void mremote_clientchg(int add); 511 512static int kidspad(u_char rxc, mousestatus_t *act); 513static int gtco_digipad(u_char, mousestatus_t *); 514 515static int usbmodule(void); 516 517int 518main(int argc, char *argv[]) 519{ 520 int c; 521 int i; 522 int j; 523 int retry; 524 525 for (i = 0; i < MOUSE_MAXBUTTON; ++i) 526 mstate[i] = &bstate[i]; 527 528 while ((c = getopt(argc, argv, "3C:DE:F:HI:PRS:VU:a:cdfhi:l:m:p:r:st:w:z:")) != -1) 529 switch(c) { 530 531 case '3': 532 rodent.flags |= Emulate3Button; 533 break; 534 535 case 'E': 536 rodent.button2timeout = atoi(optarg); 537 if ((rodent.button2timeout < 0) || 538 (rodent.button2timeout > MAX_BUTTON2TIMEOUT)) { 539 warnx("invalid argument `%s'", optarg); 540 usage(); 541 } 542 break; 543 544 case 'a': 545 i = sscanf(optarg, "%f,%f", &rodent.accelx, &rodent.accely); 546 if (i == 0) { 547 warnx("invalid acceleration argument '%s'", optarg); 548 usage(); 549 } 550 551 if (i == 1) 552 rodent.accely = rodent.accelx; 553 554 break; 555 556 case 'c': 557 rodent.flags |= ChordMiddle; 558 break; 559 560 case 'd': 561 ++debug; 562 break; 563 564 case 'f': 565 nodaemon = TRUE; 566 break; 567 568 case 'i': 569 if (strcmp(optarg, "all") == 0) 570 identify = ID_ALL; 571 else if (strcmp(optarg, "port") == 0) 572 identify = ID_PORT; 573 else if (strcmp(optarg, "if") == 0) 574 identify = ID_IF; 575 else if (strcmp(optarg, "type") == 0) 576 identify = ID_TYPE; 577 else if (strcmp(optarg, "model") == 0) 578 identify = ID_MODEL; 579 else { 580 warnx("invalid argument `%s'", optarg); 581 usage(); 582 } 583 nodaemon = TRUE; 584 break; 585 586 case 'l': 587 rodent.level = atoi(optarg); 588 if ((rodent.level < 0) || (rodent.level > 4)) { 589 warnx("invalid argument `%s'", optarg); 590 usage(); 591 } 592 break; 593 594 case 'm': 595 if (!r_installmap(optarg)) { 596 warnx("invalid argument `%s'", optarg); 597 usage(); 598 } 599 break; 600 601 case 'p': 602 rodent.portname = optarg; 603 break; 604 605 case 'r': 606 if (strcmp(optarg, "high") == 0) 607 rodent.resolution = MOUSE_RES_HIGH; 608 else if (strcmp(optarg, "medium-high") == 0) 609 rodent.resolution = MOUSE_RES_HIGH; 610 else if (strcmp(optarg, "medium-low") == 0) 611 rodent.resolution = MOUSE_RES_MEDIUMLOW; 612 else if (strcmp(optarg, "low") == 0) 613 rodent.resolution = MOUSE_RES_LOW; 614 else if (strcmp(optarg, "default") == 0) 615 rodent.resolution = MOUSE_RES_DEFAULT; 616 else { 617 rodent.resolution = atoi(optarg); 618 if (rodent.resolution <= 0) { 619 warnx("invalid argument `%s'", optarg); 620 usage(); 621 } 622 } 623 break; 624 625 case 's': 626 rodent.baudrate = 9600; 627 break; 628 629 case 'w': 630 i = atoi(optarg); 631 if ((i <= 0) || (i > MOUSE_MAXBUTTON)) { 632 warnx("invalid argument `%s'", optarg); 633 usage(); 634 } 635 rodent.wmode = 1 << (i - 1); 636 break; 637 638 case 'z': 639 if (strcmp(optarg, "x") == 0) 640 rodent.zmap[0] = MOUSE_XAXIS; 641 else if (strcmp(optarg, "y") == 0) 642 rodent.zmap[0] = MOUSE_YAXIS; 643 else { 644 i = atoi(optarg); 645 /* 646 * Use button i for negative Z axis movement and 647 * button (i + 1) for positive Z axis movement. 648 */ 649 if ((i <= 0) || (i > MOUSE_MAXBUTTON - 1)) { 650 warnx("invalid argument `%s'", optarg); 651 usage(); 652 } 653 rodent.zmap[0] = i; 654 rodent.zmap[1] = i + 1; 655 debug("optind: %d, optarg: '%s'", optind, optarg); 656 for (j = 1; j < 4; ++j) { 657 if ((optind >= argc) || !isdigit(*argv[optind])) 658 break; 659 i = atoi(argv[optind]); 660 if ((i <= 0) || (i > MOUSE_MAXBUTTON - 1)) { 661 warnx("invalid argument `%s'", argv[optind]); 662 usage(); 663 } 664 rodent.zmap[j] = i; 665 ++optind; 666 } 667 if ((rodent.zmap[2] != 0) && (rodent.zmap[3] == 0)) 668 rodent.zmap[3] = rodent.zmap[2] + 1; 669 } 670 break; 671 672 case 'C': 673 rodent.clickthreshold = atoi(optarg); 674 if ((rodent.clickthreshold < 0) || 675 (rodent.clickthreshold > MAX_CLICKTHRESHOLD)) { 676 warnx("invalid argument `%s'", optarg); 677 usage(); 678 } 679 break; 680 681 case 'D': 682 rodent.flags |= ClearDTR; 683 break; 684 685 case 'F': 686 rodent.rate = atoi(optarg); 687 if (rodent.rate <= 0) { 688 warnx("invalid argument `%s'", optarg); 689 usage(); 690 } 691 break; 692 693 case 'H': 694 rodent.flags |= HVirtualScroll; 695 break; 696 697 case 'I': 698 pidfile = optarg; 699 break; 700 701 case 'P': 702 rodent.flags |= NoPnP; 703 break; 704 705 case 'R': 706 rodent.flags |= ClearRTS; 707 break; 708 709 case 'S': 710 rodent.baudrate = atoi(optarg); 711 if (rodent.baudrate <= 0) { 712 warnx("invalid argument `%s'", optarg); 713 usage(); 714 } 715 debug("rodent baudrate %d", rodent.baudrate); 716 break; 717 718 case 't': 719 if (strcmp(optarg, "auto") == 0) { 720 rodent.rtype = MOUSE_PROTO_UNKNOWN; 721 rodent.flags &= ~NoPnP; 722 rodent.level = -1; 723 break; 724 } 725 for (i = 0; rnames[i]; i++) 726 if (strcmp(optarg, rnames[i]) == 0) { 727 rodent.rtype = i; 728 rodent.flags |= NoPnP; 729 rodent.level = (i == MOUSE_PROTO_SYSMOUSE) ? 1 : 0; 730 break; 731 } 732 if (rnames[i]) 733 break; 734 warnx("no such mouse type `%s'", optarg); 735 usage(); 736 737 case 'V': 738 rodent.flags |= VirtualScroll; 739 break; 740 case 'U': 741 rodent.scrollthreshold = atoi(optarg); 742 if (rodent.scrollthreshold < 0) { 743 warnx("invalid argument `%s'", optarg); 744 usage(); 745 } 746 break; 747 748 case 'h': 749 case '?': 750 default: 751 usage(); 752 } 753 754 /* fix Z axis mapping */ 755 for (i = 0; i < 4; ++i) { 756 if (rodent.zmap[i] > 0) { 757 for (j = 0; j < MOUSE_MAXBUTTON; ++j) { 758 if (mstate[j] == &bstate[rodent.zmap[i] - 1]) 759 mstate[j] = &zstate[i]; 760 } 761 rodent.zmap[i] = 1 << (rodent.zmap[i] - 1); 762 } 763 } 764 765 /* the default port name */ 766 switch(rodent.rtype) { 767 768 case MOUSE_PROTO_INPORT: 769 /* INPORT and BUS are the same... */ 770 rodent.rtype = MOUSE_PROTO_BUS; 771 /* FALLTHROUGH */ 772 case MOUSE_PROTO_BUS: 773 if (!rodent.portname) 774 rodent.portname = "/dev/mse0"; 775 break; 776 777 case MOUSE_PROTO_PS2: 778 if (!rodent.portname) 779 rodent.portname = "/dev/psm0"; 780 break; 781 782 default: 783 if (rodent.portname) 784 break; 785 warnx("no port name specified"); 786 usage(); 787 } 788 789 retry = 1; 790 if (strncmp(rodent.portname, "/dev/ums", 8) == 0) { 791 if (usbmodule() != 0) 792 retry = 5; 793 } 794 795 for (;;) { 796 if (setjmp(env) == 0) { 797 signal(SIGHUP, hup); 798 signal(SIGINT , cleanup); 799 signal(SIGQUIT, cleanup); 800 signal(SIGTERM, cleanup); 801 for (i = 0; i < retry; ++i) { 802 if (i > 0) 803 sleep(2); 804 rodent.mfd = open(rodent.portname, O_RDWR | O_NONBLOCK); 805 if (rodent.mfd != -1 || errno != ENOENT) 806 break; 807 } 808 if (rodent.mfd == -1) 809 logerr(1, "unable to open %s", rodent.portname); 810 if (r_identify() == MOUSE_PROTO_UNKNOWN) { 811 logwarnx("cannot determine mouse type on %s", rodent.portname); 812 close(rodent.mfd); 813 rodent.mfd = -1; 814 } 815 816 /* print some information */ 817 if (identify != ID_NONE) { 818 if (identify == ID_ALL) 819 printf("%s %s %s %s\n", 820 rodent.portname, r_if(rodent.hw.iftype), 821 r_name(rodent.rtype), r_model(rodent.hw.model)); 822 else if (identify & ID_PORT) 823 printf("%s\n", rodent.portname); 824 else if (identify & ID_IF) 825 printf("%s\n", r_if(rodent.hw.iftype)); 826 else if (identify & ID_TYPE) 827 printf("%s\n", r_name(rodent.rtype)); 828 else if (identify & ID_MODEL) 829 printf("%s\n", r_model(rodent.hw.model)); 830 exit(0); 831 } else { 832 debug("port: %s interface: %s type: %s model: %s", 833 rodent.portname, r_if(rodent.hw.iftype), 834 r_name(rodent.rtype), r_model(rodent.hw.model)); 835 } 836 837 if (rodent.mfd == -1) { 838 /* 839 * We cannot continue because of error. Exit if the 840 * program has not become a daemon. Otherwise, block 841 * until the the user corrects the problem and issues SIGHUP. 842 */ 843 if (!background) 844 exit(1); 845 sigpause(0); 846 } 847 848 r_init(); /* call init function */ 849 moused(); 850 } 851 852 if (rodent.mfd != -1) 853 close(rodent.mfd); 854 if (rodent.cfd != -1) 855 close(rodent.cfd); 856 rodent.mfd = rodent.cfd = -1; 857 } 858 /* NOT REACHED */ 859 860 exit(0); 861} 862 863static int 864usbmodule(void) 865{ 866 struct kld_file_stat fstat; 867 struct module_stat mstat; 868 int fileid, modid; 869 int loaded; 870 871 for (loaded = 0, fileid = kldnext(0); !loaded && fileid > 0; 872 fileid = kldnext(fileid)) { 873 fstat.version = sizeof(fstat); 874 if (kldstat(fileid, &fstat) < 0) 875 continue; 876 if (strncmp(fstat.name, "uhub/ums", 8) == 0) { 877 loaded = 1; 878 break; 879 } 880 for (modid = kldfirstmod(fileid); modid > 0; 881 modid = modfnext(modid)) { 882 mstat.version = sizeof(mstat); 883 if (modstat(modid, &mstat) < 0) 884 continue; 885 if (strncmp(mstat.name, "uhub/ums", 8) == 0) { 886 loaded = 1; 887 break; 888 } 889 } 890 } 891 if (!loaded) { 892 if (kldload("ums") != -1) 893 return 1; 894 if (errno != EEXIST) 895 logerr(1, "unable to load USB mouse driver"); 896 } 897 return 0; 898} 899 900static void 901moused(void) 902{ 903 struct mouse_info mouse; 904 mousestatus_t action0; /* original mouse action */ 905 mousestatus_t action; /* interrim buffer */ 906 mousestatus_t action2; /* mapped action */ 907 struct timeval timeout; 908 fd_set fds; 909 u_char b; 910 FILE *fp; 911 int flags; 912 int c; 913 int i; 914 915 if ((rodent.cfd = open("/dev/consolectl", O_RDWR, 0)) == -1) 916 logerr(1, "cannot open /dev/consolectl"); 917 918 if (!nodaemon && !background) { 919 if (daemon(0, 0)) { 920 logerr(1, "failed to become a daemon"); 921 } else { 922 background = TRUE; 923 fp = fopen(pidfile, "w"); 924 if (fp != NULL) { 925 fprintf(fp, "%d\n", getpid()); 926 fclose(fp); 927 } 928 } 929 } 930 931 /* clear mouse data */ 932 bzero(&action0, sizeof(action0)); 933 bzero(&action, sizeof(action)); 934 bzero(&action2, sizeof(action2)); 935 bzero(&mouse, sizeof(mouse)); 936 mouse_button_state = S0; 937 gettimeofday(&mouse_button_state_tv, NULL); 938 mouse_move_delayed = 0; 939 for (i = 0; i < MOUSE_MAXBUTTON; ++i) { 940 bstate[i].count = 0; 941 bstate[i].tv = mouse_button_state_tv; 942 } 943 for (i = 0; i < sizeof(zstate)/sizeof(zstate[0]); ++i) { 944 zstate[i].count = 0; 945 zstate[i].tv = mouse_button_state_tv; 946 } 947 948 /* choose which ioctl command to use */ 949 mouse.operation = MOUSE_MOTION_EVENT; 950 extioctl = (ioctl(rodent.cfd, CONS_MOUSECTL, &mouse) == 0); 951 952 /* process mouse data */ 953 timeout.tv_sec = 0; 954 timeout.tv_usec = 20000; /* 20 msec */ 955 for (;;) { 956 957 FD_ZERO(&fds); 958 FD_SET(rodent.mfd, &fds); 959 if (rodent.mremsfd >= 0) 960 FD_SET(rodent.mremsfd, &fds); 961 if (rodent.mremcfd >= 0) 962 FD_SET(rodent.mremcfd, &fds); 963 964 c = select(FD_SETSIZE, &fds, NULL, NULL, 965 (rodent.flags & Emulate3Button) ? &timeout : NULL); 966 if (c < 0) { /* error */ 967 logwarn("failed to read from mouse"); 968 continue; 969 } else if (c == 0) { /* timeout */ 970 /* assert(rodent.flags & Emulate3Button) */ 971 action0.button = action0.obutton; 972 action0.dx = action0.dy = action0.dz = 0; 973 action0.flags = flags = 0; 974 if (r_timeout() && r_statetrans(&action0, &action, A_TIMEOUT)) { 975 if (debug > 2) 976 debug("flags:%08x buttons:%08x obuttons:%08x", 977 action.flags, action.button, action.obutton); 978 } else { 979 action0.obutton = action0.button; 980 continue; 981 } 982 } else { 983 /* MouseRemote client connect/disconnect */ 984 if ((rodent.mremsfd >= 0) && FD_ISSET(rodent.mremsfd, &fds)) { 985 mremote_clientchg(TRUE); 986 continue; 987 } 988 if ((rodent.mremcfd >= 0) && FD_ISSET(rodent.mremcfd, &fds)) { 989 mremote_clientchg(FALSE); 990 continue; 991 } 992 /* mouse movement */ 993 if (read(rodent.mfd, &b, 1) == -1) { 994 if (errno == EWOULDBLOCK) 995 continue; 996 else 997 return; 998 } 999 if ((flags = r_protocol(b, &action0)) == 0) 1000 continue; 1001 1002 if ((rodent.flags & VirtualScroll) || (rodent.flags & HVirtualScroll)) { 1003 /* Allow middle button drags to scroll up and down */ 1004 if (action0.button == MOUSE_BUTTON2DOWN) { 1005 if (scroll_state == SCROLL_NOTSCROLLING) { 1006 scroll_state = SCROLL_PREPARE; 1007 debug("PREPARING TO SCROLL"); 1008 } 1009 debug("[BUTTON2] flags:%08x buttons:%08x obuttons:%08x", 1010 action.flags, action.button, action.obutton); 1011 } else { 1012 debug("[NOTBUTTON2] flags:%08x buttons:%08x obuttons:%08x", 1013 action.flags, action.button, action.obutton); 1014 1015 /* This isn't a middle button down... move along... */ 1016 if (scroll_state == SCROLL_SCROLLING) { 1017 /* 1018 * We were scrolling, someone let go of button 2. 1019 * Now turn autoscroll off. 1020 */ 1021 scroll_state = SCROLL_NOTSCROLLING; 1022 debug("DONE WITH SCROLLING / %d", scroll_state); 1023 } else if (scroll_state == SCROLL_PREPARE) { 1024 mousestatus_t newaction = action0; 1025 1026 /* We were preparing to scroll, but we never moved... */ 1027 r_timestamp(&action0); 1028 r_statetrans(&action0, &newaction, 1029 A(newaction.button & MOUSE_BUTTON1DOWN, 1030 action0.button & MOUSE_BUTTON3DOWN)); 1031 1032 /* Send middle down */ 1033 newaction.button = MOUSE_BUTTON2DOWN; 1034 r_click(&newaction); 1035 1036 /* Send middle up */ 1037 r_timestamp(&newaction); 1038 newaction.obutton = newaction.button; 1039 newaction.button = action0.button; 1040 r_click(&newaction); 1041 } 1042 } 1043 } 1044 1045 r_timestamp(&action0); 1046 r_statetrans(&action0, &action, 1047 A(action0.button & MOUSE_BUTTON1DOWN, 1048 action0.button & MOUSE_BUTTON3DOWN)); 1049 debug("flags:%08x buttons:%08x obuttons:%08x", action.flags, 1050 action.button, action.obutton); 1051 } 1052 action0.obutton = action0.button; 1053 flags &= MOUSE_POSCHANGED; 1054 flags |= action.obutton ^ action.button; 1055 action.flags = flags; 1056 1057 if (flags) { /* handler detected action */ 1058 r_map(&action, &action2); 1059 debug("activity : buttons 0x%08x dx %d dy %d dz %d", 1060 action2.button, action2.dx, action2.dy, action2.dz); 1061 1062 if ((rodent.flags & VirtualScroll) || (rodent.flags & HVirtualScroll)) { 1063 /* 1064 * If *only* the middle button is pressed AND we are moving 1065 * the stick/trackpoint/nipple, scroll! 1066 */ 1067 if (scroll_state == SCROLL_PREPARE) { 1068 /* Ok, Set we're really scrolling now.... */ 1069 if (action2.dy || action2.dx) 1070 scroll_state = SCROLL_SCROLLING; 1071 } 1072 if (scroll_state == SCROLL_SCROLLING) { 1073 if (rodent.flags & VirtualScroll) { 1074 scroll_movement += action2.dy; 1075 debug("SCROLL: %d", scroll_movement); 1076 1077 if (scroll_movement < -rodent.scrollthreshold) { 1078 /* Scroll down */ 1079 action2.dz = -1; 1080 scroll_movement = 0; 1081 } 1082 else if (scroll_movement > rodent.scrollthreshold) { 1083 /* Scroll up */ 1084 action2.dz = 1; 1085 scroll_movement = 0; 1086 } 1087 } 1088 if (rodent.flags & HVirtualScroll) { 1089 hscroll_movement += action2.dx; 1090 debug("HORIZONTAL SCROLL: %d", hscroll_movement); 1091 1092 if (hscroll_movement < -rodent.scrollthreshold) { 1093 action2.dz = -2; 1094 hscroll_movement = 0; 1095 } 1096 else if (hscroll_movement > rodent.scrollthreshold) { 1097 action2.dz = 2; 1098 hscroll_movement = 0; 1099 } 1100 } 1101 1102 /* Don't move while scrolling */ 1103 action2.dx = action2.dy = 0; 1104 } 1105 } 1106 1107 if (extioctl) { 1108 /* Defer clicks until we aren't VirtualScroll'ing. */ 1109 if (scroll_state == SCROLL_NOTSCROLLING) 1110 r_click(&action2); 1111 1112 if (action2.flags & MOUSE_POSCHANGED) { 1113 mouse.operation = MOUSE_MOTION_EVENT; 1114 mouse.u.data.buttons = action2.button; 1115 mouse.u.data.x = action2.dx * rodent.accelx; 1116 mouse.u.data.y = action2.dy * rodent.accely; 1117 mouse.u.data.z = action2.dz; 1118 if (debug < 2) 1119 ioctl(rodent.cfd, CONS_MOUSECTL, &mouse); 1120 } 1121 } else { 1122 mouse.operation = MOUSE_ACTION; 1123 mouse.u.data.buttons = action2.button; 1124 mouse.u.data.x = action2.dx * rodent.accelx; 1125 mouse.u.data.y = action2.dy * rodent.accely; 1126 mouse.u.data.z = action2.dz; 1127 if (debug < 2) 1128 ioctl(rodent.cfd, CONS_MOUSECTL, &mouse); 1129 } 1130 1131 /* 1132 * If the Z axis movement is mapped to an imaginary physical 1133 * button, we need to cook up a corresponding button `up' event 1134 * after sending a button `down' event. 1135 */ 1136 if ((rodent.zmap[0] > 0) && (action.dz != 0)) { 1137 action.obutton = action.button; 1138 action.dx = action.dy = action.dz = 0; 1139 r_map(&action, &action2); 1140 debug("activity : buttons 0x%08x dx %d dy %d dz %d", 1141 action2.button, action2.dx, action2.dy, action2.dz); 1142 1143 if (extioctl) { 1144 r_click(&action2); 1145 } else { 1146 mouse.operation = MOUSE_ACTION; 1147 mouse.u.data.buttons = action2.button; 1148 mouse.u.data.x = mouse.u.data.y = mouse.u.data.z = 0; 1149 if (debug < 2) 1150 ioctl(rodent.cfd, CONS_MOUSECTL, &mouse); 1151 } 1152 } 1153 } 1154 } 1155 /* NOT REACHED */ 1156} 1157 1158static void 1159hup(int sig) 1160{ 1161 longjmp(env, 1); 1162} 1163 1164static void 1165cleanup(int sig) 1166{ 1167 if (rodent.rtype == MOUSE_PROTO_X10MOUSEREM) 1168 unlink(_PATH_MOUSEREMOTE); 1169 exit(0); 1170} 1171 1172/** 1173 ** usage 1174 ** 1175 ** Complain, and free the CPU for more worthy tasks 1176 **/ 1177static void 1178usage(void) 1179{ 1180 fprintf(stderr, "%s\n%s\n%s\n%s\n", 1181 "usage: moused [-DRcdfs] [-I file] [-F rate] [-r resolution] [-S baudrate]", 1182 " [-VH [-U threshold]] [-a X [,Y]] [-C threshold] [-m N=M] [-w N]", 1183 " [-z N] [-t <mousetype>] [-l level] [-3 [-E timeout]] -p <port>", 1184 " moused [-d] -i <port|if|type|model|all> -p <port>"); 1185 exit(1); 1186} 1187 1188/* 1189 * Output an error message to syslog or stderr as appropriate. If 1190 * `errnum' is non-zero, append its string form to the message. 1191 */ 1192static void 1193log_or_warn(int log_pri, int errnum, const char *fmt, ...) 1194{ 1195 va_list ap; 1196 char buf[256]; 1197 1198 va_start(ap, fmt); 1199 vsnprintf(buf, sizeof(buf), fmt, ap); 1200 va_end(ap); 1201 if (errnum) { 1202 strlcat(buf, ": ", sizeof(buf)); 1203 strlcat(buf, strerror(errnum), sizeof(buf)); 1204 } 1205 1206 if (background) 1207 syslog(log_pri, "%s", buf); 1208 else 1209 warnx("%s", buf); 1210} 1211 1212/** 1213 ** Mouse interface code, courtesy of XFree86 3.1.2. 1214 ** 1215 ** Note: Various bits have been trimmed, and in my shortsighted enthusiasm 1216 ** to clean, reformat and rationalise naming, it's quite possible that 1217 ** some things in here have been broken. 1218 ** 1219 ** I hope not 8) 1220 ** 1221 ** The following code is derived from a module marked : 1222 **/ 1223 1224/* $XConsortium: xf86_Mouse.c,v 1.2 94/10/12 20:33:21 kaleb Exp $ */ 1225/* $XFree86: xc/programs/Xserver/hw/xfree86/common/xf86_Mouse.c,v 3.2 1995/01/28 1226 17:03:40 dawes Exp $ */ 1227/* 1228 * 1229 * Copyright 1990,91 by Thomas Roell, Dinkelscherben, Germany. 1230 * Copyright 1993 by David Dawes <dawes@physics.su.oz.au> 1231 * 1232 * Permission to use, copy, modify, distribute, and sell this software and its 1233 * documentation for any purpose is hereby granted without fee, provided that 1234 * the above copyright notice appear in all copies and that both that 1235 * copyright notice and this permission notice appear in supporting 1236 * documentation, and that the names of Thomas Roell and David Dawes not be 1237 * used in advertising or publicity pertaining to distribution of the 1238 * software without specific, written prior permission. Thomas Roell 1239 * and David Dawes makes no representations about the suitability of this 1240 * software for any purpose. It is provided "as is" without express or 1241 * implied warranty. 1242 * 1243 * THOMAS ROELL AND DAVID DAWES DISCLAIM ALL WARRANTIES WITH REGARD TO THIS 1244 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND 1245 * FITNESS, IN NO EVENT SHALL THOMAS ROELL OR DAVID DAWES BE LIABLE FOR ANY 1246 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER 1247 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF 1248 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 1249 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 1250 * 1251 */ 1252 1253/** 1254 ** GlidePoint support from XFree86 3.2. 1255 ** Derived from the module: 1256 **/ 1257 1258/* $XFree86: xc/programs/Xserver/hw/xfree86/common/xf86_Mouse.c,v 3.19 1996/10/16 14:40:51 dawes Exp $ */ 1259/* $XConsortium: xf86_Mouse.c /main/10 1996/01/30 15:16:12 kaleb $ */ 1260 1261/* the following table must be ordered by MOUSE_PROTO_XXX in mouse.h */ 1262static unsigned char proto[][7] = { 1263 /* hd_mask hd_id dp_mask dp_id bytes b4_mask b4_id */ 1264 { 0x40, 0x40, 0x40, 0x00, 3, ~0x23, 0x00 }, /* MicroSoft */ 1265 { 0xf8, 0x80, 0x00, 0x00, 5, 0x00, 0xff }, /* MouseSystems */ 1266 { 0xe0, 0x80, 0x80, 0x00, 3, 0x00, 0xff }, /* Logitech */ 1267 { 0xe0, 0x80, 0x80, 0x00, 3, 0x00, 0xff }, /* MMSeries */ 1268 { 0x40, 0x40, 0x40, 0x00, 3, ~0x33, 0x00 }, /* MouseMan */ 1269 { 0xf8, 0x80, 0x00, 0x00, 5, 0x00, 0xff }, /* Bus */ 1270 { 0xf8, 0x80, 0x00, 0x00, 5, 0x00, 0xff }, /* InPort */ 1271 { 0xc0, 0x00, 0x00, 0x00, 3, 0x00, 0xff }, /* PS/2 mouse */ 1272 { 0xe0, 0x80, 0x80, 0x00, 3, 0x00, 0xff }, /* MM HitTablet */ 1273 { 0x40, 0x40, 0x40, 0x00, 3, ~0x33, 0x00 }, /* GlidePoint */ 1274 { 0x40, 0x40, 0x40, 0x00, 3, ~0x3f, 0x00 }, /* IntelliMouse */ 1275 { 0x40, 0x40, 0x40, 0x00, 3, ~0x33, 0x00 }, /* ThinkingMouse */ 1276 { 0xf8, 0x80, 0x00, 0x00, 5, 0x00, 0xff }, /* sysmouse */ 1277 { 0x40, 0x40, 0x40, 0x00, 3, ~0x23, 0x00 }, /* X10 MouseRem */ 1278 { 0x80, 0x80, 0x00, 0x00, 5, 0x00, 0xff }, /* KIDSPAD */ 1279 { 0xc3, 0xc0, 0x00, 0x00, 6, 0x00, 0xff }, /* VersaPad */ 1280 { 0x00, 0x00, 0x00, 0x00, 1, 0x00, 0xff }, /* JogDial */ 1281#if notyet 1282 { 0xf8, 0x80, 0x00, 0x00, 5, ~0x2f, 0x10 }, /* Mariqua */ 1283#endif 1284}; 1285static unsigned char cur_proto[7]; 1286 1287static int 1288r_identify(void) 1289{ 1290 char pnpbuf[256]; /* PnP identifier string may be up to 256 bytes long */ 1291 pnpid_t pnpid; 1292 symtab_t *t; 1293 int level; 1294 int len; 1295 1296 /* set the driver operation level, if applicable */ 1297 if (rodent.level < 0) 1298 rodent.level = 1; 1299 ioctl(rodent.mfd, MOUSE_SETLEVEL, &rodent.level); 1300 rodent.level = (ioctl(rodent.mfd, MOUSE_GETLEVEL, &level) == 0) ? level : 0; 1301 1302 /* 1303 * Interrogate the driver and get some intelligence on the device... 1304 * The following ioctl functions are not always supported by device 1305 * drivers. When the driver doesn't support them, we just trust the 1306 * user to supply valid information. 1307 */ 1308 rodent.hw.iftype = MOUSE_IF_UNKNOWN; 1309 rodent.hw.model = MOUSE_MODEL_GENERIC; 1310 ioctl(rodent.mfd, MOUSE_GETHWINFO, &rodent.hw); 1311 1312 if (rodent.rtype != MOUSE_PROTO_UNKNOWN) 1313 bcopy(proto[rodent.rtype], cur_proto, sizeof(cur_proto)); 1314 rodent.mode.protocol = MOUSE_PROTO_UNKNOWN; 1315 rodent.mode.rate = -1; 1316 rodent.mode.resolution = MOUSE_RES_UNKNOWN; 1317 rodent.mode.accelfactor = 0; 1318 rodent.mode.level = 0; 1319 if (ioctl(rodent.mfd, MOUSE_GETMODE, &rodent.mode) == 0) { 1320 if ((rodent.mode.protocol == MOUSE_PROTO_UNKNOWN) 1321 || (rodent.mode.protocol >= sizeof(proto)/sizeof(proto[0]))) { 1322 logwarnx("unknown mouse protocol (%d)", rodent.mode.protocol); 1323 return MOUSE_PROTO_UNKNOWN; 1324 } else { 1325 /* INPORT and BUS are the same... */ 1326 if (rodent.mode.protocol == MOUSE_PROTO_INPORT) 1327 rodent.mode.protocol = MOUSE_PROTO_BUS; 1328 if (rodent.mode.protocol != rodent.rtype) { 1329 /* Hmm, the driver doesn't agree with the user... */ 1330 if (rodent.rtype != MOUSE_PROTO_UNKNOWN) 1331 logwarnx("mouse type mismatch (%s != %s), %s is assumed", 1332 r_name(rodent.mode.protocol), r_name(rodent.rtype), 1333 r_name(rodent.mode.protocol)); 1334 rodent.rtype = rodent.mode.protocol; 1335 bcopy(proto[rodent.rtype], cur_proto, sizeof(cur_proto)); 1336 } 1337 } 1338 cur_proto[4] = rodent.mode.packetsize; 1339 cur_proto[0] = rodent.mode.syncmask[0]; /* header byte bit mask */ 1340 cur_proto[1] = rodent.mode.syncmask[1]; /* header bit pattern */ 1341 } 1342 1343 /* maybe this is a PnP mouse... */ 1344 if (rodent.mode.protocol == MOUSE_PROTO_UNKNOWN) { 1345 1346 if (rodent.flags & NoPnP) 1347 return rodent.rtype; 1348 if (((len = pnpgets(pnpbuf)) <= 0) || !pnpparse(&pnpid, pnpbuf, len)) 1349 return rodent.rtype; 1350 1351 debug("PnP serial mouse: '%*.*s' '%*.*s' '%*.*s'", 1352 pnpid.neisaid, pnpid.neisaid, pnpid.eisaid, 1353 pnpid.ncompat, pnpid.ncompat, pnpid.compat, 1354 pnpid.ndescription, pnpid.ndescription, pnpid.description); 1355 1356 /* we have a valid PnP serial device ID */ 1357 rodent.hw.iftype = MOUSE_IF_SERIAL; 1358 t = pnpproto(&pnpid); 1359 if (t != NULL) { 1360 rodent.mode.protocol = t->val; 1361 rodent.hw.model = t->val2; 1362 } else { 1363 rodent.mode.protocol = MOUSE_PROTO_UNKNOWN; 1364 } 1365 if (rodent.mode.protocol == MOUSE_PROTO_INPORT) 1366 rodent.mode.protocol = MOUSE_PROTO_BUS; 1367 1368 /* make final adjustment */ 1369 if (rodent.mode.protocol != MOUSE_PROTO_UNKNOWN) { 1370 if (rodent.mode.protocol != rodent.rtype) { 1371 /* Hmm, the device doesn't agree with the user... */ 1372 if (rodent.rtype != MOUSE_PROTO_UNKNOWN) 1373 logwarnx("mouse type mismatch (%s != %s), %s is assumed", 1374 r_name(rodent.mode.protocol), r_name(rodent.rtype), 1375 r_name(rodent.mode.protocol)); 1376 rodent.rtype = rodent.mode.protocol; 1377 bcopy(proto[rodent.rtype], cur_proto, sizeof(cur_proto)); 1378 } 1379 } 1380 } 1381 1382 debug("proto params: %02x %02x %02x %02x %d %02x %02x", 1383 cur_proto[0], cur_proto[1], cur_proto[2], cur_proto[3], 1384 cur_proto[4], cur_proto[5], cur_proto[6]); 1385 1386 return rodent.rtype; 1387} 1388 1389static char * 1390r_if(int iftype) 1391{ 1392 char *s; 1393 1394 s = gettokenname(rifs, iftype); 1395 return (s == NULL) ? "unknown" : s; 1396} 1397 1398static char * 1399r_name(int type) 1400{ 1401 return ((type == MOUSE_PROTO_UNKNOWN) 1402 || (type > sizeof(rnames)/sizeof(rnames[0]) - 1)) 1403 ? "unknown" : rnames[type]; 1404} 1405 1406static char * 1407r_model(int model) 1408{ 1409 char *s; 1410 1411 s = gettokenname(rmodels, model); 1412 return (s == NULL) ? "unknown" : s; 1413} 1414 1415static void 1416r_init(void) 1417{ 1418 unsigned char buf[16]; /* scrach buffer */ 1419 fd_set fds; 1420 char *s; 1421 char c; 1422 int i; 1423 1424 /** 1425 ** This comment is a little out of context here, but it contains 1426 ** some useful information... 1427 ******************************************************************** 1428 ** 1429 ** The following lines take care of the Logitech MouseMan protocols. 1430 ** 1431 ** NOTE: There are different versions of both MouseMan and TrackMan! 1432 ** Hence I add another protocol P_LOGIMAN, which the user can 1433 ** specify as MouseMan in his XF86Config file. This entry was 1434 ** formerly handled as a special case of P_MS. However, people 1435 ** who don't have the middle button problem, can still specify 1436 ** Microsoft and use P_MS. 1437 ** 1438 ** By default, these mice should use a 3 byte Microsoft protocol 1439 ** plus a 4th byte for the middle button. However, the mouse might 1440 ** have switched to a different protocol before we use it, so I send 1441 ** the proper sequence just in case. 1442 ** 1443 ** NOTE: - all commands to (at least the European) MouseMan have to 1444 ** be sent at 1200 Baud. 1445 ** - each command starts with a '*'. 1446 ** - whenever the MouseMan receives a '*', it will switch back 1447 ** to 1200 Baud. Hence I have to select the desired protocol 1448 ** first, then select the baud rate. 1449 ** 1450 ** The protocols supported by the (European) MouseMan are: 1451 ** - 5 byte packed binary protocol, as with the Mouse Systems 1452 ** mouse. Selected by sequence "*U". 1453 ** - 2 button 3 byte MicroSoft compatible protocol. Selected 1454 ** by sequence "*V". 1455 ** - 3 button 3+1 byte MicroSoft compatible protocol (default). 1456 ** Selected by sequence "*X". 1457 ** 1458 ** The following baud rates are supported: 1459 ** - 1200 Baud (default). Selected by sequence "*n". 1460 ** - 9600 Baud. Selected by sequence "*q". 1461 ** 1462 ** Selecting a sample rate is no longer supported with the MouseMan! 1463 ** Some additional lines in xf86Config.c take care of ill configured 1464 ** baud rates and sample rates. (The user will get an error.) 1465 */ 1466 1467 switch (rodent.rtype) { 1468 1469 case MOUSE_PROTO_LOGI: 1470 /* 1471 * The baud rate selection command must be sent at the current 1472 * baud rate; try all likely settings 1473 */ 1474 setmousespeed(9600, rodent.baudrate, rodentcflags[rodent.rtype]); 1475 setmousespeed(4800, rodent.baudrate, rodentcflags[rodent.rtype]); 1476 setmousespeed(2400, rodent.baudrate, rodentcflags[rodent.rtype]); 1477 setmousespeed(1200, rodent.baudrate, rodentcflags[rodent.rtype]); 1478 /* select MM series data format */ 1479 write(rodent.mfd, "S", 1); 1480 setmousespeed(rodent.baudrate, rodent.baudrate, 1481 rodentcflags[MOUSE_PROTO_MM]); 1482 /* select report rate/frequency */ 1483 if (rodent.rate <= 0) write(rodent.mfd, "O", 1); 1484 else if (rodent.rate <= 15) write(rodent.mfd, "J", 1); 1485 else if (rodent.rate <= 27) write(rodent.mfd, "K", 1); 1486 else if (rodent.rate <= 42) write(rodent.mfd, "L", 1); 1487 else if (rodent.rate <= 60) write(rodent.mfd, "R", 1); 1488 else if (rodent.rate <= 85) write(rodent.mfd, "M", 1); 1489 else if (rodent.rate <= 125) write(rodent.mfd, "Q", 1); 1490 else write(rodent.mfd, "N", 1); 1491 break; 1492 1493 case MOUSE_PROTO_LOGIMOUSEMAN: 1494 /* The command must always be sent at 1200 baud */ 1495 setmousespeed(1200, 1200, rodentcflags[rodent.rtype]); 1496 write(rodent.mfd, "*X", 2); 1497 setmousespeed(1200, rodent.baudrate, rodentcflags[rodent.rtype]); 1498 break; 1499 1500 case MOUSE_PROTO_HITTAB: 1501 setmousespeed(1200, rodent.baudrate, rodentcflags[rodent.rtype]); 1502 1503 /* 1504 * Initialize Hitachi PUMA Plus - Model 1212E to desired settings. 1505 * The tablet must be configured to be in MM mode, NO parity, 1506 * Binary Format. xf86Info.sampleRate controls the sensativity 1507 * of the tablet. We only use this tablet for it's 4-button puck 1508 * so we don't run in "Absolute Mode" 1509 */ 1510 write(rodent.mfd, "z8", 2); /* Set Parity = "NONE" */ 1511 usleep(50000); 1512 write(rodent.mfd, "zb", 2); /* Set Format = "Binary" */ 1513 usleep(50000); 1514 write(rodent.mfd, "@", 1); /* Set Report Mode = "Stream" */ 1515 usleep(50000); 1516 write(rodent.mfd, "R", 1); /* Set Output Rate = "45 rps" */ 1517 usleep(50000); 1518 write(rodent.mfd, "I\x20", 2); /* Set Incrememtal Mode "20" */ 1519 usleep(50000); 1520 write(rodent.mfd, "E", 1); /* Set Data Type = "Relative */ 1521 usleep(50000); 1522 1523 /* Resolution is in 'lines per inch' on the Hitachi tablet */ 1524 if (rodent.resolution == MOUSE_RES_LOW) c = 'g'; 1525 else if (rodent.resolution == MOUSE_RES_MEDIUMLOW) c = 'e'; 1526 else if (rodent.resolution == MOUSE_RES_MEDIUMHIGH) c = 'h'; 1527 else if (rodent.resolution == MOUSE_RES_HIGH) c = 'd'; 1528 else if (rodent.resolution <= 40) c = 'g'; 1529 else if (rodent.resolution <= 100) c = 'd'; 1530 else if (rodent.resolution <= 200) c = 'e'; 1531 else if (rodent.resolution <= 500) c = 'h'; 1532 else if (rodent.resolution <= 1000) c = 'j'; 1533 else c = 'd'; 1534 write(rodent.mfd, &c, 1); 1535 usleep(50000); 1536 1537 write(rodent.mfd, "\021", 1); /* Resume DATA output */ 1538 break; 1539 1540 case MOUSE_PROTO_THINK: 1541 setmousespeed(1200, rodent.baudrate, rodentcflags[rodent.rtype]); 1542 /* the PnP ID string may be sent again, discard it */ 1543 usleep(200000); 1544 i = FREAD; 1545 ioctl(rodent.mfd, TIOCFLUSH, &i); 1546 /* send the command to initialize the beast */ 1547 for (s = "E5E5"; *s; ++s) { 1548 write(rodent.mfd, s, 1); 1549 FD_ZERO(&fds); 1550 FD_SET(rodent.mfd, &fds); 1551 if (select(FD_SETSIZE, &fds, NULL, NULL, NULL) <= 0) 1552 break; 1553 read(rodent.mfd, &c, 1); 1554 debug("%c", c); 1555 if (c != *s) 1556 break; 1557 } 1558 break; 1559 1560 case MOUSE_PROTO_JOGDIAL: 1561 break; 1562 case MOUSE_PROTO_MSC: 1563 setmousespeed(1200, rodent.baudrate, rodentcflags[rodent.rtype]); 1564 if (rodent.flags & ClearDTR) { 1565 i = TIOCM_DTR; 1566 ioctl(rodent.mfd, TIOCMBIC, &i); 1567 } 1568 if (rodent.flags & ClearRTS) { 1569 i = TIOCM_RTS; 1570 ioctl(rodent.mfd, TIOCMBIC, &i); 1571 } 1572 break; 1573 1574 case MOUSE_PROTO_SYSMOUSE: 1575 if (rodent.hw.iftype == MOUSE_IF_SYSMOUSE) 1576 setmousespeed(1200, rodent.baudrate, rodentcflags[rodent.rtype]); 1577 /* FALLTHROUGH */ 1578 1579 case MOUSE_PROTO_BUS: 1580 case MOUSE_PROTO_INPORT: 1581 case MOUSE_PROTO_PS2: 1582 if (rodent.rate >= 0) 1583 rodent.mode.rate = rodent.rate; 1584 if (rodent.resolution != MOUSE_RES_UNKNOWN) 1585 rodent.mode.resolution = rodent.resolution; 1586 ioctl(rodent.mfd, MOUSE_SETMODE, &rodent.mode); 1587 break; 1588 1589 case MOUSE_PROTO_X10MOUSEREM: 1590 mremote_serversetup(); 1591 setmousespeed(1200, rodent.baudrate, rodentcflags[rodent.rtype]); 1592 break; 1593 1594 1595 case MOUSE_PROTO_VERSAPAD: 1596 tcsendbreak(rodent.mfd, 0); /* send break for 400 msec */ 1597 i = FREAD; 1598 ioctl(rodent.mfd, TIOCFLUSH, &i); 1599 for (i = 0; i < 7; ++i) { 1600 FD_ZERO(&fds); 1601 FD_SET(rodent.mfd, &fds); 1602 if (select(FD_SETSIZE, &fds, NULL, NULL, NULL) <= 0) 1603 break; 1604 read(rodent.mfd, &c, 1); 1605 buf[i] = c; 1606 } 1607 debug("%s\n", buf); 1608 if ((buf[0] != 'V') || (buf[1] != 'P')|| (buf[7] != '\r')) 1609 break; 1610 setmousespeed(9600, rodent.baudrate, rodentcflags[rodent.rtype]); 1611 tcsendbreak(rodent.mfd, 0); /* send break for 400 msec again */ 1612 for (i = 0; i < 7; ++i) { 1613 FD_ZERO(&fds); 1614 FD_SET(rodent.mfd, &fds); 1615 if (select(FD_SETSIZE, &fds, NULL, NULL, NULL) <= 0) 1616 break; 1617 read(rodent.mfd, &c, 1); 1618 debug("%c", c); 1619 if (c != buf[i]) 1620 break; 1621 } 1622 i = FREAD; 1623 ioctl(rodent.mfd, TIOCFLUSH, &i); 1624 break; 1625 1626 default: 1627 setmousespeed(1200, rodent.baudrate, rodentcflags[rodent.rtype]); 1628 break; 1629 } 1630} 1631 1632static int 1633r_protocol(u_char rBuf, mousestatus_t *act) 1634{ 1635 /* MOUSE_MSS_BUTTON?DOWN -> MOUSE_BUTTON?DOWN */ 1636 static int butmapmss[4] = { /* Microsoft, MouseMan, GlidePoint, 1637 IntelliMouse, Thinking Mouse */ 1638 0, 1639 MOUSE_BUTTON3DOWN, 1640 MOUSE_BUTTON1DOWN, 1641 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, 1642 }; 1643 static int butmapmss2[4] = { /* Microsoft, MouseMan, GlidePoint, 1644 Thinking Mouse */ 1645 0, 1646 MOUSE_BUTTON4DOWN, 1647 MOUSE_BUTTON2DOWN, 1648 MOUSE_BUTTON2DOWN | MOUSE_BUTTON4DOWN, 1649 }; 1650 /* MOUSE_INTELLI_BUTTON?DOWN -> MOUSE_BUTTON?DOWN */ 1651 static int butmapintelli[4] = { /* IntelliMouse, NetMouse, Mie Mouse, 1652 MouseMan+ */ 1653 0, 1654 MOUSE_BUTTON2DOWN, 1655 MOUSE_BUTTON4DOWN, 1656 MOUSE_BUTTON2DOWN | MOUSE_BUTTON4DOWN, 1657 }; 1658 /* MOUSE_MSC_BUTTON?UP -> MOUSE_BUTTON?DOWN */ 1659 static int butmapmsc[8] = { /* MouseSystems, MMSeries, Logitech, 1660 Bus, sysmouse */ 1661 0, 1662 MOUSE_BUTTON3DOWN, 1663 MOUSE_BUTTON2DOWN, 1664 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN, 1665 MOUSE_BUTTON1DOWN, 1666 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, 1667 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN, 1668 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN 1669 }; 1670 /* MOUSE_PS2_BUTTON?DOWN -> MOUSE_BUTTON?DOWN */ 1671 static int butmapps2[8] = { /* PS/2 */ 1672 0, 1673 MOUSE_BUTTON1DOWN, 1674 MOUSE_BUTTON3DOWN, 1675 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, 1676 MOUSE_BUTTON2DOWN, 1677 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN, 1678 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN, 1679 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN 1680 }; 1681 /* for Hitachi tablet */ 1682 static int butmaphit[8] = { /* MM HitTablet */ 1683 0, 1684 MOUSE_BUTTON3DOWN, 1685 MOUSE_BUTTON2DOWN, 1686 MOUSE_BUTTON1DOWN, 1687 MOUSE_BUTTON4DOWN, 1688 MOUSE_BUTTON5DOWN, 1689 MOUSE_BUTTON6DOWN, 1690 MOUSE_BUTTON7DOWN, 1691 }; 1692 /* for serial VersaPad */ 1693 static int butmapversa[8] = { /* VersaPad */ 1694 0, 1695 0, 1696 MOUSE_BUTTON3DOWN, 1697 MOUSE_BUTTON3DOWN, 1698 MOUSE_BUTTON1DOWN, 1699 MOUSE_BUTTON1DOWN, 1700 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, 1701 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, 1702 }; 1703 /* for PS/2 VersaPad */ 1704 static int butmapversaps2[8] = { /* VersaPad */ 1705 0, 1706 MOUSE_BUTTON3DOWN, 1707 0, 1708 MOUSE_BUTTON3DOWN, 1709 MOUSE_BUTTON1DOWN, 1710 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, 1711 MOUSE_BUTTON1DOWN, 1712 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, 1713 }; 1714 static int pBufP = 0; 1715 static unsigned char pBuf[8]; 1716 static int prev_x, prev_y; 1717 static int on = FALSE; 1718 int x, y; 1719 1720 debug("received char 0x%x",(int)rBuf); 1721 if (rodent.rtype == MOUSE_PROTO_KIDSPAD) 1722 return kidspad(rBuf, act) ; 1723 if (rodent.rtype == MOUSE_PROTO_GTCO_DIGIPAD) 1724 return gtco_digipad(rBuf, act); 1725 1726 /* 1727 * Hack for resyncing: We check here for a package that is: 1728 * a) illegal (detected by wrong data-package header) 1729 * b) invalid (0x80 == -128 and that might be wrong for MouseSystems) 1730 * c) bad header-package 1731 * 1732 * NOTE: b) is a voilation of the MouseSystems-Protocol, since values of 1733 * -128 are allowed, but since they are very seldom we can easily 1734 * use them as package-header with no button pressed. 1735 * NOTE/2: On a PS/2 mouse any byte is valid as a data byte. Furthermore, 1736 * 0x80 is not valid as a header byte. For a PS/2 mouse we skip 1737 * checking data bytes. 1738 * For resyncing a PS/2 mouse we require the two most significant 1739 * bits in the header byte to be 0. These are the overflow bits, 1740 * and in case of an overflow we actually lose sync. Overflows 1741 * are very rare, however, and we quickly gain sync again after 1742 * an overflow condition. This is the best we can do. (Actually, 1743 * we could use bit 0x08 in the header byte for resyncing, since 1744 * that bit is supposed to be always on, but nobody told 1745 * Microsoft...) 1746 */ 1747 1748 if (pBufP != 0 && rodent.rtype != MOUSE_PROTO_PS2 && 1749 ((rBuf & cur_proto[2]) != cur_proto[3] || rBuf == 0x80)) 1750 { 1751 pBufP = 0; /* skip package */ 1752 } 1753 1754 if (pBufP == 0 && (rBuf & cur_proto[0]) != cur_proto[1]) 1755 return 0; 1756 1757 /* is there an extra data byte? */ 1758 if (pBufP >= cur_proto[4] && (rBuf & cur_proto[0]) != cur_proto[1]) 1759 { 1760 /* 1761 * Hack for Logitech MouseMan Mouse - Middle button 1762 * 1763 * Unfortunately this mouse has variable length packets: the standard 1764 * Microsoft 3 byte packet plus an optional 4th byte whenever the 1765 * middle button status changes. 1766 * 1767 * We have already processed the standard packet with the movement 1768 * and button info. Now post an event message with the old status 1769 * of the left and right buttons and the updated middle button. 1770 */ 1771 1772 /* 1773 * Even worse, different MouseMen and TrackMen differ in the 4th 1774 * byte: some will send 0x00/0x20, others 0x01/0x21, or even 1775 * 0x02/0x22, so I have to strip off the lower bits. 1776 * 1777 * [JCH-96/01/21] 1778 * HACK for ALPS "fourth button". (It's bit 0x10 of the "fourth byte" 1779 * and it is activated by tapping the glidepad with the finger! 8^) 1780 * We map it to bit bit3, and the reverse map in xf86Events just has 1781 * to be extended so that it is identified as Button 4. The lower 1782 * half of the reverse-map may remain unchanged. 1783 */ 1784 1785 /* 1786 * [KY-97/08/03] 1787 * Receive the fourth byte only when preceding three bytes have 1788 * been detected (pBufP >= cur_proto[4]). In the previous 1789 * versions, the test was pBufP == 0; thus, we may have mistakingly 1790 * received a byte even if we didn't see anything preceding 1791 * the byte. 1792 */ 1793 1794 if ((rBuf & cur_proto[5]) != cur_proto[6]) { 1795 pBufP = 0; 1796 return 0; 1797 } 1798 1799 switch (rodent.rtype) { 1800#if notyet 1801 case MOUSE_PROTO_MARIQUA: 1802 /* 1803 * This mouse has 16! buttons in addition to the standard 1804 * three of them. They return 0x10 though 0x1f in the 1805 * so-called `ten key' mode and 0x30 though 0x3f in the 1806 * `function key' mode. As there are only 31 bits for 1807 * button state (including the standard three), we ignore 1808 * the bit 0x20 and don't distinguish the two modes. 1809 */ 1810 act->dx = act->dy = act->dz = 0; 1811 act->obutton = act->button; 1812 rBuf &= 0x1f; 1813 act->button = (1 << (rBuf - 13)) 1814 | (act->obutton & (MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN)); 1815 /* 1816 * FIXME: this is a button "down" event. There needs to be 1817 * a corresponding button "up" event... XXX 1818 */ 1819 break; 1820#endif /* notyet */ 1821 case MOUSE_PROTO_JOGDIAL: 1822 break; 1823 1824 /* 1825 * IntelliMouse, NetMouse (including NetMouse Pro) and Mie Mouse 1826 * always send the fourth byte, whereas the fourth byte is 1827 * optional for GlidePoint and ThinkingMouse. The fourth byte 1828 * is also optional for MouseMan+ and FirstMouse+ in their 1829 * native mode. It is always sent if they are in the IntelliMouse 1830 * compatible mode. 1831 */ 1832 case MOUSE_PROTO_INTELLI: /* IntelliMouse, NetMouse, Mie Mouse, 1833 MouseMan+ */ 1834 act->dx = act->dy = 0; 1835 act->dz = (rBuf & 0x08) ? (rBuf & 0x0f) - 16 : (rBuf & 0x0f); 1836 if ((act->dz >= 7) || (act->dz <= -7)) 1837 act->dz = 0; 1838 act->obutton = act->button; 1839 act->button = butmapintelli[(rBuf & MOUSE_MSS_BUTTONS) >> 4] 1840 | (act->obutton & (MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN)); 1841 break; 1842 1843 default: 1844 act->dx = act->dy = act->dz = 0; 1845 act->obutton = act->button; 1846 act->button = butmapmss2[(rBuf & MOUSE_MSS_BUTTONS) >> 4] 1847 | (act->obutton & (MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN)); 1848 break; 1849 } 1850 1851 act->flags = ((act->dx || act->dy || act->dz) ? MOUSE_POSCHANGED : 0) 1852 | (act->obutton ^ act->button); 1853 pBufP = 0; 1854 return act->flags; 1855 } 1856 1857 if (pBufP >= cur_proto[4]) 1858 pBufP = 0; 1859 pBuf[pBufP++] = rBuf; 1860 if (pBufP != cur_proto[4]) 1861 return 0; 1862 1863 /* 1864 * assembly full package 1865 */ 1866 1867 debug("assembled full packet (len %d) %x,%x,%x,%x,%x,%x,%x,%x", 1868 cur_proto[4], 1869 pBuf[0], pBuf[1], pBuf[2], pBuf[3], 1870 pBuf[4], pBuf[5], pBuf[6], pBuf[7]); 1871 1872 act->dz = 0; 1873 act->obutton = act->button; 1874 switch (rodent.rtype) 1875 { 1876 case MOUSE_PROTO_MS: /* Microsoft */ 1877 case MOUSE_PROTO_LOGIMOUSEMAN: /* MouseMan/TrackMan */ 1878 case MOUSE_PROTO_X10MOUSEREM: /* X10 MouseRemote */ 1879 act->button = act->obutton & MOUSE_BUTTON4DOWN; 1880 if (rodent.flags & ChordMiddle) 1881 act->button |= ((pBuf[0] & MOUSE_MSS_BUTTONS) == MOUSE_MSS_BUTTONS) 1882 ? MOUSE_BUTTON2DOWN 1883 : butmapmss[(pBuf[0] & MOUSE_MSS_BUTTONS) >> 4]; 1884 else 1885 act->button |= (act->obutton & MOUSE_BUTTON2DOWN) 1886 | butmapmss[(pBuf[0] & MOUSE_MSS_BUTTONS) >> 4]; 1887 1888 /* Send X10 btn events to remote client (ensure -128-+127 range) */ 1889 if ((rodent.rtype == MOUSE_PROTO_X10MOUSEREM) && 1890 ((pBuf[0] & 0xFC) == 0x44) && (pBuf[2] == 0x3F)) { 1891 if (rodent.mremcfd >= 0) { 1892 unsigned char key = (signed char)(((pBuf[0] & 0x03) << 6) | 1893 (pBuf[1] & 0x3F)); 1894 write(rodent.mremcfd, &key, 1); 1895 } 1896 return 0; 1897 } 1898 1899 act->dx = (signed char)(((pBuf[0] & 0x03) << 6) | (pBuf[1] & 0x3F)); 1900 act->dy = (signed char)(((pBuf[0] & 0x0C) << 4) | (pBuf[2] & 0x3F)); 1901 break; 1902 1903 case MOUSE_PROTO_GLIDEPOINT: /* GlidePoint */ 1904 case MOUSE_PROTO_THINK: /* ThinkingMouse */ 1905 case MOUSE_PROTO_INTELLI: /* IntelliMouse, NetMouse, Mie Mouse, 1906 MouseMan+ */ 1907 act->button = (act->obutton & (MOUSE_BUTTON2DOWN | MOUSE_BUTTON4DOWN)) 1908 | butmapmss[(pBuf[0] & MOUSE_MSS_BUTTONS) >> 4]; 1909 act->dx = (signed char)(((pBuf[0] & 0x03) << 6) | (pBuf[1] & 0x3F)); 1910 act->dy = (signed char)(((pBuf[0] & 0x0C) << 4) | (pBuf[2] & 0x3F)); 1911 break; 1912 1913 case MOUSE_PROTO_MSC: /* MouseSystems Corp */ 1914#if notyet 1915 case MOUSE_PROTO_MARIQUA: /* Mariqua */ 1916#endif 1917 act->button = butmapmsc[(~pBuf[0]) & MOUSE_MSC_BUTTONS]; 1918 act->dx = (signed char)(pBuf[1]) + (signed char)(pBuf[3]); 1919 act->dy = - ((signed char)(pBuf[2]) + (signed char)(pBuf[4])); 1920 break; 1921 1922 case MOUSE_PROTO_JOGDIAL: /* JogDial */ 1923 if (rBuf == 0x6c) 1924 act->dz = -1; 1925 if (rBuf == 0x72) 1926 act->dz = 1; 1927 if (rBuf == 0x64) 1928 act->button = MOUSE_BUTTON1DOWN; 1929 if (rBuf == 0x75) 1930 act->button = 0; 1931 break; 1932 1933 case MOUSE_PROTO_HITTAB: /* MM HitTablet */ 1934 act->button = butmaphit[pBuf[0] & 0x07]; 1935 act->dx = (pBuf[0] & MOUSE_MM_XPOSITIVE) ? pBuf[1] : - pBuf[1]; 1936 act->dy = (pBuf[0] & MOUSE_MM_YPOSITIVE) ? - pBuf[2] : pBuf[2]; 1937 break; 1938 1939 case MOUSE_PROTO_MM: /* MM Series */ 1940 case MOUSE_PROTO_LOGI: /* Logitech Mice */ 1941 act->button = butmapmsc[pBuf[0] & MOUSE_MSC_BUTTONS]; 1942 act->dx = (pBuf[0] & MOUSE_MM_XPOSITIVE) ? pBuf[1] : - pBuf[1]; 1943 act->dy = (pBuf[0] & MOUSE_MM_YPOSITIVE) ? - pBuf[2] : pBuf[2]; 1944 break; 1945 1946 case MOUSE_PROTO_VERSAPAD: /* VersaPad */ 1947 act->button = butmapversa[(pBuf[0] & MOUSE_VERSA_BUTTONS) >> 3]; 1948 act->button |= (pBuf[0] & MOUSE_VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0; 1949 act->dx = act->dy = 0; 1950 if (!(pBuf[0] & MOUSE_VERSA_IN_USE)) { 1951 on = FALSE; 1952 break; 1953 } 1954 x = (pBuf[2] << 6) | pBuf[1]; 1955 if (x & 0x800) 1956 x -= 0x1000; 1957 y = (pBuf[4] << 6) | pBuf[3]; 1958 if (y & 0x800) 1959 y -= 0x1000; 1960 if (on) { 1961 act->dx = prev_x - x; 1962 act->dy = prev_y - y; 1963 } else { 1964 on = TRUE; 1965 } 1966 prev_x = x; 1967 prev_y = y; 1968 break; 1969 1970 case MOUSE_PROTO_BUS: /* Bus */ 1971 case MOUSE_PROTO_INPORT: /* InPort */ 1972 act->button = butmapmsc[(~pBuf[0]) & MOUSE_MSC_BUTTONS]; 1973 act->dx = (signed char)pBuf[1]; 1974 act->dy = - (signed char)pBuf[2]; 1975 break; 1976 1977 case MOUSE_PROTO_PS2: /* PS/2 */ 1978 act->button = butmapps2[pBuf[0] & MOUSE_PS2_BUTTONS]; 1979 act->dx = (pBuf[0] & MOUSE_PS2_XNEG) ? pBuf[1] - 256 : pBuf[1]; 1980 act->dy = (pBuf[0] & MOUSE_PS2_YNEG) ? -(pBuf[2] - 256) : -pBuf[2]; 1981 /* 1982 * Moused usually operates the psm driver at the operation level 1 1983 * which sends mouse data in MOUSE_PROTO_SYSMOUSE protocol. 1984 * The following code takes effect only when the user explicitly 1985 * requets the level 2 at which wheel movement and additional button 1986 * actions are encoded in model-dependent formats. At the level 0 1987 * the following code is no-op because the psm driver says the model 1988 * is MOUSE_MODEL_GENERIC. 1989 */ 1990 switch (rodent.hw.model) { 1991 case MOUSE_MODEL_EXPLORER: 1992 /* wheel and additional button data is in the fourth byte */ 1993 act->dz = (pBuf[3] & MOUSE_EXPLORER_ZNEG) 1994 ? (pBuf[3] & 0x0f) - 16 : (pBuf[3] & 0x0f); 1995 act->button |= (pBuf[3] & MOUSE_EXPLORER_BUTTON4DOWN) 1996 ? MOUSE_BUTTON4DOWN : 0; 1997 act->button |= (pBuf[3] & MOUSE_EXPLORER_BUTTON5DOWN) 1998 ? MOUSE_BUTTON5DOWN : 0; 1999 break; 2000 case MOUSE_MODEL_INTELLI: 2001 case MOUSE_MODEL_NET: 2002 /* wheel data is in the fourth byte */ 2003 act->dz = (signed char)pBuf[3]; 2004 if ((act->dz >= 7) || (act->dz <= -7)) 2005 act->dz = 0; 2006 /* some compatible mice may have additional buttons */ 2007 act->button |= (pBuf[0] & MOUSE_PS2INTELLI_BUTTON4DOWN) 2008 ? MOUSE_BUTTON4DOWN : 0; 2009 act->button |= (pBuf[0] & MOUSE_PS2INTELLI_BUTTON5DOWN) 2010 ? MOUSE_BUTTON5DOWN : 0; 2011 break; 2012 case MOUSE_MODEL_MOUSEMANPLUS: 2013 if (((pBuf[0] & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC) 2014 && (abs(act->dx) > 191) 2015 && MOUSE_PS2PLUS_CHECKBITS(pBuf)) { 2016 /* the extended data packet encodes button and wheel events */ 2017 switch (MOUSE_PS2PLUS_PACKET_TYPE(pBuf)) { 2018 case 1: 2019 /* wheel data packet */ 2020 act->dx = act->dy = 0; 2021 if (pBuf[2] & 0x80) { 2022 /* horizontal roller count - ignore it XXX*/ 2023 } else { 2024 /* vertical roller count */ 2025 act->dz = (pBuf[2] & MOUSE_PS2PLUS_ZNEG) 2026 ? (pBuf[2] & 0x0f) - 16 : (pBuf[2] & 0x0f); 2027 } 2028 act->button |= (pBuf[2] & MOUSE_PS2PLUS_BUTTON4DOWN) 2029 ? MOUSE_BUTTON4DOWN : 0; 2030 act->button |= (pBuf[2] & MOUSE_PS2PLUS_BUTTON5DOWN) 2031 ? MOUSE_BUTTON5DOWN : 0; 2032 break; 2033 case 2: 2034 /* this packet type is reserved by Logitech */ 2035 /* 2036 * IBM ScrollPoint Mouse uses this packet type to 2037 * encode both vertical and horizontal scroll movement. 2038 */ 2039 act->dx = act->dy = 0; 2040 /* horizontal roller count */ 2041 if (pBuf[2] & 0x0f) 2042 act->dz = (pBuf[2] & MOUSE_SPOINT_WNEG) ? -2 : 2; 2043 /* vertical roller count */ 2044 if (pBuf[2] & 0xf0) 2045 act->dz = (pBuf[2] & MOUSE_SPOINT_ZNEG) ? -1 : 1; 2046#if 0 2047 /* vertical roller count */ 2048 act->dz = (pBuf[2] & MOUSE_SPOINT_ZNEG) 2049 ? ((pBuf[2] >> 4) & 0x0f) - 16 2050 : ((pBuf[2] >> 4) & 0x0f); 2051 /* horizontal roller count */ 2052 act->dw = (pBuf[2] & MOUSE_SPOINT_WNEG) 2053 ? (pBuf[2] & 0x0f) - 16 : (pBuf[2] & 0x0f); 2054#endif 2055 break; 2056 case 0: 2057 /* device type packet - shouldn't happen */ 2058 /* FALLTHROUGH */ 2059 default: 2060 act->dx = act->dy = 0; 2061 act->button = act->obutton; 2062 debug("unknown PS2++ packet type %d: 0x%02x 0x%02x 0x%02x\n", 2063 MOUSE_PS2PLUS_PACKET_TYPE(pBuf), 2064 pBuf[0], pBuf[1], pBuf[2]); 2065 break; 2066 } 2067 } else { 2068 /* preserve button states */ 2069 act->button |= act->obutton & MOUSE_EXTBUTTONS; 2070 } 2071 break; 2072 case MOUSE_MODEL_GLIDEPOINT: 2073 /* `tapping' action */ 2074 act->button |= ((pBuf[0] & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN; 2075 break; 2076 case MOUSE_MODEL_NETSCROLL: 2077 /* three addtional bytes encode buttons and wheel events */ 2078 act->button |= (pBuf[3] & MOUSE_PS2_BUTTON3DOWN) 2079 ? MOUSE_BUTTON4DOWN : 0; 2080 act->button |= (pBuf[3] & MOUSE_PS2_BUTTON1DOWN) 2081 ? MOUSE_BUTTON5DOWN : 0; 2082 act->dz = (pBuf[3] & MOUSE_PS2_XNEG) ? pBuf[4] - 256 : pBuf[4]; 2083 break; 2084 case MOUSE_MODEL_THINK: 2085 /* the fourth button state in the first byte */ 2086 act->button |= (pBuf[0] & MOUSE_PS2_TAP) ? MOUSE_BUTTON4DOWN : 0; 2087 break; 2088 case MOUSE_MODEL_VERSAPAD: 2089 act->button = butmapversaps2[pBuf[0] & MOUSE_PS2VERSA_BUTTONS]; 2090 act->button |= 2091 (pBuf[0] & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0; 2092 act->dx = act->dy = 0; 2093 if (!(pBuf[0] & MOUSE_PS2VERSA_IN_USE)) { 2094 on = FALSE; 2095 break; 2096 } 2097 x = ((pBuf[4] << 8) & 0xf00) | pBuf[1]; 2098 if (x & 0x800) 2099 x -= 0x1000; 2100 y = ((pBuf[4] << 4) & 0xf00) | pBuf[2]; 2101 if (y & 0x800) 2102 y -= 0x1000; 2103 if (on) { 2104 act->dx = prev_x - x; 2105 act->dy = prev_y - y; 2106 } else { 2107 on = TRUE; 2108 } 2109 prev_x = x; 2110 prev_y = y; 2111 break; 2112 case MOUSE_MODEL_4D: 2113 act->dx = (pBuf[1] & 0x80) ? pBuf[1] - 256 : pBuf[1]; 2114 act->dy = (pBuf[2] & 0x80) ? -(pBuf[2] - 256) : -pBuf[2]; 2115 switch (pBuf[0] & MOUSE_4D_WHEELBITS) { 2116 case 0x10: 2117 act->dz = 1; 2118 break; 2119 case 0x30: 2120 act->dz = -1; 2121 break; 2122 case 0x40: /* 2nd wheel rolling right XXX */ 2123 act->dz = 2; 2124 break; 2125 case 0xc0: /* 2nd wheel rolling left XXX */ 2126 act->dz = -2; 2127 break; 2128 } 2129 break; 2130 case MOUSE_MODEL_4DPLUS: 2131 if ((act->dx < 16 - 256) && (act->dy > 256 - 16)) { 2132 act->dx = act->dy = 0; 2133 if (pBuf[2] & MOUSE_4DPLUS_BUTTON4DOWN) 2134 act->button |= MOUSE_BUTTON4DOWN; 2135 act->dz = (pBuf[2] & MOUSE_4DPLUS_ZNEG) 2136 ? ((pBuf[2] & 0x07) - 8) : (pBuf[2] & 0x07); 2137 } else { 2138 /* preserve previous button states */ 2139 act->button |= act->obutton & MOUSE_EXTBUTTONS; 2140 } 2141 break; 2142 case MOUSE_MODEL_GENERIC: 2143 default: 2144 break; 2145 } 2146 break; 2147 2148 case MOUSE_PROTO_SYSMOUSE: /* sysmouse */ 2149 act->button = butmapmsc[(~pBuf[0]) & MOUSE_SYS_STDBUTTONS]; 2150 act->dx = (signed char)(pBuf[1]) + (signed char)(pBuf[3]); 2151 act->dy = - ((signed char)(pBuf[2]) + (signed char)(pBuf[4])); 2152 if (rodent.level == 1) { 2153 act->dz = ((signed char)(pBuf[5] << 1) + (signed char)(pBuf[6] << 1)) >> 1; 2154 act->button |= ((~pBuf[7] & MOUSE_SYS_EXTBUTTONS) << 3); 2155 } 2156 break; 2157 2158 default: 2159 return 0; 2160 } 2161 /* 2162 * We don't reset pBufP here yet, as there may be an additional data 2163 * byte in some protocols. See above. 2164 */ 2165 2166 /* has something changed? */ 2167 act->flags = ((act->dx || act->dy || act->dz) ? MOUSE_POSCHANGED : 0) 2168 | (act->obutton ^ act->button); 2169 2170 return act->flags; 2171} 2172 2173static int 2174r_statetrans(mousestatus_t *a1, mousestatus_t *a2, int trans) 2175{ 2176 int changed; 2177 int flags; 2178 2179 a2->dx = a1->dx; 2180 a2->dy = a1->dy; 2181 a2->dz = a1->dz; 2182 a2->obutton = a2->button; 2183 a2->button = a1->button; 2184 a2->flags = a1->flags; 2185 changed = FALSE; 2186 2187 if (rodent.flags & Emulate3Button) { 2188 if (debug > 2) 2189 debug("state:%d, trans:%d -> state:%d", 2190 mouse_button_state, trans, 2191 states[mouse_button_state].s[trans]); 2192 /* 2193 * Avoid re-ordering button and movement events. While a button 2194 * event is deferred, throw away up to BUTTON2_MAXMOVE movement 2195 * events to allow for mouse jitter. If more movement events 2196 * occur, then complete the deferred button events immediately. 2197 */ 2198 if ((a2->dx != 0 || a2->dy != 0) && 2199 S_DELAYED(states[mouse_button_state].s[trans])) { 2200 if (++mouse_move_delayed > BUTTON2_MAXMOVE) { 2201 mouse_move_delayed = 0; 2202 mouse_button_state = 2203 states[mouse_button_state].s[A_TIMEOUT]; 2204 changed = TRUE; 2205 } else 2206 a2->dx = a2->dy = 0; 2207 } else 2208 mouse_move_delayed = 0; 2209 if (mouse_button_state != states[mouse_button_state].s[trans]) 2210 changed = TRUE; 2211 if (changed) 2212 gettimeofday(&mouse_button_state_tv, NULL); 2213 mouse_button_state = states[mouse_button_state].s[trans]; 2214 a2->button &= 2215 ~(MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN); 2216 a2->button &= states[mouse_button_state].mask; 2217 a2->button |= states[mouse_button_state].buttons; 2218 flags = a2->flags & MOUSE_POSCHANGED; 2219 flags |= a2->obutton ^ a2->button; 2220 if (flags & MOUSE_BUTTON2DOWN) { 2221 a2->flags = flags & MOUSE_BUTTON2DOWN; 2222 r_timestamp(a2); 2223 } 2224 a2->flags = flags; 2225 } 2226 return changed; 2227} 2228 2229/* phisical to logical button mapping */ 2230static int p2l[MOUSE_MAXBUTTON] = { 2231 MOUSE_BUTTON1DOWN, MOUSE_BUTTON2DOWN, MOUSE_BUTTON3DOWN, MOUSE_BUTTON4DOWN, 2232 MOUSE_BUTTON5DOWN, MOUSE_BUTTON6DOWN, MOUSE_BUTTON7DOWN, MOUSE_BUTTON8DOWN, 2233 0x00000100, 0x00000200, 0x00000400, 0x00000800, 2234 0x00001000, 0x00002000, 0x00004000, 0x00008000, 2235 0x00010000, 0x00020000, 0x00040000, 0x00080000, 2236 0x00100000, 0x00200000, 0x00400000, 0x00800000, 2237 0x01000000, 0x02000000, 0x04000000, 0x08000000, 2238 0x10000000, 0x20000000, 0x40000000, 2239}; 2240 2241static char * 2242skipspace(char *s) 2243{ 2244 while(isspace(*s)) 2245 ++s; 2246 return s; 2247} 2248 2249static int 2250r_installmap(char *arg) 2251{ 2252 int pbutton; 2253 int lbutton; 2254 char *s; 2255 2256 while (*arg) { 2257 arg = skipspace(arg); 2258 s = arg; 2259 while (isdigit(*arg)) 2260 ++arg; 2261 arg = skipspace(arg); 2262 if ((arg <= s) || (*arg != '=')) 2263 return FALSE; 2264 lbutton = atoi(s); 2265 2266 arg = skipspace(++arg); 2267 s = arg; 2268 while (isdigit(*arg)) 2269 ++arg; 2270 if ((arg <= s) || (!isspace(*arg) && (*arg != '\0'))) 2271 return FALSE; 2272 pbutton = atoi(s); 2273 2274 if ((lbutton <= 0) || (lbutton > MOUSE_MAXBUTTON)) 2275 return FALSE; 2276 if ((pbutton <= 0) || (pbutton > MOUSE_MAXBUTTON)) 2277 return FALSE; 2278 p2l[pbutton - 1] = 1 << (lbutton - 1); 2279 mstate[lbutton - 1] = &bstate[pbutton - 1]; 2280 } 2281 2282 return TRUE; 2283} 2284 2285static void 2286r_map(mousestatus_t *act1, mousestatus_t *act2) 2287{ 2288 register int pb; 2289 register int pbuttons; 2290 int lbuttons; 2291 2292 pbuttons = act1->button; 2293 lbuttons = 0; 2294 2295 act2->obutton = act2->button; 2296 if (pbuttons & rodent.wmode) { 2297 pbuttons &= ~rodent.wmode; 2298 act1->dz = act1->dy; 2299 act1->dx = 0; 2300 act1->dy = 0; 2301 } 2302 act2->dx = act1->dx; 2303 act2->dy = act1->dy; 2304 act2->dz = act1->dz; 2305 2306 switch (rodent.zmap[0]) { 2307 case 0: /* do nothing */ 2308 break; 2309 case MOUSE_XAXIS: 2310 if (act1->dz != 0) { 2311 act2->dx = act1->dz; 2312 act2->dz = 0; 2313 } 2314 break; 2315 case MOUSE_YAXIS: 2316 if (act1->dz != 0) { 2317 act2->dy = act1->dz; 2318 act2->dz = 0; 2319 } 2320 break; 2321 default: /* buttons */ 2322 pbuttons &= ~(rodent.zmap[0] | rodent.zmap[1] 2323 | rodent.zmap[2] | rodent.zmap[3]); 2324 if ((act1->dz < -1) && rodent.zmap[2]) { 2325 pbuttons |= rodent.zmap[2]; 2326 zstate[2].count = 1; 2327 } else if (act1->dz < 0) { 2328 pbuttons |= rodent.zmap[0]; 2329 zstate[0].count = 1; 2330 } else if ((act1->dz > 1) && rodent.zmap[3]) { 2331 pbuttons |= rodent.zmap[3]; 2332 zstate[3].count = 1; 2333 } else if (act1->dz > 0) { 2334 pbuttons |= rodent.zmap[1]; 2335 zstate[1].count = 1; 2336 } 2337 act2->dz = 0; 2338 break; 2339 } 2340 2341 for (pb = 0; (pb < MOUSE_MAXBUTTON) && (pbuttons != 0); ++pb) { 2342 lbuttons |= (pbuttons & 1) ? p2l[pb] : 0; 2343 pbuttons >>= 1; 2344 } 2345 act2->button = lbuttons; 2346 2347 act2->flags = ((act2->dx || act2->dy || act2->dz) ? MOUSE_POSCHANGED : 0) 2348 | (act2->obutton ^ act2->button); 2349} 2350 2351static void 2352r_timestamp(mousestatus_t *act) 2353{ 2354 struct timeval tv; 2355 struct timeval tv1; 2356 struct timeval tv2; 2357 struct timeval tv3; 2358 int button; 2359 int mask; 2360 int i; 2361 2362 mask = act->flags & MOUSE_BUTTONS; 2363#if 0 2364 if (mask == 0) 2365 return; 2366#endif 2367 2368 gettimeofday(&tv1, NULL); 2369 2370 /* double click threshold */ 2371 tv2.tv_sec = rodent.clickthreshold/1000; 2372 tv2.tv_usec = (rodent.clickthreshold%1000)*1000; 2373 timersub(&tv1, &tv2, &tv); 2374 debug("tv: %ld %ld", tv.tv_sec, tv.tv_usec); 2375 2376 /* 3 button emulation timeout */ 2377 tv2.tv_sec = rodent.button2timeout/1000; 2378 tv2.tv_usec = (rodent.button2timeout%1000)*1000; 2379 timersub(&tv1, &tv2, &tv3); 2380 2381 button = MOUSE_BUTTON1DOWN; 2382 for (i = 0; (i < MOUSE_MAXBUTTON) && (mask != 0); ++i) { 2383 if (mask & 1) { 2384 if (act->button & button) { 2385 /* the button is down */ 2386 debug(" : %ld %ld", 2387 bstate[i].tv.tv_sec, bstate[i].tv.tv_usec); 2388 if (timercmp(&tv, &bstate[i].tv, >)) { 2389 bstate[i].count = 1; 2390 } else { 2391 ++bstate[i].count; 2392 } 2393 bstate[i].tv = tv1; 2394 } else { 2395 /* the button is up */ 2396 bstate[i].tv = tv1; 2397 } 2398 } else { 2399 if (act->button & button) { 2400 /* the button has been down */ 2401 if (timercmp(&tv3, &bstate[i].tv, >)) { 2402 bstate[i].count = 1; 2403 bstate[i].tv = tv1; 2404 act->flags |= button; 2405 debug("button %d timeout", i + 1); 2406 } 2407 } else { 2408 /* the button has been up */ 2409 } 2410 } 2411 button <<= 1; 2412 mask >>= 1; 2413 } 2414} 2415 2416static int 2417r_timeout(void) 2418{ 2419 struct timeval tv; 2420 struct timeval tv1; 2421 struct timeval tv2; 2422 2423 if (states[mouse_button_state].timeout) 2424 return TRUE; 2425 gettimeofday(&tv1, NULL); 2426 tv2.tv_sec = rodent.button2timeout/1000; 2427 tv2.tv_usec = (rodent.button2timeout%1000)*1000; 2428 timersub(&tv1, &tv2, &tv); 2429 return timercmp(&tv, &mouse_button_state_tv, >); 2430} 2431 2432static void 2433r_click(mousestatus_t *act) 2434{ 2435 struct mouse_info mouse; 2436 int button; 2437 int mask; 2438 int i; 2439 2440 mask = act->flags & MOUSE_BUTTONS; 2441 if (mask == 0) 2442 return; 2443 2444 button = MOUSE_BUTTON1DOWN; 2445 for (i = 0; (i < MOUSE_MAXBUTTON) && (mask != 0); ++i) { 2446 if (mask & 1) { 2447 debug("mstate[%d]->count:%d", i, mstate[i]->count); 2448 if (act->button & button) { 2449 /* the button is down */ 2450 mouse.u.event.value = mstate[i]->count; 2451 } else { 2452 /* the button is up */ 2453 mouse.u.event.value = 0; 2454 } 2455 mouse.operation = MOUSE_BUTTON_EVENT; 2456 mouse.u.event.id = button; 2457 if (debug < 2) 2458 ioctl(rodent.cfd, CONS_MOUSECTL, &mouse); 2459 debug("button %d count %d", i + 1, mouse.u.event.value); 2460 } 2461 button <<= 1; 2462 mask >>= 1; 2463 } 2464} 2465 2466/* $XConsortium: posix_tty.c,v 1.3 95/01/05 20:42:55 kaleb Exp $ */ 2467/* $XFree86: xc/programs/Xserver/hw/xfree86/os-support/shared/posix_tty.c,v 3.4 1995/01/28 17:05:03 dawes Exp $ */ 2468/* 2469 * Copyright 1993 by David Dawes <dawes@physics.su.oz.au> 2470 * 2471 * Permission to use, copy, modify, distribute, and sell this software and its 2472 * documentation for any purpose is hereby granted without fee, provided that 2473 * the above copyright notice appear in all copies and that both that 2474 * copyright notice and this permission notice appear in supporting 2475 * documentation, and that the name of David Dawes 2476 * not be used in advertising or publicity pertaining to distribution of 2477 * the software without specific, written prior permission. 2478 * David Dawes makes no representations about the suitability of this 2479 * software for any purpose. It is provided "as is" without express or 2480 * implied warranty. 2481 * 2482 * DAVID DAWES DISCLAIMS ALL WARRANTIES WITH REGARD TO 2483 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND 2484 * FITNESS, IN NO EVENT SHALL DAVID DAWES BE LIABLE FOR 2485 * ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER 2486 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF 2487 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 2488 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 2489 * 2490 */ 2491 2492 2493static void 2494setmousespeed(int old, int new, unsigned cflag) 2495{ 2496 struct termios tty; 2497 char *c; 2498 2499 if (tcgetattr(rodent.mfd, &tty) < 0) 2500 { 2501 logwarn("unable to get status of mouse fd"); 2502 return; 2503 } 2504 2505 tty.c_iflag = IGNBRK | IGNPAR; 2506 tty.c_oflag = 0; 2507 tty.c_lflag = 0; 2508 tty.c_cflag = (tcflag_t)cflag; 2509 tty.c_cc[VTIME] = 0; 2510 tty.c_cc[VMIN] = 1; 2511 2512 switch (old) 2513 { 2514 case 9600: 2515 cfsetispeed(&tty, B9600); 2516 cfsetospeed(&tty, B9600); 2517 break; 2518 case 4800: 2519 cfsetispeed(&tty, B4800); 2520 cfsetospeed(&tty, B4800); 2521 break; 2522 case 2400: 2523 cfsetispeed(&tty, B2400); 2524 cfsetospeed(&tty, B2400); 2525 break; 2526 case 1200: 2527 default: 2528 cfsetispeed(&tty, B1200); 2529 cfsetospeed(&tty, B1200); 2530 } 2531 2532 if (tcsetattr(rodent.mfd, TCSADRAIN, &tty) < 0) 2533 { 2534 logwarn("unable to set status of mouse fd"); 2535 return; 2536 } 2537 2538 switch (new) 2539 { 2540 case 9600: 2541 c = "*q"; 2542 cfsetispeed(&tty, B9600); 2543 cfsetospeed(&tty, B9600); 2544 break; 2545 case 4800: 2546 c = "*p"; 2547 cfsetispeed(&tty, B4800); 2548 cfsetospeed(&tty, B4800); 2549 break; 2550 case 2400: 2551 c = "*o"; 2552 cfsetispeed(&tty, B2400); 2553 cfsetospeed(&tty, B2400); 2554 break; 2555 case 1200: 2556 default: 2557 c = "*n"; 2558 cfsetispeed(&tty, B1200); 2559 cfsetospeed(&tty, B1200); 2560 } 2561 2562 if (rodent.rtype == MOUSE_PROTO_LOGIMOUSEMAN 2563 || rodent.rtype == MOUSE_PROTO_LOGI) 2564 { 2565 if (write(rodent.mfd, c, 2) != 2) 2566 { 2567 logwarn("unable to write to mouse fd"); 2568 return; 2569 } 2570 } 2571 usleep(100000); 2572 2573 if (tcsetattr(rodent.mfd, TCSADRAIN, &tty) < 0) 2574 logwarn("unable to set status of mouse fd"); 2575} 2576 2577/* 2578 * PnP COM device support 2579 * 2580 * It's a simplistic implementation, but it works :-) 2581 * KY, 31/7/97. 2582 */ 2583 2584/* 2585 * Try to elicit a PnP ID as described in 2586 * Microsoft, Hayes: "Plug and Play External COM Device Specification, 2587 * rev 1.00", 1995. 2588 * 2589 * The routine does not fully implement the COM Enumerator as par Section 2590 * 2.1 of the document. In particular, we don't have idle state in which 2591 * the driver software monitors the com port for dynamic connection or 2592 * removal of a device at the port, because `moused' simply quits if no 2593 * device is found. 2594 * 2595 * In addition, as PnP COM device enumeration procedure slightly has 2596 * changed since its first publication, devices which follow earlier 2597 * revisions of the above spec. may fail to respond if the rev 1.0 2598 * procedure is used. XXX 2599 */ 2600static int 2601pnpwakeup1(void) 2602{ 2603 struct timeval timeout; 2604 fd_set fds; 2605 int i; 2606 2607 /* 2608 * This is the procedure described in rev 1.0 of PnP COM device spec. 2609 * Unfortunately, some devices which comform to earlier revisions of 2610 * the spec gets confused and do not return the ID string... 2611 */ 2612 debug("PnP COM device rev 1.0 probe..."); 2613 2614 /* port initialization (2.1.2) */ 2615 ioctl(rodent.mfd, TIOCMGET, &i); 2616 i |= TIOCM_DTR; /* DTR = 1 */ 2617 i &= ~TIOCM_RTS; /* RTS = 0 */ 2618 ioctl(rodent.mfd, TIOCMSET, &i); 2619 usleep(240000); 2620 2621 /* 2622 * The PnP COM device spec. dictates that the mouse must set DSR 2623 * in response to DTR (by hardware or by software) and that if DSR is 2624 * not asserted, the host computer should think that there is no device 2625 * at this serial port. But some mice just don't do that... 2626 */ 2627 ioctl(rodent.mfd, TIOCMGET, &i); 2628 debug("modem status 0%o", i); 2629 if ((i & TIOCM_DSR) == 0) 2630 return FALSE; 2631 2632 /* port setup, 1st phase (2.1.3) */ 2633 setmousespeed(1200, 1200, (CS7 | CREAD | CLOCAL | HUPCL)); 2634 i = TIOCM_DTR | TIOCM_RTS; /* DTR = 0, RTS = 0 */ 2635 ioctl(rodent.mfd, TIOCMBIC, &i); 2636 usleep(240000); 2637 i = TIOCM_DTR; /* DTR = 1, RTS = 0 */ 2638 ioctl(rodent.mfd, TIOCMBIS, &i); 2639 usleep(240000); 2640 2641 /* wait for response, 1st phase (2.1.4) */ 2642 i = FREAD; 2643 ioctl(rodent.mfd, TIOCFLUSH, &i); 2644 i = TIOCM_RTS; /* DTR = 1, RTS = 1 */ 2645 ioctl(rodent.mfd, TIOCMBIS, &i); 2646 2647 /* try to read something */ 2648 FD_ZERO(&fds); 2649 FD_SET(rodent.mfd, &fds); 2650 timeout.tv_sec = 0; 2651 timeout.tv_usec = 240000; 2652 if (select(FD_SETSIZE, &fds, NULL, NULL, &timeout) > 0) { 2653 debug("pnpwakeup1(): valid response in first phase."); 2654 return TRUE; 2655 } 2656 2657 /* port setup, 2nd phase (2.1.5) */ 2658 i = TIOCM_DTR | TIOCM_RTS; /* DTR = 0, RTS = 0 */ 2659 ioctl(rodent.mfd, TIOCMBIC, &i); 2660 usleep(240000); 2661 2662 /* wait for respose, 2nd phase (2.1.6) */ 2663 i = FREAD; 2664 ioctl(rodent.mfd, TIOCFLUSH, &i); 2665 i = TIOCM_DTR | TIOCM_RTS; /* DTR = 1, RTS = 1 */ 2666 ioctl(rodent.mfd, TIOCMBIS, &i); 2667 2668 /* try to read something */ 2669 FD_ZERO(&fds); 2670 FD_SET(rodent.mfd, &fds); 2671 timeout.tv_sec = 0; 2672 timeout.tv_usec = 240000; 2673 if (select(FD_SETSIZE, &fds, NULL, NULL, &timeout) > 0) { 2674 debug("pnpwakeup1(): valid response in second phase."); 2675 return TRUE; 2676 } 2677 2678 return FALSE; 2679} 2680 2681static int 2682pnpwakeup2(void) 2683{ 2684 struct timeval timeout; 2685 fd_set fds; 2686 int i; 2687 2688 /* 2689 * This is a simplified procedure; it simply toggles RTS. 2690 */ 2691 debug("alternate probe..."); 2692 2693 ioctl(rodent.mfd, TIOCMGET, &i); 2694 i |= TIOCM_DTR; /* DTR = 1 */ 2695 i &= ~TIOCM_RTS; /* RTS = 0 */ 2696 ioctl(rodent.mfd, TIOCMSET, &i); 2697 usleep(240000); 2698 2699 setmousespeed(1200, 1200, (CS7 | CREAD | CLOCAL | HUPCL)); 2700 2701 /* wait for respose */ 2702 i = FREAD; 2703 ioctl(rodent.mfd, TIOCFLUSH, &i); 2704 i = TIOCM_DTR | TIOCM_RTS; /* DTR = 1, RTS = 1 */ 2705 ioctl(rodent.mfd, TIOCMBIS, &i); 2706 2707 /* try to read something */ 2708 FD_ZERO(&fds); 2709 FD_SET(rodent.mfd, &fds); 2710 timeout.tv_sec = 0; 2711 timeout.tv_usec = 240000; 2712 if (select(FD_SETSIZE, &fds, NULL, NULL, &timeout) > 0) { 2713 debug("pnpwakeup2(): valid response."); 2714 return TRUE; 2715 } 2716 2717 return FALSE; 2718} 2719 2720static int 2721pnpgets(char *buf) 2722{ 2723 struct timeval timeout; 2724 fd_set fds; 2725 int begin; 2726 int i; 2727 char c; 2728 2729 if (!pnpwakeup1() && !pnpwakeup2()) { 2730 /* 2731 * According to PnP spec, we should set DTR = 1 and RTS = 0 while 2732 * in idle state. But, `moused' shall set DTR = RTS = 1 and proceed, 2733 * assuming there is something at the port even if it didn't 2734 * respond to the PnP enumeration procedure. 2735 */ 2736 i = TIOCM_DTR | TIOCM_RTS; /* DTR = 1, RTS = 1 */ 2737 ioctl(rodent.mfd, TIOCMBIS, &i); 2738 return 0; 2739 } 2740 2741 /* collect PnP COM device ID (2.1.7) */ 2742 begin = -1; 2743 i = 0; 2744 usleep(240000); /* the mouse must send `Begin ID' within 200msec */ 2745 while (read(rodent.mfd, &c, 1) == 1) { 2746 /* we may see "M", or "M3..." before `Begin ID' */ 2747 buf[i++] = c; 2748 if ((c == 0x08) || (c == 0x28)) { /* Begin ID */ 2749 debug("begin-id %02x", c); 2750 begin = i - 1; 2751 break; 2752 } 2753 debug("%c %02x", c, c); 2754 if (i >= 256) 2755 break; 2756 } 2757 if (begin < 0) { 2758 /* we haven't seen `Begin ID' in time... */ 2759 goto connect_idle; 2760 } 2761 2762 ++c; /* make it `End ID' */ 2763 for (;;) { 2764 FD_ZERO(&fds); 2765 FD_SET(rodent.mfd, &fds); 2766 timeout.tv_sec = 0; 2767 timeout.tv_usec = 240000; 2768 if (select(FD_SETSIZE, &fds, NULL, NULL, &timeout) <= 0) 2769 break; 2770 2771 read(rodent.mfd, &buf[i], 1); 2772 if (buf[i++] == c) /* End ID */ 2773 break; 2774 if (i >= 256) 2775 break; 2776 } 2777 if (begin > 0) { 2778 i -= begin; 2779 bcopy(&buf[begin], &buf[0], i); 2780 } 2781 /* string may not be human readable... */ 2782 debug("len:%d, '%-*.*s'", i, i, i, buf); 2783 2784 if (buf[i - 1] == c) 2785 return i; /* a valid PnP string */ 2786 2787 /* 2788 * According to PnP spec, we should set DTR = 1 and RTS = 0 while 2789 * in idle state. But, `moused' shall leave the modem control lines 2790 * as they are. See above. 2791 */ 2792connect_idle: 2793 2794 /* we may still have something in the buffer */ 2795 return ((i > 0) ? i : 0); 2796} 2797 2798static int 2799pnpparse(pnpid_t *id, char *buf, int len) 2800{ 2801 char s[3]; 2802 int offset; 2803 int sum = 0; 2804 int i, j; 2805 2806 id->revision = 0; 2807 id->eisaid = NULL; 2808 id->serial = NULL; 2809 id->class = NULL; 2810 id->compat = NULL; 2811 id->description = NULL; 2812 id->neisaid = 0; 2813 id->nserial = 0; 2814 id->nclass = 0; 2815 id->ncompat = 0; 2816 id->ndescription = 0; 2817 2818 if ((buf[0] != 0x28) && (buf[0] != 0x08)) { 2819 /* non-PnP mice */ 2820 switch(buf[0]) { 2821 default: 2822 return FALSE; 2823 case 'M': /* Microsoft */ 2824 id->eisaid = "PNP0F01"; 2825 break; 2826 case 'H': /* MouseSystems */ 2827 id->eisaid = "PNP0F04"; 2828 break; 2829 } 2830 id->neisaid = strlen(id->eisaid); 2831 id->class = "MOUSE"; 2832 id->nclass = strlen(id->class); 2833 debug("non-PnP mouse '%c'", buf[0]); 2834 return TRUE; 2835 } 2836 2837 /* PnP mice */ 2838 offset = 0x28 - buf[0]; 2839 2840 /* calculate checksum */ 2841 for (i = 0; i < len - 3; ++i) { 2842 sum += buf[i]; 2843 buf[i] += offset; 2844 } 2845 sum += buf[len - 1]; 2846 for (; i < len; ++i) 2847 buf[i] += offset; 2848 debug("PnP ID string: '%*.*s'", len, len, buf); 2849 2850 /* revision */ 2851 buf[1] -= offset; 2852 buf[2] -= offset; 2853 id->revision = ((buf[1] & 0x3f) << 6) | (buf[2] & 0x3f); 2854 debug("PnP rev %d.%02d", id->revision / 100, id->revision % 100); 2855 2856 /* EISA vender and product ID */ 2857 id->eisaid = &buf[3]; 2858 id->neisaid = 7; 2859 2860 /* option strings */ 2861 i = 10; 2862 if (buf[i] == '\\') { 2863 /* device serial # */ 2864 for (j = ++i; i < len; ++i) { 2865 if (buf[i] == '\\') 2866 break; 2867 } 2868 if (i >= len) 2869 i -= 3; 2870 if (i - j == 8) { 2871 id->serial = &buf[j]; 2872 id->nserial = 8; 2873 } 2874 } 2875 if (buf[i] == '\\') { 2876 /* PnP class */ 2877 for (j = ++i; i < len; ++i) { 2878 if (buf[i] == '\\') 2879 break; 2880 } 2881 if (i >= len) 2882 i -= 3; 2883 if (i > j + 1) { 2884 id->class = &buf[j]; 2885 id->nclass = i - j; 2886 } 2887 } 2888 if (buf[i] == '\\') { 2889 /* compatible driver */ 2890 for (j = ++i; i < len; ++i) { 2891 if (buf[i] == '\\') 2892 break; 2893 } 2894 /* 2895 * PnP COM spec prior to v0.96 allowed '*' in this field, 2896 * it's not allowed now; just igore it. 2897 */ 2898 if (buf[j] == '*') 2899 ++j; 2900 if (i >= len) 2901 i -= 3; 2902 if (i > j + 1) { 2903 id->compat = &buf[j]; 2904 id->ncompat = i - j; 2905 } 2906 } 2907 if (buf[i] == '\\') { 2908 /* product description */ 2909 for (j = ++i; i < len; ++i) { 2910 if (buf[i] == ';') 2911 break; 2912 } 2913 if (i >= len) 2914 i -= 3; 2915 if (i > j + 1) { 2916 id->description = &buf[j]; 2917 id->ndescription = i - j; 2918 } 2919 } 2920 2921 /* checksum exists if there are any optional fields */ 2922 if ((id->nserial > 0) || (id->nclass > 0) 2923 || (id->ncompat > 0) || (id->ndescription > 0)) { 2924 debug("PnP checksum: 0x%X", sum); 2925 sprintf(s, "%02X", sum & 0x0ff); 2926 if (strncmp(s, &buf[len - 3], 2) != 0) { 2927#if 0 2928 /* 2929 * I found some mice do not comply with the PnP COM device 2930 * spec regarding checksum... XXX 2931 */ 2932 logwarnx("PnP checksum error", 0); 2933 return FALSE; 2934#endif 2935 } 2936 } 2937 2938 return TRUE; 2939} 2940 2941static symtab_t * 2942pnpproto(pnpid_t *id) 2943{ 2944 symtab_t *t; 2945 int i, j; 2946 2947 if (id->nclass > 0) 2948 if (strncmp(id->class, "MOUSE", id->nclass) != 0 && 2949 strncmp(id->class, "TABLET", id->nclass) != 0) 2950 /* this is not a mouse! */ 2951 return NULL; 2952 2953 if (id->neisaid > 0) { 2954 t = gettoken(pnpprod, id->eisaid, id->neisaid); 2955 if (t->val != MOUSE_PROTO_UNKNOWN) 2956 return t; 2957 } 2958 2959 /* 2960 * The 'Compatible drivers' field may contain more than one 2961 * ID separated by ','. 2962 */ 2963 if (id->ncompat <= 0) 2964 return NULL; 2965 for (i = 0; i < id->ncompat; ++i) { 2966 for (j = i; id->compat[i] != ','; ++i) 2967 if (i >= id->ncompat) 2968 break; 2969 if (i > j) { 2970 t = gettoken(pnpprod, id->compat + j, i - j); 2971 if (t->val != MOUSE_PROTO_UNKNOWN) 2972 return t; 2973 } 2974 } 2975 2976 return NULL; 2977} 2978 2979/* name/val mapping */ 2980 2981static symtab_t * 2982gettoken(symtab_t *tab, char *s, int len) 2983{ 2984 int i; 2985 2986 for (i = 0; tab[i].name != NULL; ++i) { 2987 if (strncmp(tab[i].name, s, len) == 0) 2988 break; 2989 } 2990 return &tab[i]; 2991} 2992 2993static char * 2994gettokenname(symtab_t *tab, int val) 2995{ 2996 int i; 2997 2998 for (i = 0; tab[i].name != NULL; ++i) { 2999 if (tab[i].val == val) 3000 return tab[i].name; 3001 } 3002 return NULL; 3003} 3004 3005 3006/* 3007 * code to read from the Genius Kidspad tablet. 3008 3009The tablet responds to the COM PnP protocol 1.0 with EISA-ID KYE0005, 3010and to pre-pnp probes (RTS toggle) with 'T' (tablet ?) 30119600, 8 bit, parity odd. 3012 3013The tablet puts out 5 bytes. b0 (mask 0xb8, value 0xb8) contains 3014the proximity, tip and button info: 3015 (byte0 & 0x1) true = tip pressed 3016 (byte0 & 0x2) true = button pressed 3017 (byte0 & 0x40) false = pen in proximity of tablet. 3018 3019The next 4 bytes are used for coordinates xl, xh, yl, yh (7 bits valid). 3020 3021Only absolute coordinates are returned, so we use the following approach: 3022we store the last coordinates sent when the pen went out of the tablet, 3023 3024 3025 * 3026 */ 3027 3028typedef enum { 3029 S_IDLE, S_PROXY, S_FIRST, S_DOWN, S_UP 3030} k_status ; 3031 3032static int 3033kidspad(u_char rxc, mousestatus_t *act) 3034{ 3035 static int buf[5]; 3036 static int buflen = 0, b_prev = 0 , x_prev = -1, y_prev = -1 ; 3037 static k_status status = S_IDLE ; 3038 static struct timeval old, now ; 3039 3040 int x, y ; 3041 3042 if (buflen > 0 && (rxc & 0x80)) { 3043 fprintf(stderr, "invalid code %d 0x%x\n", buflen, rxc); 3044 buflen = 0 ; 3045 } 3046 if (buflen == 0 && (rxc & 0xb8) != 0xb8) { 3047 fprintf(stderr, "invalid code 0 0x%x\n", rxc); 3048 return 0 ; /* invalid code, no action */ 3049 } 3050 buf[buflen++] = rxc ; 3051 if (buflen < 5) 3052 return 0 ; 3053 3054 buflen = 0 ; /* for next time... */ 3055 3056 x = buf[1]+128*(buf[2] - 7) ; 3057 if (x < 0) x = 0 ; 3058 y = 28*128 - (buf[3] + 128* (buf[4] - 7)) ; 3059 if (y < 0) y = 0 ; 3060 3061 x /= 8 ; 3062 y /= 8 ; 3063 3064 act->flags = 0 ; 3065 act->obutton = act->button ; 3066 act->dx = act->dy = act->dz = 0 ; 3067 gettimeofday(&now, NULL); 3068 if (buf[0] & 0x40) /* pen went out of reach */ 3069 status = S_IDLE ; 3070 else if (status == S_IDLE) { /* pen is newly near the tablet */ 3071 act->flags |= MOUSE_POSCHANGED ; /* force update */ 3072 status = S_PROXY ; 3073 x_prev = x ; 3074 y_prev = y ; 3075 } 3076 old = now ; 3077 act->dx = x - x_prev ; 3078 act->dy = y - y_prev ; 3079 if (act->dx || act->dy) 3080 act->flags |= MOUSE_POSCHANGED ; 3081 x_prev = x ; 3082 y_prev = y ; 3083 if (b_prev != 0 && b_prev != buf[0]) { /* possibly record button change */ 3084 act->button = 0 ; 3085 if (buf[0] & 0x01) /* tip pressed */ 3086 act->button |= MOUSE_BUTTON1DOWN ; 3087 if (buf[0] & 0x02) /* button pressed */ 3088 act->button |= MOUSE_BUTTON2DOWN ; 3089 act->flags |= MOUSE_BUTTONSCHANGED ; 3090 } 3091 b_prev = buf[0] ; 3092 return act->flags ; 3093} 3094 3095static int 3096gtco_digipad (u_char rxc, mousestatus_t *act) 3097{ 3098 static u_char buf[5]; 3099 static int buflen = 0, b_prev = 0 , x_prev = -1, y_prev = -1 ; 3100 static k_status status = S_IDLE ; 3101 int x, y; 3102 3103#define GTCO_HEADER 0x80 3104#define GTCO_PROXIMITY 0x40 3105#define GTCO_START (GTCO_HEADER|GTCO_PROXIMITY) 3106#define GTCO_BUTTONMASK 0x3c 3107 3108 3109 if (buflen > 0 && ((rxc & GTCO_HEADER) != GTCO_HEADER)) { 3110 fprintf(stderr, "invalid code %d 0x%x\n", buflen, rxc); 3111 buflen = 0 ; 3112 } 3113 if (buflen == 0 && (rxc & GTCO_START) != GTCO_START) { 3114 fprintf(stderr, "invalid code 0 0x%x\n", rxc); 3115 return 0 ; /* invalid code, no action */ 3116 } 3117 3118 buf[buflen++] = rxc ; 3119 if (buflen < 5) 3120 return 0 ; 3121 3122 buflen = 0 ; /* for next time... */ 3123 3124 x = ((buf[2] & ~GTCO_START) << 6 | (buf[1] & ~GTCO_START)); 3125 y = 4768 - ((buf[4] & ~GTCO_START) << 6 | (buf[3] & ~GTCO_START)); 3126 3127 x /= 2.5; 3128 y /= 2.5; 3129 3130 act->flags = 0 ; 3131 act->obutton = act->button ; 3132 act->dx = act->dy = act->dz = 0 ; 3133 3134 if ((buf[0] & 0x40) == 0) /* pen went out of reach */ 3135 status = S_IDLE ; 3136 else if (status == S_IDLE) { /* pen is newly near the tablet */ 3137 act->flags |= MOUSE_POSCHANGED ; /* force update */ 3138 status = S_PROXY ; 3139 x_prev = x ; 3140 y_prev = y ; 3141 } 3142 3143 act->dx = x - x_prev ; 3144 act->dy = y - y_prev ; 3145 if (act->dx || act->dy) 3146 act->flags |= MOUSE_POSCHANGED ; 3147 x_prev = x ; 3148 y_prev = y ; 3149 3150 /* possibly record button change */ 3151 if (b_prev != 0 && b_prev != buf[0]) { 3152 act->button = 0 ; 3153 if (buf[0] & 0x04) /* tip pressed/yellow */ 3154 act->button |= MOUSE_BUTTON1DOWN ; 3155 if (buf[0] & 0x08) /* grey/white */ 3156 act->button |= MOUSE_BUTTON2DOWN ; 3157 if (buf[0] & 0x10) /* black/green */ 3158 act->button |= MOUSE_BUTTON3DOWN ; 3159 if (buf[0] & 0x20) /* tip+grey/blue */ 3160 act->button |= MOUSE_BUTTON4DOWN ; 3161 act->flags |= MOUSE_BUTTONSCHANGED ; 3162 } 3163 b_prev = buf[0] ; 3164 return act->flags ; 3165} 3166 3167static void 3168mremote_serversetup() 3169{ 3170 struct sockaddr_un ad; 3171 3172 /* Open a UNIX domain stream socket to listen for mouse remote clients */ 3173 unlink(_PATH_MOUSEREMOTE); 3174 3175 if ((rodent.mremsfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) 3176 logerrx(1, "unable to create unix domain socket %s",_PATH_MOUSEREMOTE); 3177 3178 umask(0111); 3179 3180 bzero(&ad, sizeof(ad)); 3181 ad.sun_family = AF_UNIX; 3182 strcpy(ad.sun_path, _PATH_MOUSEREMOTE); 3183#ifndef SUN_LEN 3184#define SUN_LEN(unp) (((char *)(unp)->sun_path - (char *)(unp)) + \ 3185 strlen((unp)->path)) 3186#endif 3187 if (bind(rodent.mremsfd, (struct sockaddr *) &ad, SUN_LEN(&ad)) < 0) 3188 logerrx(1, "unable to bind unix domain socket %s", _PATH_MOUSEREMOTE); 3189 3190 listen(rodent.mremsfd, 1); 3191} 3192 3193static void 3194mremote_clientchg(int add) 3195{ 3196 struct sockaddr_un ad; 3197 int ad_len, fd; 3198 3199 if (rodent.rtype != MOUSE_PROTO_X10MOUSEREM) 3200 return; 3201 3202 if (add) { 3203 /* Accept client connection, if we don't already have one */ 3204 ad_len = sizeof(ad); 3205 fd = accept(rodent.mremsfd, (struct sockaddr *) &ad, &ad_len); 3206 if (fd < 0) 3207 logwarnx("failed accept on mouse remote socket"); 3208 3209 if (rodent.mremcfd < 0) { 3210 rodent.mremcfd = fd; 3211 debug("remote client connect...accepted"); 3212 } 3213 else { 3214 close(fd); 3215 debug("another remote client connect...disconnected"); 3216 } 3217 } 3218 else { 3219 /* Client disconnected */ 3220 debug("remote client disconnected"); 3221 close(rodent.mremcfd); 3222 rodent.mremcfd = -1; 3223 } 3224} 3225