Skip to content

Commit d614d01

Browse files
committed
sway/input/cursor: implement pointer-warp-v1
Ref: https://gitlab.freedesktop.org/wlroots/wlroots/-/merge_requests/5367
1 parent 512d64b commit d614d01

4 files changed

Lines changed: 56 additions & 0 deletions

File tree

include/sway/input/cursor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,5 @@ const char *get_mouse_button_name(uint32_t button);
147147

148148
void handle_request_set_cursor_shape(struct wl_listener *listener, void *data);
149149

150+
void handle_pointer_warp(struct wl_listener *listener, void *data);
150151
#endif

include/sway/server.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ struct sway_server {
8181
struct wlr_pointer_constraints_v1 *pointer_constraints;
8282
struct wl_listener pointer_constraint;
8383

84+
struct wlr_pointer_warp_v1 *pointer_warp_v1;
85+
struct wl_listener pointer_warp;
86+
8487
struct wlr_xdg_output_manager_v1 *xdg_output_manager_v1;
8588

8689
struct wlr_output_manager_v1 *output_manager_v1;

sway/input/cursor.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <wlr/types/wlr_cursor_shape_v1.h>
1010
#include <wlr/types/wlr_pointer.h>
1111
#include <wlr/types/wlr_relative_pointer_v1.h>
12+
#include <wlr/types/wlr_pointer_warp_v1.h>
1213
#include <wlr/types/wlr_touch.h>
1314
#include <wlr/types/wlr_tablet_v2.h>
1415
#include <wlr/types/wlr_tablet_pad.h>
@@ -1415,3 +1416,43 @@ void handle_request_set_cursor_shape(struct wl_listener *listener, void *data) {
14151416

14161417
cursor_set_image(seat->cursor, wlr_cursor_shape_v1_name(event->shape), focused_client);
14171418
}
1419+
1420+
void handle_pointer_warp(struct wl_listener *listener, void *data) {
1421+
const struct wlr_pointer_warp_v1_event_warp *event = data;
1422+
struct sway_seat *seat = event->seat_client->seat->data;
1423+
struct sway_cursor *cursor = seat->cursor;
1424+
1425+
struct wl_client *focused_client = NULL;
1426+
struct wlr_surface *focused_surface =
1427+
seat->wlr_seat->pointer_state.focused_surface;
1428+
if (focused_surface != NULL) {
1429+
focused_client = wl_resource_get_client(focused_surface->resource);
1430+
}
1431+
1432+
if (focused_client == NULL || event->seat_client->client != focused_client) {
1433+
sway_log(SWAY_DEBUG, "denying request to warp cursor from unfocused client");
1434+
return;
1435+
}
1436+
1437+
struct wlr_box surface_box = {
1438+
.width = event->surface->current.width,
1439+
.height = event->surface->current.height,
1440+
};
1441+
1442+
if (!wlr_box_contains_point(&surface_box, event->x, event->y)) {
1443+
sway_log(SWAY_DEBUG, "denying request to warp cursor out of surface");
1444+
return;
1445+
}
1446+
1447+
struct sway_view *view = view_from_wlr_surface(event->surface);
1448+
if (view == NULL) {
1449+
sway_log(SWAY_DEBUG, "unsupported surface for cursor warp");
1450+
return;
1451+
}
1452+
1453+
struct sway_container *con = view->container;
1454+
double lx = event->x + con->pending.content_x - view->geometry.x;
1455+
double ly = event->y + con->pending.content_y - view->geometry.y;
1456+
wlr_cursor_warp(cursor->cursor, NULL, lx, ly);
1457+
cursor_rebase(cursor);
1458+
}

sway/server.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <wlr/types/wlr_output_management_v1.h>
3434
#include <wlr/types/wlr_output_power_management_v1.h>
3535
#include <wlr/types/wlr_pointer_constraints_v1.h>
36+
#include <wlr/types/wlr_pointer_warp_v1.h>
3637
#include <wlr/types/wlr_presentation_time.h>
3738
#include <wlr/types/wlr_primary_selection_v1.h>
3839
#include <wlr/types/wlr_relative_pointer_v1.h>
@@ -79,6 +80,7 @@
7980
#define SWAY_FOREIGN_TOPLEVEL_LIST_VERSION 1
8081
#define SWAY_PRESENTATION_VERSION 2
8182
#define SWAY_XDG_DECORATION_VERSION 2
83+
#define SWAY_POINTER_WARP_VERSION 1
8284

8385
bool unsupported_gpu_detected = false;
8486

@@ -426,6 +428,14 @@ bool server_init(struct sway_server *server) {
426428
wl_signal_add(&server->pointer_constraints->events.new_constraint,
427429
&server->pointer_constraint);
428430

431+
server->pointer_warp_v1 = wlr_pointer_warp_v1_create(server->wl_display, SWAY_POINTER_WARP_VERSION);
432+
if (!server->pointer_warp_v1) {
433+
sway_log(SWAY_ERROR, "Failed to create pointer warp");
434+
return false;
435+
}
436+
server->pointer_warp.notify = handle_pointer_warp;
437+
wl_signal_add(&server->pointer_warp_v1->events.warp, &server->pointer_warp);
438+
429439
if (!wlr_presentation_create(server->wl_display, server->backend, SWAY_PRESENTATION_VERSION)) {
430440
sway_log(SWAY_ERROR, "Failed to create presentation");
431441
return false;
@@ -724,6 +734,7 @@ void server_fini(struct sway_server *server) {
724734
wl_list_remove(&server->server_decoration.link);
725735
wl_list_remove(&server->xdg_decoration.link);
726736
wl_list_remove(&server->pointer_constraint.link);
737+
wl_list_remove(&server->pointer_warp.link);
727738
wl_list_remove(&server->output_manager_apply.link);
728739
wl_list_remove(&server->output_manager_test.link);
729740
wl_list_remove(&server->output_power_manager_set_mode.link);

0 commit comments

Comments
 (0)