diff --git a/src/platforms/rcore_comma.c b/src/platforms/rcore_comma.c index 0fe51cff1..be60c7663 100644 --- a/src/platforms/rcore_comma.c +++ b/src/platforms/rcore_comma.c @@ -50,6 +50,9 @@ #include #include #include +#include + +#include #include #include @@ -65,6 +68,18 @@ // Types and Structures Definition //---------------------------------------------------------------------------------- +struct finger { + TouchAction action; + TouchAction gesture_action; + int x; + int y; +}; + +struct touch { + struct finger fingers[1]; + int fd; +}; + // hold all the low level wayland stuff struct wayland_platform { struct wl_compositor *wl_compositor; @@ -94,6 +109,7 @@ struct egl_platform { typedef struct { struct wayland_platform wayland; struct egl_platform egl; + struct touch touch; } PlatformData; //---------------------------------------------------------------------------------- @@ -272,6 +288,30 @@ static int init_egl () { return 0; } +static int init_touch(const char *dev_path) { + platform.touch.fd = open(dev_path, O_RDONLY|O_NONBLOCK); + if (platform.touch.fd < 0) { + TRACELOG(LOG_WARNING, "COMMA: Failed to open touch device at %s", dev_path); + return -1; + } + + platform.touch.fingers[0].x = -1; + platform.touch.fingers[0].y = -1; + platform.touch.fingers[0].action = TOUCH_ACTION_UP; + platform.touch.fingers[0].gesture_action = TOUCH_ACTION_UP; + + CORE.Input.Touch.currentTouchState[0] = 0; + CORE.Input.Mouse.currentPosition.x = -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.y = -1; + CORE.Input.Mouse.previousButtonState[0] = 0; + + return 0; +} //---------------------------------------------------------------------------------- // Module Internal Functions Declaration @@ -549,8 +589,73 @@ const char *GetKeyName(int key) { return ""; } -// Register all input events void PollInputEvents(void) { + // slot i is for events of finger i + static int slot = 0; + + // must be called every frames + UpdateGestures(); + + 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.Touch.pointCount = 0; + + struct input_event event = {0}; + 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 + + CORE.Input.Touch.pointCount = 1; + + if (platform.touch.fingers[slot].action == TOUCH_ACTION_DOWN) { + // detect new touch on screen + platform.touch.fingers[slot].gesture_action = platform.touch.fingers[slot].gesture_action == TOUCH_ACTION_UP ? TOUCH_ACTION_DOWN : TOUCH_ACTION_MOVE; + + CORE.Input.Touch.position[slot].x = platform.touch.fingers[slot].x; + CORE.Input.Touch.position[slot].y = platform.touch.fingers[slot].y; + CORE.Input.Touch.currentTouchState[slot] = 1; + // map touch state on mouse for conveniance + CORE.Input.Mouse.currentPosition.x = platform.touch.fingers[slot].x; + 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, ...) + GestureEvent gestureEvent = {0}; + gestureEvent.touchAction = platform.touch.fingers[slot].gesture_action; + gestureEvent.pointCount = CORE.Input.Touch.pointCount; + gestureEvent.pointId[slot] = slot; + gestureEvent.position[slot] = CORE.Input.Touch.position[slot]; + ProcessGestureEvent(gestureEvent); + } + + if (event.type == EV_ABS) { + if (event.code == ABS_MT_SLOT) { // new finger on the screen + slot = event.value; + } + + 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; + } else if (event.code == ABS_MT_POSITION_X) { + platform.touch.fingers[slot].y = CORE.Window.screen.height - event.value; + } else if (event.code == ABS_MT_POSITION_Y) { + platform.touch.fingers[slot].x = event.value; + } + } + } + } } //---------------------------------------------------------------------------------- @@ -581,6 +686,11 @@ int InitPlatform(void) { return -1; } + if (init_touch("/dev/input/event2")) { + TRACELOG(LOG_FATAL, "COMMA: Failed to initialize touch device"); + return -1; + } + SetupFramebuffer(CORE.Window.display.width, CORE.Window.display.height); rlLoadExtensions(eglGetProcAddress); InitTimer();