summaryrefslogtreecommitdiff
path: root/usr.sbin/moused/moused.c
blob: d04507e0a2838ccc07a33cfd2c57c8104f89f96e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
/*
 * Copyright (c) 2000 Jean-Baptiste Marchand, Julien Montagne and Jerome Verdon
 * 
 * Copyright (c) 1998 by Kazutaka Yokota
 *
 * Copyright (c) 1995 Michael Smith
 * 
 * Copyright (c) 1993 by David Dawes <dawes@xfree86.org>
 *
 * Copyright (c) 1990,91 by Thomas Roell, Dinkelscherben, Germany.
 *
 * All rights reserved.
 *
 * Most of this code was taken from the FreeBSD moused daemon, written by
 * Michael Smith. The FreeBSD moused daemon already contained code from the 
 * Xfree Project, written by David Dawes and Thomas Roell and Kazutaka Yokota.
 *
 * Adaptation to OpenBSD was done by Jean-Baptiste Marchand, Julien Montagne
 * and Jerome Verdon.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by
 *      David Dawes, Jean-Baptiste Marchand, Julien Montagne, Thomas Roell,
 *      Michael Smith, Jerome Verdon and Kazutaka Yokota.
 * 4. The name authors may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *
 */

#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/tty.h>
#include <machine/pcvt_ioctl.h>

#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <setjmp.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <syslog.h>
#include <varargs.h>

#include "moused.h"

extern char *optarg;
extern int optind;
extern char *__progname;

int debug = 0;
int nodaemon = FALSE;
int background = FALSE;
int identify = ID_NONE;
char *pidfile = "/var/run/moused.pid";
static jmp_buf env;

/*
 * Most of the structures are from the Xfree Project
 */

/* Buttons status (for multiple click detection) */

static struct {
    int count;		/* 0: up, 1: single click, 2: double click,... */
    struct timeval tv;	/* timestamp on the last `up' event */
} buttonstate[MOUSE_MAXBUTTON];

/* Mouse physical interfaces */

static symtab_t mouse_ifs[] = {
    { "serial",		MOUSE_IF_SERIAL },
    { "bus",		MOUSE_IF_BUS },
    { "inport",		MOUSE_IF_INPORT },
    { "ps/2",		MOUSE_IF_PS2 },
    { "usb",		MOUSE_IF_USB },
    { NULL,		MOUSE_IF_UNKNOWN }
};

/* Mouse model names */

static symtab_t	mouse_models[] = {
    { "NetScroll",	MOUSE_MODEL_NETSCROLL },
    { "NetMouse",	MOUSE_MODEL_NET },
    { "GlidePoint",	MOUSE_MODEL_GLIDEPOINT },
    { "ThinkingMouse",	MOUSE_MODEL_THINK },
    { "IntelliMouse",	MOUSE_MODEL_INTELLI },
    { "EasyScroll",	MOUSE_MODEL_EASYSCROLL },
    { "MouseMan+",	MOUSE_MODEL_MOUSEMANPLUS },
    { "Kidspad",	MOUSE_MODEL_KIDSPAD },
    { "VersaPad",	MOUSE_MODEL_VERSAPAD },
    { "generic",	MOUSE_MODEL_GENERIC },
    { NULL, 		MOUSE_MODEL_UNKNOWN },
};

/* 
 *  Cflags of each mouse protocol, ordered by P_XXX
 */

static unsigned short mousecflags[] =
{
    (CS7	           | CREAD | CLOCAL | HUPCL ),	/* MicroSoft */
    (CS8 | CSTOPB	   | CREAD | CLOCAL | HUPCL ),	/* MouseSystems */
    (CS8 | CSTOPB	   | CREAD | CLOCAL | HUPCL ),	/* Logitech */
    (CS8 | PARENB | PARODD | CREAD | CLOCAL | HUPCL ),	/* MMSeries */
    (CS7		   | CREAD | CLOCAL | HUPCL ),	/* MouseMan */
    0,							/* Bus */
    0,							/* InPort */
    0,							/* PS/2 */
    (CS8		   | CREAD | CLOCAL | HUPCL ),	/* MM HitTablet */
    (CS7	           | CREAD | CLOCAL | HUPCL ),	/* GlidePoint */
    (CS7                   | CREAD | CLOCAL | HUPCL ),	/* IntelliMouse */
    (CS7                   | CREAD | CLOCAL | HUPCL ),	/* Thinking Mouse */
};

/* Array ordered by P_XXX giving protocol properties */

static unsigned char proto[][7] = {
    /*  hd_mask hd_id   dp_mask dp_id   bytes b4_mask b4_id */
    { 	0x40,	0x40,	0x40,	0x00,	3,   ~0x23,  0x00 }, /* MicroSoft */
    {	0xf8,	0x80,	0x00,	0x00,	5,    0x00,  0xff }, /* MouseSystems */
    {	0xe0,	0x80,	0x80,	0x00,	3,    0x00,  0xff }, /* Logitech */
    {	0xe0,	0x80,	0x80,	0x00,	3,    0x00,  0xff }, /* MMSeries */
    { 	0x40,	0x40,	0x40,	0x00,	3,   ~0x33,  0x00 }, /* MouseMan */
    {	0xf8,	0x80,	0x00,	0x00,	5,    0x00,  0xff }, /* Bus */
    {	0xf8,	0x80,	0x00,	0x00,	5,    0x00,  0xff }, /* InPort */
    {	0xc0,	0x00,	0x00,	0x00,	3,    0x00,  0xff }, /* PS/2 mouse */
    {	0xe0,	0x80,	0x80,	0x00,	3,    0x00,  0xff }, /* MM HitTablet */
    { 	0x40,	0x40,	0x40,	0x00,	3,   ~0x33,  0x00 }, /* GlidePoint */
    { 	0x40,	0x40,	0x40,	0x00,	3,   ~0x3f,  0x00 }, /* IntelliMouse */
    { 	0x40,	0x40,	0x40,	0x00,	3,   ~0x33,  0x00 }, /* ThinkingMouse */
};

/* 
 * array ordered by P_XXX (mouse protocols) giving the protocol corresponding
 * to the name of a mouse 
 */

static char *mouse_names[] = {
    "microsoft",
    "mousesystems",
    "logitech",
    "mmseries",
    "mouseman",
    "busmouse",
    "inportmouse",
    "ps/2",
    "mmhitab",
    "glidepoint",
    "intellimouse",
    "thinkingmouse",
    NULL
};

/* protocol currently used */

static unsigned char cur_proto[7];

mouse_t mouse = {
    flags : 0, 
    portname : NULL,
    proto : P_UNKNOWN,
    baudrate : 1200, 
    old_baudrate : 1200,
    rate : MOUSE_RATE_UNKNOWN,
    resolution : MOUSE_RES_UNKNOWN, 
    zmap: 0,
    wmode: 0,
    mfd : -1,
    clickthreshold : 500,	/* 0.5 sec */
};

/* PnP EISA/product IDs */
static symtab_t pnpprod[] = {
    { "KML0001",P_THINKING,MOUSE_MODEL_THINK},	/* Kensignton ThinkingMouse */
    { "MSH0001",P_IMSERIAL,MOUSE_MODEL_INTELLI},/* MS IntelliMouse */
    { "MSH0004",P_IMSERIAL,MOUSE_MODEL_INTELLI},/* MS IntelliMouse TrackBall */
    { "KYEEZ00",P_MS,MOUSE_MODEL_GENERIC}, /* Genius EZScroll */
    { "KYE0001",P_MS,MOUSE_MODEL_GENERIC}, /* Genius PnP Mouse */
    { "KYE0003",P_IMSERIAL,MOUSE_MODEL_NET},/* Genius NetMouse */
    { "LGI800C",P_IMSERIAL,MOUSE_MODEL_MOUSEMANPLUS}, /* Logitech MouseMan (4 button model) */
    { "LGI8050",P_IMSERIAL,MOUSE_MODEL_MOUSEMANPLUS}, /* Logitech MouseMan+ */
    { "LGI8051",P_IMSERIAL,MOUSE_MODEL_MOUSEMANPLUS}, /* Logitech FirstMouse+ */
    { "LGI8001",P_LOGIMAN,MOUSE_MODEL_GENERIC},	/* Logitech serial */

    { "PNP0F00",P_BM,MOUSE_MODEL_GENERIC },	/* MS bus */
    { "PNP0F01",P_MS,MOUSE_MODEL_GENERIC },	/* MS serial */
    { "PNP0F02",P_BM,MOUSE_MODEL_GENERIC },	/* MS InPort */
    { "PNP0F03",P_PS2,MOUSE_MODEL_GENERIC },	/* MS PS/2 */
    /*
     * EzScroll returns PNP0F04 in the compatible device field; but it
     * doesn't look compatible... XXX
     */
    { "PNP0F04",P_MSC,MOUSE_MODEL_GENERIC },	/* MouseSystems */ 
    { "PNP0F05",P_MSC,MOUSE_MODEL_GENERIC },	/* MouseSystems */ 
    { "PNP0F08",P_LOGIMAN,MOUSE_MODEL_GENERIC },/* Logitech serial */
    { "PNP0F09",P_MS ,MOUSE_MODEL_GENERIC },	/* MS BallPoint serial */
    { "PNP0F0A",P_MS ,MOUSE_MODEL_GENERIC },	/* MS PnP serial */
    { "PNP0F0B",P_MS ,MOUSE_MODEL_GENERIC },	/* MS PnP BallPoint serial */
    { "PNP0F0C",P_MS ,MOUSE_MODEL_GENERIC },	/* MS serial comatible */
    { "PNP0F0D",P_BM,MOUSE_MODEL_GENERIC  },	/* MS InPort comatible */
    { "PNP0F0E",P_PS2,MOUSE_MODEL_GENERIC  },	/* MS PS/2 comatible */
    { "PNP0F0F",P_MS,MOUSE_MODEL_GENERIC  },	/* MS BallPoint comatible */
    { "PNP0F11",P_BM,MOUSE_MODEL_GENERIC },	/* MS bus comatible */
    { "PNP0F12",P_PS2,MOUSE_MODEL_GENERIC  },	/* Logitech PS/2 */
    { "PNP0F13",P_PS2,MOUSE_MODEL_GENERIC  },	/* PS/2 */
    { "PNP0F15",P_BM,MOUSE_MODEL_GENERIC  },	/* Logitech bus */ 
    { "PNP0F17",P_LOGIMAN,MOUSE_MODEL_GENERIC  },/* Logitech serial compat */
    { "PNP0F18",P_BM,MOUSE_MODEL_GENERIC  },	/* Logitech bus compatible */
    { "PNP0F19",P_PS2,MOUSE_MODEL_GENERIC  },	/* Logitech PS/2 compatible */
    { NULL,		-1 },
};

