]> git.mdlowis.com Git - proto/labwc.git/commitdiff
touch: store initial coordinate adjustments
authorbi4k8 <bi4k8@github>
Sat, 26 Aug 2023 17:01:42 +0000 (17:01 +0000)
committerConsolatis <35009135+Consolatis@users.noreply.github.com>
Sun, 27 Aug 2023 21:13:24 +0000 (23:13 +0200)
this avoids touch offsets jumping when a touch point moves off of a surface

include/labwc.h
src/seat.c
src/touch.c

index 71ec2ca5250edb87dfa75685c3bf695313f21f46..d83d8a00145842481317ad03f43a08b489652cac 100644 (file)
@@ -102,6 +102,8 @@ struct seat {
        struct server *server;
        struct wlr_keyboard_group *keyboard_group;
 
+       struct wl_list touch_points; /* struct touch_point.link */
+
        /*
         * Enum of most recent server-side cursor image.  Set by
         * cursor_set().  Cleared when a client surface is entered
index ce30886738efae12d8b01947bca3fef32b7ba5eb..4af628cc8a4914a6abf81c9454a491d8f0372dcb 100644 (file)
@@ -329,6 +329,7 @@ seat_init(struct server *server)
                exit(EXIT_FAILURE);
        }
 
+       wl_list_init(&seat->touch_points);
        wl_list_init(&seat->constraint_commit.link);
        wl_list_init(&seat->inputs);
        seat->new_input.notify = new_input_notify;
index d87e02f026a7d958f81fa8e522907dff40c67dbf..3fbc4f31c09c2936e443479656d306fd777e1946 100644 (file)
@@ -1,22 +1,36 @@
 // SPDX-License-Identifier: GPL-2.0-only
+#include <wayland-util.h>
 #include <wlr/types/wlr_touch.h>
 #include "idle.h"
 #include "labwc.h"
+#include "common/mem.h"
 #include "common/scene-helpers.h"
 
+/* Holds layout -> surface offsets to report motion events in relative coords */
+struct touch_point {
+       int32_t touch_id;
+       uint32_t x_offset;
+       uint32_t y_offset;
+       struct wl_list link; /* seat::touch_points */
+};
+
 static struct wlr_surface*
 touch_get_coords(struct seat *seat, struct wlr_touch *touch, double x, double y,
-               double *sx, double *sy)
+               double *x_offset, double *y_offset)
 {
        /* Convert coordinates: first [0, 1] => layout, then layout => surface */
        double lx, ly;
        wlr_cursor_absolute_to_layout_coords(seat->cursor, &touch->base,
                x, y, &lx, &ly);
 
+       double sx, sy;
        struct wlr_scene_node *node =
-               wlr_scene_node_at(&seat->server->scene->tree.node, lx, ly, sx, sy);
+               wlr_scene_node_at(&seat->server->scene->tree.node, lx, ly, &sx, &sy);
 
-       /* Find the surface and return it if it accepts touch events. */
+       *x_offset = lx - sx;
+       *y_offset = ly - sy;
+
+       /* Find the surface and return it if it accepts touch events */
        struct wlr_surface *surface = lab_wlr_surface_from_node(node);
 
        if (surface && !wlr_surface_accepts_touch(seat->seat, surface)) {
@@ -32,10 +46,22 @@ touch_motion(struct wl_listener *listener, void *data)
        struct wlr_touch_motion_event *event = data;
        idle_manager_notify_activity(seat->seat);
 
-       double sx, sy;
-       if (touch_get_coords(seat, event->touch, event->x, event->y, &sx, &sy)) {
-               wlr_seat_touch_notify_motion(seat->seat, event->time_msec,
-                       event->touch_id, sx, sy);
+       /* Convert coordinates: first [0, 1] => layout, then apply offsets */
+       double lx, ly;
+       wlr_cursor_absolute_to_layout_coords(seat->cursor, &event->touch->base,
+               event->x, event->y, &lx, &ly);
+
+       /* Find existing touch point to determine initial offsets to subtract */
+       struct touch_point *touch_point;
+       wl_list_for_each(touch_point, &seat->touch_points, link) {
+               if (touch_point->touch_id == event->touch_id) {
+                       double sx = lx - touch_point->x_offset;
+                       double sy = ly - touch_point->y_offset;
+
+                       wlr_seat_touch_notify_motion(seat->seat, event->time_msec,
+                               event->touch_id, sx, sy);
+                       return;
+               }
        }
 }
 
@@ -53,9 +79,26 @@ touch_down(struct wl_listener *listener, void *data)
        struct seat *seat = wl_container_of(listener, seat, touch_down);
        struct wlr_touch_down_event *event = data;
 
-       double sx, sy;
+       /* Compute layout => surface offset and save for this touch point */
+       double x_offset, y_offset;
        struct wlr_surface *surface = touch_get_coords(seat, event->touch,
-                       event->x, event->y, &sx, &sy);
+                       event->x, event->y, &x_offset, &y_offset);
+
+       struct touch_point *touch_point = znew(*touch_point);
+       touch_point->touch_id = event->touch_id;
+       touch_point->x_offset = x_offset;
+       touch_point->y_offset = y_offset;
+
+       wl_list_insert(&seat->touch_points, &touch_point->link);
+
+       double lx, ly;
+       wlr_cursor_absolute_to_layout_coords(seat->cursor, &event->touch->base,
+               event->x, event->y, &lx, &ly);
+
+       /* Apply offsets to get surface coords before reporting event */
+       double sx = lx - x_offset;
+       double sy = ly - y_offset;
+
        if (surface) {
                wlr_seat_touch_notify_down(seat->seat, surface,
                        event->time_msec, event->touch_id, sx, sy);
@@ -68,6 +111,16 @@ touch_up(struct wl_listener *listener, void *data)
        struct seat *seat = wl_container_of(listener, seat, touch_up);
        struct wlr_touch_up_event *event = data;
 
+       /* Remove the touch point from the seat */
+       struct touch_point *touch_point, *tmp;
+       wl_list_for_each_safe(touch_point, tmp, &seat->touch_points, link) {
+               if (touch_point->touch_id == event->touch_id) {
+                       wl_list_remove(&touch_point->link);
+                       zfree(touch_point);
+                       break;
+               }
+       }
+
        wlr_seat_touch_notify_up(seat->seat, event->time_msec, event->touch_id);
 }