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