2 ** Copyright (c) 1995 Michael Smith, All rights reserved.
4 ** Redistribution and use in source and binary forms, with or without
5 ** modification, are permitted provided that the following conditions
7 ** 1. Redistributions of source code must retain the above copyright
8 ** notice, this list of conditions and the following disclaimer as
9 ** the first lines of this file unmodified.
10 ** 2. Redistributions in binary form must reproduce the above copyright
11 ** notice, this list of conditions and the following disclaimer in the
12 ** documentation and/or other materials provided with the distribution.
13 ** 3. All advertising materials mentioning features or use of this software
14 ** must display the following acknowledgment:
15 ** This product includes software developed by Michael Smith.
16 ** 4. The name of the author may not be used to endorse or promote products
17 ** derived from this software without specific prior written permission.
20 ** THIS SOFTWARE IS PROVIDED BY Michael Smith ``AS IS'' AND ANY
21 ** EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Michael Smith BE LIABLE FOR
24 ** ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 ** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 ** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27 ** BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29 ** OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 ** EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 ** Mouse daemon : listens to a serial port, the bus mouse interface, or
38 ** the PS/2 mouse port for mouse data stream, interprets data and passes
39 ** ioctls off to the console driver.
41 ** The mouse interface functions are derived closely from the mouse
42 ** handler in the XFree86 X server. Many thanks to the XFree86 people
43 ** for their great work!
47 #include <sys/cdefs.h>
48 __FBSDID("$FreeBSD$");
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 */
82 /* Abort 3-button emulation delay after this many movement events. */
83 #define BUTTON2_MAXMOVE 3
88 #define MOUSE_XAXIS (-1)
89 #define MOUSE_YAXIS (-2)
91 /* Logitech PS2++ protocol */
92 #define MOUSE_PS2PLUS_CHECKBITS(b) \
93 ((((b[2] & 0x03) << 2) | 0x02) == (b[1] & 0x0f))
94 #define MOUSE_PS2PLUS_PACKET_TYPE(b) \
95 (((b[0] & 0x30) >> 2) | ((b[1] & 0x30) >> 4))
97 #define ChordMiddle 0x0001
98 #define Emulate3Button 0x0002
99 #define ClearDTR 0x0004
100 #define ClearRTS 0x0008
102 #define VirtualScroll 0x0020
103 #define HVirtualScroll 0x0040
104 #define ExponentialAcc 0x0080
111 #define ID_ALL (ID_PORT | ID_IF | ID_TYPE | ID_MODEL)
113 /* Operations on timespecs */
114 #define tsclr(tvp) ((tvp)->tv_sec = (tvp)->tv_nsec = 0)
115 #define tscmp(tvp, uvp, cmp) \
116 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
117 ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
118 ((tvp)->tv_sec cmp (uvp)->tv_sec))
119 #define tssub(tvp, uvp, vvp) \
121 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
122 (vvp)->tv_nsec = (tvp)->tv_nsec - (uvp)->tv_nsec; \
123 if ((vvp)->tv_nsec < 0) { \
125 (vvp)->tv_nsec += 1000000000; \
129 #define debug(...) do { \
130 if (debug && nodaemon) \
131 warnx(__VA_ARGS__); \
134 #define logerr(e, ...) do { \
135 log_or_warn(LOG_DAEMON | LOG_ERR, errno, __VA_ARGS__); \
139 #define logerrx(e, ...) do { \
140 log_or_warn(LOG_DAEMON | LOG_ERR, 0, __VA_ARGS__); \
144 #define logwarn(...) \
145 log_or_warn(LOG_DAEMON | LOG_WARNING, errno, __VA_ARGS__)
147 #define logwarnx(...) \
148 log_or_warn(LOG_DAEMON | LOG_WARNING, 0, __VA_ARGS__)
152 /* symbol table entry */
159 /* serial PnP ID string */
161 int revision; /* PnP revision, 100 for 1.00 */
162 const char *eisaid; /* EISA ID including mfr ID and product ID */
163 char *serial; /* serial No, optional */
164 const char *class; /* device class, optional */
165 char *compat; /* list of compatible drivers, optional */
166 char *description; /* product description, optional */
167 int neisaid; /* length of the above fields... */
174 /* global variables */
177 int nodaemon = FALSE;
178 int background = FALSE;
180 int identify = ID_NONE;
181 int extioctl = FALSE;
182 const char *pidfile = "/var/run/moused.pid";
185 #define SCROLL_NOTSCROLLING 0
186 #define SCROLL_PREPARE 1
187 #define SCROLL_SCROLLING 2
189 static int scroll_state;
190 static int scroll_movement;
191 static int hscroll_movement;
193 /* local variables */
195 /* interface (the table must be ordered by MOUSE_IF_XXX in mouse.h) */
196 static symtab_t rifs[] = {
197 { "serial", MOUSE_IF_SERIAL, 0 },
198 { "bus", MOUSE_IF_BUS, 0 },
199 { "inport", MOUSE_IF_INPORT, 0 },
200 { "ps/2", MOUSE_IF_PS2, 0 },
201 { "sysmouse", MOUSE_IF_SYSMOUSE, 0 },
202 { "usb", MOUSE_IF_USB, 0 },
203 { NULL, MOUSE_IF_UNKNOWN, 0 },
206 /* types (the table must be ordered by MOUSE_PROTO_XXX in mouse.h) */
207 static const char *rnames[] = {
233 static symtab_t rmodels[] = {
234 { "NetScroll", MOUSE_MODEL_NETSCROLL, 0 },
235 { "NetMouse/NetScroll Optical", MOUSE_MODEL_NET, 0 },
236 { "GlidePoint", MOUSE_MODEL_GLIDEPOINT, 0 },
237 { "ThinkingMouse", MOUSE_MODEL_THINK, 0 },
238 { "IntelliMouse", MOUSE_MODEL_INTELLI, 0 },
239 { "EasyScroll/SmartScroll", MOUSE_MODEL_EASYSCROLL, 0 },
240 { "MouseMan+", MOUSE_MODEL_MOUSEMANPLUS, 0 },
241 { "Kidspad", MOUSE_MODEL_KIDSPAD, 0 },
242 { "VersaPad", MOUSE_MODEL_VERSAPAD, 0 },
243 { "IntelliMouse Explorer", MOUSE_MODEL_EXPLORER, 0 },
244 { "4D Mouse", MOUSE_MODEL_4D, 0 },
245 { "4D+ Mouse", MOUSE_MODEL_4DPLUS, 0 },
246 { "Synaptics Touchpad", MOUSE_MODEL_SYNAPTICS, 0 },
247 { "generic", MOUSE_MODEL_GENERIC, 0 },
248 { NULL, MOUSE_MODEL_UNKNOWN, 0 },
251 /* PnP EISA/product IDs */
252 static symtab_t pnpprod[] = {
253 /* Kensignton ThinkingMouse */
254 { "KML0001", MOUSE_PROTO_THINK, MOUSE_MODEL_THINK },
255 /* MS IntelliMouse */
256 { "MSH0001", MOUSE_PROTO_INTELLI, MOUSE_MODEL_INTELLI },
257 /* MS IntelliMouse TrackBall */
258 { "MSH0004", MOUSE_PROTO_INTELLI, MOUSE_MODEL_INTELLI },
259 /* Tremon Wheel Mouse MUSD */
260 { "HTK0001", MOUSE_PROTO_INTELLI, MOUSE_MODEL_INTELLI },
261 /* Genius PnP Mouse */
262 { "KYE0001", MOUSE_PROTO_MS, MOUSE_MODEL_GENERIC },
263 /* MouseSystems SmartScroll Mouse (OEM from Genius?) */
264 { "KYE0002", MOUSE_PROTO_MS, MOUSE_MODEL_EASYSCROLL },
265 /* Genius NetMouse */
266 { "KYE0003", MOUSE_PROTO_INTELLI, MOUSE_MODEL_NET },
267 /* Genius Kidspad, Easypad and other tablets */
268 { "KYE0005", MOUSE_PROTO_KIDSPAD, MOUSE_MODEL_KIDSPAD },
269 /* Genius EZScroll */
270 { "KYEEZ00", MOUSE_PROTO_MS, MOUSE_MODEL_EASYSCROLL },
271 /* Logitech Cordless MouseMan Wheel */
272 { "LGI8033", MOUSE_PROTO_INTELLI, MOUSE_MODEL_MOUSEMANPLUS },
273 /* Logitech MouseMan (new 4 button model) */
274 { "LGI800C", MOUSE_PROTO_INTELLI, MOUSE_MODEL_MOUSEMANPLUS },
275 /* Logitech MouseMan+ */
276 { "LGI8050", MOUSE_PROTO_INTELLI, MOUSE_MODEL_MOUSEMANPLUS },
277 /* Logitech FirstMouse+ */
278 { "LGI8051", MOUSE_PROTO_INTELLI, MOUSE_MODEL_MOUSEMANPLUS },
279 /* Logitech serial */
280 { "LGI8001", MOUSE_PROTO_LOGIMOUSEMAN, MOUSE_MODEL_GENERIC },
281 /* A4 Tech 4D/4D+ Mouse */
282 { "A4W0005", MOUSE_PROTO_INTELLI, MOUSE_MODEL_4D },
283 /* 8D Scroll Mouse */
284 { "PEC9802", MOUSE_PROTO_INTELLI, MOUSE_MODEL_INTELLI },
285 /* Mitsumi Wireless Scroll Mouse */
286 { "MTM6401", MOUSE_PROTO_INTELLI, MOUSE_MODEL_INTELLI },
289 { "PNP0F00", MOUSE_PROTO_BUS, MOUSE_MODEL_GENERIC },
291 { "PNP0F01", MOUSE_PROTO_MS, MOUSE_MODEL_GENERIC },
293 { "PNP0F02", MOUSE_PROTO_INPORT, MOUSE_MODEL_GENERIC },
295 { "PNP0F03", MOUSE_PROTO_PS2, MOUSE_MODEL_GENERIC },
297 * EzScroll returns PNP0F04 in the compatible device field; but it
298 * doesn't look compatible... XXX
301 { "PNP0F04", MOUSE_PROTO_MSC, MOUSE_MODEL_GENERIC },
303 { "PNP0F05", MOUSE_PROTO_MSC, MOUSE_MODEL_GENERIC },
306 { "PNP0F06", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC },
308 { "PNP0F07", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC },
310 /* Logitech serial */
311 { "PNP0F08", MOUSE_PROTO_LOGIMOUSEMAN, MOUSE_MODEL_GENERIC },
312 /* MS BallPoint serial */
313 { "PNP0F09", MOUSE_PROTO_MS, MOUSE_MODEL_GENERIC },
315 { "PNP0F0A", MOUSE_PROTO_MS, MOUSE_MODEL_GENERIC },
316 /* MS PnP BallPoint serial */
317 { "PNP0F0B", MOUSE_PROTO_MS, MOUSE_MODEL_GENERIC },
318 /* MS serial comatible */
319 { "PNP0F0C", MOUSE_PROTO_MS, MOUSE_MODEL_GENERIC },
320 /* MS InPort comatible */
321 { "PNP0F0D", MOUSE_PROTO_INPORT, MOUSE_MODEL_GENERIC },
322 /* MS PS/2 comatible */
323 { "PNP0F0E", MOUSE_PROTO_PS2, MOUSE_MODEL_GENERIC },
324 /* MS BallPoint comatible */
325 { "PNP0F0F", MOUSE_PROTO_MS, MOUSE_MODEL_GENERIC },
328 { "PNP0F10", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC },
330 /* MS bus comatible */
331 { "PNP0F11", MOUSE_PROTO_BUS, MOUSE_MODEL_GENERIC },
333 { "PNP0F12", MOUSE_PROTO_PS2, MOUSE_MODEL_GENERIC },
335 { "PNP0F13", MOUSE_PROTO_PS2, MOUSE_MODEL_GENERIC },
338 { "PNP0F14", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC },
341 { "PNP0F15", MOUSE_PROTO_BUS, MOUSE_MODEL_GENERIC },
344 { "PNP0F16", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC },
346 /* Logitech serial compat */
347 { "PNP0F17", MOUSE_PROTO_LOGIMOUSEMAN, MOUSE_MODEL_GENERIC },
348 /* Logitech bus compatible */
349 { "PNP0F18", MOUSE_PROTO_BUS, MOUSE_MODEL_GENERIC },
350 /* Logitech PS/2 compatible */
351 { "PNP0F19", MOUSE_PROTO_PS2, MOUSE_MODEL_GENERIC },
353 /* Logitech SWIFT compatible */
354 { "PNP0F1A", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC },
356 { "PNP0F1B", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC },
357 /* Compaq LTE TrackBall PS/2 */
358 { "PNP0F1C", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC },
359 /* Compaq LTE TrackBall serial */
360 { "PNP0F1D", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC },
361 /* MS Kidts Trackball */
362 { "PNP0F1E", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC },
364 /* Interlink VersaPad */
365 { "LNK0001", MOUSE_PROTO_VERSAPAD, MOUSE_MODEL_VERSAPAD },
367 { NULL, MOUSE_PROTO_UNKNOWN, MOUSE_MODEL_GENERIC },
370 /* the table must be ordered by MOUSE_PROTO_XXX in mouse.h */
371 static unsigned short rodentcflags[] =
373 (CS7 | CREAD | CLOCAL | HUPCL), /* MicroSoft */
374 (CS8 | CSTOPB | CREAD | CLOCAL | HUPCL), /* MouseSystems */
375 (CS8 | CSTOPB | CREAD | CLOCAL | HUPCL), /* Logitech */
376 (CS8 | PARENB | PARODD | CREAD | CLOCAL | HUPCL), /* MMSeries */
377 (CS7 | CREAD | CLOCAL | HUPCL), /* MouseMan */
381 (CS8 | CREAD | CLOCAL | HUPCL), /* MM HitTablet */
382 (CS7 | CREAD | CLOCAL | HUPCL), /* GlidePoint */
383 (CS7 | CREAD | CLOCAL | HUPCL), /* IntelliMouse */
384 (CS7 | CREAD | CLOCAL | HUPCL), /* Thinking Mouse */
385 (CS8 | CSTOPB | CREAD | CLOCAL | HUPCL), /* sysmouse */
386 (CS7 | CREAD | CLOCAL | HUPCL), /* X10 MouseRemote */
387 (CS8 | PARENB | PARODD | CREAD | CLOCAL | HUPCL), /* kidspad etc. */
388 (CS8 | CREAD | CLOCAL | HUPCL), /* VersaPad */
391 (CS8 | CSTOPB | CREAD | CLOCAL | HUPCL), /* Mariqua */
393 (CS8 | CREAD | HUPCL ), /* GTCO Digi-Pad */
396 static struct rodentparam {
398 const char *portname; /* /dev/XXX */
399 int rtype; /* MOUSE_PROTO_XXX */
400 int level; /* operation level: 0 or greater */
402 int rate; /* report rate */
403 int resolution; /* MOUSE_RES_XXX or a positive number */
404 int zmap[4]; /* MOUSE_{X|Y}AXIS or a button number */
405 int wmode; /* wheel mode button number */
406 int mfd; /* mouse file descriptor */
407 int cfd; /* /dev/consolectl file descriptor */
408 int mremsfd; /* mouse remote server file descriptor */
409 int mremcfd; /* mouse remote client file descriptor */
410 long clickthreshold; /* double click speed in msec */
411 long button2timeout; /* 3 button emulation timeout */
412 mousehw_t hw; /* mouse device hardware information */
413 mousemode_t mode; /* protocol information */
414 float accelx; /* Acceleration in the X axis */
415 float accely; /* Acceleration in the Y axis */
416 float expoaccel; /* Exponential acceleration */
417 float expoffset; /* Movement offset for exponential accel. */
418 float remainx; /* Remainder on X and Y axis, respectively... */
419 float remainy; /* ... to compensate for rounding errors. */
420 int scrollthreshold; /* Movement distance before virtual scrolling */
424 .rtype = MOUSE_PROTO_UNKNOWN,
428 .resolution = MOUSE_RES_UNKNOWN,
429 .zmap = { 0, 0, 0, 0 },
435 .clickthreshold = DFLT_CLICKTHRESHOLD,
436 .button2timeout = DFLT_BUTTON2TIMEOUT,
443 .scrollthreshold = DFLT_SCROLLTHRESHOLD,
447 struct button_state {
448 int count; /* 0: up, 1: single click, 2: double click,... */
449 struct timespec ts; /* timestamp on the last button event */
451 static struct button_state bstate[MOUSE_MAXBUTTON]; /* button state */
452 static struct button_state *mstate[MOUSE_MAXBUTTON];/* mapped button st.*/
453 static struct button_state zstate[4]; /* Z/W axis state */
455 /* state machine for 3 button emulation */
457 #define S0 0 /* start */
458 #define S1 1 /* button 1 delayed down */
459 #define S2 2 /* button 3 delayed down */
460 #define S3 3 /* both buttons down -> button 2 down */
461 #define S4 4 /* button 1 delayed up */
462 #define S5 5 /* button 1 down */
463 #define S6 6 /* button 3 down */
464 #define S7 7 /* both buttons down */
465 #define S8 8 /* button 3 delayed up */
466 #define S9 9 /* button 1 or 3 up after S3 */
468 #define A(b1, b3) (((b1) ? 2 : 0) | ((b3) ? 1 : 0))
470 #define S_DELAYED(st) (states[st].s[A_TIMEOUT] != (st))
473 int s[A_TIMEOUT + 1];
479 { { S0, S2, S1, S3, S0 }, 0, ~(MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN), FALSE },
481 { { S4, S2, S1, S3, S5 }, 0, ~MOUSE_BUTTON1DOWN, FALSE },
483 { { S8, S2, S1, S3, S6 }, 0, ~MOUSE_BUTTON3DOWN, FALSE },
485 { { S0, S9, S9, S3, S3 }, MOUSE_BUTTON2DOWN, ~0, FALSE },
487 { { S0, S2, S1, S3, S0 }, MOUSE_BUTTON1DOWN, ~0, TRUE },
489 { { S0, S2, S5, S7, S5 }, MOUSE_BUTTON1DOWN, ~0, FALSE },
491 { { S0, S6, S1, S7, S6 }, MOUSE_BUTTON3DOWN, ~0, FALSE },
493 { { S0, S6, S5, S7, S7 }, MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, ~0, FALSE },
495 { { S0, S2, S1, S3, S0 }, MOUSE_BUTTON3DOWN, ~0, TRUE },
497 { { S0, S9, S9, S3, S9 }, 0, ~(MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN), FALSE },
499 static int mouse_button_state;
500 static struct timespec mouse_button_state_ts;
501 static int mouse_move_delayed;
509 static int drift_distance = 4; /* max steps X+Y */
510 static int drift_time = 500; /* in 0.5 sec */
511 static struct timespec drift_time_ts;
512 static struct timespec drift_2time_ts; /* 2*drift_time */
513 static int drift_after = 4000; /* 4 sec */
514 static struct timespec drift_after_ts;
515 static int drift_terminate = FALSE;
516 static struct timespec drift_current_ts;
517 static struct timespec drift_tmp;
518 static struct timespec drift_last_activity = {0, 0};
519 static struct timespec drift_since = {0, 0};
520 static struct drift_xy drift_last = {0, 0}; /* steps in last drift_time */
521 static struct drift_xy drift_previous = {0, 0}; /* steps in prev. drift_time */
523 /* function prototypes */
525 static void linacc(int, int, int*, int*);
526 static void expoacc(int, int, int*, int*);
527 static void moused(void);
528 static void hup(int sig);
529 static void cleanup(int sig);
530 static void pause_mouse(int sig);
531 static void usage(void);
532 static void log_or_warn(int log_pri, int errnum, const char *fmt, ...)
535 static int r_identify(void);
536 static const char *r_if(int type);
537 static const char *r_name(int type);
538 static const char *r_model(int model);
539 static void r_init(void);
540 static int r_protocol(u_char b, mousestatus_t *act);
541 static int r_statetrans(mousestatus_t *a1, mousestatus_t *a2, int trans);
542 static int r_installmap(char *arg);
543 static void r_map(mousestatus_t *act1, mousestatus_t *act2);
544 static void r_timestamp(mousestatus_t *act);
545 static int r_timeout(void);
546 static void r_click(mousestatus_t *act);
547 static void setmousespeed(int old, int new, unsigned cflag);
549 static int pnpwakeup1(void);
550 static int pnpwakeup2(void);
551 static int pnpgets(char *buf);
552 static int pnpparse(pnpid_t *id, char *buf, int len);
553 static symtab_t *pnpproto(pnpid_t *id);
555 static symtab_t *gettoken(symtab_t *tab, const char *s, int len);
556 static const char *gettokenname(symtab_t *tab, int val);
558 static void mremote_serversetup(void);
559 static void mremote_clientchg(int add);
561 static int kidspad(u_char rxc, mousestatus_t *act);
562 static int gtco_digipad(u_char, mousestatus_t *);
564 static int usbmodule(void);
567 main(int argc, char *argv[])
574 for (i = 0; i < MOUSE_MAXBUTTON; ++i)
575 mstate[i] = &bstate[i];
577 while ((c = getopt(argc, argv, "3A:C:DE:F:HI:PRS:T:VU:a:cdfhi:l:m:p:r:st:w:z:")) != -1)
581 rodent.flags |= Emulate3Button;
585 rodent.button2timeout = atoi(optarg);
586 if ((rodent.button2timeout < 0) ||
587 (rodent.button2timeout > MAX_BUTTON2TIMEOUT)) {
588 warnx("invalid argument `%s'", optarg);
594 i = sscanf(optarg, "%f,%f", &rodent.accelx, &rodent.accely);
596 warnx("invalid linear acceleration argument '%s'", optarg);
601 rodent.accely = rodent.accelx;
606 rodent.flags |= ExponentialAcc;
607 i = sscanf(optarg, "%f,%f", &rodent.expoaccel, &rodent.expoffset);
609 warnx("invalid exponential acceleration argument '%s'", optarg);
614 rodent.expoffset = 1.0;
619 rodent.flags |= ChordMiddle;
631 if (strcmp(optarg, "all") == 0)
633 else if (strcmp(optarg, "port") == 0)
635 else if (strcmp(optarg, "if") == 0)
637 else if (strcmp(optarg, "type") == 0)
639 else if (strcmp(optarg, "model") == 0)
642 warnx("invalid argument `%s'", optarg);
649 rodent.level = atoi(optarg);
650 if ((rodent.level < 0) || (rodent.level > 4)) {
651 warnx("invalid argument `%s'", optarg);
657 if (!r_installmap(optarg)) {
658 warnx("invalid argument `%s'", optarg);
664 rodent.portname = optarg;
668 if (strcmp(optarg, "high") == 0)
669 rodent.resolution = MOUSE_RES_HIGH;
670 else if (strcmp(optarg, "medium-high") == 0)
671 rodent.resolution = MOUSE_RES_HIGH;
672 else if (strcmp(optarg, "medium-low") == 0)
673 rodent.resolution = MOUSE_RES_MEDIUMLOW;
674 else if (strcmp(optarg, "low") == 0)
675 rodent.resolution = MOUSE_RES_LOW;
676 else if (strcmp(optarg, "default") == 0)
677 rodent.resolution = MOUSE_RES_DEFAULT;
679 rodent.resolution = atoi(optarg);
680 if (rodent.resolution <= 0) {
681 warnx("invalid argument `%s'", optarg);
688 rodent.baudrate = 9600;
693 if ((i <= 0) || (i > MOUSE_MAXBUTTON)) {
694 warnx("invalid argument `%s'", optarg);
697 rodent.wmode = 1 << (i - 1);
701 if (strcmp(optarg, "x") == 0)
702 rodent.zmap[0] = MOUSE_XAXIS;
703 else if (strcmp(optarg, "y") == 0)
704 rodent.zmap[0] = MOUSE_YAXIS;
708 * Use button i for negative Z axis movement and
709 * button (i + 1) for positive Z axis movement.
711 if ((i <= 0) || (i > MOUSE_MAXBUTTON - 1)) {
712 warnx("invalid argument `%s'", optarg);
716 rodent.zmap[1] = i + 1;
717 debug("optind: %d, optarg: '%s'", optind, optarg);
718 for (j = 1; j < 4; ++j) {
719 if ((optind >= argc) || !isdigit(*argv[optind]))
721 i = atoi(argv[optind]);
722 if ((i <= 0) || (i > MOUSE_MAXBUTTON - 1)) {
723 warnx("invalid argument `%s'", argv[optind]);
729 if ((rodent.zmap[2] != 0) && (rodent.zmap[3] == 0))
730 rodent.zmap[3] = rodent.zmap[2] + 1;
735 rodent.clickthreshold = atoi(optarg);
736 if ((rodent.clickthreshold < 0) ||
737 (rodent.clickthreshold > MAX_CLICKTHRESHOLD)) {
738 warnx("invalid argument `%s'", optarg);
744 rodent.flags |= ClearDTR;
748 rodent.rate = atoi(optarg);
749 if (rodent.rate <= 0) {
750 warnx("invalid argument `%s'", optarg);
756 rodent.flags |= HVirtualScroll;
764 rodent.flags |= NoPnP;
768 rodent.flags |= ClearRTS;
772 rodent.baudrate = atoi(optarg);
773 if (rodent.baudrate <= 0) {
774 warnx("invalid argument `%s'", optarg);
777 debug("rodent baudrate %d", rodent.baudrate);
781 drift_terminate = TRUE;
782 sscanf(optarg, "%d,%d,%d", &drift_distance, &drift_time,
784 if (drift_distance <= 0 || drift_time <= 0 || drift_after <= 0) {
785 warnx("invalid argument `%s'", optarg);
788 debug("terminate drift: distance %d, time %d, after %d",
789 drift_distance, drift_time, drift_after);
790 drift_time_ts.tv_sec = drift_time / 1000;
791 drift_time_ts.tv_nsec = (drift_time % 1000) * 1000000;
792 drift_2time_ts.tv_sec = (drift_time *= 2) / 1000;
793 drift_2time_ts.tv_nsec = (drift_time % 1000) * 1000000;
794 drift_after_ts.tv_sec = drift_after / 1000;
795 drift_after_ts.tv_nsec = (drift_after % 1000) * 1000000;
799 if (strcmp(optarg, "auto") == 0) {
800 rodent.rtype = MOUSE_PROTO_UNKNOWN;
801 rodent.flags &= ~NoPnP;
805 for (i = 0; rnames[i] != NULL; i++)
806 if (strcmp(optarg, rnames[i]) == 0) {
808 rodent.flags |= NoPnP;
809 rodent.level = (i == MOUSE_PROTO_SYSMOUSE) ? 1 : 0;
812 if (rnames[i] == NULL) {
813 warnx("no such mouse type `%s'", optarg);
819 rodent.flags |= VirtualScroll;
822 rodent.scrollthreshold = atoi(optarg);
823 if (rodent.scrollthreshold < 0) {
824 warnx("invalid argument `%s'", optarg);
835 /* fix Z axis mapping */
836 for (i = 0; i < 4; ++i) {
837 if (rodent.zmap[i] > 0) {
838 for (j = 0; j < MOUSE_MAXBUTTON; ++j) {
839 if (mstate[j] == &bstate[rodent.zmap[i] - 1])
840 mstate[j] = &zstate[i];
842 rodent.zmap[i] = 1 << (rodent.zmap[i] - 1);
846 /* the default port name */
847 switch(rodent.rtype) {
849 case MOUSE_PROTO_INPORT:
850 /* INPORT and BUS are the same... */
851 rodent.rtype = MOUSE_PROTO_BUS;
853 case MOUSE_PROTO_BUS:
854 if (!rodent.portname)
855 rodent.portname = "/dev/mse0";
858 case MOUSE_PROTO_PS2:
859 if (!rodent.portname)
860 rodent.portname = "/dev/psm0";
866 warnx("no port name specified");
871 if (strncmp(rodent.portname, "/dev/ums", 8) == 0) {
872 if (usbmodule() != 0)
877 if (setjmp(env) == 0) {
879 signal(SIGINT , cleanup);
880 signal(SIGQUIT, cleanup);
881 signal(SIGTERM, cleanup);
882 signal(SIGUSR1, pause_mouse);
883 for (i = 0; i < retry; ++i) {
886 rodent.mfd = open(rodent.portname, O_RDWR | O_NONBLOCK);
887 if (rodent.mfd != -1 || errno != ENOENT)
890 if (rodent.mfd == -1)
891 logerr(1, "unable to open %s", rodent.portname);
892 if (r_identify() == MOUSE_PROTO_UNKNOWN) {
893 logwarnx("cannot determine mouse type on %s", rodent.portname);
898 /* print some information */
899 if (identify != ID_NONE) {
900 if (identify == ID_ALL)
901 printf("%s %s %s %s\n",
902 rodent.portname, r_if(rodent.hw.iftype),
903 r_name(rodent.rtype), r_model(rodent.hw.model));
904 else if (identify & ID_PORT)
905 printf("%s\n", rodent.portname);
906 else if (identify & ID_IF)
907 printf("%s\n", r_if(rodent.hw.iftype));
908 else if (identify & ID_TYPE)
909 printf("%s\n", r_name(rodent.rtype));
910 else if (identify & ID_MODEL)
911 printf("%s\n", r_model(rodent.hw.model));
914 debug("port: %s interface: %s type: %s model: %s",
915 rodent.portname, r_if(rodent.hw.iftype),
916 r_name(rodent.rtype), r_model(rodent.hw.model));
919 if (rodent.mfd == -1) {
921 * We cannot continue because of error. Exit if the
922 * program has not become a daemon. Otherwise, block
923 * until the the user corrects the problem and issues SIGHUP.
930 r_init(); /* call init function */
934 if (rodent.mfd != -1)
936 if (rodent.cfd != -1)
938 rodent.mfd = rodent.cfd = -1;
948 return (kld_isloaded("uhub/ums") || kld_load("ums") != -1);
952 * Function to calculate linear acceleration.
954 * If there are any rounding errors, the remainder
955 * is stored in the remainx and remainy variables
956 * and taken into account upon the next movement.
960 linacc(int dx, int dy, int *movex, int *movey)
964 if (dx == 0 && dy == 0) {
968 fdx = dx * rodent.accelx + rodent.remainx;
969 fdy = dy * rodent.accely + rodent.remainy;
970 *movex = lround(fdx);
971 *movey = lround(fdy);
972 rodent.remainx = fdx - *movex;
973 rodent.remainy = fdy - *movey;
977 * Function to calculate exponential acceleration.
978 * (Also includes linear acceleration if enabled.)
980 * In order to give a smoother behaviour, we record the four
981 * most recent non-zero movements and use their average value
982 * to calculate the acceleration.
986 expoacc(int dx, int dy, int *movex, int *movey)
988 static float lastlength[3] = {0.0, 0.0, 0.0};
989 float fdx, fdy, length, lbase, accel;
991 if (dx == 0 && dy == 0) {
995 fdx = dx * rodent.accelx;
996 fdy = dy * rodent.accely;
997 length = sqrtf((fdx * fdx) + (fdy * fdy)); /* Pythagoras */
998 length = (length + lastlength[0] + lastlength[1] + lastlength[2]) / 4;
999 lbase = length / rodent.expoffset;
1000 accel = powf(lbase, rodent.expoaccel) / lbase;
1001 fdx = fdx * accel + rodent.remainx;
1002 fdy = fdy * accel + rodent.remainy;
1003 *movex = lroundf(fdx);
1004 *movey = lroundf(fdy);
1005 rodent.remainx = fdx - *movex;
1006 rodent.remainy = fdy - *movey;
1007 lastlength[2] = lastlength[1];
1008 lastlength[1] = lastlength[0];
1009 lastlength[0] = length; /* Insert new average, not original length! */
1015 struct mouse_info mouse;
1016 mousestatus_t action0; /* original mouse action */
1017 mousestatus_t action; /* interrim buffer */
1018 mousestatus_t action2; /* mapped action */
1019 struct timeval timeout;
1027 if ((rodent.cfd = open("/dev/consolectl", O_RDWR, 0)) == -1)
1028 logerr(1, "cannot open /dev/consolectl");
1030 if (!nodaemon && !background) {
1031 pfh = pidfile_open(pidfile, 0600, &mpid);
1033 if (errno == EEXIST)
1034 logerrx(1, "moused already running, pid: %d", mpid);
1035 logwarn("cannot open pid file");
1038 int saved_errno = errno;
1039 pidfile_remove(pfh);
1040 errno = saved_errno;
1041 logerr(1, "failed to become a daemon");
1048 /* clear mouse data */
1049 bzero(&action0, sizeof(action0));
1050 bzero(&action, sizeof(action));
1051 bzero(&action2, sizeof(action2));
1052 bzero(&mouse, sizeof(mouse));
1053 mouse_button_state = S0;
1054 clock_gettime(CLOCK_MONOTONIC_FAST, &mouse_button_state_ts);
1055 mouse_move_delayed = 0;
1056 for (i = 0; i < MOUSE_MAXBUTTON; ++i) {
1057 bstate[i].count = 0;
1058 bstate[i].ts = mouse_button_state_ts;
1060 for (i = 0; i < (int)(sizeof(zstate) / sizeof(zstate[0])); ++i) {
1061 zstate[i].count = 0;
1062 zstate[i].ts = mouse_button_state_ts;
1065 /* choose which ioctl command to use */
1066 mouse.operation = MOUSE_MOTION_EVENT;
1067 extioctl = (ioctl(rodent.cfd, CONS_MOUSECTL, &mouse) == 0);
1069 /* process mouse data */
1071 timeout.tv_usec = 20000; /* 20 msec */
1075 FD_SET(rodent.mfd, &fds);
1076 if (rodent.mremsfd >= 0)
1077 FD_SET(rodent.mremsfd, &fds);
1078 if (rodent.mremcfd >= 0)
1079 FD_SET(rodent.mremcfd, &fds);
1081 c = select(FD_SETSIZE, &fds, NULL, NULL,
1082 (rodent.flags & Emulate3Button) ? &timeout : NULL);
1083 if (c < 0) { /* error */
1084 logwarn("failed to read from mouse");
1086 } else if (c == 0) { /* timeout */
1087 /* assert(rodent.flags & Emulate3Button) */
1088 action0.button = action0.obutton;
1089 action0.dx = action0.dy = action0.dz = 0;
1090 action0.flags = flags = 0;
1091 if (r_timeout() && r_statetrans(&action0, &action, A_TIMEOUT)) {
1093 debug("flags:%08x buttons:%08x obuttons:%08x",
1094 action.flags, action.button, action.obutton);
1096 action0.obutton = action0.button;
1100 /* MouseRemote client connect/disconnect */
1101 if ((rodent.mremsfd >= 0) && FD_ISSET(rodent.mremsfd, &fds)) {
1102 mremote_clientchg(TRUE);
1105 if ((rodent.mremcfd >= 0) && FD_ISSET(rodent.mremcfd, &fds)) {
1106 mremote_clientchg(FALSE);
1109 /* mouse movement */
1110 if (read(rodent.mfd, &b, 1) == -1) {
1111 if (errno == EWOULDBLOCK)
1116 if ((flags = r_protocol(b, &action0)) == 0)
1119 if ((rodent.flags & VirtualScroll) || (rodent.flags & HVirtualScroll)) {
1120 /* Allow middle button drags to scroll up and down */
1121 if (action0.button == MOUSE_BUTTON2DOWN) {
1122 if (scroll_state == SCROLL_NOTSCROLLING) {
1123 scroll_state = SCROLL_PREPARE;
1124 debug("PREPARING TO SCROLL");
1126 debug("[BUTTON2] flags:%08x buttons:%08x obuttons:%08x",
1127 action.flags, action.button, action.obutton);
1129 debug("[NOTBUTTON2] flags:%08x buttons:%08x obuttons:%08x",
1130 action.flags, action.button, action.obutton);
1132 /* This isn't a middle button down... move along... */
1133 if (scroll_state == SCROLL_SCROLLING) {
1135 * We were scrolling, someone let go of button 2.
1136 * Now turn autoscroll off.
1138 scroll_state = SCROLL_NOTSCROLLING;
1139 debug("DONE WITH SCROLLING / %d", scroll_state);
1140 } else if (scroll_state == SCROLL_PREPARE) {
1141 mousestatus_t newaction = action0;
1143 /* We were preparing to scroll, but we never moved... */
1144 r_timestamp(&action0);
1145 r_statetrans(&action0, &newaction,
1146 A(newaction.button & MOUSE_BUTTON1DOWN,
1147 action0.button & MOUSE_BUTTON3DOWN));
1149 /* Send middle down */
1150 newaction.button = MOUSE_BUTTON2DOWN;
1151 r_click(&newaction);
1153 /* Send middle up */
1154 r_timestamp(&newaction);
1155 newaction.obutton = newaction.button;
1156 newaction.button = action0.button;
1157 r_click(&newaction);
1162 r_timestamp(&action0);
1163 r_statetrans(&action0, &action,
1164 A(action0.button & MOUSE_BUTTON1DOWN,
1165 action0.button & MOUSE_BUTTON3DOWN));
1166 debug("flags:%08x buttons:%08x obuttons:%08x", action.flags,
1167 action.button, action.obutton);
1169 action0.obutton = action0.button;
1170 flags &= MOUSE_POSCHANGED;
1171 flags |= action.obutton ^ action.button;
1172 action.flags = flags;
1174 if (flags) { /* handler detected action */
1175 r_map(&action, &action2);
1176 debug("activity : buttons 0x%08x dx %d dy %d dz %d",
1177 action2.button, action2.dx, action2.dy, action2.dz);
1179 if ((rodent.flags & VirtualScroll) || (rodent.flags & HVirtualScroll)) {
1181 * If *only* the middle button is pressed AND we are moving
1182 * the stick/trackpoint/nipple, scroll!
1184 if (scroll_state == SCROLL_PREPARE) {
1185 /* Ok, Set we're really scrolling now.... */
1186 if (action2.dy || action2.dx)
1187 scroll_state = SCROLL_SCROLLING;
1189 if (scroll_state == SCROLL_SCROLLING) {
1190 if (rodent.flags & VirtualScroll) {
1191 scroll_movement += action2.dy;
1192 debug("SCROLL: %d", scroll_movement);
1194 if (scroll_movement < -rodent.scrollthreshold) {
1197 scroll_movement = 0;
1199 else if (scroll_movement > rodent.scrollthreshold) {
1202 scroll_movement = 0;
1205 if (rodent.flags & HVirtualScroll) {
1206 hscroll_movement += action2.dx;
1207 debug("HORIZONTAL SCROLL: %d", hscroll_movement);
1209 if (hscroll_movement < -rodent.scrollthreshold) {
1211 hscroll_movement = 0;
1213 else if (hscroll_movement > rodent.scrollthreshold) {
1215 hscroll_movement = 0;
1219 /* Don't move while scrolling */
1220 action2.dx = action2.dy = 0;
1224 if (drift_terminate) {
1225 if ((flags & MOUSE_POSCHANGED) == 0 || action.dz || action2.dz)
1226 drift_last_activity = drift_current_ts;
1228 /* X or/and Y movement only - possibly drift */
1229 tssub(&drift_current_ts, &drift_last_activity, &drift_tmp);
1230 if (tscmp(&drift_tmp, &drift_after_ts, >)) {
1231 tssub(&drift_current_ts, &drift_since, &drift_tmp);
1232 if (tscmp(&drift_tmp, &drift_time_ts, <)) {
1233 drift_last.x += action2.dx;
1234 drift_last.y += action2.dy;
1236 /* discard old accumulated steps (drift) */
1237 if (tscmp(&drift_tmp, &drift_2time_ts, >))
1238 drift_previous.x = drift_previous.y = 0;
1240 drift_previous = drift_last;
1241 drift_last.x = action2.dx;
1242 drift_last.y = action2.dy;
1243 drift_since = drift_current_ts;
1245 if (abs(drift_last.x) + abs(drift_last.y)
1247 /* real movement, pass all accumulated steps */
1248 action2.dx = drift_previous.x + drift_last.x;
1249 action2.dy = drift_previous.y + drift_last.y;
1250 /* and reset accumulators */
1251 tsclr(&drift_since);
1252 drift_last.x = drift_last.y = 0;
1253 /* drift_previous will be cleared at next movement*/
1254 drift_last_activity = drift_current_ts;
1256 continue; /* don't pass current movement to
1264 /* Defer clicks until we aren't VirtualScroll'ing. */
1265 if (scroll_state == SCROLL_NOTSCROLLING)
1268 if (action2.flags & MOUSE_POSCHANGED) {
1269 mouse.operation = MOUSE_MOTION_EVENT;
1270 mouse.u.data.buttons = action2.button;
1271 if (rodent.flags & ExponentialAcc) {
1272 expoacc(action2.dx, action2.dy,
1273 &mouse.u.data.x, &mouse.u.data.y);
1276 linacc(action2.dx, action2.dy,
1277 &mouse.u.data.x, &mouse.u.data.y);
1279 mouse.u.data.z = action2.dz;
1282 ioctl(rodent.cfd, CONS_MOUSECTL, &mouse);
1285 mouse.operation = MOUSE_ACTION;
1286 mouse.u.data.buttons = action2.button;
1287 if (rodent.flags & ExponentialAcc) {
1288 expoacc(action2.dx, action2.dy,
1289 &mouse.u.data.x, &mouse.u.data.y);
1292 linacc(action2.dx, action2.dy,
1293 &mouse.u.data.x, &mouse.u.data.y);
1295 mouse.u.data.z = action2.dz;
1298 ioctl(rodent.cfd, CONS_MOUSECTL, &mouse);
1302 * If the Z axis movement is mapped to an imaginary physical
1303 * button, we need to cook up a corresponding button `up' event
1304 * after sending a button `down' event.
1306 if ((rodent.zmap[0] > 0) && (action.dz != 0)) {
1307 action.obutton = action.button;
1308 action.dx = action.dy = action.dz = 0;
1309 r_map(&action, &action2);
1310 debug("activity : buttons 0x%08x dx %d dy %d dz %d",
1311 action2.button, action2.dx, action2.dy, action2.dz);
1316 mouse.operation = MOUSE_ACTION;
1317 mouse.u.data.buttons = action2.button;
1318 mouse.u.data.x = mouse.u.data.y = mouse.u.data.z = 0;
1321 ioctl(rodent.cfd, CONS_MOUSECTL, &mouse);
1330 hup(__unused int sig)
1336 cleanup(__unused int sig)
1338 if (rodent.rtype == MOUSE_PROTO_X10MOUSEREM)
1339 unlink(_PATH_MOUSEREMOTE);
1344 pause_mouse(__unused int sig)
1352 ** Complain, and free the CPU for more worthy tasks
1357 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
1358 "usage: moused [-DRcdfs] [-I file] [-F rate] [-r resolution] [-S baudrate]",
1359 " [-VH [-U threshold]] [-a X[,Y]] [-C threshold] [-m N=M] [-w N]",
1360 " [-z N] [-t <mousetype>] [-l level] [-3 [-E timeout]]",
1361 " [-T distance[,time[,after]]] -p <port>",
1362 " moused [-d] -i <port|if|type|model|all> -p <port>");
1367 * Output an error message to syslog or stderr as appropriate. If
1368 * `errnum' is non-zero, append its string form to the message.
1371 log_or_warn(int log_pri, int errnum, const char *fmt, ...)
1377 vsnprintf(buf, sizeof(buf), fmt, ap);
1380 strlcat(buf, ": ", sizeof(buf));
1381 strlcat(buf, strerror(errnum), sizeof(buf));
1385 syslog(log_pri, "%s", buf);
1391 ** Mouse interface code, courtesy of XFree86 3.1.2.
1393 ** Note: Various bits have been trimmed, and in my shortsighted enthusiasm
1394 ** to clean, reformat and rationalise naming, it's quite possible that
1395 ** some things in here have been broken.
1399 ** The following code is derived from a module marked :
1402 /* $XConsortium: xf86_Mouse.c,v 1.2 94/10/12 20:33:21 kaleb Exp $ */
1403 /* $XFree86: xc/programs/Xserver/hw/xfree86/common/xf86_Mouse.c,v 3.2 1995/01/28
1404 17:03:40 dawes Exp $ */
1407 * Copyright 1990,91 by Thomas Roell, Dinkelscherben, Germany.
1408 * Copyright 1993 by David Dawes <dawes@physics.su.oz.au>
1410 * Permission to use, copy, modify, distribute, and sell this software and its
1411 * documentation for any purpose is hereby granted without fee, provided that
1412 * the above copyright notice appear in all copies and that both that
1413 * copyright notice and this permission notice appear in supporting
1414 * documentation, and that the names of Thomas Roell and David Dawes not be
1415 * used in advertising or publicity pertaining to distribution of the
1416 * software without specific, written prior permission. Thomas Roell
1417 * and David Dawes makes no representations about the suitability of this
1418 * software for any purpose. It is provided "as is" without express or
1421 * THOMAS ROELL AND DAVID DAWES DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
1422 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
1423 * FITNESS, IN NO EVENT SHALL THOMAS ROELL OR DAVID DAWES BE LIABLE FOR ANY
1424 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
1425 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
1426 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
1427 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1432 ** GlidePoint support from XFree86 3.2.
1433 ** Derived from the module:
1436 /* $XFree86: xc/programs/Xserver/hw/xfree86/common/xf86_Mouse.c,v 3.19 1996/10/16 14:40:51 dawes Exp $ */
1437 /* $XConsortium: xf86_Mouse.c /main/10 1996/01/30 15:16:12 kaleb $ */
1439 /* the following table must be ordered by MOUSE_PROTO_XXX in mouse.h */
1440 static unsigned char proto[][7] = {
1441 /* hd_mask hd_id dp_mask dp_id bytes b4_mask b4_id */
1442 { 0x40, 0x40, 0x40, 0x00, 3, ~0x23, 0x00 }, /* MicroSoft */
1443 { 0xf8, 0x80, 0x00, 0x00, 5, 0x00, 0xff }, /* MouseSystems */
1444 { 0xe0, 0x80, 0x80, 0x00, 3, 0x00, 0xff }, /* Logitech */
1445 { 0xe0, 0x80, 0x80, 0x00, 3, 0x00, 0xff }, /* MMSeries */
1446 { 0x40, 0x40, 0x40, 0x00, 3, ~0x33, 0x00 }, /* MouseMan */
1447 { 0xf8, 0x80, 0x00, 0x00, 5, 0x00, 0xff }, /* Bus */
1448 { 0xf8, 0x80, 0x00, 0x00, 5, 0x00, 0xff }, /* InPort */
1449 { 0xc0, 0x00, 0x00, 0x00, 3, 0x00, 0xff }, /* PS/2 mouse */
1450 { 0xe0, 0x80, 0x80, 0x00, 3, 0x00, 0xff }, /* MM HitTablet */
1451 { 0x40, 0x40, 0x40, 0x00, 3, ~0x33, 0x00 }, /* GlidePoint */
1452 { 0x40, 0x40, 0x40, 0x00, 3, ~0x3f, 0x00 }, /* IntelliMouse */
1453 { 0x40, 0x40, 0x40, 0x00, 3, ~0x33, 0x00 }, /* ThinkingMouse */
1454 { 0xf8, 0x80, 0x00, 0x00, 5, 0x00, 0xff }, /* sysmouse */
1455 { 0x40, 0x40, 0x40, 0x00, 3, ~0x23, 0x00 }, /* X10 MouseRem */
1456 { 0x80, 0x80, 0x00, 0x00, 5, 0x00, 0xff }, /* KIDSPAD */
1457 { 0xc3, 0xc0, 0x00, 0x00, 6, 0x00, 0xff }, /* VersaPad */
1458 { 0x00, 0x00, 0x00, 0x00, 1, 0x00, 0xff }, /* JogDial */
1460 { 0xf8, 0x80, 0x00, 0x00, 5, ~0x2f, 0x10 }, /* Mariqua */
1463 static unsigned char cur_proto[7];
1468 char pnpbuf[256]; /* PnP identifier string may be up to 256 bytes long */
1474 /* set the driver operation level, if applicable */
1475 if (rodent.level < 0)
1477 ioctl(rodent.mfd, MOUSE_SETLEVEL, &rodent.level);
1478 rodent.level = (ioctl(rodent.mfd, MOUSE_GETLEVEL, &level) == 0) ? level : 0;
1481 * Interrogate the driver and get some intelligence on the device...
1482 * The following ioctl functions are not always supported by device
1483 * drivers. When the driver doesn't support them, we just trust the
1484 * user to supply valid information.
1486 rodent.hw.iftype = MOUSE_IF_UNKNOWN;
1487 rodent.hw.model = MOUSE_MODEL_GENERIC;
1488 ioctl(rodent.mfd, MOUSE_GETHWINFO, &rodent.hw);
1490 if (rodent.rtype != MOUSE_PROTO_UNKNOWN)
1491 bcopy(proto[rodent.rtype], cur_proto, sizeof(cur_proto));
1492 rodent.mode.protocol = MOUSE_PROTO_UNKNOWN;
1493 rodent.mode.rate = -1;
1494 rodent.mode.resolution = MOUSE_RES_UNKNOWN;
1495 rodent.mode.accelfactor = 0;
1496 rodent.mode.level = 0;
1497 if (ioctl(rodent.mfd, MOUSE_GETMODE, &rodent.mode) == 0) {
1498 if (rodent.mode.protocol == MOUSE_PROTO_UNKNOWN ||
1499 rodent.mode.protocol >= (int)(sizeof(proto) / sizeof(proto[0]))) {
1500 logwarnx("unknown mouse protocol (%d)", rodent.mode.protocol);
1501 return (MOUSE_PROTO_UNKNOWN);
1503 /* INPORT and BUS are the same... */
1504 if (rodent.mode.protocol == MOUSE_PROTO_INPORT)
1505 rodent.mode.protocol = MOUSE_PROTO_BUS;
1506 if (rodent.mode.protocol != rodent.rtype) {
1507 /* Hmm, the driver doesn't agree with the user... */
1508 if (rodent.rtype != MOUSE_PROTO_UNKNOWN)
1509 logwarnx("mouse type mismatch (%s != %s), %s is assumed",
1510 r_name(rodent.mode.protocol), r_name(rodent.rtype),
1511 r_name(rodent.mode.protocol));
1512 rodent.rtype = rodent.mode.protocol;
1513 bcopy(proto[rodent.rtype], cur_proto, sizeof(cur_proto));
1516 cur_proto[4] = rodent.mode.packetsize;
1517 cur_proto[0] = rodent.mode.syncmask[0]; /* header byte bit mask */
1518 cur_proto[1] = rodent.mode.syncmask[1]; /* header bit pattern */
1521 /* maybe this is a PnP mouse... */
1522 if (rodent.mode.protocol == MOUSE_PROTO_UNKNOWN) {
1524 if (rodent.flags & NoPnP)
1525 return (rodent.rtype);
1526 if (((len = pnpgets(pnpbuf)) <= 0) || !pnpparse(&pnpid, pnpbuf, len))
1527 return (rodent.rtype);
1529 debug("PnP serial mouse: '%*.*s' '%*.*s' '%*.*s'",
1530 pnpid.neisaid, pnpid.neisaid, pnpid.eisaid,
1531 pnpid.ncompat, pnpid.ncompat, pnpid.compat,
1532 pnpid.ndescription, pnpid.ndescription, pnpid.description);
1534 /* we have a valid PnP serial device ID */
1535 rodent.hw.iftype = MOUSE_IF_SERIAL;
1536 t = pnpproto(&pnpid);
1538 rodent.mode.protocol = t->val;
1539 rodent.hw.model = t->val2;
1541 rodent.mode.protocol = MOUSE_PROTO_UNKNOWN;
1543 if (rodent.mode.protocol == MOUSE_PROTO_INPORT)
1544 rodent.mode.protocol = MOUSE_PROTO_BUS;
1546 /* make final adjustment */
1547 if (rodent.mode.protocol != MOUSE_PROTO_UNKNOWN) {
1548 if (rodent.mode.protocol != rodent.rtype) {
1549 /* Hmm, the device doesn't agree with the user... */
1550 if (rodent.rtype != MOUSE_PROTO_UNKNOWN)
1551 logwarnx("mouse type mismatch (%s != %s), %s is assumed",
1552 r_name(rodent.mode.protocol), r_name(rodent.rtype),
1553 r_name(rodent.mode.protocol));
1554 rodent.rtype = rodent.mode.protocol;
1555 bcopy(proto[rodent.rtype], cur_proto, sizeof(cur_proto));
1560 debug("proto params: %02x %02x %02x %02x %d %02x %02x",
1561 cur_proto[0], cur_proto[1], cur_proto[2], cur_proto[3],
1562 cur_proto[4], cur_proto[5], cur_proto[6]);
1564 return (rodent.rtype);
1571 return (gettokenname(rifs, iftype));
1577 const char *unknown = "unknown";
1579 return (type == MOUSE_PROTO_UNKNOWN ||
1580 type >= (int)(sizeof(rnames) / sizeof(rnames[0])) ?
1581 unknown : rnames[type]);
1588 return (gettokenname(rmodels, model));
1594 unsigned char buf[16]; /* scrach buffer */
1601 ** This comment is a little out of context here, but it contains
1602 ** some useful information...
1603 ********************************************************************
1605 ** The following lines take care of the Logitech MouseMan protocols.
1607 ** NOTE: There are different versions of both MouseMan and TrackMan!
1608 ** Hence I add another protocol P_LOGIMAN, which the user can
1609 ** specify as MouseMan in his XF86Config file. This entry was
1610 ** formerly handled as a special case of P_MS. However, people
1611 ** who don't have the middle button problem, can still specify
1612 ** Microsoft and use P_MS.
1614 ** By default, these mice should use a 3 byte Microsoft protocol
1615 ** plus a 4th byte for the middle button. However, the mouse might
1616 ** have switched to a different protocol before we use it, so I send
1617 ** the proper sequence just in case.
1619 ** NOTE: - all commands to (at least the European) MouseMan have to
1620 ** be sent at 1200 Baud.
1621 ** - each command starts with a '*'.
1622 ** - whenever the MouseMan receives a '*', it will switch back
1623 ** to 1200 Baud. Hence I have to select the desired protocol
1624 ** first, then select the baud rate.
1626 ** The protocols supported by the (European) MouseMan are:
1627 ** - 5 byte packed binary protocol, as with the Mouse Systems
1628 ** mouse. Selected by sequence "*U".
1629 ** - 2 button 3 byte MicroSoft compatible protocol. Selected
1630 ** by sequence "*V".
1631 ** - 3 button 3+1 byte MicroSoft compatible protocol (default).
1632 ** Selected by sequence "*X".
1634 ** The following baud rates are supported:
1635 ** - 1200 Baud (default). Selected by sequence "*n".
1636 ** - 9600 Baud. Selected by sequence "*q".
1638 ** Selecting a sample rate is no longer supported with the MouseMan!
1639 ** Some additional lines in xf86Config.c take care of ill configured
1640 ** baud rates and sample rates. (The user will get an error.)
1643 switch (rodent.rtype) {
1645 case MOUSE_PROTO_LOGI:
1647 * The baud rate selection command must be sent at the current
1648 * baud rate; try all likely settings
1650 setmousespeed(9600, rodent.baudrate, rodentcflags[rodent.rtype]);
1651 setmousespeed(4800, rodent.baudrate, rodentcflags[rodent.rtype]);
1652 setmousespeed(2400, rodent.baudrate, rodentcflags[rodent.rtype]);
1653 setmousespeed(1200, rodent.baudrate, rodentcflags[rodent.rtype]);
1654 /* select MM series data format */
1655 write(rodent.mfd, "S", 1);
1656 setmousespeed(rodent.baudrate, rodent.baudrate,
1657 rodentcflags[MOUSE_PROTO_MM]);
1658 /* select report rate/frequency */
1659 if (rodent.rate <= 0) write(rodent.mfd, "O", 1);
1660 else if (rodent.rate <= 15) write(rodent.mfd, "J", 1);
1661 else if (rodent.rate <= 27) write(rodent.mfd, "K", 1);
1662 else if (rodent.rate <= 42) write(rodent.mfd, "L", 1);
1663 else if (rodent.rate <= 60) write(rodent.mfd, "R", 1);
1664 else if (rodent.rate <= 85) write(rodent.mfd, "M", 1);
1665 else if (rodent.rate <= 125) write(rodent.mfd, "Q", 1);
1666 else write(rodent.mfd, "N", 1);
1669 case MOUSE_PROTO_LOGIMOUSEMAN:
1670 /* The command must always be sent at 1200 baud */
1671 setmousespeed(1200, 1200, rodentcflags[rodent.rtype]);
1672 write(rodent.mfd, "*X", 2);
1673 setmousespeed(1200, rodent.baudrate, rodentcflags[rodent.rtype]);
1676 case MOUSE_PROTO_HITTAB:
1677 setmousespeed(1200, rodent.baudrate, rodentcflags[rodent.rtype]);
1680 * Initialize Hitachi PUMA Plus - Model 1212E to desired settings.
1681 * The tablet must be configured to be in MM mode, NO parity,
1682 * Binary Format. xf86Info.sampleRate controls the sensativity
1683 * of the tablet. We only use this tablet for it's 4-button puck
1684 * so we don't run in "Absolute Mode"
1686 write(rodent.mfd, "z8", 2); /* Set Parity = "NONE" */
1688 write(rodent.mfd, "zb", 2); /* Set Format = "Binary" */
1690 write(rodent.mfd, "@", 1); /* Set Report Mode = "Stream" */
1692 write(rodent.mfd, "R", 1); /* Set Output Rate = "45 rps" */
1694 write(rodent.mfd, "I\x20", 2); /* Set Incrememtal Mode "20" */
1696 write(rodent.mfd, "E", 1); /* Set Data Type = "Relative */
1699 /* Resolution is in 'lines per inch' on the Hitachi tablet */
1700 if (rodent.resolution == MOUSE_RES_LOW) c = 'g';
1701 else if (rodent.resolution == MOUSE_RES_MEDIUMLOW) c = 'e';
1702 else if (rodent.resolution == MOUSE_RES_MEDIUMHIGH) c = 'h';
1703 else if (rodent.resolution == MOUSE_RES_HIGH) c = 'd';
1704 else if (rodent.resolution <= 40) c = 'g';
1705 else if (rodent.resolution <= 100) c = 'd';
1706 else if (rodent.resolution <= 200) c = 'e';
1707 else if (rodent.resolution <= 500) c = 'h';
1708 else if (rodent.resolution <= 1000) c = 'j';
1710 write(rodent.mfd, &c, 1);
1713 write(rodent.mfd, "\021", 1); /* Resume DATA output */
1716 case MOUSE_PROTO_THINK:
1717 setmousespeed(1200, rodent.baudrate, rodentcflags[rodent.rtype]);
1718 /* the PnP ID string may be sent again, discard it */
1721 ioctl(rodent.mfd, TIOCFLUSH, &i);
1722 /* send the command to initialize the beast */
1723 for (s = "E5E5"; *s; ++s) {
1724 write(rodent.mfd, s, 1);
1726 FD_SET(rodent.mfd, &fds);
1727 if (select(FD_SETSIZE, &fds, NULL, NULL, NULL) <= 0)
1729 read(rodent.mfd, &c, 1);
1736 case MOUSE_PROTO_JOGDIAL:
1738 case MOUSE_PROTO_MSC:
1739 setmousespeed(1200, rodent.baudrate, rodentcflags[rodent.rtype]);
1740 if (rodent.flags & ClearDTR) {
1742 ioctl(rodent.mfd, TIOCMBIC, &i);
1744 if (rodent.flags & ClearRTS) {
1746 ioctl(rodent.mfd, TIOCMBIC, &i);
1750 case MOUSE_PROTO_SYSMOUSE:
1751 if (rodent.hw.iftype == MOUSE_IF_SYSMOUSE)
1752 setmousespeed(1200, rodent.baudrate, rodentcflags[rodent.rtype]);
1755 case MOUSE_PROTO_BUS:
1756 case MOUSE_PROTO_INPORT:
1757 case MOUSE_PROTO_PS2:
1758 if (rodent.rate >= 0)
1759 rodent.mode.rate = rodent.rate;
1760 if (rodent.resolution != MOUSE_RES_UNKNOWN)
1761 rodent.mode.resolution = rodent.resolution;
1762 ioctl(rodent.mfd, MOUSE_SETMODE, &rodent.mode);
1765 case MOUSE_PROTO_X10MOUSEREM:
1766 mremote_serversetup();
1767 setmousespeed(1200, rodent.baudrate, rodentcflags[rodent.rtype]);
1771 case MOUSE_PROTO_VERSAPAD:
1772 tcsendbreak(rodent.mfd, 0); /* send break for 400 msec */
1774 ioctl(rodent.mfd, TIOCFLUSH, &i);
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);
1784 if ((buf[0] != 'V') || (buf[1] != 'P')|| (buf[7] != '\r'))
1786 setmousespeed(9600, rodent.baudrate, rodentcflags[rodent.rtype]);
1787 tcsendbreak(rodent.mfd, 0); /* send break for 400 msec again */
1788 for (i = 0; i < 7; ++i) {
1790 FD_SET(rodent.mfd, &fds);
1791 if (select(FD_SETSIZE, &fds, NULL, NULL, NULL) <= 0)
1793 read(rodent.mfd, &c, 1);
1799 ioctl(rodent.mfd, TIOCFLUSH, &i);
1803 setmousespeed(1200, rodent.baudrate, rodentcflags[rodent.rtype]);
1809 r_protocol(u_char rBuf, mousestatus_t *act)
1811 /* MOUSE_MSS_BUTTON?DOWN -> MOUSE_BUTTON?DOWN */
1812 static int butmapmss[4] = { /* Microsoft, MouseMan, GlidePoint,
1813 IntelliMouse, Thinking Mouse */
1817 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1819 static int butmapmss2[4] = { /* Microsoft, MouseMan, GlidePoint,
1824 MOUSE_BUTTON2DOWN | MOUSE_BUTTON4DOWN,
1826 /* MOUSE_INTELLI_BUTTON?DOWN -> MOUSE_BUTTON?DOWN */
1827 static int butmapintelli[4] = { /* IntelliMouse, NetMouse, Mie Mouse,
1832 MOUSE_BUTTON2DOWN | MOUSE_BUTTON4DOWN,
1834 /* MOUSE_MSC_BUTTON?UP -> MOUSE_BUTTON?DOWN */
1835 static int butmapmsc[8] = { /* MouseSystems, MMSeries, Logitech,
1840 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
1842 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1843 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
1844 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
1846 /* MOUSE_PS2_BUTTON?DOWN -> MOUSE_BUTTON?DOWN */
1847 static int butmapps2[8] = { /* PS/2 */
1851 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1853 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
1854 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
1855 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
1857 /* for Hitachi tablet */
1858 static int butmaphit[8] = { /* MM HitTablet */
1868 /* for serial VersaPad */
1869 static int butmapversa[8] = { /* VersaPad */
1876 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1877 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1879 /* for PS/2 VersaPad */
1880 static int butmapversaps2[8] = { /* VersaPad */
1886 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1888 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1890 static int pBufP = 0;
1891 static unsigned char pBuf[8];
1892 static int prev_x, prev_y;
1893 static int on = FALSE;
1896 debug("received char 0x%x",(int)rBuf);
1897 if (rodent.rtype == MOUSE_PROTO_KIDSPAD)
1898 return (kidspad(rBuf, act));
1899 if (rodent.rtype == MOUSE_PROTO_GTCO_DIGIPAD)
1900 return (gtco_digipad(rBuf, act));
1903 * Hack for resyncing: We check here for a package that is:
1904 * a) illegal (detected by wrong data-package header)
1905 * b) invalid (0x80 == -128 and that might be wrong for MouseSystems)
1906 * c) bad header-package
1908 * NOTE: b) is a voilation of the MouseSystems-Protocol, since values of
1909 * -128 are allowed, but since they are very seldom we can easily
1910 * use them as package-header with no button pressed.
1911 * NOTE/2: On a PS/2 mouse any byte is valid as a data byte. Furthermore,
1912 * 0x80 is not valid as a header byte. For a PS/2 mouse we skip
1913 * checking data bytes.
1914 * For resyncing a PS/2 mouse we require the two most significant
1915 * bits in the header byte to be 0. These are the overflow bits,
1916 * and in case of an overflow we actually lose sync. Overflows
1917 * are very rare, however, and we quickly gain sync again after
1918 * an overflow condition. This is the best we can do. (Actually,
1919 * we could use bit 0x08 in the header byte for resyncing, since
1920 * that bit is supposed to be always on, but nobody told
1924 if (pBufP != 0 && rodent.rtype != MOUSE_PROTO_PS2 &&
1925 ((rBuf & cur_proto[2]) != cur_proto[3] || rBuf == 0x80))
1927 pBufP = 0; /* skip package */
1930 if (pBufP == 0 && (rBuf & cur_proto[0]) != cur_proto[1])
1933 /* is there an extra data byte? */
1934 if (pBufP >= cur_proto[4] && (rBuf & cur_proto[0]) != cur_proto[1])
1937 * Hack for Logitech MouseMan Mouse - Middle button
1939 * Unfortunately this mouse has variable length packets: the standard
1940 * Microsoft 3 byte packet plus an optional 4th byte whenever the
1941 * middle button status changes.
1943 * We have already processed the standard packet with the movement
1944 * and button info. Now post an event message with the old status
1945 * of the left and right buttons and the updated middle button.
1949 * Even worse, different MouseMen and TrackMen differ in the 4th
1950 * byte: some will send 0x00/0x20, others 0x01/0x21, or even
1951 * 0x02/0x22, so I have to strip off the lower bits.
1954 * HACK for ALPS "fourth button". (It's bit 0x10 of the "fourth byte"
1955 * and it is activated by tapping the glidepad with the finger! 8^)
1956 * We map it to bit bit3, and the reverse map in xf86Events just has
1957 * to be extended so that it is identified as Button 4. The lower
1958 * half of the reverse-map may remain unchanged.
1963 * Receive the fourth byte only when preceding three bytes have
1964 * been detected (pBufP >= cur_proto[4]). In the previous
1965 * versions, the test was pBufP == 0; thus, we may have mistakingly
1966 * received a byte even if we didn't see anything preceding
1970 if ((rBuf & cur_proto[5]) != cur_proto[6]) {
1975 switch (rodent.rtype) {
1977 case MOUSE_PROTO_MARIQUA:
1979 * This mouse has 16! buttons in addition to the standard
1980 * three of them. They return 0x10 though 0x1f in the
1981 * so-called `ten key' mode and 0x30 though 0x3f in the
1982 * `function key' mode. As there are only 31 bits for
1983 * button state (including the standard three), we ignore
1984 * the bit 0x20 and don't distinguish the two modes.
1986 act->dx = act->dy = act->dz = 0;
1987 act->obutton = act->button;
1989 act->button = (1 << (rBuf - 13))
1990 | (act->obutton & (MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN));
1992 * FIXME: this is a button "down" event. There needs to be
1993 * a corresponding button "up" event... XXX
1997 case MOUSE_PROTO_JOGDIAL:
2001 * IntelliMouse, NetMouse (including NetMouse Pro) and Mie Mouse
2002 * always send the fourth byte, whereas the fourth byte is
2003 * optional for GlidePoint and ThinkingMouse. The fourth byte
2004 * is also optional for MouseMan+ and FirstMouse+ in their
2005 * native mode. It is always sent if they are in the IntelliMouse
2008 case MOUSE_PROTO_INTELLI: /* IntelliMouse, NetMouse, Mie Mouse,
2010 act->dx = act->dy = 0;
2011 act->dz = (rBuf & 0x08) ? (rBuf & 0x0f) - 16 : (rBuf & 0x0f);
2012 if ((act->dz >= 7) || (act->dz <= -7))
2014 act->obutton = act->button;
2015 act->button = butmapintelli[(rBuf & MOUSE_MSS_BUTTONS) >> 4]
2016 | (act->obutton & (MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN));
2020 act->dx = act->dy = act->dz = 0;
2021 act->obutton = act->button;
2022 act->button = butmapmss2[(rBuf & MOUSE_MSS_BUTTONS) >> 4]
2023 | (act->obutton & (MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN));
2027 act->flags = ((act->dx || act->dy || act->dz) ? MOUSE_POSCHANGED : 0)
2028 | (act->obutton ^ act->button);
2030 return (act->flags);
2033 if (pBufP >= cur_proto[4])
2035 pBuf[pBufP++] = rBuf;
2036 if (pBufP != cur_proto[4])
2040 * assembly full package
2043 debug("assembled full packet (len %d) %x,%x,%x,%x,%x,%x,%x,%x",
2045 pBuf[0], pBuf[1], pBuf[2], pBuf[3],
2046 pBuf[4], pBuf[5], pBuf[6], pBuf[7]);
2049 act->obutton = act->button;
2050 switch (rodent.rtype)
2052 case MOUSE_PROTO_MS: /* Microsoft */
2053 case MOUSE_PROTO_LOGIMOUSEMAN: /* MouseMan/TrackMan */
2054 case MOUSE_PROTO_X10MOUSEREM: /* X10 MouseRemote */
2055 act->button = act->obutton & MOUSE_BUTTON4DOWN;
2056 if (rodent.flags & ChordMiddle)
2057 act->button |= ((pBuf[0] & MOUSE_MSS_BUTTONS) == MOUSE_MSS_BUTTONS)
2059 : butmapmss[(pBuf[0] & MOUSE_MSS_BUTTONS) >> 4];
2061 act->button |= (act->obutton & MOUSE_BUTTON2DOWN)
2062 | butmapmss[(pBuf[0] & MOUSE_MSS_BUTTONS) >> 4];
2064 /* Send X10 btn events to remote client (ensure -128-+127 range) */
2065 if ((rodent.rtype == MOUSE_PROTO_X10MOUSEREM) &&
2066 ((pBuf[0] & 0xFC) == 0x44) && (pBuf[2] == 0x3F)) {
2067 if (rodent.mremcfd >= 0) {
2068 unsigned char key = (signed char)(((pBuf[0] & 0x03) << 6) |
2070 write(rodent.mremcfd, &key, 1);
2075 act->dx = (signed char)(((pBuf[0] & 0x03) << 6) | (pBuf[1] & 0x3F));
2076 act->dy = (signed char)(((pBuf[0] & 0x0C) << 4) | (pBuf[2] & 0x3F));
2079 case MOUSE_PROTO_GLIDEPOINT: /* GlidePoint */
2080 case MOUSE_PROTO_THINK: /* ThinkingMouse */
2081 case MOUSE_PROTO_INTELLI: /* IntelliMouse, NetMouse, Mie Mouse,
2083 act->button = (act->obutton & (MOUSE_BUTTON2DOWN | MOUSE_BUTTON4DOWN))
2084 | butmapmss[(pBuf[0] & MOUSE_MSS_BUTTONS) >> 4];
2085 act->dx = (signed char)(((pBuf[0] & 0x03) << 6) | (pBuf[1] & 0x3F));
2086 act->dy = (signed char)(((pBuf[0] & 0x0C) << 4) | (pBuf[2] & 0x3F));
2089 case MOUSE_PROTO_MSC: /* MouseSystems Corp */
2091 case MOUSE_PROTO_MARIQUA: /* Mariqua */
2093 act->button = butmapmsc[(~pBuf[0]) & MOUSE_MSC_BUTTONS];
2094 act->dx = (signed char)(pBuf[1]) + (signed char)(pBuf[3]);
2095 act->dy = - ((signed char)(pBuf[2]) + (signed char)(pBuf[4]));
2098 case MOUSE_PROTO_JOGDIAL: /* JogDial */
2104 act->button = MOUSE_BUTTON1DOWN;
2109 case MOUSE_PROTO_HITTAB: /* MM HitTablet */
2110 act->button = butmaphit[pBuf[0] & 0x07];
2111 act->dx = (pBuf[0] & MOUSE_MM_XPOSITIVE) ? pBuf[1] : - pBuf[1];
2112 act->dy = (pBuf[0] & MOUSE_MM_YPOSITIVE) ? - pBuf[2] : pBuf[2];
2115 case MOUSE_PROTO_MM: /* MM Series */
2116 case MOUSE_PROTO_LOGI: /* Logitech Mice */
2117 act->button = butmapmsc[pBuf[0] & MOUSE_MSC_BUTTONS];
2118 act->dx = (pBuf[0] & MOUSE_MM_XPOSITIVE) ? pBuf[1] : - pBuf[1];
2119 act->dy = (pBuf[0] & MOUSE_MM_YPOSITIVE) ? - pBuf[2] : pBuf[2];
2122 case MOUSE_PROTO_VERSAPAD: /* VersaPad */
2123 act->button = butmapversa[(pBuf[0] & MOUSE_VERSA_BUTTONS) >> 3];
2124 act->button |= (pBuf[0] & MOUSE_VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
2125 act->dx = act->dy = 0;
2126 if (!(pBuf[0] & MOUSE_VERSA_IN_USE)) {
2130 x = (pBuf[2] << 6) | pBuf[1];
2133 y = (pBuf[4] << 6) | pBuf[3];
2137 act->dx = prev_x - x;
2138 act->dy = prev_y - y;
2146 case MOUSE_PROTO_BUS: /* Bus */
2147 case MOUSE_PROTO_INPORT: /* InPort */
2148 act->button = butmapmsc[(~pBuf[0]) & MOUSE_MSC_BUTTONS];
2149 act->dx = (signed char)pBuf[1];
2150 act->dy = - (signed char)pBuf[2];
2153 case MOUSE_PROTO_PS2: /* PS/2 */
2154 act->button = butmapps2[pBuf[0] & MOUSE_PS2_BUTTONS];
2155 act->dx = (pBuf[0] & MOUSE_PS2_XNEG) ? pBuf[1] - 256 : pBuf[1];
2156 act->dy = (pBuf[0] & MOUSE_PS2_YNEG) ? -(pBuf[2] - 256) : -pBuf[2];
2158 * Moused usually operates the psm driver at the operation level 1
2159 * which sends mouse data in MOUSE_PROTO_SYSMOUSE protocol.
2160 * The following code takes effect only when the user explicitly
2161 * requets the level 2 at which wheel movement and additional button
2162 * actions are encoded in model-dependent formats. At the level 0
2163 * the following code is no-op because the psm driver says the model
2164 * is MOUSE_MODEL_GENERIC.
2166 switch (rodent.hw.model) {
2167 case MOUSE_MODEL_EXPLORER:
2168 /* wheel and additional button data is in the fourth byte */
2169 act->dz = (pBuf[3] & MOUSE_EXPLORER_ZNEG)
2170 ? (pBuf[3] & 0x0f) - 16 : (pBuf[3] & 0x0f);
2171 act->button |= (pBuf[3] & MOUSE_EXPLORER_BUTTON4DOWN)
2172 ? MOUSE_BUTTON4DOWN : 0;
2173 act->button |= (pBuf[3] & MOUSE_EXPLORER_BUTTON5DOWN)
2174 ? MOUSE_BUTTON5DOWN : 0;
2176 case MOUSE_MODEL_INTELLI:
2177 case MOUSE_MODEL_NET:
2178 /* wheel data is in the fourth byte */
2179 act->dz = (signed char)pBuf[3];
2180 if ((act->dz >= 7) || (act->dz <= -7))
2182 /* some compatible mice may have additional buttons */
2183 act->button |= (pBuf[0] & MOUSE_PS2INTELLI_BUTTON4DOWN)
2184 ? MOUSE_BUTTON4DOWN : 0;
2185 act->button |= (pBuf[0] & MOUSE_PS2INTELLI_BUTTON5DOWN)
2186 ? MOUSE_BUTTON5DOWN : 0;
2188 case MOUSE_MODEL_MOUSEMANPLUS:
2189 if (((pBuf[0] & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC)
2190 && (abs(act->dx) > 191)
2191 && MOUSE_PS2PLUS_CHECKBITS(pBuf)) {
2192 /* the extended data packet encodes button and wheel events */
2193 switch (MOUSE_PS2PLUS_PACKET_TYPE(pBuf)) {
2195 /* wheel data packet */
2196 act->dx = act->dy = 0;
2197 if (pBuf[2] & 0x80) {
2198 /* horizontal roller count - ignore it XXX*/
2200 /* vertical roller count */
2201 act->dz = (pBuf[2] & MOUSE_PS2PLUS_ZNEG)
2202 ? (pBuf[2] & 0x0f) - 16 : (pBuf[2] & 0x0f);
2204 act->button |= (pBuf[2] & MOUSE_PS2PLUS_BUTTON4DOWN)
2205 ? MOUSE_BUTTON4DOWN : 0;
2206 act->button |= (pBuf[2] & MOUSE_PS2PLUS_BUTTON5DOWN)
2207 ? MOUSE_BUTTON5DOWN : 0;
2210 /* this packet type is reserved by Logitech */
2212 * IBM ScrollPoint Mouse uses this packet type to
2213 * encode both vertical and horizontal scroll movement.
2215 act->dx = act->dy = 0;
2216 /* horizontal roller count */
2218 act->dz = (pBuf[2] & MOUSE_SPOINT_WNEG) ? -2 : 2;
2219 /* vertical roller count */
2221 act->dz = (pBuf[2] & MOUSE_SPOINT_ZNEG) ? -1 : 1;
2223 /* vertical roller count */
2224 act->dz = (pBuf[2] & MOUSE_SPOINT_ZNEG)
2225 ? ((pBuf[2] >> 4) & 0x0f) - 16
2226 : ((pBuf[2] >> 4) & 0x0f);
2227 /* horizontal roller count */
2228 act->dw = (pBuf[2] & MOUSE_SPOINT_WNEG)
2229 ? (pBuf[2] & 0x0f) - 16 : (pBuf[2] & 0x0f);
2233 /* device type packet - shouldn't happen */
2236 act->dx = act->dy = 0;
2237 act->button = act->obutton;
2238 debug("unknown PS2++ packet type %d: 0x%02x 0x%02x 0x%02x\n",
2239 MOUSE_PS2PLUS_PACKET_TYPE(pBuf),
2240 pBuf[0], pBuf[1], pBuf[2]);
2244 /* preserve button states */
2245 act->button |= act->obutton & MOUSE_EXTBUTTONS;
2248 case MOUSE_MODEL_GLIDEPOINT:
2249 /* `tapping' action */
2250 act->button |= ((pBuf[0] & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
2252 case MOUSE_MODEL_NETSCROLL:
2253 /* three addtional bytes encode buttons and wheel events */
2254 act->button |= (pBuf[3] & MOUSE_PS2_BUTTON3DOWN)
2255 ? MOUSE_BUTTON4DOWN : 0;
2256 act->button |= (pBuf[3] & MOUSE_PS2_BUTTON1DOWN)
2257 ? MOUSE_BUTTON5DOWN : 0;
2258 act->dz = (pBuf[3] & MOUSE_PS2_XNEG) ? pBuf[4] - 256 : pBuf[4];
2260 case MOUSE_MODEL_THINK:
2261 /* the fourth button state in the first byte */
2262 act->button |= (pBuf[0] & MOUSE_PS2_TAP) ? MOUSE_BUTTON4DOWN : 0;
2264 case MOUSE_MODEL_VERSAPAD:
2265 act->button = butmapversaps2[pBuf[0] & MOUSE_PS2VERSA_BUTTONS];
2267 (pBuf[0] & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
2268 act->dx = act->dy = 0;
2269 if (!(pBuf[0] & MOUSE_PS2VERSA_IN_USE)) {
2273 x = ((pBuf[4] << 8) & 0xf00) | pBuf[1];
2276 y = ((pBuf[4] << 4) & 0xf00) | pBuf[2];
2280 act->dx = prev_x - x;
2281 act->dy = prev_y - y;
2288 case MOUSE_MODEL_4D:
2289 act->dx = (pBuf[1] & 0x80) ? pBuf[1] - 256 : pBuf[1];
2290 act->dy = (pBuf[2] & 0x80) ? -(pBuf[2] - 256) : -pBuf[2];
2291 switch (pBuf[0] & MOUSE_4D_WHEELBITS) {
2298 case 0x40: /* 2nd wheel rolling right XXX */
2301 case 0xc0: /* 2nd wheel rolling left XXX */
2306 case MOUSE_MODEL_4DPLUS:
2307 if ((act->dx < 16 - 256) && (act->dy > 256 - 16)) {
2308 act->dx = act->dy = 0;
2309 if (pBuf[2] & MOUSE_4DPLUS_BUTTON4DOWN)
2310 act->button |= MOUSE_BUTTON4DOWN;
2311 act->dz = (pBuf[2] & MOUSE_4DPLUS_ZNEG)
2312 ? ((pBuf[2] & 0x07) - 8) : (pBuf[2] & 0x07);
2314 /* preserve previous button states */
2315 act->button |= act->obutton & MOUSE_EXTBUTTONS;
2318 case MOUSE_MODEL_GENERIC:
2324 case MOUSE_PROTO_SYSMOUSE: /* sysmouse */
2325 act->button = butmapmsc[(~pBuf[0]) & MOUSE_SYS_STDBUTTONS];
2326 act->dx = (signed char)(pBuf[1]) + (signed char)(pBuf[3]);
2327 act->dy = - ((signed char)(pBuf[2]) + (signed char)(pBuf[4]));
2328 if (rodent.level == 1) {
2329 act->dz = ((signed char)(pBuf[5] << 1) + (signed char)(pBuf[6] << 1)) >> 1;
2330 act->button |= ((~pBuf[7] & MOUSE_SYS_EXTBUTTONS) << 3);
2338 * We don't reset pBufP here yet, as there may be an additional data
2339 * byte in some protocols. See above.
2342 /* has something changed? */
2343 act->flags = ((act->dx || act->dy || act->dz) ? MOUSE_POSCHANGED : 0)
2344 | (act->obutton ^ act->button);
2346 return (act->flags);
2350 r_statetrans(mousestatus_t *a1, mousestatus_t *a2, int trans)
2358 a2->obutton = a2->button;
2359 a2->button = a1->button;
2360 a2->flags = a1->flags;
2363 if (rodent.flags & Emulate3Button) {
2365 debug("state:%d, trans:%d -> state:%d",
2366 mouse_button_state, trans,
2367 states[mouse_button_state].s[trans]);
2369 * Avoid re-ordering button and movement events. While a button
2370 * event is deferred, throw away up to BUTTON2_MAXMOVE movement
2371 * events to allow for mouse jitter. If more movement events
2372 * occur, then complete the deferred button events immediately.
2374 if ((a2->dx != 0 || a2->dy != 0) &&
2375 S_DELAYED(states[mouse_button_state].s[trans])) {
2376 if (++mouse_move_delayed > BUTTON2_MAXMOVE) {
2377 mouse_move_delayed = 0;
2378 mouse_button_state =
2379 states[mouse_button_state].s[A_TIMEOUT];
2382 a2->dx = a2->dy = 0;
2384 mouse_move_delayed = 0;
2385 if (mouse_button_state != states[mouse_button_state].s[trans])
2388 clock_gettime(CLOCK_MONOTONIC_FAST, &mouse_button_state_ts);
2389 mouse_button_state = states[mouse_button_state].s[trans];
2391 ~(MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN);
2392 a2->button &= states[mouse_button_state].mask;
2393 a2->button |= states[mouse_button_state].buttons;
2394 flags = a2->flags & MOUSE_POSCHANGED;
2395 flags |= a2->obutton ^ a2->button;
2396 if (flags & MOUSE_BUTTON2DOWN) {
2397 a2->flags = flags & MOUSE_BUTTON2DOWN;
2405 /* phisical to logical button mapping */
2406 static int p2l[MOUSE_MAXBUTTON] = {
2407 MOUSE_BUTTON1DOWN, MOUSE_BUTTON2DOWN, MOUSE_BUTTON3DOWN, MOUSE_BUTTON4DOWN,
2408 MOUSE_BUTTON5DOWN, MOUSE_BUTTON6DOWN, MOUSE_BUTTON7DOWN, MOUSE_BUTTON8DOWN,
2409 0x00000100, 0x00000200, 0x00000400, 0x00000800,
2410 0x00001000, 0x00002000, 0x00004000, 0x00008000,
2411 0x00010000, 0x00020000, 0x00040000, 0x00080000,
2412 0x00100000, 0x00200000, 0x00400000, 0x00800000,
2413 0x01000000, 0x02000000, 0x04000000, 0x08000000,
2414 0x10000000, 0x20000000, 0x40000000,
2426 r_installmap(char *arg)
2433 arg = skipspace(arg);
2435 while (isdigit(*arg))
2437 arg = skipspace(arg);
2438 if ((arg <= s) || (*arg != '='))
2442 arg = skipspace(++arg);
2444 while (isdigit(*arg))
2446 if ((arg <= s) || (!isspace(*arg) && (*arg != '\0')))
2450 if ((lbutton <= 0) || (lbutton > MOUSE_MAXBUTTON))
2452 if ((pbutton <= 0) || (pbutton > MOUSE_MAXBUTTON))
2454 p2l[pbutton - 1] = 1 << (lbutton - 1);
2455 mstate[lbutton - 1] = &bstate[pbutton - 1];
2462 r_map(mousestatus_t *act1, mousestatus_t *act2)
2465 register int pbuttons;
2468 pbuttons = act1->button;
2471 act2->obutton = act2->button;
2472 if (pbuttons & rodent.wmode) {
2473 pbuttons &= ~rodent.wmode;
2474 act1->dz = act1->dy;
2478 act2->dx = act1->dx;
2479 act2->dy = act1->dy;
2480 act2->dz = act1->dz;
2482 switch (rodent.zmap[0]) {
2483 case 0: /* do nothing */
2486 if (act1->dz != 0) {
2487 act2->dx = act1->dz;
2492 if (act1->dz != 0) {
2493 act2->dy = act1->dz;
2497 default: /* buttons */
2498 pbuttons &= ~(rodent.zmap[0] | rodent.zmap[1]
2499 | rodent.zmap[2] | rodent.zmap[3]);
2500 if ((act1->dz < -1) && rodent.zmap[2]) {
2501 pbuttons |= rodent.zmap[2];
2502 zstate[2].count = 1;
2503 } else if (act1->dz < 0) {
2504 pbuttons |= rodent.zmap[0];
2505 zstate[0].count = 1;
2506 } else if ((act1->dz > 1) && rodent.zmap[3]) {
2507 pbuttons |= rodent.zmap[3];
2508 zstate[3].count = 1;
2509 } else if (act1->dz > 0) {
2510 pbuttons |= rodent.zmap[1];
2511 zstate[1].count = 1;
2517 for (pb = 0; (pb < MOUSE_MAXBUTTON) && (pbuttons != 0); ++pb) {
2518 lbuttons |= (pbuttons & 1) ? p2l[pb] : 0;
2521 act2->button = lbuttons;
2523 act2->flags = ((act2->dx || act2->dy || act2->dz) ? MOUSE_POSCHANGED : 0)
2524 | (act2->obutton ^ act2->button);
2528 r_timestamp(mousestatus_t *act)
2531 struct timespec ts1;
2532 struct timespec ts2;
2533 struct timespec ts3;
2538 mask = act->flags & MOUSE_BUTTONS;
2544 clock_gettime(CLOCK_MONOTONIC_FAST, &ts1);
2545 drift_current_ts = ts1;
2547 /* double click threshold */
2548 ts2.tv_sec = rodent.clickthreshold / 1000;
2549 ts2.tv_nsec = (rodent.clickthreshold % 1000) * 1000000;
2550 tssub(&ts1, &ts2, &ts);
2551 debug("ts: %jd %ld", (intmax_t)ts.tv_sec, ts.tv_nsec);
2553 /* 3 button emulation timeout */
2554 ts2.tv_sec = rodent.button2timeout / 1000;
2555 ts2.tv_nsec = (rodent.button2timeout % 1000) * 1000000;
2556 tssub(&ts1, &ts2, &ts3);
2558 button = MOUSE_BUTTON1DOWN;
2559 for (i = 0; (i < MOUSE_MAXBUTTON) && (mask != 0); ++i) {
2561 if (act->button & button) {
2562 /* the button is down */
2564 (intmax_t)bstate[i].ts.tv_sec, bstate[i].ts.tv_nsec);
2565 if (tscmp(&ts, &bstate[i].ts, >)) {
2566 bstate[i].count = 1;
2572 /* the button is up */
2576 if (act->button & button) {
2577 /* the button has been down */
2578 if (tscmp(&ts3, &bstate[i].ts, >)) {
2579 bstate[i].count = 1;
2581 act->flags |= button;
2582 debug("button %d timeout", i + 1);
2585 /* the button has been up */
2597 struct timespec ts1;
2598 struct timespec ts2;
2600 if (states[mouse_button_state].timeout)
2602 clock_gettime(CLOCK_MONOTONIC_FAST, &ts1);
2603 ts2.tv_sec = rodent.button2timeout / 1000;
2604 ts2.tv_nsec = (rodent.button2timeout % 1000) * 1000000;
2605 tssub(&ts1, &ts2, &ts);
2606 return (tscmp(&ts, &mouse_button_state_ts, >));
2610 r_click(mousestatus_t *act)
2612 struct mouse_info mouse;
2617 mask = act->flags & MOUSE_BUTTONS;
2621 button = MOUSE_BUTTON1DOWN;
2622 for (i = 0; (i < MOUSE_MAXBUTTON) && (mask != 0); ++i) {
2624 debug("mstate[%d]->count:%d", i, mstate[i]->count);
2625 if (act->button & button) {
2626 /* the button is down */
2627 mouse.u.event.value = mstate[i]->count;
2629 /* the button is up */
2630 mouse.u.event.value = 0;
2632 mouse.operation = MOUSE_BUTTON_EVENT;
2633 mouse.u.event.id = button;
2636 ioctl(rodent.cfd, CONS_MOUSECTL, &mouse);
2637 debug("button %d count %d", i + 1, mouse.u.event.value);
2644 /* $XConsortium: posix_tty.c,v 1.3 95/01/05 20:42:55 kaleb Exp $ */
2645 /* $XFree86: xc/programs/Xserver/hw/xfree86/os-support/shared/posix_tty.c,v 3.4 1995/01/28 17:05:03 dawes Exp $ */
2647 * Copyright 1993 by David Dawes <dawes@physics.su.oz.au>
2649 * Permission to use, copy, modify, distribute, and sell this software and its
2650 * documentation for any purpose is hereby granted without fee, provided that
2651 * the above copyright notice appear in all copies and that both that
2652 * copyright notice and this permission notice appear in supporting
2653 * documentation, and that the name of David Dawes
2654 * not be used in advertising or publicity pertaining to distribution of
2655 * the software without specific, written prior permission.
2656 * David Dawes makes no representations about the suitability of this
2657 * software for any purpose. It is provided "as is" without express or
2660 * DAVID DAWES DISCLAIMS ALL WARRANTIES WITH REGARD TO
2661 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
2662 * FITNESS, IN NO EVENT SHALL DAVID DAWES BE LIABLE FOR
2663 * ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
2664 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
2665 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
2666 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2672 setmousespeed(int old, int new, unsigned cflag)
2677 if (tcgetattr(rodent.mfd, &tty) < 0)
2679 logwarn("unable to get status of mouse fd");
2683 tty.c_iflag = IGNBRK | IGNPAR;
2686 tty.c_cflag = (tcflag_t)cflag;
2687 tty.c_cc[VTIME] = 0;
2693 cfsetispeed(&tty, B9600);
2694 cfsetospeed(&tty, B9600);
2697 cfsetispeed(&tty, B4800);
2698 cfsetospeed(&tty, B4800);
2701 cfsetispeed(&tty, B2400);
2702 cfsetospeed(&tty, B2400);
2706 cfsetispeed(&tty, B1200);
2707 cfsetospeed(&tty, B1200);
2710 if (tcsetattr(rodent.mfd, TCSADRAIN, &tty) < 0)
2712 logwarn("unable to set status of mouse fd");
2720 cfsetispeed(&tty, B9600);
2721 cfsetospeed(&tty, B9600);
2725 cfsetispeed(&tty, B4800);
2726 cfsetospeed(&tty, B4800);
2730 cfsetispeed(&tty, B2400);
2731 cfsetospeed(&tty, B2400);
2736 cfsetispeed(&tty, B1200);
2737 cfsetospeed(&tty, B1200);
2740 if (rodent.rtype == MOUSE_PROTO_LOGIMOUSEMAN
2741 || rodent.rtype == MOUSE_PROTO_LOGI)
2743 if (write(rodent.mfd, c, 2) != 2)
2745 logwarn("unable to write to mouse fd");
2751 if (tcsetattr(rodent.mfd, TCSADRAIN, &tty) < 0)
2752 logwarn("unable to set status of mouse fd");
2756 * PnP COM device support
2758 * It's a simplistic implementation, but it works :-)
2763 * Try to elicit a PnP ID as described in
2764 * Microsoft, Hayes: "Plug and Play External COM Device Specification,
2767 * The routine does not fully implement the COM Enumerator as par Section
2768 * 2.1 of the document. In particular, we don't have idle state in which
2769 * the driver software monitors the com port for dynamic connection or
2770 * removal of a device at the port, because `moused' simply quits if no
2773 * In addition, as PnP COM device enumeration procedure slightly has
2774 * changed since its first publication, devices which follow earlier
2775 * revisions of the above spec. may fail to respond if the rev 1.0
2776 * procedure is used. XXX
2781 struct timeval timeout;
2786 * This is the procedure described in rev 1.0 of PnP COM device spec.
2787 * Unfortunately, some devices which comform to earlier revisions of
2788 * the spec gets confused and do not return the ID string...
2790 debug("PnP COM device rev 1.0 probe...");
2792 /* port initialization (2.1.2) */
2793 ioctl(rodent.mfd, TIOCMGET, &i);
2794 i |= TIOCM_DTR; /* DTR = 1 */
2795 i &= ~TIOCM_RTS; /* RTS = 0 */
2796 ioctl(rodent.mfd, TIOCMSET, &i);
2800 * The PnP COM device spec. dictates that the mouse must set DSR
2801 * in response to DTR (by hardware or by software) and that if DSR is
2802 * not asserted, the host computer should think that there is no device
2803 * at this serial port. But some mice just don't do that...
2805 ioctl(rodent.mfd, TIOCMGET, &i);
2806 debug("modem status 0%o", i);
2807 if ((i & TIOCM_DSR) == 0)
2810 /* port setup, 1st phase (2.1.3) */
2811 setmousespeed(1200, 1200, (CS7 | CREAD | CLOCAL | HUPCL));
2812 i = TIOCM_DTR | TIOCM_RTS; /* DTR = 0, RTS = 0 */
2813 ioctl(rodent.mfd, TIOCMBIC, &i);
2815 i = TIOCM_DTR; /* DTR = 1, RTS = 0 */
2816 ioctl(rodent.mfd, TIOCMBIS, &i);
2819 /* wait for response, 1st phase (2.1.4) */
2821 ioctl(rodent.mfd, TIOCFLUSH, &i);
2822 i = TIOCM_RTS; /* DTR = 1, RTS = 1 */
2823 ioctl(rodent.mfd, TIOCMBIS, &i);
2825 /* try to read something */
2827 FD_SET(rodent.mfd, &fds);
2829 timeout.tv_usec = 240000;
2830 if (select(FD_SETSIZE, &fds, NULL, NULL, &timeout) > 0) {
2831 debug("pnpwakeup1(): valid response in first phase.");
2835 /* port setup, 2nd phase (2.1.5) */
2836 i = TIOCM_DTR | TIOCM_RTS; /* DTR = 0, RTS = 0 */
2837 ioctl(rodent.mfd, TIOCMBIC, &i);
2840 /* wait for respose, 2nd phase (2.1.6) */
2842 ioctl(rodent.mfd, TIOCFLUSH, &i);
2843 i = TIOCM_DTR | TIOCM_RTS; /* DTR = 1, RTS = 1 */
2844 ioctl(rodent.mfd, TIOCMBIS, &i);
2846 /* try to read something */
2848 FD_SET(rodent.mfd, &fds);
2850 timeout.tv_usec = 240000;
2851 if (select(FD_SETSIZE, &fds, NULL, NULL, &timeout) > 0) {
2852 debug("pnpwakeup1(): valid response in second phase.");
2862 struct timeval timeout;
2867 * This is a simplified procedure; it simply toggles RTS.
2869 debug("alternate probe...");
2871 ioctl(rodent.mfd, TIOCMGET, &i);
2872 i |= TIOCM_DTR; /* DTR = 1 */
2873 i &= ~TIOCM_RTS; /* RTS = 0 */
2874 ioctl(rodent.mfd, TIOCMSET, &i);
2877 setmousespeed(1200, 1200, (CS7 | CREAD | CLOCAL | HUPCL));
2879 /* wait for respose */
2881 ioctl(rodent.mfd, TIOCFLUSH, &i);
2882 i = TIOCM_DTR | TIOCM_RTS; /* DTR = 1, RTS = 1 */
2883 ioctl(rodent.mfd, TIOCMBIS, &i);
2885 /* try to read something */
2887 FD_SET(rodent.mfd, &fds);
2889 timeout.tv_usec = 240000;
2890 if (select(FD_SETSIZE, &fds, NULL, NULL, &timeout) > 0) {
2891 debug("pnpwakeup2(): valid response.");
2901 struct timeval timeout;
2907 if (!pnpwakeup1() && !pnpwakeup2()) {
2909 * According to PnP spec, we should set DTR = 1 and RTS = 0 while
2910 * in idle state. But, `moused' shall set DTR = RTS = 1 and proceed,
2911 * assuming there is something at the port even if it didn't
2912 * respond to the PnP enumeration procedure.
2914 i = TIOCM_DTR | TIOCM_RTS; /* DTR = 1, RTS = 1 */
2915 ioctl(rodent.mfd, TIOCMBIS, &i);
2919 /* collect PnP COM device ID (2.1.7) */
2922 usleep(240000); /* the mouse must send `Begin ID' within 200msec */
2923 while (read(rodent.mfd, &c, 1) == 1) {
2924 /* we may see "M", or "M3..." before `Begin ID' */
2926 if ((c == 0x08) || (c == 0x28)) { /* Begin ID */
2927 debug("begin-id %02x", c);
2931 debug("%c %02x", c, c);
2936 /* we haven't seen `Begin ID' in time... */
2940 ++c; /* make it `End ID' */
2943 FD_SET(rodent.mfd, &fds);
2945 timeout.tv_usec = 240000;
2946 if (select(FD_SETSIZE, &fds, NULL, NULL, &timeout) <= 0)
2949 read(rodent.mfd, &buf[i], 1);
2950 if (buf[i++] == c) /* End ID */
2957 bcopy(&buf[begin], &buf[0], i);
2959 /* string may not be human readable... */
2960 debug("len:%d, '%-*.*s'", i, i, i, buf);
2962 if (buf[i - 1] == c)
2963 return (i); /* a valid PnP string */
2966 * According to PnP spec, we should set DTR = 1 and RTS = 0 while
2967 * in idle state. But, `moused' shall leave the modem control lines
2968 * as they are. See above.
2972 /* we may still have something in the buffer */
2973 return ((i > 0) ? i : 0);
2977 pnpparse(pnpid_t *id, char *buf, int len)
2989 id->description = NULL;
2994 id->ndescription = 0;
2996 if ((buf[0] != 0x28) && (buf[0] != 0x08)) {
3001 case 'M': /* Microsoft */
3002 id->eisaid = "PNP0F01";
3004 case 'H': /* MouseSystems */
3005 id->eisaid = "PNP0F04";
3008 id->neisaid = strlen(id->eisaid);
3009 id->class = "MOUSE";
3010 id->nclass = strlen(id->class);
3011 debug("non-PnP mouse '%c'", buf[0]);
3016 offset = 0x28 - buf[0];
3018 /* calculate checksum */
3019 for (i = 0; i < len - 3; ++i) {
3023 sum += buf[len - 1];
3024 for (; i < len; ++i)
3026 debug("PnP ID string: '%*.*s'", len, len, buf);
3031 id->revision = ((buf[1] & 0x3f) << 6) | (buf[2] & 0x3f);
3032 debug("PnP rev %d.%02d", id->revision / 100, id->revision % 100);
3034 /* EISA vender and product ID */
3035 id->eisaid = &buf[3];
3038 /* option strings */
3040 if (buf[i] == '\\') {
3041 /* device serial # */
3042 for (j = ++i; i < len; ++i) {
3049 id->serial = &buf[j];
3053 if (buf[i] == '\\') {
3055 for (j = ++i; i < len; ++i) {
3062 id->class = &buf[j];
3066 if (buf[i] == '\\') {
3067 /* compatible driver */
3068 for (j = ++i; i < len; ++i) {
3073 * PnP COM spec prior to v0.96 allowed '*' in this field,
3074 * it's not allowed now; just igore it.
3081 id->compat = &buf[j];
3082 id->ncompat = i - j;
3085 if (buf[i] == '\\') {
3086 /* product description */
3087 for (j = ++i; i < len; ++i) {
3094 id->description = &buf[j];
3095 id->ndescription = i - j;
3099 /* checksum exists if there are any optional fields */
3100 if ((id->nserial > 0) || (id->nclass > 0)
3101 || (id->ncompat > 0) || (id->ndescription > 0)) {
3102 debug("PnP checksum: 0x%X", sum);
3103 sprintf(s, "%02X", sum & 0x0ff);
3104 if (strncmp(s, &buf[len - 3], 2) != 0) {
3107 * I found some mice do not comply with the PnP COM device
3108 * spec regarding checksum... XXX
3110 logwarnx("PnP checksum error", 0);
3120 pnpproto(pnpid_t *id)
3126 if (strncmp(id->class, "MOUSE", id->nclass) != 0 &&
3127 strncmp(id->class, "TABLET", id->nclass) != 0)
3128 /* this is not a mouse! */
3131 if (id->neisaid > 0) {
3132 t = gettoken(pnpprod, id->eisaid, id->neisaid);
3133 if (t->val != MOUSE_PROTO_UNKNOWN)
3138 * The 'Compatible drivers' field may contain more than one
3139 * ID separated by ','.
3141 if (id->ncompat <= 0)
3143 for (i = 0; i < id->ncompat; ++i) {
3144 for (j = i; id->compat[i] != ','; ++i)
3145 if (i >= id->ncompat)
3148 t = gettoken(pnpprod, id->compat + j, i - j);
3149 if (t->val != MOUSE_PROTO_UNKNOWN)
3157 /* name/val mapping */
3160 gettoken(symtab_t *tab, const char *s, int len)
3164 for (i = 0; tab[i].name != NULL; ++i) {
3165 if (strncmp(tab[i].name, s, len) == 0)
3172 gettokenname(symtab_t *tab, int val)
3174 static const char unknown[] = "unknown";
3177 for (i = 0; tab[i].name != NULL; ++i) {
3178 if (tab[i].val == val)
3179 return (tab[i].name);
3186 * code to read from the Genius Kidspad tablet.
3188 The tablet responds to the COM PnP protocol 1.0 with EISA-ID KYE0005,
3189 and to pre-pnp probes (RTS toggle) with 'T' (tablet ?)
3190 9600, 8 bit, parity odd.
3192 The tablet puts out 5 bytes. b0 (mask 0xb8, value 0xb8) contains
3193 the proximity, tip and button info:
3194 (byte0 & 0x1) true = tip pressed
3195 (byte0 & 0x2) true = button pressed
3196 (byte0 & 0x40) false = pen in proximity of tablet.
3198 The next 4 bytes are used for coordinates xl, xh, yl, yh (7 bits valid).
3200 Only absolute coordinates are returned, so we use the following approach:
3201 we store the last coordinates sent when the pen went out of the tablet,
3208 S_IDLE, S_PROXY, S_FIRST, S_DOWN, S_UP
3212 kidspad(u_char rxc, mousestatus_t *act)
3215 static int buflen = 0, b_prev = 0 , x_prev = -1, y_prev = -1;
3216 static k_status status = S_IDLE;
3217 static struct timespec old, now;
3221 if (buflen > 0 && (rxc & 0x80)) {
3222 fprintf(stderr, "invalid code %d 0x%x\n", buflen, rxc);
3225 if (buflen == 0 && (rxc & 0xb8) != 0xb8) {
3226 fprintf(stderr, "invalid code 0 0x%x\n", rxc);
3227 return (0); /* invalid code, no action */
3229 buf[buflen++] = rxc;
3233 buflen = 0; /* for next time... */
3235 x = buf[1]+128*(buf[2] - 7);
3237 y = 28*128 - (buf[3] + 128* (buf[4] - 7));
3244 act->obutton = act->button;
3245 act->dx = act->dy = act->dz = 0;
3246 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
3247 if (buf[0] & 0x40) /* pen went out of reach */
3249 else if (status == S_IDLE) { /* pen is newly near the tablet */
3250 act->flags |= MOUSE_POSCHANGED; /* force update */
3256 act->dx = x - x_prev;
3257 act->dy = y - y_prev;
3258 if (act->dx || act->dy)
3259 act->flags |= MOUSE_POSCHANGED;
3262 if (b_prev != 0 && b_prev != buf[0]) { /* possibly record button change */
3264 if (buf[0] & 0x01) /* tip pressed */
3265 act->button |= MOUSE_BUTTON1DOWN;
3266 if (buf[0] & 0x02) /* button pressed */
3267 act->button |= MOUSE_BUTTON2DOWN;
3268 act->flags |= MOUSE_BUTTONSCHANGED;
3271 return (act->flags);
3275 gtco_digipad (u_char rxc, mousestatus_t *act)
3277 static u_char buf[5];
3278 static int buflen = 0, b_prev = 0 , x_prev = -1, y_prev = -1;
3279 static k_status status = S_IDLE;
3282 #define GTCO_HEADER 0x80
3283 #define GTCO_PROXIMITY 0x40
3284 #define GTCO_START (GTCO_HEADER|GTCO_PROXIMITY)
3285 #define GTCO_BUTTONMASK 0x3c
3288 if (buflen > 0 && ((rxc & GTCO_HEADER) != GTCO_HEADER)) {
3289 fprintf(stderr, "invalid code %d 0x%x\n", buflen, rxc);
3292 if (buflen == 0 && (rxc & GTCO_START) != GTCO_START) {
3293 fprintf(stderr, "invalid code 0 0x%x\n", rxc);
3294 return (0); /* invalid code, no action */
3297 buf[buflen++] = rxc;
3301 buflen = 0; /* for next time... */
3303 x = ((buf[2] & ~GTCO_START) << 6 | (buf[1] & ~GTCO_START));
3304 y = 4768 - ((buf[4] & ~GTCO_START) << 6 | (buf[3] & ~GTCO_START));
3310 act->obutton = act->button;
3311 act->dx = act->dy = act->dz = 0;
3313 if ((buf[0] & 0x40) == 0) /* pen went out of reach */
3315 else if (status == S_IDLE) { /* pen is newly near the tablet */
3316 act->flags |= MOUSE_POSCHANGED; /* force update */
3322 act->dx = x - x_prev;
3323 act->dy = y - y_prev;
3324 if (act->dx || act->dy)
3325 act->flags |= MOUSE_POSCHANGED;
3329 /* possibly record button change */
3330 if (b_prev != 0 && b_prev != buf[0]) {
3332 if (buf[0] & 0x04) {
3333 /* tip pressed/yellow */
3334 act->button |= MOUSE_BUTTON1DOWN;
3336 if (buf[0] & 0x08) {
3338 act->button |= MOUSE_BUTTON2DOWN;
3340 if (buf[0] & 0x10) {
3342 act->button |= MOUSE_BUTTON3DOWN;
3344 if (buf[0] & 0x20) {
3346 act->button |= MOUSE_BUTTON4DOWN;
3348 act->flags |= MOUSE_BUTTONSCHANGED;
3351 return (act->flags);
3355 mremote_serversetup(void)
3357 struct sockaddr_un ad;
3359 /* Open a UNIX domain stream socket to listen for mouse remote clients */
3360 unlink(_PATH_MOUSEREMOTE);
3362 if ((rodent.mremsfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
3363 logerrx(1, "unable to create unix domain socket %s",_PATH_MOUSEREMOTE);
3367 bzero(&ad, sizeof(ad));
3368 ad.sun_family = AF_UNIX;
3369 strcpy(ad.sun_path, _PATH_MOUSEREMOTE);
3371 #define SUN_LEN(unp) (((char *)(unp)->sun_path - (char *)(unp)) + \
3372 strlen((unp)->path))
3374 if (bind(rodent.mremsfd, (struct sockaddr *) &ad, SUN_LEN(&ad)) < 0)
3375 logerrx(1, "unable to bind unix domain socket %s", _PATH_MOUSEREMOTE);
3377 listen(rodent.mremsfd, 1);
3381 mremote_clientchg(int add)
3383 struct sockaddr_un ad;
3387 if (rodent.rtype != MOUSE_PROTO_X10MOUSEREM)
3391 /* Accept client connection, if we don't already have one */
3392 ad_len = sizeof(ad);
3393 fd = accept(rodent.mremsfd, (struct sockaddr *) &ad, &ad_len);
3395 logwarnx("failed accept on mouse remote socket");
3397 if (rodent.mremcfd < 0) {
3398 rodent.mremcfd = fd;
3399 debug("remote client connect...accepted");
3403 debug("another remote client connect...disconnected");
3407 /* Client disconnected */
3408 debug("remote client disconnected");
3409 close(rodent.mremcfd);
3410 rodent.mremcfd = -1;