Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2013 Intel Corporation |
| 3 | * |
Bryce Harrington | a0bbfea | 2015-06-11 15:35:43 -0700 | [diff] [blame] | 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files (the |
| 6 | * "Software"), to deal in the Software without restriction, including |
| 7 | * without limitation the rights to use, copy, modify, merge, publish, |
| 8 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 9 | * permit persons to whom the Software is furnished to do so, subject to |
| 10 | * the following conditions: |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 11 | * |
Bryce Harrington | a0bbfea | 2015-06-11 15:35:43 -0700 | [diff] [blame] | 12 | * The above copyright notice and this permission notice (including the |
| 13 | * next paragraph) shall be included in all copies or substantial |
| 14 | * portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 20 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 21 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | * SOFTWARE. |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 24 | */ |
| 25 | |
Daniel Stone | 8e7a8bd | 2013-08-15 01:10:24 +0100 | [diff] [blame] | 26 | #include "config.h" |
| 27 | |
Jonas Ådahl | d3414f2 | 2016-07-22 17:56:31 +0800 | [diff] [blame] | 28 | #include <stdbool.h> |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 29 | #include <stdlib.h> |
| 30 | #include <stdint.h> |
| 31 | #include <string.h> |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 32 | #include <sys/mman.h> |
| 33 | #include <assert.h> |
| 34 | #include <unistd.h> |
Jonas Ådahl | d0be2bb | 2015-04-30 17:56:37 +0800 | [diff] [blame] | 35 | #include <values.h> |
Matt Roper | 01a9273 | 2013-06-24 16:52:44 +0100 | [diff] [blame] | 36 | #include <fcntl.h> |
Ander Conselvan de Oliveira | 54e90c7 | 2013-12-13 22:10:56 +0200 | [diff] [blame] | 37 | #include <limits.h> |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 38 | |
Jon Cruz | 35b2eaa | 2015-06-15 15:37:08 -0700 | [diff] [blame] | 39 | #include "shared/helpers.h" |
Jon Cruz | 4678bab | 2015-06-15 15:37:07 -0700 | [diff] [blame] | 40 | #include "shared/os-compatibility.h" |
Alexandros Frantzis | 84b31f8 | 2017-11-24 18:01:46 +0200 | [diff] [blame] | 41 | #include "shared/timespec-util.h" |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 42 | #include "compositor.h" |
Daniel Stone | 7dbb0e1 | 2016-11-24 15:30:41 +0000 | [diff] [blame] | 43 | #include "relative-pointer-unstable-v1-server-protocol.h" |
| 44 | #include "pointer-constraints-unstable-v1-server-protocol.h" |
Jonas Ådahl | d3414f2 | 2016-07-22 17:56:31 +0800 | [diff] [blame] | 45 | |
| 46 | enum pointer_constraint_type { |
| 47 | POINTER_CONSTRAINT_TYPE_LOCK, |
| 48 | POINTER_CONSTRAINT_TYPE_CONFINE, |
| 49 | }; |
| 50 | |
Jonas Ådahl | d0be2bb | 2015-04-30 17:56:37 +0800 | [diff] [blame] | 51 | enum motion_direction { |
| 52 | MOTION_DIRECTION_POSITIVE_X = 1 << 0, |
| 53 | MOTION_DIRECTION_NEGATIVE_X = 1 << 1, |
| 54 | MOTION_DIRECTION_POSITIVE_Y = 1 << 2, |
| 55 | MOTION_DIRECTION_NEGATIVE_Y = 1 << 3, |
| 56 | }; |
| 57 | |
| 58 | struct vec2d { |
| 59 | double x, y; |
| 60 | }; |
| 61 | |
| 62 | struct line { |
| 63 | struct vec2d a; |
| 64 | struct vec2d b; |
| 65 | }; |
| 66 | |
| 67 | struct border { |
| 68 | struct line line; |
| 69 | enum motion_direction blocking_dir; |
| 70 | }; |
| 71 | |
Jonas Ådahl | d3414f2 | 2016-07-22 17:56:31 +0800 | [diff] [blame] | 72 | static void |
| 73 | maybe_warp_confined_pointer(struct weston_pointer_constraint *constraint); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 74 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 75 | static void |
| 76 | empty_region(pixman_region32_t *region) |
| 77 | { |
| 78 | pixman_region32_fini(region); |
| 79 | pixman_region32_init(region); |
| 80 | } |
| 81 | |
Jonas Ådahl | d3414f2 | 2016-07-22 17:56:31 +0800 | [diff] [blame] | 82 | static void |
| 83 | region_init_infinite(pixman_region32_t *region) |
| 84 | { |
| 85 | pixman_region32_init_rect(region, INT32_MIN, INT32_MIN, |
| 86 | UINT32_MAX, UINT32_MAX); |
| 87 | } |
| 88 | |
Jonas Ådahl | 2cbf293 | 2015-07-22 12:05:38 +0800 | [diff] [blame] | 89 | static struct weston_pointer_client * |
| 90 | weston_pointer_client_create(struct wl_client *client) |
| 91 | { |
| 92 | struct weston_pointer_client *pointer_client; |
| 93 | |
| 94 | pointer_client = zalloc(sizeof *pointer_client); |
| 95 | if (!pointer_client) |
| 96 | return NULL; |
| 97 | |
| 98 | pointer_client->client = client; |
| 99 | wl_list_init(&pointer_client->pointer_resources); |
Jonas Ådahl | 30d61d8 | 2014-10-22 21:21:17 +0200 | [diff] [blame] | 100 | wl_list_init(&pointer_client->relative_pointer_resources); |
Jonas Ådahl | 2cbf293 | 2015-07-22 12:05:38 +0800 | [diff] [blame] | 101 | |
| 102 | return pointer_client; |
| 103 | } |
| 104 | |
| 105 | static void |
| 106 | weston_pointer_client_destroy(struct weston_pointer_client *pointer_client) |
| 107 | { |
| 108 | free(pointer_client); |
| 109 | } |
| 110 | |
| 111 | static bool |
| 112 | weston_pointer_client_is_empty(struct weston_pointer_client *pointer_client) |
| 113 | { |
Jonas Ådahl | 30d61d8 | 2014-10-22 21:21:17 +0200 | [diff] [blame] | 114 | return (wl_list_empty(&pointer_client->pointer_resources) && |
| 115 | wl_list_empty(&pointer_client->relative_pointer_resources)); |
Jonas Ådahl | 2cbf293 | 2015-07-22 12:05:38 +0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | static struct weston_pointer_client * |
| 119 | weston_pointer_get_pointer_client(struct weston_pointer *pointer, |
| 120 | struct wl_client *client) |
| 121 | { |
| 122 | struct weston_pointer_client *pointer_client; |
| 123 | |
| 124 | wl_list_for_each(pointer_client, &pointer->pointer_clients, link) { |
| 125 | if (pointer_client->client == client) |
| 126 | return pointer_client; |
| 127 | } |
| 128 | |
| 129 | return NULL; |
| 130 | } |
| 131 | |
| 132 | static struct weston_pointer_client * |
| 133 | weston_pointer_ensure_pointer_client(struct weston_pointer *pointer, |
| 134 | struct wl_client *client) |
| 135 | { |
| 136 | struct weston_pointer_client *pointer_client; |
| 137 | |
| 138 | pointer_client = weston_pointer_get_pointer_client(pointer, client); |
| 139 | if (pointer_client) |
| 140 | return pointer_client; |
| 141 | |
| 142 | pointer_client = weston_pointer_client_create(client); |
| 143 | wl_list_insert(&pointer->pointer_clients, &pointer_client->link); |
| 144 | |
| 145 | if (pointer->focus && |
| 146 | pointer->focus->surface->resource && |
| 147 | wl_resource_get_client(pointer->focus->surface->resource) == client) { |
| 148 | pointer->focus_client = pointer_client; |
| 149 | } |
| 150 | |
| 151 | return pointer_client; |
| 152 | } |
| 153 | |
| 154 | static void |
| 155 | weston_pointer_cleanup_pointer_client(struct weston_pointer *pointer, |
| 156 | struct weston_pointer_client *pointer_client) |
| 157 | { |
| 158 | if (weston_pointer_client_is_empty(pointer_client)) { |
| 159 | if (pointer->focus_client == pointer_client) |
| 160 | pointer->focus_client = NULL; |
| 161 | wl_list_remove(&pointer_client->link); |
| 162 | weston_pointer_client_destroy(pointer_client); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | static void |
| 167 | unbind_pointer_client_resource(struct wl_resource *resource) |
| 168 | { |
| 169 | struct weston_pointer *pointer = wl_resource_get_user_data(resource); |
| 170 | struct wl_client *client = wl_resource_get_client(resource); |
| 171 | struct weston_pointer_client *pointer_client; |
| 172 | |
| 173 | pointer_client = weston_pointer_get_pointer_client(pointer, client); |
| 174 | assert(pointer_client); |
| 175 | |
| 176 | wl_list_remove(wl_resource_get_link(resource)); |
| 177 | weston_pointer_cleanup_pointer_client(pointer, pointer_client); |
| 178 | } |
| 179 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 180 | static void unbind_resource(struct wl_resource *resource) |
| 181 | { |
Jason Ekstrand | 44a3863 | 2013-06-14 10:08:00 -0500 | [diff] [blame] | 182 | wl_list_remove(wl_resource_get_link(resource)); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 183 | } |
| 184 | |
Jonas Ådahl | 3042ffe | 2013-10-17 23:04:08 +0200 | [diff] [blame] | 185 | WL_EXPORT void |
Jonas Ådahl | 30d61d8 | 2014-10-22 21:21:17 +0200 | [diff] [blame] | 186 | weston_pointer_motion_to_abs(struct weston_pointer *pointer, |
| 187 | struct weston_pointer_motion_event *event, |
| 188 | wl_fixed_t *x, wl_fixed_t *y) |
| 189 | { |
| 190 | if (event->mask & WESTON_POINTER_MOTION_ABS) { |
| 191 | *x = wl_fixed_from_double(event->x); |
| 192 | *y = wl_fixed_from_double(event->y); |
| 193 | } else if (event->mask & WESTON_POINTER_MOTION_REL) { |
| 194 | *x = pointer->x + wl_fixed_from_double(event->dx); |
| 195 | *y = pointer->y + wl_fixed_from_double(event->dy); |
| 196 | } else { |
| 197 | assert(!"invalid motion event"); |
| 198 | *x = *y = 0; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | static bool |
| 203 | weston_pointer_motion_to_rel(struct weston_pointer *pointer, |
| 204 | struct weston_pointer_motion_event *event, |
| 205 | double *dx, double *dy, |
| 206 | double *dx_unaccel, double *dy_unaccel) |
| 207 | { |
| 208 | if (event->mask & WESTON_POINTER_MOTION_REL && |
| 209 | event->mask & WESTON_POINTER_MOTION_REL_UNACCEL) { |
| 210 | *dx = event->dx; |
| 211 | *dy = event->dy; |
| 212 | *dx_unaccel = event->dx_unaccel; |
| 213 | *dy_unaccel = event->dy_unaccel; |
| 214 | return true; |
| 215 | } else if (event->mask & WESTON_POINTER_MOTION_REL) { |
| 216 | *dx_unaccel = *dx = event->dx; |
| 217 | *dy_unaccel = *dy = event->dy; |
| 218 | return true; |
| 219 | } else if (event->mask & WESTON_POINTER_MOTION_REL_UNACCEL) { |
| 220 | *dx_unaccel = *dx = event->dx_unaccel; |
| 221 | *dy_unaccel = *dy = event->dy_unaccel; |
| 222 | return true; |
| 223 | } else { |
| 224 | return false; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | WL_EXPORT void |
Kristian Høgsberg | a71e8b2 | 2013-05-06 21:51:21 -0400 | [diff] [blame] | 229 | weston_seat_repick(struct weston_seat *seat) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 230 | { |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 231 | const struct weston_pointer *pointer = weston_seat_get_pointer(seat); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 232 | |
Derek Foreman | 1b786ee | 2015-06-03 15:53:23 -0500 | [diff] [blame] | 233 | if (!pointer) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 234 | return; |
| 235 | |
Derek Foreman | 1b786ee | 2015-06-03 15:53:23 -0500 | [diff] [blame] | 236 | pointer->grab->interface->focus(pointer->grab); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | static void |
| 240 | weston_compositor_idle_inhibit(struct weston_compositor *compositor) |
| 241 | { |
| 242 | weston_compositor_wake(compositor); |
| 243 | compositor->idle_inhibit++; |
| 244 | } |
| 245 | |
| 246 | static void |
| 247 | weston_compositor_idle_release(struct weston_compositor *compositor) |
| 248 | { |
| 249 | compositor->idle_inhibit--; |
| 250 | weston_compositor_wake(compositor); |
| 251 | } |
| 252 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 253 | static void |
Giulio Camuffo | 576fe2a | 2013-11-20 18:00:24 +0100 | [diff] [blame] | 254 | pointer_focus_view_destroyed(struct wl_listener *listener, void *data) |
| 255 | { |
| 256 | struct weston_pointer *pointer = |
| 257 | container_of(listener, struct weston_pointer, |
| 258 | focus_view_listener); |
| 259 | |
Derek Foreman | f9318d1 | 2015-05-11 15:40:11 -0500 | [diff] [blame] | 260 | weston_pointer_clear_focus(pointer); |
Giulio Camuffo | 576fe2a | 2013-11-20 18:00:24 +0100 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | static void |
| 264 | pointer_focus_resource_destroyed(struct wl_listener *listener, void *data) |
| 265 | { |
| 266 | struct weston_pointer *pointer = |
| 267 | container_of(listener, struct weston_pointer, |
| 268 | focus_resource_listener); |
| 269 | |
Derek Foreman | f9318d1 | 2015-05-11 15:40:11 -0500 | [diff] [blame] | 270 | weston_pointer_clear_focus(pointer); |
Giulio Camuffo | 576fe2a | 2013-11-20 18:00:24 +0100 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | static void |
| 274 | keyboard_focus_resource_destroyed(struct wl_listener *listener, void *data) |
| 275 | { |
| 276 | struct weston_keyboard *keyboard = |
| 277 | container_of(listener, struct weston_keyboard, |
| 278 | focus_resource_listener); |
| 279 | |
| 280 | weston_keyboard_set_focus(keyboard, NULL); |
| 281 | } |
| 282 | |
| 283 | static void |
| 284 | touch_focus_view_destroyed(struct wl_listener *listener, void *data) |
| 285 | { |
| 286 | struct weston_touch *touch = |
| 287 | container_of(listener, struct weston_touch, |
| 288 | focus_view_listener); |
| 289 | |
Derek Foreman | 4c93c08 | 2015-04-30 16:45:41 -0500 | [diff] [blame] | 290 | weston_touch_set_focus(touch, NULL); |
Giulio Camuffo | 576fe2a | 2013-11-20 18:00:24 +0100 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | static void |
| 294 | touch_focus_resource_destroyed(struct wl_listener *listener, void *data) |
| 295 | { |
| 296 | struct weston_touch *touch = |
| 297 | container_of(listener, struct weston_touch, |
| 298 | focus_resource_listener); |
| 299 | |
Derek Foreman | 4c93c08 | 2015-04-30 16:45:41 -0500 | [diff] [blame] | 300 | weston_touch_set_focus(touch, NULL); |
Giulio Camuffo | 576fe2a | 2013-11-20 18:00:24 +0100 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | static void |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 304 | move_resources(struct wl_list *destination, struct wl_list *source) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 305 | { |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 306 | wl_list_insert_list(destination, source); |
| 307 | wl_list_init(source); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | static void |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 311 | move_resources_for_client(struct wl_list *destination, |
| 312 | struct wl_list *source, |
| 313 | struct wl_client *client) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 314 | { |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 315 | struct wl_resource *resource, *tmp; |
| 316 | wl_resource_for_each_safe(resource, tmp, source) { |
| 317 | if (wl_resource_get_client(resource) == client) { |
| 318 | wl_list_remove(wl_resource_get_link(resource)); |
| 319 | wl_list_insert(destination, |
| 320 | wl_resource_get_link(resource)); |
| 321 | } |
| 322 | } |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | static void |
Kristian Høgsberg | b27901c | 2013-10-28 15:32:02 -0700 | [diff] [blame] | 326 | default_grab_pointer_focus(struct weston_pointer_grab *grab) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 327 | { |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 328 | struct weston_pointer *pointer = grab->pointer; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 329 | struct weston_view *view; |
Kristian Høgsberg | 6848c25 | 2013-05-08 22:02:59 -0400 | [diff] [blame] | 330 | wl_fixed_t sx, sy; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 331 | |
| 332 | if (pointer->button_count > 0) |
| 333 | return; |
| 334 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 335 | view = weston_compositor_pick_view(pointer->seat->compositor, |
| 336 | pointer->x, pointer->y, |
| 337 | &sx, &sy); |
Kristian Høgsberg | 6848c25 | 2013-05-08 22:02:59 -0400 | [diff] [blame] | 338 | |
Kristian Høgsberg | db1fccb | 2014-02-05 17:14:42 -0800 | [diff] [blame] | 339 | if (pointer->focus != view || pointer->sx != sx || pointer->sy != sy) |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 340 | weston_pointer_set_focus(pointer, view, sx, sy); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | static void |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 344 | pointer_send_relative_motion(struct weston_pointer *pointer, |
Alexandros Frantzis | 84b31f8 | 2017-11-24 18:01:46 +0200 | [diff] [blame] | 345 | const struct timespec *time, |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 346 | struct weston_pointer_motion_event *event) |
Jonas Ådahl | 30d61d8 | 2014-10-22 21:21:17 +0200 | [diff] [blame] | 347 | { |
| 348 | uint64_t time_usec; |
| 349 | double dx, dy, dx_unaccel, dy_unaccel; |
| 350 | wl_fixed_t dxf, dyf, dxf_unaccel, dyf_unaccel; |
| 351 | struct wl_list *resource_list; |
| 352 | struct wl_resource *resource; |
| 353 | |
| 354 | if (!pointer->focus_client) |
| 355 | return; |
| 356 | |
| 357 | if (!weston_pointer_motion_to_rel(pointer, event, |
| 358 | &dx, &dy, |
| 359 | &dx_unaccel, &dy_unaccel)) |
| 360 | return; |
| 361 | |
| 362 | resource_list = &pointer->focus_client->relative_pointer_resources; |
Alexandros Frantzis | 84b31f8 | 2017-11-24 18:01:46 +0200 | [diff] [blame] | 363 | time_usec = timespec_to_usec(&event->time); |
Jonas Ådahl | 30d61d8 | 2014-10-22 21:21:17 +0200 | [diff] [blame] | 364 | if (time_usec == 0) |
Alexandros Frantzis | 84b31f8 | 2017-11-24 18:01:46 +0200 | [diff] [blame] | 365 | time_usec = timespec_to_usec(time); |
Jonas Ådahl | 30d61d8 | 2014-10-22 21:21:17 +0200 | [diff] [blame] | 366 | |
| 367 | dxf = wl_fixed_from_double(dx); |
| 368 | dyf = wl_fixed_from_double(dy); |
| 369 | dxf_unaccel = wl_fixed_from_double(dx_unaccel); |
| 370 | dyf_unaccel = wl_fixed_from_double(dy_unaccel); |
| 371 | |
| 372 | wl_resource_for_each(resource, resource_list) { |
| 373 | zwp_relative_pointer_v1_send_relative_motion( |
| 374 | resource, |
| 375 | (uint32_t) (time_usec >> 32), |
| 376 | (uint32_t) time_usec, |
| 377 | dxf, dyf, |
| 378 | dxf_unaccel, dyf_unaccel); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | static void |
Alexandros Frantzis | 84b31f8 | 2017-11-24 18:01:46 +0200 | [diff] [blame] | 383 | pointer_send_motion(struct weston_pointer *pointer, |
| 384 | const struct timespec *time, |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 385 | wl_fixed_t sx, wl_fixed_t sy) |
Jonas Ådahl | f44942e | 2016-07-22 17:54:55 +0800 | [diff] [blame] | 386 | { |
| 387 | struct wl_list *resource_list; |
| 388 | struct wl_resource *resource; |
Alexandros Frantzis | 84b31f8 | 2017-11-24 18:01:46 +0200 | [diff] [blame] | 389 | uint32_t msecs; |
Jonas Ådahl | f44942e | 2016-07-22 17:54:55 +0800 | [diff] [blame] | 390 | |
| 391 | if (!pointer->focus_client) |
| 392 | return; |
| 393 | |
| 394 | resource_list = &pointer->focus_client->pointer_resources; |
Alexandros Frantzis | 84b31f8 | 2017-11-24 18:01:46 +0200 | [diff] [blame] | 395 | msecs = timespec_to_msec(time); |
Jonas Ådahl | f44942e | 2016-07-22 17:54:55 +0800 | [diff] [blame] | 396 | wl_resource_for_each(resource, resource_list) |
Alexandros Frantzis | 84b31f8 | 2017-11-24 18:01:46 +0200 | [diff] [blame] | 397 | wl_pointer_send_motion(resource, msecs, sx, sy); |
Jonas Ådahl | f44942e | 2016-07-22 17:54:55 +0800 | [diff] [blame] | 398 | } |
| 399 | |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 400 | WL_EXPORT void |
Alexandros Frantzis | 84b31f8 | 2017-11-24 18:01:46 +0200 | [diff] [blame] | 401 | weston_pointer_send_motion(struct weston_pointer *pointer, |
| 402 | const struct timespec *time, |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 403 | struct weston_pointer_motion_event *event) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 404 | { |
Jonas Ådahl | d251010 | 2014-10-05 21:39:14 +0200 | [diff] [blame] | 405 | wl_fixed_t x, y; |
Jonas Ådahl | 8283c34 | 2015-04-24 15:26:17 +0800 | [diff] [blame] | 406 | wl_fixed_t old_sx = pointer->sx; |
| 407 | wl_fixed_t old_sy = pointer->sy; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 408 | |
Jonas Ådahl | d251010 | 2014-10-05 21:39:14 +0200 | [diff] [blame] | 409 | if (pointer->focus) { |
| 410 | weston_pointer_motion_to_abs(pointer, event, &x, &y); |
Kristian Høgsberg | db1fccb | 2014-02-05 17:14:42 -0800 | [diff] [blame] | 411 | weston_view_from_global_fixed(pointer->focus, x, y, |
| 412 | &pointer->sx, &pointer->sy); |
Jonas Ådahl | d251010 | 2014-10-05 21:39:14 +0200 | [diff] [blame] | 413 | } |
Kristian Høgsberg | db1fccb | 2014-02-05 17:14:42 -0800 | [diff] [blame] | 414 | |
Jonas Ådahl | d251010 | 2014-10-05 21:39:14 +0200 | [diff] [blame] | 415 | weston_pointer_move(pointer, event); |
Giulio Camuffo | 1959ab8 | 2013-11-14 23:42:52 +0100 | [diff] [blame] | 416 | |
Jonas Ådahl | f44942e | 2016-07-22 17:54:55 +0800 | [diff] [blame] | 417 | if (old_sx != pointer->sx || old_sy != pointer->sy) { |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 418 | pointer_send_motion(pointer, time, |
| 419 | pointer->sx, pointer->sy); |
Kristian Høgsberg | be6403e | 2013-05-08 21:03:21 -0400 | [diff] [blame] | 420 | } |
Jonas Ådahl | 30d61d8 | 2014-10-22 21:21:17 +0200 | [diff] [blame] | 421 | |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 422 | pointer_send_relative_motion(pointer, time, event); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | static void |
Alexandros Frantzis | 84b31f8 | 2017-11-24 18:01:46 +0200 | [diff] [blame] | 426 | default_grab_pointer_motion(struct weston_pointer_grab *grab, |
| 427 | const struct timespec *time, |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 428 | struct weston_pointer_motion_event *event) |
| 429 | { |
| 430 | weston_pointer_send_motion(grab->pointer, time, event); |
| 431 | } |
| 432 | |
| 433 | /** Check if the pointer has focused resources. |
| 434 | * |
| 435 | * \param pointer The pointer to check for focused resources. |
| 436 | * \return Whether or not this pointer has focused resources |
| 437 | */ |
| 438 | WL_EXPORT bool |
| 439 | weston_pointer_has_focus_resource(struct weston_pointer *pointer) |
| 440 | { |
| 441 | if (!pointer->focus_client) |
| 442 | return false; |
| 443 | |
| 444 | if (wl_list_empty(&pointer->focus_client->pointer_resources)) |
| 445 | return false; |
| 446 | |
| 447 | return true; |
| 448 | } |
| 449 | |
| 450 | /** Send wl_pointer.button events to focused resources. |
| 451 | * |
| 452 | * \param pointer The pointer where the button events originates from. |
| 453 | * \param time The timestamp of the event |
| 454 | * \param button The button value of the event |
| 455 | * \param value The state enum value of the event |
| 456 | * |
| 457 | * For every resource that is currently in focus, send a wl_pointer.button event |
| 458 | * with the passed parameters. The focused resources are the wl_pointer |
| 459 | * resources of the client which currently has the surface with pointer focus. |
| 460 | */ |
| 461 | WL_EXPORT void |
Jonas Ådahl | c02ac11 | 2016-07-22 17:55:43 +0800 | [diff] [blame] | 462 | weston_pointer_send_button(struct weston_pointer *pointer, |
Alexandros Frantzis | 215bedc | 2017-11-16 18:20:55 +0200 | [diff] [blame] | 463 | const struct timespec *time, uint32_t button, |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 464 | enum wl_pointer_button_state state) |
Jonas Ådahl | c02ac11 | 2016-07-22 17:55:43 +0800 | [diff] [blame] | 465 | { |
| 466 | struct wl_display *display = pointer->seat->compositor->wl_display; |
| 467 | struct wl_list *resource_list; |
| 468 | struct wl_resource *resource; |
| 469 | uint32_t serial; |
Alexandros Frantzis | 215bedc | 2017-11-16 18:20:55 +0200 | [diff] [blame] | 470 | uint32_t msecs; |
Jonas Ådahl | c02ac11 | 2016-07-22 17:55:43 +0800 | [diff] [blame] | 471 | |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 472 | if (!weston_pointer_has_focus_resource(pointer)) |
Jonas Ådahl | c02ac11 | 2016-07-22 17:55:43 +0800 | [diff] [blame] | 473 | return; |
| 474 | |
| 475 | resource_list = &pointer->focus_client->pointer_resources; |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 476 | serial = wl_display_next_serial(display); |
Alexandros Frantzis | 215bedc | 2017-11-16 18:20:55 +0200 | [diff] [blame] | 477 | msecs = timespec_to_msec(time); |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 478 | wl_resource_for_each(resource, resource_list) |
Alexandros Frantzis | 215bedc | 2017-11-16 18:20:55 +0200 | [diff] [blame] | 479 | wl_pointer_send_button(resource, serial, msecs, button, state); |
Jonas Ådahl | c02ac11 | 2016-07-22 17:55:43 +0800 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | static void |
Kristian Høgsberg | b27901c | 2013-10-28 15:32:02 -0700 | [diff] [blame] | 483 | default_grab_pointer_button(struct weston_pointer_grab *grab, |
Alexandros Frantzis | 215bedc | 2017-11-16 18:20:55 +0200 | [diff] [blame] | 484 | const struct timespec *time, uint32_t button, |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 485 | enum wl_pointer_button_state state) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 486 | { |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 487 | struct weston_pointer *pointer = grab->pointer; |
Kristian Høgsberg | e122b7b | 2013-05-08 16:47:00 -0400 | [diff] [blame] | 488 | struct weston_compositor *compositor = pointer->seat->compositor; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 489 | struct weston_view *view; |
Kristian Høgsberg | e122b7b | 2013-05-08 16:47:00 -0400 | [diff] [blame] | 490 | wl_fixed_t sx, sy; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 491 | |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 492 | weston_pointer_send_button(pointer, time, button, state); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 493 | |
| 494 | if (pointer->button_count == 0 && |
Kristian Høgsberg | e122b7b | 2013-05-08 16:47:00 -0400 | [diff] [blame] | 495 | state == WL_POINTER_BUTTON_STATE_RELEASED) { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 496 | view = weston_compositor_pick_view(compositor, |
| 497 | pointer->x, pointer->y, |
| 498 | &sx, &sy); |
Kristian Høgsberg | e122b7b | 2013-05-08 16:47:00 -0400 | [diff] [blame] | 499 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 500 | weston_pointer_set_focus(pointer, view, sx, sy); |
Kristian Høgsberg | e122b7b | 2013-05-08 16:47:00 -0400 | [diff] [blame] | 501 | } |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 502 | } |
| 503 | |
Jonas Ådahl | 0336ca0 | 2014-10-04 16:28:29 +0200 | [diff] [blame] | 504 | /** Send wl_pointer.axis events to focused resources. |
| 505 | * |
| 506 | * \param pointer The pointer where the axis events originates from. |
| 507 | * \param time The timestamp of the event |
| 508 | * \param axis The axis enum value of the event |
| 509 | * \param value The axis value of the event |
| 510 | * |
| 511 | * For every resource that is currently in focus, send a wl_pointer.axis event |
| 512 | * with the passed parameters. The focused resources are the wl_pointer |
| 513 | * resources of the client which currently has the surface with pointer focus. |
| 514 | */ |
| 515 | WL_EXPORT void |
| 516 | weston_pointer_send_axis(struct weston_pointer *pointer, |
Alexandros Frantzis | 8032194 | 2017-11-16 18:20:56 +0200 | [diff] [blame] | 517 | const struct timespec *time, |
Peter Hutterer | 89b6a49 | 2016-01-18 15:58:17 +1000 | [diff] [blame] | 518 | struct weston_pointer_axis_event *event) |
Jonas Ådahl | 0336ca0 | 2014-10-04 16:28:29 +0200 | [diff] [blame] | 519 | { |
| 520 | struct wl_resource *resource; |
| 521 | struct wl_list *resource_list; |
Alexandros Frantzis | 8032194 | 2017-11-16 18:20:56 +0200 | [diff] [blame] | 522 | uint32_t msecs; |
Jonas Ådahl | 0336ca0 | 2014-10-04 16:28:29 +0200 | [diff] [blame] | 523 | |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 524 | if (!weston_pointer_has_focus_resource(pointer)) |
Jonas Ådahl | 2cbf293 | 2015-07-22 12:05:38 +0800 | [diff] [blame] | 525 | return; |
| 526 | |
| 527 | resource_list = &pointer->focus_client->pointer_resources; |
Alexandros Frantzis | 8032194 | 2017-11-16 18:20:56 +0200 | [diff] [blame] | 528 | msecs = timespec_to_msec(time); |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 529 | wl_resource_for_each(resource, resource_list) { |
| 530 | if (event->has_discrete && |
| 531 | wl_resource_get_version(resource) >= |
| 532 | WL_POINTER_AXIS_DISCRETE_SINCE_VERSION) |
| 533 | wl_pointer_send_axis_discrete(resource, event->axis, |
| 534 | event->discrete); |
| 535 | |
| 536 | if (event->value) |
Alexandros Frantzis | 8032194 | 2017-11-16 18:20:56 +0200 | [diff] [blame] | 537 | wl_pointer_send_axis(resource, msecs, |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 538 | event->axis, |
| 539 | wl_fixed_from_double(event->value)); |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 540 | else if (wl_resource_get_version(resource) >= |
| 541 | WL_POINTER_AXIS_STOP_SINCE_VERSION) |
Alexandros Frantzis | 8032194 | 2017-11-16 18:20:56 +0200 | [diff] [blame] | 542 | wl_pointer_send_axis_stop(resource, msecs, |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 543 | event->axis); |
| 544 | } |
| 545 | } |
| 546 | |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 547 | /** Send wl_pointer.axis_source events to focused resources. |
| 548 | * |
| 549 | * \param pointer The pointer where the axis_source events originates from. |
| 550 | * \param source The axis_source enum value of the event |
| 551 | * |
| 552 | * For every resource that is currently in focus, send a wl_pointer.axis_source |
| 553 | * event with the passed parameter. The focused resources are the wl_pointer |
| 554 | * resources of the client which currently has the surface with pointer focus. |
| 555 | */ |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 556 | WL_EXPORT void |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 557 | weston_pointer_send_axis_source(struct weston_pointer *pointer, |
| 558 | enum wl_pointer_axis_source source) |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 559 | { |
| 560 | struct wl_resource *resource; |
| 561 | struct wl_list *resource_list; |
| 562 | |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 563 | if (!weston_pointer_has_focus_resource(pointer)) |
Jonas Ådahl | ed6014a | 2016-04-21 10:21:48 +0800 | [diff] [blame] | 564 | return; |
| 565 | |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 566 | resource_list = &pointer->focus_client->pointer_resources; |
| 567 | wl_resource_for_each(resource, resource_list) { |
| 568 | if (wl_resource_get_version(resource) >= |
| 569 | WL_POINTER_AXIS_SOURCE_SINCE_VERSION) { |
| 570 | wl_pointer_send_axis_source(resource, source); |
| 571 | } |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | static void |
| 576 | pointer_send_frame(struct wl_resource *resource) |
| 577 | { |
| 578 | if (wl_resource_get_version(resource) >= |
| 579 | WL_POINTER_FRAME_SINCE_VERSION) { |
| 580 | wl_pointer_send_frame(resource); |
| 581 | } |
| 582 | } |
| 583 | |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 584 | /** Send wl_pointer.frame events to focused resources. |
| 585 | * |
| 586 | * \param pointer The pointer where the frame events originates from. |
| 587 | * |
| 588 | * For every resource that is currently in focus, send a wl_pointer.frame event. |
| 589 | * The focused resources are the wl_pointer resources of the client which |
| 590 | * currently has the surface with pointer focus. |
| 591 | */ |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 592 | WL_EXPORT void |
| 593 | weston_pointer_send_frame(struct weston_pointer *pointer) |
| 594 | { |
| 595 | struct wl_resource *resource; |
| 596 | struct wl_list *resource_list; |
| 597 | |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 598 | if (!weston_pointer_has_focus_resource(pointer)) |
Derek Foreman | 8efa31b | 2016-01-29 10:29:46 -0600 | [diff] [blame] | 599 | return; |
| 600 | |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 601 | resource_list = &pointer->focus_client->pointer_resources; |
Jonas Ådahl | 0336ca0 | 2014-10-04 16:28:29 +0200 | [diff] [blame] | 602 | wl_resource_for_each(resource, resource_list) |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 603 | pointer_send_frame(resource); |
Jonas Ådahl | 0336ca0 | 2014-10-04 16:28:29 +0200 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | static void |
| 607 | default_grab_pointer_axis(struct weston_pointer_grab *grab, |
Alexandros Frantzis | 8032194 | 2017-11-16 18:20:56 +0200 | [diff] [blame] | 608 | const struct timespec *time, |
Peter Hutterer | 89b6a49 | 2016-01-18 15:58:17 +1000 | [diff] [blame] | 609 | struct weston_pointer_axis_event *event) |
Jonas Ådahl | 0336ca0 | 2014-10-04 16:28:29 +0200 | [diff] [blame] | 610 | { |
Peter Hutterer | 89b6a49 | 2016-01-18 15:58:17 +1000 | [diff] [blame] | 611 | weston_pointer_send_axis(grab->pointer, time, event); |
Jonas Ådahl | 0336ca0 | 2014-10-04 16:28:29 +0200 | [diff] [blame] | 612 | } |
| 613 | |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 614 | static void |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 615 | default_grab_pointer_axis_source(struct weston_pointer_grab *grab, |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 616 | enum wl_pointer_axis_source source) |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 617 | { |
| 618 | weston_pointer_send_axis_source(grab->pointer, source); |
| 619 | } |
| 620 | |
| 621 | static void |
| 622 | default_grab_pointer_frame(struct weston_pointer_grab *grab) |
| 623 | { |
| 624 | weston_pointer_send_frame(grab->pointer); |
| 625 | } |
| 626 | |
| 627 | static void |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 628 | default_grab_pointer_cancel(struct weston_pointer_grab *grab) |
| 629 | { |
| 630 | } |
| 631 | |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 632 | static const struct weston_pointer_grab_interface |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 633 | default_pointer_grab_interface = { |
Kristian Høgsberg | b27901c | 2013-10-28 15:32:02 -0700 | [diff] [blame] | 634 | default_grab_pointer_focus, |
| 635 | default_grab_pointer_motion, |
| 636 | default_grab_pointer_button, |
Jonas Ådahl | 0336ca0 | 2014-10-04 16:28:29 +0200 | [diff] [blame] | 637 | default_grab_pointer_axis, |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 638 | default_grab_pointer_axis_source, |
| 639 | default_grab_pointer_frame, |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 640 | default_grab_pointer_cancel, |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 641 | }; |
| 642 | |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 643 | /** Check if the touch has focused resources. |
| 644 | * |
| 645 | * \param touch The touch to check for focused resources. |
| 646 | * \return Whether or not this touch has focused resources |
| 647 | */ |
| 648 | WL_EXPORT bool |
| 649 | weston_touch_has_focus_resource(struct weston_touch *touch) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 650 | { |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 651 | if (!touch->focus) |
| 652 | return false; |
| 653 | |
| 654 | if (wl_list_empty(&touch->focus_resource_list)) |
| 655 | return false; |
| 656 | |
| 657 | return true; |
| 658 | } |
| 659 | |
| 660 | /** Send wl_touch.down events to focused resources. |
| 661 | * |
| 662 | * \param touch The touch where the down events originates from. |
| 663 | * \param time The timestamp of the event |
| 664 | * \param touch_id The touch_id value of the event |
| 665 | * \param x The x value of the event |
| 666 | * \param y The y value of the event |
| 667 | * |
| 668 | * For every resource that is currently in focus, send a wl_touch.down event |
| 669 | * with the passed parameters. The focused resources are the wl_touch |
| 670 | * resources of the client which currently has the surface with touch focus. |
| 671 | */ |
| 672 | WL_EXPORT void |
Alexandros Frantzis | 9448deb | 2017-11-16 18:20:58 +0200 | [diff] [blame] | 673 | weston_touch_send_down(struct weston_touch *touch, const struct timespec *time, |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 674 | int touch_id, wl_fixed_t x, wl_fixed_t y) |
| 675 | { |
Rob Bradford | 880ebc7 | 2013-07-22 17:31:38 +0100 | [diff] [blame] | 676 | struct wl_display *display = touch->seat->compositor->wl_display; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 677 | uint32_t serial; |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 678 | struct wl_resource *resource; |
| 679 | struct wl_list *resource_list; |
Giulio Camuffo | 61ed7b6 | 2015-07-08 11:55:28 +0300 | [diff] [blame] | 680 | wl_fixed_t sx, sy; |
Alexandros Frantzis | 9448deb | 2017-11-16 18:20:58 +0200 | [diff] [blame] | 681 | uint32_t msecs; |
Giulio Camuffo | 61ed7b6 | 2015-07-08 11:55:28 +0300 | [diff] [blame] | 682 | |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 683 | if (!weston_touch_has_focus_resource(touch)) |
Bryce Harrington | 2c40d1d | 2016-02-02 10:18:48 -0800 | [diff] [blame] | 684 | return; |
| 685 | |
Giulio Camuffo | 61ed7b6 | 2015-07-08 11:55:28 +0300 | [diff] [blame] | 686 | weston_view_from_global_fixed(touch->focus, x, y, &sx, &sy); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 687 | |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 688 | resource_list = &touch->focus_resource_list; |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 689 | serial = wl_display_next_serial(display); |
Alexandros Frantzis | 9448deb | 2017-11-16 18:20:58 +0200 | [diff] [blame] | 690 | msecs = timespec_to_msec(time); |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 691 | wl_resource_for_each(resource, resource_list) |
Alexandros Frantzis | 9448deb | 2017-11-16 18:20:58 +0200 | [diff] [blame] | 692 | wl_touch_send_down(resource, serial, msecs, |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 693 | touch->focus->surface->resource, |
| 694 | touch_id, sx, sy); |
| 695 | } |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 696 | |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 697 | static void |
Alexandros Frantzis | 9448deb | 2017-11-16 18:20:58 +0200 | [diff] [blame] | 698 | default_grab_touch_down(struct weston_touch_grab *grab, |
| 699 | const struct timespec *time, int touch_id, |
| 700 | wl_fixed_t x, wl_fixed_t y) |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 701 | { |
| 702 | weston_touch_send_down(grab->touch, time, touch_id, x, y); |
| 703 | } |
| 704 | |
| 705 | /** Send wl_touch.up events to focused resources. |
| 706 | * |
| 707 | * \param touch The touch where the up events originates from. |
| 708 | * \param time The timestamp of the event |
| 709 | * \param touch_id The touch_id value of the event |
| 710 | * |
| 711 | * For every resource that is currently in focus, send a wl_touch.up event |
| 712 | * with the passed parameters. The focused resources are the wl_touch |
| 713 | * resources of the client which currently has the surface with touch focus. |
| 714 | */ |
| 715 | WL_EXPORT void |
Alexandros Frantzis | 27a51b8 | 2017-11-16 18:20:59 +0200 | [diff] [blame] | 716 | weston_touch_send_up(struct weston_touch *touch, const struct timespec *time, |
| 717 | int touch_id) |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 718 | { |
| 719 | struct wl_display *display = touch->seat->compositor->wl_display; |
| 720 | uint32_t serial; |
| 721 | struct wl_resource *resource; |
| 722 | struct wl_list *resource_list; |
Alexandros Frantzis | 27a51b8 | 2017-11-16 18:20:59 +0200 | [diff] [blame] | 723 | uint32_t msecs; |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 724 | |
| 725 | if (!weston_touch_has_focus_resource(touch)) |
| 726 | return; |
| 727 | |
| 728 | resource_list = &touch->focus_resource_list; |
| 729 | serial = wl_display_next_serial(display); |
Alexandros Frantzis | 27a51b8 | 2017-11-16 18:20:59 +0200 | [diff] [blame] | 730 | msecs = timespec_to_msec(time); |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 731 | wl_resource_for_each(resource, resource_list) |
Alexandros Frantzis | 27a51b8 | 2017-11-16 18:20:59 +0200 | [diff] [blame] | 732 | wl_touch_send_up(resource, serial, msecs, touch_id); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 733 | } |
| 734 | |
Kristian Høgsberg | e329f36 | 2013-05-06 22:19:57 -0400 | [diff] [blame] | 735 | static void |
| 736 | default_grab_touch_up(struct weston_touch_grab *grab, |
Alexandros Frantzis | 27a51b8 | 2017-11-16 18:20:59 +0200 | [diff] [blame] | 737 | const struct timespec *time, int touch_id) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 738 | { |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 739 | weston_touch_send_up(grab->touch, time, touch_id); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 740 | } |
| 741 | |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 742 | /** Send wl_touch.motion events to focused resources. |
| 743 | * |
| 744 | * \param touch The touch where the motion events originates from. |
| 745 | * \param time The timestamp of the event |
| 746 | * \param touch_id The touch_id value of the event |
| 747 | * \param x The x value of the event |
| 748 | * \param y The y value of the event |
| 749 | * |
| 750 | * For every resource that is currently in focus, send a wl_touch.motion event |
| 751 | * with the passed parameters. The focused resources are the wl_touch |
| 752 | * resources of the client which currently has the surface with touch focus. |
| 753 | */ |
| 754 | WL_EXPORT void |
Alexandros Frantzis | 7d2abcf | 2017-11-16 18:21:00 +0200 | [diff] [blame] | 755 | weston_touch_send_motion(struct weston_touch *touch, |
| 756 | const struct timespec *time, int touch_id, |
| 757 | wl_fixed_t x, wl_fixed_t y) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 758 | { |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 759 | struct wl_resource *resource; |
| 760 | struct wl_list *resource_list; |
Giulio Camuffo | 61ed7b6 | 2015-07-08 11:55:28 +0300 | [diff] [blame] | 761 | wl_fixed_t sx, sy; |
Alexandros Frantzis | 7d2abcf | 2017-11-16 18:21:00 +0200 | [diff] [blame] | 762 | uint32_t msecs; |
Giulio Camuffo | 61ed7b6 | 2015-07-08 11:55:28 +0300 | [diff] [blame] | 763 | |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 764 | if (!weston_touch_has_focus_resource(touch)) |
| 765 | return; |
| 766 | |
Giulio Camuffo | 61ed7b6 | 2015-07-08 11:55:28 +0300 | [diff] [blame] | 767 | weston_view_from_global_fixed(touch->focus, x, y, &sx, &sy); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 768 | |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 769 | resource_list = &touch->focus_resource_list; |
Alexandros Frantzis | 7d2abcf | 2017-11-16 18:21:00 +0200 | [diff] [blame] | 770 | msecs = timespec_to_msec(time); |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 771 | wl_resource_for_each(resource, resource_list) { |
Alexandros Frantzis | 7d2abcf | 2017-11-16 18:21:00 +0200 | [diff] [blame] | 772 | wl_touch_send_motion(resource, msecs, |
Kristian Høgsberg | e329f36 | 2013-05-06 22:19:57 -0400 | [diff] [blame] | 773 | touch_id, sx, sy); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 774 | } |
| 775 | } |
| 776 | |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 777 | static void |
Alexandros Frantzis | 7d2abcf | 2017-11-16 18:21:00 +0200 | [diff] [blame] | 778 | default_grab_touch_motion(struct weston_touch_grab *grab, |
| 779 | const struct timespec *time, int touch_id, |
| 780 | wl_fixed_t x, wl_fixed_t y) |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 781 | { |
| 782 | weston_touch_send_motion(grab->touch, time, touch_id, x, y); |
| 783 | } |
| 784 | |
| 785 | |
| 786 | /** Send wl_touch.frame events to focused resources. |
| 787 | * |
| 788 | * \param touch The touch where the frame events originates from. |
| 789 | * |
| 790 | * For every resource that is currently in focus, send a wl_touch.frame event. |
| 791 | * The focused resources are the wl_touch resources of the client which |
| 792 | * currently has the surface with touch focus. |
| 793 | */ |
| 794 | WL_EXPORT void |
| 795 | weston_touch_send_frame(struct weston_touch *touch) |
Jonas Ådahl | 1679f23 | 2014-04-12 09:39:51 +0200 | [diff] [blame] | 796 | { |
| 797 | struct wl_resource *resource; |
| 798 | |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 799 | if (!weston_touch_has_focus_resource(touch)) |
| 800 | return; |
| 801 | |
| 802 | wl_resource_for_each(resource, &touch->focus_resource_list) |
Jonas Ådahl | 1679f23 | 2014-04-12 09:39:51 +0200 | [diff] [blame] | 803 | wl_touch_send_frame(resource); |
| 804 | } |
| 805 | |
| 806 | static void |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 807 | default_grab_touch_frame(struct weston_touch_grab *grab) |
| 808 | { |
| 809 | weston_touch_send_frame(grab->touch); |
| 810 | } |
| 811 | |
| 812 | static void |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 813 | default_grab_touch_cancel(struct weston_touch_grab *grab) |
| 814 | { |
| 815 | } |
| 816 | |
Kristian Høgsberg | e329f36 | 2013-05-06 22:19:57 -0400 | [diff] [blame] | 817 | static const struct weston_touch_grab_interface default_touch_grab_interface = { |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 818 | default_grab_touch_down, |
| 819 | default_grab_touch_up, |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 820 | default_grab_touch_motion, |
Jonas Ådahl | 1679f23 | 2014-04-12 09:39:51 +0200 | [diff] [blame] | 821 | default_grab_touch_frame, |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 822 | default_grab_touch_cancel, |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 823 | }; |
| 824 | |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 825 | /** Check if the keyboard has focused resources. |
| 826 | * |
| 827 | * \param keyboard The keyboard to check for focused resources. |
| 828 | * \return Whether or not this keyboard has focused resources |
| 829 | */ |
| 830 | WL_EXPORT bool |
| 831 | weston_keyboard_has_focus_resource(struct weston_keyboard *keyboard) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 832 | { |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 833 | if (!keyboard->focus) |
| 834 | return false; |
| 835 | |
| 836 | if (wl_list_empty(&keyboard->focus_resource_list)) |
| 837 | return false; |
| 838 | |
| 839 | return true; |
| 840 | } |
| 841 | |
| 842 | /** Send wl_keyboard.key events to focused resources. |
| 843 | * |
| 844 | * \param keyboard The keyboard where the key events originates from. |
| 845 | * \param time The timestamp of the event |
| 846 | * \param key The key value of the event |
| 847 | * \param state The state enum value of the event |
| 848 | * |
| 849 | * For every resource that is currently in focus, send a wl_keyboard.key event |
| 850 | * with the passed parameters. The focused resources are the wl_keyboard |
| 851 | * resources of the client which currently has the surface with keyboard focus. |
| 852 | */ |
| 853 | WL_EXPORT void |
| 854 | weston_keyboard_send_key(struct weston_keyboard *keyboard, |
Alexandros Frantzis | 47e79c8 | 2017-11-16 18:20:57 +0200 | [diff] [blame] | 855 | const struct timespec *time, uint32_t key, |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 856 | enum wl_keyboard_key_state state) |
| 857 | { |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 858 | struct wl_resource *resource; |
Rob Bradford | 880ebc7 | 2013-07-22 17:31:38 +0100 | [diff] [blame] | 859 | struct wl_display *display = keyboard->seat->compositor->wl_display; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 860 | uint32_t serial; |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 861 | struct wl_list *resource_list; |
Alexandros Frantzis | 47e79c8 | 2017-11-16 18:20:57 +0200 | [diff] [blame] | 862 | uint32_t msecs; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 863 | |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 864 | if (!weston_keyboard_has_focus_resource(keyboard)) |
| 865 | return; |
| 866 | |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 867 | resource_list = &keyboard->focus_resource_list; |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 868 | serial = wl_display_next_serial(display); |
Alexandros Frantzis | 47e79c8 | 2017-11-16 18:20:57 +0200 | [diff] [blame] | 869 | msecs = timespec_to_msec(time); |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 870 | wl_resource_for_each(resource, resource_list) |
Alexandros Frantzis | 47e79c8 | 2017-11-16 18:20:57 +0200 | [diff] [blame] | 871 | wl_keyboard_send_key(resource, serial, msecs, key, state); |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 872 | }; |
| 873 | |
| 874 | static void |
| 875 | default_grab_keyboard_key(struct weston_keyboard_grab *grab, |
Alexandros Frantzis | 47e79c8 | 2017-11-16 18:20:57 +0200 | [diff] [blame] | 876 | const struct timespec *time, uint32_t key, |
| 877 | uint32_t state) |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 878 | { |
| 879 | weston_keyboard_send_key(grab->keyboard, time, key, state); |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | static void |
| 883 | send_modifiers_to_resource(struct weston_keyboard *keyboard, |
| 884 | struct wl_resource *resource, |
| 885 | uint32_t serial) |
| 886 | { |
| 887 | wl_keyboard_send_modifiers(resource, |
| 888 | serial, |
| 889 | keyboard->modifiers.mods_depressed, |
| 890 | keyboard->modifiers.mods_latched, |
| 891 | keyboard->modifiers.mods_locked, |
| 892 | keyboard->modifiers.group); |
| 893 | } |
| 894 | |
| 895 | static void |
| 896 | send_modifiers_to_client_in_list(struct wl_client *client, |
| 897 | struct wl_list *list, |
| 898 | uint32_t serial, |
| 899 | struct weston_keyboard *keyboard) |
| 900 | { |
| 901 | struct wl_resource *resource; |
| 902 | |
| 903 | wl_resource_for_each(resource, list) { |
| 904 | if (wl_resource_get_client(resource) == client) |
| 905 | send_modifiers_to_resource(keyboard, |
| 906 | resource, |
| 907 | serial); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 908 | } |
| 909 | } |
| 910 | |
Jonas Ådahl | 2cbf293 | 2015-07-22 12:05:38 +0800 | [diff] [blame] | 911 | static struct weston_pointer_client * |
| 912 | find_pointer_client_for_surface(struct weston_pointer *pointer, |
| 913 | struct weston_surface *surface) |
| 914 | { |
| 915 | struct wl_client *client; |
| 916 | |
| 917 | if (!surface) |
| 918 | return NULL; |
| 919 | |
| 920 | if (!surface->resource) |
| 921 | return NULL; |
| 922 | |
| 923 | client = wl_resource_get_client(surface->resource); |
| 924 | return weston_pointer_get_pointer_client(pointer, client); |
| 925 | } |
| 926 | |
| 927 | static struct weston_pointer_client * |
| 928 | find_pointer_client_for_view(struct weston_pointer *pointer, struct weston_view *view) |
| 929 | { |
| 930 | if (!view) |
| 931 | return NULL; |
| 932 | |
| 933 | return find_pointer_client_for_surface(pointer, view->surface); |
| 934 | } |
| 935 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 936 | static struct wl_resource * |
Kristian Høgsberg | fe7aa90 | 2013-05-08 09:54:37 -0400 | [diff] [blame] | 937 | find_resource_for_surface(struct wl_list *list, struct weston_surface *surface) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 938 | { |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 939 | if (!surface) |
| 940 | return NULL; |
| 941 | |
Jason Ekstrand | 44a3863 | 2013-06-14 10:08:00 -0500 | [diff] [blame] | 942 | if (!surface->resource) |
| 943 | return NULL; |
Stefan Schmidt | fda2652 | 2013-09-17 10:54:09 +0100 | [diff] [blame] | 944 | |
Jason Ekstrand | 44a3863 | 2013-06-14 10:08:00 -0500 | [diff] [blame] | 945 | return wl_resource_find_for_client(list, wl_resource_get_client(surface->resource)); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 946 | } |
| 947 | |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 948 | /** Send wl_keyboard.modifiers events to focused resources and pointer |
| 949 | * focused resources. |
| 950 | * |
| 951 | * \param keyboard The keyboard where the modifiers events originates from. |
| 952 | * \param serial The serial of the event |
| 953 | * \param mods_depressed The mods_depressed value of the event |
| 954 | * \param mods_latched The mods_latched value of the event |
| 955 | * \param mods_locked The mods_locked value of the event |
| 956 | * \param group The group value of the event |
| 957 | * |
| 958 | * For every resource that is currently in focus, send a wl_keyboard.modifiers |
| 959 | * event with the passed parameters. The focused resources are the wl_keyboard |
| 960 | * resources of the client which currently has the surface with keyboard focus. |
| 961 | * This also sends wl_keyboard.modifiers events to the wl_keyboard resources of |
| 962 | * the client having pointer focus (if different from the keyboard focus client). |
| 963 | */ |
| 964 | WL_EXPORT void |
| 965 | weston_keyboard_send_modifiers(struct weston_keyboard *keyboard, |
| 966 | uint32_t serial, uint32_t mods_depressed, |
| 967 | uint32_t mods_latched, |
| 968 | uint32_t mods_locked, uint32_t group) |
| 969 | { |
| 970 | struct weston_pointer *pointer = |
| 971 | weston_seat_get_pointer(keyboard->seat); |
| 972 | |
| 973 | if (weston_keyboard_has_focus_resource(keyboard)) { |
| 974 | struct wl_list *resource_list; |
| 975 | struct wl_resource *resource; |
| 976 | |
| 977 | resource_list = &keyboard->focus_resource_list; |
| 978 | wl_resource_for_each(resource, resource_list) { |
| 979 | wl_keyboard_send_modifiers(resource, serial, |
| 980 | mods_depressed, mods_latched, |
| 981 | mods_locked, group); |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | if (pointer && pointer->focus && pointer->focus->surface->resource && |
| 986 | pointer->focus->surface != keyboard->focus) { |
| 987 | struct wl_client *pointer_client = |
| 988 | wl_resource_get_client(pointer->focus->surface->resource); |
| 989 | |
| 990 | send_modifiers_to_client_in_list(pointer_client, |
| 991 | &keyboard->resource_list, |
| 992 | serial, |
| 993 | keyboard); |
| 994 | } |
| 995 | } |
| 996 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 997 | static void |
Kristian Høgsberg | b27901c | 2013-10-28 15:32:02 -0700 | [diff] [blame] | 998 | default_grab_keyboard_modifiers(struct weston_keyboard_grab *grab, |
| 999 | uint32_t serial, uint32_t mods_depressed, |
| 1000 | uint32_t mods_latched, |
| 1001 | uint32_t mods_locked, uint32_t group) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1002 | { |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 1003 | weston_keyboard_send_modifiers(grab->keyboard, serial, mods_depressed, |
| 1004 | mods_latched, mods_locked, group); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1005 | } |
| 1006 | |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 1007 | static void |
| 1008 | default_grab_keyboard_cancel(struct weston_keyboard_grab *grab) |
| 1009 | { |
| 1010 | } |
| 1011 | |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 1012 | static const struct weston_keyboard_grab_interface |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1013 | default_keyboard_grab_interface = { |
Kristian Høgsberg | b27901c | 2013-10-28 15:32:02 -0700 | [diff] [blame] | 1014 | default_grab_keyboard_key, |
| 1015 | default_grab_keyboard_modifiers, |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 1016 | default_grab_keyboard_cancel, |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1017 | }; |
| 1018 | |
Kristian Høgsberg | 195b869 | 2013-05-08 15:02:05 -0400 | [diff] [blame] | 1019 | static void |
| 1020 | pointer_unmap_sprite(struct weston_pointer *pointer) |
| 1021 | { |
Pekka Paalanen | c557ff7 | 2014-11-12 16:42:52 +0200 | [diff] [blame] | 1022 | struct weston_surface *surface = pointer->sprite->surface; |
| 1023 | |
| 1024 | if (weston_surface_is_mapped(surface)) |
| 1025 | weston_surface_unmap(surface); |
Kristian Høgsberg | 195b869 | 2013-05-08 15:02:05 -0400 | [diff] [blame] | 1026 | |
| 1027 | wl_list_remove(&pointer->sprite_destroy_listener.link); |
Quentin Glidic | 2edc3d5 | 2016-08-12 10:41:33 +0200 | [diff] [blame] | 1028 | surface->committed = NULL; |
| 1029 | surface->committed_private = NULL; |
Pekka Paalanen | 8274d90 | 2014-08-06 19:36:51 +0300 | [diff] [blame] | 1030 | weston_surface_set_label_func(surface, NULL); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1031 | weston_view_destroy(pointer->sprite); |
Kristian Høgsberg | 195b869 | 2013-05-08 15:02:05 -0400 | [diff] [blame] | 1032 | pointer->sprite = NULL; |
| 1033 | } |
| 1034 | |
| 1035 | static void |
| 1036 | pointer_handle_sprite_destroy(struct wl_listener *listener, void *data) |
| 1037 | { |
| 1038 | struct weston_pointer *pointer = |
| 1039 | container_of(listener, struct weston_pointer, |
| 1040 | sprite_destroy_listener); |
| 1041 | |
| 1042 | pointer->sprite = NULL; |
| 1043 | } |
| 1044 | |
Jonas Ådahl | 3e12e63 | 2013-12-02 22:05:05 +0100 | [diff] [blame] | 1045 | static void |
| 1046 | weston_pointer_reset_state(struct weston_pointer *pointer) |
| 1047 | { |
| 1048 | pointer->button_count = 0; |
| 1049 | } |
| 1050 | |
Ander Conselvan de Oliveira | f84327a | 2014-01-29 18:47:51 +0200 | [diff] [blame] | 1051 | static void |
| 1052 | weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data); |
| 1053 | |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 1054 | WL_EXPORT struct weston_pointer * |
Giulio Camuffo | cdb4d29 | 2013-11-14 23:42:53 +0100 | [diff] [blame] | 1055 | weston_pointer_create(struct weston_seat *seat) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1056 | { |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 1057 | struct weston_pointer *pointer; |
| 1058 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 1059 | pointer = zalloc(sizeof *pointer); |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 1060 | if (pointer == NULL) |
| 1061 | return NULL; |
| 1062 | |
Jonas Ådahl | 2cbf293 | 2015-07-22 12:05:38 +0800 | [diff] [blame] | 1063 | wl_list_init(&pointer->pointer_clients); |
Giulio Camuffo | cdb4d29 | 2013-11-14 23:42:53 +0100 | [diff] [blame] | 1064 | weston_pointer_set_default_grab(pointer, |
| 1065 | seat->compositor->default_pointer_grab); |
Giulio Camuffo | 576fe2a | 2013-11-20 18:00:24 +0100 | [diff] [blame] | 1066 | wl_list_init(&pointer->focus_resource_listener.link); |
| 1067 | pointer->focus_resource_listener.notify = pointer_focus_resource_destroyed; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1068 | pointer->default_grab.pointer = pointer; |
| 1069 | pointer->grab = &pointer->default_grab; |
Giulio Camuffo | 6fcb378 | 2013-11-14 23:42:50 +0100 | [diff] [blame] | 1070 | wl_signal_init(&pointer->motion_signal); |
Emilio Pozuelo Monfort | aa7a476 | 2013-11-19 11:37:15 +0100 | [diff] [blame] | 1071 | wl_signal_init(&pointer->focus_signal); |
Giulio Camuffo | 576fe2a | 2013-11-20 18:00:24 +0100 | [diff] [blame] | 1072 | wl_list_init(&pointer->focus_view_listener.link); |
Jonas Ådahl | 3eb4ddd | 2016-07-22 17:52:58 +0800 | [diff] [blame] | 1073 | wl_signal_init(&pointer->destroy_signal); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1074 | |
Kristian Høgsberg | 195b869 | 2013-05-08 15:02:05 -0400 | [diff] [blame] | 1075 | pointer->sprite_destroy_listener.notify = pointer_handle_sprite_destroy; |
| 1076 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1077 | /* FIXME: Pick better co-ords. */ |
| 1078 | pointer->x = wl_fixed_from_int(100); |
| 1079 | pointer->y = wl_fixed_from_int(100); |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 1080 | |
Ander Conselvan de Oliveira | f84327a | 2014-01-29 18:47:51 +0200 | [diff] [blame] | 1081 | pointer->output_destroy_listener.notify = |
| 1082 | weston_pointer_handle_output_destroy; |
| 1083 | wl_signal_add(&seat->compositor->output_destroyed_signal, |
| 1084 | &pointer->output_destroy_listener); |
| 1085 | |
Derek Foreman | f9318d1 | 2015-05-11 15:40:11 -0500 | [diff] [blame] | 1086 | pointer->sx = wl_fixed_from_int(-1000000); |
| 1087 | pointer->sy = wl_fixed_from_int(-1000000); |
| 1088 | |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 1089 | return pointer; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | WL_EXPORT void |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 1093 | weston_pointer_destroy(struct weston_pointer *pointer) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1094 | { |
Jonas Ådahl | 3eb4ddd | 2016-07-22 17:52:58 +0800 | [diff] [blame] | 1095 | wl_signal_emit(&pointer->destroy_signal, pointer); |
| 1096 | |
Kristian Høgsberg | 195b869 | 2013-05-08 15:02:05 -0400 | [diff] [blame] | 1097 | if (pointer->sprite) |
| 1098 | pointer_unmap_sprite(pointer); |
| 1099 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1100 | /* XXX: What about pointer->resource_list? */ |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1101 | |
Giulio Camuffo | 576fe2a | 2013-11-20 18:00:24 +0100 | [diff] [blame] | 1102 | wl_list_remove(&pointer->focus_resource_listener.link); |
| 1103 | wl_list_remove(&pointer->focus_view_listener.link); |
Ander Conselvan de Oliveira | f84327a | 2014-01-29 18:47:51 +0200 | [diff] [blame] | 1104 | wl_list_remove(&pointer->output_destroy_listener.link); |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 1105 | free(pointer); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1106 | } |
| 1107 | |
Giulio Camuffo | cdb4d29 | 2013-11-14 23:42:53 +0100 | [diff] [blame] | 1108 | void |
| 1109 | weston_pointer_set_default_grab(struct weston_pointer *pointer, |
| 1110 | const struct weston_pointer_grab_interface *interface) |
| 1111 | { |
| 1112 | if (interface) |
| 1113 | pointer->default_grab.interface = interface; |
| 1114 | else |
| 1115 | pointer->default_grab.interface = |
| 1116 | &default_pointer_grab_interface; |
| 1117 | } |
| 1118 | |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 1119 | WL_EXPORT struct weston_keyboard * |
| 1120 | weston_keyboard_create(void) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1121 | { |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 1122 | struct weston_keyboard *keyboard; |
| 1123 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 1124 | keyboard = zalloc(sizeof *keyboard); |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 1125 | if (keyboard == NULL) |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1126 | return NULL; |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 1127 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1128 | wl_list_init(&keyboard->resource_list); |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1129 | wl_list_init(&keyboard->focus_resource_list); |
Giulio Camuffo | 576fe2a | 2013-11-20 18:00:24 +0100 | [diff] [blame] | 1130 | wl_list_init(&keyboard->focus_resource_listener.link); |
| 1131 | keyboard->focus_resource_listener.notify = keyboard_focus_resource_destroyed; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1132 | wl_array_init(&keyboard->keys); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1133 | keyboard->default_grab.interface = &default_keyboard_grab_interface; |
| 1134 | keyboard->default_grab.keyboard = keyboard; |
| 1135 | keyboard->grab = &keyboard->default_grab; |
| 1136 | wl_signal_init(&keyboard->focus_signal); |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 1137 | |
| 1138 | return keyboard; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1139 | } |
| 1140 | |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1141 | static void |
| 1142 | weston_xkb_info_destroy(struct weston_xkb_info *xkb_info); |
| 1143 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1144 | WL_EXPORT void |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 1145 | weston_keyboard_destroy(struct weston_keyboard *keyboard) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1146 | { |
Alexandros Frantzis | 7a314d6 | 2018-01-26 18:47:56 +0200 | [diff] [blame^] | 1147 | struct wl_resource *resource; |
| 1148 | |
| 1149 | wl_resource_for_each(resource, &keyboard->resource_list) { |
| 1150 | wl_resource_set_user_data(resource, NULL); |
| 1151 | } |
| 1152 | |
| 1153 | wl_resource_for_each(resource, &keyboard->focus_resource_list) { |
| 1154 | wl_resource_set_user_data(resource, NULL); |
| 1155 | } |
| 1156 | |
| 1157 | wl_list_remove(&keyboard->resource_list); |
| 1158 | wl_list_remove(&keyboard->focus_resource_list); |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1159 | |
Derek Foreman | 185d158 | 2017-06-28 11:17:23 -0500 | [diff] [blame] | 1160 | xkb_state_unref(keyboard->xkb_state.state); |
| 1161 | if (keyboard->xkb_info) |
| 1162 | weston_xkb_info_destroy(keyboard->xkb_info); |
| 1163 | xkb_keymap_unref(keyboard->pending_keymap); |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1164 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1165 | wl_array_release(&keyboard->keys); |
Giulio Camuffo | 576fe2a | 2013-11-20 18:00:24 +0100 | [diff] [blame] | 1166 | wl_list_remove(&keyboard->focus_resource_listener.link); |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 1167 | free(keyboard); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1168 | } |
| 1169 | |
Jonas Ådahl | cbfa7f7 | 2013-12-02 22:05:04 +0100 | [diff] [blame] | 1170 | static void |
| 1171 | weston_touch_reset_state(struct weston_touch *touch) |
| 1172 | { |
| 1173 | touch->num_tp = 0; |
| 1174 | } |
| 1175 | |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 1176 | WL_EXPORT struct weston_touch * |
| 1177 | weston_touch_create(void) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1178 | { |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 1179 | struct weston_touch *touch; |
| 1180 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 1181 | touch = zalloc(sizeof *touch); |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 1182 | if (touch == NULL) |
| 1183 | return NULL; |
| 1184 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1185 | wl_list_init(&touch->resource_list); |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1186 | wl_list_init(&touch->focus_resource_list); |
Giulio Camuffo | 576fe2a | 2013-11-20 18:00:24 +0100 | [diff] [blame] | 1187 | wl_list_init(&touch->focus_view_listener.link); |
| 1188 | touch->focus_view_listener.notify = touch_focus_view_destroyed; |
| 1189 | wl_list_init(&touch->focus_resource_listener.link); |
| 1190 | touch->focus_resource_listener.notify = touch_focus_resource_destroyed; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1191 | touch->default_grab.interface = &default_touch_grab_interface; |
| 1192 | touch->default_grab.touch = touch; |
| 1193 | touch->grab = &touch->default_grab; |
| 1194 | wl_signal_init(&touch->focus_signal); |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 1195 | |
| 1196 | return touch; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | WL_EXPORT void |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 1200 | weston_touch_destroy(struct weston_touch *touch) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1201 | { |
| 1202 | /* XXX: What about touch->resource_list? */ |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1203 | |
Giulio Camuffo | 576fe2a | 2013-11-20 18:00:24 +0100 | [diff] [blame] | 1204 | wl_list_remove(&touch->focus_view_listener.link); |
| 1205 | wl_list_remove(&touch->focus_resource_listener.link); |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 1206 | free(touch); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1207 | } |
| 1208 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1209 | static void |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1210 | seat_send_updated_caps(struct weston_seat *seat) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1211 | { |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1212 | enum wl_seat_capability caps = 0; |
Rob Bradford | 6e737f5 | 2013-09-06 17:48:19 +0100 | [diff] [blame] | 1213 | struct wl_resource *resource; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1214 | |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 1215 | if (seat->pointer_device_count > 0) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1216 | caps |= WL_SEAT_CAPABILITY_POINTER; |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 1217 | if (seat->keyboard_device_count > 0) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1218 | caps |= WL_SEAT_CAPABILITY_KEYBOARD; |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 1219 | if (seat->touch_device_count > 0) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1220 | caps |= WL_SEAT_CAPABILITY_TOUCH; |
| 1221 | |
Rob Bradford | 6e737f5 | 2013-09-06 17:48:19 +0100 | [diff] [blame] | 1222 | wl_resource_for_each(resource, &seat->base_resource_list) { |
| 1223 | wl_seat_send_capabilities(resource, caps); |
Jason Ekstrand | 44a3863 | 2013-06-14 10:08:00 -0500 | [diff] [blame] | 1224 | } |
Jason Ekstrand | a4ab542 | 2014-04-02 19:53:45 -0500 | [diff] [blame] | 1225 | wl_signal_emit(&seat->updated_caps_signal, seat); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1226 | } |
| 1227 | |
Derek Foreman | f9318d1 | 2015-05-11 15:40:11 -0500 | [diff] [blame] | 1228 | |
| 1229 | /** Clear the pointer focus |
| 1230 | * |
| 1231 | * \param pointer the pointer to clear focus for. |
| 1232 | * |
| 1233 | * This can be used to unset pointer focus and set the co-ordinates to the |
| 1234 | * arbitrary values we use for the no focus case. |
| 1235 | * |
| 1236 | * There's no requirement to use this function. For example, passing the |
| 1237 | * results of a weston_compositor_pick_view() directly to |
| 1238 | * weston_pointer_set_focus() will do the right thing when no view is found. |
| 1239 | */ |
| 1240 | WL_EXPORT void |
| 1241 | weston_pointer_clear_focus(struct weston_pointer *pointer) |
| 1242 | { |
| 1243 | weston_pointer_set_focus(pointer, NULL, |
| 1244 | wl_fixed_from_int(-1000000), |
| 1245 | wl_fixed_from_int(-1000000)); |
| 1246 | } |
| 1247 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1248 | WL_EXPORT void |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 1249 | weston_pointer_set_focus(struct weston_pointer *pointer, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1250 | struct weston_view *view, |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 1251 | wl_fixed_t sx, wl_fixed_t sy) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1252 | { |
Jonas Ådahl | 2cbf293 | 2015-07-22 12:05:38 +0800 | [diff] [blame] | 1253 | struct weston_pointer_client *pointer_client; |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 1254 | struct weston_keyboard *kbd = weston_seat_get_keyboard(pointer->seat); |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1255 | struct wl_resource *resource; |
Jonas Ådahl | 2cbf293 | 2015-07-22 12:05:38 +0800 | [diff] [blame] | 1256 | struct wl_resource *surface_resource; |
Rob Bradford | 880ebc7 | 2013-07-22 17:31:38 +0100 | [diff] [blame] | 1257 | struct wl_display *display = pointer->seat->compositor->wl_display; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1258 | uint32_t serial; |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1259 | struct wl_list *focus_resource_list; |
Kristian Høgsberg | db1fccb | 2014-02-05 17:14:42 -0800 | [diff] [blame] | 1260 | int refocus = 0; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1261 | |
| 1262 | if ((!pointer->focus && view) || |
| 1263 | (pointer->focus && !view) || |
Kristian Høgsberg | db1fccb | 2014-02-05 17:14:42 -0800 | [diff] [blame] | 1264 | (pointer->focus && pointer->focus->surface != view->surface) || |
| 1265 | pointer->sx != sx || pointer->sy != sy) |
| 1266 | refocus = 1; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1267 | |
Jonas Ådahl | 2cbf293 | 2015-07-22 12:05:38 +0800 | [diff] [blame] | 1268 | if (pointer->focus_client && refocus) { |
| 1269 | focus_resource_list = &pointer->focus_client->pointer_resources; |
| 1270 | if (!wl_list_empty(focus_resource_list)) { |
| 1271 | serial = wl_display_next_serial(display); |
| 1272 | surface_resource = pointer->focus->surface->resource; |
| 1273 | wl_resource_for_each(resource, focus_resource_list) { |
| 1274 | wl_pointer_send_leave(resource, serial, |
| 1275 | surface_resource); |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 1276 | pointer_send_frame(resource); |
Jonas Ådahl | 2cbf293 | 2015-07-22 12:05:38 +0800 | [diff] [blame] | 1277 | } |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1278 | } |
| 1279 | |
Jonas Ådahl | 2cbf293 | 2015-07-22 12:05:38 +0800 | [diff] [blame] | 1280 | pointer->focus_client = NULL; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1281 | } |
| 1282 | |
Jonas Ådahl | 2cbf293 | 2015-07-22 12:05:38 +0800 | [diff] [blame] | 1283 | pointer_client = find_pointer_client_for_view(pointer, view); |
| 1284 | if (pointer_client && refocus) { |
| 1285 | struct wl_client *surface_client = pointer_client->client; |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1286 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1287 | serial = wl_display_next_serial(display); |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1288 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1289 | if (kbd && kbd->focus != view->surface) |
Kristian Høgsberg | cb406f1 | 2013-10-09 10:54:03 -0700 | [diff] [blame] | 1290 | send_modifiers_to_client_in_list(surface_client, |
| 1291 | &kbd->resource_list, |
| 1292 | serial, |
| 1293 | kbd); |
| 1294 | |
Jonas Ådahl | 2cbf293 | 2015-07-22 12:05:38 +0800 | [diff] [blame] | 1295 | pointer->focus_client = pointer_client; |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1296 | |
Jonas Ådahl | 2cbf293 | 2015-07-22 12:05:38 +0800 | [diff] [blame] | 1297 | focus_resource_list = &pointer->focus_client->pointer_resources; |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1298 | wl_resource_for_each(resource, focus_resource_list) { |
| 1299 | wl_pointer_send_enter(resource, |
| 1300 | serial, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1301 | view->surface->resource, |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1302 | sx, sy); |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 1303 | pointer_send_frame(resource); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1304 | } |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1305 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1306 | pointer->focus_serial = serial; |
| 1307 | } |
| 1308 | |
Giulio Camuffo | 65a07f8 | 2013-12-06 12:46:27 +0100 | [diff] [blame] | 1309 | wl_list_remove(&pointer->focus_view_listener.link); |
| 1310 | wl_list_init(&pointer->focus_view_listener.link); |
| 1311 | wl_list_remove(&pointer->focus_resource_listener.link); |
| 1312 | wl_list_init(&pointer->focus_resource_listener.link); |
Emilio Pozuelo Monfort | aa7a476 | 2013-11-19 11:37:15 +0100 | [diff] [blame] | 1313 | if (view) |
Giulio Camuffo | 576fe2a | 2013-11-20 18:00:24 +0100 | [diff] [blame] | 1314 | wl_signal_add(&view->destroy_signal, &pointer->focus_view_listener); |
Giulio Camuffo | 65a07f8 | 2013-12-06 12:46:27 +0100 | [diff] [blame] | 1315 | if (view && view->surface->resource) |
| 1316 | wl_resource_add_destroy_listener(view->surface->resource, |
| 1317 | &pointer->focus_resource_listener); |
| 1318 | |
| 1319 | pointer->focus = view; |
| 1320 | pointer->focus_view_listener.notify = pointer_focus_view_destroyed; |
Kristian Høgsberg | db1fccb | 2014-02-05 17:14:42 -0800 | [diff] [blame] | 1321 | pointer->sx = sx; |
| 1322 | pointer->sy = sy; |
| 1323 | |
Derek Foreman | f9318d1 | 2015-05-11 15:40:11 -0500 | [diff] [blame] | 1324 | assert(view || sx == wl_fixed_from_int(-1000000)); |
| 1325 | assert(view || sy == wl_fixed_from_int(-1000000)); |
| 1326 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1327 | wl_signal_emit(&pointer->focus_signal, pointer); |
| 1328 | } |
| 1329 | |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1330 | static void |
| 1331 | send_enter_to_resource_list(struct wl_list *list, |
| 1332 | struct weston_keyboard *keyboard, |
| 1333 | struct weston_surface *surface, |
| 1334 | uint32_t serial) |
| 1335 | { |
| 1336 | struct wl_resource *resource; |
| 1337 | |
| 1338 | wl_resource_for_each(resource, list) { |
| 1339 | send_modifiers_to_resource(keyboard, resource, serial); |
| 1340 | wl_keyboard_send_enter(resource, serial, |
| 1341 | surface->resource, |
| 1342 | &keyboard->keys); |
| 1343 | } |
| 1344 | } |
| 1345 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1346 | WL_EXPORT void |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 1347 | weston_keyboard_set_focus(struct weston_keyboard *keyboard, |
Kristian Høgsberg | fe7aa90 | 2013-05-08 09:54:37 -0400 | [diff] [blame] | 1348 | struct weston_surface *surface) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1349 | { |
Quentin Glidic | 85d5554 | 2017-07-21 14:02:40 +0200 | [diff] [blame] | 1350 | struct weston_seat *seat = keyboard->seat; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1351 | struct wl_resource *resource; |
Rob Bradford | 880ebc7 | 2013-07-22 17:31:38 +0100 | [diff] [blame] | 1352 | struct wl_display *display = keyboard->seat->compositor->wl_display; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1353 | uint32_t serial; |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1354 | struct wl_list *focus_resource_list; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1355 | |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1356 | focus_resource_list = &keyboard->focus_resource_list; |
| 1357 | |
| 1358 | if (!wl_list_empty(focus_resource_list) && keyboard->focus != surface) { |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1359 | serial = wl_display_next_serial(display); |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1360 | wl_resource_for_each(resource, focus_resource_list) { |
| 1361 | wl_keyboard_send_leave(resource, serial, |
| 1362 | keyboard->focus->resource); |
| 1363 | } |
| 1364 | move_resources(&keyboard->resource_list, focus_resource_list); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1365 | } |
| 1366 | |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1367 | if (find_resource_for_surface(&keyboard->resource_list, surface) && |
| 1368 | keyboard->focus != surface) { |
| 1369 | struct wl_client *surface_client = |
| 1370 | wl_resource_get_client(surface->resource); |
| 1371 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1372 | serial = wl_display_next_serial(display); |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1373 | |
| 1374 | move_resources_for_client(focus_resource_list, |
| 1375 | &keyboard->resource_list, |
| 1376 | surface_client); |
| 1377 | send_enter_to_resource_list(focus_resource_list, |
| 1378 | keyboard, |
| 1379 | surface, |
| 1380 | serial); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1381 | keyboard->focus_serial = serial; |
Giulio Camuffo | 65a07f8 | 2013-12-06 12:46:27 +0100 | [diff] [blame] | 1382 | } |
| 1383 | |
Quentin Glidic | 85d5554 | 2017-07-21 14:02:40 +0200 | [diff] [blame] | 1384 | if (seat->saved_kbd_focus) { |
| 1385 | wl_list_remove(&seat->saved_kbd_focus_listener.link); |
| 1386 | seat->saved_kbd_focus = NULL; |
| 1387 | } |
| 1388 | |
Giulio Camuffo | 65a07f8 | 2013-12-06 12:46:27 +0100 | [diff] [blame] | 1389 | wl_list_remove(&keyboard->focus_resource_listener.link); |
| 1390 | wl_list_init(&keyboard->focus_resource_listener.link); |
| 1391 | if (surface && surface->resource) |
Giulio Camuffo | 576fe2a | 2013-11-20 18:00:24 +0100 | [diff] [blame] | 1392 | wl_resource_add_destroy_listener(surface->resource, |
| 1393 | &keyboard->focus_resource_listener); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1394 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1395 | keyboard->focus = surface; |
| 1396 | wl_signal_emit(&keyboard->focus_signal, keyboard); |
| 1397 | } |
| 1398 | |
Giulio Camuffo | a20ca81 | 2014-11-22 11:16:56 +0200 | [diff] [blame] | 1399 | /* Users of this function must manually manage the keyboard focus */ |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1400 | WL_EXPORT void |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 1401 | weston_keyboard_start_grab(struct weston_keyboard *keyboard, |
| 1402 | struct weston_keyboard_grab *grab) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1403 | { |
| 1404 | keyboard->grab = grab; |
| 1405 | grab->keyboard = keyboard; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1406 | } |
| 1407 | |
| 1408 | WL_EXPORT void |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 1409 | weston_keyboard_end_grab(struct weston_keyboard *keyboard) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1410 | { |
| 1411 | keyboard->grab = &keyboard->default_grab; |
| 1412 | } |
| 1413 | |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 1414 | static void |
| 1415 | weston_keyboard_cancel_grab(struct weston_keyboard *keyboard) |
| 1416 | { |
| 1417 | keyboard->grab->interface->cancel(keyboard->grab); |
| 1418 | } |
| 1419 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1420 | WL_EXPORT void |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 1421 | weston_pointer_start_grab(struct weston_pointer *pointer, |
| 1422 | struct weston_pointer_grab *grab) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1423 | { |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1424 | pointer->grab = grab; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1425 | grab->pointer = pointer; |
Kristian Høgsberg | da751b8 | 2013-07-04 00:58:07 -0400 | [diff] [blame] | 1426 | pointer->grab->interface->focus(pointer->grab); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | WL_EXPORT void |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 1430 | weston_pointer_end_grab(struct weston_pointer *pointer) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1431 | { |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1432 | pointer->grab = &pointer->default_grab; |
Kristian Høgsberg | da751b8 | 2013-07-04 00:58:07 -0400 | [diff] [blame] | 1433 | pointer->grab->interface->focus(pointer->grab); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1434 | } |
| 1435 | |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 1436 | static void |
| 1437 | weston_pointer_cancel_grab(struct weston_pointer *pointer) |
| 1438 | { |
| 1439 | pointer->grab->interface->cancel(pointer->grab); |
| 1440 | } |
| 1441 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1442 | WL_EXPORT void |
Kristian Høgsberg | e329f36 | 2013-05-06 22:19:57 -0400 | [diff] [blame] | 1443 | weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1444 | { |
| 1445 | touch->grab = grab; |
| 1446 | grab->touch = touch; |
| 1447 | } |
| 1448 | |
| 1449 | WL_EXPORT void |
Kristian Høgsberg | e329f36 | 2013-05-06 22:19:57 -0400 | [diff] [blame] | 1450 | weston_touch_end_grab(struct weston_touch *touch) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1451 | { |
| 1452 | touch->grab = &touch->default_grab; |
| 1453 | } |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1454 | |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 1455 | static void |
| 1456 | weston_touch_cancel_grab(struct weston_touch *touch) |
| 1457 | { |
| 1458 | touch->grab->interface->cancel(touch->grab); |
| 1459 | } |
| 1460 | |
Ander Conselvan de Oliveira | 54e90c7 | 2013-12-13 22:10:56 +0200 | [diff] [blame] | 1461 | static void |
| 1462 | weston_pointer_clamp_for_output(struct weston_pointer *pointer, |
| 1463 | struct weston_output *output, |
| 1464 | wl_fixed_t *fx, wl_fixed_t *fy) |
| 1465 | { |
| 1466 | int x, y; |
| 1467 | |
| 1468 | x = wl_fixed_to_int(*fx); |
| 1469 | y = wl_fixed_to_int(*fy); |
| 1470 | |
| 1471 | if (x < output->x) |
| 1472 | *fx = wl_fixed_from_int(output->x); |
| 1473 | else if (x >= output->x + output->width) |
| 1474 | *fx = wl_fixed_from_int(output->x + |
| 1475 | output->width - 1); |
| 1476 | if (y < output->y) |
| 1477 | *fy = wl_fixed_from_int(output->y); |
| 1478 | else if (y >= output->y + output->height) |
| 1479 | *fy = wl_fixed_from_int(output->y + |
| 1480 | output->height - 1); |
| 1481 | } |
| 1482 | |
Rob Bradford | 806d8c0 | 2013-06-25 18:56:41 +0100 | [diff] [blame] | 1483 | WL_EXPORT void |
| 1484 | weston_pointer_clamp(struct weston_pointer *pointer, wl_fixed_t *fx, wl_fixed_t *fy) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1485 | { |
Rob Bradford | 806d8c0 | 2013-06-25 18:56:41 +0100 | [diff] [blame] | 1486 | struct weston_compositor *ec = pointer->seat->compositor; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1487 | struct weston_output *output, *prev = NULL; |
| 1488 | int x, y, old_x, old_y, valid = 0; |
| 1489 | |
| 1490 | x = wl_fixed_to_int(*fx); |
| 1491 | y = wl_fixed_to_int(*fy); |
Rob Bradford | 806d8c0 | 2013-06-25 18:56:41 +0100 | [diff] [blame] | 1492 | old_x = wl_fixed_to_int(pointer->x); |
| 1493 | old_y = wl_fixed_to_int(pointer->y); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1494 | |
| 1495 | wl_list_for_each(output, &ec->output_list, link) { |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 1496 | if (pointer->seat->output && pointer->seat->output != output) |
| 1497 | continue; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1498 | if (pixman_region32_contains_point(&output->region, |
| 1499 | x, y, NULL)) |
| 1500 | valid = 1; |
| 1501 | if (pixman_region32_contains_point(&output->region, |
| 1502 | old_x, old_y, NULL)) |
| 1503 | prev = output; |
| 1504 | } |
| 1505 | |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 1506 | if (!prev) |
| 1507 | prev = pointer->seat->output; |
| 1508 | |
Ander Conselvan de Oliveira | 54e90c7 | 2013-12-13 22:10:56 +0200 | [diff] [blame] | 1509 | if (prev && !valid) |
| 1510 | weston_pointer_clamp_for_output(pointer, prev, fx, fy); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1511 | } |
| 1512 | |
Jonas Ådahl | d251010 | 2014-10-05 21:39:14 +0200 | [diff] [blame] | 1513 | static void |
| 1514 | weston_pointer_move_to(struct weston_pointer *pointer, |
| 1515 | wl_fixed_t x, wl_fixed_t y) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1516 | { |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1517 | int32_t ix, iy; |
| 1518 | |
Rob Bradford | 806d8c0 | 2013-06-25 18:56:41 +0100 | [diff] [blame] | 1519 | weston_pointer_clamp (pointer, &x, &y); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1520 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1521 | pointer->x = x; |
| 1522 | pointer->y = y; |
| 1523 | |
| 1524 | ix = wl_fixed_to_int(x); |
| 1525 | iy = wl_fixed_to_int(y); |
| 1526 | |
Kristian Høgsberg | 195b869 | 2013-05-08 15:02:05 -0400 | [diff] [blame] | 1527 | if (pointer->sprite) { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1528 | weston_view_set_position(pointer->sprite, |
| 1529 | ix - pointer->hotspot_x, |
| 1530 | iy - pointer->hotspot_y); |
| 1531 | weston_view_schedule_repaint(pointer->sprite); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1532 | } |
Giulio Camuffo | 6fcb378 | 2013-11-14 23:42:50 +0100 | [diff] [blame] | 1533 | |
Giulio Camuffo | 1959ab8 | 2013-11-14 23:42:52 +0100 | [diff] [blame] | 1534 | pointer->grab->interface->focus(pointer->grab); |
Giulio Camuffo | 6fcb378 | 2013-11-14 23:42:50 +0100 | [diff] [blame] | 1535 | wl_signal_emit(&pointer->motion_signal, pointer); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1536 | } |
| 1537 | |
Jonas Ådahl | d251010 | 2014-10-05 21:39:14 +0200 | [diff] [blame] | 1538 | WL_EXPORT void |
Jonas Ådahl | d251010 | 2014-10-05 21:39:14 +0200 | [diff] [blame] | 1539 | weston_pointer_move(struct weston_pointer *pointer, |
| 1540 | struct weston_pointer_motion_event *event) |
| 1541 | { |
| 1542 | wl_fixed_t x, y; |
| 1543 | |
| 1544 | weston_pointer_motion_to_abs(pointer, event, &x, &y); |
| 1545 | weston_pointer_move_to(pointer, x, y); |
| 1546 | } |
| 1547 | |
Ander Conselvan de Oliveira | 54e90c7 | 2013-12-13 22:10:56 +0200 | [diff] [blame] | 1548 | /** Verify if the pointer is in a valid position and move it if it isn't. |
| 1549 | */ |
Ander Conselvan de Oliveira | f84327a | 2014-01-29 18:47:51 +0200 | [diff] [blame] | 1550 | static void |
| 1551 | weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data) |
Ander Conselvan de Oliveira | 54e90c7 | 2013-12-13 22:10:56 +0200 | [diff] [blame] | 1552 | { |
Ander Conselvan de Oliveira | f84327a | 2014-01-29 18:47:51 +0200 | [diff] [blame] | 1553 | struct weston_pointer *pointer; |
| 1554 | struct weston_compositor *ec; |
Ander Conselvan de Oliveira | 54e90c7 | 2013-12-13 22:10:56 +0200 | [diff] [blame] | 1555 | struct weston_output *output, *closest = NULL; |
| 1556 | int x, y, distance, min = INT_MAX; |
| 1557 | wl_fixed_t fx, fy; |
| 1558 | |
Ander Conselvan de Oliveira | f84327a | 2014-01-29 18:47:51 +0200 | [diff] [blame] | 1559 | pointer = container_of(listener, struct weston_pointer, |
| 1560 | output_destroy_listener); |
| 1561 | ec = pointer->seat->compositor; |
| 1562 | |
Ander Conselvan de Oliveira | 54e90c7 | 2013-12-13 22:10:56 +0200 | [diff] [blame] | 1563 | x = wl_fixed_to_int(pointer->x); |
| 1564 | y = wl_fixed_to_int(pointer->y); |
| 1565 | |
| 1566 | wl_list_for_each(output, &ec->output_list, link) { |
| 1567 | if (pixman_region32_contains_point(&output->region, |
| 1568 | x, y, NULL)) |
| 1569 | return; |
| 1570 | |
| 1571 | /* Aproximante the distance from the pointer to the center of |
| 1572 | * the output. */ |
| 1573 | distance = abs(output->x + output->width / 2 - x) + |
| 1574 | abs(output->y + output->height / 2 - y); |
| 1575 | if (distance < min) { |
| 1576 | min = distance; |
| 1577 | closest = output; |
| 1578 | } |
| 1579 | } |
| 1580 | |
| 1581 | /* Nothing to do if there's no output left. */ |
| 1582 | if (!closest) |
| 1583 | return; |
| 1584 | |
| 1585 | fx = pointer->x; |
| 1586 | fy = pointer->y; |
| 1587 | |
| 1588 | weston_pointer_clamp_for_output(pointer, closest, &fx, &fy); |
Jonas Ådahl | d251010 | 2014-10-05 21:39:14 +0200 | [diff] [blame] | 1589 | weston_pointer_move_to(pointer, fx, fy); |
Ander Conselvan de Oliveira | 54e90c7 | 2013-12-13 22:10:56 +0200 | [diff] [blame] | 1590 | } |
| 1591 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1592 | WL_EXPORT void |
| 1593 | notify_motion(struct weston_seat *seat, |
Alexandros Frantzis | 84b31f8 | 2017-11-24 18:01:46 +0200 | [diff] [blame] | 1594 | const struct timespec *time, |
Jonas Ådahl | d251010 | 2014-10-05 21:39:14 +0200 | [diff] [blame] | 1595 | struct weston_pointer_motion_event *event) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1596 | { |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1597 | struct weston_compositor *ec = seat->compositor; |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 1598 | struct weston_pointer *pointer = weston_seat_get_pointer(seat); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1599 | |
| 1600 | weston_compositor_wake(ec); |
Jonas Ådahl | d251010 | 2014-10-05 21:39:14 +0200 | [diff] [blame] | 1601 | pointer->grab->interface->motion(pointer->grab, time, event); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1602 | } |
| 1603 | |
Daniel Stone | 96d47c0 | 2013-11-19 11:37:12 +0100 | [diff] [blame] | 1604 | static void |
| 1605 | run_modifier_bindings(struct weston_seat *seat, uint32_t old, uint32_t new) |
| 1606 | { |
| 1607 | struct weston_compositor *compositor = seat->compositor; |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 1608 | struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat); |
Daniel Stone | 96d47c0 | 2013-11-19 11:37:12 +0100 | [diff] [blame] | 1609 | uint32_t diff; |
| 1610 | unsigned int i; |
| 1611 | struct { |
| 1612 | uint32_t xkb; |
| 1613 | enum weston_keyboard_modifier weston; |
| 1614 | } mods[] = { |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1615 | { keyboard->xkb_info->ctrl_mod, MODIFIER_CTRL }, |
| 1616 | { keyboard->xkb_info->alt_mod, MODIFIER_ALT }, |
| 1617 | { keyboard->xkb_info->super_mod, MODIFIER_SUPER }, |
| 1618 | { keyboard->xkb_info->shift_mod, MODIFIER_SHIFT }, |
Daniel Stone | 96d47c0 | 2013-11-19 11:37:12 +0100 | [diff] [blame] | 1619 | }; |
| 1620 | |
| 1621 | diff = new & ~old; |
| 1622 | for (i = 0; i < ARRAY_LENGTH(mods); i++) { |
| 1623 | if (diff & (1 << mods[i].xkb)) |
| 1624 | weston_compositor_run_modifier_binding(compositor, |
Derek Foreman | 99a6a2d | 2015-07-15 13:00:43 -0500 | [diff] [blame] | 1625 | keyboard, |
Daniel Stone | 96d47c0 | 2013-11-19 11:37:12 +0100 | [diff] [blame] | 1626 | mods[i].weston, |
| 1627 | WL_KEYBOARD_KEY_STATE_PRESSED); |
| 1628 | } |
| 1629 | |
| 1630 | diff = old & ~new; |
| 1631 | for (i = 0; i < ARRAY_LENGTH(mods); i++) { |
| 1632 | if (diff & (1 << mods[i].xkb)) |
| 1633 | weston_compositor_run_modifier_binding(compositor, |
Derek Foreman | 99a6a2d | 2015-07-15 13:00:43 -0500 | [diff] [blame] | 1634 | keyboard, |
Daniel Stone | 96d47c0 | 2013-11-19 11:37:12 +0100 | [diff] [blame] | 1635 | mods[i].weston, |
| 1636 | WL_KEYBOARD_KEY_STATE_RELEASED); |
| 1637 | } |
| 1638 | } |
| 1639 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1640 | WL_EXPORT void |
Alexandros Frantzis | 84b31f8 | 2017-11-24 18:01:46 +0200 | [diff] [blame] | 1641 | notify_motion_absolute(struct weston_seat *seat, const struct timespec *time, |
| 1642 | double x, double y) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1643 | { |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1644 | struct weston_compositor *ec = seat->compositor; |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 1645 | struct weston_pointer *pointer = weston_seat_get_pointer(seat); |
Jonas Ådahl | d251010 | 2014-10-05 21:39:14 +0200 | [diff] [blame] | 1646 | struct weston_pointer_motion_event event = { 0 }; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1647 | |
| 1648 | weston_compositor_wake(ec); |
Jonas Ådahl | d251010 | 2014-10-05 21:39:14 +0200 | [diff] [blame] | 1649 | |
| 1650 | event = (struct weston_pointer_motion_event) { |
| 1651 | .mask = WESTON_POINTER_MOTION_ABS, |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 1652 | .x = x, |
| 1653 | .y = y, |
Jonas Ådahl | d251010 | 2014-10-05 21:39:14 +0200 | [diff] [blame] | 1654 | }; |
| 1655 | |
| 1656 | pointer->grab->interface->motion(pointer->grab, time, &event); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1657 | } |
| 1658 | |
Jonas Ådahl | 94e2e2d | 2014-10-18 18:42:19 +0200 | [diff] [blame] | 1659 | static unsigned int |
| 1660 | peek_next_activate_serial(struct weston_compositor *c) |
| 1661 | { |
| 1662 | unsigned serial = c->activate_serial + 1; |
| 1663 | |
| 1664 | return serial == 0 ? 1 : serial; |
| 1665 | } |
| 1666 | |
| 1667 | static void |
| 1668 | inc_activate_serial(struct weston_compositor *c) |
| 1669 | { |
| 1670 | c->activate_serial = peek_next_activate_serial (c); |
| 1671 | } |
| 1672 | |
| 1673 | WL_EXPORT void |
| 1674 | weston_view_activate(struct weston_view *view, |
| 1675 | struct weston_seat *seat, |
| 1676 | uint32_t flags) |
| 1677 | { |
| 1678 | struct weston_compositor *compositor = seat->compositor; |
| 1679 | |
| 1680 | if (flags & WESTON_ACTIVATE_FLAG_CLICKED) { |
| 1681 | view->click_to_activate_serial = |
| 1682 | peek_next_activate_serial(compositor); |
| 1683 | } |
| 1684 | |
| 1685 | weston_seat_set_keyboard_focus(seat, view->surface); |
| 1686 | } |
| 1687 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1688 | WL_EXPORT void |
Alexandros Frantzis | 215bedc | 2017-11-16 18:20:55 +0200 | [diff] [blame] | 1689 | notify_button(struct weston_seat *seat, const struct timespec *time, |
| 1690 | int32_t button, enum wl_pointer_button_state state) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1691 | { |
| 1692 | struct weston_compositor *compositor = seat->compositor; |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 1693 | struct weston_pointer *pointer = weston_seat_get_pointer(seat); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1694 | |
| 1695 | if (state == WL_POINTER_BUTTON_STATE_PRESSED) { |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1696 | weston_compositor_idle_inhibit(compositor); |
| 1697 | if (pointer->button_count == 0) { |
| 1698 | pointer->grab_button = button; |
Alexandros Frantzis | 215bedc | 2017-11-16 18:20:55 +0200 | [diff] [blame] | 1699 | pointer->grab_time = *time; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1700 | pointer->grab_x = pointer->x; |
| 1701 | pointer->grab_y = pointer->y; |
| 1702 | } |
| 1703 | pointer->button_count++; |
| 1704 | } else { |
| 1705 | weston_compositor_idle_release(compositor); |
| 1706 | pointer->button_count--; |
| 1707 | } |
| 1708 | |
Derek Foreman | 99a6a2d | 2015-07-15 13:00:43 -0500 | [diff] [blame] | 1709 | weston_compositor_run_button_binding(compositor, pointer, time, button, |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1710 | state); |
| 1711 | |
| 1712 | pointer->grab->interface->button(pointer->grab, time, button, state); |
| 1713 | |
| 1714 | if (pointer->button_count == 1) |
| 1715 | pointer->grab_serial = |
| 1716 | wl_display_get_serial(compositor->wl_display); |
| 1717 | } |
| 1718 | |
| 1719 | WL_EXPORT void |
Alexandros Frantzis | 8032194 | 2017-11-16 18:20:56 +0200 | [diff] [blame] | 1720 | notify_axis(struct weston_seat *seat, const struct timespec *time, |
Peter Hutterer | 89b6a49 | 2016-01-18 15:58:17 +1000 | [diff] [blame] | 1721 | struct weston_pointer_axis_event *event) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1722 | { |
| 1723 | struct weston_compositor *compositor = seat->compositor; |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 1724 | struct weston_pointer *pointer = weston_seat_get_pointer(seat); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1725 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1726 | weston_compositor_wake(compositor); |
| 1727 | |
Derek Foreman | 99a6a2d | 2015-07-15 13:00:43 -0500 | [diff] [blame] | 1728 | if (weston_compositor_run_axis_binding(compositor, pointer, |
Peter Hutterer | 89b6a49 | 2016-01-18 15:58:17 +1000 | [diff] [blame] | 1729 | time, event)) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1730 | return; |
| 1731 | |
Peter Hutterer | 89b6a49 | 2016-01-18 15:58:17 +1000 | [diff] [blame] | 1732 | pointer->grab->interface->axis(pointer->grab, time, event); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1733 | } |
| 1734 | |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 1735 | WL_EXPORT void |
| 1736 | notify_axis_source(struct weston_seat *seat, uint32_t source) |
| 1737 | { |
| 1738 | struct weston_compositor *compositor = seat->compositor; |
| 1739 | struct weston_pointer *pointer = weston_seat_get_pointer(seat); |
| 1740 | |
| 1741 | weston_compositor_wake(compositor); |
| 1742 | |
| 1743 | pointer->grab->interface->axis_source(pointer->grab, source); |
| 1744 | } |
| 1745 | |
| 1746 | WL_EXPORT void |
| 1747 | notify_pointer_frame(struct weston_seat *seat) |
| 1748 | { |
| 1749 | struct weston_compositor *compositor = seat->compositor; |
| 1750 | struct weston_pointer *pointer = weston_seat_get_pointer(seat); |
| 1751 | |
| 1752 | weston_compositor_wake(compositor); |
| 1753 | |
| 1754 | pointer->grab->interface->frame(pointer->grab); |
| 1755 | } |
| 1756 | |
Giulio Camuffo | 6ef444d | 2014-08-28 19:44:09 +0300 | [diff] [blame] | 1757 | WL_EXPORT int |
| 1758 | weston_keyboard_set_locks(struct weston_keyboard *keyboard, |
| 1759 | uint32_t mask, uint32_t value) |
| 1760 | { |
Giulio Camuffo | 6ef444d | 2014-08-28 19:44:09 +0300 | [diff] [blame] | 1761 | uint32_t serial; |
| 1762 | xkb_mod_mask_t mods_depressed, mods_latched, mods_locked, group; |
| 1763 | xkb_mod_mask_t num, caps; |
| 1764 | |
| 1765 | /* We don't want the leds to go out of sync with the actual state |
| 1766 | * so if the backend has no way to change the leds don't try to |
| 1767 | * change the state */ |
| 1768 | if (!keyboard->seat->led_update) |
| 1769 | return -1; |
| 1770 | |
| 1771 | mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state, |
| 1772 | XKB_STATE_DEPRESSED); |
| 1773 | mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state, |
| 1774 | XKB_STATE_LATCHED); |
| 1775 | mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state, |
| 1776 | XKB_STATE_LOCKED); |
| 1777 | group = xkb_state_serialize_group(keyboard->xkb_state.state, |
| 1778 | XKB_STATE_EFFECTIVE); |
| 1779 | |
| 1780 | num = (1 << keyboard->xkb_info->mod2_mod); |
| 1781 | caps = (1 << keyboard->xkb_info->caps_mod); |
| 1782 | if (mask & WESTON_NUM_LOCK) { |
| 1783 | if (value & WESTON_NUM_LOCK) |
| 1784 | mods_locked |= num; |
| 1785 | else |
| 1786 | mods_locked &= ~num; |
| 1787 | } |
| 1788 | if (mask & WESTON_CAPS_LOCK) { |
| 1789 | if (value & WESTON_CAPS_LOCK) |
| 1790 | mods_locked |= caps; |
| 1791 | else |
| 1792 | mods_locked &= ~caps; |
| 1793 | } |
| 1794 | |
| 1795 | xkb_state_update_mask(keyboard->xkb_state.state, mods_depressed, |
| 1796 | mods_latched, mods_locked, 0, 0, group); |
| 1797 | |
| 1798 | serial = wl_display_next_serial( |
| 1799 | keyboard->seat->compositor->wl_display); |
| 1800 | notify_modifiers(keyboard->seat, serial); |
| 1801 | |
| 1802 | return 0; |
Giulio Camuffo | 6ef444d | 2014-08-28 19:44:09 +0300 | [diff] [blame] | 1803 | } |
| 1804 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1805 | WL_EXPORT void |
| 1806 | notify_modifiers(struct weston_seat *seat, uint32_t serial) |
| 1807 | { |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 1808 | struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1809 | struct weston_keyboard_grab *grab = keyboard->grab; |
| 1810 | uint32_t mods_depressed, mods_latched, mods_locked, group; |
| 1811 | uint32_t mods_lookup; |
| 1812 | enum weston_led leds = 0; |
| 1813 | int changed = 0; |
| 1814 | |
| 1815 | /* Serialize and update our internal state, checking to see if it's |
| 1816 | * different to the previous state. */ |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1817 | mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state, |
Ran Benita | 2e1968f | 2014-08-19 23:59:51 +0300 | [diff] [blame] | 1818 | XKB_STATE_MODS_DEPRESSED); |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1819 | mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state, |
Ran Benita | 2e1968f | 2014-08-19 23:59:51 +0300 | [diff] [blame] | 1820 | XKB_STATE_MODS_LATCHED); |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1821 | mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state, |
Ran Benita | 2e1968f | 2014-08-19 23:59:51 +0300 | [diff] [blame] | 1822 | XKB_STATE_MODS_LOCKED); |
| 1823 | group = xkb_state_serialize_layout(keyboard->xkb_state.state, |
| 1824 | XKB_STATE_LAYOUT_EFFECTIVE); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1825 | |
Derek Foreman | 244e99e | 2015-06-03 15:53:26 -0500 | [diff] [blame] | 1826 | if (mods_depressed != keyboard->modifiers.mods_depressed || |
| 1827 | mods_latched != keyboard->modifiers.mods_latched || |
| 1828 | mods_locked != keyboard->modifiers.mods_locked || |
| 1829 | group != keyboard->modifiers.group) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1830 | changed = 1; |
| 1831 | |
Derek Foreman | 244e99e | 2015-06-03 15:53:26 -0500 | [diff] [blame] | 1832 | run_modifier_bindings(seat, keyboard->modifiers.mods_depressed, |
Daniel Stone | 96d47c0 | 2013-11-19 11:37:12 +0100 | [diff] [blame] | 1833 | mods_depressed); |
| 1834 | |
Derek Foreman | 244e99e | 2015-06-03 15:53:26 -0500 | [diff] [blame] | 1835 | keyboard->modifiers.mods_depressed = mods_depressed; |
| 1836 | keyboard->modifiers.mods_latched = mods_latched; |
| 1837 | keyboard->modifiers.mods_locked = mods_locked; |
| 1838 | keyboard->modifiers.group = group; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1839 | |
| 1840 | /* And update the modifier_state for bindings. */ |
| 1841 | mods_lookup = mods_depressed | mods_latched; |
| 1842 | seat->modifier_state = 0; |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1843 | if (mods_lookup & (1 << keyboard->xkb_info->ctrl_mod)) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1844 | seat->modifier_state |= MODIFIER_CTRL; |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1845 | if (mods_lookup & (1 << keyboard->xkb_info->alt_mod)) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1846 | seat->modifier_state |= MODIFIER_ALT; |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1847 | if (mods_lookup & (1 << keyboard->xkb_info->super_mod)) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1848 | seat->modifier_state |= MODIFIER_SUPER; |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1849 | if (mods_lookup & (1 << keyboard->xkb_info->shift_mod)) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1850 | seat->modifier_state |= MODIFIER_SHIFT; |
| 1851 | |
| 1852 | /* Finally, notify the compositor that LEDs have changed. */ |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1853 | if (xkb_state_led_index_is_active(keyboard->xkb_state.state, |
| 1854 | keyboard->xkb_info->num_led)) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1855 | leds |= LED_NUM_LOCK; |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1856 | if (xkb_state_led_index_is_active(keyboard->xkb_state.state, |
| 1857 | keyboard->xkb_info->caps_led)) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1858 | leds |= LED_CAPS_LOCK; |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1859 | if (xkb_state_led_index_is_active(keyboard->xkb_state.state, |
| 1860 | keyboard->xkb_info->scroll_led)) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1861 | leds |= LED_SCROLL_LOCK; |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1862 | if (leds != keyboard->xkb_state.leds && seat->led_update) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1863 | seat->led_update(seat, leds); |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1864 | keyboard->xkb_state.leds = leds; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1865 | |
| 1866 | if (changed) { |
| 1867 | grab->interface->modifiers(grab, |
| 1868 | serial, |
| 1869 | keyboard->modifiers.mods_depressed, |
| 1870 | keyboard->modifiers.mods_latched, |
| 1871 | keyboard->modifiers.mods_locked, |
| 1872 | keyboard->modifiers.group); |
| 1873 | } |
| 1874 | } |
| 1875 | |
| 1876 | static void |
| 1877 | update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key, |
| 1878 | enum wl_keyboard_key_state state) |
| 1879 | { |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 1880 | struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1881 | enum xkb_key_direction direction; |
| 1882 | |
| 1883 | if (state == WL_KEYBOARD_KEY_STATE_PRESSED) |
| 1884 | direction = XKB_KEY_DOWN; |
| 1885 | else |
| 1886 | direction = XKB_KEY_UP; |
| 1887 | |
| 1888 | /* Offset the keycode by 8, as the evdev XKB rules reflect X's |
| 1889 | * broken keycode system, which starts at 8. */ |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1890 | xkb_state_update_key(keyboard->xkb_state.state, key + 8, direction); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1891 | |
| 1892 | notify_modifiers(seat, serial); |
| 1893 | } |
Rui Matos | 65196bc | 2013-10-10 19:44:19 +0200 | [diff] [blame] | 1894 | |
| 1895 | static void |
| 1896 | send_keymap(struct wl_resource *resource, struct weston_xkb_info *xkb_info) |
| 1897 | { |
| 1898 | wl_keyboard_send_keymap(resource, |
| 1899 | WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, |
| 1900 | xkb_info->keymap_fd, |
| 1901 | xkb_info->keymap_size); |
| 1902 | } |
| 1903 | |
| 1904 | static void |
| 1905 | send_modifiers(struct wl_resource *resource, uint32_t serial, struct weston_keyboard *keyboard) |
| 1906 | { |
| 1907 | wl_keyboard_send_modifiers(resource, serial, |
| 1908 | keyboard->modifiers.mods_depressed, |
| 1909 | keyboard->modifiers.mods_latched, |
| 1910 | keyboard->modifiers.mods_locked, |
| 1911 | keyboard->modifiers.group); |
| 1912 | } |
| 1913 | |
| 1914 | static struct weston_xkb_info * |
| 1915 | weston_xkb_info_create(struct xkb_keymap *keymap); |
Rui Matos | 65196bc | 2013-10-10 19:44:19 +0200 | [diff] [blame] | 1916 | |
| 1917 | static void |
| 1918 | update_keymap(struct weston_seat *seat) |
| 1919 | { |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 1920 | struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat); |
Rui Matos | 65196bc | 2013-10-10 19:44:19 +0200 | [diff] [blame] | 1921 | struct wl_resource *resource; |
| 1922 | struct weston_xkb_info *xkb_info; |
| 1923 | struct xkb_state *state; |
| 1924 | xkb_mod_mask_t latched_mods; |
| 1925 | xkb_mod_mask_t locked_mods; |
| 1926 | |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1927 | xkb_info = weston_xkb_info_create(keyboard->pending_keymap); |
Rui Matos | 65196bc | 2013-10-10 19:44:19 +0200 | [diff] [blame] | 1928 | |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1929 | xkb_keymap_unref(keyboard->pending_keymap); |
| 1930 | keyboard->pending_keymap = NULL; |
Rui Matos | 65196bc | 2013-10-10 19:44:19 +0200 | [diff] [blame] | 1931 | |
| 1932 | if (!xkb_info) { |
| 1933 | weston_log("failed to create XKB info\n"); |
| 1934 | return; |
| 1935 | } |
| 1936 | |
| 1937 | state = xkb_state_new(xkb_info->keymap); |
| 1938 | if (!state) { |
| 1939 | weston_log("failed to initialise XKB state\n"); |
| 1940 | weston_xkb_info_destroy(xkb_info); |
| 1941 | return; |
| 1942 | } |
| 1943 | |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1944 | latched_mods = xkb_state_serialize_mods(keyboard->xkb_state.state, |
| 1945 | XKB_STATE_MODS_LATCHED); |
| 1946 | locked_mods = xkb_state_serialize_mods(keyboard->xkb_state.state, |
| 1947 | XKB_STATE_MODS_LOCKED); |
Rui Matos | 65196bc | 2013-10-10 19:44:19 +0200 | [diff] [blame] | 1948 | xkb_state_update_mask(state, |
| 1949 | 0, /* depressed */ |
| 1950 | latched_mods, |
| 1951 | locked_mods, |
| 1952 | 0, 0, 0); |
| 1953 | |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1954 | weston_xkb_info_destroy(keyboard->xkb_info); |
| 1955 | keyboard->xkb_info = xkb_info; |
Rui Matos | 65196bc | 2013-10-10 19:44:19 +0200 | [diff] [blame] | 1956 | |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1957 | xkb_state_unref(keyboard->xkb_state.state); |
| 1958 | keyboard->xkb_state.state = state; |
Rui Matos | 65196bc | 2013-10-10 19:44:19 +0200 | [diff] [blame] | 1959 | |
Derek Foreman | bc91e54 | 2015-06-03 15:53:27 -0500 | [diff] [blame] | 1960 | wl_resource_for_each(resource, &keyboard->resource_list) |
Rui Matos | 65196bc | 2013-10-10 19:44:19 +0200 | [diff] [blame] | 1961 | send_keymap(resource, xkb_info); |
Derek Foreman | bc91e54 | 2015-06-03 15:53:27 -0500 | [diff] [blame] | 1962 | wl_resource_for_each(resource, &keyboard->focus_resource_list) |
Rui Matos | 65196bc | 2013-10-10 19:44:19 +0200 | [diff] [blame] | 1963 | send_keymap(resource, xkb_info); |
| 1964 | |
| 1965 | notify_modifiers(seat, wl_display_next_serial(seat->compositor->wl_display)); |
| 1966 | |
| 1967 | if (!latched_mods && !locked_mods) |
| 1968 | return; |
| 1969 | |
Derek Foreman | bc91e54 | 2015-06-03 15:53:27 -0500 | [diff] [blame] | 1970 | wl_resource_for_each(resource, &keyboard->resource_list) |
| 1971 | send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), keyboard); |
| 1972 | wl_resource_for_each(resource, &keyboard->focus_resource_list) |
| 1973 | send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), keyboard); |
Rui Matos | 65196bc | 2013-10-10 19:44:19 +0200 | [diff] [blame] | 1974 | } |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1975 | |
| 1976 | WL_EXPORT void |
Alexandros Frantzis | 47e79c8 | 2017-11-16 18:20:57 +0200 | [diff] [blame] | 1977 | notify_key(struct weston_seat *seat, const struct timespec *time, uint32_t key, |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1978 | enum wl_keyboard_key_state state, |
| 1979 | enum weston_key_state_update update_state) |
| 1980 | { |
| 1981 | struct weston_compositor *compositor = seat->compositor; |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 1982 | struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1983 | struct weston_keyboard_grab *grab = keyboard->grab; |
Pekka Paalanen | 86b5396 | 2014-11-19 13:43:32 +0200 | [diff] [blame] | 1984 | uint32_t *k, *end; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1985 | |
| 1986 | if (state == WL_KEYBOARD_KEY_STATE_PRESSED) { |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1987 | weston_compositor_idle_inhibit(compositor); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1988 | } else { |
| 1989 | weston_compositor_idle_release(compositor); |
| 1990 | } |
| 1991 | |
Pekka Paalanen | 86b5396 | 2014-11-19 13:43:32 +0200 | [diff] [blame] | 1992 | end = keyboard->keys.data + keyboard->keys.size; |
| 1993 | for (k = keyboard->keys.data; k < end; k++) { |
| 1994 | if (*k == key) { |
| 1995 | /* Ignore server-generated repeats. */ |
| 1996 | if (state == WL_KEYBOARD_KEY_STATE_PRESSED) |
| 1997 | return; |
| 1998 | *k = *--end; |
| 1999 | } |
| 2000 | } |
| 2001 | keyboard->keys.size = (void *) end - keyboard->keys.data; |
| 2002 | if (state == WL_KEYBOARD_KEY_STATE_PRESSED) { |
| 2003 | k = wl_array_add(&keyboard->keys, sizeof *k); |
| 2004 | *k = key; |
| 2005 | } |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2006 | |
| 2007 | if (grab == &keyboard->default_grab || |
| 2008 | grab == &keyboard->input_method_grab) { |
Derek Foreman | 99a6a2d | 2015-07-15 13:00:43 -0500 | [diff] [blame] | 2009 | weston_compositor_run_key_binding(compositor, keyboard, time, |
| 2010 | key, state); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2011 | grab = keyboard->grab; |
| 2012 | } |
| 2013 | |
| 2014 | grab->interface->key(grab, time, key, state); |
| 2015 | |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 2016 | if (keyboard->pending_keymap && |
Pekka Paalanen | 86b5396 | 2014-11-19 13:43:32 +0200 | [diff] [blame] | 2017 | keyboard->keys.size == 0) |
Rui Matos | 65196bc | 2013-10-10 19:44:19 +0200 | [diff] [blame] | 2018 | update_keymap(seat); |
| 2019 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2020 | if (update_state == STATE_UPDATE_AUTOMATIC) { |
| 2021 | update_modifier_state(seat, |
| 2022 | wl_display_get_serial(compositor->wl_display), |
| 2023 | key, |
| 2024 | state); |
| 2025 | } |
Giulio Camuffo | b6ddf6c | 2015-02-06 19:06:54 +0200 | [diff] [blame] | 2026 | |
Olivier Fourdan | df84dbe | 2016-06-30 16:01:56 +0200 | [diff] [blame] | 2027 | keyboard->grab_serial = wl_display_get_serial(compositor->wl_display); |
Giulio Camuffo | b6ddf6c | 2015-02-06 19:06:54 +0200 | [diff] [blame] | 2028 | if (state == WL_KEYBOARD_KEY_STATE_PRESSED) { |
Alexandros Frantzis | 47e79c8 | 2017-11-16 18:20:57 +0200 | [diff] [blame] | 2029 | keyboard->grab_time = *time; |
Giulio Camuffo | b6ddf6c | 2015-02-06 19:06:54 +0200 | [diff] [blame] | 2030 | keyboard->grab_key = key; |
| 2031 | } |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2032 | } |
| 2033 | |
| 2034 | WL_EXPORT void |
| 2035 | notify_pointer_focus(struct weston_seat *seat, struct weston_output *output, |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 2036 | double x, double y) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2037 | { |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 2038 | struct weston_pointer *pointer = weston_seat_get_pointer(seat); |
| 2039 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2040 | if (output) { |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 2041 | weston_pointer_move_to(pointer, |
| 2042 | wl_fixed_from_double(x), |
| 2043 | wl_fixed_from_double(y)); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2044 | } else { |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 2045 | /* FIXME: We should call weston_pointer_set_focus(seat, |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2046 | * NULL) here, but somehow that breaks re-entry... */ |
| 2047 | } |
| 2048 | } |
| 2049 | |
| 2050 | static void |
| 2051 | destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data) |
| 2052 | { |
| 2053 | struct weston_seat *ws; |
| 2054 | |
| 2055 | ws = container_of(listener, struct weston_seat, |
| 2056 | saved_kbd_focus_listener); |
| 2057 | |
| 2058 | ws->saved_kbd_focus = NULL; |
| 2059 | } |
| 2060 | |
| 2061 | WL_EXPORT void |
| 2062 | notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys, |
| 2063 | enum weston_key_state_update update_state) |
| 2064 | { |
| 2065 | struct weston_compositor *compositor = seat->compositor; |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 2066 | struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat); |
Kristian Høgsberg | fe7aa90 | 2013-05-08 09:54:37 -0400 | [diff] [blame] | 2067 | struct weston_surface *surface; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2068 | uint32_t *k, serial; |
| 2069 | |
| 2070 | serial = wl_display_next_serial(compositor->wl_display); |
| 2071 | wl_array_copy(&keyboard->keys, keys); |
| 2072 | wl_array_for_each(k, &keyboard->keys) { |
| 2073 | weston_compositor_idle_inhibit(compositor); |
| 2074 | if (update_state == STATE_UPDATE_AUTOMATIC) |
| 2075 | update_modifier_state(seat, serial, *k, |
| 2076 | WL_KEYBOARD_KEY_STATE_PRESSED); |
| 2077 | } |
| 2078 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2079 | surface = seat->saved_kbd_focus; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2080 | if (surface) { |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2081 | weston_keyboard_set_focus(keyboard, surface); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2082 | } |
| 2083 | } |
| 2084 | |
| 2085 | WL_EXPORT void |
| 2086 | notify_keyboard_focus_out(struct weston_seat *seat) |
| 2087 | { |
| 2088 | struct weston_compositor *compositor = seat->compositor; |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 2089 | struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat); |
| 2090 | struct weston_pointer *pointer = weston_seat_get_pointer(seat); |
Quentin Glidic | 85d5554 | 2017-07-21 14:02:40 +0200 | [diff] [blame] | 2091 | struct weston_surface *focus = keyboard->focus; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2092 | uint32_t *k, serial; |
| 2093 | |
| 2094 | serial = wl_display_next_serial(compositor->wl_display); |
| 2095 | wl_array_for_each(k, &keyboard->keys) { |
| 2096 | weston_compositor_idle_release(compositor); |
| 2097 | update_modifier_state(seat, serial, *k, |
| 2098 | WL_KEYBOARD_KEY_STATE_RELEASED); |
| 2099 | } |
| 2100 | |
| 2101 | seat->modifier_state = 0; |
| 2102 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2103 | weston_keyboard_set_focus(keyboard, NULL); |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 2104 | weston_keyboard_cancel_grab(keyboard); |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 2105 | if (pointer) |
| 2106 | weston_pointer_cancel_grab(pointer); |
Quentin Glidic | 85d5554 | 2017-07-21 14:02:40 +0200 | [diff] [blame] | 2107 | |
| 2108 | if (focus) { |
| 2109 | seat->saved_kbd_focus = focus; |
| 2110 | seat->saved_kbd_focus_listener.notify = |
| 2111 | destroy_device_saved_kbd_focus; |
| 2112 | wl_signal_add(&focus->destroy_signal, |
| 2113 | &seat->saved_kbd_focus_listener); |
| 2114 | } |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2115 | } |
| 2116 | |
Michael Fu | a2bb791 | 2013-07-23 15:51:06 +0800 | [diff] [blame] | 2117 | WL_EXPORT void |
Derek Foreman | 4c93c08 | 2015-04-30 16:45:41 -0500 | [diff] [blame] | 2118 | weston_touch_set_focus(struct weston_touch *touch, struct weston_view *view) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2119 | { |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 2120 | struct wl_list *focus_resource_list; |
| 2121 | |
Derek Foreman | 4c93c08 | 2015-04-30 16:45:41 -0500 | [diff] [blame] | 2122 | focus_resource_list = &touch->focus_resource_list; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2123 | |
Derek Foreman | 4c93c08 | 2015-04-30 16:45:41 -0500 | [diff] [blame] | 2124 | if (view && touch->focus && |
| 2125 | touch->focus->surface == view->surface) { |
| 2126 | touch->focus = view; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2127 | return; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2128 | } |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2129 | |
Derek Foreman | 4c93c08 | 2015-04-30 16:45:41 -0500 | [diff] [blame] | 2130 | wl_list_remove(&touch->focus_resource_listener.link); |
| 2131 | wl_list_init(&touch->focus_resource_listener.link); |
| 2132 | wl_list_remove(&touch->focus_view_listener.link); |
| 2133 | wl_list_init(&touch->focus_view_listener.link); |
Giulio Camuffo | 576fe2a | 2013-11-20 18:00:24 +0100 | [diff] [blame] | 2134 | |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 2135 | if (!wl_list_empty(focus_resource_list)) { |
Derek Foreman | 4c93c08 | 2015-04-30 16:45:41 -0500 | [diff] [blame] | 2136 | move_resources(&touch->resource_list, |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 2137 | focus_resource_list); |
| 2138 | } |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2139 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2140 | if (view) { |
Derek Foreman | 362656b | 2014-09-04 10:23:05 -0500 | [diff] [blame] | 2141 | struct wl_client *surface_client; |
| 2142 | |
| 2143 | if (!view->surface->resource) { |
Derek Foreman | 4c93c08 | 2015-04-30 16:45:41 -0500 | [diff] [blame] | 2144 | touch->focus = NULL; |
Derek Foreman | 362656b | 2014-09-04 10:23:05 -0500 | [diff] [blame] | 2145 | return; |
| 2146 | } |
| 2147 | |
| 2148 | surface_client = wl_resource_get_client(view->surface->resource); |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 2149 | move_resources_for_client(focus_resource_list, |
Derek Foreman | 4c93c08 | 2015-04-30 16:45:41 -0500 | [diff] [blame] | 2150 | &touch->resource_list, |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 2151 | surface_client); |
Giulio Camuffo | 576fe2a | 2013-11-20 18:00:24 +0100 | [diff] [blame] | 2152 | wl_resource_add_destroy_listener(view->surface->resource, |
Derek Foreman | 4c93c08 | 2015-04-30 16:45:41 -0500 | [diff] [blame] | 2153 | &touch->focus_resource_listener); |
| 2154 | wl_signal_add(&view->destroy_signal, &touch->focus_view_listener); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2155 | } |
Derek Foreman | 4c93c08 | 2015-04-30 16:45:41 -0500 | [diff] [blame] | 2156 | touch->focus = view; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2157 | } |
| 2158 | |
| 2159 | /** |
| 2160 | * notify_touch - emulates button touches and notifies surfaces accordingly. |
| 2161 | * |
| 2162 | * It assumes always the correct cycle sequence until it gets here: touch_down |
| 2163 | * → touch_update → ... → touch_update → touch_end. The driver is responsible |
| 2164 | * for sending along such order. |
| 2165 | * |
| 2166 | */ |
| 2167 | WL_EXPORT void |
Alexandros Frantzis | 9448deb | 2017-11-16 18:20:58 +0200 | [diff] [blame] | 2168 | notify_touch(struct weston_seat *seat, const struct timespec *time, |
| 2169 | int touch_id, double double_x, double double_y, int touch_type) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2170 | { |
| 2171 | struct weston_compositor *ec = seat->compositor; |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 2172 | struct weston_touch *touch = weston_seat_get_touch(seat); |
Kristian Høgsberg | e329f36 | 2013-05-06 22:19:57 -0400 | [diff] [blame] | 2173 | struct weston_touch_grab *grab = touch->grab; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2174 | struct weston_view *ev; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2175 | wl_fixed_t sx, sy; |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 2176 | wl_fixed_t x = wl_fixed_from_double(double_x); |
| 2177 | wl_fixed_t y = wl_fixed_from_double(double_y); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2178 | |
| 2179 | /* Update grab's global coordinates. */ |
Neil Roberts | 306fe08 | 2013-10-03 16:43:06 +0100 | [diff] [blame] | 2180 | if (touch_id == touch->grab_touch_id && touch_type != WL_TOUCH_UP) { |
| 2181 | touch->grab_x = x; |
| 2182 | touch->grab_y = y; |
| 2183 | } |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2184 | |
| 2185 | switch (touch_type) { |
| 2186 | case WL_TOUCH_DOWN: |
| 2187 | weston_compositor_idle_inhibit(ec); |
| 2188 | |
Jonas Ådahl | 9484b69 | 2013-12-02 22:05:03 +0100 | [diff] [blame] | 2189 | touch->num_tp++; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2190 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2191 | /* the first finger down picks the view, and all further go |
| 2192 | * to that view for the remainder of the touch session i.e. |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2193 | * until all touch points are up again. */ |
Jonas Ådahl | 9484b69 | 2013-12-02 22:05:03 +0100 | [diff] [blame] | 2194 | if (touch->num_tp == 1) { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2195 | ev = weston_compositor_pick_view(ec, x, y, &sx, &sy); |
Derek Foreman | 4c93c08 | 2015-04-30 16:45:41 -0500 | [diff] [blame] | 2196 | weston_touch_set_focus(touch, ev); |
Giulio Camuffo | 61ed7b6 | 2015-07-08 11:55:28 +0300 | [diff] [blame] | 2197 | } else if (!touch->focus) { |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2198 | /* Unexpected condition: We have non-initial touch but |
| 2199 | * there is no focused surface. |
| 2200 | */ |
Chris Michael | 3f607d3 | 2015-10-07 11:59:49 -0400 | [diff] [blame] | 2201 | weston_log("touch event received with %d points down " |
Jonas Ådahl | 9484b69 | 2013-12-02 22:05:03 +0100 | [diff] [blame] | 2202 | "but no surface focused\n", touch->num_tp); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2203 | return; |
| 2204 | } |
| 2205 | |
Derek Foreman | 99a6a2d | 2015-07-15 13:00:43 -0500 | [diff] [blame] | 2206 | weston_compositor_run_touch_binding(ec, touch, |
Kristian Høgsberg | c896401 | 2014-02-05 14:25:18 -0800 | [diff] [blame] | 2207 | time, touch_type); |
| 2208 | |
Giulio Camuffo | 61ed7b6 | 2015-07-08 11:55:28 +0300 | [diff] [blame] | 2209 | grab->interface->down(grab, time, touch_id, x, y); |
Jonas Ådahl | 9484b69 | 2013-12-02 22:05:03 +0100 | [diff] [blame] | 2210 | if (touch->num_tp == 1) { |
Rusty Lynch | f1407ff | 2013-08-08 21:13:57 -0700 | [diff] [blame] | 2211 | touch->grab_serial = |
| 2212 | wl_display_get_serial(ec->wl_display); |
Neil Roberts | 306fe08 | 2013-10-03 16:43:06 +0100 | [diff] [blame] | 2213 | touch->grab_touch_id = touch_id; |
Alexandros Frantzis | 9448deb | 2017-11-16 18:20:58 +0200 | [diff] [blame] | 2214 | touch->grab_time = *time; |
Rusty Lynch | f1407ff | 2013-08-08 21:13:57 -0700 | [diff] [blame] | 2215 | touch->grab_x = x; |
| 2216 | touch->grab_y = y; |
| 2217 | } |
| 2218 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2219 | break; |
| 2220 | case WL_TOUCH_MOTION: |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2221 | ev = touch->focus; |
| 2222 | if (!ev) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2223 | break; |
| 2224 | |
Alexandros Frantzis | 7d2abcf | 2017-11-16 18:21:00 +0200 | [diff] [blame] | 2225 | grab->interface->motion(grab, time, touch_id, x, y); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2226 | break; |
| 2227 | case WL_TOUCH_UP: |
Kristian Høgsberg | a30e29a | 2014-01-08 22:29:20 -0800 | [diff] [blame] | 2228 | if (touch->num_tp == 0) { |
| 2229 | /* This can happen if we start out with one or |
| 2230 | * more fingers on the touch screen, in which |
| 2231 | * case we didn't get the corresponding down |
| 2232 | * event. */ |
| 2233 | weston_log("unmatched touch up event\n"); |
| 2234 | break; |
| 2235 | } |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2236 | weston_compositor_idle_release(ec); |
Jonas Ådahl | 9484b69 | 2013-12-02 22:05:03 +0100 | [diff] [blame] | 2237 | touch->num_tp--; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2238 | |
Alexandros Frantzis | 27a51b8 | 2017-11-16 18:20:59 +0200 | [diff] [blame] | 2239 | grab->interface->up(grab, time, touch_id); |
Jonas Ådahl | 9484b69 | 2013-12-02 22:05:03 +0100 | [diff] [blame] | 2240 | if (touch->num_tp == 0) |
Derek Foreman | 4c93c08 | 2015-04-30 16:45:41 -0500 | [diff] [blame] | 2241 | weston_touch_set_focus(touch, NULL); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2242 | break; |
| 2243 | } |
| 2244 | } |
| 2245 | |
Jonas Ådahl | 1679f23 | 2014-04-12 09:39:51 +0200 | [diff] [blame] | 2246 | WL_EXPORT void |
| 2247 | notify_touch_frame(struct weston_seat *seat) |
| 2248 | { |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 2249 | struct weston_touch *touch = weston_seat_get_touch(seat); |
Jonas Ådahl | 1679f23 | 2014-04-12 09:39:51 +0200 | [diff] [blame] | 2250 | struct weston_touch_grab *grab = touch->grab; |
| 2251 | |
| 2252 | grab->interface->frame(grab); |
| 2253 | } |
| 2254 | |
Derek Foreman | 3cc004a | 2015-11-06 15:56:09 -0600 | [diff] [blame] | 2255 | WL_EXPORT void |
| 2256 | notify_touch_cancel(struct weston_seat *seat) |
| 2257 | { |
| 2258 | struct weston_touch *touch = weston_seat_get_touch(seat); |
| 2259 | struct weston_touch_grab *grab = touch->grab; |
| 2260 | |
| 2261 | grab->interface->cancel(grab); |
| 2262 | } |
| 2263 | |
Pekka Paalanen | 8274d90 | 2014-08-06 19:36:51 +0300 | [diff] [blame] | 2264 | static int |
| 2265 | pointer_cursor_surface_get_label(struct weston_surface *surface, |
| 2266 | char *buf, size_t len) |
| 2267 | { |
| 2268 | return snprintf(buf, len, "cursor"); |
| 2269 | } |
| 2270 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2271 | static void |
Quentin Glidic | 2edc3d5 | 2016-08-12 10:41:33 +0200 | [diff] [blame] | 2272 | pointer_cursor_surface_committed(struct weston_surface *es, |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 2273 | int32_t dx, int32_t dy) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2274 | { |
Quentin Glidic | 2edc3d5 | 2016-08-12 10:41:33 +0200 | [diff] [blame] | 2275 | struct weston_pointer *pointer = es->committed_private; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2276 | int x, y; |
| 2277 | |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 2278 | if (es->width == 0) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2279 | return; |
| 2280 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2281 | assert(es == pointer->sprite->surface); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2282 | |
Kristian Høgsberg | 195b869 | 2013-05-08 15:02:05 -0400 | [diff] [blame] | 2283 | pointer->hotspot_x -= dx; |
| 2284 | pointer->hotspot_y -= dy; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2285 | |
Kristian Høgsberg | 195b869 | 2013-05-08 15:02:05 -0400 | [diff] [blame] | 2286 | x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x; |
| 2287 | y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2288 | |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 2289 | weston_view_set_position(pointer->sprite, x, y); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2290 | |
| 2291 | empty_region(&es->pending.input); |
Ander Conselvan de Oliveira | 23900f7 | 2014-01-31 16:07:51 +0200 | [diff] [blame] | 2292 | empty_region(&es->input); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2293 | |
| 2294 | if (!weston_surface_is_mapped(es)) { |
Giulio Camuffo | 412e6a5 | 2014-07-09 22:12:56 +0300 | [diff] [blame] | 2295 | weston_layer_entry_insert(&es->compositor->cursor_layer.view_list, |
| 2296 | &pointer->sprite->layer_link); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2297 | weston_view_update_transform(pointer->sprite); |
Armin Krezović | f8486c3 | 2016-06-30 06:04:28 +0200 | [diff] [blame] | 2298 | es->is_mapped = true; |
| 2299 | pointer->sprite->is_mapped = true; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2300 | } |
| 2301 | } |
| 2302 | |
| 2303 | static void |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2304 | pointer_set_cursor(struct wl_client *client, struct wl_resource *resource, |
| 2305 | uint32_t serial, struct wl_resource *surface_resource, |
| 2306 | int32_t x, int32_t y) |
| 2307 | { |
Jason Ekstrand | 44a3863 | 2013-06-14 10:08:00 -0500 | [diff] [blame] | 2308 | struct weston_pointer *pointer = wl_resource_get_user_data(resource); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2309 | struct weston_surface *surface = NULL; |
| 2310 | |
| 2311 | if (surface_resource) |
Jason Ekstrand | 0f2ef7e | 2013-06-14 10:07:53 -0500 | [diff] [blame] | 2312 | surface = wl_resource_get_user_data(surface_resource); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2313 | |
Kristian Høgsberg | 195b869 | 2013-05-08 15:02:05 -0400 | [diff] [blame] | 2314 | if (pointer->focus == NULL) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2315 | return; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2316 | /* pointer->focus->surface->resource can be NULL. Surfaces like the |
Giulio Camuffo | 1fd4b01 | 2013-06-20 18:13:07 +0200 | [diff] [blame] | 2317 | black_surface used in shell.c for fullscreen don't have |
| 2318 | a resource, but can still have focus */ |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2319 | if (pointer->focus->surface->resource == NULL) |
Giulio Camuffo | 1fd4b01 | 2013-06-20 18:13:07 +0200 | [diff] [blame] | 2320 | return; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2321 | if (wl_resource_get_client(pointer->focus->surface->resource) != client) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2322 | return; |
Kristian Høgsberg | 195b869 | 2013-05-08 15:02:05 -0400 | [diff] [blame] | 2323 | if (pointer->focus_serial - serial > UINT32_MAX / 2) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2324 | return; |
| 2325 | |
Derek Foreman | 4e53c53 | 2015-03-23 10:55:32 -0500 | [diff] [blame] | 2326 | if (!surface) { |
| 2327 | if (pointer->sprite) |
| 2328 | pointer_unmap_sprite(pointer); |
| 2329 | return; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2330 | } |
| 2331 | |
Jonas Ådahl | b407024 | 2015-03-18 15:08:03 +0800 | [diff] [blame] | 2332 | if (pointer->sprite && pointer->sprite->surface == surface && |
| 2333 | pointer->hotspot_x == x && pointer->hotspot_y == y) |
| 2334 | return; |
| 2335 | |
Derek Foreman | 4e53c53 | 2015-03-23 10:55:32 -0500 | [diff] [blame] | 2336 | if (!pointer->sprite || pointer->sprite->surface != surface) { |
| 2337 | if (weston_surface_set_role(surface, "wl_pointer-cursor", |
| 2338 | resource, |
| 2339 | WL_POINTER_ERROR_ROLE) < 0) |
| 2340 | return; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2341 | |
Derek Foreman | 4e53c53 | 2015-03-23 10:55:32 -0500 | [diff] [blame] | 2342 | if (pointer->sprite) |
| 2343 | pointer_unmap_sprite(pointer); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2344 | |
Derek Foreman | 4e53c53 | 2015-03-23 10:55:32 -0500 | [diff] [blame] | 2345 | wl_signal_add(&surface->destroy_signal, |
| 2346 | &pointer->sprite_destroy_listener); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2347 | |
Quentin Glidic | 2edc3d5 | 2016-08-12 10:41:33 +0200 | [diff] [blame] | 2348 | surface->committed = pointer_cursor_surface_committed; |
| 2349 | surface->committed_private = pointer; |
Derek Foreman | 4e53c53 | 2015-03-23 10:55:32 -0500 | [diff] [blame] | 2350 | weston_surface_set_label_func(surface, |
| 2351 | pointer_cursor_surface_get_label); |
| 2352 | pointer->sprite = weston_view_create(surface); |
| 2353 | } |
| 2354 | |
Kristian Høgsberg | 195b869 | 2013-05-08 15:02:05 -0400 | [diff] [blame] | 2355 | pointer->hotspot_x = x; |
| 2356 | pointer->hotspot_y = y; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2357 | |
Jonas Ådahl | 16fe4dc | 2014-09-08 19:33:41 +0200 | [diff] [blame] | 2358 | if (surface->buffer_ref.buffer) { |
Quentin Glidic | 2edc3d5 | 2016-08-12 10:41:33 +0200 | [diff] [blame] | 2359 | pointer_cursor_surface_committed(surface, 0, 0); |
Jonas Ådahl | 16fe4dc | 2014-09-08 19:33:41 +0200 | [diff] [blame] | 2360 | weston_view_schedule_repaint(pointer->sprite); |
| 2361 | } |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2362 | } |
| 2363 | |
Kristian Høgsberg | 69e25fc | 2013-08-13 20:11:02 +0100 | [diff] [blame] | 2364 | static void |
| 2365 | pointer_release(struct wl_client *client, struct wl_resource *resource) |
| 2366 | { |
| 2367 | wl_resource_destroy(resource); |
| 2368 | } |
| 2369 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2370 | static const struct wl_pointer_interface pointer_interface = { |
Kristian Høgsberg | 69e25fc | 2013-08-13 20:11:02 +0100 | [diff] [blame] | 2371 | pointer_set_cursor, |
| 2372 | pointer_release |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2373 | }; |
| 2374 | |
| 2375 | static void |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2376 | seat_get_pointer(struct wl_client *client, struct wl_resource *resource, |
| 2377 | uint32_t id) |
| 2378 | { |
Jason Ekstrand | 44a3863 | 2013-06-14 10:08:00 -0500 | [diff] [blame] | 2379 | struct weston_seat *seat = wl_resource_get_user_data(resource); |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 2380 | /* We use the pointer_state directly, which means we'll |
| 2381 | * give a wl_pointer if the seat has ever had one - even though |
| 2382 | * the spec explicitly states that this request only takes effect |
| 2383 | * if the seat has the pointer capability. |
| 2384 | * |
| 2385 | * This prevents a race between the compositor sending new |
| 2386 | * capabilities and the client trying to use the old ones. |
| 2387 | */ |
| 2388 | struct weston_pointer *pointer = seat->pointer_state; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2389 | struct wl_resource *cr; |
Jonas Ådahl | 2cbf293 | 2015-07-22 12:05:38 +0800 | [diff] [blame] | 2390 | struct weston_pointer_client *pointer_client; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2391 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 2392 | if (!pointer) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2393 | return; |
| 2394 | |
Jason Ekstrand | a85118c | 2013-06-27 20:17:02 -0500 | [diff] [blame] | 2395 | cr = wl_resource_create(client, &wl_pointer_interface, |
| 2396 | wl_resource_get_version(resource), id); |
Kristian Høgsberg | 0ff7908 | 2013-08-06 16:46:25 -0700 | [diff] [blame] | 2397 | if (cr == NULL) { |
| 2398 | wl_client_post_no_memory(client); |
| 2399 | return; |
| 2400 | } |
| 2401 | |
Jonas Ådahl | 2cbf293 | 2015-07-22 12:05:38 +0800 | [diff] [blame] | 2402 | pointer_client = weston_pointer_ensure_pointer_client(pointer, client); |
| 2403 | if (!pointer_client) { |
| 2404 | wl_client_post_no_memory(client); |
| 2405 | return; |
| 2406 | } |
| 2407 | |
| 2408 | wl_list_insert(&pointer_client->pointer_resources, |
| 2409 | wl_resource_get_link(cr)); |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 2410 | wl_resource_set_implementation(cr, &pointer_interface, pointer, |
Jonas Ådahl | 2cbf293 | 2015-07-22 12:05:38 +0800 | [diff] [blame] | 2411 | unbind_pointer_client_resource); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2412 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 2413 | if (pointer->focus && pointer->focus->surface->resource && |
| 2414 | wl_resource_get_client(pointer->focus->surface->resource) == client) { |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2415 | wl_fixed_t sx, sy; |
| 2416 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 2417 | weston_view_from_global_fixed(pointer->focus, |
| 2418 | pointer->x, |
| 2419 | pointer->y, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 2420 | &sx, &sy); |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 2421 | |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 2422 | wl_pointer_send_enter(cr, |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 2423 | pointer->focus_serial, |
| 2424 | pointer->focus->surface->resource, |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 2425 | sx, sy); |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 2426 | pointer_send_frame(cr); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2427 | } |
| 2428 | } |
| 2429 | |
| 2430 | static void |
Kristian Høgsberg | 69e25fc | 2013-08-13 20:11:02 +0100 | [diff] [blame] | 2431 | keyboard_release(struct wl_client *client, struct wl_resource *resource) |
| 2432 | { |
| 2433 | wl_resource_destroy(resource); |
| 2434 | } |
| 2435 | |
| 2436 | static const struct wl_keyboard_interface keyboard_interface = { |
| 2437 | keyboard_release |
| 2438 | }; |
| 2439 | |
Derek Foreman | 280e7dd | 2014-10-03 13:13:42 -0500 | [diff] [blame] | 2440 | static bool |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 2441 | should_send_modifiers_to_client(struct weston_seat *seat, |
| 2442 | struct wl_client *client) |
| 2443 | { |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 2444 | struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat); |
| 2445 | struct weston_pointer *pointer = weston_seat_get_pointer(seat); |
| 2446 | |
| 2447 | if (keyboard && |
| 2448 | keyboard->focus && |
| 2449 | keyboard->focus->resource && |
| 2450 | wl_resource_get_client(keyboard->focus->resource) == client) |
Derek Foreman | 280e7dd | 2014-10-03 13:13:42 -0500 | [diff] [blame] | 2451 | return true; |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 2452 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 2453 | if (pointer && |
| 2454 | pointer->focus && |
| 2455 | pointer->focus->surface->resource && |
| 2456 | wl_resource_get_client(pointer->focus->surface->resource) == client) |
Derek Foreman | 280e7dd | 2014-10-03 13:13:42 -0500 | [diff] [blame] | 2457 | return true; |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 2458 | |
Derek Foreman | 280e7dd | 2014-10-03 13:13:42 -0500 | [diff] [blame] | 2459 | return false; |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 2460 | } |
| 2461 | |
Kristian Høgsberg | 69e25fc | 2013-08-13 20:11:02 +0100 | [diff] [blame] | 2462 | static void |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2463 | seat_get_keyboard(struct wl_client *client, struct wl_resource *resource, |
| 2464 | uint32_t id) |
| 2465 | { |
Jason Ekstrand | 44a3863 | 2013-06-14 10:08:00 -0500 | [diff] [blame] | 2466 | struct weston_seat *seat = wl_resource_get_user_data(resource); |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 2467 | /* We use the keyboard_state directly, which means we'll |
| 2468 | * give a wl_keyboard if the seat has ever had one - even though |
| 2469 | * the spec explicitly states that this request only takes effect |
| 2470 | * if the seat has the keyboard capability. |
| 2471 | * |
| 2472 | * This prevents a race between the compositor sending new |
| 2473 | * capabilities and the client trying to use the old ones. |
| 2474 | */ |
| 2475 | struct weston_keyboard *keyboard = seat->keyboard_state; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2476 | struct wl_resource *cr; |
| 2477 | |
Derek Foreman | 345c9f3 | 2015-06-03 15:53:28 -0500 | [diff] [blame] | 2478 | if (!keyboard) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2479 | return; |
| 2480 | |
Jason Ekstrand | a85118c | 2013-06-27 20:17:02 -0500 | [diff] [blame] | 2481 | cr = wl_resource_create(client, &wl_keyboard_interface, |
| 2482 | wl_resource_get_version(resource), id); |
Kristian Høgsberg | 0ff7908 | 2013-08-06 16:46:25 -0700 | [diff] [blame] | 2483 | if (cr == NULL) { |
| 2484 | wl_client_post_no_memory(client); |
| 2485 | return; |
| 2486 | } |
| 2487 | |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 2488 | /* May be moved to focused list later by either |
| 2489 | * weston_keyboard_set_focus or directly if this client is already |
| 2490 | * focused */ |
Derek Foreman | 345c9f3 | 2015-06-03 15:53:28 -0500 | [diff] [blame] | 2491 | wl_list_insert(&keyboard->resource_list, wl_resource_get_link(cr)); |
Kristian Høgsberg | 69e25fc | 2013-08-13 20:11:02 +0100 | [diff] [blame] | 2492 | wl_resource_set_implementation(cr, &keyboard_interface, |
Alexandros Frantzis | 7a314d6 | 2018-01-26 18:47:56 +0200 | [diff] [blame^] | 2493 | keyboard, unbind_resource); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2494 | |
Jonny Lamb | 66a41a0 | 2014-08-12 14:58:25 +0200 | [diff] [blame] | 2495 | if (wl_resource_get_version(cr) >= WL_KEYBOARD_REPEAT_INFO_SINCE_VERSION) { |
| 2496 | wl_keyboard_send_repeat_info(cr, |
| 2497 | seat->compositor->kb_repeat_rate, |
| 2498 | seat->compositor->kb_repeat_delay); |
| 2499 | } |
Jasper St. Pierre | d8c6aeb | 2014-08-04 13:43:24 -0400 | [diff] [blame] | 2500 | |
Derek Foreman | 185d158 | 2017-06-28 11:17:23 -0500 | [diff] [blame] | 2501 | wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, |
| 2502 | keyboard->xkb_info->keymap_fd, |
| 2503 | keyboard->xkb_info->keymap_size); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2504 | |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 2505 | if (should_send_modifiers_to_client(seat, client)) { |
Derek Foreman | 345c9f3 | 2015-06-03 15:53:28 -0500 | [diff] [blame] | 2506 | send_modifiers_to_resource(keyboard, |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 2507 | cr, |
Derek Foreman | 345c9f3 | 2015-06-03 15:53:28 -0500 | [diff] [blame] | 2508 | keyboard->focus_serial); |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 2509 | } |
| 2510 | |
Derek Foreman | 345c9f3 | 2015-06-03 15:53:28 -0500 | [diff] [blame] | 2511 | if (keyboard->focus && keyboard->focus->resource && |
| 2512 | wl_resource_get_client(keyboard->focus->resource) == client) { |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 2513 | struct weston_surface *surface = |
Derek Foreman | 345c9f3 | 2015-06-03 15:53:28 -0500 | [diff] [blame] | 2514 | (struct weston_surface *)keyboard->focus; |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 2515 | |
| 2516 | wl_list_remove(wl_resource_get_link(cr)); |
Derek Foreman | 345c9f3 | 2015-06-03 15:53:28 -0500 | [diff] [blame] | 2517 | wl_list_insert(&keyboard->focus_resource_list, |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 2518 | wl_resource_get_link(cr)); |
| 2519 | wl_keyboard_send_enter(cr, |
Derek Foreman | 345c9f3 | 2015-06-03 15:53:28 -0500 | [diff] [blame] | 2520 | keyboard->focus_serial, |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 2521 | surface->resource, |
Derek Foreman | 345c9f3 | 2015-06-03 15:53:28 -0500 | [diff] [blame] | 2522 | &keyboard->keys); |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 2523 | |
| 2524 | /* If this is the first keyboard resource for this |
| 2525 | * client... */ |
Derek Foreman | 345c9f3 | 2015-06-03 15:53:28 -0500 | [diff] [blame] | 2526 | if (keyboard->focus_resource_list.prev == |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 2527 | wl_resource_get_link(cr)) |
| 2528 | wl_data_device_set_keyboard_focus(seat); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2529 | } |
| 2530 | } |
| 2531 | |
| 2532 | static void |
Kristian Høgsberg | 69e25fc | 2013-08-13 20:11:02 +0100 | [diff] [blame] | 2533 | touch_release(struct wl_client *client, struct wl_resource *resource) |
| 2534 | { |
| 2535 | wl_resource_destroy(resource); |
| 2536 | } |
| 2537 | |
| 2538 | static const struct wl_touch_interface touch_interface = { |
| 2539 | touch_release |
| 2540 | }; |
| 2541 | |
| 2542 | static void |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2543 | seat_get_touch(struct wl_client *client, struct wl_resource *resource, |
| 2544 | uint32_t id) |
| 2545 | { |
Jason Ekstrand | 44a3863 | 2013-06-14 10:08:00 -0500 | [diff] [blame] | 2546 | struct weston_seat *seat = wl_resource_get_user_data(resource); |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 2547 | /* We use the touch_state directly, which means we'll |
| 2548 | * give a wl_touch if the seat has ever had one - even though |
| 2549 | * the spec explicitly states that this request only takes effect |
| 2550 | * if the seat has the touch capability. |
| 2551 | * |
| 2552 | * This prevents a race between the compositor sending new |
| 2553 | * capabilities and the client trying to use the old ones. |
| 2554 | */ |
| 2555 | struct weston_touch *touch = seat->touch_state; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2556 | struct wl_resource *cr; |
| 2557 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 2558 | if (!touch) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2559 | return; |
| 2560 | |
Jason Ekstrand | a85118c | 2013-06-27 20:17:02 -0500 | [diff] [blame] | 2561 | cr = wl_resource_create(client, &wl_touch_interface, |
| 2562 | wl_resource_get_version(resource), id); |
Kristian Høgsberg | 0ff7908 | 2013-08-06 16:46:25 -0700 | [diff] [blame] | 2563 | if (cr == NULL) { |
| 2564 | wl_client_post_no_memory(client); |
| 2565 | return; |
| 2566 | } |
| 2567 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 2568 | if (touch->focus && |
| 2569 | wl_resource_get_client(touch->focus->surface->resource) == client) { |
Chokshi, Mitul | d669714 | 2015-10-09 08:28:47 +0000 | [diff] [blame] | 2570 | wl_list_insert(&touch->focus_resource_list, |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 2571 | wl_resource_get_link(cr)); |
| 2572 | } else { |
Chokshi, Mitul | d669714 | 2015-10-09 08:28:47 +0000 | [diff] [blame] | 2573 | wl_list_insert(&touch->resource_list, |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 2574 | wl_resource_get_link(cr)); |
| 2575 | } |
Kristian Høgsberg | 69e25fc | 2013-08-13 20:11:02 +0100 | [diff] [blame] | 2576 | wl_resource_set_implementation(cr, &touch_interface, |
| 2577 | seat, unbind_resource); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2578 | } |
| 2579 | |
Quentin Glidic | aab1d36 | 2016-03-13 17:49:08 +0100 | [diff] [blame] | 2580 | static void |
| 2581 | seat_release(struct wl_client *client, struct wl_resource *resource) |
| 2582 | { |
| 2583 | wl_resource_destroy(resource); |
| 2584 | } |
| 2585 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2586 | static const struct wl_seat_interface seat_interface = { |
| 2587 | seat_get_pointer, |
| 2588 | seat_get_keyboard, |
| 2589 | seat_get_touch, |
Quentin Glidic | aab1d36 | 2016-03-13 17:49:08 +0100 | [diff] [blame] | 2590 | seat_release, |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2591 | }; |
| 2592 | |
| 2593 | static void |
| 2594 | bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id) |
| 2595 | { |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 2596 | struct weston_seat *seat = data; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2597 | struct wl_resource *resource; |
| 2598 | enum wl_seat_capability caps = 0; |
| 2599 | |
Jason Ekstrand | a85118c | 2013-06-27 20:17:02 -0500 | [diff] [blame] | 2600 | resource = wl_resource_create(client, |
Derek Foreman | 1909c10 | 2015-11-26 14:17:47 -0600 | [diff] [blame] | 2601 | &wl_seat_interface, version, id); |
Jason Ekstrand | 44a3863 | 2013-06-14 10:08:00 -0500 | [diff] [blame] | 2602 | wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource)); |
Jason Ekstrand | a85118c | 2013-06-27 20:17:02 -0500 | [diff] [blame] | 2603 | wl_resource_set_implementation(resource, &seat_interface, data, |
| 2604 | unbind_resource); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2605 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 2606 | if (weston_seat_get_pointer(seat)) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2607 | caps |= WL_SEAT_CAPABILITY_POINTER; |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 2608 | if (weston_seat_get_keyboard(seat)) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2609 | caps |= WL_SEAT_CAPABILITY_KEYBOARD; |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 2610 | if (weston_seat_get_touch(seat)) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2611 | caps |= WL_SEAT_CAPABILITY_TOUCH; |
| 2612 | |
| 2613 | wl_seat_send_capabilities(resource, caps); |
Jasper St. Pierre | 0013a29 | 2014-08-07 16:43:11 -0400 | [diff] [blame] | 2614 | if (version >= WL_SEAT_NAME_SINCE_VERSION) |
Rob Bradford | e445ae6 | 2013-05-31 18:09:51 +0100 | [diff] [blame] | 2615 | wl_seat_send_name(resource, seat->seat_name); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2616 | } |
| 2617 | |
Jonas Ådahl | 30d61d8 | 2014-10-22 21:21:17 +0200 | [diff] [blame] | 2618 | static void |
| 2619 | relative_pointer_destroy(struct wl_client *client, |
| 2620 | struct wl_resource *resource) |
| 2621 | { |
| 2622 | wl_resource_destroy(resource); |
| 2623 | } |
| 2624 | |
| 2625 | static const struct zwp_relative_pointer_v1_interface relative_pointer_interface = { |
| 2626 | relative_pointer_destroy |
| 2627 | }; |
| 2628 | |
| 2629 | static void |
| 2630 | relative_pointer_manager_destroy(struct wl_client *client, |
| 2631 | struct wl_resource *resource) |
| 2632 | { |
| 2633 | wl_resource_destroy(resource); |
| 2634 | } |
| 2635 | |
| 2636 | static void |
| 2637 | relative_pointer_manager_get_relative_pointer(struct wl_client *client, |
| 2638 | struct wl_resource *resource, |
| 2639 | uint32_t id, |
| 2640 | struct wl_resource *pointer_resource) |
| 2641 | { |
| 2642 | struct weston_pointer *pointer = |
| 2643 | wl_resource_get_user_data(pointer_resource); |
| 2644 | struct weston_pointer_client *pointer_client; |
| 2645 | struct wl_resource *cr; |
| 2646 | |
| 2647 | cr = wl_resource_create(client, &zwp_relative_pointer_v1_interface, |
| 2648 | wl_resource_get_version(resource), id); |
| 2649 | if (cr == NULL) { |
| 2650 | wl_client_post_no_memory(client); |
| 2651 | return; |
| 2652 | } |
| 2653 | |
| 2654 | pointer_client = weston_pointer_ensure_pointer_client(pointer, client); |
| 2655 | if (!pointer_client) { |
| 2656 | wl_client_post_no_memory(client); |
| 2657 | return; |
| 2658 | } |
| 2659 | |
| 2660 | wl_list_insert(&pointer_client->relative_pointer_resources, |
| 2661 | wl_resource_get_link(cr)); |
| 2662 | wl_resource_set_implementation(cr, &relative_pointer_interface, |
| 2663 | pointer, |
| 2664 | unbind_pointer_client_resource); |
| 2665 | } |
| 2666 | |
| 2667 | static const struct zwp_relative_pointer_manager_v1_interface relative_pointer_manager = { |
| 2668 | relative_pointer_manager_destroy, |
| 2669 | relative_pointer_manager_get_relative_pointer, |
| 2670 | }; |
| 2671 | |
| 2672 | static void |
| 2673 | bind_relative_pointer_manager(struct wl_client *client, void *data, |
| 2674 | uint32_t version, uint32_t id) |
| 2675 | { |
| 2676 | struct weston_compositor *compositor = data; |
| 2677 | struct wl_resource *resource; |
| 2678 | |
| 2679 | resource = wl_resource_create(client, |
| 2680 | &zwp_relative_pointer_manager_v1_interface, |
| 2681 | 1, id); |
| 2682 | |
| 2683 | wl_resource_set_implementation(resource, &relative_pointer_manager, |
| 2684 | compositor, |
| 2685 | NULL); |
| 2686 | } |
| 2687 | |
Giulio Camuffo | 0358af4 | 2016-06-02 21:48:08 +0300 | [diff] [blame] | 2688 | WL_EXPORT int |
| 2689 | weston_compositor_set_xkb_rule_names(struct weston_compositor *ec, |
| 2690 | struct xkb_rule_names *names) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2691 | { |
| 2692 | if (ec->xkb_context == NULL) { |
| 2693 | ec->xkb_context = xkb_context_new(0); |
| 2694 | if (ec->xkb_context == NULL) { |
| 2695 | weston_log("failed to create XKB context\n"); |
| 2696 | return -1; |
| 2697 | } |
| 2698 | } |
| 2699 | |
| 2700 | if (names) |
| 2701 | ec->xkb_names = *names; |
| 2702 | if (!ec->xkb_names.rules) |
| 2703 | ec->xkb_names.rules = strdup("evdev"); |
| 2704 | if (!ec->xkb_names.model) |
| 2705 | ec->xkb_names.model = strdup("pc105"); |
| 2706 | if (!ec->xkb_names.layout) |
| 2707 | ec->xkb_names.layout = strdup("us"); |
| 2708 | |
| 2709 | return 0; |
| 2710 | } |
| 2711 | |
Stefan Schmidt | fda2652 | 2013-09-17 10:54:09 +0100 | [diff] [blame] | 2712 | static void |
Andrew Wedgbury | 9a6f02a | 2013-09-05 13:31:40 +0000 | [diff] [blame] | 2713 | weston_xkb_info_destroy(struct weston_xkb_info *xkb_info) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2714 | { |
Andrew Wedgbury | 9a6f02a | 2013-09-05 13:31:40 +0000 | [diff] [blame] | 2715 | if (--xkb_info->ref_count > 0) |
| 2716 | return; |
| 2717 | |
Ran Benita | c9c7415 | 2014-08-19 23:59:52 +0300 | [diff] [blame] | 2718 | xkb_keymap_unref(xkb_info->keymap); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2719 | |
| 2720 | if (xkb_info->keymap_area) |
| 2721 | munmap(xkb_info->keymap_area, xkb_info->keymap_size); |
| 2722 | if (xkb_info->keymap_fd >= 0) |
| 2723 | close(xkb_info->keymap_fd); |
Andrew Wedgbury | 9a6f02a | 2013-09-05 13:31:40 +0000 | [diff] [blame] | 2724 | free(xkb_info); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2725 | } |
| 2726 | |
| 2727 | void |
| 2728 | weston_compositor_xkb_destroy(struct weston_compositor *ec) |
| 2729 | { |
| 2730 | free((char *) ec->xkb_names.rules); |
| 2731 | free((char *) ec->xkb_names.model); |
| 2732 | free((char *) ec->xkb_names.layout); |
| 2733 | free((char *) ec->xkb_names.variant); |
| 2734 | free((char *) ec->xkb_names.options); |
Stefan Schmidt | fda2652 | 2013-09-17 10:54:09 +0100 | [diff] [blame] | 2735 | |
Andrew Wedgbury | 9a6f02a | 2013-09-05 13:31:40 +0000 | [diff] [blame] | 2736 | if (ec->xkb_info) |
| 2737 | weston_xkb_info_destroy(ec->xkb_info); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2738 | xkb_context_unref(ec->xkb_context); |
| 2739 | } |
| 2740 | |
Andrew Wedgbury | 9a6f02a | 2013-09-05 13:31:40 +0000 | [diff] [blame] | 2741 | static struct weston_xkb_info * |
| 2742 | weston_xkb_info_create(struct xkb_keymap *keymap) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2743 | { |
Andrew Wedgbury | 9a6f02a | 2013-09-05 13:31:40 +0000 | [diff] [blame] | 2744 | struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info); |
| 2745 | if (xkb_info == NULL) |
| 2746 | return NULL; |
| 2747 | |
Ran Benita | 2e1968f | 2014-08-19 23:59:51 +0300 | [diff] [blame] | 2748 | xkb_info->keymap = xkb_keymap_ref(keymap); |
Andrew Wedgbury | 9a6f02a | 2013-09-05 13:31:40 +0000 | [diff] [blame] | 2749 | xkb_info->ref_count = 1; |
| 2750 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2751 | char *keymap_str; |
| 2752 | |
Ran Benita | 2e1968f | 2014-08-19 23:59:51 +0300 | [diff] [blame] | 2753 | xkb_info->shift_mod = xkb_keymap_mod_get_index(xkb_info->keymap, |
| 2754 | XKB_MOD_NAME_SHIFT); |
| 2755 | xkb_info->caps_mod = xkb_keymap_mod_get_index(xkb_info->keymap, |
| 2756 | XKB_MOD_NAME_CAPS); |
| 2757 | xkb_info->ctrl_mod = xkb_keymap_mod_get_index(xkb_info->keymap, |
| 2758 | XKB_MOD_NAME_CTRL); |
| 2759 | xkb_info->alt_mod = xkb_keymap_mod_get_index(xkb_info->keymap, |
| 2760 | XKB_MOD_NAME_ALT); |
| 2761 | xkb_info->mod2_mod = xkb_keymap_mod_get_index(xkb_info->keymap, |
| 2762 | "Mod2"); |
| 2763 | xkb_info->mod3_mod = xkb_keymap_mod_get_index(xkb_info->keymap, |
| 2764 | "Mod3"); |
| 2765 | xkb_info->super_mod = xkb_keymap_mod_get_index(xkb_info->keymap, |
| 2766 | XKB_MOD_NAME_LOGO); |
| 2767 | xkb_info->mod5_mod = xkb_keymap_mod_get_index(xkb_info->keymap, |
| 2768 | "Mod5"); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2769 | |
Ran Benita | 2e1968f | 2014-08-19 23:59:51 +0300 | [diff] [blame] | 2770 | xkb_info->num_led = xkb_keymap_led_get_index(xkb_info->keymap, |
| 2771 | XKB_LED_NAME_NUM); |
| 2772 | xkb_info->caps_led = xkb_keymap_led_get_index(xkb_info->keymap, |
| 2773 | XKB_LED_NAME_CAPS); |
| 2774 | xkb_info->scroll_led = xkb_keymap_led_get_index(xkb_info->keymap, |
| 2775 | XKB_LED_NAME_SCROLL); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2776 | |
Ran Benita | 2e1968f | 2014-08-19 23:59:51 +0300 | [diff] [blame] | 2777 | keymap_str = xkb_keymap_get_as_string(xkb_info->keymap, |
| 2778 | XKB_KEYMAP_FORMAT_TEXT_V1); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2779 | if (keymap_str == NULL) { |
| 2780 | weston_log("failed to get string version of keymap\n"); |
Andrew Wedgbury | 9a6f02a | 2013-09-05 13:31:40 +0000 | [diff] [blame] | 2781 | goto err_keymap; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2782 | } |
| 2783 | xkb_info->keymap_size = strlen(keymap_str) + 1; |
| 2784 | |
| 2785 | xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size); |
| 2786 | if (xkb_info->keymap_fd < 0) { |
| 2787 | weston_log("creating a keymap file for %lu bytes failed: %m\n", |
| 2788 | (unsigned long) xkb_info->keymap_size); |
| 2789 | goto err_keymap_str; |
| 2790 | } |
| 2791 | |
| 2792 | xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size, |
| 2793 | PROT_READ | PROT_WRITE, |
| 2794 | MAP_SHARED, xkb_info->keymap_fd, 0); |
| 2795 | if (xkb_info->keymap_area == MAP_FAILED) { |
| 2796 | weston_log("failed to mmap() %lu bytes\n", |
| 2797 | (unsigned long) xkb_info->keymap_size); |
| 2798 | goto err_dev_zero; |
| 2799 | } |
| 2800 | strcpy(xkb_info->keymap_area, keymap_str); |
| 2801 | free(keymap_str); |
| 2802 | |
Andrew Wedgbury | 9a6f02a | 2013-09-05 13:31:40 +0000 | [diff] [blame] | 2803 | return xkb_info; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2804 | |
| 2805 | err_dev_zero: |
| 2806 | close(xkb_info->keymap_fd); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2807 | err_keymap_str: |
| 2808 | free(keymap_str); |
Andrew Wedgbury | 9a6f02a | 2013-09-05 13:31:40 +0000 | [diff] [blame] | 2809 | err_keymap: |
Ran Benita | 2e1968f | 2014-08-19 23:59:51 +0300 | [diff] [blame] | 2810 | xkb_keymap_unref(xkb_info->keymap); |
Andrew Wedgbury | 9a6f02a | 2013-09-05 13:31:40 +0000 | [diff] [blame] | 2811 | free(xkb_info); |
| 2812 | return NULL; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2813 | } |
| 2814 | |
| 2815 | static int |
| 2816 | weston_compositor_build_global_keymap(struct weston_compositor *ec) |
| 2817 | { |
Andrew Wedgbury | 9a6f02a | 2013-09-05 13:31:40 +0000 | [diff] [blame] | 2818 | struct xkb_keymap *keymap; |
| 2819 | |
| 2820 | if (ec->xkb_info != NULL) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2821 | return 0; |
| 2822 | |
Ran Benita | 2e1968f | 2014-08-19 23:59:51 +0300 | [diff] [blame] | 2823 | keymap = xkb_keymap_new_from_names(ec->xkb_context, |
| 2824 | &ec->xkb_names, |
| 2825 | 0); |
Andrew Wedgbury | 9a6f02a | 2013-09-05 13:31:40 +0000 | [diff] [blame] | 2826 | if (keymap == NULL) { |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2827 | weston_log("failed to compile global XKB keymap\n"); |
| 2828 | weston_log(" tried rules %s, model %s, layout %s, variant %s, " |
| 2829 | "options %s\n", |
| 2830 | ec->xkb_names.rules, ec->xkb_names.model, |
| 2831 | ec->xkb_names.layout, ec->xkb_names.variant, |
| 2832 | ec->xkb_names.options); |
| 2833 | return -1; |
| 2834 | } |
| 2835 | |
Andrew Wedgbury | 9a6f02a | 2013-09-05 13:31:40 +0000 | [diff] [blame] | 2836 | ec->xkb_info = weston_xkb_info_create(keymap); |
Rui Matos | 73d9395 | 2013-10-24 19:28:41 +0200 | [diff] [blame] | 2837 | xkb_keymap_unref(keymap); |
Stefan Schmidt | fda2652 | 2013-09-17 10:54:09 +0100 | [diff] [blame] | 2838 | if (ec->xkb_info == NULL) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2839 | return -1; |
| 2840 | |
| 2841 | return 0; |
| 2842 | } |
| 2843 | |
Rui Matos | 65196bc | 2013-10-10 19:44:19 +0200 | [diff] [blame] | 2844 | WL_EXPORT void |
| 2845 | weston_seat_update_keymap(struct weston_seat *seat, struct xkb_keymap *keymap) |
| 2846 | { |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 2847 | struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat); |
| 2848 | |
| 2849 | if (!keyboard || !keymap) |
Rui Matos | 65196bc | 2013-10-10 19:44:19 +0200 | [diff] [blame] | 2850 | return; |
| 2851 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 2852 | xkb_keymap_unref(keyboard->pending_keymap); |
| 2853 | keyboard->pending_keymap = xkb_keymap_ref(keymap); |
Rui Matos | 65196bc | 2013-10-10 19:44:19 +0200 | [diff] [blame] | 2854 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 2855 | if (keyboard->keys.size == 0) |
Rui Matos | 65196bc | 2013-10-10 19:44:19 +0200 | [diff] [blame] | 2856 | update_keymap(seat); |
Rui Matos | 65196bc | 2013-10-10 19:44:19 +0200 | [diff] [blame] | 2857 | } |
| 2858 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2859 | WL_EXPORT int |
| 2860 | weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap) |
| 2861 | { |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 2862 | struct weston_keyboard *keyboard; |
| 2863 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 2864 | if (seat->keyboard_state) { |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 2865 | seat->keyboard_device_count += 1; |
| 2866 | if (seat->keyboard_device_count == 1) |
| 2867 | seat_send_updated_caps(seat); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2868 | return 0; |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 2869 | } |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2870 | |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 2871 | keyboard = weston_keyboard_create(); |
| 2872 | if (keyboard == NULL) { |
| 2873 | weston_log("failed to allocate weston keyboard struct\n"); |
| 2874 | return -1; |
| 2875 | } |
| 2876 | |
Derek Foreman | 185d158 | 2017-06-28 11:17:23 -0500 | [diff] [blame] | 2877 | if (keymap != NULL) { |
| 2878 | keyboard->xkb_info = weston_xkb_info_create(keymap); |
| 2879 | if (keyboard->xkb_info == NULL) |
Ander Conselvan de Oliveira | 4d363cf | 2014-01-31 17:35:45 +0200 | [diff] [blame] | 2880 | goto err; |
Derek Foreman | 185d158 | 2017-06-28 11:17:23 -0500 | [diff] [blame] | 2881 | } else { |
| 2882 | if (weston_compositor_build_global_keymap(seat->compositor) < 0) |
| 2883 | goto err; |
| 2884 | keyboard->xkb_info = seat->compositor->xkb_info; |
| 2885 | keyboard->xkb_info->ref_count++; |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 2886 | } |
Derek Foreman | 185d158 | 2017-06-28 11:17:23 -0500 | [diff] [blame] | 2887 | |
| 2888 | keyboard->xkb_state.state = xkb_state_new(keyboard->xkb_info->keymap); |
| 2889 | if (keyboard->xkb_state.state == NULL) { |
| 2890 | weston_log("failed to initialise XKB state\n"); |
| 2891 | goto err; |
| 2892 | } |
| 2893 | |
| 2894 | keyboard->xkb_state.leds = 0; |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 2895 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 2896 | seat->keyboard_state = keyboard; |
Ander Conselvan de Oliveira | 4d363cf | 2014-01-31 17:35:45 +0200 | [diff] [blame] | 2897 | seat->keyboard_device_count = 1; |
| 2898 | keyboard->seat = seat; |
| 2899 | |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 2900 | seat_send_updated_caps(seat); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2901 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2902 | return 0; |
Ander Conselvan de Oliveira | 4d363cf | 2014-01-31 17:35:45 +0200 | [diff] [blame] | 2903 | |
| 2904 | err: |
| 2905 | if (keyboard->xkb_info) |
| 2906 | weston_xkb_info_destroy(keyboard->xkb_info); |
| 2907 | free(keyboard); |
| 2908 | |
| 2909 | return -1; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2910 | } |
| 2911 | |
Jonas Ådahl | 91fed54 | 2013-12-03 09:14:27 +0100 | [diff] [blame] | 2912 | static void |
| 2913 | weston_keyboard_reset_state(struct weston_keyboard *keyboard) |
| 2914 | { |
| 2915 | struct weston_seat *seat = keyboard->seat; |
| 2916 | struct xkb_state *state; |
| 2917 | |
Derek Foreman | 185d158 | 2017-06-28 11:17:23 -0500 | [diff] [blame] | 2918 | state = xkb_state_new(keyboard->xkb_info->keymap); |
| 2919 | if (!state) { |
| 2920 | weston_log("failed to reset XKB state\n"); |
| 2921 | return; |
Jonas Ådahl | 91fed54 | 2013-12-03 09:14:27 +0100 | [diff] [blame] | 2922 | } |
Derek Foreman | 185d158 | 2017-06-28 11:17:23 -0500 | [diff] [blame] | 2923 | xkb_state_unref(keyboard->xkb_state.state); |
| 2924 | keyboard->xkb_state.state = state; |
| 2925 | |
| 2926 | keyboard->xkb_state.leds = 0; |
Jonas Ådahl | 91fed54 | 2013-12-03 09:14:27 +0100 | [diff] [blame] | 2927 | |
| 2928 | seat->modifier_state = 0; |
| 2929 | } |
| 2930 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2931 | WL_EXPORT void |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 2932 | weston_seat_release_keyboard(struct weston_seat *seat) |
| 2933 | { |
| 2934 | seat->keyboard_device_count--; |
Derek Foreman | d621df2 | 2014-11-19 11:04:12 -0600 | [diff] [blame] | 2935 | assert(seat->keyboard_device_count >= 0); |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 2936 | if (seat->keyboard_device_count == 0) { |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 2937 | weston_keyboard_set_focus(seat->keyboard_state, NULL); |
| 2938 | weston_keyboard_cancel_grab(seat->keyboard_state); |
| 2939 | weston_keyboard_reset_state(seat->keyboard_state); |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 2940 | seat_send_updated_caps(seat); |
| 2941 | } |
| 2942 | } |
| 2943 | |
| 2944 | WL_EXPORT void |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2945 | weston_seat_init_pointer(struct weston_seat *seat) |
| 2946 | { |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 2947 | struct weston_pointer *pointer; |
| 2948 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 2949 | if (seat->pointer_state) { |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 2950 | seat->pointer_device_count += 1; |
| 2951 | if (seat->pointer_device_count == 1) |
| 2952 | seat_send_updated_caps(seat); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2953 | return; |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 2954 | } |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2955 | |
Giulio Camuffo | cdb4d29 | 2013-11-14 23:42:53 +0100 | [diff] [blame] | 2956 | pointer = weston_pointer_create(seat); |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 2957 | if (pointer == NULL) |
| 2958 | return; |
| 2959 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 2960 | seat->pointer_state = pointer; |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 2961 | seat->pointer_device_count = 1; |
| 2962 | pointer->seat = seat; |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 2963 | |
| 2964 | seat_send_updated_caps(seat); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2965 | } |
| 2966 | |
| 2967 | WL_EXPORT void |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 2968 | weston_seat_release_pointer(struct weston_seat *seat) |
| 2969 | { |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 2970 | struct weston_pointer *pointer = seat->pointer_state; |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 2971 | |
| 2972 | seat->pointer_device_count--; |
| 2973 | if (seat->pointer_device_count == 0) { |
Derek Foreman | f9318d1 | 2015-05-11 15:40:11 -0500 | [diff] [blame] | 2974 | weston_pointer_clear_focus(pointer); |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 2975 | weston_pointer_cancel_grab(pointer); |
Jonas Ådahl | 630bae8 | 2013-10-17 23:04:06 +0200 | [diff] [blame] | 2976 | |
Jonas Ådahl | a493274 | 2013-10-17 23:04:07 +0200 | [diff] [blame] | 2977 | if (pointer->sprite) |
| 2978 | pointer_unmap_sprite(pointer); |
| 2979 | |
Jonas Ådahl | 3e12e63 | 2013-12-02 22:05:05 +0100 | [diff] [blame] | 2980 | weston_pointer_reset_state(pointer); |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 2981 | seat_send_updated_caps(seat); |
Derek Foreman | fd5ca51 | 2015-01-07 15:00:25 -0600 | [diff] [blame] | 2982 | |
| 2983 | /* seat->pointer is intentionally not destroyed so that |
| 2984 | * a newly attached pointer on this seat will retain |
| 2985 | * the previous cursor co-ordinates. |
| 2986 | */ |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 2987 | } |
| 2988 | } |
| 2989 | |
| 2990 | WL_EXPORT void |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2991 | weston_seat_init_touch(struct weston_seat *seat) |
| 2992 | { |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 2993 | struct weston_touch *touch; |
| 2994 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 2995 | if (seat->touch_state) { |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 2996 | seat->touch_device_count += 1; |
| 2997 | if (seat->touch_device_count == 1) |
| 2998 | seat_send_updated_caps(seat); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2999 | return; |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 3000 | } |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 3001 | |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 3002 | touch = weston_touch_create(); |
| 3003 | if (touch == NULL) |
| 3004 | return; |
| 3005 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 3006 | seat->touch_state = touch; |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 3007 | seat->touch_device_count = 1; |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 3008 | touch->seat = seat; |
| 3009 | |
| 3010 | seat_send_updated_caps(seat); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 3011 | } |
| 3012 | |
| 3013 | WL_EXPORT void |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 3014 | weston_seat_release_touch(struct weston_seat *seat) |
| 3015 | { |
| 3016 | seat->touch_device_count--; |
| 3017 | if (seat->touch_device_count == 0) { |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 3018 | weston_touch_set_focus(seat->touch_state, NULL); |
| 3019 | weston_touch_cancel_grab(seat->touch_state); |
| 3020 | weston_touch_reset_state(seat->touch_state); |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 3021 | seat_send_updated_caps(seat); |
| 3022 | } |
| 3023 | } |
| 3024 | |
| 3025 | WL_EXPORT void |
Rob Bradford | 9af5f9e | 2013-05-31 18:09:50 +0100 | [diff] [blame] | 3026 | weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec, |
| 3027 | const char *seat_name) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 3028 | { |
Kristian Høgsberg | 4a2a274 | 2013-05-06 22:24:50 -0400 | [diff] [blame] | 3029 | memset(seat, 0, sizeof *seat); |
| 3030 | |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 3031 | seat->selection_data_source = NULL; |
| 3032 | wl_list_init(&seat->base_resource_list); |
| 3033 | wl_signal_init(&seat->selection_signal); |
| 3034 | wl_list_init(&seat->drag_resource_list); |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 3035 | wl_signal_init(&seat->destroy_signal); |
Jason Ekstrand | a4ab542 | 2014-04-02 19:53:45 -0500 | [diff] [blame] | 3036 | wl_signal_init(&seat->updated_caps_signal); |
Kristian Høgsberg | 4a2a274 | 2013-05-06 22:24:50 -0400 | [diff] [blame] | 3037 | |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 3038 | seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 5, |
Kristian Høgsberg | 919cddb | 2013-07-08 19:03:57 -0400 | [diff] [blame] | 3039 | seat, bind_seat); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 3040 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 3041 | seat->compositor = ec; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 3042 | seat->modifier_state = 0; |
Rob Bradford | 9af5f9e | 2013-05-31 18:09:50 +0100 | [diff] [blame] | 3043 | seat->seat_name = strdup(seat_name); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 3044 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 3045 | wl_list_insert(ec->seat_list.prev, &seat->link); |
| 3046 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 3047 | clipboard_create(seat); |
| 3048 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 3049 | wl_signal_emit(&ec->seat_created_signal, seat); |
| 3050 | } |
| 3051 | |
| 3052 | WL_EXPORT void |
| 3053 | weston_seat_release(struct weston_seat *seat) |
| 3054 | { |
| 3055 | wl_list_remove(&seat->link); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 3056 | |
Jonas Ådahl | 1afb238 | 2014-01-03 19:46:51 +0100 | [diff] [blame] | 3057 | if (seat->saved_kbd_focus) |
| 3058 | wl_list_remove(&seat->saved_kbd_focus_listener.link); |
| 3059 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 3060 | if (seat->pointer_state) |
| 3061 | weston_pointer_destroy(seat->pointer_state); |
| 3062 | if (seat->keyboard_state) |
| 3063 | weston_keyboard_destroy(seat->keyboard_state); |
| 3064 | if (seat->touch_state) |
| 3065 | weston_touch_destroy(seat->touch_state); |
Kristian Høgsberg | 4a2a274 | 2013-05-06 22:24:50 -0400 | [diff] [blame] | 3066 | |
Rob Bradford | 9af5f9e | 2013-05-31 18:09:50 +0100 | [diff] [blame] | 3067 | free (seat->seat_name); |
| 3068 | |
Kristian Høgsberg | 919cddb | 2013-07-08 19:03:57 -0400 | [diff] [blame] | 3069 | wl_global_destroy(seat->global); |
Kristian Høgsberg | aaadc77 | 2013-07-08 16:20:31 -0400 | [diff] [blame] | 3070 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 3071 | wl_signal_emit(&seat->destroy_signal, seat); |
| 3072 | } |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 3073 | |
| 3074 | /** Get a seat's keyboard pointer |
| 3075 | * |
| 3076 | * \param seat The seat to query |
| 3077 | * \return The seat's keyboard pointer, or NULL if no keyboard is present |
| 3078 | * |
| 3079 | * The keyboard pointer for a seat isn't freed when all keyboards are removed, |
| 3080 | * so it should only be used when the seat's keyboard_device_count is greater |
| 3081 | * than zero. This function does that test and only returns a pointer |
| 3082 | * when a keyboard is present. |
| 3083 | */ |
| 3084 | WL_EXPORT struct weston_keyboard * |
| 3085 | weston_seat_get_keyboard(struct weston_seat *seat) |
| 3086 | { |
| 3087 | if (!seat) |
| 3088 | return NULL; |
| 3089 | |
| 3090 | if (seat->keyboard_device_count) |
| 3091 | return seat->keyboard_state; |
| 3092 | |
| 3093 | return NULL; |
| 3094 | } |
| 3095 | |
| 3096 | /** Get a seat's pointer pointer |
| 3097 | * |
| 3098 | * \param seat The seat to query |
| 3099 | * \return The seat's pointer pointer, or NULL if no pointer device is present |
| 3100 | * |
| 3101 | * The pointer pointer for a seat isn't freed when all mice are removed, |
| 3102 | * so it should only be used when the seat's pointer_device_count is greater |
| 3103 | * than zero. This function does that test and only returns a pointer |
| 3104 | * when a pointing device is present. |
| 3105 | */ |
| 3106 | WL_EXPORT struct weston_pointer * |
| 3107 | weston_seat_get_pointer(struct weston_seat *seat) |
| 3108 | { |
| 3109 | if (!seat) |
| 3110 | return NULL; |
| 3111 | |
| 3112 | if (seat->pointer_device_count) |
| 3113 | return seat->pointer_state; |
| 3114 | |
| 3115 | return NULL; |
| 3116 | } |
| 3117 | |
Jonas Ådahl | d3414f2 | 2016-07-22 17:56:31 +0800 | [diff] [blame] | 3118 | static const struct zwp_locked_pointer_v1_interface locked_pointer_interface; |
| 3119 | static const struct zwp_confined_pointer_v1_interface confined_pointer_interface; |
| 3120 | |
| 3121 | static enum pointer_constraint_type |
| 3122 | pointer_constraint_get_type(struct weston_pointer_constraint *constraint) |
| 3123 | { |
| 3124 | if (wl_resource_instance_of(constraint->resource, |
| 3125 | &zwp_locked_pointer_v1_interface, |
| 3126 | &locked_pointer_interface)) { |
| 3127 | return POINTER_CONSTRAINT_TYPE_LOCK; |
| 3128 | } else if (wl_resource_instance_of(constraint->resource, |
| 3129 | &zwp_confined_pointer_v1_interface, |
| 3130 | &confined_pointer_interface)) { |
| 3131 | return POINTER_CONSTRAINT_TYPE_CONFINE; |
| 3132 | } |
| 3133 | |
| 3134 | abort(); |
| 3135 | return 0; |
| 3136 | } |
| 3137 | |
| 3138 | static void |
| 3139 | pointer_constraint_notify_activated(struct weston_pointer_constraint *constraint) |
| 3140 | { |
| 3141 | struct wl_resource *resource = constraint->resource; |
| 3142 | |
| 3143 | switch (pointer_constraint_get_type(constraint)) { |
| 3144 | case POINTER_CONSTRAINT_TYPE_LOCK: |
| 3145 | zwp_locked_pointer_v1_send_locked(resource); |
| 3146 | break; |
| 3147 | case POINTER_CONSTRAINT_TYPE_CONFINE: |
| 3148 | zwp_confined_pointer_v1_send_confined(resource); |
| 3149 | break; |
| 3150 | } |
| 3151 | } |
| 3152 | |
| 3153 | static void |
| 3154 | pointer_constraint_notify_deactivated(struct weston_pointer_constraint *constraint) |
| 3155 | { |
| 3156 | struct wl_resource *resource = constraint->resource; |
| 3157 | |
| 3158 | switch (pointer_constraint_get_type(constraint)) { |
| 3159 | case POINTER_CONSTRAINT_TYPE_LOCK: |
| 3160 | zwp_locked_pointer_v1_send_unlocked(resource); |
| 3161 | break; |
| 3162 | case POINTER_CONSTRAINT_TYPE_CONFINE: |
| 3163 | zwp_confined_pointer_v1_send_unconfined(resource); |
| 3164 | break; |
| 3165 | } |
| 3166 | } |
| 3167 | |
| 3168 | static struct weston_pointer_constraint * |
| 3169 | get_pointer_constraint_for_pointer(struct weston_surface *surface, |
| 3170 | struct weston_pointer *pointer) |
| 3171 | { |
| 3172 | struct weston_pointer_constraint *constraint; |
| 3173 | |
| 3174 | wl_list_for_each(constraint, &surface->pointer_constraints, link) { |
| 3175 | if (constraint->pointer == pointer) |
| 3176 | return constraint; |
| 3177 | } |
| 3178 | |
| 3179 | return NULL; |
| 3180 | } |
| 3181 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 3182 | /** Get a seat's touch pointer |
| 3183 | * |
| 3184 | * \param seat The seat to query |
| 3185 | * \return The seat's touch pointer, or NULL if no touch device is present |
| 3186 | * |
| 3187 | * The touch pointer for a seat isn't freed when all touch devices are removed, |
| 3188 | * so it should only be used when the seat's touch_device_count is greater |
| 3189 | * than zero. This function does that test and only returns a pointer |
| 3190 | * when a touch device is present. |
| 3191 | */ |
| 3192 | WL_EXPORT struct weston_touch * |
| 3193 | weston_seat_get_touch(struct weston_seat *seat) |
| 3194 | { |
| 3195 | if (!seat) |
| 3196 | return NULL; |
| 3197 | |
| 3198 | if (seat->touch_device_count) |
| 3199 | return seat->touch_state; |
| 3200 | |
| 3201 | return NULL; |
| 3202 | } |
Bryce Harrington | 24f917e | 2016-06-29 19:04:07 -0700 | [diff] [blame] | 3203 | |
| 3204 | /** Sets the keyboard focus to the given surface |
| 3205 | * |
| 3206 | * \param seat The seat to query |
| 3207 | */ |
| 3208 | WL_EXPORT void |
| 3209 | weston_seat_set_keyboard_focus(struct weston_seat *seat, |
| 3210 | struct weston_surface *surface) |
| 3211 | { |
| 3212 | struct weston_compositor *compositor = seat->compositor; |
| 3213 | struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat); |
Jonas Ådahl | f7deb6a | 2016-07-22 17:50:26 +0800 | [diff] [blame] | 3214 | struct weston_surface_activation_data activation_data; |
Bryce Harrington | 24f917e | 2016-06-29 19:04:07 -0700 | [diff] [blame] | 3215 | |
Jonas Ådahl | ef8e1c3 | 2016-03-15 20:28:51 +0800 | [diff] [blame] | 3216 | if (keyboard && keyboard->focus != surface) { |
Bryce Harrington | 24f917e | 2016-06-29 19:04:07 -0700 | [diff] [blame] | 3217 | weston_keyboard_set_focus(keyboard, surface); |
| 3218 | wl_data_device_set_keyboard_focus(seat); |
| 3219 | } |
| 3220 | |
Jonas Ådahl | 94e2e2d | 2014-10-18 18:42:19 +0200 | [diff] [blame] | 3221 | inc_activate_serial(compositor); |
Jonas Ådahl | f7deb6a | 2016-07-22 17:50:26 +0800 | [diff] [blame] | 3222 | |
| 3223 | activation_data = (struct weston_surface_activation_data) { |
| 3224 | .surface = surface, |
| 3225 | .seat = seat, |
| 3226 | }; |
| 3227 | wl_signal_emit(&compositor->activate_signal, &activation_data); |
Bryce Harrington | 24f917e | 2016-06-29 19:04:07 -0700 | [diff] [blame] | 3228 | } |
Jonas Ådahl | 30d61d8 | 2014-10-22 21:21:17 +0200 | [diff] [blame] | 3229 | |
Jonas Ådahl | d3414f2 | 2016-07-22 17:56:31 +0800 | [diff] [blame] | 3230 | static void |
| 3231 | enable_pointer_constraint(struct weston_pointer_constraint *constraint, |
| 3232 | struct weston_view *view) |
| 3233 | { |
| 3234 | assert(constraint->view == NULL); |
| 3235 | constraint->view = view; |
| 3236 | pointer_constraint_notify_activated(constraint); |
| 3237 | weston_pointer_start_grab(constraint->pointer, &constraint->grab); |
| 3238 | wl_list_remove(&constraint->surface_destroy_listener.link); |
| 3239 | wl_list_init(&constraint->surface_destroy_listener.link); |
| 3240 | } |
| 3241 | |
| 3242 | static bool |
| 3243 | is_pointer_constraint_enabled(struct weston_pointer_constraint *constraint) |
| 3244 | { |
| 3245 | return constraint->view != NULL; |
| 3246 | } |
| 3247 | |
| 3248 | static void |
| 3249 | weston_pointer_constraint_disable(struct weston_pointer_constraint *constraint) |
| 3250 | { |
| 3251 | constraint->view = NULL; |
| 3252 | pointer_constraint_notify_deactivated(constraint); |
| 3253 | weston_pointer_end_grab(constraint->grab.pointer); |
| 3254 | } |
| 3255 | |
| 3256 | void |
| 3257 | weston_pointer_constraint_destroy(struct weston_pointer_constraint *constraint) |
| 3258 | { |
| 3259 | if (is_pointer_constraint_enabled(constraint)) |
| 3260 | weston_pointer_constraint_disable(constraint); |
| 3261 | |
| 3262 | wl_list_remove(&constraint->pointer_destroy_listener.link); |
| 3263 | wl_list_remove(&constraint->surface_destroy_listener.link); |
| 3264 | wl_list_remove(&constraint->surface_commit_listener.link); |
| 3265 | wl_list_remove(&constraint->surface_activate_listener.link); |
| 3266 | |
| 3267 | wl_resource_set_user_data(constraint->resource, NULL); |
| 3268 | pixman_region32_fini(&constraint->region); |
| 3269 | wl_list_remove(&constraint->link); |
| 3270 | free(constraint); |
| 3271 | } |
| 3272 | |
| 3273 | static void |
| 3274 | disable_pointer_constraint(struct weston_pointer_constraint *constraint) |
| 3275 | { |
| 3276 | switch (constraint->lifetime) { |
| 3277 | case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT: |
| 3278 | weston_pointer_constraint_destroy(constraint); |
| 3279 | break; |
| 3280 | case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT: |
| 3281 | weston_pointer_constraint_disable(constraint); |
| 3282 | break; |
| 3283 | } |
| 3284 | } |
| 3285 | |
| 3286 | static bool |
| 3287 | is_within_constraint_region(struct weston_pointer_constraint *constraint, |
| 3288 | wl_fixed_t sx, wl_fixed_t sy) |
| 3289 | { |
| 3290 | struct weston_surface *surface = constraint->surface; |
| 3291 | pixman_region32_t constraint_region; |
| 3292 | bool result; |
| 3293 | |
| 3294 | pixman_region32_init(&constraint_region); |
| 3295 | pixman_region32_intersect(&constraint_region, |
| 3296 | &surface->input, |
| 3297 | &constraint->region); |
| 3298 | result = pixman_region32_contains_point(&constraint_region, |
| 3299 | wl_fixed_to_int(sx), |
| 3300 | wl_fixed_to_int(sy), |
| 3301 | NULL); |
| 3302 | pixman_region32_fini(&constraint_region); |
| 3303 | |
| 3304 | return result; |
| 3305 | } |
| 3306 | |
| 3307 | static void |
| 3308 | maybe_enable_pointer_constraint(struct weston_pointer_constraint *constraint) |
| 3309 | { |
| 3310 | struct weston_surface *surface = constraint->surface; |
| 3311 | struct weston_view *vit; |
| 3312 | struct weston_view *view = NULL; |
| 3313 | struct weston_pointer *pointer = constraint->pointer; |
| 3314 | struct weston_keyboard *keyboard; |
| 3315 | struct weston_seat *seat = pointer->seat; |
| 3316 | int32_t x, y; |
| 3317 | |
| 3318 | /* Postpone if no view of the surface was most recently clicked. */ |
| 3319 | wl_list_for_each(vit, &surface->views, surface_link) { |
| 3320 | if (vit->click_to_activate_serial == |
| 3321 | surface->compositor->activate_serial) { |
| 3322 | view = vit; |
| 3323 | } |
| 3324 | } |
| 3325 | if (view == NULL) |
| 3326 | return; |
| 3327 | |
| 3328 | /* Postpone if surface doesn't have keyboard focus. */ |
| 3329 | keyboard = weston_seat_get_keyboard(seat); |
| 3330 | if (!keyboard || keyboard->focus != surface) |
| 3331 | return; |
| 3332 | |
| 3333 | /* Postpone constraint if the pointer is not within the |
| 3334 | * constraint region. |
| 3335 | */ |
| 3336 | weston_view_from_global(view, |
| 3337 | wl_fixed_to_int(pointer->x), |
| 3338 | wl_fixed_to_int(pointer->y), |
| 3339 | &x, &y); |
| 3340 | if (!is_within_constraint_region(constraint, |
| 3341 | wl_fixed_from_int(x), |
| 3342 | wl_fixed_from_int(y))) |
| 3343 | return; |
| 3344 | |
| 3345 | enable_pointer_constraint(constraint, view); |
| 3346 | } |
| 3347 | |
| 3348 | static void |
| 3349 | locked_pointer_grab_pointer_focus(struct weston_pointer_grab *grab) |
| 3350 | { |
| 3351 | } |
| 3352 | |
| 3353 | static void |
| 3354 | locked_pointer_grab_pointer_motion(struct weston_pointer_grab *grab, |
Alexandros Frantzis | 84b31f8 | 2017-11-24 18:01:46 +0200 | [diff] [blame] | 3355 | const struct timespec *time, |
Jonas Ådahl | d3414f2 | 2016-07-22 17:56:31 +0800 | [diff] [blame] | 3356 | struct weston_pointer_motion_event *event) |
| 3357 | { |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 3358 | pointer_send_relative_motion(grab->pointer, time, event); |
Jonas Ådahl | d3414f2 | 2016-07-22 17:56:31 +0800 | [diff] [blame] | 3359 | } |
| 3360 | |
| 3361 | static void |
| 3362 | locked_pointer_grab_pointer_button(struct weston_pointer_grab *grab, |
Alexandros Frantzis | 215bedc | 2017-11-16 18:20:55 +0200 | [diff] [blame] | 3363 | const struct timespec *time, |
Jonas Ådahl | d3414f2 | 2016-07-22 17:56:31 +0800 | [diff] [blame] | 3364 | uint32_t button, |
| 3365 | uint32_t state_w) |
| 3366 | { |
| 3367 | weston_pointer_send_button(grab->pointer, time, button, state_w); |
| 3368 | } |
| 3369 | |
| 3370 | static void |
| 3371 | locked_pointer_grab_pointer_axis(struct weston_pointer_grab *grab, |
Alexandros Frantzis | 8032194 | 2017-11-16 18:20:56 +0200 | [diff] [blame] | 3372 | const struct timespec *time, |
Jonas Ådahl | d3414f2 | 2016-07-22 17:56:31 +0800 | [diff] [blame] | 3373 | struct weston_pointer_axis_event *event) |
| 3374 | { |
| 3375 | weston_pointer_send_axis(grab->pointer, time, event); |
| 3376 | } |
| 3377 | |
| 3378 | static void |
| 3379 | locked_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab, |
| 3380 | uint32_t source) |
| 3381 | { |
| 3382 | weston_pointer_send_axis_source(grab->pointer, source); |
| 3383 | } |
| 3384 | |
| 3385 | static void |
| 3386 | locked_pointer_grab_pointer_frame(struct weston_pointer_grab *grab) |
| 3387 | { |
| 3388 | weston_pointer_send_frame(grab->pointer); |
| 3389 | } |
| 3390 | |
| 3391 | static void |
| 3392 | locked_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab) |
| 3393 | { |
| 3394 | struct weston_pointer_constraint *constraint = |
| 3395 | container_of(grab, struct weston_pointer_constraint, grab); |
| 3396 | |
| 3397 | disable_pointer_constraint(constraint); |
| 3398 | } |
| 3399 | |
| 3400 | static const struct weston_pointer_grab_interface |
| 3401 | locked_pointer_grab_interface = { |
| 3402 | locked_pointer_grab_pointer_focus, |
| 3403 | locked_pointer_grab_pointer_motion, |
| 3404 | locked_pointer_grab_pointer_button, |
| 3405 | locked_pointer_grab_pointer_axis, |
| 3406 | locked_pointer_grab_pointer_axis_source, |
| 3407 | locked_pointer_grab_pointer_frame, |
| 3408 | locked_pointer_grab_pointer_cancel, |
| 3409 | }; |
| 3410 | |
| 3411 | static void |
| 3412 | pointer_constraint_constrain_resource_destroyed(struct wl_resource *resource) |
| 3413 | { |
| 3414 | struct weston_pointer_constraint *constraint = |
| 3415 | wl_resource_get_user_data(resource); |
| 3416 | |
| 3417 | if (!constraint) |
| 3418 | return; |
| 3419 | |
| 3420 | weston_pointer_constraint_destroy(constraint); |
| 3421 | } |
| 3422 | |
| 3423 | static void |
| 3424 | pointer_constraint_surface_activate(struct wl_listener *listener, void *data) |
| 3425 | { |
| 3426 | struct weston_surface_activation_data *activation = data; |
| 3427 | struct weston_pointer *pointer; |
| 3428 | struct weston_surface *focus = activation->surface; |
| 3429 | struct weston_pointer_constraint *constraint = |
| 3430 | container_of(listener, struct weston_pointer_constraint, |
| 3431 | surface_activate_listener); |
| 3432 | bool is_constraint_surface; |
| 3433 | |
| 3434 | pointer = weston_seat_get_pointer(activation->seat); |
| 3435 | if (!pointer) |
| 3436 | return; |
| 3437 | |
| 3438 | is_constraint_surface = |
| 3439 | get_pointer_constraint_for_pointer(focus, pointer) == constraint; |
| 3440 | |
| 3441 | if (is_constraint_surface && |
| 3442 | !is_pointer_constraint_enabled(constraint)) |
| 3443 | maybe_enable_pointer_constraint(constraint); |
| 3444 | else if (!is_constraint_surface && |
| 3445 | is_pointer_constraint_enabled(constraint)) |
| 3446 | disable_pointer_constraint(constraint); |
| 3447 | } |
| 3448 | |
| 3449 | static void |
| 3450 | pointer_constraint_pointer_destroyed(struct wl_listener *listener, void *data) |
| 3451 | { |
| 3452 | struct weston_pointer_constraint *constraint = |
| 3453 | container_of(listener, struct weston_pointer_constraint, |
| 3454 | pointer_destroy_listener); |
| 3455 | |
| 3456 | weston_pointer_constraint_destroy(constraint); |
| 3457 | } |
| 3458 | |
| 3459 | static void |
| 3460 | pointer_constraint_surface_destroyed(struct wl_listener *listener, void *data) |
| 3461 | { |
| 3462 | struct weston_pointer_constraint *constraint = |
| 3463 | container_of(listener, struct weston_pointer_constraint, |
| 3464 | surface_destroy_listener); |
| 3465 | |
| 3466 | weston_pointer_constraint_destroy(constraint); |
| 3467 | } |
| 3468 | |
| 3469 | static void |
| 3470 | pointer_constraint_surface_committed(struct wl_listener *listener, void *data) |
| 3471 | { |
| 3472 | struct weston_pointer_constraint *constraint = |
| 3473 | container_of(listener, struct weston_pointer_constraint, |
| 3474 | surface_commit_listener); |
| 3475 | |
| 3476 | if (constraint->region_is_pending) { |
| 3477 | constraint->region_is_pending = false; |
| 3478 | pixman_region32_copy(&constraint->region, |
| 3479 | &constraint->region_pending); |
| 3480 | pixman_region32_fini(&constraint->region_pending); |
| 3481 | pixman_region32_init(&constraint->region_pending); |
| 3482 | } |
| 3483 | |
| 3484 | if (constraint->hint_is_pending) { |
| 3485 | constraint->hint_is_pending = false; |
| 3486 | |
| 3487 | constraint->hint_is_pending = true; |
| 3488 | constraint->hint_x = constraint->hint_x_pending; |
| 3489 | constraint->hint_y = constraint->hint_y_pending; |
| 3490 | } |
| 3491 | |
| 3492 | if (pointer_constraint_get_type(constraint) == |
| 3493 | POINTER_CONSTRAINT_TYPE_CONFINE && |
| 3494 | is_pointer_constraint_enabled(constraint)) |
| 3495 | maybe_warp_confined_pointer(constraint); |
| 3496 | } |
| 3497 | |
| 3498 | static struct weston_pointer_constraint * |
| 3499 | weston_pointer_constraint_create(struct weston_surface *surface, |
| 3500 | struct weston_pointer *pointer, |
| 3501 | struct weston_region *region, |
| 3502 | enum zwp_pointer_constraints_v1_lifetime lifetime, |
| 3503 | struct wl_resource *cr, |
| 3504 | const struct weston_pointer_grab_interface *grab_interface) |
| 3505 | { |
| 3506 | struct weston_pointer_constraint *constraint; |
| 3507 | |
| 3508 | constraint = zalloc(sizeof *constraint); |
| 3509 | if (!constraint) |
| 3510 | return NULL; |
| 3511 | |
| 3512 | constraint->lifetime = lifetime; |
| 3513 | pixman_region32_init(&constraint->region); |
| 3514 | pixman_region32_init(&constraint->region_pending); |
| 3515 | wl_list_insert(&surface->pointer_constraints, &constraint->link); |
| 3516 | constraint->surface = surface; |
| 3517 | constraint->pointer = pointer; |
| 3518 | constraint->resource = cr; |
| 3519 | constraint->grab.interface = grab_interface; |
| 3520 | if (region) { |
| 3521 | pixman_region32_copy(&constraint->region, |
| 3522 | ®ion->region); |
| 3523 | } else { |
| 3524 | pixman_region32_fini(&constraint->region); |
| 3525 | region_init_infinite(&constraint->region); |
| 3526 | } |
| 3527 | |
| 3528 | constraint->surface_activate_listener.notify = |
| 3529 | pointer_constraint_surface_activate; |
| 3530 | constraint->surface_destroy_listener.notify = |
| 3531 | pointer_constraint_surface_destroyed; |
| 3532 | constraint->surface_commit_listener.notify = |
| 3533 | pointer_constraint_surface_committed; |
| 3534 | constraint->pointer_destroy_listener.notify = |
| 3535 | pointer_constraint_pointer_destroyed; |
| 3536 | |
| 3537 | wl_signal_add(&surface->compositor->activate_signal, |
| 3538 | &constraint->surface_activate_listener); |
| 3539 | wl_signal_add(&pointer->destroy_signal, |
| 3540 | &constraint->pointer_destroy_listener); |
| 3541 | wl_signal_add(&surface->destroy_signal, |
| 3542 | &constraint->surface_destroy_listener); |
| 3543 | wl_signal_add(&surface->commit_signal, |
| 3544 | &constraint->surface_commit_listener); |
| 3545 | |
| 3546 | return constraint; |
| 3547 | } |
| 3548 | |
| 3549 | static void |
| 3550 | init_pointer_constraint(struct wl_resource *pointer_constraints_resource, |
| 3551 | uint32_t id, |
| 3552 | struct weston_surface *surface, |
| 3553 | struct weston_pointer *pointer, |
| 3554 | struct weston_region *region, |
| 3555 | enum zwp_pointer_constraints_v1_lifetime lifetime, |
| 3556 | const struct wl_interface *interface, |
| 3557 | const void *implementation, |
| 3558 | const struct weston_pointer_grab_interface *grab_interface) |
| 3559 | { |
| 3560 | struct wl_client *client = |
| 3561 | wl_resource_get_client(pointer_constraints_resource); |
| 3562 | struct wl_resource *cr; |
| 3563 | struct weston_pointer_constraint *constraint; |
| 3564 | |
| 3565 | if (get_pointer_constraint_for_pointer(surface, pointer)) { |
| 3566 | wl_resource_post_error(pointer_constraints_resource, |
| 3567 | ZWP_POINTER_CONSTRAINTS_V1_ERROR_ALREADY_CONSTRAINED, |
| 3568 | "the pointer has a lock/confine request on this surface"); |
| 3569 | return; |
| 3570 | } |
| 3571 | |
| 3572 | cr = wl_resource_create(client, interface, |
| 3573 | wl_resource_get_version(pointer_constraints_resource), |
| 3574 | id); |
| 3575 | if (cr == NULL) { |
| 3576 | wl_client_post_no_memory(client); |
| 3577 | return; |
| 3578 | } |
| 3579 | |
| 3580 | constraint = weston_pointer_constraint_create(surface, pointer, |
| 3581 | region, lifetime, |
| 3582 | cr, grab_interface); |
| 3583 | if (constraint == NULL) { |
| 3584 | wl_client_post_no_memory(client); |
| 3585 | return; |
| 3586 | } |
| 3587 | |
| 3588 | wl_resource_set_implementation(cr, implementation, constraint, |
| 3589 | pointer_constraint_constrain_resource_destroyed); |
| 3590 | |
| 3591 | maybe_enable_pointer_constraint(constraint); |
| 3592 | } |
| 3593 | |
| 3594 | static void |
| 3595 | pointer_constraints_destroy(struct wl_client *client, |
| 3596 | struct wl_resource *resource) |
| 3597 | { |
| 3598 | wl_resource_destroy(resource); |
| 3599 | } |
| 3600 | |
| 3601 | static void |
| 3602 | locked_pointer_destroy(struct wl_client *client, |
| 3603 | struct wl_resource *resource) |
| 3604 | { |
| 3605 | struct weston_pointer_constraint *constraint = |
| 3606 | wl_resource_get_user_data(resource); |
| 3607 | wl_fixed_t x, y; |
| 3608 | |
| 3609 | if (constraint && constraint->view && constraint->hint_is_pending && |
| 3610 | is_within_constraint_region(constraint, |
| 3611 | constraint->hint_x, |
| 3612 | constraint->hint_y)) { |
| 3613 | weston_view_to_global_fixed(constraint->view, |
| 3614 | constraint->hint_x, |
| 3615 | constraint->hint_y, |
| 3616 | &x, &y); |
| 3617 | weston_pointer_move_to(constraint->pointer, x, y); |
| 3618 | } |
| 3619 | wl_resource_destroy(resource); |
| 3620 | } |
| 3621 | |
| 3622 | static void |
| 3623 | locked_pointer_set_cursor_position_hint(struct wl_client *client, |
| 3624 | struct wl_resource *resource, |
| 3625 | wl_fixed_t surface_x, |
| 3626 | wl_fixed_t surface_y) |
| 3627 | { |
| 3628 | struct weston_pointer_constraint *constraint = |
| 3629 | wl_resource_get_user_data(resource); |
| 3630 | |
| 3631 | /* Ignore a set cursor hint that was sent after the lock was cancelled. |
| 3632 | */ |
| 3633 | if (!constraint || |
| 3634 | !constraint->resource || |
| 3635 | constraint->resource != resource) |
| 3636 | return; |
| 3637 | |
| 3638 | constraint->hint_is_pending = true; |
| 3639 | constraint->hint_x_pending = surface_x; |
| 3640 | constraint->hint_y_pending = surface_y; |
| 3641 | } |
| 3642 | |
| 3643 | static void |
| 3644 | locked_pointer_set_region(struct wl_client *client, |
| 3645 | struct wl_resource *resource, |
| 3646 | struct wl_resource *region_resource) |
| 3647 | { |
| 3648 | struct weston_pointer_constraint *constraint = |
| 3649 | wl_resource_get_user_data(resource); |
| 3650 | struct weston_region *region = region_resource ? |
| 3651 | wl_resource_get_user_data(region_resource) : NULL; |
| 3652 | |
| 3653 | if (!constraint) |
| 3654 | return; |
| 3655 | |
| 3656 | if (region) { |
| 3657 | pixman_region32_copy(&constraint->region_pending, |
| 3658 | ®ion->region); |
| 3659 | } else { |
| 3660 | pixman_region32_fini(&constraint->region_pending); |
| 3661 | region_init_infinite(&constraint->region_pending); |
| 3662 | } |
| 3663 | constraint->region_is_pending = true; |
| 3664 | } |
| 3665 | |
| 3666 | |
| 3667 | static const struct zwp_locked_pointer_v1_interface locked_pointer_interface = { |
| 3668 | locked_pointer_destroy, |
| 3669 | locked_pointer_set_cursor_position_hint, |
| 3670 | locked_pointer_set_region, |
| 3671 | }; |
| 3672 | |
| 3673 | static void |
| 3674 | pointer_constraints_lock_pointer(struct wl_client *client, |
| 3675 | struct wl_resource *resource, |
| 3676 | uint32_t id, |
| 3677 | struct wl_resource *surface_resource, |
| 3678 | struct wl_resource *pointer_resource, |
| 3679 | struct wl_resource *region_resource, |
| 3680 | uint32_t lifetime) |
| 3681 | { |
| 3682 | struct weston_surface *surface = |
| 3683 | wl_resource_get_user_data(surface_resource); |
| 3684 | struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource); |
| 3685 | struct weston_region *region = region_resource ? |
| 3686 | wl_resource_get_user_data(region_resource) : NULL; |
| 3687 | |
| 3688 | init_pointer_constraint(resource, id, surface, pointer, region, lifetime, |
| 3689 | &zwp_locked_pointer_v1_interface, |
| 3690 | &locked_pointer_interface, |
| 3691 | &locked_pointer_grab_interface); |
| 3692 | } |
| 3693 | |
| 3694 | static void |
| 3695 | confined_pointer_grab_pointer_focus(struct weston_pointer_grab *grab) |
| 3696 | { |
| 3697 | } |
| 3698 | |
Jonas Ådahl | d0be2bb | 2015-04-30 17:56:37 +0800 | [diff] [blame] | 3699 | static double |
| 3700 | vec2d_cross_product(struct vec2d a, struct vec2d b) |
| 3701 | { |
| 3702 | return a.x * b.y - a.y * b.x; |
| 3703 | } |
| 3704 | |
| 3705 | static struct vec2d |
| 3706 | vec2d_add(struct vec2d a, struct vec2d b) |
| 3707 | { |
| 3708 | return (struct vec2d) { |
| 3709 | .x = a.x + b.x, |
| 3710 | .y = a.y + b.y, |
| 3711 | }; |
| 3712 | } |
| 3713 | |
| 3714 | static struct vec2d |
| 3715 | vec2d_subtract(struct vec2d a, struct vec2d b) |
| 3716 | { |
| 3717 | return (struct vec2d) { |
| 3718 | .x = a.x - b.x, |
| 3719 | .y = a.y - b.y, |
| 3720 | }; |
| 3721 | } |
| 3722 | |
| 3723 | static struct vec2d |
| 3724 | vec2d_multiply_constant(double c, struct vec2d a) |
| 3725 | { |
| 3726 | return (struct vec2d) { |
| 3727 | .x = c * a.x, |
| 3728 | .y = c * a.y, |
| 3729 | }; |
| 3730 | } |
| 3731 | |
| 3732 | static bool |
| 3733 | lines_intersect(struct line *line1, struct line *line2, |
| 3734 | struct vec2d *intersection) |
| 3735 | { |
| 3736 | struct vec2d p = line1->a; |
| 3737 | struct vec2d r = vec2d_subtract(line1->b, line1->a); |
| 3738 | struct vec2d q = line2->a; |
| 3739 | struct vec2d s = vec2d_subtract(line2->b, line2->a); |
| 3740 | double rxs; |
| 3741 | double sxr; |
| 3742 | double t; |
| 3743 | double u; |
| 3744 | |
| 3745 | /* |
| 3746 | * The line (p, r) and (q, s) intersects where |
| 3747 | * |
| 3748 | * p + t r = q + u s |
| 3749 | * |
| 3750 | * Calculate t: |
| 3751 | * |
| 3752 | * (p + t r) × s = (q + u s) × s |
| 3753 | * p × s + t (r × s) = q × s + u (s × s) |
| 3754 | * p × s + t (r × s) = q × s |
| 3755 | * t (r × s) = q × s - p × s |
| 3756 | * t (r × s) = (q - p) × s |
| 3757 | * t = ((q - p) × s) / (r × s) |
| 3758 | * |
| 3759 | * Using the same method, for u we get: |
| 3760 | * |
| 3761 | * u = ((p - q) × r) / (s × r) |
| 3762 | */ |
| 3763 | |
| 3764 | rxs = vec2d_cross_product(r, s); |
| 3765 | sxr = vec2d_cross_product(s, r); |
| 3766 | |
| 3767 | /* If r × s = 0 then the lines are either parallel or collinear. */ |
| 3768 | if (fabs(rxs) < DBL_MIN) |
| 3769 | return false; |
| 3770 | |
| 3771 | t = vec2d_cross_product(vec2d_subtract(q, p), s) / rxs; |
| 3772 | u = vec2d_cross_product(vec2d_subtract(p, q), r) / sxr; |
| 3773 | |
| 3774 | /* The lines only intersect if 0 ≤ t ≤ 1 and 0 ≤ u ≤ 1. */ |
| 3775 | if (t < 0.0 || t > 1.0 || u < 0.0 || u > 1.0) |
| 3776 | return false; |
| 3777 | |
| 3778 | *intersection = vec2d_add(p, vec2d_multiply_constant(t, r)); |
| 3779 | return true; |
| 3780 | } |
| 3781 | |
| 3782 | static struct border * |
| 3783 | add_border(struct wl_array *array, |
| 3784 | double x1, double y1, |
| 3785 | double x2, double y2, |
| 3786 | enum motion_direction blocking_dir) |
| 3787 | { |
| 3788 | struct border *border = wl_array_add(array, sizeof *border); |
| 3789 | |
| 3790 | *border = (struct border) { |
| 3791 | .line = (struct line) { |
| 3792 | .a = (struct vec2d) { |
| 3793 | .x = x1, |
| 3794 | .y = y1, |
| 3795 | }, |
| 3796 | .b = (struct vec2d) { |
| 3797 | .x = x2, |
| 3798 | .y = y2, |
| 3799 | }, |
| 3800 | }, |
| 3801 | .blocking_dir = blocking_dir, |
| 3802 | }; |
| 3803 | |
| 3804 | return border; |
| 3805 | } |
| 3806 | |
| 3807 | static int |
| 3808 | compare_lines_x(const void *a, const void *b) |
| 3809 | { |
| 3810 | const struct border *border_a = a; |
| 3811 | const struct border *border_b = b; |
| 3812 | |
| 3813 | |
| 3814 | if (border_a->line.a.x == border_b->line.a.x) |
| 3815 | return border_a->line.b.x < border_b->line.b.x; |
| 3816 | else |
| 3817 | return border_a->line.a.x > border_b->line.a.x; |
| 3818 | } |
| 3819 | |
| 3820 | static void |
| 3821 | add_non_overlapping_edges(pixman_box32_t *boxes, |
| 3822 | int band_above_start, |
| 3823 | int band_below_start, |
| 3824 | int band_below_end, |
| 3825 | struct wl_array *borders) |
| 3826 | { |
| 3827 | int i; |
| 3828 | struct wl_array band_merge; |
| 3829 | struct border *border; |
| 3830 | struct border *prev_border; |
| 3831 | struct border *new_border; |
| 3832 | |
| 3833 | wl_array_init(&band_merge); |
| 3834 | |
| 3835 | /* Add bottom band of previous row, and top band of current row, and |
| 3836 | * sort them so lower left x coordinate comes first. If there are two |
| 3837 | * borders with the same left x coordinate, the wider one comes first. |
| 3838 | */ |
| 3839 | for (i = band_above_start; i < band_below_start; i++) { |
| 3840 | pixman_box32_t *box = &boxes[i]; |
| 3841 | add_border(&band_merge, box->x1, box->y2, box->x2, box->y2, |
| 3842 | MOTION_DIRECTION_POSITIVE_Y); |
| 3843 | } |
| 3844 | for (i = band_below_start; i < band_below_end; i++) { |
| 3845 | pixman_box32_t *box= &boxes[i]; |
| 3846 | add_border(&band_merge, box->x1, box->y1, box->x2, box->y1, |
| 3847 | MOTION_DIRECTION_NEGATIVE_Y); |
| 3848 | } |
| 3849 | qsort(band_merge.data, |
| 3850 | band_merge.size / sizeof *border, |
| 3851 | sizeof *border, |
| 3852 | compare_lines_x); |
| 3853 | |
| 3854 | /* Combine the two combined bands so that any overlapping border is |
| 3855 | * eliminated. */ |
| 3856 | prev_border = NULL; |
| 3857 | wl_array_for_each(border, &band_merge) { |
| 3858 | assert(border->line.a.y == border->line.b.y); |
| 3859 | assert(!prev_border || |
| 3860 | prev_border->line.a.y == border->line.a.y); |
| 3861 | assert(!prev_border || |
| 3862 | (prev_border->line.a.x != border->line.a.x || |
| 3863 | prev_border->line.b.x != border->line.b.x)); |
| 3864 | assert(!prev_border || |
| 3865 | prev_border->line.a.x <= border->line.a.x); |
| 3866 | |
| 3867 | if (prev_border && |
| 3868 | prev_border->line.a.x == border->line.a.x) { |
| 3869 | /* |
| 3870 | * ------------ + |
| 3871 | * ------- = |
| 3872 | * [ ]----- |
| 3873 | */ |
| 3874 | prev_border->line.a.x = border->line.b.x; |
| 3875 | } else if (prev_border && |
| 3876 | prev_border->line.b.x == border->line.b.x) { |
| 3877 | /* |
| 3878 | * ------------ + |
| 3879 | * ------ = |
| 3880 | * ------[ ] |
| 3881 | */ |
| 3882 | prev_border->line.b.x = border->line.a.x; |
| 3883 | } else if (prev_border && |
| 3884 | prev_border->line.b.x == border->line.a.x) { |
| 3885 | /* |
| 3886 | * -------- + |
| 3887 | * ------ = |
| 3888 | * -------------- |
| 3889 | */ |
| 3890 | prev_border->line.b.x = border->line.b.x; |
| 3891 | } else if (prev_border && |
| 3892 | prev_border->line.b.x >= border->line.a.x) { |
| 3893 | /* |
| 3894 | * --------------- + |
| 3895 | * ------ = |
| 3896 | * -----[ ]---- |
| 3897 | */ |
| 3898 | new_border = add_border(borders, |
| 3899 | border->line.b.x, |
| 3900 | border->line.b.y, |
| 3901 | prev_border->line.b.x, |
| 3902 | prev_border->line.b.y, |
| 3903 | prev_border->blocking_dir); |
| 3904 | prev_border->line.b.x = border->line.a.x; |
| 3905 | prev_border = new_border; |
| 3906 | } else { |
| 3907 | assert(!prev_border || |
| 3908 | prev_border->line.b.x < border->line.a.x); |
| 3909 | /* |
| 3910 | * First border or non-overlapping. |
| 3911 | * |
| 3912 | * ----- + |
| 3913 | * ----- = |
| 3914 | * ----- ----- |
| 3915 | */ |
| 3916 | new_border = wl_array_add(borders, sizeof *border); |
| 3917 | *new_border = *border; |
| 3918 | prev_border = new_border; |
| 3919 | } |
| 3920 | } |
| 3921 | |
| 3922 | wl_array_release(&band_merge); |
| 3923 | } |
| 3924 | |
| 3925 | static void |
| 3926 | add_band_bottom_edges(pixman_box32_t *boxes, |
| 3927 | int band_start, |
| 3928 | int band_end, |
| 3929 | struct wl_array *borders) |
| 3930 | { |
| 3931 | int i; |
| 3932 | |
| 3933 | for (i = band_start; i < band_end; i++) { |
| 3934 | add_border(borders, |
| 3935 | boxes[i].x1, boxes[i].y2, |
| 3936 | boxes[i].x2, boxes[i].y2, |
| 3937 | MOTION_DIRECTION_POSITIVE_Y); |
| 3938 | } |
| 3939 | } |
| 3940 | |
| 3941 | static void |
| 3942 | region_to_outline(pixman_region32_t *region, struct wl_array *borders) |
| 3943 | { |
| 3944 | pixman_box32_t *boxes; |
| 3945 | int num_boxes; |
| 3946 | int i; |
| 3947 | int top_most, bottom_most; |
| 3948 | int current_roof; |
| 3949 | int prev_top; |
| 3950 | int band_start, prev_band_start; |
| 3951 | |
| 3952 | /* |
| 3953 | * Remove any overlapping lines from the set of rectangles. Note that |
| 3954 | * pixman regions are grouped as rows of rectangles, where rectangles |
| 3955 | * in one row never touch or overlap and are all of the same height. |
| 3956 | * |
| 3957 | * -------- --- -------- --- |
| 3958 | * | | | | | | | | |
| 3959 | * ----------====---- --- ----------- ----- --- |
| 3960 | * | | => | | |
| 3961 | * ----==========--------- ----- ---------- |
| 3962 | * | | | | |
| 3963 | * ------------------- ------------------- |
| 3964 | * |
| 3965 | */ |
| 3966 | |
| 3967 | boxes = pixman_region32_rectangles(region, &num_boxes); |
| 3968 | prev_top = 0; |
| 3969 | top_most = boxes[0].y1; |
| 3970 | current_roof = top_most; |
| 3971 | bottom_most = boxes[num_boxes - 1].y2; |
| 3972 | band_start = 0; |
| 3973 | prev_band_start = 0; |
| 3974 | for (i = 0; i < num_boxes; i++) { |
| 3975 | /* Detect if there is a vertical empty space, and add the lower |
| 3976 | * level of the previous band if so was the case. */ |
| 3977 | if (i > 0 && |
| 3978 | boxes[i].y1 != prev_top && |
| 3979 | boxes[i].y1 != boxes[i - 1].y2) { |
| 3980 | current_roof = boxes[i].y1; |
| 3981 | add_band_bottom_edges(boxes, |
| 3982 | band_start, |
| 3983 | i, |
| 3984 | borders); |
| 3985 | } |
| 3986 | |
| 3987 | /* Special case adding the last band, since it won't be handled |
| 3988 | * by the band change detection below. */ |
| 3989 | if (boxes[i].y1 != current_roof && i == num_boxes - 1) { |
| 3990 | if (boxes[i].y1 != prev_top) { |
| 3991 | /* The last band is a single box, so we don't |
| 3992 | * have a prev_band_start to tell us when the |
| 3993 | * previous band started. */ |
| 3994 | add_non_overlapping_edges(boxes, |
| 3995 | band_start, |
| 3996 | i, |
| 3997 | i + 1, |
| 3998 | borders); |
| 3999 | } else { |
| 4000 | add_non_overlapping_edges(boxes, |
| 4001 | prev_band_start, |
| 4002 | band_start, |
| 4003 | i + 1, |
| 4004 | borders); |
| 4005 | } |
| 4006 | } |
| 4007 | |
| 4008 | /* Detect when passing a band and combine the top border of the |
| 4009 | * just passed band with the bottom band of the previous band. |
| 4010 | */ |
| 4011 | if (boxes[i].y1 != top_most && boxes[i].y1 != prev_top) { |
| 4012 | /* Combine the two passed bands. */ |
| 4013 | if (prev_top != current_roof) { |
| 4014 | add_non_overlapping_edges(boxes, |
| 4015 | prev_band_start, |
| 4016 | band_start, |
| 4017 | i, |
| 4018 | borders); |
| 4019 | } |
| 4020 | |
| 4021 | prev_band_start = band_start; |
| 4022 | band_start = i; |
| 4023 | } |
| 4024 | |
| 4025 | /* Add the top border if the box is part of the current roof. */ |
| 4026 | if (boxes[i].y1 == current_roof) { |
| 4027 | add_border(borders, |
| 4028 | boxes[i].x1, boxes[i].y1, |
| 4029 | boxes[i].x2, boxes[i].y1, |
| 4030 | MOTION_DIRECTION_NEGATIVE_Y); |
| 4031 | } |
| 4032 | |
| 4033 | /* Add the bottom border of the last band. */ |
| 4034 | if (boxes[i].y2 == bottom_most) { |
| 4035 | add_border(borders, |
| 4036 | boxes[i].x1, boxes[i].y2, |
| 4037 | boxes[i].x2, boxes[i].y2, |
| 4038 | MOTION_DIRECTION_POSITIVE_Y); |
| 4039 | } |
| 4040 | |
| 4041 | /* Always add the left border. */ |
| 4042 | add_border(borders, |
| 4043 | boxes[i].x1, boxes[i].y1, |
| 4044 | boxes[i].x1, boxes[i].y2, |
| 4045 | MOTION_DIRECTION_NEGATIVE_X); |
| 4046 | |
| 4047 | /* Always add the right border. */ |
| 4048 | add_border(borders, |
| 4049 | boxes[i].x2, boxes[i].y1, |
| 4050 | boxes[i].x2, boxes[i].y2, |
| 4051 | MOTION_DIRECTION_POSITIVE_X); |
| 4052 | |
| 4053 | prev_top = boxes[i].y1; |
| 4054 | } |
| 4055 | } |
| 4056 | |
| 4057 | static bool |
| 4058 | is_border_horizontal (struct border *border) |
| 4059 | { |
| 4060 | return border->line.a.y == border->line.b.y; |
| 4061 | } |
| 4062 | |
| 4063 | static bool |
| 4064 | is_border_blocking_directions(struct border *border, |
| 4065 | uint32_t directions) |
| 4066 | { |
| 4067 | /* Don't block parallel motions. */ |
| 4068 | if (is_border_horizontal(border)) { |
| 4069 | if ((directions & (MOTION_DIRECTION_POSITIVE_Y | |
| 4070 | MOTION_DIRECTION_NEGATIVE_Y)) == 0) |
| 4071 | return false; |
| 4072 | } else { |
| 4073 | if ((directions & (MOTION_DIRECTION_POSITIVE_X | |
| 4074 | MOTION_DIRECTION_NEGATIVE_X)) == 0) |
| 4075 | return false; |
| 4076 | } |
| 4077 | |
| 4078 | return (~border->blocking_dir & directions) != directions; |
| 4079 | } |
| 4080 | |
| 4081 | static struct border * |
| 4082 | get_closest_border(struct wl_array *borders, |
| 4083 | struct line *motion, |
| 4084 | uint32_t directions) |
| 4085 | { |
| 4086 | struct border *border; |
| 4087 | struct vec2d intersection; |
| 4088 | struct vec2d delta; |
| 4089 | double distance_2; |
| 4090 | struct border *closest_border = NULL; |
| 4091 | double closest_distance_2 = DBL_MAX; |
| 4092 | |
| 4093 | wl_array_for_each(border, borders) { |
| 4094 | if (!is_border_blocking_directions(border, directions)) |
| 4095 | continue; |
| 4096 | |
| 4097 | if (!lines_intersect(&border->line, motion, &intersection)) |
| 4098 | continue; |
| 4099 | |
| 4100 | delta = vec2d_subtract(intersection, motion->a); |
| 4101 | distance_2 = delta.x*delta.x + delta.y*delta.y; |
| 4102 | if (distance_2 < closest_distance_2) { |
| 4103 | closest_border = border; |
| 4104 | closest_distance_2 = distance_2; |
| 4105 | } |
| 4106 | } |
| 4107 | |
| 4108 | return closest_border; |
| 4109 | } |
| 4110 | |
| 4111 | static void |
| 4112 | clamp_to_border(struct border *border, |
| 4113 | struct line *motion, |
| 4114 | uint32_t *motion_dir) |
| 4115 | { |
| 4116 | /* |
| 4117 | * When clamping either rightward or downward motions, the motion needs |
| 4118 | * to be clamped so that the destination coordinate does not end up on |
| 4119 | * the border (see weston_pointer_clamp_event_to_region). Do this by |
| 4120 | * clamping such motions to the border minus the smallest possible |
| 4121 | * wl_fixed_t value. |
| 4122 | */ |
| 4123 | if (is_border_horizontal(border)) { |
| 4124 | if (*motion_dir & MOTION_DIRECTION_POSITIVE_Y) |
| 4125 | motion->b.y = border->line.a.y - wl_fixed_to_double(1); |
| 4126 | else |
| 4127 | motion->b.y = border->line.a.y; |
| 4128 | *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_Y | |
| 4129 | MOTION_DIRECTION_NEGATIVE_Y); |
| 4130 | } else { |
| 4131 | if (*motion_dir & MOTION_DIRECTION_POSITIVE_X) |
| 4132 | motion->b.x = border->line.a.x - wl_fixed_to_double(1); |
| 4133 | else |
| 4134 | motion->b.x = border->line.a.x; |
| 4135 | *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_X | |
| 4136 | MOTION_DIRECTION_NEGATIVE_X); |
| 4137 | } |
| 4138 | } |
| 4139 | |
| 4140 | static uint32_t |
| 4141 | get_motion_directions(struct line *motion) |
| 4142 | { |
| 4143 | uint32_t directions = 0; |
| 4144 | |
| 4145 | if (motion->a.x < motion->b.x) |
| 4146 | directions |= MOTION_DIRECTION_POSITIVE_X; |
| 4147 | else if (motion->a.x > motion->b.x) |
| 4148 | directions |= MOTION_DIRECTION_NEGATIVE_X; |
| 4149 | if (motion->a.y < motion->b.y) |
| 4150 | directions |= MOTION_DIRECTION_POSITIVE_Y; |
| 4151 | else if (motion->a.y > motion->b.y) |
| 4152 | directions |= MOTION_DIRECTION_NEGATIVE_Y; |
| 4153 | |
| 4154 | return directions; |
| 4155 | } |
| 4156 | |
Jonas Ådahl | d3414f2 | 2016-07-22 17:56:31 +0800 | [diff] [blame] | 4157 | static void |
| 4158 | weston_pointer_clamp_event_to_region(struct weston_pointer *pointer, |
| 4159 | struct weston_pointer_motion_event *event, |
| 4160 | pixman_region32_t *region, |
| 4161 | wl_fixed_t *clamped_x, |
| 4162 | wl_fixed_t *clamped_y) |
| 4163 | { |
| 4164 | wl_fixed_t x, y; |
| 4165 | wl_fixed_t sx, sy; |
Jonas Ådahl | d0be2bb | 2015-04-30 17:56:37 +0800 | [diff] [blame] | 4166 | wl_fixed_t old_sx = pointer->sx; |
| 4167 | wl_fixed_t old_sy = pointer->sy; |
| 4168 | struct wl_array borders; |
| 4169 | struct line motion; |
| 4170 | struct border *closest_border; |
| 4171 | float new_x_f, new_y_f; |
| 4172 | uint32_t directions; |
Jonas Ådahl | d3414f2 | 2016-07-22 17:56:31 +0800 | [diff] [blame] | 4173 | |
| 4174 | weston_pointer_motion_to_abs(pointer, event, &x, &y); |
| 4175 | weston_view_from_global_fixed(pointer->focus, x, y, &sx, &sy); |
| 4176 | |
Jonas Ådahl | d0be2bb | 2015-04-30 17:56:37 +0800 | [diff] [blame] | 4177 | wl_array_init(&borders); |
Jonas Ådahl | d3414f2 | 2016-07-22 17:56:31 +0800 | [diff] [blame] | 4178 | |
Jonas Ådahl | d0be2bb | 2015-04-30 17:56:37 +0800 | [diff] [blame] | 4179 | /* |
| 4180 | * Generate borders given the confine region we are to use. The borders |
| 4181 | * are defined to be the outer region of the allowed area. This means |
| 4182 | * top/left borders are "within" the allowed area, while bottom/right |
| 4183 | * borders are outside. This needs to be considered when clamping |
| 4184 | * confined motion vectors. |
| 4185 | */ |
| 4186 | region_to_outline(region, &borders); |
Jonas Ådahl | d3414f2 | 2016-07-22 17:56:31 +0800 | [diff] [blame] | 4187 | |
Jonas Ådahl | d0be2bb | 2015-04-30 17:56:37 +0800 | [diff] [blame] | 4188 | motion = (struct line) { |
| 4189 | .a = (struct vec2d) { |
| 4190 | .x = wl_fixed_to_double(old_sx), |
| 4191 | .y = wl_fixed_to_double(old_sy), |
| 4192 | }, |
| 4193 | .b = (struct vec2d) { |
| 4194 | .x = wl_fixed_to_double(sx), |
| 4195 | .y = wl_fixed_to_double(sy), |
| 4196 | }, |
| 4197 | }; |
| 4198 | directions = get_motion_directions(&motion); |
| 4199 | |
| 4200 | while (directions) { |
| 4201 | closest_border = get_closest_border(&borders, |
| 4202 | &motion, |
| 4203 | directions); |
| 4204 | if (closest_border) |
| 4205 | clamp_to_border(closest_border, &motion, &directions); |
| 4206 | else |
| 4207 | break; |
| 4208 | } |
| 4209 | |
| 4210 | weston_view_to_global_float(pointer->focus, |
| 4211 | (float) motion.b.x, (float) motion.b.y, |
| 4212 | &new_x_f, &new_y_f); |
| 4213 | *clamped_x = wl_fixed_from_double(new_x_f); |
| 4214 | *clamped_y = wl_fixed_from_double(new_y_f); |
| 4215 | |
| 4216 | wl_array_release(&borders); |
| 4217 | } |
| 4218 | |
| 4219 | static double |
| 4220 | point_to_border_distance_2(struct border *border, double x, double y) |
| 4221 | { |
| 4222 | double orig_x, orig_y; |
| 4223 | double dx, dy; |
| 4224 | |
| 4225 | if (is_border_horizontal(border)) { |
| 4226 | if (x < border->line.a.x) |
| 4227 | orig_x = border->line.a.x; |
| 4228 | else if (x > border->line.b.x) |
| 4229 | orig_x = border->line.b.x; |
| 4230 | else |
| 4231 | orig_x = x; |
| 4232 | orig_y = border->line.a.y; |
| 4233 | } else { |
| 4234 | if (y < border->line.a.y) |
| 4235 | orig_y = border->line.a.y; |
| 4236 | else if (y > border->line.b.y) |
| 4237 | orig_y = border->line.b.y; |
| 4238 | else |
| 4239 | orig_y = y; |
| 4240 | orig_x = border->line.a.x; |
| 4241 | } |
| 4242 | |
| 4243 | |
| 4244 | dx = fabs(orig_x - x); |
| 4245 | dy = fabs(orig_y - y); |
| 4246 | return dx*dx + dy*dy; |
| 4247 | } |
| 4248 | |
| 4249 | static void |
| 4250 | warp_to_behind_border(struct border *border, wl_fixed_t *sx, wl_fixed_t *sy) |
| 4251 | { |
| 4252 | switch (border->blocking_dir) { |
| 4253 | case MOTION_DIRECTION_POSITIVE_X: |
| 4254 | case MOTION_DIRECTION_NEGATIVE_X: |
| 4255 | if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_X) |
| 4256 | *sx = wl_fixed_from_double(border->line.a.x) - 1; |
| 4257 | else |
| 4258 | *sx = wl_fixed_from_double(border->line.a.x) + 1; |
| 4259 | if (*sy < wl_fixed_from_double(border->line.a.y)) |
| 4260 | *sy = wl_fixed_from_double(border->line.a.y) + 1; |
| 4261 | else if (*sy > wl_fixed_from_double(border->line.b.y)) |
| 4262 | *sy = wl_fixed_from_double(border->line.b.y) - 1; |
| 4263 | break; |
| 4264 | case MOTION_DIRECTION_POSITIVE_Y: |
| 4265 | case MOTION_DIRECTION_NEGATIVE_Y: |
| 4266 | if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_Y) |
| 4267 | *sy = wl_fixed_from_double(border->line.a.y) - 1; |
| 4268 | else |
| 4269 | *sy = wl_fixed_from_double(border->line.a.y) + 1; |
| 4270 | if (*sx < wl_fixed_from_double(border->line.a.x)) |
| 4271 | *sx = wl_fixed_from_double(border->line.a.x) + 1; |
| 4272 | else if (*sx > wl_fixed_from_double(border->line.b.x)) |
| 4273 | *sx = wl_fixed_from_double(border->line.b.x) - 1; |
| 4274 | break; |
| 4275 | } |
Jonas Ådahl | d3414f2 | 2016-07-22 17:56:31 +0800 | [diff] [blame] | 4276 | } |
| 4277 | |
| 4278 | static void |
| 4279 | maybe_warp_confined_pointer(struct weston_pointer_constraint *constraint) |
| 4280 | { |
| 4281 | wl_fixed_t x; |
| 4282 | wl_fixed_t y; |
| 4283 | wl_fixed_t sx; |
| 4284 | wl_fixed_t sy; |
| 4285 | |
| 4286 | weston_view_from_global_fixed(constraint->view, |
| 4287 | constraint->pointer->x, |
| 4288 | constraint->pointer->y, |
| 4289 | &sx, |
| 4290 | &sy); |
| 4291 | |
| 4292 | if (!is_within_constraint_region(constraint, sx, sy)) { |
Jonas Ådahl | d0be2bb | 2015-04-30 17:56:37 +0800 | [diff] [blame] | 4293 | double xf = wl_fixed_to_double(sx); |
| 4294 | double yf = wl_fixed_to_double(sy); |
| 4295 | pixman_region32_t confine_region; |
| 4296 | struct wl_array borders; |
| 4297 | struct border *border; |
| 4298 | double closest_distance_2 = DBL_MAX; |
| 4299 | struct border *closest_border = NULL; |
Jonas Ådahl | d3414f2 | 2016-07-22 17:56:31 +0800 | [diff] [blame] | 4300 | |
Jonas Ådahl | d0be2bb | 2015-04-30 17:56:37 +0800 | [diff] [blame] | 4301 | wl_array_init(&borders); |
Jonas Ådahl | d3414f2 | 2016-07-22 17:56:31 +0800 | [diff] [blame] | 4302 | |
Jonas Ådahl | d0be2bb | 2015-04-30 17:56:37 +0800 | [diff] [blame] | 4303 | pixman_region32_init(&confine_region); |
| 4304 | pixman_region32_intersect(&confine_region, |
| 4305 | &constraint->view->surface->input, |
| 4306 | &constraint->region); |
| 4307 | region_to_outline(&confine_region, &borders); |
| 4308 | pixman_region32_fini(&confine_region); |
| 4309 | |
| 4310 | wl_array_for_each(border, &borders) { |
| 4311 | double distance_2; |
| 4312 | |
| 4313 | distance_2 = point_to_border_distance_2(border, xf, yf); |
| 4314 | if (distance_2 < closest_distance_2) { |
| 4315 | closest_border = border; |
| 4316 | closest_distance_2 = distance_2; |
| 4317 | } |
| 4318 | } |
| 4319 | assert(closest_border); |
| 4320 | |
| 4321 | warp_to_behind_border(closest_border, &sx, &sy); |
| 4322 | |
| 4323 | wl_array_release(&borders); |
Jonas Ådahl | d3414f2 | 2016-07-22 17:56:31 +0800 | [diff] [blame] | 4324 | |
| 4325 | weston_view_to_global_fixed(constraint->view, sx, sy, &x, &y); |
| 4326 | weston_pointer_move_to(constraint->pointer, x, y); |
| 4327 | } |
| 4328 | } |
| 4329 | |
| 4330 | static void |
| 4331 | confined_pointer_grab_pointer_motion(struct weston_pointer_grab *grab, |
Alexandros Frantzis | 84b31f8 | 2017-11-24 18:01:46 +0200 | [diff] [blame] | 4332 | const struct timespec *time, |
Jonas Ådahl | d3414f2 | 2016-07-22 17:56:31 +0800 | [diff] [blame] | 4333 | struct weston_pointer_motion_event *event) |
| 4334 | { |
| 4335 | struct weston_pointer_constraint *constraint = |
| 4336 | container_of(grab, struct weston_pointer_constraint, grab); |
| 4337 | struct weston_pointer *pointer = grab->pointer; |
| 4338 | struct weston_surface *surface; |
| 4339 | wl_fixed_t x, y; |
| 4340 | wl_fixed_t old_sx = pointer->sx; |
| 4341 | wl_fixed_t old_sy = pointer->sy; |
| 4342 | pixman_region32_t confine_region; |
| 4343 | |
| 4344 | assert(pointer->focus); |
| 4345 | assert(pointer->focus->surface == constraint->surface); |
| 4346 | |
| 4347 | surface = pointer->focus->surface; |
| 4348 | |
| 4349 | pixman_region32_init(&confine_region); |
| 4350 | pixman_region32_intersect(&confine_region, |
| 4351 | &surface->input, |
| 4352 | &constraint->region); |
| 4353 | weston_pointer_clamp_event_to_region(pointer, event, |
| 4354 | &confine_region, &x, &y); |
| 4355 | weston_pointer_move_to(pointer, x, y); |
| 4356 | pixman_region32_fini(&confine_region); |
| 4357 | |
| 4358 | weston_view_from_global_fixed(pointer->focus, x, y, |
| 4359 | &pointer->sx, &pointer->sy); |
| 4360 | |
| 4361 | if (old_sx != pointer->sx || old_sy != pointer->sy) { |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 4362 | pointer_send_motion(pointer, time, |
| 4363 | pointer->sx, pointer->sy); |
Jonas Ådahl | d3414f2 | 2016-07-22 17:56:31 +0800 | [diff] [blame] | 4364 | } |
| 4365 | |
Quentin Glidic | cde1345 | 2016-08-12 10:41:32 +0200 | [diff] [blame] | 4366 | pointer_send_relative_motion(pointer, time, event); |
Jonas Ådahl | d3414f2 | 2016-07-22 17:56:31 +0800 | [diff] [blame] | 4367 | } |
| 4368 | |
| 4369 | static void |
| 4370 | confined_pointer_grab_pointer_button(struct weston_pointer_grab *grab, |
Alexandros Frantzis | 215bedc | 2017-11-16 18:20:55 +0200 | [diff] [blame] | 4371 | const struct timespec *time, |
Jonas Ådahl | d3414f2 | 2016-07-22 17:56:31 +0800 | [diff] [blame] | 4372 | uint32_t button, |
| 4373 | uint32_t state_w) |
| 4374 | { |
| 4375 | weston_pointer_send_button(grab->pointer, time, button, state_w); |
| 4376 | } |
| 4377 | |
| 4378 | static void |
| 4379 | confined_pointer_grab_pointer_axis(struct weston_pointer_grab *grab, |
Alexandros Frantzis | 8032194 | 2017-11-16 18:20:56 +0200 | [diff] [blame] | 4380 | const struct timespec *time, |
Jonas Ådahl | d3414f2 | 2016-07-22 17:56:31 +0800 | [diff] [blame] | 4381 | struct weston_pointer_axis_event *event) |
| 4382 | { |
| 4383 | weston_pointer_send_axis(grab->pointer, time, event); |
| 4384 | } |
| 4385 | |
| 4386 | static void |
| 4387 | confined_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab, |
| 4388 | uint32_t source) |
| 4389 | { |
| 4390 | weston_pointer_send_axis_source(grab->pointer, source); |
| 4391 | } |
| 4392 | |
| 4393 | static void |
| 4394 | confined_pointer_grab_pointer_frame(struct weston_pointer_grab *grab) |
| 4395 | { |
| 4396 | weston_pointer_send_frame(grab->pointer); |
| 4397 | } |
| 4398 | |
| 4399 | static void |
| 4400 | confined_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab) |
| 4401 | { |
| 4402 | struct weston_pointer_constraint *constraint = |
| 4403 | container_of(grab, struct weston_pointer_constraint, grab); |
| 4404 | |
| 4405 | disable_pointer_constraint(constraint); |
| 4406 | |
| 4407 | /* If this is a persistent constraint, re-add the surface destroy signal |
| 4408 | * listener only if we are currently not destroying the surface. */ |
| 4409 | switch (constraint->lifetime) { |
| 4410 | case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT: |
| 4411 | if (constraint->surface->resource) |
| 4412 | wl_signal_add(&constraint->surface->destroy_signal, |
| 4413 | &constraint->surface_destroy_listener); |
| 4414 | break; |
| 4415 | case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT: |
| 4416 | break; |
| 4417 | } |
| 4418 | } |
| 4419 | |
| 4420 | static const struct weston_pointer_grab_interface |
| 4421 | confined_pointer_grab_interface = { |
| 4422 | confined_pointer_grab_pointer_focus, |
| 4423 | confined_pointer_grab_pointer_motion, |
| 4424 | confined_pointer_grab_pointer_button, |
| 4425 | confined_pointer_grab_pointer_axis, |
| 4426 | confined_pointer_grab_pointer_axis_source, |
| 4427 | confined_pointer_grab_pointer_frame, |
| 4428 | confined_pointer_grab_pointer_cancel, |
| 4429 | }; |
| 4430 | |
| 4431 | static void |
| 4432 | confined_pointer_destroy(struct wl_client *client, |
| 4433 | struct wl_resource *resource) |
| 4434 | { |
| 4435 | wl_resource_destroy(resource); |
| 4436 | } |
| 4437 | |
| 4438 | static void |
| 4439 | confined_pointer_set_region(struct wl_client *client, |
| 4440 | struct wl_resource *resource, |
| 4441 | struct wl_resource *region_resource) |
| 4442 | { |
| 4443 | struct weston_pointer_constraint *constraint = |
| 4444 | wl_resource_get_user_data(resource); |
| 4445 | struct weston_region *region = region_resource ? |
| 4446 | wl_resource_get_user_data(region_resource) : NULL; |
| 4447 | |
| 4448 | if (!constraint) |
| 4449 | return; |
| 4450 | |
| 4451 | if (region) { |
| 4452 | pixman_region32_copy(&constraint->region_pending, |
| 4453 | ®ion->region); |
| 4454 | } else { |
| 4455 | pixman_region32_fini(&constraint->region_pending); |
| 4456 | region_init_infinite(&constraint->region_pending); |
| 4457 | } |
| 4458 | constraint->region_is_pending = true; |
| 4459 | } |
| 4460 | |
| 4461 | static const struct zwp_confined_pointer_v1_interface confined_pointer_interface = { |
| 4462 | confined_pointer_destroy, |
| 4463 | confined_pointer_set_region, |
| 4464 | }; |
| 4465 | |
| 4466 | static void |
| 4467 | pointer_constraints_confine_pointer(struct wl_client *client, |
| 4468 | struct wl_resource *resource, |
| 4469 | uint32_t id, |
| 4470 | struct wl_resource *surface_resource, |
| 4471 | struct wl_resource *pointer_resource, |
| 4472 | struct wl_resource *region_resource, |
| 4473 | uint32_t lifetime) |
| 4474 | { |
| 4475 | struct weston_surface *surface = |
| 4476 | wl_resource_get_user_data(surface_resource); |
| 4477 | struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource); |
| 4478 | struct weston_region *region = region_resource ? |
| 4479 | wl_resource_get_user_data(region_resource) : NULL; |
| 4480 | |
| 4481 | init_pointer_constraint(resource, id, surface, pointer, region, lifetime, |
| 4482 | &zwp_confined_pointer_v1_interface, |
| 4483 | &confined_pointer_interface, |
| 4484 | &confined_pointer_grab_interface); |
| 4485 | } |
| 4486 | |
| 4487 | static const struct zwp_pointer_constraints_v1_interface pointer_constraints_interface = { |
| 4488 | pointer_constraints_destroy, |
| 4489 | pointer_constraints_lock_pointer, |
| 4490 | pointer_constraints_confine_pointer, |
| 4491 | }; |
| 4492 | |
| 4493 | static void |
| 4494 | bind_pointer_constraints(struct wl_client *client, void *data, |
| 4495 | uint32_t version, uint32_t id) |
| 4496 | { |
| 4497 | struct wl_resource *resource; |
| 4498 | |
| 4499 | resource = wl_resource_create(client, |
| 4500 | &zwp_pointer_constraints_v1_interface, |
| 4501 | 1, id); |
| 4502 | |
| 4503 | wl_resource_set_implementation(resource, &pointer_constraints_interface, |
| 4504 | NULL, NULL); |
| 4505 | } |
| 4506 | |
Jonas Ådahl | 30d61d8 | 2014-10-22 21:21:17 +0200 | [diff] [blame] | 4507 | int |
| 4508 | weston_input_init(struct weston_compositor *compositor) |
| 4509 | { |
| 4510 | if (!wl_global_create(compositor->wl_display, |
| 4511 | &zwp_relative_pointer_manager_v1_interface, 1, |
| 4512 | compositor, bind_relative_pointer_manager)) |
| 4513 | return -1; |
| 4514 | |
Jonas Ådahl | d3414f2 | 2016-07-22 17:56:31 +0800 | [diff] [blame] | 4515 | if (!wl_global_create(compositor->wl_display, |
| 4516 | &zwp_pointer_constraints_v1_interface, 1, |
| 4517 | NULL, bind_pointer_constraints)) |
| 4518 | return -1; |
| 4519 | |
Jonas Ådahl | 30d61d8 | 2014-10-22 21:21:17 +0200 | [diff] [blame] | 4520 | return 0; |
| 4521 | } |