Cleanup previous PR

Improve the touch event API and expose the momentum scroller

- Add a timestamp to GLFWTouchEvent, converted from the wl_touch event
  time to the monotonic() clock
- Cancel momentum scrolling when a finger touches the screen and add
  glfwFeedMomentumScroller() and glfwCancelMomentumScroll() so that
  applications can drive touch scrolling through the momentum scroller
- Report every touch point as pressed before it is reported as
  released, so GLFW_TOUCH_BEGIN never carries a released point
- Replace the as_state sentinel with an explicit report mode
- Update pointer_serial from touch so the primary selection can be set
  after a touch selection
- Document the guarantees of the touch API
This commit is contained in:
Kovid Goyal 2026-10-02 11:19:05 +05:30
parent 8da7732ca6
commit 8d7c77038f
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
7 changed files with 191 additions and 35 deletions

View file

@ -113,6 +113,8 @@ def generate_wrappers(glfw_header: str) -> None:
pid_t glfwWaylandCompositorPID(void)
double glfwGetWaylandCurrentMonitorFractionalScale(void)
void glfwConfigureMomentumScroller(double friction, double min_velocity, double max_velocity, unsigned timer_interval)
void glfwFeedMomentumScroller(GLFWwindow *window, double dx, double dy, int keyboard_modifiers, bool stopped, monotonic_t timestamp)
void glfwCancelMomentumScroll(void)
unsigned long long glfwDBusUserNotify(const GLFWDBUSNotificationData *n, GLFWDBusnotificationcreatedfun callback, void *data)
void glfwDBusSetUserNotificationHandler(GLFWDBusnotificationactivatedfun handler)
int glfwSetX11LaunchCommand(GLFWwindow *handle, char **argv, int argc)

23
glfw/glfw3.h vendored
View file

