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