blob: f7754297358873da7b79c79c38b5a79b3aee35c4 [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;
leng.fangdbaf6fa2024-06-20 19:31:04 +08001016 bool is_int_key = false;
1017 bool add_key_flg = false;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001018
Quentin Glidiccde13452016-08-12 10:41:32 +02001019 if (!weston_keyboard_has_focus_resource(keyboard))
1020 return;
1021
Neil Roberts96d790e2013-09-19 17:32:00 +01001022 resource_list = &keyboard->focus_resource_list;
Quentin Glidiccde13452016-08-12 10:41:32 +02001023 serial = wl_display_next_serial(display);
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02001024 msecs = timespec_to_msec(time);
leng.fangdbaf6fa2024-06-20 19:31:04 +08001025 is_int_key = weston_keyboard_is_intercepted_keycode(
1026 keyboard, key,
1027 0, 0, 0, 0,
1028 serial, state, msecs, false);
1029 if (is_int_key)
1030 return;
1031
1032 add_key_flg = (state == WL_KEYBOARD_KEY_STATE_PRESSED);
Alexandros Frantzis2b442482018-02-20 14:05:50 +02001033 wl_resource_for_each(resource, resource_list) {
leng.fangdbaf6fa2024-06-20 19:31:04 +08001034 if (add_key_flg) {
1035 weston_keyboard_add_key_info(keyboard, resource, key);
1036 send_timestamps_for_input_resource(resource,
1037 &keyboard->timestamps_list,
1038 time);
1039 wl_keyboard_send_key(resource, serial, msecs, key, state);
1040 }
Alexandros Frantzis2b442482018-02-20 14:05:50 +02001041 }
leng.fangdbaf6fa2024-06-20 19:31:04 +08001042 if (!add_key_flg) {
1043 while (!wl_list_empty(&keyboard->key_info)) {
1044 weston_keyboard_get_resource_and_key(keyboard, &resource, &key);
1045 send_timestamps_for_input_resource(resource,
1046 &keyboard->timestamps_list,
1047 time);
1048 wl_keyboard_send_key(resource, serial, msecs, key, state);
1049 }
1050 }
1051}
Quentin Glidiccde13452016-08-12 10:41:32 +02001052
1053static void
1054default_grab_keyboard_key(struct weston_keyboard_grab *grab,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02001055 const struct timespec *time, uint32_t key,
1056 uint32_t state)
Quentin Glidiccde13452016-08-12 10:41:32 +02001057{
1058 weston_keyboard_send_key(grab->keyboard, time, key, state);
Neil Roberts96d790e2013-09-19 17:32:00 +01001059}
1060
1061static void
1062send_modifiers_to_resource(struct weston_keyboard *keyboard,
1063 struct wl_resource *resource,
1064 uint32_t serial)
1065{
1066 wl_keyboard_send_modifiers(resource,
1067 serial,
1068 keyboard->modifiers.mods_depressed,
1069 keyboard->modifiers.mods_latched,
1070 keyboard->modifiers.mods_locked,
1071 keyboard->modifiers.group);
1072}
1073
1074static void
1075send_modifiers_to_client_in_list(struct wl_client *client,
1076 struct wl_list *list,
1077 uint32_t serial,
1078 struct weston_keyboard *keyboard)
1079{
1080 struct wl_resource *resource;
1081
1082 wl_resource_for_each(resource, list) {
1083 if (wl_resource_get_client(resource) == client)
1084 send_modifiers_to_resource(keyboard,
1085 resource,
1086 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001087 }
1088}
1089
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001090static struct weston_pointer_client *
1091find_pointer_client_for_surface(struct weston_pointer *pointer,
1092 struct weston_surface *surface)
1093{
1094 struct wl_client *client;
1095
1096 if (!surface)
1097 return NULL;
1098
1099 if (!surface->resource)
1100 return NULL;
1101
1102 client = wl_resource_get_client(surface->resource);
1103 return weston_pointer_get_pointer_client(pointer, client);
1104}
1105
1106static struct weston_pointer_client *
1107find_pointer_client_for_view(struct weston_pointer *pointer, struct weston_view *view)
1108{
1109 if (!view)
1110 return NULL;
1111
1112 return find_pointer_client_for_surface(pointer, view->surface);
1113}
1114
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001115static struct wl_resource *
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001116find_resource_for_surface(struct wl_list *list, struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001117{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001118 if (!surface)
1119 return NULL;
1120
Jason Ekstrand44a38632013-06-14 10:08:00 -05001121 if (!surface->resource)
1122 return NULL;
Stefan Schmidtfda26522013-09-17 10:54:09 +01001123
Jason Ekstrand44a38632013-06-14 10:08:00 -05001124 return wl_resource_find_for_client(list, wl_resource_get_client(surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001125}
1126
Quentin Glidiccde13452016-08-12 10:41:32 +02001127/** Send wl_keyboard.modifiers events to focused resources and pointer
1128 * focused resources.
1129 *
1130 * \param keyboard The keyboard where the modifiers events originates from.
1131 * \param serial The serial of the event
1132 * \param mods_depressed The mods_depressed value of the event
1133 * \param mods_latched The mods_latched value of the event
1134 * \param mods_locked The mods_locked value of the event
1135 * \param group The group value of the event
1136 *
1137 * For every resource that is currently in focus, send a wl_keyboard.modifiers
1138 * event with the passed parameters. The focused resources are the wl_keyboard
1139 * resources of the client which currently has the surface with keyboard focus.
1140 * This also sends wl_keyboard.modifiers events to the wl_keyboard resources of
1141 * the client having pointer focus (if different from the keyboard focus client).
1142 */
1143WL_EXPORT void
1144weston_keyboard_send_modifiers(struct weston_keyboard *keyboard,
1145 uint32_t serial, uint32_t mods_depressed,
1146 uint32_t mods_latched,
1147 uint32_t mods_locked, uint32_t group)
1148{
1149 struct weston_pointer *pointer =
1150 weston_seat_get_pointer(keyboard->seat);
1151
1152 if (weston_keyboard_has_focus_resource(keyboard)) {
1153 struct wl_list *resource_list;
1154 struct wl_resource *resource;
1155
1156 resource_list = &keyboard->focus_resource_list;
1157 wl_resource_for_each(resource, resource_list) {
1158 wl_keyboard_send_modifiers(resource, serial,
1159 mods_depressed, mods_latched,
1160 mods_locked, group);
1161 }
1162 }
1163
1164 if (pointer && pointer->focus && pointer->focus->surface->resource &&
1165 pointer->focus->surface != keyboard->focus) {
1166 struct wl_client *pointer_client =
1167 wl_resource_get_client(pointer->focus->surface->resource);
1168
1169 send_modifiers_to_client_in_list(pointer_client,
1170 &keyboard->resource_list,
1171 serial,
1172 keyboard);
1173 }
1174}
1175
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001176static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -07001177default_grab_keyboard_modifiers(struct weston_keyboard_grab *grab,
1178 uint32_t serial, uint32_t mods_depressed,
1179 uint32_t mods_latched,
1180 uint32_t mods_locked, uint32_t group)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001181{
Quentin Glidiccde13452016-08-12 10:41:32 +02001182 weston_keyboard_send_modifiers(grab->keyboard, serial, mods_depressed,
1183 mods_latched, mods_locked, group);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001184}
1185
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001186static void
1187default_grab_keyboard_cancel(struct weston_keyboard_grab *grab)
1188{
1189}
1190
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001191static const struct weston_keyboard_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001192 default_keyboard_grab_interface = {
Kristian Høgsbergb27901c2013-10-28 15:32:02 -07001193 default_grab_keyboard_key,
1194 default_grab_keyboard_modifiers,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001195 default_grab_keyboard_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001196};
1197
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001198static void
1199pointer_unmap_sprite(struct weston_pointer *pointer)
1200{
Pekka Paalanenc557ff72014-11-12 16:42:52 +02001201 struct weston_surface *surface = pointer->sprite->surface;
1202
1203 if (weston_surface_is_mapped(surface))
1204 weston_surface_unmap(surface);
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001205
1206 wl_list_remove(&pointer->sprite_destroy_listener.link);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02001207 surface->committed = NULL;
1208 surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03001209 weston_surface_set_label_func(surface, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001210 weston_view_destroy(pointer->sprite);
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001211 pointer->sprite = NULL;
1212}
1213
1214static void
1215pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
1216{
1217 struct weston_pointer *pointer =
1218 container_of(listener, struct weston_pointer,
1219 sprite_destroy_listener);
1220
1221 pointer->sprite = NULL;
1222}
1223
Jonas Ådahl3e12e632013-12-02 22:05:05 +01001224static void
1225weston_pointer_reset_state(struct weston_pointer *pointer)
1226{
1227 pointer->button_count = 0;
1228}
1229
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001230static void
1231weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data);
1232
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001233static struct weston_pointer *
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001234weston_pointer_create(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001235{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001236 struct weston_pointer *pointer;
1237
Peter Huttererf3d62272013-08-08 11:57:05 +10001238 pointer = zalloc(sizeof *pointer);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001239 if (pointer == NULL)
1240 return NULL;
1241
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001242 wl_list_init(&pointer->pointer_clients);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001243 weston_pointer_set_default_grab(pointer,
1244 seat->compositor->default_pointer_grab);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001245 wl_list_init(&pointer->focus_resource_listener.link);
1246 pointer->focus_resource_listener.notify = pointer_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001247 pointer->default_grab.pointer = pointer;
1248 pointer->grab = &pointer->default_grab;
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001249 wl_signal_init(&pointer->motion_signal);
Emilio Pozuelo Monfortaa7a4762013-11-19 11:37:15 +01001250 wl_signal_init(&pointer->focus_signal);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001251 wl_list_init(&pointer->focus_view_listener.link);
Jonas Ådahl3eb4ddd2016-07-22 17:52:58 +08001252 wl_signal_init(&pointer->destroy_signal);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +02001253 wl_list_init(&pointer->timestamps_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001254
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001255 pointer->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
1256
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001257 /* FIXME: Pick better co-ords. */
1258 pointer->x = wl_fixed_from_int(100);
1259 pointer->y = wl_fixed_from_int(100);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001260
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001261 pointer->output_destroy_listener.notify =
1262 weston_pointer_handle_output_destroy;
1263 wl_signal_add(&seat->compositor->output_destroyed_signal,
1264 &pointer->output_destroy_listener);
1265
Derek Foremanf9318d12015-05-11 15:40:11 -05001266 pointer->sx = wl_fixed_from_int(-1000000);
1267 pointer->sy = wl_fixed_from_int(-1000000);
1268
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001269 return pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001270}
1271
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001272static void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001273weston_pointer_destroy(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001274{
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +02001275 struct weston_pointer_client *pointer_client, *tmp;
1276
Jonas Ådahl3eb4ddd2016-07-22 17:52:58 +08001277 wl_signal_emit(&pointer->destroy_signal, pointer);
1278
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001279 if (pointer->sprite)
1280 pointer_unmap_sprite(pointer);
1281
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +02001282 wl_list_for_each_safe(pointer_client, tmp, &pointer->pointer_clients,
1283 link) {
1284 wl_list_remove(&pointer_client->link);
1285 weston_pointer_client_destroy(pointer_client);
1286 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001287
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001288 wl_list_remove(&pointer->focus_resource_listener.link);
1289 wl_list_remove(&pointer->focus_view_listener.link);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001290 wl_list_remove(&pointer->output_destroy_listener.link);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +02001291 wl_list_remove(&pointer->timestamps_list);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001292 free(pointer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001293}
1294
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001295void
1296weston_pointer_set_default_grab(struct weston_pointer *pointer,
1297 const struct weston_pointer_grab_interface *interface)
1298{
1299 if (interface)
1300 pointer->default_grab.interface = interface;
1301 else
1302 pointer->default_grab.interface =
1303 &default_pointer_grab_interface;
1304}
1305
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001306static struct weston_keyboard *
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001307weston_keyboard_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001308{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001309 struct weston_keyboard *keyboard;
1310
Peter Huttererf3d62272013-08-08 11:57:05 +10001311 keyboard = zalloc(sizeof *keyboard);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001312 if (keyboard == NULL)
Neil Roberts96d790e2013-09-19 17:32:00 +01001313 return NULL;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001314
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001315 wl_list_init(&keyboard->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +01001316 wl_list_init(&keyboard->focus_resource_list);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001317 wl_list_init(&keyboard->focus_resource_listener.link);
1318 keyboard->focus_resource_listener.notify = keyboard_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001319 wl_array_init(&keyboard->keys);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001320 keyboard->default_grab.interface = &default_keyboard_grab_interface;
1321 keyboard->default_grab.keyboard = keyboard;
1322 keyboard->grab = &keyboard->default_grab;
1323 wl_signal_init(&keyboard->focus_signal);
Alexandros Frantzis2b442482018-02-20 14:05:50 +02001324 wl_list_init(&keyboard->timestamps_list);
leng.fangdbaf6fa2024-06-20 19:31:04 +08001325 wl_list_init(&keyboard->intercept_resource);
1326 wl_list_init(&keyboard->key_info);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001327
1328 return keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001329}
1330
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001331static void
1332weston_xkb_info_destroy(struct weston_xkb_info *xkb_info);
1333
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001334static void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001335weston_keyboard_destroy(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001336{
Alexandros Frantzis7a314d62018-01-26 18:47:56 +02001337 struct wl_resource *resource;
1338
1339 wl_resource_for_each(resource, &keyboard->resource_list) {
1340 wl_resource_set_user_data(resource, NULL);
1341 }
1342
1343 wl_resource_for_each(resource, &keyboard->focus_resource_list) {
1344 wl_resource_set_user_data(resource, NULL);
1345 }
1346
1347 wl_list_remove(&keyboard->resource_list);
1348 wl_list_remove(&keyboard->focus_resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +01001349
Derek Foreman185d1582017-06-28 11:17:23 -05001350 xkb_state_unref(keyboard->xkb_state.state);
1351 if (keyboard->xkb_info)
1352 weston_xkb_info_destroy(keyboard->xkb_info);
1353 xkb_keymap_unref(keyboard->pending_keymap);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001354
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001355 wl_array_release(&keyboard->keys);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001356 wl_list_remove(&keyboard->focus_resource_listener.link);
Alexandros Frantzis2b442482018-02-20 14:05:50 +02001357 wl_list_remove(&keyboard->timestamps_list);
leng.fangdbaf6fa2024-06-20 19:31:04 +08001358 weston_keyboard_clear_key_intercept(keyboard);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001359 free(keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001360}
1361
Jonas Ådahlcbfa7f72013-12-02 22:05:04 +01001362static void
1363weston_touch_reset_state(struct weston_touch *touch)
1364{
1365 touch->num_tp = 0;
1366}
1367
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001368static struct weston_touch *
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001369weston_touch_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001370{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001371 struct weston_touch *touch;
1372
Peter Huttererf3d62272013-08-08 11:57:05 +10001373 touch = zalloc(sizeof *touch);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001374 if (touch == NULL)
1375 return NULL;
1376
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -05001377 wl_list_init(&touch->device_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001378 wl_list_init(&touch->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +01001379 wl_list_init(&touch->focus_resource_list);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001380 wl_list_init(&touch->focus_view_listener.link);
1381 touch->focus_view_listener.notify = touch_focus_view_destroyed;
1382 wl_list_init(&touch->focus_resource_listener.link);
1383 touch->focus_resource_listener.notify = touch_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001384 touch->default_grab.interface = &default_touch_grab_interface;
1385 touch->default_grab.touch = touch;
1386 touch->grab = &touch->default_grab;
1387 wl_signal_init(&touch->focus_signal);
Alexandros Frantzisd7157842018-02-20 14:07:03 +02001388 wl_list_init(&touch->timestamps_list);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001389
1390 return touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001391}
1392
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001393static void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001394weston_touch_destroy(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001395{
Alexandros Frantzisb0b598c2018-01-26 18:47:57 +02001396 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01001397
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -05001398 assert(wl_list_empty(&touch->device_list));
1399
Alexandros Frantzisb0b598c2018-01-26 18:47:57 +02001400 wl_resource_for_each(resource, &touch->resource_list) {
1401 wl_resource_set_user_data(resource, NULL);
1402 }
1403
1404 wl_resource_for_each(resource, &touch->focus_resource_list) {
1405 wl_resource_set_user_data(resource, NULL);
1406 }
1407
1408 wl_list_remove(&touch->resource_list);
1409 wl_list_remove(&touch->focus_resource_list);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001410 wl_list_remove(&touch->focus_view_listener.link);
1411 wl_list_remove(&touch->focus_resource_listener.link);
Alexandros Frantzisd7157842018-02-20 14:07:03 +02001412 wl_list_remove(&touch->timestamps_list);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001413 free(touch);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001414}
1415
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001416static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04001417seat_send_updated_caps(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001418{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001419 enum wl_seat_capability caps = 0;
Rob Bradford6e737f52013-09-06 17:48:19 +01001420 struct wl_resource *resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001421
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001422 if (seat->pointer_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001423 caps |= WL_SEAT_CAPABILITY_POINTER;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001424 if (seat->keyboard_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001425 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001426 if (seat->touch_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001427 caps |= WL_SEAT_CAPABILITY_TOUCH;
1428
Rob Bradford6e737f52013-09-06 17:48:19 +01001429 wl_resource_for_each(resource, &seat->base_resource_list) {
1430 wl_seat_send_capabilities(resource, caps);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001431 }
Jason Ekstranda4ab5422014-04-02 19:53:45 -05001432 wl_signal_emit(&seat->updated_caps_signal, seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001433}
1434
Derek Foremanf9318d12015-05-11 15:40:11 -05001435
1436/** Clear the pointer focus
1437 *
1438 * \param pointer the pointer to clear focus for.
1439 *
1440 * This can be used to unset pointer focus and set the co-ordinates to the
1441 * arbitrary values we use for the no focus case.
1442 *
1443 * There's no requirement to use this function. For example, passing the
1444 * results of a weston_compositor_pick_view() directly to
1445 * weston_pointer_set_focus() will do the right thing when no view is found.
1446 */
1447WL_EXPORT void
1448weston_pointer_clear_focus(struct weston_pointer *pointer)
1449{
1450 weston_pointer_set_focus(pointer, NULL,
1451 wl_fixed_from_int(-1000000),
1452 wl_fixed_from_int(-1000000));
1453}
1454
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001455WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001456weston_pointer_set_focus(struct weston_pointer *pointer,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001457 struct weston_view *view,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001458 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001459{
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001460 struct weston_pointer_client *pointer_client;
Derek Foreman1281a362015-07-31 16:55:32 -05001461 struct weston_keyboard *kbd = weston_seat_get_keyboard(pointer->seat);
Neil Roberts96d790e2013-09-19 17:32:00 +01001462 struct wl_resource *resource;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001463 struct wl_resource *surface_resource;
Rob Bradford880ebc72013-07-22 17:31:38 +01001464 struct wl_display *display = pointer->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001465 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01001466 struct wl_list *focus_resource_list;
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001467 int refocus = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001468
1469 if ((!pointer->focus && view) ||
1470 (pointer->focus && !view) ||
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001471 (pointer->focus && pointer->focus->surface != view->surface) ||
1472 pointer->sx != sx || pointer->sy != sy)
1473 refocus = 1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001474
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001475 if (pointer->focus_client && refocus) {
1476 focus_resource_list = &pointer->focus_client->pointer_resources;
1477 if (!wl_list_empty(focus_resource_list)) {
1478 serial = wl_display_next_serial(display);
1479 surface_resource = pointer->focus->surface->resource;
1480 wl_resource_for_each(resource, focus_resource_list) {
1481 wl_pointer_send_leave(resource, serial,
1482 surface_resource);
Peter Hutterer87743e92016-01-18 16:38:22 +10001483 pointer_send_frame(resource);
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001484 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001485 }
1486
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001487 pointer->focus_client = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001488 }
1489
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001490 pointer_client = find_pointer_client_for_view(pointer, view);
1491 if (pointer_client && refocus) {
1492 struct wl_client *surface_client = pointer_client->client;
Neil Roberts96d790e2013-09-19 17:32:00 +01001493
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001494 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001495
Jason Ekstranda7af7042013-10-12 22:38:11 -05001496 if (kbd && kbd->focus != view->surface)
Kristian Høgsbergcb406f12013-10-09 10:54:03 -07001497 send_modifiers_to_client_in_list(surface_client,
1498 &kbd->resource_list,
1499 serial,
1500 kbd);
1501
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001502 pointer->focus_client = pointer_client;
Neil Roberts96d790e2013-09-19 17:32:00 +01001503
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001504 focus_resource_list = &pointer->focus_client->pointer_resources;
Neil Roberts96d790e2013-09-19 17:32:00 +01001505 wl_resource_for_each(resource, focus_resource_list) {
1506 wl_pointer_send_enter(resource,
1507 serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001508 view->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01001509 sx, sy);
Peter Hutterer87743e92016-01-18 16:38:22 +10001510 pointer_send_frame(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001511 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001512
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001513 pointer->focus_serial = serial;
1514 }
1515
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001516 wl_list_remove(&pointer->focus_view_listener.link);
1517 wl_list_init(&pointer->focus_view_listener.link);
1518 wl_list_remove(&pointer->focus_resource_listener.link);
1519 wl_list_init(&pointer->focus_resource_listener.link);
Emilio Pozuelo Monfortaa7a4762013-11-19 11:37:15 +01001520 if (view)
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001521 wl_signal_add(&view->destroy_signal, &pointer->focus_view_listener);
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001522 if (view && view->surface->resource)
1523 wl_resource_add_destroy_listener(view->surface->resource,
1524 &pointer->focus_resource_listener);
1525
1526 pointer->focus = view;
1527 pointer->focus_view_listener.notify = pointer_focus_view_destroyed;
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001528 pointer->sx = sx;
1529 pointer->sy = sy;
1530
Derek Foremanf9318d12015-05-11 15:40:11 -05001531 assert(view || sx == wl_fixed_from_int(-1000000));
1532 assert(view || sy == wl_fixed_from_int(-1000000));
1533
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001534 wl_signal_emit(&pointer->focus_signal, pointer);
1535}
1536
Neil Roberts96d790e2013-09-19 17:32:00 +01001537static void
1538send_enter_to_resource_list(struct wl_list *list,
1539 struct weston_keyboard *keyboard,
1540 struct weston_surface *surface,
1541 uint32_t serial)
1542{
1543 struct wl_resource *resource;
1544
1545 wl_resource_for_each(resource, list) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001546 wl_keyboard_send_enter(resource, serial,
1547 surface->resource,
1548 &keyboard->keys);
Kirill Chibisovc46c70d2020-06-17 01:43:57 +03001549 send_modifiers_to_resource(keyboard, resource, serial);
Neil Roberts96d790e2013-09-19 17:32:00 +01001550 }
1551}
1552
leng.fang32af9fc2024-06-13 11:22:15 +08001553static bool weston_keyboard_need_enter_surface( struct weston_keyboard *keyboard,
1554 struct weston_surface *surface)
1555{
1556 bool enter = false;
1557 if ( (find_resource_for_surface(&keyboard->resource_list, surface)
1558 || find_resource_for_surface(&keyboard->focus_resource_list, surface)) &&
1559 keyboard->focus != surface ) {
1560 enter = true;
1561 }
1562 weston_log("\n %s %d, enter surface:%p, enter:%d222\n", __FUNCTION__,__LINE__, surface, enter);
1563 return enter;
1564}
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001565WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001566weston_keyboard_set_focus(struct weston_keyboard *keyboard,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001567 struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001568{
Quentin Glidic85d55542017-07-21 14:02:40 +02001569 struct weston_seat *seat = keyboard->seat;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001570 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +01001571 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001572 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01001573 struct wl_list *focus_resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001574
Pekka Paalanen72e183b2018-02-22 16:55:15 +02001575 /* Keyboard focus on a surface without a client is equivalent to NULL
1576 * focus as nothing would react to the keyboard events anyway.
1577 * Just set focus to NULL instead - the destroy listener hangs on the
1578 * wl_resource anyway.
1579 */
1580 if (surface && !surface->resource)
1581 surface = NULL;
1582
Neil Roberts96d790e2013-09-19 17:32:00 +01001583 focus_resource_list = &keyboard->focus_resource_list;
1584
leng.fang32af9fc2024-06-13 11:22:15 +08001585 if (weston_keyboard_need_enter_surface( keyboard, surface ) || surface == NULL)
1586 {
1587 if (!wl_list_empty(focus_resource_list) && keyboard->focus != surface) {
1588 serial = wl_display_next_serial(display);
1589 wl_resource_for_each(resource, focus_resource_list) {
1590 if (surface && keyboard && keyboard->focus)
1591 surface->compositor->last_keyboard_focus = keyboard->focus;
1592 weston_log("\n %s %d, leave surface:%p\n", __FUNCTION__,__LINE__, keyboard->focus);
1593 wl_keyboard_send_leave(resource, serial,
1594 keyboard->focus->resource);
1595 }
1596 move_resources(&keyboard->resource_list, focus_resource_list);
leng.fangdbaf6fa2024-06-20 19:31:04 +08001597 }
1598 if ( surface == NULL && keyboard && keyboard->focus) {
1599 surface = keyboard->focus->compositor->last_keyboard_focus;
1600 if (surface == keyboard->focus)
1601 surface = NULL;
1602 weston_log("\n %s %d, need back to surface:%p\n", __FUNCTION__,__LINE__, surface);
Neil Roberts96d790e2013-09-19 17:32:00 +01001603 }
leng.fang32af9fc2024-06-13 11:22:15 +08001604
1605 if (find_resource_for_surface(&keyboard->resource_list, surface) &&
1606 keyboard->focus != surface) {
1607 struct wl_client *surface_client =
1608 wl_resource_get_client(surface->resource);
1609
1610 serial = wl_display_next_serial(display);
1611
1612 move_resources_for_client(focus_resource_list,
1613 &keyboard->resource_list,
1614 surface_client);
1615 send_enter_to_resource_list(focus_resource_list,
1616 keyboard,
1617 surface,
1618 serial);
1619 keyboard->focus_serial = serial;
1620 }
1621
1622 /* Since this function gets called from the surface destroy handler
1623 * we can't just remove the kbd focus listener, or we might corrupt
1624 * the list it's in.
1625 * Instead, we'll just set a flag to ignore the focus when the
1626 * compositor regains kbd focus.
1627 */
1628 seat->use_saved_kbd_focus = false;
1629
1630 wl_list_remove(&keyboard->focus_resource_listener.link);
1631 wl_list_init(&keyboard->focus_resource_listener.link);
1632 if (surface)
1633 wl_resource_add_destroy_listener(surface->resource,
1634 &keyboard->focus_resource_listener);
1635
1636 keyboard->focus = surface;
1637 wl_signal_emit(&keyboard->focus_signal, keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001638 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001639}
1640
Giulio Camuffoa20ca812014-11-22 11:16:56 +02001641/* Users of this function must manually manage the keyboard focus */
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001642WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001643weston_keyboard_start_grab(struct weston_keyboard *keyboard,
1644 struct weston_keyboard_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001645{
1646 keyboard->grab = grab;
1647 grab->keyboard = keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001648}
1649
1650WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001651weston_keyboard_end_grab(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001652{
1653 keyboard->grab = &keyboard->default_grab;
1654}
1655
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001656static void
1657weston_keyboard_cancel_grab(struct weston_keyboard *keyboard)
1658{
1659 keyboard->grab->interface->cancel(keyboard->grab);
1660}
1661
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001662WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001663weston_pointer_start_grab(struct weston_pointer *pointer,
1664 struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001665{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001666 pointer->grab = grab;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001667 grab->pointer = pointer;
Kristian Høgsbergda751b82013-07-04 00:58:07 -04001668 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001669}
1670
1671WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001672weston_pointer_end_grab(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001673{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001674 pointer->grab = &pointer->default_grab;
Kristian Høgsbergda751b82013-07-04 00:58:07 -04001675 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001676}
1677
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001678static void
1679weston_pointer_cancel_grab(struct weston_pointer *pointer)
1680{
1681 pointer->grab->interface->cancel(pointer->grab);
1682}
1683
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001684WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -04001685weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001686{
1687 touch->grab = grab;
1688 grab->touch = touch;
1689}
1690
1691WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -04001692weston_touch_end_grab(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001693{
1694 touch->grab = &touch->default_grab;
1695}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001696
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001697static void
1698weston_touch_cancel_grab(struct weston_touch *touch)
1699{
1700 touch->grab->interface->cancel(touch->grab);
1701}
1702
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001703static void
1704weston_pointer_clamp_for_output(struct weston_pointer *pointer,
1705 struct weston_output *output,
1706 wl_fixed_t *fx, wl_fixed_t *fy)
1707{
1708 int x, y;
1709
1710 x = wl_fixed_to_int(*fx);
1711 y = wl_fixed_to_int(*fy);
1712
1713 if (x < output->x)
1714 *fx = wl_fixed_from_int(output->x);
1715 else if (x >= output->x + output->width)
1716 *fx = wl_fixed_from_int(output->x +
1717 output->width - 1);
1718 if (y < output->y)
1719 *fy = wl_fixed_from_int(output->y);
1720 else if (y >= output->y + output->height)
1721 *fy = wl_fixed_from_int(output->y +
1722 output->height - 1);
1723}
1724
Rob Bradford806d8c02013-06-25 18:56:41 +01001725WL_EXPORT void
1726weston_pointer_clamp(struct weston_pointer *pointer, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001727{
Rob Bradford806d8c02013-06-25 18:56:41 +01001728 struct weston_compositor *ec = pointer->seat->compositor;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001729 struct weston_output *output, *prev = NULL;
1730 int x, y, old_x, old_y, valid = 0;
1731
1732 x = wl_fixed_to_int(*fx);
1733 y = wl_fixed_to_int(*fy);
Rob Bradford806d8c02013-06-25 18:56:41 +01001734 old_x = wl_fixed_to_int(pointer->x);
1735 old_y = wl_fixed_to_int(pointer->y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001736
1737 wl_list_for_each(output, &ec->output_list, link) {
Rob Bradford66bd9f52013-06-25 18:56:42 +01001738 if (pointer->seat->output && pointer->seat->output != output)
1739 continue;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001740 if (pixman_region32_contains_point(&output->region,
1741 x, y, NULL))
1742 valid = 1;
1743 if (pixman_region32_contains_point(&output->region,
1744 old_x, old_y, NULL))
1745 prev = output;
1746 }
1747
Rob Bradford66bd9f52013-06-25 18:56:42 +01001748 if (!prev)
1749 prev = pointer->seat->output;
1750
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001751 if (prev && !valid)
1752 weston_pointer_clamp_for_output(pointer, prev, fx, fy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001753}
1754
Jonas Ådahld2510102014-10-05 21:39:14 +02001755static void
1756weston_pointer_move_to(struct weston_pointer *pointer,
1757 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001758{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001759 int32_t ix, iy;
1760
Rob Bradford806d8c02013-06-25 18:56:41 +01001761 weston_pointer_clamp (pointer, &x, &y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001762
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001763 pointer->x = x;
1764 pointer->y = y;
1765
1766 ix = wl_fixed_to_int(x);
1767 iy = wl_fixed_to_int(y);
1768
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001769 if (pointer->sprite) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001770 weston_view_set_position(pointer->sprite,
1771 ix - pointer->hotspot_x,
1772 iy - pointer->hotspot_y);
1773 weston_view_schedule_repaint(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001774 }
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001775
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001776 pointer->grab->interface->focus(pointer->grab);
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001777 wl_signal_emit(&pointer->motion_signal, pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001778}
1779
Jonas Ådahld2510102014-10-05 21:39:14 +02001780WL_EXPORT void
Jonas Ådahld2510102014-10-05 21:39:14 +02001781weston_pointer_move(struct weston_pointer *pointer,
1782 struct weston_pointer_motion_event *event)
1783{
1784 wl_fixed_t x, y;
1785
1786 weston_pointer_motion_to_abs(pointer, event, &x, &y);
1787 weston_pointer_move_to(pointer, x, y);
1788}
1789
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001790/** Verify if the pointer is in a valid position and move it if it isn't.
1791 */
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001792static void
1793weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001794{
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001795 struct weston_pointer *pointer;
1796 struct weston_compositor *ec;
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001797 struct weston_output *output, *closest = NULL;
1798 int x, y, distance, min = INT_MAX;
1799 wl_fixed_t fx, fy;
1800
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001801 pointer = container_of(listener, struct weston_pointer,
1802 output_destroy_listener);
1803 ec = pointer->seat->compositor;
1804
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001805 x = wl_fixed_to_int(pointer->x);
1806 y = wl_fixed_to_int(pointer->y);
1807
1808 wl_list_for_each(output, &ec->output_list, link) {
1809 if (pixman_region32_contains_point(&output->region,
1810 x, y, NULL))
1811 return;
1812
1813 /* Aproximante the distance from the pointer to the center of
1814 * the output. */
1815 distance = abs(output->x + output->width / 2 - x) +
1816 abs(output->y + output->height / 2 - y);
1817 if (distance < min) {
1818 min = distance;
1819 closest = output;
1820 }
1821 }
1822
1823 /* Nothing to do if there's no output left. */
1824 if (!closest)
1825 return;
1826
1827 fx = pointer->x;
1828 fy = pointer->y;
1829
1830 weston_pointer_clamp_for_output(pointer, closest, &fx, &fy);
Jonas Ådahld2510102014-10-05 21:39:14 +02001831 weston_pointer_move_to(pointer, fx, fy);
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001832}
1833
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001834WL_EXPORT void
1835notify_motion(struct weston_seat *seat,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001836 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001837 struct weston_pointer_motion_event *event)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001838{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001839 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001840 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001841
1842 weston_compositor_wake(ec);
Jonas Ådahld2510102014-10-05 21:39:14 +02001843 pointer->grab->interface->motion(pointer->grab, time, event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001844}
1845
Daniel Stone96d47c02013-11-19 11:37:12 +01001846static void
1847run_modifier_bindings(struct weston_seat *seat, uint32_t old, uint32_t new)
1848{
1849 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001850 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Daniel Stone96d47c02013-11-19 11:37:12 +01001851 uint32_t diff;
1852 unsigned int i;
1853 struct {
1854 uint32_t xkb;
1855 enum weston_keyboard_modifier weston;
1856 } mods[] = {
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001857 { keyboard->xkb_info->ctrl_mod, MODIFIER_CTRL },
1858 { keyboard->xkb_info->alt_mod, MODIFIER_ALT },
1859 { keyboard->xkb_info->super_mod, MODIFIER_SUPER },
1860 { keyboard->xkb_info->shift_mod, MODIFIER_SHIFT },
Daniel Stone96d47c02013-11-19 11:37:12 +01001861 };
1862
1863 diff = new & ~old;
1864 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
1865 if (diff & (1 << mods[i].xkb))
1866 weston_compositor_run_modifier_binding(compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001867 keyboard,
Daniel Stone96d47c02013-11-19 11:37:12 +01001868 mods[i].weston,
1869 WL_KEYBOARD_KEY_STATE_PRESSED);
1870 }
1871
1872 diff = old & ~new;
1873 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
1874 if (diff & (1 << mods[i].xkb))
1875 weston_compositor_run_modifier_binding(compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001876 keyboard,
Daniel Stone96d47c02013-11-19 11:37:12 +01001877 mods[i].weston,
1878 WL_KEYBOARD_KEY_STATE_RELEASED);
1879 }
1880}
1881
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001882WL_EXPORT void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001883notify_motion_absolute(struct weston_seat *seat, const struct timespec *time,
1884 double x, double y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001885{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001886 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001887 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Jonas Ådahld2510102014-10-05 21:39:14 +02001888 struct weston_pointer_motion_event event = { 0 };
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001889
1890 weston_compositor_wake(ec);
Jonas Ådahld2510102014-10-05 21:39:14 +02001891
1892 event = (struct weston_pointer_motion_event) {
1893 .mask = WESTON_POINTER_MOTION_ABS,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001894 .x = x,
1895 .y = y,
Jonas Ådahld2510102014-10-05 21:39:14 +02001896 };
1897
1898 pointer->grab->interface->motion(pointer->grab, time, &event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001899}
1900
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02001901static unsigned int
1902peek_next_activate_serial(struct weston_compositor *c)
1903{
1904 unsigned serial = c->activate_serial + 1;
1905
1906 return serial == 0 ? 1 : serial;
1907}
1908
1909static void
1910inc_activate_serial(struct weston_compositor *c)
1911{
1912 c->activate_serial = peek_next_activate_serial (c);
1913}
1914
1915WL_EXPORT void
Marius Vladd6ccc8b2021-03-05 22:07:30 +02001916weston_view_activate_input(struct weston_view *view,
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02001917 struct weston_seat *seat,
1918 uint32_t flags)
1919{
1920 struct weston_compositor *compositor = seat->compositor;
1921
1922 if (flags & WESTON_ACTIVATE_FLAG_CLICKED) {
1923 view->click_to_activate_serial =
1924 peek_next_activate_serial(compositor);
1925 }
1926
1927 weston_seat_set_keyboard_focus(seat, view->surface);
1928}
1929
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001930WL_EXPORT void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001931notify_button(struct weston_seat *seat, const struct timespec *time,
1932 int32_t button, enum wl_pointer_button_state state)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001933{
1934 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001935 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001936
1937 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001938 weston_compositor_idle_inhibit(compositor);
1939 if (pointer->button_count == 0) {
1940 pointer->grab_button = button;
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001941 pointer->grab_time = *time;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001942 pointer->grab_x = pointer->x;
1943 pointer->grab_y = pointer->y;
1944 }
1945 pointer->button_count++;
1946 } else {
1947 weston_compositor_idle_release(compositor);
1948 pointer->button_count--;
1949 }
1950
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001951 weston_compositor_run_button_binding(compositor, pointer, time, button,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001952 state);
1953
1954 pointer->grab->interface->button(pointer->grab, time, button, state);
1955
1956 if (pointer->button_count == 1)
1957 pointer->grab_serial =
1958 wl_display_get_serial(compositor->wl_display);
1959}
1960
1961WL_EXPORT void
Alexandros Frantzis80321942017-11-16 18:20:56 +02001962notify_axis(struct weston_seat *seat, const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001963 struct weston_pointer_axis_event *event)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001964{
1965 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001966 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001967
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001968 weston_compositor_wake(compositor);
1969
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001970 if (weston_compositor_run_axis_binding(compositor, pointer,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001971 time, event))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001972 return;
1973
Peter Hutterer89b6a492016-01-18 15:58:17 +10001974 pointer->grab->interface->axis(pointer->grab, time, event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001975}
1976
Peter Hutterer87743e92016-01-18 16:38:22 +10001977WL_EXPORT void
1978notify_axis_source(struct weston_seat *seat, uint32_t source)
1979{
1980 struct weston_compositor *compositor = seat->compositor;
1981 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1982
1983 weston_compositor_wake(compositor);
1984
1985 pointer->grab->interface->axis_source(pointer->grab, source);
1986}
1987
1988WL_EXPORT void
1989notify_pointer_frame(struct weston_seat *seat)
1990{
1991 struct weston_compositor *compositor = seat->compositor;
1992 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1993
1994 weston_compositor_wake(compositor);
1995
1996 pointer->grab->interface->frame(pointer->grab);
1997}
1998
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001999WL_EXPORT int
2000weston_keyboard_set_locks(struct weston_keyboard *keyboard,
2001 uint32_t mask, uint32_t value)
2002{
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03002003 uint32_t serial;
2004 xkb_mod_mask_t mods_depressed, mods_latched, mods_locked, group;
2005 xkb_mod_mask_t num, caps;
2006
2007 /* We don't want the leds to go out of sync with the actual state
2008 * so if the backend has no way to change the leds don't try to
2009 * change the state */
2010 if (!keyboard->seat->led_update)
2011 return -1;
2012
2013 mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
2014 XKB_STATE_DEPRESSED);
2015 mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
2016 XKB_STATE_LATCHED);
2017 mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
2018 XKB_STATE_LOCKED);
2019 group = xkb_state_serialize_group(keyboard->xkb_state.state,
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02002020 XKB_STATE_EFFECTIVE);
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03002021
2022 num = (1 << keyboard->xkb_info->mod2_mod);
2023 caps = (1 << keyboard->xkb_info->caps_mod);
2024 if (mask & WESTON_NUM_LOCK) {
2025 if (value & WESTON_NUM_LOCK)
2026 mods_locked |= num;
2027 else
2028 mods_locked &= ~num;
2029 }
2030 if (mask & WESTON_CAPS_LOCK) {
2031 if (value & WESTON_CAPS_LOCK)
2032 mods_locked |= caps;
2033 else
2034 mods_locked &= ~caps;
2035 }
2036
2037 xkb_state_update_mask(keyboard->xkb_state.state, mods_depressed,
2038 mods_latched, mods_locked, 0, 0, group);
2039
2040 serial = wl_display_next_serial(
2041 keyboard->seat->compositor->wl_display);
2042 notify_modifiers(keyboard->seat, serial);
2043
2044 return 0;
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03002045}
2046
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002047WL_EXPORT void
2048notify_modifiers(struct weston_seat *seat, uint32_t serial)
2049{
Derek Foreman1281a362015-07-31 16:55:32 -05002050 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002051 struct weston_keyboard_grab *grab = keyboard->grab;
2052 uint32_t mods_depressed, mods_latched, mods_locked, group;
2053 uint32_t mods_lookup;
2054 enum weston_led leds = 0;
2055 int changed = 0;
2056
2057 /* Serialize and update our internal state, checking to see if it's
2058 * different to the previous state. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002059 mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03002060 XKB_STATE_MODS_DEPRESSED);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002061 mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03002062 XKB_STATE_MODS_LATCHED);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002063 mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03002064 XKB_STATE_MODS_LOCKED);
2065 group = xkb_state_serialize_layout(keyboard->xkb_state.state,
2066 XKB_STATE_LAYOUT_EFFECTIVE);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002067
Derek Foreman244e99e2015-06-03 15:53:26 -05002068 if (mods_depressed != keyboard->modifiers.mods_depressed ||
2069 mods_latched != keyboard->modifiers.mods_latched ||
2070 mods_locked != keyboard->modifiers.mods_locked ||
2071 group != keyboard->modifiers.group)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002072 changed = 1;
2073
Derek Foreman244e99e2015-06-03 15:53:26 -05002074 run_modifier_bindings(seat, keyboard->modifiers.mods_depressed,
Daniel Stone96d47c02013-11-19 11:37:12 +01002075 mods_depressed);
2076
Derek Foreman244e99e2015-06-03 15:53:26 -05002077 keyboard->modifiers.mods_depressed = mods_depressed;
2078 keyboard->modifiers.mods_latched = mods_latched;
2079 keyboard->modifiers.mods_locked = mods_locked;
2080 keyboard->modifiers.group = group;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002081
2082 /* And update the modifier_state for bindings. */
2083 mods_lookup = mods_depressed | mods_latched;
2084 seat->modifier_state = 0;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002085 if (mods_lookup & (1 << keyboard->xkb_info->ctrl_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002086 seat->modifier_state |= MODIFIER_CTRL;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002087 if (mods_lookup & (1 << keyboard->xkb_info->alt_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002088 seat->modifier_state |= MODIFIER_ALT;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002089 if (mods_lookup & (1 << keyboard->xkb_info->super_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002090 seat->modifier_state |= MODIFIER_SUPER;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002091 if (mods_lookup & (1 << keyboard->xkb_info->shift_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002092 seat->modifier_state |= MODIFIER_SHIFT;
2093
2094 /* Finally, notify the compositor that LEDs have changed. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002095 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
2096 keyboard->xkb_info->num_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002097 leds |= LED_NUM_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002098 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
2099 keyboard->xkb_info->caps_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002100 leds |= LED_CAPS_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002101 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
2102 keyboard->xkb_info->scroll_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002103 leds |= LED_SCROLL_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002104 if (leds != keyboard->xkb_state.leds && seat->led_update)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002105 seat->led_update(seat, leds);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002106 keyboard->xkb_state.leds = leds;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002107
2108 if (changed) {
2109 grab->interface->modifiers(grab,
2110 serial,
2111 keyboard->modifiers.mods_depressed,
2112 keyboard->modifiers.mods_latched,
2113 keyboard->modifiers.mods_locked,
2114 keyboard->modifiers.group);
2115 }
2116}
2117
2118static void
2119update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
2120 enum wl_keyboard_key_state state)
2121{
Derek Foreman1281a362015-07-31 16:55:32 -05002122 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002123 enum xkb_key_direction direction;
2124
2125 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2126 direction = XKB_KEY_DOWN;
2127 else
2128 direction = XKB_KEY_UP;
2129
2130 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
2131 * broken keycode system, which starts at 8. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002132 xkb_state_update_key(keyboard->xkb_state.state, key + 8, direction);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002133
2134 notify_modifiers(seat, serial);
2135}
Rui Matos65196bc2013-10-10 19:44:19 +02002136
Derek Foremanf8f7fd62017-06-28 11:41:43 -05002137WL_EXPORT void
2138weston_keyboard_send_keymap(struct weston_keyboard *kbd, struct wl_resource *resource)
Rui Matos65196bc2013-10-10 19:44:19 +02002139{
Derek Foremanf8f7fd62017-06-28 11:41:43 -05002140 struct weston_xkb_info *xkb_info = kbd->xkb_info;
Derek Foreman76829fc2017-06-28 12:17:46 -05002141 int fd;
Sebastian Wickabec5122019-11-01 02:38:45 +01002142 size_t size;
2143 enum ro_anonymous_file_mapmode mapmode;
Derek Foremanf8f7fd62017-06-28 11:41:43 -05002144
Sebastian Wickabec5122019-11-01 02:38:45 +01002145 if (wl_resource_get_version(resource) < 7)
2146 mapmode = RO_ANONYMOUS_FILE_MAPMODE_SHARED;
2147 else
2148 mapmode = RO_ANONYMOUS_FILE_MAPMODE_PRIVATE;
2149
2150 fd = os_ro_anonymous_file_get_fd(xkb_info->keymap_rofile, mapmode);
2151 size = os_ro_anonymous_file_size(xkb_info->keymap_rofile);
2152
2153 if (fd == -1) {
2154 weston_log("creating a keymap file failed: %s\n",
Antonio Borneo39578632019-04-26 23:57:31 +02002155 strerror(errno));
Derek Foreman76829fc2017-06-28 12:17:46 -05002156 return;
2157 }
2158
Rui Matos65196bc2013-10-10 19:44:19 +02002159 wl_keyboard_send_keymap(resource,
2160 WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
Derek Foreman76829fc2017-06-28 12:17:46 -05002161 fd,
Sebastian Wickabec5122019-11-01 02:38:45 +01002162 size);
2163
2164 os_ro_anonymous_file_put_fd(fd);
Rui Matos65196bc2013-10-10 19:44:19 +02002165}
2166
2167static void
2168send_modifiers(struct wl_resource *resource, uint32_t serial, struct weston_keyboard *keyboard)
2169{
2170 wl_keyboard_send_modifiers(resource, serial,
2171 keyboard->modifiers.mods_depressed,
2172 keyboard->modifiers.mods_latched,
2173 keyboard->modifiers.mods_locked,
2174 keyboard->modifiers.group);
2175}
2176
2177static struct weston_xkb_info *
2178weston_xkb_info_create(struct xkb_keymap *keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02002179
2180static void
2181update_keymap(struct weston_seat *seat)
2182{
Derek Foreman1281a362015-07-31 16:55:32 -05002183 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Rui Matos65196bc2013-10-10 19:44:19 +02002184 struct wl_resource *resource;
2185 struct weston_xkb_info *xkb_info;
2186 struct xkb_state *state;
2187 xkb_mod_mask_t latched_mods;
2188 xkb_mod_mask_t locked_mods;
2189
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002190 xkb_info = weston_xkb_info_create(keyboard->pending_keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02002191
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002192 xkb_keymap_unref(keyboard->pending_keymap);
2193 keyboard->pending_keymap = NULL;
Rui Matos65196bc2013-10-10 19:44:19 +02002194
2195 if (!xkb_info) {
2196 weston_log("failed to create XKB info\n");
2197 return;
2198 }
2199
2200 state = xkb_state_new(xkb_info->keymap);
2201 if (!state) {
2202 weston_log("failed to initialise XKB state\n");
2203 weston_xkb_info_destroy(xkb_info);
2204 return;
2205 }
2206
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002207 latched_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
2208 XKB_STATE_MODS_LATCHED);
2209 locked_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
2210 XKB_STATE_MODS_LOCKED);
Rui Matos65196bc2013-10-10 19:44:19 +02002211 xkb_state_update_mask(state,
2212 0, /* depressed */
2213 latched_mods,
2214 locked_mods,
2215 0, 0, 0);
2216
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002217 weston_xkb_info_destroy(keyboard->xkb_info);
2218 keyboard->xkb_info = xkb_info;
Rui Matos65196bc2013-10-10 19:44:19 +02002219
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002220 xkb_state_unref(keyboard->xkb_state.state);
2221 keyboard->xkb_state.state = state;
Rui Matos65196bc2013-10-10 19:44:19 +02002222
Derek Foremanbc91e542015-06-03 15:53:27 -05002223 wl_resource_for_each(resource, &keyboard->resource_list)
Derek Foremanf8f7fd62017-06-28 11:41:43 -05002224 weston_keyboard_send_keymap(keyboard, resource);
Derek Foremanbc91e542015-06-03 15:53:27 -05002225 wl_resource_for_each(resource, &keyboard->focus_resource_list)
Derek Foremanf8f7fd62017-06-28 11:41:43 -05002226 weston_keyboard_send_keymap(keyboard, resource);
Rui Matos65196bc2013-10-10 19:44:19 +02002227
2228 notify_modifiers(seat, wl_display_next_serial(seat->compositor->wl_display));
2229
2230 if (!latched_mods && !locked_mods)
2231 return;
2232
Derek Foremanbc91e542015-06-03 15:53:27 -05002233 wl_resource_for_each(resource, &keyboard->resource_list)
2234 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), keyboard);
2235 wl_resource_for_each(resource, &keyboard->focus_resource_list)
2236 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), keyboard);
Rui Matos65196bc2013-10-10 19:44:19 +02002237}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002238
2239WL_EXPORT void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02002240notify_key(struct weston_seat *seat, const struct timespec *time, uint32_t key,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002241 enum wl_keyboard_key_state state,
2242 enum weston_key_state_update update_state)
2243{
2244 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002245 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002246 struct weston_keyboard_grab *grab = keyboard->grab;
Pekka Paalanen86b53962014-11-19 13:43:32 +02002247 uint32_t *k, *end;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002248
2249 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002250 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002251 } else {
2252 weston_compositor_idle_release(compositor);
2253 }
2254
Pekka Paalanen86b53962014-11-19 13:43:32 +02002255 end = keyboard->keys.data + keyboard->keys.size;
2256 for (k = keyboard->keys.data; k < end; k++) {
2257 if (*k == key) {
2258 /* Ignore server-generated repeats. */
2259 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2260 return;
2261 *k = *--end;
2262 }
2263 }
2264 keyboard->keys.size = (void *) end - keyboard->keys.data;
2265 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2266 k = wl_array_add(&keyboard->keys, sizeof *k);
2267 *k = key;
2268 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002269
2270 if (grab == &keyboard->default_grab ||
2271 grab == &keyboard->input_method_grab) {
Derek Foreman99a6a2d2015-07-15 13:00:43 -05002272 weston_compositor_run_key_binding(compositor, keyboard, time,
2273 key, state);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002274 grab = keyboard->grab;
2275 }
2276
2277 grab->interface->key(grab, time, key, state);
2278
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002279 if (keyboard->pending_keymap &&
Pekka Paalanen86b53962014-11-19 13:43:32 +02002280 keyboard->keys.size == 0)
Rui Matos65196bc2013-10-10 19:44:19 +02002281 update_keymap(seat);
2282
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002283 if (update_state == STATE_UPDATE_AUTOMATIC) {
2284 update_modifier_state(seat,
2285 wl_display_get_serial(compositor->wl_display),
2286 key,
2287 state);
2288 }
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002289
Olivier Fourdandf84dbe2016-06-30 16:01:56 +02002290 keyboard->grab_serial = wl_display_get_serial(compositor->wl_display);
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002291 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02002292 keyboard->grab_time = *time;
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002293 keyboard->grab_key = key;
2294 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002295}
2296
2297WL_EXPORT void
2298notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002299 double x, double y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002300{
Derek Foreman1281a362015-07-31 16:55:32 -05002301 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2302
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002303 if (output) {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002304 weston_pointer_move_to(pointer,
2305 wl_fixed_from_double(x),
2306 wl_fixed_from_double(y));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002307 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002308 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002309 * NULL) here, but somehow that breaks re-entry... */
2310 }
2311}
2312
2313static void
2314destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
2315{
2316 struct weston_seat *ws;
2317
2318 ws = container_of(listener, struct weston_seat,
2319 saved_kbd_focus_listener);
2320
2321 ws->saved_kbd_focus = NULL;
Derek Foremana822afa2021-08-10 17:17:24 -05002322
2323 wl_list_remove(&ws->saved_kbd_focus_listener.link);
2324 ws->saved_kbd_focus_listener.notify = NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002325}
2326
2327WL_EXPORT void
2328notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
2329 enum weston_key_state_update update_state)
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);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04002333 struct weston_surface *surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002334 uint32_t *k, serial;
2335
2336 serial = wl_display_next_serial(compositor->wl_display);
2337 wl_array_copy(&keyboard->keys, keys);
2338 wl_array_for_each(k, &keyboard->keys) {
2339 weston_compositor_idle_inhibit(compositor);
2340 if (update_state == STATE_UPDATE_AUTOMATIC)
2341 update_modifier_state(seat, serial, *k,
2342 WL_KEYBOARD_KEY_STATE_PRESSED);
2343 }
2344
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002345 surface = seat->saved_kbd_focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002346 if (surface) {
Derek Foremana822afa2021-08-10 17:17:24 -05002347 wl_list_remove(&seat->saved_kbd_focus_listener.link);
2348 seat->saved_kbd_focus_listener.notify = NULL;
2349 seat->saved_kbd_focus = NULL;
2350 if (seat->use_saved_kbd_focus)
2351 weston_keyboard_set_focus(keyboard, surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002352 }
2353}
2354
2355WL_EXPORT void
2356notify_keyboard_focus_out(struct weston_seat *seat)
2357{
2358 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002359 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2360 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Quentin Glidic85d55542017-07-21 14:02:40 +02002361 struct weston_surface *focus = keyboard->focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002362 uint32_t *k, serial;
2363
2364 serial = wl_display_next_serial(compositor->wl_display);
2365 wl_array_for_each(k, &keyboard->keys) {
2366 weston_compositor_idle_release(compositor);
2367 update_modifier_state(seat, serial, *k,
2368 WL_KEYBOARD_KEY_STATE_RELEASED);
2369 }
2370
2371 seat->modifier_state = 0;
2372
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002373 weston_keyboard_set_focus(keyboard, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002374 weston_keyboard_cancel_grab(keyboard);
Derek Foreman1281a362015-07-31 16:55:32 -05002375 if (pointer)
2376 weston_pointer_cancel_grab(pointer);
Quentin Glidic85d55542017-07-21 14:02:40 +02002377
2378 if (focus) {
Derek Foremana822afa2021-08-10 17:17:24 -05002379 seat->use_saved_kbd_focus = true;
Quentin Glidic85d55542017-07-21 14:02:40 +02002380 seat->saved_kbd_focus = focus;
2381 seat->saved_kbd_focus_listener.notify =
2382 destroy_device_saved_kbd_focus;
2383 wl_signal_add(&focus->destroy_signal,
2384 &seat->saved_kbd_focus_listener);
2385 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002386}
2387
Michael Fua2bb7912013-07-23 15:51:06 +08002388WL_EXPORT void
Derek Foreman4c93c082015-04-30 16:45:41 -05002389weston_touch_set_focus(struct weston_touch *touch, struct weston_view *view)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002390{
Neil Roberts96d790e2013-09-19 17:32:00 +01002391 struct wl_list *focus_resource_list;
2392
Derek Foreman4c93c082015-04-30 16:45:41 -05002393 focus_resource_list = &touch->focus_resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002394
Derek Foreman4c93c082015-04-30 16:45:41 -05002395 if (view && touch->focus &&
2396 touch->focus->surface == view->surface) {
2397 touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002398 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002399 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002400
Derek Foreman4c93c082015-04-30 16:45:41 -05002401 wl_list_remove(&touch->focus_resource_listener.link);
2402 wl_list_init(&touch->focus_resource_listener.link);
2403 wl_list_remove(&touch->focus_view_listener.link);
2404 wl_list_init(&touch->focus_view_listener.link);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01002405
Neil Roberts96d790e2013-09-19 17:32:00 +01002406 if (!wl_list_empty(focus_resource_list)) {
Derek Foreman4c93c082015-04-30 16:45:41 -05002407 move_resources(&touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002408 focus_resource_list);
2409 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002410
Jason Ekstranda7af7042013-10-12 22:38:11 -05002411 if (view) {
Derek Foreman362656b2014-09-04 10:23:05 -05002412 struct wl_client *surface_client;
2413
2414 if (!view->surface->resource) {
Derek Foreman4c93c082015-04-30 16:45:41 -05002415 touch->focus = NULL;
Derek Foreman362656b2014-09-04 10:23:05 -05002416 return;
2417 }
2418
2419 surface_client = wl_resource_get_client(view->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +01002420 move_resources_for_client(focus_resource_list,
Derek Foreman4c93c082015-04-30 16:45:41 -05002421 &touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002422 surface_client);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01002423 wl_resource_add_destroy_listener(view->surface->resource,
Derek Foreman4c93c082015-04-30 16:45:41 -05002424 &touch->focus_resource_listener);
2425 wl_signal_add(&view->destroy_signal, &touch->focus_view_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002426 }
Derek Foreman4c93c082015-04-30 16:45:41 -05002427 touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002428}
2429
Pekka Paalanenf4062532018-02-26 16:18:29 +02002430static void
2431process_touch_normal(struct weston_touch_device *device,
2432 const struct timespec *time, int touch_id,
2433 double double_x, double double_y, int touch_type)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002434{
Pekka Paalanenbcbce332018-02-26 14:43:00 +02002435 struct weston_touch *touch = device->aggregate;
2436 struct weston_touch_grab *grab = device->aggregate->grab;
2437 struct weston_compositor *ec = device->aggregate->seat->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002438 struct weston_view *ev;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002439 wl_fixed_t sx, sy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002440 wl_fixed_t x = wl_fixed_from_double(double_x);
2441 wl_fixed_t y = wl_fixed_from_double(double_y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002442
2443 /* Update grab's global coordinates. */
Neil Roberts306fe082013-10-03 16:43:06 +01002444 if (touch_id == touch->grab_touch_id && touch_type != WL_TOUCH_UP) {
2445 touch->grab_x = x;
2446 touch->grab_y = y;
2447 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002448
2449 switch (touch_type) {
2450 case WL_TOUCH_DOWN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002451 /* the first finger down picks the view, and all further go
2452 * to that view for the remainder of the touch session i.e.
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002453 * until all touch points are up again. */
Jonas Ådahl9484b692013-12-02 22:05:03 +01002454 if (touch->num_tp == 1) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002455 ev = weston_compositor_pick_view(ec, x, y, &sx, &sy);
Derek Foreman4c93c082015-04-30 16:45:41 -05002456 weston_touch_set_focus(touch, ev);
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002457 } else if (!touch->focus) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002458 /* Unexpected condition: We have non-initial touch but
2459 * there is no focused surface.
2460 */
Chris Michael3f607d32015-10-07 11:59:49 -04002461 weston_log("touch event received with %d points down "
Jonas Ådahl9484b692013-12-02 22:05:03 +01002462 "but no surface focused\n", touch->num_tp);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002463 return;
2464 }
2465
Derek Foreman99a6a2d2015-07-15 13:00:43 -05002466 weston_compositor_run_touch_binding(ec, touch,
Kristian Høgsbergc8964012014-02-05 14:25:18 -08002467 time, touch_type);
2468
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002469 grab->interface->down(grab, time, touch_id, x, y);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002470 if (touch->num_tp == 1) {
Rusty Lynchf1407ff2013-08-08 21:13:57 -07002471 touch->grab_serial =
2472 wl_display_get_serial(ec->wl_display);
Neil Roberts306fe082013-10-03 16:43:06 +01002473 touch->grab_touch_id = touch_id;
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02002474 touch->grab_time = *time;
Rusty Lynchf1407ff2013-08-08 21:13:57 -07002475 touch->grab_x = x;
2476 touch->grab_y = y;
2477 }
2478
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002479 break;
2480 case WL_TOUCH_MOTION:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002481 ev = touch->focus;
2482 if (!ev)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002483 break;
2484
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +02002485 grab->interface->motion(grab, time, touch_id, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002486 break;
2487 case WL_TOUCH_UP:
Alexandros Frantzis27a51b82017-11-16 18:20:59 +02002488 grab->interface->up(grab, time, touch_id);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002489 if (touch->num_tp == 0)
Derek Foreman4c93c082015-04-30 16:45:41 -05002490 weston_touch_set_focus(touch, NULL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002491 break;
2492 }
2493}
2494
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002495static enum weston_touch_mode
2496get_next_touch_mode(enum weston_touch_mode from)
2497{
2498 switch (from) {
2499 case WESTON_TOUCH_MODE_PREP_NORMAL:
2500 return WESTON_TOUCH_MODE_NORMAL;
2501
2502 case WESTON_TOUCH_MODE_PREP_CALIB:
2503 return WESTON_TOUCH_MODE_CALIB;
2504
2505 case WESTON_TOUCH_MODE_NORMAL:
2506 case WESTON_TOUCH_MODE_CALIB:
2507 return from;
2508 }
2509
2510 return WESTON_TOUCH_MODE_NORMAL;
2511}
2512
2513/** Global touch mode update
2514 *
2515 * If no seat has a touch down and the compositor is in a PREP touch mode,
2516 * set the compositor to the goal touch mode.
2517 *
2518 * Calls calibrator if touch mode changed.
2519 */
2520static void
2521weston_compositor_update_touch_mode(struct weston_compositor *compositor)
2522{
2523 struct weston_seat *seat;
2524 struct weston_touch *touch;
2525 enum weston_touch_mode goal;
2526
2527 wl_list_for_each(seat, &compositor->seat_list, link) {
2528 touch = weston_seat_get_touch(seat);
2529 if (!touch)
2530 continue;
2531
2532 if (touch->num_tp > 0)
2533 return;
2534 }
2535
2536 goal = get_next_touch_mode(compositor->touch_mode);
2537 if (compositor->touch_mode != goal) {
2538 compositor->touch_mode = goal;
2539 touch_calibrator_mode_changed(compositor);
2540 }
2541}
2542
2543/** Start transition to normal touch event handling
2544 *
2545 * The touch event mode changes when all touches on all touch devices have
2546 * been lifted. If no touches are currently down, the transition is immediate.
2547 *
2548 * \sa weston_touch_mode
2549 */
2550void
2551weston_compositor_set_touch_mode_normal(struct weston_compositor *compositor)
2552{
2553 switch (compositor->touch_mode) {
2554 case WESTON_TOUCH_MODE_PREP_NORMAL:
2555 case WESTON_TOUCH_MODE_NORMAL:
2556 return;
2557 case WESTON_TOUCH_MODE_PREP_CALIB:
2558 compositor->touch_mode = WESTON_TOUCH_MODE_NORMAL;
2559 touch_calibrator_mode_changed(compositor);
2560 return;
2561 case WESTON_TOUCH_MODE_CALIB:
2562 compositor->touch_mode = WESTON_TOUCH_MODE_PREP_NORMAL;
2563 }
2564
2565 weston_compositor_update_touch_mode(compositor);
2566}
2567
2568/** Start transition to calibrator touch event handling
2569 *
2570 * The touch event mode changes when all touches on all touch devices have
2571 * been lifted. If no touches are currently down, the transition is immediate.
2572 *
2573 * \sa weston_touch_mode
2574 */
2575void
2576weston_compositor_set_touch_mode_calib(struct weston_compositor *compositor)
2577{
2578 switch (compositor->touch_mode) {
2579 case WESTON_TOUCH_MODE_PREP_CALIB:
2580 case WESTON_TOUCH_MODE_CALIB:
2581 assert(0);
2582 return;
2583 case WESTON_TOUCH_MODE_PREP_NORMAL:
2584 compositor->touch_mode = WESTON_TOUCH_MODE_CALIB;
2585 touch_calibrator_mode_changed(compositor);
2586 return;
2587 case WESTON_TOUCH_MODE_NORMAL:
2588 compositor->touch_mode = WESTON_TOUCH_MODE_PREP_CALIB;
2589 }
2590
2591 weston_compositor_update_touch_mode(compositor);
2592}
2593
Pekka Paalanenf4062532018-02-26 16:18:29 +02002594/** Feed in touch down, motion, and up events, calibratable device.
2595 *
2596 * It assumes always the correct cycle sequence until it gets here: touch_down
2597 * → touch_update → ... → touch_update → touch_end. The driver is responsible
2598 * for sending along such order.
2599 *
2600 * \param device The physical device that generated the event.
2601 * \param time The event timestamp.
2602 * \param touch_id ID for the touch point of this event (multi-touch).
Marius Vlada2dace22019-06-12 16:05:44 +03002603 * \param x X coordinate in compositor global space.
2604 * \param y Y coordinate in compositor global space.
Pekka Paalanenf4062532018-02-26 16:18:29 +02002605 * \param norm Normalized device X, Y coordinates in calibration space, or NULL.
2606 * \param touch_type Either WL_TOUCH_DOWN, WL_TOUCH_UP, or WL_TOUCH_MOTION.
2607 *
2608 * Coordinates double_x and double_y are used for normal operation.
2609 *
2610 * Coordinates norm are only used for touch device calibration. If and only if
2611 * the weston_touch_device does not support calibrating, norm must be NULL.
2612 *
2613 * The calibration space is the normalized coordinate space
2614 * [0.0, 1.0]×[0.0, 1.0] of the weston_touch_device. This is assumed to
2615 * map to the similar normalized coordinate space of the associated
2616 * weston_output.
2617 */
2618WL_EXPORT void
2619notify_touch_normalized(struct weston_touch_device *device,
2620 const struct timespec *time,
2621 int touch_id,
2622 double x, double y,
2623 const struct weston_point2d_device_normalized *norm,
2624 int touch_type)
2625{
Louis-Francis Ratté-Boulianne813a06e2017-11-28 20:42:47 -05002626 struct weston_seat *seat = device->aggregate->seat;
2627 struct weston_touch *touch = device->aggregate;
2628
Pekka Paalanenf4062532018-02-26 16:18:29 +02002629 if (touch_type != WL_TOUCH_UP) {
2630 if (weston_touch_device_can_calibrate(device))
2631 assert(norm != NULL);
2632 else
2633 assert(norm == NULL);
2634 }
2635
Louis-Francis Ratté-Boulianne813a06e2017-11-28 20:42:47 -05002636 /* Update touchpoints count regardless of the current mode. */
2637 switch (touch_type) {
2638 case WL_TOUCH_DOWN:
2639 weston_compositor_idle_inhibit(seat->compositor);
2640
2641 touch->num_tp++;
2642 break;
2643 case WL_TOUCH_UP:
2644 if (touch->num_tp == 0) {
2645 /* This can happen if we start out with one or
2646 * more fingers on the touch screen, in which
2647 * case we didn't get the corresponding down
2648 * event. */
Pekka Paalanen332a45e2018-03-02 14:20:59 +02002649 weston_log("Unmatched touch up event on seat %s, device %s\n",
2650 seat->seat_name, device->syspath);
2651 return;
Louis-Francis Ratté-Boulianne813a06e2017-11-28 20:42:47 -05002652 }
2653 weston_compositor_idle_release(seat->compositor);
2654
2655 touch->num_tp--;
2656 break;
2657 default:
2658 break;
2659 }
2660
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002661 /* Properly forward the touch event */
2662 switch (weston_touch_device_get_mode(device)) {
2663 case WESTON_TOUCH_MODE_NORMAL:
2664 case WESTON_TOUCH_MODE_PREP_CALIB:
2665 process_touch_normal(device, time, touch_id, x, y, touch_type);
2666 break;
2667 case WESTON_TOUCH_MODE_CALIB:
2668 case WESTON_TOUCH_MODE_PREP_NORMAL:
Louis-Francis Ratté-Boulianne83630982017-11-28 20:42:47 -05002669 notify_touch_calibrator(device, time, touch_id,
2670 norm, touch_type);
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002671 break;
2672 }
Pekka Paalanenf4062532018-02-26 16:18:29 +02002673}
2674
Jonas Ådahl1679f232014-04-12 09:39:51 +02002675WL_EXPORT void
Pekka Paalanenbcbce332018-02-26 14:43:00 +02002676notify_touch_frame(struct weston_touch_device *device)
Jonas Ådahl1679f232014-04-12 09:39:51 +02002677{
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002678 struct weston_touch_grab *grab;
Jonas Ådahl1679f232014-04-12 09:39:51 +02002679
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002680 switch (weston_touch_device_get_mode(device)) {
2681 case WESTON_TOUCH_MODE_NORMAL:
2682 case WESTON_TOUCH_MODE_PREP_CALIB:
2683 grab = device->aggregate->grab;
2684 grab->interface->frame(grab);
2685 break;
2686 case WESTON_TOUCH_MODE_CALIB:
2687 case WESTON_TOUCH_MODE_PREP_NORMAL:
Louis-Francis Ratté-Boulianne83630982017-11-28 20:42:47 -05002688 notify_touch_calibrator_frame(device);
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002689 break;
2690 }
2691
2692 weston_compositor_update_touch_mode(device->aggregate->seat->compositor);
Jonas Ådahl1679f232014-04-12 09:39:51 +02002693}
2694
Derek Foreman3cc004a2015-11-06 15:56:09 -06002695WL_EXPORT void
Pekka Paalanenbcbce332018-02-26 14:43:00 +02002696notify_touch_cancel(struct weston_touch_device *device)
Derek Foreman3cc004a2015-11-06 15:56:09 -06002697{
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002698 struct weston_touch_grab *grab;
Derek Foreman3cc004a2015-11-06 15:56:09 -06002699
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002700 switch (weston_touch_device_get_mode(device)) {
2701 case WESTON_TOUCH_MODE_NORMAL:
2702 case WESTON_TOUCH_MODE_PREP_CALIB:
2703 grab = device->aggregate->grab;
2704 grab->interface->cancel(grab);
2705 break;
2706 case WESTON_TOUCH_MODE_CALIB:
2707 case WESTON_TOUCH_MODE_PREP_NORMAL:
Louis-Francis Ratté-Boulianne83630982017-11-28 20:42:47 -05002708 notify_touch_calibrator_cancel(device);
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002709 break;
2710 }
2711
2712 weston_compositor_update_touch_mode(device->aggregate->seat->compositor);
Derek Foreman3cc004a2015-11-06 15:56:09 -06002713}
2714
Pekka Paalanen8274d902014-08-06 19:36:51 +03002715static int
2716pointer_cursor_surface_get_label(struct weston_surface *surface,
2717 char *buf, size_t len)
2718{
2719 return snprintf(buf, len, "cursor");
2720}
2721
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002722static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002723pointer_cursor_surface_committed(struct weston_surface *es,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002724 int32_t dx, int32_t dy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002725{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002726 struct weston_pointer *pointer = es->committed_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002727 int x, y;
2728
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002729 if (es->width == 0)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002730 return;
2731
Jason Ekstranda7af7042013-10-12 22:38:11 -05002732 assert(es == pointer->sprite->surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002733
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002734 pointer->hotspot_x -= dx;
2735 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002736
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002737 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
2738 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002739
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002740 weston_view_set_position(pointer->sprite, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002741
2742 empty_region(&es->pending.input);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02002743 empty_region(&es->input);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002744
2745 if (!weston_surface_is_mapped(es)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002746 weston_layer_entry_insert(&es->compositor->cursor_layer.view_list,
2747 &pointer->sprite->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002748 weston_view_update_transform(pointer->sprite);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002749 es->is_mapped = true;
2750 pointer->sprite->is_mapped = true;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002751 }
2752}
2753
2754static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002755pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2756 uint32_t serial, struct wl_resource *surface_resource,
2757 int32_t x, int32_t y)
2758{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002759 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002760 struct weston_surface *surface = NULL;
2761
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +02002762 if (!pointer)
2763 return;
2764
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002765 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002766 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002767
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002768 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002769 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002770 /* pointer->focus->surface->resource can be NULL. Surfaces like the
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02002771 black_surface used in shell.c for fullscreen don't have
2772 a resource, but can still have focus */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002773 if (pointer->focus->surface->resource == NULL)
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02002774 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002775 if (wl_resource_get_client(pointer->focus->surface->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002776 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002777 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002778 return;
2779
Derek Foreman4e53c532015-03-23 10:55:32 -05002780 if (!surface) {
2781 if (pointer->sprite)
2782 pointer_unmap_sprite(pointer);
2783 return;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002784 }
2785
Jonas Ådahlb4070242015-03-18 15:08:03 +08002786 if (pointer->sprite && pointer->sprite->surface == surface &&
2787 pointer->hotspot_x == x && pointer->hotspot_y == y)
2788 return;
2789
Derek Foreman4e53c532015-03-23 10:55:32 -05002790 if (!pointer->sprite || pointer->sprite->surface != surface) {
2791 if (weston_surface_set_role(surface, "wl_pointer-cursor",
2792 resource,
2793 WL_POINTER_ERROR_ROLE) < 0)
2794 return;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002795
Derek Foreman4e53c532015-03-23 10:55:32 -05002796 if (pointer->sprite)
2797 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002798
Derek Foreman4e53c532015-03-23 10:55:32 -05002799 wl_signal_add(&surface->destroy_signal,
2800 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002801
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002802 surface->committed = pointer_cursor_surface_committed;
2803 surface->committed_private = pointer;
Derek Foreman4e53c532015-03-23 10:55:32 -05002804 weston_surface_set_label_func(surface,
2805 pointer_cursor_surface_get_label);
2806 pointer->sprite = weston_view_create(surface);
2807 }
2808
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002809 pointer->hotspot_x = x;
2810 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002811
Alexandros Frantzis28d66342021-06-09 10:56:26 +03002812 if (surface->width != 0) {
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002813 pointer_cursor_surface_committed(surface, 0, 0);
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +02002814 weston_view_schedule_repaint(pointer->sprite);
2815 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002816}
2817
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002818static void
2819pointer_release(struct wl_client *client, struct wl_resource *resource)
2820{
2821 wl_resource_destroy(resource);
2822}
2823
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002824static const struct wl_pointer_interface pointer_interface = {
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002825 pointer_set_cursor,
2826 pointer_release
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002827};
2828
2829static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002830seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2831 uint32_t id)
2832{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002833 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002834 /* We use the pointer_state directly, which means we'll
2835 * give a wl_pointer if the seat has ever had one - even though
2836 * the spec explicitly states that this request only takes effect
2837 * if the seat has the pointer capability.
2838 *
2839 * This prevents a race between the compositor sending new
2840 * capabilities and the client trying to use the old ones.
2841 */
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002842 struct weston_pointer *pointer = seat ? seat->pointer_state : NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002843 struct wl_resource *cr;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002844 struct weston_pointer_client *pointer_client;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002845
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02002846 cr = wl_resource_create(client, &wl_pointer_interface,
Jason Ekstranda85118c2013-06-27 20:17:02 -05002847 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002848 if (cr == NULL) {
2849 wl_client_post_no_memory(client);
2850 return;
2851 }
2852
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002853 wl_list_init(wl_resource_get_link(cr));
2854 wl_resource_set_implementation(cr, &pointer_interface, pointer,
2855 unbind_pointer_client_resource);
2856
2857 /* If we don't have a pointer_state, the resource is inert, so there
2858 * is nothing more to set up */
2859 if (!pointer)
2860 return;
2861
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002862 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
2863 if (!pointer_client) {
2864 wl_client_post_no_memory(client);
2865 return;
2866 }
2867
2868 wl_list_insert(&pointer_client->pointer_resources,
2869 wl_resource_get_link(cr));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002870
Derek Foreman1281a362015-07-31 16:55:32 -05002871 if (pointer->focus && pointer->focus->surface->resource &&
2872 wl_resource_get_client(pointer->focus->surface->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002873 wl_fixed_t sx, sy;
2874
Derek Foreman1281a362015-07-31 16:55:32 -05002875 weston_view_from_global_fixed(pointer->focus,
2876 pointer->x,
2877 pointer->y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002878 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002879
Neil Roberts96d790e2013-09-19 17:32:00 +01002880 wl_pointer_send_enter(cr,
Derek Foreman1281a362015-07-31 16:55:32 -05002881 pointer->focus_serial,
2882 pointer->focus->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01002883 sx, sy);
Peter Hutterer87743e92016-01-18 16:38:22 +10002884 pointer_send_frame(cr);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002885 }
2886}
2887
2888static void
Alexandros Frantzis2b442482018-02-20 14:05:50 +02002889destroy_keyboard_resource(struct wl_resource *resource)
2890{
2891 struct weston_keyboard *keyboard = wl_resource_get_user_data(resource);
2892
2893 wl_list_remove(wl_resource_get_link(resource));
2894
2895 if (keyboard) {
2896 remove_input_resource_from_timestamps(resource,
2897 &keyboard->timestamps_list);
leng.fangdbaf6fa2024-06-20 19:31:04 +08002898 weston_keyboard_clear_key_info_for_resource(keyboard, resource);
Alexandros Frantzis2b442482018-02-20 14:05:50 +02002899 }
2900}
2901
2902static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002903keyboard_release(struct wl_client *client, struct wl_resource *resource)
2904{
2905 wl_resource_destroy(resource);
2906}
2907
2908static const struct wl_keyboard_interface keyboard_interface = {
2909 keyboard_release
2910};
2911
2912static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002913seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2914 uint32_t id)
2915{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002916 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002917 /* We use the keyboard_state directly, which means we'll
2918 * give a wl_keyboard if the seat has ever had one - even though
2919 * the spec explicitly states that this request only takes effect
2920 * if the seat has the keyboard capability.
2921 *
2922 * This prevents a race between the compositor sending new
2923 * capabilities and the client trying to use the old ones.
2924 */
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002925 struct weston_keyboard *keyboard = seat ? seat->keyboard_state : NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002926 struct wl_resource *cr;
2927
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02002928 cr = wl_resource_create(client, &wl_keyboard_interface,
Jason Ekstranda85118c2013-06-27 20:17:02 -05002929 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002930 if (cr == NULL) {
2931 wl_client_post_no_memory(client);
2932 return;
2933 }
2934
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002935 wl_list_init(wl_resource_get_link(cr));
2936 wl_resource_set_implementation(cr, &keyboard_interface,
Alexandros Frantzis2b442482018-02-20 14:05:50 +02002937 keyboard, destroy_keyboard_resource);
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002938
2939 /* If we don't have a keyboard_state, the resource is inert, so there
2940 * is nothing more to set up */
2941 if (!keyboard)
2942 return;
2943
Neil Roberts96d790e2013-09-19 17:32:00 +01002944 /* May be moved to focused list later by either
2945 * weston_keyboard_set_focus or directly if this client is already
2946 * focused */
Derek Foreman345c9f32015-06-03 15:53:28 -05002947 wl_list_insert(&keyboard->resource_list, wl_resource_get_link(cr));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002948
Jonny Lamb66a41a02014-08-12 14:58:25 +02002949 if (wl_resource_get_version(cr) >= WL_KEYBOARD_REPEAT_INFO_SINCE_VERSION) {
2950 wl_keyboard_send_repeat_info(cr,
2951 seat->compositor->kb_repeat_rate,
2952 seat->compositor->kb_repeat_delay);
2953 }
Jasper St. Pierred8c6aeb2014-08-04 13:43:24 -04002954
Derek Foremanf8f7fd62017-06-28 11:41:43 -05002955 weston_keyboard_send_keymap(keyboard, cr);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002956
Derek Foreman345c9f32015-06-03 15:53:28 -05002957 if (keyboard->focus && keyboard->focus->resource &&
2958 wl_resource_get_client(keyboard->focus->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01002959 struct weston_surface *surface =
Derek Foreman345c9f32015-06-03 15:53:28 -05002960 (struct weston_surface *)keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +01002961
2962 wl_list_remove(wl_resource_get_link(cr));
Derek Foreman345c9f32015-06-03 15:53:28 -05002963 wl_list_insert(&keyboard->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002964 wl_resource_get_link(cr));
2965 wl_keyboard_send_enter(cr,
Derek Foreman345c9f32015-06-03 15:53:28 -05002966 keyboard->focus_serial,
Neil Roberts96d790e2013-09-19 17:32:00 +01002967 surface->resource,
Derek Foreman345c9f32015-06-03 15:53:28 -05002968 &keyboard->keys);
Neil Roberts96d790e2013-09-19 17:32:00 +01002969
Kirill Chibisovc46c70d2020-06-17 01:43:57 +03002970 send_modifiers_to_resource(keyboard,
2971 cr,
2972 keyboard->focus_serial);
2973
Neil Roberts96d790e2013-09-19 17:32:00 +01002974 /* If this is the first keyboard resource for this
2975 * client... */
Derek Foreman345c9f32015-06-03 15:53:28 -05002976 if (keyboard->focus_resource_list.prev ==
Neil Roberts96d790e2013-09-19 17:32:00 +01002977 wl_resource_get_link(cr))
2978 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002979 }
2980}
2981
2982static void
Alexandros Frantzisd7157842018-02-20 14:07:03 +02002983destroy_touch_resource(struct wl_resource *resource)
2984{
2985 struct weston_touch *touch = wl_resource_get_user_data(resource);
2986
2987 wl_list_remove(wl_resource_get_link(resource));
2988
2989 if (touch) {
2990 remove_input_resource_from_timestamps(resource,
2991 &touch->timestamps_list);
2992 }
2993}
2994
2995static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002996touch_release(struct wl_client *client, struct wl_resource *resource)
2997{
2998 wl_resource_destroy(resource);
2999}
3000
3001static const struct wl_touch_interface touch_interface = {
3002 touch_release
3003};
3004
3005static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003006seat_get_touch(struct wl_client *client, struct wl_resource *resource,
3007 uint32_t id)
3008{
Jason Ekstrand44a38632013-06-14 10:08:00 -05003009 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05003010 /* We use the touch_state directly, which means we'll
3011 * give a wl_touch if the seat has ever had one - even though
3012 * the spec explicitly states that this request only takes effect
3013 * if the seat has the touch capability.
3014 *
3015 * This prevents a race between the compositor sending new
3016 * capabilities and the client trying to use the old ones.
3017 */
Alexandros Frantzis8480d132018-02-15 13:07:09 +02003018 struct weston_touch *touch = seat ? seat->touch_state : NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003019 struct wl_resource *cr;
3020
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02003021 cr = wl_resource_create(client, &wl_touch_interface,
Jason Ekstranda85118c2013-06-27 20:17:02 -05003022 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003023 if (cr == NULL) {
3024 wl_client_post_no_memory(client);
3025 return;
3026 }
3027
Alexandros Frantzis8480d132018-02-15 13:07:09 +02003028 wl_list_init(wl_resource_get_link(cr));
3029 wl_resource_set_implementation(cr, &touch_interface,
Alexandros Frantzisd7157842018-02-20 14:07:03 +02003030 touch, destroy_touch_resource);
Alexandros Frantzis8480d132018-02-15 13:07:09 +02003031
3032 /* If we don't have a touch_state, the resource is inert, so there
3033 * is nothing more to set up */
3034 if (!touch)
3035 return;
3036
Derek Foreman1281a362015-07-31 16:55:32 -05003037 if (touch->focus &&
3038 wl_resource_get_client(touch->focus->surface->resource) == client) {
Chokshi, Mituld6697142015-10-09 08:28:47 +00003039 wl_list_insert(&touch->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01003040 wl_resource_get_link(cr));
3041 } else {
Chokshi, Mituld6697142015-10-09 08:28:47 +00003042 wl_list_insert(&touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01003043 wl_resource_get_link(cr));
3044 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003045}
3046
Quentin Glidicaab1d362016-03-13 17:49:08 +01003047static void
3048seat_release(struct wl_client *client, struct wl_resource *resource)
3049{
3050 wl_resource_destroy(resource);
3051}
3052
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003053static const struct wl_seat_interface seat_interface = {
3054 seat_get_pointer,
3055 seat_get_keyboard,
3056 seat_get_touch,
Quentin Glidicaab1d362016-03-13 17:49:08 +01003057 seat_release,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003058};
3059
3060static void
3061bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3062{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003063 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003064 struct wl_resource *resource;
3065 enum wl_seat_capability caps = 0;
3066
Jason Ekstranda85118c2013-06-27 20:17:02 -05003067 resource = wl_resource_create(client,
Derek Foreman1909c102015-11-26 14:17:47 -06003068 &wl_seat_interface, version, id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05003069 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003070 wl_resource_set_implementation(resource, &seat_interface, data,
3071 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003072
Derek Foreman1281a362015-07-31 16:55:32 -05003073 if (weston_seat_get_pointer(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003074 caps |= WL_SEAT_CAPABILITY_POINTER;
Derek Foreman1281a362015-07-31 16:55:32 -05003075 if (weston_seat_get_keyboard(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003076 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Derek Foreman1281a362015-07-31 16:55:32 -05003077 if (weston_seat_get_touch(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003078 caps |= WL_SEAT_CAPABILITY_TOUCH;
3079
3080 wl_seat_send_capabilities(resource, caps);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003081 if (version >= WL_SEAT_NAME_SINCE_VERSION)
Rob Bradforde445ae62013-05-31 18:09:51 +01003082 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003083}
3084
Jonas Ådahl30d61d82014-10-22 21:21:17 +02003085static void
3086relative_pointer_destroy(struct wl_client *client,
3087 struct wl_resource *resource)
3088{
3089 wl_resource_destroy(resource);
3090}
3091
3092static const struct zwp_relative_pointer_v1_interface relative_pointer_interface = {
3093 relative_pointer_destroy
3094};
3095
3096static void
3097relative_pointer_manager_destroy(struct wl_client *client,
3098 struct wl_resource *resource)
3099{
3100 wl_resource_destroy(resource);
3101}
3102
3103static void
3104relative_pointer_manager_get_relative_pointer(struct wl_client *client,
3105 struct wl_resource *resource,
3106 uint32_t id,
3107 struct wl_resource *pointer_resource)
3108{
3109 struct weston_pointer *pointer =
3110 wl_resource_get_user_data(pointer_resource);
3111 struct weston_pointer_client *pointer_client;
3112 struct wl_resource *cr;
3113
3114 cr = wl_resource_create(client, &zwp_relative_pointer_v1_interface,
3115 wl_resource_get_version(resource), id);
3116 if (cr == NULL) {
3117 wl_client_post_no_memory(client);
3118 return;
3119 }
3120
3121 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
3122 if (!pointer_client) {
3123 wl_client_post_no_memory(client);
3124 return;
3125 }
3126
3127 wl_list_insert(&pointer_client->relative_pointer_resources,
3128 wl_resource_get_link(cr));
3129 wl_resource_set_implementation(cr, &relative_pointer_interface,
3130 pointer,
3131 unbind_pointer_client_resource);
3132}
3133
3134static const struct zwp_relative_pointer_manager_v1_interface relative_pointer_manager = {
3135 relative_pointer_manager_destroy,
3136 relative_pointer_manager_get_relative_pointer,
3137};
3138
3139static void
3140bind_relative_pointer_manager(struct wl_client *client, void *data,
3141 uint32_t version, uint32_t id)
3142{
3143 struct weston_compositor *compositor = data;
3144 struct wl_resource *resource;
3145
3146 resource = wl_resource_create(client,
3147 &zwp_relative_pointer_manager_v1_interface,
3148 1, id);
3149
3150 wl_resource_set_implementation(resource, &relative_pointer_manager,
3151 compositor,
3152 NULL);
3153}
3154
Giulio Camuffo0358af42016-06-02 21:48:08 +03003155WL_EXPORT int
3156weston_compositor_set_xkb_rule_names(struct weston_compositor *ec,
3157 struct xkb_rule_names *names)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003158{
3159 if (ec->xkb_context == NULL) {
Peter Hutterera2086bb2020-05-13 15:24:27 +10003160 ec->xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003161 if (ec->xkb_context == NULL) {
3162 weston_log("failed to create XKB context\n");
3163 return -1;
3164 }
3165 }
3166
3167 if (names)
3168 ec->xkb_names = *names;
3169 if (!ec->xkb_names.rules)
3170 ec->xkb_names.rules = strdup("evdev");
3171 if (!ec->xkb_names.model)
3172 ec->xkb_names.model = strdup("pc105");
3173 if (!ec->xkb_names.layout)
3174 ec->xkb_names.layout = strdup("us");
3175
3176 return 0;
3177}
3178
Stefan Schmidtfda26522013-09-17 10:54:09 +01003179static void
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003180weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003181{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003182 if (--xkb_info->ref_count > 0)
3183 return;
3184
Ran Benitac9c74152014-08-19 23:59:52 +03003185 xkb_keymap_unref(xkb_info->keymap);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003186
Sebastian Wickabec5122019-11-01 02:38:45 +01003187 os_ro_anonymous_file_destroy(xkb_info->keymap_rofile);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003188 free(xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003189}
3190
3191void
3192weston_compositor_xkb_destroy(struct weston_compositor *ec)
3193{
3194 free((char *) ec->xkb_names.rules);
3195 free((char *) ec->xkb_names.model);
3196 free((char *) ec->xkb_names.layout);
3197 free((char *) ec->xkb_names.variant);
3198 free((char *) ec->xkb_names.options);
Stefan Schmidtfda26522013-09-17 10:54:09 +01003199
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003200 if (ec->xkb_info)
3201 weston_xkb_info_destroy(ec->xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003202 xkb_context_unref(ec->xkb_context);
3203}
3204
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003205static struct weston_xkb_info *
3206weston_xkb_info_create(struct xkb_keymap *keymap)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003207{
Sebastian Wickabec5122019-11-01 02:38:45 +01003208 char *keymap_string;
3209 size_t keymap_size;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003210 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
3211 if (xkb_info == NULL)
3212 return NULL;
3213
Ran Benita2e1968f2014-08-19 23:59:51 +03003214 xkb_info->keymap = xkb_keymap_ref(keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003215 xkb_info->ref_count = 1;
3216
Ran Benita2e1968f2014-08-19 23:59:51 +03003217 xkb_info->shift_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3218 XKB_MOD_NAME_SHIFT);
3219 xkb_info->caps_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3220 XKB_MOD_NAME_CAPS);
3221 xkb_info->ctrl_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3222 XKB_MOD_NAME_CTRL);
3223 xkb_info->alt_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3224 XKB_MOD_NAME_ALT);
3225 xkb_info->mod2_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3226 "Mod2");
3227 xkb_info->mod3_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3228 "Mod3");
3229 xkb_info->super_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3230 XKB_MOD_NAME_LOGO);
3231 xkb_info->mod5_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3232 "Mod5");
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003233
Ran Benita2e1968f2014-08-19 23:59:51 +03003234 xkb_info->num_led = xkb_keymap_led_get_index(xkb_info->keymap,
3235 XKB_LED_NAME_NUM);
3236 xkb_info->caps_led = xkb_keymap_led_get_index(xkb_info->keymap,
3237 XKB_LED_NAME_CAPS);
3238 xkb_info->scroll_led = xkb_keymap_led_get_index(xkb_info->keymap,
3239 XKB_LED_NAME_SCROLL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003240
Sebastian Wickabec5122019-11-01 02:38:45 +01003241 keymap_string = xkb_keymap_get_as_string(xkb_info->keymap,
Derek Foreman76829fc2017-06-28 12:17:46 -05003242 XKB_KEYMAP_FORMAT_TEXT_V1);
Sebastian Wickabec5122019-11-01 02:38:45 +01003243 if (keymap_string == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003244 weston_log("failed to get string version of keymap\n");
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003245 goto err_keymap;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003246 }
Sebastian Wickabec5122019-11-01 02:38:45 +01003247 keymap_size = strlen(keymap_string) + 1;
3248
3249 xkb_info->keymap_rofile = os_ro_anonymous_file_create(keymap_size,
3250 keymap_string);
3251 free(keymap_string);
3252
3253 if (!xkb_info->keymap_rofile) {
3254 weston_log("failed to create anonymous file for keymap\n");
3255 goto err_keymap;
3256 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003257
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003258 return xkb_info;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003259
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003260err_keymap:
Ran Benita2e1968f2014-08-19 23:59:51 +03003261 xkb_keymap_unref(xkb_info->keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003262 free(xkb_info);
3263 return NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003264}
3265
3266static int
3267weston_compositor_build_global_keymap(struct weston_compositor *ec)
3268{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003269 struct xkb_keymap *keymap;
3270
3271 if (ec->xkb_info != NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003272 return 0;
3273
Ran Benita2e1968f2014-08-19 23:59:51 +03003274 keymap = xkb_keymap_new_from_names(ec->xkb_context,
3275 &ec->xkb_names,
3276 0);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003277 if (keymap == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003278 weston_log("failed to compile global XKB keymap\n");
3279 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
3280 "options %s\n",
3281 ec->xkb_names.rules, ec->xkb_names.model,
3282 ec->xkb_names.layout, ec->xkb_names.variant,
3283 ec->xkb_names.options);
3284 return -1;
3285 }
3286
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003287 ec->xkb_info = weston_xkb_info_create(keymap);
Rui Matos73d93952013-10-24 19:28:41 +02003288 xkb_keymap_unref(keymap);
Stefan Schmidtfda26522013-09-17 10:54:09 +01003289 if (ec->xkb_info == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003290 return -1;
3291
3292 return 0;
3293}
3294
Rui Matos65196bc2013-10-10 19:44:19 +02003295WL_EXPORT void
3296weston_seat_update_keymap(struct weston_seat *seat, struct xkb_keymap *keymap)
3297{
Derek Foreman1281a362015-07-31 16:55:32 -05003298 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
3299
3300 if (!keyboard || !keymap)
Rui Matos65196bc2013-10-10 19:44:19 +02003301 return;
3302
Derek Foreman1281a362015-07-31 16:55:32 -05003303 xkb_keymap_unref(keyboard->pending_keymap);
3304 keyboard->pending_keymap = xkb_keymap_ref(keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02003305
Derek Foreman1281a362015-07-31 16:55:32 -05003306 if (keyboard->keys.size == 0)
Rui Matos65196bc2013-10-10 19:44:19 +02003307 update_keymap(seat);
Rui Matos65196bc2013-10-10 19:44:19 +02003308}
3309
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003310WL_EXPORT int
3311weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
3312{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003313 struct weston_keyboard *keyboard;
3314
Derek Foreman1281a362015-07-31 16:55:32 -05003315 if (seat->keyboard_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003316 seat->keyboard_device_count += 1;
3317 if (seat->keyboard_device_count == 1)
3318 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003319 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003320 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003321
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003322 keyboard = weston_keyboard_create();
3323 if (keyboard == NULL) {
3324 weston_log("failed to allocate weston keyboard struct\n");
3325 return -1;
3326 }
3327
Derek Foreman185d1582017-06-28 11:17:23 -05003328 if (keymap != NULL) {
3329 keyboard->xkb_info = weston_xkb_info_create(keymap);
3330 if (keyboard->xkb_info == NULL)
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02003331 goto err;
Derek Foreman185d1582017-06-28 11:17:23 -05003332 } else {
3333 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
3334 goto err;
3335 keyboard->xkb_info = seat->compositor->xkb_info;
3336 keyboard->xkb_info->ref_count++;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01003337 }
Derek Foreman185d1582017-06-28 11:17:23 -05003338
3339 keyboard->xkb_state.state = xkb_state_new(keyboard->xkb_info->keymap);
3340 if (keyboard->xkb_state.state == NULL) {
3341 weston_log("failed to initialise XKB state\n");
3342 goto err;
3343 }
3344
3345 keyboard->xkb_state.leds = 0;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01003346
Derek Foreman1281a362015-07-31 16:55:32 -05003347 seat->keyboard_state = keyboard;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02003348 seat->keyboard_device_count = 1;
3349 keyboard->seat = seat;
3350
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003351 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003352
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003353 return 0;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02003354
3355err:
3356 if (keyboard->xkb_info)
3357 weston_xkb_info_destroy(keyboard->xkb_info);
3358 free(keyboard);
3359
3360 return -1;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003361}
3362
Jonas Ådahl91fed542013-12-03 09:14:27 +01003363static void
3364weston_keyboard_reset_state(struct weston_keyboard *keyboard)
3365{
3366 struct weston_seat *seat = keyboard->seat;
3367 struct xkb_state *state;
3368
Derek Foreman185d1582017-06-28 11:17:23 -05003369 state = xkb_state_new(keyboard->xkb_info->keymap);
3370 if (!state) {
3371 weston_log("failed to reset XKB state\n");
3372 return;
Jonas Ådahl91fed542013-12-03 09:14:27 +01003373 }
Derek Foreman185d1582017-06-28 11:17:23 -05003374 xkb_state_unref(keyboard->xkb_state.state);
3375 keyboard->xkb_state.state = state;
3376
3377 keyboard->xkb_state.leds = 0;
Jonas Ådahl91fed542013-12-03 09:14:27 +01003378
3379 seat->modifier_state = 0;
3380}
3381
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003382WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003383weston_seat_release_keyboard(struct weston_seat *seat)
3384{
3385 seat->keyboard_device_count--;
Derek Foremand621df22014-11-19 11:04:12 -06003386 assert(seat->keyboard_device_count >= 0);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003387 if (seat->keyboard_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05003388 weston_keyboard_set_focus(seat->keyboard_state, NULL);
3389 weston_keyboard_cancel_grab(seat->keyboard_state);
3390 weston_keyboard_reset_state(seat->keyboard_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003391 seat_send_updated_caps(seat);
3392 }
3393}
3394
Marius Vladeb34f822021-03-30 23:09:05 +03003395WL_EXPORT int
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003396weston_seat_init_pointer(struct weston_seat *seat)
3397{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003398 struct weston_pointer *pointer;
3399
Derek Foreman1281a362015-07-31 16:55:32 -05003400 if (seat->pointer_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003401 seat->pointer_device_count += 1;
3402 if (seat->pointer_device_count == 1)
3403 seat_send_updated_caps(seat);
Marius Vladeb34f822021-03-30 23:09:05 +03003404 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003405 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003406
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003407 pointer = weston_pointer_create(seat);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003408 if (pointer == NULL)
Marius Vladeb34f822021-03-30 23:09:05 +03003409 return -1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003410
Derek Foreman1281a362015-07-31 16:55:32 -05003411 seat->pointer_state = pointer;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003412 seat->pointer_device_count = 1;
3413 pointer->seat = seat;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003414
3415 seat_send_updated_caps(seat);
Marius Vladeb34f822021-03-30 23:09:05 +03003416
3417 return 0;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003418}
3419
3420WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003421weston_seat_release_pointer(struct weston_seat *seat)
3422{
Derek Foreman1281a362015-07-31 16:55:32 -05003423 struct weston_pointer *pointer = seat->pointer_state;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003424
3425 seat->pointer_device_count--;
3426 if (seat->pointer_device_count == 0) {
Derek Foremanf9318d12015-05-11 15:40:11 -05003427 weston_pointer_clear_focus(pointer);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003428 weston_pointer_cancel_grab(pointer);
Jonas Ådahl630bae82013-10-17 23:04:06 +02003429
Jonas Ådahla4932742013-10-17 23:04:07 +02003430 if (pointer->sprite)
3431 pointer_unmap_sprite(pointer);
3432
Jonas Ådahl3e12e632013-12-02 22:05:05 +01003433 weston_pointer_reset_state(pointer);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003434 seat_send_updated_caps(seat);
Derek Foremanfd5ca512015-01-07 15:00:25 -06003435
3436 /* seat->pointer is intentionally not destroyed so that
3437 * a newly attached pointer on this seat will retain
3438 * the previous cursor co-ordinates.
3439 */
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003440 }
3441}
3442
Marius Vladeb34f822021-03-30 23:09:05 +03003443WL_EXPORT int
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003444weston_seat_init_touch(struct weston_seat *seat)
3445{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003446 struct weston_touch *touch;
3447
Derek Foreman1281a362015-07-31 16:55:32 -05003448 if (seat->touch_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003449 seat->touch_device_count += 1;
3450 if (seat->touch_device_count == 1)
3451 seat_send_updated_caps(seat);
Marius Vladeb34f822021-03-30 23:09:05 +03003452 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003453 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003454
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003455 touch = weston_touch_create();
3456 if (touch == NULL)
Marius Vladeb34f822021-03-30 23:09:05 +03003457 return -1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003458
Derek Foreman1281a362015-07-31 16:55:32 -05003459 seat->touch_state = touch;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003460 seat->touch_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003461 touch->seat = seat;
3462
3463 seat_send_updated_caps(seat);
Marius Vladeb34f822021-03-30 23:09:05 +03003464
3465 return 0;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003466}
3467
3468WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003469weston_seat_release_touch(struct weston_seat *seat)
3470{
3471 seat->touch_device_count--;
3472 if (seat->touch_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05003473 weston_touch_set_focus(seat->touch_state, NULL);
3474 weston_touch_cancel_grab(seat->touch_state);
3475 weston_touch_reset_state(seat->touch_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003476 seat_send_updated_caps(seat);
3477 }
3478}
3479
3480WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003481weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
3482 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003483{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003484 memset(seat, 0, sizeof *seat);
3485
Kristian Høgsberge3148752013-05-06 23:19:49 -04003486 seat->selection_data_source = NULL;
3487 wl_list_init(&seat->base_resource_list);
3488 wl_signal_init(&seat->selection_signal);
3489 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04003490 wl_signal_init(&seat->destroy_signal);
Jason Ekstranda4ab5422014-04-02 19:53:45 -05003491 wl_signal_init(&seat->updated_caps_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003492
Sebastian Wickabec5122019-11-01 02:38:45 +01003493 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface,
3494 MIN(wl_seat_interface.version, 7),
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003495 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003496
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003497 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003498 seat->modifier_state = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003499 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003500
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003501 wl_list_insert(ec->seat_list.prev, &seat->link);
3502
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003503 clipboard_create(seat);
3504
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003505 wl_signal_emit(&ec->seat_created_signal, seat);
3506}
3507
3508WL_EXPORT void
3509weston_seat_release(struct weston_seat *seat)
3510{
Alexandros Frantzis8480d132018-02-15 13:07:09 +02003511 struct wl_resource *resource;
3512
3513 wl_resource_for_each(resource, &seat->base_resource_list) {
3514 wl_resource_set_user_data(resource, NULL);
3515 }
3516
3517 wl_resource_for_each(resource, &seat->drag_resource_list) {
3518 wl_resource_set_user_data(resource, NULL);
3519 }
3520
3521 wl_list_remove(&seat->base_resource_list);
3522 wl_list_remove(&seat->drag_resource_list);
3523
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003524 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003525
Jonas Ådahl1afb2382014-01-03 19:46:51 +01003526 if (seat->saved_kbd_focus)
3527 wl_list_remove(&seat->saved_kbd_focus_listener.link);
3528
Derek Foreman1281a362015-07-31 16:55:32 -05003529 if (seat->pointer_state)
3530 weston_pointer_destroy(seat->pointer_state);
3531 if (seat->keyboard_state)
3532 weston_keyboard_destroy(seat->keyboard_state);
3533 if (seat->touch_state)
3534 weston_touch_destroy(seat->touch_state);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003535
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003536 free (seat->seat_name);
3537
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003538 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04003539
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003540 wl_signal_emit(&seat->destroy_signal, seat);
3541}
Derek Foreman1281a362015-07-31 16:55:32 -05003542
3543/** Get a seat's keyboard pointer
3544 *
3545 * \param seat The seat to query
3546 * \return The seat's keyboard pointer, or NULL if no keyboard is present
3547 *
3548 * The keyboard pointer for a seat isn't freed when all keyboards are removed,
3549 * so it should only be used when the seat's keyboard_device_count is greater
3550 * than zero. This function does that test and only returns a pointer
3551 * when a keyboard is present.
3552 */
3553WL_EXPORT struct weston_keyboard *
3554weston_seat_get_keyboard(struct weston_seat *seat)
3555{
3556 if (!seat)
3557 return NULL;
3558
3559 if (seat->keyboard_device_count)
3560 return seat->keyboard_state;
3561
3562 return NULL;
3563}
3564
3565/** Get a seat's pointer pointer
3566 *
3567 * \param seat The seat to query
3568 * \return The seat's pointer pointer, or NULL if no pointer device is present
3569 *
3570 * The pointer pointer for a seat isn't freed when all mice are removed,
3571 * so it should only be used when the seat's pointer_device_count is greater
3572 * than zero. This function does that test and only returns a pointer
3573 * when a pointing device is present.
3574 */
3575WL_EXPORT struct weston_pointer *
3576weston_seat_get_pointer(struct weston_seat *seat)
3577{
3578 if (!seat)
3579 return NULL;
3580
3581 if (seat->pointer_device_count)
3582 return seat->pointer_state;
3583
3584 return NULL;
3585}
3586
Jonas Ådahld3414f22016-07-22 17:56:31 +08003587static const struct zwp_locked_pointer_v1_interface locked_pointer_interface;
3588static const struct zwp_confined_pointer_v1_interface confined_pointer_interface;
3589
3590static enum pointer_constraint_type
3591pointer_constraint_get_type(struct weston_pointer_constraint *constraint)
3592{
3593 if (wl_resource_instance_of(constraint->resource,
3594 &zwp_locked_pointer_v1_interface,
3595 &locked_pointer_interface)) {
3596 return POINTER_CONSTRAINT_TYPE_LOCK;
3597 } else if (wl_resource_instance_of(constraint->resource,
3598 &zwp_confined_pointer_v1_interface,
3599 &confined_pointer_interface)) {
3600 return POINTER_CONSTRAINT_TYPE_CONFINE;
3601 }
3602
3603 abort();
3604 return 0;
3605}
3606
3607static void
3608pointer_constraint_notify_activated(struct weston_pointer_constraint *constraint)
3609{
3610 struct wl_resource *resource = constraint->resource;
3611
3612 switch (pointer_constraint_get_type(constraint)) {
3613 case POINTER_CONSTRAINT_TYPE_LOCK:
3614 zwp_locked_pointer_v1_send_locked(resource);
3615 break;
3616 case POINTER_CONSTRAINT_TYPE_CONFINE:
3617 zwp_confined_pointer_v1_send_confined(resource);
3618 break;
3619 }
3620}
3621
3622static void
3623pointer_constraint_notify_deactivated(struct weston_pointer_constraint *constraint)
3624{
3625 struct wl_resource *resource = constraint->resource;
3626
3627 switch (pointer_constraint_get_type(constraint)) {
3628 case POINTER_CONSTRAINT_TYPE_LOCK:
3629 zwp_locked_pointer_v1_send_unlocked(resource);
3630 break;
3631 case POINTER_CONSTRAINT_TYPE_CONFINE:
3632 zwp_confined_pointer_v1_send_unconfined(resource);
3633 break;
3634 }
3635}
3636
3637static struct weston_pointer_constraint *
3638get_pointer_constraint_for_pointer(struct weston_surface *surface,
3639 struct weston_pointer *pointer)
3640{
3641 struct weston_pointer_constraint *constraint;
3642
3643 wl_list_for_each(constraint, &surface->pointer_constraints, link) {
3644 if (constraint->pointer == pointer)
3645 return constraint;
3646 }
3647
3648 return NULL;
3649}
3650
Derek Foreman1281a362015-07-31 16:55:32 -05003651/** Get a seat's touch pointer
3652 *
3653 * \param seat The seat to query
3654 * \return The seat's touch pointer, or NULL if no touch device is present
3655 *
3656 * The touch pointer for a seat isn't freed when all touch devices are removed,
3657 * so it should only be used when the seat's touch_device_count is greater
3658 * than zero. This function does that test and only returns a pointer
3659 * when a touch device is present.
3660 */
3661WL_EXPORT struct weston_touch *
3662weston_seat_get_touch(struct weston_seat *seat)
3663{
3664 if (!seat)
3665 return NULL;
3666
3667 if (seat->touch_device_count)
3668 return seat->touch_state;
3669
3670 return NULL;
3671}
Bryce Harrington24f917e2016-06-29 19:04:07 -07003672
3673/** Sets the keyboard focus to the given surface
3674 *
Marius Vlada2dace22019-06-12 16:05:44 +03003675 * \param surface the surface to focus on
Bryce Harrington24f917e2016-06-29 19:04:07 -07003676 * \param seat The seat to query
3677 */
3678WL_EXPORT void
3679weston_seat_set_keyboard_focus(struct weston_seat *seat,
3680 struct weston_surface *surface)
3681{
3682 struct weston_compositor *compositor = seat->compositor;
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003683 struct weston_surface_activation_data activation_data;
Bryce Harrington24f917e2016-06-29 19:04:07 -07003684
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003685 inc_activate_serial(compositor);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003686
3687 activation_data = (struct weston_surface_activation_data) {
3688 .surface = surface,
3689 .seat = seat,
3690 };
3691 wl_signal_emit(&compositor->activate_signal, &activation_data);
Bryce Harrington24f917e2016-06-29 19:04:07 -07003692}
Jonas Ådahl30d61d82014-10-22 21:21:17 +02003693
Jonas Ådahld3414f22016-07-22 17:56:31 +08003694static void
3695enable_pointer_constraint(struct weston_pointer_constraint *constraint,
3696 struct weston_view *view)
3697{
3698 assert(constraint->view == NULL);
3699 constraint->view = view;
3700 pointer_constraint_notify_activated(constraint);
3701 weston_pointer_start_grab(constraint->pointer, &constraint->grab);
3702 wl_list_remove(&constraint->surface_destroy_listener.link);
3703 wl_list_init(&constraint->surface_destroy_listener.link);
3704}
3705
3706static bool
3707is_pointer_constraint_enabled(struct weston_pointer_constraint *constraint)
3708{
3709 return constraint->view != NULL;
3710}
3711
3712static void
3713weston_pointer_constraint_disable(struct weston_pointer_constraint *constraint)
3714{
3715 constraint->view = NULL;
3716 pointer_constraint_notify_deactivated(constraint);
3717 weston_pointer_end_grab(constraint->grab.pointer);
3718}
3719
3720void
3721weston_pointer_constraint_destroy(struct weston_pointer_constraint *constraint)
3722{
3723 if (is_pointer_constraint_enabled(constraint))
3724 weston_pointer_constraint_disable(constraint);
3725
3726 wl_list_remove(&constraint->pointer_destroy_listener.link);
3727 wl_list_remove(&constraint->surface_destroy_listener.link);
3728 wl_list_remove(&constraint->surface_commit_listener.link);
3729 wl_list_remove(&constraint->surface_activate_listener.link);
3730
3731 wl_resource_set_user_data(constraint->resource, NULL);
3732 pixman_region32_fini(&constraint->region);
3733 wl_list_remove(&constraint->link);
3734 free(constraint);
3735}
3736
3737static void
3738disable_pointer_constraint(struct weston_pointer_constraint *constraint)
3739{
3740 switch (constraint->lifetime) {
3741 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT:
3742 weston_pointer_constraint_destroy(constraint);
3743 break;
3744 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT:
3745 weston_pointer_constraint_disable(constraint);
3746 break;
3747 }
3748}
3749
3750static bool
3751is_within_constraint_region(struct weston_pointer_constraint *constraint,
3752 wl_fixed_t sx, wl_fixed_t sy)
3753{
3754 struct weston_surface *surface = constraint->surface;
3755 pixman_region32_t constraint_region;
3756 bool result;
3757
3758 pixman_region32_init(&constraint_region);
3759 pixman_region32_intersect(&constraint_region,
3760 &surface->input,
3761 &constraint->region);
3762 result = pixman_region32_contains_point(&constraint_region,
3763 wl_fixed_to_int(sx),
3764 wl_fixed_to_int(sy),
3765 NULL);
3766 pixman_region32_fini(&constraint_region);
3767
3768 return result;
3769}
3770
3771static void
3772maybe_enable_pointer_constraint(struct weston_pointer_constraint *constraint)
3773{
3774 struct weston_surface *surface = constraint->surface;
3775 struct weston_view *vit;
3776 struct weston_view *view = NULL;
3777 struct weston_pointer *pointer = constraint->pointer;
3778 struct weston_keyboard *keyboard;
3779 struct weston_seat *seat = pointer->seat;
3780 int32_t x, y;
3781
3782 /* Postpone if no view of the surface was most recently clicked. */
3783 wl_list_for_each(vit, &surface->views, surface_link) {
3784 if (vit->click_to_activate_serial ==
3785 surface->compositor->activate_serial) {
3786 view = vit;
3787 }
3788 }
3789 if (view == NULL)
3790 return;
3791
3792 /* Postpone if surface doesn't have keyboard focus. */
3793 keyboard = weston_seat_get_keyboard(seat);
3794 if (!keyboard || keyboard->focus != surface)
3795 return;
3796
3797 /* Postpone constraint if the pointer is not within the
3798 * constraint region.
3799 */
3800 weston_view_from_global(view,
3801 wl_fixed_to_int(pointer->x),
3802 wl_fixed_to_int(pointer->y),
3803 &x, &y);
3804 if (!is_within_constraint_region(constraint,
3805 wl_fixed_from_int(x),
3806 wl_fixed_from_int(y)))
3807 return;
3808
3809 enable_pointer_constraint(constraint, view);
3810}
3811
3812static void
3813locked_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
3814{
3815}
3816
3817static void
3818locked_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02003819 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003820 struct weston_pointer_motion_event *event)
3821{
Quentin Glidiccde13452016-08-12 10:41:32 +02003822 pointer_send_relative_motion(grab->pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08003823}
3824
3825static void
3826locked_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003827 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003828 uint32_t button,
3829 uint32_t state_w)
3830{
3831 weston_pointer_send_button(grab->pointer, time, button, state_w);
3832}
3833
3834static void
3835locked_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02003836 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003837 struct weston_pointer_axis_event *event)
3838{
3839 weston_pointer_send_axis(grab->pointer, time, event);
3840}
3841
3842static void
3843locked_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
3844 uint32_t source)
3845{
3846 weston_pointer_send_axis_source(grab->pointer, source);
3847}
3848
3849static void
3850locked_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
3851{
3852 weston_pointer_send_frame(grab->pointer);
3853}
3854
3855static void
3856locked_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
3857{
3858 struct weston_pointer_constraint *constraint =
3859 container_of(grab, struct weston_pointer_constraint, grab);
3860
3861 disable_pointer_constraint(constraint);
3862}
3863
3864static const struct weston_pointer_grab_interface
3865 locked_pointer_grab_interface = {
3866 locked_pointer_grab_pointer_focus,
3867 locked_pointer_grab_pointer_motion,
3868 locked_pointer_grab_pointer_button,
3869 locked_pointer_grab_pointer_axis,
3870 locked_pointer_grab_pointer_axis_source,
3871 locked_pointer_grab_pointer_frame,
3872 locked_pointer_grab_pointer_cancel,
3873};
3874
3875static void
3876pointer_constraint_constrain_resource_destroyed(struct wl_resource *resource)
3877{
3878 struct weston_pointer_constraint *constraint =
3879 wl_resource_get_user_data(resource);
3880
3881 if (!constraint)
3882 return;
3883
3884 weston_pointer_constraint_destroy(constraint);
3885}
3886
3887static void
3888pointer_constraint_surface_activate(struct wl_listener *listener, void *data)
3889{
3890 struct weston_surface_activation_data *activation = data;
3891 struct weston_pointer *pointer;
3892 struct weston_surface *focus = activation->surface;
3893 struct weston_pointer_constraint *constraint =
3894 container_of(listener, struct weston_pointer_constraint,
3895 surface_activate_listener);
3896 bool is_constraint_surface;
3897
3898 pointer = weston_seat_get_pointer(activation->seat);
3899 if (!pointer)
3900 return;
3901
3902 is_constraint_surface =
3903 get_pointer_constraint_for_pointer(focus, pointer) == constraint;
3904
3905 if (is_constraint_surface &&
3906 !is_pointer_constraint_enabled(constraint))
3907 maybe_enable_pointer_constraint(constraint);
3908 else if (!is_constraint_surface &&
3909 is_pointer_constraint_enabled(constraint))
3910 disable_pointer_constraint(constraint);
3911}
3912
3913static void
3914pointer_constraint_pointer_destroyed(struct wl_listener *listener, void *data)
3915{
3916 struct weston_pointer_constraint *constraint =
3917 container_of(listener, struct weston_pointer_constraint,
3918 pointer_destroy_listener);
3919
3920 weston_pointer_constraint_destroy(constraint);
3921}
3922
3923static void
3924pointer_constraint_surface_destroyed(struct wl_listener *listener, void *data)
3925{
3926 struct weston_pointer_constraint *constraint =
3927 container_of(listener, struct weston_pointer_constraint,
3928 surface_destroy_listener);
3929
3930 weston_pointer_constraint_destroy(constraint);
3931}
3932
3933static void
3934pointer_constraint_surface_committed(struct wl_listener *listener, void *data)
3935{
3936 struct weston_pointer_constraint *constraint =
3937 container_of(listener, struct weston_pointer_constraint,
3938 surface_commit_listener);
3939
3940 if (constraint->region_is_pending) {
3941 constraint->region_is_pending = false;
3942 pixman_region32_copy(&constraint->region,
3943 &constraint->region_pending);
3944 pixman_region32_fini(&constraint->region_pending);
3945 pixman_region32_init(&constraint->region_pending);
3946 }
3947
3948 if (constraint->hint_is_pending) {
3949 constraint->hint_is_pending = false;
3950
3951 constraint->hint_is_pending = true;
3952 constraint->hint_x = constraint->hint_x_pending;
3953 constraint->hint_y = constraint->hint_y_pending;
3954 }
3955
3956 if (pointer_constraint_get_type(constraint) ==
3957 POINTER_CONSTRAINT_TYPE_CONFINE &&
3958 is_pointer_constraint_enabled(constraint))
3959 maybe_warp_confined_pointer(constraint);
3960}
3961
3962static struct weston_pointer_constraint *
3963weston_pointer_constraint_create(struct weston_surface *surface,
3964 struct weston_pointer *pointer,
3965 struct weston_region *region,
3966 enum zwp_pointer_constraints_v1_lifetime lifetime,
3967 struct wl_resource *cr,
3968 const struct weston_pointer_grab_interface *grab_interface)
3969{
3970 struct weston_pointer_constraint *constraint;
3971
3972 constraint = zalloc(sizeof *constraint);
3973 if (!constraint)
3974 return NULL;
3975
3976 constraint->lifetime = lifetime;
3977 pixman_region32_init(&constraint->region);
3978 pixman_region32_init(&constraint->region_pending);
3979 wl_list_insert(&surface->pointer_constraints, &constraint->link);
3980 constraint->surface = surface;
3981 constraint->pointer = pointer;
3982 constraint->resource = cr;
3983 constraint->grab.interface = grab_interface;
3984 if (region) {
3985 pixman_region32_copy(&constraint->region,
3986 &region->region);
3987 } else {
3988 pixman_region32_fini(&constraint->region);
3989 region_init_infinite(&constraint->region);
3990 }
3991
3992 constraint->surface_activate_listener.notify =
3993 pointer_constraint_surface_activate;
3994 constraint->surface_destroy_listener.notify =
3995 pointer_constraint_surface_destroyed;
3996 constraint->surface_commit_listener.notify =
3997 pointer_constraint_surface_committed;
3998 constraint->pointer_destroy_listener.notify =
3999 pointer_constraint_pointer_destroyed;
4000
4001 wl_signal_add(&surface->compositor->activate_signal,
4002 &constraint->surface_activate_listener);
4003 wl_signal_add(&pointer->destroy_signal,
4004 &constraint->pointer_destroy_listener);
4005 wl_signal_add(&surface->destroy_signal,
4006 &constraint->surface_destroy_listener);
4007 wl_signal_add(&surface->commit_signal,
4008 &constraint->surface_commit_listener);
4009
4010 return constraint;
4011}
4012
4013static void
4014init_pointer_constraint(struct wl_resource *pointer_constraints_resource,
4015 uint32_t id,
4016 struct weston_surface *surface,
4017 struct weston_pointer *pointer,
4018 struct weston_region *region,
4019 enum zwp_pointer_constraints_v1_lifetime lifetime,
4020 const struct wl_interface *interface,
4021 const void *implementation,
4022 const struct weston_pointer_grab_interface *grab_interface)
4023{
4024 struct wl_client *client =
4025 wl_resource_get_client(pointer_constraints_resource);
4026 struct wl_resource *cr;
4027 struct weston_pointer_constraint *constraint;
4028
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02004029 if (pointer && get_pointer_constraint_for_pointer(surface, pointer)) {
Jonas Ådahld3414f22016-07-22 17:56:31 +08004030 wl_resource_post_error(pointer_constraints_resource,
4031 ZWP_POINTER_CONSTRAINTS_V1_ERROR_ALREADY_CONSTRAINED,
4032 "the pointer has a lock/confine request on this surface");
4033 return;
4034 }
4035
4036 cr = wl_resource_create(client, interface,
4037 wl_resource_get_version(pointer_constraints_resource),
4038 id);
4039 if (cr == NULL) {
4040 wl_client_post_no_memory(client);
4041 return;
4042 }
4043
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02004044 if (pointer) {
4045 constraint = weston_pointer_constraint_create(surface, pointer,
4046 region, lifetime,
4047 cr, grab_interface);
4048 if (constraint == NULL) {
4049 wl_client_post_no_memory(client);
4050 return;
4051 }
4052 } else {
4053 constraint = NULL;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004054 }
4055
4056 wl_resource_set_implementation(cr, implementation, constraint,
4057 pointer_constraint_constrain_resource_destroyed);
4058
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02004059 if (constraint)
4060 maybe_enable_pointer_constraint(constraint);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004061}
4062
4063static void
4064pointer_constraints_destroy(struct wl_client *client,
4065 struct wl_resource *resource)
4066{
4067 wl_resource_destroy(resource);
4068}
4069
4070static void
4071locked_pointer_destroy(struct wl_client *client,
4072 struct wl_resource *resource)
4073{
4074 struct weston_pointer_constraint *constraint =
4075 wl_resource_get_user_data(resource);
4076 wl_fixed_t x, y;
4077
4078 if (constraint && constraint->view && constraint->hint_is_pending &&
4079 is_within_constraint_region(constraint,
4080 constraint->hint_x,
4081 constraint->hint_y)) {
4082 weston_view_to_global_fixed(constraint->view,
4083 constraint->hint_x,
4084 constraint->hint_y,
4085 &x, &y);
4086 weston_pointer_move_to(constraint->pointer, x, y);
4087 }
4088 wl_resource_destroy(resource);
4089}
4090
4091static void
4092locked_pointer_set_cursor_position_hint(struct wl_client *client,
4093 struct wl_resource *resource,
4094 wl_fixed_t surface_x,
4095 wl_fixed_t surface_y)
4096{
4097 struct weston_pointer_constraint *constraint =
4098 wl_resource_get_user_data(resource);
4099
4100 /* Ignore a set cursor hint that was sent after the lock was cancelled.
4101 */
4102 if (!constraint ||
4103 !constraint->resource ||
4104 constraint->resource != resource)
4105 return;
4106
4107 constraint->hint_is_pending = true;
4108 constraint->hint_x_pending = surface_x;
4109 constraint->hint_y_pending = surface_y;
4110}
4111
4112static void
4113locked_pointer_set_region(struct wl_client *client,
4114 struct wl_resource *resource,
4115 struct wl_resource *region_resource)
4116{
4117 struct weston_pointer_constraint *constraint =
4118 wl_resource_get_user_data(resource);
4119 struct weston_region *region = region_resource ?
4120 wl_resource_get_user_data(region_resource) : NULL;
4121
4122 if (!constraint)
4123 return;
4124
4125 if (region) {
4126 pixman_region32_copy(&constraint->region_pending,
4127 &region->region);
4128 } else {
4129 pixman_region32_fini(&constraint->region_pending);
4130 region_init_infinite(&constraint->region_pending);
4131 }
4132 constraint->region_is_pending = true;
4133}
4134
4135
4136static const struct zwp_locked_pointer_v1_interface locked_pointer_interface = {
4137 locked_pointer_destroy,
4138 locked_pointer_set_cursor_position_hint,
4139 locked_pointer_set_region,
4140};
4141
4142static void
4143pointer_constraints_lock_pointer(struct wl_client *client,
4144 struct wl_resource *resource,
4145 uint32_t id,
4146 struct wl_resource *surface_resource,
4147 struct wl_resource *pointer_resource,
4148 struct wl_resource *region_resource,
4149 uint32_t lifetime)
4150{
4151 struct weston_surface *surface =
4152 wl_resource_get_user_data(surface_resource);
4153 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
4154 struct weston_region *region = region_resource ?
4155 wl_resource_get_user_data(region_resource) : NULL;
4156
4157 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
4158 &zwp_locked_pointer_v1_interface,
4159 &locked_pointer_interface,
4160 &locked_pointer_grab_interface);
4161}
4162
4163static void
4164confined_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
4165{
4166}
4167
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004168static double
4169vec2d_cross_product(struct vec2d a, struct vec2d b)
4170{
4171 return a.x * b.y - a.y * b.x;
4172}
4173
4174static struct vec2d
4175vec2d_add(struct vec2d a, struct vec2d b)
4176{
4177 return (struct vec2d) {
4178 .x = a.x + b.x,
4179 .y = a.y + b.y,
4180 };
4181}
4182
4183static struct vec2d
4184vec2d_subtract(struct vec2d a, struct vec2d b)
4185{
4186 return (struct vec2d) {
4187 .x = a.x - b.x,
4188 .y = a.y - b.y,
4189 };
4190}
4191
4192static struct vec2d
4193vec2d_multiply_constant(double c, struct vec2d a)
4194{
4195 return (struct vec2d) {
4196 .x = c * a.x,
4197 .y = c * a.y,
4198 };
4199}
4200
4201static bool
4202lines_intersect(struct line *line1, struct line *line2,
4203 struct vec2d *intersection)
4204{
4205 struct vec2d p = line1->a;
4206 struct vec2d r = vec2d_subtract(line1->b, line1->a);
4207 struct vec2d q = line2->a;
4208 struct vec2d s = vec2d_subtract(line2->b, line2->a);
4209 double rxs;
4210 double sxr;
4211 double t;
4212 double u;
4213
4214 /*
4215 * The line (p, r) and (q, s) intersects where
4216 *
4217 * p + t r = q + u s
4218 *
4219 * Calculate t:
4220 *
4221 * (p + t r) × s = (q + u s) × s
4222 * p × s + t (r × s) = q × s + u (s × s)
4223 * p × s + t (r × s) = q × s
4224 * t (r × s) = q × s - p × s
4225 * t (r × s) = (q - p) × s
4226 * t = ((q - p) × s) / (r × s)
4227 *
4228 * Using the same method, for u we get:
4229 *
4230 * u = ((p - q) × r) / (s × r)
4231 */
4232
4233 rxs = vec2d_cross_product(r, s);
4234 sxr = vec2d_cross_product(s, r);
4235
4236 /* If r × s = 0 then the lines are either parallel or collinear. */
4237 if (fabs(rxs) < DBL_MIN)
4238 return false;
4239
4240 t = vec2d_cross_product(vec2d_subtract(q, p), s) / rxs;
4241 u = vec2d_cross_product(vec2d_subtract(p, q), r) / sxr;
4242
4243 /* The lines only intersect if 0 ≤ t ≤ 1 and 0 ≤ u ≤ 1. */
4244 if (t < 0.0 || t > 1.0 || u < 0.0 || u > 1.0)
4245 return false;
4246
4247 *intersection = vec2d_add(p, vec2d_multiply_constant(t, r));
4248 return true;
4249}
4250
4251static struct border *
4252add_border(struct wl_array *array,
4253 double x1, double y1,
4254 double x2, double y2,
4255 enum motion_direction blocking_dir)
4256{
4257 struct border *border = wl_array_add(array, sizeof *border);
4258
4259 *border = (struct border) {
4260 .line = (struct line) {
4261 .a = (struct vec2d) {
4262 .x = x1,
4263 .y = y1,
4264 },
4265 .b = (struct vec2d) {
4266 .x = x2,
4267 .y = y2,
4268 },
4269 },
4270 .blocking_dir = blocking_dir,
4271 };
4272
4273 return border;
4274}
4275
4276static int
4277compare_lines_x(const void *a, const void *b)
4278{
4279 const struct border *border_a = a;
4280 const struct border *border_b = b;
4281
4282
4283 if (border_a->line.a.x == border_b->line.a.x)
4284 return border_a->line.b.x < border_b->line.b.x;
4285 else
4286 return border_a->line.a.x > border_b->line.a.x;
4287}
4288
4289static void
4290add_non_overlapping_edges(pixman_box32_t *boxes,
4291 int band_above_start,
4292 int band_below_start,
4293 int band_below_end,
4294 struct wl_array *borders)
4295{
4296 int i;
4297 struct wl_array band_merge;
4298 struct border *border;
4299 struct border *prev_border;
4300 struct border *new_border;
4301
4302 wl_array_init(&band_merge);
4303
4304 /* Add bottom band of previous row, and top band of current row, and
4305 * sort them so lower left x coordinate comes first. If there are two
4306 * borders with the same left x coordinate, the wider one comes first.
4307 */
4308 for (i = band_above_start; i < band_below_start; i++) {
4309 pixman_box32_t *box = &boxes[i];
4310 add_border(&band_merge, box->x1, box->y2, box->x2, box->y2,
4311 MOTION_DIRECTION_POSITIVE_Y);
4312 }
4313 for (i = band_below_start; i < band_below_end; i++) {
4314 pixman_box32_t *box= &boxes[i];
4315 add_border(&band_merge, box->x1, box->y1, box->x2, box->y1,
4316 MOTION_DIRECTION_NEGATIVE_Y);
4317 }
4318 qsort(band_merge.data,
4319 band_merge.size / sizeof *border,
4320 sizeof *border,
4321 compare_lines_x);
4322
4323 /* Combine the two combined bands so that any overlapping border is
4324 * eliminated. */
4325 prev_border = NULL;
4326 wl_array_for_each(border, &band_merge) {
4327 assert(border->line.a.y == border->line.b.y);
4328 assert(!prev_border ||
4329 prev_border->line.a.y == border->line.a.y);
4330 assert(!prev_border ||
4331 (prev_border->line.a.x != border->line.a.x ||
4332 prev_border->line.b.x != border->line.b.x));
4333 assert(!prev_border ||
4334 prev_border->line.a.x <= border->line.a.x);
4335
4336 if (prev_border &&
4337 prev_border->line.a.x == border->line.a.x) {
4338 /*
4339 * ------------ +
4340 * ------- =
4341 * [ ]-----
4342 */
4343 prev_border->line.a.x = border->line.b.x;
4344 } else if (prev_border &&
4345 prev_border->line.b.x == border->line.b.x) {
4346 /*
4347 * ------------ +
4348 * ------ =
4349 * ------[ ]
4350 */
4351 prev_border->line.b.x = border->line.a.x;
4352 } else if (prev_border &&
4353 prev_border->line.b.x == border->line.a.x) {
4354 /*
4355 * -------- +
4356 * ------ =
4357 * --------------
4358 */
4359 prev_border->line.b.x = border->line.b.x;
4360 } else if (prev_border &&
4361 prev_border->line.b.x >= border->line.a.x) {
4362 /*
4363 * --------------- +
4364 * ------ =
4365 * -----[ ]----
4366 */
4367 new_border = add_border(borders,
4368 border->line.b.x,
4369 border->line.b.y,
4370 prev_border->line.b.x,
4371 prev_border->line.b.y,
4372 prev_border->blocking_dir);
4373 prev_border->line.b.x = border->line.a.x;
4374 prev_border = new_border;
4375 } else {
4376 assert(!prev_border ||
4377 prev_border->line.b.x < border->line.a.x);
4378 /*
4379 * First border or non-overlapping.
4380 *
4381 * ----- +
4382 * ----- =
4383 * ----- -----
4384 */
4385 new_border = wl_array_add(borders, sizeof *border);
4386 *new_border = *border;
4387 prev_border = new_border;
4388 }
4389 }
4390
4391 wl_array_release(&band_merge);
4392}
4393
4394static void
4395add_band_bottom_edges(pixman_box32_t *boxes,
4396 int band_start,
4397 int band_end,
4398 struct wl_array *borders)
4399{
4400 int i;
4401
4402 for (i = band_start; i < band_end; i++) {
4403 add_border(borders,
4404 boxes[i].x1, boxes[i].y2,
4405 boxes[i].x2, boxes[i].y2,
4406 MOTION_DIRECTION_POSITIVE_Y);
4407 }
4408}
4409
4410static void
4411region_to_outline(pixman_region32_t *region, struct wl_array *borders)
4412{
4413 pixman_box32_t *boxes;
4414 int num_boxes;
4415 int i;
4416 int top_most, bottom_most;
4417 int current_roof;
4418 int prev_top;
4419 int band_start, prev_band_start;
4420
4421 /*
4422 * Remove any overlapping lines from the set of rectangles. Note that
4423 * pixman regions are grouped as rows of rectangles, where rectangles
4424 * in one row never touch or overlap and are all of the same height.
4425 *
4426 * -------- --- -------- ---
4427 * | | | | | | | |
4428 * ----------====---- --- ----------- ----- ---
4429 * | | => | |
4430 * ----==========--------- ----- ----------
4431 * | | | |
4432 * ------------------- -------------------
4433 *
4434 */
4435
4436 boxes = pixman_region32_rectangles(region, &num_boxes);
4437 prev_top = 0;
4438 top_most = boxes[0].y1;
4439 current_roof = top_most;
4440 bottom_most = boxes[num_boxes - 1].y2;
4441 band_start = 0;
4442 prev_band_start = 0;
4443 for (i = 0; i < num_boxes; i++) {
4444 /* Detect if there is a vertical empty space, and add the lower
4445 * level of the previous band if so was the case. */
4446 if (i > 0 &&
4447 boxes[i].y1 != prev_top &&
4448 boxes[i].y1 != boxes[i - 1].y2) {
4449 current_roof = boxes[i].y1;
4450 add_band_bottom_edges(boxes,
4451 band_start,
4452 i,
4453 borders);
4454 }
4455
4456 /* Special case adding the last band, since it won't be handled
4457 * by the band change detection below. */
4458 if (boxes[i].y1 != current_roof && i == num_boxes - 1) {
4459 if (boxes[i].y1 != prev_top) {
4460 /* The last band is a single box, so we don't
4461 * have a prev_band_start to tell us when the
4462 * previous band started. */
4463 add_non_overlapping_edges(boxes,
4464 band_start,
4465 i,
4466 i + 1,
4467 borders);
4468 } else {
4469 add_non_overlapping_edges(boxes,
4470 prev_band_start,
4471 band_start,
4472 i + 1,
4473 borders);
4474 }
4475 }
4476
4477 /* Detect when passing a band and combine the top border of the
4478 * just passed band with the bottom band of the previous band.
4479 */
4480 if (boxes[i].y1 != top_most && boxes[i].y1 != prev_top) {
4481 /* Combine the two passed bands. */
4482 if (prev_top != current_roof) {
4483 add_non_overlapping_edges(boxes,
4484 prev_band_start,
4485 band_start,
4486 i,
4487 borders);
4488 }
4489
4490 prev_band_start = band_start;
4491 band_start = i;
4492 }
4493
4494 /* Add the top border if the box is part of the current roof. */
4495 if (boxes[i].y1 == current_roof) {
4496 add_border(borders,
4497 boxes[i].x1, boxes[i].y1,
4498 boxes[i].x2, boxes[i].y1,
4499 MOTION_DIRECTION_NEGATIVE_Y);
4500 }
4501
4502 /* Add the bottom border of the last band. */
4503 if (boxes[i].y2 == bottom_most) {
4504 add_border(borders,
4505 boxes[i].x1, boxes[i].y2,
4506 boxes[i].x2, boxes[i].y2,
4507 MOTION_DIRECTION_POSITIVE_Y);
4508 }
4509
4510 /* Always add the left border. */
4511 add_border(borders,
4512 boxes[i].x1, boxes[i].y1,
4513 boxes[i].x1, boxes[i].y2,
4514 MOTION_DIRECTION_NEGATIVE_X);
4515
4516 /* Always add the right border. */
4517 add_border(borders,
4518 boxes[i].x2, boxes[i].y1,
4519 boxes[i].x2, boxes[i].y2,
4520 MOTION_DIRECTION_POSITIVE_X);
4521
4522 prev_top = boxes[i].y1;
4523 }
4524}
4525
4526static bool
4527is_border_horizontal (struct border *border)
4528{
4529 return border->line.a.y == border->line.b.y;
4530}
4531
4532static bool
4533is_border_blocking_directions(struct border *border,
4534 uint32_t directions)
4535{
4536 /* Don't block parallel motions. */
4537 if (is_border_horizontal(border)) {
4538 if ((directions & (MOTION_DIRECTION_POSITIVE_Y |
4539 MOTION_DIRECTION_NEGATIVE_Y)) == 0)
4540 return false;
4541 } else {
4542 if ((directions & (MOTION_DIRECTION_POSITIVE_X |
4543 MOTION_DIRECTION_NEGATIVE_X)) == 0)
4544 return false;
4545 }
4546
4547 return (~border->blocking_dir & directions) != directions;
4548}
4549
4550static struct border *
4551get_closest_border(struct wl_array *borders,
4552 struct line *motion,
4553 uint32_t directions)
4554{
4555 struct border *border;
4556 struct vec2d intersection;
4557 struct vec2d delta;
4558 double distance_2;
4559 struct border *closest_border = NULL;
4560 double closest_distance_2 = DBL_MAX;
4561
4562 wl_array_for_each(border, borders) {
4563 if (!is_border_blocking_directions(border, directions))
4564 continue;
4565
4566 if (!lines_intersect(&border->line, motion, &intersection))
4567 continue;
4568
4569 delta = vec2d_subtract(intersection, motion->a);
4570 distance_2 = delta.x*delta.x + delta.y*delta.y;
4571 if (distance_2 < closest_distance_2) {
4572 closest_border = border;
4573 closest_distance_2 = distance_2;
4574 }
4575 }
4576
4577 return closest_border;
4578}
4579
4580static void
4581clamp_to_border(struct border *border,
4582 struct line *motion,
4583 uint32_t *motion_dir)
4584{
4585 /*
4586 * When clamping either rightward or downward motions, the motion needs
4587 * to be clamped so that the destination coordinate does not end up on
4588 * the border (see weston_pointer_clamp_event_to_region). Do this by
4589 * clamping such motions to the border minus the smallest possible
4590 * wl_fixed_t value.
4591 */
4592 if (is_border_horizontal(border)) {
4593 if (*motion_dir & MOTION_DIRECTION_POSITIVE_Y)
4594 motion->b.y = border->line.a.y - wl_fixed_to_double(1);
4595 else
4596 motion->b.y = border->line.a.y;
4597 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_Y |
4598 MOTION_DIRECTION_NEGATIVE_Y);
4599 } else {
4600 if (*motion_dir & MOTION_DIRECTION_POSITIVE_X)
4601 motion->b.x = border->line.a.x - wl_fixed_to_double(1);
4602 else
4603 motion->b.x = border->line.a.x;
4604 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_X |
4605 MOTION_DIRECTION_NEGATIVE_X);
4606 }
4607}
4608
4609static uint32_t
4610get_motion_directions(struct line *motion)
4611{
4612 uint32_t directions = 0;
4613
4614 if (motion->a.x < motion->b.x)
4615 directions |= MOTION_DIRECTION_POSITIVE_X;
4616 else if (motion->a.x > motion->b.x)
4617 directions |= MOTION_DIRECTION_NEGATIVE_X;
4618 if (motion->a.y < motion->b.y)
4619 directions |= MOTION_DIRECTION_POSITIVE_Y;
4620 else if (motion->a.y > motion->b.y)
4621 directions |= MOTION_DIRECTION_NEGATIVE_Y;
4622
4623 return directions;
4624}
4625
Jonas Ådahld3414f22016-07-22 17:56:31 +08004626static void
4627weston_pointer_clamp_event_to_region(struct weston_pointer *pointer,
4628 struct weston_pointer_motion_event *event,
4629 pixman_region32_t *region,
4630 wl_fixed_t *clamped_x,
4631 wl_fixed_t *clamped_y)
4632{
4633 wl_fixed_t x, y;
4634 wl_fixed_t sx, sy;
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004635 wl_fixed_t old_sx = pointer->sx;
4636 wl_fixed_t old_sy = pointer->sy;
4637 struct wl_array borders;
4638 struct line motion;
4639 struct border *closest_border;
4640 float new_x_f, new_y_f;
4641 uint32_t directions;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004642
4643 weston_pointer_motion_to_abs(pointer, event, &x, &y);
4644 weston_view_from_global_fixed(pointer->focus, x, y, &sx, &sy);
4645
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004646 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004647
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004648 /*
4649 * Generate borders given the confine region we are to use. The borders
4650 * are defined to be the outer region of the allowed area. This means
4651 * top/left borders are "within" the allowed area, while bottom/right
4652 * borders are outside. This needs to be considered when clamping
4653 * confined motion vectors.
4654 */
4655 region_to_outline(region, &borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004656
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004657 motion = (struct line) {
4658 .a = (struct vec2d) {
4659 .x = wl_fixed_to_double(old_sx),
4660 .y = wl_fixed_to_double(old_sy),
4661 },
4662 .b = (struct vec2d) {
4663 .x = wl_fixed_to_double(sx),
4664 .y = wl_fixed_to_double(sy),
4665 },
4666 };
4667 directions = get_motion_directions(&motion);
4668
4669 while (directions) {
4670 closest_border = get_closest_border(&borders,
4671 &motion,
4672 directions);
4673 if (closest_border)
4674 clamp_to_border(closest_border, &motion, &directions);
4675 else
4676 break;
4677 }
4678
4679 weston_view_to_global_float(pointer->focus,
4680 (float) motion.b.x, (float) motion.b.y,
4681 &new_x_f, &new_y_f);
4682 *clamped_x = wl_fixed_from_double(new_x_f);
4683 *clamped_y = wl_fixed_from_double(new_y_f);
4684
4685 wl_array_release(&borders);
4686}
4687
4688static double
4689point_to_border_distance_2(struct border *border, double x, double y)
4690{
4691 double orig_x, orig_y;
4692 double dx, dy;
4693
4694 if (is_border_horizontal(border)) {
4695 if (x < border->line.a.x)
4696 orig_x = border->line.a.x;
4697 else if (x > border->line.b.x)
4698 orig_x = border->line.b.x;
4699 else
4700 orig_x = x;
4701 orig_y = border->line.a.y;
4702 } else {
4703 if (y < border->line.a.y)
4704 orig_y = border->line.a.y;
4705 else if (y > border->line.b.y)
4706 orig_y = border->line.b.y;
4707 else
4708 orig_y = y;
4709 orig_x = border->line.a.x;
4710 }
4711
4712
4713 dx = fabs(orig_x - x);
4714 dy = fabs(orig_y - y);
4715 return dx*dx + dy*dy;
4716}
4717
4718static void
4719warp_to_behind_border(struct border *border, wl_fixed_t *sx, wl_fixed_t *sy)
4720{
4721 switch (border->blocking_dir) {
4722 case MOTION_DIRECTION_POSITIVE_X:
4723 case MOTION_DIRECTION_NEGATIVE_X:
4724 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_X)
4725 *sx = wl_fixed_from_double(border->line.a.x) - 1;
4726 else
4727 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4728 if (*sy < wl_fixed_from_double(border->line.a.y))
4729 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4730 else if (*sy > wl_fixed_from_double(border->line.b.y))
4731 *sy = wl_fixed_from_double(border->line.b.y) - 1;
4732 break;
4733 case MOTION_DIRECTION_POSITIVE_Y:
4734 case MOTION_DIRECTION_NEGATIVE_Y:
4735 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_Y)
4736 *sy = wl_fixed_from_double(border->line.a.y) - 1;
4737 else
4738 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4739 if (*sx < wl_fixed_from_double(border->line.a.x))
4740 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4741 else if (*sx > wl_fixed_from_double(border->line.b.x))
4742 *sx = wl_fixed_from_double(border->line.b.x) - 1;
4743 break;
4744 }
Jonas Ådahld3414f22016-07-22 17:56:31 +08004745}
4746
4747static void
4748maybe_warp_confined_pointer(struct weston_pointer_constraint *constraint)
4749{
4750 wl_fixed_t x;
4751 wl_fixed_t y;
4752 wl_fixed_t sx;
4753 wl_fixed_t sy;
4754
4755 weston_view_from_global_fixed(constraint->view,
4756 constraint->pointer->x,
4757 constraint->pointer->y,
4758 &sx,
4759 &sy);
4760
4761 if (!is_within_constraint_region(constraint, sx, sy)) {
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004762 double xf = wl_fixed_to_double(sx);
4763 double yf = wl_fixed_to_double(sy);
4764 pixman_region32_t confine_region;
4765 struct wl_array borders;
4766 struct border *border;
4767 double closest_distance_2 = DBL_MAX;
4768 struct border *closest_border = NULL;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004769
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004770 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004771
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004772 pixman_region32_init(&confine_region);
4773 pixman_region32_intersect(&confine_region,
4774 &constraint->view->surface->input,
4775 &constraint->region);
4776 region_to_outline(&confine_region, &borders);
4777 pixman_region32_fini(&confine_region);
4778
4779 wl_array_for_each(border, &borders) {
4780 double distance_2;
4781
4782 distance_2 = point_to_border_distance_2(border, xf, yf);
4783 if (distance_2 < closest_distance_2) {
4784 closest_border = border;
4785 closest_distance_2 = distance_2;
4786 }
4787 }
4788 assert(closest_border);
4789
4790 warp_to_behind_border(closest_border, &sx, &sy);
4791
4792 wl_array_release(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004793
4794 weston_view_to_global_fixed(constraint->view, sx, sy, &x, &y);
4795 weston_pointer_move_to(constraint->pointer, x, y);
4796 }
4797}
4798
4799static void
4800confined_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02004801 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004802 struct weston_pointer_motion_event *event)
4803{
4804 struct weston_pointer_constraint *constraint =
4805 container_of(grab, struct weston_pointer_constraint, grab);
4806 struct weston_pointer *pointer = grab->pointer;
4807 struct weston_surface *surface;
4808 wl_fixed_t x, y;
4809 wl_fixed_t old_sx = pointer->sx;
4810 wl_fixed_t old_sy = pointer->sy;
4811 pixman_region32_t confine_region;
4812
4813 assert(pointer->focus);
4814 assert(pointer->focus->surface == constraint->surface);
4815
4816 surface = pointer->focus->surface;
4817
4818 pixman_region32_init(&confine_region);
4819 pixman_region32_intersect(&confine_region,
4820 &surface->input,
4821 &constraint->region);
4822 weston_pointer_clamp_event_to_region(pointer, event,
4823 &confine_region, &x, &y);
4824 weston_pointer_move_to(pointer, x, y);
4825 pixman_region32_fini(&confine_region);
4826
4827 weston_view_from_global_fixed(pointer->focus, x, y,
4828 &pointer->sx, &pointer->sy);
4829
4830 if (old_sx != pointer->sx || old_sy != pointer->sy) {
Quentin Glidiccde13452016-08-12 10:41:32 +02004831 pointer_send_motion(pointer, time,
4832 pointer->sx, pointer->sy);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004833 }
4834
Quentin Glidiccde13452016-08-12 10:41:32 +02004835 pointer_send_relative_motion(pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004836}
4837
4838static void
4839confined_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02004840 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004841 uint32_t button,
4842 uint32_t state_w)
4843{
4844 weston_pointer_send_button(grab->pointer, time, button, state_w);
4845}
4846
4847static void
4848confined_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02004849 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004850 struct weston_pointer_axis_event *event)
4851{
4852 weston_pointer_send_axis(grab->pointer, time, event);
4853}
4854
4855static void
4856confined_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
4857 uint32_t source)
4858{
4859 weston_pointer_send_axis_source(grab->pointer, source);
4860}
4861
4862static void
4863confined_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
4864{
4865 weston_pointer_send_frame(grab->pointer);
4866}
4867
4868static void
4869confined_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
4870{
4871 struct weston_pointer_constraint *constraint =
4872 container_of(grab, struct weston_pointer_constraint, grab);
4873
4874 disable_pointer_constraint(constraint);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004875}
4876
4877static const struct weston_pointer_grab_interface
4878 confined_pointer_grab_interface = {
4879 confined_pointer_grab_pointer_focus,
4880 confined_pointer_grab_pointer_motion,
4881 confined_pointer_grab_pointer_button,
4882 confined_pointer_grab_pointer_axis,
4883 confined_pointer_grab_pointer_axis_source,
4884 confined_pointer_grab_pointer_frame,
4885 confined_pointer_grab_pointer_cancel,
4886};
4887
4888static void
4889confined_pointer_destroy(struct wl_client *client,
4890 struct wl_resource *resource)
4891{
4892 wl_resource_destroy(resource);
4893}
4894
4895static void
4896confined_pointer_set_region(struct wl_client *client,
4897 struct wl_resource *resource,
4898 struct wl_resource *region_resource)
4899{
4900 struct weston_pointer_constraint *constraint =
4901 wl_resource_get_user_data(resource);
4902 struct weston_region *region = region_resource ?
4903 wl_resource_get_user_data(region_resource) : NULL;
4904
4905 if (!constraint)
4906 return;
4907
4908 if (region) {
4909 pixman_region32_copy(&constraint->region_pending,
4910 &region->region);
4911 } else {
4912 pixman_region32_fini(&constraint->region_pending);
4913 region_init_infinite(&constraint->region_pending);
4914 }
4915 constraint->region_is_pending = true;
4916}
4917
4918static const struct zwp_confined_pointer_v1_interface confined_pointer_interface = {
4919 confined_pointer_destroy,
4920 confined_pointer_set_region,
4921};
4922
4923static void
4924pointer_constraints_confine_pointer(struct wl_client *client,
4925 struct wl_resource *resource,
4926 uint32_t id,
4927 struct wl_resource *surface_resource,
4928 struct wl_resource *pointer_resource,
4929 struct wl_resource *region_resource,
4930 uint32_t lifetime)
4931{
4932 struct weston_surface *surface =
4933 wl_resource_get_user_data(surface_resource);
4934 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
4935 struct weston_region *region = region_resource ?
4936 wl_resource_get_user_data(region_resource) : NULL;
4937
4938 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
4939 &zwp_confined_pointer_v1_interface,
4940 &confined_pointer_interface,
4941 &confined_pointer_grab_interface);
4942}
4943
4944static const struct zwp_pointer_constraints_v1_interface pointer_constraints_interface = {
4945 pointer_constraints_destroy,
4946 pointer_constraints_lock_pointer,
4947 pointer_constraints_confine_pointer,
4948};
4949
4950static void
4951bind_pointer_constraints(struct wl_client *client, void *data,
4952 uint32_t version, uint32_t id)
4953{
4954 struct wl_resource *resource;
4955
4956 resource = wl_resource_create(client,
4957 &zwp_pointer_constraints_v1_interface,
4958 1, id);
4959
4960 wl_resource_set_implementation(resource, &pointer_constraints_interface,
4961 NULL, NULL);
4962}
4963
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004964static void
Alexandros Frantzis2b442482018-02-20 14:05:50 +02004965input_timestamps_destroy(struct wl_client *client,
4966 struct wl_resource *resource)
4967{
4968 wl_resource_destroy(resource);
4969}
4970
4971static const struct zwp_input_timestamps_v1_interface
4972 input_timestamps_interface = {
4973 input_timestamps_destroy,
4974};
4975
4976static void
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004977input_timestamps_manager_destroy(struct wl_client *client,
4978 struct wl_resource *resource)
4979{
4980 wl_resource_destroy(resource);
4981}
4982
4983static void
4984input_timestamps_manager_get_keyboard_timestamps(struct wl_client *client,
4985 struct wl_resource *resource,
4986 uint32_t id,
4987 struct wl_resource *keyboard_resource)
4988{
Alexandros Frantzis2b442482018-02-20 14:05:50 +02004989 struct weston_keyboard *keyboard =
4990 wl_resource_get_user_data(keyboard_resource);
4991 struct wl_resource *input_ts;
4992
4993 input_ts = wl_resource_create(client,
4994 &zwp_input_timestamps_v1_interface,
4995 1, id);
4996 if (!input_ts) {
4997 wl_client_post_no_memory(client);
4998 return;
4999 }
5000
5001 if (keyboard) {
5002 wl_list_insert(&keyboard->timestamps_list,
5003 wl_resource_get_link(input_ts));
5004 } else {
5005 wl_list_init(wl_resource_get_link(input_ts));
5006 }
5007
5008 wl_resource_set_implementation(input_ts,
5009 &input_timestamps_interface,
5010 keyboard_resource,
5011 unbind_resource);
Alexandros Frantzis538749d2018-02-16 18:44:16 +02005012}
5013
5014static void
5015input_timestamps_manager_get_pointer_timestamps(struct wl_client *client,
5016 struct wl_resource *resource,
5017 uint32_t id,
5018 struct wl_resource *pointer_resource)
5019{
Alexandros Frantzisdb907b72018-02-20 14:06:26 +02005020 struct weston_pointer *pointer =
5021 wl_resource_get_user_data(pointer_resource);
5022 struct wl_resource *input_ts;
5023
5024 input_ts = wl_resource_create(client,
5025 &zwp_input_timestamps_v1_interface,
5026 1, id);
5027 if (!input_ts) {
5028 wl_client_post_no_memory(client);
5029 return;
5030 }
5031
5032 if (pointer) {
5033 wl_list_insert(&pointer->timestamps_list,
5034 wl_resource_get_link(input_ts));
5035 } else {
5036 wl_list_init(wl_resource_get_link(input_ts));
5037 }
5038
5039 wl_resource_set_implementation(input_ts,
5040 &input_timestamps_interface,
5041 pointer_resource,
5042 unbind_resource);
Alexandros Frantzis538749d2018-02-16 18:44:16 +02005043}
5044
5045static void
5046input_timestamps_manager_get_touch_timestamps(struct wl_client *client,
5047 struct wl_resource *resource,
5048 uint32_t id,
5049 struct wl_resource *touch_resource)
5050{
Alexandros Frantzisd7157842018-02-20 14:07:03 +02005051 struct weston_touch *touch = wl_resource_get_user_data(touch_resource);
5052 struct wl_resource *input_ts;
5053
5054 input_ts = wl_resource_create(client,
5055 &zwp_input_timestamps_v1_interface,
5056 1, id);
5057 if (!input_ts) {
5058 wl_client_post_no_memory(client);
5059 return;
5060 }
5061
5062 if (touch) {
5063 wl_list_insert(&touch->timestamps_list,
5064 wl_resource_get_link(input_ts));
5065 } else {
5066 wl_list_init(wl_resource_get_link(input_ts));
5067 }
5068
5069 wl_resource_set_implementation(input_ts,
5070 &input_timestamps_interface,
5071 touch_resource,
5072 unbind_resource);
Alexandros Frantzis538749d2018-02-16 18:44:16 +02005073}
5074
5075static const struct zwp_input_timestamps_manager_v1_interface
5076 input_timestamps_manager_interface = {
5077 input_timestamps_manager_destroy,
5078 input_timestamps_manager_get_keyboard_timestamps,
5079 input_timestamps_manager_get_pointer_timestamps,
5080 input_timestamps_manager_get_touch_timestamps,
5081};
5082
5083static void
5084bind_input_timestamps_manager(struct wl_client *client, void *data,
5085 uint32_t version, uint32_t id)
5086{
5087 struct wl_resource *resource =
5088 wl_resource_create(client,
5089 &zwp_input_timestamps_manager_v1_interface,
5090 1, id);
5091
5092 if (resource == NULL) {
5093 wl_client_post_no_memory(client);
5094 return;
5095 }
5096
5097 wl_resource_set_implementation(resource,
5098 &input_timestamps_manager_interface,
5099 NULL, NULL);
5100}
5101
Jonas Ådahl30d61d82014-10-22 21:21:17 +02005102int
5103weston_input_init(struct weston_compositor *compositor)
5104{
5105 if (!wl_global_create(compositor->wl_display,
5106 &zwp_relative_pointer_manager_v1_interface, 1,
5107 compositor, bind_relative_pointer_manager))
5108 return -1;
5109
Jonas Ådahld3414f22016-07-22 17:56:31 +08005110 if (!wl_global_create(compositor->wl_display,
5111 &zwp_pointer_constraints_v1_interface, 1,
5112 NULL, bind_pointer_constraints))
5113 return -1;
5114
Alexandros Frantzis538749d2018-02-16 18:44:16 +02005115 if (!wl_global_create(compositor->wl_display,
5116 &zwp_input_timestamps_manager_v1_interface, 1,
5117 NULL, bind_input_timestamps_manager))
5118 return -1;
5119
Jonas Ådahl30d61d82014-10-22 21:21:17 +02005120 return 0;
5121}