comma: real multi-touch (#7)

This commit is contained in:
Maxime Desroches 2025-05-26 18:37:57 -07:00 committed by GitHub
parent 3cf4f3abdd
commit e0823aace6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -70,13 +70,12 @@
struct finger { struct finger {
TouchAction action; TouchAction action;
TouchAction gesture_action;
int x; int x;
int y; int y;
}; };
struct touch { struct touch {
struct finger fingers[1]; struct finger fingers[MAX_TOUCH_POINTS];
int fd; int fd;
int canonical; int canonical;
}; };
@ -312,20 +311,24 @@ static int init_touch(const char *dev_path, const char *origin_path) {
return -1; return -1;
} }
platform.touch.fingers[0].x = -1; for (int i = 0; i < MAX_TOUCH_POINTS; ++i) {
platform.touch.fingers[0].y = -1; platform.touch.fingers[i].x = -1;
platform.touch.fingers[0].action = TOUCH_ACTION_UP; platform.touch.fingers[i].y = -1;
platform.touch.fingers[0].gesture_action = TOUCH_ACTION_UP; platform.touch.fingers[i].action = TOUCH_ACTION_UP;
CORE.Input.Touch.currentTouchState[0] = 0; CORE.Input.Touch.currentTouchState[0] = 0;
CORE.Input.Touch.previousTouchState[0] = 0;
}
for (int i = 0; i < MAX_MOUSE_BUTTONS; ++i) {
CORE.Input.Mouse.currentButtonState[i] = 0;
CORE.Input.Mouse.previousButtonState[i] = 0;
}
CORE.Input.Mouse.currentPosition.x = -1; CORE.Input.Mouse.currentPosition.x = -1;
CORE.Input.Mouse.currentPosition.y = -1; CORE.Input.Mouse.currentPosition.y = -1;
CORE.Input.Mouse.currentButtonState[0] = 0;
CORE.Input.Touch.previousTouchState[0] = 0;
CORE.Input.Mouse.previousPosition.x = -1; CORE.Input.Mouse.previousPosition.x = -1;
CORE.Input.Mouse.previousPosition.y = -1; CORE.Input.Mouse.previousPosition.y = -1;
CORE.Input.Mouse.previousButtonState[0] = 0;
return 0; return 0;
} }
@ -614,60 +617,47 @@ void PollInputEvents(void) {
// slot i is for events of finger i // slot i is for events of finger i
static int slot = 0; static int slot = 0;
// must be called every frames for (int i = 0; i < MAX_TOUCH_POINTS; ++i) {
UpdateGestures(); CORE.Input.Touch.previousTouchState[i] = CORE.Input.Touch.currentTouchState[i];
}
for (int i = 0; i < MAX_MOUSE_BUTTONS; ++i) {
CORE.Input.Mouse.previousButtonState[i] = CORE.Input.Mouse.currentButtonState[i];
}
CORE.Input.Touch.previousTouchState[0] = CORE.Input.Touch.currentTouchState[0];
CORE.Input.Mouse.previousButtonState[0] = CORE.Input.Mouse.currentButtonState[0];
CORE.Input.Mouse.previousPosition = CORE.Input.Mouse.currentPosition; CORE.Input.Mouse.previousPosition = CORE.Input.Mouse.currentPosition;
CORE.Input.Touch.pointCount = 0; CORE.Input.Touch.pointCount = 0;
struct input_event event = {0}; struct input_event event = {0};
while (read(platform.touch.fd, &event, sizeof(struct input_event)) == sizeof(struct input_event)) { while (read(platform.touch.fd, &event, sizeof(struct input_event)) == sizeof(struct input_event)) {
if (event.type == SYN_REPORT && slot == 0) { // sync event for main finger if (event.type == SYN_REPORT) { // synchronization frame. Expose completed events back to the library
CORE.Input.Touch.pointCount = 1; for (int i = 0; i < MAX_TOUCH_POINTS; ++i) {
if (platform.touch.fingers[i].action == TOUCH_ACTION_DOWN) {
if (platform.touch.fingers[slot].action == TOUCH_ACTION_DOWN) { CORE.Input.Touch.position[i].x = platform.touch.fingers[i].x;
// detect new touch on screen CORE.Input.Touch.position[i].y = platform.touch.fingers[i].y;
platform.touch.fingers[slot].gesture_action = platform.touch.fingers[slot].gesture_action == TOUCH_ACTION_UP ? TOUCH_ACTION_DOWN : TOUCH_ACTION_MOVE; CORE.Input.Touch.currentTouchState[i] = 1;
CORE.Input.Touch.position[slot].x = platform.touch.fingers[slot].x; // map main finger on mouse for conveniance. raylib already does that
CORE.Input.Touch.position[slot].y = platform.touch.fingers[slot].y; // for pressed state, but not pos
CORE.Input.Touch.currentTouchState[slot] = 1; if (i == 0) {
// map touch state on mouse for conveniance CORE.Input.Mouse.currentPosition.x = platform.touch.fingers[i].x;
CORE.Input.Mouse.currentPosition.x = platform.touch.fingers[slot].x; CORE.Input.Mouse.currentPosition.y = platform.touch.fingers[i].y;
CORE.Input.Mouse.currentPosition.y = platform.touch.fingers[slot].y;
CORE.Input.Mouse.currentButtonState[slot] = 1;
} else if (platform.touch.fingers[slot].action == TOUCH_ACTION_UP) {
// finger off the screen
platform.touch.fingers[slot].gesture_action = TOUCH_ACTION_UP;
CORE.Input.Touch.position[slot].x = -1;
CORE.Input.Touch.position[slot].y = -1;
CORE.Input.Touch.currentTouchState[slot] = 0;
CORE.Input.Mouse.currentButtonState[slot] = 0;
CORE.Input.Touch.previousTouchState[slot] = 1;
CORE.Input.Mouse.previousButtonState[slot] = 1;
} }
// interpret gestures (scroll, pinch, drag, ...) } else if (platform.touch.fingers[i].action == TOUCH_ACTION_UP) {
GestureEvent gestureEvent = {0}; CORE.Input.Touch.position[i].x = -1;
gestureEvent.touchAction = platform.touch.fingers[slot].gesture_action; CORE.Input.Touch.position[i].y = -1;
gestureEvent.pointCount = CORE.Input.Touch.pointCount; CORE.Input.Touch.currentTouchState[i] = 0;
gestureEvent.pointId[slot] = slot; }
gestureEvent.position[slot] = CORE.Input.Touch.position[slot];
ProcessGestureEvent(gestureEvent);
} }
if (event.type == EV_ABS) { } else if (event.type == EV_ABS) { // raw events. Process these untill we get a sync frame
if (event.code == ABS_MT_SLOT) { // new finger on the screen
if (event.code == ABS_MT_SLOT) { // switch finger
slot = event.value; slot = event.value;
} } else if (event.code == ABS_MT_TRACKING_ID) { // finger on screen or not
if (slot == 0) { // only handle events for main finger
if (event.code == ABS_MT_TRACKING_ID) {
platform.touch.fingers[slot].action = event.value == -1 ? TOUCH_ACTION_UP : TOUCH_ACTION_DOWN; platform.touch.fingers[slot].action = event.value == -1 ? TOUCH_ACTION_UP : TOUCH_ACTION_DOWN;
} else if (event.code == ABS_MT_POSITION_X) { } else if (event.code == ABS_MT_POSITION_X) {
platform.touch.fingers[slot].y = (1 - platform.touch.canonical) * (CORE.Window.screen.height - event.value) + (platform.touch.canonical * event.value); platform.touch.fingers[slot].y = (1 - platform.touch.canonical) * (CORE.Window.screen.height - event.value) + (platform.touch.canonical * event.value);
@ -676,6 +666,10 @@ void PollInputEvents(void) {
} }
} }
} }
// count how many fingers are left on the screen after processing all events
for (int i = 0; i < MAX_TOUCH_POINTS; ++i) {
CORE.Input.Touch.pointCount += platform.touch.fingers[i].action != TOUCH_ACTION_UP;
} }
} }