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 __FBSDID("$FreeBSD$");
52 #include <sys/param.h>
53 #include <sys/consio.h>
54 #include <sys/mouse.h>
55 #include <sys/socket.h>
78 #define MAX_CLICKTHRESHOLD 2000 /* 2 seconds */
79 #define MAX_BUTTON2TIMEOUT 2000 /* 2 seconds */
80 #define DFLT_CLICKTHRESHOLD 500 /* 0.5 second */
81 #define DFLT_BUTTON2TIMEOUT 100 /* 0.1 second */
82 #define DFLT_SCROLLTHRESHOLD 3 /* 3 pixels */
83 #define DFLT_SCROLLSPEED 2 /* 2 pixels */
85 /* Abort 3-button emulation delay after this many movement events. */
86 #define BUTTON2_MAXMOVE 3
91 #define MOUSE_XAXIS (-1)
92 #define MOUSE_YAXIS (-2)
94 /* Logitech PS2++ protocol */
95 #define MOUSE_PS2PLUS_CHECKBITS(b) \
96 ((((b[2] & 0x03) << 2) | 0x02) == (b[1] & 0x0f))
97 #define MOUSE_PS2PLUS_PACKET_TYPE(b) \
98 (((b[0] & 0x30) >> 2) | ((b[1] & 0x30) >> 4))
100 #define ChordMiddle 0x0001
101 #define Emulate3Button 0x0002
102 #define ClearDTR 0x0004
103 #define ClearRTS 0x0008
105 #define VirtualScroll 0x0020
106 #define HVirtualScroll 0x0040
107 #define ExponentialAcc 0x0080
114 #define ID_ALL (ID_PORT | ID_IF | ID_TYPE | ID_MODEL)
116 /* Operations on timespecs */
117 #define tsclr(tvp) ((tvp)->tv_sec = (tvp)->tv_nsec = 0)
118 #define tscmp(tvp, uvp, cmp) \
119 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
120 ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
121 ((tvp)->tv_sec cmp (uvp)->tv_sec))
122 #define tssub(tvp, uvp, vvp) \
124 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
125 (vvp)->tv_nsec = (tvp)->tv_nsec - (uvp)->tv_nsec; \
126 if ((vvp)->tv_nsec < 0) { \
128 (vvp)->tv_nsec += 1000000000; \
132 #define debug(...) do { \
133 if (debug && nodaemon) \
134 warnx(__VA_ARGS__); \
137 #define logerr(e, ...) do { \
138 log_or_warn(LOG_DAEMON | LOG_ERR, errno, __VA_ARGS__); \
142 #define logerrx(e, ...) do { \
143 log_or_warn(LOG_DAEMON | LOG_ERR, 0, __VA_ARGS__); \
147 #define logwarn(...) \
148 log_or_warn(LOG_DAEMON | LOG_WARNING, errno, __VA_ARGS__)
150 #define logwarnx(...) \
151 log_or_warn(LOG_DAEMON | LOG_WARNING, 0, __VA_ARGS__)
155 /* symbol table entry */
162 /* serial PnP ID string */
164 int revision; /* PnP revision, 100 for 1.00 */
165 const char *eisaid; /* EISA ID including mfr ID and product ID */
166 char *serial; /* serial No, optional */
167 const char *class; /* device class, optional */
168 char *compat; /* list of compatible drivers, optional */
169 char *description; /* product description, optional */
170 int neisaid; /* length of the above fields... */
177 /* global variables */
179 static int debug = 0;
180 static int nodaemon = FALSE;
181 static int background = FALSE;
182 static int paused = FALSE;
183 static int identify = ID_NONE;
184 static int extioctl = FALSE;
185 static const char *pidfile = "/var/run/moused.pid";
186 static struct pidfh *pfh;
188 #define SCROLL_NOTSCROLLING 0
189 #define SCROLL_PREPARE 1
190 #define SCROLL_SCROLLING 2
192 static int scroll_state;
193 static int scroll_movement;
194 static int hscroll_movement;
196 /* local variables */
198 /* interface (the table must be ordered by MOUSE_IF_XXX in mouse.h) */
199 static symtab_t rifs[] = {
200 { "serial", MOUSE_IF_SERIAL, 0 },
201 { "bus", MOUSE_IF_BUS, 0 },
202 { "inport", MOUSE_IF_INPORT, 0 },
203 { "ps/2", MOUSE_IF_PS2, 0 },
204 { "sysmouse", MOUSE_IF_SYSMOUSE, 0 },
205 { "usb", MOUSE_IF_USB, 0 },
206 { NULL, MOUSE_IF_UNKNOWN, 0 },
209 /* types (the table must be ordered by MOUSE_PROTO_XXX in mouse.h) */
210 static const char *rnames[] = {
236 static symtab_t rmodels[] = {
237 { "NetScroll", MOUSE_MODEL_NETSCROLL, 0 },
238 { "NetMouse/NetScroll Optical", MOUSE_MODEL_NET, 0 },
239 { "GlidePoint", MOUSE_MODEL_GLIDEPOINT, 0 },
240 { "ThinkingMouse", MOUSE_MODEL_THINK, 0 },
241 { "IntelliMouse", MOUSE_MODEL_INTELLI, 0 },
242 { "EasyScroll/SmartScroll", MOUSE_MODEL_EASYSCROLL, 0 },
243 { "MouseMan+", MOUSE_MODEL_MOUSEMANPLUS, 0 },
244 { "Kidspad", MOUSE_MODEL_KIDSPAD, 0 },
245 { "VersaPad", MOUSE_MODEL_VERSAPAD, 0 },
246 { "IntelliMouse Explorer", MOUSE_MODEL_EXPLORER, 0 },
247 { "4D Mouse", MOUSE_MODEL_4D, 0 },
248 { "4D+ Mouse", MOUSE_MODEL_4DPLUS, 0 },
249 { "Synaptics Touchpad", MOUSE_MODEL_SYNAPTICS, 0 },
250 { "TrackPoint", MOUSE_MODEL_TRACKPOINT, 0 },
251 { "Elantech Touchpad", MOUSE_MODEL_ELANTECH, 0 },
252 { "generic", MOUSE_MODEL_GENERIC, 0 },
253 { NULL, MOUSE_MODEL_UNKNOWN, 0 },
256 /* PnP EISA/product IDs */
257 static symtab_t pnpprod[] = {
258 /* Kensignton ThinkingMouse */
259 { "KML0001", MOUSE_PROTO_THINK, MOUSE_MODEL_THINK },
260 /* MS IntelliMouse */
261 { "MSH0001", MOUSE_PROTO_INTELLI, MOUSE_MODEL_INTELLI },
262 /* MS IntelliMouse TrackBall */
263 { "MSH0004", MOUSE_PROTO_INTELLI, MOUSE_MODEL_INTELLI },
264 /* Tremon Wheel Mouse MUSD */
265 { "HTK0001", MOUSE_PROTO_INTELLI, MOUSE_MODEL_INTELLI },
266 /* Genius PnP Mouse */
267 { "KYE0001", MOUSE_PROTO_MS, MOUSE_MODEL_GENERIC },
268 /* MouseSystems SmartScroll Mouse (OEM from Genius?) */
269 { "KYE0002", MOUSE_PROTO_MS, MOUSE_MODEL_EASYSCROLL },
270 /* Genius NetMouse */
271 { "KYE0003", MOUSE_PROTO_INTELLI, MOUSE_MODEL_NET },
272 /* Genius Kidspad, Easypad and other tablets */
273 { "KYE0005", MOUSE_PROTO_KIDSPAD, MOUSE_MODEL_KIDSPAD },
274 /* Genius EZScroll */
275 { "KYEEZ00", MOUSE_PROTO_MS, MOUSE_MODEL_EASYSCROLL },
276 /* Logitech Cordless MouseMan Wheel */
277 { "LGI8033", MOUSE_PROTO_INTELLI, MOUSE_MODEL_MOUSEMANPLUS },
278 /* Logitech MouseMan (new 4 button model) */
279 { "LGI800C", MOUSE_PROTO_INTELLI, MOUSE_MODEL_MOUSEMANPLUS },
280 /* Logitech MouseMan+ */
281 { "LGI8050", MOUSE_PROTO_INTELLI, MOUSE_MODEL_MOUSEMANPLUS },
282 /* Logitech FirstMouse+ */
283 { "LGI8051", MOUSE_PROTO_INTELLI, MOUSE_MODEL_MOUSEMANPLUS },
284 /* Logitech serial */
285 { "LGI8001", MOUSE_PROTO_LOGIMOUSEMAN, MOUSE_MODEL_GENERIC },
286 /* A4 Tech 4D/4D+ Mouse */
287 { "A4W0005", MOUSE_PROTO_INTELLI, MOUSE_MODEL_4D },
288 /* 8D Scroll Mouse */
289 { "PEC9802", MOUSE_PROTO_INTELLI, MOUSE_MODEL_INTELLI },
290 /* Mitsumi Wireless Scroll Mouse */
291 { "MTM6401", MOUSE_PROTO_INTELLI, MOUSE_MODEL_INTELLI },
294 { "PNP0F00", MOUSE_PROTO_BUS, MOUSE_MODEL_GENERIC },
296 { "PNP0F01", MOUSE_PROTO_MS, MOUSE_MODEL_GENERIC },
298 { "PNP0F02", MOUSE_PROTO_INPORT, MOUSE_MODEL_GENERIC },
300 { "PNP0F03", MOUSE_PROTO_PS2, MOUSE_MODEL_GENERIC },
302 * EzScroll returns PNP0F04 in the compatible device field; but it
303 * doesn't look compatible... XXX
306 { "PNP0F04", MOUSE_PROTO_MSC, MOUSE_MODEL_GENERIC },
308 { "PNP0F05", MOUSE_PROTO_MSC, MOUSE_MODEL_GENERIC },
311 { "PNP0F06", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC },
313 { "PNP0F07", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC },
315 /* Logitech serial */
316 { "PNP0F08", MOUSE_PROTO_LOGIMOUSEMAN, MOUSE_MODEL_GENERIC },
317 /* MS BallPoint serial */
318 { "PNP0F09", MOUSE_PROTO_MS, MOUSE_MODEL_GENERIC },
320 { "PNP0F0A", MOUSE_PROTO_MS, MOUSE_MODEL_GENERIC },
321 /* MS PnP BallPoint serial */
322 { "PNP0F0B", MOUSE_PROTO_MS, MOUSE_MODEL_GENERIC },
323 /* MS serial comatible */
324 { "PNP0F0C", MOUSE_PROTO_MS, MOUSE_MODEL_GENERIC },
325 /* MS InPort comatible */
326 { "PNP0F0D", MOUSE_PROTO_INPORT, MOUSE_MODEL_GENERIC },
327 /* MS PS/2 comatible */
328 { "PNP0F0E", MOUSE_PROTO_PS2, MOUSE_MODEL_GENERIC },
329 /* MS BallPoint comatible */
330 { "PNP0F0F", MOUSE_PROTO_MS, MOUSE_MODEL_GENERIC },
333 { "PNP0F10", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC },
335 /* MS bus comatible */
336 { "PNP0F11", MOUSE_PROTO_BUS, MOUSE_MODEL_GENERIC },
338 { "PNP0F12", MOUSE_PROTO_PS2, MOUSE_MODEL_GENERIC },
340 { "PNP0F13", MOUSE_PROTO_PS2, MOUSE_MODEL_GENERIC },
343 { "PNP0F14", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC },
346 { "PNP0F15", MOUSE_PROTO_BUS, MOUSE_MODEL_GENERIC },
349 { "PNP0F16", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC },
351 /* Logitech serial compat */
352 { "PNP0F17", MOUSE_PROTO_LOGIMOUSEMAN, MOUSE_MODEL_GENERIC },
353 /* Logitech bus compatible */
354 { "PNP0F18", MOUSE_PROTO_BUS, MOUSE_MODEL_GENERIC },
355 /* Logitech PS/2 compatible */
356 { "PNP0F19", MOUSE_PROTO_PS2, MOUSE_MODEL_GENERIC },
358 /* Logitech SWIFT compatible */
359 { "PNP0F1A", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC },
361 { "PNP0F1B", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC },
362 /* Compaq LTE TrackBall PS/2 */
363 { "PNP0F1C", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC },
364 /* Compaq LTE TrackBall serial */
365 { "PNP0F1D", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC },
366 /* MS Kidts Trackball */
367 { "PNP0F1E", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC },
369 /* Interlink VersaPad */
370 { "LNK0001", MOUSE_PROTO_VERSAPAD, MOUSE_MODEL_VERSAPAD },
372 { NULL, MOUSE_PROTO_UNKNOWN, MOUSE_MODEL_GENERIC },
375 /* the table must be ordered by MOUSE_PROTO_XXX in mouse.h */
376 static unsigned short rodentcflags[] =
378 (CS7 | CREAD | CLOCAL | HUPCL), /* MicroSoft */
379 (CS8 | CSTOPB | CREAD | CLOCAL | HUPCL), /* MouseSystems */
380 (CS8 | CSTOPB | CREAD | CLOCAL | HUPCL), /* Logitech */
381 (CS8 | PARENB | PARODD | CREAD | CLOCAL | HUPCL), /* MMSeries */
382 (CS7 | CREAD | CLOCAL | HUPCL), /* MouseMan */
386 (CS8 | CREAD | CLOCAL | HUPCL), /* MM HitTablet */
387 (CS7 | CREAD | CLOCAL | HUPCL), /* GlidePoint */
388 (CS7 | CREAD | CLOCAL | HUPCL), /* IntelliMouse */
389 (CS7 | CREAD | CLOCAL | HUPCL), /* Thinking Mouse */
390 (CS8 | CSTOPB | CREAD | CLOCAL | HUPCL), /* sysmouse */
391 (CS7 | CREAD | CLOCAL | HUPCL), /* X10 MouseRemote */
392 (CS8 | PARENB | PARODD | CREAD | CLOCAL | HUPCL), /* kidspad etc. */
393 (CS8 | CREAD | CLOCAL | HUPCL), /* VersaPad */
396 (CS8 | CSTOPB | CREAD | CLOCAL | HUPCL), /* Mariqua */
398 (CS8 | CREAD | HUPCL ), /* GTCO Digi-Pad */
401 static struct rodentparam {
403 const char *portname; /* /dev/XXX */
404 int rtype; /* MOUSE_PROTO_XXX */
405 int level; /* operation level: 0 or greater */
407 int rate; /* report rate */
408 int resolution; /* MOUSE_RES_XXX or a positive number */
409 int zmap[4]; /* MOUSE_{X|Y}AXIS or a button number */
410 int wmode; /* wheel mode button number */
411 int mfd; /* mouse file descriptor */
412 int cfd; /* /dev/consolectl file descriptor */
413 int mremsfd; /* mouse remote server file descriptor */
414 int mremcfd; /* mouse remote client file descriptor */
415 int is_removable; /* set if device is removable, like USB */
416 long clickthreshold; /* double click speed in msec */
417 long button2timeout; /* 3 button emulation timeout */
418 mousehw_t hw; /* mouse device hardware information */
419 mousemode_t mode; /* protocol information */
420 float accelx; /* Acceleration in the X axis */
421 float accely; /* Acceleration in the Y axis */
422 float expoaccel; /* Exponential acceleration */
423 float expoffset; /* Movement offset for exponential accel. */
424 float remainx; /* Remainder on X and Y axis, respectively... */
425 float remainy; /* ... to compensate for rounding errors. */
426 int scrollthreshold; /* Movement distance before virtual scrolling */
427 int scrollspeed; /* Movement distance to rate of scrolling */
431 .rtype = MOUSE_PROTO_UNKNOWN,
435 .resolution = MOUSE_RES_UNKNOWN,
436 .zmap = { 0, 0, 0, 0 },
443 .clickthreshold = DFLT_CLICKTHRESHOLD,
444 .button2timeout = DFLT_BUTTON2TIMEOUT,
451 .scrollthreshold = DFLT_SCROLLTHRESHOLD,
452 .scrollspeed = DFLT_SCROLLSPEED,
456 struct button_state {
457 int count; /* 0: up, 1: single click, 2: double click,... */
458 struct timespec ts; /* timestamp on the last button event */
460 static struct button_state bstate[MOUSE_MAXBUTTON]; /* button state */
461 static struct button_state *mstate[MOUSE_MAXBUTTON];/* mapped button st.*/
462 static struct button_state zstate[4]; /* Z/W axis state */
464 /* state machine for 3 button emulation */
466 #define S0 0 /* start */
467 #define S1 1 /* button 1 delayed down */
468 #define S2 2 /* button 3 delayed down */
469 #define S3 3 /* both buttons down -> button 2 down */
470 #define S4 4 /* button 1 delayed up */
471 #define S5 5 /* button 1 down */
472 #define S6 6 /* button 3 down */
473 #define S7 7 /* both buttons down */
474 #define S8 8 /* button 3 delayed up */
475 #define S9 9 /* button 1 or 3 up after S3 */
477 #define A(b1, b3) (((b1) ? 2 : 0) | ((b3) ? 1 : 0))
479 #define S_DELAYED(st) (states[st].s[A_TIMEOUT] != (st))
482 int s[A_TIMEOUT + 1];
488 { { S0, S2, S1, S3, S0 }, 0, ~(MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN), FALSE },
490 { { S4, S2, S1, S3, S5 }, 0, ~MOUSE_BUTTON1DOWN, FALSE },
492 { { S8, S2, S1, S3, S6 }, 0, ~MOUSE_BUTTON3DOWN, FALSE },
494 { { S0, S9, S9, S3, S3 }, MOUSE_BUTTON2DOWN, ~0, FALSE },
496 { { S0, S2, S1, S3, S0 }, MOUSE_BUTTON1DOWN, ~0, TRUE },
498 { { S0, S2, S5, S7, S5 }, MOUSE_BUTTON1DOWN, ~0, FALSE },
500 { { S0, S6, S1, S7, S6 }, MOUSE_BUTTON3DOWN, ~0, FALSE },
502 { { S0, S6, S5, S7, S7 }, MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, ~0, FALSE },
504 { { S0, S2, S1, S3, S0 }, MOUSE_BUTTON3DOWN, ~0, TRUE },
506 { { S0, S9, S9, S3, S9 }, 0, ~(MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN), FALSE },
508 static int mouse_button_state;
509 static struct timespec mouse_button_state_ts;
510 static int mouse_move_delayed;
518 static int drift_distance = 4; /* max steps X+Y */
519 static int drift_time = 500; /* in 0.5 sec */
520 static struct timespec drift_time_ts;
521 static struct timespec drift_2time_ts; /* 2*drift_time */
522 static int drift_after = 4000; /* 4 sec */
523 static struct timespec drift_after_ts;
524 static int drift_terminate = FALSE;
525 static struct timespec drift_current_ts;
526 static struct timespec drift_tmp;
527 static struct timespec drift_last_activity = {0, 0};
528 static struct timespec drift_since = {0, 0};
529 static struct drift_xy drift_last = {0, 0}; /* steps in last drift_time */
530 static struct drift_xy drift_previous = {0, 0}; /* steps in prev. drift_time */
532 /* function prototypes */
534 static void linacc(int, int, int*, int*);
535 static void expoacc(int, int, int*, int*);
536 static void moused(void);
537 static void hup(int sig);
538 static void cleanup(int sig);
539 static void pause_mouse(int sig);
540 static void usage(void);
541 static void log_or_warn(int log_pri, int errnum, const char *fmt, ...)
544 static int r_identify(void);
545 static const char *r_if(int type);
546 static const char *r_name(int type);
547 static const char *r_model(int model);
548 static void r_init(void);
549 static int r_protocol(u_char b, mousestatus_t *act);
550 static int r_statetrans(mousestatus_t *a1, mousestatus_t *a2, int trans);
551 static int r_installmap(char *arg);
552 static void r_map(mousestatus_t *act1, mousestatus_t *act2);
553 static void r_timestamp(mousestatus_t *act);
554 static int r_timeout(void);
555 static void r_click(mousestatus_t *act);
556 static void setmousespeed(int old, int new, unsigned cflag);
558 static int pnpwakeup1(void);
559 static int pnpwakeup2(void);
560 static int pnpgets(char *buf);
561 static int pnpparse(pnpid_t *id, char *buf, int len);
562 static symtab_t *pnpproto(pnpid_t *id);
564 static symtab_t *gettoken(symtab_t *tab, const char *s, int len);
565 static const char *gettokenname(symtab_t *tab, int val);
567 static void mremote_serversetup(void);
568 static void mremote_clientchg(int add);
570 static int kidspad(u_char rxc, mousestatus_t *act);
571 static int gtco_digipad(u_char, mousestatus_t *);
574 main(int argc, char *argv[])
580 for (i = 0; i < MOUSE_MAXBUTTON; ++i)
581 mstate[i] = &bstate[i];
583 while ((c = getopt(argc, argv, "3A:C:DE:F:HI:L:PRS:T:VU:a:cdfhi:l:m:p:r:st:w:z:")) != -1)
587 rodent.flags |= Emulate3Button;
591 rodent.button2timeout = atoi(optarg);
592 if ((rodent.button2timeout < 0) ||
593 (rodent.button2timeout > MAX_BUTTON2TIMEOUT)) {
594 warnx("invalid argument `%s'", optarg);
600 i = sscanf(optarg, "%f,%f", &rodent.accelx, &rodent.accely);
602 warnx("invalid linear acceleration argument '%s'", optarg);
607 rodent.accely = rodent.accelx;
612 rodent.flags |= ExponentialAcc;
613 i = sscanf(optarg, "%f,%f", &rodent.expoaccel, &rodent.expoffset);
615 warnx("invalid exponential acceleration argument '%s'", optarg);
620 rodent.expoffset = 1.0;
625 rodent.flags |= ChordMiddle;
637 if (strcmp(optarg, "all") == 0)
639 else if (strcmp(optarg, "port") == 0)
641 else if (strcmp(optarg, "if") == 0)
643 else if (strcmp(optarg, "type") == 0)
645 else if (strcmp(optarg, "model") == 0)
648 warnx("invalid argument `%s'", optarg);
655 rodent.level = atoi(optarg);
656 if ((rodent.level < 0) || (rodent.level > 4)) {
657 warnx("invalid argument `%s'", optarg);
663 if (!r_installmap(optarg)) {
664 warnx("invalid argument `%s'", optarg);
670 rodent.portname = optarg;
674 if (strcmp(optarg, "high") == 0)
675 rodent.resolution = MOUSE_RES_HIGH;
676 else if (strcmp(optarg, "medium-high") == 0)
677 rodent.resolution = MOUSE_RES_HIGH;
678 else if (strcmp(optarg, "medium-low") == 0)
679 rodent.resolution = MOUSE_RES_MEDIUMLOW;
680 else if (strcmp(optarg, "low") == 0)
681 rodent.resolution = MOUSE_RES_LOW;
682 else if (strcmp(optarg, "default") == 0)
683 rodent.resolution = MOUSE_RES_DEFAULT;
685 rodent.resolution = atoi(optarg);
686 if (rodent.resolution <= 0) {
687 warnx("invalid argument `%s'", optarg);
694 rodent.baudrate = 9600;
699 if ((i <= 0) || (i > MOUSE_MAXBUTTON)) {
700 warnx("invalid argument `%s'", optarg);
703 rodent.wmode = 1 << (i - 1);
707 if (strcmp(optarg, "x") == 0)
708 rodent.zmap[0] = MOUSE_XAXIS;
709 else if (strcmp(optarg, "y") == 0)
710 rodent.zmap[0] = MOUSE_YAXIS;
714 * Use button i for negative Z axis movement and
715 * button (i + 1) for positive Z axis movement.
717 if ((i <= 0) || (i > MOUSE_MAXBUTTON - 1)) {
718 warnx("invalid argument `%s'", optarg);
722 rodent.zmap[1] = i + 1;
723 debug("optind: %d, optarg: '%s'", optind, optarg);
724 for (j = 1; j < 4; ++j) {
725 if ((optind >= argc) || !isdigit(*argv[optind]))
727 i = atoi(argv[optind]);
728 if ((i <= 0) || (i > MOUSE_MAXBUTTON - 1)) {
729 warnx("invalid argument `%s'", argv[optind]);
735 if ((rodent.zmap[2] != 0) && (rodent.zmap[3] == 0))
736 rodent.zmap[3] = rodent.zmap[2] + 1;
741 rodent.clickthreshold = atoi(optarg);
742 if ((rodent.clickthreshold < 0) ||
743 (rodent.clickthreshold > MAX_CLICKTHRESHOLD)) {
744 warnx("invalid argument `%s'", optarg);
750 rodent.flags |= ClearDTR;
754 rodent.rate = atoi(optarg);
755 if (rodent.rate <= 0) {
756 warnx("invalid argument `%s'", optarg);
762 rodent.flags |= HVirtualScroll;
770 rodent.scrollspeed = atoi(optarg);
771 if (rodent.scrollspeed < 0) {
772 warnx("invalid argument `%s'", optarg);
778 rodent.flags |= NoPnP;
782 rodent.flags |= ClearRTS;
786 rodent.baudrate = atoi(optarg);
787 if (rodent.baudrate <= 0) {
788 warnx("invalid argument `%s'", optarg);
791 debug("rodent baudrate %d", rodent.baudrate);
795 drift_terminate = TRUE;
796 sscanf(optarg, "%d,%d,%d", &drift_distance, &drift_time,
798 if (drift_distance <= 0 || drift_time <= 0 || drift_after <= 0) {
799 warnx("invalid argument `%s'", optarg);
802 debug("terminate drift: distance %d, time %d, after %d",
803 drift_distance, drift_time, drift_after);
804 drift_time_ts.tv_sec = drift_time / 1000;
805 drift_time_ts.tv_nsec = (drift_time % 1000) * 1000000;
806 drift_2time_ts.tv_sec = (drift_time *= 2) / 1000;
807 drift_2time_ts.tv_nsec = (drift_time % 1000) * 1000000;
808 drift_after_ts.tv_sec = drift_after / 1000;
809 drift_after_ts.tv_nsec = (drift_after % 1000) * 1000000;
813 if (strcmp(optarg, "auto") == 0) {
814 rodent.rtype = MOUSE_PROTO_UNKNOWN;
815 rodent.flags &= ~NoPnP;
819 for (i = 0; rnames[i] != NULL; i++)
820 if (strcmp(optarg, rnames[i]) == 0) {
822 rodent.flags |= NoPnP;
823 rodent.level = (i == MOUSE_PROTO_SYSMOUSE) ? 1 : 0;
826 if (rnames[i] == NULL) {
827 warnx("no such mouse type `%s'", optarg);
833 rodent.flags |= VirtualScroll;
836 rodent.scrollthreshold = atoi(optarg);
837 if (rodent.scrollthreshold < 0) {
838 warnx("invalid argument `%s'", optarg);
849 /* fix Z axis mapping */
850 for (i = 0; i < 4; ++i) {
851 if (rodent.zmap[i] > 0) {
852 for (j = 0; j < MOUSE_MAXBUTTON; ++j) {
853 if (mstate[j] == &bstate[rodent.zmap[i] - 1])
854 mstate[j] = &zstate[i];
856 rodent.zmap[i] = 1 << (rodent.zmap[i] - 1);
860 /* the default port name */
861 switch(rodent.rtype) {
863 case MOUSE_PROTO_INPORT:
864 /* INPORT and BUS are the same... */
865 rodent.rtype = MOUSE_PROTO_BUS;
867 case MOUSE_PROTO_BUS:
868 if (!rodent.portname)
869 rodent.portname = "/dev/mse0";
872 case MOUSE_PROTO_PS2:
873 if (!rodent.portname)
874 rodent.portname = "/dev/psm0";
880 warnx("no port name specified");
884 if (strncmp(rodent.portname, "/dev/ums", 8) == 0)
885 rodent.is_removable = 1;
888 if (setjmp(env) == 0) {
890 signal(SIGINT , cleanup);
891 signal(SIGQUIT, cleanup);
892 signal(SIGTERM, cleanup);
893 signal(SIGUSR1, pause_mouse);
895 rodent.mfd = open(rodent.portname, O_RDWR | O_NONBLOCK);
896 if (rodent.mfd == -1)
897 logerr(1, "unable to open %s", rodent.portname);
898 if (r_identify() == MOUSE_PROTO_UNKNOWN) {
899 logwarnx("cannot determine mouse type on %s", rodent.portname);
904 /* print some information */
905 if (identify != ID_NONE) {
906 if (identify == ID_ALL)
907 printf("%s %s %s %s\n",
908 rodent.portname, r_if(rodent.hw.iftype),
909 r_name(rodent.rtype), r_model(rodent.hw.model));
910 else if (identify & ID_PORT)
911 printf("%s\n", rodent.portname);
912 else if (identify & ID_IF)
913 printf("%s\n", r_if(rodent.hw.iftype));
914 else if (identify & ID_TYPE)
915 printf("%s\n", r_name(rodent.rtype));
916 else if (identify & ID_MODEL)
917 printf("%s\n", r_model(rodent.hw.model));
920 debug("port: %s interface: %s type: %s model: %s",
921 rodent.portname, r_if(rodent.hw.iftype),
922 r_name(rodent.rtype), r_model(rodent.hw.model));
925 if (rodent.mfd == -1) {
927 * We cannot continue because of error. Exit if the
928 * program has not become a daemon. Otherwise, block
929 * until the user corrects the problem and issues SIGHUP.
936 r_init(); /* call init function */
940 if (rodent.mfd != -1)
942 if (rodent.cfd != -1)
944 rodent.mfd = rodent.cfd = -1;
945 if (rodent.is_removable)
954 * Function to calculate linear acceleration.
956 * If there are any rounding errors, the remainder
957 * is stored in the remainx and remainy variables
958 * and taken into account upon the next movement.
962 linacc(int dx, int dy, int *movex, int *movey)
966 if (dx == 0 && dy == 0) {
970 fdx = dx * rodent.accelx + rodent.remainx;
971 fdy = dy * rodent.accely + rodent.remainy;
972 *movex = lround(fdx);
973 *movey = lround(fdy);
974 rodent.remainx = fdx - *movex;
975 rodent.remainy = fdy - *movey;
979 * Function to calculate exponential acceleration.
980 * (Also includes linear acceleration if enabled.)
982 * In order to give a smoother behaviour, we record the four
983 * most recent non-zero movements and use their average value
984 * to calculate the acceleration.
988 expoacc(int dx, int dy, int *movex, int *movey)
990 static float lastlength[3] = {0.0, 0.0, 0.0};
991 float fdx, fdy, length, lbase, accel;
993 if (dx == 0 && dy == 0) {
997 fdx = dx * rodent.accelx;
998 fdy = dy * rodent.accely;
999 length = sqrtf((fdx * fdx) + (fdy * fdy)); /* Pythagoras */
1000 length = (length + lastlength[0] + lastlength[1] + lastlength[2]) / 4;
1001 lbase = length / rodent.expoffset;
1002 accel = powf(lbase, rodent.expoaccel) / lbase;
1003 fdx = fdx * accel + rodent.remainx;
1004 fdy = fdy * accel + rodent.remainy;
1005 *movex = lroundf(fdx);
1006 *movey = lroundf(fdy);
1007 rodent.remainx = fdx - *movex;
1008 rodent.remainy = fdy - *movey;
1009 lastlength[2] = lastlength[1];
1010 lastlength[1] = lastlength[0];
1011 lastlength[0] = length; /* Insert new average, not original length! */
1017 struct mouse_info mouse;
1018 mousestatus_t action0; /* original mouse action */
1019 mousestatus_t action; /* interim buffer */
1020 mousestatus_t action2; /* mapped action */
1021 struct timeval timeout;
1029 if ((rodent.cfd = open("/dev/consolectl", O_RDWR, 0)) == -1)
1030 logerr(1, "cannot open /dev/consolectl");
1032 if (!nodaemon && !background) {
1033 pfh = pidfile_open(pidfile, 0600, &mpid);
1035 if (errno == EEXIST)
1036 logerrx(1, "moused already running, pid: %d", mpid);
1037 logwarn("cannot open pid file");
1040 int saved_errno = errno;
1041 pidfile_remove(pfh);
1042 errno = saved_errno;
1043 logerr(1, "failed to become a daemon");
1050 /* clear mouse data */
1051 bzero(&action0, sizeof(action0));
1052 bzero(&action, sizeof(action));
1053 bzero(&action2, sizeof(action2));
1054 bzero(&mouse, sizeof(mouse));
1055 mouse_button_state = S0;
1056 clock_gettime(CLOCK_MONOTONIC_FAST, &mouse_button_state_ts);
1057 mouse_move_delayed = 0;
1058 for (i = 0; i < MOUSE_MAXBUTTON; ++i) {
1059 bstate[i].count = 0;
1060 bstate[i].ts = mouse_button_state_ts;
1062 for (i = 0; i < (int)(sizeof(zstate) / sizeof(zstate[0])); ++i) {
1063 zstate[i].count = 0;
1064 zstate[i].ts = mouse_button_state_ts;
1067 /* choose which ioctl command to use */
1068 mouse.operation = MOUSE_MOTION_EVENT;
1069 extioctl = (ioctl(rodent.cfd, CONS_MOUSECTL, &mouse) == 0);
1071 /* process mouse data */
1073 timeout.tv_usec = 20000; /* 20 msec */
1077 FD_SET(rodent.mfd, &fds);
1078 if (rodent.mremsfd >= 0)
1079 FD_SET(rodent.mremsfd, &fds);
1080 if (rodent.mremcfd >= 0)
1081 FD_SET(rodent.mremcfd, &fds);
1083 c = select(FD_SETSIZE, &fds, NULL, NULL,
1084 ((rodent.flags & Emulate3Button) &&
1085 S_DELAYED(mouse_button_state)) ? &timeout : NULL);
1086 if (c < 0) { /* error */
1087 logwarn("failed to read from mouse");
1089 } else if (c == 0) { /* timeout */
1090 /* assert(rodent.flags & Emulate3Button) */
1091 action0.button = action0.obutton;
1092 action0.dx = action0.dy = action0.dz = 0;
1093 action0.flags = flags = 0;
1094 if (r_timeout() && r_statetrans(&action0, &action, A_TIMEOUT)) {
1096 debug("flags:%08x buttons:%08x obuttons:%08x",
1097 action.flags, action.button, action.obutton);
1099 action0.obutton = action0.button;
1103 /* MouseRemote client connect/disconnect */
1104 if ((rodent.mremsfd >= 0) && FD_ISSET(rodent.mremsfd, &fds)) {
1105 mremote_clientchg(TRUE);
1108 if ((rodent.mremcfd >= 0) && FD_ISSET(rodent.mremcfd, &fds)) {
1109 mremote_clientchg(FALSE);
1112 /* mouse movement */
1113 if (read(rodent.mfd, &b, 1) == -1) {
1114 if (errno == EWOULDBLOCK)
1119 if ((flags = r_protocol(b, &action0)) == 0)
1122 if ((rodent.flags & VirtualScroll) || (rodent.flags & HVirtualScroll)) {
1123 /* Allow middle button drags to scroll up and down */
1124 if (action0.button == MOUSE_BUTTON2DOWN) {
1125 if (scroll_state == SCROLL_NOTSCROLLING) {
1126 scroll_state = SCROLL_PREPARE;
1127 scroll_movement = hscroll_movement = 0;
1128 debug("PREPARING TO SCROLL");
1130 debug("[BUTTON2] flags:%08x buttons:%08x obuttons:%08x",
1131 action.flags, action.button, action.obutton);
1133 debug("[NOTBUTTON2] flags:%08x buttons:%08x obuttons:%08x",
1134 action.flags, action.button, action.obutton);
1136 /* This isn't a middle button down... move along... */
1137 if (scroll_state == SCROLL_SCROLLING) {
1139 * We were scrolling, someone let go of button 2.
1140 * Now turn autoscroll off.
1142 scroll_state = SCROLL_NOTSCROLLING;
1143 debug("DONE WITH SCROLLING / %d", scroll_state);
1144 } else if (scroll_state == SCROLL_PREPARE) {
1145 mousestatus_t newaction = action0;
1147 /* We were preparing to scroll, but we never moved... */
1148 r_timestamp(&action0);
1149 r_statetrans(&action0, &newaction,
1150 A(newaction.button & MOUSE_BUTTON1DOWN,
1151 action0.button & MOUSE_BUTTON3DOWN));
1153 /* Send middle down */
1154 newaction.button = MOUSE_BUTTON2DOWN;
1155 r_click(&newaction);
1157 /* Send middle up */
1158 r_timestamp(&newaction);
1159 newaction.obutton = newaction.button;
1160 newaction.button = action0.button;
1161 r_click(&newaction);
1166 r_timestamp(&action0);
1167 r_statetrans(&action0, &action,
1168 A(action0.button & MOUSE_BUTTON1DOWN,
1169 action0.button & MOUSE_BUTTON3DOWN));
1170 debug("flags:%08x buttons:%08x obuttons:%08x", action.flags,
1171 action.button, action.obutton);
1173 action0.obutton = action0.button;
1174 flags &= MOUSE_POSCHANGED;
1175 flags |= action.obutton ^ action.button;
1176 action.flags = flags;
1178 if (flags) { /* handler detected action */
1179 r_map(&action, &action2);
1180 debug("activity : buttons 0x%08x dx %d dy %d dz %d",
1181 action2.button, action2.dx, action2.dy, action2.dz);
1183 if ((rodent.flags & VirtualScroll) || (rodent.flags & HVirtualScroll)) {
1185 * If *only* the middle button is pressed AND we are moving
1186 * the stick/trackpoint/nipple, scroll!
1188 if (scroll_state == SCROLL_PREPARE) {
1189 /* Middle button down, waiting for movement threshold */
1190 if (action2.dy || action2.dx) {
1191 if (rodent.flags & VirtualScroll) {
1192 scroll_movement += action2.dy;
1193 if (scroll_movement < -rodent.scrollthreshold) {
1194 scroll_state = SCROLL_SCROLLING;
1195 } else if (scroll_movement > rodent.scrollthreshold) {
1196 scroll_state = SCROLL_SCROLLING;
1199 if (rodent.flags & HVirtualScroll) {
1200 hscroll_movement += action2.dx;
1201 if (hscroll_movement < -rodent.scrollthreshold) {
1202 scroll_state = SCROLL_SCROLLING;
1203 } else if (hscroll_movement > rodent.scrollthreshold) {
1204 scroll_state = SCROLL_SCROLLING;
1207 if (scroll_state == SCROLL_SCROLLING) scroll_movement = hscroll_movement = 0;
1209 } else if (scroll_state == SCROLL_SCROLLING) {
1210 if (rodent.flags & VirtualScroll) {
1211 scroll_movement += action2.dy;
1212 debug("SCROLL: %d", scroll_movement);
1213 if (scroll_movement < -rodent.scrollspeed) {
1216 scroll_movement = 0;
1218 else if (scroll_movement > rodent.scrollspeed) {
1221 scroll_movement = 0;
1224 if (rodent.flags & HVirtualScroll) {
1225 hscroll_movement += action2.dx;
1226 debug("HORIZONTAL SCROLL: %d", hscroll_movement);
1228 if (hscroll_movement < -rodent.scrollspeed) {
1230 hscroll_movement = 0;
1232 else if (hscroll_movement > rodent.scrollspeed) {
1234 hscroll_movement = 0;
1238 /* Don't move while scrolling */
1239 action2.dx = action2.dy = 0;
1243 if (drift_terminate) {
1244 if ((flags & MOUSE_POSCHANGED) == 0 || action.dz || action2.dz)
1245 drift_last_activity = drift_current_ts;
1247 /* X or/and Y movement only - possibly drift */
1248 tssub(&drift_current_ts, &drift_last_activity, &drift_tmp);
1249 if (tscmp(&drift_tmp, &drift_after_ts, >)) {
1250 tssub(&drift_current_ts, &drift_since, &drift_tmp);
1251 if (tscmp(&drift_tmp, &drift_time_ts, <)) {
1252 drift_last.x += action2.dx;
1253 drift_last.y += action2.dy;
1255 /* discard old accumulated steps (drift) */
1256 if (tscmp(&drift_tmp, &drift_2time_ts, >))
1257 drift_previous.x = drift_previous.y = 0;
1259 drift_previous = drift_last;
1260 drift_last.x = action2.dx;
1261 drift_last.y = action2.dy;
1262 drift_since = drift_current_ts;
1264 if (abs(drift_last.x) + abs(drift_last.y)
1266 /* real movement, pass all accumulated steps */
1267 action2.dx = drift_previous.x + drift_last.x;
1268 action2.dy = drift_previous.y + drift_last.y;
1269 /* and reset accumulators */
1270 tsclr(&drift_since);
1271 drift_last.x = drift_last.y = 0;
1272 /* drift_previous will be cleared at next movement*/
1273 drift_last_activity = drift_current_ts;
1275 continue; /* don't pass current movement to
1283 /* Defer clicks until we aren't VirtualScroll'ing. */
1284 if (scroll_state == SCROLL_NOTSCROLLING)
1287 if (action2.flags & MOUSE_POSCHANGED) {
1288 mouse.operation = MOUSE_MOTION_EVENT;
1289 mouse.u.data.buttons = action2.button;
1290 if (rodent.flags & ExponentialAcc) {
1291 expoacc(action2.dx, action2.dy,
1292 &mouse.u.data.x, &mouse.u.data.y);
1295 linacc(action2.dx, action2.dy,
1296 &mouse.u.data.x, &mouse.u.data.y);
1298 mouse.u.data.z = action2.dz;
1301 ioctl(rodent.cfd, CONS_MOUSECTL, &mouse);
1304 mouse.operation = MOUSE_ACTION;
1305 mouse.u.data.buttons = action2.button;
1306 if (rodent.flags & ExponentialAcc) {
1307 expoacc(action2.dx, action2.dy,
1308 &mouse.u.data.x, &mouse.u.data.y);
1311 linacc(action2.dx, action2.dy,
1312 &mouse.u.data.x, &mouse.u.data.y);
1314 mouse.u.data.z = action2.dz;
1317 ioctl(rodent.cfd, CONS_MOUSECTL, &mouse);
1321 * If the Z axis movement is mapped to an imaginary physical
1322 * button, we need to cook up a corresponding button `up' event
1323 * after sending a button `down' event.
1325 if ((rodent.zmap[0] > 0) && (action.dz != 0)) {
1326 action.obutton = action.button;
1327 action.dx = action.dy = action.dz = 0;
1328 r_map(&action, &action2);
1329 debug("activity : buttons 0x%08x dx %d dy %d dz %d",
1330 action2.button, action2.dx, action2.dy, action2.dz);
1335 mouse.operation = MOUSE_ACTION;
1336 mouse.u.data.buttons = action2.button;
1337 mouse.u.data.x = mouse.u.data.y = mouse.u.data.z = 0;
1340 ioctl(rodent.cfd, CONS_MOUSECTL, &mouse);
1349 hup(__unused int sig)
1355 cleanup(__unused int sig)
1357 if (rodent.rtype == MOUSE_PROTO_X10MOUSEREM)
1358 unlink(_PATH_MOUSEREMOTE);
1363 pause_mouse(__unused int sig)
1371 ** Complain, and free the CPU for more worthy tasks
1376 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
1377 "usage: moused [-DRcdfs] [-I file] [-F rate] [-r resolution] [-S baudrate]",
1378 " [-VH [-U threshold]] [-a X[,Y]] [-C threshold] [-m N=M] [-w N]",
1379 " [-z N] [-t <mousetype>] [-l level] [-3 [-E timeout]]",
1380 " [-T distance[,time[,after]]] -p <port>",
1381 " moused [-d] -i <port|if|type|model|all> -p <port>");
1386 * Output an error message to syslog or stderr as appropriate. If
1387 * `errnum' is non-zero, append its string form to the message.
1390 log_or_warn(int log_pri, int errnum, const char *fmt, ...)
1396 vsnprintf(buf, sizeof(buf), fmt, ap);
1399 strlcat(buf, ": ", sizeof(buf));
1400 strlcat(buf, strerror(errnum), sizeof(buf));
1404 syslog(log_pri, "%s", buf);
1410 ** Mouse interface code, courtesy of XFree86 3.1.2.
1412 ** Note: Various bits have been trimmed, and in my shortsighted enthusiasm
1413 ** to clean, reformat and rationalise naming, it's quite possible that
1414 ** some things in here have been broken.
1418 ** The following code is derived from a module marked :
1421 /* $XConsortium: xf86_Mouse.c,v 1.2 94/10/12 20:33:21 kaleb Exp $ */
1422 /* $XFree86: xc/programs/Xserver/hw/xfree86/common/xf86_Mouse.c,v 3.2 1995/01/28
1423 17:03:40 dawes Exp $ */
1426 * Copyright 1990,91 by Thomas Roell, Dinkelscherben, Germany.
1427 * Copyright 1993 by David Dawes <dawes@physics.su.oz.au>
1429 * Permission to use, copy, modify, distribute, and sell this software and its
1430 * documentation for any purpose is hereby granted without fee, provided that
1431 * the above copyright notice appear in all copies and that both that
1432 * copyright notice and this permission notice appear in supporting
1433 * documentation, and that the names of Thomas Roell and David Dawes not be
1434 * used in advertising or publicity pertaining to distribution of the
1435 * software without specific, written prior permission. Thomas Roell
1436 * and David Dawes makes no representations about the suitability of this
1437 * software for any purpose. It is provided "as is" without express or
1440 * THOMAS ROELL AND DAVID DAWES DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
1441 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
1442 * FITNESS, IN NO EVENT SHALL THOMAS ROELL OR DAVID DAWES BE LIABLE FOR ANY
1443 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
1444 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
1445 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
1446 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1451 ** GlidePoint support from XFree86 3.2.
1452 ** Derived from the module:
1455 /* $XFree86: xc/programs/Xserver/hw/xfree86/common/xf86_Mouse.c,v 3.19 1996/10/16 14:40:51 dawes Exp $ */
1456 /* $XConsortium: xf86_Mouse.c /main/10 1996/01/30 15:16:12 kaleb $ */
1458 /* the following table must be ordered by MOUSE_PROTO_XXX in mouse.h */
1459 static unsigned char proto[][7] = {
1460 /* hd_mask hd_id dp_mask dp_id bytes b4_mask b4_id */
1461 { 0x40, 0x40, 0x40, 0x00, 3, ~0x23, 0x00 }, /* MicroSoft */
1462 { 0xf8, 0x80, 0x00, 0x00, 5, 0x00, 0xff }, /* MouseSystems */
1463 { 0xe0, 0x80, 0x80, 0x00, 3, 0x00, 0xff }, /* Logitech */
1464 { 0xe0, 0x80, 0x80, 0x00, 3, 0x00, 0xff }, /* MMSeries */
1465 { 0x40, 0x40, 0x40, 0x00, 3, ~0x33, 0x00 }, /* MouseMan */
1466 { 0xf8, 0x80, 0x00, 0x00, 5, 0x00, 0xff }, /* Bus */
1467 { 0xf8, 0x80, 0x00, 0x00, 5, 0x00, 0xff }, /* InPort */
1468 { 0xc0, 0x00, 0x00, 0x00, 3, 0x00, 0xff }, /* PS/2 mouse */
1469 { 0xe0, 0x80, 0x80, 0x00, 3, 0x00, 0xff }, /* MM HitTablet */
1470 { 0x40, 0x40, 0x40, 0x00, 3, ~0x33, 0x00 }, /* GlidePoint */
1471 { 0x40, 0x40, 0x40, 0x00, 3, ~0x3f, 0x00 }, /* IntelliMouse */
1472 { 0x40, 0x40, 0x40, 0x00, 3, ~0x33, 0x00 }, /* ThinkingMouse */
1473 { 0xf8, 0x80, 0x00, 0x00, 5, 0x00, 0xff }, /* sysmouse */
1474 { 0x40, 0x40, 0x40, 0x00, 3, ~0x23, 0x00 }, /* X10 MouseRem */
1475 { 0x80, 0x80, 0x00, 0x00, 5, 0x00, 0xff }, /* KIDSPAD */
1476 { 0xc3, 0xc0, 0x00, 0x00, 6, 0x00, 0xff }, /* VersaPad */
1477 { 0x00, 0x00, 0x00, 0x00, 1, 0x00, 0xff }, /* JogDial */
1479 { 0xf8, 0x80, 0x00, 0x00, 5, ~0x2f, 0x10 }, /* Mariqua */
1482 static unsigned char cur_proto[7];
1487 char pnpbuf[256]; /* PnP identifier string may be up to 256 bytes long */
1493 /* set the driver operation level, if applicable */
1494 if (rodent.level < 0)
1496 ioctl(rodent.mfd, MOUSE_SETLEVEL, &rodent.level);
1497 rodent.level = (ioctl(rodent.mfd, MOUSE_GETLEVEL, &level) == 0) ? level : 0;
1500 * Interrogate the driver and get some intelligence on the device...
1501 * The following ioctl functions are not always supported by device
1502 * drivers. When the driver doesn't support them, we just trust the
1503 * user to supply valid information.
1505 rodent.hw.iftype = MOUSE_IF_UNKNOWN;
1506 rodent.hw.model = MOUSE_MODEL_GENERIC;
1507 ioctl(rodent.mfd, MOUSE_GETHWINFO, &rodent.hw);
1509 if (rodent.rtype != MOUSE_PROTO_UNKNOWN)
1510 bcopy(proto[rodent.rtype], cur_proto, sizeof(cur_proto));
1511 rodent.mode.protocol = MOUSE_PROTO_UNKNOWN;
1512 rodent.mode.rate = -1;
1513 rodent.mode.resolution = MOUSE_RES_UNKNOWN;
1514 rodent.mode.accelfactor = 0;
1515 rodent.mode.level = 0;
1516 if (ioctl(rodent.mfd, MOUSE_GETMODE, &rodent.mode) == 0) {
1517 if (rodent.mode.protocol == MOUSE_PROTO_UNKNOWN ||
1518 rodent.mode.protocol >= (int)(sizeof(proto) / sizeof(proto[0]))) {
1519 logwarnx("unknown mouse protocol (%d)", rodent.mode.protocol);
1520 return (MOUSE_PROTO_UNKNOWN);
1522 /* INPORT and BUS are the same... */
1523 if (rodent.mode.protocol == MOUSE_PROTO_INPORT)
1524 rodent.mode.protocol = MOUSE_PROTO_BUS;
1525 if (rodent.mode.protocol != rodent.rtype) {
1526 /* Hmm, the driver doesn't agree with the user... */
1527 if (rodent.rtype != MOUSE_PROTO_UNKNOWN)
1528 logwarnx("mouse type mismatch (%s != %s), %s is assumed",
1529 r_name(rodent.mode.protocol), r_name(rodent.rtype),
1530 r_name(rodent.mode.protocol));
1531 rodent.rtype = rodent.mode.protocol;
1532 bcopy(proto[rodent.rtype], cur_proto, sizeof(cur_proto));
1535 cur_proto[4] = rodent.mode.packetsize;
1536 cur_proto[0] = rodent.mode.syncmask[0]; /* header byte bit mask */
1537 cur_proto[1] = rodent.mode.syncmask[1]; /* header bit pattern */
1540 /* maybe this is a PnP mouse... */
1541 if (rodent.mode.protocol == MOUSE_PROTO_UNKNOWN) {
1543 if (rodent.flags & NoPnP)
1544 return (rodent.rtype);
1545 if (((len = pnpgets(pnpbuf)) <= 0) || !pnpparse(&pnpid, pnpbuf, len))
1546 return (rodent.rtype);
1548 debug("PnP serial mouse: '%*.*s' '%*.*s' '%*.*s'",
1549 pnpid.neisaid, pnpid.neisaid, pnpid.eisaid,
1550 pnpid.ncompat, pnpid.ncompat, pnpid.compat,
1551 pnpid.ndescription, pnpid.ndescription, pnpid.description);
1553 /* we have a valid PnP serial device ID */
1554 rodent.hw.iftype = MOUSE_IF_SERIAL;
1555 t = pnpproto(&pnpid);
1557 rodent.mode.protocol = t->val;
1558 rodent.hw.model = t->val2;
1560 rodent.mode.protocol = MOUSE_PROTO_UNKNOWN;
1562 if (rodent.mode.protocol == MOUSE_PROTO_INPORT)
1563 rodent.mode.protocol = MOUSE_PROTO_BUS;
1565 /* make final adjustment */
1566 if (rodent.mode.protocol != MOUSE_PROTO_UNKNOWN) {
1567 if (rodent.mode.protocol != rodent.rtype) {
1568 /* Hmm, the device doesn't agree with the user... */
1569 if (rodent.rtype != MOUSE_PROTO_UNKNOWN)
1570 logwarnx("mouse type mismatch (%s != %s), %s is assumed",
1571 r_name(rodent.mode.protocol), r_name(rodent.rtype),
1572 r_name(rodent.mode.protocol));
1573 rodent.rtype = rodent.mode.protocol;
1574 bcopy(proto[rodent.rtype], cur_proto, sizeof(cur_proto));
1579 debug("proto params: %02x %02x %02x %02x %d %02x %02x",
1580 cur_proto[0], cur_proto[1], cur_proto[2], cur_proto[3],
1581 cur_proto[4], cur_proto[5], cur_proto[6]);
1583 return (rodent.rtype);
1590 return (gettokenname(rifs, iftype));
1596 const char *unknown = "unknown";
1598 return (type == MOUSE_PROTO_UNKNOWN ||
1599 type >= (int)(sizeof(rnames) / sizeof(rnames[0])) ?
1600 unknown : rnames[type]);
1607 return (gettokenname(rmodels, model));
1613 unsigned char buf[16]; /* scrach buffer */
1620 ** This comment is a little out of context here, but it contains
1621 ** some useful information...
1622 ********************************************************************
1624 ** The following lines take care of the Logitech MouseMan protocols.
1626 ** NOTE: There are different versions of both MouseMan and TrackMan!
1627 ** Hence I add another protocol P_LOGIMAN, which the user can
1628 ** specify as MouseMan in his XF86Config file. This entry was
1629 ** formerly handled as a special case of P_MS. However, people
1630 ** who don't have the middle button problem, can still specify
1631 ** Microsoft and use P_MS.
1633 ** By default, these mice should use a 3 byte Microsoft protocol
1634 ** plus a 4th byte for the middle button. However, the mouse might
1635 ** have switched to a different protocol before we use it, so I send
1636 ** the proper sequence just in case.
1638 ** NOTE: - all commands to (at least the European) MouseMan have to
1639 ** be sent at 1200 Baud.
1640 ** - each command starts with a '*'.
1641 ** - whenever the MouseMan receives a '*', it will switch back
1642 ** to 1200 Baud. Hence I have to select the desired protocol
1643 ** first, then select the baud rate.
1645 ** The protocols supported by the (European) MouseMan are:
1646 ** - 5 byte packed binary protocol, as with the Mouse Systems
1647 ** mouse. Selected by sequence "*U".
1648 ** - 2 button 3 byte MicroSoft compatible protocol. Selected
1649 ** by sequence "*V".
1650 ** - 3 button 3+1 byte MicroSoft compatible protocol (default).
1651 ** Selected by sequence "*X".
1653 ** The following baud rates are supported:
1654 ** - 1200 Baud (default). Selected by sequence "*n".
1655 ** - 9600 Baud. Selected by sequence "*q".
1657 ** Selecting a sample rate is no longer supported with the MouseMan!
1658 ** Some additional lines in xf86Config.c take care of ill configured
1659 ** baud rates and sample rates. (The user will get an error.)
1662 switch (rodent.rtype) {
1664 case MOUSE_PROTO_LOGI:
1666 * The baud rate selection command must be sent at the current
1667 * baud rate; try all likely settings
1669 setmousespeed(9600, rodent.baudrate, rodentcflags[rodent.rtype]);
1670 setmousespeed(4800, rodent.baudrate, rodentcflags[rodent.rtype]);
1671 setmousespeed(2400, rodent.baudrate, rodentcflags[rodent.rtype]);
1672 setmousespeed(1200, rodent.baudrate, rodentcflags[rodent.rtype]);
1673 /* select MM series data format */
1674 write(rodent.mfd, "S", 1);
1675 setmousespeed(rodent.baudrate, rodent.baudrate,
1676 rodentcflags[MOUSE_PROTO_MM]);
1677 /* select report rate/frequency */
1678 if (rodent.rate <= 0) write(rodent.mfd, "O", 1);
1679 else if (rodent.rate <= 15) write(rodent.mfd, "J", 1);
1680 else if (rodent.rate <= 27) write(rodent.mfd, "K", 1);
1681 else if (rodent.rate <= 42) write(rodent.mfd, "L", 1);
1682 else if (rodent.rate <= 60) write(rodent.mfd, "R", 1);
1683 else if (rodent.rate <= 85) write(rodent.mfd, "M", 1);
1684 else if (rodent.rate <= 125) write(rodent.mfd, "Q", 1);
1685 else write(rodent.mfd, "N", 1);
1688 case MOUSE_PROTO_LOGIMOUSEMAN:
1689 /* The command must always be sent at 1200 baud */
1690 setmousespeed(1200, 1200, rodentcflags[rodent.rtype]);
1691 write(rodent.mfd, "*X", 2);
1692 setmousespeed(1200, rodent.baudrate, rodentcflags[rodent.rtype]);
1695 case MOUSE_PROTO_HITTAB:
1696 setmousespeed(1200, rodent.baudrate, rodentcflags[rodent.rtype]);
1699 * Initialize Hitachi PUMA Plus - Model 1212E to desired settings.
1700 * The tablet must be configured to be in MM mode, NO parity,
1701 * Binary Format. xf86Info.sampleRate controls the sensativity
1702 * of the tablet. We only use this tablet for it's 4-button puck
1703 * so we don't run in "Absolute Mode"
1705 write(rodent.mfd, "z8", 2); /* Set Parity = "NONE" */
1707 write(rodent.mfd, "zb", 2); /* Set Format = "Binary" */
1709 write(rodent.mfd, "@", 1); /* Set Report Mode = "Stream" */
1711 write(rodent.mfd, "R", 1); /* Set Output Rate = "45 rps" */
1713 write(rodent.mfd, "I\x20", 2); /* Set Incrememtal Mode "20" */
1715 write(rodent.mfd, "E", 1); /* Set Data Type = "Relative */
1718 /* Resolution is in 'lines per inch' on the Hitachi tablet */
1719 if (rodent.resolution == MOUSE_RES_LOW) c = 'g';
1720 else if (rodent.resolution == MOUSE_RES_MEDIUMLOW) c = 'e';
1721 else if (rodent.resolution == MOUSE_RES_MEDIUMHIGH) c = 'h';
1722 else if (rodent.resolution == MOUSE_RES_HIGH) c = 'd';
1723 else if (rodent.resolution <= 40) c = 'g';
1724 else if (rodent.resolution <= 100) c = 'd';
1725 else if (rodent.resolution <= 200) c = 'e';
1726 else if (rodent.resolution <= 500) c = 'h';
1727 else if (rodent.resolution <= 1000) c = 'j';
1729 write(rodent.mfd, &c, 1);
1732 write(rodent.mfd, "\021", 1); /* Resume DATA output */
1735 case MOUSE_PROTO_THINK:
1736 setmousespeed(1200, rodent.baudrate, rodentcflags[rodent.rtype]);
1737 /* the PnP ID string may be sent again, discard it */
1740 ioctl(rodent.mfd, TIOCFLUSH, &i);
1741 /* send the command to initialize the beast */
1742 for (s = "E5E5"; *s; ++s) {
1743 write(rodent.mfd, s, 1);
1745 FD_SET(rodent.mfd, &fds);
1746 if (select(FD_SETSIZE, &fds, NULL, NULL, NULL) <= 0)
1748 read(rodent.mfd, &c, 1);
1755 case MOUSE_PROTO_JOGDIAL:
1757 case MOUSE_PROTO_MSC:
1758 setmousespeed(1200, rodent.baudrate, rodentcflags[rodent.rtype]);
1759 if (rodent.flags & ClearDTR) {
1761 ioctl(rodent.mfd, TIOCMBIC, &i);
1763 if (rodent.flags & ClearRTS) {
1765 ioctl(rodent.mfd, TIOCMBIC, &i);
1769 case MOUSE_PROTO_SYSMOUSE:
1770 if (rodent.hw.iftype == MOUSE_IF_SYSMOUSE)
1771 setmousespeed(1200, rodent.baudrate, rodentcflags[rodent.rtype]);
1774 case MOUSE_PROTO_BUS:
1775 case MOUSE_PROTO_INPORT:
1776 case MOUSE_PROTO_PS2:
1777 if (rodent.rate >= 0)
1778 rodent.mode.rate = rodent.rate;
1779 if (rodent.resolution != MOUSE_RES_UNKNOWN)
1780 rodent.mode.resolution = rodent.resolution;
1781 ioctl(rodent.mfd, MOUSE_SETMODE, &rodent.mode);
1784 case MOUSE_PROTO_X10MOUSEREM:
1785 mremote_serversetup();
1786 setmousespeed(1200, rodent.baudrate, rodentcflags[rodent.rtype]);
1790 case MOUSE_PROTO_VERSAPAD:
1791 tcsendbreak(rodent.mfd, 0); /* send break for 400 msec */
1793 ioctl(rodent.mfd, TIOCFLUSH, &i);
1794 for (i = 0; i < 7; ++i) {
1796 FD_SET(rodent.mfd, &fds);
1797 if (select(FD_SETSIZE, &fds, NULL, NULL, NULL) <= 0)
1799 read(rodent.mfd, &c, 1);
1803 if ((buf[0] != 'V') || (buf[1] != 'P')|| (buf[7] != '\r'))
1805 setmousespeed(9600, rodent.baudrate, rodentcflags[rodent.rtype]);
1806 tcsendbreak(rodent.mfd, 0); /* send break for 400 msec again */
1807 for (i = 0; i < 7; ++i) {
1809 FD_SET(rodent.mfd, &fds);
1810 if (select(FD_SETSIZE, &fds, NULL, NULL, NULL) <= 0)
1812 read(rodent.mfd, &c, 1);
1818 ioctl(rodent.mfd, TIOCFLUSH, &i);
1822 setmousespeed(1200, rodent.baudrate, rodentcflags[rodent.rtype]);
1828 r_protocol(u_char rBuf, mousestatus_t *act)
1830 /* MOUSE_MSS_BUTTON?DOWN -> MOUSE_BUTTON?DOWN */
1831 static int butmapmss[4] = { /* Microsoft, MouseMan, GlidePoint,
1832 IntelliMouse, Thinking Mouse */
1836 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1838 static int butmapmss2[4] = { /* Microsoft, MouseMan, GlidePoint,
1843 MOUSE_BUTTON2DOWN | MOUSE_BUTTON4DOWN,
1845 /* MOUSE_INTELLI_BUTTON?DOWN -> MOUSE_BUTTON?DOWN */
1846 static int butmapintelli[4] = { /* IntelliMouse, NetMouse, Mie Mouse,
1851 MOUSE_BUTTON2DOWN | MOUSE_BUTTON4DOWN,
1853 /* MOUSE_MSC_BUTTON?UP -> MOUSE_BUTTON?DOWN */
1854 static int butmapmsc[8] = { /* MouseSystems, MMSeries, Logitech,
1859 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
1861 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1862 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
1863 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
1865 /* MOUSE_PS2_BUTTON?DOWN -> MOUSE_BUTTON?DOWN */
1866 static int butmapps2[8] = { /* PS/2 */
1870 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1872 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
1873 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
1874 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
1876 /* for Hitachi tablet */
1877 static int butmaphit[8] = { /* MM HitTablet */
1887 /* for serial VersaPad */
1888 static int butmapversa[8] = { /* VersaPad */
1895 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1896 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1898 /* for PS/2 VersaPad */
1899 static int butmapversaps2[8] = { /* VersaPad */
1905 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1907 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1909 static int pBufP = 0;
1910 static unsigned char pBuf[8];
1911 static int prev_x, prev_y;
1912 static int on = FALSE;
1915 debug("received char 0x%x",(int)rBuf);
1916 if (rodent.rtype == MOUSE_PROTO_KIDSPAD)
1917 return (kidspad(rBuf, act));
1918 if (rodent.rtype == MOUSE_PROTO_GTCO_DIGIPAD)
1919 return (gtco_digipad(rBuf, act));
1922 * Hack for resyncing: We check here for a package that is:
1923 * a) illegal (detected by wrong data-package header)
1924 * b) invalid (0x80 == -128 and that might be wrong for MouseSystems)
1925 * c) bad header-package
1927 * NOTE: b) is a voilation of the MouseSystems-Protocol, since values of
1928 * -128 are allowed, but since they are very seldom we can easily
1929 * use them as package-header with no button pressed.
1930 * NOTE/2: On a PS/2 mouse any byte is valid as a data byte. Furthermore,
1931 * 0x80 is not valid as a header byte. For a PS/2 mouse we skip
1932 * checking data bytes.
1933 * For resyncing a PS/2 mouse we require the two most significant
1934 * bits in the header byte to be 0. These are the overflow bits,
1935 * and in case of an overflow we actually lose sync. Overflows
1936 * are very rare, however, and we quickly gain sync again after
1937 * an overflow condition. This is the best we can do. (Actually,
1938 * we could use bit 0x08 in the header byte for resyncing, since
1939 * that bit is supposed to be always on, but nobody told
1943 if (pBufP != 0 && rodent.rtype != MOUSE_PROTO_PS2 &&
1944 ((rBuf & cur_proto[2]) != cur_proto[3] || rBuf == 0x80))
1946 pBufP = 0; /* skip package */
1949 if (pBufP == 0 && (rBuf & cur_proto[0]) != cur_proto[1])
1952 /* is there an extra data byte? */
1953 if (pBufP >= cur_proto[4] && (rBuf & cur_proto[0]) != cur_proto[1])
1956 * Hack for Logitech MouseMan Mouse - Middle button
1958 * Unfortunately this mouse has variable length packets: the standard
1959 * Microsoft 3 byte packet plus an optional 4th byte whenever the
1960 * middle button status changes.
1962 * We have already processed the standard packet with the movement
1963 * and button info. Now post an event message with the old status
1964 * of the left and right buttons and the updated middle button.
1968 * Even worse, different MouseMen and TrackMen differ in the 4th
1969 * byte: some will send 0x00/0x20, others 0x01/0x21, or even
1970 * 0x02/0x22, so I have to strip off the lower bits.
1973 * HACK for ALPS "fourth button". (It's bit 0x10 of the "fourth byte"
1974 * and it is activated by tapping the glidepad with the finger! 8^)
1975 * We map it to bit bit3, and the reverse map in xf86Events just has
1976 * to be extended so that it is identified as Button 4. The lower
1977 * half of the reverse-map may remain unchanged.
1982 * Receive the fourth byte only when preceding three bytes have
1983 * been detected (pBufP >= cur_proto[4]). In the previous
1984 * versions, the test was pBufP == 0; thus, we may have mistakingly
1985 * received a byte even if we didn't see anything preceding
1989 if ((rBuf & cur_proto[5]) != cur_proto[6]) {
1994 switch (rodent.rtype) {
1996 case MOUSE_PROTO_MARIQUA:
1998 * This mouse has 16! buttons in addition to the standard
1999 * three of them. They return 0x10 though 0x1f in the
2000 * so-called `ten key' mode and 0x30 though 0x3f in the
2001 * `function key' mode. As there are only 31 bits for
2002 * button state (including the standard three), we ignore
2003 * the bit 0x20 and don't distinguish the two modes.
2005 act->dx = act->dy = act->dz = 0;
2006 act->obutton = act->button;
2008 act->button = (1 << (rBuf - 13))
2009 | (act->obutton & (MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN));
2011 * FIXME: this is a button "down" event. There needs to be
2012 * a corresponding button "up" event... XXX
2016 case MOUSE_PROTO_JOGDIAL:
2020 * IntelliMouse, NetMouse (including NetMouse Pro) and Mie Mouse
2021 * always send the fourth byte, whereas the fourth byte is
2022 * optional for GlidePoint and ThinkingMouse. The fourth byte
2023 * is also optional for MouseMan+ and FirstMouse+ in their
2024 * native mode. It is always sent if they are in the IntelliMouse
2027 case MOUSE_PROTO_INTELLI: /* IntelliMouse, NetMouse, Mie Mouse,
2029 act->dx = act->dy = 0;
2030 act->dz = (rBuf & 0x08) ? (rBuf & 0x0f) - 16 : (rBuf & 0x0f);
2031 if ((act->dz >= 7) || (act->dz <= -7))
2033 act->obutton = act->button;
2034 act->button = butmapintelli[(rBuf & MOUSE_MSS_BUTTONS) >> 4]
2035 | (act->obutton & (MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN));
2039 act->dx = act->dy = act->dz = 0;
2040 act->obutton = act->button;
2041 act->button = butmapmss2[(rBuf & MOUSE_MSS_BUTTONS) >> 4]
2042 | (act->obutton & (MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN));
2046 act->flags = ((act->dx || act->dy || act->dz) ? MOUSE_POSCHANGED : 0)
2047 | (act->obutton ^ act->button);
2049 return (act->flags);
2052 if (pBufP >= cur_proto[4])
2054 pBuf[pBufP++] = rBuf;
2055 if (pBufP != cur_proto[4])
2059 * assembly full package
2062 debug("assembled full packet (len %d) %x,%x,%x,%x,%x,%x,%x,%x",
2064 pBuf[0], pBuf[1], pBuf[2], pBuf[3],
2065 pBuf[4], pBuf[5], pBuf[6], pBuf[7]);
2068 act->obutton = act->button;
2069 switch (rodent.rtype)
2071 case MOUSE_PROTO_MS: /* Microsoft */
2072 case MOUSE_PROTO_LOGIMOUSEMAN: /* MouseMan/TrackMan */
2073 case MOUSE_PROTO_X10MOUSEREM: /* X10 MouseRemote */
2074 act->button = act->obutton & MOUSE_BUTTON4DOWN;
2075 if (rodent.flags & ChordMiddle)
2076 act->button |= ((pBuf[0] & MOUSE_MSS_BUTTONS) == MOUSE_MSS_BUTTONS)
2078 : butmapmss[(pBuf[0] & MOUSE_MSS_BUTTONS) >> 4];
2080 act->button |= (act->obutton & MOUSE_BUTTON2DOWN)
2081 | butmapmss[(pBuf[0] & MOUSE_MSS_BUTTONS) >> 4];
2083 /* Send X10 btn events to remote client (ensure -128-+127 range) */
2084 if ((rodent.rtype == MOUSE_PROTO_X10MOUSEREM) &&
2085 ((pBuf[0] & 0xFC) == 0x44) && (pBuf[2] == 0x3F)) {
2086 if (rodent.mremcfd >= 0) {
2087 unsigned char key = (signed char)(((pBuf[0] & 0x03) << 6) |
2089 write(rodent.mremcfd, &key, 1);
2094 act->dx = (signed char)(((pBuf[0] & 0x03) << 6) | (pBuf[1] & 0x3F));
2095 act->dy = (signed char)(((pBuf[0] & 0x0C) << 4) | (pBuf[2] & 0x3F));
2098 case MOUSE_PROTO_GLIDEPOINT: /* GlidePoint */
2099 case MOUSE_PROTO_THINK: /* ThinkingMouse */
2100 case MOUSE_PROTO_INTELLI: /* IntelliMouse, NetMouse, Mie Mouse,
2102 act->button = (act->obutton & (MOUSE_BUTTON2DOWN | MOUSE_BUTTON4DOWN))
2103 | butmapmss[(pBuf[0] & MOUSE_MSS_BUTTONS) >> 4];
2104 act->dx = (signed char)(((pBuf[0] & 0x03) << 6) | (pBuf[1] & 0x3F));
2105 act->dy = (signed char)(((pBuf[0] & 0x0C) << 4) | (pBuf[2] & 0x3F));
2108 case MOUSE_PROTO_MSC: /* MouseSystems Corp */
2110 case MOUSE_PROTO_MARIQUA: /* Mariqua */
2112 act->button = butmapmsc[(~pBuf[0]) & MOUSE_MSC_BUTTONS];
2113 act->dx = (signed char)(pBuf[1]) + (signed char)(pBuf[3]);
2114 act->dy = - ((signed char)(pBuf[2]) + (signed char)(pBuf[4]));
2117 case MOUSE_PROTO_JOGDIAL: /* JogDial */
2123 act->button = MOUSE_BUTTON1DOWN;
2128 case MOUSE_PROTO_HITTAB: /* MM HitTablet */
2129 act->button = butmaphit[pBuf[0] & 0x07];
2130 act->dx = (pBuf[0] & MOUSE_MM_XPOSITIVE) ? pBuf[1] : - pBuf[1];
2131 act->dy = (pBuf[0] & MOUSE_MM_YPOSITIVE) ? - pBuf[2] : pBuf[2];
2134 case MOUSE_PROTO_MM: /* MM Series */
2135 case MOUSE_PROTO_LOGI: /* Logitech Mice */
2136 act->button = butmapmsc[pBuf[0] & MOUSE_MSC_BUTTONS];
2137 act->dx = (pBuf[0] & MOUSE_MM_XPOSITIVE) ? pBuf[1] : - pBuf[1];
2138 act->dy = (pBuf[0] & MOUSE_MM_YPOSITIVE) ? - pBuf[2] : pBuf[2];
2141 case MOUSE_PROTO_VERSAPAD: /* VersaPad */
2142 act->button = butmapversa[(pBuf[0] & MOUSE_VERSA_BUTTONS) >> 3];
2143 act->button |= (pBuf[0] & MOUSE_VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
2144 act->dx = act->dy = 0;
2145 if (!(pBuf[0] & MOUSE_VERSA_IN_USE)) {
2149 x = (pBuf[2] << 6) | pBuf[1];
2152 y = (pBuf[4] << 6) | pBuf[3];
2156 act->dx = prev_x - x;
2157 act->dy = prev_y - y;
2165 case MOUSE_PROTO_BUS: /* Bus */
2166 case MOUSE_PROTO_INPORT: /* InPort */
2167 act->button = butmapmsc[(~pBuf[0]) & MOUSE_MSC_BUTTONS];
2168 act->dx = (signed char)pBuf[1];
2169 act->dy = - (signed char)pBuf[2];
2172 case MOUSE_PROTO_PS2: /* PS/2 */
2173 act->button = butmapps2[pBuf[0] & MOUSE_PS2_BUTTONS];
2174 act->dx = (pBuf[0] & MOUSE_PS2_XNEG) ? pBuf[1] - 256 : pBuf[1];
2175 act->dy = (pBuf[0] & MOUSE_PS2_YNEG) ? -(pBuf[2] - 256) : -pBuf[2];
2177 * Moused usually operates the psm driver at the operation level 1
2178 * which sends mouse data in MOUSE_PROTO_SYSMOUSE protocol.
2179 * The following code takes effect only when the user explicitly
2180 * requets the level 2 at which wheel movement and additional button
2181 * actions are encoded in model-dependent formats. At the level 0
2182 * the following code is no-op because the psm driver says the model
2183 * is MOUSE_MODEL_GENERIC.
2185 switch (rodent.hw.model) {
2186 case MOUSE_MODEL_EXPLORER:
2187 /* wheel and additional button data is in the fourth byte */
2188 act->dz = (pBuf[3] & MOUSE_EXPLORER_ZNEG)
2189 ? (pBuf[3] & 0x0f) - 16 : (pBuf[3] & 0x0f);
2190 act->button |= (pBuf[3] & MOUSE_EXPLORER_BUTTON4DOWN)
2191 ? MOUSE_BUTTON4DOWN : 0;
2192 act->button |= (pBuf[3] & MOUSE_EXPLORER_BUTTON5DOWN)
2193 ? MOUSE_BUTTON5DOWN : 0;
2195 case MOUSE_MODEL_INTELLI:
2196 case MOUSE_MODEL_NET:
2197 /* wheel data is in the fourth byte */
2198 act->dz = (signed char)pBuf[3];
2199 if ((act->dz >= 7) || (act->dz <= -7))
2201 /* some compatible mice may have additional buttons */
2202 act->button |= (pBuf[0] & MOUSE_PS2INTELLI_BUTTON4DOWN)
2203 ? MOUSE_BUTTON4DOWN : 0;
2204 act->button |= (pBuf[0] & MOUSE_PS2INTELLI_BUTTON5DOWN)
2205 ? MOUSE_BUTTON5DOWN : 0;
2207 case MOUSE_MODEL_MOUSEMANPLUS:
2208 if (((pBuf[0] & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC)
2209 && (abs(act->dx) > 191)
2210 && MOUSE_PS2PLUS_CHECKBITS(pBuf)) {
2211 /* the extended data packet encodes button and wheel events */
2212 switch (MOUSE_PS2PLUS_PACKET_TYPE(pBuf)) {
2214 /* wheel data packet */
2215 act->dx = act->dy = 0;
2216 if (pBuf[2] & 0x80) {
2217 /* horizontal roller count - ignore it XXX*/
2219 /* vertical roller count */
2220 act->dz = (pBuf[2] & MOUSE_PS2PLUS_ZNEG)
2221 ? (pBuf[2] & 0x0f) - 16 : (pBuf[2] & 0x0f);
2223 act->button |= (pBuf[2] & MOUSE_PS2PLUS_BUTTON4DOWN)
2224 ? MOUSE_BUTTON4DOWN : 0;
2225 act->button |= (pBuf[2] & MOUSE_PS2PLUS_BUTTON5DOWN)
2226 ? MOUSE_BUTTON5DOWN : 0;
2229 /* this packet type is reserved by Logitech */
2231 * IBM ScrollPoint Mouse uses this packet type to
2232 * encode both vertical and horizontal scroll movement.
2234 act->dx = act->dy = 0;
2235 /* horizontal roller count */
2237 act->dz = (pBuf[2] & MOUSE_SPOINT_WNEG) ? -2 : 2;
2238 /* vertical roller count */
2240 act->dz = (pBuf[2] & MOUSE_SPOINT_ZNEG) ? -1 : 1;
2242 /* vertical roller count */
2243 act->dz = (pBuf[2] & MOUSE_SPOINT_ZNEG)
2244 ? ((pBuf[2] >> 4) & 0x0f) - 16
2245 : ((pBuf[2] >> 4) & 0x0f);
2246 /* horizontal roller count */
2247 act->dw = (pBuf[2] & MOUSE_SPOINT_WNEG)
2248 ? (pBuf[2] & 0x0f) - 16 : (pBuf[2] & 0x0f);
2252 /* device type packet - shouldn't happen */
2255 act->dx = act->dy = 0;
2256 act->button = act->obutton;
2257 debug("unknown PS2++ packet type %d: 0x%02x 0x%02x 0x%02x\n",
2258 MOUSE_PS2PLUS_PACKET_TYPE(pBuf),
2259 pBuf[0], pBuf[1], pBuf[2]);
2263 /* preserve button states */
2264 act->button |= act->obutton & MOUSE_EXTBUTTONS;
2267 case MOUSE_MODEL_GLIDEPOINT:
2268 /* `tapping' action */
2269 act->button |= ((pBuf[0] & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
2271 case MOUSE_MODEL_NETSCROLL:
2272 /* three additional bytes encode buttons and wheel events */
2273 act->button |= (pBuf[3] & MOUSE_PS2_BUTTON3DOWN)
2274 ? MOUSE_BUTTON4DOWN : 0;
2275 act->button |= (pBuf[3] & MOUSE_PS2_BUTTON1DOWN)
2276 ? MOUSE_BUTTON5DOWN : 0;
2277 act->dz = (pBuf[3] & MOUSE_PS2_XNEG) ? pBuf[4] - 256 : pBuf[4];
2279 case MOUSE_MODEL_THINK:
2280 /* the fourth button state in the first byte */
2281 act->button |= (pBuf[0] & MOUSE_PS2_TAP) ? MOUSE_BUTTON4DOWN : 0;
2283 case MOUSE_MODEL_VERSAPAD:
2284 act->button = butmapversaps2[pBuf[0] & MOUSE_PS2VERSA_BUTTONS];
2286 (pBuf[0] & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
2287 act->dx = act->dy = 0;
2288 if (!(pBuf[0] & MOUSE_PS2VERSA_IN_USE)) {
2292 x = ((pBuf[4] << 8) & 0xf00) | pBuf[1];
2295 y = ((pBuf[4] << 4) & 0xf00) | pBuf[2];
2299 act->dx = prev_x - x;
2300 act->dy = prev_y - y;
2307 case MOUSE_MODEL_4D:
2308 act->dx = (pBuf[1] & 0x80) ? pBuf[1] - 256 : pBuf[1];
2309 act->dy = (pBuf[2] & 0x80) ? -(pBuf[2] - 256) : -pBuf[2];
2310 switch (pBuf[0] & MOUSE_4D_WHEELBITS) {
2317 case 0x40: /* 2nd wheel rolling right XXX */
2320 case 0xc0: /* 2nd wheel rolling left XXX */
2325 case MOUSE_MODEL_4DPLUS:
2326 if ((act->dx < 16 - 256) && (act->dy > 256 - 16)) {
2327 act->dx = act->dy = 0;
2328 if (pBuf[2] & MOUSE_4DPLUS_BUTTON4DOWN)
2329 act->button |= MOUSE_BUTTON4DOWN;
2330 act->dz = (pBuf[2] & MOUSE_4DPLUS_ZNEG)
2331 ? ((pBuf[2] & 0x07) - 8) : (pBuf[2] & 0x07);
2333 /* preserve previous button states */
2334 act->button |= act->obutton & MOUSE_EXTBUTTONS;
2337 case MOUSE_MODEL_GENERIC:
2343 case MOUSE_PROTO_SYSMOUSE: /* sysmouse */
2344 act->button = butmapmsc[(~pBuf[0]) & MOUSE_SYS_STDBUTTONS];
2345 act->dx = (signed char)(pBuf[1]) + (signed char)(pBuf[3]);
2346 act->dy = - ((signed char)(pBuf[2]) + (signed char)(pBuf[4]));
2347 if (rodent.level == 1) {
2348 act->dz = ((signed char)(pBuf[5] << 1) + (signed char)(pBuf[6] << 1)) >> 1;
2349 act->button |= ((~pBuf[7] & MOUSE_SYS_EXTBUTTONS) << 3);
2357 * We don't reset pBufP here yet, as there may be an additional data
2358 * byte in some protocols. See above.
2361 /* has something changed? */
2362 act->flags = ((act->dx || act->dy || act->dz) ? MOUSE_POSCHANGED : 0)
2363 | (act->obutton ^ act->button);
2365 return (act->flags);
2369 r_statetrans(mousestatus_t *a1, mousestatus_t *a2, int trans)
2377 a2->obutton = a2->button;
2378 a2->button = a1->button;
2379 a2->flags = a1->flags;
2382 if (rodent.flags & Emulate3Button) {
2384 debug("state:%d, trans:%d -> state:%d",
2385 mouse_button_state, trans,
2386 states[mouse_button_state].s[trans]);
2388 * Avoid re-ordering button and movement events. While a button
2389 * event is deferred, throw away up to BUTTON2_MAXMOVE movement
2390 * events to allow for mouse jitter. If more movement events
2391 * occur, then complete the deferred button events immediately.
2393 if ((a2->dx != 0 || a2->dy != 0) &&
2394 S_DELAYED(states[mouse_button_state].s[trans])) {
2395 if (++mouse_move_delayed > BUTTON2_MAXMOVE) {
2396 mouse_move_delayed = 0;
2397 mouse_button_state =
2398 states[mouse_button_state].s[A_TIMEOUT];
2401 a2->dx = a2->dy = 0;
2403 mouse_move_delayed = 0;
2404 if (mouse_button_state != states[mouse_button_state].s[trans])
2407 clock_gettime(CLOCK_MONOTONIC_FAST, &mouse_button_state_ts);
2408 mouse_button_state = states[mouse_button_state].s[trans];
2410 ~(MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN);
2411 a2->button &= states[mouse_button_state].mask;
2412 a2->button |= states[mouse_button_state].buttons;
2413 flags = a2->flags & MOUSE_POSCHANGED;
2414 flags |= a2->obutton ^ a2->button;
2415 if (flags & MOUSE_BUTTON2DOWN) {
2416 a2->flags = flags & MOUSE_BUTTON2DOWN;
2424 /* phisical to logical button mapping */
2425 static int p2l[MOUSE_MAXBUTTON] = {
2426 MOUSE_BUTTON1DOWN, MOUSE_BUTTON2DOWN, MOUSE_BUTTON3DOWN, MOUSE_BUTTON4DOWN,
2427 MOUSE_BUTTON5DOWN, MOUSE_BUTTON6DOWN, MOUSE_BUTTON7DOWN, MOUSE_BUTTON8DOWN,
2428 0x00000100, 0x00000200, 0x00000400, 0x00000800,
2429 0x00001000, 0x00002000, 0x00004000, 0x00008000,
2430 0x00010000, 0x00020000, 0x00040000, 0x00080000,
2431 0x00100000, 0x00200000, 0x00400000, 0x00800000,
2432 0x01000000, 0x02000000, 0x04000000, 0x08000000,
2433 0x10000000, 0x20000000, 0x40000000,
2445 r_installmap(char *arg)
2452 arg = skipspace(arg);
2454 while (isdigit(*arg))
2456 arg = skipspace(arg);
2457 if ((arg <= s) || (*arg != '='))
2461 arg = skipspace(++arg);
2463 while (isdigit(*arg))
2465 if ((arg <= s) || (!isspace(*arg) && (*arg != '\0')))
2469 if ((lbutton <= 0) || (lbutton > MOUSE_MAXBUTTON))
2471 if ((pbutton <= 0) || (pbutton > MOUSE_MAXBUTTON))
2473 p2l[pbutton - 1] = 1 << (lbutton - 1);
2474 mstate[lbutton - 1] = &bstate[pbutton - 1];
2481 r_map(mousestatus_t *act1, mousestatus_t *act2)
2484 register int pbuttons;
2487 pbuttons = act1->button;
2490 act2->obutton = act2->button;
2491 if (pbuttons & rodent.wmode) {
2492 pbuttons &= ~rodent.wmode;
2493 act1->dz = act1->dy;
2497 act2->dx = act1->dx;
2498 act2->dy = act1->dy;
2499 act2->dz = act1->dz;
2501 switch (rodent.zmap[0]) {
2502 case 0: /* do nothing */
2505 if (act1->dz != 0) {
2506 act2->dx = act1->dz;
2511 if (act1->dz != 0) {
2512 act2->dy = act1->dz;
2516 default: /* buttons */
2517 pbuttons &= ~(rodent.zmap[0] | rodent.zmap[1]
2518 | rodent.zmap[2] | rodent.zmap[3]);
2519 if ((act1->dz < -1) && rodent.zmap[2]) {
2520 pbuttons |= rodent.zmap[2];
2521 zstate[2].count = 1;
2522 } else if (act1->dz < 0) {
2523 pbuttons |= rodent.zmap[0];
2524 zstate[0].count = 1;
2525 } else if ((act1->dz > 1) && rodent.zmap[3]) {
2526 pbuttons |= rodent.zmap[3];
2527 zstate[3].count = 1;
2528 } else if (act1->dz > 0) {
2529 pbuttons |= rodent.zmap[1];
2530 zstate[1].count = 1;
2536 for (pb = 0; (pb < MOUSE_MAXBUTTON) && (pbuttons != 0); ++pb) {
2537 lbuttons |= (pbuttons & 1) ? p2l[pb] : 0;
2540 act2->button = lbuttons;
2542 act2->flags = ((act2->dx || act2->dy || act2->dz) ? MOUSE_POSCHANGED : 0)
2543 | (act2->obutton ^ act2->button);
2547 r_timestamp(mousestatus_t *act)
2550 struct timespec ts1;
2551 struct timespec ts2;
2552 struct timespec ts3;
2557 mask = act->flags & MOUSE_BUTTONS;
2563 clock_gettime(CLOCK_MONOTONIC_FAST, &ts1);
2564 drift_current_ts = ts1;
2566 /* double click threshold */
2567 ts2.tv_sec = rodent.clickthreshold / 1000;
2568 ts2.tv_nsec = (rodent.clickthreshold % 1000) * 1000000;
2569 tssub(&ts1, &ts2, &ts);
2570 debug("ts: %jd %ld", (intmax_t)ts.tv_sec, ts.tv_nsec);
2572 /* 3 button emulation timeout */
2573 ts2.tv_sec = rodent.button2timeout / 1000;
2574 ts2.tv_nsec = (rodent.button2timeout % 1000) * 1000000;
2575 tssub(&ts1, &ts2, &ts3);
2577 button = MOUSE_BUTTON1DOWN;
2578 for (i = 0; (i < MOUSE_MAXBUTTON) && (mask != 0); ++i) {
2580 if (act->button & button) {
2581 /* the button is down */
2583 (intmax_t)bstate[i].ts.tv_sec, bstate[i].ts.tv_nsec);
2584 if (tscmp(&ts, &bstate[i].ts, >)) {
2585 bstate[i].count = 1;
2591 /* the button is up */
2595 if (act->button & button) {
2596 /* the button has been down */
2597 if (tscmp(&ts3, &bstate[i].ts, >)) {
2598 bstate[i].count = 1;
2600 act->flags |= button;
2601 debug("button %d timeout", i + 1);
2604 /* the button has been up */
2616 struct timespec ts1;
2617 struct timespec ts2;
2619 if (states[mouse_button_state].timeout)
2621 clock_gettime(CLOCK_MONOTONIC_FAST, &ts1);
2622 ts2.tv_sec = rodent.button2timeout / 1000;
2623 ts2.tv_nsec = (rodent.button2timeout % 1000) * 1000000;
2624 tssub(&ts1, &ts2, &ts);
2625 return (tscmp(&ts, &mouse_button_state_ts, >));
2629 r_click(mousestatus_t *act)
2631 struct mouse_info mouse;
2636 mask = act->flags & MOUSE_BUTTONS;
2640 button = MOUSE_BUTTON1DOWN;
2641 for (i = 0; (i < MOUSE_MAXBUTTON) && (mask != 0); ++i) {
2643 debug("mstate[%d]->count:%d", i, mstate[i]->count);
2644 if (act->button & button) {
2645 /* the button is down */
2646 mouse.u.event.value = mstate[i]->count;
2648 /* the button is up */
2649 mouse.u.event.value = 0;
2651 mouse.operation = MOUSE_BUTTON_EVENT;
2652 mouse.u.event.id = button;
2655 ioctl(rodent.cfd, CONS_MOUSECTL, &mouse);
2656 debug("button %d count %d", i + 1, mouse.u.event.value);
2663 /* $XConsortium: posix_tty.c,v 1.3 95/01/05 20:42:55 kaleb Exp $ */
2664 /* $XFree86: xc/programs/Xserver/hw/xfree86/os-support/shared/posix_tty.c,v 3.4 1995/01/28 17:05:03 dawes Exp $ */
2666 * Copyright 1993 by David Dawes <dawes@physics.su.oz.au>
2668 * Permission to use, copy, modify, distribute, and sell this software and its
2669 * documentation for any purpose is hereby granted without fee, provided that
2670 * the above copyright notice appear in all copies and that both that
2671 * copyright notice and this permission notice appear in supporting
2672 * documentation, and that the name of David Dawes
2673 * not be used in advertising or publicity pertaining to distribution of
2674 * the software without specific, written prior permission.
2675 * David Dawes makes no representations about the suitability of this
2676 * software for any purpose. It is provided "as is" without express or
2679 * DAVID DAWES DISCLAIMS ALL WARRANTIES WITH REGARD TO
2680 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
2681 * FITNESS, IN NO EVENT SHALL DAVID DAWES BE LIABLE FOR
2682 * ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
2683 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
2684 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
2685 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2691 setmousespeed(int old, int new, unsigned cflag)
2696 if (tcgetattr(rodent.mfd, &tty) < 0)
2698 logwarn("unable to get status of mouse fd");
2702 tty.c_iflag = IGNBRK | IGNPAR;
2705 tty.c_cflag = (tcflag_t)cflag;
2706 tty.c_cc[VTIME] = 0;
2712 cfsetispeed(&tty, B9600);
2713 cfsetospeed(&tty, B9600);
2716 cfsetispeed(&tty, B4800);
2717 cfsetospeed(&tty, B4800);
2720 cfsetispeed(&tty, B2400);
2721 cfsetospeed(&tty, B2400);
2725 cfsetispeed(&tty, B1200);
2726 cfsetospeed(&tty, B1200);
2729 if (tcsetattr(rodent.mfd, TCSADRAIN, &tty) < 0)
2731 logwarn("unable to set status of mouse fd");
2739 cfsetispeed(&tty, B9600);
2740 cfsetospeed(&tty, B9600);
2744 cfsetispeed(&tty, B4800);
2745 cfsetospeed(&tty, B4800);
2749 cfsetispeed(&tty, B2400);
2750 cfsetospeed(&tty, B2400);
2755 cfsetispeed(&tty, B1200);
2756 cfsetospeed(&tty, B1200);
2759 if (rodent.rtype == MOUSE_PROTO_LOGIMOUSEMAN
2760 || rodent.rtype == MOUSE_PROTO_LOGI)
2762 if (write(rodent.mfd, c, 2) != 2)
2764 logwarn("unable to write to mouse fd");
2770 if (tcsetattr(rodent.mfd, TCSADRAIN, &tty) < 0)
2771 logwarn("unable to set status of mouse fd");
2775 * PnP COM device support
2777 * It's a simplistic implementation, but it works :-)
2782 * Try to elicit a PnP ID as described in
2783 * Microsoft, Hayes: "Plug and Play External COM Device Specification,
2786 * The routine does not fully implement the COM Enumerator as par Section
2787 * 2.1 of the document. In particular, we don't have idle state in which
2788 * the driver software monitors the com port for dynamic connection or
2789 * removal of a device at the port, because `moused' simply quits if no
2792 * In addition, as PnP COM device enumeration procedure slightly has
2793 * changed since its first publication, devices which follow earlier
2794 * revisions of the above spec. may fail to respond if the rev 1.0
2795 * procedure is used. XXX
2800 struct timeval timeout;
2805 * This is the procedure described in rev 1.0 of PnP COM device spec.
2806 * Unfortunately, some devices which comform to earlier revisions of
2807 * the spec gets confused and do not return the ID string...
2809 debug("PnP COM device rev 1.0 probe...");
2811 /* port initialization (2.1.2) */
2812 ioctl(rodent.mfd, TIOCMGET, &i);
2813 i |= TIOCM_DTR; /* DTR = 1 */
2814 i &= ~TIOCM_RTS; /* RTS = 0 */
2815 ioctl(rodent.mfd, TIOCMSET, &i);
2819 * The PnP COM device spec. dictates that the mouse must set DSR
2820 * in response to DTR (by hardware or by software) and that if DSR is
2821 * not asserted, the host computer should think that there is no device
2822 * at this serial port. But some mice just don't do that...
2824 ioctl(rodent.mfd, TIOCMGET, &i);
2825 debug("modem status 0%o", i);
2826 if ((i & TIOCM_DSR) == 0)
2829 /* port setup, 1st phase (2.1.3) */
2830 setmousespeed(1200, 1200, (CS7 | CREAD | CLOCAL | HUPCL));
2831 i = TIOCM_DTR | TIOCM_RTS; /* DTR = 0, RTS = 0 */
2832 ioctl(rodent.mfd, TIOCMBIC, &i);
2834 i = TIOCM_DTR; /* DTR = 1, RTS = 0 */
2835 ioctl(rodent.mfd, TIOCMBIS, &i);
2838 /* wait for response, 1st phase (2.1.4) */
2840 ioctl(rodent.mfd, TIOCFLUSH, &i);
2841 i = TIOCM_RTS; /* DTR = 1, RTS = 1 */
2842 ioctl(rodent.mfd, TIOCMBIS, &i);
2844 /* try to read something */
2846 FD_SET(rodent.mfd, &fds);
2848 timeout.tv_usec = 240000;
2849 if (select(FD_SETSIZE, &fds, NULL, NULL, &timeout) > 0) {
2850 debug("pnpwakeup1(): valid response in first phase.");
2854 /* port setup, 2nd phase (2.1.5) */
2855 i = TIOCM_DTR | TIOCM_RTS; /* DTR = 0, RTS = 0 */
2856 ioctl(rodent.mfd, TIOCMBIC, &i);
2859 /* wait for respose, 2nd phase (2.1.6) */
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("pnpwakeup1(): valid response in second phase.");
2881 struct timeval timeout;
2886 * This is a simplified procedure; it simply toggles RTS.
2888 debug("alternate probe...");
2890 ioctl(rodent.mfd, TIOCMGET, &i);
2891 i |= TIOCM_DTR; /* DTR = 1 */
2892 i &= ~TIOCM_RTS; /* RTS = 0 */
2893 ioctl(rodent.mfd, TIOCMSET, &i);
2896 setmousespeed(1200, 1200, (CS7 | CREAD | CLOCAL | HUPCL));
2898 /* wait for respose */
2900 ioctl(rodent.mfd, TIOCFLUSH, &i);
2901 i = TIOCM_DTR | TIOCM_RTS; /* DTR = 1, RTS = 1 */
2902 ioctl(rodent.mfd, TIOCMBIS, &i);
2904 /* try to read something */
2906 FD_SET(rodent.mfd, &fds);
2908 timeout.tv_usec = 240000;
2909 if (select(FD_SETSIZE, &fds, NULL, NULL, &timeout) > 0) {
2910 debug("pnpwakeup2(): valid response.");
2920 struct timeval timeout;
2926 if (!pnpwakeup1() && !pnpwakeup2()) {
2928 * According to PnP spec, we should set DTR = 1 and RTS = 0 while
2929 * in idle state. But, `moused' shall set DTR = RTS = 1 and proceed,
2930 * assuming there is something at the port even if it didn't
2931 * respond to the PnP enumeration procedure.
2933 i = TIOCM_DTR | TIOCM_RTS; /* DTR = 1, RTS = 1 */
2934 ioctl(rodent.mfd, TIOCMBIS, &i);
2938 /* collect PnP COM device ID (2.1.7) */
2941 usleep(240000); /* the mouse must send `Begin ID' within 200msec */
2942 while (read(rodent.mfd, &c, 1) == 1) {
2943 /* we may see "M", or "M3..." before `Begin ID' */
2945 if ((c == 0x08) || (c == 0x28)) { /* Begin ID */
2946 debug("begin-id %02x", c);
2950 debug("%c %02x", c, c);
2955 /* we haven't seen `Begin ID' in time... */
2959 ++c; /* make it `End ID' */
2962 FD_SET(rodent.mfd, &fds);
2964 timeout.tv_usec = 240000;
2965 if (select(FD_SETSIZE, &fds, NULL, NULL, &timeout) <= 0)
2968 read(rodent.mfd, &buf[i], 1);
2969 if (buf[i++] == c) /* End ID */
2976 bcopy(&buf[begin], &buf[0], i);
2978 /* string may not be human readable... */
2979 debug("len:%d, '%-*.*s'", i, i, i, buf);
2981 if (buf[i - 1] == c)
2982 return (i); /* a valid PnP string */
2985 * According to PnP spec, we should set DTR = 1 and RTS = 0 while
2986 * in idle state. But, `moused' shall leave the modem control lines
2987 * as they are. See above.
2991 /* we may still have something in the buffer */
2996 pnpparse(pnpid_t *id, char *buf, int len)
3008 id->description = NULL;
3013 id->ndescription = 0;
3015 if ((buf[0] != 0x28) && (buf[0] != 0x08)) {
3020 case 'M': /* Microsoft */
3021 id->eisaid = "PNP0F01";
3023 case 'H': /* MouseSystems */
3024 id->eisaid = "PNP0F04";
3027 id->neisaid = strlen(id->eisaid);
3028 id->class = "MOUSE";
3029 id->nclass = strlen(id->class);
3030 debug("non-PnP mouse '%c'", buf[0]);
3035 offset = 0x28 - buf[0];
3037 /* calculate checksum */
3038 for (i = 0; i < len - 3; ++i) {
3042 sum += buf[len - 1];
3043 for (; i < len; ++i)
3045 debug("PnP ID string: '%*.*s'", len, len, buf);
3050 id->revision = ((buf[1] & 0x3f) << 6) | (buf[2] & 0x3f);
3051 debug("PnP rev %d.%02d", id->revision / 100, id->revision % 100);
3053 /* EISA vender and product ID */
3054 id->eisaid = &buf[3];
3057 /* option strings */
3059 if (buf[i] == '\\') {
3060 /* device serial # */
3061 for (j = ++i; i < len; ++i) {
3068 id->serial = &buf[j];
3072 if (buf[i] == '\\') {
3074 for (j = ++i; i < len; ++i) {
3081 id->class = &buf[j];
3085 if (buf[i] == '\\') {
3086 /* compatible driver */
3087 for (j = ++i; i < len; ++i) {
3092 * PnP COM spec prior to v0.96 allowed '*' in this field,
3093 * it's not allowed now; just igore it.
3100 id->compat = &buf[j];
3101 id->ncompat = i - j;
3104 if (buf[i] == '\\') {
3105 /* product description */
3106 for (j = ++i; i < len; ++i) {
3113 id->description = &buf[j];
3114 id->ndescription = i - j;
3118 /* checksum exists if there are any optional fields */
3119 if ((id->nserial > 0) || (id->nclass > 0)
3120 || (id->ncompat > 0) || (id->ndescription > 0)) {
3121 debug("PnP checksum: 0x%X", sum);
3122 sprintf(s, "%02X", sum & 0x0ff);
3123 if (strncmp(s, &buf[len - 3], 2) != 0) {
3126 * I found some mice do not comply with the PnP COM device
3127 * spec regarding checksum... XXX
3129 logwarnx("PnP checksum error", 0);
3139 pnpproto(pnpid_t *id)
3145 if (strncmp(id->class, "MOUSE", id->nclass) != 0 &&
3146 strncmp(id->class, "TABLET", id->nclass) != 0)
3147 /* this is not a mouse! */
3150 if (id->neisaid > 0) {
3151 t = gettoken(pnpprod, id->eisaid, id->neisaid);
3152 if (t->val != MOUSE_PROTO_UNKNOWN)
3157 * The 'Compatible drivers' field may contain more than one
3158 * ID separated by ','.
3160 if (id->ncompat <= 0)
3162 for (i = 0; i < id->ncompat; ++i) {
3163 for (j = i; id->compat[i] != ','; ++i)
3164 if (i >= id->ncompat)
3167 t = gettoken(pnpprod, id->compat + j, i - j);
3168 if (t->val != MOUSE_PROTO_UNKNOWN)
3176 /* name/val mapping */
3179 gettoken(symtab_t *tab, const char *s, int len)
3183 for (i = 0; tab[i].name != NULL; ++i) {
3184 if (strncmp(tab[i].name, s, len) == 0)
3191 gettokenname(symtab_t *tab, int val)
3193 static const char unknown[] = "unknown";
3196 for (i = 0; tab[i].name != NULL; ++i) {
3197 if (tab[i].val == val)
3198 return (tab[i].name);
3205 * code to read from the Genius Kidspad tablet.
3207 The tablet responds to the COM PnP protocol 1.0 with EISA-ID KYE0005,
3208 and to pre-pnp probes (RTS toggle) with 'T' (tablet ?)
3209 9600, 8 bit, parity odd.
3211 The tablet puts out 5 bytes. b0 (mask 0xb8, value 0xb8) contains
3212 the proximity, tip and button info:
3213 (byte0 & 0x1) true = tip pressed
3214 (byte0 & 0x2) true = button pressed
3215 (byte0 & 0x40) false = pen in proximity of tablet.
3217 The next 4 bytes are used for coordinates xl, xh, yl, yh (7 bits valid).
3219 Only absolute coordinates are returned, so we use the following approach:
3220 we store the last coordinates sent when the pen went out of the tablet,
3227 S_IDLE, S_PROXY, S_FIRST, S_DOWN, S_UP
3231 kidspad(u_char rxc, mousestatus_t *act)
3234 static int buflen = 0, b_prev = 0 , x_prev = -1, y_prev = -1;
3235 static k_status status = S_IDLE;
3236 static struct timespec now;
3240 if (buflen > 0 && (rxc & 0x80)) {
3241 fprintf(stderr, "invalid code %d 0x%x\n", buflen, rxc);
3244 if (buflen == 0 && (rxc & 0xb8) != 0xb8) {
3245 fprintf(stderr, "invalid code 0 0x%x\n", rxc);
3246 return (0); /* invalid code, no action */
3248 buf[buflen++] = rxc;
3252 buflen = 0; /* for next time... */
3254 x = buf[1]+128*(buf[2] - 7);
3256 y = 28*128 - (buf[3] + 128* (buf[4] - 7));
3263 act->obutton = act->button;
3264 act->dx = act->dy = act->dz = 0;
3265 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
3266 if (buf[0] & 0x40) /* pen went out of reach */
3268 else if (status == S_IDLE) { /* pen is newly near the tablet */
3269 act->flags |= MOUSE_POSCHANGED; /* force update */
3274 act->dx = x - x_prev;
3275 act->dy = y - y_prev;
3276 if (act->dx || act->dy)
3277 act->flags |= MOUSE_POSCHANGED;
3280 if (b_prev != 0 && b_prev != buf[0]) { /* possibly record button change */
3282 if (buf[0] & 0x01) /* tip pressed */
3283 act->button |= MOUSE_BUTTON1DOWN;
3284 if (buf[0] & 0x02) /* button pressed */
3285 act->button |= MOUSE_BUTTON2DOWN;
3286 act->flags |= MOUSE_BUTTONSCHANGED;
3289 return (act->flags);
3293 gtco_digipad (u_char rxc, mousestatus_t *act)
3295 static u_char buf[5];
3296 static int buflen = 0, b_prev = 0 , x_prev = -1, y_prev = -1;
3297 static k_status status = S_IDLE;
3300 #define GTCO_HEADER 0x80
3301 #define GTCO_PROXIMITY 0x40
3302 #define GTCO_START (GTCO_HEADER|GTCO_PROXIMITY)
3303 #define GTCO_BUTTONMASK 0x3c
3306 if (buflen > 0 && ((rxc & GTCO_HEADER) != GTCO_HEADER)) {
3307 fprintf(stderr, "invalid code %d 0x%x\n", buflen, rxc);
3310 if (buflen == 0 && (rxc & GTCO_START) != GTCO_START) {
3311 fprintf(stderr, "invalid code 0 0x%x\n", rxc);
3312 return (0); /* invalid code, no action */
3315 buf[buflen++] = rxc;
3319 buflen = 0; /* for next time... */
3321 x = ((buf[2] & ~GTCO_START) << 6 | (buf[1] & ~GTCO_START));
3322 y = 4768 - ((buf[4] & ~GTCO_START) << 6 | (buf[3] & ~GTCO_START));
3328 act->obutton = act->button;
3329 act->dx = act->dy = act->dz = 0;
3331 if ((buf[0] & 0x40) == 0) /* pen went out of reach */
3333 else if (status == S_IDLE) { /* pen is newly near the tablet */
3334 act->flags |= MOUSE_POSCHANGED; /* force update */
3340 act->dx = x - x_prev;
3341 act->dy = y - y_prev;
3342 if (act->dx || act->dy)
3343 act->flags |= MOUSE_POSCHANGED;
3347 /* possibly record button change */
3348 if (b_prev != 0 && b_prev != buf[0]) {
3350 if (buf[0] & 0x04) {
3351 /* tip pressed/yellow */
3352 act->button |= MOUSE_BUTTON1DOWN;
3354 if (buf[0] & 0x08) {
3356 act->button |= MOUSE_BUTTON2DOWN;
3358 if (buf[0] & 0x10) {
3360 act->button |= MOUSE_BUTTON3DOWN;
3362 if (buf[0] & 0x20) {
3364 act->button |= MOUSE_BUTTON4DOWN;
3366 act->flags |= MOUSE_BUTTONSCHANGED;
3369 return (act->flags);
3373 mremote_serversetup(void)
3375 struct sockaddr_un ad;
3377 /* Open a UNIX domain stream socket to listen for mouse remote clients */
3378 unlink(_PATH_MOUSEREMOTE);
3380 if ((rodent.mremsfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
3381 logerrx(1, "unable to create unix domain socket %s",_PATH_MOUSEREMOTE);
3385 bzero(&ad, sizeof(ad));
3386 ad.sun_family = AF_UNIX;
3387 strcpy(ad.sun_path, _PATH_MOUSEREMOTE);
3389 #define SUN_LEN(unp) (((char *)(unp)->sun_path - (char *)(unp)) + \
3390 strlen((unp)->path))
3392 if (bind(rodent.mremsfd, (struct sockaddr *) &ad, SUN_LEN(&ad)) < 0)
3393 logerrx(1, "unable to bind unix domain socket %s", _PATH_MOUSEREMOTE);
3395 listen(rodent.mremsfd, 1);
3399 mremote_clientchg(int add)
3401 struct sockaddr_un ad;
3405 if (rodent.rtype != MOUSE_PROTO_X10MOUSEREM)
3409 /* Accept client connection, if we don't already have one */
3410 ad_len = sizeof(ad);
3411 fd = accept(rodent.mremsfd, (struct sockaddr *) &ad, &ad_len);
3413 logwarnx("failed accept on mouse remote socket");
3415 if (rodent.mremcfd < 0) {
3416 rodent.mremcfd = fd;
3417 debug("remote client connect...accepted");
3421 debug("another remote client connect...disconnected");
3425 /* Client disconnected */
3426 debug("remote client disconnected");
3427 close(rodent.mremcfd);
3428 rodent.mremcfd = -1;