blob: 99c39f7c6bbc6c549387e65fad302a3c2a86eb3c [file] [log] [blame]
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001/*
2 * Copyright © 2013 Intel Corporation
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -05003 * Copyright 2017-2018 Collabora, Ltd.
4 * Copyright 2017-2018 General Electric Company
Kristian Høgsberg2158a882013-04-18 15:07:39 -04005 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07006 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
Kristian Høgsberg2158a882013-04-18 15:07:39 -040013 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070014 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
Kristian Høgsberg2158a882013-04-18 15:07:39 -040026 */
27
Daniel Stone8e7a8bd2013-08-15 01:10:24 +010028#include "config.h"
29
Jonas Ådahld3414f22016-07-22 17:56:31 +080030#include <stdbool.h>
Kristian Høgsberg2158a882013-04-18 15:07:39 -040031#include <stdlib.h>
32#include <stdint.h>
33#include <string.h>
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040034#include <sys/mman.h>
35#include <assert.h>
36#include <unistd.h>
Jonas Ådahld0be2bb2015-04-30 17:56:37 +080037#include <values.h>
Matt Roper01a92732013-06-24 16:52:44 +010038#include <fcntl.h>
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +020039#include <limits.h>
Antonio Borneo39578632019-04-26 23:57:31 +020040#include <errno.h>
Kristian Høgsberg2158a882013-04-18 15:07:39 -040041
Jon Cruz35b2eaa2015-06-15 15:37:08 -070042#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070043#include "shared/os-compatibility.h"
Alexandros Frantzis84b31f82017-11-24 18:01:46 +020044#include "shared/timespec-util.h"
Pekka Paalanen3d5d9472019-03-28 16:28:47 +020045#include <libweston/libweston.h>
Daniel Stone7dbb0e12016-11-24 15:30:41 +000046#include "relative-pointer-unstable-v1-server-protocol.h"
47#include "pointer-constraints-unstable-v1-server-protocol.h"
Alexandros Frantzis538749d2018-02-16 18:44:16 +020048#include "input-timestamps-unstable-v1-server-protocol.h"
Jonas Ådahld3414f22016-07-22 17:56:31 +080049
50enum pointer_constraint_type {
51 POINTER_CONSTRAINT_TYPE_LOCK,
52 POINTER_CONSTRAINT_TYPE_CONFINE,
53};
54
Jonas Ådahld0be2bb2015-04-30 17:56:37 +080055enum motion_direction {
56 MOTION_DIRECTION_POSITIVE_X = 1 << 0,
57 MOTION_DIRECTION_NEGATIVE_X = 1 << 1,
58 MOTION_DIRECTION_POSITIVE_Y = 1 << 2,
59 MOTION_DIRECTION_NEGATIVE_Y = 1 << 3,
60};
61
62struct vec2d {
63 double x, y;
64};
65
66struct line {
67 struct vec2d a;
68 struct vec2d b;
69};
70
71struct border {
72 struct line line;
73 enum motion_direction blocking_dir;
74};
75
Jonas Ådahld3414f22016-07-22 17:56:31 +080076static void
77maybe_warp_confined_pointer(struct weston_pointer_constraint *constraint);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040078
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040079static void
80empty_region(pixman_region32_t *region)
81{
82 pixman_region32_fini(region);
83 pixman_region32_init(region);
84}
85
Jonas Ådahld3414f22016-07-22 17:56:31 +080086static void
87region_init_infinite(pixman_region32_t *region)
88{
89 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
90 UINT32_MAX, UINT32_MAX);
91}
92
Alexandros Frantzis2b442482018-02-20 14:05:50 +020093static void
94send_timestamp(struct wl_resource *resource,
95 const struct timespec *time)
96{
97 uint32_t tv_sec_hi, tv_sec_lo, tv_nsec;
98
99 timespec_to_proto(time, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
100 zwp_input_timestamps_v1_send_timestamp(resource, tv_sec_hi, tv_sec_lo,
101 tv_nsec);
102}
103
104static void
105send_timestamps_for_input_resource(struct wl_resource *input_resource,
106 struct wl_list *list,
107 const struct timespec *time)
108{
109 struct wl_resource *resource;
110
111 wl_resource_for_each(resource, list) {
112 if (wl_resource_get_user_data(resource) == input_resource)
113 send_timestamp(resource, time);
114 }
115}
116
117static void
118remove_input_resource_from_timestamps(struct wl_resource *input_resource,
119 struct wl_list *list)
120{
121 struct wl_resource *resource;
122
123 wl_resource_for_each(resource, list) {
124 if (wl_resource_get_user_data(resource) == input_resource)
125 wl_resource_set_user_data(resource, NULL);
126 }
127}
128
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -0500129/** Register a touchscreen input device
130 *
131 * \param touch The parent weston_touch that identifies the seat.
132 * \param syspath Unique device name.
133 * \param backend_data Backend private data if necessary.
134 * \param ops Calibration operations, or NULL for not able to run calibration.
135 * \return New touch device, or NULL on failure.
136 */
137WL_EXPORT struct weston_touch_device *
138weston_touch_create_touch_device(struct weston_touch *touch,
139 const char *syspath,
140 void *backend_data,
141 const struct weston_touch_device_ops *ops)
142{
143 struct weston_touch_device *device;
144
145 assert(syspath);
146 if (ops) {
147 assert(ops->get_output);
148 assert(ops->get_calibration_head_name);
149 assert(ops->get_calibration);
150 assert(ops->set_calibration);
151 }
152
153 device = zalloc(sizeof *device);
154 if (!device)
155 return NULL;
156
157 wl_signal_init(&device->destroy_signal);
158
159 device->syspath = strdup(syspath);
160 if (!device->syspath) {
161 free(device);
162 return NULL;
163 }
164
165 device->backend_data = backend_data;
166 device->ops = ops;
167
168 device->aggregate = touch;
169 wl_list_insert(touch->device_list.prev, &device->link);
170
171 return device;
172}
173
174/** Destroy the touch device. */
175WL_EXPORT void
176weston_touch_device_destroy(struct weston_touch_device *device)
177{
178 wl_list_remove(&device->link);
179 wl_signal_emit(&device->destroy_signal, device);
180 free(device->syspath);
181 free(device);
182}
183
184/** Is it possible to run calibration on this touch device? */
185WL_EXPORT bool
186weston_touch_device_can_calibrate(struct weston_touch_device *device)
187{
188 return !!device->ops;
189}
190
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -0500191static enum weston_touch_mode
192weston_touch_device_get_mode(struct weston_touch_device *device)
193{
194 return device->aggregate->seat->compositor->touch_mode;
195}
196
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800197static struct weston_pointer_client *
198weston_pointer_client_create(struct wl_client *client)
199{
200 struct weston_pointer_client *pointer_client;
201
202 pointer_client = zalloc(sizeof *pointer_client);
203 if (!pointer_client)
204 return NULL;
205
206 pointer_client->client = client;
207 wl_list_init(&pointer_client->pointer_resources);
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200208 wl_list_init(&pointer_client->relative_pointer_resources);
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800209
210 return pointer_client;
211}
212
213static void
214weston_pointer_client_destroy(struct weston_pointer_client *pointer_client)
215{
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +0200216 struct wl_resource *resource;
217
218 wl_resource_for_each(resource, &pointer_client->pointer_resources) {
219 wl_resource_set_user_data(resource, NULL);
220 }
221
222 wl_resource_for_each(resource,
223 &pointer_client->relative_pointer_resources) {
224 wl_resource_set_user_data(resource, NULL);
225 }
226
227 wl_list_remove(&pointer_client->pointer_resources);
228 wl_list_remove(&pointer_client->relative_pointer_resources);
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800229 free(pointer_client);
230}
231
232static bool
233weston_pointer_client_is_empty(struct weston_pointer_client *pointer_client)
234{
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200235 return (wl_list_empty(&pointer_client->pointer_resources) &&
236 wl_list_empty(&pointer_client->relative_pointer_resources));
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800237}
238
239static struct weston_pointer_client *
240weston_pointer_get_pointer_client(struct weston_pointer *pointer,
241 struct wl_client *client)
242{
243 struct weston_pointer_client *pointer_client;
244
245 wl_list_for_each(pointer_client, &pointer->pointer_clients, link) {
246 if (pointer_client->client == client)
247 return pointer_client;
248 }
249
250 return NULL;
251}
252
253static struct weston_pointer_client *
254weston_pointer_ensure_pointer_client(struct weston_pointer *pointer,
255 struct wl_client *client)
256{
257 struct weston_pointer_client *pointer_client;
258
259 pointer_client = weston_pointer_get_pointer_client(pointer, client);
260 if (pointer_client)
261 return pointer_client;
262
263 pointer_client = weston_pointer_client_create(client);
264 wl_list_insert(&pointer->pointer_clients, &pointer_client->link);
265
266 if (pointer->focus &&
267 pointer->focus->surface->resource &&
268 wl_resource_get_client(pointer->focus->surface->resource) == client) {
269 pointer->focus_client = pointer_client;
270 }
271
272 return pointer_client;
273}
274
275static void
276weston_pointer_cleanup_pointer_client(struct weston_pointer *pointer,
277 struct weston_pointer_client *pointer_client)
278{
279 if (weston_pointer_client_is_empty(pointer_client)) {
280 if (pointer->focus_client == pointer_client)
281 pointer->focus_client = NULL;
282 wl_list_remove(&pointer_client->link);
283 weston_pointer_client_destroy(pointer_client);
284 }
285}
286
287static void
288unbind_pointer_client_resource(struct wl_resource *resource)
289{
290 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
291 struct wl_client *client = wl_resource_get_client(resource);
292 struct weston_pointer_client *pointer_client;
293
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800294 wl_list_remove(wl_resource_get_link(resource));
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +0200295
296 if (pointer) {
297 pointer_client = weston_pointer_get_pointer_client(pointer,
298 client);
299 assert(pointer_client);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200300 remove_input_resource_from_timestamps(resource,
301 &pointer->timestamps_list);
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +0200302 weston_pointer_cleanup_pointer_client(pointer, pointer_client);
303 }
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800304}
305
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400306static void unbind_resource(struct wl_resource *resource)
307{
Jason Ekstrand44a38632013-06-14 10:08:00 -0500308 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400309}
310
Jonas Ådahl3042ffe2013-10-17 23:04:08 +0200311WL_EXPORT void
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200312weston_pointer_motion_to_abs(struct weston_pointer *pointer,
313 struct weston_pointer_motion_event *event,
314 wl_fixed_t *x, wl_fixed_t *y)
315{
316 if (event->mask & WESTON_POINTER_MOTION_ABS) {
317 *x = wl_fixed_from_double(event->x);
318 *y = wl_fixed_from_double(event->y);
319 } else if (event->mask & WESTON_POINTER_MOTION_REL) {
320 *x = pointer->x + wl_fixed_from_double(event->dx);
321 *y = pointer->y + wl_fixed_from_double(event->dy);
322 } else {
323 assert(!"invalid motion event");
324 *x = *y = 0;
325 }
326}
327
328static bool
329weston_pointer_motion_to_rel(struct weston_pointer *pointer,
330 struct weston_pointer_motion_event *event,
331 double *dx, double *dy,
332 double *dx_unaccel, double *dy_unaccel)
333{
334 if (event->mask & WESTON_POINTER_MOTION_REL &&
335 event->mask & WESTON_POINTER_MOTION_REL_UNACCEL) {
336 *dx = event->dx;
337 *dy = event->dy;
338 *dx_unaccel = event->dx_unaccel;
339 *dy_unaccel = event->dy_unaccel;
340 return true;
341 } else if (event->mask & WESTON_POINTER_MOTION_REL) {
342 *dx_unaccel = *dx = event->dx;
343 *dy_unaccel = *dy = event->dy;
344 return true;
345 } else if (event->mask & WESTON_POINTER_MOTION_REL_UNACCEL) {
346 *dx_unaccel = *dx = event->dx_unaccel;
347 *dy_unaccel = *dy = event->dy_unaccel;
348 return true;
349 } else {
350 return false;
351 }
352}
353
354WL_EXPORT void
Kristian Høgsberga71e8b22013-05-06 21:51:21 -0400355weston_seat_repick(struct weston_seat *seat)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400356{
Derek Foreman1281a362015-07-31 16:55:32 -0500357 const struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400358
Derek Foreman1b786ee2015-06-03 15:53:23 -0500359 if (!pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400360 return;
361
Derek Foreman1b786ee2015-06-03 15:53:23 -0500362 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400363}
364
365static void
366weston_compositor_idle_inhibit(struct weston_compositor *compositor)
367{
368 weston_compositor_wake(compositor);
369 compositor->idle_inhibit++;
370}
371
372static void
373weston_compositor_idle_release(struct weston_compositor *compositor)
374{
375 compositor->idle_inhibit--;
376 weston_compositor_wake(compositor);
377}
378
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400379static void
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100380pointer_focus_view_destroyed(struct wl_listener *listener, void *data)
381{
382 struct weston_pointer *pointer =
383 container_of(listener, struct weston_pointer,
384 focus_view_listener);
385
Derek Foremanf9318d12015-05-11 15:40:11 -0500386 weston_pointer_clear_focus(pointer);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100387}
388
389static void
390pointer_focus_resource_destroyed(struct wl_listener *listener, void *data)
391{
392 struct weston_pointer *pointer =
393 container_of(listener, struct weston_pointer,
394 focus_resource_listener);
395
Derek Foremanf9318d12015-05-11 15:40:11 -0500396 weston_pointer_clear_focus(pointer);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100397}
398
399static void
400keyboard_focus_resource_destroyed(struct wl_listener *listener, void *data)
401{
402 struct weston_keyboard *keyboard =
403 container_of(listener, struct weston_keyboard,
404 focus_resource_listener);
405
406 weston_keyboard_set_focus(keyboard, NULL);
407}
408
409static void
410touch_focus_view_destroyed(struct wl_listener *listener, void *data)
411{
412 struct weston_touch *touch =
413 container_of(listener, struct weston_touch,
414 focus_view_listener);
415
Derek Foreman4c93c082015-04-30 16:45:41 -0500416 weston_touch_set_focus(touch, NULL);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100417}
418
419static void
420touch_focus_resource_destroyed(struct wl_listener *listener, void *data)
421{
422 struct weston_touch *touch =
423 container_of(listener, struct weston_touch,
424 focus_resource_listener);
425
Derek Foreman4c93c082015-04-30 16:45:41 -0500426 weston_touch_set_focus(touch, NULL);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100427}
428
429static void
Neil Roberts96d790e2013-09-19 17:32:00 +0100430move_resources(struct wl_list *destination, struct wl_list *source)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400431{
Neil Roberts96d790e2013-09-19 17:32:00 +0100432 wl_list_insert_list(destination, source);
433 wl_list_init(source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400434}
435
436static void
Neil Roberts96d790e2013-09-19 17:32:00 +0100437move_resources_for_client(struct wl_list *destination,
438 struct wl_list *source,
439 struct wl_client *client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400440{
Neil Roberts96d790e2013-09-19 17:32:00 +0100441 struct wl_resource *resource, *tmp;
442 wl_resource_for_each_safe(resource, tmp, source) {
443 if (wl_resource_get_client(resource) == client) {
444 wl_list_remove(wl_resource_get_link(resource));
445 wl_list_insert(destination,
446 wl_resource_get_link(resource));
447 }
448 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400449}
450
451static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700452default_grab_pointer_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400453{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400454 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500455 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400456 wl_fixed_t sx, sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400457
458 if (pointer->button_count > 0)
459 return;
460
Jason Ekstranda7af7042013-10-12 22:38:11 -0500461 view = weston_compositor_pick_view(pointer->seat->compositor,
462 pointer->x, pointer->y,
463 &sx, &sy);
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400464
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -0800465 if (pointer->focus != view || pointer->sx != sx || pointer->sy != sy)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500466 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400467}
468
469static void
Quentin Glidiccde13452016-08-12 10:41:32 +0200470pointer_send_relative_motion(struct weston_pointer *pointer,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200471 const struct timespec *time,
Quentin Glidiccde13452016-08-12 10:41:32 +0200472 struct weston_pointer_motion_event *event)
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200473{
474 uint64_t time_usec;
475 double dx, dy, dx_unaccel, dy_unaccel;
476 wl_fixed_t dxf, dyf, dxf_unaccel, dyf_unaccel;
477 struct wl_list *resource_list;
478 struct wl_resource *resource;
479
480 if (!pointer->focus_client)
481 return;
482
483 if (!weston_pointer_motion_to_rel(pointer, event,
484 &dx, &dy,
485 &dx_unaccel, &dy_unaccel))
486 return;
487
488 resource_list = &pointer->focus_client->relative_pointer_resources;
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200489 time_usec = timespec_to_usec(&event->time);
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200490 if (time_usec == 0)
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200491 time_usec = timespec_to_usec(time);
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200492
493 dxf = wl_fixed_from_double(dx);
494 dyf = wl_fixed_from_double(dy);
495 dxf_unaccel = wl_fixed_from_double(dx_unaccel);
496 dyf_unaccel = wl_fixed_from_double(dy_unaccel);
497
498 wl_resource_for_each(resource, resource_list) {
499 zwp_relative_pointer_v1_send_relative_motion(
500 resource,
501 (uint32_t) (time_usec >> 32),
502 (uint32_t) time_usec,
503 dxf, dyf,
504 dxf_unaccel, dyf_unaccel);
505 }
506}
507
508static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200509pointer_send_motion(struct weston_pointer *pointer,
510 const struct timespec *time,
Quentin Glidiccde13452016-08-12 10:41:32 +0200511 wl_fixed_t sx, wl_fixed_t sy)
Jonas Ådahlf44942e2016-07-22 17:54:55 +0800512{
513 struct wl_list *resource_list;
514 struct wl_resource *resource;
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200515 uint32_t msecs;
Jonas Ådahlf44942e2016-07-22 17:54:55 +0800516
517 if (!pointer->focus_client)
518 return;
519
520 resource_list = &pointer->focus_client->pointer_resources;
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200521 msecs = timespec_to_msec(time);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200522 wl_resource_for_each(resource, resource_list) {
523 send_timestamps_for_input_resource(resource,
524 &pointer->timestamps_list,
525 time);
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200526 wl_pointer_send_motion(resource, msecs, sx, sy);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200527 }
Jonas Ådahlf44942e2016-07-22 17:54:55 +0800528}
529
Quentin Glidiccde13452016-08-12 10:41:32 +0200530WL_EXPORT void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200531weston_pointer_send_motion(struct weston_pointer *pointer,
532 const struct timespec *time,
Quentin Glidiccde13452016-08-12 10:41:32 +0200533 struct weston_pointer_motion_event *event)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400534{
Jonas Ådahld2510102014-10-05 21:39:14 +0200535 wl_fixed_t x, y;
Jonas Ådahl8283c342015-04-24 15:26:17 +0800536 wl_fixed_t old_sx = pointer->sx;
537 wl_fixed_t old_sy = pointer->sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400538
Jonas Ådahld2510102014-10-05 21:39:14 +0200539 if (pointer->focus) {
540 weston_pointer_motion_to_abs(pointer, event, &x, &y);
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -0800541 weston_view_from_global_fixed(pointer->focus, x, y,
542 &pointer->sx, &pointer->sy);
Jonas Ådahld2510102014-10-05 21:39:14 +0200543 }
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -0800544
Jonas Ådahld2510102014-10-05 21:39:14 +0200545 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100546
Jonas Ådahlf44942e2016-07-22 17:54:55 +0800547 if (old_sx != pointer->sx || old_sy != pointer->sy) {
Quentin Glidiccde13452016-08-12 10:41:32 +0200548 pointer_send_motion(pointer, time,
549 pointer->sx, pointer->sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400550 }
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200551
Quentin Glidiccde13452016-08-12 10:41:32 +0200552 pointer_send_relative_motion(pointer, time, event);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400553}
554
555static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200556default_grab_pointer_motion(struct weston_pointer_grab *grab,
557 const struct timespec *time,
Quentin Glidiccde13452016-08-12 10:41:32 +0200558 struct weston_pointer_motion_event *event)
559{
560 weston_pointer_send_motion(grab->pointer, time, event);
561}
562
563/** Check if the pointer has focused resources.
564 *
565 * \param pointer The pointer to check for focused resources.
566 * \return Whether or not this pointer has focused resources
567 */
568WL_EXPORT bool
569weston_pointer_has_focus_resource(struct weston_pointer *pointer)
570{
571 if (!pointer->focus_client)
572 return false;
573
574 if (wl_list_empty(&pointer->focus_client->pointer_resources))
575 return false;
576
577 return true;
578}
579
580/** Send wl_pointer.button events to focused resources.
581 *
582 * \param pointer The pointer where the button events originates from.
583 * \param time The timestamp of the event
584 * \param button The button value of the event
Marius Vlada2dace22019-06-12 16:05:44 +0300585 * \param state The state enum value of the event
Quentin Glidiccde13452016-08-12 10:41:32 +0200586 *
587 * For every resource that is currently in focus, send a wl_pointer.button event
588 * with the passed parameters. The focused resources are the wl_pointer
589 * resources of the client which currently has the surface with pointer focus.
590 */
591WL_EXPORT void
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800592weston_pointer_send_button(struct weston_pointer *pointer,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200593 const struct timespec *time, uint32_t button,
Quentin Glidiccde13452016-08-12 10:41:32 +0200594 enum wl_pointer_button_state state)
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800595{
596 struct wl_display *display = pointer->seat->compositor->wl_display;
597 struct wl_list *resource_list;
598 struct wl_resource *resource;
599 uint32_t serial;
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200600 uint32_t msecs;
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800601
Quentin Glidiccde13452016-08-12 10:41:32 +0200602 if (!weston_pointer_has_focus_resource(pointer))
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800603 return;
604
605 resource_list = &pointer->focus_client->pointer_resources;
Quentin Glidiccde13452016-08-12 10:41:32 +0200606 serial = wl_display_next_serial(display);
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200607 msecs = timespec_to_msec(time);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200608 wl_resource_for_each(resource, resource_list) {
609 send_timestamps_for_input_resource(resource,
610 &pointer->timestamps_list,
611 time);
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200612 wl_pointer_send_button(resource, serial, msecs, button, state);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200613 }
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800614}
615
616static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700617default_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200618 const struct timespec *time, uint32_t button,
Quentin Glidiccde13452016-08-12 10:41:32 +0200619 enum wl_pointer_button_state state)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400620{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400621 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400622 struct weston_compositor *compositor = pointer->seat->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500623 struct weston_view *view;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400624 wl_fixed_t sx, sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400625
Quentin Glidiccde13452016-08-12 10:41:32 +0200626 weston_pointer_send_button(pointer, time, button, state);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400627
628 if (pointer->button_count == 0 &&
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400629 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500630 view = weston_compositor_pick_view(compositor,
631 pointer->x, pointer->y,
632 &sx, &sy);
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400633
Jason Ekstranda7af7042013-10-12 22:38:11 -0500634 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400635 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400636}
637
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200638/** Send wl_pointer.axis events to focused resources.
639 *
640 * \param pointer The pointer where the axis events originates from.
641 * \param time The timestamp of the event
Marius Vlada2dace22019-06-12 16:05:44 +0300642 * \param event The axis value of the event
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200643 *
644 * For every resource that is currently in focus, send a wl_pointer.axis event
645 * with the passed parameters. The focused resources are the wl_pointer
646 * resources of the client which currently has the surface with pointer focus.
647 */
648WL_EXPORT void
649weston_pointer_send_axis(struct weston_pointer *pointer,
Alexandros Frantzis80321942017-11-16 18:20:56 +0200650 const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +1000651 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200652{
653 struct wl_resource *resource;
654 struct wl_list *resource_list;
Alexandros Frantzis80321942017-11-16 18:20:56 +0200655 uint32_t msecs;
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200656
Quentin Glidiccde13452016-08-12 10:41:32 +0200657 if (!weston_pointer_has_focus_resource(pointer))
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800658 return;
659
660 resource_list = &pointer->focus_client->pointer_resources;
Alexandros Frantzis80321942017-11-16 18:20:56 +0200661 msecs = timespec_to_msec(time);
Peter Hutterer87743e92016-01-18 16:38:22 +1000662 wl_resource_for_each(resource, resource_list) {
663 if (event->has_discrete &&
664 wl_resource_get_version(resource) >=
665 WL_POINTER_AXIS_DISCRETE_SINCE_VERSION)
666 wl_pointer_send_axis_discrete(resource, event->axis,
667 event->discrete);
668
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200669 if (event->value) {
670 send_timestamps_for_input_resource(resource,
671 &pointer->timestamps_list,
672 time);
Alexandros Frantzis80321942017-11-16 18:20:56 +0200673 wl_pointer_send_axis(resource, msecs,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200674 event->axis,
675 wl_fixed_from_double(event->value));
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200676 } else if (wl_resource_get_version(resource) >=
677 WL_POINTER_AXIS_STOP_SINCE_VERSION) {
678 send_timestamps_for_input_resource(resource,
679 &pointer->timestamps_list,
680 time);
Alexandros Frantzis80321942017-11-16 18:20:56 +0200681 wl_pointer_send_axis_stop(resource, msecs,
Peter Hutterer87743e92016-01-18 16:38:22 +1000682 event->axis);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200683 }
Peter Hutterer87743e92016-01-18 16:38:22 +1000684 }
685}
686
Quentin Glidiccde13452016-08-12 10:41:32 +0200687/** Send wl_pointer.axis_source events to focused resources.
688 *
689 * \param pointer The pointer where the axis_source events originates from.
690 * \param source The axis_source enum value of the event
691 *
692 * For every resource that is currently in focus, send a wl_pointer.axis_source
693 * event with the passed parameter. The focused resources are the wl_pointer
694 * resources of the client which currently has the surface with pointer focus.
695 */
Peter Hutterer87743e92016-01-18 16:38:22 +1000696WL_EXPORT void
Quentin Glidiccde13452016-08-12 10:41:32 +0200697weston_pointer_send_axis_source(struct weston_pointer *pointer,
698 enum wl_pointer_axis_source source)
Peter Hutterer87743e92016-01-18 16:38:22 +1000699{
700 struct wl_resource *resource;
701 struct wl_list *resource_list;
702
Quentin Glidiccde13452016-08-12 10:41:32 +0200703 if (!weston_pointer_has_focus_resource(pointer))
Jonas Ådahled6014a2016-04-21 10:21:48 +0800704 return;
705
Peter Hutterer87743e92016-01-18 16:38:22 +1000706 resource_list = &pointer->focus_client->pointer_resources;
707 wl_resource_for_each(resource, resource_list) {
708 if (wl_resource_get_version(resource) >=
709 WL_POINTER_AXIS_SOURCE_SINCE_VERSION) {
710 wl_pointer_send_axis_source(resource, source);
711 }
712 }
713}
714
715static void
716pointer_send_frame(struct wl_resource *resource)
717{
718 if (wl_resource_get_version(resource) >=
719 WL_POINTER_FRAME_SINCE_VERSION) {
720 wl_pointer_send_frame(resource);
721 }
722}
723
Quentin Glidiccde13452016-08-12 10:41:32 +0200724/** Send wl_pointer.frame events to focused resources.
725 *
726 * \param pointer The pointer where the frame events originates from.
727 *
728 * For every resource that is currently in focus, send a wl_pointer.frame event.
729 * The focused resources are the wl_pointer resources of the client which
730 * currently has the surface with pointer focus.
731 */
Peter Hutterer87743e92016-01-18 16:38:22 +1000732WL_EXPORT void
733weston_pointer_send_frame(struct weston_pointer *pointer)
734{
735 struct wl_resource *resource;
736 struct wl_list *resource_list;
737
Quentin Glidiccde13452016-08-12 10:41:32 +0200738 if (!weston_pointer_has_focus_resource(pointer))
Derek Foreman8efa31b2016-01-29 10:29:46 -0600739 return;
740
Peter Hutterer87743e92016-01-18 16:38:22 +1000741 resource_list = &pointer->focus_client->pointer_resources;
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200742 wl_resource_for_each(resource, resource_list)
Peter Hutterer87743e92016-01-18 16:38:22 +1000743 pointer_send_frame(resource);
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200744}
745
746static void
747default_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +0200748 const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +1000749 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200750{
Peter Hutterer89b6a492016-01-18 15:58:17 +1000751 weston_pointer_send_axis(grab->pointer, time, event);
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200752}
753
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200754static void
Peter Hutterer87743e92016-01-18 16:38:22 +1000755default_grab_pointer_axis_source(struct weston_pointer_grab *grab,
Quentin Glidiccde13452016-08-12 10:41:32 +0200756 enum wl_pointer_axis_source source)
Peter Hutterer87743e92016-01-18 16:38:22 +1000757{
758 weston_pointer_send_axis_source(grab->pointer, source);
759}
760
761static void
762default_grab_pointer_frame(struct weston_pointer_grab *grab)
763{
764 weston_pointer_send_frame(grab->pointer);
765}
766
767static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200768default_grab_pointer_cancel(struct weston_pointer_grab *grab)
769{
770}
771
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400772static const struct weston_pointer_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400773 default_pointer_grab_interface = {
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700774 default_grab_pointer_focus,
775 default_grab_pointer_motion,
776 default_grab_pointer_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200777 default_grab_pointer_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +1000778 default_grab_pointer_axis_source,
779 default_grab_pointer_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200780 default_grab_pointer_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400781};
782
Quentin Glidiccde13452016-08-12 10:41:32 +0200783/** Check if the touch has focused resources.
784 *
785 * \param touch The touch to check for focused resources.
786 * \return Whether or not this touch has focused resources
787 */
788WL_EXPORT bool
789weston_touch_has_focus_resource(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400790{
Quentin Glidiccde13452016-08-12 10:41:32 +0200791 if (!touch->focus)
792 return false;
793
794 if (wl_list_empty(&touch->focus_resource_list))
795 return false;
796
797 return true;
798}
799
800/** Send wl_touch.down events to focused resources.
801 *
802 * \param touch The touch where the down events originates from.
803 * \param time The timestamp of the event
804 * \param touch_id The touch_id value of the event
805 * \param x The x value of the event
806 * \param y The y value of the event
807 *
808 * For every resource that is currently in focus, send a wl_touch.down event
809 * with the passed parameters. The focused resources are the wl_touch
810 * resources of the client which currently has the surface with touch focus.
811 */
812WL_EXPORT void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200813weston_touch_send_down(struct weston_touch *touch, const struct timespec *time,
Quentin Glidiccde13452016-08-12 10:41:32 +0200814 int touch_id, wl_fixed_t x, wl_fixed_t y)
815{
Rob Bradford880ebc72013-07-22 17:31:38 +0100816 struct wl_display *display = touch->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400817 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100818 struct wl_resource *resource;
819 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300820 wl_fixed_t sx, sy;
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200821 uint32_t msecs;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300822
Quentin Glidiccde13452016-08-12 10:41:32 +0200823 if (!weston_touch_has_focus_resource(touch))
Bryce Harrington2c40d1d2016-02-02 10:18:48 -0800824 return;
825
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300826 weston_view_from_global_fixed(touch->focus, x, y, &sx, &sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400827
Neil Roberts96d790e2013-09-19 17:32:00 +0100828 resource_list = &touch->focus_resource_list;
Quentin Glidiccde13452016-08-12 10:41:32 +0200829 serial = wl_display_next_serial(display);
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200830 msecs = timespec_to_msec(time);
Alexandros Frantzisd7157842018-02-20 14:07:03 +0200831 wl_resource_for_each(resource, resource_list) {
832 send_timestamps_for_input_resource(resource,
833 &touch->timestamps_list,
834 time);
835 wl_touch_send_down(resource, serial, msecs,
836 touch->focus->surface->resource,
837 touch_id, sx, sy);
838 }
Quentin Glidiccde13452016-08-12 10:41:32 +0200839}
Neil Roberts96d790e2013-09-19 17:32:00 +0100840
Quentin Glidiccde13452016-08-12 10:41:32 +0200841static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200842default_grab_touch_down(struct weston_touch_grab *grab,
843 const struct timespec *time, int touch_id,
844 wl_fixed_t x, wl_fixed_t y)
Quentin Glidiccde13452016-08-12 10:41:32 +0200845{
846 weston_touch_send_down(grab->touch, time, touch_id, x, y);
847}
848
849/** Send wl_touch.up events to focused resources.
850 *
851 * \param touch The touch where the up events originates from.
852 * \param time The timestamp of the event
853 * \param touch_id The touch_id value of the event
854 *
855 * For every resource that is currently in focus, send a wl_touch.up event
856 * with the passed parameters. The focused resources are the wl_touch
857 * resources of the client which currently has the surface with touch focus.
858 */
859WL_EXPORT void
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200860weston_touch_send_up(struct weston_touch *touch, const struct timespec *time,
861 int touch_id)
Quentin Glidiccde13452016-08-12 10:41:32 +0200862{
863 struct wl_display *display = touch->seat->compositor->wl_display;
864 uint32_t serial;
865 struct wl_resource *resource;
866 struct wl_list *resource_list;
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200867 uint32_t msecs;
Quentin Glidiccde13452016-08-12 10:41:32 +0200868
869 if (!weston_touch_has_focus_resource(touch))
870 return;
871
872 resource_list = &touch->focus_resource_list;
873 serial = wl_display_next_serial(display);
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200874 msecs = timespec_to_msec(time);
Alexandros Frantzisd7157842018-02-20 14:07:03 +0200875 wl_resource_for_each(resource, resource_list) {
876 send_timestamps_for_input_resource(resource,
877 &touch->timestamps_list,
878 time);
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200879 wl_touch_send_up(resource, serial, msecs, touch_id);
Alexandros Frantzisd7157842018-02-20 14:07:03 +0200880 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400881}
882
Kristian Høgsberge329f362013-05-06 22:19:57 -0400883static void
884default_grab_touch_up(struct weston_touch_grab *grab,
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200885 const struct timespec *time, int touch_id)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400886{
Quentin Glidiccde13452016-08-12 10:41:32 +0200887 weston_touch_send_up(grab->touch, time, touch_id);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400888}
889
Quentin Glidiccde13452016-08-12 10:41:32 +0200890/** Send wl_touch.motion events to focused resources.
891 *
892 * \param touch The touch where the motion events originates from.
893 * \param time The timestamp of the event
894 * \param touch_id The touch_id value of the event
895 * \param x The x value of the event
896 * \param y The y value of the event
897 *
898 * For every resource that is currently in focus, send a wl_touch.motion event
899 * with the passed parameters. The focused resources are the wl_touch
900 * resources of the client which currently has the surface with touch focus.
901 */
902WL_EXPORT void
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200903weston_touch_send_motion(struct weston_touch *touch,
904 const struct timespec *time, int touch_id,
905 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400906{
Neil Roberts96d790e2013-09-19 17:32:00 +0100907 struct wl_resource *resource;
908 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300909 wl_fixed_t sx, sy;
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200910 uint32_t msecs;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300911
Quentin Glidiccde13452016-08-12 10:41:32 +0200912 if (!weston_touch_has_focus_resource(touch))
913 return;
914
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300915 weston_view_from_global_fixed(touch->focus, x, y, &sx, &sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400916
Neil Roberts96d790e2013-09-19 17:32:00 +0100917 resource_list = &touch->focus_resource_list;
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200918 msecs = timespec_to_msec(time);
Neil Roberts96d790e2013-09-19 17:32:00 +0100919 wl_resource_for_each(resource, resource_list) {
Alexandros Frantzisd7157842018-02-20 14:07:03 +0200920 send_timestamps_for_input_resource(resource,
921 &touch->timestamps_list,
922 time);
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200923 wl_touch_send_motion(resource, msecs,
Kristian Høgsberge329f362013-05-06 22:19:57 -0400924 touch_id, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400925 }
926}
927
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200928static void
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200929default_grab_touch_motion(struct weston_touch_grab *grab,
930 const struct timespec *time, int touch_id,
931 wl_fixed_t x, wl_fixed_t y)
Quentin Glidiccde13452016-08-12 10:41:32 +0200932{
933 weston_touch_send_motion(grab->touch, time, touch_id, x, y);
934}
935
936
937/** Send wl_touch.frame events to focused resources.
938 *
939 * \param touch The touch where the frame events originates from.
940 *
941 * For every resource that is currently in focus, send a wl_touch.frame event.
942 * The focused resources are the wl_touch resources of the client which
943 * currently has the surface with touch focus.
944 */
945WL_EXPORT void
946weston_touch_send_frame(struct weston_touch *touch)
Jonas Ådahl1679f232014-04-12 09:39:51 +0200947{
948 struct wl_resource *resource;
949
Quentin Glidiccde13452016-08-12 10:41:32 +0200950 if (!weston_touch_has_focus_resource(touch))
951 return;
952
953 wl_resource_for_each(resource, &touch->focus_resource_list)
Jonas Ådahl1679f232014-04-12 09:39:51 +0200954 wl_touch_send_frame(resource);
955}
956
957static void
Quentin Glidiccde13452016-08-12 10:41:32 +0200958default_grab_touch_frame(struct weston_touch_grab *grab)
959{
960 weston_touch_send_frame(grab->touch);
961}
962
963static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200964default_grab_touch_cancel(struct weston_touch_grab *grab)
965{
966}
967
Kristian Høgsberge329f362013-05-06 22:19:57 -0400968static const struct weston_touch_grab_interface default_touch_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400969 default_grab_touch_down,
970 default_grab_touch_up,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200971 default_grab_touch_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +0200972 default_grab_touch_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200973 default_grab_touch_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400974};
975
Quentin Glidiccde13452016-08-12 10:41:32 +0200976/** Check if the keyboard has focused resources.
977 *
978 * \param keyboard The keyboard to check for focused resources.
979 * \return Whether or not this keyboard has focused resources
980 */
981WL_EXPORT bool
982weston_keyboard_has_focus_resource(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400983{
Quentin Glidiccde13452016-08-12 10:41:32 +0200984 if (!keyboard->focus)
985 return false;
986
987 if (wl_list_empty(&keyboard->focus_resource_list))
988 return false;
989
990 return true;
991}
992
993/** Send wl_keyboard.key events to focused resources.
994 *
995 * \param keyboard The keyboard where the key events originates from.
996 * \param time The timestamp of the event
997 * \param key The key value of the event
998 * \param state The state enum value of the event
999 *
1000 * For every resource that is currently in focus, send a wl_keyboard.key event
1001 * with the passed parameters. The focused resources are the wl_keyboard
1002 * resources of the client which currently has the surface with keyboard focus.
1003 */
1004WL_EXPORT void
1005weston_keyboard_send_key(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02001006 const struct timespec *time, uint32_t key,
Quentin Glidiccde13452016-08-12 10:41:32 +02001007 enum wl_keyboard_key_state state)
1008{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001009 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +01001010 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001011 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01001012 struct wl_list *resource_list;
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02001013 uint32_t msecs;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001014
Quentin Glidiccde13452016-08-12 10:41:32 +02001015 if (!weston_keyboard_has_focus_resource(keyboard))
1016 return;
1017
Neil Roberts96d790e2013-09-19 17:32:00 +01001018 resource_list = &keyboard->focus_resource_list;
Quentin Glidiccde13452016-08-12 10:41:32 +02001019 serial = wl_display_next_serial(display);
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02001020 msecs = timespec_to_msec(time);
Alexandros Frantzis2b442482018-02-20 14:05:50 +02001021 wl_resource_for_each(resource, resource_list) {
1022 send_timestamps_for_input_resource(resource,
1023 &keyboard->timestamps_list,
1024 time);
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02001025 wl_keyboard_send_key(resource, serial, msecs, key, state);
Alexandros Frantzis2b442482018-02-20 14:05:50 +02001026 }
Quentin Glidiccde13452016-08-12 10:41:32 +02001027};
1028
1029static void
1030default_grab_keyboard_key(struct weston_keyboard_grab *grab,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02001031 const struct timespec *time, uint32_t key,
1032 uint32_t state)
Quentin Glidiccde13452016-08-12 10:41:32 +02001033{
1034 weston_keyboard_send_key(grab->keyboard, time, key, state);
Neil Roberts96d790e2013-09-19 17:32:00 +01001035}
1036
1037static void
1038send_modifiers_to_resource(struct weston_keyboard *keyboard,
1039 struct wl_resource *resource,
1040 uint32_t serial)
1041{
1042 wl_keyboard_send_modifiers(resource,
1043 serial,
1044 keyboard->modifiers.mods_depressed,
1045 keyboard->modifiers.mods_latched,
1046 keyboard->modifiers.mods_locked,
1047 keyboard->modifiers.group);
1048}
1049
1050static void
1051send_modifiers_to_client_in_list(struct wl_client *client,
1052 struct wl_list *list,
1053 uint32_t serial,
1054 struct weston_keyboard *keyboard)
1055{
1056 struct wl_resource *resource;
1057
1058 wl_resource_for_each(resource, list) {
1059 if (wl_resource_get_client(resource) == client)
1060 send_modifiers_to_resource(keyboard,
1061 resource,
1062 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001063 }
1064}
1065
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001066static struct weston_pointer_client *
1067find_pointer_client_for_surface(struct weston_pointer *pointer,
1068 struct weston_surface *surface)
1069{
1070 struct wl_client *client;
1071
1072 if (!surface)
1073 return NULL;
1074
1075 if (!surface->resource)
1076 return NULL;
1077
1078 client = wl_resource_get_client(surface->resource);
1079 return weston_pointer_get_pointer_client(pointer, client);
1080}
1081
1082static struct weston_pointer_client *
1083find_pointer_client_for_view(struct weston_pointer *pointer, struct weston_view *view)
1084{
1085 if (!view)
1086 return NULL;
1087
1088 return find_pointer_client_for_surface(pointer, view->surface);
1089}
1090
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001091static struct wl_resource *
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001092find_resource_for_surface(struct wl_list *list, struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001093{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001094 if (!surface)
1095 return NULL;
1096
Jason Ekstrand44a38632013-06-14 10:08:00 -05001097 if (!surface->resource)
1098 return NULL;
Stefan Schmidtfda26522013-09-17 10:54:09 +01001099
Jason Ekstrand44a38632013-06-14 10:08:00 -05001100 return wl_resource_find_for_client(list, wl_resource_get_client(surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001101}
1102
Quentin Glidiccde13452016-08-12 10:41:32 +02001103/** Send wl_keyboard.modifiers events to focused resources and pointer
1104 * focused resources.
1105 *
1106 * \param keyboard The keyboard where the modifiers events originates from.
1107 * \param serial The serial of the event
1108 * \param mods_depressed The mods_depressed value of the event
1109 * \param mods_latched The mods_latched value of the event
1110 * \param mods_locked The mods_locked value of the event
1111 * \param group The group value of the event
1112 *
1113 * For every resource that is currently in focus, send a wl_keyboard.modifiers
1114 * event with the passed parameters. The focused resources are the wl_keyboard
1115 * resources of the client which currently has the surface with keyboard focus.
1116 * This also sends wl_keyboard.modifiers events to the wl_keyboard resources of
1117 * the client having pointer focus (if different from the keyboard focus client).
1118 */
1119WL_EXPORT void
1120weston_keyboard_send_modifiers(struct weston_keyboard *keyboard,
1121 uint32_t serial, uint32_t mods_depressed,
1122 uint32_t mods_latched,
1123 uint32_t mods_locked, uint32_t group)
1124{
1125 struct weston_pointer *pointer =
1126 weston_seat_get_pointer(keyboard->seat);
1127
1128 if (weston_keyboard_has_focus_resource(keyboard)) {
1129 struct wl_list *resource_list;
1130 struct wl_resource *resource;
1131
1132 resource_list = &keyboard->focus_resource_list;
1133 wl_resource_for_each(resource, resource_list) {
1134 wl_keyboard_send_modifiers(resource, serial,
1135 mods_depressed, mods_latched,
1136 mods_locked, group);
1137 }
1138 }
1139
1140 if (pointer && pointer->focus && pointer->focus->surface->resource &&
1141 pointer->focus->surface != keyboard->focus) {
1142 struct wl_client *pointer_client =
1143 wl_resource_get_client(pointer->focus->surface->resource);
1144
1145 send_modifiers_to_client_in_list(pointer_client,
1146 &keyboard->resource_list,
1147 serial,
1148 keyboard);
1149 }
1150}
1151
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001152static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -07001153default_grab_keyboard_modifiers(struct weston_keyboard_grab *grab,
1154 uint32_t serial, uint32_t mods_depressed,
1155 uint32_t mods_latched,
1156 uint32_t mods_locked, uint32_t group)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001157{
Quentin Glidiccde13452016-08-12 10:41:32 +02001158 weston_keyboard_send_modifiers(grab->keyboard, serial, mods_depressed,
1159 mods_latched, mods_locked, group);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001160}
1161
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001162static void
1163default_grab_keyboard_cancel(struct weston_keyboard_grab *grab)
1164{
1165}
1166
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001167static const struct weston_keyboard_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001168 default_keyboard_grab_interface = {
Kristian Høgsbergb27901c2013-10-28 15:32:02 -07001169 default_grab_keyboard_key,
1170 default_grab_keyboard_modifiers,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001171 default_grab_keyboard_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001172};
1173
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001174static void
1175pointer_unmap_sprite(struct weston_pointer *pointer)
1176{
Pekka Paalanenc557ff72014-11-12 16:42:52 +02001177 struct weston_surface *surface = pointer->sprite->surface;
1178
1179 if (weston_surface_is_mapped(surface))
1180 weston_surface_unmap(surface);
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001181
1182 wl_list_remove(&pointer->sprite_destroy_listener.link);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02001183 surface->committed = NULL;
1184 surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03001185 weston_surface_set_label_func(surface, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001186 weston_view_destroy(pointer->sprite);
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001187 pointer->sprite = NULL;
1188}
1189
1190static void
1191pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
1192{
1193 struct weston_pointer *pointer =
1194 container_of(listener, struct weston_pointer,
1195 sprite_destroy_listener);
1196
1197 pointer->sprite = NULL;
1198}
1199
Jonas Ådahl3e12e632013-12-02 22:05:05 +01001200static void
1201weston_pointer_reset_state(struct weston_pointer *pointer)
1202{
1203 pointer->button_count = 0;
1204}
1205
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001206static void
1207weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data);
1208
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001209static struct weston_pointer *
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001210weston_pointer_create(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001211{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001212 struct weston_pointer *pointer;
1213
Peter Huttererf3d62272013-08-08 11:57:05 +10001214 pointer = zalloc(sizeof *pointer);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001215 if (pointer == NULL)
1216 return NULL;
1217
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001218 wl_list_init(&pointer->pointer_clients);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001219 weston_pointer_set_default_grab(pointer,
1220 seat->compositor->default_pointer_grab);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001221 wl_list_init(&pointer->focus_resource_listener.link);
1222 pointer->focus_resource_listener.notify = pointer_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001223 pointer->default_grab.pointer = pointer;
1224 pointer->grab = &pointer->default_grab;
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001225 wl_signal_init(&pointer->motion_signal);
Emilio Pozuelo Monfortaa7a4762013-11-19 11:37:15 +01001226 wl_signal_init(&pointer->focus_signal);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001227 wl_list_init(&pointer->focus_view_listener.link);
Jonas Ådahl3eb4ddd2016-07-22 17:52:58 +08001228 wl_signal_init(&pointer->destroy_signal);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +02001229 wl_list_init(&pointer->timestamps_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001230
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001231 pointer->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
1232
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001233 /* FIXME: Pick better co-ords. */
1234 pointer->x = wl_fixed_from_int(100);
1235 pointer->y = wl_fixed_from_int(100);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001236
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001237 pointer->output_destroy_listener.notify =
1238 weston_pointer_handle_output_destroy;
1239 wl_signal_add(&seat->compositor->output_destroyed_signal,
1240 &pointer->output_destroy_listener);
1241
Derek Foremanf9318d12015-05-11 15:40:11 -05001242 pointer->sx = wl_fixed_from_int(-1000000);
1243 pointer->sy = wl_fixed_from_int(-1000000);
1244
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001245 return pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001246}
1247
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001248static void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001249weston_pointer_destroy(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001250{
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +02001251 struct weston_pointer_client *pointer_client, *tmp;
1252
Jonas Ådahl3eb4ddd2016-07-22 17:52:58 +08001253 wl_signal_emit(&pointer->destroy_signal, pointer);
1254
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001255 if (pointer->sprite)
1256 pointer_unmap_sprite(pointer);
1257
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +02001258 wl_list_for_each_safe(pointer_client, tmp, &pointer->pointer_clients,
1259 link) {
1260 wl_list_remove(&pointer_client->link);
1261 weston_pointer_client_destroy(pointer_client);
1262 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001263
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001264 wl_list_remove(&pointer->focus_resource_listener.link);
1265 wl_list_remove(&pointer->focus_view_listener.link);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001266 wl_list_remove(&pointer->output_destroy_listener.link);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +02001267 wl_list_remove(&pointer->timestamps_list);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001268 free(pointer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001269}
1270
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001271void
1272weston_pointer_set_default_grab(struct weston_pointer *pointer,
1273 const struct weston_pointer_grab_interface *interface)
1274{
1275 if (interface)
1276 pointer->default_grab.interface = interface;
1277 else
1278 pointer->default_grab.interface =
1279 &default_pointer_grab_interface;
1280}
1281
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001282static struct weston_keyboard *
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001283weston_keyboard_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001284{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001285 struct weston_keyboard *keyboard;
1286
Peter Huttererf3d62272013-08-08 11:57:05 +10001287 keyboard = zalloc(sizeof *keyboard);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001288 if (keyboard == NULL)
Neil Roberts96d790e2013-09-19 17:32:00 +01001289 return NULL;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001290
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001291 wl_list_init(&keyboard->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +01001292 wl_list_init(&keyboard->focus_resource_list);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001293 wl_list_init(&keyboard->focus_resource_listener.link);
1294 keyboard->focus_resource_listener.notify = keyboard_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001295 wl_array_init(&keyboard->keys);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001296 keyboard->default_grab.interface = &default_keyboard_grab_interface;
1297 keyboard->default_grab.keyboard = keyboard;
1298 keyboard->grab = &keyboard->default_grab;
1299 wl_signal_init(&keyboard->focus_signal);
Alexandros Frantzis2b442482018-02-20 14:05:50 +02001300 wl_list_init(&keyboard->timestamps_list);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001301
1302 return keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001303}
1304
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001305static void
1306weston_xkb_info_destroy(struct weston_xkb_info *xkb_info);
1307
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001308static void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001309weston_keyboard_destroy(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001310{
Alexandros Frantzis7a314d62018-01-26 18:47:56 +02001311 struct wl_resource *resource;
1312
1313 wl_resource_for_each(resource, &keyboard->resource_list) {
1314 wl_resource_set_user_data(resource, NULL);
1315 }
1316
1317 wl_resource_for_each(resource, &keyboard->focus_resource_list) {
1318 wl_resource_set_user_data(resource, NULL);
1319 }
1320
1321 wl_list_remove(&keyboard->resource_list);
1322 wl_list_remove(&keyboard->focus_resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +01001323
Derek Foreman185d1582017-06-28 11:17:23 -05001324 xkb_state_unref(keyboard->xkb_state.state);
1325 if (keyboard->xkb_info)
1326 weston_xkb_info_destroy(keyboard->xkb_info);
1327 xkb_keymap_unref(keyboard->pending_keymap);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001328
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001329 wl_array_release(&keyboard->keys);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001330 wl_list_remove(&keyboard->focus_resource_listener.link);
Alexandros Frantzis2b442482018-02-20 14:05:50 +02001331 wl_list_remove(&keyboard->timestamps_list);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001332 free(keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001333}
1334
Jonas Ådahlcbfa7f72013-12-02 22:05:04 +01001335static void
1336weston_touch_reset_state(struct weston_touch *touch)
1337{
1338 touch->num_tp = 0;
1339}
1340
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001341static struct weston_touch *
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001342weston_touch_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001343{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001344 struct weston_touch *touch;
1345
Peter Huttererf3d62272013-08-08 11:57:05 +10001346 touch = zalloc(sizeof *touch);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001347 if (touch == NULL)
1348 return NULL;
1349
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -05001350 wl_list_init(&touch->device_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001351 wl_list_init(&touch->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +01001352 wl_list_init(&touch->focus_resource_list);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001353 wl_list_init(&touch->focus_view_listener.link);
1354 touch->focus_view_listener.notify = touch_focus_view_destroyed;
1355 wl_list_init(&touch->focus_resource_listener.link);
1356 touch->focus_resource_listener.notify = touch_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001357 touch->default_grab.interface = &default_touch_grab_interface;
1358 touch->default_grab.touch = touch;
1359 touch->grab = &touch->default_grab;
1360 wl_signal_init(&touch->focus_signal);
Alexandros Frantzisd7157842018-02-20 14:07:03 +02001361 wl_list_init(&touch->timestamps_list);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001362
1363 return touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001364}
1365
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001366static void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001367weston_touch_destroy(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001368{
Alexandros Frantzisb0b598c2018-01-26 18:47:57 +02001369 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01001370
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -05001371 assert(wl_list_empty(&touch->device_list));
1372
Alexandros Frantzisb0b598c2018-01-26 18:47:57 +02001373 wl_resource_for_each(resource, &touch->resource_list) {
1374 wl_resource_set_user_data(resource, NULL);
1375 }
1376
1377 wl_resource_for_each(resource, &touch->focus_resource_list) {
1378 wl_resource_set_user_data(resource, NULL);
1379 }
1380
1381 wl_list_remove(&touch->resource_list);
1382 wl_list_remove(&touch->focus_resource_list);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001383 wl_list_remove(&touch->focus_view_listener.link);
1384 wl_list_remove(&touch->focus_resource_listener.link);
Alexandros Frantzisd7157842018-02-20 14:07:03 +02001385 wl_list_remove(&touch->timestamps_list);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001386 free(touch);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001387}
1388
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001389static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04001390seat_send_updated_caps(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001391{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001392 enum wl_seat_capability caps = 0;
Rob Bradford6e737f52013-09-06 17:48:19 +01001393 struct wl_resource *resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001394
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001395 if (seat->pointer_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001396 caps |= WL_SEAT_CAPABILITY_POINTER;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001397 if (seat->keyboard_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001398 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001399 if (seat->touch_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001400 caps |= WL_SEAT_CAPABILITY_TOUCH;
1401
Rob Bradford6e737f52013-09-06 17:48:19 +01001402 wl_resource_for_each(resource, &seat->base_resource_list) {
1403 wl_seat_send_capabilities(resource, caps);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001404 }
Jason Ekstranda4ab5422014-04-02 19:53:45 -05001405 wl_signal_emit(&seat->updated_caps_signal, seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001406}
1407
Derek Foremanf9318d12015-05-11 15:40:11 -05001408
1409/** Clear the pointer focus
1410 *
1411 * \param pointer the pointer to clear focus for.
1412 *
1413 * This can be used to unset pointer focus and set the co-ordinates to the
1414 * arbitrary values we use for the no focus case.
1415 *
1416 * There's no requirement to use this function. For example, passing the
1417 * results of a weston_compositor_pick_view() directly to
1418 * weston_pointer_set_focus() will do the right thing when no view is found.
1419 */
1420WL_EXPORT void
1421weston_pointer_clear_focus(struct weston_pointer *pointer)
1422{
1423 weston_pointer_set_focus(pointer, NULL,
1424 wl_fixed_from_int(-1000000),
1425 wl_fixed_from_int(-1000000));
1426}
1427
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001428WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001429weston_pointer_set_focus(struct weston_pointer *pointer,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001430 struct weston_view *view,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001431 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001432{
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001433 struct weston_pointer_client *pointer_client;
Derek Foreman1281a362015-07-31 16:55:32 -05001434 struct weston_keyboard *kbd = weston_seat_get_keyboard(pointer->seat);
Neil Roberts96d790e2013-09-19 17:32:00 +01001435 struct wl_resource *resource;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001436 struct wl_resource *surface_resource;
Rob Bradford880ebc72013-07-22 17:31:38 +01001437 struct wl_display *display = pointer->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001438 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01001439 struct wl_list *focus_resource_list;
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001440 int refocus = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001441
1442 if ((!pointer->focus && view) ||
1443 (pointer->focus && !view) ||
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001444 (pointer->focus && pointer->focus->surface != view->surface) ||
1445 pointer->sx != sx || pointer->sy != sy)
1446 refocus = 1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001447
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001448 if (pointer->focus_client && refocus) {
1449 focus_resource_list = &pointer->focus_client->pointer_resources;
1450 if (!wl_list_empty(focus_resource_list)) {
1451 serial = wl_display_next_serial(display);
1452 surface_resource = pointer->focus->surface->resource;
1453 wl_resource_for_each(resource, focus_resource_list) {
1454 wl_pointer_send_leave(resource, serial,
1455 surface_resource);
Peter Hutterer87743e92016-01-18 16:38:22 +10001456 pointer_send_frame(resource);
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001457 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001458 }
1459
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001460 pointer->focus_client = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001461 }
1462
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001463 pointer_client = find_pointer_client_for_view(pointer, view);
1464 if (pointer_client && refocus) {
1465 struct wl_client *surface_client = pointer_client->client;
Neil Roberts96d790e2013-09-19 17:32:00 +01001466
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001467 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001468
Jason Ekstranda7af7042013-10-12 22:38:11 -05001469 if (kbd && kbd->focus != view->surface)
Kristian Høgsbergcb406f12013-10-09 10:54:03 -07001470 send_modifiers_to_client_in_list(surface_client,
1471 &kbd->resource_list,
1472 serial,
1473 kbd);
1474
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001475 pointer->focus_client = pointer_client;
Neil Roberts96d790e2013-09-19 17:32:00 +01001476
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001477 focus_resource_list = &pointer->focus_client->pointer_resources;
Neil Roberts96d790e2013-09-19 17:32:00 +01001478 wl_resource_for_each(resource, focus_resource_list) {
1479 wl_pointer_send_enter(resource,
1480 serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001481 view->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01001482 sx, sy);
Peter Hutterer87743e92016-01-18 16:38:22 +10001483 pointer_send_frame(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001484 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001485
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001486 pointer->focus_serial = serial;
1487 }
1488
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001489 wl_list_remove(&pointer->focus_view_listener.link);
1490 wl_list_init(&pointer->focus_view_listener.link);
1491 wl_list_remove(&pointer->focus_resource_listener.link);
1492 wl_list_init(&pointer->focus_resource_listener.link);
Emilio Pozuelo Monfortaa7a4762013-11-19 11:37:15 +01001493 if (view)
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001494 wl_signal_add(&view->destroy_signal, &pointer->focus_view_listener);
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001495 if (view && view->surface->resource)
1496 wl_resource_add_destroy_listener(view->surface->resource,
1497 &pointer->focus_resource_listener);
1498
1499 pointer->focus = view;
1500 pointer->focus_view_listener.notify = pointer_focus_view_destroyed;
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001501 pointer->sx = sx;
1502 pointer->sy = sy;
1503
Derek Foremanf9318d12015-05-11 15:40:11 -05001504 assert(view || sx == wl_fixed_from_int(-1000000));
1505 assert(view || sy == wl_fixed_from_int(-1000000));
1506
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001507 wl_signal_emit(&pointer->focus_signal, pointer);
1508}
1509
Neil Roberts96d790e2013-09-19 17:32:00 +01001510static void
1511send_enter_to_resource_list(struct wl_list *list,
1512 struct weston_keyboard *keyboard,
1513 struct weston_surface *surface,
1514 uint32_t serial)
1515{
1516 struct wl_resource *resource;
1517
1518 wl_resource_for_each(resource, list) {
1519 send_modifiers_to_resource(keyboard, resource, serial);
1520 wl_keyboard_send_enter(resource, serial,
1521 surface->resource,
1522 &keyboard->keys);
1523 }
1524}
1525
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001526WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001527weston_keyboard_set_focus(struct weston_keyboard *keyboard,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001528 struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001529{
Quentin Glidic85d55542017-07-21 14:02:40 +02001530 struct weston_seat *seat = keyboard->seat;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001531 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +01001532 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001533 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01001534 struct wl_list *focus_resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001535
Pekka Paalanen72e183b2018-02-22 16:55:15 +02001536 /* Keyboard focus on a surface without a client is equivalent to NULL
1537 * focus as nothing would react to the keyboard events anyway.
1538 * Just set focus to NULL instead - the destroy listener hangs on the
1539 * wl_resource anyway.
1540 */
1541 if (surface && !surface->resource)
1542 surface = NULL;
1543
Neil Roberts96d790e2013-09-19 17:32:00 +01001544 focus_resource_list = &keyboard->focus_resource_list;
1545
1546 if (!wl_list_empty(focus_resource_list) && keyboard->focus != surface) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001547 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001548 wl_resource_for_each(resource, focus_resource_list) {
1549 wl_keyboard_send_leave(resource, serial,
1550 keyboard->focus->resource);
1551 }
1552 move_resources(&keyboard->resource_list, focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001553 }
1554
Neil Roberts96d790e2013-09-19 17:32:00 +01001555 if (find_resource_for_surface(&keyboard->resource_list, surface) &&
1556 keyboard->focus != surface) {
1557 struct wl_client *surface_client =
1558 wl_resource_get_client(surface->resource);
1559
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001560 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001561
1562 move_resources_for_client(focus_resource_list,
1563 &keyboard->resource_list,
1564 surface_client);
1565 send_enter_to_resource_list(focus_resource_list,
1566 keyboard,
1567 surface,
1568 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001569 keyboard->focus_serial = serial;
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001570 }
1571
Quentin Glidic85d55542017-07-21 14:02:40 +02001572 if (seat->saved_kbd_focus) {
1573 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1574 seat->saved_kbd_focus = NULL;
1575 }
1576
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001577 wl_list_remove(&keyboard->focus_resource_listener.link);
1578 wl_list_init(&keyboard->focus_resource_listener.link);
Pekka Paalanen72e183b2018-02-22 16:55:15 +02001579 if (surface)
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001580 wl_resource_add_destroy_listener(surface->resource,
1581 &keyboard->focus_resource_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001582
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001583 keyboard->focus = surface;
1584 wl_signal_emit(&keyboard->focus_signal, keyboard);
1585}
1586
Giulio Camuffoa20ca812014-11-22 11:16:56 +02001587/* Users of this function must manually manage the keyboard focus */
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001588WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001589weston_keyboard_start_grab(struct weston_keyboard *keyboard,
1590 struct weston_keyboard_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001591{
1592 keyboard->grab = grab;
1593 grab->keyboard = keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001594}
1595
1596WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001597weston_keyboard_end_grab(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001598{
1599 keyboard->grab = &keyboard->default_grab;
1600}
1601
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001602static void
1603weston_keyboard_cancel_grab(struct weston_keyboard *keyboard)
1604{
1605 keyboard->grab->interface->cancel(keyboard->grab);
1606}
1607
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001608WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001609weston_pointer_start_grab(struct weston_pointer *pointer,
1610 struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001611{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001612 pointer->grab = grab;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001613 grab->pointer = pointer;
Kristian Høgsbergda751b82013-07-04 00:58:07 -04001614 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001615}
1616
1617WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001618weston_pointer_end_grab(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001619{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001620 pointer->grab = &pointer->default_grab;
Kristian Høgsbergda751b82013-07-04 00:58:07 -04001621 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001622}
1623
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001624static void
1625weston_pointer_cancel_grab(struct weston_pointer *pointer)
1626{
1627 pointer->grab->interface->cancel(pointer->grab);
1628}
1629
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001630WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -04001631weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001632{
1633 touch->grab = grab;
1634 grab->touch = touch;
1635}
1636
1637WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -04001638weston_touch_end_grab(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001639{
1640 touch->grab = &touch->default_grab;
1641}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001642
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001643static void
1644weston_touch_cancel_grab(struct weston_touch *touch)
1645{
1646 touch->grab->interface->cancel(touch->grab);
1647}
1648
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001649static void
1650weston_pointer_clamp_for_output(struct weston_pointer *pointer,
1651 struct weston_output *output,
1652 wl_fixed_t *fx, wl_fixed_t *fy)
1653{
1654 int x, y;
1655
1656 x = wl_fixed_to_int(*fx);
1657 y = wl_fixed_to_int(*fy);
1658
1659 if (x < output->x)
1660 *fx = wl_fixed_from_int(output->x);
1661 else if (x >= output->x + output->width)
1662 *fx = wl_fixed_from_int(output->x +
1663 output->width - 1);
1664 if (y < output->y)
1665 *fy = wl_fixed_from_int(output->y);
1666 else if (y >= output->y + output->height)
1667 *fy = wl_fixed_from_int(output->y +
1668 output->height - 1);
1669}
1670
Rob Bradford806d8c02013-06-25 18:56:41 +01001671WL_EXPORT void
1672weston_pointer_clamp(struct weston_pointer *pointer, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001673{
Rob Bradford806d8c02013-06-25 18:56:41 +01001674 struct weston_compositor *ec = pointer->seat->compositor;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001675 struct weston_output *output, *prev = NULL;
1676 int x, y, old_x, old_y, valid = 0;
1677
1678 x = wl_fixed_to_int(*fx);
1679 y = wl_fixed_to_int(*fy);
Rob Bradford806d8c02013-06-25 18:56:41 +01001680 old_x = wl_fixed_to_int(pointer->x);
1681 old_y = wl_fixed_to_int(pointer->y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001682
1683 wl_list_for_each(output, &ec->output_list, link) {
Rob Bradford66bd9f52013-06-25 18:56:42 +01001684 if (pointer->seat->output && pointer->seat->output != output)
1685 continue;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001686 if (pixman_region32_contains_point(&output->region,
1687 x, y, NULL))
1688 valid = 1;
1689 if (pixman_region32_contains_point(&output->region,
1690 old_x, old_y, NULL))
1691 prev = output;
1692 }
1693
Rob Bradford66bd9f52013-06-25 18:56:42 +01001694 if (!prev)
1695 prev = pointer->seat->output;
1696
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001697 if (prev && !valid)
1698 weston_pointer_clamp_for_output(pointer, prev, fx, fy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001699}
1700
Jonas Ådahld2510102014-10-05 21:39:14 +02001701static void
1702weston_pointer_move_to(struct weston_pointer *pointer,
1703 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001704{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001705 int32_t ix, iy;
1706
Rob Bradford806d8c02013-06-25 18:56:41 +01001707 weston_pointer_clamp (pointer, &x, &y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001708
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001709 pointer->x = x;
1710 pointer->y = y;
1711
1712 ix = wl_fixed_to_int(x);
1713 iy = wl_fixed_to_int(y);
1714
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001715 if (pointer->sprite) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001716 weston_view_set_position(pointer->sprite,
1717 ix - pointer->hotspot_x,
1718 iy - pointer->hotspot_y);
1719 weston_view_schedule_repaint(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001720 }
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001721
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001722 pointer->grab->interface->focus(pointer->grab);
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001723 wl_signal_emit(&pointer->motion_signal, pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001724}
1725
Jonas Ådahld2510102014-10-05 21:39:14 +02001726WL_EXPORT void
Jonas Ådahld2510102014-10-05 21:39:14 +02001727weston_pointer_move(struct weston_pointer *pointer,
1728 struct weston_pointer_motion_event *event)
1729{
1730 wl_fixed_t x, y;
1731
1732 weston_pointer_motion_to_abs(pointer, event, &x, &y);
1733 weston_pointer_move_to(pointer, x, y);
1734}
1735
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001736/** Verify if the pointer is in a valid position and move it if it isn't.
1737 */
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001738static void
1739weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001740{
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001741 struct weston_pointer *pointer;
1742 struct weston_compositor *ec;
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001743 struct weston_output *output, *closest = NULL;
1744 int x, y, distance, min = INT_MAX;
1745 wl_fixed_t fx, fy;
1746
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001747 pointer = container_of(listener, struct weston_pointer,
1748 output_destroy_listener);
1749 ec = pointer->seat->compositor;
1750
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001751 x = wl_fixed_to_int(pointer->x);
1752 y = wl_fixed_to_int(pointer->y);
1753
1754 wl_list_for_each(output, &ec->output_list, link) {
1755 if (pixman_region32_contains_point(&output->region,
1756 x, y, NULL))
1757 return;
1758
1759 /* Aproximante the distance from the pointer to the center of
1760 * the output. */
1761 distance = abs(output->x + output->width / 2 - x) +
1762 abs(output->y + output->height / 2 - y);
1763 if (distance < min) {
1764 min = distance;
1765 closest = output;
1766 }
1767 }
1768
1769 /* Nothing to do if there's no output left. */
1770 if (!closest)
1771 return;
1772
1773 fx = pointer->x;
1774 fy = pointer->y;
1775
1776 weston_pointer_clamp_for_output(pointer, closest, &fx, &fy);
Jonas Ådahld2510102014-10-05 21:39:14 +02001777 weston_pointer_move_to(pointer, fx, fy);
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001778}
1779
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001780WL_EXPORT void
1781notify_motion(struct weston_seat *seat,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001782 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001783 struct weston_pointer_motion_event *event)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001784{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001785 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001786 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001787
1788 weston_compositor_wake(ec);
Jonas Ådahld2510102014-10-05 21:39:14 +02001789 pointer->grab->interface->motion(pointer->grab, time, event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001790}
1791
Daniel Stone96d47c02013-11-19 11:37:12 +01001792static void
1793run_modifier_bindings(struct weston_seat *seat, uint32_t old, uint32_t new)
1794{
1795 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001796 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Daniel Stone96d47c02013-11-19 11:37:12 +01001797 uint32_t diff;
1798 unsigned int i;
1799 struct {
1800 uint32_t xkb;
1801 enum weston_keyboard_modifier weston;
1802 } mods[] = {
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001803 { keyboard->xkb_info->ctrl_mod, MODIFIER_CTRL },
1804 { keyboard->xkb_info->alt_mod, MODIFIER_ALT },
1805 { keyboard->xkb_info->super_mod, MODIFIER_SUPER },
1806 { keyboard->xkb_info->shift_mod, MODIFIER_SHIFT },
Daniel Stone96d47c02013-11-19 11:37:12 +01001807 };
1808
1809 diff = new & ~old;
1810 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
1811 if (diff & (1 << mods[i].xkb))
1812 weston_compositor_run_modifier_binding(compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001813 keyboard,
Daniel Stone96d47c02013-11-19 11:37:12 +01001814 mods[i].weston,
1815 WL_KEYBOARD_KEY_STATE_PRESSED);
1816 }
1817
1818 diff = old & ~new;
1819 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
1820 if (diff & (1 << mods[i].xkb))
1821 weston_compositor_run_modifier_binding(compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001822 keyboard,
Daniel Stone96d47c02013-11-19 11:37:12 +01001823 mods[i].weston,
1824 WL_KEYBOARD_KEY_STATE_RELEASED);
1825 }
1826}
1827
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001828WL_EXPORT void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001829notify_motion_absolute(struct weston_seat *seat, const struct timespec *time,
1830 double x, double y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001831{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001832 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001833 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Jonas Ådahld2510102014-10-05 21:39:14 +02001834 struct weston_pointer_motion_event event = { 0 };
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001835
1836 weston_compositor_wake(ec);
Jonas Ådahld2510102014-10-05 21:39:14 +02001837
1838 event = (struct weston_pointer_motion_event) {
1839 .mask = WESTON_POINTER_MOTION_ABS,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001840 .x = x,
1841 .y = y,
Jonas Ådahld2510102014-10-05 21:39:14 +02001842 };
1843
1844 pointer->grab->interface->motion(pointer->grab, time, &event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001845}
1846
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02001847static unsigned int
1848peek_next_activate_serial(struct weston_compositor *c)
1849{
1850 unsigned serial = c->activate_serial + 1;
1851
1852 return serial == 0 ? 1 : serial;
1853}
1854
1855static void
1856inc_activate_serial(struct weston_compositor *c)
1857{
1858 c->activate_serial = peek_next_activate_serial (c);
1859}
1860
1861WL_EXPORT void
1862weston_view_activate(struct weston_view *view,
1863 struct weston_seat *seat,
1864 uint32_t flags)
1865{
1866 struct weston_compositor *compositor = seat->compositor;
1867
1868 if (flags & WESTON_ACTIVATE_FLAG_CLICKED) {
1869 view->click_to_activate_serial =
1870 peek_next_activate_serial(compositor);
1871 }
1872
1873 weston_seat_set_keyboard_focus(seat, view->surface);
1874}
1875
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001876WL_EXPORT void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001877notify_button(struct weston_seat *seat, const struct timespec *time,
1878 int32_t button, enum wl_pointer_button_state state)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001879{
1880 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001881 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001882
1883 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001884 weston_compositor_idle_inhibit(compositor);
1885 if (pointer->button_count == 0) {
1886 pointer->grab_button = button;
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001887 pointer->grab_time = *time;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001888 pointer->grab_x = pointer->x;
1889 pointer->grab_y = pointer->y;
1890 }
1891 pointer->button_count++;
1892 } else {
1893 weston_compositor_idle_release(compositor);
1894 pointer->button_count--;
1895 }
1896
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001897 weston_compositor_run_button_binding(compositor, pointer, time, button,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001898 state);
1899
1900 pointer->grab->interface->button(pointer->grab, time, button, state);
1901
1902 if (pointer->button_count == 1)
1903 pointer->grab_serial =
1904 wl_display_get_serial(compositor->wl_display);
1905}
1906
1907WL_EXPORT void
Alexandros Frantzis80321942017-11-16 18:20:56 +02001908notify_axis(struct weston_seat *seat, const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001909 struct weston_pointer_axis_event *event)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001910{
1911 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001912 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001913
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001914 weston_compositor_wake(compositor);
1915
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001916 if (weston_compositor_run_axis_binding(compositor, pointer,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001917 time, event))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001918 return;
1919
Peter Hutterer89b6a492016-01-18 15:58:17 +10001920 pointer->grab->interface->axis(pointer->grab, time, event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001921}
1922
Peter Hutterer87743e92016-01-18 16:38:22 +10001923WL_EXPORT void
1924notify_axis_source(struct weston_seat *seat, uint32_t source)
1925{
1926 struct weston_compositor *compositor = seat->compositor;
1927 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1928
1929 weston_compositor_wake(compositor);
1930
1931 pointer->grab->interface->axis_source(pointer->grab, source);
1932}
1933
1934WL_EXPORT void
1935notify_pointer_frame(struct weston_seat *seat)
1936{
1937 struct weston_compositor *compositor = seat->compositor;
1938 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1939
1940 weston_compositor_wake(compositor);
1941
1942 pointer->grab->interface->frame(pointer->grab);
1943}
1944
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001945WL_EXPORT int
1946weston_keyboard_set_locks(struct weston_keyboard *keyboard,
1947 uint32_t mask, uint32_t value)
1948{
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001949 uint32_t serial;
1950 xkb_mod_mask_t mods_depressed, mods_latched, mods_locked, group;
1951 xkb_mod_mask_t num, caps;
1952
1953 /* We don't want the leds to go out of sync with the actual state
1954 * so if the backend has no way to change the leds don't try to
1955 * change the state */
1956 if (!keyboard->seat->led_update)
1957 return -1;
1958
1959 mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
1960 XKB_STATE_DEPRESSED);
1961 mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
1962 XKB_STATE_LATCHED);
1963 mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
1964 XKB_STATE_LOCKED);
1965 group = xkb_state_serialize_group(keyboard->xkb_state.state,
1966 XKB_STATE_EFFECTIVE);
1967
1968 num = (1 << keyboard->xkb_info->mod2_mod);
1969 caps = (1 << keyboard->xkb_info->caps_mod);
1970 if (mask & WESTON_NUM_LOCK) {
1971 if (value & WESTON_NUM_LOCK)
1972 mods_locked |= num;
1973 else
1974 mods_locked &= ~num;
1975 }
1976 if (mask & WESTON_CAPS_LOCK) {
1977 if (value & WESTON_CAPS_LOCK)
1978 mods_locked |= caps;
1979 else
1980 mods_locked &= ~caps;
1981 }
1982
1983 xkb_state_update_mask(keyboard->xkb_state.state, mods_depressed,
1984 mods_latched, mods_locked, 0, 0, group);
1985
1986 serial = wl_display_next_serial(
1987 keyboard->seat->compositor->wl_display);
1988 notify_modifiers(keyboard->seat, serial);
1989
1990 return 0;
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001991}
1992
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001993WL_EXPORT void
1994notify_modifiers(struct weston_seat *seat, uint32_t serial)
1995{
Derek Foreman1281a362015-07-31 16:55:32 -05001996 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001997 struct weston_keyboard_grab *grab = keyboard->grab;
1998 uint32_t mods_depressed, mods_latched, mods_locked, group;
1999 uint32_t mods_lookup;
2000 enum weston_led leds = 0;
2001 int changed = 0;
2002
2003 /* Serialize and update our internal state, checking to see if it's
2004 * different to the previous state. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002005 mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03002006 XKB_STATE_MODS_DEPRESSED);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002007 mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03002008 XKB_STATE_MODS_LATCHED);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002009 mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03002010 XKB_STATE_MODS_LOCKED);
2011 group = xkb_state_serialize_layout(keyboard->xkb_state.state,
2012 XKB_STATE_LAYOUT_EFFECTIVE);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002013
Derek Foreman244e99e2015-06-03 15:53:26 -05002014 if (mods_depressed != keyboard->modifiers.mods_depressed ||
2015 mods_latched != keyboard->modifiers.mods_latched ||
2016 mods_locked != keyboard->modifiers.mods_locked ||
2017 group != keyboard->modifiers.group)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002018 changed = 1;
2019
Derek Foreman244e99e2015-06-03 15:53:26 -05002020 run_modifier_bindings(seat, keyboard->modifiers.mods_depressed,
Daniel Stone96d47c02013-11-19 11:37:12 +01002021 mods_depressed);
2022
Derek Foreman244e99e2015-06-03 15:53:26 -05002023 keyboard->modifiers.mods_depressed = mods_depressed;
2024 keyboard->modifiers.mods_latched = mods_latched;
2025 keyboard->modifiers.mods_locked = mods_locked;
2026 keyboard->modifiers.group = group;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002027
2028 /* And update the modifier_state for bindings. */
2029 mods_lookup = mods_depressed | mods_latched;
2030 seat->modifier_state = 0;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002031 if (mods_lookup & (1 << keyboard->xkb_info->ctrl_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002032 seat->modifier_state |= MODIFIER_CTRL;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002033 if (mods_lookup & (1 << keyboard->xkb_info->alt_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002034 seat->modifier_state |= MODIFIER_ALT;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002035 if (mods_lookup & (1 << keyboard->xkb_info->super_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002036 seat->modifier_state |= MODIFIER_SUPER;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002037 if (mods_lookup & (1 << keyboard->xkb_info->shift_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002038 seat->modifier_state |= MODIFIER_SHIFT;
2039
2040 /* Finally, notify the compositor that LEDs have changed. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002041 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
2042 keyboard->xkb_info->num_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002043 leds |= LED_NUM_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002044 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
2045 keyboard->xkb_info->caps_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002046 leds |= LED_CAPS_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002047 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
2048 keyboard->xkb_info->scroll_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002049 leds |= LED_SCROLL_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002050 if (leds != keyboard->xkb_state.leds && seat->led_update)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002051 seat->led_update(seat, leds);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002052 keyboard->xkb_state.leds = leds;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002053
2054 if (changed) {
2055 grab->interface->modifiers(grab,
2056 serial,
2057 keyboard->modifiers.mods_depressed,
2058 keyboard->modifiers.mods_latched,
2059 keyboard->modifiers.mods_locked,
2060 keyboard->modifiers.group);
2061 }
2062}
2063
2064static void
2065update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
2066 enum wl_keyboard_key_state state)
2067{
Derek Foreman1281a362015-07-31 16:55:32 -05002068 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002069 enum xkb_key_direction direction;
2070
2071 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2072 direction = XKB_KEY_DOWN;
2073 else
2074 direction = XKB_KEY_UP;
2075
2076 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
2077 * broken keycode system, which starts at 8. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002078 xkb_state_update_key(keyboard->xkb_state.state, key + 8, direction);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002079
2080 notify_modifiers(seat, serial);
2081}
Rui Matos65196bc2013-10-10 19:44:19 +02002082
Derek Foremanf8f7fd62017-06-28 11:41:43 -05002083WL_EXPORT void
2084weston_keyboard_send_keymap(struct weston_keyboard *kbd, struct wl_resource *resource)
Rui Matos65196bc2013-10-10 19:44:19 +02002085{
Derek Foremanf8f7fd62017-06-28 11:41:43 -05002086 struct weston_xkb_info *xkb_info = kbd->xkb_info;
Derek Foreman76829fc2017-06-28 12:17:46 -05002087 void *area;
2088 int fd;
Derek Foremanf8f7fd62017-06-28 11:41:43 -05002089
Derek Foreman76829fc2017-06-28 12:17:46 -05002090 fd = os_create_anonymous_file(xkb_info->keymap_size);
2091 if (fd < 0) {
Antonio Borneo39578632019-04-26 23:57:31 +02002092 weston_log("creating a keymap file for %lu bytes failed: %s\n",
2093 (unsigned long) xkb_info->keymap_size,
2094 strerror(errno));
Derek Foreman76829fc2017-06-28 12:17:46 -05002095 return;
2096 }
2097
2098 area = mmap(NULL, xkb_info->keymap_size, PROT_READ | PROT_WRITE,
2099 MAP_SHARED, fd, 0);
2100 if (area == MAP_FAILED) {
2101 weston_log("failed to mmap() %lu bytes\n",
2102 (unsigned long) xkb_info->keymap_size);
2103 goto err_mmap;
2104 }
2105 strcpy(area, xkb_info->keymap_string);
2106 munmap(area, xkb_info->keymap_size);
Rui Matos65196bc2013-10-10 19:44:19 +02002107 wl_keyboard_send_keymap(resource,
2108 WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
Derek Foreman76829fc2017-06-28 12:17:46 -05002109 fd,
Rui Matos65196bc2013-10-10 19:44:19 +02002110 xkb_info->keymap_size);
Derek Foreman76829fc2017-06-28 12:17:46 -05002111err_mmap:
2112 close(fd);
Rui Matos65196bc2013-10-10 19:44:19 +02002113}
2114
2115static void
2116send_modifiers(struct wl_resource *resource, uint32_t serial, struct weston_keyboard *keyboard)
2117{
2118 wl_keyboard_send_modifiers(resource, serial,
2119 keyboard->modifiers.mods_depressed,
2120 keyboard->modifiers.mods_latched,
2121 keyboard->modifiers.mods_locked,
2122 keyboard->modifiers.group);
2123}
2124
2125static struct weston_xkb_info *
2126weston_xkb_info_create(struct xkb_keymap *keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02002127
2128static void
2129update_keymap(struct weston_seat *seat)
2130{
Derek Foreman1281a362015-07-31 16:55:32 -05002131 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Rui Matos65196bc2013-10-10 19:44:19 +02002132 struct wl_resource *resource;
2133 struct weston_xkb_info *xkb_info;
2134 struct xkb_state *state;
2135 xkb_mod_mask_t latched_mods;
2136 xkb_mod_mask_t locked_mods;
2137
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002138 xkb_info = weston_xkb_info_create(keyboard->pending_keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02002139
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002140 xkb_keymap_unref(keyboard->pending_keymap);
2141 keyboard->pending_keymap = NULL;
Rui Matos65196bc2013-10-10 19:44:19 +02002142
2143 if (!xkb_info) {
2144 weston_log("failed to create XKB info\n");
2145 return;
2146 }
2147
2148 state = xkb_state_new(xkb_info->keymap);
2149 if (!state) {
2150 weston_log("failed to initialise XKB state\n");
2151 weston_xkb_info_destroy(xkb_info);
2152 return;
2153 }
2154
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002155 latched_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
2156 XKB_STATE_MODS_LATCHED);
2157 locked_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
2158 XKB_STATE_MODS_LOCKED);
Rui Matos65196bc2013-10-10 19:44:19 +02002159 xkb_state_update_mask(state,
2160 0, /* depressed */
2161 latched_mods,
2162 locked_mods,
2163 0, 0, 0);
2164
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002165 weston_xkb_info_destroy(keyboard->xkb_info);
2166 keyboard->xkb_info = xkb_info;
Rui Matos65196bc2013-10-10 19:44:19 +02002167
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002168 xkb_state_unref(keyboard->xkb_state.state);
2169 keyboard->xkb_state.state = state;
Rui Matos65196bc2013-10-10 19:44:19 +02002170
Derek Foremanbc91e542015-06-03 15:53:27 -05002171 wl_resource_for_each(resource, &keyboard->resource_list)
Derek Foremanf8f7fd62017-06-28 11:41:43 -05002172 weston_keyboard_send_keymap(keyboard, resource);
Derek Foremanbc91e542015-06-03 15:53:27 -05002173 wl_resource_for_each(resource, &keyboard->focus_resource_list)
Derek Foremanf8f7fd62017-06-28 11:41:43 -05002174 weston_keyboard_send_keymap(keyboard, resource);
Rui Matos65196bc2013-10-10 19:44:19 +02002175
2176 notify_modifiers(seat, wl_display_next_serial(seat->compositor->wl_display));
2177
2178 if (!latched_mods && !locked_mods)
2179 return;
2180
Derek Foremanbc91e542015-06-03 15:53:27 -05002181 wl_resource_for_each(resource, &keyboard->resource_list)
2182 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), keyboard);
2183 wl_resource_for_each(resource, &keyboard->focus_resource_list)
2184 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), keyboard);
Rui Matos65196bc2013-10-10 19:44:19 +02002185}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002186
2187WL_EXPORT void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02002188notify_key(struct weston_seat *seat, const struct timespec *time, uint32_t key,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002189 enum wl_keyboard_key_state state,
2190 enum weston_key_state_update update_state)
2191{
2192 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002193 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002194 struct weston_keyboard_grab *grab = keyboard->grab;
Pekka Paalanen86b53962014-11-19 13:43:32 +02002195 uint32_t *k, *end;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002196
2197 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002198 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002199 } else {
2200 weston_compositor_idle_release(compositor);
2201 }
2202
Pekka Paalanen86b53962014-11-19 13:43:32 +02002203 end = keyboard->keys.data + keyboard->keys.size;
2204 for (k = keyboard->keys.data; k < end; k++) {
2205 if (*k == key) {
2206 /* Ignore server-generated repeats. */
2207 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2208 return;
2209 *k = *--end;
2210 }
2211 }
2212 keyboard->keys.size = (void *) end - keyboard->keys.data;
2213 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2214 k = wl_array_add(&keyboard->keys, sizeof *k);
2215 *k = key;
2216 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002217
2218 if (grab == &keyboard->default_grab ||
2219 grab == &keyboard->input_method_grab) {
Derek Foreman99a6a2d2015-07-15 13:00:43 -05002220 weston_compositor_run_key_binding(compositor, keyboard, time,
2221 key, state);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002222 grab = keyboard->grab;
2223 }
2224
2225 grab->interface->key(grab, time, key, state);
2226
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002227 if (keyboard->pending_keymap &&
Pekka Paalanen86b53962014-11-19 13:43:32 +02002228 keyboard->keys.size == 0)
Rui Matos65196bc2013-10-10 19:44:19 +02002229 update_keymap(seat);
2230
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002231 if (update_state == STATE_UPDATE_AUTOMATIC) {
2232 update_modifier_state(seat,
2233 wl_display_get_serial(compositor->wl_display),
2234 key,
2235 state);
2236 }
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002237
Olivier Fourdandf84dbe2016-06-30 16:01:56 +02002238 keyboard->grab_serial = wl_display_get_serial(compositor->wl_display);
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002239 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02002240 keyboard->grab_time = *time;
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002241 keyboard->grab_key = key;
2242 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002243}
2244
2245WL_EXPORT void
2246notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002247 double x, double y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002248{
Derek Foreman1281a362015-07-31 16:55:32 -05002249 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2250
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002251 if (output) {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002252 weston_pointer_move_to(pointer,
2253 wl_fixed_from_double(x),
2254 wl_fixed_from_double(y));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002255 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002256 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002257 * NULL) here, but somehow that breaks re-entry... */
2258 }
2259}
2260
2261static void
2262destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
2263{
2264 struct weston_seat *ws;
2265
2266 ws = container_of(listener, struct weston_seat,
2267 saved_kbd_focus_listener);
2268
2269 ws->saved_kbd_focus = NULL;
2270}
2271
2272WL_EXPORT void
2273notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
2274 enum weston_key_state_update update_state)
2275{
2276 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002277 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04002278 struct weston_surface *surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002279 uint32_t *k, serial;
2280
2281 serial = wl_display_next_serial(compositor->wl_display);
2282 wl_array_copy(&keyboard->keys, keys);
2283 wl_array_for_each(k, &keyboard->keys) {
2284 weston_compositor_idle_inhibit(compositor);
2285 if (update_state == STATE_UPDATE_AUTOMATIC)
2286 update_modifier_state(seat, serial, *k,
2287 WL_KEYBOARD_KEY_STATE_PRESSED);
2288 }
2289
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002290 surface = seat->saved_kbd_focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002291 if (surface) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002292 weston_keyboard_set_focus(keyboard, surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002293 }
2294}
2295
2296WL_EXPORT void
2297notify_keyboard_focus_out(struct weston_seat *seat)
2298{
2299 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002300 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2301 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Quentin Glidic85d55542017-07-21 14:02:40 +02002302 struct weston_surface *focus = keyboard->focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002303 uint32_t *k, serial;
2304
2305 serial = wl_display_next_serial(compositor->wl_display);
2306 wl_array_for_each(k, &keyboard->keys) {
2307 weston_compositor_idle_release(compositor);
2308 update_modifier_state(seat, serial, *k,
2309 WL_KEYBOARD_KEY_STATE_RELEASED);
2310 }
2311
2312 seat->modifier_state = 0;
2313
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002314 weston_keyboard_set_focus(keyboard, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002315 weston_keyboard_cancel_grab(keyboard);
Derek Foreman1281a362015-07-31 16:55:32 -05002316 if (pointer)
2317 weston_pointer_cancel_grab(pointer);
Quentin Glidic85d55542017-07-21 14:02:40 +02002318
2319 if (focus) {
2320 seat->saved_kbd_focus = focus;
2321 seat->saved_kbd_focus_listener.notify =
2322 destroy_device_saved_kbd_focus;
2323 wl_signal_add(&focus->destroy_signal,
2324 &seat->saved_kbd_focus_listener);
2325 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002326}
2327
Michael Fua2bb7912013-07-23 15:51:06 +08002328WL_EXPORT void
Derek Foreman4c93c082015-04-30 16:45:41 -05002329weston_touch_set_focus(struct weston_touch *touch, struct weston_view *view)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002330{
Neil Roberts96d790e2013-09-19 17:32:00 +01002331 struct wl_list *focus_resource_list;
2332
Derek Foreman4c93c082015-04-30 16:45:41 -05002333 focus_resource_list = &touch->focus_resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002334
Derek Foreman4c93c082015-04-30 16:45:41 -05002335 if (view && touch->focus &&
2336 touch->focus->surface == view->surface) {
2337 touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002338 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002339 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002340
Derek Foreman4c93c082015-04-30 16:45:41 -05002341 wl_list_remove(&touch->focus_resource_listener.link);
2342 wl_list_init(&touch->focus_resource_listener.link);
2343 wl_list_remove(&touch->focus_view_listener.link);
2344 wl_list_init(&touch->focus_view_listener.link);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01002345
Neil Roberts96d790e2013-09-19 17:32:00 +01002346 if (!wl_list_empty(focus_resource_list)) {
Derek Foreman4c93c082015-04-30 16:45:41 -05002347 move_resources(&touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002348 focus_resource_list);
2349 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002350
Jason Ekstranda7af7042013-10-12 22:38:11 -05002351 if (view) {
Derek Foreman362656b2014-09-04 10:23:05 -05002352 struct wl_client *surface_client;
2353
2354 if (!view->surface->resource) {
Derek Foreman4c93c082015-04-30 16:45:41 -05002355 touch->focus = NULL;
Derek Foreman362656b2014-09-04 10:23:05 -05002356 return;
2357 }
2358
2359 surface_client = wl_resource_get_client(view->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +01002360 move_resources_for_client(focus_resource_list,
Derek Foreman4c93c082015-04-30 16:45:41 -05002361 &touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002362 surface_client);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01002363 wl_resource_add_destroy_listener(view->surface->resource,
Derek Foreman4c93c082015-04-30 16:45:41 -05002364 &touch->focus_resource_listener);
2365 wl_signal_add(&view->destroy_signal, &touch->focus_view_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002366 }
Derek Foreman4c93c082015-04-30 16:45:41 -05002367 touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002368}
2369
Pekka Paalanenf4062532018-02-26 16:18:29 +02002370static void
2371process_touch_normal(struct weston_touch_device *device,
2372 const struct timespec *time, int touch_id,
2373 double double_x, double double_y, int touch_type)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002374{
Pekka Paalanenbcbce332018-02-26 14:43:00 +02002375 struct weston_touch *touch = device->aggregate;
2376 struct weston_touch_grab *grab = device->aggregate->grab;
2377 struct weston_compositor *ec = device->aggregate->seat->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002378 struct weston_view *ev;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002379 wl_fixed_t sx, sy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002380 wl_fixed_t x = wl_fixed_from_double(double_x);
2381 wl_fixed_t y = wl_fixed_from_double(double_y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002382
2383 /* Update grab's global coordinates. */
Neil Roberts306fe082013-10-03 16:43:06 +01002384 if (touch_id == touch->grab_touch_id && touch_type != WL_TOUCH_UP) {
2385 touch->grab_x = x;
2386 touch->grab_y = y;
2387 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002388
2389 switch (touch_type) {
2390 case WL_TOUCH_DOWN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002391 /* the first finger down picks the view, and all further go
2392 * to that view for the remainder of the touch session i.e.
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002393 * until all touch points are up again. */
Jonas Ådahl9484b692013-12-02 22:05:03 +01002394 if (touch->num_tp == 1) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002395 ev = weston_compositor_pick_view(ec, x, y, &sx, &sy);
Derek Foreman4c93c082015-04-30 16:45:41 -05002396 weston_touch_set_focus(touch, ev);
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002397 } else if (!touch->focus) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002398 /* Unexpected condition: We have non-initial touch but
2399 * there is no focused surface.
2400 */
Chris Michael3f607d32015-10-07 11:59:49 -04002401 weston_log("touch event received with %d points down "
Jonas Ådahl9484b692013-12-02 22:05:03 +01002402 "but no surface focused\n", touch->num_tp);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002403 return;
2404 }
2405
Derek Foreman99a6a2d2015-07-15 13:00:43 -05002406 weston_compositor_run_touch_binding(ec, touch,
Kristian Høgsbergc8964012014-02-05 14:25:18 -08002407 time, touch_type);
2408
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002409 grab->interface->down(grab, time, touch_id, x, y);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002410 if (touch->num_tp == 1) {
Rusty Lynchf1407ff2013-08-08 21:13:57 -07002411 touch->grab_serial =
2412 wl_display_get_serial(ec->wl_display);
Neil Roberts306fe082013-10-03 16:43:06 +01002413 touch->grab_touch_id = touch_id;
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02002414 touch->grab_time = *time;
Rusty Lynchf1407ff2013-08-08 21:13:57 -07002415 touch->grab_x = x;
2416 touch->grab_y = y;
2417 }
2418
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002419 break;
2420 case WL_TOUCH_MOTION:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002421 ev = touch->focus;
2422 if (!ev)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002423 break;
2424
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +02002425 grab->interface->motion(grab, time, touch_id, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002426 break;
2427 case WL_TOUCH_UP:
Alexandros Frantzis27a51b82017-11-16 18:20:59 +02002428 grab->interface->up(grab, time, touch_id);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002429 if (touch->num_tp == 0)
Derek Foreman4c93c082015-04-30 16:45:41 -05002430 weston_touch_set_focus(touch, NULL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002431 break;
2432 }
2433}
2434
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002435static enum weston_touch_mode
2436get_next_touch_mode(enum weston_touch_mode from)
2437{
2438 switch (from) {
2439 case WESTON_TOUCH_MODE_PREP_NORMAL:
2440 return WESTON_TOUCH_MODE_NORMAL;
2441
2442 case WESTON_TOUCH_MODE_PREP_CALIB:
2443 return WESTON_TOUCH_MODE_CALIB;
2444
2445 case WESTON_TOUCH_MODE_NORMAL:
2446 case WESTON_TOUCH_MODE_CALIB:
2447 return from;
2448 }
2449
2450 return WESTON_TOUCH_MODE_NORMAL;
2451}
2452
2453/** Global touch mode update
2454 *
2455 * If no seat has a touch down and the compositor is in a PREP touch mode,
2456 * set the compositor to the goal touch mode.
2457 *
2458 * Calls calibrator if touch mode changed.
2459 */
2460static void
2461weston_compositor_update_touch_mode(struct weston_compositor *compositor)
2462{
2463 struct weston_seat *seat;
2464 struct weston_touch *touch;
2465 enum weston_touch_mode goal;
2466
2467 wl_list_for_each(seat, &compositor->seat_list, link) {
2468 touch = weston_seat_get_touch(seat);
2469 if (!touch)
2470 continue;
2471
2472 if (touch->num_tp > 0)
2473 return;
2474 }
2475
2476 goal = get_next_touch_mode(compositor->touch_mode);
2477 if (compositor->touch_mode != goal) {
2478 compositor->touch_mode = goal;
2479 touch_calibrator_mode_changed(compositor);
2480 }
2481}
2482
2483/** Start transition to normal touch event handling
2484 *
2485 * The touch event mode changes when all touches on all touch devices have
2486 * been lifted. If no touches are currently down, the transition is immediate.
2487 *
2488 * \sa weston_touch_mode
2489 */
2490void
2491weston_compositor_set_touch_mode_normal(struct weston_compositor *compositor)
2492{
2493 switch (compositor->touch_mode) {
2494 case WESTON_TOUCH_MODE_PREP_NORMAL:
2495 case WESTON_TOUCH_MODE_NORMAL:
2496 return;
2497 case WESTON_TOUCH_MODE_PREP_CALIB:
2498 compositor->touch_mode = WESTON_TOUCH_MODE_NORMAL;
2499 touch_calibrator_mode_changed(compositor);
2500 return;
2501 case WESTON_TOUCH_MODE_CALIB:
2502 compositor->touch_mode = WESTON_TOUCH_MODE_PREP_NORMAL;
2503 }
2504
2505 weston_compositor_update_touch_mode(compositor);
2506}
2507
2508/** Start transition to calibrator touch event handling
2509 *
2510 * The touch event mode changes when all touches on all touch devices have
2511 * been lifted. If no touches are currently down, the transition is immediate.
2512 *
2513 * \sa weston_touch_mode
2514 */
2515void
2516weston_compositor_set_touch_mode_calib(struct weston_compositor *compositor)
2517{
2518 switch (compositor->touch_mode) {
2519 case WESTON_TOUCH_MODE_PREP_CALIB:
2520 case WESTON_TOUCH_MODE_CALIB:
2521 assert(0);
2522 return;
2523 case WESTON_TOUCH_MODE_PREP_NORMAL:
2524 compositor->touch_mode = WESTON_TOUCH_MODE_CALIB;
2525 touch_calibrator_mode_changed(compositor);
2526 return;
2527 case WESTON_TOUCH_MODE_NORMAL:
2528 compositor->touch_mode = WESTON_TOUCH_MODE_PREP_CALIB;
2529 }
2530
2531 weston_compositor_update_touch_mode(compositor);
2532}
2533
Pekka Paalanenf4062532018-02-26 16:18:29 +02002534/** Feed in touch down, motion, and up events, calibratable device.
2535 *
2536 * It assumes always the correct cycle sequence until it gets here: touch_down
2537 * → touch_update → ... → touch_update → touch_end. The driver is responsible
2538 * for sending along such order.
2539 *
2540 * \param device The physical device that generated the event.
2541 * \param time The event timestamp.
2542 * \param touch_id ID for the touch point of this event (multi-touch).
Marius Vlada2dace22019-06-12 16:05:44 +03002543 * \param x X coordinate in compositor global space.
2544 * \param y Y coordinate in compositor global space.
Pekka Paalanenf4062532018-02-26 16:18:29 +02002545 * \param norm Normalized device X, Y coordinates in calibration space, or NULL.
2546 * \param touch_type Either WL_TOUCH_DOWN, WL_TOUCH_UP, or WL_TOUCH_MOTION.
2547 *
2548 * Coordinates double_x and double_y are used for normal operation.
2549 *
2550 * Coordinates norm are only used for touch device calibration. If and only if
2551 * the weston_touch_device does not support calibrating, norm must be NULL.
2552 *
2553 * The calibration space is the normalized coordinate space
2554 * [0.0, 1.0]×[0.0, 1.0] of the weston_touch_device. This is assumed to
2555 * map to the similar normalized coordinate space of the associated
2556 * weston_output.
2557 */
2558WL_EXPORT void
2559notify_touch_normalized(struct weston_touch_device *device,
2560 const struct timespec *time,
2561 int touch_id,
2562 double x, double y,
2563 const struct weston_point2d_device_normalized *norm,
2564 int touch_type)
2565{
Louis-Francis Ratté-Boulianne813a06e2017-11-28 20:42:47 -05002566 struct weston_seat *seat = device->aggregate->seat;
2567 struct weston_touch *touch = device->aggregate;
2568
Pekka Paalanenf4062532018-02-26 16:18:29 +02002569 if (touch_type != WL_TOUCH_UP) {
2570 if (weston_touch_device_can_calibrate(device))
2571 assert(norm != NULL);
2572 else
2573 assert(norm == NULL);
2574 }
2575
Louis-Francis Ratté-Boulianne813a06e2017-11-28 20:42:47 -05002576 /* Update touchpoints count regardless of the current mode. */
2577 switch (touch_type) {
2578 case WL_TOUCH_DOWN:
2579 weston_compositor_idle_inhibit(seat->compositor);
2580
2581 touch->num_tp++;
2582 break;
2583 case WL_TOUCH_UP:
2584 if (touch->num_tp == 0) {
2585 /* This can happen if we start out with one or
2586 * more fingers on the touch screen, in which
2587 * case we didn't get the corresponding down
2588 * event. */
Pekka Paalanen332a45e2018-03-02 14:20:59 +02002589 weston_log("Unmatched touch up event on seat %s, device %s\n",
2590 seat->seat_name, device->syspath);
2591 return;
Louis-Francis Ratté-Boulianne813a06e2017-11-28 20:42:47 -05002592 }
2593 weston_compositor_idle_release(seat->compositor);
2594
2595 touch->num_tp--;
2596 break;
2597 default:
2598 break;
2599 }
2600
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002601 /* Properly forward the touch event */
2602 switch (weston_touch_device_get_mode(device)) {
2603 case WESTON_TOUCH_MODE_NORMAL:
2604 case WESTON_TOUCH_MODE_PREP_CALIB:
2605 process_touch_normal(device, time, touch_id, x, y, touch_type);
2606 break;
2607 case WESTON_TOUCH_MODE_CALIB:
2608 case WESTON_TOUCH_MODE_PREP_NORMAL:
Louis-Francis Ratté-Boulianne83630982017-11-28 20:42:47 -05002609 notify_touch_calibrator(device, time, touch_id,
2610 norm, touch_type);
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002611 break;
2612 }
Pekka Paalanenf4062532018-02-26 16:18:29 +02002613}
2614
Jonas Ådahl1679f232014-04-12 09:39:51 +02002615WL_EXPORT void
Pekka Paalanenbcbce332018-02-26 14:43:00 +02002616notify_touch_frame(struct weston_touch_device *device)
Jonas Ådahl1679f232014-04-12 09:39:51 +02002617{
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002618 struct weston_touch_grab *grab;
Jonas Ådahl1679f232014-04-12 09:39:51 +02002619
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002620 switch (weston_touch_device_get_mode(device)) {
2621 case WESTON_TOUCH_MODE_NORMAL:
2622 case WESTON_TOUCH_MODE_PREP_CALIB:
2623 grab = device->aggregate->grab;
2624 grab->interface->frame(grab);
2625 break;
2626 case WESTON_TOUCH_MODE_CALIB:
2627 case WESTON_TOUCH_MODE_PREP_NORMAL:
Louis-Francis Ratté-Boulianne83630982017-11-28 20:42:47 -05002628 notify_touch_calibrator_frame(device);
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002629 break;
2630 }
2631
2632 weston_compositor_update_touch_mode(device->aggregate->seat->compositor);
Jonas Ådahl1679f232014-04-12 09:39:51 +02002633}
2634
Derek Foreman3cc004a2015-11-06 15:56:09 -06002635WL_EXPORT void
Pekka Paalanenbcbce332018-02-26 14:43:00 +02002636notify_touch_cancel(struct weston_touch_device *device)
Derek Foreman3cc004a2015-11-06 15:56:09 -06002637{
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002638 struct weston_touch_grab *grab;
Derek Foreman3cc004a2015-11-06 15:56:09 -06002639
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002640 switch (weston_touch_device_get_mode(device)) {
2641 case WESTON_TOUCH_MODE_NORMAL:
2642 case WESTON_TOUCH_MODE_PREP_CALIB:
2643 grab = device->aggregate->grab;
2644 grab->interface->cancel(grab);
2645 break;
2646 case WESTON_TOUCH_MODE_CALIB:
2647 case WESTON_TOUCH_MODE_PREP_NORMAL:
Louis-Francis Ratté-Boulianne83630982017-11-28 20:42:47 -05002648 notify_touch_calibrator_cancel(device);
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002649 break;
2650 }
2651
2652 weston_compositor_update_touch_mode(device->aggregate->seat->compositor);
Derek Foreman3cc004a2015-11-06 15:56:09 -06002653}
2654
Pekka Paalanen8274d902014-08-06 19:36:51 +03002655static int
2656pointer_cursor_surface_get_label(struct weston_surface *surface,
2657 char *buf, size_t len)
2658{
2659 return snprintf(buf, len, "cursor");
2660}
2661
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002662static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002663pointer_cursor_surface_committed(struct weston_surface *es,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002664 int32_t dx, int32_t dy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002665{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002666 struct weston_pointer *pointer = es->committed_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002667 int x, y;
2668
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002669 if (es->width == 0)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002670 return;
2671
Jason Ekstranda7af7042013-10-12 22:38:11 -05002672 assert(es == pointer->sprite->surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002673
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002674 pointer->hotspot_x -= dx;
2675 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002676
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002677 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
2678 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002679
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002680 weston_view_set_position(pointer->sprite, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002681
2682 empty_region(&es->pending.input);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02002683 empty_region(&es->input);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002684
2685 if (!weston_surface_is_mapped(es)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002686 weston_layer_entry_insert(&es->compositor->cursor_layer.view_list,
2687 &pointer->sprite->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002688 weston_view_update_transform(pointer->sprite);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002689 es->is_mapped = true;
2690 pointer->sprite->is_mapped = true;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002691 }
2692}
2693
2694static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002695pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2696 uint32_t serial, struct wl_resource *surface_resource,
2697 int32_t x, int32_t y)
2698{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002699 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002700 struct weston_surface *surface = NULL;
2701
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +02002702 if (!pointer)
2703 return;
2704
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002705 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002706 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002707
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002708 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002709 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002710 /* pointer->focus->surface->resource can be NULL. Surfaces like the
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02002711 black_surface used in shell.c for fullscreen don't have
2712 a resource, but can still have focus */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002713 if (pointer->focus->surface->resource == NULL)
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02002714 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002715 if (wl_resource_get_client(pointer->focus->surface->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002716 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002717 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002718 return;
2719
Derek Foreman4e53c532015-03-23 10:55:32 -05002720 if (!surface) {
2721 if (pointer->sprite)
2722 pointer_unmap_sprite(pointer);
2723 return;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002724 }
2725
Jonas Ådahlb4070242015-03-18 15:08:03 +08002726 if (pointer->sprite && pointer->sprite->surface == surface &&
2727 pointer->hotspot_x == x && pointer->hotspot_y == y)
2728 return;
2729
Derek Foreman4e53c532015-03-23 10:55:32 -05002730 if (!pointer->sprite || pointer->sprite->surface != surface) {
2731 if (weston_surface_set_role(surface, "wl_pointer-cursor",
2732 resource,
2733 WL_POINTER_ERROR_ROLE) < 0)
2734 return;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002735
Derek Foreman4e53c532015-03-23 10:55:32 -05002736 if (pointer->sprite)
2737 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002738
Derek Foreman4e53c532015-03-23 10:55:32 -05002739 wl_signal_add(&surface->destroy_signal,
2740 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002741
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002742 surface->committed = pointer_cursor_surface_committed;
2743 surface->committed_private = pointer;
Derek Foreman4e53c532015-03-23 10:55:32 -05002744 weston_surface_set_label_func(surface,
2745 pointer_cursor_surface_get_label);
2746 pointer->sprite = weston_view_create(surface);
2747 }
2748
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002749 pointer->hotspot_x = x;
2750 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002751
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +02002752 if (surface->buffer_ref.buffer) {
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002753 pointer_cursor_surface_committed(surface, 0, 0);
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +02002754 weston_view_schedule_repaint(pointer->sprite);
2755 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002756}
2757
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002758static void
2759pointer_release(struct wl_client *client, struct wl_resource *resource)
2760{
2761 wl_resource_destroy(resource);
2762}
2763
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002764static const struct wl_pointer_interface pointer_interface = {
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002765 pointer_set_cursor,
2766 pointer_release
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002767};
2768
2769static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002770seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2771 uint32_t id)
2772{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002773 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002774 /* We use the pointer_state directly, which means we'll
2775 * give a wl_pointer if the seat has ever had one - even though
2776 * the spec explicitly states that this request only takes effect
2777 * if the seat has the pointer capability.
2778 *
2779 * This prevents a race between the compositor sending new
2780 * capabilities and the client trying to use the old ones.
2781 */
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002782 struct weston_pointer *pointer = seat ? seat->pointer_state : NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002783 struct wl_resource *cr;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002784 struct weston_pointer_client *pointer_client;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002785
Jason Ekstranda85118c2013-06-27 20:17:02 -05002786 cr = wl_resource_create(client, &wl_pointer_interface,
2787 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002788 if (cr == NULL) {
2789 wl_client_post_no_memory(client);
2790 return;
2791 }
2792
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002793 wl_list_init(wl_resource_get_link(cr));
2794 wl_resource_set_implementation(cr, &pointer_interface, pointer,
2795 unbind_pointer_client_resource);
2796
2797 /* If we don't have a pointer_state, the resource is inert, so there
2798 * is nothing more to set up */
2799 if (!pointer)
2800 return;
2801
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002802 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
2803 if (!pointer_client) {
2804 wl_client_post_no_memory(client);
2805 return;
2806 }
2807
2808 wl_list_insert(&pointer_client->pointer_resources,
2809 wl_resource_get_link(cr));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002810
Derek Foreman1281a362015-07-31 16:55:32 -05002811 if (pointer->focus && pointer->focus->surface->resource &&
2812 wl_resource_get_client(pointer->focus->surface->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002813 wl_fixed_t sx, sy;
2814
Derek Foreman1281a362015-07-31 16:55:32 -05002815 weston_view_from_global_fixed(pointer->focus,
2816 pointer->x,
2817 pointer->y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002818 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002819
Neil Roberts96d790e2013-09-19 17:32:00 +01002820 wl_pointer_send_enter(cr,
Derek Foreman1281a362015-07-31 16:55:32 -05002821 pointer->focus_serial,
2822 pointer->focus->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01002823 sx, sy);
Peter Hutterer87743e92016-01-18 16:38:22 +10002824 pointer_send_frame(cr);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002825 }
2826}
2827
2828static void
Alexandros Frantzis2b442482018-02-20 14:05:50 +02002829destroy_keyboard_resource(struct wl_resource *resource)
2830{
2831 struct weston_keyboard *keyboard = wl_resource_get_user_data(resource);
2832
2833 wl_list_remove(wl_resource_get_link(resource));
2834
2835 if (keyboard) {
2836 remove_input_resource_from_timestamps(resource,
2837 &keyboard->timestamps_list);
2838 }
2839}
2840
2841static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002842keyboard_release(struct wl_client *client, struct wl_resource *resource)
2843{
2844 wl_resource_destroy(resource);
2845}
2846
2847static const struct wl_keyboard_interface keyboard_interface = {
2848 keyboard_release
2849};
2850
Derek Foreman280e7dd2014-10-03 13:13:42 -05002851static bool
Neil Roberts96d790e2013-09-19 17:32:00 +01002852should_send_modifiers_to_client(struct weston_seat *seat,
2853 struct wl_client *client)
2854{
Derek Foreman1281a362015-07-31 16:55:32 -05002855 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2856 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2857
2858 if (keyboard &&
2859 keyboard->focus &&
2860 keyboard->focus->resource &&
2861 wl_resource_get_client(keyboard->focus->resource) == client)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002862 return true;
Neil Roberts96d790e2013-09-19 17:32:00 +01002863
Derek Foreman1281a362015-07-31 16:55:32 -05002864 if (pointer &&
2865 pointer->focus &&
2866 pointer->focus->surface->resource &&
2867 wl_resource_get_client(pointer->focus->surface->resource) == client)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002868 return true;
Neil Roberts96d790e2013-09-19 17:32:00 +01002869
Derek Foreman280e7dd2014-10-03 13:13:42 -05002870 return false;
Neil Roberts96d790e2013-09-19 17:32:00 +01002871}
2872
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002873static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002874seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2875 uint32_t id)
2876{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002877 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002878 /* We use the keyboard_state directly, which means we'll
2879 * give a wl_keyboard if the seat has ever had one - even though
2880 * the spec explicitly states that this request only takes effect
2881 * if the seat has the keyboard capability.
2882 *
2883 * This prevents a race between the compositor sending new
2884 * capabilities and the client trying to use the old ones.
2885 */
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002886 struct weston_keyboard *keyboard = seat ? seat->keyboard_state : NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002887 struct wl_resource *cr;
2888
Jason Ekstranda85118c2013-06-27 20:17:02 -05002889 cr = wl_resource_create(client, &wl_keyboard_interface,
2890 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002891 if (cr == NULL) {
2892 wl_client_post_no_memory(client);
2893 return;
2894 }
2895
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002896 wl_list_init(wl_resource_get_link(cr));
2897 wl_resource_set_implementation(cr, &keyboard_interface,
Alexandros Frantzis2b442482018-02-20 14:05:50 +02002898 keyboard, destroy_keyboard_resource);
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002899
2900 /* If we don't have a keyboard_state, the resource is inert, so there
2901 * is nothing more to set up */
2902 if (!keyboard)
2903 return;
2904
Neil Roberts96d790e2013-09-19 17:32:00 +01002905 /* May be moved to focused list later by either
2906 * weston_keyboard_set_focus or directly if this client is already
2907 * focused */
Derek Foreman345c9f32015-06-03 15:53:28 -05002908 wl_list_insert(&keyboard->resource_list, wl_resource_get_link(cr));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002909
Jonny Lamb66a41a02014-08-12 14:58:25 +02002910 if (wl_resource_get_version(cr) >= WL_KEYBOARD_REPEAT_INFO_SINCE_VERSION) {
2911 wl_keyboard_send_repeat_info(cr,
2912 seat->compositor->kb_repeat_rate,
2913 seat->compositor->kb_repeat_delay);
2914 }
Jasper St. Pierred8c6aeb2014-08-04 13:43:24 -04002915
Derek Foremanf8f7fd62017-06-28 11:41:43 -05002916 weston_keyboard_send_keymap(keyboard, cr);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002917
Neil Roberts96d790e2013-09-19 17:32:00 +01002918 if (should_send_modifiers_to_client(seat, client)) {
Derek Foreman345c9f32015-06-03 15:53:28 -05002919 send_modifiers_to_resource(keyboard,
Neil Roberts96d790e2013-09-19 17:32:00 +01002920 cr,
Derek Foreman345c9f32015-06-03 15:53:28 -05002921 keyboard->focus_serial);
Neil Roberts96d790e2013-09-19 17:32:00 +01002922 }
2923
Derek Foreman345c9f32015-06-03 15:53:28 -05002924 if (keyboard->focus && keyboard->focus->resource &&
2925 wl_resource_get_client(keyboard->focus->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01002926 struct weston_surface *surface =
Derek Foreman345c9f32015-06-03 15:53:28 -05002927 (struct weston_surface *)keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +01002928
2929 wl_list_remove(wl_resource_get_link(cr));
Derek Foreman345c9f32015-06-03 15:53:28 -05002930 wl_list_insert(&keyboard->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002931 wl_resource_get_link(cr));
2932 wl_keyboard_send_enter(cr,
Derek Foreman345c9f32015-06-03 15:53:28 -05002933 keyboard->focus_serial,
Neil Roberts96d790e2013-09-19 17:32:00 +01002934 surface->resource,
Derek Foreman345c9f32015-06-03 15:53:28 -05002935 &keyboard->keys);
Neil Roberts96d790e2013-09-19 17:32:00 +01002936
2937 /* If this is the first keyboard resource for this
2938 * client... */
Derek Foreman345c9f32015-06-03 15:53:28 -05002939 if (keyboard->focus_resource_list.prev ==
Neil Roberts96d790e2013-09-19 17:32:00 +01002940 wl_resource_get_link(cr))
2941 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002942 }
2943}
2944
2945static void
Alexandros Frantzisd7157842018-02-20 14:07:03 +02002946destroy_touch_resource(struct wl_resource *resource)
2947{
2948 struct weston_touch *touch = wl_resource_get_user_data(resource);
2949
2950 wl_list_remove(wl_resource_get_link(resource));
2951
2952 if (touch) {
2953 remove_input_resource_from_timestamps(resource,
2954 &touch->timestamps_list);
2955 }
2956}
2957
2958static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002959touch_release(struct wl_client *client, struct wl_resource *resource)
2960{
2961 wl_resource_destroy(resource);
2962}
2963
2964static const struct wl_touch_interface touch_interface = {
2965 touch_release
2966};
2967
2968static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002969seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2970 uint32_t id)
2971{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002972 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002973 /* We use the touch_state directly, which means we'll
2974 * give a wl_touch if the seat has ever had one - even though
2975 * the spec explicitly states that this request only takes effect
2976 * if the seat has the touch capability.
2977 *
2978 * This prevents a race between the compositor sending new
2979 * capabilities and the client trying to use the old ones.
2980 */
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002981 struct weston_touch *touch = seat ? seat->touch_state : NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002982 struct wl_resource *cr;
2983
Jason Ekstranda85118c2013-06-27 20:17:02 -05002984 cr = wl_resource_create(client, &wl_touch_interface,
2985 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002986 if (cr == NULL) {
2987 wl_client_post_no_memory(client);
2988 return;
2989 }
2990
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002991 wl_list_init(wl_resource_get_link(cr));
2992 wl_resource_set_implementation(cr, &touch_interface,
Alexandros Frantzisd7157842018-02-20 14:07:03 +02002993 touch, destroy_touch_resource);
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002994
2995 /* If we don't have a touch_state, the resource is inert, so there
2996 * is nothing more to set up */
2997 if (!touch)
2998 return;
2999
Derek Foreman1281a362015-07-31 16:55:32 -05003000 if (touch->focus &&
3001 wl_resource_get_client(touch->focus->surface->resource) == client) {
Chokshi, Mituld6697142015-10-09 08:28:47 +00003002 wl_list_insert(&touch->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01003003 wl_resource_get_link(cr));
3004 } else {
Chokshi, Mituld6697142015-10-09 08:28:47 +00003005 wl_list_insert(&touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01003006 wl_resource_get_link(cr));
3007 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003008}
3009
Quentin Glidicaab1d362016-03-13 17:49:08 +01003010static void
3011seat_release(struct wl_client *client, struct wl_resource *resource)
3012{
3013 wl_resource_destroy(resource);
3014}
3015
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003016static const struct wl_seat_interface seat_interface = {
3017 seat_get_pointer,
3018 seat_get_keyboard,
3019 seat_get_touch,
Quentin Glidicaab1d362016-03-13 17:49:08 +01003020 seat_release,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003021};
3022
3023static void
3024bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3025{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003026 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003027 struct wl_resource *resource;
3028 enum wl_seat_capability caps = 0;
3029
Jason Ekstranda85118c2013-06-27 20:17:02 -05003030 resource = wl_resource_create(client,
Derek Foreman1909c102015-11-26 14:17:47 -06003031 &wl_seat_interface, version, id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05003032 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003033 wl_resource_set_implementation(resource, &seat_interface, data,
3034 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003035
Derek Foreman1281a362015-07-31 16:55:32 -05003036 if (weston_seat_get_pointer(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003037 caps |= WL_SEAT_CAPABILITY_POINTER;
Derek Foreman1281a362015-07-31 16:55:32 -05003038 if (weston_seat_get_keyboard(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003039 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Derek Foreman1281a362015-07-31 16:55:32 -05003040 if (weston_seat_get_touch(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003041 caps |= WL_SEAT_CAPABILITY_TOUCH;
3042
3043 wl_seat_send_capabilities(resource, caps);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003044 if (version >= WL_SEAT_NAME_SINCE_VERSION)
Rob Bradforde445ae62013-05-31 18:09:51 +01003045 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003046}
3047
Jonas Ådahl30d61d82014-10-22 21:21:17 +02003048static void
3049relative_pointer_destroy(struct wl_client *client,
3050 struct wl_resource *resource)
3051{
3052 wl_resource_destroy(resource);
3053}
3054
3055static const struct zwp_relative_pointer_v1_interface relative_pointer_interface = {
3056 relative_pointer_destroy
3057};
3058
3059static void
3060relative_pointer_manager_destroy(struct wl_client *client,
3061 struct wl_resource *resource)
3062{
3063 wl_resource_destroy(resource);
3064}
3065
3066static void
3067relative_pointer_manager_get_relative_pointer(struct wl_client *client,
3068 struct wl_resource *resource,
3069 uint32_t id,
3070 struct wl_resource *pointer_resource)
3071{
3072 struct weston_pointer *pointer =
3073 wl_resource_get_user_data(pointer_resource);
3074 struct weston_pointer_client *pointer_client;
3075 struct wl_resource *cr;
3076
3077 cr = wl_resource_create(client, &zwp_relative_pointer_v1_interface,
3078 wl_resource_get_version(resource), id);
3079 if (cr == NULL) {
3080 wl_client_post_no_memory(client);
3081 return;
3082 }
3083
3084 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
3085 if (!pointer_client) {
3086 wl_client_post_no_memory(client);
3087 return;
3088 }
3089
3090 wl_list_insert(&pointer_client->relative_pointer_resources,
3091 wl_resource_get_link(cr));
3092 wl_resource_set_implementation(cr, &relative_pointer_interface,
3093 pointer,
3094 unbind_pointer_client_resource);
3095}
3096
3097static const struct zwp_relative_pointer_manager_v1_interface relative_pointer_manager = {
3098 relative_pointer_manager_destroy,
3099 relative_pointer_manager_get_relative_pointer,
3100};
3101
3102static void
3103bind_relative_pointer_manager(struct wl_client *client, void *data,
3104 uint32_t version, uint32_t id)
3105{
3106 struct weston_compositor *compositor = data;
3107 struct wl_resource *resource;
3108
3109 resource = wl_resource_create(client,
3110 &zwp_relative_pointer_manager_v1_interface,
3111 1, id);
3112
3113 wl_resource_set_implementation(resource, &relative_pointer_manager,
3114 compositor,
3115 NULL);
3116}
3117
Giulio Camuffo0358af42016-06-02 21:48:08 +03003118WL_EXPORT int
3119weston_compositor_set_xkb_rule_names(struct weston_compositor *ec,
3120 struct xkb_rule_names *names)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003121{
3122 if (ec->xkb_context == NULL) {
3123 ec->xkb_context = xkb_context_new(0);
3124 if (ec->xkb_context == NULL) {
3125 weston_log("failed to create XKB context\n");
3126 return -1;
3127 }
3128 }
3129
3130 if (names)
3131 ec->xkb_names = *names;
3132 if (!ec->xkb_names.rules)
3133 ec->xkb_names.rules = strdup("evdev");
3134 if (!ec->xkb_names.model)
3135 ec->xkb_names.model = strdup("pc105");
3136 if (!ec->xkb_names.layout)
3137 ec->xkb_names.layout = strdup("us");
3138
3139 return 0;
3140}
3141
Stefan Schmidtfda26522013-09-17 10:54:09 +01003142static void
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003143weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003144{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003145 if (--xkb_info->ref_count > 0)
3146 return;
3147
Ran Benitac9c74152014-08-19 23:59:52 +03003148 xkb_keymap_unref(xkb_info->keymap);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003149
Derek Foreman76829fc2017-06-28 12:17:46 -05003150 if (xkb_info->keymap_string)
3151 free(xkb_info->keymap_string);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003152 free(xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003153}
3154
3155void
3156weston_compositor_xkb_destroy(struct weston_compositor *ec)
3157{
3158 free((char *) ec->xkb_names.rules);
3159 free((char *) ec->xkb_names.model);
3160 free((char *) ec->xkb_names.layout);
3161 free((char *) ec->xkb_names.variant);
3162 free((char *) ec->xkb_names.options);
Stefan Schmidtfda26522013-09-17 10:54:09 +01003163
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003164 if (ec->xkb_info)
3165 weston_xkb_info_destroy(ec->xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003166 xkb_context_unref(ec->xkb_context);
3167}
3168
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003169static struct weston_xkb_info *
3170weston_xkb_info_create(struct xkb_keymap *keymap)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003171{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003172 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
3173 if (xkb_info == NULL)
3174 return NULL;
3175
Ran Benita2e1968f2014-08-19 23:59:51 +03003176 xkb_info->keymap = xkb_keymap_ref(keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003177 xkb_info->ref_count = 1;
3178
Ran Benita2e1968f2014-08-19 23:59:51 +03003179 xkb_info->shift_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3180 XKB_MOD_NAME_SHIFT);
3181 xkb_info->caps_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3182 XKB_MOD_NAME_CAPS);
3183 xkb_info->ctrl_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3184 XKB_MOD_NAME_CTRL);
3185 xkb_info->alt_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3186 XKB_MOD_NAME_ALT);
3187 xkb_info->mod2_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3188 "Mod2");
3189 xkb_info->mod3_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3190 "Mod3");
3191 xkb_info->super_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3192 XKB_MOD_NAME_LOGO);
3193 xkb_info->mod5_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3194 "Mod5");
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003195
Ran Benita2e1968f2014-08-19 23:59:51 +03003196 xkb_info->num_led = xkb_keymap_led_get_index(xkb_info->keymap,
3197 XKB_LED_NAME_NUM);
3198 xkb_info->caps_led = xkb_keymap_led_get_index(xkb_info->keymap,
3199 XKB_LED_NAME_CAPS);
3200 xkb_info->scroll_led = xkb_keymap_led_get_index(xkb_info->keymap,
3201 XKB_LED_NAME_SCROLL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003202
Derek Foreman76829fc2017-06-28 12:17:46 -05003203 xkb_info->keymap_string = xkb_keymap_get_as_string(xkb_info->keymap,
3204 XKB_KEYMAP_FORMAT_TEXT_V1);
3205 if (xkb_info->keymap_string == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003206 weston_log("failed to get string version of keymap\n");
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003207 goto err_keymap;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003208 }
Derek Foreman76829fc2017-06-28 12:17:46 -05003209 xkb_info->keymap_size = strlen(xkb_info->keymap_string) + 1;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003210
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003211 return xkb_info;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003212
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003213err_keymap:
Ran Benita2e1968f2014-08-19 23:59:51 +03003214 xkb_keymap_unref(xkb_info->keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003215 free(xkb_info);
3216 return NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003217}
3218
3219static int
3220weston_compositor_build_global_keymap(struct weston_compositor *ec)
3221{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003222 struct xkb_keymap *keymap;
3223
3224 if (ec->xkb_info != NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003225 return 0;
3226
Ran Benita2e1968f2014-08-19 23:59:51 +03003227 keymap = xkb_keymap_new_from_names(ec->xkb_context,
3228 &ec->xkb_names,
3229 0);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003230 if (keymap == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003231 weston_log("failed to compile global XKB keymap\n");
3232 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
3233 "options %s\n",
3234 ec->xkb_names.rules, ec->xkb_names.model,
3235 ec->xkb_names.layout, ec->xkb_names.variant,
3236 ec->xkb_names.options);
3237 return -1;
3238 }
3239
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003240 ec->xkb_info = weston_xkb_info_create(keymap);
Rui Matos73d93952013-10-24 19:28:41 +02003241 xkb_keymap_unref(keymap);
Stefan Schmidtfda26522013-09-17 10:54:09 +01003242 if (ec->xkb_info == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003243 return -1;
3244
3245 return 0;
3246}
3247
Rui Matos65196bc2013-10-10 19:44:19 +02003248WL_EXPORT void
3249weston_seat_update_keymap(struct weston_seat *seat, struct xkb_keymap *keymap)
3250{
Derek Foreman1281a362015-07-31 16:55:32 -05003251 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
3252
3253 if (!keyboard || !keymap)
Rui Matos65196bc2013-10-10 19:44:19 +02003254 return;
3255
Derek Foreman1281a362015-07-31 16:55:32 -05003256 xkb_keymap_unref(keyboard->pending_keymap);
3257 keyboard->pending_keymap = xkb_keymap_ref(keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02003258
Derek Foreman1281a362015-07-31 16:55:32 -05003259 if (keyboard->keys.size == 0)
Rui Matos65196bc2013-10-10 19:44:19 +02003260 update_keymap(seat);
Rui Matos65196bc2013-10-10 19:44:19 +02003261}
3262
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003263WL_EXPORT int
3264weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
3265{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003266 struct weston_keyboard *keyboard;
3267
Derek Foreman1281a362015-07-31 16:55:32 -05003268 if (seat->keyboard_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003269 seat->keyboard_device_count += 1;
3270 if (seat->keyboard_device_count == 1)
3271 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003272 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003273 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003274
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003275 keyboard = weston_keyboard_create();
3276 if (keyboard == NULL) {
3277 weston_log("failed to allocate weston keyboard struct\n");
3278 return -1;
3279 }
3280
Derek Foreman185d1582017-06-28 11:17:23 -05003281 if (keymap != NULL) {
3282 keyboard->xkb_info = weston_xkb_info_create(keymap);
3283 if (keyboard->xkb_info == NULL)
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02003284 goto err;
Derek Foreman185d1582017-06-28 11:17:23 -05003285 } else {
3286 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
3287 goto err;
3288 keyboard->xkb_info = seat->compositor->xkb_info;
3289 keyboard->xkb_info->ref_count++;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01003290 }
Derek Foreman185d1582017-06-28 11:17:23 -05003291
3292 keyboard->xkb_state.state = xkb_state_new(keyboard->xkb_info->keymap);
3293 if (keyboard->xkb_state.state == NULL) {
3294 weston_log("failed to initialise XKB state\n");
3295 goto err;
3296 }
3297
3298 keyboard->xkb_state.leds = 0;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01003299
Derek Foreman1281a362015-07-31 16:55:32 -05003300 seat->keyboard_state = keyboard;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02003301 seat->keyboard_device_count = 1;
3302 keyboard->seat = seat;
3303
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003304 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003305
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003306 return 0;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02003307
3308err:
3309 if (keyboard->xkb_info)
3310 weston_xkb_info_destroy(keyboard->xkb_info);
3311 free(keyboard);
3312
3313 return -1;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003314}
3315
Jonas Ådahl91fed542013-12-03 09:14:27 +01003316static void
3317weston_keyboard_reset_state(struct weston_keyboard *keyboard)
3318{
3319 struct weston_seat *seat = keyboard->seat;
3320 struct xkb_state *state;
3321
Derek Foreman185d1582017-06-28 11:17:23 -05003322 state = xkb_state_new(keyboard->xkb_info->keymap);
3323 if (!state) {
3324 weston_log("failed to reset XKB state\n");
3325 return;
Jonas Ådahl91fed542013-12-03 09:14:27 +01003326 }
Derek Foreman185d1582017-06-28 11:17:23 -05003327 xkb_state_unref(keyboard->xkb_state.state);
3328 keyboard->xkb_state.state = state;
3329
3330 keyboard->xkb_state.leds = 0;
Jonas Ådahl91fed542013-12-03 09:14:27 +01003331
3332 seat->modifier_state = 0;
3333}
3334
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003335WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003336weston_seat_release_keyboard(struct weston_seat *seat)
3337{
3338 seat->keyboard_device_count--;
Derek Foremand621df22014-11-19 11:04:12 -06003339 assert(seat->keyboard_device_count >= 0);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003340 if (seat->keyboard_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05003341 weston_keyboard_set_focus(seat->keyboard_state, NULL);
3342 weston_keyboard_cancel_grab(seat->keyboard_state);
3343 weston_keyboard_reset_state(seat->keyboard_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003344 seat_send_updated_caps(seat);
3345 }
3346}
3347
3348WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003349weston_seat_init_pointer(struct weston_seat *seat)
3350{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003351 struct weston_pointer *pointer;
3352
Derek Foreman1281a362015-07-31 16:55:32 -05003353 if (seat->pointer_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003354 seat->pointer_device_count += 1;
3355 if (seat->pointer_device_count == 1)
3356 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003357 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003358 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003359
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003360 pointer = weston_pointer_create(seat);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003361 if (pointer == NULL)
3362 return;
3363
Derek Foreman1281a362015-07-31 16:55:32 -05003364 seat->pointer_state = pointer;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003365 seat->pointer_device_count = 1;
3366 pointer->seat = seat;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003367
3368 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003369}
3370
3371WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003372weston_seat_release_pointer(struct weston_seat *seat)
3373{
Derek Foreman1281a362015-07-31 16:55:32 -05003374 struct weston_pointer *pointer = seat->pointer_state;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003375
3376 seat->pointer_device_count--;
3377 if (seat->pointer_device_count == 0) {
Derek Foremanf9318d12015-05-11 15:40:11 -05003378 weston_pointer_clear_focus(pointer);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003379 weston_pointer_cancel_grab(pointer);
Jonas Ådahl630bae82013-10-17 23:04:06 +02003380
Jonas Ådahla4932742013-10-17 23:04:07 +02003381 if (pointer->sprite)
3382 pointer_unmap_sprite(pointer);
3383
Jonas Ådahl3e12e632013-12-02 22:05:05 +01003384 weston_pointer_reset_state(pointer);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003385 seat_send_updated_caps(seat);
Derek Foremanfd5ca512015-01-07 15:00:25 -06003386
3387 /* seat->pointer is intentionally not destroyed so that
3388 * a newly attached pointer on this seat will retain
3389 * the previous cursor co-ordinates.
3390 */
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003391 }
3392}
3393
3394WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003395weston_seat_init_touch(struct weston_seat *seat)
3396{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003397 struct weston_touch *touch;
3398
Derek Foreman1281a362015-07-31 16:55:32 -05003399 if (seat->touch_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003400 seat->touch_device_count += 1;
3401 if (seat->touch_device_count == 1)
3402 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003403 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003404 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003405
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003406 touch = weston_touch_create();
3407 if (touch == NULL)
3408 return;
3409
Derek Foreman1281a362015-07-31 16:55:32 -05003410 seat->touch_state = touch;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003411 seat->touch_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003412 touch->seat = seat;
3413
3414 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003415}
3416
3417WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003418weston_seat_release_touch(struct weston_seat *seat)
3419{
3420 seat->touch_device_count--;
3421 if (seat->touch_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05003422 weston_touch_set_focus(seat->touch_state, NULL);
3423 weston_touch_cancel_grab(seat->touch_state);
3424 weston_touch_reset_state(seat->touch_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003425 seat_send_updated_caps(seat);
3426 }
3427}
3428
3429WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003430weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
3431 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003432{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003433 memset(seat, 0, sizeof *seat);
3434
Kristian Høgsberge3148752013-05-06 23:19:49 -04003435 seat->selection_data_source = NULL;
3436 wl_list_init(&seat->base_resource_list);
3437 wl_signal_init(&seat->selection_signal);
3438 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04003439 wl_signal_init(&seat->destroy_signal);
Jason Ekstranda4ab5422014-04-02 19:53:45 -05003440 wl_signal_init(&seat->updated_caps_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003441
Peter Hutterer87743e92016-01-18 16:38:22 +10003442 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 5,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003443 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003444
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003445 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003446 seat->modifier_state = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003447 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003448
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003449 wl_list_insert(ec->seat_list.prev, &seat->link);
3450
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003451 clipboard_create(seat);
3452
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003453 wl_signal_emit(&ec->seat_created_signal, seat);
3454}
3455
3456WL_EXPORT void
3457weston_seat_release(struct weston_seat *seat)
3458{
Alexandros Frantzis8480d132018-02-15 13:07:09 +02003459 struct wl_resource *resource;
3460
3461 wl_resource_for_each(resource, &seat->base_resource_list) {
3462 wl_resource_set_user_data(resource, NULL);
3463 }
3464
3465 wl_resource_for_each(resource, &seat->drag_resource_list) {
3466 wl_resource_set_user_data(resource, NULL);
3467 }
3468
3469 wl_list_remove(&seat->base_resource_list);
3470 wl_list_remove(&seat->drag_resource_list);
3471
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003472 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003473
Jonas Ådahl1afb2382014-01-03 19:46:51 +01003474 if (seat->saved_kbd_focus)
3475 wl_list_remove(&seat->saved_kbd_focus_listener.link);
3476
Derek Foreman1281a362015-07-31 16:55:32 -05003477 if (seat->pointer_state)
3478 weston_pointer_destroy(seat->pointer_state);
3479 if (seat->keyboard_state)
3480 weston_keyboard_destroy(seat->keyboard_state);
3481 if (seat->touch_state)
3482 weston_touch_destroy(seat->touch_state);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003483
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003484 free (seat->seat_name);
3485
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003486 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04003487
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003488 wl_signal_emit(&seat->destroy_signal, seat);
3489}
Derek Foreman1281a362015-07-31 16:55:32 -05003490
3491/** Get a seat's keyboard pointer
3492 *
3493 * \param seat The seat to query
3494 * \return The seat's keyboard pointer, or NULL if no keyboard is present
3495 *
3496 * The keyboard pointer for a seat isn't freed when all keyboards are removed,
3497 * so it should only be used when the seat's keyboard_device_count is greater
3498 * than zero. This function does that test and only returns a pointer
3499 * when a keyboard is present.
3500 */
3501WL_EXPORT struct weston_keyboard *
3502weston_seat_get_keyboard(struct weston_seat *seat)
3503{
3504 if (!seat)
3505 return NULL;
3506
3507 if (seat->keyboard_device_count)
3508 return seat->keyboard_state;
3509
3510 return NULL;
3511}
3512
3513/** Get a seat's pointer pointer
3514 *
3515 * \param seat The seat to query
3516 * \return The seat's pointer pointer, or NULL if no pointer device is present
3517 *
3518 * The pointer pointer for a seat isn't freed when all mice are removed,
3519 * so it should only be used when the seat's pointer_device_count is greater
3520 * than zero. This function does that test and only returns a pointer
3521 * when a pointing device is present.
3522 */
3523WL_EXPORT struct weston_pointer *
3524weston_seat_get_pointer(struct weston_seat *seat)
3525{
3526 if (!seat)
3527 return NULL;
3528
3529 if (seat->pointer_device_count)
3530 return seat->pointer_state;
3531
3532 return NULL;
3533}
3534
Jonas Ådahld3414f22016-07-22 17:56:31 +08003535static const struct zwp_locked_pointer_v1_interface locked_pointer_interface;
3536static const struct zwp_confined_pointer_v1_interface confined_pointer_interface;
3537
3538static enum pointer_constraint_type
3539pointer_constraint_get_type(struct weston_pointer_constraint *constraint)
3540{
3541 if (wl_resource_instance_of(constraint->resource,
3542 &zwp_locked_pointer_v1_interface,
3543 &locked_pointer_interface)) {
3544 return POINTER_CONSTRAINT_TYPE_LOCK;
3545 } else if (wl_resource_instance_of(constraint->resource,
3546 &zwp_confined_pointer_v1_interface,
3547 &confined_pointer_interface)) {
3548 return POINTER_CONSTRAINT_TYPE_CONFINE;
3549 }
3550
3551 abort();
3552 return 0;
3553}
3554
3555static void
3556pointer_constraint_notify_activated(struct weston_pointer_constraint *constraint)
3557{
3558 struct wl_resource *resource = constraint->resource;
3559
3560 switch (pointer_constraint_get_type(constraint)) {
3561 case POINTER_CONSTRAINT_TYPE_LOCK:
3562 zwp_locked_pointer_v1_send_locked(resource);
3563 break;
3564 case POINTER_CONSTRAINT_TYPE_CONFINE:
3565 zwp_confined_pointer_v1_send_confined(resource);
3566 break;
3567 }
3568}
3569
3570static void
3571pointer_constraint_notify_deactivated(struct weston_pointer_constraint *constraint)
3572{
3573 struct wl_resource *resource = constraint->resource;
3574
3575 switch (pointer_constraint_get_type(constraint)) {
3576 case POINTER_CONSTRAINT_TYPE_LOCK:
3577 zwp_locked_pointer_v1_send_unlocked(resource);
3578 break;
3579 case POINTER_CONSTRAINT_TYPE_CONFINE:
3580 zwp_confined_pointer_v1_send_unconfined(resource);
3581 break;
3582 }
3583}
3584
3585static struct weston_pointer_constraint *
3586get_pointer_constraint_for_pointer(struct weston_surface *surface,
3587 struct weston_pointer *pointer)
3588{
3589 struct weston_pointer_constraint *constraint;
3590
3591 wl_list_for_each(constraint, &surface->pointer_constraints, link) {
3592 if (constraint->pointer == pointer)
3593 return constraint;
3594 }
3595
3596 return NULL;
3597}
3598
Derek Foreman1281a362015-07-31 16:55:32 -05003599/** Get a seat's touch pointer
3600 *
3601 * \param seat The seat to query
3602 * \return The seat's touch pointer, or NULL if no touch device is present
3603 *
3604 * The touch pointer for a seat isn't freed when all touch devices are removed,
3605 * so it should only be used when the seat's touch_device_count is greater
3606 * than zero. This function does that test and only returns a pointer
3607 * when a touch device is present.
3608 */
3609WL_EXPORT struct weston_touch *
3610weston_seat_get_touch(struct weston_seat *seat)
3611{
3612 if (!seat)
3613 return NULL;
3614
3615 if (seat->touch_device_count)
3616 return seat->touch_state;
3617
3618 return NULL;
3619}
Bryce Harrington24f917e2016-06-29 19:04:07 -07003620
3621/** Sets the keyboard focus to the given surface
3622 *
Marius Vlada2dace22019-06-12 16:05:44 +03003623 * \param surface the surface to focus on
Bryce Harrington24f917e2016-06-29 19:04:07 -07003624 * \param seat The seat to query
3625 */
3626WL_EXPORT void
3627weston_seat_set_keyboard_focus(struct weston_seat *seat,
3628 struct weston_surface *surface)
3629{
3630 struct weston_compositor *compositor = seat->compositor;
3631 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003632 struct weston_surface_activation_data activation_data;
Bryce Harrington24f917e2016-06-29 19:04:07 -07003633
Jonas Ådahlef8e1c32016-03-15 20:28:51 +08003634 if (keyboard && keyboard->focus != surface) {
Bryce Harrington24f917e2016-06-29 19:04:07 -07003635 weston_keyboard_set_focus(keyboard, surface);
3636 wl_data_device_set_keyboard_focus(seat);
3637 }
3638
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003639 inc_activate_serial(compositor);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003640
3641 activation_data = (struct weston_surface_activation_data) {
3642 .surface = surface,
3643 .seat = seat,
3644 };
3645 wl_signal_emit(&compositor->activate_signal, &activation_data);
Bryce Harrington24f917e2016-06-29 19:04:07 -07003646}
Jonas Ådahl30d61d82014-10-22 21:21:17 +02003647
Jonas Ådahld3414f22016-07-22 17:56:31 +08003648static void
3649enable_pointer_constraint(struct weston_pointer_constraint *constraint,
3650 struct weston_view *view)
3651{
3652 assert(constraint->view == NULL);
3653 constraint->view = view;
3654 pointer_constraint_notify_activated(constraint);
3655 weston_pointer_start_grab(constraint->pointer, &constraint->grab);
3656 wl_list_remove(&constraint->surface_destroy_listener.link);
3657 wl_list_init(&constraint->surface_destroy_listener.link);
3658}
3659
3660static bool
3661is_pointer_constraint_enabled(struct weston_pointer_constraint *constraint)
3662{
3663 return constraint->view != NULL;
3664}
3665
3666static void
3667weston_pointer_constraint_disable(struct weston_pointer_constraint *constraint)
3668{
3669 constraint->view = NULL;
3670 pointer_constraint_notify_deactivated(constraint);
3671 weston_pointer_end_grab(constraint->grab.pointer);
3672}
3673
3674void
3675weston_pointer_constraint_destroy(struct weston_pointer_constraint *constraint)
3676{
3677 if (is_pointer_constraint_enabled(constraint))
3678 weston_pointer_constraint_disable(constraint);
3679
3680 wl_list_remove(&constraint->pointer_destroy_listener.link);
3681 wl_list_remove(&constraint->surface_destroy_listener.link);
3682 wl_list_remove(&constraint->surface_commit_listener.link);
3683 wl_list_remove(&constraint->surface_activate_listener.link);
3684
3685 wl_resource_set_user_data(constraint->resource, NULL);
3686 pixman_region32_fini(&constraint->region);
3687 wl_list_remove(&constraint->link);
3688 free(constraint);
3689}
3690
3691static void
3692disable_pointer_constraint(struct weston_pointer_constraint *constraint)
3693{
3694 switch (constraint->lifetime) {
3695 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT:
3696 weston_pointer_constraint_destroy(constraint);
3697 break;
3698 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT:
3699 weston_pointer_constraint_disable(constraint);
3700 break;
3701 }
3702}
3703
3704static bool
3705is_within_constraint_region(struct weston_pointer_constraint *constraint,
3706 wl_fixed_t sx, wl_fixed_t sy)
3707{
3708 struct weston_surface *surface = constraint->surface;
3709 pixman_region32_t constraint_region;
3710 bool result;
3711
3712 pixman_region32_init(&constraint_region);
3713 pixman_region32_intersect(&constraint_region,
3714 &surface->input,
3715 &constraint->region);
3716 result = pixman_region32_contains_point(&constraint_region,
3717 wl_fixed_to_int(sx),
3718 wl_fixed_to_int(sy),
3719 NULL);
3720 pixman_region32_fini(&constraint_region);
3721
3722 return result;
3723}
3724
3725static void
3726maybe_enable_pointer_constraint(struct weston_pointer_constraint *constraint)
3727{
3728 struct weston_surface *surface = constraint->surface;
3729 struct weston_view *vit;
3730 struct weston_view *view = NULL;
3731 struct weston_pointer *pointer = constraint->pointer;
3732 struct weston_keyboard *keyboard;
3733 struct weston_seat *seat = pointer->seat;
3734 int32_t x, y;
3735
3736 /* Postpone if no view of the surface was most recently clicked. */
3737 wl_list_for_each(vit, &surface->views, surface_link) {
3738 if (vit->click_to_activate_serial ==
3739 surface->compositor->activate_serial) {
3740 view = vit;
3741 }
3742 }
3743 if (view == NULL)
3744 return;
3745
3746 /* Postpone if surface doesn't have keyboard focus. */
3747 keyboard = weston_seat_get_keyboard(seat);
3748 if (!keyboard || keyboard->focus != surface)
3749 return;
3750
3751 /* Postpone constraint if the pointer is not within the
3752 * constraint region.
3753 */
3754 weston_view_from_global(view,
3755 wl_fixed_to_int(pointer->x),
3756 wl_fixed_to_int(pointer->y),
3757 &x, &y);
3758 if (!is_within_constraint_region(constraint,
3759 wl_fixed_from_int(x),
3760 wl_fixed_from_int(y)))
3761 return;
3762
3763 enable_pointer_constraint(constraint, view);
3764}
3765
3766static void
3767locked_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
3768{
3769}
3770
3771static void
3772locked_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02003773 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003774 struct weston_pointer_motion_event *event)
3775{
Quentin Glidiccde13452016-08-12 10:41:32 +02003776 pointer_send_relative_motion(grab->pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08003777}
3778
3779static void
3780locked_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003781 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003782 uint32_t button,
3783 uint32_t state_w)
3784{
3785 weston_pointer_send_button(grab->pointer, time, button, state_w);
3786}
3787
3788static void
3789locked_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02003790 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003791 struct weston_pointer_axis_event *event)
3792{
3793 weston_pointer_send_axis(grab->pointer, time, event);
3794}
3795
3796static void
3797locked_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
3798 uint32_t source)
3799{
3800 weston_pointer_send_axis_source(grab->pointer, source);
3801}
3802
3803static void
3804locked_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
3805{
3806 weston_pointer_send_frame(grab->pointer);
3807}
3808
3809static void
3810locked_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
3811{
3812 struct weston_pointer_constraint *constraint =
3813 container_of(grab, struct weston_pointer_constraint, grab);
3814
3815 disable_pointer_constraint(constraint);
3816}
3817
3818static const struct weston_pointer_grab_interface
3819 locked_pointer_grab_interface = {
3820 locked_pointer_grab_pointer_focus,
3821 locked_pointer_grab_pointer_motion,
3822 locked_pointer_grab_pointer_button,
3823 locked_pointer_grab_pointer_axis,
3824 locked_pointer_grab_pointer_axis_source,
3825 locked_pointer_grab_pointer_frame,
3826 locked_pointer_grab_pointer_cancel,
3827};
3828
3829static void
3830pointer_constraint_constrain_resource_destroyed(struct wl_resource *resource)
3831{
3832 struct weston_pointer_constraint *constraint =
3833 wl_resource_get_user_data(resource);
3834
3835 if (!constraint)
3836 return;
3837
3838 weston_pointer_constraint_destroy(constraint);
3839}
3840
3841static void
3842pointer_constraint_surface_activate(struct wl_listener *listener, void *data)
3843{
3844 struct weston_surface_activation_data *activation = data;
3845 struct weston_pointer *pointer;
3846 struct weston_surface *focus = activation->surface;
3847 struct weston_pointer_constraint *constraint =
3848 container_of(listener, struct weston_pointer_constraint,
3849 surface_activate_listener);
3850 bool is_constraint_surface;
3851
3852 pointer = weston_seat_get_pointer(activation->seat);
3853 if (!pointer)
3854 return;
3855
3856 is_constraint_surface =
3857 get_pointer_constraint_for_pointer(focus, pointer) == constraint;
3858
3859 if (is_constraint_surface &&
3860 !is_pointer_constraint_enabled(constraint))
3861 maybe_enable_pointer_constraint(constraint);
3862 else if (!is_constraint_surface &&
3863 is_pointer_constraint_enabled(constraint))
3864 disable_pointer_constraint(constraint);
3865}
3866
3867static void
3868pointer_constraint_pointer_destroyed(struct wl_listener *listener, void *data)
3869{
3870 struct weston_pointer_constraint *constraint =
3871 container_of(listener, struct weston_pointer_constraint,
3872 pointer_destroy_listener);
3873
3874 weston_pointer_constraint_destroy(constraint);
3875}
3876
3877static void
3878pointer_constraint_surface_destroyed(struct wl_listener *listener, void *data)
3879{
3880 struct weston_pointer_constraint *constraint =
3881 container_of(listener, struct weston_pointer_constraint,
3882 surface_destroy_listener);
3883
3884 weston_pointer_constraint_destroy(constraint);
3885}
3886
3887static void
3888pointer_constraint_surface_committed(struct wl_listener *listener, void *data)
3889{
3890 struct weston_pointer_constraint *constraint =
3891 container_of(listener, struct weston_pointer_constraint,
3892 surface_commit_listener);
3893
3894 if (constraint->region_is_pending) {
3895 constraint->region_is_pending = false;
3896 pixman_region32_copy(&constraint->region,
3897 &constraint->region_pending);
3898 pixman_region32_fini(&constraint->region_pending);
3899 pixman_region32_init(&constraint->region_pending);
3900 }
3901
3902 if (constraint->hint_is_pending) {
3903 constraint->hint_is_pending = false;
3904
3905 constraint->hint_is_pending = true;
3906 constraint->hint_x = constraint->hint_x_pending;
3907 constraint->hint_y = constraint->hint_y_pending;
3908 }
3909
3910 if (pointer_constraint_get_type(constraint) ==
3911 POINTER_CONSTRAINT_TYPE_CONFINE &&
3912 is_pointer_constraint_enabled(constraint))
3913 maybe_warp_confined_pointer(constraint);
3914}
3915
3916static struct weston_pointer_constraint *
3917weston_pointer_constraint_create(struct weston_surface *surface,
3918 struct weston_pointer *pointer,
3919 struct weston_region *region,
3920 enum zwp_pointer_constraints_v1_lifetime lifetime,
3921 struct wl_resource *cr,
3922 const struct weston_pointer_grab_interface *grab_interface)
3923{
3924 struct weston_pointer_constraint *constraint;
3925
3926 constraint = zalloc(sizeof *constraint);
3927 if (!constraint)
3928 return NULL;
3929
3930 constraint->lifetime = lifetime;
3931 pixman_region32_init(&constraint->region);
3932 pixman_region32_init(&constraint->region_pending);
3933 wl_list_insert(&surface->pointer_constraints, &constraint->link);
3934 constraint->surface = surface;
3935 constraint->pointer = pointer;
3936 constraint->resource = cr;
3937 constraint->grab.interface = grab_interface;
3938 if (region) {
3939 pixman_region32_copy(&constraint->region,
3940 &region->region);
3941 } else {
3942 pixman_region32_fini(&constraint->region);
3943 region_init_infinite(&constraint->region);
3944 }
3945
3946 constraint->surface_activate_listener.notify =
3947 pointer_constraint_surface_activate;
3948 constraint->surface_destroy_listener.notify =
3949 pointer_constraint_surface_destroyed;
3950 constraint->surface_commit_listener.notify =
3951 pointer_constraint_surface_committed;
3952 constraint->pointer_destroy_listener.notify =
3953 pointer_constraint_pointer_destroyed;
3954
3955 wl_signal_add(&surface->compositor->activate_signal,
3956 &constraint->surface_activate_listener);
3957 wl_signal_add(&pointer->destroy_signal,
3958 &constraint->pointer_destroy_listener);
3959 wl_signal_add(&surface->destroy_signal,
3960 &constraint->surface_destroy_listener);
3961 wl_signal_add(&surface->commit_signal,
3962 &constraint->surface_commit_listener);
3963
3964 return constraint;
3965}
3966
3967static void
3968init_pointer_constraint(struct wl_resource *pointer_constraints_resource,
3969 uint32_t id,
3970 struct weston_surface *surface,
3971 struct weston_pointer *pointer,
3972 struct weston_region *region,
3973 enum zwp_pointer_constraints_v1_lifetime lifetime,
3974 const struct wl_interface *interface,
3975 const void *implementation,
3976 const struct weston_pointer_grab_interface *grab_interface)
3977{
3978 struct wl_client *client =
3979 wl_resource_get_client(pointer_constraints_resource);
3980 struct wl_resource *cr;
3981 struct weston_pointer_constraint *constraint;
3982
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02003983 if (pointer && get_pointer_constraint_for_pointer(surface, pointer)) {
Jonas Ådahld3414f22016-07-22 17:56:31 +08003984 wl_resource_post_error(pointer_constraints_resource,
3985 ZWP_POINTER_CONSTRAINTS_V1_ERROR_ALREADY_CONSTRAINED,
3986 "the pointer has a lock/confine request on this surface");
3987 return;
3988 }
3989
3990 cr = wl_resource_create(client, interface,
3991 wl_resource_get_version(pointer_constraints_resource),
3992 id);
3993 if (cr == NULL) {
3994 wl_client_post_no_memory(client);
3995 return;
3996 }
3997
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02003998 if (pointer) {
3999 constraint = weston_pointer_constraint_create(surface, pointer,
4000 region, lifetime,
4001 cr, grab_interface);
4002 if (constraint == NULL) {
4003 wl_client_post_no_memory(client);
4004 return;
4005 }
4006 } else {
4007 constraint = NULL;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004008 }
4009
4010 wl_resource_set_implementation(cr, implementation, constraint,
4011 pointer_constraint_constrain_resource_destroyed);
4012
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02004013 if (constraint)
4014 maybe_enable_pointer_constraint(constraint);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004015}
4016
4017static void
4018pointer_constraints_destroy(struct wl_client *client,
4019 struct wl_resource *resource)
4020{
4021 wl_resource_destroy(resource);
4022}
4023
4024static void
4025locked_pointer_destroy(struct wl_client *client,
4026 struct wl_resource *resource)
4027{
4028 struct weston_pointer_constraint *constraint =
4029 wl_resource_get_user_data(resource);
4030 wl_fixed_t x, y;
4031
4032 if (constraint && constraint->view && constraint->hint_is_pending &&
4033 is_within_constraint_region(constraint,
4034 constraint->hint_x,
4035 constraint->hint_y)) {
4036 weston_view_to_global_fixed(constraint->view,
4037 constraint->hint_x,
4038 constraint->hint_y,
4039 &x, &y);
4040 weston_pointer_move_to(constraint->pointer, x, y);
4041 }
4042 wl_resource_destroy(resource);
4043}
4044
4045static void
4046locked_pointer_set_cursor_position_hint(struct wl_client *client,
4047 struct wl_resource *resource,
4048 wl_fixed_t surface_x,
4049 wl_fixed_t surface_y)
4050{
4051 struct weston_pointer_constraint *constraint =
4052 wl_resource_get_user_data(resource);
4053
4054 /* Ignore a set cursor hint that was sent after the lock was cancelled.
4055 */
4056 if (!constraint ||
4057 !constraint->resource ||
4058 constraint->resource != resource)
4059 return;
4060
4061 constraint->hint_is_pending = true;
4062 constraint->hint_x_pending = surface_x;
4063 constraint->hint_y_pending = surface_y;
4064}
4065
4066static void
4067locked_pointer_set_region(struct wl_client *client,
4068 struct wl_resource *resource,
4069 struct wl_resource *region_resource)
4070{
4071 struct weston_pointer_constraint *constraint =
4072 wl_resource_get_user_data(resource);
4073 struct weston_region *region = region_resource ?
4074 wl_resource_get_user_data(region_resource) : NULL;
4075
4076 if (!constraint)
4077 return;
4078
4079 if (region) {
4080 pixman_region32_copy(&constraint->region_pending,
4081 &region->region);
4082 } else {
4083 pixman_region32_fini(&constraint->region_pending);
4084 region_init_infinite(&constraint->region_pending);
4085 }
4086 constraint->region_is_pending = true;
4087}
4088
4089
4090static const struct zwp_locked_pointer_v1_interface locked_pointer_interface = {
4091 locked_pointer_destroy,
4092 locked_pointer_set_cursor_position_hint,
4093 locked_pointer_set_region,
4094};
4095
4096static void
4097pointer_constraints_lock_pointer(struct wl_client *client,
4098 struct wl_resource *resource,
4099 uint32_t id,
4100 struct wl_resource *surface_resource,
4101 struct wl_resource *pointer_resource,
4102 struct wl_resource *region_resource,
4103 uint32_t lifetime)
4104{
4105 struct weston_surface *surface =
4106 wl_resource_get_user_data(surface_resource);
4107 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
4108 struct weston_region *region = region_resource ?
4109 wl_resource_get_user_data(region_resource) : NULL;
4110
4111 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
4112 &zwp_locked_pointer_v1_interface,
4113 &locked_pointer_interface,
4114 &locked_pointer_grab_interface);
4115}
4116
4117static void
4118confined_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
4119{
4120}
4121
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004122static double
4123vec2d_cross_product(struct vec2d a, struct vec2d b)
4124{
4125 return a.x * b.y - a.y * b.x;
4126}
4127
4128static struct vec2d
4129vec2d_add(struct vec2d a, struct vec2d b)
4130{
4131 return (struct vec2d) {
4132 .x = a.x + b.x,
4133 .y = a.y + b.y,
4134 };
4135}
4136
4137static struct vec2d
4138vec2d_subtract(struct vec2d a, struct vec2d b)
4139{
4140 return (struct vec2d) {
4141 .x = a.x - b.x,
4142 .y = a.y - b.y,
4143 };
4144}
4145
4146static struct vec2d
4147vec2d_multiply_constant(double c, struct vec2d a)
4148{
4149 return (struct vec2d) {
4150 .x = c * a.x,
4151 .y = c * a.y,
4152 };
4153}
4154
4155static bool
4156lines_intersect(struct line *line1, struct line *line2,
4157 struct vec2d *intersection)
4158{
4159 struct vec2d p = line1->a;
4160 struct vec2d r = vec2d_subtract(line1->b, line1->a);
4161 struct vec2d q = line2->a;
4162 struct vec2d s = vec2d_subtract(line2->b, line2->a);
4163 double rxs;
4164 double sxr;
4165 double t;
4166 double u;
4167
4168 /*
4169 * The line (p, r) and (q, s) intersects where
4170 *
4171 * p + t r = q + u s
4172 *
4173 * Calculate t:
4174 *
4175 * (p + t r) × s = (q + u s) × s
4176 * p × s + t (r × s) = q × s + u (s × s)
4177 * p × s + t (r × s) = q × s
4178 * t (r × s) = q × s - p × s
4179 * t (r × s) = (q - p) × s
4180 * t = ((q - p) × s) / (r × s)
4181 *
4182 * Using the same method, for u we get:
4183 *
4184 * u = ((p - q) × r) / (s × r)
4185 */
4186
4187 rxs = vec2d_cross_product(r, s);
4188 sxr = vec2d_cross_product(s, r);
4189
4190 /* If r × s = 0 then the lines are either parallel or collinear. */
4191 if (fabs(rxs) < DBL_MIN)
4192 return false;
4193
4194 t = vec2d_cross_product(vec2d_subtract(q, p), s) / rxs;
4195 u = vec2d_cross_product(vec2d_subtract(p, q), r) / sxr;
4196
4197 /* The lines only intersect if 0 ≤ t ≤ 1 and 0 ≤ u ≤ 1. */
4198 if (t < 0.0 || t > 1.0 || u < 0.0 || u > 1.0)
4199 return false;
4200
4201 *intersection = vec2d_add(p, vec2d_multiply_constant(t, r));
4202 return true;
4203}
4204
4205static struct border *
4206add_border(struct wl_array *array,
4207 double x1, double y1,
4208 double x2, double y2,
4209 enum motion_direction blocking_dir)
4210{
4211 struct border *border = wl_array_add(array, sizeof *border);
4212
4213 *border = (struct border) {
4214 .line = (struct line) {
4215 .a = (struct vec2d) {
4216 .x = x1,
4217 .y = y1,
4218 },
4219 .b = (struct vec2d) {
4220 .x = x2,
4221 .y = y2,
4222 },
4223 },
4224 .blocking_dir = blocking_dir,
4225 };
4226
4227 return border;
4228}
4229
4230static int
4231compare_lines_x(const void *a, const void *b)
4232{
4233 const struct border *border_a = a;
4234 const struct border *border_b = b;
4235
4236
4237 if (border_a->line.a.x == border_b->line.a.x)
4238 return border_a->line.b.x < border_b->line.b.x;
4239 else
4240 return border_a->line.a.x > border_b->line.a.x;
4241}
4242
4243static void
4244add_non_overlapping_edges(pixman_box32_t *boxes,
4245 int band_above_start,
4246 int band_below_start,
4247 int band_below_end,
4248 struct wl_array *borders)
4249{
4250 int i;
4251 struct wl_array band_merge;
4252 struct border *border;
4253 struct border *prev_border;
4254 struct border *new_border;
4255
4256 wl_array_init(&band_merge);
4257
4258 /* Add bottom band of previous row, and top band of current row, and
4259 * sort them so lower left x coordinate comes first. If there are two
4260 * borders with the same left x coordinate, the wider one comes first.
4261 */
4262 for (i = band_above_start; i < band_below_start; i++) {
4263 pixman_box32_t *box = &boxes[i];
4264 add_border(&band_merge, box->x1, box->y2, box->x2, box->y2,
4265 MOTION_DIRECTION_POSITIVE_Y);
4266 }
4267 for (i = band_below_start; i < band_below_end; i++) {
4268 pixman_box32_t *box= &boxes[i];
4269 add_border(&band_merge, box->x1, box->y1, box->x2, box->y1,
4270 MOTION_DIRECTION_NEGATIVE_Y);
4271 }
4272 qsort(band_merge.data,
4273 band_merge.size / sizeof *border,
4274 sizeof *border,
4275 compare_lines_x);
4276
4277 /* Combine the two combined bands so that any overlapping border is
4278 * eliminated. */
4279 prev_border = NULL;
4280 wl_array_for_each(border, &band_merge) {
4281 assert(border->line.a.y == border->line.b.y);
4282 assert(!prev_border ||
4283 prev_border->line.a.y == border->line.a.y);
4284 assert(!prev_border ||
4285 (prev_border->line.a.x != border->line.a.x ||
4286 prev_border->line.b.x != border->line.b.x));
4287 assert(!prev_border ||
4288 prev_border->line.a.x <= border->line.a.x);
4289
4290 if (prev_border &&
4291 prev_border->line.a.x == border->line.a.x) {
4292 /*
4293 * ------------ +
4294 * ------- =
4295 * [ ]-----
4296 */
4297 prev_border->line.a.x = border->line.b.x;
4298 } else if (prev_border &&
4299 prev_border->line.b.x == border->line.b.x) {
4300 /*
4301 * ------------ +
4302 * ------ =
4303 * ------[ ]
4304 */
4305 prev_border->line.b.x = border->line.a.x;
4306 } else if (prev_border &&
4307 prev_border->line.b.x == border->line.a.x) {
4308 /*
4309 * -------- +
4310 * ------ =
4311 * --------------
4312 */
4313 prev_border->line.b.x = border->line.b.x;
4314 } else if (prev_border &&
4315 prev_border->line.b.x >= border->line.a.x) {
4316 /*
4317 * --------------- +
4318 * ------ =
4319 * -----[ ]----
4320 */
4321 new_border = add_border(borders,
4322 border->line.b.x,
4323 border->line.b.y,
4324 prev_border->line.b.x,
4325 prev_border->line.b.y,
4326 prev_border->blocking_dir);
4327 prev_border->line.b.x = border->line.a.x;
4328 prev_border = new_border;
4329 } else {
4330 assert(!prev_border ||
4331 prev_border->line.b.x < border->line.a.x);
4332 /*
4333 * First border or non-overlapping.
4334 *
4335 * ----- +
4336 * ----- =
4337 * ----- -----
4338 */
4339 new_border = wl_array_add(borders, sizeof *border);
4340 *new_border = *border;
4341 prev_border = new_border;
4342 }
4343 }
4344
4345 wl_array_release(&band_merge);
4346}
4347
4348static void
4349add_band_bottom_edges(pixman_box32_t *boxes,
4350 int band_start,
4351 int band_end,
4352 struct wl_array *borders)
4353{
4354 int i;
4355
4356 for (i = band_start; i < band_end; i++) {
4357 add_border(borders,
4358 boxes[i].x1, boxes[i].y2,
4359 boxes[i].x2, boxes[i].y2,
4360 MOTION_DIRECTION_POSITIVE_Y);
4361 }
4362}
4363
4364static void
4365region_to_outline(pixman_region32_t *region, struct wl_array *borders)
4366{
4367 pixman_box32_t *boxes;
4368 int num_boxes;
4369 int i;
4370 int top_most, bottom_most;
4371 int current_roof;
4372 int prev_top;
4373 int band_start, prev_band_start;
4374
4375 /*
4376 * Remove any overlapping lines from the set of rectangles. Note that
4377 * pixman regions are grouped as rows of rectangles, where rectangles
4378 * in one row never touch or overlap and are all of the same height.
4379 *
4380 * -------- --- -------- ---
4381 * | | | | | | | |
4382 * ----------====---- --- ----------- ----- ---
4383 * | | => | |
4384 * ----==========--------- ----- ----------
4385 * | | | |
4386 * ------------------- -------------------
4387 *
4388 */
4389
4390 boxes = pixman_region32_rectangles(region, &num_boxes);
4391 prev_top = 0;
4392 top_most = boxes[0].y1;
4393 current_roof = top_most;
4394 bottom_most = boxes[num_boxes - 1].y2;
4395 band_start = 0;
4396 prev_band_start = 0;
4397 for (i = 0; i < num_boxes; i++) {
4398 /* Detect if there is a vertical empty space, and add the lower
4399 * level of the previous band if so was the case. */
4400 if (i > 0 &&
4401 boxes[i].y1 != prev_top &&
4402 boxes[i].y1 != boxes[i - 1].y2) {
4403 current_roof = boxes[i].y1;
4404 add_band_bottom_edges(boxes,
4405 band_start,
4406 i,
4407 borders);
4408 }
4409
4410 /* Special case adding the last band, since it won't be handled
4411 * by the band change detection below. */
4412 if (boxes[i].y1 != current_roof && i == num_boxes - 1) {
4413 if (boxes[i].y1 != prev_top) {
4414 /* The last band is a single box, so we don't
4415 * have a prev_band_start to tell us when the
4416 * previous band started. */
4417 add_non_overlapping_edges(boxes,
4418 band_start,
4419 i,
4420 i + 1,
4421 borders);
4422 } else {
4423 add_non_overlapping_edges(boxes,
4424 prev_band_start,
4425 band_start,
4426 i + 1,
4427 borders);
4428 }
4429 }
4430
4431 /* Detect when passing a band and combine the top border of the
4432 * just passed band with the bottom band of the previous band.
4433 */
4434 if (boxes[i].y1 != top_most && boxes[i].y1 != prev_top) {
4435 /* Combine the two passed bands. */
4436 if (prev_top != current_roof) {
4437 add_non_overlapping_edges(boxes,
4438 prev_band_start,
4439 band_start,
4440 i,
4441 borders);
4442 }
4443
4444 prev_band_start = band_start;
4445 band_start = i;
4446 }
4447
4448 /* Add the top border if the box is part of the current roof. */
4449 if (boxes[i].y1 == current_roof) {
4450 add_border(borders,
4451 boxes[i].x1, boxes[i].y1,
4452 boxes[i].x2, boxes[i].y1,
4453 MOTION_DIRECTION_NEGATIVE_Y);
4454 }
4455
4456 /* Add the bottom border of the last band. */
4457 if (boxes[i].y2 == bottom_most) {
4458 add_border(borders,
4459 boxes[i].x1, boxes[i].y2,
4460 boxes[i].x2, boxes[i].y2,
4461 MOTION_DIRECTION_POSITIVE_Y);
4462 }
4463
4464 /* Always add the left border. */
4465 add_border(borders,
4466 boxes[i].x1, boxes[i].y1,
4467 boxes[i].x1, boxes[i].y2,
4468 MOTION_DIRECTION_NEGATIVE_X);
4469
4470 /* Always add the right border. */
4471 add_border(borders,
4472 boxes[i].x2, boxes[i].y1,
4473 boxes[i].x2, boxes[i].y2,
4474 MOTION_DIRECTION_POSITIVE_X);
4475
4476 prev_top = boxes[i].y1;
4477 }
4478}
4479
4480static bool
4481is_border_horizontal (struct border *border)
4482{
4483 return border->line.a.y == border->line.b.y;
4484}
4485
4486static bool
4487is_border_blocking_directions(struct border *border,
4488 uint32_t directions)
4489{
4490 /* Don't block parallel motions. */
4491 if (is_border_horizontal(border)) {
4492 if ((directions & (MOTION_DIRECTION_POSITIVE_Y |
4493 MOTION_DIRECTION_NEGATIVE_Y)) == 0)
4494 return false;
4495 } else {
4496 if ((directions & (MOTION_DIRECTION_POSITIVE_X |
4497 MOTION_DIRECTION_NEGATIVE_X)) == 0)
4498 return false;
4499 }
4500
4501 return (~border->blocking_dir & directions) != directions;
4502}
4503
4504static struct border *
4505get_closest_border(struct wl_array *borders,
4506 struct line *motion,
4507 uint32_t directions)
4508{
4509 struct border *border;
4510 struct vec2d intersection;
4511 struct vec2d delta;
4512 double distance_2;
4513 struct border *closest_border = NULL;
4514 double closest_distance_2 = DBL_MAX;
4515
4516 wl_array_for_each(border, borders) {
4517 if (!is_border_blocking_directions(border, directions))
4518 continue;
4519
4520 if (!lines_intersect(&border->line, motion, &intersection))
4521 continue;
4522
4523 delta = vec2d_subtract(intersection, motion->a);
4524 distance_2 = delta.x*delta.x + delta.y*delta.y;
4525 if (distance_2 < closest_distance_2) {
4526 closest_border = border;
4527 closest_distance_2 = distance_2;
4528 }
4529 }
4530
4531 return closest_border;
4532}
4533
4534static void
4535clamp_to_border(struct border *border,
4536 struct line *motion,
4537 uint32_t *motion_dir)
4538{
4539 /*
4540 * When clamping either rightward or downward motions, the motion needs
4541 * to be clamped so that the destination coordinate does not end up on
4542 * the border (see weston_pointer_clamp_event_to_region). Do this by
4543 * clamping such motions to the border minus the smallest possible
4544 * wl_fixed_t value.
4545 */
4546 if (is_border_horizontal(border)) {
4547 if (*motion_dir & MOTION_DIRECTION_POSITIVE_Y)
4548 motion->b.y = border->line.a.y - wl_fixed_to_double(1);
4549 else
4550 motion->b.y = border->line.a.y;
4551 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_Y |
4552 MOTION_DIRECTION_NEGATIVE_Y);
4553 } else {
4554 if (*motion_dir & MOTION_DIRECTION_POSITIVE_X)
4555 motion->b.x = border->line.a.x - wl_fixed_to_double(1);
4556 else
4557 motion->b.x = border->line.a.x;
4558 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_X |
4559 MOTION_DIRECTION_NEGATIVE_X);
4560 }
4561}
4562
4563static uint32_t
4564get_motion_directions(struct line *motion)
4565{
4566 uint32_t directions = 0;
4567
4568 if (motion->a.x < motion->b.x)
4569 directions |= MOTION_DIRECTION_POSITIVE_X;
4570 else if (motion->a.x > motion->b.x)
4571 directions |= MOTION_DIRECTION_NEGATIVE_X;
4572 if (motion->a.y < motion->b.y)
4573 directions |= MOTION_DIRECTION_POSITIVE_Y;
4574 else if (motion->a.y > motion->b.y)
4575 directions |= MOTION_DIRECTION_NEGATIVE_Y;
4576
4577 return directions;
4578}
4579
Jonas Ådahld3414f22016-07-22 17:56:31 +08004580static void
4581weston_pointer_clamp_event_to_region(struct weston_pointer *pointer,
4582 struct weston_pointer_motion_event *event,
4583 pixman_region32_t *region,
4584 wl_fixed_t *clamped_x,
4585 wl_fixed_t *clamped_y)
4586{
4587 wl_fixed_t x, y;
4588 wl_fixed_t sx, sy;
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004589 wl_fixed_t old_sx = pointer->sx;
4590 wl_fixed_t old_sy = pointer->sy;
4591 struct wl_array borders;
4592 struct line motion;
4593 struct border *closest_border;
4594 float new_x_f, new_y_f;
4595 uint32_t directions;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004596
4597 weston_pointer_motion_to_abs(pointer, event, &x, &y);
4598 weston_view_from_global_fixed(pointer->focus, x, y, &sx, &sy);
4599
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004600 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004601
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004602 /*
4603 * Generate borders given the confine region we are to use. The borders
4604 * are defined to be the outer region of the allowed area. This means
4605 * top/left borders are "within" the allowed area, while bottom/right
4606 * borders are outside. This needs to be considered when clamping
4607 * confined motion vectors.
4608 */
4609 region_to_outline(region, &borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004610
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004611 motion = (struct line) {
4612 .a = (struct vec2d) {
4613 .x = wl_fixed_to_double(old_sx),
4614 .y = wl_fixed_to_double(old_sy),
4615 },
4616 .b = (struct vec2d) {
4617 .x = wl_fixed_to_double(sx),
4618 .y = wl_fixed_to_double(sy),
4619 },
4620 };
4621 directions = get_motion_directions(&motion);
4622
4623 while (directions) {
4624 closest_border = get_closest_border(&borders,
4625 &motion,
4626 directions);
4627 if (closest_border)
4628 clamp_to_border(closest_border, &motion, &directions);
4629 else
4630 break;
4631 }
4632
4633 weston_view_to_global_float(pointer->focus,
4634 (float) motion.b.x, (float) motion.b.y,
4635 &new_x_f, &new_y_f);
4636 *clamped_x = wl_fixed_from_double(new_x_f);
4637 *clamped_y = wl_fixed_from_double(new_y_f);
4638
4639 wl_array_release(&borders);
4640}
4641
4642static double
4643point_to_border_distance_2(struct border *border, double x, double y)
4644{
4645 double orig_x, orig_y;
4646 double dx, dy;
4647
4648 if (is_border_horizontal(border)) {
4649 if (x < border->line.a.x)
4650 orig_x = border->line.a.x;
4651 else if (x > border->line.b.x)
4652 orig_x = border->line.b.x;
4653 else
4654 orig_x = x;
4655 orig_y = border->line.a.y;
4656 } else {
4657 if (y < border->line.a.y)
4658 orig_y = border->line.a.y;
4659 else if (y > border->line.b.y)
4660 orig_y = border->line.b.y;
4661 else
4662 orig_y = y;
4663 orig_x = border->line.a.x;
4664 }
4665
4666
4667 dx = fabs(orig_x - x);
4668 dy = fabs(orig_y - y);
4669 return dx*dx + dy*dy;
4670}
4671
4672static void
4673warp_to_behind_border(struct border *border, wl_fixed_t *sx, wl_fixed_t *sy)
4674{
4675 switch (border->blocking_dir) {
4676 case MOTION_DIRECTION_POSITIVE_X:
4677 case MOTION_DIRECTION_NEGATIVE_X:
4678 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_X)
4679 *sx = wl_fixed_from_double(border->line.a.x) - 1;
4680 else
4681 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4682 if (*sy < wl_fixed_from_double(border->line.a.y))
4683 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4684 else if (*sy > wl_fixed_from_double(border->line.b.y))
4685 *sy = wl_fixed_from_double(border->line.b.y) - 1;
4686 break;
4687 case MOTION_DIRECTION_POSITIVE_Y:
4688 case MOTION_DIRECTION_NEGATIVE_Y:
4689 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_Y)
4690 *sy = wl_fixed_from_double(border->line.a.y) - 1;
4691 else
4692 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4693 if (*sx < wl_fixed_from_double(border->line.a.x))
4694 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4695 else if (*sx > wl_fixed_from_double(border->line.b.x))
4696 *sx = wl_fixed_from_double(border->line.b.x) - 1;
4697 break;
4698 }
Jonas Ådahld3414f22016-07-22 17:56:31 +08004699}
4700
4701static void
4702maybe_warp_confined_pointer(struct weston_pointer_constraint *constraint)
4703{
4704 wl_fixed_t x;
4705 wl_fixed_t y;
4706 wl_fixed_t sx;
4707 wl_fixed_t sy;
4708
4709 weston_view_from_global_fixed(constraint->view,
4710 constraint->pointer->x,
4711 constraint->pointer->y,
4712 &sx,
4713 &sy);
4714
4715 if (!is_within_constraint_region(constraint, sx, sy)) {
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004716 double xf = wl_fixed_to_double(sx);
4717 double yf = wl_fixed_to_double(sy);
4718 pixman_region32_t confine_region;
4719 struct wl_array borders;
4720 struct border *border;
4721 double closest_distance_2 = DBL_MAX;
4722 struct border *closest_border = NULL;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004723
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004724 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004725
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004726 pixman_region32_init(&confine_region);
4727 pixman_region32_intersect(&confine_region,
4728 &constraint->view->surface->input,
4729 &constraint->region);
4730 region_to_outline(&confine_region, &borders);
4731 pixman_region32_fini(&confine_region);
4732
4733 wl_array_for_each(border, &borders) {
4734 double distance_2;
4735
4736 distance_2 = point_to_border_distance_2(border, xf, yf);
4737 if (distance_2 < closest_distance_2) {
4738 closest_border = border;
4739 closest_distance_2 = distance_2;
4740 }
4741 }
4742 assert(closest_border);
4743
4744 warp_to_behind_border(closest_border, &sx, &sy);
4745
4746 wl_array_release(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004747
4748 weston_view_to_global_fixed(constraint->view, sx, sy, &x, &y);
4749 weston_pointer_move_to(constraint->pointer, x, y);
4750 }
4751}
4752
4753static void
4754confined_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02004755 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004756 struct weston_pointer_motion_event *event)
4757{
4758 struct weston_pointer_constraint *constraint =
4759 container_of(grab, struct weston_pointer_constraint, grab);
4760 struct weston_pointer *pointer = grab->pointer;
4761 struct weston_surface *surface;
4762 wl_fixed_t x, y;
4763 wl_fixed_t old_sx = pointer->sx;
4764 wl_fixed_t old_sy = pointer->sy;
4765 pixman_region32_t confine_region;
4766
4767 assert(pointer->focus);
4768 assert(pointer->focus->surface == constraint->surface);
4769
4770 surface = pointer->focus->surface;
4771
4772 pixman_region32_init(&confine_region);
4773 pixman_region32_intersect(&confine_region,
4774 &surface->input,
4775 &constraint->region);
4776 weston_pointer_clamp_event_to_region(pointer, event,
4777 &confine_region, &x, &y);
4778 weston_pointer_move_to(pointer, x, y);
4779 pixman_region32_fini(&confine_region);
4780
4781 weston_view_from_global_fixed(pointer->focus, x, y,
4782 &pointer->sx, &pointer->sy);
4783
4784 if (old_sx != pointer->sx || old_sy != pointer->sy) {
Quentin Glidiccde13452016-08-12 10:41:32 +02004785 pointer_send_motion(pointer, time,
4786 pointer->sx, pointer->sy);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004787 }
4788
Quentin Glidiccde13452016-08-12 10:41:32 +02004789 pointer_send_relative_motion(pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004790}
4791
4792static void
4793confined_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02004794 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004795 uint32_t button,
4796 uint32_t state_w)
4797{
4798 weston_pointer_send_button(grab->pointer, time, button, state_w);
4799}
4800
4801static void
4802confined_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02004803 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004804 struct weston_pointer_axis_event *event)
4805{
4806 weston_pointer_send_axis(grab->pointer, time, event);
4807}
4808
4809static void
4810confined_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
4811 uint32_t source)
4812{
4813 weston_pointer_send_axis_source(grab->pointer, source);
4814}
4815
4816static void
4817confined_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
4818{
4819 weston_pointer_send_frame(grab->pointer);
4820}
4821
4822static void
4823confined_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
4824{
4825 struct weston_pointer_constraint *constraint =
4826 container_of(grab, struct weston_pointer_constraint, grab);
4827
4828 disable_pointer_constraint(constraint);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004829}
4830
4831static const struct weston_pointer_grab_interface
4832 confined_pointer_grab_interface = {
4833 confined_pointer_grab_pointer_focus,
4834 confined_pointer_grab_pointer_motion,
4835 confined_pointer_grab_pointer_button,
4836 confined_pointer_grab_pointer_axis,
4837 confined_pointer_grab_pointer_axis_source,
4838 confined_pointer_grab_pointer_frame,
4839 confined_pointer_grab_pointer_cancel,
4840};
4841
4842static void
4843confined_pointer_destroy(struct wl_client *client,
4844 struct wl_resource *resource)
4845{
4846 wl_resource_destroy(resource);
4847}
4848
4849static void
4850confined_pointer_set_region(struct wl_client *client,
4851 struct wl_resource *resource,
4852 struct wl_resource *region_resource)
4853{
4854 struct weston_pointer_constraint *constraint =
4855 wl_resource_get_user_data(resource);
4856 struct weston_region *region = region_resource ?
4857 wl_resource_get_user_data(region_resource) : NULL;
4858
4859 if (!constraint)
4860 return;
4861
4862 if (region) {
4863 pixman_region32_copy(&constraint->region_pending,
4864 &region->region);
4865 } else {
4866 pixman_region32_fini(&constraint->region_pending);
4867 region_init_infinite(&constraint->region_pending);
4868 }
4869 constraint->region_is_pending = true;
4870}
4871
4872static const struct zwp_confined_pointer_v1_interface confined_pointer_interface = {
4873 confined_pointer_destroy,
4874 confined_pointer_set_region,
4875};
4876
4877static void
4878pointer_constraints_confine_pointer(struct wl_client *client,
4879 struct wl_resource *resource,
4880 uint32_t id,
4881 struct wl_resource *surface_resource,
4882 struct wl_resource *pointer_resource,
4883 struct wl_resource *region_resource,
4884 uint32_t lifetime)
4885{
4886 struct weston_surface *surface =
4887 wl_resource_get_user_data(surface_resource);
4888 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
4889 struct weston_region *region = region_resource ?
4890 wl_resource_get_user_data(region_resource) : NULL;
4891
4892 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
4893 &zwp_confined_pointer_v1_interface,
4894 &confined_pointer_interface,
4895 &confined_pointer_grab_interface);
4896}
4897
4898static const struct zwp_pointer_constraints_v1_interface pointer_constraints_interface = {
4899 pointer_constraints_destroy,
4900 pointer_constraints_lock_pointer,
4901 pointer_constraints_confine_pointer,
4902};
4903
4904static void
4905bind_pointer_constraints(struct wl_client *client, void *data,
4906 uint32_t version, uint32_t id)
4907{
4908 struct wl_resource *resource;
4909
4910 resource = wl_resource_create(client,
4911 &zwp_pointer_constraints_v1_interface,
4912 1, id);
4913
4914 wl_resource_set_implementation(resource, &pointer_constraints_interface,
4915 NULL, NULL);
4916}
4917
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004918static void
Alexandros Frantzis2b442482018-02-20 14:05:50 +02004919input_timestamps_destroy(struct wl_client *client,
4920 struct wl_resource *resource)
4921{
4922 wl_resource_destroy(resource);
4923}
4924
4925static const struct zwp_input_timestamps_v1_interface
4926 input_timestamps_interface = {
4927 input_timestamps_destroy,
4928};
4929
4930static void
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004931input_timestamps_manager_destroy(struct wl_client *client,
4932 struct wl_resource *resource)
4933{
4934 wl_resource_destroy(resource);
4935}
4936
4937static void
4938input_timestamps_manager_get_keyboard_timestamps(struct wl_client *client,
4939 struct wl_resource *resource,
4940 uint32_t id,
4941 struct wl_resource *keyboard_resource)
4942{
Alexandros Frantzis2b442482018-02-20 14:05:50 +02004943 struct weston_keyboard *keyboard =
4944 wl_resource_get_user_data(keyboard_resource);
4945 struct wl_resource *input_ts;
4946
4947 input_ts = wl_resource_create(client,
4948 &zwp_input_timestamps_v1_interface,
4949 1, id);
4950 if (!input_ts) {
4951 wl_client_post_no_memory(client);
4952 return;
4953 }
4954
4955 if (keyboard) {
4956 wl_list_insert(&keyboard->timestamps_list,
4957 wl_resource_get_link(input_ts));
4958 } else {
4959 wl_list_init(wl_resource_get_link(input_ts));
4960 }
4961
4962 wl_resource_set_implementation(input_ts,
4963 &input_timestamps_interface,
4964 keyboard_resource,
4965 unbind_resource);
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004966}
4967
4968static void
4969input_timestamps_manager_get_pointer_timestamps(struct wl_client *client,
4970 struct wl_resource *resource,
4971 uint32_t id,
4972 struct wl_resource *pointer_resource)
4973{
Alexandros Frantzisdb907b72018-02-20 14:06:26 +02004974 struct weston_pointer *pointer =
4975 wl_resource_get_user_data(pointer_resource);
4976 struct wl_resource *input_ts;
4977
4978 input_ts = wl_resource_create(client,
4979 &zwp_input_timestamps_v1_interface,
4980 1, id);
4981 if (!input_ts) {
4982 wl_client_post_no_memory(client);
4983 return;
4984 }
4985
4986 if (pointer) {
4987 wl_list_insert(&pointer->timestamps_list,
4988 wl_resource_get_link(input_ts));
4989 } else {
4990 wl_list_init(wl_resource_get_link(input_ts));
4991 }
4992
4993 wl_resource_set_implementation(input_ts,
4994 &input_timestamps_interface,
4995 pointer_resource,
4996 unbind_resource);
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004997}
4998
4999static void
5000input_timestamps_manager_get_touch_timestamps(struct wl_client *client,
5001 struct wl_resource *resource,
5002 uint32_t id,
5003 struct wl_resource *touch_resource)
5004{
Alexandros Frantzisd7157842018-02-20 14:07:03 +02005005 struct weston_touch *touch = wl_resource_get_user_data(touch_resource);
5006 struct wl_resource *input_ts;
5007
5008 input_ts = wl_resource_create(client,
5009 &zwp_input_timestamps_v1_interface,
5010 1, id);
5011 if (!input_ts) {
5012 wl_client_post_no_memory(client);
5013 return;
5014 }
5015
5016 if (touch) {
5017 wl_list_insert(&touch->timestamps_list,
5018 wl_resource_get_link(input_ts));
5019 } else {
5020 wl_list_init(wl_resource_get_link(input_ts));
5021 }
5022
5023 wl_resource_set_implementation(input_ts,
5024 &input_timestamps_interface,
5025 touch_resource,
5026 unbind_resource);
Alexandros Frantzis538749d2018-02-16 18:44:16 +02005027}
5028
5029static const struct zwp_input_timestamps_manager_v1_interface
5030 input_timestamps_manager_interface = {
5031 input_timestamps_manager_destroy,
5032 input_timestamps_manager_get_keyboard_timestamps,
5033 input_timestamps_manager_get_pointer_timestamps,
5034 input_timestamps_manager_get_touch_timestamps,
5035};
5036
5037static void
5038bind_input_timestamps_manager(struct wl_client *client, void *data,
5039 uint32_t version, uint32_t id)
5040{
5041 struct wl_resource *resource =
5042 wl_resource_create(client,
5043 &zwp_input_timestamps_manager_v1_interface,
5044 1, id);
5045
5046 if (resource == NULL) {
5047 wl_client_post_no_memory(client);
5048 return;
5049 }
5050
5051 wl_resource_set_implementation(resource,
5052 &input_timestamps_manager_interface,
5053 NULL, NULL);
5054}
5055
Jonas Ådahl30d61d82014-10-22 21:21:17 +02005056int
5057weston_input_init(struct weston_compositor *compositor)
5058{
5059 if (!wl_global_create(compositor->wl_display,
5060 &zwp_relative_pointer_manager_v1_interface, 1,
5061 compositor, bind_relative_pointer_manager))
5062 return -1;
5063
Jonas Ådahld3414f22016-07-22 17:56:31 +08005064 if (!wl_global_create(compositor->wl_display,
5065 &zwp_pointer_constraints_v1_interface, 1,
5066 NULL, bind_pointer_constraints))
5067 return -1;
5068
Alexandros Frantzis538749d2018-02-16 18:44:16 +02005069 if (!wl_global_create(compositor->wl_display,
5070 &zwp_input_timestamps_manager_v1_interface, 1,
5071 NULL, bind_input_timestamps_manager))
5072 return -1;
5073
Jonas Ådahl30d61d82014-10-22 21:21:17 +02005074 return 0;
5075}