2 ** SPDX-License-Identifier: BSD-4-Clause
4 ** Copyright (c) 1995 Michael Smith, All rights reserved.
6 ** Redistribution and use in source and binary forms, with or without
7 ** modification, are permitted provided that the following conditions
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.
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.
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.
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!
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>
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 */
83 /* Abort 3-button emulation delay after this many movement events. */
84 #define BUTTON2_MAXMOVE 3
89 #define MOUSE_XAXIS (-1)
90 #define MOUSE_YAXIS (-2)
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))
98 #define ChordMiddle 0x0001
99 #define Emulate3Button 0x0002
100 #define ClearDTR 0x0004
101 #define ClearRTS 0x0008
103 #define VirtualScroll 0x0020
104 #define HVirtualScroll 0x0040
105 #define ExponentialAcc 0x0080
112 #define ID_ALL (ID_PORT | ID_IF | ID_TYPE | ID_MODEL)
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) \
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) { \
126 (vvp)->tv_nsec += 1000000000; \
130 #define debug(...) do { \
131 if (debug && nodaemon) \
132 warnx(__VA_ARGS__); \
135 #define logerr(e, ...) do { \
136 log_or_warn(LOG_DAEMON | LOG_ERR, errno, __VA_ARGS__); \
140 #define logerrx(e, ...) do { \
141 log_or_warn(LOG_DAEMON | LOG_ERR, 0, __VA_ARGS__); \
145 #define logwarn(...) \
146 log_or_warn(LOG_DAEMON | LOG_WARNING, errno, __VA_ARGS__)
148 #define logwarnx(...) \
149 log_or_warn(LOG_DAEMON | LOG_WARNING, 0, __VA_ARGS__)
153 /* symbol table entry */
160 /* serial PnP ID string */
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... */
175 /* global variables */
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;
186 #define SCROLL_NOTSCROLLING 0
187 #define SCROLL_PREPARE 1
188 #define SCROLL_SCROLLING 2
190 static int scroll_state;
191 static int scroll_movement;
192 static int hscroll_movement;
194 /* local variables */
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 },
205 /* types (the table must be ordered by MOUSE_PROTO_XXX in mouse.h) */
206 static const char *rnames[] = {
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 },
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 },
290 { "PNP0F01", MOUSE_PROTO_MS, MOUSE_MODEL_GENERIC },
292 { "PNP0F03", MOUSE_PROTO_PS2, MOUSE_MODEL_GENERIC },
294 * EzScroll returns PNP0F04 in the compatible device field; but it
295 * doesn't look compatible... XXX
298 { "PNP0F04", MOUSE_PROTO_MSC, MOUSE_MODEL_GENERIC },
300 { "PNP0F05", MOUSE_PROTO_MSC, MOUSE_MODEL_GENERIC },
303 { "PNP0F06", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC },
305 { "PNP0F07", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC },
307 /* Logitech serial */
308 { "PNP0F08", MOUSE_PROTO_LOGIMOUSEMAN, MOUSE_MODEL_GENERIC },
309 /* MS BallPoint serial */
310 { "PNP0F09", MOUSE_PROTO_MS, MOUSE_MODEL_GENERIC },
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 },
323 { "PNP0F10", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC },
326 { "PNP0F12", MOUSE_PROTO_PS2, MOUSE_MODEL_GENERIC },
328 { "PNP0F13", MOUSE_PROTO_PS2, MOUSE_MODEL_GENERIC },
331 { "PNP0F14", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC },
335 { "PNP0F16", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC },
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 },
342 /* Logitech SWIFT compatible */
343 { "PNP0F1A", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC },
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 },
353 /* Interlink VersaPad */
354 { "LNK0001", MOUSE_PROTO_VERSAPAD, MOUSE_MODEL_VERSAPAD },
356 { NULL, MOUSE_PROTO_UNKNOWN, MOUSE_MODEL_GENERIC },
359 /* the table must be ordered by MOUSE_PROTO_XXX in mouse.h */
360 static unsigned short rodentcflags[] =
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 */
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 */
380 (CS8 | CSTOPB | CREAD | CLOCAL | HUPCL), /* Mariqua */
382 (CS8 | CREAD | HUPCL ), /* GTCO Digi-Pad */
385 static struct rodentparam {
387 const char *portname; /* /dev/XXX */
388 int rtype; /* MOUSE_PROTO_XXX */
389 int level; /* operation level: 0 or greater */
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 */
415 .rtype = MOUSE_PROTO_UNKNOWN,
419 .resolution = MOUSE_RES_UNKNOWN,
420 .zmap = { 0, 0, 0, 0 },
427 .clickthreshold = DFLT_CLICKTHRESHOLD,
428 .button2timeout = DFLT_BUTTON2TIMEOUT,
435 .scrollthreshold = DFLT_SCROLLTHRESHOLD,
436 .scrollspeed = DFLT_SCROLLSPEED,
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 */
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 */
448 /* state machine for 3 button emulation */
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 */
461 #define A(b1, b3) (((b1) ? 2 : 0) | ((b3) ? 1 : 0))
463 #define S_DELAYED(st) (states[st].s[A_TIMEOUT] != (st))
466 int s[A_TIMEOUT + 1];
472 { { S0, S2, S1, S3, S0 }, 0, ~(MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN), FALSE },
474 { { S4, S2, S1, S3, S5 }, 0, ~MOUSE_BUTTON1DOWN, FALSE },
476 { { S8, S2, S1, S3, S6 }, 0, ~MOUSE_BUTTON3DOWN, FALSE },
478 { { S0, S9, S9, S3, S3 }, MOUSE_BUTTON2DOWN, ~0, FALSE },
480 { { S0, S2, S1, S3, S0 }, MOUSE_BUTTON1DOWN, ~0, TRUE },
482 { { S0, S2, S5, S7, S5 }, MOUSE_BUTTON1DOWN, ~0, FALSE },
484 { { S0, S6, S1, S7, S6 }, MOUSE_BUTTON3DOWN, ~0, FALSE },
486 { { S0, S6, S5, S7, S7 }, MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, ~0, FALSE },
488 { { S0, S2, S1, S3, S0 }, MOUSE_BUTTON3DOWN, ~0, TRUE },
490 { { S0, S9, S9, S3, S9 }, 0, ~(MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN), FALSE },
492 static int mouse_button_state;
493 static struct timespec mouse_button_state_ts;
494 static int mouse_move_delayed;
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 */
516 /* function prototypes */
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, ...)
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);
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);
548 static symtab_t *gettoken(symtab_t *tab, const char *s, int len);
549 static const char *gettokenname(symtab_t *tab, int val);
551 static void mremote_serversetup(void);
552 static void mremote_clientchg(int add);
554 static int kidspad(u_char rxc, mousestatus_t *act);
555 static int gtco_digipad(u_char, mousestatus_t *);
558 main(int argc, char *argv[])
564 for (i = 0; i < MOUSE_MAXBUTTON; ++i)
565 mstate[i] = &bstate[i];
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)
571 rodent.flags |= Emulate3Button;
575 rodent.button2timeout = atoi(optarg);
576 if ((rodent.button2timeout < 0) ||
577 (rodent.button2timeout > MAX_BUTTON2TIMEOUT)) {
578 warnx("invalid argument `%s'", optarg);
584 i = sscanf(optarg, "%f,%f", &rodent.accelx, &rodent.accely);
586 warnx("invalid linear acceleration argument '%s'", optarg);
591 rodent.accely = rodent.accelx;
596 rodent.flags |= ExponentialAcc;
597 i = sscanf(optarg, "%f,%f", &rodent.expoaccel, &rodent.expoffset);
599 warnx("invalid exponential acceleration argument '%s'", optarg);
604 rodent.expoffset = 1.0;
609 rodent.flags |= ChordMiddle;
621 if (strcmp(optarg, "all") == 0)
623 else if (strcmp(optarg, "port") == 0)
625 else if (strcmp(optarg, "if") == 0)
627 else if (strcmp(optarg, "type") == 0)
629 else if (strcmp(optarg, "model") == 0)
632 warnx("invalid argument `%s'", optarg);
639 rodent.level = atoi(optarg);
640 if ((rodent.level < 0) || (rodent.level > 4)) {
641 warnx("invalid argument `%s'", optarg);
647 if (!r_installmap(optarg)) {
648 warnx("invalid argument `%s'", optarg);
654 rodent.portname = optarg;
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;
669 rodent.resolution = atoi(optarg);
670 if (rodent.resolution <= 0) {
671 warnx("invalid argument `%s'", optarg);
678 rodent.baudrate = 9600;
683 if ((i <= 0) || (i > MOUSE_MAXBUTTON)) {
684 warnx("invalid argument `%s'", optarg);
687 rodent.wmode = 1 << (i - 1);
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;
698 * Use button i for negative Z axis movement and
699 * button (i + 1) for positive Z axis movement.
701 if ((i <= 0) || (i > MOUSE_MAXBUTTON - 1)) {
702 warnx("invalid argument `%s'", optarg);
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]))
711 i = atoi(argv[optind]);
712 if ((i <= 0) || (i > MOUSE_MAXBUTTON - 1)) {
713 warnx("invalid argument `%s'", argv[optind]);
719 if ((rodent.zmap[2] != 0) && (rodent.zmap[3] == 0))
720 rodent.zmap[3] = rodent.zmap[2] + 1;
725 rodent.clickthreshold = atoi(optarg);
726 if ((rodent.clickthreshold < 0) ||
727 (rodent.clickthreshold > MAX_CLICKTHRESHOLD)) {
728 warnx("invalid argument `%s'", optarg);
734 rodent.flags |= ClearDTR;
738 rodent.rate = atoi(optarg);
739 if (rodent.rate <= 0) {
740 warnx("invalid argument `%s'", optarg);
746 rodent.flags |= HVirtualScroll;
754 rodent.scrollspeed = atoi(optarg);
755 if (rodent.scrollspeed < 0) {
756 warnx("invalid argument `%s'", optarg);
762 rodent.flags |= NoPnP;
766 rodent.flags |= ClearRTS;
770 rodent.baudrate = atoi(optarg);
771 if (rodent.baudrate <= 0) {
772 warnx("invalid argument `%s'", optarg);
775 debug("rodent baudrate %d", rodent.baudrate);
779 drift_terminate = TRUE;
780 sscanf(optarg, "%d,%d,%d", &drift_distance, &drift_time,
782 if (drift_distance <= 0 || drift_time <= 0 || drift_after <= 0) {
783 warnx("invalid argument `%s'", optarg);
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;
797 if (strcmp(optarg, "auto") == 0) {
798 rodent.rtype = MOUSE_PROTO_UNKNOWN;
799 rodent.flags &= ~NoPnP;
803 for (i = 0; rnames[i] != NULL; i++)
804 if (strcmp(optarg, rnames[i]) == 0) {
806 rodent.flags |= NoPnP;
807 rodent.level = (i == MOUSE_PROTO_SYSMOUSE) ? 1 : 0;
810 if (rnames[i] == NULL) {
811 warnx("no such mouse type `%s'", optarg);
817 rodent.flags |= VirtualScroll;
820 rodent.scrollthreshold = atoi(optarg);
821 if (rodent.scrollthreshold < 0) {
822 warnx("invalid argument `%s'", optarg);
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];
840 rodent.zmap[i] = 1 << (rodent.zmap[i] - 1);
844 /* the default port name */
845 switch(rodent.rtype) {
847 case MOUSE_PROTO_PS2:
848 if (!rodent.portname)
849 rodent.portname = "/dev/psm0";
855 warnx("no port name specified");
859 if (strncmp(rodent.portname, "/dev/ums", 8) == 0)
860 rodent.is_removable = 1;
863 if (setjmp(env) == 0) {
865 signal(SIGINT , cleanup);
866 signal(SIGQUIT, cleanup);
867 signal(SIGTERM, cleanup);
868 signal(SIGUSR1, pause_mouse);
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);
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));
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));
900 if (rodent.mfd == -1) {
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.
911 r_init(); /* call init function */
915 if (rodent.mfd != -1)
917 if (rodent.cfd != -1)
919 rodent.mfd = rodent.cfd = -1;
920 if (rodent.is_removable)
929 * Function to calculate linear acceleration.
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.
937 linacc(int dx, int dy, int *movex, int *movey)
941 if (dx == 0 && dy == 0) {
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;
954 * Function to calculate exponential acceleration.
955 * (Also includes linear acceleration if enabled.)
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.
963 expoacc(int dx, int dy, int *movex, int *movey)
965 static float lastlength[3] = {0.0, 0.0, 0.0};
966 float fdx, fdy, length, lbase, accel;
968 if (dx == 0 && dy == 0) {
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! */
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;
1004 if ((rodent.cfd = open("/dev/consolectl", O_RDWR, 0)) == -1)
1005 logerr(1, "cannot open /dev/consolectl");
1007 if (!nodaemon && !background) {
1008 pfh = pidfile_open(pidfile, 0600, &mpid);
1010 if (errno == EEXIST)
1011 logerrx(1, "moused already running, pid: %d", mpid);
1012 logwarn("cannot open pid file");
1015 int saved_errno = errno;
1016 pidfile_remove(pfh);
1017 errno = saved_errno;
1018 logerr(1, "failed to become a daemon");
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;
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;
1042 /* choose which ioctl command to use */
1043 mouse.operation = MOUSE_MOTION_EVENT;
1044 extioctl = (ioctl(rodent.cfd, CONS_MOUSECTL, &mouse) == 0);
1046 /* process mouse data */
1048 timeout.tv_usec = 20000; /* 20 msec */
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);
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");
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)) {
1071 debug("flags:%08x buttons:%08x obuttons:%08x",
1072 action.flags, action.button, action.obutton);
1074 action0.obutton = action0.button;
1078 /* MouseRemote client connect/disconnect */
1079 if ((rodent.mremsfd >= 0) && FD_ISSET(rodent.mremsfd, &fds)) {
1080 mremote_clientchg(TRUE);
1083 if ((rodent.mremcfd >= 0) && FD_ISSET(rodent.mremcfd, &fds)) {
1084 mremote_clientchg(FALSE);
1087 /* mouse movement */
1088 if (read(rodent.mfd, &b, 1) == -1) {
1089 if (errno == EWOULDBLOCK)
1094 if ((flags = r_protocol(b, &action0)) == 0)
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");
1105 debug("[BUTTON2] flags:%08x buttons:%08x obuttons:%08x",
1106 action.flags, action.button, action.obutton);
1108 debug("[NOTBUTTON2] flags:%08x buttons:%08x obuttons:%08x",
1109 action.flags, action.button, action.obutton);
1111 /* This isn't a middle button down... move along... */
1112 if (scroll_state == SCROLL_SCROLLING) {
1114 * We were scrolling, someone let go of button 2.
1115 * Now turn autoscroll off.
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;
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));
1128 /* Send middle down */
1129 newaction.button = MOUSE_BUTTON2DOWN;
1130 r_click(&newaction);
1132 /* Send middle up */
1133 r_timestamp(&newaction);
1134 newaction.obutton = newaction.button;
1135 newaction.button = action0.button;
1136 r_click(&newaction);
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);
1148 action0.obutton = action0.button;
1149 flags &= MOUSE_POSCHANGED;
1150 flags |= action.obutton ^ action.button;
1151 action.flags = flags;
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);
1158 if ((rodent.flags & VirtualScroll) || (rodent.flags & HVirtualScroll)) {
1160 * If *only* the middle button is pressed AND we are moving
1161 * the stick/trackpoint/nipple, scroll!
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;
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;
1182 if (scroll_state == SCROLL_SCROLLING) scroll_movement = hscroll_movement = 0;
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) {
1191 scroll_movement = 0;
1193 else if (scroll_movement > rodent.scrollspeed) {
1196 scroll_movement = 0;
1199 if (rodent.flags & HVirtualScroll) {
1200 hscroll_movement += action2.dx;
1201 debug("HORIZONTAL SCROLL: %d", hscroll_movement);
1203 if (hscroll_movement < -rodent.scrollspeed) {
1205 hscroll_movement = 0;
1207 else if (hscroll_movement > rodent.scrollspeed) {
1209 hscroll_movement = 0;
1213 /* Don't move while scrolling */
1214 action2.dx = action2.dy = 0;
1218 if (drift_terminate) {
1219 if ((flags & MOUSE_POSCHANGED) == 0 || action.dz || action2.dz)
1220 drift_last_activity = drift_current_ts;
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;
1230 /* discard old accumulated steps (drift) */
1231 if (tscmp(&drift_tmp, &drift_2time_ts, >))
1232 drift_previous.x = drift_previous.y = 0;
1234 drift_previous = drift_last;
1235 drift_last.x = action2.dx;
1236 drift_last.y = action2.dy;
1237 drift_since = drift_current_ts;
1239 if (abs(drift_last.x) + abs(drift_last.y)
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;
1250 continue; /* don't pass current movement to
1258 /* Defer clicks until we aren't VirtualScroll'ing. */
1259 if (scroll_state == SCROLL_NOTSCROLLING)
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);
1270 linacc(action2.dx, action2.dy,
1271 &mouse.u.data.x, &mouse.u.data.y);
1273 mouse.u.data.z = action2.dz;
1276 ioctl(rodent.cfd, CONS_MOUSECTL, &mouse);
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);
1286 linacc(action2.dx, action2.dy,
1287 &mouse.u.data.x, &mouse.u.data.y);
1289 mouse.u.data.z = action2.dz;
1292 ioctl(rodent.cfd, CONS_MOUSECTL, &mouse);
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.
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);
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;
1315 ioctl(rodent.cfd, CONS_MOUSECTL, &mouse);
1324 hup(__unused int sig)
1330 cleanup(__unused int sig)
1332 if (rodent.rtype == MOUSE_PROTO_X10MOUSEREM)
1333 unlink(_PATH_MOUSEREMOTE);
1338 pause_mouse(__unused int sig)
1346 ** Complain, and free the CPU for more worthy tasks
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>");
1361 * Output an error message to syslog or stderr as appropriate. If
1362 * `errnum' is non-zero, append its string form to the message.
1365 log_or_warn(int log_pri, int errnum, const char *fmt, ...)
1371 vsnprintf(buf, sizeof(buf), fmt, ap);
1374 strlcat(buf, ": ", sizeof(buf));
1375 strlcat(buf, strerror(errnum), sizeof(buf));
1379 syslog(log_pri, "%s", buf);
1385 ** Mouse interface code, courtesy of XFree86 3.1.2.
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.
1393 ** The following code is derived from a module marked :
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 $ */
1401 * Copyright 1990,91 by Thomas Roell, Dinkelscherben, Germany.
1402 * Copyright 1993 by David Dawes <dawes@physics.su.oz.au>
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
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.
1426 ** GlidePoint support from XFree86 3.2.
1427 ** Derived from the module:
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 $ */
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 */
1454 { 0xf8, 0x80, 0x00, 0x00, 5, ~0x2f, 0x10 }, /* Mariqua */
1457 static unsigned char cur_proto[7];
1462 char pnpbuf[256]; /* PnP identifier string may be up to 256 bytes long */
1468 /* set the driver operation level, if applicable */
1469 if (rodent.level < 0)
1471 ioctl(rodent.mfd, MOUSE_SETLEVEL, &rodent.level);
1472 rodent.level = (ioctl(rodent.mfd, MOUSE_GETLEVEL, &level) == 0) ? level : 0;
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.
1480 rodent.hw.iftype = MOUSE_IF_UNKNOWN;
1481 rodent.hw.model = MOUSE_MODEL_GENERIC;
1482 ioctl(rodent.mfd, MOUSE_GETHWINFO, &rodent.hw);
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);
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));
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 */
1512 /* maybe this is a PnP mouse... */
1513 if (rodent.mode.protocol == MOUSE_PROTO_UNKNOWN) {
1515 if (rodent.flags & NoPnP)
1516 return (rodent.rtype);
1517 if (((len = pnpgets(pnpbuf)) <= 0) || !pnpparse(&pnpid, pnpbuf, len))
1518 return (rodent.rtype);
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);
1525 /* we have a valid PnP serial device ID */
1526 rodent.hw.iftype = MOUSE_IF_SERIAL;
1527 t = pnpproto(&pnpid);
1529 rodent.mode.protocol = t->val;
1530 rodent.hw.model = t->val2;
1532 rodent.mode.protocol = MOUSE_PROTO_UNKNOWN;
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));
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]);
1553 return (rodent.rtype);
1560 return (gettokenname(rifs, iftype));
1566 const char *unknown = "unknown";
1568 return (type == MOUSE_PROTO_UNKNOWN ||
1569 type >= (int)(sizeof(rnames) / sizeof(rnames[0])) ?
1570 unknown : rnames[type]);
1577 return (gettokenname(rmodels, model));
1583 unsigned char buf[16]; /* scrach buffer */
1590 ** This comment is a little out of context here, but it contains
1591 ** some useful information...
1592 ********************************************************************
1594 ** The following lines take care of the Logitech MouseMan protocols.
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.
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.
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.
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".
1623 ** The following baud rates are supported:
1624 ** - 1200 Baud (default). Selected by sequence "*n".
1625 ** - 9600 Baud. Selected by sequence "*q".
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.)
1632 switch (rodent.rtype) {
1634 case MOUSE_PROTO_LOGI:
1636 * The baud rate selection command must be sent at the current
1637 * baud rate; try all likely settings
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);
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]);
1665 case MOUSE_PROTO_HITTAB:
1666 setmousespeed(1200, rodent.baudrate, rodentcflags[rodent.rtype]);
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"
1675 write(rodent.mfd, "z8", 2); /* Set Parity = "NONE" */
1677 write(rodent.mfd, "zb", 2); /* Set Format = "Binary" */
1679 write(rodent.mfd, "@", 1); /* Set Report Mode = "Stream" */
1681 write(rodent.mfd, "R", 1); /* Set Output Rate = "45 rps" */
1683 write(rodent.mfd, "I\x20", 2); /* Set Incrememtal Mode "20" */
1685 write(rodent.mfd, "E", 1); /* Set Data Type = "Relative */
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';
1699 write(rodent.mfd, &c, 1);
1702 write(rodent.mfd, "\021", 1); /* Resume DATA output */
1705 case MOUSE_PROTO_THINK:
1706 setmousespeed(1200, rodent.baudrate, rodentcflags[rodent.rtype]);
1707 /* the PnP ID string may be sent again, discard it */
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);
1715 FD_SET(rodent.mfd, &fds);
1716 if (select(FD_SETSIZE, &fds, NULL, NULL, NULL) <= 0)
1718 read(rodent.mfd, &c, 1);
1725 case MOUSE_PROTO_JOGDIAL:
1727 case MOUSE_PROTO_MSC:
1728 setmousespeed(1200, rodent.baudrate, rodentcflags[rodent.rtype]);
1729 if (rodent.flags & ClearDTR) {
1731 ioctl(rodent.mfd, TIOCMBIC, &i);
1733 if (rodent.flags & ClearRTS) {
1735 ioctl(rodent.mfd, TIOCMBIC, &i);
1739 case MOUSE_PROTO_SYSMOUSE:
1740 if (rodent.hw.iftype == MOUSE_IF_SYSMOUSE)
1741 setmousespeed(1200, rodent.baudrate, rodentcflags[rodent.rtype]);
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);
1752 case MOUSE_PROTO_X10MOUSEREM:
1753 mremote_serversetup();
1754 setmousespeed(1200, rodent.baudrate, rodentcflags[rodent.rtype]);
1758 case MOUSE_PROTO_VERSAPAD:
1759 tcsendbreak(rodent.mfd, 0); /* send break for 400 msec */
1761 ioctl(rodent.mfd, TIOCFLUSH, &i);
1762 for (i = 0; i < 7; ++i) {
1764 FD_SET(rodent.mfd, &fds);
1765 if (select(FD_SETSIZE, &fds, NULL, NULL, NULL) <= 0)
1767 read(rodent.mfd, &c, 1);
1771 if ((buf[0] != 'V') || (buf[1] != 'P')|| (buf[7] != '\r'))
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) {
1777 FD_SET(rodent.mfd, &fds);
1778 if (select(FD_SETSIZE, &fds, NULL, NULL, NULL) <= 0)
1780 read(rodent.mfd, &c, 1);
1786 ioctl(rodent.mfd, TIOCFLUSH, &i);
1790 setmousespeed(1200, rodent.baudrate, rodentcflags[rodent.rtype]);
1796 r_protocol(u_char rBuf, mousestatus_t *act)
1798 /* MOUSE_MSS_BUTTON?DOWN -> MOUSE_BUTTON?DOWN */
1799 static int butmapmss[4] = { /* Microsoft, MouseMan, GlidePoint,
1800 IntelliMouse, Thinking Mouse */
1804 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1806 static int butmapmss2[4] = { /* Microsoft, MouseMan, GlidePoint,
1811 MOUSE_BUTTON2DOWN | MOUSE_BUTTON4DOWN,
1813 /* MOUSE_INTELLI_BUTTON?DOWN -> MOUSE_BUTTON?DOWN */
1814 static int butmapintelli[4] = { /* IntelliMouse, NetMouse, Mie Mouse,
1819 MOUSE_BUTTON2DOWN | MOUSE_BUTTON4DOWN,
1821 /* MOUSE_MSC_BUTTON?UP -> MOUSE_BUTTON?DOWN */
1822 static int butmapmsc[8] = { /* MouseSystems, MMSeries, Logitech,
1827 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
1829 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1830 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
1831 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
1833 /* MOUSE_PS2_BUTTON?DOWN -> MOUSE_BUTTON?DOWN */
1834 static int butmapps2[8] = { /* PS/2 */
1838 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1840 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
1841 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
1842 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
1844 /* for Hitachi tablet */
1845 static int butmaphit[8] = { /* MM HitTablet */
1855 /* for serial VersaPad */
1856 static int butmapversa[8] = { /* VersaPad */
1863 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1864 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1866 /* for PS/2 VersaPad */
1867 static int butmapversaps2[8] = { /* VersaPad */
1873 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1875 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1877 static int pBufP = 0;
1878 static unsigned char pBuf[8];
1879 static int prev_x, prev_y;
1880 static int on = FALSE;
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));
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
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
1911 if (pBufP != 0 && rodent.rtype != MOUSE_PROTO_PS2 &&
1912 ((rBuf & cur_proto[2]) != cur_proto[3] || rBuf == 0x80))
1914 pBufP = 0; /* skip package */
1917 if (pBufP == 0 && (rBuf & cur_proto[0]) != cur_proto[1])
1920 /* is there an extra data byte? */
1921 if (pBufP >= cur_proto[4] && (rBuf & cur_proto[0]) != cur_proto[1])
1924 * Hack for Logitech MouseMan Mouse - Middle button
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.
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.
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.
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.
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
1957 if ((rBuf & cur_proto[5]) != cur_proto[6]) {
1962 switch (rodent.rtype) {
1964 case MOUSE_PROTO_MARIQUA:
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.
1973 act->dx = act->dy = act->dz = 0;
1974 act->obutton = act->button;
1976 act->button = (1 << (rBuf - 13))
1977 | (act->obutton & (MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN));
1979 * FIXME: this is a button "down" event. There needs to be
1980 * a corresponding button "up" event... XXX
1984 case MOUSE_PROTO_JOGDIAL:
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
1995 case MOUSE_PROTO_INTELLI: /* IntelliMouse, NetMouse, Mie Mouse,
1997 act->dx = act->dy = 0;
1998 act->dz = (rBuf & 0x08) ? (rBuf & 0x0f) - 16 : (rBuf & 0x0f);
1999 if ((act->dz >= 7) || (act->dz <= -7))
2001 act->obutton = act->button;
2002 act->button = butmapintelli[(rBuf & MOUSE_MSS_BUTTONS) >> 4]
2003 | (act->obutton & (MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN));
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));
2014 act->flags = ((act->dx || act->dy || act->dz) ? MOUSE_POSCHANGED : 0)
2015 | (act->obutton ^ act->button);
2017 return (act->flags);
2020 if (pBufP >= cur_proto[4])
2022 pBuf[pBufP++] = rBuf;
2023 if (pBufP != cur_proto[4])
2027 * assembly full package
2030 debug("assembled full packet (len %d) %x,%x,%x,%x,%x,%x,%x,%x",
2032 pBuf[0], pBuf[1], pBuf[2], pBuf[3],
2033 pBuf[4], pBuf[5], pBuf[6], pBuf[7]);
2036 act->obutton = act->button;
2037 switch (rodent.rtype)
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)
2046 : butmapmss[(pBuf[0] & MOUSE_MSS_BUTTONS) >> 4];
2048 act->button |= (act->obutton & MOUSE_BUTTON2DOWN)
2049 | butmapmss[(pBuf[0] & MOUSE_MSS_BUTTONS) >> 4];
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) |
2057 write(rodent.mremcfd, &key, 1);
2062 act->dx = (signed char)(((pBuf[0] & 0x03) << 6) | (pBuf[1] & 0x3F));
2063 act->dy = (signed char)(((pBuf[0] & 0x0C) << 4) | (pBuf[2] & 0x3F));
2066 case MOUSE_PROTO_GLIDEPOINT: /* GlidePoint */
2067 case MOUSE_PROTO_THINK: /* ThinkingMouse */
2068 case MOUSE_PROTO_INTELLI: /* IntelliMouse, NetMouse, Mie Mouse,
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));
2076 case MOUSE_PROTO_MSC: /* MouseSystems Corp */
2078 case MOUSE_PROTO_MARIQUA: /* Mariqua */
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]));
2085 case MOUSE_PROTO_JOGDIAL: /* JogDial */
2091 act->button = MOUSE_BUTTON1DOWN;
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];
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];
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)) {
2117 x = (pBuf[2] << 6) | pBuf[1];
2120 y = (pBuf[4] << 6) | pBuf[3];
2124 act->dx = prev_x - x;
2125 act->dy = prev_y - y;
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];
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.
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;
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))
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;
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)) {
2175 /* wheel data packet */
2176 act->dx = act->dy = 0;
2177 if (pBuf[2] & 0x80) {
2178 /* horizontal roller count - ignore it XXX*/
2180 /* vertical roller count */
2181 act->dz = (pBuf[2] & MOUSE_PS2PLUS_ZNEG)
2182 ? (pBuf[2] & 0x0f) - 16 : (pBuf[2] & 0x0f);
2184 act->button |= (pBuf[2] & MOUSE_PS2PLUS_BUTTON4DOWN)
2185 ? MOUSE_BUTTON4DOWN : 0;
2186 act->button |= (pBuf[2] & MOUSE_PS2PLUS_BUTTON5DOWN)
2187 ? MOUSE_BUTTON5DOWN : 0;
2190 /* this packet type is reserved by Logitech */
2192 * IBM ScrollPoint Mouse uses this packet type to
2193 * encode both vertical and horizontal scroll movement.
2195 act->dx = act->dy = 0;
2196 /* horizontal roller count */
2198 act->dz = (pBuf[2] & MOUSE_SPOINT_WNEG) ? -2 : 2;
2199 /* vertical roller count */
2201 act->dz = (pBuf[2] & MOUSE_SPOINT_ZNEG) ? -1 : 1;
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);
2213 /* device type packet - shouldn't happen */
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]);
2224 /* preserve button states */
2225 act->button |= act->obutton & MOUSE_EXTBUTTONS;
2228 case MOUSE_MODEL_GLIDEPOINT:
2229 /* `tapping' action */
2230 act->button |= ((pBuf[0] & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
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];
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;
2244 case MOUSE_MODEL_VERSAPAD:
2245 act->button = butmapversaps2[pBuf[0] & MOUSE_PS2VERSA_BUTTONS];
2247 (pBuf[0] & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
2248 act->dx = act->dy = 0;
2249 if (!(pBuf[0] & MOUSE_PS2VERSA_IN_USE)) {
2253 x = ((pBuf[4] << 8) & 0xf00) | pBuf[1];
2256 y = ((pBuf[4] << 4) & 0xf00) | pBuf[2];
2260 act->dx = prev_x - x;
2261 act->dy = prev_y - y;
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) {
2278 case 0x40: /* 2nd wheel rolling right XXX */
2281 case 0xc0: /* 2nd wheel rolling left XXX */
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);
2294 /* preserve previous button states */
2295 act->button |= act->obutton & MOUSE_EXTBUTTONS;
2298 case MOUSE_MODEL_GENERIC:
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);
2318 * We don't reset pBufP here yet, as there may be an additional data
2319 * byte in some protocols. See above.
2322 /* has something changed? */
2323 act->flags = ((act->dx || act->dy || act->dz) ? MOUSE_POSCHANGED : 0)
2324 | (act->obutton ^ act->button);
2326 return (act->flags);
2330 r_statetrans(mousestatus_t *a1, mousestatus_t *a2, int trans)
2338 a2->obutton = a2->button;
2339 a2->button = a1->button;
2340 a2->flags = a1->flags;
2343 if (rodent.flags & Emulate3Button) {
2345 debug("state:%d, trans:%d -> state:%d",
2346 mouse_button_state, trans,
2347 states[mouse_button_state].s[trans]);
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.
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];
2362 a2->dx = a2->dy = 0;
2364 mouse_move_delayed = 0;
2365 if (mouse_button_state != states[mouse_button_state].s[trans])
2368 clock_gettime(CLOCK_MONOTONIC_FAST, &mouse_button_state_ts);
2369 mouse_button_state = states[mouse_button_state].s[trans];
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;
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,
2406 r_installmap(char *arg)
2413 arg = skipspace(arg);
2415 while (isdigit(*arg))
2417 arg = skipspace(arg);
2418 if ((arg <= s) || (*arg != '='))
2422 arg = skipspace(++arg);
2424 while (isdigit(*arg))
2426 if ((arg <= s) || (!isspace(*arg) && (*arg != '\0')))
2430 if ((lbutton <= 0) || (lbutton > MOUSE_MAXBUTTON))
2432 if ((pbutton <= 0) || (pbutton > MOUSE_MAXBUTTON))
2434 p2l[pbutton - 1] = 1 << (lbutton - 1);
2435 mstate[lbutton - 1] = &bstate[pbutton - 1];
2442 r_map(mousestatus_t *act1, mousestatus_t *act2)
2445 register int pbuttons;
2448 pbuttons = act1->button;
2451 act2->obutton = act2->button;
2452 if (pbuttons & rodent.wmode) {
2453 pbuttons &= ~rodent.wmode;
2454 act1->dz = act1->dy;
2458 act2->dx = act1->dx;
2459 act2->dy = act1->dy;
2460 act2->dz = act1->dz;
2462 switch (rodent.zmap[0]) {
2463 case 0: /* do nothing */
2466 if (act1->dz != 0) {
2467 act2->dx = act1->dz;
2472 if (act1->dz != 0) {
2473 act2->dy = act1->dz;
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;
2497 for (pb = 0; (pb < MOUSE_MAXBUTTON) && (pbuttons != 0); ++pb) {
2498 lbuttons |= (pbuttons & 1) ? p2l[pb] : 0;
2501 act2->button = lbuttons;
2503 act2->flags = ((act2->dx || act2->dy || act2->dz) ? MOUSE_POSCHANGED : 0)
2504 | (act2->obutton ^ act2->button);
2508 r_timestamp(mousestatus_t *act)
2511 struct timespec ts1;
2512 struct timespec ts2;
2513 struct timespec ts3;
2518 mask = act->flags & MOUSE_BUTTONS;
2524 clock_gettime(CLOCK_MONOTONIC_FAST, &ts1);
2525 drift_current_ts = ts1;
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);
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);
2538 button = MOUSE_BUTTON1DOWN;
2539 for (i = 0; (i < MOUSE_MAXBUTTON) && (mask != 0); ++i) {
2541 if (act->button & button) {
2542 /* the button is down */
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;
2552 /* the button is up */
2556 if (act->button & button) {
2557 /* the button has been down */
2558 if (tscmp(&ts3, &bstate[i].ts, >)) {
2559 bstate[i].count = 1;
2561 act->flags |= button;
2562 debug("button %d timeout", i + 1);
2565 /* the button has been up */
2577 struct timespec ts1;
2578 struct timespec ts2;
2580 if (states[mouse_button_state].timeout)
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, >));
2590 r_click(mousestatus_t *act)
2592 struct mouse_info mouse;
2597 mask = act->flags & MOUSE_BUTTONS;
2601 button = MOUSE_BUTTON1DOWN;
2602 for (i = 0; (i < MOUSE_MAXBUTTON) && (mask != 0); ++i) {
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;
2609 /* the button is up */
2610 mouse.u.event.value = 0;
2612 mouse.operation = MOUSE_BUTTON_EVENT;
2613 mouse.u.event.id = button;
2616 ioctl(rodent.cfd, CONS_MOUSECTL, &mouse);
2617 debug("button %d count %d", i + 1, mouse.u.event.value);
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 $ */
2627 * Copyright 1993 by David Dawes <dawes@physics.su.oz.au>
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
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.
2652 setmousespeed(int old, int new, unsigned cflag)
2657 if (tcgetattr(rodent.mfd, &tty) < 0)
2659 logwarn("unable to get status of mouse fd");
2663 tty.c_iflag = IGNBRK | IGNPAR;
2666 tty.c_cflag = (tcflag_t)cflag;
2667 tty.c_cc[VTIME] = 0;
2673 cfsetispeed(&tty, B9600);
2674 cfsetospeed(&tty, B9600);
2677 cfsetispeed(&tty, B4800);
2678 cfsetospeed(&tty, B4800);
2681 cfsetispeed(&tty, B2400);
2682 cfsetospeed(&tty, B2400);
2686 cfsetispeed(&tty, B1200);
2687 cfsetospeed(&tty, B1200);
2690 if (tcsetattr(rodent.mfd, TCSADRAIN, &tty) < 0)
2692 logwarn("unable to set status of mouse fd");
2700 cfsetispeed(&tty, B9600);
2701 cfsetospeed(&tty, B9600);
2705 cfsetispeed(&tty, B4800);
2706 cfsetospeed(&tty, B4800);
2710 cfsetispeed(&tty, B2400);
2711 cfsetospeed(&tty, B2400);
2716 cfsetispeed(&tty, B1200);
2717 cfsetospeed(&tty, B1200);
2720 if (rodent.rtype == MOUSE_PROTO_LOGIMOUSEMAN
2721 || rodent.rtype == MOUSE_PROTO_LOGI)
2723 if (write(rodent.mfd, c, 2) != 2)
2725 logwarn("unable to write to mouse fd");
2731 if (tcsetattr(rodent.mfd, TCSADRAIN, &tty) < 0)
2732 logwarn("unable to set status of mouse fd");
2736 * PnP COM device support
2738 * It's a simplistic implementation, but it works :-)
2743 * Try to elicit a PnP ID as described in
2744 * Microsoft, Hayes: "Plug and Play External COM Device Specification,
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
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
2761 struct timeval timeout;
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...
2770 debug("PnP COM device rev 1.0 probe...");
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);
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...
2785 ioctl(rodent.mfd, TIOCMGET, &i);
2786 debug("modem status 0%o", i);
2787 if ((i & TIOCM_DSR) == 0)
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);
2795 i = TIOCM_DTR; /* DTR = 1, RTS = 0 */
2796 ioctl(rodent.mfd, TIOCMBIS, &i);
2799 /* wait for response, 1st phase (2.1.4) */
2801 ioctl(rodent.mfd, TIOCFLUSH, &i);
2802 i = TIOCM_RTS; /* DTR = 1, RTS = 1 */
2803 ioctl(rodent.mfd, TIOCMBIS, &i);
2805 /* try to read something */
2807 FD_SET(rodent.mfd, &fds);
2809 timeout.tv_usec = 240000;
2810 if (select(FD_SETSIZE, &fds, NULL, NULL, &timeout) > 0) {
2811 debug("pnpwakeup1(): valid response in first phase.");
2815 /* port setup, 2nd phase (2.1.5) */
2816 i = TIOCM_DTR | TIOCM_RTS; /* DTR = 0, RTS = 0 */
2817 ioctl(rodent.mfd, TIOCMBIC, &i);
2820 /* wait for respose, 2nd phase (2.1.6) */
2822 ioctl(rodent.mfd, TIOCFLUSH, &i);
2823 i = TIOCM_DTR | TIOCM_RTS; /* DTR = 1, RTS = 1 */
2824 ioctl(rodent.mfd, TIOCMBIS, &i);
2826 /* try to read something */
2828 FD_SET(rodent.mfd, &fds);
2830 timeout.tv_usec = 240000;
2831 if (select(FD_SETSIZE, &fds, NULL, NULL, &timeout) > 0) {
2832 debug("pnpwakeup1(): valid response in second phase.");
2842 struct timeval timeout;
2847 * This is a simplified procedure; it simply toggles RTS.
2849 debug("alternate probe...");
2851 ioctl(rodent.mfd, TIOCMGET, &i);
2852 i |= TIOCM_DTR; /* DTR = 1 */
2853 i &= ~TIOCM_RTS; /* RTS = 0 */
2854 ioctl(rodent.mfd, TIOCMSET, &i);
2857 setmousespeed(1200, 1200, (CS7 | CREAD | CLOCAL | HUPCL));
2859 /* wait for respose */
2861 ioctl(rodent.mfd, TIOCFLUSH, &i);
2862 i = TIOCM_DTR | TIOCM_RTS; /* DTR = 1, RTS = 1 */
2863 ioctl(rodent.mfd, TIOCMBIS, &i);
2865 /* try to read something */
2867 FD_SET(rodent.mfd, &fds);
2869 timeout.tv_usec = 240000;
2870 if (select(FD_SETSIZE, &fds, NULL, NULL, &timeout) > 0) {
2871 debug("pnpwakeup2(): valid response.");
2881 struct timeval timeout;
2887 if (!pnpwakeup1() && !pnpwakeup2()) {
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.
2894 i = TIOCM_DTR | TIOCM_RTS; /* DTR = 1, RTS = 1 */
2895 ioctl(rodent.mfd, TIOCMBIS, &i);
2899 /* collect PnP COM device ID (2.1.7) */
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' */
2906 if ((c == 0x08) || (c == 0x28)) { /* Begin ID */
2907 debug("begin-id %02x", c);
2911 debug("%c %02x", c, c);
2916 /* we haven't seen `Begin ID' in time... */
2920 ++c; /* make it `End ID' */
2923 FD_SET(rodent.mfd, &fds);
2925 timeout.tv_usec = 240000;
2926 if (select(FD_SETSIZE, &fds, NULL, NULL, &timeout) <= 0)
2929 read(rodent.mfd, &buf[i], 1);
2930 if (buf[i++] == c) /* End ID */
2937 bcopy(&buf[begin], &buf[0], i);
2939 /* string may not be human readable... */
2940 debug("len:%d, '%-*.*s'", i, i, i, buf);
2942 if (buf[i - 1] == c)
2943 return (i); /* a valid PnP string */
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.
2952 /* we may still have something in the buffer */
2957 pnpparse(pnpid_t *id, char *buf, int len)
2969 id->description = NULL;
2974 id->ndescription = 0;
2976 if ((buf[0] != 0x28) && (buf[0] != 0x08)) {
2981 case 'M': /* Microsoft */
2982 id->eisaid = "PNP0F01";
2984 case 'H': /* MouseSystems */
2985 id->eisaid = "PNP0F04";
2988 id->neisaid = strlen(id->eisaid);
2989 id->class = "MOUSE";
2990 id->nclass = strlen(id->class);
2991 debug("non-PnP mouse '%c'", buf[0]);
2996 offset = 0x28 - buf[0];
2998 /* calculate checksum */
2999 for (i = 0; i < len - 3; ++i) {
3003 sum += buf[len - 1];
3004 for (; i < len; ++i)
3006 debug("PnP ID string: '%*.*s'", len, len, buf);
3011 id->revision = ((buf[1] & 0x3f) << 6) | (buf[2] & 0x3f);
3012 debug("PnP rev %d.%02d", id->revision / 100, id->revision % 100);
3014 /* EISA vendor and product ID */
3015 id->eisaid = &buf[3];
3018 /* option strings */
3020 if (buf[i] == '\\') {
3021 /* device serial # */
3022 for (j = ++i; i < len; ++i) {
3029 id->serial = &buf[j];
3033 if (buf[i] == '\\') {
3035 for (j = ++i; i < len; ++i) {
3042 id->class = &buf[j];
3046 if (buf[i] == '\\') {
3047 /* compatible driver */
3048 for (j = ++i; i < len; ++i) {
3053 * PnP COM spec prior to v0.96 allowed '*' in this field,
3054 * it's not allowed now; just ignore it.
3061 id->compat = &buf[j];
3062 id->ncompat = i - j;
3065 if (buf[i] == '\\') {
3066 /* product description */
3067 for (j = ++i; i < len; ++i) {
3074 id->description = &buf[j];
3075 id->ndescription = i - j;
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) {
3087 * I found some mice do not comply with the PnP COM device
3088 * spec regarding checksum... XXX
3090 logwarnx("PnP checksum error", 0);
3100 pnpproto(pnpid_t *id)
3106 if (strncmp(id->class, "MOUSE", id->nclass) != 0 &&
3107 strncmp(id->class, "TABLET", id->nclass) != 0)
3108 /* this is not a mouse! */
3111 if (id->neisaid > 0) {
3112 t = gettoken(pnpprod, id->eisaid, id->neisaid);
3113 if (t->val != MOUSE_PROTO_UNKNOWN)
3118 * The 'Compatible drivers' field may contain more than one
3119 * ID separated by ','.
3121 if (id->ncompat <= 0)
3123 for (i = 0; i < id->ncompat; ++i) {
3124 for (j = i; id->compat[i] != ','; ++i)
3125 if (i >= id->ncompat)
3128 t = gettoken(pnpprod, id->compat + j, i - j);
3129 if (t->val != MOUSE_PROTO_UNKNOWN)
3137 /* name/val mapping */
3140 gettoken(symtab_t *tab, const char *s, int len)
3144 for (i = 0; tab[i].name != NULL; ++i) {
3145 if (strncmp(tab[i].name, s, len) == 0)
3152 gettokenname(symtab_t *tab, int val)
3154 static const char unknown[] = "unknown";
3157 for (i = 0; tab[i].name != NULL; ++i) {
3158 if (tab[i].val == val)
3159 return (tab[i].name);
3166 * code to read from the Genius Kidspad tablet.
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.
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.
3178 The next 4 bytes are used for coordinates xl, xh, yl, yh (7 bits valid).
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,
3188 S_IDLE, S_PROXY, S_FIRST, S_DOWN, S_UP
3192 kidspad(u_char rxc, mousestatus_t *act)
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;
3201 if (buflen > 0 && (rxc & 0x80)) {
3202 fprintf(stderr, "invalid code %d 0x%x\n", buflen, rxc);
3205 if (buflen == 0 && (rxc & 0xb8) != 0xb8) {
3206 fprintf(stderr, "invalid code 0 0x%x\n", rxc);
3207 return (0); /* invalid code, no action */
3209 buf[buflen++] = rxc;
3213 buflen = 0; /* for next time... */
3215 x = buf[1]+128*(buf[2] - 7);
3217 y = 28*128 - (buf[3] + 128* (buf[4] - 7));
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 */
3229 else if (status == S_IDLE) { /* pen is newly near the tablet */
3230 act->flags |= MOUSE_POSCHANGED; /* force update */
3235 act->dx = x - x_prev;
3236 act->dy = y - y_prev;
3237 if (act->dx || act->dy)
3238 act->flags |= MOUSE_POSCHANGED;
3241 if (b_prev != 0 && b_prev != buf[0]) { /* possibly record button change */
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;
3250 return (act->flags);
3254 gtco_digipad (u_char rxc, mousestatus_t *act)
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;
3261 #define GTCO_HEADER 0x80
3262 #define GTCO_PROXIMITY 0x40
3263 #define GTCO_START (GTCO_HEADER|GTCO_PROXIMITY)
3264 #define GTCO_BUTTONMASK 0x3c
3267 if (buflen > 0 && ((rxc & GTCO_HEADER) != GTCO_HEADER)) {
3268 fprintf(stderr, "invalid code %d 0x%x\n", buflen, rxc);
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 */
3276 buf[buflen++] = rxc;
3280 buflen = 0; /* for next time... */
3282 x = ((buf[2] & ~GTCO_START) << 6 | (buf[1] & ~GTCO_START));
3283 y = 4768 - ((buf[4] & ~GTCO_START) << 6 | (buf[3] & ~GTCO_START));
3289 act->obutton = act->button;
3290 act->dx = act->dy = act->dz = 0;
3292 if ((buf[0] & 0x40) == 0) /* pen went out of reach */
3294 else if (status == S_IDLE) { /* pen is newly near the tablet */
3295 act->flags |= MOUSE_POSCHANGED; /* force update */
3301 act->dx = x - x_prev;
3302 act->dy = y - y_prev;
3303 if (act->dx || act->dy)
3304 act->flags |= MOUSE_POSCHANGED;
3308 /* possibly record button change */
3309 if (b_prev != 0 && b_prev != buf[0]) {
3311 if (buf[0] & 0x04) {
3312 /* tip pressed/yellow */
3313 act->button |= MOUSE_BUTTON1DOWN;
3315 if (buf[0] & 0x08) {
3317 act->button |= MOUSE_BUTTON2DOWN;
3319 if (buf[0] & 0x10) {
3321 act->button |= MOUSE_BUTTON3DOWN;
3323 if (buf[0] & 0x20) {
3325 act->button |= MOUSE_BUTTON4DOWN;
3327 act->flags |= MOUSE_BUTTONSCHANGED;
3330 return (act->flags);
3334 mremote_serversetup(void)
3336 struct sockaddr_un ad;
3338 /* Open a UNIX domain stream socket to listen for mouse remote clients */
3339 unlink(_PATH_MOUSEREMOTE);
3341 if ((rodent.mremsfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
3342 logerrx(1, "unable to create unix domain socket %s",_PATH_MOUSEREMOTE);
3346 bzero(&ad, sizeof(ad));
3347 ad.sun_family = AF_UNIX;
3348 strcpy(ad.sun_path, _PATH_MOUSEREMOTE);
3350 #define SUN_LEN(unp) (((char *)(unp)->sun_path - (char *)(unp)) + \
3351 strlen((unp)->path))
3353 if (bind(rodent.mremsfd, (struct sockaddr *) &ad, SUN_LEN(&ad)) < 0)
3354 logerrx(1, "unable to bind unix domain socket %s", _PATH_MOUSEREMOTE);
3356 listen(rodent.mremsfd, 1);
3360 mremote_clientchg(int add)
3362 struct sockaddr_un ad;
3366 if (rodent.rtype != MOUSE_PROTO_X10MOUSEREM)
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);
3374 logwarnx("failed accept on mouse remote socket");
3376 if (rodent.mremcfd < 0) {
3377 rodent.mremcfd = fd;
3378 debug("remote client connect...accepted");
3382 debug("another remote client connect...disconnected");
3386 /* Client disconnected */
3387 debug("remote client disconnected");
3388 close(rodent.mremcfd);
3389 rodent.mremcfd = -1;