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