diff --git a/backend/libinput/pointer.c b/backend/libinput/pointer.c index fb85cddd5..b9cf1dfe4 100644 --- a/backend/libinput/pointer.c +++ b/backend/libinput/pointer.c @@ -36,6 +36,8 @@ void handle_pointer_motion(struct libinput_event *event, usec_to_msec(libinput_event_pointer_get_time_usec(pevent)); wlr_event.delta_x = libinput_event_pointer_get_dx(pevent); wlr_event.delta_y = libinput_event_pointer_get_dy(pevent); + wlr_event.unaccel_dx = libinput_event_pointer_get_dx_unaccelerated(pevent); + wlr_event.unaccel_dy = libinput_event_pointer_get_dy_unaccelerated(pevent); wlr_signal_emit_safe(&wlr_dev->pointer->events.motion, &wlr_event); } diff --git a/examples/meson.build b/examples/meson.build index 589a73262..0811d1a2c 100644 --- a/examples/meson.build +++ b/examples/meson.build @@ -81,6 +81,10 @@ examples = { 'src': 'pointer-constraints.c', 'dep': [wayland_client, wlr_protos, wlroots], }, + 'relative-pointer': { + 'src': 'relative-pointer-unstable-v1.c', + 'dep': [wayland_client, wlr_protos, wlroots], + }, 'dmabuf-capture': { 'src': 'dmabuf-capture.c', 'dep': [ diff --git a/examples/relative-pointer-unstable-v1.c b/examples/relative-pointer-unstable-v1.c new file mode 100644 index 000000000..ce671f995 --- /dev/null +++ b/examples/relative-pointer-unstable-v1.c @@ -0,0 +1,493 @@ +#include +#include +#include +#include +#include +#include +#include +#include "pointer-constraints-unstable-v1-client-protocol.h" +#include "relative-pointer-unstable-v1-client-protocol.h" +#include "xdg-shell-client-protocol.h" + + +/** + * structs storing global state + * + * These are passed around as user data in the wayland globals. + */ + +struct egl_info { + struct wlr_egl *egl; + struct wl_egl_window *egl_window; + struct wlr_egl_surface *egl_surface; + uint32_t width; + uint32_t height; + struct wl_surface *surface; + struct wl_callback *frame_callback; +}; + +struct window { + struct egl_info *egl_info; + struct wl_pointer *pointer; + struct zwp_locked_pointer_v1 *locked_pointer; + struct zwp_relative_pointer_v1 *relative_pointer; + int32_t pointer_x, pointer_y; + uint32_t last_draw; +}; + + +/** + * surface handling and helpers + * + * draw_cursor and draw_relative_cursor draw a small 5px by 5px box around the + * cursor and relative motion coordinates respectively. draw_background colors + * the background black. + * + * The functions are somewhat duplicated, but it doesn't really matter. + */ + +static void surface_callback_handle_done(void *data, + struct wl_callback *callback, uint32_t time) { + wl_callback_destroy(callback); + struct egl_info *e = data; + e->frame_callback = NULL; +} + +static struct wl_callback_listener surface_callback_listener = { + .done = surface_callback_handle_done, +}; + +static void draw_init(struct egl_info *e) { + eglMakeCurrent(e->egl->display, e->egl_surface, + e->egl_surface, e->egl->context); + glViewport(0, 0, e->width, e->height); +} + +static void draw_cursor(struct egl_info *e, int32_t x, int32_t y) { + glEnable(GL_SCISSOR_TEST); + glScissor(x, e->height - y, 5, 5); + glClearColor(1.0f, 1.0f, 1.0f, 1.0f); /* white */ + glClear(GL_COLOR_BUFFER_BIT); + glDisable(GL_SCISSOR_TEST); +} + +static void draw_relative_cursor(struct egl_info *e, int32_t x, int32_t y) { + glEnable(GL_SCISSOR_TEST); + glScissor(x, e->height - y, 5, 5); + glClearColor(1.0f, 0.0f, 0.0f, 1.0f); /* red */ + glClear(GL_COLOR_BUFFER_BIT); + glDisable(GL_SCISSOR_TEST); +} + +static void draw_background(struct egl_info *e) { + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); /* black */ + glClear(GL_COLOR_BUFFER_BIT); +} + +static void draw_end(struct egl_info *e) { + e->frame_callback = wl_surface_frame(e->surface); + wl_callback_add_listener(e->frame_callback, &surface_callback_listener, e); + + eglSwapBuffers(e->egl->display, e->egl_surface); +} + + +/** + * registry and globals handling + */ + +static struct wl_compositor *compositor = NULL; +static struct wl_seat *seat = NULL; +static struct xdg_wm_base *wm_base = NULL; +static struct zwp_pointer_constraints_v1 *pointer_constraints = NULL; +static struct zwp_relative_pointer_manager_v1 *relative_pointer_manager = NULL; + +static void registry_handle_global(void *data, struct wl_registry *registry, + uint32_t name, const char *interface, uint32_t version) { + if (strcmp(interface, wl_compositor_interface.name) == 0) { + compositor = wl_registry_bind(registry, name, + &wl_compositor_interface, version); + } else if (strcmp(interface, xdg_wm_base_interface.name) == 0) { + wm_base = wl_registry_bind(registry, name, + &xdg_wm_base_interface, version); + } else if (strcmp(interface, wl_seat_interface.name) == 0) { + seat = wl_registry_bind(registry, name, + &wl_seat_interface, version); + } else if (strcmp(interface, zwp_pointer_constraints_v1_interface.name) == 0) { + pointer_constraints = wl_registry_bind(registry, name, + &zwp_pointer_constraints_v1_interface, version); + } else if (strcmp(interface, zwp_relative_pointer_manager_v1_interface.name) == 0) { + relative_pointer_manager = wl_registry_bind(registry, name, + &zwp_relative_pointer_manager_v1_interface, version); + } +} + +static void registry_handle_global_remove(void *data, struct wl_registry *registry, + uint32_t time) { + /* This space intentionally left blank */ +} + +static const struct wl_registry_listener registry_listener = { + .global = registry_handle_global, + .global_remove = registry_handle_global_remove, +}; + + +/** + * xdg_surface handling + */ + +static void xdg_surface_handle_configure(void *data, + struct xdg_surface *xdg_surface, uint32_t serial) { + struct egl_info *e = data; + xdg_surface_ack_configure(xdg_surface, serial); + wl_egl_window_resize(e->egl_window, e->width, e->height, 0, 0); + draw_init(e); + draw_background(e); + draw_end(e); +} + +static const struct xdg_surface_listener xdg_surface_listener = { + .configure = xdg_surface_handle_configure, +}; + + +/** + * xdg_toplevel handling + */ + +static void xdg_toplevel_handle_configure(void *data, + struct xdg_toplevel *xdg_toplevel, int32_t width, int32_t height, + struct wl_array *states) { + struct egl_info *e = data; + // TODO: Why do we get 0,0 on initialization here? (in rootston) + if (width == 0 && height == 0) { + return; + } + e->width = width; + e->height = height; +} + +static void xdg_toplevel_handle_close(void *data, + struct xdg_toplevel *xdg_toplevel) { + struct egl_info *e = data; + wlr_egl_finish(e->egl); + exit(EXIT_SUCCESS); +} + +static const struct xdg_toplevel_listener xdg_toplevel_listener = { + .configure = xdg_toplevel_handle_configure, + .close = xdg_toplevel_handle_close, +}; + + +/** + * zwp_locked_pointer handling + * + * Pointer unlocks need to be handled properly since the relative pointer needs + * to be released as well. Unlocks happen when the focus is changed, for + * example. + */ + +static void locked_pointer_handle_locked(void *data, + struct zwp_locked_pointer_v1 *zwp_locked_pointer_v1) { + /* This space intentionally left blank */ +} + +static void locked_pointer_handle_unlocked(void *data, + struct zwp_locked_pointer_v1 *zwp_locked_pointer_v1) { + struct window *w = data; + /* The locked pointer doesn't need to be destroyed since it was oneshot */ + w->locked_pointer = NULL; + if (w->relative_pointer) { + /* Destroy the relative pointer */ + zwp_relative_pointer_v1_destroy(w->relative_pointer); + w->relative_pointer = NULL; + } + draw_init(w->egl_info); + draw_background(w->egl_info); + draw_end(w->egl_info); +} + +static const struct zwp_locked_pointer_v1_listener locked_pointer_listener = { + .locked = locked_pointer_handle_locked, + .unlocked = locked_pointer_handle_unlocked, +}; + + +/** + * zwp_relative_pointer handling + * + * Handling relative_motion events is the meat of the code. The handler simply + * tries to indicate what the deltas look like. + */ + +static void relative_pointer_handle_relative_motion(void *data, + struct zwp_relative_pointer_v1 *zwp_relative_pointer_v1, + uint32_t utime_hi, uint32_t utime_lo, + wl_fixed_t dx, wl_fixed_t dy, + wl_fixed_t dx_unaccel, wl_fixed_t dy_unaccel) { + struct window *w = data; + + /** + * This renders the last location of the pointer (before it as locked), as + * well as what the position would have been after the given relative + * motion. Note that, if the device sends absolute motion events, the + * cursor location after relative motion is always identical to the actual + * cursor position. + */ + uint64_t utime = (((uint64_t) utime_hi << 32) + utime_lo) / 1000; + if (utime - w->last_draw > 30 && w->egl_info->frame_callback == NULL) { + w->last_draw = utime; + + struct egl_info *e = w->egl_info; + draw_init(e); + draw_background(e); + draw_cursor(e, w->pointer_x, w->pointer_y); + draw_relative_cursor(e, w->pointer_x + wl_fixed_to_int(dx), + w->pointer_y + wl_fixed_to_int(dy)); + draw_end(e); + } +} + +static const struct zwp_relative_pointer_v1_listener relative_pointer_listener = { + .relative_motion = relative_pointer_handle_relative_motion, +}; + + +/** + * wl_pointer handling + * + * The client toggles between locking the pointer and receiving relative motion + * events, and releasing the locked pointer and falling back to normal motion + * events, on a mouse button one (left mouse button) click. + * + * It additionally removes the cursor image, and indicates the pointer location + * via a small white box. + */ + +static void pointer_handle_button(void *data, struct wl_pointer *pointer, + uint32_t serial, uint32_t time, uint32_t button, uint32_t state_w) { + + struct window *w = data; + struct egl_info *e = w->egl_info; + + if (button == BTN_LEFT && state_w == WL_POINTER_BUTTON_STATE_PRESSED) { + if (w->locked_pointer == NULL && w->relative_pointer == NULL) { + /* Get a locked pointer and add listener */ + w->locked_pointer = zwp_pointer_constraints_v1_lock_pointer( + pointer_constraints, w->egl_info->surface, w->pointer, NULL, + ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT); + zwp_locked_pointer_v1_add_listener(w->locked_pointer, + &locked_pointer_listener, w); + + /* Get relative pointer and add listener */ + w->relative_pointer = zwp_relative_pointer_manager_v1_get_relative_pointer( + relative_pointer_manager, w->pointer); + zwp_relative_pointer_v1_add_listener(w->relative_pointer, + &relative_pointer_listener, w); + + /* Commit the surface and render */ + wl_surface_commit(e->surface); + + draw_init(e); + draw_background(e); + draw_cursor(e, w->pointer_x, w->pointer_y); + draw_end(e); + } else if (w->locked_pointer && w->relative_pointer) { + /* Destroy the locked pointer */ + zwp_locked_pointer_v1_destroy(w->locked_pointer); + w->locked_pointer = NULL; + + /* Destroy the relative pointer */ + zwp_relative_pointer_v1_destroy(w->relative_pointer); + w->relative_pointer = NULL; + + /* Render */ + draw_init(e); + draw_background(e); + draw_cursor(e, w->pointer_x, w->pointer_y); + draw_end(e); + } else { + fprintf(stderr, "Unknown state!\n"); + exit(EXIT_FAILURE); + } + } +} + +static void pointer_handle_enter(void *data, struct wl_pointer *wl_pointer, + uint32_t serial, struct wl_surface *surface, + wl_fixed_t surface_x, wl_fixed_t surface_y) { + struct window *w = data; + wl_pointer_set_cursor(w->pointer, serial, NULL, 0, 0); +} + +static void pointer_handle_leave(void *data, struct wl_pointer *wl_pointer, + uint32_t serial, struct wl_surface *surface) { + /* This space intentionally left blank */ +} + +static void pointer_handle_motion(void *data, struct wl_pointer *wl_pointer, + uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y) { + struct window *w = data; + struct egl_info *e = w->egl_info; + w->pointer_x = wl_fixed_to_int(surface_x); + w->pointer_y = wl_fixed_to_int(surface_y); + if (time - w->last_draw > 30 && e->frame_callback == NULL) { + w->last_draw = time; + draw_init(e); + draw_background(e); + draw_cursor(e, w->pointer_x, w->pointer_y); + draw_end(e); + } +} + +static void pointer_handle_axis(void *data, struct wl_pointer *wl_pointer, + uint32_t time, uint32_t axis, wl_fixed_t value) { + /* This space intentionally left blank */ +} + +static void pointer_handle_frame(void *data, struct wl_pointer *wl_pointer) { + /* This space intentionally left blank */ +} + +static void pointer_handle_axis_source(void *data, + struct wl_pointer *wl_pointer, uint32_t axis_source) { + /* This space intentionally left blank */ +} + +static void pointer_handle_axis_stop(void *data, + struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis) { + /* This space intentionally left blank */ +} + +static void pointer_handle_axis_discrete(void *data, + struct wl_pointer *wl_pointer, uint32_t axis, int32_t discrete) { + /* This space intentionally left blank */ +} + +static const struct wl_pointer_listener pointer_listener = { + .enter = pointer_handle_enter, + .leave = pointer_handle_leave, + .motion = pointer_handle_motion, + .button = pointer_handle_button, + .axis = pointer_handle_axis, + .frame = pointer_handle_frame, + .axis_source = pointer_handle_axis_source, + .axis_stop = pointer_handle_axis_stop, + .axis_discrete = pointer_handle_axis_discrete, +}; + + +/** + * relative-pointer: + * + * This client servers as an example for the relative-pointer-v1 protocol, and + * to some extent the pointer-constraints protocol as well (the locked_pointer + * interface). + * + * The intended behavior is the following. In the default state, the client + * shows a black background, and renders the pointer location as a small white + * box. No cursor is shown. In the locked state, the pointer is locked and the + * client only listens for relative motion events, which are rendered relative + * to the last unlocked pointer location by a small red box. Clicking with the + * left mouse button toggles the state. + * + * Most of the code is standard. The interesting stuff happens in "wl_pointer + * handling" (toggling states), and "zwp_relative_pointer handling" (rendering + * relative motion events). + */ + +int main(int argc, char **argv) { + + /* Connect to the display */ + + struct wl_display *display = wl_display_connect(NULL); + if (display == NULL) { + fprintf(stderr, "Could not connect to a Wayland display\n"); + return EXIT_FAILURE; + } + + /* Get the registry and set listeners */ + + struct wl_registry *registry = wl_display_get_registry(display); + wl_registry_add_listener(registry, ®istry_listener, NULL); + wl_display_dispatch(display); + wl_display_roundtrip(display); + + /* Check that all the global interfaces were captured */ + + if (compositor == NULL) { + fprintf(stderr, "wl_compositor not available\n"); + return EXIT_FAILURE; + } + if (seat == NULL) { + fprintf(stderr, "wl_seat not available\n"); + return EXIT_FAILURE; + } + if (wm_base == NULL) { + fprintf(stderr, "xdg_wm_base not available\n"); + return EXIT_FAILURE; + } + if (pointer_constraints == NULL) { + fprintf(stderr, "zwp_pointer_constraints_v1 not available\n"); + return EXIT_FAILURE; + } + if (relative_pointer_manager == NULL) { + fprintf(stderr, "zwp_relative_pointer_manager_v1 not available\n"); + return EXIT_FAILURE; + } + + /* Initialize EGL context */ + + struct egl_info *e = calloc(1, sizeof(struct egl_info)); + e->egl = calloc(1, sizeof(struct wlr_egl)); + e->width = e->height = 512; + + wlr_egl_init(e->egl, EGL_PLATFORM_WAYLAND_EXT, display, NULL, + WL_SHM_FORMAT_ARGB8888); + + /* Create the surface and xdg_toplevels, and set listeners */ + + struct wl_surface *surface = wl_compositor_create_surface(compositor); + struct xdg_surface *xdg_surface = + xdg_wm_base_get_xdg_surface(wm_base, surface); + struct xdg_toplevel *xdg_toplevel = xdg_surface_get_toplevel(xdg_surface); + + xdg_surface_add_listener(xdg_surface, &xdg_surface_listener, e); + xdg_toplevel_add_listener(xdg_toplevel, &xdg_toplevel_listener, e); + + /* Create the egl window and surface */ + + wl_surface_commit(surface); + + e->egl_window = wl_egl_window_create(surface, e->width, e->height); + e->egl_surface = wlr_egl_create_surface(e->egl, e->egl_window); + e->surface = surface; + + wl_display_roundtrip(display); + + /* Setup global state and render */ + + struct window *w = calloc(1, sizeof(struct window)); + w->egl_info = e; + + draw_init(e); + draw_background(e); + draw_end(e); + + /* Setup the pointer */ + + struct wl_pointer *pointer = wl_seat_get_pointer(seat); + wl_pointer_add_listener(pointer, &pointer_listener, w); + + w->pointer = pointer; + + /* Run display */ + + while (wl_display_dispatch(display) != -1) { + /* This space intentionally left blank */ + } + + return EXIT_SUCCESS; +} diff --git a/include/rootston/desktop.h b/include/rootston/desktop.h index b1fcaca07..56d2a1290 100644 --- a/include/rootston/desktop.h +++ b/include/rootston/desktop.h @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -65,6 +66,7 @@ struct roots_desktop { struct wlr_pointer_constraints_v1 *pointer_constraints; struct wlr_presentation *presentation; struct wlr_foreign_toplevel_manager_v1 *foreign_toplevel_manager_v1; + struct wlr_relative_pointer_manager_v1 *relative_pointer_manager; struct wl_listener new_output; struct wl_listener layout_change; diff --git a/include/wlr/types/meson.build b/include/wlr/types/meson.build index 752c0deae..dfb55ec91 100644 --- a/include/wlr/types/meson.build +++ b/include/wlr/types/meson.build @@ -27,6 +27,7 @@ install_headers( 'wlr_presentation_time.h', 'wlr_primary_selection.h', 'wlr_region.h', + 'wlr_relative_pointer_v1.h', 'wlr_screencopy_v1.h', 'wlr_screenshooter.h', 'wlr_seat.h', diff --git a/include/wlr/types/wlr_pointer.h b/include/wlr/types/wlr_pointer.h index 7dc643ae1..6da4c02a7 100644 --- a/include/wlr/types/wlr_pointer.h +++ b/include/wlr/types/wlr_pointer.h @@ -32,6 +32,7 @@ struct wlr_event_pointer_motion { struct wlr_input_device *device; uint32_t time_msec; double delta_x, delta_y; + double unaccel_dx, unaccel_dy; }; struct wlr_event_pointer_motion_absolute { diff --git a/include/wlr/types/wlr_relative_pointer_v1.h b/include/wlr/types/wlr_relative_pointer_v1.h new file mode 100644 index 000000000..f9f91219c --- /dev/null +++ b/include/wlr/types/wlr_relative_pointer_v1.h @@ -0,0 +1,81 @@ +/* + * This an unstable interface of wlroots. No guarantees are made regarding the + * future consistency of this API. + */ +#ifndef WLR_USE_UNSTABLE +#error "Add -DWLR_USE_UNSTABLE to enable unstable wlroots features" +#endif + +#ifndef WLR_TYPES_WLR_RELATIVE_POINTER_V1_H +#define WLR_TYPES_WLR_RELATIVE_POINTER_V1_H + +#include + + +/** + * This protocol specifies a set of interfaces used for making clients able to + * receive relative pointer events not obstructed by barriers (such as the + * monitor edge or pointer constraints). + */ + + +/** + * A global interface used for getting the relative pointer object for a given + * pointer. + */ + +struct wlr_relative_pointer_manager_v1 { + struct wl_global *global; + struct wl_list resources; // wl_resource_get_link() + struct wl_list relative_pointers; // wlr_relative_pointer_v1::link + + struct { + struct wl_signal destroy; + struct wl_signal new_relative_pointer; //wlr_relative_pointer_v1 + } events; + + struct wl_listener display_destroy_listener; + + void *data; +}; + + +/** + * A wp_relative_pointer object is an extension to the wl_pointer interface + * used for emitting relative pointer events. It shares the same focus as + * wl_pointer objects of the same seat and will only emit events when it has + * focus. + */ + +struct wlr_relative_pointer_v1 { + struct wl_resource *resource; + struct wl_resource *pointer; + struct wlr_seat *seat; + struct wl_list link; // wlr_relative_pointer_manager_v1::relative_pointers + + struct { + struct wl_signal destroy; + } events; + + struct wl_listener seat_destroy; + struct wl_listener pointer_destroy; + + void *data; +}; + +struct wlr_relative_pointer_manager_v1 *wlr_relative_pointer_manager_v1_create( + struct wl_display *display); +void wlr_relative_pointer_manager_v1_destroy( + struct wlr_relative_pointer_manager_v1 *relative_pointer_manager); + +/** + * Send a relative motion event to the seat with the same wl_pointer as relative_pointer + */ +void wlr_relative_pointer_v1_send_relative_motion( + struct wlr_relative_pointer_v1 *relative_pointer, uint64_t time_msec, + double dx, double dy, double dx_unaccel, double dy_unaccel); + +struct wlr_relative_pointer_v1 *wlr_relative_pointer_v1_from_resource( + struct wl_resource *resource); + +#endif diff --git a/protocol/meson.build b/protocol/meson.build index 58f570465..6d5acf377 100644 --- a/protocol/meson.build +++ b/protocol/meson.build @@ -20,6 +20,7 @@ protocols = [ [wl_protocol_dir, 'unstable/xdg-output/xdg-output-unstable-v1.xml'], [wl_protocol_dir, 'unstable/xdg-shell/xdg-shell-unstable-v6.xml'], [wl_protocol_dir, 'unstable/pointer-constraints/pointer-constraints-unstable-v1.xml'], + [wl_protocol_dir, 'unstable/relative-pointer/relative-pointer-unstable-v1.xml'], 'gamma-control.xml', 'gtk-primary-selection.xml', 'idle.xml', @@ -42,6 +43,7 @@ client_protocols = [ [wl_protocol_dir, 'unstable/xdg-decoration/xdg-decoration-unstable-v1.xml'], [wl_protocol_dir, 'unstable/xdg-shell/xdg-shell-unstable-v6.xml'], [wl_protocol_dir, 'unstable/pointer-constraints/pointer-constraints-unstable-v1.xml'], + [wl_protocol_dir, 'unstable/relative-pointer/relative-pointer-unstable-v1.xml'], 'idle.xml', 'input-method-unstable-v2.xml', 'screenshooter.xml', diff --git a/rootston/cursor.c b/rootston/cursor.c index b9ded30e7..1fdf1dbb5 100644 --- a/rootston/cursor.c +++ b/rootston/cursor.c @@ -305,11 +305,41 @@ static void roots_cursor_press_button(struct roots_cursor *cursor, } } +static void notify_relative_motion(struct roots_seat *seat, uint64_t time_msec, + double dx, double dy, double dx_unaccel, double dy_unaccel) { + struct wlr_relative_pointer_manager_v1 *relative_pointer_manager = + seat->input->server->desktop->relative_pointer_manager; + + struct wlr_seat_client *client = seat->seat->pointer_state.focused_client; + if (client == NULL) { + return; + } + + struct wlr_relative_pointer_v1 *pointer; + wl_list_for_each(pointer, &relative_pointer_manager->relative_pointers, link) { + struct wlr_seat_client *relative_pointer_client = + wlr_seat_client_from_pointer_resource(pointer->pointer); + + if (seat->seat == pointer->seat && + client == relative_pointer_client) { + wlr_relative_pointer_v1_send_relative_motion(pointer, + time_msec, dx, dy, dx_unaccel, dy_unaccel); + } + + } +} + void roots_cursor_handle_motion(struct roots_cursor *cursor, struct wlr_event_pointer_motion *event) { double dx = event->delta_x; double dy = event->delta_y; + double unaccel_dx = event->unaccel_dx; + double unaccel_dy = event->unaccel_dy; + + notify_relative_motion(cursor->seat, + (uint64_t)event->time_msec * 1000, dx, dy, unaccel_dx, unaccel_dy); + if (cursor->active_constraint) { struct roots_view *view = cursor->pointer_view->view; assert(view); @@ -349,6 +379,12 @@ void roots_cursor_handle_motion_absolute(struct roots_cursor *cursor, wlr_cursor_absolute_to_layout_coords(cursor->cursor, event->device, event->x, event->y, &lx, &ly); + double dx = lx - cursor->cursor->x; + double dy = ly - cursor->cursor->y; + + notify_relative_motion(cursor->seat, + (uint64_t)event->time_msec * 1000, dx, dy, dx, dy); + if (cursor->pointer_view) { struct roots_view *view = cursor->pointer_view->view; diff --git a/rootston/desktop.c b/rootston/desktop.c index 48e2635c9..77a52571a 100644 --- a/rootston/desktop.c +++ b/rootston/desktop.c @@ -1081,6 +1081,8 @@ struct roots_desktop *desktop_create(struct roots_server *server, wlr_presentation_create(server->wl_display, server->backend); desktop->foreign_toplevel_manager_v1 = wlr_foreign_toplevel_manager_v1_create(server->wl_display); + desktop->relative_pointer_manager = + wlr_relative_pointer_manager_v1_create(server->wl_display); return desktop; } diff --git a/types/meson.build b/types/meson.build index 1813b1449..982f607de 100644 --- a/types/meson.build +++ b/types/meson.build @@ -51,6 +51,7 @@ lib_wlr_types = static_library( 'wlr_primary_selection.c', 'wlr_region.c', 'wlr_screencopy_v1.c', + 'wlr_relative_pointer_v1.c', 'wlr_screenshooter.c', 'wlr_server_decoration.c', 'wlr_surface.c', diff --git a/types/wlr_relative_pointer_v1.c b/types/wlr_relative_pointer_v1.c new file mode 100644 index 000000000..a6e6e4b31 --- /dev/null +++ b/types/wlr_relative_pointer_v1.c @@ -0,0 +1,263 @@ +#include +#include +#include +#include +#include +#include +#include +#include "wayland-util.h" +#include "wayland-server.h" +#include "relative-pointer-unstable-v1-protocol.h" + +#define RELATIVE_POINTER_MANAGER_VERSION 1 + +static const struct zwp_relative_pointer_manager_v1_interface relative_pointer_manager_v1_impl; +static const struct zwp_relative_pointer_v1_interface relative_pointer_v1_impl; + + +/** + * helper functions + */ + +struct wlr_relative_pointer_v1 *wlr_relative_pointer_v1_from_resource(struct wl_resource *resource) { + assert(wl_resource_instance_of(resource, &zwp_relative_pointer_v1_interface, + &relative_pointer_v1_impl)); + return wl_resource_get_user_data(resource); +} + + +static struct wlr_relative_pointer_manager_v1 *relative_pointer_manager_from_resource(struct wl_resource *resource) { + assert(wl_resource_instance_of(resource, &zwp_relative_pointer_manager_v1_interface, + &relative_pointer_manager_v1_impl)); + return wl_resource_get_user_data(resource); +} + + +/** + * relative_pointer handler functions + */ + +static void relative_pointer_destroy(struct wlr_relative_pointer_v1 *relative_pointer) { + wlr_signal_emit_safe(&relative_pointer->events.destroy, relative_pointer); + + wl_list_remove(&relative_pointer->link); + wl_list_remove(&relative_pointer->seat_destroy.link); + + wl_resource_set_user_data(relative_pointer->resource, NULL); + free(relative_pointer); +} + +static void relative_pointer_v1_handle_resource_destroy(struct wl_resource *resource) { + struct wlr_relative_pointer_v1 *relative_pointer = + wlr_relative_pointer_v1_from_resource(resource); + if (relative_pointer == NULL) { + return; + } + relative_pointer_destroy(relative_pointer); +} + + +static void relative_pointer_v1_handle_destroy(struct wl_client *client, + struct wl_resource *resource) { + struct wlr_relative_pointer_v1 *relative_pointer = + wlr_relative_pointer_v1_from_resource(resource); + wlr_log(WLR_DEBUG, "relative_pointer_v1 %p released by client %p", + relative_pointer, client); + + wl_resource_destroy(resource); +} + +static void relative_pointer_handle_seat_destroy(struct wl_listener *listener, + void *data) { + struct wlr_relative_pointer_v1 *relative_pointer = + wl_container_of(listener, relative_pointer, seat_destroy); + + relative_pointer_destroy(relative_pointer); +} + +static void relative_pointer_handle_pointer_destroy(struct wl_listener *listener, + void *data) { + struct wlr_relative_pointer_v1 *relative_pointer = + wl_container_of(listener, relative_pointer, pointer_destroy); + + relative_pointer_destroy(relative_pointer); +} + +/** + * relative_pointer_manager handler functions + */ + +static void relative_pointer_manager_v1_handle_resource_destroy(struct wl_resource *resource) { + wl_list_remove(wl_resource_get_link(resource)); +} + + +static void relative_pointer_manager_v1_handle_destroy(struct wl_client *client, + struct wl_resource *resource) { + wl_resource_destroy(resource); + + wlr_log(WLR_DEBUG, "relative_pointer_v1 manager unbound from client %p", + client); +} + +static void relative_pointer_manager_v1_handle_get_relative_pointer(struct wl_client *client, + struct wl_resource *resource, uint32_t id, struct wl_resource *pointer) { + struct wlr_seat_client *seat_client = + wlr_seat_client_from_pointer_resource(pointer); + + struct wlr_relative_pointer_v1 *relative_pointer = + calloc(1, sizeof(struct wlr_relative_pointer_v1)); + if (relative_pointer == NULL) { + wl_client_post_no_memory(client); + return; + } + + struct wl_resource *relative_pointer_resource = wl_resource_create(client, + &zwp_relative_pointer_v1_interface, wl_resource_get_version(resource), id); + if (relative_pointer_resource == NULL) { + free(relative_pointer); + wl_client_post_no_memory(client); + return; + } + + relative_pointer->resource = relative_pointer_resource; + relative_pointer->seat = seat_client->seat; + relative_pointer->pointer = pointer; + + wl_signal_init(&relative_pointer->events.destroy); + + wl_resource_set_implementation(relative_pointer_resource, &relative_pointer_v1_impl, + relative_pointer, relative_pointer_v1_handle_resource_destroy); + + struct wlr_relative_pointer_manager_v1 *manager = + relative_pointer_manager_from_resource(resource); + + wl_list_insert(&manager->relative_pointers, + &relative_pointer->link); + + wl_signal_add(&relative_pointer->seat->events.destroy, + &relative_pointer->seat_destroy); + relative_pointer->seat_destroy.notify = relative_pointer_handle_seat_destroy; + + wl_resource_add_destroy_listener(relative_pointer->pointer, + &relative_pointer->pointer_destroy); + relative_pointer->pointer_destroy.notify = relative_pointer_handle_pointer_destroy; + + wlr_signal_emit_safe(&manager->events.new_relative_pointer, + relative_pointer); + + wlr_log(WLR_DEBUG, "relative_pointer_v1 %p created for client %p", + relative_pointer, client); +} + + +static void relative_pointer_manager_v1_bind(struct wl_client *wl_client, void *data, + uint32_t version, uint32_t id) { + struct wlr_relative_pointer_manager_v1 *manager = data; + + struct wl_resource *manager_resource = wl_resource_create(wl_client, + &zwp_relative_pointer_manager_v1_interface, version, id); + + if (manager_resource == NULL) { + wl_client_post_no_memory(wl_client); + return; + } + + wl_list_insert(&manager->resources, wl_resource_get_link(manager_resource)); + + wl_resource_set_implementation(manager_resource, &relative_pointer_manager_v1_impl, + manager, relative_pointer_manager_v1_handle_resource_destroy); + + wlr_log(WLR_DEBUG, "relative_pointer_v1 manager bound to client %p", + wl_client); +} + +static void handle_display_destroy(struct wl_listener *listener, void *data) { + struct wlr_relative_pointer_manager_v1 *manager = + wl_container_of(listener, manager, display_destroy_listener); + wlr_relative_pointer_manager_v1_destroy(manager); +} + + +/** + * Implementations + */ + +static const struct zwp_relative_pointer_manager_v1_interface relative_pointer_manager_v1_impl = { + .destroy = relative_pointer_manager_v1_handle_destroy, + .get_relative_pointer = relative_pointer_manager_v1_handle_get_relative_pointer, +}; + + +static const struct zwp_relative_pointer_v1_interface relative_pointer_v1_impl = { + .destroy = relative_pointer_v1_handle_destroy, +}; + + +/** + * Public functions + */ + +struct wlr_relative_pointer_manager_v1 *wlr_relative_pointer_manager_v1_create(struct wl_display *display) { + struct wlr_relative_pointer_manager_v1 *manager = + calloc(1, sizeof(struct wlr_relative_pointer_manager_v1)); + + if (manager == NULL) { + return NULL; + } + + wl_list_init(&manager->resources); + wl_list_init(&manager->relative_pointers); + + manager->global = wl_global_create(display, + &zwp_relative_pointer_manager_v1_interface, RELATIVE_POINTER_MANAGER_VERSION, + manager, relative_pointer_manager_v1_bind); + + if (manager->global == NULL) { + free(manager); + return NULL; + } + + wl_signal_init(&manager->events.destroy); + wl_signal_init(&manager->events.new_relative_pointer); + + manager->display_destroy_listener.notify = handle_display_destroy; + wl_display_add_destroy_listener(display, &manager->display_destroy_listener); + + wlr_log(WLR_DEBUG, "relative_pointer_v1 manager created"); + + return manager; +} + + +void wlr_relative_pointer_manager_v1_destroy(struct wlr_relative_pointer_manager_v1 *manager) { + if (manager == NULL) { + return; + } + + wlr_signal_emit_safe(&manager->events.destroy, manager); + wl_list_remove(&manager->display_destroy_listener.link); + + struct wl_resource *resource; + struct wl_resource *tmp_resource; + wl_resource_for_each_safe(resource, tmp_resource, &manager->resources) { + wl_resource_destroy(resource); + } + + wl_global_destroy(manager->global); + free(manager); + + wlr_log(WLR_DEBUG, "relative_pointer_v1 manager destroyed"); +} + + +void wlr_relative_pointer_v1_send_relative_motion(struct wlr_relative_pointer_v1 *relative_pointer, + uint64_t time_msec, double dx, double dy, + double dx_unaccel, double dy_unaccel) { + zwp_relative_pointer_v1_send_relative_motion(relative_pointer->resource, + (uint32_t)(time_msec >> 32), (uint32_t)time_msec, + wl_fixed_from_double(dx), wl_fixed_from_double(dy), + wl_fixed_from_double(dx_unaccel), wl_fixed_from_double(dy_unaccel)); + + wl_pointer_send_frame(relative_pointer->pointer); +}