forked from wayland-tablet/libinput
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
evdev-mt-touchpad.c
3826 lines (3234 loc) · 96.6 KB
/
evdev-mt-touchpad.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 © 2014-2015 Red Hat, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "config.h"
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <limits.h>
#if HAVE_LIBWACOM
#include <libwacom/libwacom.h>
#endif
#include "quirks.h"
#include "evdev-mt-touchpad.h"
#define DEFAULT_TRACKPOINT_ACTIVITY_TIMEOUT ms2us(300)
#define DEFAULT_TRACKPOINT_EVENT_TIMEOUT ms2us(40)
#define DEFAULT_KEYBOARD_ACTIVITY_TIMEOUT_1 ms2us(200)
#define DEFAULT_KEYBOARD_ACTIVITY_TIMEOUT_2 ms2us(500)
#define THUMB_MOVE_TIMEOUT ms2us(300)
#define FAKE_FINGER_OVERFLOW (1 << 7)
#define THUMB_IGNORE_SPEED_THRESHOLD 20 /* mm/s */
static inline struct tp_history_point*
tp_motion_history_offset(struct tp_touch *t, int offset)
{
int offset_index =
(t->history.index - offset + TOUCHPAD_HISTORY_LENGTH) %
TOUCHPAD_HISTORY_LENGTH;
return &t->history.samples[offset_index];
}
struct normalized_coords
tp_filter_motion(struct tp_dispatch *tp,
const struct device_float_coords *unaccelerated,
uint64_t time)
{
struct device_float_coords raw;
const struct normalized_coords zero = { 0.0, 0.0 };
if (device_float_is_zero(*unaccelerated))
return zero;
/* Convert to device units with x/y in the same resolution */
raw = tp_scale_to_xaxis(tp, *unaccelerated);
return filter_dispatch(tp->device->pointer.filter,
&raw, tp, time);
}
struct normalized_coords
tp_filter_motion_unaccelerated(struct tp_dispatch *tp,
const struct device_float_coords *unaccelerated,
uint64_t time)
{
struct device_float_coords raw;
const struct normalized_coords zero = { 0.0, 0.0 };
if (device_float_is_zero(*unaccelerated))
return zero;
/* Convert to device units with x/y in the same resolution */
raw = tp_scale_to_xaxis(tp, *unaccelerated);
return filter_dispatch_constant(tp->device->pointer.filter,
&raw, tp, time);
}
static inline void
tp_calculate_motion_speed(struct tp_dispatch *tp, struct tp_touch *t)
{
const struct tp_history_point *last;
struct device_coords delta;
struct phys_coords mm;
double distance;
double speed;
/* Don't do this on single-touch or semi-mt devices */
if (!tp->has_mt || tp->semi_mt)
return;
if (t->state != TOUCH_UPDATE)
return;
/* This doesn't kick in until we have at least 4 events in the
* motion history. As a side-effect, this automatically handles the
* 2fg scroll where a finger is down and moving fast before the
* other finger comes down for the scroll.
*
* We do *not* reset the speed to 0 here though. The motion history
* is reset whenever a new finger is down, so we'd be resetting the
* speed and failing.
*/
if (t->history.count < 4)
return;
/* TODO: we probably need a speed history here so we can average
* across a few events */
last = tp_motion_history_offset(t, 1);
delta.x = abs(t->point.x - last->point.x);
delta.y = abs(t->point.y - last->point.y);
mm = evdev_device_unit_delta_to_mm(tp->device, &delta);
distance = length_in_mm(mm);
speed = distance/(t->time - last->time); /* mm/us */
speed *= 1000000; /* mm/s */
t->speed.last_speed = speed;
}
static inline void
tp_motion_history_push(struct tp_touch *t)
{
int motion_index = (t->history.index + 1) % TOUCHPAD_HISTORY_LENGTH;
if (t->history.count < TOUCHPAD_HISTORY_LENGTH)
t->history.count++;
t->history.samples[motion_index].point = t->point;
t->history.samples[motion_index].time = t->time;
t->history.index = motion_index;
}
/* Idea: if we got a tuple of *very* quick moves like {Left, Right,
* Left}, or {Right, Left, Right}, it means touchpad jitters since no
* human can move like that within thresholds.
*
* We encode left moves as zeroes, and right as ones. We also drop
* the array to all zeroes when contraints are not satisfied. Then we
* search for the pattern {1,0,1}. It can't match {Left, Right, Left},
* but it does match {Left, Right, Left, Right}, so it's okay.
*
* This only looks at x changes, y changes are ignored.
*/
static inline void
tp_detect_wobbling(struct tp_dispatch *tp,
struct tp_touch *t,
uint64_t time)
{
int dx, dy;
uint64_t dtime;
const struct device_coords* prev_point;
if (tp->nfingers_down != 1 ||
tp->nfingers_down != tp->old_nfingers_down)
return;
if (tp->hysteresis.enabled || t->history.count == 0)
return;
if (!(tp->queued & TOUCHPAD_EVENT_MOTION)) {
t->hysteresis.x_motion_history = 0;
return;
}
prev_point = &tp_motion_history_offset(t, 0)->point;
dx = prev_point->x - t->point.x;
dy = prev_point->y - t->point.y;
dtime = time - tp->hysteresis.last_motion_time;
tp->hysteresis.last_motion_time = time;
if ((dx == 0 && dy != 0) || dtime > ms2us(40)) {
t->hysteresis.x_motion_history = 0;
return;
}
t->hysteresis.x_motion_history >>= 1;
if (dx > 0) { /* right move */
static const char r_l_r = 0x5; /* {Right, Left, Right} */
t->hysteresis.x_motion_history |= (1 << 2);
if (t->hysteresis.x_motion_history == r_l_r) {
tp->hysteresis.enabled = true;
evdev_log_debug(tp->device,
"hysteresis enabled. "
"See %stouchpad-jitter.html for details\n",
HTTP_DOC_LINK);
}
}
}
static inline void
tp_motion_hysteresis(struct tp_dispatch *tp,
struct tp_touch *t)
{
if (!tp->hysteresis.enabled)
return;
if (t->history.count > 0)
t->point = evdev_hysteresis(&t->point,
&t->hysteresis.center,
&tp->hysteresis.margin);
t->hysteresis.center = t->point;
}
static inline void
tp_motion_history_reset(struct tp_touch *t)
{
t->history.count = 0;
}
static inline struct tp_touch *
tp_current_touch(struct tp_dispatch *tp)
{
return &tp->touches[min(tp->slot, tp->ntouches - 1)];
}
static inline struct tp_touch *
tp_get_touch(struct tp_dispatch *tp, unsigned int slot)
{
assert(slot < tp->ntouches);
return &tp->touches[slot];
}
static inline unsigned int
tp_fake_finger_count(struct tp_dispatch *tp)
{
/* Only one of BTN_TOOL_DOUBLETAP/TRIPLETAP/... may be set at any
* time */
if (__builtin_popcount(
tp->fake_touches & ~(FAKE_FINGER_OVERFLOW|0x1)) > 1)
evdev_log_bug_kernel(tp->device,
"Invalid fake finger state %#x\n",
tp->fake_touches);
if (tp->fake_touches & FAKE_FINGER_OVERFLOW)
return FAKE_FINGER_OVERFLOW;
else /* don't count BTN_TOUCH */
return ffs(tp->fake_touches >> 1);
}
static inline bool
tp_fake_finger_is_touching(struct tp_dispatch *tp)
{
return tp->fake_touches & 0x1;
}
static inline void
tp_fake_finger_set(struct tp_dispatch *tp,
unsigned int code,
bool is_press)
{
unsigned int shift;
switch (code) {
case BTN_TOUCH:
if (!is_press)
tp->fake_touches &= ~FAKE_FINGER_OVERFLOW;
shift = 0;
break;
case BTN_TOOL_FINGER:
shift = 1;
break;
case BTN_TOOL_DOUBLETAP:
case BTN_TOOL_TRIPLETAP:
case BTN_TOOL_QUADTAP:
shift = code - BTN_TOOL_DOUBLETAP + 2;
break;
/* when QUINTTAP is released we're either switching to 6 fingers
(flag stays in place until BTN_TOUCH is released) or
one of DOUBLE/TRIPLE/QUADTAP (will clear the flag on press) */
case BTN_TOOL_QUINTTAP:
if (is_press)
tp->fake_touches |= FAKE_FINGER_OVERFLOW;
return;
default:
return;
}
if (is_press) {
tp->fake_touches &= ~FAKE_FINGER_OVERFLOW;
tp->fake_touches |= 1 << shift;
} else {
tp->fake_touches &= ~(0x1 << shift);
}
}
static inline void
tp_new_touch(struct tp_dispatch *tp, struct tp_touch *t, uint64_t time)
{
if (t->state == TOUCH_BEGIN ||
t->state == TOUCH_UPDATE ||
t->state == TOUCH_HOVERING)
return;
/* Bug #161: touch ends in the same event frame where it restarts
again. That's a kernel bug, so let's complain. */
if (t->state == TOUCH_MAYBE_END) {
evdev_log_bug_kernel(tp->device,
"touch %d ended and began in in same frame.\n",
t->index);
tp->nfingers_down++;
t->state = TOUCH_UPDATE;
t->has_ended = false;
return;
}
/* we begin the touch as hovering because until BTN_TOUCH happens we
* don't know if it's a touch down or not. And BTN_TOUCH may happen
* after ABS_MT_TRACKING_ID */
tp_motion_history_reset(t);
t->dirty = true;
t->has_ended = false;
t->was_down = false;
t->palm.state = PALM_NONE;
t->state = TOUCH_HOVERING;
t->pinned.is_pinned = false;
t->time = time;
t->speed.last_speed = 0;
t->speed.exceeded_count = 0;
t->hysteresis.x_motion_history = 0;
tp->queued |= TOUCHPAD_EVENT_MOTION;
}
static inline void
tp_begin_touch(struct tp_dispatch *tp, struct tp_touch *t, uint64_t time)
{
t->dirty = true;
t->state = TOUCH_BEGIN;
t->time = time;
t->was_down = true;
tp->nfingers_down++;
t->palm.time = time;
t->thumb.state = THUMB_STATE_MAYBE;
t->thumb.first_touch_time = time;
t->tap.is_thumb = false;
t->tap.is_palm = false;
t->speed.exceeded_count = 0;
assert(tp->nfingers_down >= 1);
tp->hysteresis.last_motion_time = time;
}
/**
* Schedule a touch to be ended, based on either the events or some
* attributes of the touch (size, pressure). In some cases we need to
* resurrect a touch that has ended, so this doesn't actually end the touch
* yet. All the TOUCH_MAYBE_END touches get properly ended once the device
* state has been processed once and we know how many zombie touches we
* need.
*/
static inline void
tp_maybe_end_touch(struct tp_dispatch *tp,
struct tp_touch *t,
uint64_t time)
{
switch (t->state) {
case TOUCH_NONE:
case TOUCH_MAYBE_END:
return;
case TOUCH_END:
evdev_log_bug_libinput(tp->device,
"touch %d: already in TOUCH_END\n",
t->index);
return;
case TOUCH_HOVERING:
case TOUCH_BEGIN:
case TOUCH_UPDATE:
break;
}
if (t->state != TOUCH_HOVERING) {
assert(tp->nfingers_down >= 1);
tp->nfingers_down--;
t->state = TOUCH_MAYBE_END;
} else {
t->state = TOUCH_NONE;
}
t->dirty = true;
}
/**
* Inverse to tp_maybe_end_touch(), restores a touch back to its previous
* state.
*/
static inline void
tp_recover_ended_touch(struct tp_dispatch *tp,
struct tp_touch *t)
{
t->dirty = true;
t->state = TOUCH_UPDATE;
tp->nfingers_down++;
}
/**
* End a touch, even if the touch sequence is still active.
* Use tp_maybe_end_touch() instead.
*/
static inline void
tp_end_touch(struct tp_dispatch *tp, struct tp_touch *t, uint64_t time)
{
if (t->state != TOUCH_MAYBE_END) {
evdev_log_bug_libinput(tp->device,
"touch %d should be MAYBE_END, is %d\n",
t->index,
t->state);
return;
}
t->dirty = true;
t->palm.state = PALM_NONE;
t->state = TOUCH_END;
t->pinned.is_pinned = false;
t->time = time;
t->palm.time = 0;
t->speed.exceeded_count = 0;
tp->queued |= TOUCHPAD_EVENT_MOTION;
}
/**
* End the touch sequence on ABS_MT_TRACKING_ID -1 or when the BTN_TOOL_* 0 is received.
*/
static inline void
tp_end_sequence(struct tp_dispatch *tp, struct tp_touch *t, uint64_t time)
{
t->has_ended = true;
tp_maybe_end_touch(tp, t, time);
}
static void
tp_stop_actions(struct tp_dispatch *tp, uint64_t time)
{
tp_edge_scroll_stop_events(tp, time);
tp_gesture_cancel(tp, time);
tp_tap_suspend(tp, time);
}
struct device_coords
tp_get_delta(struct tp_touch *t)
{
struct device_coords delta;
const struct device_coords zero = { 0.0, 0.0 };
if (t->history.count <= 1)
return zero;
delta.x = tp_motion_history_offset(t, 0)->point.x -
tp_motion_history_offset(t, 1)->point.x;
delta.y = tp_motion_history_offset(t, 0)->point.y -
tp_motion_history_offset(t, 1)->point.y;
return delta;
}
static inline int32_t
rotated(struct tp_dispatch *tp, unsigned int code, int value)
{
const struct input_absinfo *absinfo;
if (!tp->device->left_handed.enabled ||
!tp->left_handed.rotate)
return value;
switch (code) {
case ABS_X:
case ABS_MT_POSITION_X:
absinfo = tp->device->abs.absinfo_x;
break;
case ABS_Y:
case ABS_MT_POSITION_Y:
absinfo = tp->device->abs.absinfo_y;
break;
default:
abort();
}
return absinfo->maximum - (value - absinfo->minimum);
}
static void
tp_process_absolute(struct tp_dispatch *tp,
const struct input_event *e,
uint64_t time)
{
struct tp_touch *t = tp_current_touch(tp);
switch(e->code) {
case ABS_MT_POSITION_X:
evdev_device_check_abs_axis_range(tp->device,
e->code,
e->value);
t->point.x = rotated(tp, e->code, e->value);
t->time = time;
t->dirty = true;
tp->queued |= TOUCHPAD_EVENT_MOTION;
break;
case ABS_MT_POSITION_Y:
evdev_device_check_abs_axis_range(tp->device,
e->code,
e->value);
t->point.y = rotated(tp, e->code, e->value);
t->time = time;
t->dirty = true;
tp->queued |= TOUCHPAD_EVENT_MOTION;
break;
case ABS_MT_SLOT:
tp->slot = e->value;
break;
case ABS_MT_TRACKING_ID:
if (e->value != -1)
tp_new_touch(tp, t, time);
else
tp_end_sequence(tp, t, time);
break;
case ABS_MT_PRESSURE:
t->pressure = e->value;
t->time = time;
t->dirty = true;
tp->queued |= TOUCHPAD_EVENT_OTHERAXIS;
break;
case ABS_MT_TOOL_TYPE:
t->is_tool_palm = e->value == MT_TOOL_PALM;
t->time = time;
t->dirty = true;
tp->queued |= TOUCHPAD_EVENT_OTHERAXIS;
break;
case ABS_MT_TOUCH_MAJOR:
t->major = e->value;
t->dirty = true;
tp->queued |= TOUCHPAD_EVENT_OTHERAXIS;
break;
case ABS_MT_TOUCH_MINOR:
t->minor = e->value;
t->dirty = true;
tp->queued |= TOUCHPAD_EVENT_OTHERAXIS;
break;
}
}
static void
tp_process_absolute_st(struct tp_dispatch *tp,
const struct input_event *e,
uint64_t time)
{
struct tp_touch *t = tp_current_touch(tp);
switch(e->code) {
case ABS_X:
evdev_device_check_abs_axis_range(tp->device,
e->code,
e->value);
t->point.x = rotated(tp, e->code, e->value);
t->time = time;
t->dirty = true;
tp->queued |= TOUCHPAD_EVENT_MOTION;
break;
case ABS_Y:
evdev_device_check_abs_axis_range(tp->device,
e->code,
e->value);
t->point.y = rotated(tp, e->code, e->value);
t->time = time;
t->dirty = true;
tp->queued |= TOUCHPAD_EVENT_MOTION;
break;
case ABS_PRESSURE:
t->pressure = e->value;
t->time = time;
t->dirty = true;
tp->queued |= TOUCHPAD_EVENT_OTHERAXIS;
break;
}
}
static inline void
tp_restore_synaptics_touches(struct tp_dispatch *tp,
uint64_t time)
{
unsigned int i;
unsigned int nfake_touches;
nfake_touches = tp_fake_finger_count(tp);
if (nfake_touches < 3)
return;
if (tp->nfingers_down >= nfake_touches ||
(tp->nfingers_down == tp->num_slots && nfake_touches == tp->num_slots))
return;
/* Synaptics devices may end touch 2 on BTN_TOOL_TRIPLETAP
* and start it again on the next frame with different coordinates
* (#91352). We search the touches we have, if there is one that has
* just ended despite us being on tripletap, we move it back to
* update.
*/
for (i = 0; i < tp->num_slots; i++) {
struct tp_touch *t = tp_get_touch(tp, i);
if (t->state != TOUCH_MAYBE_END)
continue;
/* new touch, move it through begin to update immediately */
tp_recover_ended_touch(tp, t);
}
}
static void
tp_process_fake_touches(struct tp_dispatch *tp,
uint64_t time)
{
struct tp_touch *t;
unsigned int nfake_touches;
unsigned int i, start;
nfake_touches = tp_fake_finger_count(tp);
if (nfake_touches == FAKE_FINGER_OVERFLOW)
return;
if (tp->device->model_flags &
EVDEV_MODEL_SYNAPTICS_SERIAL_TOUCHPAD)
tp_restore_synaptics_touches(tp, time);
start = tp->has_mt ? tp->num_slots : 0;
for (i = start; i < tp->ntouches; i++) {
t = tp_get_touch(tp, i);
if (i < nfake_touches)
tp_new_touch(tp, t, time);
else
tp_end_sequence(tp, t, time);
}
}
static void
tp_process_trackpoint_button(struct tp_dispatch *tp,
const struct input_event *e,
uint64_t time)
{
struct evdev_dispatch *dispatch;
struct input_event event;
struct input_event syn_report = {
.input_event_sec = 0,
.input_event_usec = 0,
.type = EV_SYN,
.code = SYN_REPORT,
.value = 0
};
if (!tp->buttons.trackpoint)
return;
dispatch = tp->buttons.trackpoint->dispatch;
event = *e;
syn_report.input_event_sec = e->input_event_sec;
syn_report.input_event_usec = e->input_event_usec;
switch (event.code) {
case BTN_0:
event.code = BTN_LEFT;
break;
case BTN_1:
event.code = BTN_RIGHT;
break;
case BTN_2:
event.code = BTN_MIDDLE;
break;
default:
return;
}
dispatch->interface->process(dispatch,
tp->buttons.trackpoint,
&event, time);
dispatch->interface->process(dispatch,
tp->buttons.trackpoint,
&syn_report, time);
}
static void
tp_process_key(struct tp_dispatch *tp,
const struct input_event *e,
uint64_t time)
{
switch (e->code) {
case BTN_LEFT:
case BTN_MIDDLE:
case BTN_RIGHT:
tp_process_button(tp, e, time);
break;
case BTN_TOUCH:
case BTN_TOOL_FINGER:
case BTN_TOOL_DOUBLETAP:
case BTN_TOOL_TRIPLETAP:
case BTN_TOOL_QUADTAP:
case BTN_TOOL_QUINTTAP:
tp_fake_finger_set(tp, e->code, !!e->value);
break;
case BTN_0:
case BTN_1:
case BTN_2:
tp_process_trackpoint_button(tp, e, time);
break;
}
}
static void
tp_process_msc(struct tp_dispatch *tp,
const struct input_event *e,
uint64_t time)
{
if (e->code != MSC_TIMESTAMP)
return;
tp->quirks.msc_timestamp.now = e->value;
tp->queued |= TOUCHPAD_EVENT_TIMESTAMP;
}
static void
tp_unpin_finger(const struct tp_dispatch *tp, struct tp_touch *t)
{
struct phys_coords mm;
struct device_coords delta;
if (!t->pinned.is_pinned)
return;
delta.x = abs(t->point.x - t->pinned.center.x);
delta.y = abs(t->point.y - t->pinned.center.y);
mm = evdev_device_unit_delta_to_mm(tp->device, &delta);
/* 1.5mm movement -> unpin */
if (hypot(mm.x, mm.y) >= 1.5) {
t->pinned.is_pinned = false;
return;
}
}
static void
tp_pin_fingers(struct tp_dispatch *tp)
{
struct tp_touch *t;
tp_for_each_touch(tp, t) {
t->pinned.is_pinned = true;
t->pinned.center = t->point;
}
}
bool
tp_touch_active(const struct tp_dispatch *tp, const struct tp_touch *t)
{
return (t->state == TOUCH_BEGIN || t->state == TOUCH_UPDATE) &&
t->palm.state == PALM_NONE &&
!t->pinned.is_pinned &&
t->thumb.state != THUMB_STATE_YES &&
tp_button_touch_active(tp, t) &&
tp_edge_scroll_touch_active(tp, t);
}
static inline bool
tp_palm_was_in_side_edge(const struct tp_dispatch *tp, const struct tp_touch *t)
{
return t->palm.first.x < tp->palm.left_edge ||
t->palm.first.x > tp->palm.right_edge;
}
static inline bool
tp_palm_was_in_top_edge(const struct tp_dispatch *tp, const struct tp_touch *t)
{
return t->palm.first.y < tp->palm.upper_edge;
}
static inline bool
tp_palm_in_side_edge(const struct tp_dispatch *tp, const struct tp_touch *t)
{
return t->point.x < tp->palm.left_edge ||
t->point.x > tp->palm.right_edge;
}
static inline bool
tp_palm_in_top_edge(const struct tp_dispatch *tp, const struct tp_touch *t)
{
return t->point.y < tp->palm.upper_edge;
}
static inline bool
tp_palm_in_edge(const struct tp_dispatch *tp, const struct tp_touch *t)
{
return tp_palm_in_side_edge(tp, t) || tp_palm_in_top_edge(tp, t);
}
bool
tp_palm_tap_is_palm(const struct tp_dispatch *tp, const struct tp_touch *t)
{
if (t->state != TOUCH_BEGIN)
return false;
if (!tp_palm_in_edge(tp, t))
return false;
evdev_log_debug(tp->device,
"palm: touch %d: palm-tap detected\n",
t->index);
return true;
}
static bool
tp_palm_detect_dwt_triggered(struct tp_dispatch *tp,
struct tp_touch *t,
uint64_t time)
{
if (tp->dwt.dwt_enabled &&
tp->dwt.keyboard_active &&
t->state == TOUCH_BEGIN) {
t->palm.state = PALM_TYPING;
t->palm.first = t->point;
return true;
} else if (!tp->dwt.keyboard_active &&
t->state == TOUCH_UPDATE &&
t->palm.state == PALM_TYPING) {
/* If a touch has started before the first or after the last
key press, release it on timeout. Benefit: a palm rested
while typing on the touchpad will be ignored, but a touch
started once we stop typing will be able to control the
pointer (alas not tap, etc.).
*/
if (t->palm.time == 0 ||
t->palm.time > tp->dwt.keyboard_last_press_time) {
t->palm.state = PALM_NONE;
evdev_log_debug(tp->device,
"palm: touch %d released, timeout after typing\n",
t->index);
}
}
return false;
}
static bool
tp_palm_detect_trackpoint_triggered(struct tp_dispatch *tp,
struct tp_touch *t,
uint64_t time)
{
if (!tp->palm.monitor_trackpoint)
return false;
if (t->palm.state == PALM_NONE &&
t->state == TOUCH_BEGIN &&
tp->palm.trackpoint_active) {
t->palm.state = PALM_TRACKPOINT;
return true;
} else if (t->palm.state == PALM_TRACKPOINT &&
t->state == TOUCH_UPDATE &&
!tp->palm.trackpoint_active) {
if (t->palm.time == 0 ||
t->palm.time > tp->palm.trackpoint_last_event_time) {
t->palm.state = PALM_NONE;
evdev_log_debug(tp->device,
"palm: touch %d released, timeout after trackpoint\n", t->index);
}
}
return false;
}
static bool
tp_palm_detect_tool_triggered(struct tp_dispatch *tp,
struct tp_touch *t,
uint64_t time)
{
if (!tp->palm.use_mt_tool)
return false;
if (t->palm.state != PALM_NONE &&
t->palm.state != PALM_TOOL_PALM)
return false;
if (t->palm.state == PALM_NONE &&
t->is_tool_palm)
t->palm.state = PALM_TOOL_PALM;
else if (t->palm.state == PALM_TOOL_PALM &&
!t->is_tool_palm)
t->palm.state = PALM_NONE;
return t->palm.state == PALM_TOOL_PALM;
}
static inline bool
tp_palm_detect_move_out_of_edge(struct tp_dispatch *tp,
struct tp_touch *t,
uint64_t time)
{
const int PALM_TIMEOUT = ms2us(200);
int directions = 0;
struct device_float_coords delta;
int dirs;
if (time < t->palm.time + PALM_TIMEOUT && !tp_palm_in_edge(tp, t)) {
if (tp_palm_was_in_side_edge(tp, t))
directions = NE|E|SE|SW|W|NW;
else if (tp_palm_was_in_top_edge(tp, t))
directions = S|SE|SW;
if (directions) {
delta = device_delta(t->point, t->palm.first);
dirs = phys_get_direction(tp_phys_delta(tp, delta));
if ((dirs & directions) && !(dirs & ~directions))
return true;
}
}
return false;
}
static inline bool
tp_palm_detect_multifinger(struct tp_dispatch *tp, struct tp_touch *t, uint64_t time)
{
struct tp_touch *other;
if (tp->nfingers_down < 2)
return false;
/* If we have at least one other active non-palm touch make this
* touch non-palm too. This avoids palm detection during two-finger
* scrolling.
*
* Note: if both touches start in the palm zone within the same
* frame the second touch will still be PALM_NONE and thus detected
* here as non-palm touch. This is too niche to worry about for now.
*/
tp_for_each_touch(tp, other) {
if (other == t)
continue;
if (tp_touch_active(tp, other) &&
other->palm.state == PALM_NONE) {
return true;
}
}
return false;
}
static inline bool
tp_palm_detect_touch_size_triggered(struct tp_dispatch *tp,
struct tp_touch *t,
uint64_t time)
{
if (!tp->palm.use_size)
return false;
/* If a finger size is large enough for palm, we stick with that and
* force the user to release and reset the finger */
if (t->palm.state != PALM_NONE && t->palm.state != PALM_TOUCH_SIZE)
return false;
if (t->major > tp->palm.size_threshold ||
t->minor > tp->palm.size_threshold) {
if (t->palm.state != PALM_TOUCH_SIZE)
evdev_log_debug(tp->device,
"palm: touch %d size exceeded\n",
t->index);
t->palm.state = PALM_TOUCH_SIZE;
return true;
}
return false;
}
static inline bool
tp_palm_detect_edge(struct tp_dispatch *tp,
struct tp_touch *t,
uint64_t time)
{
if (t->palm.state == PALM_EDGE) {
if (tp_palm_detect_multifinger(tp, t, time)) {
t->palm.state = PALM_NONE;
evdev_log_debug(tp->device,
"palm: touch %d released, multiple fingers\n",
t->index);
/* If labelled a touch as palm, we unlabel as palm when
we move out of the palm edge zone within the timeout, provided