/*
 * XXX Functions
 */

static char *
skipspace(char *s)
{
    while(isspace(*s))
	++s;
    return s;
}

static char *
gettokenname(symtab_t *tab, int val)
{
    int i;

    for (i = 0; tab[i].name != NULL; ++i) {
	if (tab[i].val == val)
	    return tab[i].name;
    }
    return NULL;
}

static symtab_t *
gettoken(symtab_t *tab, char *s, int len)
{
    int i;

    for (i = 0; tab[i].name != NULL; ++i) {
	if (strncmp(tab[i].name, s, len) == 0)
	    break;
    }
    return &tab[i];
}

static char *
mouse_name(int type)
{
    return ((type == P_UNKNOWN) 
	|| (type > sizeof(mouse_names)/sizeof(mouse_names[0]) - 1))
	? "unknown" : mouse_names[type];
}

static char *
mouse_model(int model)
{
    char *s;

    s = gettokenname(mouse_models, model);
    return (s == NULL) ? "unknown" : s;
}

/* Fills the hardware info in the main structure */

static void
mouse_fill_hwinfo(void)
{
	/* default settings */

	mouse.hw.iftype = MOUSE_IF_UNKNOWN;
	mouse.hw.type = MOUSE_TYPE_MOUSE;	
	if (mouse.hw.model == 0) { /* If no type has been given */
		mouse.hw.model = MOUSE_MODEL_GENERIC;
	}

	if (strcmp(mouse.portname,PMS_DEV) == 0) {
		mouse.hw.iftype = MOUSE_IF_PS2;
	}

	if (strcmp(mouse.portname,LMS_DEV) == 0) {
		mouse.hw.iftype = MOUSE_IF_BUS;
	}

	if (strcmp(mouse.portname,MMS_DEV) == 0) {
		mouse.hw.iftype = MOUSE_IF_INPORT;
	}

	/* serial device begins with /dev/cua0 (9 caracters) */
	if (strncmp(mouse.portname,SERIAL_DEV,9) == 0) {
		mouse.hw.iftype = MOUSE_IF_SERIAL;
	}

}

/* Fills the mode structure in the main structure */

static void 
mouse_fill_mousemode(void)
{
	/* default settings */
	
	mouse.mode.protocol = P_UNKNOWN; 
	mouse.mode.accelfactor = 0; /* no accel */

	if (strcmp(mouse.portname,PMS_DEV) == 0) {
		mouse.mode.protocol = P_PS2;
		if (mouse.hw.model == MOUSE_MODEL_INTELLI)
			mouse.mode.packetsize = MOUSE_INTELLI_PACKETSIZE;
		else
			mouse.mode.packetsize = MOUSE_PS2_PACKETSIZE;
		mouse.mode.syncmask[0] = MOUSE_PS2_SYNCMASK;
		mouse.mode.syncmask[1] = MOUSE_PS2_SYNC;
	}
			
	if (strcmp(mouse.portname,LMS_DEV) == 0) {
		mouse.mode.protocol = P_BM;
		mouse.mode.packetsize = MOUSE_MSC_PACKETSIZE;
		mouse.mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
		mouse.mode.syncmask[1] = MOUSE_MSC_SYNC;
	}

	if (strcmp(mouse.portname,MMS_DEV) == 0) {
		mouse.hw.iftype = P_INPORT;
		mouse.mode.packetsize = MOUSE_MSC_PACKETSIZE;
		mouse.mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
		mouse.mode.syncmask[1] = MOUSE_MSC_SYNC;
	}
	if (mouse.proto != -1)
		mouse.mode.protocol = mouse.proto;
	
	/* resolution */
	
	switch (mouse.resolution) {
	case MOUSE_RES_HIGH:
	case MOUSE_RES_MEDIUMHIGH:
	case MOUSE_RES_MEDIUMLOW:
	case MOUSE_RES_LOW:
		mouse.mode.resolution = mouse.resolution;
		break;
	case MOUSE_RES_DEFAULT:
	case MOUSE_RES_UNKNOWN:
		/* default to low resolution */
		mouse.mode.resolution = MOUSE_RES_LOW;
		break;
	default:
		if (mouse.resolution >= 200)
			mouse.mode.resolution = MOUSE_RES_HIGH;
		else if (mouse.resolution >= 100)
			mouse.mode.resolution = MOUSE_RES_MEDIUMHIGH;
		else if (mouse.resolution >= 50)
			mouse.mode.resolution = MOUSE_RES_MEDIUMLOW;
		else mouse.mode.resolution = MOUSE_RES_LOW;
	}
		
	/* sample rate */

	if (mouse.rate != MOUSE_RATE_UNKNOWN) {
		if (mouse.rate >= 200)
			mouse.mode.rate = MOUSE_RATE_VERY_HIGH;
		else if (mouse.rate >= 100)
			mouse.mode.rate = MOUSE_RATE_HIGH;
		else if (mouse.rate >= 80)
			mouse.mode.rate = MOUSE_RATE_MEDIUM_HIGH;
		else if (mouse.rate >= 60)
			mouse.mode.rate = MOUSE_RATE_MEDIUM_LOW;
		else if (mouse.rate >= 40)
			mouse.mode.rate = MOUSE_RATE_LOW;
		else 
			mouse.mode.rate = MOUSE_RATE_VERY_LOW;
	}
}


static void
freedev(int sig)
{ 
  /* 
   *  close the device, when a USR1 signal is received.
   *  Tipically used by an X server so it can open the device for it's 
   *  own purpose. 
   */
  close(mouse.mfd);
  sigpause(0);
}

static void
opendev(int sig)
{
    longjmp(env, 1);
}

static void 
cleanup(int sig)
{
    char moused_flag = MOUSED_OFF;
  
    ioctl(mouse.cfd, PCVT_MOUSED, &moused_flag);
    exit(0);
}

/*
 * Begin of functions from the Xfree Project
 */

/* 
 * Functions below come from the Xfree Project and are derived from a two files
 * of Xfree86 3.3.6 with the following CVS tags :
 $XFree86: xc/programs/Xserver/hw/xfree86/common/xf86_Mouse.c,v 3.21.2.24 
 1999/12/11 19:00:42 hohndel Exp $ 
 and
 $XFree86: xc/programs/Xserver/hw/xfree86/common/xf86_PnPMouse.c,v 1.1.2.6 
 1999/07/29 09:22:51 hohndel Exp $ 
 */

void SetMouseSpeed(int old, int new, unsigned int cflag)
{
	struct termios tty;
	char *c;

	if (mouse.hw.iftype != MOUSE_IF_SERIAL)
		return;

	if (tcgetattr(mouse.mfd, &tty) < 0)
	{
		ErrorF("Warning: %s unable to get status of mouse fd (%s)\n",
		       mouse.portname, strerror(errno));
		return;
	}

	/* this will query the initial baudrate only once */
	if (mouse.old_baudrate < 0) { 
	   switch (cfgetispeed(&tty)) 
	      {
	      case B9600: 
		 mouse.old_baudrate = 9600;
		 break;
	      case B4800: 
		 mouse.old_baudrate = 4800;
		 break;
	      case B2400: 
		 mouse.old_baudrate = 2400;
		 break;
	      case B1200: 
	      default:
		 mouse.old_baudrate = 1200;
		 break;
	      }
	}

	tty.c_iflag = IGNBRK | IGNPAR;
	tty.c_oflag = 0;
	tty.c_lflag = 0;
	tty.c_cflag = (tcflag_t)cflag;
	tty.c_cc[VTIME] = 0;
	tty.c_cc[VMIN] = 1;

	switch (old)
	{
	case 9600:
		cfsetispeed(&tty, B9600);
		cfsetospeed(&tty, B9600);
		break;
	case 4800:
		cfsetispeed(&tty, B4800);
		cfsetospeed(&tty, B4800);
		break;
	case 2400:
		cfsetispeed(&tty, B2400);
		cfsetospeed(&tty, B2400);
		break;
	case 1200:
	default:
		cfsetispeed(&tty, B1200);
		cfsetospeed(&tty, B1200);
	}

	if (tcsetattr(mouse.mfd, TCSADRAIN, &tty) < 0) {
		printf("Unable to get mouse status. Exiting...\n");
		exit(1);
	}
	
	switch (new)
	{
	case 9600:
		c = "*q";
		cfsetispeed(&tty, B9600);
		cfsetospeed(&tty, B9600);
		break;
	case 4800:
		c = "*p";
		cfsetispeed(&tty, B4800);
		cfsetospeed(&tty, B4800);
		break;
	case 2400:
		c = "*o";
		cfsetispeed(&tty, B2400);
		cfsetospeed(&tty, B2400);
		break;
	case 1200:
	default:
		c = "*n";
		cfsetispeed(&tty, B1200);
		cfsetospeed(&tty, B1200);
	}

	if (mouse.proto == P_LOGIMAN || mouse.proto == P_LOGI)
	{
		if (write(mouse.mfd, c, 2) != 2) {
			printf("Unable to write to mouse. Exiting...\n");
			exit(1);
		}
	}
	usleep(100000);

	if (tcsetattr(mouse.mfd, TCSADRAIN, &tty) < 0) {
		printf("Unable to get mouse status. Exiting...\n");
		exit(1);
	}
}

