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