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