int
FlushInput(int fd)
{
	fd_set fds;
	struct timeval timeout;
	char c[4];

	if (tcflush(fd, TCIFLUSH) == 0)
		return 0;

	timeout.tv_sec = 0;
	timeout.tv_usec = 0;
	FD_ZERO(&fds);
	FD_SET(fd, &fds);
	while (select(FD_SETSIZE, &fds, NULL, NULL, &timeout) > 0) {
		read(fd, &c, sizeof(c));
		FD_ZERO(&fds);
		FD_SET(fd, &fds);
	}
	return 0;
}

/*
 * Try to elicit a PnP ID as described in 
 * Microsoft, Hayes: "Plug and Play External COM Device Specification, 
 * rev 1.00", 1995.
 *
 * The routine does not fully implement the COM Enumerator as par Section
 * 2.1 of the document.  In particular, we don't have idle state in which
 * the driver software monitors the com port for dynamic connection or 
 * removal of a device at the port, because `moused' simply quits if no 
 * device is found.
 *
 * In addition, as PnP COM device enumeration procedure slightly has 
 * changed since its first publication, devices which follow earlier
 * revisions of the above spec. may fail to respond if the rev 1.0 
 * procedure is used. XXX
 */

static int
pnpgets(int mouse_fd, char *buf)
{
    struct timeval timeout;
    fd_set fds;
    int i;
    char c;

#if 0
    /* 
     * This is the procedure described in rev 1.0 of PnP COM device spec.
     * Unfortunately, some devices which comform to earlier revisions of
     * the spec gets confused and do not return the ID string...
     */

    /* port initialization (2.1.2) */
    ioctl(mouse_fd, TIOCMGET, &i);
    i |= TIOCM_DTR;		/* DTR = 1 */
    i &= ~TIOCM_RTS;		/* RTS = 0 */
    ioctl(mouse_fd, TIOCMSET, &i);
    usleep(200000);
    if ((ioctl(mouse_fd, TIOCMGET, &i) == -1) || ((i & TIOCM_DSR) == 0))
	goto disconnect_idle;

    /* port setup, 1st phase (2.1.3) */
    SetMouseSpeed(1200, 1200, (CS7 | CREAD | CLOCAL | HUPCL));
    i = TIOCM_DTR | TIOCM_RTS;	/* DTR = 0, RTS = 0 */
    ioctl(mouse_fd, TIOCMBIC, &i);
    usleep(200000);
    i = TIOCM_DTR;		/* DTR = 1, RTS = 0 */
    ioctl(mouse_fd, TIOCMBIS, &i);
    usleep(200000);

    /* wait for response, 1st phase (2.1.4) */
    FlushInput(mouse_fd);
    i = TIOCM_RTS;		/* DTR = 1, RTS = 1 */
    ioctl(mouse_fd, TIOCMBIS, &i);

    /* try to read something */
    FD_ZERO(&fds);
    FD_SET(mouse_fd, &fds);
    timeout.tv_sec = 0;
    timeout.tv_usec = 200000;
    if (select(FD_SETSIZE, &fds, NULL, NULL, &timeout) <= 0) {

	/* port setup, 2nd phase (2.1.5) */
        i = TIOCM_DTR | TIOCM_RTS;	/* DTR = 0, RTS = 0 */
        ioctl(mouse_fd, TIOCMBIC, &i);
        usleep(200000);

	/* wait for respose, 2nd phase (2.1.6) */
	FlushInput(mouse_fd);
        i = TIOCM_DTR | TIOCM_RTS;	/* DTR = 1, RTS = 1 */
        ioctl(mouse_fd, TIOCMBIS, &i);

        /* try to read something */
        FD_ZERO(&fds);
        FD_SET(mouse_fd, &fds);
        timeout.tv_sec = 0;
        timeout.tv_usec = 200000;
        if (select(FD_SETSIZE, &fds, NULL, NULL, &timeout) <= 0)
	    goto connect_idle;
    }
#else
    
    /*
     * This is a simplified procedure; it simply toggles RTS.
     */
    SetMouseSpeed(1200, 1200, (CS7 | CREAD | CLOCAL | HUPCL));

    ioctl(mouse_fd, TIOCMGET, &i);
    i |= TIOCM_DTR;		/* DTR = 1 */
    i &= ~TIOCM_RTS;		/* RTS = 0 */
    ioctl(mouse_fd, TIOCMSET, &i);
    usleep(200000);

    /* wait for response */
    FlushInput(mouse_fd);
    i = TIOCM_DTR | TIOCM_RTS;	/* DTR = 1, RTS = 1 */
    ioctl(mouse_fd, TIOCMBIS, &i);

    /* try to read something */
    FD_ZERO(&fds);
    FD_SET(mouse_fd, &fds);
    timeout.tv_sec = 0;
    timeout.tv_usec = 200000;
    if (select(FD_SETSIZE, &fds, NULL, NULL, &timeout) <= 0)
        goto connect_idle;
#endif

    /* collect PnP COM device ID (2.1.7) */
    i = 0;
    usleep(200000);	/* the mouse must send `Begin ID' within 200msec */
    while (read(mouse_fd, &c, 1) == 1) {
	/* we may see "M", or "M3..." before `Begin ID' */
        if ((c == 0x08) || (c == 0x28)) {	/* Begin ID */
	    buf[i++] = c;
	    break;
        }
    }
    if (i <= 0) {
	/* we haven't seen `Begin ID' in time... */
	goto connect_idle;
    }

    ++c;			/* make it `End ID' */
    for (;;) {
        FD_ZERO(&fds);
        FD_SET(mouse_fd, &fds);
        timeout.tv_sec = 0;
        timeout.tv_usec = 200000;
        if (select(FD_SETSIZE, &fds, NULL, NULL, &timeout) <= 0)
	    break;

	read(mouse_fd, &buf[i], 1);
        if (buf[i++] == c)	/* End ID */
	    break;
	if (i >= 256)
	    break;
    }
    if (buf[i - 1] != c)
	goto connect_idle;
    return i;
    
#if 0
    /*
     * According to PnP spec, we should set DTR = 1 and RTS = 0 while 
     * in idle state.  But, `moused' shall set DTR = RTS = 1 and proceed, 
     * assuming there is something at the port even if it didn't 
     * respond to the PnP enumeration procedure.
     */
disconnect_idle:
    i = TIOCM_DTR | TIOCM_RTS;		/* DTR = 1, RTS = 1 */
    ioctl(mouse_fd, TIOCMBIS, &i);
#endif

connect_idle:
    return 0;
}

/*
 * pnpparse : parse a PnP string ID
 */

static int
pnpparse(pnpid_t *id, char *buf, int len)
{
    char s[3];
    int offset;
    int sum = 0;
    int i, j;

    id->revision = 0;
    id->eisaid = NULL;
    id->serial = NULL;
    id->class = NULL;
    id->compat = NULL;
    id->description = NULL;
    id->neisaid = 0;
    id->nserial = 0;
    id->nclass = 0;
    id->ncompat = 0;
    id->ndescription = 0;

    offset = 0x28 - buf[0];

    /* calculate checksum */
    for (i = 0; i < len - 3; ++i) {
	sum += buf[i];
	buf[i] += offset;
    }
    sum += buf[len - 1];
    for (; i < len; ++i)
	buf[i] += offset;
    ErrorF("Mouse: PnP ID string: '%*.*s'\n", len, len, buf);

    /* revision */
    buf[1] -= offset;
    buf[2] -= offset;
    id->revision = ((buf[1] & 0x3f) << 6) | (buf[2] & 0x3f);
    ErrorF("Mouse: PnP rev %d.%02d\n", id->revision / 100, id->revision % 100);

    /* EISA vender and product ID */
    id->eisaid = &buf[3];
    id->neisaid = 7;

    /* option strings */
    i = 10;
    if (buf[i] == '\\') {
        /* device serial # */
        for (j = ++i; i < len; ++i) {
            if (buf[i] == '\\')
		break;
        }
	if (i >= len)
	    i -= 3;
	if (i - j == 8) {
            id->serial = &buf[j];
            id->nserial = 8;
	}
    }
    if (buf[i] == '\\') {
        /* PnP class */
        for (j = ++i; i < len; ++i) {
            if (buf[i] == '\\')
		break;
        }
	if (i >= len)
	    i -= 3;
	if (i > j + 1) {
            id->class = &buf[j];
            id->nclass = i - j;
        }
    }
    if (buf[i] == '\\') {
	/* compatible driver */
        for (j = ++i; i < len; ++i) {
            if (buf[i] == '\\')
		break;
        }
	/*
	 * PnP COM spec prior to v0.96 allowed '*' in this field, 
	 * it's not allowed now; just ignore it.
	 */
	if (buf[j] == '*')
	    ++j;
	if (i >= len)
	    i -= 3;
	if (i > j + 1) {
            id->compat = &buf[j];
            id->ncompat = i - j;
        }
    }
    if (buf[i] == '\\') {
	/* product description */
        for (j = ++i; i < len; ++i) {
            if (buf[i] == ';')
		break;
        }
	if (i >= len)
	    i -= 3;
	if (i > j + 1) {
            id->description = &buf[j];
            id->ndescription = i - j;
        }
    }

    /* checksum exists if there are any optional fields */
    if ((id->nserial > 0) || (id->nclass > 0)
	|| (id->ncompat > 0) || (id->ndescription > 0)) {
#if 0
        ErrorF("Mouse: PnP checksum: 0x%02X\n", sum); 
#endif
        sprintf(s, "%02X", sum & 0x0ff);
        if (strncmp(s, &buf[len - 3], 2) != 0) {
#if 0
            /* 
	     * Checksum error!!
	     * I found some mice do not comply with the PnP COM device 
	     * spec regarding checksum... XXX
	     */
	    return FALSE;
#endif
        }
    }

    return 1;
}

