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