moused.c revision 108470
116566Ssos/**
216566Ssos ** Copyright (c) 1995 Michael Smith, All rights reserved.
316566Ssos **
416566Ssos ** Redistribution and use in source and binary forms, with or without
516566Ssos ** modification, are permitted provided that the following conditions
616566Ssos ** are met:
716566Ssos ** 1. Redistributions of source code must retain the above copyright
816566Ssos **    notice, this list of conditions and the following disclaimer as
916566Ssos **    the first lines of this file unmodified.
1016566Ssos ** 2. Redistributions in binary form must reproduce the above copyright
1116566Ssos **    notice, this list of conditions and the following disclaimer in the
1216566Ssos **    documentation and/or other materials provided with the distribution.
1316566Ssos ** 3. All advertising materials mentioning features or use of this software
1416566Ssos **    must display the following acknowledgment:
1516566Ssos **      This product includes software developed by Michael Smith.
1616566Ssos ** 4. The name of the author may not be used to endorse or promote products
1716566Ssos **    derived from this software without specific prior written permission.
1816566Ssos **
1916566Ssos **
2016566Ssos ** THIS SOFTWARE IS PROVIDED BY Michael Smith ``AS IS'' AND ANY
2116566Ssos ** EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2216566Ssos ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2316566Ssos ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Michael Smith BE LIABLE FOR
2416566Ssos ** ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2516566Ssos ** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2616566Ssos ** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
2716566Ssos ** BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2816566Ssos ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
2916566Ssos ** OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
3016566Ssos ** EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3116566Ssos **
3216566Ssos **/
3316566Ssos
3416566Ssos/**
3516566Ssos ** MOUSED.C
3616566Ssos **
3731604Syokota ** Mouse daemon : listens to a serial port, the bus mouse interface, or
3831604Syokota ** the PS/2 mouse port for mouse data stream, interprets data and passes
3931604Syokota ** ioctls off to the console driver.
4016566Ssos **
4116566Ssos ** The mouse interface functions are derived closely from the mouse
4216566Ssos ** handler in the XFree86 X server.  Many thanks to the XFree86 people
4316566Ssos ** for their great work!
4416566Ssos **
4516566Ssos **/
4616566Ssos
4729849Scharnier#ifndef lint
4829849Scharnierstatic const char rcsid[] =
4950479Speter  "$FreeBSD: head/usr.sbin/moused/moused.c 108470 2002-12-30 21:18:15Z schweikh $";
5029849Scharnier#endif /* not lint */
5129849Scharnier
5258231Syokota#include <ctype.h>
5329849Scharnier#include <err.h>
5429849Scharnier#include <errno.h>
5529849Scharnier#include <fcntl.h>
5629849Scharnier#include <limits.h>
5716566Ssos#include <stdio.h>
5816566Ssos#include <stdlib.h>
5931604Syokota#include <stdarg.h>
6016566Ssos#include <string.h>
6131604Syokota#include <ctype.h>
6231604Syokota#include <signal.h>
6331604Syokota#include <setjmp.h>
6416566Ssos#include <termios.h>
6531604Syokota#include <syslog.h>
6666860Sphk#include <sys/mouse.h>
6766834Sphk#include <sys/consio.h>
6821885Ssos#include <sys/types.h>
6921885Ssos#include <sys/time.h>
7036991Sahasty#include <sys/socket.h>
7195629Siedowse#include <sys/stat.h>
7236991Sahasty#include <sys/un.h>
7321885Ssos#include <unistd.h>
7416566Ssos
7531604Syokota#define MAX_CLICKTHRESHOLD	2000	/* 2 seconds */
7658344Syokota#define MAX_BUTTON2TIMEOUT	2000	/* 2 seconds */
7758344Syokota#define DFLT_CLICKTHRESHOLD	 500	/* 0.5 second */
7859465Syokota#define DFLT_BUTTON2TIMEOUT	 100	/* 0.1 second */
7931604Syokota
8079430Siedowse/* Abort 3-button emulation delay after this many movement events. */
8179430Siedowse#define BUTTON2_MAXMOVE	3
8279430Siedowse
8331604Syokota#define TRUE		1
8431604Syokota#define FALSE		0
8531604Syokota
8631604Syokota#define MOUSE_XAXIS	(-1)
8731604Syokota#define MOUSE_YAXIS	(-2)
8831604Syokota
8948778Syokota/* Logitech PS2++ protocol */
9048778Syokota#define MOUSE_PS2PLUS_CHECKBITS(b)	\
9148778Syokota			((((b[2] & 0x03) << 2) | 0x02) == (b[1] & 0x0f))
9248778Syokota#define MOUSE_PS2PLUS_PACKET_TYPE(b)	\
9348778Syokota			(((b[0] & 0x30) >> 2) | ((b[1] & 0x30) >> 4))
9448778Syokota
9531604Syokota#define	ChordMiddle	0x0001
9631604Syokota#define Emulate3Button	0x0002
9731604Syokota#define ClearDTR	0x0004
9831604Syokota#define ClearRTS	0x0008
9931604Syokota#define NoPnP		0x0010
10031604Syokota
10131604Syokota#define ID_NONE		0
10231604Syokota#define ID_PORT		1
10331604Syokota#define ID_IF		2
10431604Syokota#define ID_TYPE 	4
10531604Syokota#define ID_MODEL	8
10631604Syokota#define ID_ALL		(ID_PORT | ID_IF | ID_TYPE | ID_MODEL)
10731604Syokota
10895629Siedowse#define debug(fmt, args...) do {				\
10995629Siedowse	if (debug && nodaemon)					\
11095629Siedowse		warnx(fmt, ##args);				\
11195629Siedowse} while (0)
11216566Ssos
11395629Siedowse#define logerr(e, fmt, args...) do {				\
11495629Siedowse	log_or_warn(LOG_DAEMON | LOG_ERR, errno, fmt, ##args);	\
11595629Siedowse	exit(e);						\
11695629Siedowse} while (0)
11731604Syokota
11895629Siedowse#define logerrx(e, fmt, args...) do {				\
11995629Siedowse	log_or_warn(LOG_DAEMON | LOG_ERR, 0, fmt, ##args);	\
12095629Siedowse	exit(e);						\
12195629Siedowse} while (0)
12231604Syokota
12395629Siedowse#define logwarn(fmt, args...)					\
12495629Siedowse	log_or_warn(LOG_DAEMON | LOG_WARNING, errno, fmt, ##args)
12531604Syokota
12695629Siedowse#define logwarnx(fmt, args...)					\
12795629Siedowse	log_or_warn(LOG_DAEMON | LOG_WARNING, 0, fmt, ##args)
12831604Syokota
12931604Syokota/* structures */
13031604Syokota
13131604Syokota/* symbol table entry */
13231604Syokotatypedef struct {
13331604Syokota    char *name;
13431604Syokota    int val;
13531604Syokota    int val2;
13631604Syokota} symtab_t;
13731604Syokota
13831604Syokota/* serial PnP ID string */
13931604Syokotatypedef struct {
14031604Syokota    int revision;	/* PnP revision, 100 for 1.00 */
14131604Syokota    char *eisaid;	/* EISA ID including mfr ID and product ID */
14231604Syokota    char *serial;	/* serial No, optional */
14331604Syokota    char *class;	/* device class, optional */
14431604Syokota    char *compat;	/* list of compatible drivers, optional */
14531604Syokota    char *description;	/* product description, optional */
14631604Syokota    int neisaid;	/* length of the above fields... */
14731604Syokota    int nserial;
14831604Syokota    int nclass;
14931604Syokota    int ncompat;
15031604Syokota    int ndescription;
15131604Syokota} pnpid_t;
15231604Syokota
15331604Syokota/* global variables */
15431604Syokota
15516566Ssosint	debug = 0;
15631604Syokotaint	nodaemon = FALSE;
15731604Syokotaint	background = FALSE;
15831604Syokotaint	identify = ID_NONE;
15931604Syokotaint	extioctl = FALSE;
16034152Sjkhchar	*pidfile = "/var/run/moused.pid";
16116566Ssos
16231604Syokota/* local variables */
16316566Ssos
16431604Syokota/* interface (the table must be ordered by MOUSE_IF_XXX in mouse.h) */
16531604Syokotastatic symtab_t rifs[] = {
16631604Syokota    { "serial",		MOUSE_IF_SERIAL },
16731604Syokota    { "bus",		MOUSE_IF_BUS },
16831604Syokota    { "inport",		MOUSE_IF_INPORT },
16931604Syokota    { "ps/2",		MOUSE_IF_PS2 },
17031604Syokota    { "sysmouse",	MOUSE_IF_SYSMOUSE },
17144326Syokota    { "usb",		MOUSE_IF_USB },
17231604Syokota    { NULL,		MOUSE_IF_UNKNOWN },
17331604Syokota};
17416566Ssos
17531604Syokota/* types (the table must be ordered by MOUSE_PROTO_XXX in mouse.h) */
17631604Syokotastatic char *rnames[] = {
17716566Ssos    "microsoft",
17816566Ssos    "mousesystems",
17931604Syokota    "logitech",
18016566Ssos    "mmseries",
18131604Syokota    "mouseman",
18216566Ssos    "busmouse",
18331604Syokota    "inportmouse",
18416566Ssos    "ps/2",
18518222Speter    "mmhitab",
18631604Syokota    "glidepoint",
18731604Syokota    "intellimouse",
18831604Syokota    "thinkingmouse",
18931604Syokota    "sysmouse",
19036991Sahasty    "x10mouseremote",
19141271Syokota    "kidspad",
19293071Swill    "versapad",
19393071Swill    "jogdial",
19431604Syokota#if notyet
19531604Syokota    "mariqua",
19631604Syokota#endif
19716566Ssos    NULL
19816566Ssos};
19916566Ssos
20031604Syokota/* models */
20131604Syokotastatic symtab_t	rmodels[] = {
20258231Syokota    { "NetScroll",		MOUSE_MODEL_NETSCROLL },
20358231Syokota    { "NetMouse/NetScroll Optical", MOUSE_MODEL_NET },
20458231Syokota    { "GlidePoint",		MOUSE_MODEL_GLIDEPOINT },
20558231Syokota    { "ThinkingMouse",		MOUSE_MODEL_THINK },
20658231Syokota    { "IntelliMouse",		MOUSE_MODEL_INTELLI },
20758231Syokota    { "EasyScroll/SmartScroll",	MOUSE_MODEL_EASYSCROLL },
20858231Syokota    { "MouseMan+",		MOUSE_MODEL_MOUSEMANPLUS },
20958231Syokota    { "Kidspad",		MOUSE_MODEL_KIDSPAD },
21058231Syokota    { "VersaPad",		MOUSE_MODEL_VERSAPAD },
21158231Syokota    { "IntelliMouse Explorer",	MOUSE_MODEL_EXPLORER },
21258231Syokota    { "4D Mouse",		MOUSE_MODEL_4D },
21358231Syokota    { "4D+ Mouse",		MOUSE_MODEL_4DPLUS },
21458231Syokota    { "generic",		MOUSE_MODEL_GENERIC },
21558231Syokota    { NULL, 			MOUSE_MODEL_UNKNOWN },
21631604Syokota};
21731604Syokota
21831604Syokota/* PnP EISA/product IDs */
21931604Syokotastatic symtab_t pnpprod[] = {
22031604Syokota    /* Kensignton ThinkingMouse */
22131604Syokota    { "KML0001",	MOUSE_PROTO_THINK,	MOUSE_MODEL_THINK },
22231604Syokota    /* MS IntelliMouse */
22331604Syokota    { "MSH0001",	MOUSE_PROTO_INTELLI,	MOUSE_MODEL_INTELLI },
22431604Syokota    /* MS IntelliMouse TrackBall */
22531604Syokota    { "MSH0004",	MOUSE_PROTO_INTELLI,	MOUSE_MODEL_INTELLI },
22658099Sache    /* Tremon Wheel Mouse MUSD */
22758099Sache    { "HTK0001",        MOUSE_PROTO_INTELLI,    MOUSE_MODEL_INTELLI },
22833074Sache    /* Genius PnP Mouse */
22933074Sache    { "KYE0001",	MOUSE_PROTO_MS,		MOUSE_MODEL_GENERIC },
23056524Syokota    /* MouseSystems SmartScroll Mouse (OEM from Genius?) */
23156524Syokota    { "KYE0002",	MOUSE_PROTO_MS,		MOUSE_MODEL_EASYSCROLL },
23233074Sache    /* Genius NetMouse */
23333074Sache    { "KYE0003",	MOUSE_PROTO_INTELLI,	MOUSE_MODEL_NET },
23441271Syokota    /* Genius Kidspad, Easypad and other tablets */
23541271Syokota    { "KYE0005",	MOUSE_PROTO_KIDSPAD,	MOUSE_MODEL_KIDSPAD },
23631604Syokota    /* Genius EZScroll */
23731604Syokota    { "KYEEZ00",	MOUSE_PROTO_MS,		MOUSE_MODEL_EASYSCROLL },
23856335Syokota    /* Logitech Cordless MouseMan Wheel */
23956335Syokota    { "LGI8033",	MOUSE_PROTO_INTELLI,	MOUSE_MODEL_MOUSEMANPLUS },
24031949Syokota    /* Logitech MouseMan (new 4 button model) */
24131949Syokota    { "LGI800C",	MOUSE_PROTO_INTELLI,	MOUSE_MODEL_MOUSEMANPLUS },
24231604Syokota    /* Logitech MouseMan+ */
24331604Syokota    { "LGI8050",	MOUSE_PROTO_INTELLI,	MOUSE_MODEL_MOUSEMANPLUS },
24431604Syokota    /* Logitech FirstMouse+ */
24531604Syokota    { "LGI8051",	MOUSE_PROTO_INTELLI,	MOUSE_MODEL_MOUSEMANPLUS },
24632634Syokota    /* Logitech serial */
24732634Syokota    { "LGI8001",	MOUSE_PROTO_LOGIMOUSEMAN, MOUSE_MODEL_GENERIC },
24858231Syokota    /* A4 Tech 4D/4D+ Mouse */
24958231Syokota    { "A4W0005",	MOUSE_PROTO_INTELLI,	MOUSE_MODEL_4D },
25058231Syokota    /* 8D Scroll Mouse */
25158231Syokota    { "PEC9802",	MOUSE_PROTO_INTELLI,	MOUSE_MODEL_INTELLI },
25258328Syokota    /* Mitsumi Wireless Scroll Mouse */
25358328Syokota    { "MTM6401",	MOUSE_PROTO_INTELLI,	MOUSE_MODEL_INTELLI },
25431604Syokota
25531604Syokota    /* MS bus */
25631604Syokota    { "PNP0F00",	MOUSE_PROTO_BUS,	MOUSE_MODEL_GENERIC },
25731604Syokota    /* MS serial */
25831604Syokota    { "PNP0F01",	MOUSE_PROTO_MS,		MOUSE_MODEL_GENERIC },
25931604Syokota    /* MS InPort */
26031604Syokota    { "PNP0F02",	MOUSE_PROTO_INPORT,	MOUSE_MODEL_GENERIC },
26131604Syokota    /* MS PS/2 */
26231604Syokota    { "PNP0F03",	MOUSE_PROTO_PS2,	MOUSE_MODEL_GENERIC },
26331604Syokota    /*
26431604Syokota     * EzScroll returns PNP0F04 in the compatible device field; but it
26531604Syokota     * doesn't look compatible... XXX
26631604Syokota     */
26731604Syokota    /* MouseSystems */
26831604Syokota    { "PNP0F04",	MOUSE_PROTO_MSC,	MOUSE_MODEL_GENERIC },
26931604Syokota    /* MouseSystems */
27031604Syokota    { "PNP0F05",	MOUSE_PROTO_MSC,	MOUSE_MODEL_GENERIC },
27131604Syokota#if notyet
27231604Syokota    /* Genius Mouse */
27331604Syokota    { "PNP0F06",	MOUSE_PROTO_???,	MOUSE_MODEL_GENERIC },
27431604Syokota    /* Genius Mouse */
27531604Syokota    { "PNP0F07",	MOUSE_PROTO_???,	MOUSE_MODEL_GENERIC },
27631604Syokota#endif
27731604Syokota    /* Logitech serial */
27831604Syokota    { "PNP0F08",	MOUSE_PROTO_LOGIMOUSEMAN, MOUSE_MODEL_GENERIC },
27931604Syokota    /* MS BallPoint serial */
28031604Syokota    { "PNP0F09",	MOUSE_PROTO_MS,		MOUSE_MODEL_GENERIC },
28131604Syokota    /* MS PnP serial */
28231604Syokota    { "PNP0F0A",	MOUSE_PROTO_MS,		MOUSE_MODEL_GENERIC },
28331604Syokota    /* MS PnP BallPoint serial */
28431604Syokota    { "PNP0F0B",	MOUSE_PROTO_MS,		MOUSE_MODEL_GENERIC },
28531604Syokota    /* MS serial comatible */
28631604Syokota    { "PNP0F0C",	MOUSE_PROTO_MS,		MOUSE_MODEL_GENERIC },
28731604Syokota    /* MS InPort comatible */
28831604Syokota    { "PNP0F0D",	MOUSE_PROTO_INPORT,	MOUSE_MODEL_GENERIC },
28931604Syokota    /* MS PS/2 comatible */
29031604Syokota    { "PNP0F0E",	MOUSE_PROTO_PS2,	MOUSE_MODEL_GENERIC },
29131604Syokota    /* MS BallPoint comatible */
29231604Syokota    { "PNP0F0F",	MOUSE_PROTO_MS,		MOUSE_MODEL_GENERIC },
29331604Syokota#if notyet
29431604Syokota    /* TI QuickPort */
29531604Syokota    { "PNP0F10",	MOUSE_PROTO_???,	MOUSE_MODEL_GENERIC },
29631604Syokota#endif
29731604Syokota    /* MS bus comatible */
29831604Syokota    { "PNP0F11",	MOUSE_PROTO_BUS,	MOUSE_MODEL_GENERIC },
29931604Syokota    /* Logitech PS/2 */
30031604Syokota    { "PNP0F12",	MOUSE_PROTO_PS2,	MOUSE_MODEL_GENERIC },
30131604Syokota    /* PS/2 */
30231604Syokota    { "PNP0F13",	MOUSE_PROTO_PS2,	MOUSE_MODEL_GENERIC },
30331604Syokota#if notyet
30431604Syokota    /* MS Kids Mouse */
30531604Syokota    { "PNP0F14",	MOUSE_PROTO_???,	MOUSE_MODEL_GENERIC },
30631604Syokota#endif
30731604Syokota    /* Logitech bus */
30831604Syokota    { "PNP0F15",	MOUSE_PROTO_BUS,	MOUSE_MODEL_GENERIC },
30931604Syokota#if notyet
31031604Syokota    /* Logitech SWIFT */
31131604Syokota    { "PNP0F16",	MOUSE_PROTO_???,	MOUSE_MODEL_GENERIC },
31231604Syokota#endif
31331604Syokota    /* Logitech serial compat */
31431604Syokota    { "PNP0F17",	MOUSE_PROTO_LOGIMOUSEMAN, MOUSE_MODEL_GENERIC },
31531604Syokota    /* Logitech bus compatible */
31631604Syokota    { "PNP0F18",	MOUSE_PROTO_BUS,	MOUSE_MODEL_GENERIC },
31731604Syokota    /* Logitech PS/2 compatible */
31831604Syokota    { "PNP0F19",	MOUSE_PROTO_PS2,	MOUSE_MODEL_GENERIC },
31931604Syokota#if notyet
32031604Syokota    /* Logitech SWIFT compatible */
32131604Syokota    { "PNP0F1A",	MOUSE_PROTO_???,	MOUSE_MODEL_GENERIC },
32231604Syokota    /* HP Omnibook */
32331604Syokota    { "PNP0F1B",	MOUSE_PROTO_???,	MOUSE_MODEL_GENERIC },
32431604Syokota    /* Compaq LTE TrackBall PS/2 */
32531604Syokota    { "PNP0F1C",	MOUSE_PROTO_???,	MOUSE_MODEL_GENERIC },
32631604Syokota    /* Compaq LTE TrackBall serial */
32731604Syokota    { "PNP0F1D",	MOUSE_PROTO_???,	MOUSE_MODEL_GENERIC },
32831604Syokota    /* MS Kidts Trackball */
32931604Syokota    { "PNP0F1E",	MOUSE_PROTO_???,	MOUSE_MODEL_GENERIC },
33031604Syokota#endif
33149967Syokota    /* Interlink VersaPad */
33249967Syokota    { "LNK0001",	MOUSE_PROTO_VERSAPAD,	MOUSE_MODEL_VERSAPAD },
33331604Syokota
33431604Syokota    { NULL,		MOUSE_PROTO_UNKNOWN,	MOUSE_MODEL_GENERIC },
33531604Syokota};
33631604Syokota
33731604Syokota/* the table must be ordered by MOUSE_PROTO_XXX in mouse.h */
33831604Syokotastatic unsigned short rodentcflags[] =
33916566Ssos{
34016566Ssos    (CS7	           | CREAD | CLOCAL | HUPCL ),	/* MicroSoft */
34116566Ssos    (CS8 | CSTOPB	   | CREAD | CLOCAL | HUPCL ),	/* MouseSystems */
34231604Syokota    (CS8 | CSTOPB	   | CREAD | CLOCAL | HUPCL ),	/* Logitech */
34316566Ssos    (CS8 | PARENB | PARODD | CREAD | CLOCAL | HUPCL ),	/* MMSeries */
34416566Ssos    (CS7		   | CREAD | CLOCAL | HUPCL ),	/* MouseMan */
34531604Syokota    0,							/* Bus */
34631604Syokota    0,							/* InPort */
34718222Speter    0,							/* PS/2 */
34831604Syokota    (CS8		   | CREAD | CLOCAL | HUPCL ),	/* MM HitTablet */
34931604Syokota    (CS7	           | CREAD | CLOCAL | HUPCL ),	/* GlidePoint */
35031604Syokota    (CS7                   | CREAD | CLOCAL | HUPCL ),	/* IntelliMouse */
35131604Syokota    (CS7                   | CREAD | CLOCAL | HUPCL ),	/* Thinking Mouse */
35231726Syokota    (CS8 | CSTOPB	   | CREAD | CLOCAL | HUPCL ),	/* sysmouse */
35336991Sahasty    (CS7	           | CREAD | CLOCAL | HUPCL ),	/* X10 MouseRemote */
35441271Syokota    (CS8 | PARENB | PARODD | CREAD | CLOCAL | HUPCL ),	/* kidspad etc. */
35549967Syokota    (CS8		   | CREAD | CLOCAL | HUPCL ),	/* VersaPad */
35693071Swill    0,							/* JogDial */
35731604Syokota#if notyet
35831604Syokota    (CS8 | CSTOPB	   | CREAD | CLOCAL | HUPCL ),	/* Mariqua */
35931604Syokota#endif
36016566Ssos};
36116566Ssos
36231604Syokotastatic struct rodentparam {
36331604Syokota    int flags;
36431604Syokota    char *portname;		/* /dev/XXX */
36531604Syokota    int rtype;			/* MOUSE_PROTO_XXX */
36631604Syokota    int level;			/* operation level: 0 or greater */
36731604Syokota    int baudrate;
36831604Syokota    int rate;			/* report rate */
36931604Syokota    int resolution;		/* MOUSE_RES_XXX or a positive number */
37058231Syokota    int zmap[4];		/* MOUSE_{X|Y}AXIS or a button number */
37141270Syokota    int wmode;			/* wheel mode button number */
37231604Syokota    int mfd;			/* mouse file descriptor */
37331604Syokota    int cfd;			/* /dev/consolectl file descriptor */
37436991Sahasty    int mremsfd;		/* mouse remote server file descriptor */
37536991Sahasty    int mremcfd;		/* mouse remote client file descriptor */
37631604Syokota    long clickthreshold;	/* double click speed in msec */
37758344Syokota    long button2timeout;	/* 3 button emulation timeout */
37831604Syokota    mousehw_t hw;		/* mouse device hardware information */
37931604Syokota    mousemode_t mode;		/* protocol information */
38078770Sgreid    float accelx;		/* Acceleration in the X axis */
38178770Sgreid    float accely;		/* Acceleration in the Y axis */
38231604Syokota} rodent = {
38331604Syokota    flags : 0,
38431604Syokota    portname : NULL,
38531604Syokota    rtype : MOUSE_PROTO_UNKNOWN,
38631604Syokota    level : -1,
38731604Syokota    baudrate : 1200,
38831604Syokota    rate : 0,
38931604Syokota    resolution : MOUSE_RES_UNKNOWN,
39058231Syokota    zmap: { 0, 0, 0, 0 },
39141270Syokota    wmode: 0,
39231604Syokota    mfd : -1,
39331604Syokota    cfd : -1,
39436991Sahasty    mremsfd : -1,
39536991Sahasty    mremcfd : -1,
39658344Syokota    clickthreshold : DFLT_CLICKTHRESHOLD,
39758344Syokota    button2timeout : DFLT_BUTTON2TIMEOUT,
39878770Sgreid    accelx : 1.0,
39978770Sgreid    accely : 1.0,
40031604Syokota};
40116566Ssos
40231604Syokota/* button status */
40358344Syokotastruct button_state {
40431604Syokota    int count;		/* 0: up, 1: single click, 2: double click,... */
40558344Syokota    struct timeval tv;	/* timestamp on the last button event */
40658344Syokota};
40758344Syokotastatic struct button_state	bstate[MOUSE_MAXBUTTON]; /* button state */
40858344Syokotastatic struct button_state	*mstate[MOUSE_MAXBUTTON];/* mapped button st.*/
40958344Syokotastatic struct button_state	zstate[4];		 /* Z/W axis state */
41016566Ssos
41158344Syokota/* state machine for 3 button emulation */
41258344Syokota
41358344Syokota#define S0	0	/* start */
41458344Syokota#define S1	1	/* button 1 delayed down */
41558344Syokota#define S2	2	/* button 3 delayed down */
41658344Syokota#define S3	3	/* both buttons down -> button 2 down */
41758344Syokota#define S4	4	/* button 1 delayed up */
41858344Syokota#define S5	5	/* button 1 down */
41958344Syokota#define S6	6	/* button 3 down */
42058344Syokota#define S7	7	/* both buttons down */
42158344Syokota#define S8	8	/* button 3 delayed up */
42258344Syokota#define S9	9	/* button 1 or 3 up after S3 */
42358344Syokota
42458344Syokota#define A(b1, b3)	(((b1) ? 2 : 0) | ((b3) ? 1 : 0))
42558344Syokota#define A_TIMEOUT	4
42679430Siedowse#define S_DELAYED(st)	(states[st].s[A_TIMEOUT] != (st))
42758344Syokota
42858344Syokotastatic struct {
42958344Syokota    int s[A_TIMEOUT + 1];
43058344Syokota    int buttons;
43158344Syokota    int mask;
43259090Syokota    int timeout;
43358344Syokota} states[10] = {
43458344Syokota    /* S0 */
43559090Syokota    { { S0, S2, S1, S3, S0 }, 0, ~(MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN), FALSE },
43658344Syokota    /* S1 */
43759090Syokota    { { S4, S2, S1, S3, S5 }, 0, ~MOUSE_BUTTON1DOWN, FALSE },
43858344Syokota    /* S2 */
43959090Syokota    { { S8, S2, S1, S3, S6 }, 0, ~MOUSE_BUTTON3DOWN, FALSE },
44058344Syokota    /* S3 */
44159090Syokota    { { S0, S9, S9, S3, S3 }, MOUSE_BUTTON2DOWN, ~0, FALSE },
44258344Syokota    /* S4 */
44359090Syokota    { { S0, S2, S1, S3, S0 }, MOUSE_BUTTON1DOWN, ~0, TRUE },
44458344Syokota    /* S5 */
44559090Syokota    { { S0, S2, S5, S7, S5 }, MOUSE_BUTTON1DOWN, ~0, FALSE },
44658344Syokota    /* S6 */
44759090Syokota    { { S0, S6, S1, S7, S6 }, MOUSE_BUTTON3DOWN, ~0, FALSE },
44858344Syokota    /* S7 */
44959090Syokota    { { S0, S6, S5, S7, S7 }, MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, ~0, FALSE },
45058344Syokota    /* S8 */
45159090Syokota    { { S0, S2, S1, S3, S0 }, MOUSE_BUTTON3DOWN, ~0, TRUE },
45258344Syokota    /* S9 */
45359090Syokota    { { S0, S9, S9, S3, S9 }, 0, ~(MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN), FALSE },
45458344Syokota};
45558344Syokotastatic int		mouse_button_state;
45658344Syokotastatic struct timeval	mouse_button_state_tv;
45779430Siedowsestatic int		mouse_move_delayed;
45858344Syokota
45931604Syokotastatic jmp_buf env;
46016566Ssos
46131604Syokota/* function prototypes */
46216566Ssos
46331604Syokotastatic void	moused(void);
46431604Syokotastatic void	hup(int sig);
46536991Sahastystatic void	cleanup(int sig);
46631604Syokotastatic void	usage(void);
46795629Siedowsestatic void	log_or_warn(int log_pri, int errnum, const char *fmt, ...)
46895629Siedowse		    __printflike(3, 4);
46931604Syokota
47031604Syokotastatic int	r_identify(void);
47131604Syokotastatic char	*r_if(int type);
47231604Syokotastatic char	*r_name(int type);
47331604Syokotastatic char	*r_model(int model);
47431604Syokotastatic void	r_init(void);
47531604Syokotastatic int	r_protocol(u_char b, mousestatus_t *act);
47658344Syokotastatic int	r_statetrans(mousestatus_t *a1, mousestatus_t *a2, int trans);
47731604Syokotastatic int	r_installmap(char *arg);
47831604Syokotastatic void	r_map(mousestatus_t *act1, mousestatus_t *act2);
47958344Syokotastatic void	r_timestamp(mousestatus_t *act);
48058344Syokotastatic int	r_timeout(void);
48131604Syokotastatic void	r_click(mousestatus_t *act);
48231604Syokotastatic void	setmousespeed(int old, int new, unsigned cflag);
48331604Syokota
48440255Syokotastatic int	pnpwakeup1(void);
48540255Syokotastatic int	pnpwakeup2(void);
48631604Syokotastatic int	pnpgets(char *buf);
48731604Syokotastatic int	pnpparse(pnpid_t *id, char *buf, int len);
48831604Syokotastatic symtab_t	*pnpproto(pnpid_t *id);
48931604Syokota
49031604Syokotastatic symtab_t	*gettoken(symtab_t *tab, char *s, int len);
49131604Syokotastatic char	*gettokenname(symtab_t *tab, int val);
49231604Syokota
49336991Sahastystatic void	mremote_serversetup();
49436991Sahastystatic void	mremote_clientchg(int add);
49536991Sahasty
49641271Syokotastatic int kidspad(u_char rxc, mousestatus_t *act);
49741271Syokota
49851287Speterint
49916566Ssosmain(int argc, char *argv[])
50016566Ssos{
50131604Syokota    int c;
50231604Syokota    int	i;
50358231Syokota    int	j;
50416566Ssos
50558344Syokota    for (i = 0; i < MOUSE_MAXBUTTON; ++i)
50658344Syokota	mstate[i] = &bstate[i];
50758344Syokota
50878770Sgreid    while((c = getopt(argc,argv,"3C:DE:F:I:PRS:a:cdfhi:l:m:p:r:st:w:z:")) != -1)
50931604Syokota	switch(c) {
51031604Syokota
51131604Syokota	case '3':
51231604Syokota	    rodent.flags |= Emulate3Button;
51331604Syokota	    break;
51431604Syokota
51558344Syokota	case 'E':
51658344Syokota	    rodent.button2timeout = atoi(optarg);
51758344Syokota	    if ((rodent.button2timeout < 0) ||
51858344Syokota	        (rodent.button2timeout > MAX_BUTTON2TIMEOUT)) {
51958344Syokota	        warnx("invalid argument `%s'", optarg);
52058344Syokota	        usage();
52158344Syokota	    }
52258344Syokota	    break;
52358344Syokota
52478770Sgreid	case 'a':
52578770Sgreid	    i = sscanf(optarg, "%f,%f", &rodent.accelx, &rodent.accely);
52678770Sgreid	    if (i == 0) {
52778770Sgreid		warnx("invalid acceleration argument '%s'", optarg);
52878770Sgreid		usage();
52978770Sgreid	    }
53078770Sgreid
53178770Sgreid	    if (i == 1)
53278770Sgreid		rodent.accely = rodent.accelx;
53378770Sgreid
53478770Sgreid	    break;
53578770Sgreid
53616566Ssos	case 'c':
53716566Ssos	    rodent.flags |= ChordMiddle;
53816566Ssos	    break;
53916566Ssos
54016566Ssos	case 'd':
54131604Syokota	    ++debug;
54216566Ssos	    break;
54316566Ssos
54416566Ssos	case 'f':
54531604Syokota	    nodaemon = TRUE;
54616566Ssos	    break;
54716566Ssos
54831604Syokota	case 'i':
54931604Syokota	    if (strcmp(optarg, "all") == 0)
55031604Syokota	        identify = ID_ALL;
55131604Syokota	    else if (strcmp(optarg, "port") == 0)
55231604Syokota	        identify = ID_PORT;
55331604Syokota	    else if (strcmp(optarg, "if") == 0)
55431604Syokota	        identify = ID_IF;
55531604Syokota	    else if (strcmp(optarg, "type") == 0)
55631604Syokota	        identify = ID_TYPE;
55731604Syokota	    else if (strcmp(optarg, "model") == 0)
55831604Syokota	        identify = ID_MODEL;
55931604Syokota	    else {
56031604Syokota	        warnx("invalid argument `%s'", optarg);
56131604Syokota	        usage();
56231604Syokota	    }
56331604Syokota	    nodaemon = TRUE;
56431604Syokota	    break;
56531604Syokota
56631604Syokota	case 'l':
56731604Syokota	    rodent.level = atoi(optarg);
56831604Syokota	    if ((rodent.level < 0) || (rodent.level > 4)) {
56931604Syokota	        warnx("invalid argument `%s'", optarg);
57031604Syokota	        usage();
57131604Syokota	    }
57231604Syokota	    break;
57331604Syokota
57431604Syokota	case 'm':
57531604Syokota	    if (!r_installmap(optarg)) {
57631604Syokota	        warnx("invalid argument `%s'", optarg);
57731604Syokota	        usage();
57831604Syokota	    }
57931604Syokota	    break;
58031604Syokota
58116566Ssos	case 'p':
58216566Ssos	    rodent.portname = optarg;
58316566Ssos	    break;
58416566Ssos
58524377Speter	case 'r':
58631604Syokota	    if (strcmp(optarg, "high") == 0)
58731604Syokota	        rodent.resolution = MOUSE_RES_HIGH;
58831604Syokota	    else if (strcmp(optarg, "medium-high") == 0)
58931604Syokota	        rodent.resolution = MOUSE_RES_HIGH;
59031604Syokota	    else if (strcmp(optarg, "medium-low") == 0)
59131604Syokota	        rodent.resolution = MOUSE_RES_MEDIUMLOW;
59231604Syokota	    else if (strcmp(optarg, "low") == 0)
59331604Syokota	        rodent.resolution = MOUSE_RES_LOW;
59431604Syokota	    else if (strcmp(optarg, "default") == 0)
59531604Syokota	        rodent.resolution = MOUSE_RES_DEFAULT;
59631604Syokota	    else {
59731604Syokota	        rodent.resolution = atoi(optarg);
59831604Syokota	        if (rodent.resolution <= 0) {
59931604Syokota	            warnx("invalid argument `%s'", optarg);
60031604Syokota	            usage();
60131604Syokota	        }
60231604Syokota	    }
60324377Speter	    break;
60424377Speter
60516566Ssos	case 's':
60616566Ssos	    rodent.baudrate = 9600;
60718222Speter	    break;
60816566Ssos
60941270Syokota	case 'w':
61041270Syokota	    i = atoi(optarg);
61141270Syokota	    if ((i <= 0) || (i > MOUSE_MAXBUTTON)) {
61241270Syokota		warnx("invalid argument `%s'", optarg);
61341270Syokota		usage();
61441270Syokota	    }
61541270Syokota	    rodent.wmode = 1 << (i - 1);
61641270Syokota	    break;
61741270Syokota
61831604Syokota	case 'z':
61931604Syokota	    if (strcmp(optarg, "x") == 0)
62058231Syokota		rodent.zmap[0] = MOUSE_XAXIS;
62131604Syokota	    else if (strcmp(optarg, "y") == 0)
62258231Syokota		rodent.zmap[0] = MOUSE_YAXIS;
62331604Syokota            else {
62431604Syokota		i = atoi(optarg);
62531604Syokota		/*
62631604Syokota		 * Use button i for negative Z axis movement and
62731604Syokota		 * button (i + 1) for positive Z axis movement.
62831604Syokota		 */
62931604Syokota		if ((i <= 0) || (i > MOUSE_MAXBUTTON - 1)) {
63031604Syokota	            warnx("invalid argument `%s'", optarg);
63131604Syokota	            usage();
63231604Syokota		}
63358344Syokota		rodent.zmap[0] = i;
63458344Syokota		rodent.zmap[1] = i + 1;
63558861Syokota		debug("optind: %d, optarg: '%s'", optind, optarg);
63658231Syokota		for (j = 1; j < 4; ++j) {
63758231Syokota		    if ((optind >= argc) || !isdigit(*argv[optind]))
63858231Syokota			break;
63958231Syokota		    i = atoi(argv[optind]);
64058231Syokota		    if ((i <= 0) || (i > MOUSE_MAXBUTTON - 1)) {
64158231Syokota			warnx("invalid argument `%s'", argv[optind]);
64258231Syokota			usage();
64358231Syokota		    }
64458344Syokota		    rodent.zmap[j] = i;
64558231Syokota		    ++optind;
64658231Syokota		}
64759090Syokota		if ((rodent.zmap[2] != 0) && (rodent.zmap[3] == 0))
64858344Syokota		    rodent.zmap[3] = rodent.zmap[2] + 1;
64931604Syokota	    }
65018222Speter	    break;
65118222Speter
65231604Syokota	case 'C':
65331604Syokota	    rodent.clickthreshold = atoi(optarg);
65431604Syokota	    if ((rodent.clickthreshold < 0) ||
65531604Syokota	        (rodent.clickthreshold > MAX_CLICKTHRESHOLD)) {
65631604Syokota	        warnx("invalid argument `%s'", optarg);
65731604Syokota	        usage();
65831604Syokota	    }
65931604Syokota	    break;
66031604Syokota
66118222Speter	case 'D':
66231604Syokota	    rodent.flags |= ClearDTR;
66318222Speter	    break;
66418222Speter
66531604Syokota	case 'F':
66631604Syokota	    rodent.rate = atoi(optarg);
66731604Syokota	    if (rodent.rate <= 0) {
66831604Syokota	        warnx("invalid argument `%s'", optarg);
66931604Syokota	        usage();
67031604Syokota	    }
67131604Syokota	    break;
67231604Syokota
67334152Sjkh	case 'I':
67434152Sjkh	    pidfile = optarg;
67534152Sjkh	    break;
67634152Sjkh
67731604Syokota	case 'P':
67831604Syokota	    rodent.flags |= NoPnP;
67931604Syokota	    break;
68031604Syokota
68131604Syokota	case 'R':
68231604Syokota	    rodent.flags |= ClearRTS;
68331604Syokota	    break;
68431604Syokota
68518222Speter	case 'S':
68618222Speter	    rodent.baudrate = atoi(optarg);
68731604Syokota	    if (rodent.baudrate <= 0) {
68831604Syokota	        warnx("invalid argument `%s'", optarg);
68931604Syokota	        usage();
69031604Syokota	    }
69118222Speter	    debug("rodent baudrate %d", rodent.baudrate);
69218222Speter	    break;
69318222Speter
69416566Ssos	case 't':
69531949Syokota	    if (strcmp(optarg, "auto") == 0) {
69631949Syokota		rodent.rtype = MOUSE_PROTO_UNKNOWN;
69731949Syokota		rodent.flags &= ~NoPnP;
69831949Syokota		rodent.level = -1;
69931949Syokota		break;
70031949Syokota	    }
70116566Ssos	    for (i = 0; rnames[i]; i++)
70231949Syokota		if (strcmp(optarg, rnames[i]) == 0) {
70316566Ssos		    rodent.rtype = i;
70431949Syokota		    rodent.flags |= NoPnP;
70531949Syokota		    rodent.level = (i == MOUSE_PROTO_SYSMOUSE) ? 1 : 0;
70616566Ssos		    break;
70716566Ssos		}
70816566Ssos	    if (rnames[i])
70916566Ssos		break;
71031604Syokota	    warnx("no such mouse type `%s'", optarg);
71116566Ssos	    usage();
71216566Ssos
71316566Ssos	case 'h':
71416566Ssos	case '?':
71516566Ssos	default:
71616566Ssos	    usage();
71716566Ssos	}
71816566Ssos
71959090Syokota    /* fix Z axis mapping */
72059090Syokota    for (i = 0; i < 4; ++i) {
72159090Syokota	if (rodent.zmap[i] > 0) {
72259090Syokota	    for (j = 0; j < MOUSE_MAXBUTTON; ++j) {
72359090Syokota		if (mstate[j] == &bstate[rodent.zmap[i] - 1])
72459090Syokota		    mstate[j] = &zstate[i];
72559090Syokota	    }
72659090Syokota	    rodent.zmap[i] = 1 << (rodent.zmap[i] - 1);
72759090Syokota	}
72859090Syokota    }
72959090Syokota
73031604Syokota    /* the default port name */
73131604Syokota    switch(rodent.rtype) {
73231604Syokota
73331604Syokota    case MOUSE_PROTO_INPORT:
73431604Syokota        /* INPORT and BUS are the same... */
73531604Syokota	rodent.rtype = MOUSE_PROTO_BUS;
736102413Scharnier	/* FALLTHROUGH */
73731604Syokota    case MOUSE_PROTO_BUS:
73816566Ssos	if (!rodent.portname)
73916566Ssos	    rodent.portname = "/dev/mse0";
74016566Ssos	break;
74131604Syokota
74231604Syokota    case MOUSE_PROTO_PS2:
74316566Ssos	if (!rodent.portname)
74416566Ssos	    rodent.portname = "/dev/psm0";
74516566Ssos	break;
74631604Syokota
74716566Ssos    default:
74816566Ssos	if (rodent.portname)
74916566Ssos	    break;
75029849Scharnier	warnx("no port name specified");
75116566Ssos	usage();
75216566Ssos    }
75316566Ssos
75431604Syokota    for (;;) {
75531604Syokota	if (setjmp(env) == 0) {
75631604Syokota	    signal(SIGHUP, hup);
75736991Sahasty	    signal(SIGINT , cleanup);
75836991Sahasty	    signal(SIGQUIT, cleanup);
75936991Sahasty	    signal(SIGTERM, cleanup);
76031604Syokota            if ((rodent.mfd = open(rodent.portname, O_RDWR | O_NONBLOCK, 0))
76131604Syokota		== -1)
76231604Syokota	        logerr(1, "unable to open %s", rodent.portname);
76331604Syokota            if (r_identify() == MOUSE_PROTO_UNKNOWN) {
76431604Syokota	        logwarnx("cannot determine mouse type on %s", rodent.portname);
76531604Syokota	        close(rodent.mfd);
76631604Syokota	        rodent.mfd = -1;
76731604Syokota            }
76831604Syokota
76931604Syokota	    /* print some information */
77031604Syokota            if (identify != ID_NONE) {
77131604Syokota		if (identify == ID_ALL)
77231604Syokota                    printf("%s %s %s %s\n",
77331604Syokota		        rodent.portname, r_if(rodent.hw.iftype),
77431604Syokota		        r_name(rodent.rtype), r_model(rodent.hw.model));
77531604Syokota		else if (identify & ID_PORT)
77631604Syokota		    printf("%s\n", rodent.portname);
77731604Syokota		else if (identify & ID_IF)
77831604Syokota		    printf("%s\n", r_if(rodent.hw.iftype));
77931604Syokota		else if (identify & ID_TYPE)
78031604Syokota		    printf("%s\n", r_name(rodent.rtype));
78131604Syokota		else if (identify & ID_MODEL)
78231604Syokota		    printf("%s\n", r_model(rodent.hw.model));
78331604Syokota		exit(0);
78431604Syokota	    } else {
78531604Syokota                debug("port: %s  interface: %s  type: %s  model: %s",
78631604Syokota		    rodent.portname, r_if(rodent.hw.iftype),
78731604Syokota		    r_name(rodent.rtype), r_model(rodent.hw.model));
78831604Syokota	    }
78931604Syokota
79031604Syokota	    if (rodent.mfd == -1) {
79131604Syokota	        /*
79231604Syokota	         * We cannot continue because of error.  Exit if the
79331604Syokota		 * program has not become a daemon.  Otherwise, block
79431604Syokota		 * until the the user corrects the problem and issues SIGHUP.
79531604Syokota	         */
79631604Syokota	        if (!background)
79731604Syokota		    exit(1);
79831604Syokota	        sigpause(0);
79931604Syokota	    }
80031604Syokota
80131604Syokota            r_init();			/* call init function */
80231604Syokota	    moused();
80331604Syokota	}
80431604Syokota
80531604Syokota	if (rodent.mfd != -1)
80631604Syokota	    close(rodent.mfd);
80731604Syokota	if (rodent.cfd != -1)
80831604Syokota	    close(rodent.cfd);
80931604Syokota	rodent.mfd = rodent.cfd = -1;
81016566Ssos    }
81131604Syokota    /* NOT REACHED */
81216566Ssos
81331604Syokota    exit(0);
81431604Syokota}
81516566Ssos
81631604Syokotastatic void
81731604Syokotamoused(void)
81831604Syokota{
81931604Syokota    struct mouse_info mouse;
82058344Syokota    mousestatus_t action0;		/* original mouse action */
82158344Syokota    mousestatus_t action;		/* interrim buffer */
82231604Syokota    mousestatus_t action2;		/* mapped action */
82358344Syokota    struct timeval timeout;
82431604Syokota    fd_set fds;
82531604Syokota    u_char b;
82634152Sjkh    FILE *fp;
82758344Syokota    int flags;
82858344Syokota    int c;
82958344Syokota    int i;
83031604Syokota
83131604Syokota    if ((rodent.cfd = open("/dev/consolectl", O_RDWR, 0)) == -1)
83295629Siedowse	logerr(1, "cannot open /dev/consolectl");
83331604Syokota
83495629Siedowse    if (!nodaemon && !background) {
83531604Syokota	if (daemon(0, 0)) {
83695629Siedowse	    logerr(1, "failed to become a daemon");
83731604Syokota	} else {
83831604Syokota	    background = TRUE;
83934152Sjkh	    fp = fopen(pidfile, "w");
84034152Sjkh	    if (fp != NULL) {
84134152Sjkh		fprintf(fp, "%d\n", getpid());
84234152Sjkh		fclose(fp);
84334152Sjkh	    }
84416566Ssos	}
84595629Siedowse    }
84616566Ssos
84731604Syokota    /* clear mouse data */
84858344Syokota    bzero(&action0, sizeof(action0));
84931604Syokota    bzero(&action, sizeof(action));
85031604Syokota    bzero(&action2, sizeof(action2));
85131604Syokota    bzero(&mouse, sizeof(mouse));
85258344Syokota    mouse_button_state = S0;
85358344Syokota    gettimeofday(&mouse_button_state_tv, NULL);
85479430Siedowse    mouse_move_delayed = 0;
85558344Syokota    for (i = 0; i < MOUSE_MAXBUTTON; ++i) {
85658344Syokota	bstate[i].count = 0;
85758344Syokota	bstate[i].tv = mouse_button_state_tv;
85858344Syokota    }
85958344Syokota    for (i = 0; i < sizeof(zstate)/sizeof(zstate[0]); ++i) {
86058344Syokota	zstate[i].count = 0;
86158344Syokota	zstate[i].tv = mouse_button_state_tv;
86258344Syokota    }
86331604Syokota
86431604Syokota    /* choose which ioctl command to use */
86531604Syokota    mouse.operation = MOUSE_MOTION_EVENT;
86631604Syokota    extioctl = (ioctl(rodent.cfd, CONS_MOUSECTL, &mouse) == 0);
86731604Syokota
86831604Syokota    /* process mouse data */
86958344Syokota    timeout.tv_sec = 0;
87058344Syokota    timeout.tv_usec = 20000;		/* 20 msec */
87131604Syokota    for (;;) {
87231604Syokota
87321885Ssos	FD_ZERO(&fds);
87431604Syokota	FD_SET(rodent.mfd, &fds);
87558344Syokota	if (rodent.mremsfd >= 0)
87658344Syokota	    FD_SET(rodent.mremsfd, &fds);
87758344Syokota	if (rodent.mremcfd >= 0)
87858344Syokota	    FD_SET(rodent.mremcfd, &fds);
87936991Sahasty
88058344Syokota	c = select(FD_SETSIZE, &fds, NULL, NULL,
88158344Syokota		   (rodent.flags & Emulate3Button) ? &timeout : NULL);
88258344Syokota	if (c < 0) {                    /* error */
88395629Siedowse	    logwarn("failed to read from mouse");
88436991Sahasty	    continue;
88558344Syokota	} else if (c == 0) {            /* timeout */
88658344Syokota	    /* assert(rodent.flags & Emulate3Button) */
88758344Syokota	    action0.button = action0.obutton;
88858344Syokota	    action0.dx = action0.dy = action0.dz = 0;
88958344Syokota	    action0.flags = flags = 0;
89058344Syokota	    if (r_timeout() && r_statetrans(&action0, &action, A_TIMEOUT)) {
89158344Syokota		if (debug > 2)
89258344Syokota		    debug("flags:%08x buttons:%08x obuttons:%08x",
89358344Syokota			  action.flags, action.button, action.obutton);
89458344Syokota	    } else {
89558344Syokota		action0.obutton = action0.button;
89658344Syokota		continue;
89758344Syokota	    }
89858344Syokota	} else {
89958344Syokota	    /*  MouseRemote client connect/disconnect  */
90058344Syokota	    if ((rodent.mremsfd >= 0) && FD_ISSET(rodent.mremsfd, &fds)) {
90158344Syokota		mremote_clientchg(TRUE);
90258344Syokota		continue;
90358344Syokota	    }
90458344Syokota	    if ((rodent.mremcfd >= 0) && FD_ISSET(rodent.mremcfd, &fds)) {
90558344Syokota		mremote_clientchg(FALSE);
90658344Syokota		continue;
90758344Syokota	    }
90858344Syokota	    /* mouse movement */
90958344Syokota	    if (read(rodent.mfd, &b, 1) == -1) {
91058344Syokota		if (errno == EWOULDBLOCK)
91158344Syokota		    continue;
91258344Syokota		else
91358344Syokota		    return;
91458344Syokota	    }
91558344Syokota	    if ((flags = r_protocol(b, &action0)) == 0)
91658344Syokota		continue;
91758344Syokota	    r_timestamp(&action0);
91858344Syokota	    r_statetrans(&action0, &action,
91958344Syokota	    		 A(action0.button & MOUSE_BUTTON1DOWN,
92058344Syokota	    		   action0.button & MOUSE_BUTTON3DOWN));
92158344Syokota	    debug("flags:%08x buttons:%08x obuttons:%08x", action.flags,
92258344Syokota		  action.button, action.obutton);
92336991Sahasty	}
92458344Syokota	action0.obutton = action0.button;
92558344Syokota	flags &= MOUSE_POSCHANGED;
92658344Syokota	flags |= action.obutton ^ action.button;
92758344Syokota	action.flags = flags;
92836991Sahasty
92958344Syokota	if (flags) {			/* handler detected action */
93031604Syokota	    r_map(&action, &action2);
93131604Syokota	    debug("activity : buttons 0x%08x  dx %d  dy %d  dz %d",
93231604Syokota		action2.button, action2.dx, action2.dy, action2.dz);
93331604Syokota
93431604Syokota	    if (extioctl) {
93531604Syokota	        r_click(&action2);
93631604Syokota	        if (action2.flags & MOUSE_POSCHANGED) {
93731604Syokota    		    mouse.operation = MOUSE_MOTION_EVENT;
93831604Syokota	            mouse.u.data.buttons = action2.button;
93978770Sgreid	            mouse.u.data.x = action2.dx * rodent.accelx;
94078770Sgreid	            mouse.u.data.y = action2.dy * rodent.accely;
94131604Syokota	            mouse.u.data.z = action2.dz;
94231604Syokota		    if (debug < 2)
94331604Syokota	                ioctl(rodent.cfd, CONS_MOUSECTL, &mouse);
94431604Syokota	        }
94531604Syokota	    } else {
94631604Syokota	        mouse.operation = MOUSE_ACTION;
94731604Syokota	        mouse.u.data.buttons = action2.button;
94878770Sgreid	        mouse.u.data.x = action2.dx * rodent.accelx;
94978770Sgreid	        mouse.u.data.y = action2.dy * rodent.accely;
95031604Syokota	        mouse.u.data.z = action2.dz;
95131604Syokota		if (debug < 2)
95231604Syokota	            ioctl(rodent.cfd, CONS_MOUSECTL, &mouse);
95331604Syokota	    }
95431604Syokota
95531604Syokota            /*
95631604Syokota	     * If the Z axis movement is mapped to a imaginary physical
95731604Syokota	     * button, we need to cook up a corresponding button `up' event
95831604Syokota	     * after sending a button `down' event.
95931604Syokota	     */
96058231Syokota            if ((rodent.zmap[0] > 0) && (action.dz != 0)) {
96131604Syokota		action.obutton = action.button;
96231604Syokota		action.dx = action.dy = action.dz = 0;
96331604Syokota	        r_map(&action, &action2);
96431604Syokota	        debug("activity : buttons 0x%08x  dx %d  dy %d  dz %d",
96531604Syokota		    action2.button, action2.dx, action2.dy, action2.dz);
96631604Syokota
96731604Syokota	        if (extioctl) {
96831604Syokota	            r_click(&action2);
96931604Syokota	        } else {
97031604Syokota	            mouse.operation = MOUSE_ACTION;
97131604Syokota	            mouse.u.data.buttons = action2.button;
97231604Syokota		    mouse.u.data.x = mouse.u.data.y = mouse.u.data.z = 0;
97331604Syokota		    if (debug < 2)
97431604Syokota	                ioctl(rodent.cfd, CONS_MOUSECTL, &mouse);
97531604Syokota	        }
97631604Syokota	    }
97716566Ssos	}
97816566Ssos    }
97931604Syokota    /* NOT REACHED */
98016566Ssos}
98116566Ssos
98231604Syokotastatic void
98331604Syokotahup(int sig)
98431604Syokota{
98531604Syokota    longjmp(env, 1);
98631604Syokota}
98716566Ssos
98836991Sahastystatic void
98936991Sahastycleanup(int sig)
99036991Sahasty{
99136991Sahasty    if (rodent.rtype == MOUSE_PROTO_X10MOUSEREM)
99236991Sahasty	unlink(_PATH_MOUSEREMOTE);
99336991Sahasty    exit(0);
99436991Sahasty}
99536991Sahasty
99616566Ssos/**
99716566Ssos ** usage
99816566Ssos **
99916566Ssos ** Complain, and free the CPU for more worthy tasks
100016566Ssos **/
100131604Syokotastatic void
100216566Ssosusage(void)
100316566Ssos{
100481364Sdougb    fprintf(stderr, "%s\n%s\n%s\n%s\n",
100558344Syokota	"usage: moused [-DRcdfs] [-I file] [-F rate] [-r resolution] [-S baudrate]",
100678770Sgreid	"              [-a X [,Y]] [-C threshold] [-m N=M] [-w N] [-z N]",
100778770Sgreid	"              [-t <mousetype>] [-3 [-E timeout]] -p <port>",
100881364Sdougb	"       moused [-d] -i <port|if|type|model|all> -p <port>");
100916566Ssos    exit(1);
101016566Ssos}
101116566Ssos
101295629Siedowse/*
101395629Siedowse * Output an error message to syslog or stderr as appropriate. If
101495629Siedowse * `errnum' is non-zero, append its string form to the message.
101595629Siedowse */
101695629Siedowsestatic void
101795629Siedowselog_or_warn(int log_pri, int errnum, const char *fmt, ...)
101895629Siedowse{
101995629Siedowse	va_list ap;
102095629Siedowse	char buf[256];
102195629Siedowse
102295629Siedowse	va_start(ap, fmt);
102395629Siedowse	vsnprintf(buf, sizeof(buf), fmt, ap);
102495629Siedowse	va_end(ap);
102595629Siedowse	if (errnum) {
102695629Siedowse		strlcat(buf, ": ", sizeof(buf));
102795629Siedowse		strlcat(buf, strerror(errnum), sizeof(buf));
102895629Siedowse	}
102995629Siedowse
103095629Siedowse	if (background)
103195629Siedowse		syslog(log_pri, "%s", buf);
103295629Siedowse	else
103395629Siedowse		warnx("%s", buf);
103495629Siedowse}
103595629Siedowse
103616566Ssos/**
103716566Ssos ** Mouse interface code, courtesy of XFree86 3.1.2.
103816566Ssos **
103916566Ssos ** Note: Various bits have been trimmed, and in my shortsighted enthusiasm
104016566Ssos ** to clean, reformat and rationalise naming, it's quite possible that
104116566Ssos ** some things in here have been broken.
104216566Ssos **
104316566Ssos ** I hope not 8)
104416566Ssos **
104516566Ssos ** The following code is derived from a module marked :
104616566Ssos **/
104716566Ssos
104816566Ssos/* $XConsortium: xf86_Mouse.c,v 1.2 94/10/12 20:33:21 kaleb Exp $ */
104916566Ssos/* $XFree86: xc/programs/Xserver/hw/xfree86/common/xf86_Mouse.c,v 3.2 1995/01/28
105016566Ssos 17:03:40 dawes Exp $ */
105116566Ssos/*
105216566Ssos *
105316566Ssos * Copyright 1990,91 by Thomas Roell, Dinkelscherben, Germany.
105416566Ssos * Copyright 1993 by David Dawes <dawes@physics.su.oz.au>
105516566Ssos *
105616566Ssos * Permission to use, copy, modify, distribute, and sell this software and its
105716566Ssos * documentation for any purpose is hereby granted without fee, provided that
105816566Ssos * the above copyright notice appear in all copies and that both that
105916566Ssos * copyright notice and this permission notice appear in supporting
106016566Ssos * documentation, and that the names of Thomas Roell and David Dawes not be
106116566Ssos * used in advertising or publicity pertaining to distribution of the
106216566Ssos * software without specific, written prior permission.  Thomas Roell
106316566Ssos * and David Dawes makes no representations about the suitability of this
106416566Ssos * software for any purpose.  It is provided "as is" without express or
106516566Ssos * implied warranty.
106616566Ssos *
106716566Ssos * THOMAS ROELL AND DAVID DAWES DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
106816566Ssos * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
106916566Ssos * FITNESS, IN NO EVENT SHALL THOMAS ROELL OR DAVID DAWES BE LIABLE FOR ANY
107016566Ssos * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
107116566Ssos * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
107216566Ssos * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
107316566Ssos * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
107416566Ssos *
107516566Ssos */
107616566Ssos
107731604Syokota/**
107831604Syokota ** GlidePoint support from XFree86 3.2.
107931604Syokota ** Derived from the module:
108031604Syokota **/
108116566Ssos
108231604Syokota/* $XFree86: xc/programs/Xserver/hw/xfree86/common/xf86_Mouse.c,v 3.19 1996/10/16 14:40:51 dawes Exp $ */
108331604Syokota/* $XConsortium: xf86_Mouse.c /main/10 1996/01/30 15:16:12 kaleb $ */
108431604Syokota
108531604Syokota/* the following table must be ordered by MOUSE_PROTO_XXX in mouse.h */
108631604Syokotastatic unsigned char proto[][7] = {
108731604Syokota    /*  hd_mask hd_id   dp_mask dp_id   bytes b4_mask b4_id */
108831604Syokota    { 	0x40,	0x40,	0x40,	0x00,	3,   ~0x23,  0x00 }, /* MicroSoft */
108931604Syokota    {	0xf8,	0x80,	0x00,	0x00,	5,    0x00,  0xff }, /* MouseSystems */
109031604Syokota    {	0xe0,	0x80,	0x80,	0x00,	3,    0x00,  0xff }, /* Logitech */
109131604Syokota    {	0xe0,	0x80,	0x80,	0x00,	3,    0x00,  0xff }, /* MMSeries */
109234074Syokota    { 	0x40,	0x40,	0x40,	0x00,	3,   ~0x33,  0x00 }, /* MouseMan */
109331604Syokota    {	0xf8,	0x80,	0x00,	0x00,	5,    0x00,  0xff }, /* Bus */
109431604Syokota    {	0xf8,	0x80,	0x00,	0x00,	5,    0x00,  0xff }, /* InPort */
109531604Syokota    {	0xc0,	0x00,	0x00,	0x00,	3,    0x00,  0xff }, /* PS/2 mouse */
109631604Syokota    {	0xe0,	0x80,	0x80,	0x00,	3,    0x00,  0xff }, /* MM HitTablet */
109731604Syokota    { 	0x40,	0x40,	0x40,	0x00,	3,   ~0x33,  0x00 }, /* GlidePoint */
109831604Syokota    { 	0x40,	0x40,	0x40,	0x00,	3,   ~0x3f,  0x00 }, /* IntelliMouse */
109931604Syokota    { 	0x40,	0x40,	0x40,	0x00,	3,   ~0x33,  0x00 }, /* ThinkingMouse */
110031604Syokota    {	0xf8,	0x80,	0x00,	0x00,	5,    0x00,  0xff }, /* sysmouse */
110136991Sahasty    { 	0x40,	0x40,	0x40,	0x00,	3,   ~0x23,  0x00 }, /* X10 MouseRem */
110241271Syokota    {	0x80,	0x80,	0x00,	0x00,	5,    0x00,  0xff }, /* KIDSPAD */
110349967Syokota    {	0xc3,	0xc0,	0x00,	0x00,	6,    0x00,  0xff }, /* VersaPad */
110493071Swill    {	0x00,	0x00,	0x00,	0x00,	1,    0x00,  0xff }, /* JogDial */
110531604Syokota#if notyet
110631604Syokota    {	0xf8,	0x80,	0x00,	0x00,	5,   ~0x2f,  0x10 }, /* Mariqua */
110731604Syokota#endif
110831604Syokota};
110931604Syokotastatic unsigned char cur_proto[7];
111031604Syokota
111131604Syokotastatic int
111231604Syokotar_identify(void)
111331604Syokota{
111431604Syokota    char pnpbuf[256];	/* PnP identifier string may be up to 256 bytes long */
111531604Syokota    pnpid_t pnpid;
111631604Syokota    symtab_t *t;
111731604Syokota    int level;
111831604Syokota    int len;
111931604Syokota
112031604Syokota    /* set the driver operation level, if applicable */
112131604Syokota    if (rodent.level < 0)
112231604Syokota	rodent.level = 1;
112331604Syokota    ioctl(rodent.mfd, MOUSE_SETLEVEL, &rodent.level);
112431604Syokota    rodent.level = (ioctl(rodent.mfd, MOUSE_GETLEVEL, &level) == 0) ? level : 0;
112531604Syokota
112631604Syokota    /*
112731604Syokota     * Interrogate the driver and get some intelligence on the device...
112831604Syokota     * The following ioctl functions are not always supported by device
112931604Syokota     * drivers.  When the driver doesn't support them, we just trust the
113031604Syokota     * user to supply valid information.
113131604Syokota     */
113231604Syokota    rodent.hw.iftype = MOUSE_IF_UNKNOWN;
113331604Syokota    rodent.hw.model = MOUSE_MODEL_GENERIC;
113431604Syokota    ioctl(rodent.mfd, MOUSE_GETHWINFO, &rodent.hw);
113531604Syokota
113631604Syokota    if (rodent.rtype != MOUSE_PROTO_UNKNOWN)
113731604Syokota        bcopy(proto[rodent.rtype], cur_proto, sizeof(cur_proto));
113831604Syokota    rodent.mode.protocol = MOUSE_PROTO_UNKNOWN;
113931604Syokota    rodent.mode.rate = -1;
114031604Syokota    rodent.mode.resolution = MOUSE_RES_UNKNOWN;
114131604Syokota    rodent.mode.accelfactor = 0;
114231604Syokota    rodent.mode.level = 0;
114331604Syokota    if (ioctl(rodent.mfd, MOUSE_GETMODE, &rodent.mode) == 0) {
114431604Syokota        if ((rodent.mode.protocol == MOUSE_PROTO_UNKNOWN)
114531604Syokota	    || (rodent.mode.protocol >= sizeof(proto)/sizeof(proto[0]))) {
114631604Syokota	    logwarnx("unknown mouse protocol (%d)", rodent.mode.protocol);
114731604Syokota	    return MOUSE_PROTO_UNKNOWN;
114831604Syokota        } else {
114931604Syokota	    /* INPORT and BUS are the same... */
115031604Syokota	    if (rodent.mode.protocol == MOUSE_PROTO_INPORT)
115131604Syokota	        rodent.mode.protocol = MOUSE_PROTO_BUS;
115231604Syokota	    if (rodent.mode.protocol != rodent.rtype) {
115331604Syokota		/* Hmm, the driver doesn't agree with the user... */
115431604Syokota                if (rodent.rtype != MOUSE_PROTO_UNKNOWN)
115531604Syokota	            logwarnx("mouse type mismatch (%s != %s), %s is assumed",
115631604Syokota		        r_name(rodent.mode.protocol), r_name(rodent.rtype),
115731604Syokota		        r_name(rodent.mode.protocol));
115831604Syokota	        rodent.rtype = rodent.mode.protocol;
115931604Syokota                bcopy(proto[rodent.rtype], cur_proto, sizeof(cur_proto));
116031604Syokota	    }
116131604Syokota        }
116231604Syokota        cur_proto[4] = rodent.mode.packetsize;
116331604Syokota        cur_proto[0] = rodent.mode.syncmask[0];	/* header byte bit mask */
116431604Syokota        cur_proto[1] = rodent.mode.syncmask[1];	/* header bit pattern */
116531604Syokota    }
116631604Syokota
1167108470Sschweikh    /* maybe this is a PnP mouse... */
116831604Syokota    if (rodent.mode.protocol == MOUSE_PROTO_UNKNOWN) {
116931604Syokota
117031604Syokota        if (rodent.flags & NoPnP)
117131604Syokota            return rodent.rtype;
117231604Syokota	if (((len = pnpgets(pnpbuf)) <= 0) || !pnpparse(&pnpid, pnpbuf, len))
117331604Syokota            return rodent.rtype;
117431604Syokota
117531604Syokota        debug("PnP serial mouse: '%*.*s' '%*.*s' '%*.*s'",
117631604Syokota	    pnpid.neisaid, pnpid.neisaid, pnpid.eisaid,
117731604Syokota	    pnpid.ncompat, pnpid.ncompat, pnpid.compat,
117831604Syokota	    pnpid.ndescription, pnpid.ndescription, pnpid.description);
117931604Syokota
118031604Syokota	/* we have a valid PnP serial device ID */
118131604Syokota        rodent.hw.iftype = MOUSE_IF_SERIAL;
118231604Syokota	t = pnpproto(&pnpid);
118331604Syokota	if (t != NULL) {
118431604Syokota            rodent.mode.protocol = t->val;
118531604Syokota            rodent.hw.model = t->val2;
118631604Syokota	} else {
118731604Syokota            rodent.mode.protocol = MOUSE_PROTO_UNKNOWN;
118831604Syokota	}
118931604Syokota	if (rodent.mode.protocol == MOUSE_PROTO_INPORT)
119031604Syokota	    rodent.mode.protocol = MOUSE_PROTO_BUS;
119131604Syokota
119231604Syokota        /* make final adjustment */
119331604Syokota	if (rodent.mode.protocol != MOUSE_PROTO_UNKNOWN) {
119431604Syokota	    if (rodent.mode.protocol != rodent.rtype) {
119531604Syokota		/* Hmm, the device doesn't agree with the user... */
119631604Syokota                if (rodent.rtype != MOUSE_PROTO_UNKNOWN)
119731604Syokota	            logwarnx("mouse type mismatch (%s != %s), %s is assumed",
119831604Syokota		        r_name(rodent.mode.protocol), r_name(rodent.rtype),
119931604Syokota		        r_name(rodent.mode.protocol));
120031604Syokota	        rodent.rtype = rodent.mode.protocol;
120131604Syokota                bcopy(proto[rodent.rtype], cur_proto, sizeof(cur_proto));
120231604Syokota	    }
120331604Syokota	}
120431604Syokota    }
120531604Syokota
120631604Syokota    debug("proto params: %02x %02x %02x %02x %d %02x %02x",
120731604Syokota	cur_proto[0], cur_proto[1], cur_proto[2], cur_proto[3],
120831604Syokota	cur_proto[4], cur_proto[5], cur_proto[6]);
120931604Syokota
121031604Syokota    return rodent.rtype;
121131604Syokota}
121231604Syokota
121331604Syokotastatic char *
121431604Syokotar_if(int iftype)
121531604Syokota{
121631604Syokota    char *s;
121731604Syokota
121831604Syokota    s = gettokenname(rifs, iftype);
121931604Syokota    return (s == NULL) ? "unknown" : s;
122031604Syokota}
122131604Syokota
122231604Syokotastatic char *
122331604Syokotar_name(int type)
122431604Syokota{
122531604Syokota    return ((type == MOUSE_PROTO_UNKNOWN)
122631604Syokota	|| (type > sizeof(rnames)/sizeof(rnames[0]) - 1))
122731604Syokota	? "unknown" : rnames[type];
122831604Syokota}
122931604Syokota
123031604Syokotastatic char *
123131604Syokotar_model(int model)
123231604Syokota{
123331604Syokota    char *s;
123431604Syokota
123531604Syokota    s = gettokenname(rmodels, model);
123631604Syokota    return (s == NULL) ? "unknown" : s;
123731604Syokota}
123831604Syokota
123931604Syokotastatic void
124016566Ssosr_init(void)
124116566Ssos{
124249967Syokota    unsigned char buf[16];	/* scrach buffer */
124331604Syokota    fd_set fds;
124431604Syokota    char *s;
124531604Syokota    char c;
124631604Syokota    int i;
124731604Syokota
124816566Ssos    /**
124916566Ssos     ** This comment is a little out of context here, but it contains
125016566Ssos     ** some useful information...
125116566Ssos     ********************************************************************
125216566Ssos     **
125316566Ssos     ** The following lines take care of the Logitech MouseMan protocols.
125416566Ssos     **
125516566Ssos     ** NOTE: There are different versions of both MouseMan and TrackMan!
125616566Ssos     **       Hence I add another protocol P_LOGIMAN, which the user can
125716566Ssos     **       specify as MouseMan in his XF86Config file. This entry was
125816566Ssos     **       formerly handled as a special case of P_MS. However, people
125916566Ssos     **       who don't have the middle button problem, can still specify
126016566Ssos     **       Microsoft and use P_MS.
126116566Ssos     **
126216566Ssos     ** By default, these mice should use a 3 byte Microsoft protocol
126316566Ssos     ** plus a 4th byte for the middle button. However, the mouse might
126416566Ssos     ** have switched to a different protocol before we use it, so I send
126516566Ssos     ** the proper sequence just in case.
126616566Ssos     **
126716566Ssos     ** NOTE: - all commands to (at least the European) MouseMan have to
126816566Ssos     **         be sent at 1200 Baud.
126916566Ssos     **       - each command starts with a '*'.
127016566Ssos     **       - whenever the MouseMan receives a '*', it will switch back
127116566Ssos     **	 to 1200 Baud. Hence I have to select the desired protocol
127216566Ssos     **	 first, then select the baud rate.
127316566Ssos     **
127416566Ssos     ** The protocols supported by the (European) MouseMan are:
127516566Ssos     **   -  5 byte packed binary protocol, as with the Mouse Systems
127616566Ssos     **      mouse. Selected by sequence "*U".
127716566Ssos     **   -  2 button 3 byte MicroSoft compatible protocol. Selected
127816566Ssos     **      by sequence "*V".
127916566Ssos     **   -  3 button 3+1 byte MicroSoft compatible protocol (default).
128016566Ssos     **      Selected by sequence "*X".
128116566Ssos     **
128216566Ssos     ** The following baud rates are supported:
128316566Ssos     **   -  1200 Baud (default). Selected by sequence "*n".
128416566Ssos     **   -  9600 Baud. Selected by sequence "*q".
128516566Ssos     **
128616566Ssos     ** Selecting a sample rate is no longer supported with the MouseMan!
128716566Ssos     ** Some additional lines in xf86Config.c take care of ill configured
128816566Ssos     ** baud rates and sample rates. (The user will get an error.)
128916566Ssos     */
129016566Ssos
129131604Syokota    switch (rodent.rtype) {
129231604Syokota
129331604Syokota    case MOUSE_PROTO_LOGI:
129431604Syokota	/*
129531604Syokota	 * The baud rate selection command must be sent at the current
129631604Syokota	 * baud rate; try all likely settings
129731604Syokota	 */
129831604Syokota	setmousespeed(9600, rodent.baudrate, rodentcflags[rodent.rtype]);
129931604Syokota	setmousespeed(4800, rodent.baudrate, rodentcflags[rodent.rtype]);
130031604Syokota	setmousespeed(2400, rodent.baudrate, rodentcflags[rodent.rtype]);
130131604Syokota	setmousespeed(1200, rodent.baudrate, rodentcflags[rodent.rtype]);
130231604Syokota	/* select MM series data format */
130331604Syokota	write(rodent.mfd, "S", 1);
130431604Syokota	setmousespeed(rodent.baudrate, rodent.baudrate,
130531604Syokota		      rodentcflags[MOUSE_PROTO_MM]);
130631604Syokota	/* select report rate/frequency */
130731604Syokota	if      (rodent.rate <= 0)   write(rodent.mfd, "O", 1);
130831604Syokota	else if (rodent.rate <= 15)  write(rodent.mfd, "J", 1);
130931604Syokota	else if (rodent.rate <= 27)  write(rodent.mfd, "K", 1);
131031604Syokota	else if (rodent.rate <= 42)  write(rodent.mfd, "L", 1);
131131604Syokota	else if (rodent.rate <= 60)  write(rodent.mfd, "R", 1);
131231604Syokota	else if (rodent.rate <= 85)  write(rodent.mfd, "M", 1);
131331604Syokota	else if (rodent.rate <= 125) write(rodent.mfd, "Q", 1);
131431604Syokota	else			     write(rodent.mfd, "N", 1);
131531604Syokota	break;
131631604Syokota
131731604Syokota    case MOUSE_PROTO_LOGIMOUSEMAN:
131831604Syokota	/* The command must always be sent at 1200 baud */
131931604Syokota	setmousespeed(1200, 1200, rodentcflags[rodent.rtype]);
132016566Ssos	write(rodent.mfd, "*X", 2);
132131604Syokota	setmousespeed(1200, rodent.baudrate, rodentcflags[rodent.rtype]);
132231604Syokota	break;
132318222Speter
132431604Syokota    case MOUSE_PROTO_HITTAB:
132531604Syokota	setmousespeed(1200, rodent.baudrate, rodentcflags[rodent.rtype]);
132618222Speter
132731604Syokota	/*
132831604Syokota	 * Initialize Hitachi PUMA Plus - Model 1212E to desired settings.
132931604Syokota	 * The tablet must be configured to be in MM mode, NO parity,
133031604Syokota	 * Binary Format.  xf86Info.sampleRate controls the sensativity
133131604Syokota	 * of the tablet.  We only use this tablet for it's 4-button puck
133231604Syokota	 * so we don't run in "Absolute Mode"
133331604Syokota	 */
133431604Syokota	write(rodent.mfd, "z8", 2);	/* Set Parity = "NONE" */
133531604Syokota	usleep(50000);
133631604Syokota	write(rodent.mfd, "zb", 2);	/* Set Format = "Binary" */
133731604Syokota	usleep(50000);
133831604Syokota	write(rodent.mfd, "@", 1);	/* Set Report Mode = "Stream" */
133931604Syokota	usleep(50000);
134031604Syokota	write(rodent.mfd, "R", 1);	/* Set Output Rate = "45 rps" */
134131604Syokota	usleep(50000);
134231604Syokota	write(rodent.mfd, "I\x20", 2);	/* Set Incrememtal Mode "20" */
134331604Syokota	usleep(50000);
134431604Syokota	write(rodent.mfd, "E", 1);	/* Set Data Type = "Relative */
134531604Syokota	usleep(50000);
134618222Speter
134731604Syokota	/* Resolution is in 'lines per inch' on the Hitachi tablet */
134831604Syokota	if      (rodent.resolution == MOUSE_RES_LOW) 		c = 'g';
134931604Syokota	else if (rodent.resolution == MOUSE_RES_MEDIUMLOW)	c = 'e';
135031604Syokota	else if (rodent.resolution == MOUSE_RES_MEDIUMHIGH)	c = 'h';
135131604Syokota	else if (rodent.resolution == MOUSE_RES_HIGH)		c = 'd';
135231604Syokota	else if (rodent.resolution <=   40) 			c = 'g';
135331604Syokota	else if (rodent.resolution <=  100) 			c = 'd';
135431604Syokota	else if (rodent.resolution <=  200) 			c = 'e';
135531604Syokota	else if (rodent.resolution <=  500) 			c = 'h';
135631604Syokota	else if (rodent.resolution <= 1000) 			c = 'j';
135731604Syokota	else                                			c = 'd';
135831604Syokota	write(rodent.mfd, &c, 1);
135931604Syokota	usleep(50000);
136018222Speter
136131604Syokota	write(rodent.mfd, "\021", 1);	/* Resume DATA output */
136231604Syokota	break;
136331604Syokota
136431604Syokota    case MOUSE_PROTO_THINK:
136531604Syokota	setmousespeed(1200, rodent.baudrate, rodentcflags[rodent.rtype]);
136631604Syokota	/* the PnP ID string may be sent again, discard it */
136731604Syokota	usleep(200000);
136831604Syokota	i = FREAD;
136931604Syokota	ioctl(rodent.mfd, TIOCFLUSH, &i);
137031604Syokota	/* send the command to initialize the beast */
137131604Syokota	for (s = "E5E5"; *s; ++s) {
137231604Syokota	    write(rodent.mfd, s, 1);
137331604Syokota	    FD_ZERO(&fds);
137431604Syokota	    FD_SET(rodent.mfd, &fds);
137531604Syokota	    if (select(FD_SETSIZE, &fds, NULL, NULL, NULL) <= 0)
137631604Syokota		break;
137731604Syokota	    read(rodent.mfd, &c, 1);
137831604Syokota	    debug("%c", c);
137931604Syokota	    if (c != *s)
138031604Syokota	        break;
138118222Speter	}
138231604Syokota	break;
138331604Syokota
138493071Swill    case MOUSE_PROTO_JOGDIAL:
138593071Swill	break;
138631604Syokota    case MOUSE_PROTO_MSC:
138731604Syokota	setmousespeed(1200, rodent.baudrate, rodentcflags[rodent.rtype]);
138831604Syokota	if (rodent.flags & ClearDTR) {
138931604Syokota	   i = TIOCM_DTR;
139031604Syokota	   ioctl(rodent.mfd, TIOCMBIC, &i);
139131604Syokota        }
139231604Syokota        if (rodent.flags & ClearRTS) {
139331604Syokota	   i = TIOCM_RTS;
139431604Syokota	   ioctl(rodent.mfd, TIOCMBIC, &i);
139531604Syokota        }
139631604Syokota	break;
139731604Syokota
139831726Syokota    case MOUSE_PROTO_SYSMOUSE:
139931726Syokota	if (rodent.hw.iftype == MOUSE_IF_SYSMOUSE)
140031726Syokota	    setmousespeed(1200, rodent.baudrate, rodentcflags[rodent.rtype]);
1401102413Scharnier	/* FALLTHROUGH */
140231726Syokota
140331604Syokota    case MOUSE_PROTO_BUS:
140431604Syokota    case MOUSE_PROTO_INPORT:
140531604Syokota    case MOUSE_PROTO_PS2:
140631604Syokota	if (rodent.rate >= 0)
140731604Syokota	    rodent.mode.rate = rodent.rate;
140831604Syokota	if (rodent.resolution != MOUSE_RES_UNKNOWN)
140931604Syokota	    rodent.mode.resolution = rodent.resolution;
141031604Syokota	ioctl(rodent.mfd, MOUSE_SETMODE, &rodent.mode);
141131604Syokota	break;
141231604Syokota
141336991Sahasty    case MOUSE_PROTO_X10MOUSEREM:
141436991Sahasty	mremote_serversetup();
141536991Sahasty	setmousespeed(1200, rodent.baudrate, rodentcflags[rodent.rtype]);
141636991Sahasty	break;
141736991Sahasty
141836991Sahasty
141949967Syokota    case MOUSE_PROTO_VERSAPAD:
142049967Syokota	tcsendbreak(rodent.mfd, 0);	/* send break for 400 msec */
142149967Syokota	i = FREAD;
142249967Syokota	ioctl(rodent.mfd, TIOCFLUSH, &i);
142349967Syokota	for (i = 0; i < 7; ++i) {
142449967Syokota	    FD_ZERO(&fds);
142549967Syokota	    FD_SET(rodent.mfd, &fds);
142649967Syokota	    if (select(FD_SETSIZE, &fds, NULL, NULL, NULL) <= 0)
142749967Syokota		break;
142849967Syokota	    read(rodent.mfd, &c, 1);
142949967Syokota	    buf[i] = c;
143049967Syokota	}
143149967Syokota	debug("%s\n", buf);
143249967Syokota	if ((buf[0] != 'V') || (buf[1] != 'P')|| (buf[7] != '\r'))
143349967Syokota	    break;
143449967Syokota	setmousespeed(9600, rodent.baudrate, rodentcflags[rodent.rtype]);
143549967Syokota	tcsendbreak(rodent.mfd, 0);	/* send break for 400 msec again */
143649967Syokota	for (i = 0; i < 7; ++i) {
143749967Syokota	    FD_ZERO(&fds);
143849967Syokota	    FD_SET(rodent.mfd, &fds);
143949967Syokota	    if (select(FD_SETSIZE, &fds, NULL, NULL, NULL) <= 0)
144049967Syokota		break;
144149967Syokota	    read(rodent.mfd, &c, 1);
144249967Syokota	    debug("%c", c);
144349967Syokota	    if (c != buf[i])
144449967Syokota		break;
144549967Syokota	}
144649967Syokota	i = FREAD;
144749967Syokota	ioctl(rodent.mfd, TIOCFLUSH, &i);
144849967Syokota	break;
144949967Syokota
145031604Syokota    default:
145131604Syokota	setmousespeed(1200, rodent.baudrate, rodentcflags[rodent.rtype]);
145231604Syokota	break;
145316566Ssos    }
145416566Ssos}
145516566Ssos
145631604Syokotastatic int
145731604Syokotar_protocol(u_char rBuf, mousestatus_t *act)
145816566Ssos{
145931604Syokota    /* MOUSE_MSS_BUTTON?DOWN -> MOUSE_BUTTON?DOWN */
146031604Syokota    static int butmapmss[4] = {	/* Microsoft, MouseMan, GlidePoint,
146131604Syokota				   IntelliMouse, Thinking Mouse */
146231604Syokota	0,
146331604Syokota	MOUSE_BUTTON3DOWN,
146431604Syokota	MOUSE_BUTTON1DOWN,
146531604Syokota	MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
146631604Syokota    };
146731604Syokota    static int butmapmss2[4] = { /* Microsoft, MouseMan, GlidePoint,
146831604Syokota				    Thinking Mouse */
146931604Syokota	0,
147031604Syokota	MOUSE_BUTTON4DOWN,
147131604Syokota	MOUSE_BUTTON2DOWN,
147231604Syokota	MOUSE_BUTTON2DOWN | MOUSE_BUTTON4DOWN,
147331604Syokota    };
147431604Syokota    /* MOUSE_INTELLI_BUTTON?DOWN -> MOUSE_BUTTON?DOWN */
147531604Syokota    static int butmapintelli[4] = { /* IntelliMouse, NetMouse, Mie Mouse,
147631604Syokota				       MouseMan+ */
147731604Syokota	0,
147831604Syokota	MOUSE_BUTTON2DOWN,
147931604Syokota	MOUSE_BUTTON4DOWN,
148031604Syokota	MOUSE_BUTTON2DOWN | MOUSE_BUTTON4DOWN,
148131604Syokota    };
148231604Syokota    /* MOUSE_MSC_BUTTON?UP -> MOUSE_BUTTON?DOWN */
148331604Syokota    static int butmapmsc[8] = {	/* MouseSystems, MMSeries, Logitech,
148431604Syokota				   Bus, sysmouse */
148531604Syokota	0,
148631604Syokota	MOUSE_BUTTON3DOWN,
148731604Syokota	MOUSE_BUTTON2DOWN,
148831604Syokota	MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
148931604Syokota	MOUSE_BUTTON1DOWN,
149031604Syokota	MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
149131604Syokota	MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
149231604Syokota	MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
149331604Syokota    };
149431604Syokota    /* MOUSE_PS2_BUTTON?DOWN -> MOUSE_BUTTON?DOWN */
149531604Syokota    static int butmapps2[8] = {	/* PS/2 */
149631604Syokota	0,
149731604Syokota	MOUSE_BUTTON1DOWN,
149831604Syokota	MOUSE_BUTTON3DOWN,
149931604Syokota	MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
150031604Syokota	MOUSE_BUTTON2DOWN,
150131604Syokota	MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
150231604Syokota	MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
150331604Syokota	MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
150431604Syokota    };
150531604Syokota    /* for Hitachi tablet */
150631604Syokota    static int butmaphit[8] = {	/* MM HitTablet */
150731604Syokota	0,
150831604Syokota	MOUSE_BUTTON3DOWN,
150931604Syokota	MOUSE_BUTTON2DOWN,
151031604Syokota	MOUSE_BUTTON1DOWN,
151131604Syokota	MOUSE_BUTTON4DOWN,
151231604Syokota	MOUSE_BUTTON5DOWN,
151331604Syokota	MOUSE_BUTTON6DOWN,
151431604Syokota	MOUSE_BUTTON7DOWN,
151531604Syokota    };
151649967Syokota    /* for serial VersaPad */
151749967Syokota    static int butmapversa[8] = { /* VersaPad */
151849967Syokota	0,
151949967Syokota	0,
152049967Syokota	MOUSE_BUTTON3DOWN,
152149967Syokota	MOUSE_BUTTON3DOWN,
152249967Syokota	MOUSE_BUTTON1DOWN,
152349967Syokota	MOUSE_BUTTON1DOWN,
152449967Syokota	MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
152549967Syokota	MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
152649967Syokota    };
152749967Syokota    /* for PS/2 VersaPad */
152849967Syokota    static int butmapversaps2[8] = { /* VersaPad */
152949967Syokota	0,
153049967Syokota	MOUSE_BUTTON3DOWN,
153149967Syokota	0,
153249967Syokota	MOUSE_BUTTON3DOWN,
153349967Syokota	MOUSE_BUTTON1DOWN,
153449967Syokota	MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
153549967Syokota	MOUSE_BUTTON1DOWN,
153649967Syokota	MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
153749967Syokota    };
153816566Ssos    static int           pBufP = 0;
153916566Ssos    static unsigned char pBuf[8];
154049967Syokota    static int		 prev_x, prev_y;
154149967Syokota    static int		 on = FALSE;
154249967Syokota    int			 x, y;
154316566Ssos
154416566Ssos    debug("received char 0x%x",(int)rBuf);
154541271Syokota    if (rodent.rtype == MOUSE_PROTO_KIDSPAD)
154641271Syokota	return kidspad(rBuf, act) ;
154716566Ssos
154816566Ssos    /*
154916566Ssos     * Hack for resyncing: We check here for a package that is:
155016566Ssos     *  a) illegal (detected by wrong data-package header)
155116566Ssos     *  b) invalid (0x80 == -128 and that might be wrong for MouseSystems)
155216566Ssos     *  c) bad header-package
155316566Ssos     *
155416566Ssos     * NOTE: b) is a voilation of the MouseSystems-Protocol, since values of
155516566Ssos     *       -128 are allowed, but since they are very seldom we can easily
155616566Ssos     *       use them as package-header with no button pressed.
155716566Ssos     * NOTE/2: On a PS/2 mouse any byte is valid as a data byte. Furthermore,
155816566Ssos     *         0x80 is not valid as a header byte. For a PS/2 mouse we skip
155916566Ssos     *         checking data bytes.
156016566Ssos     *         For resyncing a PS/2 mouse we require the two most significant
156116566Ssos     *         bits in the header byte to be 0. These are the overflow bits,
156216566Ssos     *         and in case of an overflow we actually lose sync. Overflows
156316566Ssos     *         are very rare, however, and we quickly gain sync again after
156416566Ssos     *         an overflow condition. This is the best we can do. (Actually,
156516566Ssos     *         we could use bit 0x08 in the header byte for resyncing, since
156616566Ssos     *         that bit is supposed to be always on, but nobody told
156716566Ssos     *         Microsoft...)
156816566Ssos     */
156916566Ssos
157031604Syokota    if (pBufP != 0 && rodent.rtype != MOUSE_PROTO_PS2 &&
157131604Syokota	((rBuf & cur_proto[2]) != cur_proto[3] || rBuf == 0x80))
157216566Ssos    {
157316566Ssos	pBufP = 0;		/* skip package */
157416566Ssos    }
157516566Ssos
157631604Syokota    if (pBufP == 0 && (rBuf & cur_proto[0]) != cur_proto[1])
157731604Syokota	return 0;
157831604Syokota
157931604Syokota    /* is there an extra data byte? */
158031604Syokota    if (pBufP >= cur_proto[4] && (rBuf & cur_proto[0]) != cur_proto[1])
158116566Ssos    {
158216566Ssos	/*
158316566Ssos	 * Hack for Logitech MouseMan Mouse - Middle button
158416566Ssos	 *
158516566Ssos	 * Unfortunately this mouse has variable length packets: the standard
158616566Ssos	 * Microsoft 3 byte packet plus an optional 4th byte whenever the
158716566Ssos	 * middle button status changes.
158816566Ssos	 *
158916566Ssos	 * We have already processed the standard packet with the movement
159016566Ssos	 * and button info.  Now post an event message with the old status
159116566Ssos	 * of the left and right buttons and the updated middle button.
159216566Ssos	 */
159316566Ssos
159416566Ssos	/*
159516566Ssos	 * Even worse, different MouseMen and TrackMen differ in the 4th
159616566Ssos	 * byte: some will send 0x00/0x20, others 0x01/0x21, or even
159716566Ssos	 * 0x02/0x22, so I have to strip off the lower bits.
159831604Syokota         *
159931604Syokota         * [JCH-96/01/21]
160031604Syokota         * HACK for ALPS "fourth button". (It's bit 0x10 of the "fourth byte"
160131604Syokota         * and it is activated by tapping the glidepad with the finger! 8^)
160231604Syokota         * We map it to bit bit3, and the reverse map in xf86Events just has
160331604Syokota         * to be extended so that it is identified as Button 4. The lower
160431604Syokota         * half of the reverse-map may remain unchanged.
160516566Ssos	 */
160631604Syokota
160731604Syokota        /*
160831604Syokota	 * [KY-97/08/03]
160972645Sasmodai	 * Receive the fourth byte only when preceding three bytes have
161031604Syokota	 * been detected (pBufP >= cur_proto[4]).  In the previous
161131604Syokota	 * versions, the test was pBufP == 0; thus, we may have mistakingly
161272645Sasmodai	 * received a byte even if we didn't see anything preceding
161331604Syokota	 * the byte.
161431604Syokota	 */
161531604Syokota
161631604Syokota	if ((rBuf & cur_proto[5]) != cur_proto[6]) {
161731604Syokota            pBufP = 0;
161831604Syokota	    return 0;
161916566Ssos	}
162016566Ssos
162131604Syokota	switch (rodent.rtype) {
162231604Syokota#if notyet
162331604Syokota	case MOUSE_PROTO_MARIQUA:
162431604Syokota	    /*
162531604Syokota	     * This mouse has 16! buttons in addition to the standard
162631604Syokota	     * three of them.  They return 0x10 though 0x1f in the
162731604Syokota	     * so-called `ten key' mode and 0x30 though 0x3f in the
162831604Syokota	     * `function key' mode.  As there are only 31 bits for
162931604Syokota	     * button state (including the standard three), we ignore
163031604Syokota	     * the bit 0x20 and don't distinguish the two modes.
163131604Syokota	     */
163231604Syokota	    act->dx = act->dy = act->dz = 0;
163331604Syokota	    act->obutton = act->button;
163431604Syokota	    rBuf &= 0x1f;
163531604Syokota	    act->button = (1 << (rBuf - 13))
163631604Syokota                | (act->obutton & (MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN));
163731604Syokota	    /*
163831604Syokota	     * FIXME: this is a button "down" event. There needs to be
163931604Syokota	     * a corresponding button "up" event... XXX
164031604Syokota	     */
164131604Syokota	    break;
164231604Syokota#endif /* notyet */
164393071Swill	case MOUSE_PROTO_JOGDIAL:
164493071Swill	    break;
164531604Syokota
164631604Syokota	/*
164731604Syokota	 * IntelliMouse, NetMouse (including NetMouse Pro) and Mie Mouse
164831604Syokota	 * always send the fourth byte, whereas the fourth byte is
164931604Syokota	 * optional for GlidePoint and ThinkingMouse. The fourth byte
165031604Syokota	 * is also optional for MouseMan+ and FirstMouse+ in their
165131604Syokota	 * native mode. It is always sent if they are in the IntelliMouse
165231604Syokota	 * compatible mode.
165331604Syokota	 */
165431604Syokota	case MOUSE_PROTO_INTELLI:	/* IntelliMouse, NetMouse, Mie Mouse,
165531604Syokota					   MouseMan+ */
165631604Syokota	    act->dx = act->dy = 0;
165731604Syokota	    act->dz = (rBuf & 0x08) ? (rBuf & 0x0f) - 16 : (rBuf & 0x0f);
165858231Syokota	    if ((act->dz >= 7) || (act->dz <= -7))
165958231Syokota		act->dz = 0;
166031604Syokota	    act->obutton = act->button;
166131604Syokota	    act->button = butmapintelli[(rBuf & MOUSE_MSS_BUTTONS) >> 4]
166231604Syokota		| (act->obutton & (MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN));
166331604Syokota	    break;
166431604Syokota
166531604Syokota	default:
166631604Syokota	    act->dx = act->dy = act->dz = 0;
166731604Syokota	    act->obutton = act->button;
166831604Syokota	    act->button = butmapmss2[(rBuf & MOUSE_MSS_BUTTONS) >> 4]
166931604Syokota		| (act->obutton & (MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN));
167031604Syokota	    break;
167131604Syokota	}
167231604Syokota
167331604Syokota	act->flags = ((act->dx || act->dy || act->dz) ? MOUSE_POSCHANGED : 0)
167431604Syokota	    | (act->obutton ^ act->button);
167531604Syokota        pBufP = 0;
167631604Syokota	return act->flags;
167716566Ssos    }
167816566Ssos
167931604Syokota    if (pBufP >= cur_proto[4])
168031604Syokota	pBufP = 0;
168116566Ssos    pBuf[pBufP++] = rBuf;
168231604Syokota    if (pBufP != cur_proto[4])
168331604Syokota	return 0;
168416566Ssos
168516566Ssos    /*
168616566Ssos     * assembly full package
168716566Ssos     */
168816566Ssos
168931604Syokota    debug("assembled full packet (len %d) %x,%x,%x,%x,%x,%x,%x,%x",
169031604Syokota	cur_proto[4],
169131604Syokota	pBuf[0], pBuf[1], pBuf[2], pBuf[3],
169231604Syokota	pBuf[4], pBuf[5], pBuf[6], pBuf[7]);
169316566Ssos
169431604Syokota    act->dz = 0;
169531604Syokota    act->obutton = act->button;
169631604Syokota    switch (rodent.rtype)
169716566Ssos    {
169831604Syokota    case MOUSE_PROTO_MS:		/* Microsoft */
169931604Syokota    case MOUSE_PROTO_LOGIMOUSEMAN:	/* MouseMan/TrackMan */
170036991Sahasty    case MOUSE_PROTO_X10MOUSEREM:	/* X10 MouseRemote */
170134074Syokota	act->button = act->obutton & MOUSE_BUTTON4DOWN;
170216566Ssos	if (rodent.flags & ChordMiddle)
170334074Syokota	    act->button |= ((pBuf[0] & MOUSE_MSS_BUTTONS) == MOUSE_MSS_BUTTONS)
170431604Syokota		? MOUSE_BUTTON2DOWN
170531604Syokota		: butmapmss[(pBuf[0] & MOUSE_MSS_BUTTONS) >> 4];
170616566Ssos	else
170734074Syokota	    act->button |= (act->obutton & MOUSE_BUTTON2DOWN)
170831604Syokota		| butmapmss[(pBuf[0] & MOUSE_MSS_BUTTONS) >> 4];
170936991Sahasty
171036991Sahasty	/* Send X10 btn events to remote client (ensure -128-+127 range) */
171136991Sahasty	if ((rodent.rtype == MOUSE_PROTO_X10MOUSEREM) &&
171236991Sahasty	    ((pBuf[0] & 0xFC) == 0x44) && (pBuf[2] == 0x3F)) {
171336991Sahasty	    if (rodent.mremcfd >= 0) {
171436991Sahasty		unsigned char key = (signed char)(((pBuf[0] & 0x03) << 6) |
171536991Sahasty						  (pBuf[1] & 0x3F));
171636991Sahasty		write( rodent.mremcfd, &key, 1 );
171736991Sahasty	    }
171836991Sahasty	    return 0;
171936991Sahasty	}
172036991Sahasty
172196930Sknu	act->dx = (signed char)(((pBuf[0] & 0x03) << 6) | (pBuf[1] & 0x3F));
172296930Sknu	act->dy = (signed char)(((pBuf[0] & 0x0C) << 4) | (pBuf[2] & 0x3F));
172316566Ssos	break;
172431604Syokota
172531604Syokota    case MOUSE_PROTO_GLIDEPOINT:	/* GlidePoint */
172631604Syokota    case MOUSE_PROTO_THINK:		/* ThinkingMouse */
172731604Syokota    case MOUSE_PROTO_INTELLI:		/* IntelliMouse, NetMouse, Mie Mouse,
172831604Syokota					   MouseMan+ */
172931604Syokota	act->button = (act->obutton & (MOUSE_BUTTON2DOWN | MOUSE_BUTTON4DOWN))
173031604Syokota            | butmapmss[(pBuf[0] & MOUSE_MSS_BUTTONS) >> 4];
173196930Sknu	act->dx = (signed char)(((pBuf[0] & 0x03) << 6) | (pBuf[1] & 0x3F));
173296930Sknu	act->dy = (signed char)(((pBuf[0] & 0x0C) << 4) | (pBuf[2] & 0x3F));
173331604Syokota	break;
173416566Ssos
173531604Syokota    case MOUSE_PROTO_MSC:		/* MouseSystems Corp */
173631604Syokota#if notyet
173731604Syokota    case MOUSE_PROTO_MARIQUA:		/* Mariqua */
173831604Syokota#endif
173931604Syokota	act->button = butmapmsc[(~pBuf[0]) & MOUSE_MSC_BUTTONS];
174096930Sknu	act->dx =    (signed char)(pBuf[1]) + (signed char)(pBuf[3]);
174196930Sknu	act->dy = - ((signed char)(pBuf[2]) + (signed char)(pBuf[4]));
174216566Ssos	break;
174393071Swill
174493071Swill    case MOUSE_PROTO_JOGDIAL:		/* JogDial */
174593071Swill	    if (rBuf == 0x6c)
174696930Sknu	      act->dz = -1;
174793071Swill	    if (rBuf == 0x72)
174896930Sknu	      act->dz = 1;
174993071Swill	    if (rBuf == 0x64)
175093071Swill	      act->button = MOUSE_BUTTON1DOWN;
175193071Swill	    if (rBuf == 0x75)
175293071Swill	      act->button = 0;
175393071Swill	break;
175493071Swill
175531604Syokota    case MOUSE_PROTO_HITTAB:		/* MM HitTablet */
175631604Syokota	act->button = butmaphit[pBuf[0] & 0x07];
175731604Syokota	act->dx = (pBuf[0] & MOUSE_MM_XPOSITIVE) ?   pBuf[1] : - pBuf[1];
175831604Syokota	act->dy = (pBuf[0] & MOUSE_MM_YPOSITIVE) ? - pBuf[2] :   pBuf[2];
175918222Speter	break;
176018222Speter
176131604Syokota    case MOUSE_PROTO_MM:		/* MM Series */
176231604Syokota    case MOUSE_PROTO_LOGI:		/* Logitech Mice */
176331604Syokota	act->button = butmapmsc[pBuf[0] & MOUSE_MSC_BUTTONS];
176431604Syokota	act->dx = (pBuf[0] & MOUSE_MM_XPOSITIVE) ?   pBuf[1] : - pBuf[1];
176531604Syokota	act->dy = (pBuf[0] & MOUSE_MM_YPOSITIVE) ? - pBuf[2] :   pBuf[2];
176616566Ssos	break;
176716566Ssos
176849967Syokota    case MOUSE_PROTO_VERSAPAD:		/* VersaPad */
176949967Syokota	act->button = butmapversa[(pBuf[0] & MOUSE_VERSA_BUTTONS) >> 3];
177049967Syokota	act->button |= (pBuf[0] & MOUSE_VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
177149967Syokota	act->dx = act->dy = 0;
177249967Syokota	if (!(pBuf[0] & MOUSE_VERSA_IN_USE)) {
177349967Syokota	    on = FALSE;
177449967Syokota	    break;
177549967Syokota	}
177649967Syokota	x = (pBuf[2] << 6) | pBuf[1];
177749967Syokota	if (x & 0x800)
177849967Syokota	    x -= 0x1000;
177949967Syokota	y = (pBuf[4] << 6) | pBuf[3];
178049967Syokota	if (y & 0x800)
178149967Syokota	    y -= 0x1000;
178249967Syokota	if (on) {
178349967Syokota	    act->dx = prev_x - x;
178449967Syokota	    act->dy = prev_y - y;
178549967Syokota	} else {
178649967Syokota	    on = TRUE;
178749967Syokota	}
178849967Syokota	prev_x = x;
178949967Syokota	prev_y = y;
179049967Syokota	break;
179149967Syokota
179231604Syokota    case MOUSE_PROTO_BUS:		/* Bus */
179331604Syokota    case MOUSE_PROTO_INPORT:		/* InPort */
179431604Syokota	act->button = butmapmsc[(~pBuf[0]) & MOUSE_MSC_BUTTONS];
179596930Sknu	act->dx =   (signed char)pBuf[1];
179696930Sknu	act->dy = - (signed char)pBuf[2];
179716566Ssos	break;
179816566Ssos
179931604Syokota    case MOUSE_PROTO_PS2:		/* PS/2 */
180031604Syokota	act->button = butmapps2[pBuf[0] & MOUSE_PS2_BUTTONS];
180131604Syokota	act->dx = (pBuf[0] & MOUSE_PS2_XNEG) ?    pBuf[1] - 256  :  pBuf[1];
180231604Syokota	act->dy = (pBuf[0] & MOUSE_PS2_YNEG) ?  -(pBuf[2] - 256) : -pBuf[2];
180331604Syokota	/*
180431604Syokota	 * Moused usually operates the psm driver at the operation level 1
180531604Syokota	 * which sends mouse data in MOUSE_PROTO_SYSMOUSE protocol.
180631604Syokota	 * The following code takes effect only when the user explicitly
180731604Syokota	 * requets the level 2 at which wheel movement and additional button
180831604Syokota	 * actions are encoded in model-dependent formats. At the level 0
180931604Syokota	 * the following code is no-op because the psm driver says the model
181031604Syokota	 * is MOUSE_MODEL_GENERIC.
181131604Syokota	 */
181231604Syokota	switch (rodent.hw.model) {
181358231Syokota	case MOUSE_MODEL_EXPLORER:
181458231Syokota	    /* wheel and additional button data is in the fourth byte */
181558231Syokota	    act->dz = (pBuf[3] & MOUSE_EXPLORER_ZNEG)
181658231Syokota		? (pBuf[3] & 0x0f) - 16 : (pBuf[3] & 0x0f);
181758231Syokota	    act->button |= (pBuf[3] & MOUSE_EXPLORER_BUTTON4DOWN)
181858231Syokota		? MOUSE_BUTTON4DOWN : 0;
181958231Syokota	    act->button |= (pBuf[3] & MOUSE_EXPLORER_BUTTON5DOWN)
182058231Syokota		? MOUSE_BUTTON5DOWN : 0;
182158231Syokota	    break;
182231604Syokota	case MOUSE_MODEL_INTELLI:
182331604Syokota	case MOUSE_MODEL_NET:
182431604Syokota	    /* wheel data is in the fourth byte */
182596930Sknu	    act->dz = (signed char)pBuf[3];
182658231Syokota	    if ((act->dz >= 7) || (act->dz <= -7))
182758231Syokota		act->dz = 0;
182858231Syokota	    /* some compatible mice may have additional buttons */
182958231Syokota	    act->button |= (pBuf[0] & MOUSE_PS2INTELLI_BUTTON4DOWN)
183058231Syokota		? MOUSE_BUTTON4DOWN : 0;
183158231Syokota	    act->button |= (pBuf[0] & MOUSE_PS2INTELLI_BUTTON5DOWN)
183258231Syokota		? MOUSE_BUTTON5DOWN : 0;
183331604Syokota	    break;
183431604Syokota	case MOUSE_MODEL_MOUSEMANPLUS:
183548778Syokota	    if (((pBuf[0] & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC)
183648778Syokota		    && (abs(act->dx) > 191)
183748778Syokota		    && MOUSE_PS2PLUS_CHECKBITS(pBuf)) {
183831604Syokota		/* the extended data packet encodes button and wheel events */
183948778Syokota		switch (MOUSE_PS2PLUS_PACKET_TYPE(pBuf)) {
184048778Syokota		case 1:
184148778Syokota		    /* wheel data packet */
184248778Syokota		    act->dx = act->dy = 0;
184348778Syokota		    if (pBuf[2] & 0x80) {
184448778Syokota			/* horizontal roller count - ignore it XXX*/
184548778Syokota		    } else {
184648778Syokota			/* vertical roller count */
184748778Syokota			act->dz = (pBuf[2] & MOUSE_PS2PLUS_ZNEG)
184848778Syokota			    ? (pBuf[2] & 0x0f) - 16 : (pBuf[2] & 0x0f);
184948778Syokota		    }
185048778Syokota		    act->button |= (pBuf[2] & MOUSE_PS2PLUS_BUTTON4DOWN)
185148778Syokota			? MOUSE_BUTTON4DOWN : 0;
185248778Syokota		    act->button |= (pBuf[2] & MOUSE_PS2PLUS_BUTTON5DOWN)
185348778Syokota			? MOUSE_BUTTON5DOWN : 0;
185448778Syokota		    break;
185548778Syokota		case 2:
185658231Syokota		    /* this packet type is reserved by Logitech */
185758231Syokota		    /*
185858231Syokota		     * IBM ScrollPoint Mouse uses this packet type to
185958231Syokota		     * encode both vertical and horizontal scroll movement.
186058231Syokota		     */
186158231Syokota		    act->dx = act->dy = 0;
186258231Syokota		    /* horizontal roller count */
186358231Syokota		    if (pBuf[2] & 0x0f)
186458231Syokota			act->dz = (pBuf[2] & MOUSE_SPOINT_WNEG) ? -2 : 2;
186558231Syokota		    /* vertical roller count */
186658231Syokota		    if (pBuf[2] & 0xf0)
186758231Syokota			act->dz = (pBuf[2] & MOUSE_SPOINT_ZNEG) ? -1 : 1;
186858231Syokota#if 0
186958231Syokota		    /* vertical roller count */
187058231Syokota		    act->dz = (pBuf[2] & MOUSE_SPOINT_ZNEG)
187158231Syokota			? ((pBuf[2] >> 4) & 0x0f) - 16
187258231Syokota			: ((pBuf[2] >> 4) & 0x0f);
187358231Syokota		    /* horizontal roller count */
187458231Syokota		    act->dw = (pBuf[2] & MOUSE_SPOINT_WNEG)
187558231Syokota			? (pBuf[2] & 0x0f) - 16 : (pBuf[2] & 0x0f);
187658231Syokota#endif
187758231Syokota		    break;
187848778Syokota		case 0:
187948778Syokota		    /* device type packet - shouldn't happen */
1880102413Scharnier		    /* FALLTHROUGH */
188148778Syokota		default:
188248778Syokota		    act->dx = act->dy = 0;
188348778Syokota		    act->button = act->obutton;
188448778Syokota            	    debug("unknown PS2++ packet type %d: 0x%02x 0x%02x 0x%02x\n",
188548778Syokota			  MOUSE_PS2PLUS_PACKET_TYPE(pBuf),
188648778Syokota			  pBuf[0], pBuf[1], pBuf[2]);
188748778Syokota		    break;
188848778Syokota		}
188931604Syokota	    } else {
189031604Syokota		/* preserve button states */
189131604Syokota		act->button |= act->obutton & MOUSE_EXTBUTTONS;
189231604Syokota	    }
189331604Syokota	    break;
189431604Syokota	case MOUSE_MODEL_GLIDEPOINT:
189531604Syokota	    /* `tapping' action */
189631604Syokota	    act->button |= ((pBuf[0] & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
189731604Syokota	    break;
189831604Syokota	case MOUSE_MODEL_NETSCROLL:
189958231Syokota	    /* three addtional bytes encode buttons and wheel events */
190058231Syokota	    act->button |= (pBuf[3] & MOUSE_PS2_BUTTON3DOWN)
190131604Syokota		? MOUSE_BUTTON4DOWN : 0;
190258231Syokota	    act->button |= (pBuf[3] & MOUSE_PS2_BUTTON1DOWN)
190358231Syokota		? MOUSE_BUTTON5DOWN : 0;
190431604Syokota	    act->dz = (pBuf[3] & MOUSE_PS2_XNEG) ? pBuf[4] - 256 : pBuf[4];
190531604Syokota	    break;
190631604Syokota	case MOUSE_MODEL_THINK:
190731604Syokota	    /* the fourth button state in the first byte */
190831604Syokota	    act->button |= (pBuf[0] & MOUSE_PS2_TAP) ? MOUSE_BUTTON4DOWN : 0;
190931604Syokota	    break;
191049967Syokota	case MOUSE_MODEL_VERSAPAD:
191149967Syokota	    act->button = butmapversaps2[pBuf[0] & MOUSE_PS2VERSA_BUTTONS];
191249967Syokota	    act->button |=
191349967Syokota		(pBuf[0] & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
191449967Syokota	    act->dx = act->dy = 0;
191549967Syokota	    if (!(pBuf[0] & MOUSE_PS2VERSA_IN_USE)) {
191649967Syokota		on = FALSE;
191749967Syokota		break;
191849967Syokota	    }
191949967Syokota	    x = ((pBuf[4] << 8) & 0xf00) | pBuf[1];
192049967Syokota	    if (x & 0x800)
192149967Syokota		x -= 0x1000;
192249967Syokota	    y = ((pBuf[4] << 4) & 0xf00) | pBuf[2];
192349967Syokota	    if (y & 0x800)
192449967Syokota		y -= 0x1000;
192549967Syokota	    if (on) {
192649967Syokota		act->dx = prev_x - x;
192749967Syokota		act->dy = prev_y - y;
192849967Syokota	    } else {
192949967Syokota		on = TRUE;
193049967Syokota	    }
193149967Syokota	    prev_x = x;
193249967Syokota	    prev_y = y;
193349967Syokota	    break;
193458231Syokota	case MOUSE_MODEL_4D:
193558231Syokota	    act->dx = (pBuf[1] & 0x80) ?    pBuf[1] - 256  :  pBuf[1];
193658231Syokota	    act->dy = (pBuf[2] & 0x80) ?  -(pBuf[2] - 256) : -pBuf[2];
193758231Syokota	    switch (pBuf[0] & MOUSE_4D_WHEELBITS) {
193858231Syokota	    case 0x10:
193958231Syokota		act->dz = 1;
194058231Syokota		break;
194158231Syokota	    case 0x30:
194258231Syokota		act->dz = -1;
194358231Syokota		break;
194458231Syokota	    case 0x40:	/* 2nd wheel rolling right XXX */
194558231Syokota		act->dz = 2;
194658231Syokota		break;
194758231Syokota	    case 0xc0:	/* 2nd wheel rolling left XXX */
194858231Syokota		act->dz = -2;
194958231Syokota		break;
195058231Syokota	    }
195158231Syokota	    break;
195258231Syokota	case MOUSE_MODEL_4DPLUS:
195358231Syokota	    if ((act->dx < 16 - 256) && (act->dy > 256 - 16)) {
195458231Syokota		act->dx = act->dy = 0;
195558231Syokota		if (pBuf[2] & MOUSE_4DPLUS_BUTTON4DOWN)
195658231Syokota		    act->button |= MOUSE_BUTTON4DOWN;
195758231Syokota		act->dz = (pBuf[2] & MOUSE_4DPLUS_ZNEG)
195858231Syokota			      ? ((pBuf[2] & 0x07) - 8) : (pBuf[2] & 0x07);
195958231Syokota	    } else {
196058231Syokota		/* preserve previous button states */
196158231Syokota		act->button |= act->obutton & MOUSE_EXTBUTTONS;
196258231Syokota	    }
196358231Syokota	    break;
196431604Syokota	case MOUSE_MODEL_GENERIC:
196531604Syokota	default:
196631604Syokota	    break;
196731604Syokota	}
196816566Ssos	break;
196931604Syokota
197031604Syokota    case MOUSE_PROTO_SYSMOUSE:		/* sysmouse */
197131604Syokota	act->button = butmapmsc[(~pBuf[0]) & MOUSE_SYS_STDBUTTONS];
197296930Sknu	act->dx =    (signed char)(pBuf[1]) + (signed char)(pBuf[3]);
197396930Sknu	act->dy = - ((signed char)(pBuf[2]) + (signed char)(pBuf[4]));
197431604Syokota	if (rodent.level == 1) {
197596930Sknu	    act->dz = ((signed char)(pBuf[5] << 1) + (signed char)(pBuf[6] << 1)) >> 1;
197631604Syokota	    act->button |= ((~pBuf[7] & MOUSE_SYS_EXTBUTTONS) << 3);
197731604Syokota	}
197831604Syokota	break;
197931604Syokota
198031604Syokota    default:
198131604Syokota	return 0;
198216566Ssos    }
198331604Syokota    /*
198431604Syokota     * We don't reset pBufP here yet, as there may be an additional data
198531604Syokota     * byte in some protocols. See above.
198631604Syokota     */
198731604Syokota
198831604Syokota    /* has something changed? */
198931604Syokota    act->flags = ((act->dx || act->dy || act->dz) ? MOUSE_POSCHANGED : 0)
199031604Syokota	| (act->obutton ^ act->button);
199131604Syokota
199258344Syokota    return act->flags;
199358344Syokota}
199458344Syokota
199558344Syokotastatic int
199658344Syokotar_statetrans(mousestatus_t *a1, mousestatus_t *a2, int trans)
199758344Syokota{
199858344Syokota    int changed;
199958344Syokota    int flags;
200058344Syokota
200158344Syokota    a2->dx = a1->dx;
200258344Syokota    a2->dy = a1->dy;
200358344Syokota    a2->dz = a1->dz;
200458344Syokota    a2->obutton = a2->button;
200558344Syokota    a2->button = a1->button;
200658344Syokota    a2->flags = a1->flags;
200758344Syokota    changed = FALSE;
200858344Syokota
200931604Syokota    if (rodent.flags & Emulate3Button) {
201058344Syokota	if (debug > 2)
201158344Syokota	    debug("state:%d, trans:%d -> state:%d",
201258344Syokota		  mouse_button_state, trans,
201358344Syokota		  states[mouse_button_state].s[trans]);
201479430Siedowse	/*
201579430Siedowse	 * Avoid re-ordering button and movement events. While a button
201679430Siedowse	 * event is deferred, throw away up to BUTTON2_MAXMOVE movement
201779430Siedowse	 * events to allow for mouse jitter. If more movement events
201879430Siedowse	 * occur, then complete the deferred button events immediately.
201979430Siedowse	 */
202079430Siedowse	if ((a2->dx != 0 || a2->dy != 0) &&
202179430Siedowse	    S_DELAYED(states[mouse_button_state].s[trans])) {
202279430Siedowse		if (++mouse_move_delayed > BUTTON2_MAXMOVE) {
202379430Siedowse			mouse_move_delayed = 0;
202479430Siedowse			mouse_button_state =
202579430Siedowse			    states[mouse_button_state].s[A_TIMEOUT];
202679430Siedowse			changed = TRUE;
202779432Siedowse		} else
202879430Siedowse			a2->dx = a2->dy = 0;
202979430Siedowse	} else
203079430Siedowse		mouse_move_delayed = 0;
203179430Siedowse	if (mouse_button_state != states[mouse_button_state].s[trans])
203279430Siedowse		changed = TRUE;
203379430Siedowse	if (changed)
203479430Siedowse		gettimeofday(&mouse_button_state_tv, NULL);
203558344Syokota	mouse_button_state = states[mouse_button_state].s[trans];
203658344Syokota	a2->button &=
203758344Syokota	    ~(MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN);
203858344Syokota	a2->button &= states[mouse_button_state].mask;
203958344Syokota	a2->button |= states[mouse_button_state].buttons;
204058344Syokota	flags = a2->flags & MOUSE_POSCHANGED;
204158344Syokota	flags |= a2->obutton ^ a2->button;
204258344Syokota	if (flags & MOUSE_BUTTON2DOWN) {
204358344Syokota	    a2->flags = flags & MOUSE_BUTTON2DOWN;
204458344Syokota	    r_timestamp(a2);
204558344Syokota	}
204658344Syokota	a2->flags = flags;
204731604Syokota    }
204858344Syokota    return changed;
204916566Ssos}
205018222Speter
205131604Syokota/* phisical to logical button mapping */
205231604Syokotastatic int p2l[MOUSE_MAXBUTTON] = {
205331604Syokota    MOUSE_BUTTON1DOWN, MOUSE_BUTTON2DOWN, MOUSE_BUTTON3DOWN, MOUSE_BUTTON4DOWN,
205431604Syokota    MOUSE_BUTTON5DOWN, MOUSE_BUTTON6DOWN, MOUSE_BUTTON7DOWN, MOUSE_BUTTON8DOWN,
205531604Syokota    0x00000100,        0x00000200,        0x00000400,        0x00000800,
205631604Syokota    0x00001000,        0x00002000,        0x00004000,        0x00008000,
205731604Syokota    0x00010000,        0x00020000,        0x00040000,        0x00080000,
205831604Syokota    0x00100000,        0x00200000,        0x00400000,        0x00800000,
205931604Syokota    0x01000000,        0x02000000,        0x04000000,        0x08000000,
206031604Syokota    0x10000000,        0x20000000,        0x40000000,
206131604Syokota};
206231604Syokota
206331604Syokotastatic char *
206431604Syokotaskipspace(char *s)
206531604Syokota{
206631604Syokota    while(isspace(*s))
206731604Syokota	++s;
206831604Syokota    return s;
206931604Syokota}
207031604Syokota
207131604Syokotastatic int
207231604Syokotar_installmap(char *arg)
207331604Syokota{
207431604Syokota    int pbutton;
207531604Syokota    int lbutton;
207631604Syokota    char *s;
207731604Syokota
207831604Syokota    while (*arg) {
207931604Syokota	arg = skipspace(arg);
208031604Syokota	s = arg;
208131604Syokota	while (isdigit(*arg))
208231604Syokota	    ++arg;
208331604Syokota	arg = skipspace(arg);
208431604Syokota	if ((arg <= s) || (*arg != '='))
208531604Syokota	    return FALSE;
208631604Syokota	lbutton = atoi(s);
208731604Syokota
208831604Syokota	arg = skipspace(++arg);
208931604Syokota	s = arg;
209031604Syokota	while (isdigit(*arg))
209131604Syokota	    ++arg;
209231604Syokota	if ((arg <= s) || (!isspace(*arg) && (*arg != '\0')))
209331604Syokota	    return FALSE;
209431604Syokota	pbutton = atoi(s);
209531604Syokota
209631604Syokota	if ((lbutton <= 0) || (lbutton > MOUSE_MAXBUTTON))
209731604Syokota	    return FALSE;
209831604Syokota	if ((pbutton <= 0) || (pbutton > MOUSE_MAXBUTTON))
209931604Syokota	    return FALSE;
210031604Syokota	p2l[pbutton - 1] = 1 << (lbutton - 1);
210158344Syokota	mstate[lbutton - 1] = &bstate[pbutton - 1];
210231604Syokota    }
210331604Syokota
210431604Syokota    return TRUE;
210531604Syokota}
210631604Syokota
210731604Syokotastatic void
210831604Syokotar_map(mousestatus_t *act1, mousestatus_t *act2)
210931604Syokota{
211031604Syokota    register int pb;
211131604Syokota    register int pbuttons;
211231604Syokota    int lbuttons;
211331604Syokota
211431604Syokota    pbuttons = act1->button;
211531604Syokota    lbuttons = 0;
211631604Syokota
211731604Syokota    act2->obutton = act2->button;
211841270Syokota    if (pbuttons & rodent.wmode) {
211941270Syokota	pbuttons &= ~rodent.wmode;
212041270Syokota	act1->dz = act1->dy;
212141270Syokota	act1->dx = 0;
212241270Syokota	act1->dy = 0;
212341270Syokota    }
212431604Syokota    act2->dx = act1->dx;
212531604Syokota    act2->dy = act1->dy;
212631604Syokota    act2->dz = act1->dz;
212731604Syokota
212858231Syokota    switch (rodent.zmap[0]) {
212931604Syokota    case 0:	/* do nothing */
213031604Syokota	break;
213131604Syokota    case MOUSE_XAXIS:
213231604Syokota	if (act1->dz != 0) {
213331604Syokota	    act2->dx = act1->dz;
213431604Syokota	    act2->dz = 0;
213531604Syokota	}
213631604Syokota	break;
213731604Syokota    case MOUSE_YAXIS:
213831604Syokota	if (act1->dz != 0) {
213931604Syokota	    act2->dy = act1->dz;
214031604Syokota	    act2->dz = 0;
214131604Syokota	}
214231604Syokota	break;
214331604Syokota    default:	/* buttons */
214458231Syokota	pbuttons &= ~(rodent.zmap[0] | rodent.zmap[1]
214558231Syokota		    | rodent.zmap[2] | rodent.zmap[3]);
214658344Syokota	if ((act1->dz < -1) && rodent.zmap[2]) {
214758231Syokota	    pbuttons |= rodent.zmap[2];
214858344Syokota	    zstate[2].count = 1;
214958344Syokota	} else if (act1->dz < 0) {
215058231Syokota	    pbuttons |= rodent.zmap[0];
215158344Syokota	    zstate[0].count = 1;
215258344Syokota	} else if ((act1->dz > 1) && rodent.zmap[3]) {
215358231Syokota	    pbuttons |= rodent.zmap[3];
215458344Syokota	    zstate[3].count = 1;
215558344Syokota	} else if (act1->dz > 0) {
215658231Syokota	    pbuttons |= rodent.zmap[1];
215758344Syokota	    zstate[1].count = 1;
215858344Syokota	}
215931604Syokota	act2->dz = 0;
216031604Syokota	break;
216131604Syokota    }
216231604Syokota
216331604Syokota    for (pb = 0; (pb < MOUSE_MAXBUTTON) && (pbuttons != 0); ++pb) {
216431604Syokota	lbuttons |= (pbuttons & 1) ? p2l[pb] : 0;
216531604Syokota	pbuttons >>= 1;
216631604Syokota    }
216731604Syokota    act2->button = lbuttons;
216831604Syokota
216931604Syokota    act2->flags = ((act2->dx || act2->dy || act2->dz) ? MOUSE_POSCHANGED : 0)
217031604Syokota	| (act2->obutton ^ act2->button);
217131604Syokota}
217231604Syokota
217331604Syokotastatic void
217458344Syokotar_timestamp(mousestatus_t *act)
217531604Syokota{
217631604Syokota    struct timeval tv;
217731604Syokota    struct timeval tv1;
217831604Syokota    struct timeval tv2;
217958344Syokota    struct timeval tv3;
218031604Syokota    int button;
218131604Syokota    int mask;
218231604Syokota    int i;
218331604Syokota
218431604Syokota    mask = act->flags & MOUSE_BUTTONS;
218558344Syokota#if 0
218631604Syokota    if (mask == 0)
218731604Syokota	return;
218858344Syokota#endif
218931604Syokota
219058344Syokota    gettimeofday(&tv1, NULL);
219158344Syokota
219258344Syokota    /* double click threshold */
219331604Syokota    tv2.tv_sec = rodent.clickthreshold/1000;
219431604Syokota    tv2.tv_usec = (rodent.clickthreshold%1000)*1000;
219531604Syokota    timersub(&tv1, &tv2, &tv);
219631604Syokota    debug("tv:  %ld %ld", tv.tv_sec, tv.tv_usec);
219758344Syokota
219858344Syokota    /* 3 button emulation timeout */
219958344Syokota    tv2.tv_sec = rodent.button2timeout/1000;
220058344Syokota    tv2.tv_usec = (rodent.button2timeout%1000)*1000;
220158344Syokota    timersub(&tv1, &tv2, &tv3);
220258344Syokota
220331604Syokota    button = MOUSE_BUTTON1DOWN;
220431604Syokota    for (i = 0; (i < MOUSE_MAXBUTTON) && (mask != 0); ++i) {
220531604Syokota        if (mask & 1) {
220631604Syokota            if (act->button & button) {
220731604Syokota                /* the button is down */
220831604Syokota    		debug("  :  %ld %ld",
220958344Syokota		    bstate[i].tv.tv_sec, bstate[i].tv.tv_usec);
221058344Syokota		if (timercmp(&tv, &bstate[i].tv, >)) {
221158344Syokota                    bstate[i].count = 1;
221231604Syokota                } else {
221358344Syokota                    ++bstate[i].count;
221431604Syokota                }
221558344Syokota		bstate[i].tv = tv1;
221654634Syokota            } else {
221754634Syokota                /* the button is up */
221858344Syokota                bstate[i].tv = tv1;
221958344Syokota            }
222058344Syokota        } else {
222158344Syokota	    if (act->button & button) {
222258344Syokota		/* the button has been down */
222358344Syokota		if (timercmp(&tv3, &bstate[i].tv, >)) {
222458344Syokota		    bstate[i].count = 1;
222558344Syokota		    bstate[i].tv = tv1;
222658344Syokota		    act->flags |= button;
222758344Syokota		    debug("button %d timeout", i + 1);
222858344Syokota		}
222958344Syokota	    } else {
223058344Syokota		/* the button has been up */
223158344Syokota	    }
223258344Syokota	}
223358344Syokota	button <<= 1;
223458344Syokota	mask >>= 1;
223558344Syokota    }
223658344Syokota}
223758344Syokota
223858344Syokotastatic int
223958344Syokotar_timeout(void)
224058344Syokota{
224158344Syokota    struct timeval tv;
224258344Syokota    struct timeval tv1;
224358344Syokota    struct timeval tv2;
224458344Syokota
224559090Syokota    if (states[mouse_button_state].timeout)
224659090Syokota	return TRUE;
224758344Syokota    gettimeofday(&tv1, NULL);
224858344Syokota    tv2.tv_sec = rodent.button2timeout/1000;
224958344Syokota    tv2.tv_usec = (rodent.button2timeout%1000)*1000;
225058344Syokota    timersub(&tv1, &tv2, &tv);
225158344Syokota    return timercmp(&tv, &mouse_button_state_tv, >);
225258344Syokota}
225358344Syokota
225458344Syokotastatic void
225558344Syokotar_click(mousestatus_t *act)
225658344Syokota{
225758344Syokota    struct mouse_info mouse;
225858344Syokota    int button;
225958344Syokota    int mask;
226058344Syokota    int i;
226158344Syokota
226258344Syokota    mask = act->flags & MOUSE_BUTTONS;
226358344Syokota    if (mask == 0)
226458344Syokota	return;
226558344Syokota
226658344Syokota    button = MOUSE_BUTTON1DOWN;
226758344Syokota    for (i = 0; (i < MOUSE_MAXBUTTON) && (mask != 0); ++i) {
226858344Syokota        if (mask & 1) {
226958344Syokota	    debug("mstate[%d]->count:%d", i, mstate[i]->count);
227058344Syokota            if (act->button & button) {
227158344Syokota                /* the button is down */
227258344Syokota	        mouse.u.event.value = mstate[i]->count;
227358344Syokota            } else {
227458344Syokota                /* the button is up */
227531604Syokota	        mouse.u.event.value = 0;
227631604Syokota            }
227731604Syokota	    mouse.operation = MOUSE_BUTTON_EVENT;
227831604Syokota	    mouse.u.event.id = button;
227931604Syokota	    if (debug < 2)
228031604Syokota	        ioctl(rodent.cfd, CONS_MOUSECTL, &mouse);
228131604Syokota	    debug("button %d  count %d", i + 1, mouse.u.event.value);
228231604Syokota        }
228331604Syokota	button <<= 1;
228431604Syokota	mask >>= 1;
228531604Syokota    }
228631604Syokota}
228731604Syokota
228818222Speter/* $XConsortium: posix_tty.c,v 1.3 95/01/05 20:42:55 kaleb Exp $ */
228918222Speter/* $XFree86: xc/programs/Xserver/hw/xfree86/os-support/shared/posix_tty.c,v 3.4 1995/01/28 17:05:03 dawes Exp $ */
229018222Speter/*
229118222Speter * Copyright 1993 by David Dawes <dawes@physics.su.oz.au>
229218222Speter *
229318222Speter * Permission to use, copy, modify, distribute, and sell this software and its
229418222Speter * documentation for any purpose is hereby granted without fee, provided that
229518222Speter * the above copyright notice appear in all copies and that both that
229618222Speter * copyright notice and this permission notice appear in supporting
229718222Speter * documentation, and that the name of David Dawes
229818222Speter * not be used in advertising or publicity pertaining to distribution of
229918222Speter * the software without specific, written prior permission.
230018222Speter * David Dawes makes no representations about the suitability of this
230118222Speter * software for any purpose.  It is provided "as is" without express or
230218222Speter * implied warranty.
230318222Speter *
230418222Speter * DAVID DAWES DISCLAIMS ALL WARRANTIES WITH REGARD TO
230518222Speter * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
230618222Speter * FITNESS, IN NO EVENT SHALL DAVID DAWES BE LIABLE FOR
230718222Speter * ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
230818222Speter * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
230918222Speter * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
231018222Speter * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
231118222Speter *
231218222Speter */
231318222Speter
231418222Speter
231531604Syokotastatic void
231631604Syokotasetmousespeed(int old, int new, unsigned cflag)
231718222Speter{
231818222Speter	struct termios tty;
231918222Speter	char *c;
232018222Speter
232118222Speter	if (tcgetattr(rodent.mfd, &tty) < 0)
232218222Speter	{
232395629Siedowse		logwarn("unable to get status of mouse fd");
232431604Syokota		return;
232518222Speter	}
232618222Speter
232718222Speter	tty.c_iflag = IGNBRK | IGNPAR;
232818222Speter	tty.c_oflag = 0;
232918222Speter	tty.c_lflag = 0;
233018222Speter	tty.c_cflag = (tcflag_t)cflag;
233118222Speter	tty.c_cc[VTIME] = 0;
233218222Speter	tty.c_cc[VMIN] = 1;
233318222Speter
233418222Speter	switch (old)
233518222Speter	{
233618222Speter	case 9600:
233718222Speter		cfsetispeed(&tty, B9600);
233818222Speter		cfsetospeed(&tty, B9600);
233918222Speter		break;
234018222Speter	case 4800:
234118222Speter		cfsetispeed(&tty, B4800);
234218222Speter		cfsetospeed(&tty, B4800);
234318222Speter		break;
234418222Speter	case 2400:
234518222Speter		cfsetispeed(&tty, B2400);
234618222Speter		cfsetospeed(&tty, B2400);
234718222Speter		break;
234818222Speter	case 1200:
234918222Speter	default:
235018222Speter		cfsetispeed(&tty, B1200);
235118222Speter		cfsetospeed(&tty, B1200);
235218222Speter	}
235318222Speter
235418222Speter	if (tcsetattr(rodent.mfd, TCSADRAIN, &tty) < 0)
235518222Speter	{
235695629Siedowse		logwarn("unable to set status of mouse fd");
235731604Syokota		return;
235818222Speter	}
235918222Speter
236018222Speter	switch (new)
236118222Speter	{
236218222Speter	case 9600:
236318222Speter		c = "*q";
236418222Speter		cfsetispeed(&tty, B9600);
236518222Speter		cfsetospeed(&tty, B9600);
236618222Speter		break;
236718222Speter	case 4800:
236818222Speter		c = "*p";
236918222Speter		cfsetispeed(&tty, B4800);
237018222Speter		cfsetospeed(&tty, B4800);
237118222Speter		break;
237218222Speter	case 2400:
237318222Speter		c = "*o";
237418222Speter		cfsetispeed(&tty, B2400);
237518222Speter		cfsetospeed(&tty, B2400);
237618222Speter		break;
237718222Speter	case 1200:
237818222Speter	default:
237918222Speter		c = "*n";
238018222Speter		cfsetispeed(&tty, B1200);
238118222Speter		cfsetospeed(&tty, B1200);
238218222Speter	}
238318222Speter
238431604Syokota	if (rodent.rtype == MOUSE_PROTO_LOGIMOUSEMAN
238531604Syokota	    || rodent.rtype == MOUSE_PROTO_LOGI)
238618222Speter	{
238718222Speter		if (write(rodent.mfd, c, 2) != 2)
238818222Speter		{
238995629Siedowse			logwarn("unable to write to mouse fd");
239031604Syokota			return;
239118222Speter		}
239218222Speter	}
239318222Speter	usleep(100000);
239418222Speter
239518222Speter	if (tcsetattr(rodent.mfd, TCSADRAIN, &tty) < 0)
239695629Siedowse		logwarn("unable to set status of mouse fd");
239731604Syokota}
239831604Syokota
239931604Syokota/*
240031604Syokota * PnP COM device support
240131604Syokota *
240231604Syokota * It's a simplistic implementation, but it works :-)
240331604Syokota * KY, 31/7/97.
240431604Syokota */
240531604Syokota
240631604Syokota/*
240731604Syokota * Try to elicit a PnP ID as described in
240831604Syokota * Microsoft, Hayes: "Plug and Play External COM Device Specification,
240931604Syokota * rev 1.00", 1995.
241031604Syokota *
241131604Syokota * The routine does not fully implement the COM Enumerator as par Section
241231604Syokota * 2.1 of the document.  In particular, we don't have idle state in which
241331604Syokota * the driver software monitors the com port for dynamic connection or
241431604Syokota * removal of a device at the port, because `moused' simply quits if no
241531604Syokota * device is found.
241631604Syokota *
241731604Syokota * In addition, as PnP COM device enumeration procedure slightly has
241831604Syokota * changed since its first publication, devices which follow earlier
241931604Syokota * revisions of the above spec. may fail to respond if the rev 1.0
242031604Syokota * procedure is used. XXX
242131604Syokota */
242231604Syokotastatic int
242340255Syokotapnpwakeup1(void)
242431604Syokota{
242531604Syokota    struct timeval timeout;
242631604Syokota    fd_set fds;
242731604Syokota    int i;
242831604Syokota
242931604Syokota    /*
243031604Syokota     * This is the procedure described in rev 1.0 of PnP COM device spec.
243131604Syokota     * Unfortunately, some devices which comform to earlier revisions of
243231604Syokota     * the spec gets confused and do not return the ID string...
243331604Syokota     */
243440255Syokota    debug("PnP COM device rev 1.0 probe...");
243531604Syokota
243631604Syokota    /* port initialization (2.1.2) */
243731604Syokota    ioctl(rodent.mfd, TIOCMGET, &i);
243831604Syokota    i |= TIOCM_DTR;		/* DTR = 1 */
243931604Syokota    i &= ~TIOCM_RTS;		/* RTS = 0 */
244031604Syokota    ioctl(rodent.mfd, TIOCMSET, &i);
244140255Syokota    usleep(240000);
244231604Syokota
244340255Syokota    /*
244440255Syokota     * The PnP COM device spec. dictates that the mouse must set DSR
244540255Syokota     * in response to DTR (by hardware or by software) and that if DSR is
244640255Syokota     * not asserted, the host computer should think that there is no device
244741269Syokota     * at this serial port.  But some mice just don't do that...
244840255Syokota     */
244940255Syokota    ioctl(rodent.mfd, TIOCMGET, &i);
245040255Syokota    debug("modem status 0%o", i);
245140255Syokota    if ((i & TIOCM_DSR) == 0)
245240255Syokota	return FALSE;
245340255Syokota
245431604Syokota    /* port setup, 1st phase (2.1.3) */
245531604Syokota    setmousespeed(1200, 1200, (CS7 | CREAD | CLOCAL | HUPCL));
245631604Syokota    i = TIOCM_DTR | TIOCM_RTS;	/* DTR = 0, RTS = 0 */
245731604Syokota    ioctl(rodent.mfd, TIOCMBIC, &i);
245840255Syokota    usleep(240000);
245931604Syokota    i = TIOCM_DTR;		/* DTR = 1, RTS = 0 */
246031604Syokota    ioctl(rodent.mfd, TIOCMBIS, &i);
246140255Syokota    usleep(240000);
246231604Syokota
246331604Syokota    /* wait for response, 1st phase (2.1.4) */
246431604Syokota    i = FREAD;
246531604Syokota    ioctl(rodent.mfd, TIOCFLUSH, &i);
246631604Syokota    i = TIOCM_RTS;		/* DTR = 1, RTS = 1 */
246731604Syokota    ioctl(rodent.mfd, TIOCMBIS, &i);
246831604Syokota
246931604Syokota    /* try to read something */
247031604Syokota    FD_ZERO(&fds);
247131604Syokota    FD_SET(rodent.mfd, &fds);
247231604Syokota    timeout.tv_sec = 0;
247340255Syokota    timeout.tv_usec = 240000;
247440255Syokota    if (select(FD_SETSIZE, &fds, NULL, NULL, &timeout) > 0) {
247540255Syokota	debug("pnpwakeup1(): valid response in first phase.");
247640255Syokota	return TRUE;
247741269Syokota    }
247831604Syokota
247941269Syokota    /* port setup, 2nd phase (2.1.5) */
248041269Syokota    i = TIOCM_DTR | TIOCM_RTS;	/* DTR = 0, RTS = 0 */
248141269Syokota    ioctl(rodent.mfd, TIOCMBIC, &i);
248241269Syokota    usleep(240000);
248331604Syokota
248441269Syokota    /* wait for respose, 2nd phase (2.1.6) */
248541269Syokota    i = FREAD;
248641269Syokota    ioctl(rodent.mfd, TIOCFLUSH, &i);
248741269Syokota    i = TIOCM_DTR | TIOCM_RTS;	/* DTR = 1, RTS = 1 */
248841269Syokota    ioctl(rodent.mfd, TIOCMBIS, &i);
248931604Syokota
249041269Syokota    /* try to read something */
249141269Syokota    FD_ZERO(&fds);
249241269Syokota    FD_SET(rodent.mfd, &fds);
249341269Syokota    timeout.tv_sec = 0;
249441269Syokota    timeout.tv_usec = 240000;
249541269Syokota    if (select(FD_SETSIZE, &fds, NULL, NULL, &timeout) > 0) {
249641269Syokota	debug("pnpwakeup1(): valid response in second phase.");
249741269Syokota	return TRUE;
249831604Syokota    }
249941269Syokota
250040255Syokota    return FALSE;
250140255Syokota}
250240255Syokota
250340255Syokotastatic int
250440255Syokotapnpwakeup2(void)
250540255Syokota{
250640255Syokota    struct timeval timeout;
250740255Syokota    fd_set fds;
250840255Syokota    int i;
250940255Syokota
251031604Syokota    /*
251131604Syokota     * This is a simplified procedure; it simply toggles RTS.
251231604Syokota     */
251341269Syokota    debug("alternate probe...");
251431604Syokota
251531604Syokota    ioctl(rodent.mfd, TIOCMGET, &i);
251631604Syokota    i |= TIOCM_DTR;		/* DTR = 1 */
251731604Syokota    i &= ~TIOCM_RTS;		/* RTS = 0 */
251831604Syokota    ioctl(rodent.mfd, TIOCMSET, &i);
251940255Syokota    usleep(240000);
252031604Syokota
252131604Syokota    setmousespeed(1200, 1200, (CS7 | CREAD | CLOCAL | HUPCL));
252231604Syokota
252331604Syokota    /* wait for respose */
252431604Syokota    i = FREAD;
252531604Syokota    ioctl(rodent.mfd, TIOCFLUSH, &i);
252631604Syokota    i = TIOCM_DTR | TIOCM_RTS;	/* DTR = 1, RTS = 1 */
252731604Syokota    ioctl(rodent.mfd, TIOCMBIS, &i);
252831604Syokota
252931604Syokota    /* try to read something */
253031604Syokota    FD_ZERO(&fds);
253131604Syokota    FD_SET(rodent.mfd, &fds);
253231604Syokota    timeout.tv_sec = 0;
253340255Syokota    timeout.tv_usec = 240000;
253440255Syokota    if (select(FD_SETSIZE, &fds, NULL, NULL, &timeout) > 0) {
253540255Syokota	debug("pnpwakeup2(): valid response.");
253640255Syokota	return TRUE;
253740255Syokota    }
253841269Syokota
253940255Syokota    return FALSE;
254040255Syokota}
254131604Syokota
254240255Syokotastatic int
254340255Syokotapnpgets(char *buf)
254440255Syokota{
254540255Syokota    struct timeval timeout;
254640255Syokota    fd_set fds;
254740255Syokota    int begin;
254840255Syokota    int i;
254940255Syokota    char c;
255040255Syokota
255140255Syokota    if (!pnpwakeup1() && !pnpwakeup2()) {
255240255Syokota	/*
255340255Syokota	 * According to PnP spec, we should set DTR = 1 and RTS = 0 while
255440255Syokota	 * in idle state.  But, `moused' shall set DTR = RTS = 1 and proceed,
255540255Syokota	 * assuming there is something at the port even if it didn't
255640255Syokota	 * respond to the PnP enumeration procedure.
255740255Syokota	 */
255840255Syokota	i = TIOCM_DTR | TIOCM_RTS;		/* DTR = 1, RTS = 1 */
255940255Syokota	ioctl(rodent.mfd, TIOCMBIS, &i);
256040255Syokota	return 0;
256140255Syokota    }
256240255Syokota
256331604Syokota    /* collect PnP COM device ID (2.1.7) */
256440255Syokota    begin = -1;
256531604Syokota    i = 0;
256640255Syokota    usleep(240000);	/* the mouse must send `Begin ID' within 200msec */
256731604Syokota    while (read(rodent.mfd, &c, 1) == 1) {
256831604Syokota	/* we may see "M", or "M3..." before `Begin ID' */
256940255Syokota	buf[i++] = c;
257031604Syokota        if ((c == 0x08) || (c == 0x28)) {	/* Begin ID */
257140255Syokota	    debug("begin-id %02x", c);
257240255Syokota	    begin = i - 1;
257331604Syokota	    break;
257431604Syokota        }
257531604Syokota        debug("%c %02x", c, c);
257640255Syokota	if (i >= 256)
257740255Syokota	    break;
257831604Syokota    }
257940255Syokota    if (begin < 0) {
258031604Syokota	/* we haven't seen `Begin ID' in time... */
258131604Syokota	goto connect_idle;
258231604Syokota    }
258331604Syokota
258431604Syokota    ++c;			/* make it `End ID' */
258531604Syokota    for (;;) {
258631604Syokota        FD_ZERO(&fds);
258731604Syokota        FD_SET(rodent.mfd, &fds);
258831604Syokota        timeout.tv_sec = 0;
258940255Syokota        timeout.tv_usec = 240000;
259031604Syokota        if (select(FD_SETSIZE, &fds, NULL, NULL, &timeout) <= 0)
259131604Syokota	    break;
259231604Syokota
259331604Syokota	read(rodent.mfd, &buf[i], 1);
259431604Syokota        if (buf[i++] == c)	/* End ID */
259531604Syokota	    break;
259631604Syokota	if (i >= 256)
259731604Syokota	    break;
259831604Syokota    }
259940255Syokota    if (begin > 0) {
260040255Syokota	i -= begin;
260140255Syokota	bcopy(&buf[begin], &buf[0], i);
260240255Syokota    }
260331604Syokota    /* string may not be human readable... */
260440255Syokota    debug("len:%d, '%-*.*s'", i, i, i, buf);
260531604Syokota
260640255Syokota    if (buf[i - 1] == c)
260740255Syokota	return i;		/* a valid PnP string */
260840255Syokota
260931604Syokota    /*
261031604Syokota     * According to PnP spec, we should set DTR = 1 and RTS = 0 while
261140255Syokota     * in idle state.  But, `moused' shall leave the modem control lines
261240255Syokota     * as they are. See above.
261331604Syokota     */
261431604Syokotaconnect_idle:
261540255Syokota
261640255Syokota    /* we may still have something in the buffer */
261740255Syokota    return ((i > 0) ? i : 0);
261831604Syokota}
261931604Syokota
262031604Syokotastatic int
262131604Syokotapnpparse(pnpid_t *id, char *buf, int len)
262231604Syokota{
262331604Syokota    char s[3];
262431604Syokota    int offset;
262531604Syokota    int sum = 0;
262631604Syokota    int i, j;
262731604Syokota
262831604Syokota    id->revision = 0;
262931604Syokota    id->eisaid = NULL;
263031604Syokota    id->serial = NULL;
263131604Syokota    id->class = NULL;
263231604Syokota    id->compat = NULL;
263331604Syokota    id->description = NULL;
263431604Syokota    id->neisaid = 0;
263531604Syokota    id->nserial = 0;
263631604Syokota    id->nclass = 0;
263731604Syokota    id->ncompat = 0;
263831604Syokota    id->ndescription = 0;
263931604Syokota
264040255Syokota    if ((buf[0] != 0x28) && (buf[0] != 0x08)) {
264140255Syokota	/* non-PnP mice */
264240255Syokota	switch(buf[0]) {
264340255Syokota	default:
264440255Syokota	    return FALSE;
264540255Syokota	case 'M': /* Microsoft */
264640255Syokota	    id->eisaid = "PNP0F01";
264740255Syokota	    break;
264840255Syokota	case 'H': /* MouseSystems */
264940255Syokota	    id->eisaid = "PNP0F04";
265040255Syokota	    break;
265140255Syokota	}
265240255Syokota	id->neisaid = strlen(id->eisaid);
265340255Syokota	id->class = "MOUSE";
265440255Syokota	id->nclass = strlen(id->class);
265540255Syokota	debug("non-PnP mouse '%c'", buf[0]);
265640255Syokota	return TRUE;
265740255Syokota    }
265840255Syokota
265940255Syokota    /* PnP mice */
266031604Syokota    offset = 0x28 - buf[0];
266131604Syokota
266231604Syokota    /* calculate checksum */
266331604Syokota    for (i = 0; i < len - 3; ++i) {
266431604Syokota	sum += buf[i];
266531604Syokota	buf[i] += offset;
266631604Syokota    }
266731604Syokota    sum += buf[len - 1];
266831604Syokota    for (; i < len; ++i)
266931604Syokota	buf[i] += offset;
267031604Syokota    debug("PnP ID string: '%*.*s'", len, len, buf);
267131604Syokota
267231604Syokota    /* revision */
267331604Syokota    buf[1] -= offset;
267431604Syokota    buf[2] -= offset;
267531604Syokota    id->revision = ((buf[1] & 0x3f) << 6) | (buf[2] & 0x3f);
267631604Syokota    debug("PnP rev %d.%02d", id->revision / 100, id->revision % 100);
267731604Syokota
267831604Syokota    /* EISA vender and product ID */
267931604Syokota    id->eisaid = &buf[3];
268031604Syokota    id->neisaid = 7;
268131604Syokota
268231604Syokota    /* option strings */
268331604Syokota    i = 10;
268431604Syokota    if (buf[i] == '\\') {
268531604Syokota        /* device serial # */
268631604Syokota        for (j = ++i; i < len; ++i) {
268731604Syokota            if (buf[i] == '\\')
268831604Syokota		break;
268931604Syokota        }
269031604Syokota	if (i >= len)
269131604Syokota	    i -= 3;
269231604Syokota	if (i - j == 8) {
269331604Syokota            id->serial = &buf[j];
269431604Syokota            id->nserial = 8;
269518222Speter	}
269631604Syokota    }
269731604Syokota    if (buf[i] == '\\') {
269831604Syokota        /* PnP class */
269931604Syokota        for (j = ++i; i < len; ++i) {
270031604Syokota            if (buf[i] == '\\')
270131604Syokota		break;
270231604Syokota        }
270331604Syokota	if (i >= len)
270431604Syokota	    i -= 3;
270531604Syokota	if (i > j + 1) {
270631604Syokota            id->class = &buf[j];
270731604Syokota            id->nclass = i - j;
270831604Syokota        }
270931604Syokota    }
271031604Syokota    if (buf[i] == '\\') {
271131604Syokota	/* compatible driver */
271231604Syokota        for (j = ++i; i < len; ++i) {
271331604Syokota            if (buf[i] == '\\')
271431604Syokota		break;
271531604Syokota        }
271631604Syokota	/*
271731604Syokota	 * PnP COM spec prior to v0.96 allowed '*' in this field,
271831604Syokota	 * it's not allowed now; just igore it.
271931604Syokota	 */
272031604Syokota	if (buf[j] == '*')
272131604Syokota	    ++j;
272231604Syokota	if (i >= len)
272331604Syokota	    i -= 3;
272431604Syokota	if (i > j + 1) {
272531604Syokota            id->compat = &buf[j];
272631604Syokota            id->ncompat = i - j;
272731604Syokota        }
272831604Syokota    }
272931604Syokota    if (buf[i] == '\\') {
273031604Syokota	/* product description */
273131604Syokota        for (j = ++i; i < len; ++i) {
273231604Syokota            if (buf[i] == ';')
273331604Syokota		break;
273431604Syokota        }
273531604Syokota	if (i >= len)
273631604Syokota	    i -= 3;
273731604Syokota	if (i > j + 1) {
273831604Syokota            id->description = &buf[j];
273931604Syokota            id->ndescription = i - j;
274031604Syokota        }
274131604Syokota    }
274231604Syokota
274331604Syokota    /* checksum exists if there are any optional fields */
274431604Syokota    if ((id->nserial > 0) || (id->nclass > 0)
274531604Syokota	|| (id->ncompat > 0) || (id->ndescription > 0)) {
274631604Syokota        debug("PnP checksum: 0x%X", sum);
274731604Syokota        sprintf(s, "%02X", sum & 0x0ff);
274831604Syokota        if (strncmp(s, &buf[len - 3], 2) != 0) {
274931604Syokota#if 0
275031604Syokota            /*
275131604Syokota	     * I found some mice do not comply with the PnP COM device
275231604Syokota	     * spec regarding checksum... XXX
275331604Syokota	     */
275431604Syokota            logwarnx("PnP checksum error", 0);
275531604Syokota	    return FALSE;
275631604Syokota#endif
275731604Syokota        }
275831604Syokota    }
275931604Syokota
276031604Syokota    return TRUE;
276118222Speter}
276231604Syokota
276331604Syokotastatic symtab_t *
276431604Syokotapnpproto(pnpid_t *id)
276531604Syokota{
276631604Syokota    symtab_t *t;
276731604Syokota    int i, j;
276831604Syokota
276931604Syokota    if (id->nclass > 0)
277041271Syokota	if ( strncmp(id->class, "MOUSE", id->nclass) != 0 &&
277141271Syokota	     strncmp(id->class, "TABLET", id->nclass) != 0)
277231604Syokota	    /* this is not a mouse! */
277331604Syokota	    return NULL;
277431604Syokota
277531604Syokota    if (id->neisaid > 0) {
277631604Syokota        t = gettoken(pnpprod, id->eisaid, id->neisaid);
277731604Syokota	if (t->val != MOUSE_PROTO_UNKNOWN)
277831604Syokota            return t;
277931604Syokota    }
278031604Syokota
278131604Syokota    /*
278231604Syokota     * The 'Compatible drivers' field may contain more than one
278331604Syokota     * ID separated by ','.
278431604Syokota     */
278531604Syokota    if (id->ncompat <= 0)
278631604Syokota	return NULL;
278731604Syokota    for (i = 0; i < id->ncompat; ++i) {
278831604Syokota        for (j = i; id->compat[i] != ','; ++i)
278931604Syokota            if (i >= id->ncompat)
279031604Syokota		break;
279131604Syokota        if (i > j) {
279231604Syokota            t = gettoken(pnpprod, id->compat + j, i - j);
279331604Syokota	    if (t->val != MOUSE_PROTO_UNKNOWN)
279431604Syokota                return t;
279531604Syokota	}
279631604Syokota    }
279731604Syokota
279831604Syokota    return NULL;
279931604Syokota}
280031604Syokota
280131604Syokota/* name/val mapping */
280231604Syokota
280331604Syokotastatic symtab_t *
280431604Syokotagettoken(symtab_t *tab, char *s, int len)
280531604Syokota{
280631604Syokota    int i;
280731604Syokota
280831604Syokota    for (i = 0; tab[i].name != NULL; ++i) {
280931604Syokota	if (strncmp(tab[i].name, s, len) == 0)
281031604Syokota	    break;
281131604Syokota    }
281231604Syokota    return &tab[i];
281331604Syokota}
281431604Syokota
281531604Syokotastatic char *
281631604Syokotagettokenname(symtab_t *tab, int val)
281731604Syokota{
281831604Syokota    int i;
281931604Syokota
282031604Syokota    for (i = 0; tab[i].name != NULL; ++i) {
282131604Syokota	if (tab[i].val == val)
282231604Syokota	    return tab[i].name;
282331604Syokota    }
282431604Syokota    return NULL;
282531604Syokota}
282636991Sahasty
282741271Syokota
282841271Syokota/*
282941271Syokota * code to read from the Genius Kidspad tablet.
283041271Syokota
283141271SyokotaThe tablet responds to the COM PnP protocol 1.0 with EISA-ID KYE0005,
283241271Syokotaand to pre-pnp probes (RTS toggle) with 'T' (tablet ?)
283341271Syokota9600, 8 bit, parity odd.
283441271Syokota
283541271SyokotaThe tablet puts out 5 bytes. b0 (mask 0xb8, value 0xb8) contains
283641271Syokotathe proximity, tip and button info:
283741271Syokota   (byte0 & 0x1)	true = tip pressed
283841271Syokota   (byte0 & 0x2)	true = button pressed
283941271Syokota   (byte0 & 0x40)	false = pen in proximity of tablet.
284041271Syokota
284141271SyokotaThe next 4 bytes are used for coordinates xl, xh, yl, yh (7 bits valid).
284241271Syokota
284341271SyokotaOnly absolute coordinates are returned, so we use the following approach:
284441271Syokotawe store the last coordinates sent when the pen went out of the tablet,
284541271Syokota
284641271Syokota
284741271Syokota *
284841271Syokota */
284941271Syokota
285041271Syokotatypedef enum {
285141271Syokota    S_IDLE, S_PROXY, S_FIRST, S_DOWN, S_UP
285241271Syokota} k_status ;
285341271Syokota
285441271Syokotastatic int
285541271Syokotakidspad(u_char rxc, mousestatus_t *act)
285641271Syokota{
285795629Siedowse    static int buf[5];
285841271Syokota    static int buflen = 0, b_prev = 0 , x_prev = -1, y_prev = -1 ;
285941271Syokota    static k_status status = S_IDLE ;
286041271Syokota    static struct timeval old, now ;
286141271Syokota
286278737Sdd    int x, y ;
286341271Syokota
286441271Syokota    if (buflen > 0 && (rxc & 0x80) ) {
286541271Syokota	fprintf(stderr, "invalid code %d 0x%x\n", buflen, rxc);
286641271Syokota	buflen = 0 ;
286741271Syokota    }
286841271Syokota    if (buflen == 0 && (rxc & 0xb8) != 0xb8 ) {
286941271Syokota	fprintf(stderr, "invalid code 0 0x%x\n", rxc);
287041271Syokota	return 0 ; /* invalid code, no action */
287141271Syokota    }
287241271Syokota    buf[buflen++] = rxc ;
287341271Syokota    if (buflen < 5)
287441271Syokota	return 0 ;
287541271Syokota
287641271Syokota    buflen = 0 ; /* for next time... */
287741271Syokota
287841271Syokota    x = buf[1]+128*(buf[2] - 7) ;
287941271Syokota    if (x < 0) x = 0 ;
288041271Syokota    y = 28*128 - (buf[3] + 128* (buf[4] - 7)) ;
288141271Syokota    if (y < 0) y = 0 ;
288241271Syokota
288341271Syokota    x /= 8 ;
288441271Syokota    y /= 8 ;
288541271Syokota
288641271Syokota    act->flags = 0 ;
288741271Syokota    act->obutton = act->button ;
288841271Syokota    act->dx = act->dy = act->dz = 0 ;
288941271Syokota    gettimeofday(&now, NULL);
289041271Syokota    if ( buf[0] & 0x40 ) /* pen went out of reach */
289141271Syokota	status = S_IDLE ;
289241271Syokota    else if (status == S_IDLE) { /* pen is newly near the tablet */
289341271Syokota	act->flags |= MOUSE_POSCHANGED ; /* force update */
289441271Syokota	status = S_PROXY ;
289541271Syokota	x_prev = x ;
289641271Syokota	y_prev = y ;
289741271Syokota    }
289841271Syokota    old = now ;
289941271Syokota    act->dx = x - x_prev ;
290041271Syokota    act->dy = y - y_prev ;
290141271Syokota    if (act->dx || act->dy)
290241271Syokota	act->flags |= MOUSE_POSCHANGED ;
290341271Syokota    x_prev = x ;
290441271Syokota    y_prev = y ;
290541271Syokota    if (b_prev != 0 && b_prev != buf[0]) { /* possibly record button change */
290641271Syokota	act->button = 0 ;
290741271Syokota	if ( buf[0] & 0x01 ) /* tip pressed */
290841271Syokota	    act->button |= MOUSE_BUTTON1DOWN ;
290941271Syokota	if ( buf[0] & 0x02 ) /* button pressed */
291041271Syokota	    act->button |= MOUSE_BUTTON2DOWN ;
291141271Syokota	act->flags |= MOUSE_BUTTONSCHANGED ;
291241271Syokota    }
291341271Syokota    b_prev = buf[0] ;
291441271Syokota    return act->flags ;
291541271Syokota}
291641271Syokota
291736991Sahastystatic void
291836991Sahastymremote_serversetup()
291936991Sahasty{
292036991Sahasty    struct sockaddr_un ad;
292136991Sahasty
292236991Sahasty    /* Open a UNIX domain stream socket to listen for mouse remote clients */
292336991Sahasty    unlink(_PATH_MOUSEREMOTE);
292436991Sahasty
292536991Sahasty    if ( (rodent.mremsfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
292636991Sahasty	logerrx(1, "unable to create unix domain socket %s",_PATH_MOUSEREMOTE);
292736991Sahasty
292836991Sahasty    umask(0111);
292936991Sahasty
293036991Sahasty    bzero(&ad, sizeof(ad));
293136991Sahasty    ad.sun_family = AF_UNIX;
293236991Sahasty    strcpy(ad.sun_path, _PATH_MOUSEREMOTE);
293336991Sahasty#ifndef SUN_LEN
293436991Sahasty#define SUN_LEN(unp) ( ((char *)(unp)->sun_path - (char *)(unp)) + \
293536991Sahasty                       strlen((unp)->path) )
293636991Sahasty#endif
293736991Sahasty    if (bind(rodent.mremsfd, (struct sockaddr *) &ad, SUN_LEN(&ad)) < 0)
293836991Sahasty	logerrx(1, "unable to bind unix domain socket %s", _PATH_MOUSEREMOTE);
293936991Sahasty
294036991Sahasty    listen(rodent.mremsfd, 1);
294136991Sahasty}
294236991Sahasty
294336991Sahastystatic void
294436991Sahastymremote_clientchg(int add)
294536991Sahasty{
294636991Sahasty    struct sockaddr_un ad;
294736991Sahasty    int ad_len, fd;
294836991Sahasty
294936991Sahasty    if (rodent.rtype != MOUSE_PROTO_X10MOUSEREM)
295036991Sahasty	return;
295136991Sahasty
295236991Sahasty    if ( add ) {
295336991Sahasty	/*  Accept client connection, if we don't already have one  */
295436991Sahasty	ad_len = sizeof(ad);
295536991Sahasty	fd = accept(rodent.mremsfd, (struct sockaddr *) &ad, &ad_len);
295636991Sahasty	if (fd < 0)
295736991Sahasty	    logwarnx("failed accept on mouse remote socket");
295836991Sahasty
295936991Sahasty	if ( rodent.mremcfd < 0 ) {
296036991Sahasty	    rodent.mremcfd = fd;
296136991Sahasty	    debug("remote client connect...accepted");
296236991Sahasty	}
296336991Sahasty	else {
296436991Sahasty	    close(fd);
296536991Sahasty	    debug("another remote client connect...disconnected");
296636991Sahasty	}
296736991Sahasty    }
296836991Sahasty    else {
296936991Sahasty	/* Client disconnected */
297036991Sahasty	debug("remote client disconnected");
297136991Sahasty	close( rodent.mremcfd );
297236991Sahasty	rodent.mremcfd = -1;
297336991Sahasty    }
297436991Sahasty}
297536991Sahasty
297636991Sahasty
2977