/*
 * pnpproto : return the prototype used, based on the PnP ID string
 */

static symtab_t *
pnpproto(pnpid_t *id)
{
    symtab_t *t;
    int i, j;

    if (id->nclass > 0)
	if (strncmp(id->class, "MOUSE", id->nclass) != 0)
	    /* this is not a mouse! */
	    return NULL;

    if (id->neisaid > 0) {
        t = gettoken(pnpprod, id->eisaid, id->neisaid);
	if (t->val != -1)
            return t;
    }

    /*
     * The 'Compatible drivers' field may contain more than one
     * ID separated by ','.
     */
    if (id->ncompat <= 0)
	return NULL;
    for (i = 0; i < id->ncompat; ++i) {
        for (j = i; id->compat[i] != ','; ++i)
            if (i >= id->ncompat)
		break;
        if (i > j) {
            t = gettoken(pnpprod, id->compat + j, i - j);
	    if (t->val != -1)
                return t;
	}
    }

    return NULL;
}

/* mouse_if : returns a string giving the name of the physical interface */

static char *
mouse_if(int iftype)
{
    char *s;

    s = gettokenname(mouse_ifs, iftype);
    return (s == NULL) ? "unknown" : s;
}

/* mouse_init : inits the mouse by writing appropriate sequences */

static void
mouse_init(void)
{
    fd_set fds;
    char *s;
    char c;
    int i;

    /**
     ** This comment is a little out of context here, but it contains 
     ** some useful information...
     ********************************************************************
     **
     ** The following lines take care of the Logitech MouseMan protocols.
     **
     ** NOTE: There are diffrent versions of both MouseMan and TrackMan!
     **       Hence I add another protocol P_LOGIMAN, which the user can
     **       specify as MouseMan in his XF86Config file. This entry was
     **       formerly handled as a special case of P_MS. However, people
     **       who don't have the middle button problem, can still specify
     **       Microsoft and use P_MS.
     **
     ** By default, these mice should use a 3 byte Microsoft protocol
     ** plus a 4th byte for the middle button. However, the mouse might
     ** have switched to a different protocol before we use it, so I send
     ** the proper sequence just in case.
     **
     ** NOTE: - all commands to (at least the European) MouseMan have to
     **         be sent at 1200 Baud.
     **       - each command starts with a '*'.
     **       - whenever the MouseMan receives a '*', it will switch back
     **	 to 1200 Baud. Hence I have to select the desired protocol
     **	 first, then select the baud rate.
     **
     ** The protocols supported by the (European) MouseMan are:
     **   -  5 byte packed binary protocol, as with the Mouse Systems
     **      mouse. Selected by sequence "*U".
     **   -  2 button 3 byte MicroSoft compatible protocol. Selected
     **      by sequence "*V".
     **   -  3 button 3+1 byte MicroSoft compatible protocol (default).
     **      Selected by sequence "*X".
     **
     ** The following baud rates are supported:
     **   -  1200 Baud (default). Selected by sequence "*n".
     **   -  9600 Baud. Selected by sequence "*q".
     **
     ** Selecting a sample rate is no longer supported with the MouseMan!
     ** Some additional lines in xf86Config.c take care of ill configured
     ** baud rates and sample rates. (The user will get an error.)
     */

    switch (mouse.proto) {

    case P_LOGI:
	/* 
	 * The baud rate selection command must be sent at the current
	 * baud rate; try all likely settings 
	 */
	SetMouseSpeed(9600, mouse.baudrate, mousecflags[mouse.proto]);
	SetMouseSpeed(4800, mouse.baudrate, mousecflags[mouse.proto]);
	SetMouseSpeed(2400, mouse.baudrate, mousecflags[mouse.proto]);
	// SetMouseSpeed(1200, mouse.baudrate, mousecflags[mouse.proto]);
	/* select MM series data format */
	write(mouse.mfd, "S", 1);
	SetMouseSpeed(mouse.baudrate, mouse.baudrate,
		      mousecflags[P_MM]);
	/* select report rate/frequency */
	if      (mouse.rate <= 0)   write(mouse.mfd, "O", 1);
	else if (mouse.rate <= 15)  write(mouse.mfd, "J", 1);
	else if (mouse.rate <= 27)  write(mouse.mfd, "K", 1);
	else if (mouse.rate <= 42)  write(mouse.mfd, "L", 1);
	else if (mouse.rate <= 60)  write(mouse.mfd, "R", 1);
	else if (mouse.rate <= 85)  write(mouse.mfd, "M", 1);
	else if (mouse.rate <= 125) write(mouse.mfd, "Q", 1);
	else			     write(mouse.mfd, "N", 1);
	break;

    case P_LOGIMAN:
	/* The command must always be sent at 1200 baud */
	SetMouseSpeed(1200, 1200, mousecflags[mouse.proto]);
	write(mouse.mfd, "*X", 2);
	SetMouseSpeed(1200, mouse.baudrate, mousecflags[mouse.proto]);
	break;

    case P_MMHIT:
	SetMouseSpeed(1200, mouse.baudrate, mousecflags[mouse.proto]);

	/*
	 * Initialize Hitachi PUMA Plus - Model 1212E to desired settings.
	 * The tablet must be configured to be in MM mode, NO parity,
	 * Binary Format.  xf86Info.sampleRate controls the sensativity
	 * of the tablet.  We only use this tablet for it's 4-button puck
	 * so we don't run in "Absolute Mode"
	 */
	write(mouse.mfd, "z8", 2);	/* Set Parity = "NONE" */
	usleep(50000);
	write(mouse.mfd, "zb", 2);	/* Set Format = "Binary" */
	usleep(50000);
	write(mouse.mfd, "@", 1);	/* Set Report Mode = "Stream" */
	usleep(50000);
	write(mouse.mfd, "R", 1);	/* Set Output Rate = "45 rps" */
	usleep(50000);
	write(mouse.mfd, "I\x20", 2);	/* Set Incrememtal Mode "20" */
	usleep(50000);
	write(mouse.mfd, "E", 1);	/* Set Data Type = "Relative */
	usleep(50000);

	/* Resolution is in 'lines per inch' on the Hitachi tablet */
	if      (mouse.resolution == MOUSE_RES_LOW) 		c = 'g';
	else if (mouse.resolution == MOUSE_RES_MEDIUMLOW)	c = 'e';
	else if (mouse.resolution == MOUSE_RES_MEDIUMHIGH)	c = 'h';
	else if (mouse.resolution == MOUSE_RES_HIGH)		c = 'd';
	else if (mouse.resolution <=   40) 			c = 'g';
	else if (mouse.resolution <=  100) 			c = 'd';
	else if (mouse.resolution <=  200) 			c = 'e';
	else if (mouse.resolution <=  500) 			c = 'h';
	else if (mouse.resolution <= 1000) 			c = 'j';
	else                                			c = 'd';
	write(mouse.mfd, &c, 1);
	usleep(50000);

	write(mouse.mfd, "\021", 1);	/* Resume DATA output */
	break;

    case P_THINKING:
	SetMouseSpeed(1200, mouse.baudrate, mousecflags[mouse.proto]);
	/* the PnP ID string may be sent again, discard it */
	usleep(200000);
	i = FREAD;
	ioctl(mouse.mfd, TIOCFLUSH, &i);
	/* send the command to initialize the beast */
	for (s = "E5E5"; *s; ++s) {
	    write(mouse.mfd, s, 1);
	    FD_ZERO(&fds);
	    FD_SET(mouse.mfd, &fds);
	    if (select(FD_SETSIZE, &fds, NULL, NULL, NULL) <= 0)
		break;
	    read(mouse.mfd, &c, 1);
	    debug("%c", c);
	    if (c != *s)
	        break;
	}
	break;

    case P_MSC:
	SetMouseSpeed(1200, mouse.baudrate, mousecflags[mouse.proto]);
	if (mouse.flags & ClearDTR) {
	   i = TIOCM_DTR;
	   ioctl(mouse.mfd, TIOCMBIC, &i);
        }
        if (mouse.flags & ClearRTS) {
	   i = TIOCM_RTS;
	   ioctl(mouse.mfd, TIOCMBIC, &i);
        }
	break;

    case P_BM: 
    	break;
    
    case P_PS2:
	
  /* now sets the resolution and rate for PS/2 mice */
	
	/* always sets resolution, to a default value if no value is given */
	
	c = PS2_SET_RES;
	write(mouse.mfd, &c, 1);
	c = mouse.mode.resolution;
	write(mouse.mfd, &c, 1);
	
	if (mouse.rate != MOUSE_RATE_UNKNOWN) {
		c = PS2_SET_RATE;
		write(mouse.mfd, &c, 1);
		c = mouse.mode.rate;
		write(mouse.mfd, &c, 1);
	}

	break;

    default:
	SetMouseSpeed(1200, mouse.baudrate, mousecflags[mouse.proto]);
	break;
    }
}

		


