/
wsmouse.c
1716 lines (1515 loc) · 43.3 KB
/
wsmouse.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
/* $OpenBSD: wsmouse.c,v 1.65 2020/05/29 04:42:25 deraadt Exp $ */
/* $NetBSD: wsmouse.c,v 1.35 2005/02/27 00:27:52 perry Exp $ */
/*
* Copyright (c) 1996, 1997 Christopher G. Demetriou. 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.
* 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 Christopher G. Demetriou
* for the NetBSD Project.
* 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 THE AUTHOR ``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 AUTHOR 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.
*/
/*
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* This software was developed by the Computer Systems Engineering group
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
* contributed to Berkeley.
*
* All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Lawrence Berkeley Laboratory.
*
* 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. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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.
*
* @(#)ms.c 8.1 (Berkeley) 6/11/93
*/
/*
* Copyright (c) 2015, 2016 Ulf Brosziewski
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* Mouse driver.
*/
#include <sys/param.h>
#include <sys/conf.h>
#include <sys/ioctl.h>
#include <sys/fcntl.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/syslog.h>
#include <sys/systm.h>
#include <sys/tty.h>
#include <sys/signalvar.h>
#include <sys/device.h>
#include <sys/vnode.h>
#include <sys/poll.h>
#include <sys/malloc.h>
#include <dev/wscons/wscons_features.h>
#include <dev/wscons/wsconsio.h>
#include <dev/wscons/wsmousevar.h>
#include <dev/wscons/wseventvar.h>
#include <dev/wscons/wsmouseinput.h>
#include "wsmux.h"
#include "wsdisplay.h"
#include "wskbd.h"
#include <dev/wscons/wsmuxvar.h>
#if defined(WSMUX_DEBUG) && NWSMUX > 0
#define DPRINTF(x) if (wsmuxdebug) printf x
extern int wsmuxdebug;
#else
#define DPRINTF(x)
#endif
struct wsmouse_softc {
struct wsevsrc sc_base;
const struct wsmouse_accessops *sc_accessops;
void *sc_accesscookie;
struct wsmouseinput sc_input;
int sc_refcnt;
u_char sc_dying; /* device is being detached */
};
int wsmouse_match(struct device *, void *, void *);
void wsmouse_attach(struct device *, struct device *, void *);
int wsmouse_detach(struct device *, int);
int wsmouse_activate(struct device *, int);
int wsmouse_do_ioctl(struct wsmouse_softc *, u_long, caddr_t,
int, struct proc *);
#if NWSMUX > 0
int wsmouse_mux_open(struct wsevsrc *, struct wseventvar *);
int wsmouse_mux_close(struct wsevsrc *);
#endif
int wsmousedoioctl(struct device *, u_long, caddr_t, int,
struct proc *);
int wsmousedoopen(struct wsmouse_softc *, struct wseventvar *);
struct cfdriver wsmouse_cd = {
NULL, "wsmouse", DV_TTY
};
struct cfattach wsmouse_ca = {
sizeof (struct wsmouse_softc), wsmouse_match, wsmouse_attach,
wsmouse_detach, wsmouse_activate
};
#if NWSMUX > 0
struct wssrcops wsmouse_srcops = {
.type = WSMUX_MOUSE,
.dopen = wsmouse_mux_open,
.dclose = wsmouse_mux_close,
.dioctl = wsmousedoioctl,
.ddispioctl = NULL,
.dsetdisplay = NULL,
};
#endif
/*
* Print function (for parent devices).
*/
int
wsmousedevprint(void *aux, const char *pnp)
{
if (pnp)
printf("wsmouse at %s", pnp);
return (UNCONF);
}
int
wsmouse_match(struct device *parent, void *match, void *aux)
{
return (1);
}
void
wsmouse_attach(struct device *parent, struct device *self, void *aux)
{
struct wsmouse_softc *sc = (struct wsmouse_softc *)self;
struct wsmousedev_attach_args *ap = aux;
#if NWSMUX > 0
int mux, error;
#endif
sc->sc_accessops = ap->accessops;
sc->sc_accesscookie = ap->accesscookie;
sc->sc_input.evar = &sc->sc_base.me_evp;
#if NWSMUX > 0
sc->sc_base.me_ops = &wsmouse_srcops;
mux = sc->sc_base.me_dv.dv_cfdata->wsmousedevcf_mux;
if (mux >= 0) {
error = wsmux_attach_sc(wsmux_getmux(mux), &sc->sc_base);
if (error)
printf(" attach error=%d", error);
else
printf(" mux %d", mux);
}
#else
#if 0 /* not worth keeping, especially since the default value is not -1... */
if (sc->sc_base.me_dv.dv_cfdata->wsmousedevcf_mux >= 0)
printf(" (mux ignored)");
#endif
#endif /* NWSMUX > 0 */
printf("\n");
}
int
wsmouse_activate(struct device *self, int act)
{
struct wsmouse_softc *sc = (struct wsmouse_softc *)self;
if (act == DVACT_DEACTIVATE)
sc->sc_dying = 1;
return (0);
}
/*
* Detach a mouse. To keep track of users of the softc we keep
* a reference count that's incremented while inside, e.g., read.
* If the mouse is active and the reference count is > 0 (0 is the
* normal state) we post an event and then wait for the process
* that had the reference to wake us up again. Then we blow away the
* vnode and return (which will deallocate the softc).
*/
int
wsmouse_detach(struct device *self, int flags)
{
struct wsmouse_softc *sc = (struct wsmouse_softc *)self;
struct wseventvar *evar;
int maj, mn;
int s;
#if NWSMUX > 0
/* Tell parent mux we're leaving. */
if (sc->sc_base.me_parent != NULL) {
DPRINTF(("%s\n", __func__));
wsmux_detach_sc(&sc->sc_base);
}
#endif
/* If we're open ... */
evar = sc->sc_base.me_evp;
if (evar != NULL) {
s = spltty();
if (--sc->sc_refcnt >= 0) {
/* Wake everyone by generating a dummy event. */
if (++evar->put >= WSEVENT_QSIZE)
evar->put = 0;
WSEVENT_WAKEUP(evar);
/* Wait for processes to go away. */
if (tsleep_nsec(sc, PZERO, "wsmdet", SEC_TO_NSEC(60)))
printf("wsmouse_detach: %s didn't detach\n",
sc->sc_base.me_dv.dv_xname);
}
splx(s);
}
/* locate the major number */
for (maj = 0; maj < nchrdev; maj++)
if (cdevsw[maj].d_open == wsmouseopen)
break;
/* Nuke the vnodes for any open instances (calls close). */
mn = self->dv_unit;
vdevgone(maj, mn, mn, VCHR);
wsmouse_input_cleanup(&sc->sc_input);
return (0);
}
int
wsmouseopen(dev_t dev, int flags, int mode, struct proc *p)
{
struct wsmouse_softc *sc;
struct wseventvar *evar;
int error, unit;
unit = minor(dev);
if (unit >= wsmouse_cd.cd_ndevs || /* make sure it was attached */
(sc = wsmouse_cd.cd_devs[unit]) == NULL)
return (ENXIO);
#if NWSMUX > 0
DPRINTF(("%s: %s mux=%p\n", __func__, sc->sc_base.me_dv.dv_xname,
sc->sc_base.me_parent));
#endif
if (sc->sc_dying)
return (EIO);
if ((flags & (FREAD | FWRITE)) == FWRITE)
return (0); /* always allow open for write
so ioctl() is possible. */
#if NWSMUX > 0
if (sc->sc_base.me_parent != NULL) {
/* Grab the mouse out of the greedy hands of the mux. */
DPRINTF(("%s: detach\n", __func__));
wsmux_detach_sc(&sc->sc_base);
}
#endif
if (sc->sc_base.me_evp != NULL)
return (EBUSY);
evar = &sc->sc_base.me_evar;
if (wsevent_init(evar))
return (EBUSY);
error = wsmousedoopen(sc, evar);
if (error)
wsevent_fini(evar);
return (error);
}
int
wsmouseclose(dev_t dev, int flags, int mode, struct proc *p)
{
struct wsmouse_softc *sc =
(struct wsmouse_softc *)wsmouse_cd.cd_devs[minor(dev)];
struct wseventvar *evar = sc->sc_base.me_evp;
if ((flags & (FREAD | FWRITE)) == FWRITE)
/* Not open for read */
return (0);
sc->sc_base.me_evp = NULL;
(*sc->sc_accessops->disable)(sc->sc_accesscookie);
wsevent_fini(evar);
#if NWSMUX > 0
if (sc->sc_base.me_parent == NULL) {
int mux, error;
DPRINTF(("%s: attach\n", __func__));
mux = sc->sc_base.me_dv.dv_cfdata->wsmousedevcf_mux;
if (mux >= 0) {
error = wsmux_attach_sc(wsmux_getmux(mux), &sc->sc_base);
if (error)
printf("%s: can't attach mux (error=%d)\n",
sc->sc_base.me_dv.dv_xname, error);
}
}
#endif
return (0);
}
int
wsmousedoopen(struct wsmouse_softc *sc, struct wseventvar *evp)
{
int error;
/* The device could already be attached to a mux. */
if (sc->sc_base.me_evp != NULL)
return (EBUSY);
sc->sc_base.me_evp = evp;
wsmouse_input_reset(&sc->sc_input);
/* enable the device, and punt if that's not possible */
error = (*sc->sc_accessops->enable)(sc->sc_accesscookie);
if (error)
sc->sc_base.me_evp = NULL;
return (error);
}
int
wsmouseread(dev_t dev, struct uio *uio, int flags)
{
struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)];
int error;
if (sc->sc_dying)
return (EIO);
#ifdef DIAGNOSTIC
if (sc->sc_base.me_evp == NULL) {
printf("wsmouseread: evp == NULL\n");
return (EINVAL);
}
#endif
sc->sc_refcnt++;
error = wsevent_read(sc->sc_base.me_evp, uio, flags);
if (--sc->sc_refcnt < 0) {
wakeup(sc);
error = EIO;
}
return (error);
}
int
wsmouseioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
{
return (wsmousedoioctl(wsmouse_cd.cd_devs[minor(dev)],
cmd, data, flag, p));
}
/* A wrapper around the ioctl() workhorse to make reference counting easy. */
int
wsmousedoioctl(struct device *dv, u_long cmd, caddr_t data, int flag,
struct proc *p)
{
struct wsmouse_softc *sc = (struct wsmouse_softc *)dv;
int error;
sc->sc_refcnt++;
error = wsmouse_do_ioctl(sc, cmd, data, flag, p);
if (--sc->sc_refcnt < 0)
wakeup(sc);
return (error);
}
int
wsmouse_param_ioctl(struct wsmouse_softc *sc,
u_long cmd, struct wsmouse_param *params, u_int nparams)
{
struct wsmouse_param *buf;
int error, s, size;
if (params == NULL || nparams > WSMOUSECFG_MAX)
return (EINVAL);
size = nparams * sizeof(struct wsmouse_param);
buf = malloc(size, M_DEVBUF, M_WAITOK);
if (buf == NULL)
return (ENOMEM);
if ((error = copyin(params, buf, size))) {
free(buf, M_DEVBUF, size);
return (error);
}
s = spltty();
if (cmd == WSMOUSEIO_SETPARAMS) {
if (wsmouse_set_params((struct device *) sc, buf, nparams))
error = EINVAL;
} else {
if (wsmouse_get_params((struct device *) sc, buf, nparams))
error = EINVAL;
else
error = copyout(buf, params, size);
}
splx(s);
free(buf, M_DEVBUF, size);
return (error);
}
int
wsmouse_do_ioctl(struct wsmouse_softc *sc, u_long cmd, caddr_t data, int flag,
struct proc *p)
{
struct wseventvar *evar;
int error;
if (sc->sc_dying)
return (EIO);
/*
* Try the generic ioctls that the wsmouse interface supports.
*/
switch (cmd) {
case FIOASYNC:
case FIOSETOWN:
case TIOCSPGRP:
if ((flag & FWRITE) == 0)
return (EACCES);
}
switch (cmd) {
case FIONBIO: /* we will remove this someday (soon???) */
return (0);
case FIOASYNC:
if (sc->sc_base.me_evp == NULL)
return (EINVAL);
sc->sc_base.me_evp->async = *(int *)data != 0;
return (0);
case FIOGETOWN:
case TIOCGPGRP:
evar = sc->sc_base.me_evp;
if (evar == NULL)
return (EINVAL);
sigio_getown(&evar->sigio, cmd, data);
return (0);
case FIOSETOWN:
case TIOCSPGRP:
evar = sc->sc_base.me_evp;
if (evar == NULL)
return (EINVAL);
return (sigio_setown(&evar->sigio, cmd, data));
case WSMOUSEIO_GETPARAMS:
case WSMOUSEIO_SETPARAMS:
return (wsmouse_param_ioctl(sc, cmd,
((struct wsmouse_parameters *) data)->params,
((struct wsmouse_parameters *) data)->nparams));
}
/*
* Try the mouse driver for WSMOUSEIO ioctls. It returns -1
* if it didn't recognize the request.
*/
error = (*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd,
data, flag, p);
return (error != -1 ? error : ENOTTY);
}
int
wsmousepoll(dev_t dev, int events, struct proc *p)
{
struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)];
if (sc->sc_base.me_evp == NULL)
return (POLLERR);
return (wsevent_poll(sc->sc_base.me_evp, events, p));
}
int
wsmousekqfilter(dev_t dev, struct knote *kn)
{
struct wsmouse_softc *sc = wsmouse_cd.cd_devs[minor(dev)];
if (sc->sc_base.me_evp == NULL)
return (ENXIO);
return (wsevent_kqfilter(sc->sc_base.me_evp, kn));
}
#if NWSMUX > 0
int
wsmouse_mux_open(struct wsevsrc *me, struct wseventvar *evp)
{
struct wsmouse_softc *sc = (struct wsmouse_softc *)me;
return (wsmousedoopen(sc, evp));
}
int
wsmouse_mux_close(struct wsevsrc *me)
{
struct wsmouse_softc *sc = (struct wsmouse_softc *)me;
sc->sc_base.me_evp = NULL;
(*sc->sc_accessops->disable)(sc->sc_accesscookie);
return (0);
}
int
wsmouse_add_mux(int unit, struct wsmux_softc *muxsc)
{
struct wsmouse_softc *sc;
if (unit < 0 || unit >= wsmouse_cd.cd_ndevs ||
(sc = wsmouse_cd.cd_devs[unit]) == NULL)
return (ENXIO);
if (sc->sc_base.me_parent != NULL || sc->sc_base.me_evp != NULL)
return (EBUSY);
return (wsmux_attach_sc(muxsc, &sc->sc_base));
}
#endif /* NWSMUX > 0 */
void
wsmouse_buttons(struct device *sc, u_int buttons)
{
struct btn_state *btn = &((struct wsmouse_softc *) sc)->sc_input.btn;
if (btn->sync)
/* Restore the old state. */
btn->buttons ^= btn->sync;
btn->sync = btn->buttons ^ buttons;
btn->buttons = buttons;
}
void
wsmouse_motion(struct device *sc, int dx, int dy, int dz, int dw)
{
struct motion_state *motion =
&((struct wsmouse_softc *) sc)->sc_input.motion;
motion->dx = dx;
motion->dy = dy;
motion->dz = dz;
motion->dw = dw;
if (dx || dy || dz || dw)
motion->sync |= SYNC_DELTAS;
}
static inline void
set_x(struct position *pos, int x, u_int *sync, u_int mask)
{
if (*sync & mask) {
if (x == pos->x)
return;
pos->x -= pos->dx;
pos->acc_dx -= pos->dx;
}
if ((pos->dx = x - pos->x)) {
pos->x = x;
if ((pos->dx > 0) == (pos->acc_dx > 0))
pos->acc_dx += pos->dx;
else
pos->acc_dx = pos->dx;
*sync |= mask;
}
}
static inline void
set_y(struct position *pos, int y, u_int *sync, u_int mask)
{
if (*sync & mask) {
if (y == pos->y)
return;
pos->y -= pos->dy;
pos->acc_dy -= pos->dy;
}
if ((pos->dy = y - pos->y)) {
pos->y = y;
if ((pos->dy > 0) == (pos->acc_dy > 0))
pos->acc_dy += pos->dy;
else
pos->acc_dy = pos->dy;
*sync |= mask;
}
}
static inline void
cleardeltas(struct position *pos)
{
pos->dx = pos->acc_dx = 0;
pos->dy = pos->acc_dy = 0;
}
void
wsmouse_position(struct device *sc, int x, int y)
{
struct motion_state *motion =
&((struct wsmouse_softc *) sc)->sc_input.motion;
set_x(&motion->pos, x, &motion->sync, SYNC_X);
set_y(&motion->pos, y, &motion->sync, SYNC_Y);
}
static inline int
normalized_pressure(struct wsmouseinput *input, int pressure)
{
int limit = imax(input->touch.min_pressure, 1);
if (pressure >= limit)
return pressure;
else
return (pressure < 0 ? limit : 0);
}
void
wsmouse_touch(struct device *sc, int pressure, int contacts)
{
struct wsmouseinput *input = &((struct wsmouse_softc *) sc)->sc_input;
struct touch_state *touch = &input->touch;
pressure = normalized_pressure(input, pressure);
contacts = (pressure ? imax(contacts, 1) : 0);
if (pressure == 0 || pressure != touch->pressure) {
/*
* pressure == 0: Drivers may report possibly arbitrary
* coordinates in this case; touch_update will correct them.
*/
touch->pressure = pressure;
touch->sync |= SYNC_PRESSURE;
}
if (contacts != touch->contacts) {
touch->contacts = contacts;
touch->sync |= SYNC_CONTACTS;
}
}
void
wsmouse_mtstate(struct device *sc, int slot, int x, int y, int pressure)
{
struct wsmouseinput *input = &((struct wsmouse_softc *) sc)->sc_input;
struct mt_state *mt = &input->mt;
struct mt_slot *mts;
u_int bit;
if (slot < 0 || slot >= mt->num_slots)
return;
bit = (1 << slot);
mt->frame |= bit;
mts = &mt->slots[slot];
set_x(&mts->pos, x, mt->sync + MTS_X, bit);
set_y(&mts->pos, y, mt->sync + MTS_Y, bit);
/* Is this a new touch? */
if ((mt->touches & bit) == (mt->sync[MTS_TOUCH] & bit))
cleardeltas(&mts->pos);
pressure = normalized_pressure(input, pressure);
if (pressure != mts->pressure) {
mts->pressure = pressure;
mt->sync[MTS_PRESSURE] |= bit;
if (pressure) {
if ((mt->touches & bit) == 0) {
mt->num_touches++;
mt->touches |= bit;
mt->sync[MTS_TOUCH] |= bit;
mt->sync[MTS_X] |= bit;
mt->sync[MTS_Y] |= bit;
}
} else if (mt->touches & bit) {
mt->num_touches--;
mt->touches ^= bit;
mt->sync[MTS_TOUCH] |= bit;
mt->ptr_mask &= mt->touches;
}
}
}
void
wsmouse_set(struct device *sc, enum wsmouseval type, int value, int aux)
{
struct wsmouseinput *input = &((struct wsmouse_softc *) sc)->sc_input;
struct mt_slot *mts;
if (WSMOUSE_IS_MT_CODE(type)) {
if (aux < 0 || aux >= input->mt.num_slots)
return;
mts = &input->mt.slots[aux];
}
switch (type) {
case WSMOUSE_REL_X:
value += input->motion.pos.x; /* fall through */
case WSMOUSE_ABS_X:
wsmouse_position(sc, value, input->motion.pos.y);
return;
case WSMOUSE_REL_Y:
value += input->motion.pos.y; /* fall through */
case WSMOUSE_ABS_Y:
wsmouse_position(sc, input->motion.pos.x, value);
return;
case WSMOUSE_PRESSURE:
wsmouse_touch(sc, value, input->touch.contacts);
return;
case WSMOUSE_CONTACTS:
/* Contact counts can be overridden by wsmouse_touch. */
if (value != input->touch.contacts) {
input->touch.contacts = value;
input->touch.sync |= SYNC_CONTACTS;
}
return;
case WSMOUSE_TOUCH_WIDTH:
if (value != input->touch.width) {
input->touch.width = value;
input->touch.sync |= SYNC_TOUCH_WIDTH;
}
return;
case WSMOUSE_MT_REL_X:
value += mts->pos.x; /* fall through */
case WSMOUSE_MT_ABS_X:
wsmouse_mtstate(sc, aux, value, mts->pos.y, mts->pressure);
return;
case WSMOUSE_MT_REL_Y:
value += mts->pos.y; /* fall through */
case WSMOUSE_MT_ABS_Y:
wsmouse_mtstate(sc, aux, mts->pos.x, value, mts->pressure);
return;
case WSMOUSE_MT_PRESSURE:
wsmouse_mtstate(sc, aux, mts->pos.x, mts->pos.y, value);
return;
}
}
/* Make touch and motion state consistent. */
void
wsmouse_touch_update(struct wsmouseinput *input)
{
struct motion_state *motion = &input->motion;
struct touch_state *touch = &input->touch;
if (touch->pressure == 0) {
/*
* There may be zero coordinates, or coordinates of
* touches with pressure values below min_pressure.
*/
if (motion->sync & SYNC_POSITION) {
/* Restore valid coordinates. */
motion->pos.x -= motion->pos.dx;
motion->pos.y -= motion->pos.dy;
motion->sync &= ~SYNC_POSITION;
}
if (touch->prev_contacts == 0)
touch->sync &= ~SYNC_PRESSURE;
}
if (touch->sync & SYNC_CONTACTS)
/* Suppress pointer movement. */
cleardeltas(&motion->pos);
if ((touch->sync & SYNC_PRESSURE) && touch->min_pressure) {
if (touch->pressure >= input->filter.pressure_hi)
touch->min_pressure = input->filter.pressure_lo;
else if (touch->pressure < input->filter.pressure_lo)
touch->min_pressure = input->filter.pressure_hi;
}
}
/* Normalize multitouch state. */
void
wsmouse_mt_update(struct wsmouseinput *input)
{
int i;
/*
* The same as above: There may be arbitrary coordinates if
* (pressure == 0). Clear the sync flags for touches that have
* been released.
*/
if (input->mt.frame & ~input->mt.touches) {
for (i = MTS_X; i < MTS_SIZE; i++)
input->mt.sync[i] &= input->mt.touches;
}
}
/* Return TRUE if a coordinate update may be noise. */
int
wsmouse_hysteresis(struct wsmouseinput *input, struct position *pos)
{
return (abs(pos->acc_dx) < input->filter.h.hysteresis
&& abs(pos->acc_dy) < input->filter.v.hysteresis);
}
/*
* Select the pointer-controlling MT slot.
*
* Pointer-control is assigned to slots with non-zero motion deltas if
* at least one such slot exists. This function doesn't impose any
* restrictions on the way drivers use wsmouse_mtstate(), it covers
* partial, unordered, and "delta-filtered" input.
*
* The "cycle" is the set of slots with X/Y updates in previous sync
* operations; it will be cleared and rebuilt whenever a slot that is
* being updated is already a member. If a cycle ends that doesn't
* contain the pointer-controlling slot, a new slot will be selected.
*/
void
wsmouse_ptr_ctrl(struct wsmouseinput *input)
{
struct mt_state *mt = &input->mt;
u_int updates;
int select, slot;
mt->prev_ptr = mt->ptr;
if (mt->num_touches <= 1) {
mt->ptr = mt->touches;
mt->ptr_cycle = mt->ptr;
return;
}
updates = (mt->sync[MTS_X] | mt->sync[MTS_Y]) & ~mt->sync[MTS_TOUCH];
FOREACHBIT(updates, slot) {
/*
* Touches that just produce noise are no problem if the
* frequency of zero deltas is high enough, but there might
* be no guarantee for that.
*/
if (wsmouse_hysteresis(input, &mt->slots[slot].pos))
updates ^= (1 << slot);
}
/*
* If there is no pointer-controlling slot, or if it should be
* masked, select a new one.
*/
select = ((mt->ptr & mt->touches & ~mt->ptr_mask) == 0);
/* Remove slots without coordinate deltas from the cycle. */
mt->ptr_cycle &= ~(mt->frame ^ updates);
if (mt->ptr_cycle & updates) {
select |= ((mt->ptr_cycle & mt->ptr) == 0);
mt->ptr_cycle = updates;
} else {
mt->ptr_cycle |= updates;
}
if (select) {
if (mt->ptr_cycle & ~mt->ptr_mask)
slot = ffs(mt->ptr_cycle & ~mt->ptr_mask) - 1;
else if (mt->touches & ~mt->ptr_mask)
slot = ffs(mt->touches & ~mt->ptr_mask) - 1;
else
slot = ffs(mt->touches) - 1;
mt->ptr = (1 << slot);
}
}
/* Derive touch and motion state from MT state. */
void
wsmouse_mt_convert(struct device *sc)
{
struct wsmouseinput *input = &((struct wsmouse_softc *) sc)->sc_input;
struct mt_state *mt = &input->mt;
struct mt_slot *mts;
int slot, pressure;
wsmouse_ptr_ctrl(input);
if (mt->ptr) {
slot = ffs(mt->ptr) - 1;
mts = &mt->slots[slot];
if (mts->pos.x != input->motion.pos.x)
input->motion.sync |= SYNC_X;
if (mts->pos.y != input->motion.pos.y)
input->motion.sync |= SYNC_Y;
if (mt->ptr != mt->prev_ptr)
/* Suppress pointer movement. */
mts->pos.dx = mts->pos.dy = 0;
memcpy(&input->motion.pos, &mts->pos, sizeof(struct position));
pressure = mts->pressure;
} else {
pressure = 0;
}
wsmouse_touch(sc, pressure, mt->num_touches);
}
void
wsmouse_evq_put(struct evq_access *evq, int ev_type, int ev_value)
{
struct wscons_event *ev;
int space;
space = evq->evar->get - evq->put;
if (space != 1 && space != 1 - WSEVENT_QSIZE) {
ev = &evq->evar->q[evq->put++];
evq->put %= WSEVENT_QSIZE;
ev->type = ev_type;
ev->value = ev_value;
memcpy(&ev->time, &evq->ts, sizeof(struct timespec));
evq->result |= EVQ_RESULT_SUCCESS;
} else {
evq->result = EVQ_RESULT_OVERFLOW;
}
}
void
wsmouse_btn_sync(struct btn_state *btn, struct evq_access *evq)
{
int button, ev_type;
u_int bit, sync;
for (sync = btn->sync; sync; sync ^= bit) {
button = ffs(sync) - 1;
bit = (1 << button);
ev_type = (btn->buttons & bit) ? BTN_DOWN_EV : BTN_UP_EV;
wsmouse_evq_put(evq, ev_type, button);
}
}
/*