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