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