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