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