/
moused.c
3428 lines (3105 loc) · 97.6 KB
/
moused.c
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
/**
** Copyright (c) 1995 Michael Smith, All rights reserved.
**
** 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 as
** the first lines of this file unmodified.
** 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 acknowledgment:
** This product includes software developed by Michael Smith.
** 4. The name of the author may not be used to endorse or promote products
** derived from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY Michael Smith ``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 Michael Smith 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.
**
**/
/**
** MOUSED.C
**
** Mouse daemon : listens to a serial port, the bus mouse interface, or
** the PS/2 mouse port for mouse data stream, interprets data and passes
** ioctls off to the console driver.
**
** The mouse interface functions are derived closely from the mouse
** handler in the XFree86 X server. Many thanks to the XFree86 people
** for their great work!
**
**/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/consio.h>
#include <sys/mouse.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/un.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <libutil.h>
#include <limits.h>
#include <setjmp.h>
#include <signal.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <termios.h>
#include <unistd.h>
#include <math.h>
#define MAX_CLICKTHRESHOLD 2000 /* 2 seconds */
#define MAX_BUTTON2TIMEOUT 2000 /* 2 seconds */
#define DFLT_CLICKTHRESHOLD 500 /* 0.5 second */
#define DFLT_BUTTON2TIMEOUT 100 /* 0.1 second */
#define DFLT_SCROLLTHRESHOLD 3 /* 3 pixels */
#define DFLT_SCROLLSPEED 2 /* 2 pixels */
/* Abort 3-button emulation delay after this many movement events. */
#define BUTTON2_MAXMOVE 3
#define TRUE 1
#define FALSE 0
#define MOUSE_XAXIS (-1)
#define MOUSE_YAXIS (-2)
/* Logitech PS2++ protocol */
#define MOUSE_PS2PLUS_CHECKBITS(b) \
((((b[2] & 0x03) << 2) | 0x02) == (b[1] & 0x0f))
#define MOUSE_PS2PLUS_PACKET_TYPE(b) \
(((b[0] & 0x30) >> 2) | ((b[1] & 0x30) >> 4))
#define ChordMiddle 0x0001
#define Emulate3Button 0x0002
#define ClearDTR 0x0004
#define ClearRTS 0x0008
#define NoPnP 0x0010
#define VirtualScroll 0x0020
#define HVirtualScroll 0x0040
#define ExponentialAcc 0x0080
#define ID_NONE 0
#define ID_PORT 1
#define ID_IF 2
#define ID_TYPE 4
#define ID_MODEL 8
#define ID_ALL (ID_PORT | ID_IF | ID_TYPE | ID_MODEL)
/* Operations on timespecs */
#define tsclr(tvp) ((tvp)->tv_sec = (tvp)->tv_nsec = 0)
#define tscmp(tvp, uvp, cmp) \
(((tvp)->tv_sec == (uvp)->tv_sec) ? \
((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
((tvp)->tv_sec cmp (uvp)->tv_sec))
#define tssub(tvp, uvp, vvp) \
do { \
(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
(vvp)->tv_nsec = (tvp)->tv_nsec - (uvp)->tv_nsec; \
if ((vvp)->tv_nsec < 0) { \
(vvp)->tv_sec--; \
(vvp)->tv_nsec += 1000000000; \
} \
} while (0)
#define debug(...) do { \
if (debug && nodaemon) \
warnx(__VA_ARGS__); \
} while (0)
#define logerr(e, ...) do { \
log_or_warn(LOG_DAEMON | LOG_ERR, errno, __VA_ARGS__); \
exit(e); \
} while (0)
#define logerrx(e, ...) do { \
log_or_warn(LOG_DAEMON | LOG_ERR, 0, __VA_ARGS__); \
exit(e); \
} while (0)
#define logwarn(...) \
log_or_warn(LOG_DAEMON | LOG_WARNING, errno, __VA_ARGS__)
#define logwarnx(...) \
log_or_warn(LOG_DAEMON | LOG_WARNING, 0, __VA_ARGS__)
/* structures */
/* symbol table entry */
typedef struct {
const char *name;
int val;
int val2;
} symtab_t;
/* serial PnP ID string */
typedef struct {
int revision; /* PnP revision, 100 for 1.00 */
const char *eisaid; /* EISA ID including mfr ID and product ID */
char *serial; /* serial No, optional */
const char *class; /* device class, optional */
char *compat; /* list of compatible drivers, optional */
char *description; /* product description, optional */
int neisaid; /* length of the above fields... */
int nserial;
int nclass;
int ncompat;
int ndescription;
} pnpid_t;
/* global variables */
static int debug = 0;
static int nodaemon = FALSE;
static int background = FALSE;
static int paused = FALSE;
static int identify = ID_NONE;
static int extioctl = FALSE;
static const char *pidfile = "/var/run/moused.pid";
static struct pidfh *pfh;
#define SCROLL_NOTSCROLLING 0
#define SCROLL_PREPARE 1
#define SCROLL_SCROLLING 2
static int scroll_state;
static int scroll_movement;
static int hscroll_movement;
/* local variables */
/* interface (the table must be ordered by MOUSE_IF_XXX in mouse.h) */
static symtab_t rifs[] = {
{ "serial", MOUSE_IF_SERIAL, 0 },
{ "bus", MOUSE_IF_BUS, 0 },
{ "inport", MOUSE_IF_INPORT, 0 },
{ "ps/2", MOUSE_IF_PS2, 0 },
{ "sysmouse", MOUSE_IF_SYSMOUSE, 0 },
{ "usb", MOUSE_IF_USB, 0 },
{ NULL, MOUSE_IF_UNKNOWN, 0 },
};
/* types (the table must be ordered by MOUSE_PROTO_XXX in mouse.h) */
static const char *rnames[] = {
"microsoft",
"mousesystems",
"logitech",
"mmseries",
"mouseman",
"busmouse",
"inportmouse",
"ps/2",
"mmhitab",
"glidepoint",
"intellimouse",
"thinkingmouse",
"sysmouse",
"x10mouseremote",
"kidspad",
"versapad",
"jogdial",
#if notyet
"mariqua",
#endif
"gtco_digipad",
NULL
};
/* models */
static symtab_t rmodels[] = {
{ "NetScroll", MOUSE_MODEL_NETSCROLL, 0 },
{ "NetMouse/NetScroll Optical", MOUSE_MODEL_NET, 0 },
{ "GlidePoint", MOUSE_MODEL_GLIDEPOINT, 0 },
{ "ThinkingMouse", MOUSE_MODEL_THINK, 0 },
{ "IntelliMouse", MOUSE_MODEL_INTELLI, 0 },
{ "EasyScroll/SmartScroll", MOUSE_MODEL_EASYSCROLL, 0 },
{ "MouseMan+", MOUSE_MODEL_MOUSEMANPLUS, 0 },
{ "Kidspad", MOUSE_MODEL_KIDSPAD, 0 },
{ "VersaPad", MOUSE_MODEL_VERSAPAD, 0 },
{ "IntelliMouse Explorer", MOUSE_MODEL_EXPLORER, 0 },
{ "4D Mouse", MOUSE_MODEL_4D, 0 },
{ "4D+ Mouse", MOUSE_MODEL_4DPLUS, 0 },
{ "Synaptics Touchpad", MOUSE_MODEL_SYNAPTICS, 0 },
{ "TrackPoint", MOUSE_MODEL_TRACKPOINT, 0 },
{ "Elantech Touchpad", MOUSE_MODEL_ELANTECH, 0 },
{ "generic", MOUSE_MODEL_GENERIC, 0 },
{ NULL, MOUSE_MODEL_UNKNOWN, 0 },
};
/* PnP EISA/product IDs */
static symtab_t pnpprod[] = {
/* Kensignton ThinkingMouse */
{ "KML0001", MOUSE_PROTO_THINK, MOUSE_MODEL_THINK },
/* MS IntelliMouse */
{ "MSH0001", MOUSE_PROTO_INTELLI, MOUSE_MODEL_INTELLI },
/* MS IntelliMouse TrackBall */
{ "MSH0004", MOUSE_PROTO_INTELLI, MOUSE_MODEL_INTELLI },
/* Tremon Wheel Mouse MUSD */
{ "HTK0001", MOUSE_PROTO_INTELLI, MOUSE_MODEL_INTELLI },
/* Genius PnP Mouse */
{ "KYE0001", MOUSE_PROTO_MS, MOUSE_MODEL_GENERIC },
/* MouseSystems SmartScroll Mouse (OEM from Genius?) */
{ "KYE0002", MOUSE_PROTO_MS, MOUSE_MODEL_EASYSCROLL },
/* Genius NetMouse */
{ "KYE0003", MOUSE_PROTO_INTELLI, MOUSE_MODEL_NET },
/* Genius Kidspad, Easypad and other tablets */
{ "KYE0005", MOUSE_PROTO_KIDSPAD, MOUSE_MODEL_KIDSPAD },
/* Genius EZScroll */
{ "KYEEZ00", MOUSE_PROTO_MS, MOUSE_MODEL_EASYSCROLL },
/* Logitech Cordless MouseMan Wheel */
{ "LGI8033", MOUSE_PROTO_INTELLI, MOUSE_MODEL_MOUSEMANPLUS },
/* Logitech MouseMan (new 4 button model) */
{ "LGI800C", MOUSE_PROTO_INTELLI, MOUSE_MODEL_MOUSEMANPLUS },
/* Logitech MouseMan+ */
{ "LGI8050", MOUSE_PROTO_INTELLI, MOUSE_MODEL_MOUSEMANPLUS },
/* Logitech FirstMouse+ */
{ "LGI8051", MOUSE_PROTO_INTELLI, MOUSE_MODEL_MOUSEMANPLUS },
/* Logitech serial */
{ "LGI8001", MOUSE_PROTO_LOGIMOUSEMAN, MOUSE_MODEL_GENERIC },
/* A4 Tech 4D/4D+ Mouse */
{ "A4W0005", MOUSE_PROTO_INTELLI, MOUSE_MODEL_4D },
/* 8D Scroll Mouse */
{ "PEC9802", MOUSE_PROTO_INTELLI, MOUSE_MODEL_INTELLI },
/* Mitsumi Wireless Scroll Mouse */
{ "MTM6401", MOUSE_PROTO_INTELLI, MOUSE_MODEL_INTELLI },
/* MS bus */
{ "PNP0F00", MOUSE_PROTO_BUS, MOUSE_MODEL_GENERIC },
/* MS serial */
{ "PNP0F01", MOUSE_PROTO_MS, MOUSE_MODEL_GENERIC },
/* MS InPort */
{ "PNP0F02", MOUSE_PROTO_INPORT, MOUSE_MODEL_GENERIC },
/* MS PS/2 */
{ "PNP0F03", MOUSE_PROTO_PS2, MOUSE_MODEL_GENERIC },
/*
* EzScroll returns PNP0F04 in the compatible device field; but it
* doesn't look compatible... XXX
*/
/* MouseSystems */
{ "PNP0F04", MOUSE_PROTO_MSC, MOUSE_MODEL_GENERIC },
/* MouseSystems */
{ "PNP0F05", MOUSE_PROTO_MSC, MOUSE_MODEL_GENERIC },
#if notyet
/* Genius Mouse */
{ "PNP0F06", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC },
/* Genius Mouse */
{ "PNP0F07", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC },
#endif
/* Logitech serial */
{ "PNP0F08", MOUSE_PROTO_LOGIMOUSEMAN, MOUSE_MODEL_GENERIC },
/* MS BallPoint serial */
{ "PNP0F09", MOUSE_PROTO_MS, MOUSE_MODEL_GENERIC },
/* MS PnP serial */
{ "PNP0F0A", MOUSE_PROTO_MS, MOUSE_MODEL_GENERIC },
/* MS PnP BallPoint serial */
{ "PNP0F0B", MOUSE_PROTO_MS, MOUSE_MODEL_GENERIC },
/* MS serial comatible */
{ "PNP0F0C", MOUSE_PROTO_MS, MOUSE_MODEL_GENERIC },
/* MS InPort comatible */
{ "PNP0F0D", MOUSE_PROTO_INPORT, MOUSE_MODEL_GENERIC },
/* MS PS/2 comatible */
{ "PNP0F0E", MOUSE_PROTO_PS2, MOUSE_MODEL_GENERIC },
/* MS BallPoint comatible */
{ "PNP0F0F", MOUSE_PROTO_MS, MOUSE_MODEL_GENERIC },
#if notyet
/* TI QuickPort */
{ "PNP0F10", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC },
#endif
/* MS bus comatible */
{ "PNP0F11", MOUSE_PROTO_BUS, MOUSE_MODEL_GENERIC },
/* Logitech PS/2 */
{ "PNP0F12", MOUSE_PROTO_PS2, MOUSE_MODEL_GENERIC },
/* PS/2 */
{ "PNP0F13", MOUSE_PROTO_PS2, MOUSE_MODEL_GENERIC },
#if notyet
/* MS Kids Mouse */
{ "PNP0F14", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC },
#endif
/* Logitech bus */
{ "PNP0F15", MOUSE_PROTO_BUS, MOUSE_MODEL_GENERIC },
#if notyet
/* Logitech SWIFT */
{ "PNP0F16", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC },
#endif
/* Logitech serial compat */
{ "PNP0F17", MOUSE_PROTO_LOGIMOUSEMAN, MOUSE_MODEL_GENERIC },
/* Logitech bus compatible */
{ "PNP0F18", MOUSE_PROTO_BUS, MOUSE_MODEL_GENERIC },
/* Logitech PS/2 compatible */
{ "PNP0F19", MOUSE_PROTO_PS2, MOUSE_MODEL_GENERIC },
#if notyet
/* Logitech SWIFT compatible */
{ "PNP0F1A", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC },
/* HP Omnibook */
{ "PNP0F1B", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC },
/* Compaq LTE TrackBall PS/2 */
{ "PNP0F1C", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC },
/* Compaq LTE TrackBall serial */
{ "PNP0F1D", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC },
/* MS Kidts Trackball */
{ "PNP0F1E", MOUSE_PROTO_XXX, MOUSE_MODEL_GENERIC },
#endif
/* Interlink VersaPad */
{ "LNK0001", MOUSE_PROTO_VERSAPAD, MOUSE_MODEL_VERSAPAD },
{ NULL, MOUSE_PROTO_UNKNOWN, MOUSE_MODEL_GENERIC },
};
/* the table must be ordered by MOUSE_PROTO_XXX in mouse.h */
static unsigned short rodentcflags[] =
{
(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 */
(CS8 | CSTOPB | CREAD | CLOCAL | HUPCL), /* sysmouse */
(CS7 | CREAD | CLOCAL | HUPCL), /* X10 MouseRemote */
(CS8 | PARENB | PARODD | CREAD | CLOCAL | HUPCL), /* kidspad etc. */
(CS8 | CREAD | CLOCAL | HUPCL), /* VersaPad */
0, /* JogDial */
#if notyet
(CS8 | CSTOPB | CREAD | CLOCAL | HUPCL), /* Mariqua */
#endif
(CS8 | CREAD | HUPCL ), /* GTCO Digi-Pad */
};
static struct rodentparam {
int flags;
const char *portname; /* /dev/XXX */
int rtype; /* MOUSE_PROTO_XXX */
int level; /* operation level: 0 or greater */
int baudrate;
int rate; /* report rate */
int resolution; /* MOUSE_RES_XXX or a positive number */
int zmap[4]; /* MOUSE_{X|Y}AXIS or a button number */
int wmode; /* wheel mode button number */
int mfd; /* mouse file descriptor */
int cfd; /* /dev/consolectl file descriptor */
int mremsfd; /* mouse remote server file descriptor */
int mremcfd; /* mouse remote client file descriptor */
int is_removable; /* set if device is removable, like USB */
long clickthreshold; /* double click speed in msec */
long button2timeout; /* 3 button emulation timeout */
mousehw_t hw; /* mouse device hardware information */
mousemode_t mode; /* protocol information */
float accelx; /* Acceleration in the X axis */
float accely; /* Acceleration in the Y axis */
float expoaccel; /* Exponential acceleration */
float expoffset; /* Movement offset for exponential accel. */
float remainx; /* Remainder on X and Y axis, respectively... */
float remainy; /* ... to compensate for rounding errors. */
int scrollthreshold; /* Movement distance before virtual scrolling */
int scrollspeed; /* Movement distance to rate of scrolling */
} rodent = {
.flags = 0,
.portname = NULL,
.rtype = MOUSE_PROTO_UNKNOWN,
.level = -1,
.baudrate = 1200,
.rate = 0,
.resolution = MOUSE_RES_UNKNOWN,
.zmap = { 0, 0, 0, 0 },
.wmode = 0,
.mfd = -1,
.cfd = -1,
.mremsfd = -1,
.mremcfd = -1,
.is_removable = 0,
.clickthreshold = DFLT_CLICKTHRESHOLD,
.button2timeout = DFLT_BUTTON2TIMEOUT,
.accelx = 1.0,
.accely = 1.0,
.expoaccel = 1.0,
.expoffset = 1.0,
.remainx = 0.0,
.remainy = 0.0,
.scrollthreshold = DFLT_SCROLLTHRESHOLD,
.scrollspeed = DFLT_SCROLLSPEED,
};
/* button status */
struct button_state {
int count; /* 0: up, 1: single click, 2: double click,... */
struct timespec ts; /* timestamp on the last button event */
};
static struct button_state bstate[MOUSE_MAXBUTTON]; /* button state */
static struct button_state *mstate[MOUSE_MAXBUTTON];/* mapped button st.*/
static struct button_state zstate[4]; /* Z/W axis state */
/* state machine for 3 button emulation */
#define S0 0 /* start */
#define S1 1 /* button 1 delayed down */
#define S2 2 /* button 3 delayed down */
#define S3 3 /* both buttons down -> button 2 down */
#define S4 4 /* button 1 delayed up */
#define S5 5 /* button 1 down */
#define S6 6 /* button 3 down */
#define S7 7 /* both buttons down */
#define S8 8 /* button 3 delayed up */
#define S9 9 /* button 1 or 3 up after S3 */
#define A(b1, b3) (((b1) ? 2 : 0) | ((b3) ? 1 : 0))
#define A_TIMEOUT 4
#define S_DELAYED(st) (states[st].s[A_TIMEOUT] != (st))
static struct {
int s[A_TIMEOUT + 1];
int buttons;
int mask;
int timeout;
} states[10] = {
/* S0 */
{ { S0, S2, S1, S3, S0 }, 0, ~(MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN), FALSE },
/* S1 */
{ { S4, S2, S1, S3, S5 }, 0, ~MOUSE_BUTTON1DOWN, FALSE },
/* S2 */
{ { S8, S2, S1, S3, S6 }, 0, ~MOUSE_BUTTON3DOWN, FALSE },
/* S3 */
{ { S0, S9, S9, S3, S3 }, MOUSE_BUTTON2DOWN, ~0, FALSE },
/* S4 */
{ { S0, S2, S1, S3, S0 }, MOUSE_BUTTON1DOWN, ~0, TRUE },
/* S5 */
{ { S0, S2, S5, S7, S5 }, MOUSE_BUTTON1DOWN, ~0, FALSE },
/* S6 */
{ { S0, S6, S1, S7, S6 }, MOUSE_BUTTON3DOWN, ~0, FALSE },
/* S7 */
{ { S0, S6, S5, S7, S7 }, MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, ~0, FALSE },
/* S8 */
{ { S0, S2, S1, S3, S0 }, MOUSE_BUTTON3DOWN, ~0, TRUE },
/* S9 */
{ { S0, S9, S9, S3, S9 }, 0, ~(MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN), FALSE },
};
static int mouse_button_state;
static struct timespec mouse_button_state_ts;
static int mouse_move_delayed;
static jmp_buf env;
struct drift_xy {
int x;
int y;
};
static int drift_distance = 4; /* max steps X+Y */
static int drift_time = 500; /* in 0.5 sec */
static struct timespec drift_time_ts;
static struct timespec drift_2time_ts; /* 2*drift_time */
static int drift_after = 4000; /* 4 sec */
static struct timespec drift_after_ts;
static int drift_terminate = FALSE;
static struct timespec drift_current_ts;
static struct timespec drift_tmp;
static struct timespec drift_last_activity = {0, 0};
static struct timespec drift_since = {0, 0};
static struct drift_xy drift_last = {0, 0}; /* steps in last drift_time */
static struct drift_xy drift_previous = {0, 0}; /* steps in prev. drift_time */
/* function prototypes */
static void linacc(int, int, int*, int*);
static void expoacc(int, int, int*, int*);
static void moused(void);
static void hup(int sig);
static void cleanup(int sig);
static void pause_mouse(int sig);
static void usage(void);
static void log_or_warn(int log_pri, int errnum, const char *fmt, ...)
__printflike(3, 4);
static int r_identify(void);
static const char *r_if(int type);
static const char *r_name(int type);
static const char *r_model(int model);
static void r_init(void);
static int r_protocol(u_char b, mousestatus_t *act);
static int r_statetrans(mousestatus_t *a1, mousestatus_t *a2, int trans);
static int r_installmap(char *arg);
static void r_map(mousestatus_t *act1, mousestatus_t *act2);
static void r_timestamp(mousestatus_t *act);
static int r_timeout(void);
static void r_click(mousestatus_t *act);
static void setmousespeed(int old, int new, unsigned cflag);
static int pnpwakeup1(void);
static int pnpwakeup2(void);
static int pnpgets(char *buf);
static int pnpparse(pnpid_t *id, char *buf, int len);
static symtab_t *pnpproto(pnpid_t *id);
static symtab_t *gettoken(symtab_t *tab, const char *s, int len);
static const char *gettokenname(symtab_t *tab, int val);
static void mremote_serversetup(void);
static void mremote_clientchg(int add);
static int kidspad(u_char rxc, mousestatus_t *act);
static int gtco_digipad(u_char, mousestatus_t *);
int
main(int argc, char *argv[])
{
int c;
int i;
int j;
for (i = 0; i < MOUSE_MAXBUTTON; ++i)
mstate[i] = &bstate[i];
while ((c = getopt(argc, argv, "3A:C:DE:F:HI:L:PRS:T:VU:a:cdfhi:l:m:p:r:st:w:z:")) != -1)
switch(c) {
case '3':
rodent.flags |= Emulate3Button;
break;
case 'E':
rodent.button2timeout = atoi(optarg);
if ((rodent.button2timeout < 0) ||
(rodent.button2timeout > MAX_BUTTON2TIMEOUT)) {
warnx("invalid argument `%s'", optarg);
usage();
}
break;
case 'a':
i = sscanf(optarg, "%f,%f", &rodent.accelx, &rodent.accely);
if (i == 0) {
warnx("invalid linear acceleration argument '%s'", optarg);
usage();
}
if (i == 1)
rodent.accely = rodent.accelx;
break;
case 'A':
rodent.flags |= ExponentialAcc;
i = sscanf(optarg, "%f,%f", &rodent.expoaccel, &rodent.expoffset);
if (i == 0) {
warnx("invalid exponential acceleration argument '%s'", optarg);
usage();
}
if (i == 1)
rodent.expoffset = 1.0;
break;
case 'c':
rodent.flags |= ChordMiddle;
break;
case 'd':
++debug;
break;
case 'f':
nodaemon = TRUE;
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 {
warnx("invalid argument `%s'", optarg);
usage();
}
nodaemon = TRUE;
break;
case 'l':
rodent.level = atoi(optarg);
if ((rodent.level < 0) || (rodent.level > 4)) {
warnx("invalid argument `%s'", optarg);
usage();
}
break;
case 'm':
if (!r_installmap(optarg)) {
warnx("invalid argument `%s'", optarg);
usage();
}
break;
case 'p':
rodent.portname = optarg;
break;
case 'r':
if (strcmp(optarg, "high") == 0)
rodent.resolution = MOUSE_RES_HIGH;
else if (strcmp(optarg, "medium-high") == 0)
rodent.resolution = MOUSE_RES_HIGH;
else if (strcmp(optarg, "medium-low") == 0)
rodent.resolution = MOUSE_RES_MEDIUMLOW;
else if (strcmp(optarg, "low") == 0)
rodent.resolution = MOUSE_RES_LOW;
else if (strcmp(optarg, "default") == 0)
rodent.resolution = MOUSE_RES_DEFAULT;
else {
rodent.resolution = atoi(optarg);
if (rodent.resolution <= 0) {
warnx("invalid argument `%s'", optarg);
usage();
}
}
break;
case 's':
rodent.baudrate = 9600;
break;
case 'w':
i = atoi(optarg);
if ((i <= 0) || (i > MOUSE_MAXBUTTON)) {
warnx("invalid argument `%s'", optarg);
usage();
}
rodent.wmode = 1 << (i - 1);
break;
case 'z':
if (strcmp(optarg, "x") == 0)
rodent.zmap[0] = MOUSE_XAXIS;
else if (strcmp(optarg, "y") == 0)
rodent.zmap[0] = 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();
}
rodent.zmap[0] = i;
rodent.zmap[1] = i + 1;
debug("optind: %d, optarg: '%s'", optind, optarg);
for (j = 1; j < 4; ++j) {
if ((optind >= argc) || !isdigit(*argv[optind]))
break;
i = atoi(argv[optind]);
if ((i <= 0) || (i > MOUSE_MAXBUTTON - 1)) {
warnx("invalid argument `%s'", argv[optind]);
usage();
}
rodent.zmap[j] = i;
++optind;
}
if ((rodent.zmap[2] != 0) && (rodent.zmap[3] == 0))
rodent.zmap[3] = rodent.zmap[2] + 1;
}
break;
case 'C':
rodent.clickthreshold = atoi(optarg);
if ((rodent.clickthreshold < 0) ||
(rodent.clickthreshold > MAX_CLICKTHRESHOLD)) {
warnx("invalid argument `%s'", optarg);
usage();
}
break;
case 'D':
rodent.flags |= ClearDTR;
break;
case 'F':
rodent.rate = atoi(optarg);
if (rodent.rate <= 0) {
warnx("invalid argument `%s'", optarg);
usage();
}
break;
case 'H':
rodent.flags |= HVirtualScroll;
break;
case 'I':
pidfile = optarg;
break;
case 'L':
rodent.scrollspeed = atoi(optarg);
if (rodent.scrollspeed < 0) {
warnx("invalid argument `%s'", optarg);
usage();
}
break;
case 'P':
rodent.flags |= NoPnP;
break;
case 'R':
rodent.flags |= ClearRTS;
break;
case 'S':
rodent.baudrate = atoi(optarg);
if (rodent.baudrate <= 0) {
warnx("invalid argument `%s'", optarg);
usage();
}
debug("rodent baudrate %d", rodent.baudrate);
break;
case 'T':
drift_terminate = TRUE;
sscanf(optarg, "%d,%d,%d", &drift_distance, &drift_time,
&drift_after);
if (drift_distance <= 0 || drift_time <= 0 || drift_after <= 0) {
warnx("invalid argument `%s'", optarg);
usage();
}
debug("terminate drift: distance %d, time %d, after %d",
drift_distance, drift_time, drift_after);
drift_time_ts.tv_sec = drift_time / 1000;
drift_time_ts.tv_nsec = (drift_time % 1000) * 1000000;
drift_2time_ts.tv_sec = (drift_time *= 2) / 1000;
drift_2time_ts.tv_nsec = (drift_time % 1000) * 1000000;
drift_after_ts.tv_sec = drift_after / 1000;
drift_after_ts.tv_nsec = (drift_after % 1000) * 1000000;
break;
case 't':
if (strcmp(optarg, "auto") == 0) {
rodent.rtype = MOUSE_PROTO_UNKNOWN;
rodent.flags &= ~NoPnP;
rodent.level = -1;
break;
}
for (i = 0; rnames[i] != NULL; i++)
if (strcmp(optarg, rnames[i]) == 0) {
rodent.rtype = i;
rodent.flags |= NoPnP;
rodent.level = (i == MOUSE_PROTO_SYSMOUSE) ? 1 : 0;
break;
}
if (rnames[i] == NULL) {
warnx("no such mouse type `%s'", optarg);
usage();
}
break;
case 'V':
rodent.flags |= VirtualScroll;
break;
case 'U':
rodent.scrollthreshold = atoi(optarg);
if (rodent.scrollthreshold < 0) {
warnx("invalid argument `%s'", optarg);
usage();
}
break;
case 'h':
case '?':
default:
usage();
}
/* fix Z axis mapping */
for (i = 0; i < 4; ++i) {
if (rodent.zmap[i] > 0) {
for (j = 0; j < MOUSE_MAXBUTTON; ++j) {
if (mstate[j] == &bstate[rodent.zmap[i] - 1])
mstate[j] = &zstate[i];
}
rodent.zmap[i] = 1 << (rodent.zmap[i] - 1);
}
}
/* the default port name */
switch(rodent.rtype) {
case MOUSE_PROTO_INPORT:
/* INPORT and BUS are the same... */
rodent.rtype = MOUSE_PROTO_BUS;
/* FALLTHROUGH */
case MOUSE_PROTO_BUS:
if (!rodent.portname)
rodent.portname = "/dev/mse0";
break;
case MOUSE_PROTO_PS2:
if (!rodent.portname)
rodent.portname = "/dev/psm0";
break;
default:
if (rodent.portname)
break;
warnx("no port name specified");
usage();
}
if (strncmp(rodent.portname, "/dev/ums", 8) == 0)
rodent.is_removable = 1;
for (;;) {
if (setjmp(env) == 0) {
signal(SIGHUP, hup);
signal(SIGINT , cleanup);
signal(SIGQUIT, cleanup);
signal(SIGTERM, cleanup);
signal(SIGUSR1, pause_mouse);
rodent.mfd = open(rodent.portname, O_RDWR | O_NONBLOCK);
if (rodent.mfd == -1)
logerr(1, "unable to open %s", rodent.portname);
if (r_identify() == MOUSE_PROTO_UNKNOWN) {
logwarnx("cannot determine mouse type on %s", rodent.portname);
close(rodent.mfd);
rodent.mfd = -1;
}
/* print some information */
if (identify != ID_NONE) {
if (identify == ID_ALL)
printf("%s %s %s %s\n",
rodent.portname, r_if(rodent.hw.iftype),
r_name(rodent.rtype), r_model(rodent.hw.model));
else if (identify & ID_PORT)
printf("%s\n", rodent.portname);
else if (identify & ID_IF)
printf("%s\n", r_if(rodent.hw.iftype));
else if (identify & ID_TYPE)
printf("%s\n", r_name(rodent.rtype));
else if (identify & ID_MODEL)
printf("%s\n", r_model(rodent.hw.model));
exit(0);
} else {
debug("port: %s interface: %s type: %s model: %s",
rodent.portname, r_if(rodent.hw.iftype),
r_name(rodent.rtype), r_model(rodent.hw.model));
}
if (rodent.mfd == -1) {
/*
* We cannot continue because of error. Exit if the
* program has not become a daemon. Otherwise, block
* until the user corrects the problem and issues SIGHUP.
*/
if (!background)
exit(1);
sigpause(0);
}
r_init(); /* call init function */
moused();
}
if (rodent.mfd != -1)
close(rodent.mfd);
if (rodent.cfd != -1)
close(rodent.cfd);
rodent.mfd = rodent.cfd = -1;
if (rodent.is_removable)
exit(0);
}
/* NOT REACHED */
exit(0);
}
/*
* Function to calculate linear acceleration.
*
* If there are any rounding errors, the remainder
* is stored in the remainx and remainy variables
* and taken into account upon the next movement.
*/
static void
linacc(int dx, int dy, int *movex, int *movey)
{
float fdx, fdy;
if (dx == 0 && dy == 0) {
*movex = *movey = 0;
return;
}
fdx = dx * rodent.accelx + rodent.remainx;
fdy = dy * rodent.accely + rodent.remainy;
*movex = lround(fdx);
*movey = lround(fdy);
rodent.remainx = fdx - *movex;
rodent.remainy = fdy - *movey;
}
/*
* Function to calculate exponential acceleration.
* (Also includes linear acceleration if enabled.)
*
* In order to give a smoother behaviour, we record the four
* most recent non-zero movements and use their average value
* to calculate the acceleration.
*/
static void
expoacc(int dx, int dy, int *movex, int *movey)
{
static float lastlength[3] = {0.0, 0.0, 0.0};
float fdx, fdy, length, lbase, accel;
if (dx == 0 && dy == 0) {
*movex = *movey = 0;
return;
}
fdx = dx * rodent.accelx;
fdy = dy * rodent.accely;
length = sqrtf((fdx * fdx) + (fdy * fdy)); /* Pythagoras */
length = (length + lastlength[0] + lastlength[1] + lastlength[2]) / 4;
lbase = length / rodent.expoffset;
accel = powf(lbase, rodent.expoaccel) / lbase;