blob: 74ac2019dddc3f17acd0162ba47d17af43d95793 [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
leng.fang32af9fc2024-06-13 11:22:15 +08001528static bool weston_keyboard_need_enter_surface( struct weston_keyboard *keyboard,
1529 struct weston_surface *surface)
1530{
1531 bool enter = false;
1532 if ( (find_resource_for_surface(&keyboard->resource_list, surface)
1533 || find_resource_for_surface(&keyboard->focus_resource_list, surface)) &&
1534 keyboard->focus != surface ) {
1535 enter = true;
1536 }
1537 weston_log("\n %s %d, enter surface:%p, enter:%d222\n", __FUNCTION__,__LINE__, surface, enter);
1538 return enter;
1539}
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001540WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001541weston_keyboard_set_focus(struct weston_keyboard *keyboard,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001542 struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001543{
Quentin Glidic85d55542017-07-21 14:02:40 +02001544 struct weston_seat *seat = keyboard->seat;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001545 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +01001546 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001547 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01001548 struct wl_list *focus_resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001549
Pekka Paalanen72e183b2018-02-22 16:55:15 +02001550 /* Keyboard focus on a surface without a client is equivalent to NULL
1551 * focus as nothing would react to the keyboard events anyway.
1552 * Just set focus to NULL instead - the destroy listener hangs on the
1553 * wl_resource anyway.
1554 */
1555 if (surface && !surface->resource)
1556 surface = NULL;
1557
Neil Roberts96d790e2013-09-19 17:32:00 +01001558 focus_resource_list = &keyboard->focus_resource_list;
1559
leng.fang32af9fc2024-06-13 11:22:15 +08001560 if (weston_keyboard_need_enter_surface( keyboard, surface ) || surface == NULL)
1561 {
1562 if (!wl_list_empty(focus_resource_list) && keyboard->focus != surface) {
1563 serial = wl_display_next_serial(display);
1564 wl_resource_for_each(resource, focus_resource_list) {
1565 if (surface && keyboard && keyboard->focus)
1566 surface->compositor->last_keyboard_focus = keyboard->focus;
1567 weston_log("\n %s %d, leave surface:%p\n", __FUNCTION__,__LINE__, keyboard->focus);
1568 wl_keyboard_send_leave(resource, serial,
1569 keyboard->focus->resource);
1570 }
1571 move_resources(&keyboard->resource_list, focus_resource_list);
1572 }
1573 if ( surface == NULL && keyboard && keyboard->focus) {
1574 surface = keyboard->focus->compositor->last_keyboard_focus;
1575 weston_log("\n %s %d, need back to surface:%p\n", __FUNCTION__,__LINE__, surface);
Neil Roberts96d790e2013-09-19 17:32:00 +01001576 }
leng.fang32af9fc2024-06-13 11:22:15 +08001577
1578 if (find_resource_for_surface(&keyboard->resource_list, surface) &&
1579 keyboard->focus != surface) {
1580 struct wl_client *surface_client =
1581 wl_resource_get_client(surface->resource);
1582
1583 serial = wl_display_next_serial(display);
1584
1585 move_resources_for_client(focus_resource_list,
1586 &keyboard->resource_list,
1587 surface_client);
1588 send_enter_to_resource_list(focus_resource_list,
1589 keyboard,
1590 surface,
1591 serial);
1592 keyboard->focus_serial = serial;
1593 }
1594
1595 /* Since this function gets called from the surface destroy handler
1596 * we can't just remove the kbd focus listener, or we might corrupt
1597 * the list it's in.
1598 * Instead, we'll just set a flag to ignore the focus when the
1599 * compositor regains kbd focus.
1600 */
1601 seat->use_saved_kbd_focus = false;
1602
1603 wl_list_remove(&keyboard->focus_resource_listener.link);
1604 wl_list_init(&keyboard->focus_resource_listener.link);
1605 if (surface)
1606 wl_resource_add_destroy_listener(surface->resource,
1607 &keyboard->focus_resource_listener);
1608
1609 keyboard->focus = surface;
1610 wl_signal_emit(&keyboard->focus_signal, keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001611 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001612}
1613
Giulio Camuffoa20ca812014-11-22 11:16:56 +02001614/* Users of this function must manually manage the keyboard focus */
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001615WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001616weston_keyboard_start_grab(struct weston_keyboard *keyboard,
1617 struct weston_keyboard_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001618{
1619 keyboard->grab = grab;
1620 grab->keyboard = keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001621}
1622
1623WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001624weston_keyboard_end_grab(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001625{
1626 keyboard->grab = &keyboard->default_grab;
1627}
1628
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001629static void
1630weston_keyboard_cancel_grab(struct weston_keyboard *keyboard)
1631{
1632 keyboard->grab->interface->cancel(keyboard->grab);
1633}
1634
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001635WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001636weston_pointer_start_grab(struct weston_pointer *pointer,
1637 struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001638{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001639 pointer->grab = grab;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001640 grab->pointer = pointer;
Kristian Høgsbergda751b82013-07-04 00:58:07 -04001641 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001642}
1643
1644WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001645weston_pointer_end_grab(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001646{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001647 pointer->grab = &pointer->default_grab;
Kristian Høgsbergda751b82013-07-04 00:58:07 -04001648 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001649}
1650
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001651static void
1652weston_pointer_cancel_grab(struct weston_pointer *pointer)
1653{
1654 pointer->grab->interface->cancel(pointer->grab);
1655}
1656
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001657WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -04001658weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001659{
1660 touch->grab = grab;
1661 grab->touch = touch;
1662}
1663
1664WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -04001665weston_touch_end_grab(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001666{
1667 touch->grab = &touch->default_grab;
1668}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001669
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001670static void
1671weston_touch_cancel_grab(struct weston_touch *touch)
1672{
1673 touch->grab->interface->cancel(touch->grab);
1674}
1675
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001676static void
1677weston_pointer_clamp_for_output(struct weston_pointer *pointer,
1678 struct weston_output *output,
1679 wl_fixed_t *fx, wl_fixed_t *fy)
1680{
1681 int x, y;
1682
1683 x = wl_fixed_to_int(*fx);
1684 y = wl_fixed_to_int(*fy);
1685
1686 if (x < output->x)
1687 *fx = wl_fixed_from_int(output->x);
1688 else if (x >= output->x + output->width)
1689 *fx = wl_fixed_from_int(output->x +
1690 output->width - 1);
1691 if (y < output->y)
1692 *fy = wl_fixed_from_int(output->y);
1693 else if (y >= output->y + output->height)
1694 *fy = wl_fixed_from_int(output->y +
1695 output->height - 1);
1696}
1697
Rob Bradford806d8c02013-06-25 18:56:41 +01001698WL_EXPORT void
1699weston_pointer_clamp(struct weston_pointer *pointer, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001700{
Rob Bradford806d8c02013-06-25 18:56:41 +01001701 struct weston_compositor *ec = pointer->seat->compositor;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001702 struct weston_output *output, *prev = NULL;
1703 int x, y, old_x, old_y, valid = 0;
1704
1705 x = wl_fixed_to_int(*fx);
1706 y = wl_fixed_to_int(*fy);
Rob Bradford806d8c02013-06-25 18:56:41 +01001707 old_x = wl_fixed_to_int(pointer->x);
1708 old_y = wl_fixed_to_int(pointer->y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001709
1710 wl_list_for_each(output, &ec->output_list, link) {
Rob Bradford66bd9f52013-06-25 18:56:42 +01001711 if (pointer->seat->output && pointer->seat->output != output)
1712 continue;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001713 if (pixman_region32_contains_point(&output->region,
1714 x, y, NULL))
1715 valid = 1;
1716 if (pixman_region32_contains_point(&output->region,
1717 old_x, old_y, NULL))
1718 prev = output;
1719 }
1720
Rob Bradford66bd9f52013-06-25 18:56:42 +01001721 if (!prev)
1722 prev = pointer->seat->output;
1723
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001724 if (prev && !valid)
1725 weston_pointer_clamp_for_output(pointer, prev, fx, fy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001726}
1727
Jonas Ådahld2510102014-10-05 21:39:14 +02001728static void
1729weston_pointer_move_to(struct weston_pointer *pointer,
1730 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001731{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001732 int32_t ix, iy;
1733
Rob Bradford806d8c02013-06-25 18:56:41 +01001734 weston_pointer_clamp (pointer, &x, &y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001735
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001736 pointer->x = x;
1737 pointer->y = y;
1738
1739 ix = wl_fixed_to_int(x);
1740 iy = wl_fixed_to_int(y);
1741
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001742 if (pointer->sprite) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001743 weston_view_set_position(pointer->sprite,
1744 ix - pointer->hotspot_x,
1745 iy - pointer->hotspot_y);
1746 weston_view_schedule_repaint(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001747 }
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001748
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001749 pointer->grab->interface->focus(pointer->grab);
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001750 wl_signal_emit(&pointer->motion_signal, pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001751}
1752
Jonas Ådahld2510102014-10-05 21:39:14 +02001753WL_EXPORT void
Jonas Ådahld2510102014-10-05 21:39:14 +02001754weston_pointer_move(struct weston_pointer *pointer,
1755 struct weston_pointer_motion_event *event)
1756{
1757 wl_fixed_t x, y;
1758
1759 weston_pointer_motion_to_abs(pointer, event, &x, &y);
1760 weston_pointer_move_to(pointer, x, y);
1761}
1762
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001763/** Verify if the pointer is in a valid position and move it if it isn't.
1764 */
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001765static void
1766weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001767{
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001768 struct weston_pointer *pointer;
1769 struct weston_compositor *ec;
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001770 struct weston_output *output, *closest = NULL;
1771 int x, y, distance, min = INT_MAX;
1772 wl_fixed_t fx, fy;
1773
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001774 pointer = container_of(listener, struct weston_pointer,
1775 output_destroy_listener);
1776 ec = pointer->seat->compositor;
1777
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001778 x = wl_fixed_to_int(pointer->x);
1779 y = wl_fixed_to_int(pointer->y);
1780
1781 wl_list_for_each(output, &ec->output_list, link) {
1782 if (pixman_region32_contains_point(&output->region,
1783 x, y, NULL))
1784 return;
1785
1786 /* Aproximante the distance from the pointer to the center of
1787 * the output. */
1788 distance = abs(output->x + output->width / 2 - x) +
1789 abs(output->y + output->height / 2 - y);
1790 if (distance < min) {
1791 min = distance;
1792 closest = output;
1793 }
1794 }
1795
1796 /* Nothing to do if there's no output left. */
1797 if (!closest)
1798 return;
1799
1800 fx = pointer->x;
1801 fy = pointer->y;
1802
1803 weston_pointer_clamp_for_output(pointer, closest, &fx, &fy);
Jonas Ådahld2510102014-10-05 21:39:14 +02001804 weston_pointer_move_to(pointer, fx, fy);
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001805}
1806
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001807WL_EXPORT void
1808notify_motion(struct weston_seat *seat,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001809 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001810 struct weston_pointer_motion_event *event)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001811{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001812 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001813 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001814
1815 weston_compositor_wake(ec);
Jonas Ådahld2510102014-10-05 21:39:14 +02001816 pointer->grab->interface->motion(pointer->grab, time, event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001817}
1818
Daniel Stone96d47c02013-11-19 11:37:12 +01001819static void
1820run_modifier_bindings(struct weston_seat *seat, uint32_t old, uint32_t new)
1821{
1822 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001823 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Daniel Stone96d47c02013-11-19 11:37:12 +01001824 uint32_t diff;
1825 unsigned int i;
1826 struct {
1827 uint32_t xkb;
1828 enum weston_keyboard_modifier weston;
1829 } mods[] = {
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001830 { keyboard->xkb_info->ctrl_mod, MODIFIER_CTRL },
1831 { keyboard->xkb_info->alt_mod, MODIFIER_ALT },
1832 { keyboard->xkb_info->super_mod, MODIFIER_SUPER },
1833 { keyboard->xkb_info->shift_mod, MODIFIER_SHIFT },
Daniel Stone96d47c02013-11-19 11:37:12 +01001834 };
1835
1836 diff = new & ~old;
1837 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
1838 if (diff & (1 << mods[i].xkb))
1839 weston_compositor_run_modifier_binding(compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001840 keyboard,
Daniel Stone96d47c02013-11-19 11:37:12 +01001841 mods[i].weston,
1842 WL_KEYBOARD_KEY_STATE_PRESSED);
1843 }
1844
1845 diff = old & ~new;
1846 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
1847 if (diff & (1 << mods[i].xkb))
1848 weston_compositor_run_modifier_binding(compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001849 keyboard,
Daniel Stone96d47c02013-11-19 11:37:12 +01001850 mods[i].weston,
1851 WL_KEYBOARD_KEY_STATE_RELEASED);
1852 }
1853}
1854
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001855WL_EXPORT void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001856notify_motion_absolute(struct weston_seat *seat, const struct timespec *time,
1857 double x, double y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001858{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001859 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001860 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Jonas Ådahld2510102014-10-05 21:39:14 +02001861 struct weston_pointer_motion_event event = { 0 };
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001862
1863 weston_compositor_wake(ec);
Jonas Ådahld2510102014-10-05 21:39:14 +02001864
1865 event = (struct weston_pointer_motion_event) {
1866 .mask = WESTON_POINTER_MOTION_ABS,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001867 .x = x,
1868 .y = y,
Jonas Ådahld2510102014-10-05 21:39:14 +02001869 };
1870
1871 pointer->grab->interface->motion(pointer->grab, time, &event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001872}
1873
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02001874static unsigned int
1875peek_next_activate_serial(struct weston_compositor *c)
1876{
1877 unsigned serial = c->activate_serial + 1;
1878
1879 return serial == 0 ? 1 : serial;
1880}
1881
1882static void
1883inc_activate_serial(struct weston_compositor *c)
1884{
1885 c->activate_serial = peek_next_activate_serial (c);
1886}
1887
1888WL_EXPORT void
Marius Vladd6ccc8b2021-03-05 22:07:30 +02001889weston_view_activate_input(struct weston_view *view,
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02001890 struct weston_seat *seat,
1891 uint32_t flags)
1892{
1893 struct weston_compositor *compositor = seat->compositor;
1894
1895 if (flags & WESTON_ACTIVATE_FLAG_CLICKED) {
1896 view->click_to_activate_serial =
1897 peek_next_activate_serial(compositor);
1898 }
1899
1900 weston_seat_set_keyboard_focus(seat, view->surface);
1901}
1902
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001903WL_EXPORT void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001904notify_button(struct weston_seat *seat, const struct timespec *time,
1905 int32_t button, enum wl_pointer_button_state state)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001906{
1907 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001908 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001909
1910 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001911 weston_compositor_idle_inhibit(compositor);
1912 if (pointer->button_count == 0) {
1913 pointer->grab_button = button;
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001914 pointer->grab_time = *time;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001915 pointer->grab_x = pointer->x;
1916 pointer->grab_y = pointer->y;
1917 }
1918 pointer->button_count++;
1919 } else {
1920 weston_compositor_idle_release(compositor);
1921 pointer->button_count--;
1922 }
1923
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001924 weston_compositor_run_button_binding(compositor, pointer, time, button,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001925 state);
1926
1927 pointer->grab->interface->button(pointer->grab, time, button, state);
1928
1929 if (pointer->button_count == 1)
1930 pointer->grab_serial =
1931 wl_display_get_serial(compositor->wl_display);
1932}
1933
1934WL_EXPORT void
Alexandros Frantzis80321942017-11-16 18:20:56 +02001935notify_axis(struct weston_seat *seat, const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001936 struct weston_pointer_axis_event *event)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001937{
1938 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001939 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001940
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001941 weston_compositor_wake(compositor);
1942
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001943 if (weston_compositor_run_axis_binding(compositor, pointer,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001944 time, event))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001945 return;
1946
Peter Hutterer89b6a492016-01-18 15:58:17 +10001947 pointer->grab->interface->axis(pointer->grab, time, event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001948}
1949
Peter Hutterer87743e92016-01-18 16:38:22 +10001950WL_EXPORT void
1951notify_axis_source(struct weston_seat *seat, uint32_t source)
1952{
1953 struct weston_compositor *compositor = seat->compositor;
1954 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1955
1956 weston_compositor_wake(compositor);
1957
1958 pointer->grab->interface->axis_source(pointer->grab, source);
1959}
1960
1961WL_EXPORT void
1962notify_pointer_frame(struct weston_seat *seat)
1963{
1964 struct weston_compositor *compositor = seat->compositor;
1965 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1966
1967 weston_compositor_wake(compositor);
1968
1969 pointer->grab->interface->frame(pointer->grab);
1970}
1971
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001972WL_EXPORT int
1973weston_keyboard_set_locks(struct weston_keyboard *keyboard,
1974 uint32_t mask, uint32_t value)
1975{
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001976 uint32_t serial;
1977 xkb_mod_mask_t mods_depressed, mods_latched, mods_locked, group;
1978 xkb_mod_mask_t num, caps;
1979
1980 /* We don't want the leds to go out of sync with the actual state
1981 * so if the backend has no way to change the leds don't try to
1982 * change the state */
1983 if (!keyboard->seat->led_update)
1984 return -1;
1985
1986 mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
1987 XKB_STATE_DEPRESSED);
1988 mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
1989 XKB_STATE_LATCHED);
1990 mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
1991 XKB_STATE_LOCKED);
1992 group = xkb_state_serialize_group(keyboard->xkb_state.state,
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02001993 XKB_STATE_EFFECTIVE);
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001994
1995 num = (1 << keyboard->xkb_info->mod2_mod);
1996 caps = (1 << keyboard->xkb_info->caps_mod);
1997 if (mask & WESTON_NUM_LOCK) {
1998 if (value & WESTON_NUM_LOCK)
1999 mods_locked |= num;
2000 else
2001 mods_locked &= ~num;
2002 }
2003 if (mask & WESTON_CAPS_LOCK) {
2004 if (value & WESTON_CAPS_LOCK)
2005 mods_locked |= caps;
2006 else
2007 mods_locked &= ~caps;
2008 }
2009
2010 xkb_state_update_mask(keyboard->xkb_state.state, mods_depressed,
2011 mods_latched, mods_locked, 0, 0, group);
2012
2013 serial = wl_display_next_serial(
2014 keyboard->seat->compositor->wl_display);
2015 notify_modifiers(keyboard->seat, serial);
2016
2017 return 0;
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03002018}
2019
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002020WL_EXPORT void
2021notify_modifiers(struct weston_seat *seat, uint32_t serial)
2022{
Derek Foreman1281a362015-07-31 16:55:32 -05002023 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002024 struct weston_keyboard_grab *grab = keyboard->grab;
2025 uint32_t mods_depressed, mods_latched, mods_locked, group;
2026 uint32_t mods_lookup;
2027 enum weston_led leds = 0;
2028 int changed = 0;
2029
2030 /* Serialize and update our internal state, checking to see if it's
2031 * different to the previous state. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002032 mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03002033 XKB_STATE_MODS_DEPRESSED);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002034 mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03002035 XKB_STATE_MODS_LATCHED);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002036 mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03002037 XKB_STATE_MODS_LOCKED);
2038 group = xkb_state_serialize_layout(keyboard->xkb_state.state,
2039 XKB_STATE_LAYOUT_EFFECTIVE);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002040
Derek Foreman244e99e2015-06-03 15:53:26 -05002041 if (mods_depressed != keyboard->modifiers.mods_depressed ||
2042 mods_latched != keyboard->modifiers.mods_latched ||
2043 mods_locked != keyboard->modifiers.mods_locked ||
2044 group != keyboard->modifiers.group)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002045 changed = 1;
2046
Derek Foreman244e99e2015-06-03 15:53:26 -05002047 run_modifier_bindings(seat, keyboard->modifiers.mods_depressed,
Daniel Stone96d47c02013-11-19 11:37:12 +01002048 mods_depressed);
2049
Derek Foreman244e99e2015-06-03 15:53:26 -05002050 keyboard->modifiers.mods_depressed = mods_depressed;
2051 keyboard->modifiers.mods_latched = mods_latched;
2052 keyboard->modifiers.mods_locked = mods_locked;
2053 keyboard->modifiers.group = group;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002054
2055 /* And update the modifier_state for bindings. */
2056 mods_lookup = mods_depressed | mods_latched;
2057 seat->modifier_state = 0;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002058 if (mods_lookup & (1 << keyboard->xkb_info->ctrl_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002059 seat->modifier_state |= MODIFIER_CTRL;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002060 if (mods_lookup & (1 << keyboard->xkb_info->alt_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002061 seat->modifier_state |= MODIFIER_ALT;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002062 if (mods_lookup & (1 << keyboard->xkb_info->super_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002063 seat->modifier_state |= MODIFIER_SUPER;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002064 if (mods_lookup & (1 << keyboard->xkb_info->shift_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002065 seat->modifier_state |= MODIFIER_SHIFT;
2066
2067 /* Finally, notify the compositor that LEDs have changed. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002068 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
2069 keyboard->xkb_info->num_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002070 leds |= LED_NUM_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002071 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
2072 keyboard->xkb_info->caps_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002073 leds |= LED_CAPS_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002074 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
2075 keyboard->xkb_info->scroll_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002076 leds |= LED_SCROLL_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002077 if (leds != keyboard->xkb_state.leds && seat->led_update)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002078 seat->led_update(seat, leds);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002079 keyboard->xkb_state.leds = leds;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002080
2081 if (changed) {
2082 grab->interface->modifiers(grab,
2083 serial,
2084 keyboard->modifiers.mods_depressed,
2085 keyboard->modifiers.mods_latched,
2086 keyboard->modifiers.mods_locked,
2087 keyboard->modifiers.group);
2088 }
2089}
2090
2091static void
2092update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
2093 enum wl_keyboard_key_state state)
2094{
Derek Foreman1281a362015-07-31 16:55:32 -05002095 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002096 enum xkb_key_direction direction;
2097
2098 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2099 direction = XKB_KEY_DOWN;
2100 else
2101 direction = XKB_KEY_UP;
2102
2103 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
2104 * broken keycode system, which starts at 8. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002105 xkb_state_update_key(keyboard->xkb_state.state, key + 8, direction);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002106
2107 notify_modifiers(seat, serial);
2108}
Rui Matos65196bc2013-10-10 19:44:19 +02002109
Derek Foremanf8f7fd62017-06-28 11:41:43 -05002110WL_EXPORT void
2111weston_keyboard_send_keymap(struct weston_keyboard *kbd, struct wl_resource *resource)
Rui Matos65196bc2013-10-10 19:44:19 +02002112{
Derek Foremanf8f7fd62017-06-28 11:41:43 -05002113 struct weston_xkb_info *xkb_info = kbd->xkb_info;
Derek Foreman76829fc2017-06-28 12:17:46 -05002114 int fd;
Sebastian Wickabec5122019-11-01 02:38:45 +01002115 size_t size;
2116 enum ro_anonymous_file_mapmode mapmode;
Derek Foremanf8f7fd62017-06-28 11:41:43 -05002117
Sebastian Wickabec5122019-11-01 02:38:45 +01002118 if (wl_resource_get_version(resource) < 7)
2119 mapmode = RO_ANONYMOUS_FILE_MAPMODE_SHARED;
2120 else
2121 mapmode = RO_ANONYMOUS_FILE_MAPMODE_PRIVATE;
2122
2123 fd = os_ro_anonymous_file_get_fd(xkb_info->keymap_rofile, mapmode);
2124 size = os_ro_anonymous_file_size(xkb_info->keymap_rofile);
2125
2126 if (fd == -1) {
2127 weston_log("creating a keymap file failed: %s\n",
Antonio Borneo39578632019-04-26 23:57:31 +02002128 strerror(errno));
Derek Foreman76829fc2017-06-28 12:17:46 -05002129 return;
2130 }
2131
Rui Matos65196bc2013-10-10 19:44:19 +02002132 wl_keyboard_send_keymap(resource,
2133 WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
Derek Foreman76829fc2017-06-28 12:17:46 -05002134 fd,
Sebastian Wickabec5122019-11-01 02:38:45 +01002135 size);
2136
2137 os_ro_anonymous_file_put_fd(fd);
Rui Matos65196bc2013-10-10 19:44:19 +02002138}
2139
2140static void
2141send_modifiers(struct wl_resource *resource, uint32_t serial, struct weston_keyboard *keyboard)
2142{
2143 wl_keyboard_send_modifiers(resource, serial,
2144 keyboard->modifiers.mods_depressed,
2145 keyboard->modifiers.mods_latched,
2146 keyboard->modifiers.mods_locked,
2147 keyboard->modifiers.group);
2148}
2149
2150static struct weston_xkb_info *
2151weston_xkb_info_create(struct xkb_keymap *keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02002152
2153static void
2154update_keymap(struct weston_seat *seat)
2155{
Derek Foreman1281a362015-07-31 16:55:32 -05002156 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Rui Matos65196bc2013-10-10 19:44:19 +02002157 struct wl_resource *resource;
2158 struct weston_xkb_info *xkb_info;
2159 struct xkb_state *state;
2160 xkb_mod_mask_t latched_mods;
2161 xkb_mod_mask_t locked_mods;
2162
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002163 xkb_info = weston_xkb_info_create(keyboard->pending_keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02002164
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002165 xkb_keymap_unref(keyboard->pending_keymap);
2166 keyboard->pending_keymap = NULL;
Rui Matos65196bc2013-10-10 19:44:19 +02002167
2168 if (!xkb_info) {
2169 weston_log("failed to create XKB info\n");
2170 return;
2171 }
2172
2173 state = xkb_state_new(xkb_info->keymap);
2174 if (!state) {
2175 weston_log("failed to initialise XKB state\n");
2176 weston_xkb_info_destroy(xkb_info);
2177 return;
2178 }
2179
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002180 latched_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
2181 XKB_STATE_MODS_LATCHED);
2182 locked_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
2183 XKB_STATE_MODS_LOCKED);
Rui Matos65196bc2013-10-10 19:44:19 +02002184 xkb_state_update_mask(state,
2185 0, /* depressed */
2186 latched_mods,
2187 locked_mods,
2188 0, 0, 0);
2189
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002190 weston_xkb_info_destroy(keyboard->xkb_info);
2191 keyboard->xkb_info = xkb_info;
Rui Matos65196bc2013-10-10 19:44:19 +02002192
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002193 xkb_state_unref(keyboard->xkb_state.state);
2194 keyboard->xkb_state.state = state;
Rui Matos65196bc2013-10-10 19:44:19 +02002195
Derek Foremanbc91e542015-06-03 15:53:27 -05002196 wl_resource_for_each(resource, &keyboard->resource_list)
Derek Foremanf8f7fd62017-06-28 11:41:43 -05002197 weston_keyboard_send_keymap(keyboard, resource);
Derek Foremanbc91e542015-06-03 15:53:27 -05002198 wl_resource_for_each(resource, &keyboard->focus_resource_list)
Derek Foremanf8f7fd62017-06-28 11:41:43 -05002199 weston_keyboard_send_keymap(keyboard, resource);
Rui Matos65196bc2013-10-10 19:44:19 +02002200
2201 notify_modifiers(seat, wl_display_next_serial(seat->compositor->wl_display));
2202
2203 if (!latched_mods && !locked_mods)
2204 return;
2205
Derek Foremanbc91e542015-06-03 15:53:27 -05002206 wl_resource_for_each(resource, &keyboard->resource_list)
2207 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), keyboard);
2208 wl_resource_for_each(resource, &keyboard->focus_resource_list)
2209 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), keyboard);
Rui Matos65196bc2013-10-10 19:44:19 +02002210}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002211
2212WL_EXPORT void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02002213notify_key(struct weston_seat *seat, const struct timespec *time, uint32_t key,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002214 enum wl_keyboard_key_state state,
2215 enum weston_key_state_update update_state)
2216{
2217 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002218 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002219 struct weston_keyboard_grab *grab = keyboard->grab;
Pekka Paalanen86b53962014-11-19 13:43:32 +02002220 uint32_t *k, *end;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002221
2222 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002223 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002224 } else {
2225 weston_compositor_idle_release(compositor);
2226 }
2227
Pekka Paalanen86b53962014-11-19 13:43:32 +02002228 end = keyboard->keys.data + keyboard->keys.size;
2229 for (k = keyboard->keys.data; k < end; k++) {
2230 if (*k == key) {
2231 /* Ignore server-generated repeats. */
2232 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2233 return;
2234 *k = *--end;
2235 }
2236 }
2237 keyboard->keys.size = (void *) end - keyboard->keys.data;
2238 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2239 k = wl_array_add(&keyboard->keys, sizeof *k);
2240 *k = key;
2241 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002242
2243 if (grab == &keyboard->default_grab ||
2244 grab == &keyboard->input_method_grab) {
Derek Foreman99a6a2d2015-07-15 13:00:43 -05002245 weston_compositor_run_key_binding(compositor, keyboard, time,
2246 key, state);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002247 grab = keyboard->grab;
2248 }
2249
2250 grab->interface->key(grab, time, key, state);
2251
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002252 if (keyboard->pending_keymap &&
Pekka Paalanen86b53962014-11-19 13:43:32 +02002253 keyboard->keys.size == 0)
Rui Matos65196bc2013-10-10 19:44:19 +02002254 update_keymap(seat);
2255
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002256 if (update_state == STATE_UPDATE_AUTOMATIC) {
2257 update_modifier_state(seat,
2258 wl_display_get_serial(compositor->wl_display),
2259 key,
2260 state);
2261 }
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002262
Olivier Fourdandf84dbe2016-06-30 16:01:56 +02002263 keyboard->grab_serial = wl_display_get_serial(compositor->wl_display);
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002264 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02002265 keyboard->grab_time = *time;
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002266 keyboard->grab_key = key;
2267 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002268}
2269
2270WL_EXPORT void
2271notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002272 double x, double y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002273{
Derek Foreman1281a362015-07-31 16:55:32 -05002274 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2275
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002276 if (output) {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002277 weston_pointer_move_to(pointer,
2278 wl_fixed_from_double(x),
2279 wl_fixed_from_double(y));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002280 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002281 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002282 * NULL) here, but somehow that breaks re-entry... */
2283 }
2284}
2285
2286static void
2287destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
2288{
2289 struct weston_seat *ws;
2290
2291 ws = container_of(listener, struct weston_seat,
2292 saved_kbd_focus_listener);
2293
2294 ws->saved_kbd_focus = NULL;
Derek Foremana822afa2021-08-10 17:17:24 -05002295
2296 wl_list_remove(&ws->saved_kbd_focus_listener.link);
2297 ws->saved_kbd_focus_listener.notify = NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002298}
2299
2300WL_EXPORT void
2301notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
2302 enum weston_key_state_update update_state)
2303{
2304 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002305 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04002306 struct weston_surface *surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002307 uint32_t *k, serial;
2308
2309 serial = wl_display_next_serial(compositor->wl_display);
2310 wl_array_copy(&keyboard->keys, keys);
2311 wl_array_for_each(k, &keyboard->keys) {
2312 weston_compositor_idle_inhibit(compositor);
2313 if (update_state == STATE_UPDATE_AUTOMATIC)
2314 update_modifier_state(seat, serial, *k,
2315 WL_KEYBOARD_KEY_STATE_PRESSED);
2316 }
2317
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002318 surface = seat->saved_kbd_focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002319 if (surface) {
Derek Foremana822afa2021-08-10 17:17:24 -05002320 wl_list_remove(&seat->saved_kbd_focus_listener.link);
2321 seat->saved_kbd_focus_listener.notify = NULL;
2322 seat->saved_kbd_focus = NULL;
2323 if (seat->use_saved_kbd_focus)
2324 weston_keyboard_set_focus(keyboard, surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002325 }
2326}
2327
2328WL_EXPORT void
2329notify_keyboard_focus_out(struct weston_seat *seat)
2330{
2331 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002332 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2333 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Quentin Glidic85d55542017-07-21 14:02:40 +02002334 struct weston_surface *focus = keyboard->focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002335 uint32_t *k, serial;
2336
2337 serial = wl_display_next_serial(compositor->wl_display);
2338 wl_array_for_each(k, &keyboard->keys) {
2339 weston_compositor_idle_release(compositor);
2340 update_modifier_state(seat, serial, *k,
2341 WL_KEYBOARD_KEY_STATE_RELEASED);
2342 }
2343
2344 seat->modifier_state = 0;
2345
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002346 weston_keyboard_set_focus(keyboard, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002347 weston_keyboard_cancel_grab(keyboard);
Derek Foreman1281a362015-07-31 16:55:32 -05002348 if (pointer)
2349 weston_pointer_cancel_grab(pointer);
Quentin Glidic85d55542017-07-21 14:02:40 +02002350
2351 if (focus) {
Derek Foremana822afa2021-08-10 17:17:24 -05002352 seat->use_saved_kbd_focus = true;
Quentin Glidic85d55542017-07-21 14:02:40 +02002353 seat->saved_kbd_focus = focus;
2354 seat->saved_kbd_focus_listener.notify =
2355 destroy_device_saved_kbd_focus;
2356 wl_signal_add(&focus->destroy_signal,
2357 &seat->saved_kbd_focus_listener);
2358 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002359}
2360
Michael Fua2bb7912013-07-23 15:51:06 +08002361WL_EXPORT void
Derek Foreman4c93c082015-04-30 16:45:41 -05002362weston_touch_set_focus(struct weston_touch *touch, struct weston_view *view)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002363{
Neil Roberts96d790e2013-09-19 17:32:00 +01002364 struct wl_list *focus_resource_list;
2365
Derek Foreman4c93c082015-04-30 16:45:41 -05002366 focus_resource_list = &touch->focus_resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002367
Derek Foreman4c93c082015-04-30 16:45:41 -05002368 if (view && touch->focus &&
2369 touch->focus->surface == view->surface) {
2370 touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002371 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002372 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002373
Derek Foreman4c93c082015-04-30 16:45:41 -05002374 wl_list_remove(&touch->focus_resource_listener.link);
2375 wl_list_init(&touch->focus_resource_listener.link);
2376 wl_list_remove(&touch->focus_view_listener.link);
2377 wl_list_init(&touch->focus_view_listener.link);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01002378
Neil Roberts96d790e2013-09-19 17:32:00 +01002379 if (!wl_list_empty(focus_resource_list)) {
Derek Foreman4c93c082015-04-30 16:45:41 -05002380 move_resources(&touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002381 focus_resource_list);
2382 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002383
Jason Ekstranda7af7042013-10-12 22:38:11 -05002384 if (view) {
Derek Foreman362656b2014-09-04 10:23:05 -05002385 struct wl_client *surface_client;
2386
2387 if (!view->surface->resource) {
Derek Foreman4c93c082015-04-30 16:45:41 -05002388 touch->focus = NULL;
Derek Foreman362656b2014-09-04 10:23:05 -05002389 return;
2390 }
2391
2392 surface_client = wl_resource_get_client(view->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +01002393 move_resources_for_client(focus_resource_list,
Derek Foreman4c93c082015-04-30 16:45:41 -05002394 &touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002395 surface_client);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01002396 wl_resource_add_destroy_listener(view->surface->resource,
Derek Foreman4c93c082015-04-30 16:45:41 -05002397 &touch->focus_resource_listener);
2398 wl_signal_add(&view->destroy_signal, &touch->focus_view_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002399 }
Derek Foreman4c93c082015-04-30 16:45:41 -05002400 touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002401}
2402
Pekka Paalanenf4062532018-02-26 16:18:29 +02002403static void
2404process_touch_normal(struct weston_touch_device *device,
2405 const struct timespec *time, int touch_id,
2406 double double_x, double double_y, int touch_type)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002407{
Pekka Paalanenbcbce332018-02-26 14:43:00 +02002408 struct weston_touch *touch = device->aggregate;
2409 struct weston_touch_grab *grab = device->aggregate->grab;
2410 struct weston_compositor *ec = device->aggregate->seat->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002411 struct weston_view *ev;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002412 wl_fixed_t sx, sy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002413 wl_fixed_t x = wl_fixed_from_double(double_x);
2414 wl_fixed_t y = wl_fixed_from_double(double_y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002415
2416 /* Update grab's global coordinates. */
Neil Roberts306fe082013-10-03 16:43:06 +01002417 if (touch_id == touch->grab_touch_id && touch_type != WL_TOUCH_UP) {
2418 touch->grab_x = x;
2419 touch->grab_y = y;
2420 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002421
2422 switch (touch_type) {
2423 case WL_TOUCH_DOWN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002424 /* the first finger down picks the view, and all further go
2425 * to that view for the remainder of the touch session i.e.
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002426 * until all touch points are up again. */
Jonas Ådahl9484b692013-12-02 22:05:03 +01002427 if (touch->num_tp == 1) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002428 ev = weston_compositor_pick_view(ec, x, y, &sx, &sy);
Derek Foreman4c93c082015-04-30 16:45:41 -05002429 weston_touch_set_focus(touch, ev);
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002430 } else if (!touch->focus) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002431 /* Unexpected condition: We have non-initial touch but
2432 * there is no focused surface.
2433 */
Chris Michael3f607d32015-10-07 11:59:49 -04002434 weston_log("touch event received with %d points down "
Jonas Ådahl9484b692013-12-02 22:05:03 +01002435 "but no surface focused\n", touch->num_tp);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002436 return;
2437 }
2438
Derek Foreman99a6a2d2015-07-15 13:00:43 -05002439 weston_compositor_run_touch_binding(ec, touch,
Kristian Høgsbergc8964012014-02-05 14:25:18 -08002440 time, touch_type);
2441
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002442 grab->interface->down(grab, time, touch_id, x, y);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002443 if (touch->num_tp == 1) {
Rusty Lynchf1407ff2013-08-08 21:13:57 -07002444 touch->grab_serial =
2445 wl_display_get_serial(ec->wl_display);
Neil Roberts306fe082013-10-03 16:43:06 +01002446 touch->grab_touch_id = touch_id;
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02002447 touch->grab_time = *time;
Rusty Lynchf1407ff2013-08-08 21:13:57 -07002448 touch->grab_x = x;
2449 touch->grab_y = y;
2450 }
2451
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002452 break;
2453 case WL_TOUCH_MOTION:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002454 ev = touch->focus;
2455 if (!ev)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002456 break;
2457
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +02002458 grab->interface->motion(grab, time, touch_id, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002459 break;
2460 case WL_TOUCH_UP:
Alexandros Frantzis27a51b82017-11-16 18:20:59 +02002461 grab->interface->up(grab, time, touch_id);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002462 if (touch->num_tp == 0)
Derek Foreman4c93c082015-04-30 16:45:41 -05002463 weston_touch_set_focus(touch, NULL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002464 break;
2465 }
2466}
2467
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002468static enum weston_touch_mode
2469get_next_touch_mode(enum weston_touch_mode from)
2470{
2471 switch (from) {
2472 case WESTON_TOUCH_MODE_PREP_NORMAL:
2473 return WESTON_TOUCH_MODE_NORMAL;
2474
2475 case WESTON_TOUCH_MODE_PREP_CALIB:
2476 return WESTON_TOUCH_MODE_CALIB;
2477
2478 case WESTON_TOUCH_MODE_NORMAL:
2479 case WESTON_TOUCH_MODE_CALIB:
2480 return from;
2481 }
2482
2483 return WESTON_TOUCH_MODE_NORMAL;
2484}
2485
2486/** Global touch mode update
2487 *
2488 * If no seat has a touch down and the compositor is in a PREP touch mode,
2489 * set the compositor to the goal touch mode.
2490 *
2491 * Calls calibrator if touch mode changed.
2492 */
2493static void
2494weston_compositor_update_touch_mode(struct weston_compositor *compositor)
2495{
2496 struct weston_seat *seat;
2497 struct weston_touch *touch;
2498 enum weston_touch_mode goal;
2499
2500 wl_list_for_each(seat, &compositor->seat_list, link) {
2501 touch = weston_seat_get_touch(seat);
2502 if (!touch)
2503 continue;
2504
2505 if (touch->num_tp > 0)
2506 return;
2507 }
2508
2509 goal = get_next_touch_mode(compositor->touch_mode);
2510 if (compositor->touch_mode != goal) {
2511 compositor->touch_mode = goal;
2512 touch_calibrator_mode_changed(compositor);
2513 }
2514}
2515
2516/** Start transition to normal touch event handling
2517 *
2518 * The touch event mode changes when all touches on all touch devices have
2519 * been lifted. If no touches are currently down, the transition is immediate.
2520 *
2521 * \sa weston_touch_mode
2522 */
2523void
2524weston_compositor_set_touch_mode_normal(struct weston_compositor *compositor)
2525{
2526 switch (compositor->touch_mode) {
2527 case WESTON_TOUCH_MODE_PREP_NORMAL:
2528 case WESTON_TOUCH_MODE_NORMAL:
2529 return;
2530 case WESTON_TOUCH_MODE_PREP_CALIB:
2531 compositor->touch_mode = WESTON_TOUCH_MODE_NORMAL;
2532 touch_calibrator_mode_changed(compositor);
2533 return;
2534 case WESTON_TOUCH_MODE_CALIB:
2535 compositor->touch_mode = WESTON_TOUCH_MODE_PREP_NORMAL;
2536 }
2537
2538 weston_compositor_update_touch_mode(compositor);
2539}
2540
2541/** Start transition to calibrator touch event handling
2542 *
2543 * The touch event mode changes when all touches on all touch devices have
2544 * been lifted. If no touches are currently down, the transition is immediate.
2545 *
2546 * \sa weston_touch_mode
2547 */
2548void
2549weston_compositor_set_touch_mode_calib(struct weston_compositor *compositor)
2550{
2551 switch (compositor->touch_mode) {
2552 case WESTON_TOUCH_MODE_PREP_CALIB:
2553 case WESTON_TOUCH_MODE_CALIB:
2554 assert(0);
2555 return;
2556 case WESTON_TOUCH_MODE_PREP_NORMAL:
2557 compositor->touch_mode = WESTON_TOUCH_MODE_CALIB;
2558 touch_calibrator_mode_changed(compositor);
2559 return;
2560 case WESTON_TOUCH_MODE_NORMAL:
2561 compositor->touch_mode = WESTON_TOUCH_MODE_PREP_CALIB;
2562 }
2563
2564 weston_compositor_update_touch_mode(compositor);
2565}
2566
Pekka Paalanenf4062532018-02-26 16:18:29 +02002567/** Feed in touch down, motion, and up events, calibratable device.
2568 *
2569 * It assumes always the correct cycle sequence until it gets here: touch_down
2570 * → touch_update → ... → touch_update → touch_end. The driver is responsible
2571 * for sending along such order.
2572 *
2573 * \param device The physical device that generated the event.
2574 * \param time The event timestamp.
2575 * \param touch_id ID for the touch point of this event (multi-touch).
Marius Vlada2dace22019-06-12 16:05:44 +03002576 * \param x X coordinate in compositor global space.
2577 * \param y Y coordinate in compositor global space.
Pekka Paalanenf4062532018-02-26 16:18:29 +02002578 * \param norm Normalized device X, Y coordinates in calibration space, or NULL.
2579 * \param touch_type Either WL_TOUCH_DOWN, WL_TOUCH_UP, or WL_TOUCH_MOTION.
2580 *
2581 * Coordinates double_x and double_y are used for normal operation.
2582 *
2583 * Coordinates norm are only used for touch device calibration. If and only if
2584 * the weston_touch_device does not support calibrating, norm must be NULL.
2585 *
2586 * The calibration space is the normalized coordinate space
2587 * [0.0, 1.0]×[0.0, 1.0] of the weston_touch_device. This is assumed to
2588 * map to the similar normalized coordinate space of the associated
2589 * weston_output.
2590 */
2591WL_EXPORT void
2592notify_touch_normalized(struct weston_touch_device *device,
2593 const struct timespec *time,
2594 int touch_id,
2595 double x, double y,
2596 const struct weston_point2d_device_normalized *norm,
2597 int touch_type)
2598{
Louis-Francis Ratté-Boulianne813a06e2017-11-28 20:42:47 -05002599 struct weston_seat *seat = device->aggregate->seat;
2600 struct weston_touch *touch = device->aggregate;
2601
Pekka Paalanenf4062532018-02-26 16:18:29 +02002602 if (touch_type != WL_TOUCH_UP) {
2603 if (weston_touch_device_can_calibrate(device))
2604 assert(norm != NULL);
2605 else
2606 assert(norm == NULL);
2607 }
2608
Louis-Francis Ratté-Boulianne813a06e2017-11-28 20:42:47 -05002609 /* Update touchpoints count regardless of the current mode. */
2610 switch (touch_type) {
2611 case WL_TOUCH_DOWN:
2612 weston_compositor_idle_inhibit(seat->compositor);
2613
2614 touch->num_tp++;
2615 break;
2616 case WL_TOUCH_UP:
2617 if (touch->num_tp == 0) {
2618 /* This can happen if we start out with one or
2619 * more fingers on the touch screen, in which
2620 * case we didn't get the corresponding down
2621 * event. */
Pekka Paalanen332a45e2018-03-02 14:20:59 +02002622 weston_log("Unmatched touch up event on seat %s, device %s\n",
2623 seat->seat_name, device->syspath);
2624 return;
Louis-Francis Ratté-Boulianne813a06e2017-11-28 20:42:47 -05002625 }
2626 weston_compositor_idle_release(seat->compositor);
2627
2628 touch->num_tp--;
2629 break;
2630 default:
2631 break;
2632 }
2633
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002634 /* Properly forward the touch event */
2635 switch (weston_touch_device_get_mode(device)) {
2636 case WESTON_TOUCH_MODE_NORMAL:
2637 case WESTON_TOUCH_MODE_PREP_CALIB:
2638 process_touch_normal(device, time, touch_id, x, y, touch_type);
2639 break;
2640 case WESTON_TOUCH_MODE_CALIB:
2641 case WESTON_TOUCH_MODE_PREP_NORMAL:
Louis-Francis Ratté-Boulianne83630982017-11-28 20:42:47 -05002642 notify_touch_calibrator(device, time, touch_id,
2643 norm, touch_type);
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002644 break;
2645 }
Pekka Paalanenf4062532018-02-26 16:18:29 +02002646}
2647
Jonas Ådahl1679f232014-04-12 09:39:51 +02002648WL_EXPORT void
Pekka Paalanenbcbce332018-02-26 14:43:00 +02002649notify_touch_frame(struct weston_touch_device *device)
Jonas Ådahl1679f232014-04-12 09:39:51 +02002650{
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002651 struct weston_touch_grab *grab;
Jonas Ådahl1679f232014-04-12 09:39:51 +02002652
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002653 switch (weston_touch_device_get_mode(device)) {
2654 case WESTON_TOUCH_MODE_NORMAL:
2655 case WESTON_TOUCH_MODE_PREP_CALIB:
2656 grab = device->aggregate->grab;
2657 grab->interface->frame(grab);
2658 break;
2659 case WESTON_TOUCH_MODE_CALIB:
2660 case WESTON_TOUCH_MODE_PREP_NORMAL:
Louis-Francis Ratté-Boulianne83630982017-11-28 20:42:47 -05002661 notify_touch_calibrator_frame(device);
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002662 break;
2663 }
2664
2665 weston_compositor_update_touch_mode(device->aggregate->seat->compositor);
Jonas Ådahl1679f232014-04-12 09:39:51 +02002666}
2667
Derek Foreman3cc004a2015-11-06 15:56:09 -06002668WL_EXPORT void
Pekka Paalanenbcbce332018-02-26 14:43:00 +02002669notify_touch_cancel(struct weston_touch_device *device)
Derek Foreman3cc004a2015-11-06 15:56:09 -06002670{
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002671 struct weston_touch_grab *grab;
Derek Foreman3cc004a2015-11-06 15:56:09 -06002672
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002673 switch (weston_touch_device_get_mode(device)) {
2674 case WESTON_TOUCH_MODE_NORMAL:
2675 case WESTON_TOUCH_MODE_PREP_CALIB:
2676 grab = device->aggregate->grab;
2677 grab->interface->cancel(grab);
2678 break;
2679 case WESTON_TOUCH_MODE_CALIB:
2680 case WESTON_TOUCH_MODE_PREP_NORMAL:
Louis-Francis Ratté-Boulianne83630982017-11-28 20:42:47 -05002681 notify_touch_calibrator_cancel(device);
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002682 break;
2683 }
2684
2685 weston_compositor_update_touch_mode(device->aggregate->seat->compositor);
Derek Foreman3cc004a2015-11-06 15:56:09 -06002686}
2687
Pekka Paalanen8274d902014-08-06 19:36:51 +03002688static int
2689pointer_cursor_surface_get_label(struct weston_surface *surface,
2690 char *buf, size_t len)
2691{
2692 return snprintf(buf, len, "cursor");
2693}
2694
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002695static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002696pointer_cursor_surface_committed(struct weston_surface *es,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002697 int32_t dx, int32_t dy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002698{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002699 struct weston_pointer *pointer = es->committed_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002700 int x, y;
2701
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002702 if (es->width == 0)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002703 return;
2704
Jason Ekstranda7af7042013-10-12 22:38:11 -05002705 assert(es == pointer->sprite->surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002706
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002707 pointer->hotspot_x -= dx;
2708 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002709
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002710 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
2711 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002712
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002713 weston_view_set_position(pointer->sprite, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002714
2715 empty_region(&es->pending.input);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02002716 empty_region(&es->input);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002717
2718 if (!weston_surface_is_mapped(es)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002719 weston_layer_entry_insert(&es->compositor->cursor_layer.view_list,
2720 &pointer->sprite->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002721 weston_view_update_transform(pointer->sprite);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002722 es->is_mapped = true;
2723 pointer->sprite->is_mapped = true;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002724 }
2725}
2726
2727static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002728pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2729 uint32_t serial, struct wl_resource *surface_resource,
2730 int32_t x, int32_t y)
2731{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002732 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002733 struct weston_surface *surface = NULL;
2734
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +02002735 if (!pointer)
2736 return;
2737
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002738 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002739 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002740
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002741 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002742 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002743 /* pointer->focus->surface->resource can be NULL. Surfaces like the
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02002744 black_surface used in shell.c for fullscreen don't have
2745 a resource, but can still have focus */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002746 if (pointer->focus->surface->resource == NULL)
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02002747 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002748 if (wl_resource_get_client(pointer->focus->surface->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002749 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002750 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002751 return;
2752
Derek Foreman4e53c532015-03-23 10:55:32 -05002753 if (!surface) {
2754 if (pointer->sprite)
2755 pointer_unmap_sprite(pointer);
2756 return;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002757 }
2758
Jonas Ådahlb4070242015-03-18 15:08:03 +08002759 if (pointer->sprite && pointer->sprite->surface == surface &&
2760 pointer->hotspot_x == x && pointer->hotspot_y == y)
2761 return;
2762
Derek Foreman4e53c532015-03-23 10:55:32 -05002763 if (!pointer->sprite || pointer->sprite->surface != surface) {
2764 if (weston_surface_set_role(surface, "wl_pointer-cursor",
2765 resource,
2766 WL_POINTER_ERROR_ROLE) < 0)
2767 return;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002768
Derek Foreman4e53c532015-03-23 10:55:32 -05002769 if (pointer->sprite)
2770 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002771
Derek Foreman4e53c532015-03-23 10:55:32 -05002772 wl_signal_add(&surface->destroy_signal,
2773 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002774
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002775 surface->committed = pointer_cursor_surface_committed;
2776 surface->committed_private = pointer;
Derek Foreman4e53c532015-03-23 10:55:32 -05002777 weston_surface_set_label_func(surface,
2778 pointer_cursor_surface_get_label);
2779 pointer->sprite = weston_view_create(surface);
2780 }
2781
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002782 pointer->hotspot_x = x;
2783 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002784
Alexandros Frantzis28d66342021-06-09 10:56:26 +03002785 if (surface->width != 0) {
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002786 pointer_cursor_surface_committed(surface, 0, 0);
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +02002787 weston_view_schedule_repaint(pointer->sprite);
2788 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002789}
2790
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002791static void
2792pointer_release(struct wl_client *client, struct wl_resource *resource)
2793{
2794 wl_resource_destroy(resource);
2795}
2796
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002797static const struct wl_pointer_interface pointer_interface = {
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002798 pointer_set_cursor,
2799 pointer_release
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002800};
2801
2802static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002803seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2804 uint32_t id)
2805{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002806 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002807 /* We use the pointer_state directly, which means we'll
2808 * give a wl_pointer if the seat has ever had one - even though
2809 * the spec explicitly states that this request only takes effect
2810 * if the seat has the pointer capability.
2811 *
2812 * This prevents a race between the compositor sending new
2813 * capabilities and the client trying to use the old ones.
2814 */
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002815 struct weston_pointer *pointer = seat ? seat->pointer_state : NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002816 struct wl_resource *cr;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002817 struct weston_pointer_client *pointer_client;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002818
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02002819 cr = wl_resource_create(client, &wl_pointer_interface,
Jason Ekstranda85118c2013-06-27 20:17:02 -05002820 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002821 if (cr == NULL) {
2822 wl_client_post_no_memory(client);
2823 return;
2824 }
2825
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002826 wl_list_init(wl_resource_get_link(cr));
2827 wl_resource_set_implementation(cr, &pointer_interface, pointer,
2828 unbind_pointer_client_resource);
2829
2830 /* If we don't have a pointer_state, the resource is inert, so there
2831 * is nothing more to set up */
2832 if (!pointer)
2833 return;
2834
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002835 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
2836 if (!pointer_client) {
2837 wl_client_post_no_memory(client);
2838 return;
2839 }
2840
2841 wl_list_insert(&pointer_client->pointer_resources,
2842 wl_resource_get_link(cr));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002843
Derek Foreman1281a362015-07-31 16:55:32 -05002844 if (pointer->focus && pointer->focus->surface->resource &&
2845 wl_resource_get_client(pointer->focus->surface->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002846 wl_fixed_t sx, sy;
2847
Derek Foreman1281a362015-07-31 16:55:32 -05002848 weston_view_from_global_fixed(pointer->focus,
2849 pointer->x,
2850 pointer->y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002851 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002852
Neil Roberts96d790e2013-09-19 17:32:00 +01002853 wl_pointer_send_enter(cr,
Derek Foreman1281a362015-07-31 16:55:32 -05002854 pointer->focus_serial,
2855 pointer->focus->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01002856 sx, sy);
Peter Hutterer87743e92016-01-18 16:38:22 +10002857 pointer_send_frame(cr);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002858 }
2859}
2860
2861static void
Alexandros Frantzis2b442482018-02-20 14:05:50 +02002862destroy_keyboard_resource(struct wl_resource *resource)
2863{
2864 struct weston_keyboard *keyboard = wl_resource_get_user_data(resource);
2865
2866 wl_list_remove(wl_resource_get_link(resource));
2867
2868 if (keyboard) {
2869 remove_input_resource_from_timestamps(resource,
2870 &keyboard->timestamps_list);
2871 }
2872}
2873
2874static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002875keyboard_release(struct wl_client *client, struct wl_resource *resource)
2876{
2877 wl_resource_destroy(resource);
2878}
2879
2880static const struct wl_keyboard_interface keyboard_interface = {
2881 keyboard_release
2882};
2883
2884static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002885seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2886 uint32_t id)
2887{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002888 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002889 /* We use the keyboard_state directly, which means we'll
2890 * give a wl_keyboard if the seat has ever had one - even though
2891 * the spec explicitly states that this request only takes effect
2892 * if the seat has the keyboard capability.
2893 *
2894 * This prevents a race between the compositor sending new
2895 * capabilities and the client trying to use the old ones.
2896 */
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002897 struct weston_keyboard *keyboard = seat ? seat->keyboard_state : NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002898 struct wl_resource *cr;
2899
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02002900 cr = wl_resource_create(client, &wl_keyboard_interface,
Jason Ekstranda85118c2013-06-27 20:17:02 -05002901 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002902 if (cr == NULL) {
2903 wl_client_post_no_memory(client);
2904 return;
2905 }
2906
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002907 wl_list_init(wl_resource_get_link(cr));
2908 wl_resource_set_implementation(cr, &keyboard_interface,
Alexandros Frantzis2b442482018-02-20 14:05:50 +02002909 keyboard, destroy_keyboard_resource);
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002910
2911 /* If we don't have a keyboard_state, the resource is inert, so there
2912 * is nothing more to set up */
2913 if (!keyboard)
2914 return;
2915
Neil Roberts96d790e2013-09-19 17:32:00 +01002916 /* May be moved to focused list later by either
2917 * weston_keyboard_set_focus or directly if this client is already
2918 * focused */
Derek Foreman345c9f32015-06-03 15:53:28 -05002919 wl_list_insert(&keyboard->resource_list, wl_resource_get_link(cr));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002920
Jonny Lamb66a41a02014-08-12 14:58:25 +02002921 if (wl_resource_get_version(cr) >= WL_KEYBOARD_REPEAT_INFO_SINCE_VERSION) {
2922 wl_keyboard_send_repeat_info(cr,
2923 seat->compositor->kb_repeat_rate,
2924 seat->compositor->kb_repeat_delay);
2925 }
Jasper St. Pierred8c6aeb2014-08-04 13:43:24 -04002926
Derek Foremanf8f7fd62017-06-28 11:41:43 -05002927 weston_keyboard_send_keymap(keyboard, cr);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002928
Derek Foreman345c9f32015-06-03 15:53:28 -05002929 if (keyboard->focus && keyboard->focus->resource &&
2930 wl_resource_get_client(keyboard->focus->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01002931 struct weston_surface *surface =
Derek Foreman345c9f32015-06-03 15:53:28 -05002932 (struct weston_surface *)keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +01002933
2934 wl_list_remove(wl_resource_get_link(cr));
Derek Foreman345c9f32015-06-03 15:53:28 -05002935 wl_list_insert(&keyboard->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002936 wl_resource_get_link(cr));
2937 wl_keyboard_send_enter(cr,
Derek Foreman345c9f32015-06-03 15:53:28 -05002938 keyboard->focus_serial,
Neil Roberts96d790e2013-09-19 17:32:00 +01002939 surface->resource,
Derek Foreman345c9f32015-06-03 15:53:28 -05002940 &keyboard->keys);
Neil Roberts96d790e2013-09-19 17:32:00 +01002941
Kirill Chibisovc46c70d2020-06-17 01:43:57 +03002942 send_modifiers_to_resource(keyboard,
2943 cr,
2944 keyboard->focus_serial);
2945
Neil Roberts96d790e2013-09-19 17:32:00 +01002946 /* If this is the first keyboard resource for this
2947 * client... */
Derek Foreman345c9f32015-06-03 15:53:28 -05002948 if (keyboard->focus_resource_list.prev ==
Neil Roberts96d790e2013-09-19 17:32:00 +01002949 wl_resource_get_link(cr))
2950 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002951 }
2952}
2953
2954static void
Alexandros Frantzisd7157842018-02-20 14:07:03 +02002955destroy_touch_resource(struct wl_resource *resource)
2956{
2957 struct weston_touch *touch = wl_resource_get_user_data(resource);
2958
2959 wl_list_remove(wl_resource_get_link(resource));
2960
2961 if (touch) {
2962 remove_input_resource_from_timestamps(resource,
2963 &touch->timestamps_list);
2964 }
2965}
2966
2967static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002968touch_release(struct wl_client *client, struct wl_resource *resource)
2969{
2970 wl_resource_destroy(resource);
2971}
2972
2973static const struct wl_touch_interface touch_interface = {
2974 touch_release
2975};
2976
2977static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002978seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2979 uint32_t id)
2980{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002981 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002982 /* We use the touch_state directly, which means we'll
2983 * give a wl_touch if the seat has ever had one - even though
2984 * the spec explicitly states that this request only takes effect
2985 * if the seat has the touch capability.
2986 *
2987 * This prevents a race between the compositor sending new
2988 * capabilities and the client trying to use the old ones.
2989 */
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002990 struct weston_touch *touch = seat ? seat->touch_state : NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002991 struct wl_resource *cr;
2992
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02002993 cr = wl_resource_create(client, &wl_touch_interface,
Jason Ekstranda85118c2013-06-27 20:17:02 -05002994 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002995 if (cr == NULL) {
2996 wl_client_post_no_memory(client);
2997 return;
2998 }
2999
Alexandros Frantzis8480d132018-02-15 13:07:09 +02003000 wl_list_init(wl_resource_get_link(cr));
3001 wl_resource_set_implementation(cr, &touch_interface,
Alexandros Frantzisd7157842018-02-20 14:07:03 +02003002 touch, destroy_touch_resource);
Alexandros Frantzis8480d132018-02-15 13:07:09 +02003003
3004 /* If we don't have a touch_state, the resource is inert, so there
3005 * is nothing more to set up */
3006 if (!touch)
3007 return;
3008
Derek Foreman1281a362015-07-31 16:55:32 -05003009 if (touch->focus &&
3010 wl_resource_get_client(touch->focus->surface->resource) == client) {
Chokshi, Mituld6697142015-10-09 08:28:47 +00003011 wl_list_insert(&touch->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01003012 wl_resource_get_link(cr));
3013 } else {
Chokshi, Mituld6697142015-10-09 08:28:47 +00003014 wl_list_insert(&touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01003015 wl_resource_get_link(cr));
3016 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003017}
3018
Quentin Glidicaab1d362016-03-13 17:49:08 +01003019static void
3020seat_release(struct wl_client *client, struct wl_resource *resource)
3021{
3022 wl_resource_destroy(resource);
3023}
3024
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003025static const struct wl_seat_interface seat_interface = {
3026 seat_get_pointer,
3027 seat_get_keyboard,
3028 seat_get_touch,
Quentin Glidicaab1d362016-03-13 17:49:08 +01003029 seat_release,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003030};
3031
3032static void
3033bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3034{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003035 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003036 struct wl_resource *resource;
3037 enum wl_seat_capability caps = 0;
3038
Jason Ekstranda85118c2013-06-27 20:17:02 -05003039 resource = wl_resource_create(client,
Derek Foreman1909c102015-11-26 14:17:47 -06003040 &wl_seat_interface, version, id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05003041 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003042 wl_resource_set_implementation(resource, &seat_interface, data,
3043 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003044
Derek Foreman1281a362015-07-31 16:55:32 -05003045 if (weston_seat_get_pointer(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003046 caps |= WL_SEAT_CAPABILITY_POINTER;
Derek Foreman1281a362015-07-31 16:55:32 -05003047 if (weston_seat_get_keyboard(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003048 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Derek Foreman1281a362015-07-31 16:55:32 -05003049 if (weston_seat_get_touch(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003050 caps |= WL_SEAT_CAPABILITY_TOUCH;
3051
3052 wl_seat_send_capabilities(resource, caps);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003053 if (version >= WL_SEAT_NAME_SINCE_VERSION)
Rob Bradforde445ae62013-05-31 18:09:51 +01003054 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003055}
3056
Jonas Ådahl30d61d82014-10-22 21:21:17 +02003057static void
3058relative_pointer_destroy(struct wl_client *client,
3059 struct wl_resource *resource)
3060{
3061 wl_resource_destroy(resource);
3062}
3063
3064static const struct zwp_relative_pointer_v1_interface relative_pointer_interface = {
3065 relative_pointer_destroy
3066};
3067
3068static void
3069relative_pointer_manager_destroy(struct wl_client *client,
3070 struct wl_resource *resource)
3071{
3072 wl_resource_destroy(resource);
3073}
3074
3075static void
3076relative_pointer_manager_get_relative_pointer(struct wl_client *client,
3077 struct wl_resource *resource,
3078 uint32_t id,
3079 struct wl_resource *pointer_resource)
3080{
3081 struct weston_pointer *pointer =
3082 wl_resource_get_user_data(pointer_resource);
3083 struct weston_pointer_client *pointer_client;
3084 struct wl_resource *cr;
3085
3086 cr = wl_resource_create(client, &zwp_relative_pointer_v1_interface,
3087 wl_resource_get_version(resource), id);
3088 if (cr == NULL) {
3089 wl_client_post_no_memory(client);
3090 return;
3091 }
3092
3093 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
3094 if (!pointer_client) {
3095 wl_client_post_no_memory(client);
3096 return;
3097 }
3098
3099 wl_list_insert(&pointer_client->relative_pointer_resources,
3100 wl_resource_get_link(cr));
3101 wl_resource_set_implementation(cr, &relative_pointer_interface,
3102 pointer,
3103 unbind_pointer_client_resource);
3104}
3105
3106static const struct zwp_relative_pointer_manager_v1_interface relative_pointer_manager = {
3107 relative_pointer_manager_destroy,
3108 relative_pointer_manager_get_relative_pointer,
3109};
3110
3111static void
3112bind_relative_pointer_manager(struct wl_client *client, void *data,
3113 uint32_t version, uint32_t id)
3114{
3115 struct weston_compositor *compositor = data;
3116 struct wl_resource *resource;
3117
3118 resource = wl_resource_create(client,
3119 &zwp_relative_pointer_manager_v1_interface,
3120 1, id);
3121
3122 wl_resource_set_implementation(resource, &relative_pointer_manager,
3123 compositor,
3124 NULL);
3125}
3126
Giulio Camuffo0358af42016-06-02 21:48:08 +03003127WL_EXPORT int
3128weston_compositor_set_xkb_rule_names(struct weston_compositor *ec,
3129 struct xkb_rule_names *names)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003130{
3131 if (ec->xkb_context == NULL) {
Peter Hutterera2086bb2020-05-13 15:24:27 +10003132 ec->xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003133 if (ec->xkb_context == NULL) {
3134 weston_log("failed to create XKB context\n");
3135 return -1;
3136 }
3137 }
3138
3139 if (names)
3140 ec->xkb_names = *names;
3141 if (!ec->xkb_names.rules)
3142 ec->xkb_names.rules = strdup("evdev");
3143 if (!ec->xkb_names.model)
3144 ec->xkb_names.model = strdup("pc105");
3145 if (!ec->xkb_names.layout)
3146 ec->xkb_names.layout = strdup("us");
3147
3148 return 0;
3149}
3150
Stefan Schmidtfda26522013-09-17 10:54:09 +01003151static void
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003152weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003153{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003154 if (--xkb_info->ref_count > 0)
3155 return;
3156
Ran Benitac9c74152014-08-19 23:59:52 +03003157 xkb_keymap_unref(xkb_info->keymap);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003158
Sebastian Wickabec5122019-11-01 02:38:45 +01003159 os_ro_anonymous_file_destroy(xkb_info->keymap_rofile);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003160 free(xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003161}
3162
3163void
3164weston_compositor_xkb_destroy(struct weston_compositor *ec)
3165{
3166 free((char *) ec->xkb_names.rules);
3167 free((char *) ec->xkb_names.model);
3168 free((char *) ec->xkb_names.layout);
3169 free((char *) ec->xkb_names.variant);
3170 free((char *) ec->xkb_names.options);
Stefan Schmidtfda26522013-09-17 10:54:09 +01003171
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003172 if (ec->xkb_info)
3173 weston_xkb_info_destroy(ec->xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003174 xkb_context_unref(ec->xkb_context);
3175}
3176
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003177static struct weston_xkb_info *
3178weston_xkb_info_create(struct xkb_keymap *keymap)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003179{
Sebastian Wickabec5122019-11-01 02:38:45 +01003180 char *keymap_string;
3181 size_t keymap_size;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003182 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
3183 if (xkb_info == NULL)
3184 return NULL;
3185
Ran Benita2e1968f2014-08-19 23:59:51 +03003186 xkb_info->keymap = xkb_keymap_ref(keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003187 xkb_info->ref_count = 1;
3188
Ran Benita2e1968f2014-08-19 23:59:51 +03003189 xkb_info->shift_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3190 XKB_MOD_NAME_SHIFT);
3191 xkb_info->caps_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3192 XKB_MOD_NAME_CAPS);
3193 xkb_info->ctrl_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3194 XKB_MOD_NAME_CTRL);
3195 xkb_info->alt_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3196 XKB_MOD_NAME_ALT);
3197 xkb_info->mod2_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3198 "Mod2");
3199 xkb_info->mod3_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3200 "Mod3");
3201 xkb_info->super_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3202 XKB_MOD_NAME_LOGO);
3203 xkb_info->mod5_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3204 "Mod5");
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003205
Ran Benita2e1968f2014-08-19 23:59:51 +03003206 xkb_info->num_led = xkb_keymap_led_get_index(xkb_info->keymap,
3207 XKB_LED_NAME_NUM);
3208 xkb_info->caps_led = xkb_keymap_led_get_index(xkb_info->keymap,
3209 XKB_LED_NAME_CAPS);
3210 xkb_info->scroll_led = xkb_keymap_led_get_index(xkb_info->keymap,
3211 XKB_LED_NAME_SCROLL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003212
Sebastian Wickabec5122019-11-01 02:38:45 +01003213 keymap_string = xkb_keymap_get_as_string(xkb_info->keymap,
Derek Foreman76829fc2017-06-28 12:17:46 -05003214 XKB_KEYMAP_FORMAT_TEXT_V1);
Sebastian Wickabec5122019-11-01 02:38:45 +01003215 if (keymap_string == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003216 weston_log("failed to get string version of keymap\n");
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003217 goto err_keymap;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003218 }
Sebastian Wickabec5122019-11-01 02:38:45 +01003219 keymap_size = strlen(keymap_string) + 1;
3220
3221 xkb_info->keymap_rofile = os_ro_anonymous_file_create(keymap_size,
3222 keymap_string);
3223 free(keymap_string);
3224
3225 if (!xkb_info->keymap_rofile) {
3226 weston_log("failed to create anonymous file for keymap\n");
3227 goto err_keymap;
3228 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003229
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003230 return xkb_info;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003231
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003232err_keymap:
Ran Benita2e1968f2014-08-19 23:59:51 +03003233 xkb_keymap_unref(xkb_info->keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003234 free(xkb_info);
3235 return NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003236}
3237
3238static int
3239weston_compositor_build_global_keymap(struct weston_compositor *ec)
3240{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003241 struct xkb_keymap *keymap;
3242
3243 if (ec->xkb_info != NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003244 return 0;
3245
Ran Benita2e1968f2014-08-19 23:59:51 +03003246 keymap = xkb_keymap_new_from_names(ec->xkb_context,
3247 &ec->xkb_names,
3248 0);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003249 if (keymap == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003250 weston_log("failed to compile global XKB keymap\n");
3251 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
3252 "options %s\n",
3253 ec->xkb_names.rules, ec->xkb_names.model,
3254 ec->xkb_names.layout, ec->xkb_names.variant,
3255 ec->xkb_names.options);
3256 return -1;
3257 }
3258
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003259 ec->xkb_info = weston_xkb_info_create(keymap);
Rui Matos73d93952013-10-24 19:28:41 +02003260 xkb_keymap_unref(keymap);
Stefan Schmidtfda26522013-09-17 10:54:09 +01003261 if (ec->xkb_info == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003262 return -1;
3263
3264 return 0;
3265}
3266
Rui Matos65196bc2013-10-10 19:44:19 +02003267WL_EXPORT void
3268weston_seat_update_keymap(struct weston_seat *seat, struct xkb_keymap *keymap)
3269{
Derek Foreman1281a362015-07-31 16:55:32 -05003270 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
3271
3272 if (!keyboard || !keymap)
Rui Matos65196bc2013-10-10 19:44:19 +02003273 return;
3274
Derek Foreman1281a362015-07-31 16:55:32 -05003275 xkb_keymap_unref(keyboard->pending_keymap);
3276 keyboard->pending_keymap = xkb_keymap_ref(keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02003277
Derek Foreman1281a362015-07-31 16:55:32 -05003278 if (keyboard->keys.size == 0)
Rui Matos65196bc2013-10-10 19:44:19 +02003279 update_keymap(seat);
Rui Matos65196bc2013-10-10 19:44:19 +02003280}
3281
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003282WL_EXPORT int
3283weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
3284{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003285 struct weston_keyboard *keyboard;
3286
Derek Foreman1281a362015-07-31 16:55:32 -05003287 if (seat->keyboard_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003288 seat->keyboard_device_count += 1;
3289 if (seat->keyboard_device_count == 1)
3290 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003291 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003292 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003293
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003294 keyboard = weston_keyboard_create();
3295 if (keyboard == NULL) {
3296 weston_log("failed to allocate weston keyboard struct\n");
3297 return -1;
3298 }
3299
Derek Foreman185d1582017-06-28 11:17:23 -05003300 if (keymap != NULL) {
3301 keyboard->xkb_info = weston_xkb_info_create(keymap);
3302 if (keyboard->xkb_info == NULL)
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02003303 goto err;
Derek Foreman185d1582017-06-28 11:17:23 -05003304 } else {
3305 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
3306 goto err;
3307 keyboard->xkb_info = seat->compositor->xkb_info;
3308 keyboard->xkb_info->ref_count++;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01003309 }
Derek Foreman185d1582017-06-28 11:17:23 -05003310
3311 keyboard->xkb_state.state = xkb_state_new(keyboard->xkb_info->keymap);
3312 if (keyboard->xkb_state.state == NULL) {
3313 weston_log("failed to initialise XKB state\n");
3314 goto err;
3315 }
3316
3317 keyboard->xkb_state.leds = 0;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01003318
Derek Foreman1281a362015-07-31 16:55:32 -05003319 seat->keyboard_state = keyboard;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02003320 seat->keyboard_device_count = 1;
3321 keyboard->seat = seat;
3322
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003323 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003324
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003325 return 0;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02003326
3327err:
3328 if (keyboard->xkb_info)
3329 weston_xkb_info_destroy(keyboard->xkb_info);
3330 free(keyboard);
3331
3332 return -1;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003333}
3334
Jonas Ådahl91fed542013-12-03 09:14:27 +01003335static void
3336weston_keyboard_reset_state(struct weston_keyboard *keyboard)
3337{
3338 struct weston_seat *seat = keyboard->seat;
3339 struct xkb_state *state;
3340
Derek Foreman185d1582017-06-28 11:17:23 -05003341 state = xkb_state_new(keyboard->xkb_info->keymap);
3342 if (!state) {
3343 weston_log("failed to reset XKB state\n");
3344 return;
Jonas Ådahl91fed542013-12-03 09:14:27 +01003345 }
Derek Foreman185d1582017-06-28 11:17:23 -05003346 xkb_state_unref(keyboard->xkb_state.state);
3347 keyboard->xkb_state.state = state;
3348
3349 keyboard->xkb_state.leds = 0;
Jonas Ådahl91fed542013-12-03 09:14:27 +01003350
3351 seat->modifier_state = 0;
3352}
3353
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003354WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003355weston_seat_release_keyboard(struct weston_seat *seat)
3356{
3357 seat->keyboard_device_count--;
Derek Foremand621df22014-11-19 11:04:12 -06003358 assert(seat->keyboard_device_count >= 0);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003359 if (seat->keyboard_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05003360 weston_keyboard_set_focus(seat->keyboard_state, NULL);
3361 weston_keyboard_cancel_grab(seat->keyboard_state);
3362 weston_keyboard_reset_state(seat->keyboard_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003363 seat_send_updated_caps(seat);
3364 }
3365}
3366
Marius Vladeb34f822021-03-30 23:09:05 +03003367WL_EXPORT int
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003368weston_seat_init_pointer(struct weston_seat *seat)
3369{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003370 struct weston_pointer *pointer;
3371
Derek Foreman1281a362015-07-31 16:55:32 -05003372 if (seat->pointer_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003373 seat->pointer_device_count += 1;
3374 if (seat->pointer_device_count == 1)
3375 seat_send_updated_caps(seat);
Marius Vladeb34f822021-03-30 23:09:05 +03003376 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003377 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003378
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003379 pointer = weston_pointer_create(seat);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003380 if (pointer == NULL)
Marius Vladeb34f822021-03-30 23:09:05 +03003381 return -1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003382
Derek Foreman1281a362015-07-31 16:55:32 -05003383 seat->pointer_state = pointer;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003384 seat->pointer_device_count = 1;
3385 pointer->seat = seat;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003386
3387 seat_send_updated_caps(seat);
Marius Vladeb34f822021-03-30 23:09:05 +03003388
3389 return 0;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003390}
3391
3392WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003393weston_seat_release_pointer(struct weston_seat *seat)
3394{
Derek Foreman1281a362015-07-31 16:55:32 -05003395 struct weston_pointer *pointer = seat->pointer_state;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003396
3397 seat->pointer_device_count--;
3398 if (seat->pointer_device_count == 0) {
Derek Foremanf9318d12015-05-11 15:40:11 -05003399 weston_pointer_clear_focus(pointer);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003400 weston_pointer_cancel_grab(pointer);
Jonas Ådahl630bae82013-10-17 23:04:06 +02003401
Jonas Ådahla4932742013-10-17 23:04:07 +02003402 if (pointer->sprite)
3403 pointer_unmap_sprite(pointer);
3404
Jonas Ådahl3e12e632013-12-02 22:05:05 +01003405 weston_pointer_reset_state(pointer);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003406 seat_send_updated_caps(seat);
Derek Foremanfd5ca512015-01-07 15:00:25 -06003407
3408 /* seat->pointer is intentionally not destroyed so that
3409 * a newly attached pointer on this seat will retain
3410 * the previous cursor co-ordinates.
3411 */
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003412 }
3413}
3414
Marius Vladeb34f822021-03-30 23:09:05 +03003415WL_EXPORT int
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003416weston_seat_init_touch(struct weston_seat *seat)
3417{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003418 struct weston_touch *touch;
3419
Derek Foreman1281a362015-07-31 16:55:32 -05003420 if (seat->touch_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003421 seat->touch_device_count += 1;
3422 if (seat->touch_device_count == 1)
3423 seat_send_updated_caps(seat);
Marius Vladeb34f822021-03-30 23:09:05 +03003424 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003425 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003426
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003427 touch = weston_touch_create();
3428 if (touch == NULL)
Marius Vladeb34f822021-03-30 23:09:05 +03003429 return -1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003430
Derek Foreman1281a362015-07-31 16:55:32 -05003431 seat->touch_state = touch;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003432 seat->touch_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003433 touch->seat = seat;
3434
3435 seat_send_updated_caps(seat);
Marius Vladeb34f822021-03-30 23:09:05 +03003436
3437 return 0;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003438}
3439
3440WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003441weston_seat_release_touch(struct weston_seat *seat)
3442{
3443 seat->touch_device_count--;
3444 if (seat->touch_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05003445 weston_touch_set_focus(seat->touch_state, NULL);
3446 weston_touch_cancel_grab(seat->touch_state);
3447 weston_touch_reset_state(seat->touch_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003448 seat_send_updated_caps(seat);
3449 }
3450}
3451
3452WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003453weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
3454 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003455{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003456 memset(seat, 0, sizeof *seat);
3457
Kristian Høgsberge3148752013-05-06 23:19:49 -04003458 seat->selection_data_source = NULL;
3459 wl_list_init(&seat->base_resource_list);
3460 wl_signal_init(&seat->selection_signal);
3461 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04003462 wl_signal_init(&seat->destroy_signal);
Jason Ekstranda4ab5422014-04-02 19:53:45 -05003463 wl_signal_init(&seat->updated_caps_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003464
Sebastian Wickabec5122019-11-01 02:38:45 +01003465 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface,
3466 MIN(wl_seat_interface.version, 7),
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003467 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003468
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003469 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003470 seat->modifier_state = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003471 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003472
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003473 wl_list_insert(ec->seat_list.prev, &seat->link);
3474
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003475 clipboard_create(seat);
3476
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003477 wl_signal_emit(&ec->seat_created_signal, seat);
3478}
3479
3480WL_EXPORT void
3481weston_seat_release(struct weston_seat *seat)
3482{
Alexandros Frantzis8480d132018-02-15 13:07:09 +02003483 struct wl_resource *resource;
3484
3485 wl_resource_for_each(resource, &seat->base_resource_list) {
3486 wl_resource_set_user_data(resource, NULL);
3487 }
3488
3489 wl_resource_for_each(resource, &seat->drag_resource_list) {
3490 wl_resource_set_user_data(resource, NULL);
3491 }
3492
3493 wl_list_remove(&seat->base_resource_list);
3494 wl_list_remove(&seat->drag_resource_list);
3495
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003496 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003497
Jonas Ådahl1afb2382014-01-03 19:46:51 +01003498 if (seat->saved_kbd_focus)
3499 wl_list_remove(&seat->saved_kbd_focus_listener.link);
3500
Derek Foreman1281a362015-07-31 16:55:32 -05003501 if (seat->pointer_state)
3502 weston_pointer_destroy(seat->pointer_state);
3503 if (seat->keyboard_state)
3504 weston_keyboard_destroy(seat->keyboard_state);
3505 if (seat->touch_state)
3506 weston_touch_destroy(seat->touch_state);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003507
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003508 free (seat->seat_name);
3509
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003510 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04003511
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003512 wl_signal_emit(&seat->destroy_signal, seat);
3513}
Derek Foreman1281a362015-07-31 16:55:32 -05003514
3515/** Get a seat's keyboard pointer
3516 *
3517 * \param seat The seat to query
3518 * \return The seat's keyboard pointer, or NULL if no keyboard is present
3519 *
3520 * The keyboard pointer for a seat isn't freed when all keyboards are removed,
3521 * so it should only be used when the seat's keyboard_device_count is greater
3522 * than zero. This function does that test and only returns a pointer
3523 * when a keyboard is present.
3524 */
3525WL_EXPORT struct weston_keyboard *
3526weston_seat_get_keyboard(struct weston_seat *seat)
3527{
3528 if (!seat)
3529 return NULL;
3530
3531 if (seat->keyboard_device_count)
3532 return seat->keyboard_state;
3533
3534 return NULL;
3535}
3536
3537/** Get a seat's pointer pointer
3538 *
3539 * \param seat The seat to query
3540 * \return The seat's pointer pointer, or NULL if no pointer device is present
3541 *
3542 * The pointer pointer for a seat isn't freed when all mice are removed,
3543 * so it should only be used when the seat's pointer_device_count is greater
3544 * than zero. This function does that test and only returns a pointer
3545 * when a pointing device is present.
3546 */
3547WL_EXPORT struct weston_pointer *
3548weston_seat_get_pointer(struct weston_seat *seat)
3549{
3550 if (!seat)
3551 return NULL;
3552
3553 if (seat->pointer_device_count)
3554 return seat->pointer_state;
3555
3556 return NULL;
3557}
3558
Jonas Ådahld3414f22016-07-22 17:56:31 +08003559static const struct zwp_locked_pointer_v1_interface locked_pointer_interface;
3560static const struct zwp_confined_pointer_v1_interface confined_pointer_interface;
3561
3562static enum pointer_constraint_type
3563pointer_constraint_get_type(struct weston_pointer_constraint *constraint)
3564{
3565 if (wl_resource_instance_of(constraint->resource,
3566 &zwp_locked_pointer_v1_interface,
3567 &locked_pointer_interface)) {
3568 return POINTER_CONSTRAINT_TYPE_LOCK;
3569 } else if (wl_resource_instance_of(constraint->resource,
3570 &zwp_confined_pointer_v1_interface,
3571 &confined_pointer_interface)) {
3572 return POINTER_CONSTRAINT_TYPE_CONFINE;
3573 }
3574
3575 abort();
3576 return 0;
3577}
3578
3579static void
3580pointer_constraint_notify_activated(struct weston_pointer_constraint *constraint)
3581{
3582 struct wl_resource *resource = constraint->resource;
3583
3584 switch (pointer_constraint_get_type(constraint)) {
3585 case POINTER_CONSTRAINT_TYPE_LOCK:
3586 zwp_locked_pointer_v1_send_locked(resource);
3587 break;
3588 case POINTER_CONSTRAINT_TYPE_CONFINE:
3589 zwp_confined_pointer_v1_send_confined(resource);
3590 break;
3591 }
3592}
3593
3594static void
3595pointer_constraint_notify_deactivated(struct weston_pointer_constraint *constraint)
3596{
3597 struct wl_resource *resource = constraint->resource;
3598
3599 switch (pointer_constraint_get_type(constraint)) {
3600 case POINTER_CONSTRAINT_TYPE_LOCK:
3601 zwp_locked_pointer_v1_send_unlocked(resource);
3602 break;
3603 case POINTER_CONSTRAINT_TYPE_CONFINE:
3604 zwp_confined_pointer_v1_send_unconfined(resource);
3605 break;
3606 }
3607}
3608
3609static struct weston_pointer_constraint *
3610get_pointer_constraint_for_pointer(struct weston_surface *surface,
3611 struct weston_pointer *pointer)
3612{
3613 struct weston_pointer_constraint *constraint;
3614
3615 wl_list_for_each(constraint, &surface->pointer_constraints, link) {
3616 if (constraint->pointer == pointer)
3617 return constraint;
3618 }
3619
3620 return NULL;
3621}
3622
Derek Foreman1281a362015-07-31 16:55:32 -05003623/** Get a seat's touch pointer
3624 *
3625 * \param seat The seat to query
3626 * \return The seat's touch pointer, or NULL if no touch device is present
3627 *
3628 * The touch pointer for a seat isn't freed when all touch devices are removed,
3629 * so it should only be used when the seat's touch_device_count is greater
3630 * than zero. This function does that test and only returns a pointer
3631 * when a touch device is present.
3632 */
3633WL_EXPORT struct weston_touch *
3634weston_seat_get_touch(struct weston_seat *seat)
3635{
3636 if (!seat)
3637 return NULL;
3638
3639 if (seat->touch_device_count)
3640 return seat->touch_state;
3641
3642 return NULL;
3643}
Bryce Harrington24f917e2016-06-29 19:04:07 -07003644
3645/** Sets the keyboard focus to the given surface
3646 *
Marius Vlada2dace22019-06-12 16:05:44 +03003647 * \param surface the surface to focus on
Bryce Harrington24f917e2016-06-29 19:04:07 -07003648 * \param seat The seat to query
3649 */
3650WL_EXPORT void
3651weston_seat_set_keyboard_focus(struct weston_seat *seat,
3652 struct weston_surface *surface)
3653{
3654 struct weston_compositor *compositor = seat->compositor;
3655 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003656 struct weston_surface_activation_data activation_data;
Bryce Harrington24f917e2016-06-29 19:04:07 -07003657
Jonas Ådahlef8e1c32016-03-15 20:28:51 +08003658 if (keyboard && keyboard->focus != surface) {
Bryce Harrington24f917e2016-06-29 19:04:07 -07003659 weston_keyboard_set_focus(keyboard, surface);
3660 wl_data_device_set_keyboard_focus(seat);
3661 }
3662
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003663 inc_activate_serial(compositor);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003664
3665 activation_data = (struct weston_surface_activation_data) {
3666 .surface = surface,
3667 .seat = seat,
3668 };
3669 wl_signal_emit(&compositor->activate_signal, &activation_data);
Bryce Harrington24f917e2016-06-29 19:04:07 -07003670}
Jonas Ådahl30d61d82014-10-22 21:21:17 +02003671
Jonas Ådahld3414f22016-07-22 17:56:31 +08003672static void
3673enable_pointer_constraint(struct weston_pointer_constraint *constraint,
3674 struct weston_view *view)
3675{
3676 assert(constraint->view == NULL);
3677 constraint->view = view;
3678 pointer_constraint_notify_activated(constraint);
3679 weston_pointer_start_grab(constraint->pointer, &constraint->grab);
3680 wl_list_remove(&constraint->surface_destroy_listener.link);
3681 wl_list_init(&constraint->surface_destroy_listener.link);
3682}
3683
3684static bool
3685is_pointer_constraint_enabled(struct weston_pointer_constraint *constraint)
3686{
3687 return constraint->view != NULL;
3688}
3689
3690static void
3691weston_pointer_constraint_disable(struct weston_pointer_constraint *constraint)
3692{
3693 constraint->view = NULL;
3694 pointer_constraint_notify_deactivated(constraint);
3695 weston_pointer_end_grab(constraint->grab.pointer);
3696}
3697
3698void
3699weston_pointer_constraint_destroy(struct weston_pointer_constraint *constraint)
3700{
3701 if (is_pointer_constraint_enabled(constraint))
3702 weston_pointer_constraint_disable(constraint);
3703
3704 wl_list_remove(&constraint->pointer_destroy_listener.link);
3705 wl_list_remove(&constraint->surface_destroy_listener.link);
3706 wl_list_remove(&constraint->surface_commit_listener.link);
3707 wl_list_remove(&constraint->surface_activate_listener.link);
3708
3709 wl_resource_set_user_data(constraint->resource, NULL);
3710 pixman_region32_fini(&constraint->region);
3711 wl_list_remove(&constraint->link);
3712 free(constraint);
3713}
3714
3715static void
3716disable_pointer_constraint(struct weston_pointer_constraint *constraint)
3717{
3718 switch (constraint->lifetime) {
3719 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT:
3720 weston_pointer_constraint_destroy(constraint);
3721 break;
3722 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT:
3723 weston_pointer_constraint_disable(constraint);
3724 break;
3725 }
3726}
3727
3728static bool
3729is_within_constraint_region(struct weston_pointer_constraint *constraint,
3730 wl_fixed_t sx, wl_fixed_t sy)
3731{
3732 struct weston_surface *surface = constraint->surface;
3733 pixman_region32_t constraint_region;
3734 bool result;
3735
3736 pixman_region32_init(&constraint_region);
3737 pixman_region32_intersect(&constraint_region,
3738 &surface->input,
3739 &constraint->region);
3740 result = pixman_region32_contains_point(&constraint_region,
3741 wl_fixed_to_int(sx),
3742 wl_fixed_to_int(sy),
3743 NULL);
3744 pixman_region32_fini(&constraint_region);
3745
3746 return result;
3747}
3748
3749static void
3750maybe_enable_pointer_constraint(struct weston_pointer_constraint *constraint)
3751{
3752 struct weston_surface *surface = constraint->surface;
3753 struct weston_view *vit;
3754 struct weston_view *view = NULL;
3755 struct weston_pointer *pointer = constraint->pointer;
3756 struct weston_keyboard *keyboard;
3757 struct weston_seat *seat = pointer->seat;
3758 int32_t x, y;
3759
3760 /* Postpone if no view of the surface was most recently clicked. */
3761 wl_list_for_each(vit, &surface->views, surface_link) {
3762 if (vit->click_to_activate_serial ==
3763 surface->compositor->activate_serial) {
3764 view = vit;
3765 }
3766 }
3767 if (view == NULL)
3768 return;
3769
3770 /* Postpone if surface doesn't have keyboard focus. */
3771 keyboard = weston_seat_get_keyboard(seat);
3772 if (!keyboard || keyboard->focus != surface)
3773 return;
3774
3775 /* Postpone constraint if the pointer is not within the
3776 * constraint region.
3777 */
3778 weston_view_from_global(view,
3779 wl_fixed_to_int(pointer->x),
3780 wl_fixed_to_int(pointer->y),
3781 &x, &y);
3782 if (!is_within_constraint_region(constraint,
3783 wl_fixed_from_int(x),
3784 wl_fixed_from_int(y)))
3785 return;
3786
3787 enable_pointer_constraint(constraint, view);
3788}
3789
3790static void
3791locked_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
3792{
3793}
3794
3795static void
3796locked_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02003797 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003798 struct weston_pointer_motion_event *event)
3799{
Quentin Glidiccde13452016-08-12 10:41:32 +02003800 pointer_send_relative_motion(grab->pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08003801}
3802
3803static void
3804locked_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003805 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003806 uint32_t button,
3807 uint32_t state_w)
3808{
3809 weston_pointer_send_button(grab->pointer, time, button, state_w);
3810}
3811
3812static void
3813locked_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02003814 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003815 struct weston_pointer_axis_event *event)
3816{
3817 weston_pointer_send_axis(grab->pointer, time, event);
3818}
3819
3820static void
3821locked_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
3822 uint32_t source)
3823{
3824 weston_pointer_send_axis_source(grab->pointer, source);
3825}
3826
3827static void
3828locked_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
3829{
3830 weston_pointer_send_frame(grab->pointer);
3831}
3832
3833static void
3834locked_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
3835{
3836 struct weston_pointer_constraint *constraint =
3837 container_of(grab, struct weston_pointer_constraint, grab);
3838
3839 disable_pointer_constraint(constraint);
3840}
3841
3842static const struct weston_pointer_grab_interface
3843 locked_pointer_grab_interface = {
3844 locked_pointer_grab_pointer_focus,
3845 locked_pointer_grab_pointer_motion,
3846 locked_pointer_grab_pointer_button,
3847 locked_pointer_grab_pointer_axis,
3848 locked_pointer_grab_pointer_axis_source,
3849 locked_pointer_grab_pointer_frame,
3850 locked_pointer_grab_pointer_cancel,
3851};
3852
3853static void
3854pointer_constraint_constrain_resource_destroyed(struct wl_resource *resource)
3855{
3856 struct weston_pointer_constraint *constraint =
3857 wl_resource_get_user_data(resource);
3858
3859 if (!constraint)
3860 return;
3861
3862 weston_pointer_constraint_destroy(constraint);
3863}
3864
3865static void
3866pointer_constraint_surface_activate(struct wl_listener *listener, void *data)
3867{
3868 struct weston_surface_activation_data *activation = data;
3869 struct weston_pointer *pointer;
3870 struct weston_surface *focus = activation->surface;
3871 struct weston_pointer_constraint *constraint =
3872 container_of(listener, struct weston_pointer_constraint,
3873 surface_activate_listener);
3874 bool is_constraint_surface;
3875
3876 pointer = weston_seat_get_pointer(activation->seat);
3877 if (!pointer)
3878 return;
3879
3880 is_constraint_surface =
3881 get_pointer_constraint_for_pointer(focus, pointer) == constraint;
3882
3883 if (is_constraint_surface &&
3884 !is_pointer_constraint_enabled(constraint))
3885 maybe_enable_pointer_constraint(constraint);
3886 else if (!is_constraint_surface &&
3887 is_pointer_constraint_enabled(constraint))
3888 disable_pointer_constraint(constraint);
3889}
3890
3891static void
3892pointer_constraint_pointer_destroyed(struct wl_listener *listener, void *data)
3893{
3894 struct weston_pointer_constraint *constraint =
3895 container_of(listener, struct weston_pointer_constraint,
3896 pointer_destroy_listener);
3897
3898 weston_pointer_constraint_destroy(constraint);
3899}
3900
3901static void
3902pointer_constraint_surface_destroyed(struct wl_listener *listener, void *data)
3903{
3904 struct weston_pointer_constraint *constraint =
3905 container_of(listener, struct weston_pointer_constraint,
3906 surface_destroy_listener);
3907
3908 weston_pointer_constraint_destroy(constraint);
3909}
3910
3911static void
3912pointer_constraint_surface_committed(struct wl_listener *listener, void *data)
3913{
3914 struct weston_pointer_constraint *constraint =
3915 container_of(listener, struct weston_pointer_constraint,
3916 surface_commit_listener);
3917
3918 if (constraint->region_is_pending) {
3919 constraint->region_is_pending = false;
3920 pixman_region32_copy(&constraint->region,
3921 &constraint->region_pending);
3922 pixman_region32_fini(&constraint->region_pending);
3923 pixman_region32_init(&constraint->region_pending);
3924 }
3925
3926 if (constraint->hint_is_pending) {
3927 constraint->hint_is_pending = false;
3928
3929 constraint->hint_is_pending = true;
3930 constraint->hint_x = constraint->hint_x_pending;
3931 constraint->hint_y = constraint->hint_y_pending;
3932 }
3933
3934 if (pointer_constraint_get_type(constraint) ==
3935 POINTER_CONSTRAINT_TYPE_CONFINE &&
3936 is_pointer_constraint_enabled(constraint))
3937 maybe_warp_confined_pointer(constraint);
3938}
3939
3940static struct weston_pointer_constraint *
3941weston_pointer_constraint_create(struct weston_surface *surface,
3942 struct weston_pointer *pointer,
3943 struct weston_region *region,
3944 enum zwp_pointer_constraints_v1_lifetime lifetime,
3945 struct wl_resource *cr,
3946 const struct weston_pointer_grab_interface *grab_interface)
3947{
3948 struct weston_pointer_constraint *constraint;
3949
3950 constraint = zalloc(sizeof *constraint);
3951 if (!constraint)
3952 return NULL;
3953
3954 constraint->lifetime = lifetime;
3955 pixman_region32_init(&constraint->region);
3956 pixman_region32_init(&constraint->region_pending);
3957 wl_list_insert(&surface->pointer_constraints, &constraint->link);
3958 constraint->surface = surface;
3959 constraint->pointer = pointer;
3960 constraint->resource = cr;
3961 constraint->grab.interface = grab_interface;
3962 if (region) {
3963 pixman_region32_copy(&constraint->region,
3964 &region->region);
3965 } else {
3966 pixman_region32_fini(&constraint->region);
3967 region_init_infinite(&constraint->region);
3968 }
3969
3970 constraint->surface_activate_listener.notify =
3971 pointer_constraint_surface_activate;
3972 constraint->surface_destroy_listener.notify =
3973 pointer_constraint_surface_destroyed;
3974 constraint->surface_commit_listener.notify =
3975 pointer_constraint_surface_committed;
3976 constraint->pointer_destroy_listener.notify =
3977 pointer_constraint_pointer_destroyed;
3978
3979 wl_signal_add(&surface->compositor->activate_signal,
3980 &constraint->surface_activate_listener);
3981 wl_signal_add(&pointer->destroy_signal,
3982 &constraint->pointer_destroy_listener);
3983 wl_signal_add(&surface->destroy_signal,
3984 &constraint->surface_destroy_listener);
3985 wl_signal_add(&surface->commit_signal,
3986 &constraint->surface_commit_listener);
3987
3988 return constraint;
3989}
3990
3991static void
3992init_pointer_constraint(struct wl_resource *pointer_constraints_resource,
3993 uint32_t id,
3994 struct weston_surface *surface,
3995 struct weston_pointer *pointer,
3996 struct weston_region *region,
3997 enum zwp_pointer_constraints_v1_lifetime lifetime,
3998 const struct wl_interface *interface,
3999 const void *implementation,
4000 const struct weston_pointer_grab_interface *grab_interface)
4001{
4002 struct wl_client *client =
4003 wl_resource_get_client(pointer_constraints_resource);
4004 struct wl_resource *cr;
4005 struct weston_pointer_constraint *constraint;
4006
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02004007 if (pointer && get_pointer_constraint_for_pointer(surface, pointer)) {
Jonas Ådahld3414f22016-07-22 17:56:31 +08004008 wl_resource_post_error(pointer_constraints_resource,
4009 ZWP_POINTER_CONSTRAINTS_V1_ERROR_ALREADY_CONSTRAINED,
4010 "the pointer has a lock/confine request on this surface");
4011 return;
4012 }
4013
4014 cr = wl_resource_create(client, interface,
4015 wl_resource_get_version(pointer_constraints_resource),
4016 id);
4017 if (cr == NULL) {
4018 wl_client_post_no_memory(client);
4019 return;
4020 }
4021
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02004022 if (pointer) {
4023 constraint = weston_pointer_constraint_create(surface, pointer,
4024 region, lifetime,
4025 cr, grab_interface);
4026 if (constraint == NULL) {
4027 wl_client_post_no_memory(client);
4028 return;
4029 }
4030 } else {
4031 constraint = NULL;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004032 }
4033
4034 wl_resource_set_implementation(cr, implementation, constraint,
4035 pointer_constraint_constrain_resource_destroyed);
4036
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02004037 if (constraint)
4038 maybe_enable_pointer_constraint(constraint);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004039}
4040
4041static void
4042pointer_constraints_destroy(struct wl_client *client,
4043 struct wl_resource *resource)
4044{
4045 wl_resource_destroy(resource);
4046}
4047
4048static void
4049locked_pointer_destroy(struct wl_client *client,
4050 struct wl_resource *resource)
4051{
4052 struct weston_pointer_constraint *constraint =
4053 wl_resource_get_user_data(resource);
4054 wl_fixed_t x, y;
4055
4056 if (constraint && constraint->view && constraint->hint_is_pending &&
4057 is_within_constraint_region(constraint,
4058 constraint->hint_x,
4059 constraint->hint_y)) {
4060 weston_view_to_global_fixed(constraint->view,
4061 constraint->hint_x,
4062 constraint->hint_y,
4063 &x, &y);
4064 weston_pointer_move_to(constraint->pointer, x, y);
4065 }
4066 wl_resource_destroy(resource);
4067}
4068
4069static void
4070locked_pointer_set_cursor_position_hint(struct wl_client *client,
4071 struct wl_resource *resource,
4072 wl_fixed_t surface_x,
4073 wl_fixed_t surface_y)
4074{
4075 struct weston_pointer_constraint *constraint =
4076 wl_resource_get_user_data(resource);
4077
4078 /* Ignore a set cursor hint that was sent after the lock was cancelled.
4079 */
4080 if (!constraint ||
4081 !constraint->resource ||
4082 constraint->resource != resource)
4083 return;
4084
4085 constraint->hint_is_pending = true;
4086 constraint->hint_x_pending = surface_x;
4087 constraint->hint_y_pending = surface_y;
4088}
4089
4090static void
4091locked_pointer_set_region(struct wl_client *client,
4092 struct wl_resource *resource,
4093 struct wl_resource *region_resource)
4094{
4095 struct weston_pointer_constraint *constraint =
4096 wl_resource_get_user_data(resource);
4097 struct weston_region *region = region_resource ?
4098 wl_resource_get_user_data(region_resource) : NULL;
4099
4100 if (!constraint)
4101 return;
4102
4103 if (region) {
4104 pixman_region32_copy(&constraint->region_pending,
4105 &region->region);
4106 } else {
4107 pixman_region32_fini(&constraint->region_pending);
4108 region_init_infinite(&constraint->region_pending);
4109 }
4110 constraint->region_is_pending = true;
4111}
4112
4113
4114static const struct zwp_locked_pointer_v1_interface locked_pointer_interface = {
4115 locked_pointer_destroy,
4116 locked_pointer_set_cursor_position_hint,
4117 locked_pointer_set_region,
4118};
4119
4120static void
4121pointer_constraints_lock_pointer(struct wl_client *client,
4122 struct wl_resource *resource,
4123 uint32_t id,
4124 struct wl_resource *surface_resource,
4125 struct wl_resource *pointer_resource,
4126 struct wl_resource *region_resource,
4127 uint32_t lifetime)
4128{
4129 struct weston_surface *surface =
4130 wl_resource_get_user_data(surface_resource);
4131 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
4132 struct weston_region *region = region_resource ?
4133 wl_resource_get_user_data(region_resource) : NULL;
4134
4135 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
4136 &zwp_locked_pointer_v1_interface,
4137 &locked_pointer_interface,
4138 &locked_pointer_grab_interface);
4139}
4140
4141static void
4142confined_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
4143{
4144}
4145
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004146static double
4147vec2d_cross_product(struct vec2d a, struct vec2d b)
4148{
4149 return a.x * b.y - a.y * b.x;
4150}
4151
4152static struct vec2d
4153vec2d_add(struct vec2d a, struct vec2d b)
4154{
4155 return (struct vec2d) {
4156 .x = a.x + b.x,
4157 .y = a.y + b.y,
4158 };
4159}
4160
4161static struct vec2d
4162vec2d_subtract(struct vec2d a, struct vec2d b)
4163{
4164 return (struct vec2d) {
4165 .x = a.x - b.x,
4166 .y = a.y - b.y,
4167 };
4168}
4169
4170static struct vec2d
4171vec2d_multiply_constant(double c, struct vec2d a)
4172{
4173 return (struct vec2d) {
4174 .x = c * a.x,
4175 .y = c * a.y,
4176 };
4177}
4178
4179static bool
4180lines_intersect(struct line *line1, struct line *line2,
4181 struct vec2d *intersection)
4182{
4183 struct vec2d p = line1->a;
4184 struct vec2d r = vec2d_subtract(line1->b, line1->a);
4185 struct vec2d q = line2->a;
4186 struct vec2d s = vec2d_subtract(line2->b, line2->a);
4187 double rxs;
4188 double sxr;
4189 double t;
4190 double u;
4191
4192 /*
4193 * The line (p, r) and (q, s) intersects where
4194 *
4195 * p + t r = q + u s
4196 *
4197 * Calculate t:
4198 *
4199 * (p + t r) × s = (q + u s) × s
4200 * p × s + t (r × s) = q × s + u (s × s)
4201 * p × s + t (r × s) = q × s
4202 * t (r × s) = q × s - p × s
4203 * t (r × s) = (q - p) × s
4204 * t = ((q - p) × s) / (r × s)
4205 *
4206 * Using the same method, for u we get:
4207 *
4208 * u = ((p - q) × r) / (s × r)
4209 */
4210
4211 rxs = vec2d_cross_product(r, s);
4212 sxr = vec2d_cross_product(s, r);
4213
4214 /* If r × s = 0 then the lines are either parallel or collinear. */
4215 if (fabs(rxs) < DBL_MIN)
4216 return false;
4217
4218 t = vec2d_cross_product(vec2d_subtract(q, p), s) / rxs;
4219 u = vec2d_cross_product(vec2d_subtract(p, q), r) / sxr;
4220
4221 /* The lines only intersect if 0 ≤ t ≤ 1 and 0 ≤ u ≤ 1. */
4222 if (t < 0.0 || t > 1.0 || u < 0.0 || u > 1.0)
4223 return false;
4224
4225 *intersection = vec2d_add(p, vec2d_multiply_constant(t, r));
4226 return true;
4227}
4228
4229static struct border *
4230add_border(struct wl_array *array,
4231 double x1, double y1,
4232 double x2, double y2,
4233 enum motion_direction blocking_dir)
4234{
4235 struct border *border = wl_array_add(array, sizeof *border);
4236
4237 *border = (struct border) {
4238 .line = (struct line) {
4239 .a = (struct vec2d) {
4240 .x = x1,
4241 .y = y1,
4242 },
4243 .b = (struct vec2d) {
4244 .x = x2,
4245 .y = y2,
4246 },
4247 },
4248 .blocking_dir = blocking_dir,
4249 };
4250
4251 return border;
4252}
4253
4254static int
4255compare_lines_x(const void *a, const void *b)
4256{
4257 const struct border *border_a = a;
4258 const struct border *border_b = b;
4259
4260
4261 if (border_a->line.a.x == border_b->line.a.x)
4262 return border_a->line.b.x < border_b->line.b.x;
4263 else
4264 return border_a->line.a.x > border_b->line.a.x;
4265}
4266
4267static void
4268add_non_overlapping_edges(pixman_box32_t *boxes,
4269 int band_above_start,
4270 int band_below_start,
4271 int band_below_end,
4272 struct wl_array *borders)
4273{
4274 int i;
4275 struct wl_array band_merge;
4276 struct border *border;
4277 struct border *prev_border;
4278 struct border *new_border;
4279
4280 wl_array_init(&band_merge);
4281
4282 /* Add bottom band of previous row, and top band of current row, and
4283 * sort them so lower left x coordinate comes first. If there are two
4284 * borders with the same left x coordinate, the wider one comes first.
4285 */
4286 for (i = band_above_start; i < band_below_start; i++) {
4287 pixman_box32_t *box = &boxes[i];
4288 add_border(&band_merge, box->x1, box->y2, box->x2, box->y2,
4289 MOTION_DIRECTION_POSITIVE_Y);
4290 }
4291 for (i = band_below_start; i < band_below_end; i++) {
4292 pixman_box32_t *box= &boxes[i];
4293 add_border(&band_merge, box->x1, box->y1, box->x2, box->y1,
4294 MOTION_DIRECTION_NEGATIVE_Y);
4295 }
4296 qsort(band_merge.data,
4297 band_merge.size / sizeof *border,
4298 sizeof *border,
4299 compare_lines_x);
4300
4301 /* Combine the two combined bands so that any overlapping border is
4302 * eliminated. */
4303 prev_border = NULL;
4304 wl_array_for_each(border, &band_merge) {
4305 assert(border->line.a.y == border->line.b.y);
4306 assert(!prev_border ||
4307 prev_border->line.a.y == border->line.a.y);
4308 assert(!prev_border ||
4309 (prev_border->line.a.x != border->line.a.x ||
4310 prev_border->line.b.x != border->line.b.x));
4311 assert(!prev_border ||
4312 prev_border->line.a.x <= border->line.a.x);
4313
4314 if (prev_border &&
4315 prev_border->line.a.x == border->line.a.x) {
4316 /*
4317 * ------------ +
4318 * ------- =
4319 * [ ]-----
4320 */
4321 prev_border->line.a.x = border->line.b.x;
4322 } else if (prev_border &&
4323 prev_border->line.b.x == border->line.b.x) {
4324 /*
4325 * ------------ +
4326 * ------ =
4327 * ------[ ]
4328 */
4329 prev_border->line.b.x = border->line.a.x;
4330 } else if (prev_border &&
4331 prev_border->line.b.x == border->line.a.x) {
4332 /*
4333 * -------- +
4334 * ------ =
4335 * --------------
4336 */
4337 prev_border->line.b.x = border->line.b.x;
4338 } else if (prev_border &&
4339 prev_border->line.b.x >= border->line.a.x) {
4340 /*
4341 * --------------- +
4342 * ------ =
4343 * -----[ ]----
4344 */
4345 new_border = add_border(borders,
4346 border->line.b.x,
4347 border->line.b.y,
4348 prev_border->line.b.x,
4349 prev_border->line.b.y,
4350 prev_border->blocking_dir);
4351 prev_border->line.b.x = border->line.a.x;
4352 prev_border = new_border;
4353 } else {
4354 assert(!prev_border ||
4355 prev_border->line.b.x < border->line.a.x);
4356 /*
4357 * First border or non-overlapping.
4358 *
4359 * ----- +
4360 * ----- =
4361 * ----- -----
4362 */
4363 new_border = wl_array_add(borders, sizeof *border);
4364 *new_border = *border;
4365 prev_border = new_border;
4366 }
4367 }
4368
4369 wl_array_release(&band_merge);
4370}
4371
4372static void
4373add_band_bottom_edges(pixman_box32_t *boxes,
4374 int band_start,
4375 int band_end,
4376 struct wl_array *borders)
4377{
4378 int i;
4379
4380 for (i = band_start; i < band_end; i++) {
4381 add_border(borders,
4382 boxes[i].x1, boxes[i].y2,
4383 boxes[i].x2, boxes[i].y2,
4384 MOTION_DIRECTION_POSITIVE_Y);
4385 }
4386}
4387
4388static void
4389region_to_outline(pixman_region32_t *region, struct wl_array *borders)
4390{
4391 pixman_box32_t *boxes;
4392 int num_boxes;
4393 int i;
4394 int top_most, bottom_most;
4395 int current_roof;
4396 int prev_top;
4397 int band_start, prev_band_start;
4398
4399 /*
4400 * Remove any overlapping lines from the set of rectangles. Note that
4401 * pixman regions are grouped as rows of rectangles, where rectangles
4402 * in one row never touch or overlap and are all of the same height.
4403 *
4404 * -------- --- -------- ---
4405 * | | | | | | | |
4406 * ----------====---- --- ----------- ----- ---
4407 * | | => | |
4408 * ----==========--------- ----- ----------
4409 * | | | |
4410 * ------------------- -------------------
4411 *
4412 */
4413
4414 boxes = pixman_region32_rectangles(region, &num_boxes);
4415 prev_top = 0;
4416 top_most = boxes[0].y1;
4417 current_roof = top_most;
4418 bottom_most = boxes[num_boxes - 1].y2;
4419 band_start = 0;
4420 prev_band_start = 0;
4421 for (i = 0; i < num_boxes; i++) {
4422 /* Detect if there is a vertical empty space, and add the lower
4423 * level of the previous band if so was the case. */
4424 if (i > 0 &&
4425 boxes[i].y1 != prev_top &&
4426 boxes[i].y1 != boxes[i - 1].y2) {
4427 current_roof = boxes[i].y1;
4428 add_band_bottom_edges(boxes,
4429 band_start,
4430 i,
4431 borders);
4432 }
4433
4434 /* Special case adding the last band, since it won't be handled
4435 * by the band change detection below. */
4436 if (boxes[i].y1 != current_roof && i == num_boxes - 1) {
4437 if (boxes[i].y1 != prev_top) {
4438 /* The last band is a single box, so we don't
4439 * have a prev_band_start to tell us when the
4440 * previous band started. */
4441 add_non_overlapping_edges(boxes,
4442 band_start,
4443 i,
4444 i + 1,
4445 borders);
4446 } else {
4447 add_non_overlapping_edges(boxes,
4448 prev_band_start,
4449 band_start,
4450 i + 1,
4451 borders);
4452 }
4453 }
4454
4455 /* Detect when passing a band and combine the top border of the
4456 * just passed band with the bottom band of the previous band.
4457 */
4458 if (boxes[i].y1 != top_most && boxes[i].y1 != prev_top) {
4459 /* Combine the two passed bands. */
4460 if (prev_top != current_roof) {
4461 add_non_overlapping_edges(boxes,
4462 prev_band_start,
4463 band_start,
4464 i,
4465 borders);
4466 }
4467
4468 prev_band_start = band_start;
4469 band_start = i;
4470 }
4471
4472 /* Add the top border if the box is part of the current roof. */
4473 if (boxes[i].y1 == current_roof) {
4474 add_border(borders,
4475 boxes[i].x1, boxes[i].y1,
4476 boxes[i].x2, boxes[i].y1,
4477 MOTION_DIRECTION_NEGATIVE_Y);
4478 }
4479
4480 /* Add the bottom border of the last band. */
4481 if (boxes[i].y2 == bottom_most) {
4482 add_border(borders,
4483 boxes[i].x1, boxes[i].y2,
4484 boxes[i].x2, boxes[i].y2,
4485 MOTION_DIRECTION_POSITIVE_Y);
4486 }
4487
4488 /* Always add the left border. */
4489 add_border(borders,
4490 boxes[i].x1, boxes[i].y1,
4491 boxes[i].x1, boxes[i].y2,
4492 MOTION_DIRECTION_NEGATIVE_X);
4493
4494 /* Always add the right border. */
4495 add_border(borders,
4496 boxes[i].x2, boxes[i].y1,
4497 boxes[i].x2, boxes[i].y2,
4498 MOTION_DIRECTION_POSITIVE_X);
4499
4500 prev_top = boxes[i].y1;
4501 }
4502}
4503
4504static bool
4505is_border_horizontal (struct border *border)
4506{
4507 return border->line.a.y == border->line.b.y;
4508}
4509
4510static bool
4511is_border_blocking_directions(struct border *border,
4512 uint32_t directions)
4513{
4514 /* Don't block parallel motions. */
4515 if (is_border_horizontal(border)) {
4516 if ((directions & (MOTION_DIRECTION_POSITIVE_Y |
4517 MOTION_DIRECTION_NEGATIVE_Y)) == 0)
4518 return false;
4519 } else {
4520 if ((directions & (MOTION_DIRECTION_POSITIVE_X |
4521 MOTION_DIRECTION_NEGATIVE_X)) == 0)
4522 return false;
4523 }
4524
4525 return (~border->blocking_dir & directions) != directions;
4526}
4527
4528static struct border *
4529get_closest_border(struct wl_array *borders,
4530 struct line *motion,
4531 uint32_t directions)
4532{
4533 struct border *border;
4534 struct vec2d intersection;
4535 struct vec2d delta;
4536 double distance_2;
4537 struct border *closest_border = NULL;
4538 double closest_distance_2 = DBL_MAX;
4539
4540 wl_array_for_each(border, borders) {
4541 if (!is_border_blocking_directions(border, directions))
4542 continue;
4543
4544 if (!lines_intersect(&border->line, motion, &intersection))
4545 continue;
4546
4547 delta = vec2d_subtract(intersection, motion->a);
4548 distance_2 = delta.x*delta.x + delta.y*delta.y;
4549 if (distance_2 < closest_distance_2) {
4550 closest_border = border;
4551 closest_distance_2 = distance_2;
4552 }
4553 }
4554
4555 return closest_border;
4556}
4557
4558static void
4559clamp_to_border(struct border *border,
4560 struct line *motion,
4561 uint32_t *motion_dir)
4562{
4563 /*
4564 * When clamping either rightward or downward motions, the motion needs
4565 * to be clamped so that the destination coordinate does not end up on
4566 * the border (see weston_pointer_clamp_event_to_region). Do this by
4567 * clamping such motions to the border minus the smallest possible
4568 * wl_fixed_t value.
4569 */
4570 if (is_border_horizontal(border)) {
4571 if (*motion_dir & MOTION_DIRECTION_POSITIVE_Y)
4572 motion->b.y = border->line.a.y - wl_fixed_to_double(1);
4573 else
4574 motion->b.y = border->line.a.y;
4575 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_Y |
4576 MOTION_DIRECTION_NEGATIVE_Y);
4577 } else {
4578 if (*motion_dir & MOTION_DIRECTION_POSITIVE_X)
4579 motion->b.x = border->line.a.x - wl_fixed_to_double(1);
4580 else
4581 motion->b.x = border->line.a.x;
4582 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_X |
4583 MOTION_DIRECTION_NEGATIVE_X);
4584 }
4585}
4586
4587static uint32_t
4588get_motion_directions(struct line *motion)
4589{
4590 uint32_t directions = 0;
4591
4592 if (motion->a.x < motion->b.x)
4593 directions |= MOTION_DIRECTION_POSITIVE_X;
4594 else if (motion->a.x > motion->b.x)
4595 directions |= MOTION_DIRECTION_NEGATIVE_X;
4596 if (motion->a.y < motion->b.y)
4597 directions |= MOTION_DIRECTION_POSITIVE_Y;
4598 else if (motion->a.y > motion->b.y)
4599 directions |= MOTION_DIRECTION_NEGATIVE_Y;
4600
4601 return directions;
4602}
4603
Jonas Ådahld3414f22016-07-22 17:56:31 +08004604static void
4605weston_pointer_clamp_event_to_region(struct weston_pointer *pointer,
4606 struct weston_pointer_motion_event *event,
4607 pixman_region32_t *region,
4608 wl_fixed_t *clamped_x,
4609 wl_fixed_t *clamped_y)
4610{
4611 wl_fixed_t x, y;
4612 wl_fixed_t sx, sy;
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004613 wl_fixed_t old_sx = pointer->sx;
4614 wl_fixed_t old_sy = pointer->sy;
4615 struct wl_array borders;
4616 struct line motion;
4617 struct border *closest_border;
4618 float new_x_f, new_y_f;
4619 uint32_t directions;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004620
4621 weston_pointer_motion_to_abs(pointer, event, &x, &y);
4622 weston_view_from_global_fixed(pointer->focus, x, y, &sx, &sy);
4623
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004624 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004625
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004626 /*
4627 * Generate borders given the confine region we are to use. The borders
4628 * are defined to be the outer region of the allowed area. This means
4629 * top/left borders are "within" the allowed area, while bottom/right
4630 * borders are outside. This needs to be considered when clamping
4631 * confined motion vectors.
4632 */
4633 region_to_outline(region, &borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004634
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004635 motion = (struct line) {
4636 .a = (struct vec2d) {
4637 .x = wl_fixed_to_double(old_sx),
4638 .y = wl_fixed_to_double(old_sy),
4639 },
4640 .b = (struct vec2d) {
4641 .x = wl_fixed_to_double(sx),
4642 .y = wl_fixed_to_double(sy),
4643 },
4644 };
4645 directions = get_motion_directions(&motion);
4646
4647 while (directions) {
4648 closest_border = get_closest_border(&borders,
4649 &motion,
4650 directions);
4651 if (closest_border)
4652 clamp_to_border(closest_border, &motion, &directions);
4653 else
4654 break;
4655 }
4656
4657 weston_view_to_global_float(pointer->focus,
4658 (float) motion.b.x, (float) motion.b.y,
4659 &new_x_f, &new_y_f);
4660 *clamped_x = wl_fixed_from_double(new_x_f);
4661 *clamped_y = wl_fixed_from_double(new_y_f);
4662
4663 wl_array_release(&borders);
4664}
4665
4666static double
4667point_to_border_distance_2(struct border *border, double x, double y)
4668{
4669 double orig_x, orig_y;
4670 double dx, dy;
4671
4672 if (is_border_horizontal(border)) {
4673 if (x < border->line.a.x)
4674 orig_x = border->line.a.x;
4675 else if (x > border->line.b.x)
4676 orig_x = border->line.b.x;
4677 else
4678 orig_x = x;
4679 orig_y = border->line.a.y;
4680 } else {
4681 if (y < border->line.a.y)
4682 orig_y = border->line.a.y;
4683 else if (y > border->line.b.y)
4684 orig_y = border->line.b.y;
4685 else
4686 orig_y = y;
4687 orig_x = border->line.a.x;
4688 }
4689
4690
4691 dx = fabs(orig_x - x);
4692 dy = fabs(orig_y - y);
4693 return dx*dx + dy*dy;
4694}
4695
4696static void
4697warp_to_behind_border(struct border *border, wl_fixed_t *sx, wl_fixed_t *sy)
4698{
4699 switch (border->blocking_dir) {
4700 case MOTION_DIRECTION_POSITIVE_X:
4701 case MOTION_DIRECTION_NEGATIVE_X:
4702 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_X)
4703 *sx = wl_fixed_from_double(border->line.a.x) - 1;
4704 else
4705 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4706 if (*sy < wl_fixed_from_double(border->line.a.y))
4707 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4708 else if (*sy > wl_fixed_from_double(border->line.b.y))
4709 *sy = wl_fixed_from_double(border->line.b.y) - 1;
4710 break;
4711 case MOTION_DIRECTION_POSITIVE_Y:
4712 case MOTION_DIRECTION_NEGATIVE_Y:
4713 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_Y)
4714 *sy = wl_fixed_from_double(border->line.a.y) - 1;
4715 else
4716 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4717 if (*sx < wl_fixed_from_double(border->line.a.x))
4718 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4719 else if (*sx > wl_fixed_from_double(border->line.b.x))
4720 *sx = wl_fixed_from_double(border->line.b.x) - 1;
4721 break;
4722 }
Jonas Ådahld3414f22016-07-22 17:56:31 +08004723}
4724
4725static void
4726maybe_warp_confined_pointer(struct weston_pointer_constraint *constraint)
4727{
4728 wl_fixed_t x;
4729 wl_fixed_t y;
4730 wl_fixed_t sx;
4731 wl_fixed_t sy;
4732
4733 weston_view_from_global_fixed(constraint->view,
4734 constraint->pointer->x,
4735 constraint->pointer->y,
4736 &sx,
4737 &sy);
4738
4739 if (!is_within_constraint_region(constraint, sx, sy)) {
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004740 double xf = wl_fixed_to_double(sx);
4741 double yf = wl_fixed_to_double(sy);
4742 pixman_region32_t confine_region;
4743 struct wl_array borders;
4744 struct border *border;
4745 double closest_distance_2 = DBL_MAX;
4746 struct border *closest_border = NULL;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004747
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004748 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004749
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004750 pixman_region32_init(&confine_region);
4751 pixman_region32_intersect(&confine_region,
4752 &constraint->view->surface->input,
4753 &constraint->region);
4754 region_to_outline(&confine_region, &borders);
4755 pixman_region32_fini(&confine_region);
4756
4757 wl_array_for_each(border, &borders) {
4758 double distance_2;
4759
4760 distance_2 = point_to_border_distance_2(border, xf, yf);
4761 if (distance_2 < closest_distance_2) {
4762 closest_border = border;
4763 closest_distance_2 = distance_2;
4764 }
4765 }
4766 assert(closest_border);
4767
4768 warp_to_behind_border(closest_border, &sx, &sy);
4769
4770 wl_array_release(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004771
4772 weston_view_to_global_fixed(constraint->view, sx, sy, &x, &y);
4773 weston_pointer_move_to(constraint->pointer, x, y);
4774 }
4775}
4776
4777static void
4778confined_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02004779 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004780 struct weston_pointer_motion_event *event)
4781{
4782 struct weston_pointer_constraint *constraint =
4783 container_of(grab, struct weston_pointer_constraint, grab);
4784 struct weston_pointer *pointer = grab->pointer;
4785 struct weston_surface *surface;
4786 wl_fixed_t x, y;
4787 wl_fixed_t old_sx = pointer->sx;
4788 wl_fixed_t old_sy = pointer->sy;
4789 pixman_region32_t confine_region;
4790
4791 assert(pointer->focus);
4792 assert(pointer->focus->surface == constraint->surface);
4793
4794 surface = pointer->focus->surface;
4795
4796 pixman_region32_init(&confine_region);
4797 pixman_region32_intersect(&confine_region,
4798 &surface->input,
4799 &constraint->region);
4800 weston_pointer_clamp_event_to_region(pointer, event,
4801 &confine_region, &x, &y);
4802 weston_pointer_move_to(pointer, x, y);
4803 pixman_region32_fini(&confine_region);
4804
4805 weston_view_from_global_fixed(pointer->focus, x, y,
4806 &pointer->sx, &pointer->sy);
4807
4808 if (old_sx != pointer->sx || old_sy != pointer->sy) {
Quentin Glidiccde13452016-08-12 10:41:32 +02004809 pointer_send_motion(pointer, time,
4810 pointer->sx, pointer->sy);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004811 }
4812
Quentin Glidiccde13452016-08-12 10:41:32 +02004813 pointer_send_relative_motion(pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004814}
4815
4816static void
4817confined_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02004818 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004819 uint32_t button,
4820 uint32_t state_w)
4821{
4822 weston_pointer_send_button(grab->pointer, time, button, state_w);
4823}
4824
4825static void
4826confined_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02004827 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004828 struct weston_pointer_axis_event *event)
4829{
4830 weston_pointer_send_axis(grab->pointer, time, event);
4831}
4832
4833static void
4834confined_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
4835 uint32_t source)
4836{
4837 weston_pointer_send_axis_source(grab->pointer, source);
4838}
4839
4840static void
4841confined_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
4842{
4843 weston_pointer_send_frame(grab->pointer);
4844}
4845
4846static void
4847confined_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
4848{
4849 struct weston_pointer_constraint *constraint =
4850 container_of(grab, struct weston_pointer_constraint, grab);
4851
4852 disable_pointer_constraint(constraint);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004853}
4854
4855static const struct weston_pointer_grab_interface
4856 confined_pointer_grab_interface = {
4857 confined_pointer_grab_pointer_focus,
4858 confined_pointer_grab_pointer_motion,
4859 confined_pointer_grab_pointer_button,
4860 confined_pointer_grab_pointer_axis,
4861 confined_pointer_grab_pointer_axis_source,
4862 confined_pointer_grab_pointer_frame,
4863 confined_pointer_grab_pointer_cancel,
4864};
4865
4866static void
4867confined_pointer_destroy(struct wl_client *client,
4868 struct wl_resource *resource)
4869{
4870 wl_resource_destroy(resource);
4871}
4872
4873static void
4874confined_pointer_set_region(struct wl_client *client,
4875 struct wl_resource *resource,
4876 struct wl_resource *region_resource)
4877{
4878 struct weston_pointer_constraint *constraint =
4879 wl_resource_get_user_data(resource);
4880 struct weston_region *region = region_resource ?
4881 wl_resource_get_user_data(region_resource) : NULL;
4882
4883 if (!constraint)
4884 return;
4885
4886 if (region) {
4887 pixman_region32_copy(&constraint->region_pending,
4888 &region->region);
4889 } else {
4890 pixman_region32_fini(&constraint->region_pending);
4891 region_init_infinite(&constraint->region_pending);
4892 }
4893 constraint->region_is_pending = true;
4894}
4895
4896static const struct zwp_confined_pointer_v1_interface confined_pointer_interface = {
4897 confined_pointer_destroy,
4898 confined_pointer_set_region,
4899};
4900
4901static void
4902pointer_constraints_confine_pointer(struct wl_client *client,
4903 struct wl_resource *resource,
4904 uint32_t id,
4905 struct wl_resource *surface_resource,
4906 struct wl_resource *pointer_resource,
4907 struct wl_resource *region_resource,
4908 uint32_t lifetime)
4909{
4910 struct weston_surface *surface =
4911 wl_resource_get_user_data(surface_resource);
4912 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
4913 struct weston_region *region = region_resource ?
4914 wl_resource_get_user_data(region_resource) : NULL;
4915
4916 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
4917 &zwp_confined_pointer_v1_interface,
4918 &confined_pointer_interface,
4919 &confined_pointer_grab_interface);
4920}
4921
4922static const struct zwp_pointer_constraints_v1_interface pointer_constraints_interface = {
4923 pointer_constraints_destroy,
4924 pointer_constraints_lock_pointer,
4925 pointer_constraints_confine_pointer,
4926};
4927
4928static void
4929bind_pointer_constraints(struct wl_client *client, void *data,
4930 uint32_t version, uint32_t id)
4931{
4932 struct wl_resource *resource;
4933
4934 resource = wl_resource_create(client,
4935 &zwp_pointer_constraints_v1_interface,
4936 1, id);
4937
4938 wl_resource_set_implementation(resource, &pointer_constraints_interface,
4939 NULL, NULL);
4940}
4941
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004942static void
Alexandros Frantzis2b442482018-02-20 14:05:50 +02004943input_timestamps_destroy(struct wl_client *client,
4944 struct wl_resource *resource)
4945{
4946 wl_resource_destroy(resource);
4947}
4948
4949static const struct zwp_input_timestamps_v1_interface
4950 input_timestamps_interface = {
4951 input_timestamps_destroy,
4952};
4953
4954static void
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004955input_timestamps_manager_destroy(struct wl_client *client,
4956 struct wl_resource *resource)
4957{
4958 wl_resource_destroy(resource);
4959}
4960
4961static void
4962input_timestamps_manager_get_keyboard_timestamps(struct wl_client *client,
4963 struct wl_resource *resource,
4964 uint32_t id,
4965 struct wl_resource *keyboard_resource)
4966{
Alexandros Frantzis2b442482018-02-20 14:05:50 +02004967 struct weston_keyboard *keyboard =
4968 wl_resource_get_user_data(keyboard_resource);
4969 struct wl_resource *input_ts;
4970
4971 input_ts = wl_resource_create(client,
4972 &zwp_input_timestamps_v1_interface,
4973 1, id);
4974 if (!input_ts) {
4975 wl_client_post_no_memory(client);
4976 return;
4977 }
4978
4979 if (keyboard) {
4980 wl_list_insert(&keyboard->timestamps_list,
4981 wl_resource_get_link(input_ts));
4982 } else {
4983 wl_list_init(wl_resource_get_link(input_ts));
4984 }
4985
4986 wl_resource_set_implementation(input_ts,
4987 &input_timestamps_interface,
4988 keyboard_resource,
4989 unbind_resource);
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004990}
4991
4992static void
4993input_timestamps_manager_get_pointer_timestamps(struct wl_client *client,
4994 struct wl_resource *resource,
4995 uint32_t id,
4996 struct wl_resource *pointer_resource)
4997{
Alexandros Frantzisdb907b72018-02-20 14:06:26 +02004998 struct weston_pointer *pointer =
4999 wl_resource_get_user_data(pointer_resource);
5000 struct wl_resource *input_ts;
5001
5002 input_ts = wl_resource_create(client,
5003 &zwp_input_timestamps_v1_interface,
5004 1, id);
5005 if (!input_ts) {
5006 wl_client_post_no_memory(client);
5007 return;
5008 }
5009
5010 if (pointer) {
5011 wl_list_insert(&pointer->timestamps_list,
5012 wl_resource_get_link(input_ts));
5013 } else {
5014 wl_list_init(wl_resource_get_link(input_ts));
5015 }
5016
5017 wl_resource_set_implementation(input_ts,
5018 &input_timestamps_interface,
5019 pointer_resource,
5020 unbind_resource);
Alexandros Frantzis538749d2018-02-16 18:44:16 +02005021}
5022
5023static void
5024input_timestamps_manager_get_touch_timestamps(struct wl_client *client,
5025 struct wl_resource *resource,
5026 uint32_t id,
5027 struct wl_resource *touch_resource)
5028{
Alexandros Frantzisd7157842018-02-20 14:07:03 +02005029 struct weston_touch *touch = wl_resource_get_user_data(touch_resource);
5030 struct wl_resource *input_ts;
5031
5032 input_ts = wl_resource_create(client,
5033 &zwp_input_timestamps_v1_interface,
5034 1, id);
5035 if (!input_ts) {
5036 wl_client_post_no_memory(client);
5037 return;
5038 }
5039
5040 if (touch) {
5041 wl_list_insert(&touch->timestamps_list,
5042 wl_resource_get_link(input_ts));
5043 } else {
5044 wl_list_init(wl_resource_get_link(input_ts));
5045 }
5046
5047 wl_resource_set_implementation(input_ts,
5048 &input_timestamps_interface,
5049 touch_resource,
5050 unbind_resource);
Alexandros Frantzis538749d2018-02-16 18:44:16 +02005051}
5052
5053static const struct zwp_input_timestamps_manager_v1_interface
5054 input_timestamps_manager_interface = {
5055 input_timestamps_manager_destroy,
5056 input_timestamps_manager_get_keyboard_timestamps,
5057 input_timestamps_manager_get_pointer_timestamps,
5058 input_timestamps_manager_get_touch_timestamps,
5059};
5060
5061static void
5062bind_input_timestamps_manager(struct wl_client *client, void *data,
5063 uint32_t version, uint32_t id)
5064{
5065 struct wl_resource *resource =
5066 wl_resource_create(client,
5067 &zwp_input_timestamps_manager_v1_interface,
5068 1, id);
5069
5070 if (resource == NULL) {
5071 wl_client_post_no_memory(client);
5072 return;
5073 }
5074
5075 wl_resource_set_implementation(resource,
5076 &input_timestamps_manager_interface,
5077 NULL, NULL);
5078}
5079
Jonas Ådahl30d61d82014-10-22 21:21:17 +02005080int
5081weston_input_init(struct weston_compositor *compositor)
5082{
5083 if (!wl_global_create(compositor->wl_display,
5084 &zwp_relative_pointer_manager_v1_interface, 1,
5085 compositor, bind_relative_pointer_manager))
5086 return -1;
5087
Jonas Ådahld3414f22016-07-22 17:56:31 +08005088 if (!wl_global_create(compositor->wl_display,
5089 &zwp_pointer_constraints_v1_interface, 1,
5090 NULL, bind_pointer_constraints))
5091 return -1;
5092
Alexandros Frantzis538749d2018-02-16 18:44:16 +02005093 if (!wl_global_create(compositor->wl_display,
5094 &zwp_input_timestamps_manager_v1_interface, 1,
5095 NULL, bind_input_timestamps_manager))
5096 return -1;
5097
Jonas Ådahl30d61d82014-10-22 21:21:17 +02005098 return 0;
5099}