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