From: Alexandros Frantzis alexandros.frantzis@collabora.com
When the cursor is hidden, transition to relative mouse motion to enable mouselook in 3D games. --- dlls/winewayland.drv/Makefile.in | 1 + dlls/winewayland.drv/wayland.c | 5 ++ dlls/winewayland.drv/wayland_pointer.c | 118 ++++++++++++++++++++++++- dlls/winewayland.drv/waylanddrv.h | 3 + 4 files changed, 124 insertions(+), 3 deletions(-)
diff --git a/dlls/winewayland.drv/Makefile.in b/dlls/winewayland.drv/Makefile.in index ec1eff8d97c..b47bdb262c0 100644 --- a/dlls/winewayland.drv/Makefile.in +++ b/dlls/winewayland.drv/Makefile.in @@ -7,6 +7,7 @@ SOURCES = \ display.c \ dllmain.c \ pointer-constraints-unstable-v1.xml \ + relative-pointer-unstable-v1.xml \ version.rc \ viewporter.xml \ vulkan.c \ diff --git a/dlls/winewayland.drv/wayland.c b/dlls/winewayland.drv/wayland.c index 066e9e7c963..841b33ae560 100644 --- a/dlls/winewayland.drv/wayland.c +++ b/dlls/winewayland.drv/wayland.c @@ -159,6 +159,11 @@ static void registry_handle_global(void *data, struct wl_registry *registry, process_wayland.zwp_pointer_constraints_v1 = wl_registry_bind(registry, id, &zwp_pointer_constraints_v1_interface, 1); } + else if (strcmp(interface, "zwp_relative_pointer_manager_v1") == 0) + { + process_wayland.zwp_relative_pointer_manager_v1 = + wl_registry_bind(registry, id, &zwp_relative_pointer_manager_v1_interface, 1); + } }
static void registry_handle_global_remove(void *data, struct wl_registry *registry, diff --git a/dlls/winewayland.drv/wayland_pointer.c b/dlls/winewayland.drv/wayland_pointer.c index 79a1c6a8e60..79e72e41b9d 100644 --- a/dlls/winewayland.drv/wayland_pointer.c +++ b/dlls/winewayland.drv/wayland_pointer.c @@ -46,8 +46,7 @@ static HWND wayland_pointer_get_focused_hwnd(void) return hwnd; }
-static void pointer_handle_motion(void *data, struct wl_pointer *wl_pointer, - uint32_t time, wl_fixed_t sx, wl_fixed_t sy) +static void pointer_handle_motion_internal(wl_fixed_t sx, wl_fixed_t sy) { INPUT input = {0}; RECT *window_rect; @@ -90,6 +89,17 @@ static void pointer_handle_motion(void *data, struct wl_pointer *wl_pointer, __wine_send_input(hwnd, &input, NULL); }
+static void pointer_handle_motion(void *data, struct wl_pointer *wl_pointer, + uint32_t time, wl_fixed_t sx, wl_fixed_t sy) +{ + struct wayland_pointer *pointer = &process_wayland.pointer; + + /* Ignore absolute motion events if in relative mode. */ + if (pointer->zwp_relative_pointer_v1) return; + + pointer_handle_motion_internal(sx, sy); +} + static void wayland_set_cursor(HWND hwnd, HCURSOR hcursor, BOOL use_hcursor);
static void pointer_handle_enter(void *data, struct wl_pointer *wl_pointer, @@ -118,7 +128,7 @@ static void pointer_handle_enter(void *data, struct wl_pointer *wl_pointer, /* Handle the enter as a motion, to account for cases where the * window first appears beneath the pointer and won't get a separate * motion event. */ - pointer_handle_motion(data, wl_pointer, 0, sx, sy); + pointer_handle_motion_internal(sx, sy); }
static void pointer_handle_leave(void *data, struct wl_pointer *wl_pointer, @@ -228,6 +238,80 @@ static const struct wl_pointer_listener pointer_listener = pointer_handle_axis_discrete };
+static void relative_pointer_v1_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) +{ + INPUT input = {0}; + HWND hwnd; + POINT screen, origin; + struct wayland_surface *surface; + RECT window_rect; + + if (!(hwnd = wayland_pointer_get_focused_hwnd())) return; + if (!(surface = wayland_surface_lock_hwnd(hwnd))) return; + + window_rect = surface->window.rect; + + wayland_surface_coords_to_window(surface, + wl_fixed_to_double(dx), + wl_fixed_to_double(dy), + (int *)&screen.x, (int *)&screen.y); + + pthread_mutex_unlock(&surface->mutex); + + if (screen.x >= 0) + { + origin.x = window_rect.left; + screen.x += origin.x; + if (screen.x >= window_rect.right) screen.x = window_rect.right - 1; + } + else + { + origin.x = window_rect.right; + screen.x += origin.x; + if (screen.x < window_rect.left) screen.x = window_rect.left; + } + + if (screen.y >= 0) + { + origin.y = window_rect.top; + screen.y += origin.y; + if (screen.y >= window_rect.bottom) screen.y = window_rect.bottom - 1; + } + else + { + origin.y = window_rect.bottom; + screen.y += origin.y; + if (screen.y < window_rect.top) screen.y = window_rect.top; + } + + /* Transform the relative motion from window coordinates to physical + * coordinates required for the input event. */ + if (!NtUserLogicalToPerMonitorDPIPhysicalPoint(hwnd, &screen)) return; + if (!NtUserLogicalToPerMonitorDPIPhysicalPoint(hwnd, &origin)) return; + screen.x -= origin.x; + screen.y -= origin.y; + + input.type = INPUT_MOUSE; + input.mi.dx = screen.x; + input.mi.dy = screen.y; + input.mi.dwFlags = MOUSEEVENTF_MOVE; + + TRACE("hwnd=%p wayland_dxdy=%.2f,%.2f screen_dxdy=%d,%d\n", + hwnd, wl_fixed_to_double(dx), wl_fixed_to_double(dy), + (int)screen.x, (int)screen.y); + + __wine_send_input(hwnd, &input, NULL); +} + +static const struct zwp_relative_pointer_v1_listener relative_pointer_v1_listener = +{ + relative_pointer_v1_relative_motion +}; + void wayland_pointer_init(struct wl_pointer *wl_pointer) { struct wayland_pointer *pointer = &process_wayland.pointer; @@ -250,6 +334,11 @@ void wayland_pointer_deinit(void) zwp_confined_pointer_v1_destroy(pointer->zwp_confined_pointer_v1); pointer->zwp_confined_pointer_v1 = NULL; } + if (pointer->zwp_relative_pointer_v1) + { + zwp_relative_pointer_v1_destroy(pointer->zwp_relative_pointer_v1); + pointer->zwp_relative_pointer_v1 = NULL; + } wl_pointer_release(pointer->wl_pointer); pointer->wl_pointer = NULL; pointer->focused_hwnd = NULL; @@ -552,6 +641,28 @@ clear_cursor: } }
+static void wayland_pointer_update_relative(void) +{ + struct wayland_pointer *pointer = &process_wayland.pointer; + + if (!pointer->cursor.wl_surface && !pointer->zwp_relative_pointer_v1) + { + pointer->zwp_relative_pointer_v1 = + zwp_relative_pointer_manager_v1_get_relative_pointer( + process_wayland.zwp_relative_pointer_manager_v1, + pointer->wl_pointer); + zwp_relative_pointer_v1_add_listener(pointer->zwp_relative_pointer_v1, + &relative_pointer_v1_listener, NULL); + TRACE("Enabling relative motion\n"); + } + else if (pointer->cursor.wl_surface && pointer->zwp_relative_pointer_v1) + { + zwp_relative_pointer_v1_destroy(pointer->zwp_relative_pointer_v1); + pointer->zwp_relative_pointer_v1 = NULL; + TRACE("Disabling relative motion\n"); + } +} + static void wayland_set_cursor(HWND hwnd, HCURSOR hcursor, BOOL use_hcursor) { struct wayland_pointer *pointer = &process_wayland.pointer; @@ -578,6 +689,7 @@ static void wayland_set_cursor(HWND hwnd, HCURSOR hcursor, BOOL use_hcursor) pointer->cursor.wl_surface, pointer->cursor.hotspot_x, pointer->cursor.hotspot_y); + wayland_pointer_update_relative(); wl_display_flush(process_wayland.wl_display); } pthread_mutex_unlock(&pointer->mutex); diff --git a/dlls/winewayland.drv/waylanddrv.h b/dlls/winewayland.drv/waylanddrv.h index f3c21f2871f..69dd2a3659c 100644 --- a/dlls/winewayland.drv/waylanddrv.h +++ b/dlls/winewayland.drv/waylanddrv.h @@ -30,6 +30,7 @@ #include <xkbcommon/xkbcommon.h> #include <xkbcommon/xkbregistry.h> #include "pointer-constraints-unstable-v1-client-protocol.h" +#include "relative-pointer-unstable-v1-client-protocol.h" #include "viewporter-client-protocol.h" #include "xdg-output-unstable-v1-client-protocol.h" #include "xdg-shell-client-protocol.h" @@ -91,6 +92,7 @@ struct wayland_pointer { struct wl_pointer *wl_pointer; struct zwp_confined_pointer_v1 *zwp_confined_pointer_v1; + struct zwp_relative_pointer_v1 *zwp_relative_pointer_v1; HWND focused_hwnd; uint32_t enter_serial; uint32_t button_serial; @@ -118,6 +120,7 @@ struct wayland struct wp_viewporter *wp_viewporter; struct wl_subcompositor *wl_subcompositor; struct zwp_pointer_constraints_v1 *zwp_pointer_constraints_v1; + struct zwp_relative_pointer_manager_v1 *zwp_relative_pointer_manager_v1; struct wayland_seat seat; struct wayland_keyboard keyboard; struct wayland_pointer pointer;