@ -566,6 +566,11 @@ typedef struct GLFWScrollEvent {
int keyboard_modifiers;
} GLFWScrollEvent;
// Touch events are reported only for direct touch devices, i.e. touchscreens,
// where a touch has a position in the window. Indirect devices such as
// touchpads are reported as pointer and scroll events instead. While touch
// events are delivered for a window, the system does not also deliver
// pointer events emulated from the same touches.
typedef enum GLFWTouchEventType {
// The first finger of a sequence touched the window
GLFW_TOUCH_BEGIN = 1,
@ -578,6 +583,11 @@ typedef enum GLFWTouchEventType {
GLFW_TOUCH_CANCEL = 4,
} GLFWTouchEventType;
// Every point is reported as GLFW_TOUCH_POINT_PRESSED in exactly one event and
// then, unless the sequence is canceled, as GLFW_TOUCH_POINT_RELEASED in a
// later event. A point that is pressed and released at the same time is
// reported in two events, one after the other. So a GLFW_TOUCH_BEGIN event
// never contains a released point.
typedef enum GLFWTouchPointState {
GLFW_TOUCH_POINT_PRESSED = 1,
GLFW_TOUCH_POINT_MOVED = 2,
@ -597,8 +607,13 @@ typedef struct GLFWTouchPoint {
typedef struct GLFWTouchEvent {
GLFWTouchEventType type;
int keyboard_modifiers;
// When the system generated the event, on the same clock as monotonic(). Events
// can be delivered in bursts, so use this rather than the time of delivery to
// measure taps, long presses and the velocity of flings
monotonic_t timestamp;
// 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
// in this event. At most 16 points are tracked, further simultaneous points
// are ignored. The array is valid only during the callback
size_t num_points;
const GLFWTouchPoint *points;
} GLFWTouchEvent;
@ -5058,6 +5073,12 @@ GLFWAPI GLFWscrollfun glfwSetScrollCallback(GLFWwindow *window, GLFWscrollfun ca
* A touch point belongs to the window it first touched and is reported to
* that window alone until it is released.
*
* Only direct touch devices (touchscreens) are reported. Touchpads, including
* the macOS trackpad, are reported as pointer and scroll events. When a finger
* touches the screen, any momentum scroll in progress is canceled. To scroll
* in response to touches with the same momentum as touchpad scrolling, feed
* the finger movement to glfwFeedMomentumScroller().
*
* Only the Wayland backend reports touch events at the moment.
*
* @param[in] window The window whose callback to set.

View file

@ -200,9 +200,8 @@ glfw_is_momentum_scroll_enabled(void) {
return s.friction > 0;
}
void
glfw_handle_scroll_event_for_momentum(_GLFWwindow *w, const GLFWScrollEvent *ev, bool stopped, bool is_finger_based) {
const bool is_synthetic_momentum_start_event = stopped && momentum_scroll_gesture_detection_timeout_ms;
static void
handle_scroll_event(_GLFWwindow *w, const GLFWScrollEvent *ev, bool stopped, bool is_finger_based, monotonic_t now, bool is_synthetic_momentum_start_event) {
if (!w) {
cancel_existing_scroll(true);
return;
@ -211,8 +210,6 @@ glfw_handle_scroll_event_for_momentum(_GLFWwindow *w, const GLFWScrollEvent *ev,
if (ev->x_offset != 0 || ev->y_offset != 0) _glfwInputScroll(w, ev);
return;
}
monotonic_t now = monotonic();
if (is_synthetic_momentum_start_event) now -= ms_to_monotonic_t(momentum_scroll_gesture_detection_timeout_ms);
if (s.state == PHYSICAL_EVENT_IN_PROGRESS) {
s.physical_event.displacement.x += ev->unscaled.x;
s.physical_event.displacement.y += ev->unscaled.y;
@ -246,3 +243,41 @@ glfw_handle_scroll_event_for_momentum(_GLFWwindow *w, const GLFWScrollEvent *ev,
if (s.state == MOMENTUM_IN_PROGRESS) start_momentum_scroll(now);
else _glfwInputScroll(w, ev);
}
void
glfw_handle_scroll_event_for_momentum(_GLFWwindow *w, const GLFWScrollEvent *ev, bool stopped, bool is_finger_based) {
const bool is_synthetic_momentum_start_event = stopped && momentum_scroll_gesture_detection_timeout_ms;
monotonic_t now = monotonic();
if (is_synthetic_momentum_start_event) now -= ms_to_monotonic_t(momentum_scroll_gesture_detection_timeout_ms);
handle_scroll_event(w, ev, stopped, is_finger_based, now, is_synthetic_momentum_start_event);
}
// Scrolls window by a finger driven movement, such as a drag on a touchscreen,
// through the same momentum scroller that touchpad scrolling uses. dx and dy are
// in logical pixels, with the same sign as GLFWScrollEvent offsets, which is the
// direction the finger moved, so a finger displacement can be passed as is. Pass
// stopped=true when the finger lifts, the scroll then coasts if the finger was
// moving fast enough. timestamp is when the movement happened, on the clock of
// monotonic(), or zero for now. If momentum scrolling is disabled, this sends
// plain high resolution scroll events.
GLFWAPI void
glfwFeedMomentumScroller(GLFWwindow *handle, double dx, double dy, int keyboard_modifiers, bool stopped, monotonic_t timestamp) {
_GLFWwindow *w = (_GLFWwindow *)handle;
if (!w) return;
float xscale = 1, yscale = 1;
_glfwPlatformGetWindowContentScale(w, &xscale, &yscale);
const GLFWScrollEvent ev = {
.offset_type = GLFW_SCROLL_OFFEST_HIGHRES,
.unscaled = {.x = dx, .y = dy},
.x_offset = dx * (xscale > 0 ? xscale : 1),
.y_offset = dy * (yscale > 0 ? yscale : 1),
.keyboard_modifiers = keyboard_modifiers,
};
handle_scroll_event(w, &ev, stopped, true, timestamp > 0 ? timestamp : monotonic(), false);
}
// Stops any momentum scroll that is coasting
GLFWAPI void
glfwCancelMomentumScroll(void) {
glfw_cancel_momentum_scroll();
}

122
glfw/wl_init.c vendored
View file

@ -37,6 +37,7 @@
#include "wayland-pointer-gestures-unstable-v1-client-protocol.h"
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
@ -322,26 +323,76 @@ touch_point_index(int32_t id) {
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) {
touch_record_time(uint32_t time) {
touch.last_event_time = time;
touch.has_event_time = true;
}
// Converts the time of the last wl_touch event to monotonic() time. The base of
// Wayland event times is unspecified and they wrap, so only the age of the event
// is used, which is measured against CLOCK_MONOTONIC, the clock compositors use
static monotonic_t
touch_event_timestamp(void) {
const monotonic_t now = monotonic();
if (!touch.has_event_time) return now;
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) return now;
const uint32_t now_ms = (uint32_t)((uint64_t)ts.tv_sec * 1000u + (uint64_t)ts.tv_nsec / 1000000u);
const uint32_t age_ms = now_ms - touch.last_event_time;
// An implausible age means the compositor uses some other clock
if (age_ms > 1000) return now;
return now - ms_to_monotonic_t(age_ms);
}
static bool
touch_point_is_tap(const _GLFWWaylandTouchPoint *p) {
return p->pressed_in_frame && p->state == GLFW_TOUCH_POINT_RELEASED;
}
typedef enum TouchReportMode {
// Every point in its own state
TOUCH_REPORT_OWN_STATE,
// The points that were pressed and released in this frame as pressed, the
// rest in their own state
TOUCH_REPORT_TAPS_AS_PRESSED,
// The points that were pressed and released in this frame as released, the
// rest as stationary, as they were reported by TOUCH_REPORT_TAPS_AS_PRESSED
TOUCH_REPORT_TAPS_AS_RELEASED,
// Only the points the window has heard of, all as released
TOUCH_REPORT_CANCELED,
} TouchReportMode;
// Sends one window the points it owns, in the given event type, with their
// states decided by mode
static void
touch_send_to_window(GLFWid window_id, GLFWTouchEventType type, TouchReportMode mode, monotonic_t timestamp) {
_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,
};
const _GLFWWaylandTouchPoint *p = touch.points + i;
if (p->window_id != window_id) continue;
GLFWTouchPointState state = p->state;
switch (mode) {
case TOUCH_REPORT_OWN_STATE: break;
case TOUCH_REPORT_TAPS_AS_PRESSED:
if (touch_point_is_tap(p)) state = GLFW_TOUCH_POINT_PRESSED;
break;
case TOUCH_REPORT_TAPS_AS_RELEASED:
if (!touch_point_is_tap(p)) state = GLFW_TOUCH_POINT_STATIONARY;
break;
case TOUCH_REPORT_CANCELED:
if (p->pressed_in_frame) continue;
state = GLFW_TOUCH_POINT_RELEASED;
break;
}
points[n++] = (GLFWTouchPoint){.id = p->id, .state = state, .x = p->x, .y = p->y};
}
if (!n) return;
GLFWTouchEvent ev = {.type = type, .keyboard_modifiers = _glfw.wl.xkb.states.modifiers, .num_points = n, .points = points};
GLFWTouchEvent ev = {
.type = type, .keyboard_modifiers = _glfw.wl.xkb.states.modifiers, .timestamp = timestamp, .num_points = n, .points = points};
_glfwInputTouch(window, &ev);
}
@ -350,17 +401,23 @@ touchHandleDown(
void *data UNUSED,
struct wl_touch *wl_touch UNUSED,
uint32_t serial,
uint32_t time UNUSED,
uint32_t time,
struct wl_surface *surface,
int32_t id,
wl_fixed_t sx,
wl_fixed_t sy) {
// A finger landing stops any scroll that is coasting, as on every touch system
glfw_cancel_momentum_scroll();
_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;
// Requests such as setting the primary selection need the serial of the
// latest input event, which can come from touch as well as the pointer
_glfw.wl.pointer_serial = serial;
touch_record_time(time);
touch.points[touch.count++] = (_GLFWWaylandTouchPoint){
.id = id,
.window_id = window->id,
@ -372,49 +429,57 @@ touchHandleDown(
}
static void
touchHandleUp(void *data UNUSED, struct wl_touch *wl_touch UNUSED, uint32_t serial, uint32_t time UNUSED, int32_t id) {
touchHandleUp(void *data UNUSED, struct wl_touch *wl_touch UNUSED, uint32_t serial, uint32_t time, int32_t id) {
const ssize_t i = touch_point_index(id);
if (i < 0) return;
_glfw.wl.serial = serial;
_glfw.wl.input_serial = serial;
_glfw.wl.pointer_serial = serial;
touch_record_time(time);
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) {
touchHandleMotion(void *data UNUSED, struct wl_touch *wl_touch UNUSED, uint32_t time, int32_t id, wl_fixed_t sx, wl_fixed_t sy) {
const ssize_t i = touch_point_index(id);
if (i < 0) return;
touch_record_time(time);
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
// changed point gets one event holding all of its points. A point that was pressed
// and released within the frame is first reported as pressed in an event of its
// own, so that every point is reported pressed before it is reported released
static void
touchHandleFrame(void *data UNUSED, struct wl_touch *wl_touch UNUSED) {
const monotonic_t timestamp = touch_event_timestamp();
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;
bool changed = false, seen = false, has_taps = 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++;
const _GLFWWaylandTouchPoint *p = touch.points + j;
if (p->window_id != window_id) continue;
if (p->state != GLFW_TOUCH_POINT_STATIONARY) changed = true;
if (touch_point_is_tap(p)) has_taps = true;
if (!p->pressed_in_frame) down_before++;
if (p->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);
const GLFWTouchEventType opening = down_before ? GLFW_TOUCH_UPDATE : GLFW_TOUCH_BEGIN;
const GLFWTouchEventType closing = down_after ? GLFW_TOUCH_UPDATE : GLFW_TOUCH_END;
if (has_taps) {
touch_send_to_window(window_id, opening, TOUCH_REPORT_TAPS_AS_PRESSED, timestamp);
touch_send_to_window(window_id, closing, TOUCH_REPORT_TAPS_AS_RELEASED, timestamp);
} else touch_send_to_window(window_id, !down_before ? GLFW_TOUCH_BEGIN : closing, TOUCH_REPORT_OWN_STATE, timestamp);
}
size_t kept = 0;
for (size_t i = 0; i < touch.count; i++) {
@ -431,6 +496,7 @@ touchHandleFrame(void *data UNUSED, struct wl_touch *wl_touch UNUSED) {
// window hears of it only if it was told the sequence began
static void
touchHandleCancel(void *data UNUSED, struct wl_touch *wl_touch UNUSED) {
const monotonic_t timestamp = monotonic();
GLFWid done[arraysz(touch.points)];
size_t num_done = 0;
for (size_t i = 0; i < touch.count; i++) {
@ -439,7 +505,7 @@ touchHandleCancel(void *data UNUSED, struct wl_touch *wl_touch UNUSED) {
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_send_to_window(window_id, GLFW_TOUCH_CANCEL, TOUCH_REPORT_CANCELED, timestamp);
}
touch.count = 0;
}

3
glfw/wl_platform.h vendored
View file

@ -357,6 +357,9 @@ typedef struct _GLFWlibraryWayland {
struct {
_GLFWWaylandTouchPoint points[16];
size_t count;
// The time of the latest wl_touch event, in milliseconds of the compositor clock
uint32_t last_event_time;
bool has_event_time;
} touch_state;
struct wl_data_device_manager *dataDeviceManager;
struct wl_data_device *dataDevice;

6
kitty/glfw-wrapper.c generated
View file

@ -563,6 +563,12 @@ load_glfw(const char* path) {
*(void **) (&glfwConfigureMomentumScroller_impl) = dlsym(handle, "glfwConfigureMomentumScroller");
if (glfwConfigureMomentumScroller_impl == NULL) dlerror(); // clear error indicator
*(void **) (&glfwFeedMomentumScroller_impl) = dlsym(handle, "glfwFeedMomentumScroller");
if (glfwFeedMomentumScroller_impl == NULL) dlerror(); // clear error indicator
*(void **) (&glfwCancelMomentumScroll_impl) = dlsym(handle, "glfwCancelMomentumScroll");
if (glfwCancelMomentumScroll_impl == NULL) dlerror(); // clear error indicator
*(void **) (&glfwDBusUserNotify_impl) = dlsym(handle, "glfwDBusUserNotify");
if (glfwDBusUserNotify_impl == NULL) dlerror(); // clear error indicator

25
kitty/glfw-wrapper.h generated
View file

@ -306,6 +306,11 @@ typedef struct GLFWScrollEvent {
int keyboard_modifiers;
} GLFWScrollEvent;
// Touch events are reported only for direct touch devices, i.e. touchscreens,
// where a touch has a position in the window. Indirect devices such as
// touchpads are reported as pointer and scroll events instead. While touch
// events are delivered for a window, the system does not also deliver
// pointer events emulated from the same touches.
typedef enum GLFWTouchEventType {
// The first finger of a sequence touched the window
GLFW_TOUCH_BEGIN = 1,
@ -318,6 +323,11 @@ typedef enum GLFWTouchEventType {
GLFW_TOUCH_CANCEL = 4,
} GLFWTouchEventType;
// Every point is reported as GLFW_TOUCH_POINT_PRESSED in exactly one event and
// then, unless the sequence is canceled, as GLFW_TOUCH_POINT_RELEASED in a
// later event. A point that is pressed and released at the same time is
// reported in two events, one after the other. So a GLFW_TOUCH_BEGIN event
// never contains a released point.
typedef enum GLFWTouchPointState {
GLFW_TOUCH_POINT_PRESSED = 1,
GLFW_TOUCH_POINT_MOVED = 2,
@ -337,8 +347,13 @@ typedef struct GLFWTouchPoint {
typedef struct GLFWTouchEvent {
GLFWTouchEventType type;
int keyboard_modifiers;
// When the system generated the event, on the same clock as monotonic(). Events
// can be delivered in bursts, so use this rather than the time of delivery to
// measure taps, long presses and the velocity of flings
monotonic_t timestamp;
// 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
// in this event. At most 16 points are tracked, further simultaneous points
// are ignored. The array is valid only during the callback
size_t num_points;
const GLFWTouchPoint *points;
} GLFWTouchEvent;
@ -2647,6 +2662,14 @@ typedef void (*glfwConfigureMomentumScroller_func)(double, double, double, unsig
GFW_EXTERN glfwConfigureMomentumScroller_func glfwConfigureMomentumScroller_impl;
#define glfwConfigureMomentumScroller glfwConfigureMomentumScroller_impl
typedef void (*glfwFeedMomentumScroller_func)(GLFWwindow*, double, double, int, bool, monotonic_t);
GFW_EXTERN glfwFeedMomentumScroller_func glfwFeedMomentumScroller_impl;
#define glfwFeedMomentumScroller glfwFeedMomentumScroller_impl
typedef void (*glfwCancelMomentumScroll_func)(void);
GFW_EXTERN glfwCancelMomentumScroll_func glfwCancelMomentumScroll_impl;
#define glfwCancelMomentumScroll glfwCancelMomentumScroll_impl
typedef unsigned long long (*glfwDBusUserNotify_func)(const GLFWDBUSNotificationData*, GLFWDBusnotificationcreatedfun, void*);
GFW_EXTERN glfwDBusUserNotify_func glfwDBusUserNotify_impl;
#define glfwDBusUserNotify glfwDBusUserNotify_impl