/* mouse_identify : identify the protocol used by the mouse */

static int
mouse_identify(void)
{
    char pnpbuf[256];	/* PnP identifier string may be up to 256 bytes long */
    pnpid_t pnpid;
    symtab_t *t;
    int len;
    
    /*
     * Note : We don't have the ioctl that exist in FreeBSD so we simulate
     * them with the functions mouse_fill_hwinfo() and mouse_fill_mousemode()
     */
	mouse_fill_hwinfo();
	mouse_fill_mousemode();

	if (mouse.proto != P_UNKNOWN)
		bcopy(proto[mouse.proto], cur_proto, sizeof(cur_proto));
	
	/* INPORT and BUS are the same... */
	if (mouse.mode.protocol == P_INPORT)
		mouse.mode.protocol = P_BM;
	if (mouse.mode.protocol != mouse.proto) {
		/* Hmm, the driver doesn't agree with the user... */
		if (mouse.proto != P_UNKNOWN)
			logwarn("mouse type mismatch (%s != %s), %s is assumed",
					mouse_name(mouse.mode.protocol), 
					mouse_name(mouse.proto),
					mouse_name(mouse.mode.protocol));
		mouse.proto = mouse.mode.protocol;
		bcopy(proto[mouse.proto], cur_proto, sizeof(cur_proto));
	}
	
	cur_proto[4] = mouse.mode.packetsize;
	cur_proto[0] = mouse.mode.syncmask[0];	/* header byte bit mask */
	cur_proto[1] = mouse.mode.syncmask[1];	/* header bit pattern */

    /* maybe this is an PnP mouse... */
    if (mouse.mode.protocol == P_UNKNOWN) {

        if (mouse.flags & NoPnP)
            return mouse.proto;
	if (((len = pnpgets(mouse.mfd,pnpbuf)) <= 0) 
		|| !pnpparse(&pnpid, pnpbuf, len))
            return mouse.proto;

        debug("PnP serial mouse: '%*.*s' '%*.*s' '%*.*s'",
	    pnpid.neisaid, pnpid.neisaid, pnpid.eisaid, 
	    pnpid.ncompat, pnpid.ncompat, pnpid.compat, 
	    pnpid.ndescription, pnpid.ndescription, pnpid.description);

	/* we have a valid PnP serial device ID */
        mouse.hw.iftype = MOUSE_IF_SERIAL;
	t = pnpproto(&pnpid);
	if (t != NULL) {
            mouse.mode.protocol = t->val;
            mouse.hw.model = t->val2;
	} else {
            mouse.mode.protocol = P_UNKNOWN;
	}
	if (mouse.mode.protocol == P_INPORT)
	    mouse.mode.protocol = P_BM;

        /* make final adjustment */
	if (mouse.mode.protocol != P_UNKNOWN) {
	    if (mouse.mode.protocol != mouse.proto) {
		/* Hmm, the device doesn't agree with the user... */
                if (mouse.proto != P_UNKNOWN)
	            logwarn("mouse type mismatch (%s != %s), %s is assumed",
		        mouse_name(mouse.mode.protocol), 
			mouse_name(mouse.proto),
		        mouse_name(mouse.mode.protocol));
	        mouse.proto = mouse.mode.protocol;
                bcopy(proto[mouse.proto], cur_proto, sizeof(cur_proto));
	    }
	}
    }

    debug("proto params: %02x %02x %02x %02x %d %02x %02x",
	cur_proto[0], cur_proto[1], cur_proto[2], cur_proto[3], 
	cur_proto[4], cur_proto[5], cur_proto[6]);

    return mouse.proto;
}

/* mouse_protocol : decode bytes with the current mouse protocol */

