Skip to content

Commit cac6f6a

Browse files
maxime-desrochesComma Device
andauthored
comma: process finger removal only once (#10)
Co-authored-by: Comma Device <device@comma.ai>
1 parent 7696827 commit cac6f6a

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/platforms/rcore_comma.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,14 @@
6868
// Types and Structures Definition
6969
//----------------------------------------------------------------------------------
7070

71+
typedef enum {
72+
FINGER_STATE_REMOVED = 0, // state when finger was removed and we handled its removal + default state
73+
FINGER_STATE_REMOVING, // state when finger is currently being removed from panel (released event)
74+
FINGER_STATE_TOUCHING, // state when finger is touching panel at any time
75+
} FingerState;
76+
7177
struct finger {
72-
TouchAction action;
78+
FingerState state;
7379
int x;
7480
int y;
7581
bool resetNextFrame;
@@ -315,7 +321,7 @@ static int init_touch(const char *dev_path, const char *origin_path) {
315321
for (int i = 0; i < MAX_TOUCH_POINTS; ++i) {
316322
platform.touch.fingers[i].x = -1;
317323
platform.touch.fingers[i].y = -1;
318-
platform.touch.fingers[i].action = TOUCH_ACTION_UP;
324+
platform.touch.fingers[i].state = FINGER_STATE_REMOVED;
319325
platform.touch.fingers[i].resetNextFrame = false;
320326

321327
CORE.Input.Touch.currentTouchState[0] = 0;
@@ -640,7 +646,7 @@ void PollInputEvents(void) {
640646
if (event.type == SYN_REPORT) { // synchronization frame. Expose completed events back to the library
641647

642648
for (int i = 0; i < MAX_TOUCH_POINTS; ++i) {
643-
if (platform.touch.fingers[i].action == TOUCH_ACTION_DOWN) {
649+
if (platform.touch.fingers[i].state == FINGER_STATE_TOUCHING) {
644650

645651
CORE.Input.Touch.position[i].x = platform.touch.fingers[i].x;
646652
CORE.Input.Touch.position[i].y = platform.touch.fingers[i].y;
@@ -653,9 +659,10 @@ void PollInputEvents(void) {
653659
CORE.Input.Mouse.currentPosition.y = platform.touch.fingers[i].y;
654660
}
655661

656-
} else if (platform.touch.fingers[i].action == TOUCH_ACTION_UP) {
662+
} else if (platform.touch.fingers[i].state == FINGER_STATE_REMOVING) {
657663
CORE.Input.Touch.position[i].x = -1;
658664
CORE.Input.Touch.position[i].y = -1;
665+
659666
// if we received a touch down and up event in the same frame,
660667
// delay up event by one frame so that API user needs no special handling
661668
if (CORE.Input.Touch.previousTouchState[i] == 0) {
@@ -664,6 +671,8 @@ void PollInputEvents(void) {
664671
} else {
665672
CORE.Input.Touch.currentTouchState[i] = 0;
666673
}
674+
675+
platform.touch.fingers[i].state = FINGER_STATE_REMOVED;
667676
}
668677
}
669678

@@ -672,7 +681,7 @@ void PollInputEvents(void) {
672681
if (event.code == ABS_MT_SLOT) { // switch finger
673682
slot = event.value;
674683
} else if (event.code == ABS_MT_TRACKING_ID) { // finger on screen or not
675-
platform.touch.fingers[slot].action = event.value == -1 ? TOUCH_ACTION_UP : TOUCH_ACTION_DOWN;
684+
platform.touch.fingers[slot].state = event.value == -1 ? FINGER_STATE_REMOVING : FINGER_STATE_TOUCHING;
676685
} else if (event.code == ABS_MT_POSITION_X) {
677686
platform.touch.fingers[slot].y = (1 - platform.touch.canonical) * (CORE.Window.screen.height - event.value) + (platform.touch.canonical * event.value);
678687
} else if (event.code == ABS_MT_POSITION_Y) {
@@ -683,7 +692,7 @@ void PollInputEvents(void) {
683692

684693
// count how many fingers are left on the screen after processing all events
685694
for (int i = 0; i < MAX_TOUCH_POINTS; ++i) {
686-
CORE.Input.Touch.pointCount += platform.touch.fingers[i].action != TOUCH_ACTION_UP;
695+
CORE.Input.Touch.pointCount += platform.touch.fingers[i].state == FINGER_STATE_TOUCHING;
687696
}
688697
}
689698

0 commit comments

Comments
 (0)