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