-
Notifications
You must be signed in to change notification settings - Fork 14
/
touchpad.c
1318 lines (1091 loc) · 34.8 KB
/
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 (c) 2013 Citrix Systems, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "project.h"
#define ABS_MT_PRESSURE 0x3a /* Pressure on contact area */
#define MOVE_MULT 0.02
#define SCROLL_MULT 0.04
#define PRESSURE_UP_MULT 0.12
#define PRESSURE_DOWN_MULT 0.098
#define NUM_MOVE_HISTORY_REC 4
#define AUTOSCROLL_TIMEOUT_USEC 200000
#define TAP_DRAG_TIMEOUT_USEC 180000
#define TIMEVAL_MAX_USEC 1000000
#define HIST(a) (ts->move_hist[((ts->move_hist_index - (a) + NUM_MOVE_HISTORY_REC) % NUM_MOVE_HISTORY_REC)])
static void wrapper_send_autoscroll_event(int fd, short event , void *opaque);
static int gSlot = -2;
enum tap_state
{
TS_START = 0,
TS_TOUCH,
TS_MOVE,
TS_SINGLE_TAP_PENDING,
TS_POSSIBLE_DRAG,
TS_DRAG
};
enum edge_type
{
NO_EDGE = 0,
RIGHT_EDGE = 1,
BOTTOM_EDGE = 2,
TOP_EDGE = 4
};
enum scroll_type
{
SCROLL_UP = 0,
SCROLL_DOWN
};
struct touchpad_packet
{
int x; /* X position of finger. */
int y; /* X position of finger. */
int pressure; /* Finger pressure. */
int button_mask; /* Left or right button click. */
int btn_touch; /* Value for BTN_TOUCH reporting contact. */
struct timeval timestamp;
int fingertouching;
};
struct move_history_rec
{
int x;
int y;
struct timeval timestamp;
};
struct scroll_data
{
int up;
int down;
int left;
int right;
};
struct touchpad_state
{
int last_x; /* Last valid value of x - used when packets without an x value are recd. */
int last_y; /* Last valid value of y - used when packets without an y value are recd. */
int last_fingertouching;
enum tap_state tapstate;
struct move_history_rec move_hist[NUM_MOVE_HISTORY_REC];
int move_hist_index;
int packet_count_move;
struct scroll_data scrolldata;
int vert_scroll_on;
int scroll_y;
int scroll_packet_count;
double autoscroll_yspd;
double autoscroll_y;
double frac_x;
double frac_y;
int touchpad_off; /* Is the touchpad enabled or disabled. */
};
struct touchpad_limits
{
int minx; /* Minimum valid value of x. */
int maxx; /* Maximum valid value of x. */
int miny; /* Minimum valid value of y. */
int maxy; /* Maximum valid value of y. */
int min_pressure; /* Minimum valid value of pressure. */
int max_pressure; /* Maximum valid value of pressure. */
int right_edge; /* Right edge begins here. */
int bottom_edge; /* Bottom edge begins here. */
int top_edge; /* Top edge begins here. */
int tap_move; /* Minimum distance to move, for a move event to be generated. */
int no_x; /* Used when no x value is received in packet. */
int no_y; /* Used when no y value is received in packet. */
int no_pressure; /* Used when no pressure value is received in packet. */
int scroll_dist_vert; /* Minimum vertical scroll distance. */
double speed;
int is_clickpad;
/* Maximum x value for left button clicks. */
int clickpad_left_button_maxx;
/* True when the clickpad is pressed on the bottom edge. */
int is_clickpad_pressed;
};
struct touchpad_config
{
int tap_to_click_enabled;
int scrolling_enabled;
int speed;
};
static const int left_button_down = 0x01;
static const int left_button_up = 0x02;
static const int right_button_down = 0x04;
static const int right_button_up = 0x08;
static const int left_button_id = 1;
static struct touchpad_packet tpacket;
static struct touchpad_state tstate;
static struct touchpad_limits tlimits;
static struct touchpad_config tconfig;
static const int default_diag = 5024;
static struct event autoscroll_event;
static enum scroll_type current_scroll_type;
static int set_timer_only = 1;
static int is_autoscroll_timer_set = 0;
static struct event left_click_event;
/* get configured setting - true/false */
int touchpad_get_tap_to_click_enabled(void)
{
char buf[16];
db_read(buf, sizeof(buf), "/touchpad/tap-to-click-enable");
return strcmp("true", buf) == 0;
}
/* get configured setting - true/false */
int touchpad_get_scrolling_enabled(void)
{
char buf[16];
db_read(buf, sizeof(buf), "/touchpad/scrolling-enable");
return strcmp("true", buf) == 0;
}
/* get configured setting - int value */
int touchpad_get_speed(void)
{
char buf[16];
db_read(buf, sizeof(buf), "/touchpad/speed");
return strtol(buf, NULL, 10);
}
void touchpad_set_scrolling_enabled(int enabled)
{
db_write("/touchpad/scrolling-enable", enabled ? "true" : "false");
touchpad_reread_config();
}
void touchpad_set_tap_to_click_enabled(int enabled)
{
db_write("/touchpad/tap-to-click-enable", enabled ? "true" : "false");
touchpad_reread_config();
}
void touchpad_set_speed(int speed)
{
char buf[16];
sprintf(buf, "%d", speed);
db_write("/touchpad/speed", buf);
touchpad_reread_config();
}
static void reset_touchpad_packet(struct touchpad_packet *tp, struct touchpad_limits *tl)
{
tp->x = tl->no_x;
tp->y = tl->no_y;
tp->pressure = tl->no_pressure;
tp->button_mask = 0;
tp->timestamp.tv_sec = 0;
tp->timestamp.tv_usec = 0;
tp->fingertouching = 0;
}
static enum edge_type detect_edge(int x, int y, int right_edge, int bottom_edge, int top_edge)
{
enum edge_type etype = NO_EDGE;
if (x > right_edge)
etype |= RIGHT_EDGE;
if (y > bottom_edge)
{
etype |= BOTTOM_EDGE;
}
else if (y < top_edge)
{
etype |= TOP_EDGE;
}
return etype;
}
static int detect_finger(const struct touchpad_packet *tp, const struct touchpad_limits *tl)
{
static int fingertouching = 0;
int range = tl->max_pressure - tl->min_pressure;
if (!range) {
fingertouching = tp->btn_touch;
return fingertouching;
}
if (tp->pressure == tl->no_pressure)
return fingertouching;
if (!fingertouching)
{
if (tp->pressure > (tl->min_pressure + (range * PRESSURE_UP_MULT)))
fingertouching = 1;
}
else
{
if (tp->pressure < (tl->min_pressure + (range * PRESSURE_DOWN_MULT)))
fingertouching = 0;
}
return fingertouching;
}
static void populate_input_event(struct input_event *ev, int type, int code, int value)
{
struct timeval now;
gettimeofday(&now,NULL);
ev->time = now;
ev->type = type;
ev->code = code;
ev->value = value;
}
static void event_sync(void)
{
struct input_event sync_ev;
populate_input_event(&sync_ev, EV_SYN, SYN_REPORT, 0);
check_and_inject_event(&sync_ev, gSlot, HID_TYPE_TOUCHPAD);
}
static void event_button_change(int button_id, int value)
{
struct input_event ev;
if ((button_id < left_button_id) || (button_id > (BTN_TASK - BTN_LEFT)))
return;
populate_input_event(&ev, EV_KEY, button_id + BTN_LEFT - 1, value);
check_and_inject_event(&ev, gSlot, HID_TYPE_TOUCHPAD);
/* Generate a sync event. */
event_sync();
}
static void event_relative_move(int dist_x, int dist_y)
{
struct input_event ev[2];
if (dist_x != 0)
{
populate_input_event(&(ev[0]), EV_REL, REL_X, dist_x);
check_and_inject_event(&(ev[0]), gSlot, HID_TYPE_TOUCHPAD);
}
if (dist_y != 0)
{
populate_input_event(&(ev[1]), EV_REL, REL_Y, dist_y);
check_and_inject_event(&(ev[1]), gSlot, HID_TYPE_TOUCHPAD);
}
/* Generate a sync event. */
if ((dist_x != 0) || (dist_y != 0))
event_sync();
}
static void event_scroll(enum scroll_type scroll)
{
struct input_event ev;
int event_code = -1;
int value = 0;
switch (scroll)
{
case SCROLL_UP:
event_code = REL_WHEEL;
value = 1;
break;
case SCROLL_DOWN:
event_code = REL_WHEEL;
value = -1;
break;
default:
break;
}
populate_input_event(&ev, EV_REL, event_code, value);
check_and_inject_event(&ev, gSlot, HID_TYPE_TOUCHPAD);
/* Generate a sync event. */
event_sync();
}
/* Note: the maximum value of timeout_usec that this function can handle
is 999,999, which is the maximum value of tv_usec in struct timeval. */
static void add_timeout_usec(struct timeval *tv,
uint32_t timeout_usec)
{
uint32_t diff;
diff = TIMEVAL_MAX_USEC - tv->tv_usec;
if (diff <= timeout_usec)
{
tv->tv_usec = timeout_usec - diff;
tv->tv_sec += 1;
}
else
{
tv->tv_usec += timeout_usec;
}
}
static void delete_left_click_timer(void)
{
if (event_initialized(&left_click_event))
{
evtimer_del(&left_click_event);
}
}
static void send_left_click_event(void *opaque)
{
struct touchpad_state *ts = (struct touchpad_state *) opaque;
const int button_down = 1;
const int button_up = 0;
if (ts->tapstate == TS_SINGLE_TAP_PENDING)
{
event_button_change(left_button_id, button_down);
event_button_change(left_button_id, button_up);
ts->tapstate = TS_START;
}
delete_left_click_timer();
}
static void wrapper_send_left_click_event(int fd, short event, void *opaque)
{
send_left_click_event(opaque);
}
static void set_left_click_timer(struct touchpad_state *ts,
uint32_t timeout_usec)
{
struct timeval tv = {0, 0};
if (!event_initialized(&left_click_event))
{
evtimer_set(&left_click_event, wrapper_send_left_click_event, ts);
}
add_timeout_usec(&tv, timeout_usec);
evtimer_add(&left_click_event, &tv);
}
static void handle_gestures(struct touchpad_state *ts,
struct touchpad_packet *tp,
struct touchpad_limits *tl,
struct touchpad_config *tc)
{
static int touch_on_x = 0;
static int touch_on_y = 0;
static enum edge_type touch_on_edge = NO_EDGE;
int button = left_button_id;
int touch = 0;
int release = 0;
int move = 0;
int dx = 0;
int dy = 0;
const int button_down = 1;
const int button_up = 0;
touch = (tp->fingertouching) && !(ts->last_fingertouching);
release = !(tp->fingertouching) && (ts->last_fingertouching);
if (tp->fingertouching)
{
if (touch)
move = 0;
else if ( ((tp->x != tl->no_x) && (abs(tp->x - touch_on_x) >= tl->tap_move)) ||
((tp->y != tl->no_y) && (abs(tp->y - touch_on_y) >= tl->tap_move)) )
move = 1;
}
if (touch)
{
if (tp->x != tl->no_x)
touch_on_x = tp->x;
else
touch_on_x = ts->last_x;
if (tp->y != tl->no_y)
touch_on_y = tp->y;
else
touch_on_y = ts->last_y;
touch_on_edge = detect_edge(touch_on_x, touch_on_y,
tl->right_edge, tl->bottom_edge,
tl->top_edge);
}
switch (ts->tapstate)
{
case TS_START:
if (touch)
ts->tapstate = TS_TOUCH;
ts->frac_x = 0;
ts->frac_y = 0;
break;
case TS_TOUCH:
if (move)
{
ts->tapstate = TS_MOVE;
}
else if(release)
{
ts->tapstate = TS_START;
/* Generate left button click if configured.
For clickpads, do not generate tap clicks on the
bottom edge, otherwise a press-and-release button
click results in a double-click. */
if ( tc->tap_to_click_enabled &&
(!(tl->is_clickpad && (touch_on_edge & BOTTOM_EDGE))) )
{
/* Do not generate the left click immediately, otherwise a
double tap and drag gesture results in a double click.
The windows and ubuntu drivers seem to check the time
diff between 2 left button down events, rather than 2
left button up events, for a double click. So in case of
double tap and drag, we will not send the first tap. */
set_left_click_timer(ts, TAP_DRAG_TIMEOUT_USEC);
ts->tapstate = TS_SINGLE_TAP_PENDING;
}
}
break;
case TS_MOVE:
if (release)
{
ts->tapstate = TS_START;
}
break;
case TS_SINGLE_TAP_PENDING:
if (touch)
{
delete_left_click_timer();
if ( tc->tap_to_click_enabled &&
(!(tl->is_clickpad && (touch_on_edge & BOTTOM_EDGE))) )
{
ts->tapstate = TS_POSSIBLE_DRAG;
}
else
{
/* Send the left click that was pending. */
event_button_change(button, button_down);
event_button_change(button, button_up);
ts->tapstate = TS_TOUCH;
}
}
ts->frac_x = 0;
ts->frac_y = 0;
break;
case TS_POSSIBLE_DRAG:
if (move)
{
/* Generate a left button down event.
Tap clicks will be enabled if we are here. */
event_button_change(button, button_down);
ts->tapstate = TS_DRAG;
}
else if(release)
{
ts->tapstate = TS_START;
/* It is not a drag, just a double tap. Generate two left
clicks. Tap clicks will be enabled if we are here. */
event_button_change(button, button_down);
event_button_change(button, button_up);
event_button_change(button, button_down);
event_button_change(button, button_up);
}
break;
case TS_DRAG:
if (release)
{
ts->tapstate = TS_START;
/* Generate a left button up event.
Tap clicks will be enabled if we are here. */
event_button_change(button, button_up);
}
break;
default:
break;
}
}
static void handle_button_clicks(struct touchpad_packet *tp)
{
int button_id;
int mask;
const int button_down = 1;
const int button_up = 0;
const int num_buttons = BTN_TASK - BTN_LEFT;
/* Generate button click events, if any. */
for (button_id = 1, mask = 1; button_id <= num_buttons; mask = mask << 1, button_id++)
{
if (tp->button_mask & mask)
{
event_button_change(button_id, button_down);
}
mask = mask << 1;
if (tp->button_mask & mask)
{
event_button_change(button_id, button_up);
}
}
}
static void store_history(struct touchpad_state *ts, int x, int y, struct timeval timestamp)
{
int idx = (ts->move_hist_index + 1) % NUM_MOVE_HISTORY_REC;
ts->move_hist[idx].x = x;
ts->move_hist[idx].y = y;
ts->move_hist[idx].timestamp = timestamp;
ts->move_hist_index = idx;
}
static double estimate_delta(double x0, double x1, double x2, double x3)
{
return x0 * 0.3 + x1 * 0.1 - x2 * 0.1 - x3 * 0.3;
}
static double estimate_delta_previous(double x0, double x1)
{
return ((x0 - x1) * 0.4);
}
static void compute_deltas(struct touchpad_state *ts,
struct touchpad_packet *tp,
struct touchpad_limits *tl,
int *delta_x,
int *delta_y)
{
const int min_packet_count_move = 3;
int x_value = tp->x;
int y_value = tp->y;
double dx = 0;
double dy = 0;
double integral = 0;
if (x_value == tl->no_x)
{
x_value = ts->last_x;
}
if (y_value == tl->no_y)
{
y_value = ts->last_y;
}
if ((ts->tapstate == TS_MOVE || ts->tapstate == TS_DRAG) && !(ts->vert_scroll_on))
{
ts->packet_count_move++;
if (ts->packet_count_move > min_packet_count_move)
{
dx = estimate_delta(x_value, HIST(0).x, HIST(1).x, HIST(2).x);
dy = estimate_delta(y_value, HIST(0).y, HIST(1).y, HIST(2).y);
}
else if (ts->packet_count_move > 1)
{
dx = estimate_delta_previous(x_value, HIST(0).x);
dy = estimate_delta_previous(y_value, HIST(0).y);
}
/* Adjust deltas according to speed. */
dx = (dx * (tl->speed)) + (ts->frac_x);
ts->frac_x = modf(dx, &integral);
dx = integral;
dy = (dy * (tl->speed)) + (ts->frac_y);
ts->frac_y = modf(dy, &integral);
dy = integral;
}
else
{
ts->packet_count_move = 0;
}
*delta_x = (int) dx;
*delta_y = (int) dy;
store_history(ts, x_value, y_value, tp->timestamp);
}
static void send_autoscroll_event(void *unused)
{
struct timeval tv = {0, 0};
/* Send scroll event. */
if (!set_timer_only)
{
event_scroll(current_scroll_type);
}
if (!event_initialized(&autoscroll_event))
{
evtimer_set(&autoscroll_event, wrapper_send_autoscroll_event, NULL);
}
add_timeout_usec(&tv, AUTOSCROLL_TIMEOUT_USEC);
evtimer_add(&autoscroll_event, &tv);
is_autoscroll_timer_set = 1;
}
static void wrapper_send_autoscroll_event(int fd, short event , void *opaque)
{
send_autoscroll_event(opaque);
}
static void reset_autoscroll_timer(void)
{
struct timeval tv = {0, 0};
if (is_autoscroll_timer_set)
{
add_timeout_usec(&tv, AUTOSCROLL_TIMEOUT_USEC);
evtimer_add(&autoscroll_event, &tv);
}
}
static void free_autoscroll_timer(void)
{
if (is_autoscroll_timer_set)
{
evtimer_del(&autoscroll_event);
is_autoscroll_timer_set = 0;
}
}
static void reset_scroll_data(struct scroll_data *sd)
{
sd->up = 0;
sd->down = 0;
sd->left = 0;
sd->right = 0;
}
static void start_corner_scrolling(struct touchpad_state *ts,
struct touchpad_packet *tp,
struct touchpad_limits *tl)
{
struct timeval diff;
double elapsed_secs;
double dy;
int y_value = tp->y;
double delta = tl->scroll_dist_vert;
ts->autoscroll_y = 0;
/* Turn on autoscroll only when we reach a corner while scrolling,
not when we start scrolling from a corner. */
if (ts->scroll_packet_count > 3)
{
if (tp->y == tl->no_y)
y_value = ts->last_y;
timersub(&(HIST(0).timestamp), &(HIST(3).timestamp), &diff);
elapsed_secs = (diff.tv_sec) + (diff.tv_usec / 1000000.0);
dy = (HIST(0).y) - (HIST(3).y);
if ((elapsed_secs > 0) && (delta > 0))
{
ts->autoscroll_yspd = (dy / elapsed_secs) / delta;
ts->autoscroll_y = (y_value - ts->scroll_y) / delta;
}
/* Alps touchpads stop sending events soon after the finger stops
moving, even if it is still pressed. To handle this, we will
generate scroll events with the help of a timer. */
set_timer_only = 1;
send_autoscroll_event(NULL);
set_timer_only = 0;
if (y_value < ts->scroll_y)
current_scroll_type = SCROLL_UP;
else
current_scroll_type = SCROLL_DOWN;
}
ts->scroll_packet_count = 0;
}
static void stop_corner_scrolling(struct touchpad_state *ts)
{
ts->autoscroll_yspd = 0;
ts->scroll_packet_count = 0;
}
static void handle_scrolling(struct touchpad_state *ts,
struct touchpad_packet *tp,
struct touchpad_limits *tl,
struct touchpad_config *tc,
enum edge_type edge)
{
int x_value = tp->x;
int y_value = tp->y;
int delta;
int is_corner = 0;
double dsecs;
struct timeval diff;
const double timeout_secs = 0.2;
struct scroll_data *sd = &(ts->scrolldata);
if (!(tc->scrolling_enabled))
return;
if (tp->x == tl->no_x)
x_value = ts->last_x;
if (tp->y == tl->no_y)
y_value = ts->last_y;
reset_scroll_data(sd);
/* Reset the autoscroll timer whenever a packet is received. */
reset_autoscroll_timer();
if ((tp->fingertouching) && !(ts->last_fingertouching))
{
stop_corner_scrolling(ts);
if (edge & RIGHT_EDGE)
{
ts->vert_scroll_on = 1;
ts->scroll_y = y_value;
}
}
if (ts->vert_scroll_on && (!(edge & RIGHT_EDGE) || !(tp->fingertouching)))
ts->vert_scroll_on = 0;
/* If we were corner scrolling, but no longer in a corner, or raised finger
then stop corner scrolling. */
is_corner = ((edge & RIGHT_EDGE) && (edge & (TOP_EDGE | BOTTOM_EDGE)));
if ((ts->autoscroll_yspd) && (!(tp->fingertouching) || !(is_corner)))
{
stop_corner_scrolling(ts);
}
/* If we are no longer in a corner, or raised finger then free the
autoscroll timer. Note that yspd may have been zero when we were
corner scrolling. */
if (!(tp->fingertouching) || !(is_corner))
{
free_autoscroll_timer();
}
/* If hitting a corner (top or bottom) while vertical scrolling is on,
start corner scrolling. */
if ((ts->vert_scroll_on) && is_corner && (ts->autoscroll_yspd == 0))
{
start_corner_scrolling(ts, tp, tl);
}
if (ts->vert_scroll_on)
{
(ts->scroll_packet_count)++;
}
if (ts->vert_scroll_on)
{
delta = tl->scroll_dist_vert;
if (delta > 0)
{
while ((y_value - ts->scroll_y) > delta)
{
(sd->down)++;
ts->scroll_y += delta;
}
while ((y_value - ts->scroll_y) < -delta)
{
(sd->up)++;
ts->scroll_y -= delta;
}
}
}
if (ts->autoscroll_yspd)
{
timersub(&(tp->timestamp), &(HIST(0).timestamp), &diff);
dsecs = (diff.tv_sec) + (diff.tv_usec / 1000000.0);
/* If dsecs is not less than timeout_secs, an autoscroll event will
already have been generated by the timer, so do not generate
it again. */
if (dsecs < timeout_secs)
{
ts->autoscroll_y += (ts->autoscroll_yspd * dsecs);
while (ts->autoscroll_y > 1.0)
{
sd->down++;
ts->autoscroll_y -= 1.0;
}
while (ts->autoscroll_y < -1.0)
{
sd->up++;
ts->autoscroll_y += 1.0;
}
}
}
/* Generate scroll events. */
while ((sd->up)-- > 0)
{
event_scroll(SCROLL_UP);
}
while ((sd->down)-- > 0)
{
event_scroll(SCROLL_DOWN);
}
}
static void save_previous_packet_values(struct touchpad_state *ts,
struct touchpad_packet *tp,
struct touchpad_limits *tl)
{
ts->last_fingertouching = tp->fingertouching;
if (tp->x != tl->no_x)
{
ts->last_x = tp->x;
}
if (tp->y != tl->no_y)
{
ts->last_y = tp->y;
}
}
static void handle_clickpad(struct touchpad_state *ts,
struct touchpad_packet *tp,
struct touchpad_limits *tl,
enum edge_type edge)
{
static int button_down_x = -1;
if (edge & BOTTOM_EDGE)
{
/* clickpad reports only left button, and we need to fake
the right button depending on the touch position. */
if (tp->button_mask & left_button_down)
{
tl->is_clickpad_pressed = 1;
if (tp->x != tl->no_x)
button_down_x = tp->x;
else
button_down_x = ts->last_x;
if (button_down_x > tl->clickpad_left_button_maxx)
tp->button_mask = (tp->button_mask & (!left_button_down)) | right_button_down;
}
else if (tp->button_mask & left_button_up)
{
tl->is_clickpad_pressed = 0;
if (button_down_x > tl->clickpad_left_button_maxx)
tp->button_mask = (tp->button_mask & (!left_button_up)) | right_button_up;
button_down_x = -1;
}
handle_button_clicks(tp);
}
}
/* QUIRK:
* Some touchpads report values outside of the boundaries they pretend to
* honor. This would be handled with a quirk (e.g. hwdb + libevdev), but in
* this case, it would require a lot of refactoring to support something even
* similar.
* Instead, adjust limits if a value overflows by less than ~10% of the axis
* range. Anything above is considered spurious input (which happens with some
* multitouch input). */
static void adjust_overflow(struct touchpad_packet *tp,
struct touchpad_limits *tl)
{
int xrange = tl->maxx - tl->minx;
int yrange = tl->maxy - tl->miny;
if ((tp->x > tl->maxx) && ((tp->x - (xrange / 10)) <= tl->maxx)) {
/* Preserve the reported zone size for lack of a better idea. */
tl->right_edge = tp->x - (xrange - tl->right_edge);
tl->maxx = tp->x;
}
if ((tp->y > tl->maxy) && ((tp->y - (yrange / 10)) <= tl->maxy)) {
tl->bottom_edge = tp->y - (yrange - tl->bottom_edge);
tl->maxy = tp->y;
}
}
static void process_packet(struct touchpad_state *ts,
struct touchpad_packet *tp,
struct touchpad_limits *tl,
struct touchpad_config *tc)
{
int x_value = tp->x;
int y_value = tp->y;
int dx;
int dy;
enum edge_type edge;
if (tl->is_clickpad == 0)
handle_button_clicks(tp);
/* If the touchpad has been disabled, only generate button clicks. Do not
handle moves, scrolling etc. */
if (ts->touchpad_off == 1)
return;