blob: e3954d0ace9a18f983e653181e2c296a8404b3f8 [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>
Marius Vlad5d649b62019-07-16 23:44:21 +030046#include "backend.h"
Marius Vlad56f3a682019-07-10 14:48:39 +030047#include "libweston-internal.h"
Daniel Stone7dbb0e12016-11-24 15:30:41 +000048#include "relative-pointer-unstable-v1-server-protocol.h"
49#include "pointer-constraints-unstable-v1-server-protocol.h"
Alexandros Frantzis538749d2018-02-16 18:44:16 +020050#include "input-timestamps-unstable-v1-server-protocol.h"
Jonas Ådahld3414f22016-07-22 17:56:31 +080051
52enum pointer_constraint_type {
53 POINTER_CONSTRAINT_TYPE_LOCK,
54 POINTER_CONSTRAINT_TYPE_CONFINE,
55};
56
Jonas Ådahld0be2bb2015-04-30 17:56:37 +080057enum motion_direction {
58 MOTION_DIRECTION_POSITIVE_X = 1 << 0,
59 MOTION_DIRECTION_NEGATIVE_X = 1 << 1,
60 MOTION_DIRECTION_POSITIVE_Y = 1 << 2,
61 MOTION_DIRECTION_NEGATIVE_Y = 1 << 3,
62};
63
64struct vec2d {
65 double x, y;
66};
67
68struct line {
69 struct vec2d a;
70 struct vec2d b;
71};
72
73struct border {
74 struct line line;
75 enum motion_direction blocking_dir;
76};
77
Jonas Ådahld3414f22016-07-22 17:56:31 +080078static void
79maybe_warp_confined_pointer(struct weston_pointer_constraint *constraint);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040080
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040081static void
82empty_region(pixman_region32_t *region)
83{
84 pixman_region32_fini(region);
85 pixman_region32_init(region);
86}
87
Jonas Ådahld3414f22016-07-22 17:56:31 +080088static void
89region_init_infinite(pixman_region32_t *region)
90{
91 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
92 UINT32_MAX, UINT32_MAX);
93}
94
Alexandros Frantzis2b442482018-02-20 14:05:50 +020095static void
96send_timestamp(struct wl_resource *resource,
97 const struct timespec *time)
98{
99 uint32_t tv_sec_hi, tv_sec_lo, tv_nsec;
100
101 timespec_to_proto(time, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
102 zwp_input_timestamps_v1_send_timestamp(resource, tv_sec_hi, tv_sec_lo,
103 tv_nsec);
104}
105
106static void
107send_timestamps_for_input_resource(struct wl_resource *input_resource,
108 struct wl_list *list,
109 const struct timespec *time)
110{
111 struct wl_resource *resource;
112
113 wl_resource_for_each(resource, list) {
114 if (wl_resource_get_user_data(resource) == input_resource)
115 send_timestamp(resource, time);
116 }
117}
118
119static void
120remove_input_resource_from_timestamps(struct wl_resource *input_resource,
121 struct wl_list *list)
122{
123 struct wl_resource *resource;
124
125 wl_resource_for_each(resource, list) {
126 if (wl_resource_get_user_data(resource) == input_resource)
127 wl_resource_set_user_data(resource, NULL);
128 }
129}
130
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -0500131/** Register a touchscreen input device
132 *
133 * \param touch The parent weston_touch that identifies the seat.
134 * \param syspath Unique device name.
135 * \param backend_data Backend private data if necessary.
136 * \param ops Calibration operations, or NULL for not able to run calibration.
137 * \return New touch device, or NULL on failure.
138 */
139WL_EXPORT struct weston_touch_device *
140weston_touch_create_touch_device(struct weston_touch *touch,
141 const char *syspath,
142 void *backend_data,
143 const struct weston_touch_device_ops *ops)
144{
145 struct weston_touch_device *device;
146
147 assert(syspath);
148 if (ops) {
149 assert(ops->get_output);
150 assert(ops->get_calibration_head_name);
151 assert(ops->get_calibration);
152 assert(ops->set_calibration);
153 }
154
155 device = zalloc(sizeof *device);
156 if (!device)
157 return NULL;
158
159 wl_signal_init(&device->destroy_signal);
160
161 device->syspath = strdup(syspath);
162 if (!device->syspath) {
163 free(device);
164 return NULL;
165 }
166
167 device->backend_data = backend_data;
168 device->ops = ops;
169
170 device->aggregate = touch;
171 wl_list_insert(touch->device_list.prev, &device->link);
172
173 return device;
174}
175
176/** Destroy the touch device. */
177WL_EXPORT void
178weston_touch_device_destroy(struct weston_touch_device *device)
179{
180 wl_list_remove(&device->link);
181 wl_signal_emit(&device->destroy_signal, device);
182 free(device->syspath);
183 free(device);
184}
185
186/** Is it possible to run calibration on this touch device? */
187WL_EXPORT bool
188weston_touch_device_can_calibrate(struct weston_touch_device *device)
189{
190 return !!device->ops;
191}
192
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -0500193static enum weston_touch_mode
194weston_touch_device_get_mode(struct weston_touch_device *device)
195{
196 return device->aggregate->seat->compositor->touch_mode;
197}
198
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800199static struct weston_pointer_client *
200weston_pointer_client_create(struct wl_client *client)
201{
202 struct weston_pointer_client *pointer_client;
203
204 pointer_client = zalloc(sizeof *pointer_client);
205 if (!pointer_client)
206 return NULL;
207
208 pointer_client->client = client;
209 wl_list_init(&pointer_client->pointer_resources);
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200210 wl_list_init(&pointer_client->relative_pointer_resources);
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800211
212 return pointer_client;
213}
214
215static void
216weston_pointer_client_destroy(struct weston_pointer_client *pointer_client)
217{
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +0200218 struct wl_resource *resource;
219
220 wl_resource_for_each(resource, &pointer_client->pointer_resources) {
221 wl_resource_set_user_data(resource, NULL);
222 }
223
224 wl_resource_for_each(resource,
225 &pointer_client->relative_pointer_resources) {
226 wl_resource_set_user_data(resource, NULL);
227 }
228
229 wl_list_remove(&pointer_client->pointer_resources);
230 wl_list_remove(&pointer_client->relative_pointer_resources);
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800231 free(pointer_client);
232}
233
234static bool
235weston_pointer_client_is_empty(struct weston_pointer_client *pointer_client)
236{
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200237 return (wl_list_empty(&pointer_client->pointer_resources) &&
238 wl_list_empty(&pointer_client->relative_pointer_resources));
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800239}
240
241static struct weston_pointer_client *
242weston_pointer_get_pointer_client(struct weston_pointer *pointer,
243 struct wl_client *client)
244{
245 struct weston_pointer_client *pointer_client;
246
247 wl_list_for_each(pointer_client, &pointer->pointer_clients, link) {
248 if (pointer_client->client == client)
249 return pointer_client;
250 }
251
252 return NULL;
253}
254
255static struct weston_pointer_client *
256weston_pointer_ensure_pointer_client(struct weston_pointer *pointer,
257 struct wl_client *client)
258{
259 struct weston_pointer_client *pointer_client;
260
261 pointer_client = weston_pointer_get_pointer_client(pointer, client);
262 if (pointer_client)
263 return pointer_client;
264
265 pointer_client = weston_pointer_client_create(client);
266 wl_list_insert(&pointer->pointer_clients, &pointer_client->link);
267
268 if (pointer->focus &&
269 pointer->focus->surface->resource &&
270 wl_resource_get_client(pointer->focus->surface->resource) == client) {
271 pointer->focus_client = pointer_client;
272 }
273
274 return pointer_client;
275}
276
277static void
278weston_pointer_cleanup_pointer_client(struct weston_pointer *pointer,
279 struct weston_pointer_client *pointer_client)
280{
281 if (weston_pointer_client_is_empty(pointer_client)) {
282 if (pointer->focus_client == pointer_client)
283 pointer->focus_client = NULL;
284 wl_list_remove(&pointer_client->link);
285 weston_pointer_client_destroy(pointer_client);
286 }
287}
288
289static void
290unbind_pointer_client_resource(struct wl_resource *resource)
291{
292 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
293 struct wl_client *client = wl_resource_get_client(resource);
294 struct weston_pointer_client *pointer_client;
295
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800296 wl_list_remove(wl_resource_get_link(resource));
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +0200297
298 if (pointer) {
299 pointer_client = weston_pointer_get_pointer_client(pointer,
300 client);
301 assert(pointer_client);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200302 remove_input_resource_from_timestamps(resource,
303 &pointer->timestamps_list);
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +0200304 weston_pointer_cleanup_pointer_client(pointer, pointer_client);
305 }
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800306}
307
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400308static void unbind_resource(struct wl_resource *resource)
309{
Jason Ekstrand44a38632013-06-14 10:08:00 -0500310 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400311}
312
Jonas Ådahl3042ffe2013-10-17 23:04:08 +0200313WL_EXPORT void
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200314weston_pointer_motion_to_abs(struct weston_pointer *pointer,
315 struct weston_pointer_motion_event *event,
316 wl_fixed_t *x, wl_fixed_t *y)
317{
318 if (event->mask & WESTON_POINTER_MOTION_ABS) {
319 *x = wl_fixed_from_double(event->x);
320 *y = wl_fixed_from_double(event->y);
321 } else if (event->mask & WESTON_POINTER_MOTION_REL) {
322 *x = pointer->x + wl_fixed_from_double(event->dx);
323 *y = pointer->y + wl_fixed_from_double(event->dy);
324 } else {
325 assert(!"invalid motion event");
326 *x = *y = 0;
327 }
328}
329
330static bool
331weston_pointer_motion_to_rel(struct weston_pointer *pointer,
332 struct weston_pointer_motion_event *event,
333 double *dx, double *dy,
334 double *dx_unaccel, double *dy_unaccel)
335{
336 if (event->mask & WESTON_POINTER_MOTION_REL &&
337 event->mask & WESTON_POINTER_MOTION_REL_UNACCEL) {
338 *dx = event->dx;
339 *dy = event->dy;
340 *dx_unaccel = event->dx_unaccel;
341 *dy_unaccel = event->dy_unaccel;
342 return true;
343 } else if (event->mask & WESTON_POINTER_MOTION_REL) {
344 *dx_unaccel = *dx = event->dx;
345 *dy_unaccel = *dy = event->dy;
346 return true;
347 } else if (event->mask & WESTON_POINTER_MOTION_REL_UNACCEL) {
348 *dx_unaccel = *dx = event->dx_unaccel;
349 *dy_unaccel = *dy = event->dy_unaccel;
350 return true;
351 } else {
352 return false;
353 }
354}
355
356WL_EXPORT void
Kristian Høgsberga71e8b22013-05-06 21:51:21 -0400357weston_seat_repick(struct weston_seat *seat)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400358{
Derek Foreman1281a362015-07-31 16:55:32 -0500359 const struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400360
Derek Foreman1b786ee2015-06-03 15:53:23 -0500361 if (!pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400362 return;
363
Derek Foreman1b786ee2015-06-03 15:53:23 -0500364 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400365}
366
367static void
368weston_compositor_idle_inhibit(struct weston_compositor *compositor)
369{
370 weston_compositor_wake(compositor);
371 compositor->idle_inhibit++;
372}
373
374static void
375weston_compositor_idle_release(struct weston_compositor *compositor)
376{
377 compositor->idle_inhibit--;
378 weston_compositor_wake(compositor);
379}
380
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400381static void
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100382pointer_focus_view_destroyed(struct wl_listener *listener, void *data)
383{
384 struct weston_pointer *pointer =
385 container_of(listener, struct weston_pointer,
386 focus_view_listener);
387
Derek Foremanf9318d12015-05-11 15:40:11 -0500388 weston_pointer_clear_focus(pointer);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100389}
390
391static void
392pointer_focus_resource_destroyed(struct wl_listener *listener, void *data)
393{
394 struct weston_pointer *pointer =
395 container_of(listener, struct weston_pointer,
396 focus_resource_listener);
397
Derek Foremanf9318d12015-05-11 15:40:11 -0500398 weston_pointer_clear_focus(pointer);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100399}
400
401static void
402keyboard_focus_resource_destroyed(struct wl_listener *listener, void *data)
403{
404 struct weston_keyboard *keyboard =
405 container_of(listener, struct weston_keyboard,
406 focus_resource_listener);
407
408 weston_keyboard_set_focus(keyboard, NULL);
409}
410
411static void
412touch_focus_view_destroyed(struct wl_listener *listener, void *data)
413{
414 struct weston_touch *touch =
415 container_of(listener, struct weston_touch,
416 focus_view_listener);
417
Derek Foreman4c93c082015-04-30 16:45:41 -0500418 weston_touch_set_focus(touch, NULL);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100419}
420
421static void
422touch_focus_resource_destroyed(struct wl_listener *listener, void *data)
423{
424 struct weston_touch *touch =
425 container_of(listener, struct weston_touch,
426 focus_resource_listener);
427
Derek Foreman4c93c082015-04-30 16:45:41 -0500428 weston_touch_set_focus(touch, NULL);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100429}
430
431static void
Neil Roberts96d790e2013-09-19 17:32:00 +0100432move_resources(struct wl_list *destination, struct wl_list *source)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400433{
Neil Roberts96d790e2013-09-19 17:32:00 +0100434 wl_list_insert_list(destination, source);
435 wl_list_init(source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400436}
437
438static void
Neil Roberts96d790e2013-09-19 17:32:00 +0100439move_resources_for_client(struct wl_list *destination,
440 struct wl_list *source,
441 struct wl_client *client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400442{
Neil Roberts96d790e2013-09-19 17:32:00 +0100443 struct wl_resource *resource, *tmp;
444 wl_resource_for_each_safe(resource, tmp, source) {
445 if (wl_resource_get_client(resource) == client) {
446 wl_list_remove(wl_resource_get_link(resource));
447 wl_list_insert(destination,
448 wl_resource_get_link(resource));
449 }
450 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400451}
452
453static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700454default_grab_pointer_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400455{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400456 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500457 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400458 wl_fixed_t sx, sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400459
460 if (pointer->button_count > 0)
461 return;
462
Jason Ekstranda7af7042013-10-12 22:38:11 -0500463 view = weston_compositor_pick_view(pointer->seat->compositor,
464 pointer->x, pointer->y,
465 &sx, &sy);
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400466
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -0800467 if (pointer->focus != view || pointer->sx != sx || pointer->sy != sy)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500468 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400469}
470
471static void
Quentin Glidiccde13452016-08-12 10:41:32 +0200472pointer_send_relative_motion(struct weston_pointer *pointer,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200473 const struct timespec *time,
Quentin Glidiccde13452016-08-12 10:41:32 +0200474 struct weston_pointer_motion_event *event)
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200475{
476 uint64_t time_usec;
477 double dx, dy, dx_unaccel, dy_unaccel;
478 wl_fixed_t dxf, dyf, dxf_unaccel, dyf_unaccel;
479 struct wl_list *resource_list;
480 struct wl_resource *resource;
481
482 if (!pointer->focus_client)
483 return;
484
485 if (!weston_pointer_motion_to_rel(pointer, event,
486 &dx, &dy,
487 &dx_unaccel, &dy_unaccel))
488 return;
489
490 resource_list = &pointer->focus_client->relative_pointer_resources;
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200491 time_usec = timespec_to_usec(&event->time);
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200492 if (time_usec == 0)
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200493 time_usec = timespec_to_usec(time);
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200494
495 dxf = wl_fixed_from_double(dx);
496 dyf = wl_fixed_from_double(dy);
497 dxf_unaccel = wl_fixed_from_double(dx_unaccel);
498 dyf_unaccel = wl_fixed_from_double(dy_unaccel);
499
500 wl_resource_for_each(resource, resource_list) {
501 zwp_relative_pointer_v1_send_relative_motion(
502 resource,
503 (uint32_t) (time_usec >> 32),
504 (uint32_t) time_usec,
505 dxf, dyf,
506 dxf_unaccel, dyf_unaccel);
507 }
508}
509
510static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200511pointer_send_motion(struct weston_pointer *pointer,
512 const struct timespec *time,
Quentin Glidiccde13452016-08-12 10:41:32 +0200513 wl_fixed_t sx, wl_fixed_t sy)
Jonas Ådahlf44942e2016-07-22 17:54:55 +0800514{
515 struct wl_list *resource_list;
516 struct wl_resource *resource;
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200517 uint32_t msecs;
Jonas Ådahlf44942e2016-07-22 17:54:55 +0800518
519 if (!pointer->focus_client)
520 return;
521
522 resource_list = &pointer->focus_client->pointer_resources;
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200523 msecs = timespec_to_msec(time);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200524 wl_resource_for_each(resource, resource_list) {
525 send_timestamps_for_input_resource(resource,
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +0200526 &pointer->timestamps_list,
527 time);
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200528 wl_pointer_send_motion(resource, msecs, sx, sy);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200529 }
Jonas Ådahlf44942e2016-07-22 17:54:55 +0800530}
531
Quentin Glidiccde13452016-08-12 10:41:32 +0200532WL_EXPORT void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200533weston_pointer_send_motion(struct weston_pointer *pointer,
534 const struct timespec *time,
Quentin Glidiccde13452016-08-12 10:41:32 +0200535 struct weston_pointer_motion_event *event)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400536{
Jonas Ådahld2510102014-10-05 21:39:14 +0200537 wl_fixed_t x, y;
Jonas Ådahl8283c342015-04-24 15:26:17 +0800538 wl_fixed_t old_sx = pointer->sx;
539 wl_fixed_t old_sy = pointer->sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400540
Jonas Ådahld2510102014-10-05 21:39:14 +0200541 if (pointer->focus) {
542 weston_pointer_motion_to_abs(pointer, event, &x, &y);
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -0800543 weston_view_from_global_fixed(pointer->focus, x, y,
544 &pointer->sx, &pointer->sy);
Jonas Ådahld2510102014-10-05 21:39:14 +0200545 }
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -0800546
Jonas Ådahld2510102014-10-05 21:39:14 +0200547 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100548
Jonas Ådahlf44942e2016-07-22 17:54:55 +0800549 if (old_sx != pointer->sx || old_sy != pointer->sy) {
Quentin Glidiccde13452016-08-12 10:41:32 +0200550 pointer_send_motion(pointer, time,
551 pointer->sx, pointer->sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400552 }
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200553
Quentin Glidiccde13452016-08-12 10:41:32 +0200554 pointer_send_relative_motion(pointer, time, event);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400555}
556
557static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200558default_grab_pointer_motion(struct weston_pointer_grab *grab,
559 const struct timespec *time,
Quentin Glidiccde13452016-08-12 10:41:32 +0200560 struct weston_pointer_motion_event *event)
561{
562 weston_pointer_send_motion(grab->pointer, time, event);
563}
564
565/** Check if the pointer has focused resources.
566 *
567 * \param pointer The pointer to check for focused resources.
568 * \return Whether or not this pointer has focused resources
569 */
570WL_EXPORT bool
571weston_pointer_has_focus_resource(struct weston_pointer *pointer)
572{
573 if (!pointer->focus_client)
574 return false;
575
576 if (wl_list_empty(&pointer->focus_client->pointer_resources))
577 return false;
578
579 return true;
580}
581
582/** Send wl_pointer.button events to focused resources.
583 *
584 * \param pointer The pointer where the button events originates from.
585 * \param time The timestamp of the event
586 * \param button The button value of the event
Marius Vlada2dace22019-06-12 16:05:44 +0300587 * \param state The state enum value of the event
Quentin Glidiccde13452016-08-12 10:41:32 +0200588 *
589 * For every resource that is currently in focus, send a wl_pointer.button event
590 * with the passed parameters. The focused resources are the wl_pointer
591 * resources of the client which currently has the surface with pointer focus.
592 */
593WL_EXPORT void
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800594weston_pointer_send_button(struct weston_pointer *pointer,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200595 const struct timespec *time, uint32_t button,
Quentin Glidiccde13452016-08-12 10:41:32 +0200596 enum wl_pointer_button_state state)
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800597{
598 struct wl_display *display = pointer->seat->compositor->wl_display;
599 struct wl_list *resource_list;
600 struct wl_resource *resource;
601 uint32_t serial;
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200602 uint32_t msecs;
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800603
Quentin Glidiccde13452016-08-12 10:41:32 +0200604 if (!weston_pointer_has_focus_resource(pointer))
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800605 return;
606
607 resource_list = &pointer->focus_client->pointer_resources;
Quentin Glidiccde13452016-08-12 10:41:32 +0200608 serial = wl_display_next_serial(display);
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200609 msecs = timespec_to_msec(time);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200610 wl_resource_for_each(resource, resource_list) {
611 send_timestamps_for_input_resource(resource,
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +0200612 &pointer->timestamps_list,
613 time);
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200614 wl_pointer_send_button(resource, serial, msecs, button, state);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200615 }
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800616}
617
618static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700619default_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200620 const struct timespec *time, uint32_t button,
Quentin Glidiccde13452016-08-12 10:41:32 +0200621 enum wl_pointer_button_state state)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400622{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400623 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400624 struct weston_compositor *compositor = pointer->seat->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500625 struct weston_view *view;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400626 wl_fixed_t sx, sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400627
Quentin Glidiccde13452016-08-12 10:41:32 +0200628 weston_pointer_send_button(pointer, time, button, state);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400629
630 if (pointer->button_count == 0 &&
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400631 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500632 view = weston_compositor_pick_view(compositor,
633 pointer->x, pointer->y,
634 &sx, &sy);
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400635
Jason Ekstranda7af7042013-10-12 22:38:11 -0500636 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400637 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400638}
639
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200640/** Send wl_pointer.axis events to focused resources.
641 *
642 * \param pointer The pointer where the axis events originates from.
643 * \param time The timestamp of the event
Marius Vlada2dace22019-06-12 16:05:44 +0300644 * \param event The axis value of the event
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200645 *
646 * For every resource that is currently in focus, send a wl_pointer.axis event
647 * with the passed parameters. The focused resources are the wl_pointer
648 * resources of the client which currently has the surface with pointer focus.
649 */
650WL_EXPORT void
651weston_pointer_send_axis(struct weston_pointer *pointer,
Alexandros Frantzis80321942017-11-16 18:20:56 +0200652 const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +1000653 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200654{
655 struct wl_resource *resource;
656 struct wl_list *resource_list;
Alexandros Frantzis80321942017-11-16 18:20:56 +0200657 uint32_t msecs;
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200658
Quentin Glidiccde13452016-08-12 10:41:32 +0200659 if (!weston_pointer_has_focus_resource(pointer))
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800660 return;
661
662 resource_list = &pointer->focus_client->pointer_resources;
Alexandros Frantzis80321942017-11-16 18:20:56 +0200663 msecs = timespec_to_msec(time);
Peter Hutterer87743e92016-01-18 16:38:22 +1000664 wl_resource_for_each(resource, resource_list) {
665 if (event->has_discrete &&
666 wl_resource_get_version(resource) >=
667 WL_POINTER_AXIS_DISCRETE_SINCE_VERSION)
668 wl_pointer_send_axis_discrete(resource, event->axis,
669 event->discrete);
670
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200671 if (event->value) {
672 send_timestamps_for_input_resource(resource,
673 &pointer->timestamps_list,
674 time);
Alexandros Frantzis80321942017-11-16 18:20:56 +0200675 wl_pointer_send_axis(resource, msecs,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200676 event->axis,
677 wl_fixed_from_double(event->value));
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200678 } else if (wl_resource_get_version(resource) >=
679 WL_POINTER_AXIS_STOP_SINCE_VERSION) {
680 send_timestamps_for_input_resource(resource,
681 &pointer->timestamps_list,
682 time);
Alexandros Frantzis80321942017-11-16 18:20:56 +0200683 wl_pointer_send_axis_stop(resource, msecs,
Peter Hutterer87743e92016-01-18 16:38:22 +1000684 event->axis);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200685 }
Peter Hutterer87743e92016-01-18 16:38:22 +1000686 }
687}
688
Quentin Glidiccde13452016-08-12 10:41:32 +0200689/** Send wl_pointer.axis_source events to focused resources.
690 *
691 * \param pointer The pointer where the axis_source events originates from.
692 * \param source The axis_source enum value of the event
693 *
694 * For every resource that is currently in focus, send a wl_pointer.axis_source
695 * event with the passed parameter. The focused resources are the wl_pointer
696 * resources of the client which currently has the surface with pointer focus.
697 */
Peter Hutterer87743e92016-01-18 16:38:22 +1000698WL_EXPORT void
Quentin Glidiccde13452016-08-12 10:41:32 +0200699weston_pointer_send_axis_source(struct weston_pointer *pointer,
700 enum wl_pointer_axis_source source)
Peter Hutterer87743e92016-01-18 16:38:22 +1000701{
702 struct wl_resource *resource;
703 struct wl_list *resource_list;
704
Quentin Glidiccde13452016-08-12 10:41:32 +0200705 if (!weston_pointer_has_focus_resource(pointer))
Jonas Ådahled6014a2016-04-21 10:21:48 +0800706 return;
707
Peter Hutterer87743e92016-01-18 16:38:22 +1000708 resource_list = &pointer->focus_client->pointer_resources;
709 wl_resource_for_each(resource, resource_list) {
710 if (wl_resource_get_version(resource) >=
711 WL_POINTER_AXIS_SOURCE_SINCE_VERSION) {
712 wl_pointer_send_axis_source(resource, source);
713 }
714 }
715}
716
717static void
718pointer_send_frame(struct wl_resource *resource)
719{
720 if (wl_resource_get_version(resource) >=
721 WL_POINTER_FRAME_SINCE_VERSION) {
722 wl_pointer_send_frame(resource);
723 }
724}
725
Quentin Glidiccde13452016-08-12 10:41:32 +0200726/** Send wl_pointer.frame events to focused resources.
727 *
728 * \param pointer The pointer where the frame events originates from.
729 *
730 * For every resource that is currently in focus, send a wl_pointer.frame event.
731 * The focused resources are the wl_pointer resources of the client which
732 * currently has the surface with pointer focus.
733 */
Peter Hutterer87743e92016-01-18 16:38:22 +1000734WL_EXPORT void
735weston_pointer_send_frame(struct weston_pointer *pointer)
736{
737 struct wl_resource *resource;
738 struct wl_list *resource_list;
739
Quentin Glidiccde13452016-08-12 10:41:32 +0200740 if (!weston_pointer_has_focus_resource(pointer))
Derek Foreman8efa31b2016-01-29 10:29:46 -0600741 return;
742
Peter Hutterer87743e92016-01-18 16:38:22 +1000743 resource_list = &pointer->focus_client->pointer_resources;
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200744 wl_resource_for_each(resource, resource_list)
Peter Hutterer87743e92016-01-18 16:38:22 +1000745 pointer_send_frame(resource);
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200746}
747
748static void
749default_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +0200750 const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +1000751 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200752{
Peter Hutterer89b6a492016-01-18 15:58:17 +1000753 weston_pointer_send_axis(grab->pointer, time, event);
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200754}
755
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200756static void
Peter Hutterer87743e92016-01-18 16:38:22 +1000757default_grab_pointer_axis_source(struct weston_pointer_grab *grab,
Quentin Glidiccde13452016-08-12 10:41:32 +0200758 enum wl_pointer_axis_source source)
Peter Hutterer87743e92016-01-18 16:38:22 +1000759{
760 weston_pointer_send_axis_source(grab->pointer, source);
761}
762
763static void
764default_grab_pointer_frame(struct weston_pointer_grab *grab)
765{
766 weston_pointer_send_frame(grab->pointer);
767}
768
769static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200770default_grab_pointer_cancel(struct weston_pointer_grab *grab)
771{
772}
773
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400774static const struct weston_pointer_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400775 default_pointer_grab_interface = {
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700776 default_grab_pointer_focus,
777 default_grab_pointer_motion,
778 default_grab_pointer_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200779 default_grab_pointer_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +1000780 default_grab_pointer_axis_source,
781 default_grab_pointer_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200782 default_grab_pointer_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400783};
784
Quentin Glidiccde13452016-08-12 10:41:32 +0200785/** Check if the touch has focused resources.
786 *
787 * \param touch The touch to check for focused resources.
788 * \return Whether or not this touch has focused resources
789 */
790WL_EXPORT bool
791weston_touch_has_focus_resource(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400792{
Quentin Glidiccde13452016-08-12 10:41:32 +0200793 if (!touch->focus)
794 return false;
795
796 if (wl_list_empty(&touch->focus_resource_list))
797 return false;
798
799 return true;
800}
801
802/** Send wl_touch.down events to focused resources.
803 *
804 * \param touch The touch where the down events originates from.
805 * \param time The timestamp of the event
806 * \param touch_id The touch_id value of the event
807 * \param x The x value of the event
808 * \param y The y value of the event
809 *
810 * For every resource that is currently in focus, send a wl_touch.down event
811 * with the passed parameters. The focused resources are the wl_touch
812 * resources of the client which currently has the surface with touch focus.
813 */
814WL_EXPORT void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200815weston_touch_send_down(struct weston_touch *touch, const struct timespec *time,
Quentin Glidiccde13452016-08-12 10:41:32 +0200816 int touch_id, wl_fixed_t x, wl_fixed_t y)
817{
Rob Bradford880ebc72013-07-22 17:31:38 +0100818 struct wl_display *display = touch->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400819 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100820 struct wl_resource *resource;
821 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300822 wl_fixed_t sx, sy;
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200823 uint32_t msecs;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300824
Quentin Glidiccde13452016-08-12 10:41:32 +0200825 if (!weston_touch_has_focus_resource(touch))
Bryce Harrington2c40d1d2016-02-02 10:18:48 -0800826 return;
827
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300828 weston_view_from_global_fixed(touch->focus, x, y, &sx, &sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400829
Neil Roberts96d790e2013-09-19 17:32:00 +0100830 resource_list = &touch->focus_resource_list;
Quentin Glidiccde13452016-08-12 10:41:32 +0200831 serial = wl_display_next_serial(display);
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200832 msecs = timespec_to_msec(time);
Alexandros Frantzisd7157842018-02-20 14:07:03 +0200833 wl_resource_for_each(resource, resource_list) {
834 send_timestamps_for_input_resource(resource,
835 &touch->timestamps_list,
836 time);
837 wl_touch_send_down(resource, serial, msecs,
838 touch->focus->surface->resource,
839 touch_id, sx, sy);
840 }
Quentin Glidiccde13452016-08-12 10:41:32 +0200841}
Neil Roberts96d790e2013-09-19 17:32:00 +0100842
Quentin Glidiccde13452016-08-12 10:41:32 +0200843static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200844default_grab_touch_down(struct weston_touch_grab *grab,
845 const struct timespec *time, int touch_id,
846 wl_fixed_t x, wl_fixed_t y)
Quentin Glidiccde13452016-08-12 10:41:32 +0200847{
848 weston_touch_send_down(grab->touch, time, touch_id, x, y);
849}
850
851/** Send wl_touch.up events to focused resources.
852 *
853 * \param touch The touch where the up events originates from.
854 * \param time The timestamp of the event
855 * \param touch_id The touch_id value of the event
856 *
857 * For every resource that is currently in focus, send a wl_touch.up event
858 * with the passed parameters. The focused resources are the wl_touch
859 * resources of the client which currently has the surface with touch focus.
860 */
861WL_EXPORT void
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200862weston_touch_send_up(struct weston_touch *touch, const struct timespec *time,
863 int touch_id)
Quentin Glidiccde13452016-08-12 10:41:32 +0200864{
865 struct wl_display *display = touch->seat->compositor->wl_display;
866 uint32_t serial;
867 struct wl_resource *resource;
868 struct wl_list *resource_list;
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200869 uint32_t msecs;
Quentin Glidiccde13452016-08-12 10:41:32 +0200870
871 if (!weston_touch_has_focus_resource(touch))
872 return;
873
874 resource_list = &touch->focus_resource_list;
875 serial = wl_display_next_serial(display);
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200876 msecs = timespec_to_msec(time);
Alexandros Frantzisd7157842018-02-20 14:07:03 +0200877 wl_resource_for_each(resource, resource_list) {
878 send_timestamps_for_input_resource(resource,
879 &touch->timestamps_list,
880 time);
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200881 wl_touch_send_up(resource, serial, msecs, touch_id);
Alexandros Frantzisd7157842018-02-20 14:07:03 +0200882 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400883}
884
Kristian Høgsberge329f362013-05-06 22:19:57 -0400885static void
886default_grab_touch_up(struct weston_touch_grab *grab,
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200887 const struct timespec *time, int touch_id)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400888{
Quentin Glidiccde13452016-08-12 10:41:32 +0200889 weston_touch_send_up(grab->touch, time, touch_id);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400890}
891
Quentin Glidiccde13452016-08-12 10:41:32 +0200892/** Send wl_touch.motion events to focused resources.
893 *
894 * \param touch The touch where the motion events originates from.
895 * \param time The timestamp of the event
896 * \param touch_id The touch_id value of the event
897 * \param x The x value of the event
898 * \param y The y value of the event
899 *
900 * For every resource that is currently in focus, send a wl_touch.motion event
901 * with the passed parameters. The focused resources are the wl_touch
902 * resources of the client which currently has the surface with touch focus.
903 */
904WL_EXPORT void
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200905weston_touch_send_motion(struct weston_touch *touch,
906 const struct timespec *time, int touch_id,
907 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400908{
Neil Roberts96d790e2013-09-19 17:32:00 +0100909 struct wl_resource *resource;
910 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300911 wl_fixed_t sx, sy;
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200912 uint32_t msecs;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300913
Quentin Glidiccde13452016-08-12 10:41:32 +0200914 if (!weston_touch_has_focus_resource(touch))
915 return;
916
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300917 weston_view_from_global_fixed(touch->focus, x, y, &sx, &sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400918
Neil Roberts96d790e2013-09-19 17:32:00 +0100919 resource_list = &touch->focus_resource_list;
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200920 msecs = timespec_to_msec(time);
Neil Roberts96d790e2013-09-19 17:32:00 +0100921 wl_resource_for_each(resource, resource_list) {
Alexandros Frantzisd7157842018-02-20 14:07:03 +0200922 send_timestamps_for_input_resource(resource,
923 &touch->timestamps_list,
924 time);
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200925 wl_touch_send_motion(resource, msecs,
Kristian Høgsberge329f362013-05-06 22:19:57 -0400926 touch_id, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400927 }
928}
929
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200930static void
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200931default_grab_touch_motion(struct weston_touch_grab *grab,
932 const struct timespec *time, int touch_id,
933 wl_fixed_t x, wl_fixed_t y)
Quentin Glidiccde13452016-08-12 10:41:32 +0200934{
935 weston_touch_send_motion(grab->touch, time, touch_id, x, y);
936}
937
938
939/** Send wl_touch.frame events to focused resources.
940 *
941 * \param touch The touch where the frame events originates from.
942 *
943 * For every resource that is currently in focus, send a wl_touch.frame event.
944 * The focused resources are the wl_touch resources of the client which
945 * currently has the surface with touch focus.
946 */
947WL_EXPORT void
948weston_touch_send_frame(struct weston_touch *touch)
Jonas Ådahl1679f232014-04-12 09:39:51 +0200949{
950 struct wl_resource *resource;
951
Quentin Glidiccde13452016-08-12 10:41:32 +0200952 if (!weston_touch_has_focus_resource(touch))
953 return;
954
955 wl_resource_for_each(resource, &touch->focus_resource_list)
Jonas Ådahl1679f232014-04-12 09:39:51 +0200956 wl_touch_send_frame(resource);
957}
958
959static void
Quentin Glidiccde13452016-08-12 10:41:32 +0200960default_grab_touch_frame(struct weston_touch_grab *grab)
961{
962 weston_touch_send_frame(grab->touch);
963}
964
965static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200966default_grab_touch_cancel(struct weston_touch_grab *grab)
967{
968}
969
Kristian Høgsberge329f362013-05-06 22:19:57 -0400970static const struct weston_touch_grab_interface default_touch_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400971 default_grab_touch_down,
972 default_grab_touch_up,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200973 default_grab_touch_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +0200974 default_grab_touch_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200975 default_grab_touch_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400976};
977
Quentin Glidiccde13452016-08-12 10:41:32 +0200978/** Check if the keyboard has focused resources.
979 *
980 * \param keyboard The keyboard to check for focused resources.
981 * \return Whether or not this keyboard has focused resources
982 */
983WL_EXPORT bool
984weston_keyboard_has_focus_resource(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400985{
Quentin Glidiccde13452016-08-12 10:41:32 +0200986 if (!keyboard->focus)
987 return false;
988
989 if (wl_list_empty(&keyboard->focus_resource_list))
990 return false;
991
992 return true;
993}
994
995/** Send wl_keyboard.key events to focused resources.
996 *
997 * \param keyboard The keyboard where the key events originates from.
998 * \param time The timestamp of the event
999 * \param key The key value of the event
1000 * \param state The state enum value of the event
1001 *
1002 * For every resource that is currently in focus, send a wl_keyboard.key event
1003 * with the passed parameters. The focused resources are the wl_keyboard
1004 * resources of the client which currently has the surface with keyboard focus.
1005 */
1006WL_EXPORT void
1007weston_keyboard_send_key(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02001008 const struct timespec *time, uint32_t key,
Quentin Glidiccde13452016-08-12 10:41:32 +02001009 enum wl_keyboard_key_state state)
1010{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001011 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +01001012 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001013 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01001014 struct wl_list *resource_list;
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02001015 uint32_t msecs;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001016
Quentin Glidiccde13452016-08-12 10:41:32 +02001017 if (!weston_keyboard_has_focus_resource(keyboard))
1018 return;
1019
Neil Roberts96d790e2013-09-19 17:32:00 +01001020 resource_list = &keyboard->focus_resource_list;
Quentin Glidiccde13452016-08-12 10:41:32 +02001021 serial = wl_display_next_serial(display);
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02001022 msecs = timespec_to_msec(time);
Alexandros Frantzis2b442482018-02-20 14:05:50 +02001023 wl_resource_for_each(resource, resource_list) {
1024 send_timestamps_for_input_resource(resource,
1025 &keyboard->timestamps_list,
1026 time);
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02001027 wl_keyboard_send_key(resource, serial, msecs, key, state);
Alexandros Frantzis2b442482018-02-20 14:05:50 +02001028 }
Quentin Glidiccde13452016-08-12 10:41:32 +02001029};
1030
1031static void
1032default_grab_keyboard_key(struct weston_keyboard_grab *grab,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02001033 const struct timespec *time, uint32_t key,
1034 uint32_t state)
Quentin Glidiccde13452016-08-12 10:41:32 +02001035{
1036 weston_keyboard_send_key(grab->keyboard, time, key, state);
Neil Roberts96d790e2013-09-19 17:32:00 +01001037}
1038
1039static void
1040send_modifiers_to_resource(struct weston_keyboard *keyboard,
1041 struct wl_resource *resource,
1042 uint32_t serial)
1043{
1044 wl_keyboard_send_modifiers(resource,
1045 serial,
1046 keyboard->modifiers.mods_depressed,
1047 keyboard->modifiers.mods_latched,
1048 keyboard->modifiers.mods_locked,
1049 keyboard->modifiers.group);
1050}
1051
1052static void
1053send_modifiers_to_client_in_list(struct wl_client *client,
1054 struct wl_list *list,
1055 uint32_t serial,
1056 struct weston_keyboard *keyboard)
1057{
1058 struct wl_resource *resource;
1059
1060 wl_resource_for_each(resource, list) {
1061 if (wl_resource_get_client(resource) == client)
1062 send_modifiers_to_resource(keyboard,
1063 resource,
1064 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001065 }
1066}
1067
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001068static struct weston_pointer_client *
1069find_pointer_client_for_surface(struct weston_pointer *pointer,
1070 struct weston_surface *surface)
1071{
1072 struct wl_client *client;
1073
1074 if (!surface)
1075 return NULL;
1076
1077 if (!surface->resource)
1078 return NULL;
1079
1080 client = wl_resource_get_client(surface->resource);
1081 return weston_pointer_get_pointer_client(pointer, client);
1082}
1083
1084static struct weston_pointer_client *
1085find_pointer_client_for_view(struct weston_pointer *pointer, struct weston_view *view)
1086{
1087 if (!view)
1088 return NULL;
1089
1090 return find_pointer_client_for_surface(pointer, view->surface);
1091}
1092
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001093static struct wl_resource *
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001094find_resource_for_surface(struct wl_list *list, struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001095{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001096 if (!surface)
1097 return NULL;
1098
Jason Ekstrand44a38632013-06-14 10:08:00 -05001099 if (!surface->resource)
1100 return NULL;
Stefan Schmidtfda26522013-09-17 10:54:09 +01001101
Jason Ekstrand44a38632013-06-14 10:08:00 -05001102 return wl_resource_find_for_client(list, wl_resource_get_client(surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001103}
1104
Quentin Glidiccde13452016-08-12 10:41:32 +02001105/** Send wl_keyboard.modifiers events to focused resources and pointer
1106 * focused resources.
1107 *
1108 * \param keyboard The keyboard where the modifiers events originates from.
1109 * \param serial The serial of the event
1110 * \param mods_depressed The mods_depressed value of the event
1111 * \param mods_latched The mods_latched value of the event
1112 * \param mods_locked The mods_locked value of the event
1113 * \param group The group value of the event
1114 *
1115 * For every resource that is currently in focus, send a wl_keyboard.modifiers
1116 * event with the passed parameters. The focused resources are the wl_keyboard
1117 * resources of the client which currently has the surface with keyboard focus.
1118 * This also sends wl_keyboard.modifiers events to the wl_keyboard resources of
1119 * the client having pointer focus (if different from the keyboard focus client).
1120 */
1121WL_EXPORT void
1122weston_keyboard_send_modifiers(struct weston_keyboard *keyboard,
1123 uint32_t serial, uint32_t mods_depressed,
1124 uint32_t mods_latched,
1125 uint32_t mods_locked, uint32_t group)
1126{
1127 struct weston_pointer *pointer =
1128 weston_seat_get_pointer(keyboard->seat);
1129
1130 if (weston_keyboard_has_focus_resource(keyboard)) {
1131 struct wl_list *resource_list;
1132 struct wl_resource *resource;
1133
1134 resource_list = &keyboard->focus_resource_list;
1135 wl_resource_for_each(resource, resource_list) {
1136 wl_keyboard_send_modifiers(resource, serial,
1137 mods_depressed, mods_latched,
1138 mods_locked, group);
1139 }
1140 }
1141
1142 if (pointer && pointer->focus && pointer->focus->surface->resource &&
1143 pointer->focus->surface != keyboard->focus) {
1144 struct wl_client *pointer_client =
1145 wl_resource_get_client(pointer->focus->surface->resource);
1146
1147 send_modifiers_to_client_in_list(pointer_client,
1148 &keyboard->resource_list,
1149 serial,
1150 keyboard);
1151 }
1152}
1153
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001154static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -07001155default_grab_keyboard_modifiers(struct weston_keyboard_grab *grab,
1156 uint32_t serial, uint32_t mods_depressed,
1157 uint32_t mods_latched,
1158 uint32_t mods_locked, uint32_t group)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001159{
Quentin Glidiccde13452016-08-12 10:41:32 +02001160 weston_keyboard_send_modifiers(grab->keyboard, serial, mods_depressed,
1161 mods_latched, mods_locked, group);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001162}
1163
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001164static void
1165default_grab_keyboard_cancel(struct weston_keyboard_grab *grab)
1166{
1167}
1168
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001169static const struct weston_keyboard_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001170 default_keyboard_grab_interface = {
Kristian Høgsbergb27901c2013-10-28 15:32:02 -07001171 default_grab_keyboard_key,
1172 default_grab_keyboard_modifiers,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001173 default_grab_keyboard_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001174};
1175
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001176static void
1177pointer_unmap_sprite(struct weston_pointer *pointer)
1178{
Pekka Paalanenc557ff72014-11-12 16:42:52 +02001179 struct weston_surface *surface = pointer->sprite->surface;
1180
1181 if (weston_surface_is_mapped(surface))
1182 weston_surface_unmap(surface);
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001183
1184 wl_list_remove(&pointer->sprite_destroy_listener.link);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02001185 surface->committed = NULL;
1186 surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03001187 weston_surface_set_label_func(surface, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001188 weston_view_destroy(pointer->sprite);
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001189 pointer->sprite = NULL;
1190}
1191
1192static void
1193pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
1194{
1195 struct weston_pointer *pointer =
1196 container_of(listener, struct weston_pointer,
1197 sprite_destroy_listener);
1198
1199 pointer->sprite = NULL;
1200}
1201
Jonas Ådahl3e12e632013-12-02 22:05:05 +01001202static void
1203weston_pointer_reset_state(struct weston_pointer *pointer)
1204{
1205 pointer->button_count = 0;
1206}
1207
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001208static void
1209weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data);
1210
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001211static struct weston_pointer *
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001212weston_pointer_create(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001213{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001214 struct weston_pointer *pointer;
1215
Peter Huttererf3d62272013-08-08 11:57:05 +10001216 pointer = zalloc(sizeof *pointer);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001217 if (pointer == NULL)
1218 return NULL;
1219
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001220 wl_list_init(&pointer->pointer_clients);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001221 weston_pointer_set_default_grab(pointer,
1222 seat->compositor->default_pointer_grab);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001223 wl_list_init(&pointer->focus_resource_listener.link);
1224 pointer->focus_resource_listener.notify = pointer_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001225 pointer->default_grab.pointer = pointer;
1226 pointer->grab = &pointer->default_grab;
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001227 wl_signal_init(&pointer->motion_signal);
Emilio Pozuelo Monfortaa7a4762013-11-19 11:37:15 +01001228 wl_signal_init(&pointer->focus_signal);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001229 wl_list_init(&pointer->focus_view_listener.link);
Jonas Ådahl3eb4ddd2016-07-22 17:52:58 +08001230 wl_signal_init(&pointer->destroy_signal);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +02001231 wl_list_init(&pointer->timestamps_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001232
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001233 pointer->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
1234
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001235 /* FIXME: Pick better co-ords. */
1236 pointer->x = wl_fixed_from_int(100);
1237 pointer->y = wl_fixed_from_int(100);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001238
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001239 pointer->output_destroy_listener.notify =
1240 weston_pointer_handle_output_destroy;
1241 wl_signal_add(&seat->compositor->output_destroyed_signal,
1242 &pointer->output_destroy_listener);
1243
Derek Foremanf9318d12015-05-11 15:40:11 -05001244 pointer->sx = wl_fixed_from_int(-1000000);
1245 pointer->sy = wl_fixed_from_int(-1000000);
1246
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001247 return pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001248}
1249
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001250static void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001251weston_pointer_destroy(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001252{
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +02001253 struct weston_pointer_client *pointer_client, *tmp;
1254
Jonas Ådahl3eb4ddd2016-07-22 17:52:58 +08001255 wl_signal_emit(&pointer->destroy_signal, pointer);
1256
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001257 if (pointer->sprite)
1258 pointer_unmap_sprite(pointer);
1259
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +02001260 wl_list_for_each_safe(pointer_client, tmp, &pointer->pointer_clients,
1261 link) {
1262 wl_list_remove(&pointer_client->link);
1263 weston_pointer_client_destroy(pointer_client);
1264 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001265
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001266 wl_list_remove(&pointer->focus_resource_listener.link);
1267 wl_list_remove(&pointer->focus_view_listener.link);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001268 wl_list_remove(&pointer->output_destroy_listener.link);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +02001269 wl_list_remove(&pointer->timestamps_list);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001270 free(pointer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001271}
1272
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001273void
1274weston_pointer_set_default_grab(struct weston_pointer *pointer,
1275 const struct weston_pointer_grab_interface *interface)
1276{
1277 if (interface)
1278 pointer->default_grab.interface = interface;
1279 else
1280 pointer->default_grab.interface =
1281 &default_pointer_grab_interface;
1282}
1283
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001284static struct weston_keyboard *
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001285weston_keyboard_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001286{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001287 struct weston_keyboard *keyboard;
1288
Peter Huttererf3d62272013-08-08 11:57:05 +10001289 keyboard = zalloc(sizeof *keyboard);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001290 if (keyboard == NULL)
Neil Roberts96d790e2013-09-19 17:32:00 +01001291 return NULL;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001292
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001293 wl_list_init(&keyboard->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +01001294 wl_list_init(&keyboard->focus_resource_list);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001295 wl_list_init(&keyboard->focus_resource_listener.link);
1296 keyboard->focus_resource_listener.notify = keyboard_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001297 wl_array_init(&keyboard->keys);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001298 keyboard->default_grab.interface = &default_keyboard_grab_interface;
1299 keyboard->default_grab.keyboard = keyboard;
1300 keyboard->grab = &keyboard->default_grab;
1301 wl_signal_init(&keyboard->focus_signal);
Alexandros Frantzis2b442482018-02-20 14:05:50 +02001302 wl_list_init(&keyboard->timestamps_list);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001303
1304 return keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001305}
1306
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001307static void
1308weston_xkb_info_destroy(struct weston_xkb_info *xkb_info);
1309
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001310static void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001311weston_keyboard_destroy(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001312{
Alexandros Frantzis7a314d62018-01-26 18:47:56 +02001313 struct wl_resource *resource;
1314
1315 wl_resource_for_each(resource, &keyboard->resource_list) {
1316 wl_resource_set_user_data(resource, NULL);
1317 }
1318
1319 wl_resource_for_each(resource, &keyboard->focus_resource_list) {
1320 wl_resource_set_user_data(resource, NULL);
1321 }
1322
1323 wl_list_remove(&keyboard->resource_list);
1324 wl_list_remove(&keyboard->focus_resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +01001325
Derek Foreman185d1582017-06-28 11:17:23 -05001326 xkb_state_unref(keyboard->xkb_state.state);
1327 if (keyboard->xkb_info)
1328 weston_xkb_info_destroy(keyboard->xkb_info);
1329 xkb_keymap_unref(keyboard->pending_keymap);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001330
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001331 wl_array_release(&keyboard->keys);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001332 wl_list_remove(&keyboard->focus_resource_listener.link);
Alexandros Frantzis2b442482018-02-20 14:05:50 +02001333 wl_list_remove(&keyboard->timestamps_list);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001334 free(keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001335}
1336
Jonas Ådahlcbfa7f72013-12-02 22:05:04 +01001337static void
1338weston_touch_reset_state(struct weston_touch *touch)
1339{
1340 touch->num_tp = 0;
1341}
1342
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001343static struct weston_touch *
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001344weston_touch_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001345{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001346 struct weston_touch *touch;
1347
Peter Huttererf3d62272013-08-08 11:57:05 +10001348 touch = zalloc(sizeof *touch);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001349 if (touch == NULL)
1350 return NULL;
1351
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -05001352 wl_list_init(&touch->device_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001353 wl_list_init(&touch->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +01001354 wl_list_init(&touch->focus_resource_list);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001355 wl_list_init(&touch->focus_view_listener.link);
1356 touch->focus_view_listener.notify = touch_focus_view_destroyed;
1357 wl_list_init(&touch->focus_resource_listener.link);
1358 touch->focus_resource_listener.notify = touch_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001359 touch->default_grab.interface = &default_touch_grab_interface;
1360 touch->default_grab.touch = touch;
1361 touch->grab = &touch->default_grab;
1362 wl_signal_init(&touch->focus_signal);
Alexandros Frantzisd7157842018-02-20 14:07:03 +02001363 wl_list_init(&touch->timestamps_list);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001364
1365 return touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001366}
1367
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001368static void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001369weston_touch_destroy(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001370{
Alexandros Frantzisb0b598c2018-01-26 18:47:57 +02001371 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01001372
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -05001373 assert(wl_list_empty(&touch->device_list));
1374
Alexandros Frantzisb0b598c2018-01-26 18:47:57 +02001375 wl_resource_for_each(resource, &touch->resource_list) {
1376 wl_resource_set_user_data(resource, NULL);
1377 }
1378
1379 wl_resource_for_each(resource, &touch->focus_resource_list) {
1380 wl_resource_set_user_data(resource, NULL);
1381 }
1382
1383 wl_list_remove(&touch->resource_list);
1384 wl_list_remove(&touch->focus_resource_list);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001385 wl_list_remove(&touch->focus_view_listener.link);
1386 wl_list_remove(&touch->focus_resource_listener.link);
Alexandros Frantzisd7157842018-02-20 14:07:03 +02001387 wl_list_remove(&touch->timestamps_list);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001388 free(touch);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001389}
1390
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001391static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04001392seat_send_updated_caps(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001393{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001394 enum wl_seat_capability caps = 0;
Rob Bradford6e737f52013-09-06 17:48:19 +01001395 struct wl_resource *resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001396
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001397 if (seat->pointer_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001398 caps |= WL_SEAT_CAPABILITY_POINTER;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001399 if (seat->keyboard_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001400 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001401 if (seat->touch_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001402 caps |= WL_SEAT_CAPABILITY_TOUCH;
1403
Rob Bradford6e737f52013-09-06 17:48:19 +01001404 wl_resource_for_each(resource, &seat->base_resource_list) {
1405 wl_seat_send_capabilities(resource, caps);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001406 }
Jason Ekstranda4ab5422014-04-02 19:53:45 -05001407 wl_signal_emit(&seat->updated_caps_signal, seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001408}
1409
Derek Foremanf9318d12015-05-11 15:40:11 -05001410
1411/** Clear the pointer focus
1412 *
1413 * \param pointer the pointer to clear focus for.
1414 *
1415 * This can be used to unset pointer focus and set the co-ordinates to the
1416 * arbitrary values we use for the no focus case.
1417 *
1418 * There's no requirement to use this function. For example, passing the
1419 * results of a weston_compositor_pick_view() directly to
1420 * weston_pointer_set_focus() will do the right thing when no view is found.
1421 */
1422WL_EXPORT void
1423weston_pointer_clear_focus(struct weston_pointer *pointer)
1424{
1425 weston_pointer_set_focus(pointer, NULL,
1426 wl_fixed_from_int(-1000000),
1427 wl_fixed_from_int(-1000000));
1428}
1429
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001430WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001431weston_pointer_set_focus(struct weston_pointer *pointer,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001432 struct weston_view *view,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001433 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001434{
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001435 struct weston_pointer_client *pointer_client;
Derek Foreman1281a362015-07-31 16:55:32 -05001436 struct weston_keyboard *kbd = weston_seat_get_keyboard(pointer->seat);
Neil Roberts96d790e2013-09-19 17:32:00 +01001437 struct wl_resource *resource;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001438 struct wl_resource *surface_resource;
Rob Bradford880ebc72013-07-22 17:31:38 +01001439 struct wl_display *display = pointer->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001440 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01001441 struct wl_list *focus_resource_list;
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001442 int refocus = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001443
1444 if ((!pointer->focus && view) ||
1445 (pointer->focus && !view) ||
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001446 (pointer->focus && pointer->focus->surface != view->surface) ||
1447 pointer->sx != sx || pointer->sy != sy)
1448 refocus = 1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001449
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001450 if (pointer->focus_client && refocus) {
1451 focus_resource_list = &pointer->focus_client->pointer_resources;
1452 if (!wl_list_empty(focus_resource_list)) {
1453 serial = wl_display_next_serial(display);
1454 surface_resource = pointer->focus->surface->resource;
1455 wl_resource_for_each(resource, focus_resource_list) {
1456 wl_pointer_send_leave(resource, serial,
1457 surface_resource);
Peter Hutterer87743e92016-01-18 16:38:22 +10001458 pointer_send_frame(resource);
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001459 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001460 }
1461
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001462 pointer->focus_client = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001463 }
1464
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001465 pointer_client = find_pointer_client_for_view(pointer, view);
1466 if (pointer_client && refocus) {
1467 struct wl_client *surface_client = pointer_client->client;
Neil Roberts96d790e2013-09-19 17:32:00 +01001468
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001469 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001470
Jason Ekstranda7af7042013-10-12 22:38:11 -05001471 if (kbd && kbd->focus != view->surface)
Kristian Høgsbergcb406f12013-10-09 10:54:03 -07001472 send_modifiers_to_client_in_list(surface_client,
1473 &kbd->resource_list,
1474 serial,
1475 kbd);
1476
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001477 pointer->focus_client = pointer_client;
Neil Roberts96d790e2013-09-19 17:32:00 +01001478
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001479 focus_resource_list = &pointer->focus_client->pointer_resources;
Neil Roberts96d790e2013-09-19 17:32:00 +01001480 wl_resource_for_each(resource, focus_resource_list) {
1481 wl_pointer_send_enter(resource,
1482 serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001483 view->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01001484 sx, sy);
Peter Hutterer87743e92016-01-18 16:38:22 +10001485 pointer_send_frame(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001486 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001487
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001488 pointer->focus_serial = serial;
1489 }
1490
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001491 wl_list_remove(&pointer->focus_view_listener.link);
1492 wl_list_init(&pointer->focus_view_listener.link);
1493 wl_list_remove(&pointer->focus_resource_listener.link);
1494 wl_list_init(&pointer->focus_resource_listener.link);
Emilio Pozuelo Monfortaa7a4762013-11-19 11:37:15 +01001495 if (view)
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001496 wl_signal_add(&view->destroy_signal, &pointer->focus_view_listener);
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001497 if (view && view->surface->resource)
1498 wl_resource_add_destroy_listener(view->surface->resource,
1499 &pointer->focus_resource_listener);
1500
1501 pointer->focus = view;
1502 pointer->focus_view_listener.notify = pointer_focus_view_destroyed;
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001503 pointer->sx = sx;
1504 pointer->sy = sy;
1505
Derek Foremanf9318d12015-05-11 15:40:11 -05001506 assert(view || sx == wl_fixed_from_int(-1000000));
1507 assert(view || sy == wl_fixed_from_int(-1000000));
1508
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001509 wl_signal_emit(&pointer->focus_signal, pointer);
1510}
1511
Neil Roberts96d790e2013-09-19 17:32:00 +01001512static void
1513send_enter_to_resource_list(struct wl_list *list,
1514 struct weston_keyboard *keyboard,
1515 struct weston_surface *surface,
1516 uint32_t serial)
1517{
1518 struct wl_resource *resource;
1519
1520 wl_resource_for_each(resource, list) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001521 wl_keyboard_send_enter(resource, serial,
1522 surface->resource,
1523 &keyboard->keys);
Kirill Chibisovc46c70d2020-06-17 01:43:57 +03001524 send_modifiers_to_resource(keyboard, resource, serial);
Neil Roberts96d790e2013-09-19 17:32:00 +01001525 }
1526}
1527
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001528WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001529weston_keyboard_set_focus(struct weston_keyboard *keyboard,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001530 struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001531{
Quentin Glidic85d55542017-07-21 14:02:40 +02001532 struct weston_seat *seat = keyboard->seat;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001533 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +01001534 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001535 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01001536 struct wl_list *focus_resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001537
Pekka Paalanen72e183b2018-02-22 16:55:15 +02001538 /* Keyboard focus on a surface without a client is equivalent to NULL
1539 * focus as nothing would react to the keyboard events anyway.
1540 * Just set focus to NULL instead - the destroy listener hangs on the
1541 * wl_resource anyway.
1542 */
1543 if (surface && !surface->resource)
1544 surface = NULL;
1545
Neil Roberts96d790e2013-09-19 17:32:00 +01001546 focus_resource_list = &keyboard->focus_resource_list;
1547
1548 if (!wl_list_empty(focus_resource_list) && keyboard->focus != surface) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001549 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001550 wl_resource_for_each(resource, focus_resource_list) {
1551 wl_keyboard_send_leave(resource, serial,
1552 keyboard->focus->resource);
1553 }
1554 move_resources(&keyboard->resource_list, focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001555 }
1556
Neil Roberts96d790e2013-09-19 17:32:00 +01001557 if (find_resource_for_surface(&keyboard->resource_list, surface) &&
1558 keyboard->focus != surface) {
1559 struct wl_client *surface_client =
1560 wl_resource_get_client(surface->resource);
1561
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001562 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001563
1564 move_resources_for_client(focus_resource_list,
1565 &keyboard->resource_list,
1566 surface_client);
1567 send_enter_to_resource_list(focus_resource_list,
1568 keyboard,
1569 surface,
1570 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001571 keyboard->focus_serial = serial;
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001572 }
1573
Quentin Glidic85d55542017-07-21 14:02:40 +02001574 if (seat->saved_kbd_focus) {
1575 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1576 seat->saved_kbd_focus = NULL;
1577 }
1578
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001579 wl_list_remove(&keyboard->focus_resource_listener.link);
1580 wl_list_init(&keyboard->focus_resource_listener.link);
Pekka Paalanen72e183b2018-02-22 16:55:15 +02001581 if (surface)
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001582 wl_resource_add_destroy_listener(surface->resource,
1583 &keyboard->focus_resource_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001584
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001585 keyboard->focus = surface;
1586 wl_signal_emit(&keyboard->focus_signal, keyboard);
1587}
1588
Giulio Camuffoa20ca812014-11-22 11:16:56 +02001589/* Users of this function must manually manage the keyboard focus */
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001590WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001591weston_keyboard_start_grab(struct weston_keyboard *keyboard,
1592 struct weston_keyboard_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001593{
1594 keyboard->grab = grab;
1595 grab->keyboard = keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001596}
1597
1598WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001599weston_keyboard_end_grab(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001600{
1601 keyboard->grab = &keyboard->default_grab;
1602}
1603
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001604static void
1605weston_keyboard_cancel_grab(struct weston_keyboard *keyboard)
1606{
1607 keyboard->grab->interface->cancel(keyboard->grab);
1608}
1609
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001610WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001611weston_pointer_start_grab(struct weston_pointer *pointer,
1612 struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001613{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001614 pointer->grab = grab;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001615 grab->pointer = pointer;
Kristian Høgsbergda751b82013-07-04 00:58:07 -04001616 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001617}
1618
1619WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001620weston_pointer_end_grab(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001621{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001622 pointer->grab = &pointer->default_grab;
Kristian Høgsbergda751b82013-07-04 00:58:07 -04001623 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001624}
1625
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001626static void
1627weston_pointer_cancel_grab(struct weston_pointer *pointer)
1628{
1629 pointer->grab->interface->cancel(pointer->grab);
1630}
1631
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001632WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -04001633weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001634{
1635 touch->grab = grab;
1636 grab->touch = touch;
1637}
1638
1639WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -04001640weston_touch_end_grab(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001641{
1642 touch->grab = &touch->default_grab;
1643}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001644
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001645static void
1646weston_touch_cancel_grab(struct weston_touch *touch)
1647{
1648 touch->grab->interface->cancel(touch->grab);
1649}
1650
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001651static void
1652weston_pointer_clamp_for_output(struct weston_pointer *pointer,
1653 struct weston_output *output,
1654 wl_fixed_t *fx, wl_fixed_t *fy)
1655{
1656 int x, y;
1657
1658 x = wl_fixed_to_int(*fx);
1659 y = wl_fixed_to_int(*fy);
1660
1661 if (x < output->x)
1662 *fx = wl_fixed_from_int(output->x);
1663 else if (x >= output->x + output->width)
1664 *fx = wl_fixed_from_int(output->x +
1665 output->width - 1);
1666 if (y < output->y)
1667 *fy = wl_fixed_from_int(output->y);
1668 else if (y >= output->y + output->height)
1669 *fy = wl_fixed_from_int(output->y +
1670 output->height - 1);
1671}
1672
Rob Bradford806d8c02013-06-25 18:56:41 +01001673WL_EXPORT void
1674weston_pointer_clamp(struct weston_pointer *pointer, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001675{
Rob Bradford806d8c02013-06-25 18:56:41 +01001676 struct weston_compositor *ec = pointer->seat->compositor;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001677 struct weston_output *output, *prev = NULL;
1678 int x, y, old_x, old_y, valid = 0;
1679
1680 x = wl_fixed_to_int(*fx);
1681 y = wl_fixed_to_int(*fy);
Rob Bradford806d8c02013-06-25 18:56:41 +01001682 old_x = wl_fixed_to_int(pointer->x);
1683 old_y = wl_fixed_to_int(pointer->y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001684
1685 wl_list_for_each(output, &ec->output_list, link) {
Rob Bradford66bd9f52013-06-25 18:56:42 +01001686 if (pointer->seat->output && pointer->seat->output != output)
1687 continue;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001688 if (pixman_region32_contains_point(&output->region,
1689 x, y, NULL))
1690 valid = 1;
1691 if (pixman_region32_contains_point(&output->region,
1692 old_x, old_y, NULL))
1693 prev = output;
1694 }
1695
Rob Bradford66bd9f52013-06-25 18:56:42 +01001696 if (!prev)
1697 prev = pointer->seat->output;
1698
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001699 if (prev && !valid)
1700 weston_pointer_clamp_for_output(pointer, prev, fx, fy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001701}
1702
Jonas Ådahld2510102014-10-05 21:39:14 +02001703static void
1704weston_pointer_move_to(struct weston_pointer *pointer,
1705 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001706{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001707 int32_t ix, iy;
1708
Rob Bradford806d8c02013-06-25 18:56:41 +01001709 weston_pointer_clamp (pointer, &x, &y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001710
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001711 pointer->x = x;
1712 pointer->y = y;
1713
1714 ix = wl_fixed_to_int(x);
1715 iy = wl_fixed_to_int(y);
1716
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001717 if (pointer->sprite) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001718 weston_view_set_position(pointer->sprite,
1719 ix - pointer->hotspot_x,
1720 iy - pointer->hotspot_y);
1721 weston_view_schedule_repaint(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001722 }
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001723
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001724 pointer->grab->interface->focus(pointer->grab);
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001725 wl_signal_emit(&pointer->motion_signal, pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001726}
1727
Jonas Ådahld2510102014-10-05 21:39:14 +02001728WL_EXPORT void
Jonas Ådahld2510102014-10-05 21:39:14 +02001729weston_pointer_move(struct weston_pointer *pointer,
1730 struct weston_pointer_motion_event *event)
1731{
1732 wl_fixed_t x, y;
1733
1734 weston_pointer_motion_to_abs(pointer, event, &x, &y);
1735 weston_pointer_move_to(pointer, x, y);
1736}
1737
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001738/** Verify if the pointer is in a valid position and move it if it isn't.
1739 */
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001740static void
1741weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001742{
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001743 struct weston_pointer *pointer;
1744 struct weston_compositor *ec;
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001745 struct weston_output *output, *closest = NULL;
1746 int x, y, distance, min = INT_MAX;
1747 wl_fixed_t fx, fy;
1748
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001749 pointer = container_of(listener, struct weston_pointer,
1750 output_destroy_listener);
1751 ec = pointer->seat->compositor;
1752
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001753 x = wl_fixed_to_int(pointer->x);
1754 y = wl_fixed_to_int(pointer->y);
1755
1756 wl_list_for_each(output, &ec->output_list, link) {
1757 if (pixman_region32_contains_point(&output->region,
1758 x, y, NULL))
1759 return;
1760
1761 /* Aproximante the distance from the pointer to the center of
1762 * the output. */
1763 distance = abs(output->x + output->width / 2 - x) +
1764 abs(output->y + output->height / 2 - y);
1765 if (distance < min) {
1766 min = distance;
1767 closest = output;
1768 }
1769 }
1770
1771 /* Nothing to do if there's no output left. */
1772 if (!closest)
1773 return;
1774
1775 fx = pointer->x;
1776 fy = pointer->y;
1777
1778 weston_pointer_clamp_for_output(pointer, closest, &fx, &fy);
Jonas Ådahld2510102014-10-05 21:39:14 +02001779 weston_pointer_move_to(pointer, fx, fy);
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001780}
1781
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001782WL_EXPORT void
1783notify_motion(struct weston_seat *seat,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001784 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001785 struct weston_pointer_motion_event *event)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001786{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001787 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001788 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001789
1790 weston_compositor_wake(ec);
Jonas Ådahld2510102014-10-05 21:39:14 +02001791 pointer->grab->interface->motion(pointer->grab, time, event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001792}
1793
Daniel Stone96d47c02013-11-19 11:37:12 +01001794static void
1795run_modifier_bindings(struct weston_seat *seat, uint32_t old, uint32_t new)
1796{
1797 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001798 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Daniel Stone96d47c02013-11-19 11:37:12 +01001799 uint32_t diff;
1800 unsigned int i;
1801 struct {
1802 uint32_t xkb;
1803 enum weston_keyboard_modifier weston;
1804 } mods[] = {
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001805 { keyboard->xkb_info->ctrl_mod, MODIFIER_CTRL },
1806 { keyboard->xkb_info->alt_mod, MODIFIER_ALT },
1807 { keyboard->xkb_info->super_mod, MODIFIER_SUPER },
1808 { keyboard->xkb_info->shift_mod, MODIFIER_SHIFT },
Daniel Stone96d47c02013-11-19 11:37:12 +01001809 };
1810
1811 diff = new & ~old;
1812 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
1813 if (diff & (1 << mods[i].xkb))
1814 weston_compositor_run_modifier_binding(compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001815 keyboard,
Daniel Stone96d47c02013-11-19 11:37:12 +01001816 mods[i].weston,
1817 WL_KEYBOARD_KEY_STATE_PRESSED);
1818 }
1819
1820 diff = old & ~new;
1821 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
1822 if (diff & (1 << mods[i].xkb))
1823 weston_compositor_run_modifier_binding(compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001824 keyboard,
Daniel Stone96d47c02013-11-19 11:37:12 +01001825 mods[i].weston,
1826 WL_KEYBOARD_KEY_STATE_RELEASED);
1827 }
1828}
1829
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001830WL_EXPORT void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001831notify_motion_absolute(struct weston_seat *seat, const struct timespec *time,
1832 double x, double y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001833{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001834 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001835 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Jonas Ådahld2510102014-10-05 21:39:14 +02001836 struct weston_pointer_motion_event event = { 0 };
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001837
1838 weston_compositor_wake(ec);
Jonas Ådahld2510102014-10-05 21:39:14 +02001839
1840 event = (struct weston_pointer_motion_event) {
1841 .mask = WESTON_POINTER_MOTION_ABS,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001842 .x = x,
1843 .y = y,
Jonas Ådahld2510102014-10-05 21:39:14 +02001844 };
1845
1846 pointer->grab->interface->motion(pointer->grab, time, &event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001847}
1848
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02001849static unsigned int
1850peek_next_activate_serial(struct weston_compositor *c)
1851{
1852 unsigned serial = c->activate_serial + 1;
1853
1854 return serial == 0 ? 1 : serial;
1855}
1856
1857static void
1858inc_activate_serial(struct weston_compositor *c)
1859{
1860 c->activate_serial = peek_next_activate_serial (c);
1861}
1862
1863WL_EXPORT void
1864weston_view_activate(struct weston_view *view,
1865 struct weston_seat *seat,
1866 uint32_t flags)
1867{
1868 struct weston_compositor *compositor = seat->compositor;
1869
1870 if (flags & WESTON_ACTIVATE_FLAG_CLICKED) {
1871 view->click_to_activate_serial =
1872 peek_next_activate_serial(compositor);
1873 }
1874
1875 weston_seat_set_keyboard_focus(seat, view->surface);
1876}
1877
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001878WL_EXPORT void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001879notify_button(struct weston_seat *seat, const struct timespec *time,
1880 int32_t button, enum wl_pointer_button_state state)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001881{
1882 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001883 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001884
1885 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001886 weston_compositor_idle_inhibit(compositor);
1887 if (pointer->button_count == 0) {
1888 pointer->grab_button = button;
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001889 pointer->grab_time = *time;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001890 pointer->grab_x = pointer->x;
1891 pointer->grab_y = pointer->y;
1892 }
1893 pointer->button_count++;
1894 } else {
1895 weston_compositor_idle_release(compositor);
1896 pointer->button_count--;
1897 }
1898
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001899 weston_compositor_run_button_binding(compositor, pointer, time, button,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001900 state);
1901
1902 pointer->grab->interface->button(pointer->grab, time, button, state);
1903
1904 if (pointer->button_count == 1)
1905 pointer->grab_serial =
1906 wl_display_get_serial(compositor->wl_display);
1907}
1908
1909WL_EXPORT void
Alexandros Frantzis80321942017-11-16 18:20:56 +02001910notify_axis(struct weston_seat *seat, const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001911 struct weston_pointer_axis_event *event)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001912{
1913 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001914 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001915
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001916 weston_compositor_wake(compositor);
1917
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001918 if (weston_compositor_run_axis_binding(compositor, pointer,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001919 time, event))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001920 return;
1921
Peter Hutterer89b6a492016-01-18 15:58:17 +10001922 pointer->grab->interface->axis(pointer->grab, time, event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001923}
1924
Peter Hutterer87743e92016-01-18 16:38:22 +10001925WL_EXPORT void
1926notify_axis_source(struct weston_seat *seat, uint32_t source)
1927{
1928 struct weston_compositor *compositor = seat->compositor;
1929 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1930
1931 weston_compositor_wake(compositor);
1932
1933 pointer->grab->interface->axis_source(pointer->grab, source);
1934}
1935
1936WL_EXPORT void
1937notify_pointer_frame(struct weston_seat *seat)
1938{
1939 struct weston_compositor *compositor = seat->compositor;
1940 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1941
1942 weston_compositor_wake(compositor);
1943
1944 pointer->grab->interface->frame(pointer->grab);
1945}
1946
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001947WL_EXPORT int
1948weston_keyboard_set_locks(struct weston_keyboard *keyboard,
1949 uint32_t mask, uint32_t value)
1950{
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001951 uint32_t serial;
1952 xkb_mod_mask_t mods_depressed, mods_latched, mods_locked, group;
1953 xkb_mod_mask_t num, caps;
1954
1955 /* We don't want the leds to go out of sync with the actual state
1956 * so if the backend has no way to change the leds don't try to
1957 * change the state */
1958 if (!keyboard->seat->led_update)
1959 return -1;
1960
1961 mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
1962 XKB_STATE_DEPRESSED);
1963 mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
1964 XKB_STATE_LATCHED);
1965 mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
1966 XKB_STATE_LOCKED);
1967 group = xkb_state_serialize_group(keyboard->xkb_state.state,
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02001968 XKB_STATE_EFFECTIVE);
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001969
1970 num = (1 << keyboard->xkb_info->mod2_mod);
1971 caps = (1 << keyboard->xkb_info->caps_mod);
1972 if (mask & WESTON_NUM_LOCK) {
1973 if (value & WESTON_NUM_LOCK)
1974 mods_locked |= num;
1975 else
1976 mods_locked &= ~num;
1977 }
1978 if (mask & WESTON_CAPS_LOCK) {
1979 if (value & WESTON_CAPS_LOCK)
1980 mods_locked |= caps;
1981 else
1982 mods_locked &= ~caps;
1983 }
1984
1985 xkb_state_update_mask(keyboard->xkb_state.state, mods_depressed,
1986 mods_latched, mods_locked, 0, 0, group);
1987
1988 serial = wl_display_next_serial(
1989 keyboard->seat->compositor->wl_display);
1990 notify_modifiers(keyboard->seat, serial);
1991
1992 return 0;
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001993}
1994
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001995WL_EXPORT void
1996notify_modifiers(struct weston_seat *seat, uint32_t serial)
1997{
Derek Foreman1281a362015-07-31 16:55:32 -05001998 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001999 struct weston_keyboard_grab *grab = keyboard->grab;
2000 uint32_t mods_depressed, mods_latched, mods_locked, group;
2001 uint32_t mods_lookup;
2002 enum weston_led leds = 0;
2003 int changed = 0;
2004
2005 /* Serialize and update our internal state, checking to see if it's
2006 * different to the previous state. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002007 mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03002008 XKB_STATE_MODS_DEPRESSED);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002009 mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03002010 XKB_STATE_MODS_LATCHED);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002011 mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03002012 XKB_STATE_MODS_LOCKED);
2013 group = xkb_state_serialize_layout(keyboard->xkb_state.state,
2014 XKB_STATE_LAYOUT_EFFECTIVE);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002015
Derek Foreman244e99e2015-06-03 15:53:26 -05002016 if (mods_depressed != keyboard->modifiers.mods_depressed ||
2017 mods_latched != keyboard->modifiers.mods_latched ||
2018 mods_locked != keyboard->modifiers.mods_locked ||
2019 group != keyboard->modifiers.group)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002020 changed = 1;
2021
Derek Foreman244e99e2015-06-03 15:53:26 -05002022 run_modifier_bindings(seat, keyboard->modifiers.mods_depressed,
Daniel Stone96d47c02013-11-19 11:37:12 +01002023 mods_depressed);
2024
Derek Foreman244e99e2015-06-03 15:53:26 -05002025 keyboard->modifiers.mods_depressed = mods_depressed;
2026 keyboard->modifiers.mods_latched = mods_latched;
2027 keyboard->modifiers.mods_locked = mods_locked;
2028 keyboard->modifiers.group = group;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002029
2030 /* And update the modifier_state for bindings. */
2031 mods_lookup = mods_depressed | mods_latched;
2032 seat->modifier_state = 0;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002033 if (mods_lookup & (1 << keyboard->xkb_info->ctrl_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002034 seat->modifier_state |= MODIFIER_CTRL;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002035 if (mods_lookup & (1 << keyboard->xkb_info->alt_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002036 seat->modifier_state |= MODIFIER_ALT;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002037 if (mods_lookup & (1 << keyboard->xkb_info->super_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002038 seat->modifier_state |= MODIFIER_SUPER;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002039 if (mods_lookup & (1 << keyboard->xkb_info->shift_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002040 seat->modifier_state |= MODIFIER_SHIFT;
2041
2042 /* Finally, notify the compositor that LEDs have changed. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002043 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
2044 keyboard->xkb_info->num_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002045 leds |= LED_NUM_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002046 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
2047 keyboard->xkb_info->caps_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002048 leds |= LED_CAPS_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002049 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
2050 keyboard->xkb_info->scroll_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002051 leds |= LED_SCROLL_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002052 if (leds != keyboard->xkb_state.leds && seat->led_update)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002053 seat->led_update(seat, leds);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002054 keyboard->xkb_state.leds = leds;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002055
2056 if (changed) {
2057 grab->interface->modifiers(grab,
2058 serial,
2059 keyboard->modifiers.mods_depressed,
2060 keyboard->modifiers.mods_latched,
2061 keyboard->modifiers.mods_locked,
2062 keyboard->modifiers.group);
2063 }
2064}
2065
2066static void
2067update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
2068 enum wl_keyboard_key_state state)
2069{
Derek Foreman1281a362015-07-31 16:55:32 -05002070 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002071 enum xkb_key_direction direction;
2072
2073 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2074 direction = XKB_KEY_DOWN;
2075 else
2076 direction = XKB_KEY_UP;
2077
2078 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
2079 * broken keycode system, which starts at 8. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002080 xkb_state_update_key(keyboard->xkb_state.state, key + 8, direction);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002081
2082 notify_modifiers(seat, serial);
2083}
Rui Matos65196bc2013-10-10 19:44:19 +02002084
Derek Foremanf8f7fd62017-06-28 11:41:43 -05002085WL_EXPORT void
2086weston_keyboard_send_keymap(struct weston_keyboard *kbd, struct wl_resource *resource)
Rui Matos65196bc2013-10-10 19:44:19 +02002087{
Derek Foremanf8f7fd62017-06-28 11:41:43 -05002088 struct weston_xkb_info *xkb_info = kbd->xkb_info;
Derek Foreman76829fc2017-06-28 12:17:46 -05002089 int fd;
Sebastian Wickabec5122019-11-01 02:38:45 +01002090 size_t size;
2091 enum ro_anonymous_file_mapmode mapmode;
Derek Foremanf8f7fd62017-06-28 11:41:43 -05002092
Sebastian Wickabec5122019-11-01 02:38:45 +01002093 if (wl_resource_get_version(resource) < 7)
2094 mapmode = RO_ANONYMOUS_FILE_MAPMODE_SHARED;
2095 else
2096 mapmode = RO_ANONYMOUS_FILE_MAPMODE_PRIVATE;
2097
2098 fd = os_ro_anonymous_file_get_fd(xkb_info->keymap_rofile, mapmode);
2099 size = os_ro_anonymous_file_size(xkb_info->keymap_rofile);
2100
2101 if (fd == -1) {
2102 weston_log("creating a keymap file failed: %s\n",
Antonio Borneo39578632019-04-26 23:57:31 +02002103 strerror(errno));
Derek Foreman76829fc2017-06-28 12:17:46 -05002104 return;
2105 }
2106
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,
Sebastian Wickabec5122019-11-01 02:38:45 +01002110 size);
2111
2112 os_ro_anonymous_file_put_fd(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
Alexandros Frantzis28d66342021-06-09 10:56:26 +03002752 if (surface->width != 0) {
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
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02002786 cr = wl_resource_create(client, &wl_pointer_interface,
Jason Ekstranda85118c2013-06-27 20:17:02 -05002787 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
2851static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002852seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2853 uint32_t id)
2854{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002855 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002856 /* We use the keyboard_state directly, which means we'll
2857 * give a wl_keyboard if the seat has ever had one - even though
2858 * the spec explicitly states that this request only takes effect
2859 * if the seat has the keyboard capability.
2860 *
2861 * This prevents a race between the compositor sending new
2862 * capabilities and the client trying to use the old ones.
2863 */
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002864 struct weston_keyboard *keyboard = seat ? seat->keyboard_state : NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002865 struct wl_resource *cr;
2866
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02002867 cr = wl_resource_create(client, &wl_keyboard_interface,
Jason Ekstranda85118c2013-06-27 20:17:02 -05002868 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002869 if (cr == NULL) {
2870 wl_client_post_no_memory(client);
2871 return;
2872 }
2873
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002874 wl_list_init(wl_resource_get_link(cr));
2875 wl_resource_set_implementation(cr, &keyboard_interface,
Alexandros Frantzis2b442482018-02-20 14:05:50 +02002876 keyboard, destroy_keyboard_resource);
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002877
2878 /* If we don't have a keyboard_state, the resource is inert, so there
2879 * is nothing more to set up */
2880 if (!keyboard)
2881 return;
2882
Neil Roberts96d790e2013-09-19 17:32:00 +01002883 /* May be moved to focused list later by either
2884 * weston_keyboard_set_focus or directly if this client is already
2885 * focused */
Derek Foreman345c9f32015-06-03 15:53:28 -05002886 wl_list_insert(&keyboard->resource_list, wl_resource_get_link(cr));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002887
Jonny Lamb66a41a02014-08-12 14:58:25 +02002888 if (wl_resource_get_version(cr) >= WL_KEYBOARD_REPEAT_INFO_SINCE_VERSION) {
2889 wl_keyboard_send_repeat_info(cr,
2890 seat->compositor->kb_repeat_rate,
2891 seat->compositor->kb_repeat_delay);
2892 }
Jasper St. Pierred8c6aeb2014-08-04 13:43:24 -04002893
Derek Foremanf8f7fd62017-06-28 11:41:43 -05002894 weston_keyboard_send_keymap(keyboard, cr);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002895
Derek Foreman345c9f32015-06-03 15:53:28 -05002896 if (keyboard->focus && keyboard->focus->resource &&
2897 wl_resource_get_client(keyboard->focus->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01002898 struct weston_surface *surface =
Derek Foreman345c9f32015-06-03 15:53:28 -05002899 (struct weston_surface *)keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +01002900
2901 wl_list_remove(wl_resource_get_link(cr));
Derek Foreman345c9f32015-06-03 15:53:28 -05002902 wl_list_insert(&keyboard->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002903 wl_resource_get_link(cr));
2904 wl_keyboard_send_enter(cr,
Derek Foreman345c9f32015-06-03 15:53:28 -05002905 keyboard->focus_serial,
Neil Roberts96d790e2013-09-19 17:32:00 +01002906 surface->resource,
Derek Foreman345c9f32015-06-03 15:53:28 -05002907 &keyboard->keys);
Neil Roberts96d790e2013-09-19 17:32:00 +01002908
Kirill Chibisovc46c70d2020-06-17 01:43:57 +03002909 send_modifiers_to_resource(keyboard,
2910 cr,
2911 keyboard->focus_serial);
2912
Neil Roberts96d790e2013-09-19 17:32:00 +01002913 /* If this is the first keyboard resource for this
2914 * client... */
Derek Foreman345c9f32015-06-03 15:53:28 -05002915 if (keyboard->focus_resource_list.prev ==
Neil Roberts96d790e2013-09-19 17:32:00 +01002916 wl_resource_get_link(cr))
2917 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002918 }
2919}
2920
2921static void
Alexandros Frantzisd7157842018-02-20 14:07:03 +02002922destroy_touch_resource(struct wl_resource *resource)
2923{
2924 struct weston_touch *touch = wl_resource_get_user_data(resource);
2925
2926 wl_list_remove(wl_resource_get_link(resource));
2927
2928 if (touch) {
2929 remove_input_resource_from_timestamps(resource,
2930 &touch->timestamps_list);
2931 }
2932}
2933
2934static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002935touch_release(struct wl_client *client, struct wl_resource *resource)
2936{
2937 wl_resource_destroy(resource);
2938}
2939
2940static const struct wl_touch_interface touch_interface = {
2941 touch_release
2942};
2943
2944static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002945seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2946 uint32_t id)
2947{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002948 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002949 /* We use the touch_state directly, which means we'll
2950 * give a wl_touch if the seat has ever had one - even though
2951 * the spec explicitly states that this request only takes effect
2952 * if the seat has the touch capability.
2953 *
2954 * This prevents a race between the compositor sending new
2955 * capabilities and the client trying to use the old ones.
2956 */
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002957 struct weston_touch *touch = seat ? seat->touch_state : NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002958 struct wl_resource *cr;
2959
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02002960 cr = wl_resource_create(client, &wl_touch_interface,
Jason Ekstranda85118c2013-06-27 20:17:02 -05002961 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002962 if (cr == NULL) {
2963 wl_client_post_no_memory(client);
2964 return;
2965 }
2966
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002967 wl_list_init(wl_resource_get_link(cr));
2968 wl_resource_set_implementation(cr, &touch_interface,
Alexandros Frantzisd7157842018-02-20 14:07:03 +02002969 touch, destroy_touch_resource);
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002970
2971 /* If we don't have a touch_state, the resource is inert, so there
2972 * is nothing more to set up */
2973 if (!touch)
2974 return;
2975
Derek Foreman1281a362015-07-31 16:55:32 -05002976 if (touch->focus &&
2977 wl_resource_get_client(touch->focus->surface->resource) == client) {
Chokshi, Mituld6697142015-10-09 08:28:47 +00002978 wl_list_insert(&touch->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002979 wl_resource_get_link(cr));
2980 } else {
Chokshi, Mituld6697142015-10-09 08:28:47 +00002981 wl_list_insert(&touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002982 wl_resource_get_link(cr));
2983 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002984}
2985
Quentin Glidicaab1d362016-03-13 17:49:08 +01002986static void
2987seat_release(struct wl_client *client, struct wl_resource *resource)
2988{
2989 wl_resource_destroy(resource);
2990}
2991
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002992static const struct wl_seat_interface seat_interface = {
2993 seat_get_pointer,
2994 seat_get_keyboard,
2995 seat_get_touch,
Quentin Glidicaab1d362016-03-13 17:49:08 +01002996 seat_release,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002997};
2998
2999static void
3000bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3001{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003002 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003003 struct wl_resource *resource;
3004 enum wl_seat_capability caps = 0;
3005
Jason Ekstranda85118c2013-06-27 20:17:02 -05003006 resource = wl_resource_create(client,
Derek Foreman1909c102015-11-26 14:17:47 -06003007 &wl_seat_interface, version, id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05003008 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003009 wl_resource_set_implementation(resource, &seat_interface, data,
3010 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003011
Derek Foreman1281a362015-07-31 16:55:32 -05003012 if (weston_seat_get_pointer(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003013 caps |= WL_SEAT_CAPABILITY_POINTER;
Derek Foreman1281a362015-07-31 16:55:32 -05003014 if (weston_seat_get_keyboard(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003015 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Derek Foreman1281a362015-07-31 16:55:32 -05003016 if (weston_seat_get_touch(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003017 caps |= WL_SEAT_CAPABILITY_TOUCH;
3018
3019 wl_seat_send_capabilities(resource, caps);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003020 if (version >= WL_SEAT_NAME_SINCE_VERSION)
Rob Bradforde445ae62013-05-31 18:09:51 +01003021 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003022}
3023
Jonas Ådahl30d61d82014-10-22 21:21:17 +02003024static void
3025relative_pointer_destroy(struct wl_client *client,
3026 struct wl_resource *resource)
3027{
3028 wl_resource_destroy(resource);
3029}
3030
3031static const struct zwp_relative_pointer_v1_interface relative_pointer_interface = {
3032 relative_pointer_destroy
3033};
3034
3035static void
3036relative_pointer_manager_destroy(struct wl_client *client,
3037 struct wl_resource *resource)
3038{
3039 wl_resource_destroy(resource);
3040}
3041
3042static void
3043relative_pointer_manager_get_relative_pointer(struct wl_client *client,
3044 struct wl_resource *resource,
3045 uint32_t id,
3046 struct wl_resource *pointer_resource)
3047{
3048 struct weston_pointer *pointer =
3049 wl_resource_get_user_data(pointer_resource);
3050 struct weston_pointer_client *pointer_client;
3051 struct wl_resource *cr;
3052
3053 cr = wl_resource_create(client, &zwp_relative_pointer_v1_interface,
3054 wl_resource_get_version(resource), id);
3055 if (cr == NULL) {
3056 wl_client_post_no_memory(client);
3057 return;
3058 }
3059
3060 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
3061 if (!pointer_client) {
3062 wl_client_post_no_memory(client);
3063 return;
3064 }
3065
3066 wl_list_insert(&pointer_client->relative_pointer_resources,
3067 wl_resource_get_link(cr));
3068 wl_resource_set_implementation(cr, &relative_pointer_interface,
3069 pointer,
3070 unbind_pointer_client_resource);
3071}
3072
3073static const struct zwp_relative_pointer_manager_v1_interface relative_pointer_manager = {
3074 relative_pointer_manager_destroy,
3075 relative_pointer_manager_get_relative_pointer,
3076};
3077
3078static void
3079bind_relative_pointer_manager(struct wl_client *client, void *data,
3080 uint32_t version, uint32_t id)
3081{
3082 struct weston_compositor *compositor = data;
3083 struct wl_resource *resource;
3084
3085 resource = wl_resource_create(client,
3086 &zwp_relative_pointer_manager_v1_interface,
3087 1, id);
3088
3089 wl_resource_set_implementation(resource, &relative_pointer_manager,
3090 compositor,
3091 NULL);
3092}
3093
Giulio Camuffo0358af42016-06-02 21:48:08 +03003094WL_EXPORT int
3095weston_compositor_set_xkb_rule_names(struct weston_compositor *ec,
3096 struct xkb_rule_names *names)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003097{
3098 if (ec->xkb_context == NULL) {
Peter Hutterera2086bb2020-05-13 15:24:27 +10003099 ec->xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003100 if (ec->xkb_context == NULL) {
3101 weston_log("failed to create XKB context\n");
3102 return -1;
3103 }
3104 }
3105
3106 if (names)
3107 ec->xkb_names = *names;
3108 if (!ec->xkb_names.rules)
3109 ec->xkb_names.rules = strdup("evdev");
3110 if (!ec->xkb_names.model)
3111 ec->xkb_names.model = strdup("pc105");
3112 if (!ec->xkb_names.layout)
3113 ec->xkb_names.layout = strdup("us");
3114
3115 return 0;
3116}
3117
Stefan Schmidtfda26522013-09-17 10:54:09 +01003118static void
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003119weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003120{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003121 if (--xkb_info->ref_count > 0)
3122 return;
3123
Ran Benitac9c74152014-08-19 23:59:52 +03003124 xkb_keymap_unref(xkb_info->keymap);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003125
Sebastian Wickabec5122019-11-01 02:38:45 +01003126 os_ro_anonymous_file_destroy(xkb_info->keymap_rofile);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003127 free(xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003128}
3129
3130void
3131weston_compositor_xkb_destroy(struct weston_compositor *ec)
3132{
3133 free((char *) ec->xkb_names.rules);
3134 free((char *) ec->xkb_names.model);
3135 free((char *) ec->xkb_names.layout);
3136 free((char *) ec->xkb_names.variant);
3137 free((char *) ec->xkb_names.options);
Stefan Schmidtfda26522013-09-17 10:54:09 +01003138
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003139 if (ec->xkb_info)
3140 weston_xkb_info_destroy(ec->xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003141 xkb_context_unref(ec->xkb_context);
3142}
3143
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003144static struct weston_xkb_info *
3145weston_xkb_info_create(struct xkb_keymap *keymap)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003146{
Sebastian Wickabec5122019-11-01 02:38:45 +01003147 char *keymap_string;
3148 size_t keymap_size;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003149 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
3150 if (xkb_info == NULL)
3151 return NULL;
3152
Ran Benita2e1968f2014-08-19 23:59:51 +03003153 xkb_info->keymap = xkb_keymap_ref(keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003154 xkb_info->ref_count = 1;
3155
Ran Benita2e1968f2014-08-19 23:59:51 +03003156 xkb_info->shift_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3157 XKB_MOD_NAME_SHIFT);
3158 xkb_info->caps_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3159 XKB_MOD_NAME_CAPS);
3160 xkb_info->ctrl_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3161 XKB_MOD_NAME_CTRL);
3162 xkb_info->alt_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3163 XKB_MOD_NAME_ALT);
3164 xkb_info->mod2_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3165 "Mod2");
3166 xkb_info->mod3_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3167 "Mod3");
3168 xkb_info->super_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3169 XKB_MOD_NAME_LOGO);
3170 xkb_info->mod5_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3171 "Mod5");
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003172
Ran Benita2e1968f2014-08-19 23:59:51 +03003173 xkb_info->num_led = xkb_keymap_led_get_index(xkb_info->keymap,
3174 XKB_LED_NAME_NUM);
3175 xkb_info->caps_led = xkb_keymap_led_get_index(xkb_info->keymap,
3176 XKB_LED_NAME_CAPS);
3177 xkb_info->scroll_led = xkb_keymap_led_get_index(xkb_info->keymap,
3178 XKB_LED_NAME_SCROLL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003179
Sebastian Wickabec5122019-11-01 02:38:45 +01003180 keymap_string = xkb_keymap_get_as_string(xkb_info->keymap,
Derek Foreman76829fc2017-06-28 12:17:46 -05003181 XKB_KEYMAP_FORMAT_TEXT_V1);
Sebastian Wickabec5122019-11-01 02:38:45 +01003182 if (keymap_string == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003183 weston_log("failed to get string version of keymap\n");
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003184 goto err_keymap;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003185 }
Sebastian Wickabec5122019-11-01 02:38:45 +01003186 keymap_size = strlen(keymap_string) + 1;
3187
3188 xkb_info->keymap_rofile = os_ro_anonymous_file_create(keymap_size,
3189 keymap_string);
3190 free(keymap_string);
3191
3192 if (!xkb_info->keymap_rofile) {
3193 weston_log("failed to create anonymous file for keymap\n");
3194 goto err_keymap;
3195 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003196
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003197 return xkb_info;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003198
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003199err_keymap:
Ran Benita2e1968f2014-08-19 23:59:51 +03003200 xkb_keymap_unref(xkb_info->keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003201 free(xkb_info);
3202 return NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003203}
3204
3205static int
3206weston_compositor_build_global_keymap(struct weston_compositor *ec)
3207{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003208 struct xkb_keymap *keymap;
3209
3210 if (ec->xkb_info != NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003211 return 0;
3212
Ran Benita2e1968f2014-08-19 23:59:51 +03003213 keymap = xkb_keymap_new_from_names(ec->xkb_context,
3214 &ec->xkb_names,
3215 0);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003216 if (keymap == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003217 weston_log("failed to compile global XKB keymap\n");
3218 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
3219 "options %s\n",
3220 ec->xkb_names.rules, ec->xkb_names.model,
3221 ec->xkb_names.layout, ec->xkb_names.variant,
3222 ec->xkb_names.options);
3223 return -1;
3224 }
3225
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003226 ec->xkb_info = weston_xkb_info_create(keymap);
Rui Matos73d93952013-10-24 19:28:41 +02003227 xkb_keymap_unref(keymap);
Stefan Schmidtfda26522013-09-17 10:54:09 +01003228 if (ec->xkb_info == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003229 return -1;
3230
3231 return 0;
3232}
3233
Rui Matos65196bc2013-10-10 19:44:19 +02003234WL_EXPORT void
3235weston_seat_update_keymap(struct weston_seat *seat, struct xkb_keymap *keymap)
3236{
Derek Foreman1281a362015-07-31 16:55:32 -05003237 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
3238
3239 if (!keyboard || !keymap)
Rui Matos65196bc2013-10-10 19:44:19 +02003240 return;
3241
Derek Foreman1281a362015-07-31 16:55:32 -05003242 xkb_keymap_unref(keyboard->pending_keymap);
3243 keyboard->pending_keymap = xkb_keymap_ref(keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02003244
Derek Foreman1281a362015-07-31 16:55:32 -05003245 if (keyboard->keys.size == 0)
Rui Matos65196bc2013-10-10 19:44:19 +02003246 update_keymap(seat);
Rui Matos65196bc2013-10-10 19:44:19 +02003247}
3248
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003249WL_EXPORT int
3250weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
3251{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003252 struct weston_keyboard *keyboard;
3253
Derek Foreman1281a362015-07-31 16:55:32 -05003254 if (seat->keyboard_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003255 seat->keyboard_device_count += 1;
3256 if (seat->keyboard_device_count == 1)
3257 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003258 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003259 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003260
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003261 keyboard = weston_keyboard_create();
3262 if (keyboard == NULL) {
3263 weston_log("failed to allocate weston keyboard struct\n");
3264 return -1;
3265 }
3266
Derek Foreman185d1582017-06-28 11:17:23 -05003267 if (keymap != NULL) {
3268 keyboard->xkb_info = weston_xkb_info_create(keymap);
3269 if (keyboard->xkb_info == NULL)
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02003270 goto err;
Derek Foreman185d1582017-06-28 11:17:23 -05003271 } else {
3272 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
3273 goto err;
3274 keyboard->xkb_info = seat->compositor->xkb_info;
3275 keyboard->xkb_info->ref_count++;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01003276 }
Derek Foreman185d1582017-06-28 11:17:23 -05003277
3278 keyboard->xkb_state.state = xkb_state_new(keyboard->xkb_info->keymap);
3279 if (keyboard->xkb_state.state == NULL) {
3280 weston_log("failed to initialise XKB state\n");
3281 goto err;
3282 }
3283
3284 keyboard->xkb_state.leds = 0;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01003285
Derek Foreman1281a362015-07-31 16:55:32 -05003286 seat->keyboard_state = keyboard;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02003287 seat->keyboard_device_count = 1;
3288 keyboard->seat = seat;
3289
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003290 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003291
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003292 return 0;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02003293
3294err:
3295 if (keyboard->xkb_info)
3296 weston_xkb_info_destroy(keyboard->xkb_info);
3297 free(keyboard);
3298
3299 return -1;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003300}
3301
Jonas Ådahl91fed542013-12-03 09:14:27 +01003302static void
3303weston_keyboard_reset_state(struct weston_keyboard *keyboard)
3304{
3305 struct weston_seat *seat = keyboard->seat;
3306 struct xkb_state *state;
3307
Derek Foreman185d1582017-06-28 11:17:23 -05003308 state = xkb_state_new(keyboard->xkb_info->keymap);
3309 if (!state) {
3310 weston_log("failed to reset XKB state\n");
3311 return;
Jonas Ådahl91fed542013-12-03 09:14:27 +01003312 }
Derek Foreman185d1582017-06-28 11:17:23 -05003313 xkb_state_unref(keyboard->xkb_state.state);
3314 keyboard->xkb_state.state = state;
3315
3316 keyboard->xkb_state.leds = 0;
Jonas Ådahl91fed542013-12-03 09:14:27 +01003317
3318 seat->modifier_state = 0;
3319}
3320
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003321WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003322weston_seat_release_keyboard(struct weston_seat *seat)
3323{
3324 seat->keyboard_device_count--;
Derek Foremand621df22014-11-19 11:04:12 -06003325 assert(seat->keyboard_device_count >= 0);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003326 if (seat->keyboard_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05003327 weston_keyboard_set_focus(seat->keyboard_state, NULL);
3328 weston_keyboard_cancel_grab(seat->keyboard_state);
3329 weston_keyboard_reset_state(seat->keyboard_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003330 seat_send_updated_caps(seat);
3331 }
3332}
3333
Marius Vladeb34f822021-03-30 23:09:05 +03003334WL_EXPORT int
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003335weston_seat_init_pointer(struct weston_seat *seat)
3336{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003337 struct weston_pointer *pointer;
3338
Derek Foreman1281a362015-07-31 16:55:32 -05003339 if (seat->pointer_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003340 seat->pointer_device_count += 1;
3341 if (seat->pointer_device_count == 1)
3342 seat_send_updated_caps(seat);
Marius Vladeb34f822021-03-30 23:09:05 +03003343 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003344 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003345
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003346 pointer = weston_pointer_create(seat);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003347 if (pointer == NULL)
Marius Vladeb34f822021-03-30 23:09:05 +03003348 return -1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003349
Derek Foreman1281a362015-07-31 16:55:32 -05003350 seat->pointer_state = pointer;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003351 seat->pointer_device_count = 1;
3352 pointer->seat = seat;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003353
3354 seat_send_updated_caps(seat);
Marius Vladeb34f822021-03-30 23:09:05 +03003355
3356 return 0;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003357}
3358
3359WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003360weston_seat_release_pointer(struct weston_seat *seat)
3361{
Derek Foreman1281a362015-07-31 16:55:32 -05003362 struct weston_pointer *pointer = seat->pointer_state;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003363
3364 seat->pointer_device_count--;
3365 if (seat->pointer_device_count == 0) {
Derek Foremanf9318d12015-05-11 15:40:11 -05003366 weston_pointer_clear_focus(pointer);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003367 weston_pointer_cancel_grab(pointer);
Jonas Ådahl630bae82013-10-17 23:04:06 +02003368
Jonas Ådahla4932742013-10-17 23:04:07 +02003369 if (pointer->sprite)
3370 pointer_unmap_sprite(pointer);
3371
Jonas Ådahl3e12e632013-12-02 22:05:05 +01003372 weston_pointer_reset_state(pointer);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003373 seat_send_updated_caps(seat);
Derek Foremanfd5ca512015-01-07 15:00:25 -06003374
3375 /* seat->pointer is intentionally not destroyed so that
3376 * a newly attached pointer on this seat will retain
3377 * the previous cursor co-ordinates.
3378 */
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003379 }
3380}
3381
Marius Vladeb34f822021-03-30 23:09:05 +03003382WL_EXPORT int
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003383weston_seat_init_touch(struct weston_seat *seat)
3384{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003385 struct weston_touch *touch;
3386
Derek Foreman1281a362015-07-31 16:55:32 -05003387 if (seat->touch_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003388 seat->touch_device_count += 1;
3389 if (seat->touch_device_count == 1)
3390 seat_send_updated_caps(seat);
Marius Vladeb34f822021-03-30 23:09:05 +03003391 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003392 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003393
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003394 touch = weston_touch_create();
3395 if (touch == NULL)
Marius Vladeb34f822021-03-30 23:09:05 +03003396 return -1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003397
Derek Foreman1281a362015-07-31 16:55:32 -05003398 seat->touch_state = touch;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003399 seat->touch_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003400 touch->seat = seat;
3401
3402 seat_send_updated_caps(seat);
Marius Vladeb34f822021-03-30 23:09:05 +03003403
3404 return 0;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003405}
3406
3407WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003408weston_seat_release_touch(struct weston_seat *seat)
3409{
3410 seat->touch_device_count--;
3411 if (seat->touch_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05003412 weston_touch_set_focus(seat->touch_state, NULL);
3413 weston_touch_cancel_grab(seat->touch_state);
3414 weston_touch_reset_state(seat->touch_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003415 seat_send_updated_caps(seat);
3416 }
3417}
3418
3419WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003420weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
3421 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003422{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003423 memset(seat, 0, sizeof *seat);
3424
Kristian Høgsberge3148752013-05-06 23:19:49 -04003425 seat->selection_data_source = NULL;
3426 wl_list_init(&seat->base_resource_list);
3427 wl_signal_init(&seat->selection_signal);
3428 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04003429 wl_signal_init(&seat->destroy_signal);
Jason Ekstranda4ab5422014-04-02 19:53:45 -05003430 wl_signal_init(&seat->updated_caps_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003431
Sebastian Wickabec5122019-11-01 02:38:45 +01003432 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface,
3433 MIN(wl_seat_interface.version, 7),
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003434 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003435
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003436 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003437 seat->modifier_state = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003438 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003439
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003440 wl_list_insert(ec->seat_list.prev, &seat->link);
3441
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003442 clipboard_create(seat);
3443
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003444 wl_signal_emit(&ec->seat_created_signal, seat);
3445}
3446
3447WL_EXPORT void
3448weston_seat_release(struct weston_seat *seat)
3449{
Alexandros Frantzis8480d132018-02-15 13:07:09 +02003450 struct wl_resource *resource;
3451
3452 wl_resource_for_each(resource, &seat->base_resource_list) {
3453 wl_resource_set_user_data(resource, NULL);
3454 }
3455
3456 wl_resource_for_each(resource, &seat->drag_resource_list) {
3457 wl_resource_set_user_data(resource, NULL);
3458 }
3459
3460 wl_list_remove(&seat->base_resource_list);
3461 wl_list_remove(&seat->drag_resource_list);
3462
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003463 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003464
Jonas Ådahl1afb2382014-01-03 19:46:51 +01003465 if (seat->saved_kbd_focus)
3466 wl_list_remove(&seat->saved_kbd_focus_listener.link);
3467
Derek Foreman1281a362015-07-31 16:55:32 -05003468 if (seat->pointer_state)
3469 weston_pointer_destroy(seat->pointer_state);
3470 if (seat->keyboard_state)
3471 weston_keyboard_destroy(seat->keyboard_state);
3472 if (seat->touch_state)
3473 weston_touch_destroy(seat->touch_state);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003474
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003475 free (seat->seat_name);
3476
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003477 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04003478
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003479 wl_signal_emit(&seat->destroy_signal, seat);
3480}
Derek Foreman1281a362015-07-31 16:55:32 -05003481
3482/** Get a seat's keyboard pointer
3483 *
3484 * \param seat The seat to query
3485 * \return The seat's keyboard pointer, or NULL if no keyboard is present
3486 *
3487 * The keyboard pointer for a seat isn't freed when all keyboards are removed,
3488 * so it should only be used when the seat's keyboard_device_count is greater
3489 * than zero. This function does that test and only returns a pointer
3490 * when a keyboard is present.
3491 */
3492WL_EXPORT struct weston_keyboard *
3493weston_seat_get_keyboard(struct weston_seat *seat)
3494{
3495 if (!seat)
3496 return NULL;
3497
3498 if (seat->keyboard_device_count)
3499 return seat->keyboard_state;
3500
3501 return NULL;
3502}
3503
3504/** Get a seat's pointer pointer
3505 *
3506 * \param seat The seat to query
3507 * \return The seat's pointer pointer, or NULL if no pointer device is present
3508 *
3509 * The pointer pointer for a seat isn't freed when all mice are removed,
3510 * so it should only be used when the seat's pointer_device_count is greater
3511 * than zero. This function does that test and only returns a pointer
3512 * when a pointing device is present.
3513 */
3514WL_EXPORT struct weston_pointer *
3515weston_seat_get_pointer(struct weston_seat *seat)
3516{
3517 if (!seat)
3518 return NULL;
3519
3520 if (seat->pointer_device_count)
3521 return seat->pointer_state;
3522
3523 return NULL;
3524}
3525
Jonas Ådahld3414f22016-07-22 17:56:31 +08003526static const struct zwp_locked_pointer_v1_interface locked_pointer_interface;
3527static const struct zwp_confined_pointer_v1_interface confined_pointer_interface;
3528
3529static enum pointer_constraint_type
3530pointer_constraint_get_type(struct weston_pointer_constraint *constraint)
3531{
3532 if (wl_resource_instance_of(constraint->resource,
3533 &zwp_locked_pointer_v1_interface,
3534 &locked_pointer_interface)) {
3535 return POINTER_CONSTRAINT_TYPE_LOCK;
3536 } else if (wl_resource_instance_of(constraint->resource,
3537 &zwp_confined_pointer_v1_interface,
3538 &confined_pointer_interface)) {
3539 return POINTER_CONSTRAINT_TYPE_CONFINE;
3540 }
3541
3542 abort();
3543 return 0;
3544}
3545
3546static void
3547pointer_constraint_notify_activated(struct weston_pointer_constraint *constraint)
3548{
3549 struct wl_resource *resource = constraint->resource;
3550
3551 switch (pointer_constraint_get_type(constraint)) {
3552 case POINTER_CONSTRAINT_TYPE_LOCK:
3553 zwp_locked_pointer_v1_send_locked(resource);
3554 break;
3555 case POINTER_CONSTRAINT_TYPE_CONFINE:
3556 zwp_confined_pointer_v1_send_confined(resource);
3557 break;
3558 }
3559}
3560
3561static void
3562pointer_constraint_notify_deactivated(struct weston_pointer_constraint *constraint)
3563{
3564 struct wl_resource *resource = constraint->resource;
3565
3566 switch (pointer_constraint_get_type(constraint)) {
3567 case POINTER_CONSTRAINT_TYPE_LOCK:
3568 zwp_locked_pointer_v1_send_unlocked(resource);
3569 break;
3570 case POINTER_CONSTRAINT_TYPE_CONFINE:
3571 zwp_confined_pointer_v1_send_unconfined(resource);
3572 break;
3573 }
3574}
3575
3576static struct weston_pointer_constraint *
3577get_pointer_constraint_for_pointer(struct weston_surface *surface,
3578 struct weston_pointer *pointer)
3579{
3580 struct weston_pointer_constraint *constraint;
3581
3582 wl_list_for_each(constraint, &surface->pointer_constraints, link) {
3583 if (constraint->pointer == pointer)
3584 return constraint;
3585 }
3586
3587 return NULL;
3588}
3589
Derek Foreman1281a362015-07-31 16:55:32 -05003590/** Get a seat's touch pointer
3591 *
3592 * \param seat The seat to query
3593 * \return The seat's touch pointer, or NULL if no touch device is present
3594 *
3595 * The touch pointer for a seat isn't freed when all touch devices are removed,
3596 * so it should only be used when the seat's touch_device_count is greater
3597 * than zero. This function does that test and only returns a pointer
3598 * when a touch device is present.
3599 */
3600WL_EXPORT struct weston_touch *
3601weston_seat_get_touch(struct weston_seat *seat)
3602{
3603 if (!seat)
3604 return NULL;
3605
3606 if (seat->touch_device_count)
3607 return seat->touch_state;
3608
3609 return NULL;
3610}
Bryce Harrington24f917e2016-06-29 19:04:07 -07003611
3612/** Sets the keyboard focus to the given surface
3613 *
Marius Vlada2dace22019-06-12 16:05:44 +03003614 * \param surface the surface to focus on
Bryce Harrington24f917e2016-06-29 19:04:07 -07003615 * \param seat The seat to query
3616 */
3617WL_EXPORT void
3618weston_seat_set_keyboard_focus(struct weston_seat *seat,
3619 struct weston_surface *surface)
3620{
3621 struct weston_compositor *compositor = seat->compositor;
3622 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003623 struct weston_surface_activation_data activation_data;
Bryce Harrington24f917e2016-06-29 19:04:07 -07003624
Jonas Ådahlef8e1c32016-03-15 20:28:51 +08003625 if (keyboard && keyboard->focus != surface) {
Bryce Harrington24f917e2016-06-29 19:04:07 -07003626 weston_keyboard_set_focus(keyboard, surface);
3627 wl_data_device_set_keyboard_focus(seat);
3628 }
3629
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003630 inc_activate_serial(compositor);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003631
3632 activation_data = (struct weston_surface_activation_data) {
3633 .surface = surface,
3634 .seat = seat,
3635 };
3636 wl_signal_emit(&compositor->activate_signal, &activation_data);
Bryce Harrington24f917e2016-06-29 19:04:07 -07003637}
Jonas Ådahl30d61d82014-10-22 21:21:17 +02003638
Jonas Ådahld3414f22016-07-22 17:56:31 +08003639static void
3640enable_pointer_constraint(struct weston_pointer_constraint *constraint,
3641 struct weston_view *view)
3642{
3643 assert(constraint->view == NULL);
3644 constraint->view = view;
3645 pointer_constraint_notify_activated(constraint);
3646 weston_pointer_start_grab(constraint->pointer, &constraint->grab);
3647 wl_list_remove(&constraint->surface_destroy_listener.link);
3648 wl_list_init(&constraint->surface_destroy_listener.link);
3649}
3650
3651static bool
3652is_pointer_constraint_enabled(struct weston_pointer_constraint *constraint)
3653{
3654 return constraint->view != NULL;
3655}
3656
3657static void
3658weston_pointer_constraint_disable(struct weston_pointer_constraint *constraint)
3659{
3660 constraint->view = NULL;
3661 pointer_constraint_notify_deactivated(constraint);
3662 weston_pointer_end_grab(constraint->grab.pointer);
3663}
3664
3665void
3666weston_pointer_constraint_destroy(struct weston_pointer_constraint *constraint)
3667{
3668 if (is_pointer_constraint_enabled(constraint))
3669 weston_pointer_constraint_disable(constraint);
3670
3671 wl_list_remove(&constraint->pointer_destroy_listener.link);
3672 wl_list_remove(&constraint->surface_destroy_listener.link);
3673 wl_list_remove(&constraint->surface_commit_listener.link);
3674 wl_list_remove(&constraint->surface_activate_listener.link);
3675
3676 wl_resource_set_user_data(constraint->resource, NULL);
3677 pixman_region32_fini(&constraint->region);
3678 wl_list_remove(&constraint->link);
3679 free(constraint);
3680}
3681
3682static void
3683disable_pointer_constraint(struct weston_pointer_constraint *constraint)
3684{
3685 switch (constraint->lifetime) {
3686 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT:
3687 weston_pointer_constraint_destroy(constraint);
3688 break;
3689 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT:
3690 weston_pointer_constraint_disable(constraint);
3691 break;
3692 }
3693}
3694
3695static bool
3696is_within_constraint_region(struct weston_pointer_constraint *constraint,
3697 wl_fixed_t sx, wl_fixed_t sy)
3698{
3699 struct weston_surface *surface = constraint->surface;
3700 pixman_region32_t constraint_region;
3701 bool result;
3702
3703 pixman_region32_init(&constraint_region);
3704 pixman_region32_intersect(&constraint_region,
3705 &surface->input,
3706 &constraint->region);
3707 result = pixman_region32_contains_point(&constraint_region,
3708 wl_fixed_to_int(sx),
3709 wl_fixed_to_int(sy),
3710 NULL);
3711 pixman_region32_fini(&constraint_region);
3712
3713 return result;
3714}
3715
3716static void
3717maybe_enable_pointer_constraint(struct weston_pointer_constraint *constraint)
3718{
3719 struct weston_surface *surface = constraint->surface;
3720 struct weston_view *vit;
3721 struct weston_view *view = NULL;
3722 struct weston_pointer *pointer = constraint->pointer;
3723 struct weston_keyboard *keyboard;
3724 struct weston_seat *seat = pointer->seat;
3725 int32_t x, y;
3726
3727 /* Postpone if no view of the surface was most recently clicked. */
3728 wl_list_for_each(vit, &surface->views, surface_link) {
3729 if (vit->click_to_activate_serial ==
3730 surface->compositor->activate_serial) {
3731 view = vit;
3732 }
3733 }
3734 if (view == NULL)
3735 return;
3736
3737 /* Postpone if surface doesn't have keyboard focus. */
3738 keyboard = weston_seat_get_keyboard(seat);
3739 if (!keyboard || keyboard->focus != surface)
3740 return;
3741
3742 /* Postpone constraint if the pointer is not within the
3743 * constraint region.
3744 */
3745 weston_view_from_global(view,
3746 wl_fixed_to_int(pointer->x),
3747 wl_fixed_to_int(pointer->y),
3748 &x, &y);
3749 if (!is_within_constraint_region(constraint,
3750 wl_fixed_from_int(x),
3751 wl_fixed_from_int(y)))
3752 return;
3753
3754 enable_pointer_constraint(constraint, view);
3755}
3756
3757static void
3758locked_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
3759{
3760}
3761
3762static void
3763locked_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02003764 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003765 struct weston_pointer_motion_event *event)
3766{
Quentin Glidiccde13452016-08-12 10:41:32 +02003767 pointer_send_relative_motion(grab->pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08003768}
3769
3770static void
3771locked_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003772 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003773 uint32_t button,
3774 uint32_t state_w)
3775{
3776 weston_pointer_send_button(grab->pointer, time, button, state_w);
3777}
3778
3779static void
3780locked_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02003781 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003782 struct weston_pointer_axis_event *event)
3783{
3784 weston_pointer_send_axis(grab->pointer, time, event);
3785}
3786
3787static void
3788locked_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
3789 uint32_t source)
3790{
3791 weston_pointer_send_axis_source(grab->pointer, source);
3792}
3793
3794static void
3795locked_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
3796{
3797 weston_pointer_send_frame(grab->pointer);
3798}
3799
3800static void
3801locked_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
3802{
3803 struct weston_pointer_constraint *constraint =
3804 container_of(grab, struct weston_pointer_constraint, grab);
3805
3806 disable_pointer_constraint(constraint);
3807}
3808
3809static const struct weston_pointer_grab_interface
3810 locked_pointer_grab_interface = {
3811 locked_pointer_grab_pointer_focus,
3812 locked_pointer_grab_pointer_motion,
3813 locked_pointer_grab_pointer_button,
3814 locked_pointer_grab_pointer_axis,
3815 locked_pointer_grab_pointer_axis_source,
3816 locked_pointer_grab_pointer_frame,
3817 locked_pointer_grab_pointer_cancel,
3818};
3819
3820static void
3821pointer_constraint_constrain_resource_destroyed(struct wl_resource *resource)
3822{
3823 struct weston_pointer_constraint *constraint =
3824 wl_resource_get_user_data(resource);
3825
3826 if (!constraint)
3827 return;
3828
3829 weston_pointer_constraint_destroy(constraint);
3830}
3831
3832static void
3833pointer_constraint_surface_activate(struct wl_listener *listener, void *data)
3834{
3835 struct weston_surface_activation_data *activation = data;
3836 struct weston_pointer *pointer;
3837 struct weston_surface *focus = activation->surface;
3838 struct weston_pointer_constraint *constraint =
3839 container_of(listener, struct weston_pointer_constraint,
3840 surface_activate_listener);
3841 bool is_constraint_surface;
3842
3843 pointer = weston_seat_get_pointer(activation->seat);
3844 if (!pointer)
3845 return;
3846
3847 is_constraint_surface =
3848 get_pointer_constraint_for_pointer(focus, pointer) == constraint;
3849
3850 if (is_constraint_surface &&
3851 !is_pointer_constraint_enabled(constraint))
3852 maybe_enable_pointer_constraint(constraint);
3853 else if (!is_constraint_surface &&
3854 is_pointer_constraint_enabled(constraint))
3855 disable_pointer_constraint(constraint);
3856}
3857
3858static void
3859pointer_constraint_pointer_destroyed(struct wl_listener *listener, void *data)
3860{
3861 struct weston_pointer_constraint *constraint =
3862 container_of(listener, struct weston_pointer_constraint,
3863 pointer_destroy_listener);
3864
3865 weston_pointer_constraint_destroy(constraint);
3866}
3867
3868static void
3869pointer_constraint_surface_destroyed(struct wl_listener *listener, void *data)
3870{
3871 struct weston_pointer_constraint *constraint =
3872 container_of(listener, struct weston_pointer_constraint,
3873 surface_destroy_listener);
3874
3875 weston_pointer_constraint_destroy(constraint);
3876}
3877
3878static void
3879pointer_constraint_surface_committed(struct wl_listener *listener, void *data)
3880{
3881 struct weston_pointer_constraint *constraint =
3882 container_of(listener, struct weston_pointer_constraint,
3883 surface_commit_listener);
3884
3885 if (constraint->region_is_pending) {
3886 constraint->region_is_pending = false;
3887 pixman_region32_copy(&constraint->region,
3888 &constraint->region_pending);
3889 pixman_region32_fini(&constraint->region_pending);
3890 pixman_region32_init(&constraint->region_pending);
3891 }
3892
3893 if (constraint->hint_is_pending) {
3894 constraint->hint_is_pending = false;
3895
3896 constraint->hint_is_pending = true;
3897 constraint->hint_x = constraint->hint_x_pending;
3898 constraint->hint_y = constraint->hint_y_pending;
3899 }
3900
3901 if (pointer_constraint_get_type(constraint) ==
3902 POINTER_CONSTRAINT_TYPE_CONFINE &&
3903 is_pointer_constraint_enabled(constraint))
3904 maybe_warp_confined_pointer(constraint);
3905}
3906
3907static struct weston_pointer_constraint *
3908weston_pointer_constraint_create(struct weston_surface *surface,
3909 struct weston_pointer *pointer,
3910 struct weston_region *region,
3911 enum zwp_pointer_constraints_v1_lifetime lifetime,
3912 struct wl_resource *cr,
3913 const struct weston_pointer_grab_interface *grab_interface)
3914{
3915 struct weston_pointer_constraint *constraint;
3916
3917 constraint = zalloc(sizeof *constraint);
3918 if (!constraint)
3919 return NULL;
3920
3921 constraint->lifetime = lifetime;
3922 pixman_region32_init(&constraint->region);
3923 pixman_region32_init(&constraint->region_pending);
3924 wl_list_insert(&surface->pointer_constraints, &constraint->link);
3925 constraint->surface = surface;
3926 constraint->pointer = pointer;
3927 constraint->resource = cr;
3928 constraint->grab.interface = grab_interface;
3929 if (region) {
3930 pixman_region32_copy(&constraint->region,
3931 &region->region);
3932 } else {
3933 pixman_region32_fini(&constraint->region);
3934 region_init_infinite(&constraint->region);
3935 }
3936
3937 constraint->surface_activate_listener.notify =
3938 pointer_constraint_surface_activate;
3939 constraint->surface_destroy_listener.notify =
3940 pointer_constraint_surface_destroyed;
3941 constraint->surface_commit_listener.notify =
3942 pointer_constraint_surface_committed;
3943 constraint->pointer_destroy_listener.notify =
3944 pointer_constraint_pointer_destroyed;
3945
3946 wl_signal_add(&surface->compositor->activate_signal,
3947 &constraint->surface_activate_listener);
3948 wl_signal_add(&pointer->destroy_signal,
3949 &constraint->pointer_destroy_listener);
3950 wl_signal_add(&surface->destroy_signal,
3951 &constraint->surface_destroy_listener);
3952 wl_signal_add(&surface->commit_signal,
3953 &constraint->surface_commit_listener);
3954
3955 return constraint;
3956}
3957
3958static void
3959init_pointer_constraint(struct wl_resource *pointer_constraints_resource,
3960 uint32_t id,
3961 struct weston_surface *surface,
3962 struct weston_pointer *pointer,
3963 struct weston_region *region,
3964 enum zwp_pointer_constraints_v1_lifetime lifetime,
3965 const struct wl_interface *interface,
3966 const void *implementation,
3967 const struct weston_pointer_grab_interface *grab_interface)
3968{
3969 struct wl_client *client =
3970 wl_resource_get_client(pointer_constraints_resource);
3971 struct wl_resource *cr;
3972 struct weston_pointer_constraint *constraint;
3973
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02003974 if (pointer && get_pointer_constraint_for_pointer(surface, pointer)) {
Jonas Ådahld3414f22016-07-22 17:56:31 +08003975 wl_resource_post_error(pointer_constraints_resource,
3976 ZWP_POINTER_CONSTRAINTS_V1_ERROR_ALREADY_CONSTRAINED,
3977 "the pointer has a lock/confine request on this surface");
3978 return;
3979 }
3980
3981 cr = wl_resource_create(client, interface,
3982 wl_resource_get_version(pointer_constraints_resource),
3983 id);
3984 if (cr == NULL) {
3985 wl_client_post_no_memory(client);
3986 return;
3987 }
3988
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02003989 if (pointer) {
3990 constraint = weston_pointer_constraint_create(surface, pointer,
3991 region, lifetime,
3992 cr, grab_interface);
3993 if (constraint == NULL) {
3994 wl_client_post_no_memory(client);
3995 return;
3996 }
3997 } else {
3998 constraint = NULL;
Jonas Ådahld3414f22016-07-22 17:56:31 +08003999 }
4000
4001 wl_resource_set_implementation(cr, implementation, constraint,
4002 pointer_constraint_constrain_resource_destroyed);
4003
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02004004 if (constraint)
4005 maybe_enable_pointer_constraint(constraint);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004006}
4007
4008static void
4009pointer_constraints_destroy(struct wl_client *client,
4010 struct wl_resource *resource)
4011{
4012 wl_resource_destroy(resource);
4013}
4014
4015static void
4016locked_pointer_destroy(struct wl_client *client,
4017 struct wl_resource *resource)
4018{
4019 struct weston_pointer_constraint *constraint =
4020 wl_resource_get_user_data(resource);
4021 wl_fixed_t x, y;
4022
4023 if (constraint && constraint->view && constraint->hint_is_pending &&
4024 is_within_constraint_region(constraint,
4025 constraint->hint_x,
4026 constraint->hint_y)) {
4027 weston_view_to_global_fixed(constraint->view,
4028 constraint->hint_x,
4029 constraint->hint_y,
4030 &x, &y);
4031 weston_pointer_move_to(constraint->pointer, x, y);
4032 }
4033 wl_resource_destroy(resource);
4034}
4035
4036static void
4037locked_pointer_set_cursor_position_hint(struct wl_client *client,
4038 struct wl_resource *resource,
4039 wl_fixed_t surface_x,
4040 wl_fixed_t surface_y)
4041{
4042 struct weston_pointer_constraint *constraint =
4043 wl_resource_get_user_data(resource);
4044
4045 /* Ignore a set cursor hint that was sent after the lock was cancelled.
4046 */
4047 if (!constraint ||
4048 !constraint->resource ||
4049 constraint->resource != resource)
4050 return;
4051
4052 constraint->hint_is_pending = true;
4053 constraint->hint_x_pending = surface_x;
4054 constraint->hint_y_pending = surface_y;
4055}
4056
4057static void
4058locked_pointer_set_region(struct wl_client *client,
4059 struct wl_resource *resource,
4060 struct wl_resource *region_resource)
4061{
4062 struct weston_pointer_constraint *constraint =
4063 wl_resource_get_user_data(resource);
4064 struct weston_region *region = region_resource ?
4065 wl_resource_get_user_data(region_resource) : NULL;
4066
4067 if (!constraint)
4068 return;
4069
4070 if (region) {
4071 pixman_region32_copy(&constraint->region_pending,
4072 &region->region);
4073 } else {
4074 pixman_region32_fini(&constraint->region_pending);
4075 region_init_infinite(&constraint->region_pending);
4076 }
4077 constraint->region_is_pending = true;
4078}
4079
4080
4081static const struct zwp_locked_pointer_v1_interface locked_pointer_interface = {
4082 locked_pointer_destroy,
4083 locked_pointer_set_cursor_position_hint,
4084 locked_pointer_set_region,
4085};
4086
4087static void
4088pointer_constraints_lock_pointer(struct wl_client *client,
4089 struct wl_resource *resource,
4090 uint32_t id,
4091 struct wl_resource *surface_resource,
4092 struct wl_resource *pointer_resource,
4093 struct wl_resource *region_resource,
4094 uint32_t lifetime)
4095{
4096 struct weston_surface *surface =
4097 wl_resource_get_user_data(surface_resource);
4098 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
4099 struct weston_region *region = region_resource ?
4100 wl_resource_get_user_data(region_resource) : NULL;
4101
4102 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
4103 &zwp_locked_pointer_v1_interface,
4104 &locked_pointer_interface,
4105 &locked_pointer_grab_interface);
4106}
4107
4108static void
4109confined_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
4110{
4111}
4112
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004113static double
4114vec2d_cross_product(struct vec2d a, struct vec2d b)
4115{
4116 return a.x * b.y - a.y * b.x;
4117}
4118
4119static struct vec2d
4120vec2d_add(struct vec2d a, struct vec2d b)
4121{
4122 return (struct vec2d) {
4123 .x = a.x + b.x,
4124 .y = a.y + b.y,
4125 };
4126}
4127
4128static struct vec2d
4129vec2d_subtract(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_multiply_constant(double c, struct vec2d a)
4139{
4140 return (struct vec2d) {
4141 .x = c * a.x,
4142 .y = c * a.y,
4143 };
4144}
4145
4146static bool
4147lines_intersect(struct line *line1, struct line *line2,
4148 struct vec2d *intersection)
4149{
4150 struct vec2d p = line1->a;
4151 struct vec2d r = vec2d_subtract(line1->b, line1->a);
4152 struct vec2d q = line2->a;
4153 struct vec2d s = vec2d_subtract(line2->b, line2->a);
4154 double rxs;
4155 double sxr;
4156 double t;
4157 double u;
4158
4159 /*
4160 * The line (p, r) and (q, s) intersects where
4161 *
4162 * p + t r = q + u s
4163 *
4164 * Calculate t:
4165 *
4166 * (p + t r) × s = (q + u s) × s
4167 * p × s + t (r × s) = q × s + u (s × s)
4168 * p × s + t (r × s) = q × s
4169 * t (r × s) = q × s - p × s
4170 * t (r × s) = (q - p) × s
4171 * t = ((q - p) × s) / (r × s)
4172 *
4173 * Using the same method, for u we get:
4174 *
4175 * u = ((p - q) × r) / (s × r)
4176 */
4177
4178 rxs = vec2d_cross_product(r, s);
4179 sxr = vec2d_cross_product(s, r);
4180
4181 /* If r × s = 0 then the lines are either parallel or collinear. */
4182 if (fabs(rxs) < DBL_MIN)
4183 return false;
4184
4185 t = vec2d_cross_product(vec2d_subtract(q, p), s) / rxs;
4186 u = vec2d_cross_product(vec2d_subtract(p, q), r) / sxr;
4187
4188 /* The lines only intersect if 0 ≤ t ≤ 1 and 0 ≤ u ≤ 1. */
4189 if (t < 0.0 || t > 1.0 || u < 0.0 || u > 1.0)
4190 return false;
4191
4192 *intersection = vec2d_add(p, vec2d_multiply_constant(t, r));
4193 return true;
4194}
4195
4196static struct border *
4197add_border(struct wl_array *array,
4198 double x1, double y1,
4199 double x2, double y2,
4200 enum motion_direction blocking_dir)
4201{
4202 struct border *border = wl_array_add(array, sizeof *border);
4203
4204 *border = (struct border) {
4205 .line = (struct line) {
4206 .a = (struct vec2d) {
4207 .x = x1,
4208 .y = y1,
4209 },
4210 .b = (struct vec2d) {
4211 .x = x2,
4212 .y = y2,
4213 },
4214 },
4215 .blocking_dir = blocking_dir,
4216 };
4217
4218 return border;
4219}
4220
4221static int
4222compare_lines_x(const void *a, const void *b)
4223{
4224 const struct border *border_a = a;
4225 const struct border *border_b = b;
4226
4227
4228 if (border_a->line.a.x == border_b->line.a.x)
4229 return border_a->line.b.x < border_b->line.b.x;
4230 else
4231 return border_a->line.a.x > border_b->line.a.x;
4232}
4233
4234static void
4235add_non_overlapping_edges(pixman_box32_t *boxes,
4236 int band_above_start,
4237 int band_below_start,
4238 int band_below_end,
4239 struct wl_array *borders)
4240{
4241 int i;
4242 struct wl_array band_merge;
4243 struct border *border;
4244 struct border *prev_border;
4245 struct border *new_border;
4246
4247 wl_array_init(&band_merge);
4248
4249 /* Add bottom band of previous row, and top band of current row, and
4250 * sort them so lower left x coordinate comes first. If there are two
4251 * borders with the same left x coordinate, the wider one comes first.
4252 */
4253 for (i = band_above_start; i < band_below_start; i++) {
4254 pixman_box32_t *box = &boxes[i];
4255 add_border(&band_merge, box->x1, box->y2, box->x2, box->y2,
4256 MOTION_DIRECTION_POSITIVE_Y);
4257 }
4258 for (i = band_below_start; i < band_below_end; i++) {
4259 pixman_box32_t *box= &boxes[i];
4260 add_border(&band_merge, box->x1, box->y1, box->x2, box->y1,
4261 MOTION_DIRECTION_NEGATIVE_Y);
4262 }
4263 qsort(band_merge.data,
4264 band_merge.size / sizeof *border,
4265 sizeof *border,
4266 compare_lines_x);
4267
4268 /* Combine the two combined bands so that any overlapping border is
4269 * eliminated. */
4270 prev_border = NULL;
4271 wl_array_for_each(border, &band_merge) {
4272 assert(border->line.a.y == border->line.b.y);
4273 assert(!prev_border ||
4274 prev_border->line.a.y == border->line.a.y);
4275 assert(!prev_border ||
4276 (prev_border->line.a.x != border->line.a.x ||
4277 prev_border->line.b.x != border->line.b.x));
4278 assert(!prev_border ||
4279 prev_border->line.a.x <= border->line.a.x);
4280
4281 if (prev_border &&
4282 prev_border->line.a.x == border->line.a.x) {
4283 /*
4284 * ------------ +
4285 * ------- =
4286 * [ ]-----
4287 */
4288 prev_border->line.a.x = border->line.b.x;
4289 } else if (prev_border &&
4290 prev_border->line.b.x == border->line.b.x) {
4291 /*
4292 * ------------ +
4293 * ------ =
4294 * ------[ ]
4295 */
4296 prev_border->line.b.x = border->line.a.x;
4297 } else if (prev_border &&
4298 prev_border->line.b.x == border->line.a.x) {
4299 /*
4300 * -------- +
4301 * ------ =
4302 * --------------
4303 */
4304 prev_border->line.b.x = border->line.b.x;
4305 } else if (prev_border &&
4306 prev_border->line.b.x >= border->line.a.x) {
4307 /*
4308 * --------------- +
4309 * ------ =
4310 * -----[ ]----
4311 */
4312 new_border = add_border(borders,
4313 border->line.b.x,
4314 border->line.b.y,
4315 prev_border->line.b.x,
4316 prev_border->line.b.y,
4317 prev_border->blocking_dir);
4318 prev_border->line.b.x = border->line.a.x;
4319 prev_border = new_border;
4320 } else {
4321 assert(!prev_border ||
4322 prev_border->line.b.x < border->line.a.x);
4323 /*
4324 * First border or non-overlapping.
4325 *
4326 * ----- +
4327 * ----- =
4328 * ----- -----
4329 */
4330 new_border = wl_array_add(borders, sizeof *border);
4331 *new_border = *border;
4332 prev_border = new_border;
4333 }
4334 }
4335
4336 wl_array_release(&band_merge);
4337}
4338
4339static void
4340add_band_bottom_edges(pixman_box32_t *boxes,
4341 int band_start,
4342 int band_end,
4343 struct wl_array *borders)
4344{
4345 int i;
4346
4347 for (i = band_start; i < band_end; i++) {
4348 add_border(borders,
4349 boxes[i].x1, boxes[i].y2,
4350 boxes[i].x2, boxes[i].y2,
4351 MOTION_DIRECTION_POSITIVE_Y);
4352 }
4353}
4354
4355static void
4356region_to_outline(pixman_region32_t *region, struct wl_array *borders)
4357{
4358 pixman_box32_t *boxes;
4359 int num_boxes;
4360 int i;
4361 int top_most, bottom_most;
4362 int current_roof;
4363 int prev_top;
4364 int band_start, prev_band_start;
4365
4366 /*
4367 * Remove any overlapping lines from the set of rectangles. Note that
4368 * pixman regions are grouped as rows of rectangles, where rectangles
4369 * in one row never touch or overlap and are all of the same height.
4370 *
4371 * -------- --- -------- ---
4372 * | | | | | | | |
4373 * ----------====---- --- ----------- ----- ---
4374 * | | => | |
4375 * ----==========--------- ----- ----------
4376 * | | | |
4377 * ------------------- -------------------
4378 *
4379 */
4380
4381 boxes = pixman_region32_rectangles(region, &num_boxes);
4382 prev_top = 0;
4383 top_most = boxes[0].y1;
4384 current_roof = top_most;
4385 bottom_most = boxes[num_boxes - 1].y2;
4386 band_start = 0;
4387 prev_band_start = 0;
4388 for (i = 0; i < num_boxes; i++) {
4389 /* Detect if there is a vertical empty space, and add the lower
4390 * level of the previous band if so was the case. */
4391 if (i > 0 &&
4392 boxes[i].y1 != prev_top &&
4393 boxes[i].y1 != boxes[i - 1].y2) {
4394 current_roof = boxes[i].y1;
4395 add_band_bottom_edges(boxes,
4396 band_start,
4397 i,
4398 borders);
4399 }
4400
4401 /* Special case adding the last band, since it won't be handled
4402 * by the band change detection below. */
4403 if (boxes[i].y1 != current_roof && i == num_boxes - 1) {
4404 if (boxes[i].y1 != prev_top) {
4405 /* The last band is a single box, so we don't
4406 * have a prev_band_start to tell us when the
4407 * previous band started. */
4408 add_non_overlapping_edges(boxes,
4409 band_start,
4410 i,
4411 i + 1,
4412 borders);
4413 } else {
4414 add_non_overlapping_edges(boxes,
4415 prev_band_start,
4416 band_start,
4417 i + 1,
4418 borders);
4419 }
4420 }
4421
4422 /* Detect when passing a band and combine the top border of the
4423 * just passed band with the bottom band of the previous band.
4424 */
4425 if (boxes[i].y1 != top_most && boxes[i].y1 != prev_top) {
4426 /* Combine the two passed bands. */
4427 if (prev_top != current_roof) {
4428 add_non_overlapping_edges(boxes,
4429 prev_band_start,
4430 band_start,
4431 i,
4432 borders);
4433 }
4434
4435 prev_band_start = band_start;
4436 band_start = i;
4437 }
4438
4439 /* Add the top border if the box is part of the current roof. */
4440 if (boxes[i].y1 == current_roof) {
4441 add_border(borders,
4442 boxes[i].x1, boxes[i].y1,
4443 boxes[i].x2, boxes[i].y1,
4444 MOTION_DIRECTION_NEGATIVE_Y);
4445 }
4446
4447 /* Add the bottom border of the last band. */
4448 if (boxes[i].y2 == bottom_most) {
4449 add_border(borders,
4450 boxes[i].x1, boxes[i].y2,
4451 boxes[i].x2, boxes[i].y2,
4452 MOTION_DIRECTION_POSITIVE_Y);
4453 }
4454
4455 /* Always add the left border. */
4456 add_border(borders,
4457 boxes[i].x1, boxes[i].y1,
4458 boxes[i].x1, boxes[i].y2,
4459 MOTION_DIRECTION_NEGATIVE_X);
4460
4461 /* Always add the right border. */
4462 add_border(borders,
4463 boxes[i].x2, boxes[i].y1,
4464 boxes[i].x2, boxes[i].y2,
4465 MOTION_DIRECTION_POSITIVE_X);
4466
4467 prev_top = boxes[i].y1;
4468 }
4469}
4470
4471static bool
4472is_border_horizontal (struct border *border)
4473{
4474 return border->line.a.y == border->line.b.y;
4475}
4476
4477static bool
4478is_border_blocking_directions(struct border *border,
4479 uint32_t directions)
4480{
4481 /* Don't block parallel motions. */
4482 if (is_border_horizontal(border)) {
4483 if ((directions & (MOTION_DIRECTION_POSITIVE_Y |
4484 MOTION_DIRECTION_NEGATIVE_Y)) == 0)
4485 return false;
4486 } else {
4487 if ((directions & (MOTION_DIRECTION_POSITIVE_X |
4488 MOTION_DIRECTION_NEGATIVE_X)) == 0)
4489 return false;
4490 }
4491
4492 return (~border->blocking_dir & directions) != directions;
4493}
4494
4495static struct border *
4496get_closest_border(struct wl_array *borders,
4497 struct line *motion,
4498 uint32_t directions)
4499{
4500 struct border *border;
4501 struct vec2d intersection;
4502 struct vec2d delta;
4503 double distance_2;
4504 struct border *closest_border = NULL;
4505 double closest_distance_2 = DBL_MAX;
4506
4507 wl_array_for_each(border, borders) {
4508 if (!is_border_blocking_directions(border, directions))
4509 continue;
4510
4511 if (!lines_intersect(&border->line, motion, &intersection))
4512 continue;
4513
4514 delta = vec2d_subtract(intersection, motion->a);
4515 distance_2 = delta.x*delta.x + delta.y*delta.y;
4516 if (distance_2 < closest_distance_2) {
4517 closest_border = border;
4518 closest_distance_2 = distance_2;
4519 }
4520 }
4521
4522 return closest_border;
4523}
4524
4525static void
4526clamp_to_border(struct border *border,
4527 struct line *motion,
4528 uint32_t *motion_dir)
4529{
4530 /*
4531 * When clamping either rightward or downward motions, the motion needs
4532 * to be clamped so that the destination coordinate does not end up on
4533 * the border (see weston_pointer_clamp_event_to_region). Do this by
4534 * clamping such motions to the border minus the smallest possible
4535 * wl_fixed_t value.
4536 */
4537 if (is_border_horizontal(border)) {
4538 if (*motion_dir & MOTION_DIRECTION_POSITIVE_Y)
4539 motion->b.y = border->line.a.y - wl_fixed_to_double(1);
4540 else
4541 motion->b.y = border->line.a.y;
4542 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_Y |
4543 MOTION_DIRECTION_NEGATIVE_Y);
4544 } else {
4545 if (*motion_dir & MOTION_DIRECTION_POSITIVE_X)
4546 motion->b.x = border->line.a.x - wl_fixed_to_double(1);
4547 else
4548 motion->b.x = border->line.a.x;
4549 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_X |
4550 MOTION_DIRECTION_NEGATIVE_X);
4551 }
4552}
4553
4554static uint32_t
4555get_motion_directions(struct line *motion)
4556{
4557 uint32_t directions = 0;
4558
4559 if (motion->a.x < motion->b.x)
4560 directions |= MOTION_DIRECTION_POSITIVE_X;
4561 else if (motion->a.x > motion->b.x)
4562 directions |= MOTION_DIRECTION_NEGATIVE_X;
4563 if (motion->a.y < motion->b.y)
4564 directions |= MOTION_DIRECTION_POSITIVE_Y;
4565 else if (motion->a.y > motion->b.y)
4566 directions |= MOTION_DIRECTION_NEGATIVE_Y;
4567
4568 return directions;
4569}
4570
Jonas Ådahld3414f22016-07-22 17:56:31 +08004571static void
4572weston_pointer_clamp_event_to_region(struct weston_pointer *pointer,
4573 struct weston_pointer_motion_event *event,
4574 pixman_region32_t *region,
4575 wl_fixed_t *clamped_x,
4576 wl_fixed_t *clamped_y)
4577{
4578 wl_fixed_t x, y;
4579 wl_fixed_t sx, sy;
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004580 wl_fixed_t old_sx = pointer->sx;
4581 wl_fixed_t old_sy = pointer->sy;
4582 struct wl_array borders;
4583 struct line motion;
4584 struct border *closest_border;
4585 float new_x_f, new_y_f;
4586 uint32_t directions;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004587
4588 weston_pointer_motion_to_abs(pointer, event, &x, &y);
4589 weston_view_from_global_fixed(pointer->focus, x, y, &sx, &sy);
4590
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004591 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004592
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004593 /*
4594 * Generate borders given the confine region we are to use. The borders
4595 * are defined to be the outer region of the allowed area. This means
4596 * top/left borders are "within" the allowed area, while bottom/right
4597 * borders are outside. This needs to be considered when clamping
4598 * confined motion vectors.
4599 */
4600 region_to_outline(region, &borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004601
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004602 motion = (struct line) {
4603 .a = (struct vec2d) {
4604 .x = wl_fixed_to_double(old_sx),
4605 .y = wl_fixed_to_double(old_sy),
4606 },
4607 .b = (struct vec2d) {
4608 .x = wl_fixed_to_double(sx),
4609 .y = wl_fixed_to_double(sy),
4610 },
4611 };
4612 directions = get_motion_directions(&motion);
4613
4614 while (directions) {
4615 closest_border = get_closest_border(&borders,
4616 &motion,
4617 directions);
4618 if (closest_border)
4619 clamp_to_border(closest_border, &motion, &directions);
4620 else
4621 break;
4622 }
4623
4624 weston_view_to_global_float(pointer->focus,
4625 (float) motion.b.x, (float) motion.b.y,
4626 &new_x_f, &new_y_f);
4627 *clamped_x = wl_fixed_from_double(new_x_f);
4628 *clamped_y = wl_fixed_from_double(new_y_f);
4629
4630 wl_array_release(&borders);
4631}
4632
4633static double
4634point_to_border_distance_2(struct border *border, double x, double y)
4635{
4636 double orig_x, orig_y;
4637 double dx, dy;
4638
4639 if (is_border_horizontal(border)) {
4640 if (x < border->line.a.x)
4641 orig_x = border->line.a.x;
4642 else if (x > border->line.b.x)
4643 orig_x = border->line.b.x;
4644 else
4645 orig_x = x;
4646 orig_y = border->line.a.y;
4647 } else {
4648 if (y < border->line.a.y)
4649 orig_y = border->line.a.y;
4650 else if (y > border->line.b.y)
4651 orig_y = border->line.b.y;
4652 else
4653 orig_y = y;
4654 orig_x = border->line.a.x;
4655 }
4656
4657
4658 dx = fabs(orig_x - x);
4659 dy = fabs(orig_y - y);
4660 return dx*dx + dy*dy;
4661}
4662
4663static void
4664warp_to_behind_border(struct border *border, wl_fixed_t *sx, wl_fixed_t *sy)
4665{
4666 switch (border->blocking_dir) {
4667 case MOTION_DIRECTION_POSITIVE_X:
4668 case MOTION_DIRECTION_NEGATIVE_X:
4669 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_X)
4670 *sx = wl_fixed_from_double(border->line.a.x) - 1;
4671 else
4672 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4673 if (*sy < wl_fixed_from_double(border->line.a.y))
4674 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4675 else if (*sy > wl_fixed_from_double(border->line.b.y))
4676 *sy = wl_fixed_from_double(border->line.b.y) - 1;
4677 break;
4678 case MOTION_DIRECTION_POSITIVE_Y:
4679 case MOTION_DIRECTION_NEGATIVE_Y:
4680 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_Y)
4681 *sy = wl_fixed_from_double(border->line.a.y) - 1;
4682 else
4683 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4684 if (*sx < wl_fixed_from_double(border->line.a.x))
4685 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4686 else if (*sx > wl_fixed_from_double(border->line.b.x))
4687 *sx = wl_fixed_from_double(border->line.b.x) - 1;
4688 break;
4689 }
Jonas Ådahld3414f22016-07-22 17:56:31 +08004690}
4691
4692static void
4693maybe_warp_confined_pointer(struct weston_pointer_constraint *constraint)
4694{
4695 wl_fixed_t x;
4696 wl_fixed_t y;
4697 wl_fixed_t sx;
4698 wl_fixed_t sy;
4699
4700 weston_view_from_global_fixed(constraint->view,
4701 constraint->pointer->x,
4702 constraint->pointer->y,
4703 &sx,
4704 &sy);
4705
4706 if (!is_within_constraint_region(constraint, sx, sy)) {
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004707 double xf = wl_fixed_to_double(sx);
4708 double yf = wl_fixed_to_double(sy);
4709 pixman_region32_t confine_region;
4710 struct wl_array borders;
4711 struct border *border;
4712 double closest_distance_2 = DBL_MAX;
4713 struct border *closest_border = NULL;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004714
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004715 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004716
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004717 pixman_region32_init(&confine_region);
4718 pixman_region32_intersect(&confine_region,
4719 &constraint->view->surface->input,
4720 &constraint->region);
4721 region_to_outline(&confine_region, &borders);
4722 pixman_region32_fini(&confine_region);
4723
4724 wl_array_for_each(border, &borders) {
4725 double distance_2;
4726
4727 distance_2 = point_to_border_distance_2(border, xf, yf);
4728 if (distance_2 < closest_distance_2) {
4729 closest_border = border;
4730 closest_distance_2 = distance_2;
4731 }
4732 }
4733 assert(closest_border);
4734
4735 warp_to_behind_border(closest_border, &sx, &sy);
4736
4737 wl_array_release(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004738
4739 weston_view_to_global_fixed(constraint->view, sx, sy, &x, &y);
4740 weston_pointer_move_to(constraint->pointer, x, y);
4741 }
4742}
4743
4744static void
4745confined_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02004746 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004747 struct weston_pointer_motion_event *event)
4748{
4749 struct weston_pointer_constraint *constraint =
4750 container_of(grab, struct weston_pointer_constraint, grab);
4751 struct weston_pointer *pointer = grab->pointer;
4752 struct weston_surface *surface;
4753 wl_fixed_t x, y;
4754 wl_fixed_t old_sx = pointer->sx;
4755 wl_fixed_t old_sy = pointer->sy;
4756 pixman_region32_t confine_region;
4757
4758 assert(pointer->focus);
4759 assert(pointer->focus->surface == constraint->surface);
4760
4761 surface = pointer->focus->surface;
4762
4763 pixman_region32_init(&confine_region);
4764 pixman_region32_intersect(&confine_region,
4765 &surface->input,
4766 &constraint->region);
4767 weston_pointer_clamp_event_to_region(pointer, event,
4768 &confine_region, &x, &y);
4769 weston_pointer_move_to(pointer, x, y);
4770 pixman_region32_fini(&confine_region);
4771
4772 weston_view_from_global_fixed(pointer->focus, x, y,
4773 &pointer->sx, &pointer->sy);
4774
4775 if (old_sx != pointer->sx || old_sy != pointer->sy) {
Quentin Glidiccde13452016-08-12 10:41:32 +02004776 pointer_send_motion(pointer, time,
4777 pointer->sx, pointer->sy);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004778 }
4779
Quentin Glidiccde13452016-08-12 10:41:32 +02004780 pointer_send_relative_motion(pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004781}
4782
4783static void
4784confined_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02004785 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004786 uint32_t button,
4787 uint32_t state_w)
4788{
4789 weston_pointer_send_button(grab->pointer, time, button, state_w);
4790}
4791
4792static void
4793confined_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02004794 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004795 struct weston_pointer_axis_event *event)
4796{
4797 weston_pointer_send_axis(grab->pointer, time, event);
4798}
4799
4800static void
4801confined_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
4802 uint32_t source)
4803{
4804 weston_pointer_send_axis_source(grab->pointer, source);
4805}
4806
4807static void
4808confined_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
4809{
4810 weston_pointer_send_frame(grab->pointer);
4811}
4812
4813static void
4814confined_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
4815{
4816 struct weston_pointer_constraint *constraint =
4817 container_of(grab, struct weston_pointer_constraint, grab);
4818
4819 disable_pointer_constraint(constraint);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004820}
4821
4822static const struct weston_pointer_grab_interface
4823 confined_pointer_grab_interface = {
4824 confined_pointer_grab_pointer_focus,
4825 confined_pointer_grab_pointer_motion,
4826 confined_pointer_grab_pointer_button,
4827 confined_pointer_grab_pointer_axis,
4828 confined_pointer_grab_pointer_axis_source,
4829 confined_pointer_grab_pointer_frame,
4830 confined_pointer_grab_pointer_cancel,
4831};
4832
4833static void
4834confined_pointer_destroy(struct wl_client *client,
4835 struct wl_resource *resource)
4836{
4837 wl_resource_destroy(resource);
4838}
4839
4840static void
4841confined_pointer_set_region(struct wl_client *client,
4842 struct wl_resource *resource,
4843 struct wl_resource *region_resource)
4844{
4845 struct weston_pointer_constraint *constraint =
4846 wl_resource_get_user_data(resource);
4847 struct weston_region *region = region_resource ?
4848 wl_resource_get_user_data(region_resource) : NULL;
4849
4850 if (!constraint)
4851 return;
4852
4853 if (region) {
4854 pixman_region32_copy(&constraint->region_pending,
4855 &region->region);
4856 } else {
4857 pixman_region32_fini(&constraint->region_pending);
4858 region_init_infinite(&constraint->region_pending);
4859 }
4860 constraint->region_is_pending = true;
4861}
4862
4863static const struct zwp_confined_pointer_v1_interface confined_pointer_interface = {
4864 confined_pointer_destroy,
4865 confined_pointer_set_region,
4866};
4867
4868static void
4869pointer_constraints_confine_pointer(struct wl_client *client,
4870 struct wl_resource *resource,
4871 uint32_t id,
4872 struct wl_resource *surface_resource,
4873 struct wl_resource *pointer_resource,
4874 struct wl_resource *region_resource,
4875 uint32_t lifetime)
4876{
4877 struct weston_surface *surface =
4878 wl_resource_get_user_data(surface_resource);
4879 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
4880 struct weston_region *region = region_resource ?
4881 wl_resource_get_user_data(region_resource) : NULL;
4882
4883 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
4884 &zwp_confined_pointer_v1_interface,
4885 &confined_pointer_interface,
4886 &confined_pointer_grab_interface);
4887}
4888
4889static const struct zwp_pointer_constraints_v1_interface pointer_constraints_interface = {
4890 pointer_constraints_destroy,
4891 pointer_constraints_lock_pointer,
4892 pointer_constraints_confine_pointer,
4893};
4894
4895static void
4896bind_pointer_constraints(struct wl_client *client, void *data,
4897 uint32_t version, uint32_t id)
4898{
4899 struct wl_resource *resource;
4900
4901 resource = wl_resource_create(client,
4902 &zwp_pointer_constraints_v1_interface,
4903 1, id);
4904
4905 wl_resource_set_implementation(resource, &pointer_constraints_interface,
4906 NULL, NULL);
4907}
4908
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004909static void
Alexandros Frantzis2b442482018-02-20 14:05:50 +02004910input_timestamps_destroy(struct wl_client *client,
4911 struct wl_resource *resource)
4912{
4913 wl_resource_destroy(resource);
4914}
4915
4916static const struct zwp_input_timestamps_v1_interface
4917 input_timestamps_interface = {
4918 input_timestamps_destroy,
4919};
4920
4921static void
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004922input_timestamps_manager_destroy(struct wl_client *client,
4923 struct wl_resource *resource)
4924{
4925 wl_resource_destroy(resource);
4926}
4927
4928static void
4929input_timestamps_manager_get_keyboard_timestamps(struct wl_client *client,
4930 struct wl_resource *resource,
4931 uint32_t id,
4932 struct wl_resource *keyboard_resource)
4933{
Alexandros Frantzis2b442482018-02-20 14:05:50 +02004934 struct weston_keyboard *keyboard =
4935 wl_resource_get_user_data(keyboard_resource);
4936 struct wl_resource *input_ts;
4937
4938 input_ts = wl_resource_create(client,
4939 &zwp_input_timestamps_v1_interface,
4940 1, id);
4941 if (!input_ts) {
4942 wl_client_post_no_memory(client);
4943 return;
4944 }
4945
4946 if (keyboard) {
4947 wl_list_insert(&keyboard->timestamps_list,
4948 wl_resource_get_link(input_ts));
4949 } else {
4950 wl_list_init(wl_resource_get_link(input_ts));
4951 }
4952
4953 wl_resource_set_implementation(input_ts,
4954 &input_timestamps_interface,
4955 keyboard_resource,
4956 unbind_resource);
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004957}
4958
4959static void
4960input_timestamps_manager_get_pointer_timestamps(struct wl_client *client,
4961 struct wl_resource *resource,
4962 uint32_t id,
4963 struct wl_resource *pointer_resource)
4964{
Alexandros Frantzisdb907b72018-02-20 14:06:26 +02004965 struct weston_pointer *pointer =
4966 wl_resource_get_user_data(pointer_resource);
4967 struct wl_resource *input_ts;
4968
4969 input_ts = wl_resource_create(client,
4970 &zwp_input_timestamps_v1_interface,
4971 1, id);
4972 if (!input_ts) {
4973 wl_client_post_no_memory(client);
4974 return;
4975 }
4976
4977 if (pointer) {
4978 wl_list_insert(&pointer->timestamps_list,
4979 wl_resource_get_link(input_ts));
4980 } else {
4981 wl_list_init(wl_resource_get_link(input_ts));
4982 }
4983
4984 wl_resource_set_implementation(input_ts,
4985 &input_timestamps_interface,
4986 pointer_resource,
4987 unbind_resource);
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004988}
4989
4990static void
4991input_timestamps_manager_get_touch_timestamps(struct wl_client *client,
4992 struct wl_resource *resource,
4993 uint32_t id,
4994 struct wl_resource *touch_resource)
4995{
Alexandros Frantzisd7157842018-02-20 14:07:03 +02004996 struct weston_touch *touch = wl_resource_get_user_data(touch_resource);
4997 struct wl_resource *input_ts;
4998
4999 input_ts = wl_resource_create(client,
5000 &zwp_input_timestamps_v1_interface,
5001 1, id);
5002 if (!input_ts) {
5003 wl_client_post_no_memory(client);
5004 return;
5005 }
5006
5007 if (touch) {
5008 wl_list_insert(&touch->timestamps_list,
5009 wl_resource_get_link(input_ts));
5010 } else {
5011 wl_list_init(wl_resource_get_link(input_ts));
5012 }
5013
5014 wl_resource_set_implementation(input_ts,
5015 &input_timestamps_interface,
5016 touch_resource,
5017 unbind_resource);
Alexandros Frantzis538749d2018-02-16 18:44:16 +02005018}
5019
5020static const struct zwp_input_timestamps_manager_v1_interface
5021 input_timestamps_manager_interface = {
5022 input_timestamps_manager_destroy,
5023 input_timestamps_manager_get_keyboard_timestamps,
5024 input_timestamps_manager_get_pointer_timestamps,
5025 input_timestamps_manager_get_touch_timestamps,
5026};
5027
5028static void
5029bind_input_timestamps_manager(struct wl_client *client, void *data,
5030 uint32_t version, uint32_t id)
5031{
5032 struct wl_resource *resource =
5033 wl_resource_create(client,
5034 &zwp_input_timestamps_manager_v1_interface,
5035 1, id);
5036
5037 if (resource == NULL) {
5038 wl_client_post_no_memory(client);
5039 return;
5040 }
5041
5042 wl_resource_set_implementation(resource,
5043 &input_timestamps_manager_interface,
5044 NULL, NULL);
5045}
5046
Jonas Ådahl30d61d82014-10-22 21:21:17 +02005047int
5048weston_input_init(struct weston_compositor *compositor)
5049{
5050 if (!wl_global_create(compositor->wl_display,
5051 &zwp_relative_pointer_manager_v1_interface, 1,
5052 compositor, bind_relative_pointer_manager))
5053 return -1;
5054
Jonas Ådahld3414f22016-07-22 17:56:31 +08005055 if (!wl_global_create(compositor->wl_display,
5056 &zwp_pointer_constraints_v1_interface, 1,
5057 NULL, bind_pointer_constraints))
5058 return -1;
5059
Alexandros Frantzis538749d2018-02-16 18:44:16 +02005060 if (!wl_global_create(compositor->wl_display,
5061 &zwp_input_timestamps_manager_v1_interface, 1,
5062 NULL, bind_input_timestamps_manager))
5063 return -1;
5064
Jonas Ådahl30d61d82014-10-22 21:21:17 +02005065 return 0;
5066}