From ee45ed2306c0f8e94ea51cd4e68311efd9fb9418 Mon Sep 17 00:00:00 2001 From: rokokol Date: Sat, 26 Sep 2026 15:05:51 +0300 Subject: [PATCH] glfw: Report touch events Add a touch callback that reports abstract touch events. The design follows QTouchEvent and NSTouch. A sequence opens with GLFW_TOUCH_BEGIN, goes on with GLFW_TOUCH_UPDATE and closes with GLFW_TOUCH_END or GLFW_TOUCH_CANCEL. Each event carries every point that is down on the window, with its id, its state and its position. The Wayland backend binds wl_touch and collects the down, motion and up events until the frame event. It then sends one event to each window that has a changed point. A point belongs to the window it first touched, because wl_touch gives its position relative to that surface. A cancel from the compositor reaches only the windows that were told that the sequence began. Touches on client-side decorations are not reported. glfw does not turn these events into mouse events. That is left to the application. Assisted-by: Claude Code:claude-opus-5-5 (mostly) --- glfw/glfw3.h | 88 ++++++++++++++++++++++++ glfw/input.c | 17 +++++ glfw/internal.h | 2 + glfw/wl_init.c | 159 +++++++++++++++++++++++++++++++++++++++++++ glfw/wl_platform.h | 16 +++++ kitty/glfw-wrapper.c | 3 + kitty/glfw-wrapper.h | 60 ++++++++++++++++ 7 files changed, 345 insertions(+) diff --git a/glfw/glfw3.h b/glfw/glfw3.h index c689f8607..dab387ccc 100644 --- a/glfw/glfw3.h +++ b/glfw/glfw3.h @@ -566,6 +566,43 @@ typedef struct GLFWScrollEvent { int keyboard_modifiers; } GLFWScrollEvent; +typedef enum GLFWTouchEventType { + // The first finger of a sequence touched the window + GLFW_TOUCH_BEGIN = 1, + // A finger touched, moved or lifted while others stay down + GLFW_TOUCH_UPDATE = 2, + // The last finger of the sequence lifted + GLFW_TOUCH_END = 3, + // The system took the sequence over, for example for a gesture of its own; + // the application must discard everything the sequence did so far + GLFW_TOUCH_CANCEL = 4, +} GLFWTouchEventType; + +typedef enum GLFWTouchPointState { + GLFW_TOUCH_POINT_PRESSED = 1, + GLFW_TOUCH_POINT_MOVED = 2, + GLFW_TOUCH_POINT_STATIONARY = 3, + GLFW_TOUCH_POINT_RELEASED = 4, +} GLFWTouchPointState; + +typedef struct GLFWTouchPoint { + // Unique among the points that are down at the same time; the system can + // reuse it after the point is released + int32_t id; + GLFWTouchPointState state; + // In window content coordinates, the same as the cursor position + double x, y; +} GLFWTouchPoint; + +typedef struct GLFWTouchEvent { + GLFWTouchEventType type; + int keyboard_modifiers; + // Every point that is down on the window, and the points that were released + // in this event. The array is valid only during the callback + size_t num_points; + const GLFWTouchPoint *points; +} GLFWTouchEvent; + /*! @defgroup joysticks Joysticks * @brief Joystick IDs. * @@ -1780,6 +1817,25 @@ typedef void (*GLFWcursorenterfun)(GLFWwindow *, int); */ typedef void (*GLFWscrollfun)(GLFWwindow *, const GLFWScrollEvent *); +/*! @brief The function pointer type for touch callbacks. + * + * This is the function pointer type for touch callbacks. A touch callback + * function has the following signature: + * @code + * void function_name(GLFWwindow* window, const GLFWTouchEvent* event) + * @endcode + * + * @param[in] window The window that received the event. + * @param[in] event The touch event. A sequence opens with @ref + * GLFW_TOUCH_BEGIN and closes with either @ref GLFW_TOUCH_END or @ref + * GLFW_TOUCH_CANCEL. + * + * @sa @ref glfwSetTouchCallback + * + * @ingroup input + */ +typedef void (*GLFWtouchfun)(GLFWwindow *, const GLFWTouchEvent *); + /*! @brief The function pointer type for key callbacks. * * This is the function pointer type for key callbacks. A keyboard @@ -4993,6 +5049,38 @@ GLFWAPI GLFWcursorenterfun glfwSetCursorEnterCallback(GLFWwindow *window, GLFWcu */ GLFWAPI GLFWscrollfun glfwSetScrollCallback(GLFWwindow *window, GLFWscrollfun callback); +/*! @brief Sets the touch callback. + * + * This function sets the touch callback of the specified window, which is + * called when fingers touch the window content on a touchscreen. The events + * are not converted to mouse events: that is left to the application. + * + * A touch point belongs to the window it first touched and is reported to + * that window alone until it is released. + * + * Only the Wayland backend reports touch events at the moment. + * + * @param[in] window The window whose callback to set. + * @param[in] callback The new touch callback, or `NULL` to remove the + * currently set callback. + * @return The previously set callback, or `NULL` if no callback was set or the + * library had not been [initialized](@ref intro_init). + * + * @callback_signature + * @code + * void function_name(GLFWwindow* window, const GLFWTouchEvent* event) + * @endcode + * For more information about the callback parameters, see the + * [function pointer type](@ref GLFWtouchfun). + * + * @errors Possible errors include @ref GLFW_NOT_INITIALIZED. + * + * @thread_safety This function must only be called from the main thread. + * + * @ingroup input + */ +GLFWAPI GLFWtouchfun glfwSetTouchCallback(GLFWwindow *window, GLFWtouchfun callback); + GLFWAPI GLFWliveresizefun glfwSetLiveResizeCallback(GLFWwindow *window, GLFWliveresizefun callback); GLFWAPI GLFWdropeventfun glfwSetDropEventCallback(GLFWwindow *window, GLFWdropeventfun callback); diff --git a/glfw/input.c b/glfw/input.c index fdf6ecc22..10675b220 100644 --- a/glfw/input.c +++ b/glfw/input.c @@ -314,6 +314,13 @@ _glfwInputScroll(_GLFWwindow *window, const GLFWScrollEvent *ev) { if (window->callbacks.scroll) window->callbacks.scroll((GLFWwindow *)window, ev); } +// Notifies shared code of a touch event +// +void +_glfwInputTouch(_GLFWwindow *window, const GLFWTouchEvent *ev) { + if (window->callbacks.touch) window->callbacks.touch((GLFWwindow *)window, ev); +} + // Notifies shared code of a mouse button click event // void @@ -1090,6 +1097,16 @@ glfwSetScrollCallback(GLFWwindow *handle, GLFWscrollfun cbfun) { return cbfun; } +GLFWAPI GLFWtouchfun +glfwSetTouchCallback(GLFWwindow *handle, GLFWtouchfun cbfun) { + _GLFWwindow *window = (_GLFWwindow *)handle; + assert(window != NULL); + + _GLFW_REQUIRE_INIT_OR_RETURN(NULL); + _GLFW_SWAP_POINTERS(window->callbacks.touch, cbfun); + return cbfun; +} + GLFWAPI GLFWdropeventfun glfwSetDropEventCallback(GLFWwindow *handle, GLFWdropeventfun cbfun) { _GLFWwindow *window = (_GLFWwindow *)handle; diff --git a/glfw/internal.h b/glfw/internal.h index ee2ce2272..f89d61653 100644 --- a/glfw/internal.h +++ b/glfw/internal.h @@ -470,6 +470,7 @@ struct _GLFWwindow { GLFWcursorposfun cursorPos; GLFWcursorenterfun cursorEnter; GLFWscrollfun scroll; + GLFWtouchfun touch; GLFWkeyboardfun keyboard; GLFWliveresizefun liveResize; @@ -803,6 +804,7 @@ void _glfwInputWindowMonitor(_GLFWwindow *window, _GLFWmonitor *monitor); void _glfwInputKeyboard(_GLFWwindow *window, GLFWkeyevent *ev); void _glfwInputClipboardLost(GLFWClipboardType which); void _glfwInputScroll(_GLFWwindow *window, const GLFWScrollEvent *ev); +void _glfwInputTouch(_GLFWwindow *window, const GLFWTouchEvent *ev); void _glfwInputMouseClick(_GLFWwindow *window, int button, int action, int mods); void _glfwInputCursorPos(_GLFWwindow *window, double xpos, double ypos); void _glfwInputCursorEnter(_GLFWwindow *window, bool entered); diff --git a/glfw/wl_init.c b/glfw/wl_init.c index a06313f2b..8d6018f3a 100644 --- a/glfw/wl_init.c +++ b/glfw/wl_init.c @@ -313,6 +313,155 @@ static const struct wl_pointer_listener pointerListener = { #endif }; +#define touch (_glfw.wl.touch_state) + +static ssize_t +touch_point_index(int32_t id) { + for (size_t i = 0; i < touch.count; i++) + if (touch.points[i].id == id) return i; + return -1; +} + +// Sends one window the points it owns, in the given event type. When as_state +// is non-zero, every point is reported in that state instead of its own, and +// include_new decides whether points the window has not heard of yet go too +static void +touch_send_to_window(GLFWid window_id, GLFWTouchEventType type, GLFWTouchPointState as_state, bool include_new) { + _GLFWwindow *window = _glfwWindowForId(window_id); + if (!window) return; + GLFWTouchPoint points[arraysz(touch.points)]; + size_t n = 0; + for (size_t i = 0; i < touch.count; i++) { + if (touch.points[i].window_id != window_id || (!include_new && touch.points[i].pressed_in_frame)) continue; + points[n++] = (GLFWTouchPoint){ + .id = touch.points[i].id, + .state = as_state ? as_state : touch.points[i].state, + .x = touch.points[i].x, + .y = touch.points[i].y, + }; + } + if (!n) return; + GLFWTouchEvent ev = {.type = type, .keyboard_modifiers = _glfw.wl.xkb.states.modifiers, .num_points = n, .points = points}; + _glfwInputTouch(window, &ev); +} + +static void +touchHandleDown( + void *data UNUSED, + struct wl_touch *wl_touch UNUSED, + uint32_t serial, + uint32_t time UNUSED, + struct wl_surface *surface, + int32_t id, + wl_fixed_t sx, + wl_fixed_t sy) { + _GLFWwindow *window = get_window_from_surface(surface); + // A touch on a client-side decoration is not reported + if (!window || surface != window->wl.surface) return; + if (touch.count >= arraysz(touch.points) || touch_point_index(id) > -1) return; + _glfw.wl.serial = serial; + _glfw.wl.input_serial = serial; + touch.points[touch.count++] = (_GLFWWaylandTouchPoint){ + .id = id, + .window_id = window->id, + .state = GLFW_TOUCH_POINT_PRESSED, + .pressed_in_frame = true, + .x = wl_fixed_to_double(sx), + .y = wl_fixed_to_double(sy), + }; +} + +static void +touchHandleUp(void *data UNUSED, struct wl_touch *wl_touch UNUSED, uint32_t serial, uint32_t time UNUSED, int32_t id) { + const ssize_t i = touch_point_index(id); + if (i < 0) return; + _glfw.wl.serial = serial; + _glfw.wl.input_serial = serial; + touch.points[i].state = GLFW_TOUCH_POINT_RELEASED; +} + +static void +touchHandleMotion(void *data UNUSED, struct wl_touch *wl_touch UNUSED, uint32_t time UNUSED, int32_t id, wl_fixed_t sx, wl_fixed_t sy) { + const ssize_t i = touch_point_index(id); + if (i < 0) return; + touch.points[i].x = wl_fixed_to_double(sx); + touch.points[i].y = wl_fixed_to_double(sy); + if (touch.points[i].state == GLFW_TOUCH_POINT_STATIONARY) touch.points[i].state = GLFW_TOUCH_POINT_MOVED; +} + +// A frame ends a group of events that happened at the same time. Each window with a +// changed point gets one event holding all of its points +static void +touchHandleFrame(void *data UNUSED, struct wl_touch *wl_touch UNUSED) { + GLFWid done[arraysz(touch.points)]; + size_t num_done = 0; + for (size_t i = 0; i < touch.count; i++) { + const GLFWid window_id = touch.points[i].window_id; + bool changed = false, seen = false; + for (size_t d = 0; d < num_done && !seen; d++) seen = done[d] == window_id; + if (seen) continue; + done[num_done++] = window_id; + size_t down_before = 0, down_after = 0; + for (size_t j = i; j < touch.count; j++) { + if (touch.points[j].window_id != window_id) continue; + if (touch.points[j].state != GLFW_TOUCH_POINT_STATIONARY) changed = true; + if (!touch.points[j].pressed_in_frame) down_before++; + if (touch.points[j].state != GLFW_TOUCH_POINT_RELEASED) down_after++; + } + if (!changed) continue; + if (!down_before && !down_after) { + // Every finger landed and lifted within this frame: the sequence still + // opens before it closes + touch_send_to_window(window_id, GLFW_TOUCH_BEGIN, GLFW_TOUCH_POINT_PRESSED, true); + touch_send_to_window(window_id, GLFW_TOUCH_END, 0, true); + } else touch_send_to_window(window_id, !down_before ? GLFW_TOUCH_BEGIN : !down_after ? GLFW_TOUCH_END : GLFW_TOUCH_UPDATE, 0, true); + } + size_t kept = 0; + for (size_t i = 0; i < touch.count; i++) { + if (touch.points[i].state == GLFW_TOUCH_POINT_RELEASED) continue; + touch.points[kept] = touch.points[i]; + touch.points[kept].state = GLFW_TOUCH_POINT_STATIONARY; + touch.points[kept].pressed_in_frame = false; + kept++; + } + touch.count = kept; +} + +// The compositor took every active sequence over, for a gesture of its own. A +// window hears of it only if it was told the sequence began +static void +touchHandleCancel(void *data UNUSED, struct wl_touch *wl_touch UNUSED) { + GLFWid done[arraysz(touch.points)]; + size_t num_done = 0; + for (size_t i = 0; i < touch.count; i++) { + const GLFWid window_id = touch.points[i].window_id; + bool seen = false; + for (size_t d = 0; d < num_done && !seen; d++) seen = done[d] == window_id; + if (seen) continue; + done[num_done++] = window_id; + touch_send_to_window(window_id, GLFW_TOUCH_CANCEL, GLFW_TOUCH_POINT_RELEASED, false); + } + touch.count = 0; +} + +static void +touchHandleShape(void *data UNUSED, struct wl_touch *wl_touch UNUSED, int32_t id UNUSED, wl_fixed_t major UNUSED, wl_fixed_t minor UNUSED) {} + +static void +touchHandleOrientation(void *data UNUSED, struct wl_touch *wl_touch UNUSED, int32_t id UNUSED, wl_fixed_t orientation UNUSED) {} + +#undef touch + +static const struct wl_touch_listener touchListener = { + .down = touchHandleDown, + .up = touchHandleUp, + .motion = touchHandleMotion, + .frame = touchHandleFrame, + .cancel = touchHandleCancel, + .shape = touchHandleShape, + .orientation = touchHandleOrientation, +}; + static void keyboardHandleKeymap(void *data UNUSED, struct wl_keyboard *keyboard UNUSED, uint32_t format, int fd, uint32_t size) { char *mapStr; @@ -517,6 +666,15 @@ seatHandleCapabilities(void *data UNUSED, struct wl_seat *seat, enum wl_seat_cap if (_glfw.wl.cursorAnimationTimer) toggleTimer(&_glfw.wl.eventLoopData, _glfw.wl.cursorAnimationTimer, 0); } + if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !_glfw.wl.touch) { + _glfw.wl.touch = wl_seat_get_touch(seat); + wl_touch_add_listener(_glfw.wl.touch, &touchListener, NULL); + } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && _glfw.wl.touch) { + touchHandleCancel(NULL, _glfw.wl.touch); + wl_touch_destroy(_glfw.wl.touch); + _glfw.wl.touch = NULL; + } + if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !_glfw.wl.keyboard) { _glfw.wl.keyboard = wl_seat_get_keyboard(seat); wl_keyboard_add_listener(_glfw.wl.keyboard, &keyboardListener, NULL); @@ -900,6 +1058,7 @@ _glfwPlatformTerminate(void) { if (_glfw.wl.virtual_keyboard_manager) zwp_virtual_keyboard_manager_v1_destroy(_glfw.wl.virtual_keyboard_manager); if (_glfw.wl.pointer) wl_pointer_destroy(_glfw.wl.pointer); if (_glfw.wl.keyboard) wl_keyboard_destroy(_glfw.wl.keyboard); + if (_glfw.wl.touch) wl_touch_destroy(_glfw.wl.touch); if (_glfw.wl.seat) wl_seat_destroy(_glfw.wl.seat); if (_glfw.wl.relativePointerManager) zwp_relative_pointer_manager_v1_destroy(_glfw.wl.relativePointerManager); if (_glfw.wl.pointerConstraints) zwp_pointer_constraints_v1_destroy(_glfw.wl.pointerConstraints); diff --git a/glfw/wl_platform.h b/glfw/wl_platform.h index 92cb433bb..55fb0e97f 100644 --- a/glfw/wl_platform.h +++ b/glfw/wl_platform.h @@ -331,6 +331,14 @@ typedef struct _GLFWWaylandDataOffer { size_t dd_capacity, dd_count; } _GLFWWaylandDataOffer; +typedef struct _GLFWWaylandTouchPoint { + int32_t id; + GLFWid window_id; + GLFWTouchPointState state; + bool pressed_in_frame; + double x, y; +} _GLFWWaylandTouchPoint; + // Wayland-specific global data // typedef struct _GLFWlibraryWayland { @@ -342,6 +350,14 @@ typedef struct _GLFWlibraryWayland { struct wl_seat *seat; struct wl_pointer *pointer; struct wl_keyboard *keyboard; + struct wl_touch *touch; + // The touch points that are down, each tied to the window it landed on. + // Changes collect here and are reported at the wl_touch frame event. + // pressed_in_frame marks a point the application has not heard of yet + struct { + _GLFWWaylandTouchPoint points[16]; + size_t count; + } touch_state; struct wl_data_device_manager *dataDeviceManager; struct wl_data_device *dataDevice; struct xdg_wm_base *wmBase; diff --git a/kitty/glfw-wrapper.c b/kitty/glfw-wrapper.c index fe21c3c13..575b594a5 100644 --- a/kitty/glfw-wrapper.c +++ b/kitty/glfw-wrapper.c @@ -362,6 +362,9 @@ load_glfw(const char* path) { *(void **) (&glfwSetScrollCallback_impl) = dlsym(handle, "glfwSetScrollCallback"); if (glfwSetScrollCallback_impl == NULL) fail("Failed to load glfw function glfwSetScrollCallback with error: %s", dlerror()); + *(void **) (&glfwSetTouchCallback_impl) = dlsym(handle, "glfwSetTouchCallback"); + if (glfwSetTouchCallback_impl == NULL) fail("Failed to load glfw function glfwSetTouchCallback with error: %s", dlerror()); + *(void **) (&glfwSetLiveResizeCallback_impl) = dlsym(handle, "glfwSetLiveResizeCallback"); if (glfwSetLiveResizeCallback_impl == NULL) fail("Failed to load glfw function glfwSetLiveResizeCallback with error: %s", dlerror()); diff --git a/kitty/glfw-wrapper.h b/kitty/glfw-wrapper.h index 868d5ea49..c6b8871a1 100644 --- a/kitty/glfw-wrapper.h +++ b/kitty/glfw-wrapper.h @@ -306,6 +306,43 @@ typedef struct GLFWScrollEvent { int keyboard_modifiers; } GLFWScrollEvent; +typedef enum GLFWTouchEventType { + // The first finger of a sequence touched the window + GLFW_TOUCH_BEGIN = 1, + // A finger touched, moved or lifted while others stay down + GLFW_TOUCH_UPDATE = 2, + // The last finger of the sequence lifted + GLFW_TOUCH_END = 3, + // The system took the sequence over, for example for a gesture of its own; + // the application must discard everything the sequence did so far + GLFW_TOUCH_CANCEL = 4, +} GLFWTouchEventType; + +typedef enum GLFWTouchPointState { + GLFW_TOUCH_POINT_PRESSED = 1, + GLFW_TOUCH_POINT_MOVED = 2, + GLFW_TOUCH_POINT_STATIONARY = 3, + GLFW_TOUCH_POINT_RELEASED = 4, +} GLFWTouchPointState; + +typedef struct GLFWTouchPoint { + // Unique among the points that are down at the same time; the system can + // reuse it after the point is released + int32_t id; + GLFWTouchPointState state; + // In window content coordinates, the same as the cursor position + double x, y; +} GLFWTouchPoint; + +typedef struct GLFWTouchEvent { + GLFWTouchEventType type; + int keyboard_modifiers; + // Every point that is down on the window, and the points that were released + // in this event. The array is valid only during the callback + size_t num_points; + const GLFWTouchPoint *points; +} GLFWTouchEvent; + /*! @defgroup joysticks Joysticks * @brief Joystick IDs. * @@ -1520,6 +1557,25 @@ typedef void (*GLFWcursorenterfun)(GLFWwindow *, int); */ typedef void (*GLFWscrollfun)(GLFWwindow *, const GLFWScrollEvent *); +/*! @brief The function pointer type for touch callbacks. + * + * This is the function pointer type for touch callbacks. A touch callback + * function has the following signature: + * @code + * void function_name(GLFWwindow* window, const GLFWTouchEvent* event) + * @endcode + * + * @param[in] window The window that received the event. + * @param[in] event The touch event. A sequence opens with @ref + * GLFW_TOUCH_BEGIN and closes with either @ref GLFW_TOUCH_END or @ref + * GLFW_TOUCH_CANCEL. + * + * @sa @ref glfwSetTouchCallback + * + * @ingroup input + */ +typedef void (*GLFWtouchfun)(GLFWwindow *, const GLFWTouchEvent *); + /*! @brief The function pointer type for key callbacks. * * This is the function pointer type for key callbacks. A keyboard @@ -2323,6 +2379,10 @@ typedef GLFWscrollfun (*glfwSetScrollCallback_func)(GLFWwindow*, GLFWscrollfun); GFW_EXTERN glfwSetScrollCallback_func glfwSetScrollCallback_impl; #define glfwSetScrollCallback glfwSetScrollCallback_impl +typedef GLFWtouchfun (*glfwSetTouchCallback_func)(GLFWwindow*, GLFWtouchfun); +GFW_EXTERN glfwSetTouchCallback_func glfwSetTouchCallback_impl; +#define glfwSetTouchCallback glfwSetTouchCallback_impl + typedef GLFWliveresizefun (*glfwSetLiveResizeCallback_func)(GLFWwindow*, GLFWliveresizefun); GFW_EXTERN glfwSetLiveResizeCallback_func glfwSetLiveResizeCallback_impl; #define glfwSetLiveResizeCallback glfwSetLiveResizeCallback_impl