-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforce.c
More file actions
964 lines (886 loc) · 31.5 KB
/
Copy pathforce.c
File metadata and controls
964 lines (886 loc) · 31.5 KB
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
/*
* force.c - Multitouch uinput passthrough with force-aware button rewriting.
*
* The physical touchpad is grabbed so its BTN_LEFT can be classified, then
* every other event is mirrored to a virtual multitouch touchpad. This leaves
* pointer acceleration, scrolling, tapping, and multi-finger gestures to
* libinput/the compositor. Optional trackball inertia uses a second relative
* pointer only after a one-finger lift.
*/
#include "force.h"
#include <errno.h>
#include <fcntl.h>
#include <linux/input.h>
#include <linux/uinput.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#define GOODIX_VENDOR 0x27c6
#define GOODIX_PRODUCT 0x01e0
#define DIRECT_RETRY_MS 2000U
#define SPIN_TICK_MS 2U
#define SENS_GHOST_WINDOW_MS 90U
#define SPIN_FRICTION 0.984
#define SPIN_STOP_SPEED 0.05
#define BITS_PER_LONG (sizeof(unsigned long) * 8U)
#define BIT_WORDS(maximum) (((maximum) + BITS_PER_LONG) / BITS_PER_LONG)
#define BIT_IS_SET(bits, bit) \
(((bits)[(bit) / BITS_PER_LONG] >> ((bit) % BITS_PER_LONG)) & 1UL)
static void sens_rearm(direct_control *dc);
static double absolute(double value)
{
return value < 0.0 ? -value : value;
}
static int take_integer(double *value)
{
int whole = (int)*value;
*value -= (double)whole;
return whole;
}
int direct_slot_count_from_range(int minimum, int maximum)
{
int count = maximum - minimum + 1;
if (count < 1)
return 1;
if (count > DIRECT_SLOT_MAX)
return DIRECT_SLOT_MAX;
return count;
}
static int write_events(int fd, const struct input_event *events, int count)
{
const unsigned char *data = (const unsigned char *)events;
size_t remaining = sizeof(*events) * (size_t)count;
while (remaining > 0) {
ssize_t written = write(fd, data, remaining);
if (written < 0) {
if (errno == EINTR)
continue;
return -1;
}
if (written == 0) {
errno = EIO;
return -1;
}
data += written;
remaining -= (size_t)written;
}
return 0;
}
static int emit_button(direct_control *dc, int code, int value)
{
if (dc->touchpad_fd < 0)
return 0;
struct input_event events[2] = {0};
events[0].type = EV_KEY;
events[0].code = (uint16_t)code;
events[0].value = value;
events[1].type = EV_SYN;
events[1].code = SYN_REPORT;
return write_events(dc->touchpad_fd, events, 2);
}
static int emit_relative(direct_control *dc, int x, int y)
{
if (dc->touchpad_fd < 0 || (!x && !y))
return 0;
struct input_event events[3] = {0};
int count = 0;
if (x) {
events[count].type = EV_REL;
events[count].code = REL_X;
events[count++].value = x;
}
if (y) {
events[count].type = EV_REL;
events[count].code = REL_Y;
events[count++].value = y;
}
events[count].type = EV_SYN;
events[count].code = SYN_REPORT;
count++;
return write_events(dc->touchpad_fd, events, count);
}
/* Find a physical Goodix event node whose name contains name_part. */
static int open_physical_event(const char *name_part, char *path,
size_t path_size)
{
for (int i = 0; i < 64; i++) {
char candidate[64];
snprintf(candidate, sizeof(candidate), "/dev/input/event%d", i);
int fd = open(candidate, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
if (fd < 0)
continue;
struct input_id id;
char name[128] = "";
if (ioctl(fd, EVIOCGID, &id) == 0 &&
ioctl(fd, EVIOCGNAME(sizeof(name)), name) >= 0 &&
id.bustype == BUS_I2C && id.vendor == GOODIX_VENDOR &&
id.product == GOODIX_PRODUCT && strstr(name, name_part)) {
if (path)
snprintf(path, path_size, "%s", candidate);
return fd;
}
close(fd);
}
errno = ENODEV;
return -1;
}
static int finalize_uinput_device(int fd, const char *name, uint16_t product)
{
struct uinput_setup setup;
memset(&setup, 0, sizeof(setup));
setup.id.bustype = BUS_VIRTUAL;
setup.id.vendor = GOODIX_VENDOR;
setup.id.product = product;
setup.id.version = 1;
snprintf(setup.name, sizeof(setup.name), "%s", name);
if (ioctl(fd, UI_DEV_SETUP, &setup) < 0 || ioctl(fd, UI_DEV_CREATE) < 0)
return -1;
return 0;
}
static int open_virtual_touchpad(int physical_fd)
{
int fd = open("/dev/uinput", O_WRONLY | O_CLOEXEC);
if (fd < 0)
return -1;
if (ioctl(fd, UI_SET_EVBIT, EV_KEY) == 0) {
unsigned long keybits[BIT_WORDS(KEY_MAX)] = {0};
if (ioctl(physical_fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits) >= 0) {
for (int k = 0; k <= KEY_MAX; k++) {
if (BIT_IS_SET(keybits, k))
ioctl(fd, UI_SET_KEYBIT, k);
}
}
ioctl(fd, UI_SET_KEYBIT, BTN_LEFT);
ioctl(fd, UI_SET_KEYBIT, BTN_RIGHT);
ioctl(fd, UI_SET_KEYBIT, BTN_MIDDLE);
ioctl(fd, UI_SET_KEYBIT, BTN_TOUCH);
ioctl(fd, UI_SET_KEYBIT, BTN_TOOL_FINGER);
}
if (ioctl(fd, UI_SET_EVBIT, EV_REL) == 0) {
ioctl(fd, UI_SET_RELBIT, REL_X);
ioctl(fd, UI_SET_RELBIT, REL_Y);
}
unsigned long propbits[BIT_WORDS(INPUT_PROP_MAX)] = {0};
if (ioctl(physical_fd, EVIOCGPROP(sizeof(propbits)), propbits) >= 0) {
for (int p = 0; p <= INPUT_PROP_MAX; p++) {
/* Never advertise BUTTONPAD: libinput discards BTN_RIGHT from
* clickpads, which would break the rewritten click actions. */
if (p == INPUT_PROP_BUTTONPAD)
continue;
if (BIT_IS_SET(propbits, p))
ioctl(fd, UI_SET_PROPBIT, p);
}
}
if (ioctl(fd, UI_SET_EVBIT, EV_ABS) == 0) {
/* No ABS_PRESSURE/ABS_MT_PRESSURE: the firmware advertises them
* but always reports 0. The physical pad only works because a
* name-matched libinput quirk (ModelPressurePad) disables
* pressure-based touch detection; no quirk matches this virtual
* device, so advertising pressure would make libinput wait for
* pressure that never comes and ignore every touch. */
static const int abs_codes[] = {
ABS_X, ABS_Y, ABS_MT_SLOT,
ABS_MT_TOUCH_MAJOR, ABS_MT_TOUCH_MINOR,
ABS_MT_ORIENTATION, ABS_MT_POSITION_X,
ABS_MT_POSITION_Y, ABS_MT_TOOL_TYPE,
ABS_MT_TRACKING_ID
};
for (size_t i = 0; i < sizeof(abs_codes) / sizeof(abs_codes[0]); i++) {
int code = abs_codes[i];
struct uinput_abs_setup setup;
memset(&setup, 0, sizeof(setup));
setup.code = (uint16_t)code;
if (ioctl(physical_fd, EVIOCGABS(code), &setup.absinfo) == 0) {
if (ioctl(fd, UI_SET_ABSBIT, code) == 0)
ioctl(fd, UI_ABS_SETUP, &setup);
}
}
}
if (finalize_uinput_device(fd, "MateBook Touchpad Passthrough",
GOODIX_PRODUCT) < 0)
goto fail;
return fd;
fail:;
int saved = errno;
close(fd);
errno = saved;
return -1;
}
static void destroy_virtual(int *fd)
{
if (*fd < 0)
return;
ioctl(*fd, UI_DEV_DESTROY);
close(*fd);
*fd = -1;
}
static void reset_motion(direct_control *dc)
{
memset(dc->slots, 0, sizeof(dc->slots));
for (int i = 0; i < DIRECT_SLOT_MAX; i++)
dc->slots[i].tracking_id = -1;
dc->slot = 0;
dc->slot_count = 0;
dc->previous_contacts = 0;
dc->motion_tracking_id = -1;
dc->pointer_fraction_x = 0.0;
dc->pointer_fraction_y = 0.0;
dc->velocity_x = 0.0;
dc->velocity_y = 0.0;
dc->physical_button = false;
dc->pending_click = false;
dc->emitted_button = 0;
dc->peak_area = 0;
dc->spinning = false;
dc->sens_state = SENS_ST_IDLE;
dc->sens_debounce_t0 = 0;
}
static bool load_slot_axis(direct_control *dc, int fd, int code)
{
int values[DIRECT_SLOT_MAX + 1] = {0};
values[0] = code;
size_t bytes = ((size_t)dc->slot_count + 1U) * sizeof(values[0]);
if (ioctl(fd, EVIOCGMTSLOTS(bytes), values) < 0)
return false;
for (int i = 0; i < dc->slot_count; i++) {
direct_slot *slot = &dc->slots[i];
switch (code) {
case ABS_MT_TRACKING_ID:
slot->tracking_id = values[i + 1];
slot->active = values[i + 1] >= 0;
break;
case ABS_MT_POSITION_X: slot->x = values[i + 1]; break;
case ABS_MT_POSITION_Y: slot->y = values[i + 1]; break;
case ABS_MT_TOUCH_MAJOR: slot->width = values[i + 1]; break;
case ABS_MT_TOUCH_MINOR: slot->height = values[i + 1]; break;
default: break;
}
}
return true;
}
static void load_kernel_slots(direct_control *dc, int fd)
{
struct input_absinfo slot_info;
if (ioctl(fd, EVIOCGABS(ABS_MT_SLOT), &slot_info) < 0) {
dc->slot_count = 1;
} else {
dc->slot_count = direct_slot_count_from_range(slot_info.minimum,
slot_info.maximum);
}
load_slot_axis(dc, fd, ABS_MT_TRACKING_ID);
bool have_x = load_slot_axis(dc, fd, ABS_MT_POSITION_X);
bool have_y = load_slot_axis(dc, fd, ABS_MT_POSITION_Y);
load_slot_axis(dc, fd, ABS_MT_TOUCH_MAJOR);
load_slot_axis(dc, fd, ABS_MT_TOUCH_MINOR);
for (int i = 0; i < dc->slot_count; i++)
dc->slots[i].have_position = have_x && have_y;
}
static void close_runtime(direct_control *dc)
{
if (dc->emitted_button && dc->touchpad_fd >= 0)
emit_button(dc, dc->emitted_button, 0);
if (dc->hidraw_fd >= 0)
sens_rearm(dc);
if (dc->input_fd >= 0) {
ioctl(dc->input_fd, EVIOCGRAB, 0);
close(dc->input_fd);
dc->input_fd = -1;
}
if (dc->mouse_fd >= 0) {
ioctl(dc->mouse_fd, EVIOCGRAB, 0);
close(dc->mouse_fd);
dc->mouse_fd = -1;
}
destroy_virtual(&dc->touchpad_fd);
reset_motion(dc);
}
static int open_runtime(direct_control *dc)
{
char path[64];
dc->input_fd = open_physical_event("Touchpad", path, sizeof(path));
if (dc->input_fd < 0)
goto fail;
dc->touchpad_fd = open_virtual_touchpad(dc->input_fd);
if (dc->touchpad_fd < 0)
goto fail;
if (ioctl(dc->input_fd, EVIOCGRAB, 1) < 0)
goto fail;
/* The hardware registers a sibling "Mouse" node that keeps emitting
* ungrabbed pointer events; libinput then sees a live external mouse
* and suppresses the virtual touchpad. Grab it to silence it - its
* events are never read, the kernel ring just overwrites in place. */
dc->mouse_fd = open_physical_event("Mouse", NULL, 0);
if (dc->mouse_fd >= 0 && ioctl(dc->mouse_fd, EVIOCGRAB, 1) < 0) {
close(dc->mouse_fd);
dc->mouse_fd = -1;
}
reset_motion(dc);
load_kernel_slots(dc, dc->input_fd);
dc->unavailable_reported = false;
printf("multitouch passthrough active on %s (%d slots)%s%s\n", path,
dc->slot_count,
dc->mouse_fd >= 0 ? ", mouse node silenced" : "",
dc->trackball ? ", trackball enabled" : "");
fflush(stdout);
return 0;
fail:;
int saved = errno;
close_runtime(dc);
errno = saved;
return -1;
}
static void parse_click_action(const char *val, click_action *act, const char *default_val)
{
memset(act, 0, sizeof(*act));
const char *str = (val && val[0]) ? val : default_val;
snprintf(act->raw_val, sizeof(act->raw_val), "%s", str);
if (!strcasecmp(str, "none")) {
return;
} else if (!strcasecmp(str, "left") || !strcasecmp(str, "BTN_LEFT")) {
act->button = BTN_LEFT;
} else if (!strcasecmp(str, "right") || !strcasecmp(str, "BTN_RIGHT")) {
act->button = BTN_RIGHT;
} else if (!strcasecmp(str, "middle") || !strcasecmp(str, "BTN_MIDDLE")) {
act->button = BTN_MIDDLE;
} else if (!strncasecmp(str, "exec:", 5)) {
snprintf(act->exec_cmd, sizeof(act->exec_cmd), "%s", str + 5);
} else {
char errbuf[128];
int n = combo_parse(str, act->combo_codes, errbuf, sizeof(errbuf));
if (n > 0)
act->ncodes = n;
}
}
static void load_settings(direct_control *dc, const config *cfg)
{
dc->contact_area_threshold = cfg_get_int(cfg, "direct-control",
"contact-area-threshold", 12, 1);
dc->area_classified_click = cfg_get_bool(cfg, "direct-control",
"area-classified-click", true);
dc->click_window_ms = cfg_get_int(cfg, "direct-control",
"click-window-ms", 40, 0);
if (dc->click_window_ms > 200)
dc->click_window_ms = 200;
dc->sens_classified_click = cfg_get_bool(cfg, "direct-control",
"sens-classified-click", false);
dc->sens_debounce_ms = cfg_get_int(cfg, "direct-control",
"sens-debounce-ms", 100, 10);
if (dc->sens_debounce_ms > 500)
dc->sens_debounce_ms = 500;
haptic_level sens = HAPTIC_LEVEL_HIGH;
haptic_level vib = HAPTIC_LEVEL_MEDIUM;
haptics_parse_level(cfg_get(cfg, "general", "click-sensitivity"), &sens);
haptics_parse_level(cfg_get(cfg, "general", "vibration-intensity"), &vib);
dc->baseline_sensitivity = sens;
dc->user_vibration = vib;
haptic_level heavy = HAPTIC_LEVEL_LOW;
haptics_parse_level(cfg_get(cfg, "direct-control", "sens-heavy-level"),
&heavy);
dc->sens_heavy_level = haptics_heavy_threshold(sens, heavy);
parse_click_action(cfg_get(cfg, "direct-control", "click-light-small"),
&dc->action_light_small, "left");
parse_click_action(cfg_get(cfg, "direct-control", "click-light-large"),
&dc->action_light_large, "right");
parse_click_action(cfg_get(cfg, "direct-control", "click-heavy-small"),
&dc->action_heavy_small, "middle");
parse_click_action(cfg_get(cfg, "direct-control", "click-heavy-large"),
&dc->action_heavy_large, "right");
dc->trackball = cfg_get_bool(cfg, "direct-control", "trackball", false);
dc->pointer_speed = cfg_get_float(cfg, "direct-control", "pointer-speed", 0.12);
if (dc->pointer_speed < 0.01) dc->pointer_speed = 0.01;
if (dc->pointer_speed > 2.0) dc->pointer_speed = 2.0;
dc->pointer_acceleration = cfg_get_float(cfg, "direct-control",
"pointer-acceleration", 0.015);
if (dc->pointer_acceleration < 0.0) dc->pointer_acceleration = 0.0;
if (dc->pointer_acceleration > 0.2) dc->pointer_acceleration = 0.2;
}
static void report_retry(direct_control *dc, uint64_t now_ms)
{
if (!dc->unavailable_reported) {
fprintf(stderr, "multitouch passthrough unavailable: %s; will retry\n",
strerror(errno));
dc->unavailable_reported = true;
}
dc->retry_at_ms = now_ms + DIRECT_RETRY_MS;
}
void direct_configure(direct_control *dc, const config *cfg, bool debug,
uint64_t now_ms, int hidraw_fd)
{
dc->debug = debug;
dc->hidraw_fd = hidraw_fd;
load_settings(dc, cfg);
dc->wanted = cfg_get_bool(cfg, "direct-control", "enabled", false);
if (!dc->wanted) {
if (dc->input_fd >= 0)
printf("multitouch passthrough disabled\n");
close_runtime(dc);
return;
}
if (dc->input_fd >= 0)
return;
if (open_runtime(dc) == 0)
return;
report_retry(dc, now_ms);
}
static int active_area(const direct_control *dc)
{
int peak = 0;
for (int i = 0; i < dc->slot_count; i++) {
const direct_slot *slot = &dc->slots[i];
if (!slot->active)
continue;
int area = slot->width * slot->height;
if (area > peak)
peak = area;
}
return peak;
}
static const click_action *choose_classified_action(const direct_control *dc,
bool is_heavy, bool is_large)
{
if (is_heavy) {
return is_large ? &dc->action_heavy_large : &dc->action_heavy_small;
} else {
return is_large ? &dc->action_light_large : &dc->action_light_small;
}
}
static int execute_click_down(direct_control *dc, const click_action *act)
{
if (act->button) {
dc->emitted_button = act->button;
return emit_button(dc, act->button, 1);
}
dc->emitted_button = 0;
if (act->ncodes > 0) {
if (dc->kb && dc->kb->fd >= 0)
vkbd_press_combo(dc->kb, act->combo_codes, act->ncodes);
} else if (act->exec_cmd[0]) {
if (fork() == 0) {
execl("/bin/sh", "sh", "-c", act->exec_cmd, (char *)NULL);
_exit(127);
}
}
return 0;
}
static void execute_click_up(direct_control *dc)
{
if (dc->emitted_button) {
emit_button(dc, dc->emitted_button, 0);
dc->emitted_button = 0;
}
}
static bool classified_is_large(const direct_control *dc)
{
return dc->area_classified_click &&
dc->peak_area >= dc->contact_area_threshold;
}
/* Without sens classification the only dimension is contact area:
* light action, small or large. Force needs sens-classified-click. */
static int choose_pending_click(direct_control *dc)
{
if (!dc->pending_click)
return 0;
dc->pending_click = false;
const click_action *act =
choose_classified_action(dc, false, classified_is_large(dc));
if (execute_click_down(dc, act) < 0)
return -1;
if (dc->debug) {
printf("direct click: area=%d (large >= %d) -> %s\n",
dc->peak_area, dc->contact_area_threshold, act->raw_val);
fflush(stdout);
}
return 0;
}
/* ---- sens-classified clicks ---------------------------------------------
*
* The firmware exposes a click actuation threshold (click sensitivity).
* Baseline is the user's setting (typically HIGH = featherlight). On a
* press we drop the threshold to LOW: a soft press cannot hold LOW and the
* firmware releases the button within ~70ms (light click); a firm press
* stays latched through the debounce window (heavy click).
*
* Raising the threshold back while a finger still rests on the pad makes
* the firmware re-evaluate the resting force and can fire a ghost press
* ~50ms later. Instead of waiting for finger lift to re-arm, we re-arm
* immediately after classification and SWALLOW any press that arrives
* within SENS_GHOST_WINDOW_MS of the re-arm: drop to LOW to unlatch it and
* emit nothing. A swallowed press that survives the debounce window was a
* real fast firm press and becomes a heavy click. Vibration stays muted
* from press to re-arm settling, so ghost edges are silent; each light
* click keeps exactly one haptic pulse (the initial press).
*/
static void sens_set_lowered(direct_control *dc, bool lowered)
{
haptics_set_click_sensitivity(dc->hidraw_fd, lowered
? dc->sens_heavy_level
: dc->baseline_sensitivity);
dc->sens_lowered = lowered;
}
static void sens_set_muted(direct_control *dc, bool muted)
{
if (dc->sens_muted == muted)
return;
haptics_set_vibration_intensity(dc->hidraw_fd, muted
? HAPTIC_LEVEL_LOW
: dc->user_vibration);
dc->sens_muted = muted;
}
/* Restore firmware baseline. Called at finger lift and teardown. */
static void sens_rearm(direct_control *dc)
{
if (dc->sens_lowered)
sens_set_lowered(dc, false);
sens_set_muted(dc, false);
dc->sens_state = SENS_ST_IDLE;
}
/* Classification decided: unmute and start the button hold. */
static int sens_classify(direct_control *dc, bool is_heavy)
{
sens_set_muted(dc, false);
dc->sens_state = is_heavy ? SENS_ST_HEAVY : SENS_ST_LIGHT;
const click_action *act =
choose_classified_action(dc, is_heavy, classified_is_large(dc));
int rc = execute_click_down(dc, act);
if (dc->debug) {
printf("direct click (sensitivity): force=%s area=%d -> %s\n",
is_heavy ? "heavy" : "light", dc->peak_area, act->raw_val);
fflush(stdout);
}
return rc;
}
/* Re-arm the threshold right away and watch for the firmware ghost press. */
static void sens_enter_rearm(direct_control *dc, uint64_t now_ms)
{
sens_set_muted(dc, true);
sens_set_lowered(dc, false);
dc->sens_state = SENS_ST_REARM;
dc->sens_debounce_t0 = now_ms;
}
static int sens_handle_button(direct_control *dc, bool pressed,
uint64_t now_ms)
{
if (pressed) {
switch (dc->sens_state) {
case SENS_ST_REARM: {
bool ghost = now_ms - dc->sens_debounce_t0 <
SENS_GHOST_WINDOW_MS;
sens_set_lowered(dc, true);
dc->sens_state = ghost ? SENS_ST_SWALLOW : SENS_ST_DEBOUNCE;
dc->sens_debounce_t0 = now_ms;
if (dc->debug && ghost) {
printf("direct click (sensitivity): ghost press swallowed\n");
fflush(stdout);
}
return 0;
}
case SENS_ST_IDLE:
sens_set_muted(dc, true);
sens_set_lowered(dc, true);
dc->sens_state = SENS_ST_DEBOUNCE;
dc->sens_debounce_t0 = now_ms;
if (dc->debug) {
printf("direct click (sensitivity): press -> LOW, "
"debounce %dms\n", dc->sens_debounce_ms);
fflush(stdout);
}
return 0;
default:
return 0;
}
}
switch (dc->sens_state) {
case SENS_ST_DEBOUNCE: {
/* Could not hold the LOW threshold: light click. */
const click_action *act =
choose_classified_action(dc, false, classified_is_large(dc));
int rc = execute_click_down(dc, act);
execute_click_up(dc);
if (dc->debug) {
printf("direct click (sensitivity): light, area=%d -> %s\n",
dc->peak_area, act->raw_val);
fflush(stdout);
}
sens_enter_rearm(dc, now_ms);
return rc;
}
case SENS_ST_SWALLOW:
/* Phantom release of a swallowed ghost press: emit nothing. */
sens_enter_rearm(dc, now_ms);
return 0;
case SENS_ST_LIGHT:
case SENS_ST_HEAVY:
execute_click_up(dc);
sens_enter_rearm(dc, now_ms);
return 0;
default:
return 0;
}
}
static int handle_button(direct_control *dc, int value, uint64_t now_ms)
{
bool pressed = value != 0;
if (pressed == dc->physical_button)
return 0;
dc->physical_button = pressed;
if (pressed)
dc->peak_area = active_area(dc);
if (dc->sens_classified_click && dc->hidraw_fd >= 0)
return sens_handle_button(dc, pressed, now_ms);
if (pressed) {
dc->pending_click = true;
if (dc->area_classified_click && dc->click_window_ms > 0) {
/* Sample the contact area a little longer before classifying. */
dc->click_deadline_ms = now_ms + (uint64_t)dc->click_window_ms;
return 0;
}
return choose_pending_click(dc);
}
if (choose_pending_click(dc) < 0)
return -1;
execute_click_up(dc);
return 0;
}
static double scale_delta(const direct_control *dc, int delta)
{
double magnitude = delta < 0 ? -(double)delta : (double)delta;
return (double)delta * dc->pointer_speed *
(1.0 + dc->pointer_acceleration * magnitude);
}
static void process_syn(direct_control *dc, uint64_t now_ms)
{
int contacts = 0;
int single = -1;
int area = 0;
bool track_area = dc->pending_click ||
dc->sens_state == SENS_ST_DEBOUNCE ||
dc->sens_state == SENS_ST_SWALLOW;
for (int i = 0; i < dc->slot_count; i++) {
direct_slot *slot = &dc->slots[i];
if (!slot->active || !slot->have_position)
continue;
contacts++;
single = i;
if (track_area) {
int slot_area = slot->width * slot->height;
if (slot_area > area)
area = slot_area;
}
}
if (area > dc->peak_area)
dc->peak_area = area;
if (contacts > 0)
dc->spinning = false;
if (contacts == 0 && dc->sens_classified_click && dc->hidraw_fd >= 0 &&
(dc->sens_state != SENS_ST_IDLE || dc->sens_lowered ||
dc->sens_muted)) {
if (dc->sens_state == SENS_ST_LIGHT ||
dc->sens_state == SENS_ST_HEAVY)
execute_click_up(dc);
sens_rearm(dc);
}
if (dc->trackball && contacts == 1) {
direct_slot *slot = &dc->slots[single];
if (dc->previous_contacts == 1 &&
dc->motion_tracking_id == slot->tracking_id) {
double sx = scale_delta(dc, slot->x - dc->previous_x);
double sy = scale_delta(dc, slot->y - dc->previous_y);
dc->velocity_x = dc->velocity_x * 0.65 + sx * 0.35;
dc->velocity_y = dc->velocity_y * 0.65 + sy * 0.35;
} else {
dc->velocity_x = 0.0;
dc->velocity_y = 0.0;
dc->pointer_fraction_x = 0.0;
dc->pointer_fraction_y = 0.0;
}
dc->motion_tracking_id = slot->tracking_id;
dc->previous_x = slot->x;
dc->previous_y = slot->y;
} else if (contacts >= 2) {
dc->velocity_x = 0.0;
dc->velocity_y = 0.0;
} else if (dc->previous_contacts == 1 && dc->trackball &&
dc->touchpad_fd >= 0 &&
absolute(dc->velocity_x) + absolute(dc->velocity_y) > 0.35) {
dc->spinning = true;
dc->spin_at_ms = now_ms + SPIN_TICK_MS;
}
dc->previous_contacts = contacts;
}
static void process_state_event(direct_control *dc,
const struct input_event *event,
uint64_t now_ms)
{
if (event->type == EV_ABS) {
if (event->code == ABS_MT_SLOT) {
if (event->value >= 0 && event->value < dc->slot_count)
dc->slot = event->value;
return;
}
direct_slot *slot = &dc->slots[dc->slot];
switch (event->code) {
case ABS_MT_TRACKING_ID:
if (event->value < 0) {
slot->active = false;
slot->tracking_id = -1;
} else {
slot->active = true;
slot->tracking_id = event->value;
}
break;
case ABS_MT_POSITION_X:
slot->x = event->value;
slot->have_position = true;
break;
case ABS_MT_POSITION_Y:
slot->y = event->value;
slot->have_position = true;
break;
case ABS_MT_TOUCH_MAJOR: slot->width = event->value; break;
case ABS_MT_TOUCH_MINOR: slot->height = event->value; break;
default: break;
}
} else if (event->type == EV_SYN && event->code == SYN_REPORT) {
process_syn(dc, now_ms);
}
}
static void runtime_lost(direct_control *dc, uint64_t now_ms, int error)
{
fprintf(stderr, "multitouch passthrough disconnected: %s; "
"waiting to reconnect\n",
error ? strerror(error) : "end of device");
close_runtime(dc);
dc->retry_at_ms = now_ms + DIRECT_RETRY_MS;
}
void direct_handle_readable(direct_control *dc, uint64_t now_ms)
{
for (;;) {
struct input_event events[64];
ssize_t n = read(dc->input_fd, events, sizeof(events));
if (n < 0) {
if (errno == EAGAIN)
return;
if (errno == EINTR)
continue;
runtime_lost(dc, now_ms, errno);
return;
}
if (n == 0) {
runtime_lost(dc, now_ms, 0);
return;
}
int out = 0;
size_t count = (size_t)n / sizeof(events[0]);
for (size_t i = 0; i < count; i++) {
struct input_event *event = &events[i];
if (event->type == EV_SYN && event->code == SYN_DROPPED) {
if (write_events(dc->touchpad_fd, events, out) < 0)
runtime_lost(dc, now_ms, errno);
else
runtime_lost(dc, now_ms, EOVERFLOW);
return;
}
if (event->type == EV_KEY && event->code == BTN_LEFT) {
if (handle_button(dc, event->value, now_ms) < 0) {
runtime_lost(dc, now_ms, errno);
return;
}
continue;
}
process_state_event(dc, event, now_ms);
if ((size_t)out != i)
events[out] = *event;
out++;
}
if (write_events(dc->touchpad_fd, events, out) < 0) {
runtime_lost(dc, now_ms, errno);
return;
}
if ((size_t)n < sizeof(events))
return;
}
}
void direct_tick(direct_control *dc, uint64_t now_ms)
{
if (!dc->wanted)
return;
if (dc->input_fd < 0) {
if (now_ms < dc->retry_at_ms)
return;
if (open_runtime(dc) < 0) {
report_retry(dc, now_ms);
}
return;
}
if (dc->sens_classified_click && dc->hidraw_fd >= 0) {
/* Debounce expired with the button still latched through the LOW
* threshold: this press carries real force - a heavy click. A
* swallowed ghost-window press that survives was equally real. */
if ((dc->sens_state == SENS_ST_DEBOUNCE ||
dc->sens_state == SENS_ST_SWALLOW) &&
now_ms >= dc->sens_debounce_t0 + (uint64_t)dc->sens_debounce_ms)
sens_classify(dc, dc->physical_button);
/* Ghost window elapsed without a ghost: re-arm settled. */
else if (dc->sens_state == SENS_ST_REARM &&
now_ms >= dc->sens_debounce_t0 + SENS_GHOST_WINDOW_MS)
sens_rearm(dc);
}
if (dc->pending_click && now_ms >= dc->click_deadline_ms &&
choose_pending_click(dc) < 0) {
runtime_lost(dc, now_ms, errno);
return;
}
if (dc->spinning && now_ms >= dc->spin_at_ms) {
dc->pointer_fraction_x += dc->velocity_x;
dc->pointer_fraction_y += dc->velocity_y;
int x = take_integer(&dc->pointer_fraction_x);
int y = take_integer(&dc->pointer_fraction_y);
if (emit_relative(dc, x, y) < 0) {
destroy_virtual(&dc->touchpad_fd);
dc->spinning = false;
return;
}
dc->velocity_x *= SPIN_FRICTION;
dc->velocity_y *= SPIN_FRICTION;
if (absolute(dc->velocity_x) < SPIN_STOP_SPEED &&
absolute(dc->velocity_y) < SPIN_STOP_SPEED) {
dc->spinning = false;
} else {
dc->spin_at_ms = now_ms + SPIN_TICK_MS;
}
}
}
int direct_timeout_ms(const direct_control *dc, uint64_t now_ms)
{
if (!dc->wanted)
return -1;
uint64_t target = UINT64_MAX;
if (dc->input_fd < 0)
target = dc->retry_at_ms;
if (dc->pending_click && dc->click_deadline_ms < target)
target = dc->click_deadline_ms;
if (dc->sens_classified_click) {
uint64_t sens_target = 0;
if (dc->sens_state == SENS_ST_DEBOUNCE ||
dc->sens_state == SENS_ST_SWALLOW)
sens_target = dc->sens_debounce_t0 +
(uint64_t)dc->sens_debounce_ms;
else if (dc->sens_state == SENS_ST_REARM)
sens_target = dc->sens_debounce_t0 + SENS_GHOST_WINDOW_MS;
if (sens_target && sens_target < target)
target = sens_target;
}
if (dc->spinning && dc->spin_at_ms < target)
target = dc->spin_at_ms;
if (target == UINT64_MAX)
return -1;
if (target <= now_ms)
return 0;
return (int)(target - now_ms);
}
void direct_close(direct_control *dc)
{
dc->wanted = false;
close_runtime(dc);
}