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