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