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