|
| 1 | +/* |
| 2 | + * Wayland Support |
| 3 | + * |
| 4 | + * Copyright (C) 2025 Red Hat Inc. |
| 5 | + * |
| 6 | + * This library is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 2 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This library is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public |
| 17 | + * License along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + * |
| 19 | + * Author: Carlos Garnacho <carlosg@gnome.org> |
| 20 | + */ |
| 21 | + |
| 22 | +#include "config.h" |
| 23 | + |
| 24 | +#include "meta-wayland-pointer-warp.h" |
| 25 | + |
| 26 | +#include "meta-wayland-private.h" |
| 27 | +#include "meta-wayland-seat.h" |
| 28 | +#include "meta-wayland-surface.h" |
| 29 | + |
| 30 | +#include "pointer-warp-v1-server-protocol.h" |
| 31 | + |
| 32 | +struct _MetaWaylandPointerWarp |
| 33 | +{ |
| 34 | + MetaWaylandSeat *seat; |
| 35 | + struct wl_list resource_list; |
| 36 | +}; |
| 37 | + |
| 38 | +static void |
| 39 | +pointer_warp_destroy (struct wl_client *client, |
| 40 | + struct wl_resource *resource) |
| 41 | +{ |
| 42 | + wl_resource_destroy (resource); |
| 43 | +} |
| 44 | + |
| 45 | +static void |
| 46 | +pointer_warp_perform (struct wl_client *client, |
| 47 | + struct wl_resource *resource, |
| 48 | + struct wl_resource *surface_resource, |
| 49 | + struct wl_resource *pointer_resource, |
| 50 | + wl_fixed_t x, |
| 51 | + wl_fixed_t y, |
| 52 | + uint32_t serial) |
| 53 | +{ |
| 54 | + ClutterSeat *seat; |
| 55 | + MetaWaylandSurface *surface = wl_resource_get_user_data (surface_resource); |
| 56 | + MetaWaylandPointer *pointer = wl_resource_get_user_data (pointer_resource); |
| 57 | + MetaSurfaceActor *surface_actor = meta_wayland_surface_get_actor (surface); |
| 58 | + graphene_point3d_t coords = |
| 59 | + GRAPHENE_POINT3D_INIT ((float) wl_fixed_to_double (x), |
| 60 | + (float) wl_fixed_to_double (y), |
| 61 | + 0.0); |
| 62 | + |
| 63 | + /* Not focused and implicitly grabbed */ |
| 64 | + if (!meta_wayland_pointer_get_grab_info (pointer, surface, serial, TRUE, |
| 65 | + NULL, NULL, NULL)) |
| 66 | + return; |
| 67 | + |
| 68 | + /* Outside of actor */ |
| 69 | + if (!surface_actor || |
| 70 | + x < 0 || x > clutter_actor_get_width (CLUTTER_ACTOR (surface_actor)) || |
| 71 | + y < 0 || y > clutter_actor_get_height (CLUTTER_ACTOR (surface_actor))) |
| 72 | + return; |
| 73 | + |
| 74 | + clutter_actor_apply_transform_to_point (CLUTTER_ACTOR (surface_actor), |
| 75 | + &coords, &coords); |
| 76 | + |
| 77 | + seat = clutter_backend_get_default_seat (clutter_get_default_backend ()); |
| 78 | + |
| 79 | + clutter_seat_warp_pointer (seat, (int) coords.x, (int) coords.y); |
| 80 | +} |
| 81 | + |
| 82 | +static struct wp_pointer_warp_v1_interface pointer_warp_interface = { |
| 83 | + pointer_warp_destroy, |
| 84 | + pointer_warp_perform, |
| 85 | +}; |
| 86 | + |
| 87 | +static void |
| 88 | +unbind_resource (struct wl_resource *resource) |
| 89 | +{ |
| 90 | + wl_list_remove (wl_resource_get_link (resource)); |
| 91 | +} |
| 92 | + |
| 93 | +static void |
| 94 | +bind_pointer_warp (struct wl_client *client, |
| 95 | + void *data, |
| 96 | + uint32_t version, |
| 97 | + uint32_t id) |
| 98 | +{ |
| 99 | + MetaWaylandPointerWarp *pointer_warp = data; |
| 100 | + struct wl_resource *resource; |
| 101 | + |
| 102 | + resource = wl_resource_create (client, &wp_pointer_warp_v1_interface, |
| 103 | + MIN (version, META_WP_POINTER_WARP_VERSION), |
| 104 | + id); |
| 105 | + wl_resource_set_implementation (resource, &pointer_warp_interface, |
| 106 | + pointer_warp, unbind_resource); |
| 107 | + wl_resource_set_user_data (resource, pointer_warp); |
| 108 | + wl_list_insert (&pointer_warp->resource_list, |
| 109 | + wl_resource_get_link (resource)); |
| 110 | +} |
| 111 | + |
| 112 | +MetaWaylandPointerWarp * |
| 113 | +meta_wayland_pointer_warp_new (MetaWaylandSeat *seat) |
| 114 | +{ |
| 115 | + MetaWaylandCompositor *compositor = meta_wayland_compositor_get_default (); |
| 116 | + MetaWaylandPointerWarp *pointer_warp; |
| 117 | + |
| 118 | + pointer_warp = g_new0 (MetaWaylandPointerWarp, 1); |
| 119 | + pointer_warp->seat = seat; |
| 120 | + wl_list_init (&pointer_warp->resource_list); |
| 121 | + |
| 122 | + wl_global_create (compositor->wayland_display, |
| 123 | + &wp_pointer_warp_v1_interface, |
| 124 | + META_WP_POINTER_WARP_VERSION, |
| 125 | + pointer_warp, bind_pointer_warp); |
| 126 | + |
| 127 | + return pointer_warp; |
| 128 | +} |
| 129 | + |
| 130 | +void |
| 131 | +meta_wayland_pointer_warp_destroy (MetaWaylandPointerWarp *pointer_warp) |
| 132 | +{ |
| 133 | + wl_list_remove (&pointer_warp->resource_list); |
| 134 | + g_free (pointer_warp); |
| 135 | +} |
0 commit comments