static int
mouse_protocol(u_char rBuf, mousestatus_t *act)
{
    /* MOUSE_MSS_BUTTON?DOWN -> MOUSE_BUTTON?DOWN */
    static int butmapmss[4] = {	/* Microsoft, MouseMan, GlidePoint, 
				   IntelliMouse, Thinking Mouse */
	0, 
	MOUSE_BUTTON3DOWN, 
	MOUSE_BUTTON1DOWN, 
	MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, 
    };
    static int butmapmss2[4] = { /* Microsoft, MouseMan, GlidePoint, 
				    Thinking Mouse */
	0, 
	MOUSE_BUTTON4DOWN, 
	MOUSE_BUTTON2DOWN, 
	MOUSE_BUTTON2DOWN | MOUSE_BUTTON4DOWN, 
    };
    /* MOUSE_INTELLI_BUTTON?DOWN -> MOUSE_BUTTON?DOWN */
    static int butmapintelli[4] = { /* IntelliMouse, NetMouse, Mie Mouse,
				       MouseMan+ */
	0, 
	MOUSE_BUTTON2DOWN, 
	MOUSE_BUTTON4DOWN, 
	MOUSE_BUTTON2DOWN | MOUSE_BUTTON4DOWN, 
    };
    /* MOUSE_MSC_BUTTON?UP -> MOUSE_BUTTON?DOWN */
    static int butmapmsc[8] = {	/* MouseSystems, MMSeries, Logitech, 
				   Bus, sysmouse */
	0, 
	MOUSE_BUTTON3DOWN, 
	MOUSE_BUTTON2DOWN, 
	MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN, 
	MOUSE_BUTTON1DOWN, 
	MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, 
	MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
	MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
    };
    /* MOUSE_PS2_BUTTON?DOWN -> MOUSE_BUTTON?DOWN */
    static int butmapps2[8] = {	/* PS/2 */
	0, 
	MOUSE_BUTTON1DOWN, 
	MOUSE_BUTTON3DOWN, 
	MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, 
	MOUSE_BUTTON2DOWN, 
	MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN, 
	MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
	MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
    };
    /* for Hitachi tablet */
    static int butmaphit[8] = {	/* MM HitTablet */
	0, 
	MOUSE_BUTTON3DOWN, 
	MOUSE_BUTTON2DOWN, 
	MOUSE_BUTTON1DOWN, 
	MOUSE_BUTTON4DOWN, 
	MOUSE_BUTTON5DOWN, 
	MOUSE_BUTTON6DOWN, 
	MOUSE_BUTTON7DOWN, 
    };
    /* for PS/2 VersaPad */
    static int butmapversaps2[8] = { /* VersaPad */
	0, 
	MOUSE_BUTTON3DOWN, 
	0, 
	MOUSE_BUTTON3DOWN, 
	MOUSE_BUTTON1DOWN, 
	MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, 
	MOUSE_BUTTON1DOWN, 
	MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, 
    };
    static int           pBufP = 0;
    static unsigned char pBuf[8];
    static int		 prev_x, prev_y;
    static int		 on = FALSE;
    int			 x, y;

    debug("received char 0x%x",(int)rBuf);

    /*
     * Hack for resyncing: We check here for a package that is:
     *  a) illegal (detected by wrong data-package header)
     *  b) invalid (0x80 == -128 and that might be wrong for MouseSystems)
     *  c) bad header-package
     *
     * NOTE: b) is a voilation of the MouseSystems-Protocol, since values of
     *       -128 are allowed, but since they are very seldom we can easily
     *       use them as package-header with no button pressed.
     * NOTE/2: On a PS/2 mouse any byte is valid as a data byte. Furthermore,
     *         0x80 is not valid as a header byte. For a PS/2 mouse we skip
     *         checking data bytes.
     *         For resyncing a PS/2 mouse we require the two most significant
     *         bits in the header byte to be 0. These are the overflow bits,
     *         and in case of an overflow we actually lose sync. Overflows
     *         are very rare, however, and we quickly gain sync again after
     *         an overflow condition. This is the best we can do. (Actually,
     *         we could use bit 0x08 in the header byte for resyncing, since
     *         that bit is supposed to be always on, but nobody told
     *         Microsoft...)
     */

    if (pBufP != 0 && mouse.proto != P_PS2 &&
	((rBuf & cur_proto[2]) != cur_proto[3] || rBuf == 0x80))
    {
	pBufP = 0;		/* skip package */
    }
    
    if (pBufP == 0 && (rBuf & cur_proto[0]) != cur_proto[1])
	return 0;

    /* is there an extra data byte? */
    if (pBufP >= cur_proto[4] && (rBuf & cur_proto[0]) != cur_proto[1])
    {
	/*
	 * Hack for Logitech MouseMan Mouse - Middle button
	 *
	 * Unfortunately this mouse has variable length packets: the standard
	 * Microsoft 3 byte packet plus an optional 4th byte whenever the
	 * middle button status changes.
	 *
	 * We have already processed the standard packet with the movement
	 * and button info.  Now post an event message with the old status
	 * of the left and right buttons and the updated middle button.
	 */

	/*
	 * Even worse, different MouseMen and TrackMen differ in the 4th
	 * byte: some will send 0x00/0x20, others 0x01/0x21, or even
	 * 0x02/0x22, so I have to strip off the lower bits.
         *
         * [JCH-96/01/21]
         * HACK for ALPS "fourth button". (It's bit 0x10 of the "fourth byte"
         * and it is activated by tapping the glidepad with the finger! 8^)
         * We map it to bit bit3, and the reverse map in xf86Events just has
         * to be extended so that it is identified as Button 4. The lower
         * half of the reverse-map may remain unchanged.
	 */

        /*
	 * [KY-97/08/03]
	 * Receive the fourth byte only when preceeding three bytes have
	 * been detected (pBufP >= cur_proto[4]).  In the previous
	 * versions, the test was pBufP == 0; thus, we may have mistakingly
	 * received a byte even if we didn't see anything preceeding 
	 * the byte.
	 */

	if ((rBuf & cur_proto[5]) != cur_proto[6]) {
            pBufP = 0;
	    return 0;
	}

	switch (mouse.proto) {

	/*
	 * IntelliMouse, NetMouse (including NetMouse Pro) and Mie Mouse
	 * always send the fourth byte, whereas the fourth byte is
	 * optional for GlidePoint and ThinkingMouse. The fourth byte 
	 * is also optional for MouseMan+ and FirstMouse+ in their 
	 * native mode. It is always sent if they are in the IntelliMouse 
	 * compatible mode.
	 */ 
	case P_IMSERIAL:	/* IntelliMouse, NetMouse, Mie Mouse,
					   MouseMan+ */
	    act->dx = act->dy = 0;
	    act->dz = (rBuf & 0x08) ? (rBuf & 0x0f) - 16 : (rBuf & 0x0f);
	    act->obutton = act->button;
	    act->button = butmapintelli[(rBuf & MOUSE_MSS_BUTTONS) >> 4]
		| (act->obutton & (MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN));
	    break;

	default:
	    act->dx = act->dy = act->dz = 0;
	    act->obutton = act->button;
	    act->button = butmapmss2[(rBuf & MOUSE_MSS_BUTTONS) >> 4]
		| (act->obutton & (MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN));
	    break;
	}

	act->flags = ((act->dx || act->dy || act->dz) ? MOUSE_POSCHANGED : 0)
	    | (act->obutton ^ act->button);
        pBufP = 0;
	return act->flags;
    }
        
    if (pBufP >= cur_proto[4])
	pBufP = 0;
    pBuf[pBufP++] = rBuf;
    if (pBufP != cur_proto[4])
	return 0;
    
    /*
     * assembly full package
     */

    debug("assembled full packet (len %d) %x,%x,%x,%x,%x,%x,%x,%x",
	cur_proto[4], 
	pBuf[0], pBuf[1], pBuf[2], pBuf[3], 
	pBuf[4], pBuf[5], pBuf[6], pBuf[7]);

    act->dz = 0;
    act->obutton = act->button;
    switch (mouse.proto) 
    {
    case P_MS:		/* Microsoft */
    case P_LOGIMAN:	/* MouseMan/TrackMan */
    case P_GLIDEPOINT:	/* GlidePoint */
    case P_THINKING:		/* ThinkingMouse */
    case P_IMSERIAL:		/* IntelliMouse, NetMouse, Mie Mouse,
					   MouseMan+ */
	act->button = (act->obutton & (MOUSE_BUTTON2DOWN | MOUSE_BUTTON4DOWN))
            | butmapmss[(pBuf[0] & MOUSE_MSS_BUTTONS) >> 4];
	act->dx = (char)(((pBuf[0] & 0x03) << 6) | (pBuf[1] & 0x3F));
	act->dy = (char)(((pBuf[0] & 0x0C) << 4) | (pBuf[2] & 0x3F));
	break;
      
    case P_MSC:		/* MouseSystems Corp */
	act->button = butmapmsc[(~pBuf[0]) & MOUSE_MSC_BUTTONS];
	act->dx =    (char)(pBuf[1]) + (char)(pBuf[3]);
	act->dy = - ((char)(pBuf[2]) + (char)(pBuf[4]));
	break;
      
    case P_MMHIT:		/* MM HitTablet */
	act->button = butmaphit[pBuf[0] & 0x07];
	act->dx = (pBuf[0] & MOUSE_MM_XPOSITIVE) ?   pBuf[1] : - pBuf[1];
	act->dy = (pBuf[0] & MOUSE_MM_YPOSITIVE) ? - pBuf[2] :   pBuf[2];
	break;

    case P_MM:		/* MM Series */
    case P_LOGI:		/* Logitech Mice */
	act->button = butmapmsc[pBuf[0] & MOUSE_MSC_BUTTONS];
	act->dx = (pBuf[0] & MOUSE_MM_XPOSITIVE) ?   pBuf[1] : - pBuf[1];
	act->dy = (pBuf[0] & MOUSE_MM_YPOSITIVE) ? - pBuf[2] :   pBuf[2];
	break;
      
    case P_BM:		/* Bus */
    case P_INPORT:		/* InPort */
	act->button = butmapmsc[(~pBuf[0]) & MOUSE_MSC_BUTTONS];
	act->dx =   (char)pBuf[1];
	act->dy = - (char)pBuf[2];
	break;

    case P_PS2:		/* PS/2 */
	act->button = butmapps2[pBuf[0] & MOUSE_PS2_BUTTONS];
	act->dx = (pBuf[0] & MOUSE_PS2_XNEG) ?    pBuf[1] - 256  :  pBuf[1];
	act->dy = (pBuf[0] & MOUSE_PS2_YNEG) ?  -(pBuf[2] - 256) : -pBuf[2];
	/*
	 * Moused usually operates the psm driver at the operation level 1
	 * which sends mouse data in P_SYSMOUSE protocol.
	 * The following code takes effect only when the user explicitly 
	 * requets the level 2 at which wheel movement and additional button 
	 * actions are encoded in model-dependent formats. At the level 0
	 * the following code is no-op because the psm driver says the model
	 * is MOUSE_MODEL_GENERIC.
	 */
	switch (mouse.hw.model) {
	case MOUSE_MODEL_INTELLI:
	case MOUSE_MODEL_NET:
	    /* wheel data is in the fourth byte */
	    act->dz = (char)pBuf[3];
	    break;
	case MOUSE_MODEL_MOUSEMANPLUS:
	    if (((pBuf[0] & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC)
		    && (abs(act->dx) > 191)
		    && MOUSE_PS2PLUS_CHECKBITS(pBuf)) {
		/* the extended data packet encodes button and wheel events */
		switch (MOUSE_PS2PLUS_PACKET_TYPE(pBuf)) {
		case 1:
		    /* wheel data packet */
		    act->dx = act->dy = 0;
		    if (pBuf[2] & 0x80) {
			/* horizontal roller count - ignore it XXX*/
		    } else {
			/* vertical roller count */
			act->dz = (pBuf[2] & MOUSE_PS2PLUS_ZNEG)
			    ? (pBuf[2] & 0x0f) - 16 : (pBuf[2] & 0x0f);
		    }
		    act->button |= (pBuf[2] & MOUSE_PS2PLUS_BUTTON4DOWN)
			? MOUSE_BUTTON4DOWN : 0;
		    act->button |= (pBuf[2] & MOUSE_PS2PLUS_BUTTON5DOWN)
			? MOUSE_BUTTON5DOWN : 0;
		    break;
		case 2:
		    /* this packet type is reserved, and currently ignored */
		    /* FALL THROUGH */
		case 0:
		    /* device type packet - shouldn't happen */
		    /* FALL THROUGH */
		default:
		    act->dx = act->dy = 0;
		    act->button = act->obutton;
            	    debug("unknown PS2++ packet type %d: 0x%02x 0x%02x 0x%02x\n",
			  MOUSE_PS2PLUS_PACKET_TYPE(pBuf),
			  pBuf[0], pBuf[1], pBuf[2]);
		    break;
		}
	    } else {
		/* preserve button states */
		act->button |= act->obutton & MOUSE_EXTBUTTONS;
	    }
	    break;
	case MOUSE_MODEL_GLIDEPOINT:
	    /* `tapping' action */
	    act->button |= ((pBuf[0] & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
	    break;
	case MOUSE_MODEL_NETSCROLL:
	    /* three addtional bytes encode button and wheel events */
	    act->button |= (pBuf[3] & MOUSE_PS2_BUTTON3DOWN) 
		? MOUSE_BUTTON4DOWN : 0;
	    act->dz = (pBuf[3] & MOUSE_PS2_XNEG) ? pBuf[4] - 256 : pBuf[4];
	    break;
	case MOUSE_MODEL_THINK:
	    /* the fourth button state in the first byte */
	    act->button |= (pBuf[0] & MOUSE_PS2_TAP) ? MOUSE_BUTTON4DOWN : 0;
	    break;
	case MOUSE_MODEL_VERSAPAD:
	    act->button = butmapversaps2[pBuf[0] & MOUSE_PS2VERSA_BUTTONS];
	    act->button |=
		(pBuf[0] & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
	    act->dx = act->dy = 0;
	    if (!(pBuf[0] & MOUSE_PS2VERSA_IN_USE)) {
		on = FALSE;
		break;
	    }
	    x = ((pBuf[4] << 8) & 0xf00) | pBuf[1];
	    if (x & 0x800)
		x -= 0x1000;
	    y = ((pBuf[4] << 4) & 0xf00) | pBuf[2];
	    if (y & 0x800)
		y -= 0x1000;
	    if (on) {
		act->dx = prev_x - x;
		act->dy = prev_y - y;
	    } else {
		on = TRUE;
	    }
	    prev_x = x;
	    prev_y = y;
	    break;
	case MOUSE_MODEL_GENERIC:
	default:
	    break;
	}
	break;

    default:
	return 0;
    }
    /* 
     * We don't reset pBufP here yet, as there may be an additional data
     * byte in some protocols. See above.
     */

    /* has something changed? */
    act->flags = ((act->dx || act->dy || act->dz) ? MOUSE_POSCHANGED : 0)
	| (act->obutton ^ act->button);

    if (mouse.flags & Emulate3Button) {
	if (((act->flags & (MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN))
	        == (MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN))
	    && ((act->button & (MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN))
	        == (MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN))) {
	    act->button &= ~(MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN);
	    act->button |= MOUSE_BUTTON2DOWN;
	} else if ((act->obutton & MOUSE_BUTTON2DOWN)
	    && ((act->button & (MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN))
	        != (MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN))) {
	    act->button &= ~(MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN 
			       | MOUSE_BUTTON3DOWN);
	}
	act->flags &= MOUSE_POSCHANGED;
	act->flags |= act->obutton ^ act->button;
    }

    return act->flags;
}

/*
 * Buttons remapping
 */

/* physical to logical button mapping */
static int p2l[MOUSE_MAXBUTTON] = {
    MOUSE_BUTTON1DOWN, MOUSE_BUTTON2DOWN, MOUSE_BUTTON3DOWN, MOUSE_BUTTON4DOWN, 
    MOUSE_BUTTON5DOWN, MOUSE_BUTTON6DOWN, MOUSE_BUTTON7DOWN, MOUSE_BUTTON8DOWN, 
    0x00000100,        0x00000200,        0x00000400,        0x00000800,
    0x00001000,        0x00002000,        0x00004000,        0x00008000,
    0x00010000,        0x00020000,        0x00040000,        0x00080000,
    0x00100000,        0x00200000,        0x00400000,        0x00800000,
    0x01000000,        0x02000000,        0x04000000,        0x08000000,
    0x10000000,        0x20000000,        0x40000000,
};

/*
 * End of functions from the Xfree Project
 */

/* mouse_installmap : install a map between physical and logical buttons */

static int
mouse_installmap(char *arg)
{
    int pbutton;
    int lbutton;
    char *s;

    while (*arg) {
	arg = skipspace(arg);
	s = arg;
	while (isdigit(*arg))
	    ++arg;
	arg = skipspace(arg);
	if ((arg <= s) || (*arg != '='))
	    return FALSE;
	lbutton = atoi(s);

	arg = skipspace(++arg);
	s = arg;
	while (isdigit(*arg))
	    ++arg;
	if ((arg <= s) || (!isspace(*arg) && (*arg != '\0')))
	    return FALSE;
	pbutton = atoi(s);

	if ((lbutton <= 0) || (lbutton > MOUSE_MAXBUTTON))
	    return FALSE;
	if ((pbutton <= 0) || (pbutton > MOUSE_MAXBUTTON))
	    return FALSE;
	p2l[pbutton - 1] = 1 << (lbutton - 1);
    }

    return TRUE;
}

/* mouse_map : convert physical buttons to logical buttons */

static void
mouse_map(mousestatus_t *act1, mousestatus_t *act2)
{
    int pb;
    int pbuttons;
    int lbuttons;

    pbuttons = act1->button;
    lbuttons = 0;

    act2->obutton = act2->button;
    if (pbuttons & mouse.wmode) {
	pbuttons &= ~mouse.wmode;
	act1->dz = act1->dy;
	act1->dx = 0;
	act1->dy = 0;
    }
    act2->dx = act1->dx;
    act2->dy = act1->dy;
    act2->dz = act1->dz;

    switch (mouse.zmap) {
    case 0:	/* do nothing */
	break;
    case MOUSE_XAXIS:
	if (act1->dz != 0) {
	    act2->dx = act1->dz;
	    act2->dz = 0;
	}
	break;
    case MOUSE_YAXIS:
	if (act1->dz != 0) {
	    act2->dy = act1->dz;
	    act2->dz = 0;
	}
	break;
    default:	/* buttons */
	pbuttons &= ~(mouse.zmap | (mouse.zmap << 1));
	if (act1->dz < 0)
	    pbuttons |= mouse.zmap;
	else if (act1->dz > 0)
	    pbuttons |= (mouse.zmap << 1);
	act2->dz = 0;
	break;
    }

    for (pb = 0; (pb < MOUSE_MAXBUTTON) && (pbuttons != 0); ++pb) {
	lbuttons |= (pbuttons & 1) ? p2l[pb] : 0;
	pbuttons >>= 1;
    }
    act2->button = lbuttons;

    act2->flags = ((act2->dx || act2->dy || act2->dz) ? MOUSE_POSCHANGED : 0)
	| (act2->obutton ^ act2->button);
}

/* 
 * Function to handle button click 
 * Note that an ioctl is sent for each button 
 */

static void
mouse_click(mousestatus_t *act)
{
    mouse_info_t mouse_infos;
    struct timeval tv;
    struct timeval tv1;
    struct timeval tv2;
    struct timezone tz;
    int button;
    int mask;
    int i;

    mask = act->flags & MOUSE_BUTTONS;
    if (mask == 0)
	return;

    gettimeofday(&tv1, &tz);
    tv2.tv_sec = mouse.clickthreshold/1000;
    tv2.tv_usec = (mouse.clickthreshold%1000)*1000;
    timersub(&tv1, &tv2, &tv); 
    debug("tv:  %ld %ld", tv.tv_sec, tv.tv_usec);
    button = MOUSE_BUTTON1DOWN;
    for (i = 0; (i < MOUSE_MAXBUTTON) && (mask != 0); ++i) {
        if (mask & 1) {
            if (act->button & button) {
                /* the button is down */
    		debug("  :  %ld %ld", 
		    buttonstate[i].tv.tv_sec, buttonstate[i].tv.tv_usec);
		if (timercmp(&tv, &buttonstate[i].tv, >)) {
                    buttonstate[i].tv.tv_sec = 0;
                    buttonstate[i].tv.tv_usec = 0;
                    buttonstate[i].count = 1;
                } else {
                    ++buttonstate[i].count;
                }
	        mouse_infos.u.event.value = buttonstate[i].count;
            } else {
                /* the button is up */
                buttonstate[i].tv = tv1;
	        mouse_infos.u.event.value = 0;
            }
	    mouse_infos.operation = MOUSE_BUTTON_EVENT;
	    mouse_infos.u.event.id = button;
	    ioctl(mouse.cfd, PCVT_MOUSECTL, &mouse_infos);
	    debug("button %d  count %d\n", i + 1, mouse_infos.u.event.value);
	}
	button <<= 1;
	mask >>= 1;
    }
}

/* 
 * moused : main function 
 *        - wait for mouse events
 *        - translate according to the current protocol
 *        - execute the actions associated to events
 */

static void
moused(void)
{
    mouse_info_t mouse_infos;
    mousestatus_t action;		/* original mouse action */
    mousestatus_t action2;		/* mapped action */
    fd_set fds;
    u_char b;
    FILE *fp;
    char moused_flag;

    if ((mouse.cfd = open("/dev/pcvtctl", O_RDWR, 0)) == -1)
	logerr(1, "cannot open /dev/pcvtctl");

    if (!nodaemon && !background) {
	if (daemon(0, 0)) {
	    logerr(1, "failed to become a daemon");
	} else {
	  background = TRUE;
	    fp = fopen(pidfile, "w");
	    if (fp != NULL) {
		fprintf(fp, "%d\n", getpid());
		fclose(fp);
	    }
	}
    }
	
    /* display initial cursor */
    mouse_infos.operation = MOUSE_INIT;

    moused_flag = MOUSED_ON;
    ioctl(mouse.cfd, PCVT_MOUSED, &moused_flag);
    ioctl(mouse.cfd, PCVT_MOUSECTL, &mouse_infos);
    
    /* clear mouse data */
    bzero(&action, sizeof(action));
    bzero(&action2, sizeof(action2));
    bzero(&buttonstate, sizeof(buttonstate));
    bzero(&mouse_infos, sizeof(mouse));

    /* process mouse data */
    for (;;) {

	FD_ZERO(&fds);
	FD_SET(mouse.mfd, &fds);

	if (select(FD_SETSIZE, &fds, NULL, NULL, NULL) <= 0)
	    logwarn("failed to read from mouse");

	/*  mouse event  */
	read(mouse.mfd, &b, 1);
	if (mouse_protocol(b, &action)) {	/* handler detected action */
	    mouse_map(&action, &action2);
#if 0
	    fprintf("activity : buttons 0x%08x  dx %d  dy %d  dz %d\n",
		action2.button, action2.dx, action2.dy, action2.dz);
#endif
	    mouse_click(&action2);
	    if (action2.flags & MOUSE_POSCHANGED) {
		    mouse_infos.operation = MOUSE_MOTION_EVENT;
		    mouse_infos.u.data.buttons = action2.button;
		    mouse_infos.u.data.x = action2.dx;
	            mouse_infos.u.data.y = action2.dy;
	            mouse_infos.u.data.z = action2.dz;
		    ioctl(mouse.cfd, PCVT_MOUSECTL, &mouse_infos);
	    }
	    /*
	     * If the Z axis movement is mapped to a imaginary physical 
	     * button, we need to cook up a corresponding button `up' event
	     * after sending a button `down' event.
	     */
            if ((mouse.zmap > 0) && (action.dz != 0)) {
		action.obutton = action.button;
		action.dx = action.dy = action.dz = 0;
	        mouse_map(&action, &action2);
	        debug("activity : buttons 0x%08x  dx %d  dy %d  dz %d",
		    action2.button, action2.dx, action2.dy, action2.dz);
		
		mouse_click(&action2);
	    } 
	    /* NOT REACHED */
	}
    }
}    

static void 
usage(void)
{
	printf("usage : %s [-3DPRcdfs] [-I file] [-F rate] [-r resolution ] [-S baudrate]",__progname);
	printf("                  [-C threshold] [-t protocol] [-m type] [-M N=M] [-b buttons]");
	printf("                    [-w N] [-z target] -p port \n");
	printf("        %s -i info -p port \n",__progname);
	exit(1);
}

int
main(int argc, char **argv)
{
	int opt;
	int i;

	while ((opt = (getopt(argc,argv,"3DPRb:cdfhi:m:st:I:F:r:M:S:C:p:w:z:"))) != -1) {
		switch (opt) {
			case '3':
				mouse.flags |= Emulate3Button;
				break;
			case 'D':
				mouse.flags |= ClearDTR;
				break;
			case 'P':
				mouse.flags |= NoPnP;
				break;
			case 'R':
				mouse.flags |= ClearRTS;
				break;
			case 'b':
				mouse.hw.buttons = atoi(optarg);
				break;
			case 'c':
				mouse.flags |= ChordMiddle;			
				break;
			case 'd':
				++debug;
				break;
			case 'f':
				nodaemon = TRUE;
				break;
			case 'M':
				if (!mouse_installmap(optarg)) {
					warnx("invalid mapping `%s'\n", optarg);
					usage();
				}
				break;
			case 's':
				mouse.baudrate = 9600;
				break;	
			case 't':
				if (strcmp(optarg, "auto") == 0) {
					mouse.proto = P_UNKNOWN;
					mouse.flags &= ~NoPnP;
					break;
				}
				for (i = 0; mouse_names[i]; i++)
					if (strcmp(optarg,mouse_names[i]) == 0){
						mouse.proto = i;
						mouse.flags |= NoPnP;
						break;
					}
				if (mouse_names[i])
					break;
				printf("no such mouse protocol `%s'\n", optarg);
				usage();
				break;
			case 'm':
				for (i = 0 ; mouse_models[i].name; i++)
					if (strcmp(optarg,mouse_models[i].name) == 0){
						mouse.hw.model =
							mouse_models[i].val;
						break;
					}
				if (mouse_models[i].name)
					break;
				printf("no such mouse type `%s'\n", optarg);
				usage();
				break;
			case 'i':
				if (strcmp(optarg, "all") == 0)
					identify = ID_ALL;
				else if (strcmp(optarg, "port") == 0)
					identify = ID_PORT;
				else if (strcmp(optarg, "if") == 0)
					identify = ID_IF;
				else if (strcmp(optarg, "type") == 0)
					identify = ID_TYPE;
				else if (strcmp(optarg, "model") == 0)
					identify = ID_MODEL;
				else {
					printf("invalid argument `%s'", optarg);
					usage();
				}
				break;
			case 'I':
				pidfile = optarg;
				break;
			case 'F':
				mouse.rate = atoi(optarg);
				if (mouse.rate <= 0) {
					printf("invalid rate `%s'\n", optarg);
					usage();
				}
				break;
			case 'r':
				if (strcmp(optarg, "high") == 0)
					mouse.resolution = MOUSE_RES_HIGH;
				else if (strcmp(optarg, "medium-high") == 0)
					mouse.resolution = MOUSE_RES_MEDIUMHIGH;
				else if (strcmp(optarg, "medium-low") == 0)
					mouse.resolution = MOUSE_RES_MEDIUMLOW;
				else if (strcmp(optarg, "low") == 0)
					mouse.resolution = MOUSE_RES_LOW;
				else if (strcmp(optarg, "default") == 0)
					mouse.resolution = MOUSE_RES_DEFAULT;
				else {
					mouse.resolution = atoi(optarg);
					if (mouse.resolution <= 0) {
						printf("invalid resolution `%s'\n", optarg);
						usage();
					}
				}
				break;
			case 'S':
				mouse.baudrate = atoi(optarg);
				if (mouse.baudrate <= 0) {
					printf("invalid baudrate `%s'\n", optarg);
					usage();
				}
				break;
			case 'C':
#define MAX_CLICKTHRESHOLD 2000 /* max delay for double click */
				
				mouse.clickthreshold = atoi(optarg);
				if ((mouse.clickthreshold < 0) || 
				(mouse.clickthreshold > MAX_CLICKTHRESHOLD)) {
					printf("invalid threshold `%s': max value is %d\n"
						, optarg,MAX_CLICKTHRESHOLD);
					usage();
				}
				break;
			case 'p':
				mouse.portname = strdup(optarg);
				break;
			case 'h':
				usage();
				break;
			case 'w':
				i = atoi(optarg);
				if ((i <= 0) || (i > MOUSE_MAXBUTTON)) {
					warnx("invalid argument `%s'", optarg);
					usage();
				}
				mouse.wmode = 1 << (i - 1);
				break;
			case 'z':
				if (strcmp(optarg, "x") == 0)
					mouse.zmap = MOUSE_XAXIS;
				else if (strcmp(optarg, "y") == 0)
					mouse.zmap = MOUSE_YAXIS;
				else {
					i = atoi(optarg);
				/* 
				 * Use button i for negative Z axis movement 
				 * and button (i + 1) for positive Z axis 
				 * movement.
			         */
					if ((i <= 0) || 
					(i > MOUSE_MAXBUTTON - 1)) {
						warnx("invalid argument `%s'", 
								optarg);
						usage();
					}
					mouse.zmap = 1 << (i - 1);
				}
				break;
			default:
				usage();
		}
	}
	/* 
	 * mouse device name : 
	 * Logitech bus mouse : /dev/lms0
	 * Microsoft bus mouse (also refered as inport) : /dev/mms0 
	 * PS/2 mouse : /dev/pms0 
	 *
	 * Note that Logitech and Microsoft bus mouse speak the same
	 * protocol (BusMouse).
	 */
	switch(mouse.proto) {
		case P_INPORT:
			/* Inport and Bus use the same protocol (BusMouse) */
			mouse.proto = P_BM;
			mouse.portname = MMS_DEV;
			break;
		case P_BM:
			if (!mouse.portname)
				mouse.portname = LMS_DEV;
			break;
		case P_PS2:
			if (!mouse.portname)
				mouse.portname = PMS_DEV;
			break;
		default:
			if (mouse.portname) /* serial mouse */
				break;
			printf("no port name specified\n");
			usage();
	}
	
	for (;;) {
		if (setjmp(env) == 0) {
			signal(SIGUSR1, freedev);
			signal(SIGUSR2, opendev);
			signal(SIGINT , cleanup);
			signal(SIGQUIT, cleanup);
			signal(SIGTERM, cleanup);
			signal(SIGKILL, cleanup);
			if ((mouse.mfd = open(mouse.portname, 
					      O_RDWR | O_NONBLOCK, 0)) == -1) 
				logerr(1, "unable to open %s", mouse.portname);
			if (mouse_identify() == P_UNKNOWN) {
				logwarn("cannot determine mouse type on %s", mouse.portname);
				close(mouse.mfd);
				mouse.mfd = -1;
			}

			/* print some information */
			if (identify != ID_NONE) {
				if (identify == ID_ALL)
					printf("%s %s %s %s\n", 
					mouse.portname,
					mouse_if(mouse.hw.iftype),
					mouse_name(mouse.proto), 
					mouse_model(mouse.hw.model));
				else if (identify & ID_PORT)
					printf("%s\n", mouse.portname);
				else if (identify & ID_IF)
					printf("%s\n", mouse_if(mouse.hw.iftype));
				else if (identify & ID_TYPE)
					printf("%s\n", mouse_name(mouse.proto));
				else if (identify & ID_MODEL)
					printf("%s\n", mouse_model(mouse.hw.model));
				exit(0);
			} else {
				debug("port: %s  interface: %s  type: %s  model: %s", 
				mouse.portname, mouse_if(mouse.hw.iftype),
				mouse_name(mouse.proto), mouse_model(mouse.hw.model));
			}

			if (mouse.mfd == -1) {
			/*
		         * We cannot continue because of error.  Exit if the 
		         * program has not become a daemon.  Otherwise, block 
		         * until the the user corrects the problem and issues 
			 * SIGHUP. 
			 */
				if (!background)
					exit(1);
				sigpause(0);
			}

			mouse_init(); /* init mouse */
			moused();
	}

		if (mouse.mfd != -1)
			close(mouse.mfd);
	}
	/* NOT REACHED */

	exit(0);
	return (0);
}