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