xref: /openbsd/usr.sbin/wsmoused/wsmoused.c (revision ded45aa8)
1*ded45aa8Sderaadt /* $OpenBSD: wsmoused.c,v 1.6 2001/11/02 16:19:48 deraadt Exp $ */
2673a75b7Saaron 
3673a75b7Saaron /*
4673a75b7Saaron  * Copyright (c) 2001 Jean-Baptiste Marchand, Julien Montagne and Jerome Verdon
5673a75b7Saaron  *
6673a75b7Saaron  * Copyright (c) 1998 by Kazutaka Yokota
7673a75b7Saaron  *
8673a75b7Saaron  * Copyright (c) 1995 Michael Smith
9673a75b7Saaron  *
10673a75b7Saaron  * Copyright (c) 1993 by David Dawes <dawes@xfree86.org>
11673a75b7Saaron  *
12673a75b7Saaron  * Copyright (c) 1990,91 by Thomas Roell, Dinkelscherben, Germany.
13673a75b7Saaron  *
14673a75b7Saaron  * All rights reserved.
15673a75b7Saaron  *
16673a75b7Saaron  * Most of this code was taken from the FreeBSD moused daemon, written by
17673a75b7Saaron  * Michael Smith. The FreeBSD moused daemon already contained code from the
18673a75b7Saaron  * Xfree Project, written by David Dawes and Thomas Roell and Kazutaka Yokota.
19673a75b7Saaron  *
20673a75b7Saaron  * Adaptation to OpenBSD was done by Jean-Baptiste Marchand, Julien Montagne
21673a75b7Saaron  * and Jerome Verdon.
22673a75b7Saaron  *
23673a75b7Saaron  * Redistribution and use in source and binary forms, with or without
24673a75b7Saaron  * modification, are permitted provided that the following conditions
25673a75b7Saaron  * are met:
26673a75b7Saaron  * 1. Redistributions of source code must retain the above copyright
27673a75b7Saaron  *    notice, this list of conditions and the following disclaimer.
28673a75b7Saaron  * 2. Redistributions in binary form must reproduce the above copyright
29673a75b7Saaron  *    notice, this list of conditions and the following disclaimer in the
30673a75b7Saaron  *    documentation and/or other materials provided with the distribution.
31673a75b7Saaron  * 3. All advertising materials mentioning features or use of this software
32673a75b7Saaron  *    must display the following acknowledgement:
33673a75b7Saaron  *	This product includes software developed by
34673a75b7Saaron  *      David Dawes, Jean-Baptiste Marchand, Julien Montagne, Thomas Roell,
35673a75b7Saaron  *      Michael Smith, Jerome Verdon and Kazutaka Yokota.
36673a75b7Saaron  * 4. The name authors may not be used to endorse or promote products
37673a75b7Saaron  *    derived from this software without specific prior written permission.
38673a75b7Saaron  *
39673a75b7Saaron  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
40673a75b7Saaron  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41673a75b7Saaron  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
42673a75b7Saaron  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
43673a75b7Saaron  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44673a75b7Saaron  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
45673a75b7Saaron  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
46673a75b7Saaron  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
47673a75b7Saaron  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
48673a75b7Saaron  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49673a75b7Saaron  *
50673a75b7Saaron  *
51673a75b7Saaron  */
52673a75b7Saaron 
53673a75b7Saaron #include <sys/ioctl.h>
54673a75b7Saaron #include <sys/types.h>
55673a75b7Saaron #include <sys/time.h>
56673a75b7Saaron #include <sys/tty.h>
57673a75b7Saaron #include <dev/wscons/wsconsio.h>
58673a75b7Saaron 
59673a75b7Saaron #include <ctype.h>
60673a75b7Saaron #include <err.h>
61673a75b7Saaron #include <errno.h>
62673a75b7Saaron #include <fcntl.h>
63673a75b7Saaron #include <unistd.h>
64673a75b7Saaron #include <setjmp.h>
65673a75b7Saaron #include <signal.h>
66673a75b7Saaron #include <poll.h>
67673a75b7Saaron #include <stdio.h>
68673a75b7Saaron #include <string.h>
69673a75b7Saaron #include <stdlib.h>
70673a75b7Saaron #include <syslog.h>
71673a75b7Saaron #include <varargs.h>
72673a75b7Saaron 
73673a75b7Saaron #include "mouse_protocols.h"
74673a75b7Saaron #include "wsmoused.h"
75673a75b7Saaron 
76673a75b7Saaron extern char *__progname;
77673a75b7Saaron extern char *mouse_names[];
78673a75b7Saaron 
79673a75b7Saaron int debug = 0;
80673a75b7Saaron int nodaemon = FALSE;
81673a75b7Saaron int background = FALSE;
82673a75b7Saaron int identify = FALSE;
83673a75b7Saaron char *pidfile = "/var/run/wsmoused.pid";
84673a75b7Saaron 
85673a75b7Saaron mouse_t mouse = {
86673a75b7Saaron     flags : 0,
87673a75b7Saaron     portname : NULL,
88673a75b7Saaron     proto : P_UNKNOWN,
89673a75b7Saaron     baudrate : 1200,
90673a75b7Saaron     old_baudrate : 1200,
91673a75b7Saaron     rate : MOUSE_RATE_UNKNOWN,
92673a75b7Saaron     resolution : MOUSE_RES_UNKNOWN,
93673a75b7Saaron     zmap : 0,
94673a75b7Saaron     wmode : 0,
95673a75b7Saaron     mfd : -1,
96673a75b7Saaron     clickthreshold : 500,	/* 0.5 sec */
97673a75b7Saaron };
98673a75b7Saaron 
99673a75b7Saaron /* identify the type of a wsmouse supported mouse */
100673a75b7Saaron void
101673a75b7Saaron wsmouse_identify(void)
102673a75b7Saaron {
103673a75b7Saaron 	unsigned int type;
104673a75b7Saaron 
105b47703b0Smiod 	if (mouse.mfd != -1) {
106b47703b0Smiod 		if (ioctl(mouse.mfd, WSMOUSEIO_GTYPE, &type) == -1)
107b47703b0Smiod 			err(1, "can't detect mouse type");
108673a75b7Saaron 	       	printf("wsmouse supported mouse: ");
109673a75b7Saaron 		switch (type) {
110673a75b7Saaron 		case WSMOUSE_TYPE_VSXXX:
111673a75b7Saaron 			printf("DEC serial\n");
112673a75b7Saaron 			break;
113673a75b7Saaron 		case WSMOUSE_TYPE_PS2:
114673a75b7Saaron 			printf("PS/2 compatible\n");
115673a75b7Saaron 			break;
116673a75b7Saaron 		case WSMOUSE_TYPE_USB:
117673a75b7Saaron 			printf("USB\n");
118673a75b7Saaron 			break;
119673a75b7Saaron 		case WSMOUSE_TYPE_LMS:
120673a75b7Saaron 			printf("Logitech busmouse\n");
121673a75b7Saaron 			break;
122673a75b7Saaron 		case WSMOUSE_TYPE_MMS:
123673a75b7Saaron 			printf("Microsoft InPort mouse\n");
124673a75b7Saaron 			break;
125673a75b7Saaron 		case WSMOUSE_TYPE_TPANEL:
126673a75b7Saaron 			printf("Generic Touch Panel\n");
127673a75b7Saaron 			break;
128673a75b7Saaron 		case WSMOUSE_TYPE_NEXT:
129673a75b7Saaron 			printf("NeXT\n");
130673a75b7Saaron 			break;
131673a75b7Saaron 		case WSMOUSE_TYPE_ARCHIMEDES:
132673a75b7Saaron 			printf("Archimedes\n");
133673a75b7Saaron 			break;
134673a75b7Saaron 		default:
135673a75b7Saaron 			printf("Unknown\n");
136673a75b7Saaron 			break;
137673a75b7Saaron 		}
138b47703b0Smiod 	} else
139b47703b0Smiod 		warnx("unable to open %s\n", mouse.portname);
140673a75b7Saaron }
141673a75b7Saaron 
142673a75b7Saaron 
143673a75b7Saaron 
144673a75b7Saaron /* wsmouse_init : init a wsmouse compatible mouse */
145673a75b7Saaron void
146673a75b7Saaron wsmouse_init(void)
147673a75b7Saaron {
148673a75b7Saaron 	unsigned int res = WSMOUSE_RES_MIN;
149673a75b7Saaron 	unsigned int rate = WSMOUSE_RATE_DEFAULT;
150673a75b7Saaron 
151673a75b7Saaron 	ioctl(mouse.mfd, WSMOUSEIO_SRES, &res);
152673a75b7Saaron 	ioctl(mouse.mfd, WSMOUSEIO_SRATE, &rate);
153673a75b7Saaron }
154673a75b7Saaron 
155673a75b7Saaron 
156673a75b7Saaron /*
157673a75b7Saaron  * Buttons remapping
158673a75b7Saaron  */
159673a75b7Saaron 
160673a75b7Saaron /* physical to logical button mapping */
161673a75b7Saaron static int p2l[MOUSE_MAXBUTTON] = {
162673a75b7Saaron 	MOUSE_BUTTON1,	MOUSE_BUTTON2,	MOUSE_BUTTON3,	MOUSE_BUTTON4,
163673a75b7Saaron 	MOUSE_BUTTON5,	MOUSE_BUTTON6,	MOUSE_BUTTON7,	MOUSE_BUTTON8,
164673a75b7Saaron };
165673a75b7Saaron 
166673a75b7Saaron static char *
167673a75b7Saaron skipspace(char *s)
168673a75b7Saaron {
169673a75b7Saaron 	while(isspace(*s))
170673a75b7Saaron 		++s;
171673a75b7Saaron 	return s;
172673a75b7Saaron }
173673a75b7Saaron 
174673a75b7Saaron /* mouse_installmap : install a map between physical and logical buttons */
175673a75b7Saaron static int
176673a75b7Saaron mouse_installmap(char *arg)
177673a75b7Saaron {
178673a75b7Saaron 	int pbutton;
179673a75b7Saaron 	int lbutton;
180673a75b7Saaron 	char *s;
181673a75b7Saaron 
182673a75b7Saaron 	while (*arg) {
183673a75b7Saaron 		arg = skipspace(arg);
184673a75b7Saaron 		s = arg;
185673a75b7Saaron 		while (isdigit(*arg))
186673a75b7Saaron 			++arg;
187673a75b7Saaron 		arg = skipspace(arg);
188673a75b7Saaron 		if ((arg <= s) || (*arg != '='))
189673a75b7Saaron 			return FALSE;
190673a75b7Saaron 		lbutton = atoi(s);
191673a75b7Saaron 
192673a75b7Saaron 		arg = skipspace(++arg);
193673a75b7Saaron 		s = arg;
194673a75b7Saaron 		while (isdigit(*arg))
195673a75b7Saaron 			++arg;
196b47703b0Smiod 		if (arg <= s || (!isspace(*arg) && *arg != '\0'))
197673a75b7Saaron 			return FALSE;
198673a75b7Saaron 		pbutton = atoi(s);
199673a75b7Saaron 
200b47703b0Smiod 		if (lbutton <= 0 || lbutton > MOUSE_MAXBUTTON)
201673a75b7Saaron 			return FALSE;
202b47703b0Smiod 		if (pbutton <= 0 || pbutton > MOUSE_MAXBUTTON)
203673a75b7Saaron 			return FALSE;
204673a75b7Saaron 		p2l[pbutton - 1] = lbutton - 1;
205673a75b7Saaron 	}
206673a75b7Saaron 
207673a75b7Saaron 	return TRUE;
208673a75b7Saaron }
209673a75b7Saaron 
210673a75b7Saaron /* mouse_map : converts physical buttons to logical buttons */
211673a75b7Saaron static void
212673a75b7Saaron mouse_map(struct wscons_event *orig, struct wscons_event *mapped)
213673a75b7Saaron {
214673a75b7Saaron 	mapped->type = orig->type;
215673a75b7Saaron 	mapped->value = p2l[orig->value];
216673a75b7Saaron }
217673a75b7Saaron 
218673a75b7Saaron /* terminate signals handler */
219673a75b7Saaron static void
220673a75b7Saaron terminate(int sig)
221673a75b7Saaron {
222673a75b7Saaron 	struct wscons_event event;
223e02f5e47Sjbm 	unsigned int res;
224673a75b7Saaron 
225673a75b7Saaron 	if (mouse.mfd != -1) {
226673a75b7Saaron 		event.type = WSCONS_EVENT_WSMOUSED_OFF;
227673a75b7Saaron 		ioctl(mouse.mfd, WSDISPLAYIO_WSMOUSED, &event);
228e02f5e47Sjbm 		res = WSMOUSE_RES_DEFAULT;
229e02f5e47Sjbm 		ioctl(mouse.mfd, WSMOUSEIO_SRES, &res);
230673a75b7Saaron 		close(mouse.mfd);
231673a75b7Saaron 		mouse.mfd = -1;
232673a75b7Saaron 	}
233673a75b7Saaron 	unlink(pidfile);
234*ded45aa8Sderaadt 	_exit(0);
235673a75b7Saaron }
236673a75b7Saaron 
237673a75b7Saaron /* buttons status (for multiple click detection) */
238673a75b7Saaron static struct {
239673a75b7Saaron 	int count;		/* 0: up, 1: single click, 2: double click,... */
240673a75b7Saaron 	struct timeval tv;	/* timestamp on the last `up' event */
241673a75b7Saaron } buttonstate[MOUSE_MAXBUTTON];
242673a75b7Saaron 
243673a75b7Saaron /*
244673a75b7Saaron  * handle button click
245673a75b7Saaron  * Note that an ioctl is sent for each button
246673a75b7Saaron  */
247673a75b7Saaron static void
248673a75b7Saaron mouse_click(struct wscons_event *event)
249673a75b7Saaron {
250673a75b7Saaron 	struct timeval max_date;
251673a75b7Saaron 	struct timeval now;
252673a75b7Saaron 	struct timeval delay;
253673a75b7Saaron 	struct timezone tz;
254673a75b7Saaron 	int i = event->value; /* button number */
255673a75b7Saaron 
256673a75b7Saaron 	gettimeofday(&now, &tz);
257673a75b7Saaron 	delay.tv_sec = mouse.clickthreshold / 1000;
258673a75b7Saaron 	delay.tv_usec = (mouse.clickthreshold % 1000) * 1000;
259673a75b7Saaron 	timersub(&now, &delay, &max_date);
260673a75b7Saaron 
261673a75b7Saaron 	if (event->type == WSCONS_EVENT_MOUSE_DOWN) {
262673a75b7Saaron 		if (timercmp(&max_date, &buttonstate[i].tv, >)) {
263673a75b7Saaron 			buttonstate[i].tv.tv_sec = 0;
264673a75b7Saaron 			buttonstate[i].tv.tv_usec = 0;
265673a75b7Saaron 			buttonstate[i].count = 1;
266b47703b0Smiod 		} else {
267673a75b7Saaron 			buttonstate[i].count++;
268673a75b7Saaron 		}
269673a75b7Saaron 	} else {
270673a75b7Saaron 		/* button is up */
271673a75b7Saaron 		buttonstate[i].tv.tv_sec = now.tv_sec;
272673a75b7Saaron 		buttonstate[i].tv.tv_usec = now.tv_usec;
273673a75b7Saaron 	}
274673a75b7Saaron 
275673a75b7Saaron 	/*
276673a75b7Saaron 	 * we use the time field of wscons_event structure to put the number
277673a75b7Saaron 	 * of multiple clicks
278673a75b7Saaron 	 */
279673a75b7Saaron 	if (event->type == WSCONS_EVENT_MOUSE_DOWN) {
280673a75b7Saaron 		event->time.tv_sec = buttonstate[i].count;
281673a75b7Saaron 		event->time.tv_nsec = 0;
282b47703b0Smiod 	} else {
283673a75b7Saaron 		/* button is up */
284673a75b7Saaron 		event->time.tv_sec = 0;
285673a75b7Saaron 		event->time.tv_nsec = 0;
286673a75b7Saaron 	}
287673a75b7Saaron 	ioctl(mouse.cfd, WSDISPLAYIO_WSMOUSED, event);
288673a75b7Saaron }
289673a75b7Saaron 
290673a75b7Saaron 
291673a75b7Saaron /* workaround for cursor speed on serial mice */
292673a75b7Saaron static void
293673a75b7Saaron normalize_event(struct wscons_event *event)
294673a75b7Saaron {
295673a75b7Saaron 	int dx, dy;
296673a75b7Saaron 	int two_power = 1;
297673a75b7Saaron 
298673a75b7Saaron /* 2: normal speed, 3: slower cursor, 1: faster cursor */
299673a75b7Saaron #define NORMALIZE_DIVISOR 3
300673a75b7Saaron 
301b47703b0Smiod 	switch (event->type) {
302b47703b0Smiod 	case WSCONS_EVENT_MOUSE_DELTA_X:
303673a75b7Saaron 		dx = abs(event->value);
304673a75b7Saaron 		while (dx > 2) {
305673a75b7Saaron 			two_power++;
306673a75b7Saaron 			dx = dx / 2;
307673a75b7Saaron 		}
308673a75b7Saaron 		event->value = event->value / (NORMALIZE_DIVISOR * two_power);
309b47703b0Smiod 		break;
310b47703b0Smiod 	case WSCONS_EVENT_MOUSE_DELTA_Y:
311673a75b7Saaron 		two_power = 1;
312673a75b7Saaron 		dy = abs(event->value);
313673a75b7Saaron 		while (dy > 2) {
314673a75b7Saaron 			two_power++;
315673a75b7Saaron 			dy = dy / 2;
316673a75b7Saaron 		}
317673a75b7Saaron 		event->value = event->value / (NORMALIZE_DIVISOR * two_power);
318b47703b0Smiod 		break;
319673a75b7Saaron 	}
320673a75b7Saaron }
321673a75b7Saaron 
322673a75b7Saaron /* send a wscons_event to the kernel */
323673a75b7Saaron static void
324673a75b7Saaron treat_event(struct wscons_event *event)
325673a75b7Saaron {
326673a75b7Saaron 	struct wscons_event mapped_event;
327673a75b7Saaron 
328673a75b7Saaron 	if (IS_MOTION_EVENT(event->type)) {
329673a75b7Saaron 		ioctl(mouse.cfd, WSDISPLAYIO_WSMOUSED, event);
330b47703b0Smiod 	} else if (IS_BUTTON_EVENT(event->type)) {
331673a75b7Saaron 		mouse_map(event, &mapped_event);
332673a75b7Saaron 		mouse_click(&mapped_event);
333673a75b7Saaron 	}
334673a75b7Saaron }
335673a75b7Saaron 
336673a75b7Saaron 
337673a75b7Saaron /* split a full mouse event into multiples wscons events */
338673a75b7Saaron static void
339673a75b7Saaron split_event(mousestatus_t *act)
340673a75b7Saaron {
341673a75b7Saaron 	struct wscons_event event;
342673a75b7Saaron 	int button, i, mask;
343673a75b7Saaron 
344673a75b7Saaron 	if (act->dx != 0) {
345673a75b7Saaron 		event.type = WSCONS_EVENT_MOUSE_DELTA_X;
346673a75b7Saaron 		event.value = act->dx;
347673a75b7Saaron 		normalize_event(&event);
348673a75b7Saaron 		treat_event(&event);
349673a75b7Saaron 	}
350673a75b7Saaron 	if (act->dy != 0) {
351673a75b7Saaron 		event.type = WSCONS_EVENT_MOUSE_DELTA_Y;
352673a75b7Saaron 		event.value = 0 - act->dy;
353673a75b7Saaron 		normalize_event(&event);
354673a75b7Saaron 		treat_event(&event);
355673a75b7Saaron 	}
356673a75b7Saaron 	if (act->dz != 0) {
357673a75b7Saaron 		event.type = WSCONS_EVENT_MOUSE_DELTA_Z;
358673a75b7Saaron 		event.value = act->dz;
359673a75b7Saaron 		treat_event(&event);
360673a75b7Saaron 	}
361673a75b7Saaron 
362673a75b7Saaron 	/* buttons state */
363673a75b7Saaron 	mask = act->flags & MOUSE_BUTTONS;
364673a75b7Saaron 	if (mask == 0)
365673a75b7Saaron 		/* no button modified */
366673a75b7Saaron 		return;
367673a75b7Saaron 
368673a75b7Saaron 	button = MOUSE_BUTTON1DOWN;
369673a75b7Saaron 	for (i = 0; (i < MOUSE_MAXBUTTON) && (mask != 0); i++) {
370673a75b7Saaron 		if (mask & 1) {
371b47703b0Smiod 			event.type = (act->button & button) ?
372b47703b0Smiod 			    WSCONS_EVENT_MOUSE_DOWN : WSCONS_EVENT_MOUSE_UP;
373673a75b7Saaron 			event.value = i;
374673a75b7Saaron 			treat_event(&event);
375673a75b7Saaron 		}
376673a75b7Saaron 		button <<= 1;
377673a75b7Saaron 		mask >>= 1;
378673a75b7Saaron 	}
379673a75b7Saaron 
380673a75b7Saaron }
381673a75b7Saaron 
382673a75b7Saaron /* main function */
383673a75b7Saaron static void
384673a75b7Saaron wsmoused(void)
385673a75b7Saaron {
386673a75b7Saaron 	mousestatus_t action;
387673a75b7Saaron 	struct wscons_event event; /* original wscons_event */
388673a75b7Saaron 	struct pollfd pfd[1];
389673a75b7Saaron 	int res;
390673a75b7Saaron 	u_char b;
391673a75b7Saaron 	FILE *fp;
392673a75b7Saaron 
393673a75b7Saaron 	if ((mouse.cfd = open("/dev/ttyCcfg", O_RDWR, 0)) == -1)
394673a75b7Saaron 		logerr(1, "cannot open /dev/ttyCcfg");
395673a75b7Saaron 
396673a75b7Saaron 	if (!nodaemon && !background) {
397673a75b7Saaron 		if (daemon(0, 0)) {
398673a75b7Saaron 			logerr(1, "failed to become a daemon");
399673a75b7Saaron 		} else {
400673a75b7Saaron 			background = TRUE;
401673a75b7Saaron 			fp = fopen(pidfile, "w");
402673a75b7Saaron 			if (fp != NULL) {
403673a75b7Saaron 				fprintf(fp, "%d\n", getpid());
404673a75b7Saaron 				fclose(fp);
405673a75b7Saaron 			}
406673a75b7Saaron 		}
407673a75b7Saaron 	}
408673a75b7Saaron 
409673a75b7Saaron 	/* initialization */
410673a75b7Saaron 
411673a75b7Saaron 	event.type = WSCONS_EVENT_WSMOUSED_ON;
412673a75b7Saaron 	res = ioctl(mouse.cfd, WSDISPLAYIO_WSMOUSED, &event);
413673a75b7Saaron 	if (res != 0) {
414673a75b7Saaron 		/* the display driver has no getchar() method */
415b47703b0Smiod 		errx(1, "this display driver has no support for wsmoused\n");
416673a75b7Saaron 	}
417673a75b7Saaron 
418673a75b7Saaron 	bzero(&action, sizeof(action));
419673a75b7Saaron 	bzero(&event, sizeof(event));
420673a75b7Saaron 	bzero(&buttonstate, sizeof(buttonstate));
421673a75b7Saaron 
422673a75b7Saaron 	pfd[0].fd = mouse.mfd;
423673a75b7Saaron 	pfd[0].events = POLLIN;
424673a75b7Saaron 
425673a75b7Saaron 	/* process mouse data */
426673a75b7Saaron 	for (;;) {
42729f11fc7Sfgsch 		if (poll(pfd, 1, INFTIM) <= 0)
428673a75b7Saaron 			logwarn("failed to read from mouse");
429673a75b7Saaron 		if (IS_WSMOUSE_DEV(mouse.portname)) {
430673a75b7Saaron 			/* wsmouse supported mouse */
431673a75b7Saaron 			read(mouse.mfd, &event, sizeof(event));
432673a75b7Saaron 			treat_event(&event);
433b47703b0Smiod 		} else {
434673a75b7Saaron 			/* serial mouse (not wsmouse supported) */
435673a75b7Saaron 			res = read(mouse.mfd, &b, 1);
436673a75b7Saaron 
437673a75b7Saaron 			/* if we have a full mouse event */
438673a75b7Saaron 			if (mouse_protocol(b, &action))
439673a75b7Saaron 				/* split it as multiple wscons_event */
440673a75b7Saaron 				split_event(&action);
441673a75b7Saaron 		}
442673a75b7Saaron 	}
443673a75b7Saaron }
444673a75b7Saaron 
445673a75b7Saaron 
446673a75b7Saaron static void
447673a75b7Saaron usage(void)
448673a75b7Saaron {
449b47703b0Smiod 	fprintf(stderr, "usage: %s [-2df] [-t protocol] [-C threshold] [-I file] \
450673a75b7Saaron [-M N=M] [-p port]", __progname);
451b47703b0Smiod 	fprintf(stderr, "       %s -i [-p port]\n", __progname);
452673a75b7Saaron 	exit(1);
453673a75b7Saaron }
454673a75b7Saaron 
455673a75b7Saaron int
456673a75b7Saaron main(int argc, char **argv)
457673a75b7Saaron {
458673a75b7Saaron 	int opt;
459673a75b7Saaron 	int i;
460673a75b7Saaron 
461673a75b7Saaron #define GETOPT_STRING "2dfhip:t:C:I:M:"
462673a75b7Saaron 	while ((opt = (getopt(argc, argv, GETOPT_STRING))) != -1) {
463673a75b7Saaron 		switch (opt) {
464673a75b7Saaron 		case '2':
465673a75b7Saaron 			/* on two button mice, right button pastes */
466673a75b7Saaron 			p2l[MOUSE_BUTTON3] = MOUSE_BUTTON2;
467673a75b7Saaron 			break;
468673a75b7Saaron 		case 'd':
469673a75b7Saaron 			++debug;
470673a75b7Saaron 			break;
471673a75b7Saaron 		case 'f':
472673a75b7Saaron 			nodaemon = TRUE;
473673a75b7Saaron 			break;
474673a75b7Saaron 		case 'h':
475673a75b7Saaron 			usage();
476673a75b7Saaron 			break;
477673a75b7Saaron 		case 'i':
478673a75b7Saaron 			identify = TRUE;
479673a75b7Saaron 			break;
480673a75b7Saaron 		case 'p':
481673a75b7Saaron 			mouse.portname = strdup(optarg);
482673a75b7Saaron 			break;
483673a75b7Saaron 		case 't':
484673a75b7Saaron 			if (strcmp(optarg, "auto") == 0) {
485673a75b7Saaron 				mouse.proto = P_UNKNOWN;
486673a75b7Saaron 				mouse.flags &= ~NoPnP;
487673a75b7Saaron 				break;
488673a75b7Saaron 			}
489b47703b0Smiod 			for (i = 0; mouse_names[i] != NULL; i++)
490673a75b7Saaron 				if (strcmp(optarg,mouse_names[i]) == 0) {
491673a75b7Saaron 					mouse.proto = i;
492673a75b7Saaron 					mouse.flags |= NoPnP;
493673a75b7Saaron 					break;
494673a75b7Saaron 				}
495b47703b0Smiod 			if (mouse_names[i] != NULL)
496673a75b7Saaron 				break;
497b47703b0Smiod 			warnx("no such mouse protocol `%s'\n", optarg);
498673a75b7Saaron 			usage();
499673a75b7Saaron 			break;
500673a75b7Saaron 		case 'C':
501673a75b7Saaron #define MAX_CLICKTHRESHOLD 2000 /* max delay for double click */
502673a75b7Saaron 			mouse.clickthreshold = atoi(optarg);
503b47703b0Smiod 			if (mouse.clickthreshold < 0 ||
504b47703b0Smiod 			    mouse.clickthreshold > MAX_CLICKTHRESHOLD) {
505b47703b0Smiod 				warnx("invalid threshold `%s': max value is %d\n",
506b47703b0Smiod 				    optarg, MAX_CLICKTHRESHOLD);
507673a75b7Saaron 				usage();
508673a75b7Saaron 			}
509673a75b7Saaron 			break;
510673a75b7Saaron 		case 'I':
511673a75b7Saaron 			pidfile = optarg;
512673a75b7Saaron 			break;
513673a75b7Saaron 		case 'M':
514673a75b7Saaron 			if (!mouse_installmap(optarg)) {
515673a75b7Saaron 				warnx("invalid mapping `%s'\n", optarg);
516673a75b7Saaron 				usage();
517673a75b7Saaron 			}
518673a75b7Saaron 			break;
519673a75b7Saaron 		default:
520673a75b7Saaron 			usage();
521673a75b7Saaron 		}
522673a75b7Saaron 	}
523b47703b0Smiod 	if (mouse.portname == NULL)
524673a75b7Saaron 		/* default is /dev/wsmouse */
525b47703b0Smiod 		mouse.portname = WSMOUSE_DEV;
526673a75b7Saaron 
527673a75b7Saaron 	for (;;) {
528673a75b7Saaron 		signal(SIGINT , terminate);
529673a75b7Saaron 		signal(SIGQUIT, terminate);
530673a75b7Saaron 		signal(SIGTERM, terminate);
531673a75b7Saaron 		signal(SIGKILL, terminate);
532673a75b7Saaron 		if ((mouse.mfd = open(mouse.portname,
533673a75b7Saaron 		    O_RDONLY | O_NONBLOCK, 0)) == -1)
534673a75b7Saaron 			logerr(1, "unable to open %s", mouse.portname);
535673a75b7Saaron 		if (IS_SERIAL_DEV(mouse.portname)) {
536673a75b7Saaron 			if (mouse_identify() == P_UNKNOWN) {
537673a75b7Saaron 				close(mouse.mfd);
538b47703b0Smiod 				logerr(1, "cannot determine mouse type on %s",
539b47703b0Smiod 				    mouse.portname);
540673a75b7Saaron 			}
541673a75b7Saaron 		}
542673a75b7Saaron 
543673a75b7Saaron 		if (identify == TRUE) {
544673a75b7Saaron 			if (IS_WSMOUSE_DEV(mouse.portname))
545673a75b7Saaron 				wsmouse_identify();
546673a75b7Saaron 			else
547673a75b7Saaron 				printf("serial mouse: %s type\n",
5481a0145d3Spvalchev 				    (char *)mouse_name(mouse.proto));
549b47703b0Smiod 			exit(0);
550673a75b7Saaron 		}
551673a75b7Saaron 
552673a75b7Saaron 		mouse_init();
553673a75b7Saaron 		wsmoused();
554673a75b7Saaron 	}
555673a75b7Saaron }
556