blob: 46b6d62cf705edc58e439eeaab64d71ef51ead50 [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
leng.fange9d800e2024-07-12 13:31:59 +08001019 add_key_flg = (state == WL_KEYBOARD_KEY_STATE_PRESSED);
1020 if (!weston_keyboard_has_focus_resource(keyboard) && add_key_flg)
Quentin Glidiccde13452016-08-12 10:41:32 +02001021 return;
1022
Neil Roberts96d790e2013-09-19 17:32:00 +01001023 resource_list = &keyboard->focus_resource_list;
Quentin Glidiccde13452016-08-12 10:41:32 +02001024 serial = wl_display_next_serial(display);
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02001025 msecs = timespec_to_msec(time);
leng.fangdbaf6fa2024-06-20 19:31:04 +08001026 is_int_key = weston_keyboard_is_intercepted_keycode(
1027 keyboard, key,
1028 0, 0, 0, 0,
1029 serial, state, msecs, false);
1030 if (is_int_key)
1031 return;
1032
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);
leng.fangbd0b8832024-08-09 18:36:50 +08001327 wl_list_init(&keyboard->virtual_key_info);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001328
1329 return keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001330}
1331
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001332static void
1333weston_xkb_info_destroy(struct weston_xkb_info *xkb_info);
1334
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001335static void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001336weston_keyboard_destroy(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001337{
Alexandros Frantzis7a314d62018-01-26 18:47:56 +02001338 struct wl_resource *resource;
1339
1340 wl_resource_for_each(resource, &keyboard->resource_list) {
1341 wl_resource_set_user_data(resource, NULL);
1342 }
1343
1344 wl_resource_for_each(resource, &keyboard->focus_resource_list) {
1345 wl_resource_set_user_data(resource, NULL);
1346 }
1347
1348 wl_list_remove(&keyboard->resource_list);
1349 wl_list_remove(&keyboard->focus_resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +01001350
Derek Foreman185d1582017-06-28 11:17:23 -05001351 xkb_state_unref(keyboard->xkb_state.state);
1352 if (keyboard->xkb_info)
1353 weston_xkb_info_destroy(keyboard->xkb_info);
1354 xkb_keymap_unref(keyboard->pending_keymap);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001355
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001356 wl_array_release(&keyboard->keys);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001357 wl_list_remove(&keyboard->focus_resource_listener.link);
Alexandros Frantzis2b442482018-02-20 14:05:50 +02001358 wl_list_remove(&keyboard->timestamps_list);
leng.fangdbaf6fa2024-06-20 19:31:04 +08001359 weston_keyboard_clear_key_intercept(keyboard);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001360 free(keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001361}
1362
Jonas Ådahlcbfa7f72013-12-02 22:05:04 +01001363static void
1364weston_touch_reset_state(struct weston_touch *touch)
1365{
1366 touch->num_tp = 0;
1367}
1368
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001369static struct weston_touch *
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001370weston_touch_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001371{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001372 struct weston_touch *touch;
1373
Peter Huttererf3d62272013-08-08 11:57:05 +10001374 touch = zalloc(sizeof *touch);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001375 if (touch == NULL)
1376 return NULL;
1377
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -05001378 wl_list_init(&touch->device_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001379 wl_list_init(&touch->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +01001380 wl_list_init(&touch->focus_resource_list);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001381 wl_list_init(&touch->focus_view_listener.link);
1382 touch->focus_view_listener.notify = touch_focus_view_destroyed;
1383 wl_list_init(&touch->focus_resource_listener.link);
1384 touch->focus_resource_listener.notify = touch_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001385 touch->default_grab.interface = &default_touch_grab_interface;
1386 touch->default_grab.touch = touch;
1387 touch->grab = &touch->default_grab;
1388 wl_signal_init(&touch->focus_signal);
Alexandros Frantzisd7157842018-02-20 14:07:03 +02001389 wl_list_init(&touch->timestamps_list);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001390
1391 return touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001392}
1393
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001394static void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001395weston_touch_destroy(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001396{
Alexandros Frantzisb0b598c2018-01-26 18:47:57 +02001397 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01001398
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -05001399 assert(wl_list_empty(&touch->device_list));
1400
Alexandros Frantzisb0b598c2018-01-26 18:47:57 +02001401 wl_resource_for_each(resource, &touch->resource_list) {
1402 wl_resource_set_user_data(resource, NULL);
1403 }
1404
1405 wl_resource_for_each(resource, &touch->focus_resource_list) {
1406 wl_resource_set_user_data(resource, NULL);
1407 }
1408
1409 wl_list_remove(&touch->resource_list);
1410 wl_list_remove(&touch->focus_resource_list);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001411 wl_list_remove(&touch->focus_view_listener.link);
1412 wl_list_remove(&touch->focus_resource_listener.link);
Alexandros Frantzisd7157842018-02-20 14:07:03 +02001413 wl_list_remove(&touch->timestamps_list);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001414 free(touch);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001415}
1416
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001417static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04001418seat_send_updated_caps(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001419{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001420 enum wl_seat_capability caps = 0;
Rob Bradford6e737f52013-09-06 17:48:19 +01001421 struct wl_resource *resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001422
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001423 if (seat->pointer_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001424 caps |= WL_SEAT_CAPABILITY_POINTER;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001425 if (seat->keyboard_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001426 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001427 if (seat->touch_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001428 caps |= WL_SEAT_CAPABILITY_TOUCH;
1429
Rob Bradford6e737f52013-09-06 17:48:19 +01001430 wl_resource_for_each(resource, &seat->base_resource_list) {
1431 wl_seat_send_capabilities(resource, caps);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001432 }
Jason Ekstranda4ab5422014-04-02 19:53:45 -05001433 wl_signal_emit(&seat->updated_caps_signal, seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001434}
1435
Derek Foremanf9318d12015-05-11 15:40:11 -05001436
1437/** Clear the pointer focus
1438 *
1439 * \param pointer the pointer to clear focus for.
1440 *
1441 * This can be used to unset pointer focus and set the co-ordinates to the
1442 * arbitrary values we use for the no focus case.
1443 *
1444 * There's no requirement to use this function. For example, passing the
1445 * results of a weston_compositor_pick_view() directly to
1446 * weston_pointer_set_focus() will do the right thing when no view is found.
1447 */
1448WL_EXPORT void
1449weston_pointer_clear_focus(struct weston_pointer *pointer)
1450{
1451 weston_pointer_set_focus(pointer, NULL,
1452 wl_fixed_from_int(-1000000),
1453 wl_fixed_from_int(-1000000));
1454}
1455
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001456WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001457weston_pointer_set_focus(struct weston_pointer *pointer,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001458 struct weston_view *view,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001459 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001460{
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001461 struct weston_pointer_client *pointer_client;
Derek Foreman1281a362015-07-31 16:55:32 -05001462 struct weston_keyboard *kbd = weston_seat_get_keyboard(pointer->seat);
Neil Roberts96d790e2013-09-19 17:32:00 +01001463 struct wl_resource *resource;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001464 struct wl_resource *surface_resource;
Rob Bradford880ebc72013-07-22 17:31:38 +01001465 struct wl_display *display = pointer->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001466 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01001467 struct wl_list *focus_resource_list;
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001468 int refocus = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001469
1470 if ((!pointer->focus && view) ||
1471 (pointer->focus && !view) ||
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001472 (pointer->focus && pointer->focus->surface != view->surface) ||
1473 pointer->sx != sx || pointer->sy != sy)
1474 refocus = 1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001475
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001476 if (pointer->focus_client && refocus) {
1477 focus_resource_list = &pointer->focus_client->pointer_resources;
1478 if (!wl_list_empty(focus_resource_list)) {
1479 serial = wl_display_next_serial(display);
1480 surface_resource = pointer->focus->surface->resource;
1481 wl_resource_for_each(resource, focus_resource_list) {
1482 wl_pointer_send_leave(resource, serial,
1483 surface_resource);
Peter Hutterer87743e92016-01-18 16:38:22 +10001484 pointer_send_frame(resource);
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001485 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001486 }
1487
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001488 pointer->focus_client = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001489 }
1490
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001491 pointer_client = find_pointer_client_for_view(pointer, view);
1492 if (pointer_client && refocus) {
1493 struct wl_client *surface_client = pointer_client->client;
Neil Roberts96d790e2013-09-19 17:32:00 +01001494
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001495 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001496
Jason Ekstranda7af7042013-10-12 22:38:11 -05001497 if (kbd && kbd->focus != view->surface)
Kristian Høgsbergcb406f12013-10-09 10:54:03 -07001498 send_modifiers_to_client_in_list(surface_client,
1499 &kbd->resource_list,
1500 serial,
1501 kbd);
1502
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001503 pointer->focus_client = pointer_client;
Neil Roberts96d790e2013-09-19 17:32:00 +01001504
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001505 focus_resource_list = &pointer->focus_client->pointer_resources;
Neil Roberts96d790e2013-09-19 17:32:00 +01001506 wl_resource_for_each(resource, focus_resource_list) {
1507 wl_pointer_send_enter(resource,
1508 serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001509 view->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01001510 sx, sy);
Peter Hutterer87743e92016-01-18 16:38:22 +10001511 pointer_send_frame(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001512 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001513
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001514 pointer->focus_serial = serial;
1515 }
1516
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001517 wl_list_remove(&pointer->focus_view_listener.link);
1518 wl_list_init(&pointer->focus_view_listener.link);
1519 wl_list_remove(&pointer->focus_resource_listener.link);
1520 wl_list_init(&pointer->focus_resource_listener.link);
Emilio Pozuelo Monfortaa7a4762013-11-19 11:37:15 +01001521 if (view)
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001522 wl_signal_add(&view->destroy_signal, &pointer->focus_view_listener);
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001523 if (view && view->surface->resource)
1524 wl_resource_add_destroy_listener(view->surface->resource,
1525 &pointer->focus_resource_listener);
1526
1527 pointer->focus = view;
1528 pointer->focus_view_listener.notify = pointer_focus_view_destroyed;
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001529 pointer->sx = sx;
1530 pointer->sy = sy;
1531
Derek Foremanf9318d12015-05-11 15:40:11 -05001532 assert(view || sx == wl_fixed_from_int(-1000000));
1533 assert(view || sy == wl_fixed_from_int(-1000000));
1534
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001535 wl_signal_emit(&pointer->focus_signal, pointer);
1536}
1537
Neil Roberts96d790e2013-09-19 17:32:00 +01001538static void
1539send_enter_to_resource_list(struct wl_list *list,
1540 struct weston_keyboard *keyboard,
1541 struct weston_surface *surface,
1542 uint32_t serial)
1543{
1544 struct wl_resource *resource;
1545
1546 wl_resource_for_each(resource, list) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001547 wl_keyboard_send_enter(resource, serial,
1548 surface->resource,
1549 &keyboard->keys);
Kirill Chibisovc46c70d2020-06-17 01:43:57 +03001550 send_modifiers_to_resource(keyboard, resource, serial);
Neil Roberts96d790e2013-09-19 17:32:00 +01001551 }
1552}
1553
leng.fang32af9fc2024-06-13 11:22:15 +08001554static bool weston_keyboard_need_enter_surface( struct weston_keyboard *keyboard,
1555 struct weston_surface *surface)
1556{
1557 bool enter = false;
1558 if ( (find_resource_for_surface(&keyboard->resource_list, surface)
1559 || find_resource_for_surface(&keyboard->focus_resource_list, surface)) &&
1560 keyboard->focus != surface ) {
1561 enter = true;
1562 }
limin.tian531dd232025-01-07 12:09:27 +00001563 weston_log("%s %d, enter surface:%p, enter:%d\n", __FUNCTION__,__LINE__, surface, enter);
leng.fang32af9fc2024-06-13 11:22:15 +08001564 return enter;
1565}
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001566WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001567weston_keyboard_set_focus(struct weston_keyboard *keyboard,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001568 struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001569{
Quentin Glidic85d55542017-07-21 14:02:40 +02001570 struct weston_seat *seat = keyboard->seat;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001571 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +01001572 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001573 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01001574 struct wl_list *focus_resource_list;
leng.fang27f8ab72024-08-22 14:47:07 +08001575 struct weston_surface* last_keyboard_focus = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001576
Pekka Paalanen72e183b2018-02-22 16:55:15 +02001577 /* Keyboard focus on a surface without a client is equivalent to NULL
1578 * focus as nothing would react to the keyboard events anyway.
1579 * Just set focus to NULL instead - the destroy listener hangs on the
1580 * wl_resource anyway.
1581 */
1582 if (surface && !surface->resource)
1583 surface = NULL;
1584
Neil Roberts96d790e2013-09-19 17:32:00 +01001585 focus_resource_list = &keyboard->focus_resource_list;
1586
leng.fang32af9fc2024-06-13 11:22:15 +08001587 if (weston_keyboard_need_enter_surface( keyboard, surface ) || surface == NULL)
1588 {
leng.fang27f8ab72024-08-22 14:47:07 +08001589 if (keyboard->focus)
1590 last_keyboard_focus = keyboard->focus->compositor->last_keyboard_focus;
leng.fang32af9fc2024-06-13 11:22:15 +08001591 if (!wl_list_empty(focus_resource_list) && keyboard->focus != surface) {
1592 serial = wl_display_next_serial(display);
1593 wl_resource_for_each(resource, focus_resource_list) {
leng.fange9d800e2024-07-12 13:31:59 +08001594 if (keyboard && keyboard->focus)
1595 keyboard->focus->compositor->last_keyboard_focus = keyboard->focus;
limin.tian531dd232025-01-07 12:09:27 +00001596 weston_log(" %s %d, leave surface:%p\n", __FUNCTION__,__LINE__, keyboard->focus);
leng.fang32af9fc2024-06-13 11:22:15 +08001597 wl_keyboard_send_leave(resource, serial,
1598 keyboard->focus->resource);
1599 }
1600 move_resources(&keyboard->resource_list, focus_resource_list);
leng.fangdbaf6fa2024-06-20 19:31:04 +08001601 }
1602 if ( surface == NULL && keyboard && keyboard->focus) {
leng.fang27f8ab72024-08-22 14:47:07 +08001603 surface = last_keyboard_focus;
leng.fangdbaf6fa2024-06-20 19:31:04 +08001604 if (surface == keyboard->focus)
1605 surface = NULL;
limin.tian531dd232025-01-07 12:09:27 +00001606 weston_log(" %s %d, need back to surface:%p\n", __FUNCTION__,__LINE__, surface);
Neil Roberts96d790e2013-09-19 17:32:00 +01001607 }
leng.fang32af9fc2024-06-13 11:22:15 +08001608
1609 if (find_resource_for_surface(&keyboard->resource_list, surface) &&
1610 keyboard->focus != surface) {
1611 struct wl_client *surface_client =
1612 wl_resource_get_client(surface->resource);
1613
1614 serial = wl_display_next_serial(display);
1615
1616 move_resources_for_client(focus_resource_list,
1617 &keyboard->resource_list,
1618 surface_client);
1619 send_enter_to_resource_list(focus_resource_list,
1620 keyboard,
1621 surface,
1622 serial);
1623 keyboard->focus_serial = serial;
1624 }
1625
1626 /* Since this function gets called from the surface destroy handler
1627 * we can't just remove the kbd focus listener, or we might corrupt
1628 * the list it's in.
1629 * Instead, we'll just set a flag to ignore the focus when the
1630 * compositor regains kbd focus.
1631 */
1632 seat->use_saved_kbd_focus = false;
1633
1634 wl_list_remove(&keyboard->focus_resource_listener.link);
1635 wl_list_init(&keyboard->focus_resource_listener.link);
1636 if (surface)
1637 wl_resource_add_destroy_listener(surface->resource,
1638 &keyboard->focus_resource_listener);
1639
1640 keyboard->focus = surface;
1641 wl_signal_emit(&keyboard->focus_signal, keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001642 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001643}
1644
Giulio Camuffoa20ca812014-11-22 11:16:56 +02001645/* Users of this function must manually manage the keyboard focus */
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001646WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001647weston_keyboard_start_grab(struct weston_keyboard *keyboard,
1648 struct weston_keyboard_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001649{
1650 keyboard->grab = grab;
1651 grab->keyboard = keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001652}
1653
1654WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001655weston_keyboard_end_grab(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001656{
1657 keyboard->grab = &keyboard->default_grab;
1658}
1659
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001660static void
1661weston_keyboard_cancel_grab(struct weston_keyboard *keyboard)
1662{
1663 keyboard->grab->interface->cancel(keyboard->grab);
1664}
1665
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001666WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001667weston_pointer_start_grab(struct weston_pointer *pointer,
1668 struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001669{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001670 pointer->grab = grab;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001671 grab->pointer = pointer;
Kristian Høgsbergda751b82013-07-04 00:58:07 -04001672 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001673}
1674
1675WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001676weston_pointer_end_grab(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001677{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001678 pointer->grab = &pointer->default_grab;
Kristian Høgsbergda751b82013-07-04 00:58:07 -04001679 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001680}
1681
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001682static void
1683weston_pointer_cancel_grab(struct weston_pointer *pointer)
1684{
1685 pointer->grab->interface->cancel(pointer->grab);
1686}
1687
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001688WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -04001689weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001690{
1691 touch->grab = grab;
1692 grab->touch = touch;
1693}
1694
1695WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -04001696weston_touch_end_grab(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001697{
1698 touch->grab = &touch->default_grab;
1699}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001700
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001701static void
1702weston_touch_cancel_grab(struct weston_touch *touch)
1703{
1704 touch->grab->interface->cancel(touch->grab);
1705}
1706
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001707static void
1708weston_pointer_clamp_for_output(struct weston_pointer *pointer,
1709 struct weston_output *output,
1710 wl_fixed_t *fx, wl_fixed_t *fy)
1711{
1712 int x, y;
1713
1714 x = wl_fixed_to_int(*fx);
1715 y = wl_fixed_to_int(*fy);
1716
1717 if (x < output->x)
1718 *fx = wl_fixed_from_int(output->x);
1719 else if (x >= output->x + output->width)
1720 *fx = wl_fixed_from_int(output->x +
1721 output->width - 1);
1722 if (y < output->y)
1723 *fy = wl_fixed_from_int(output->y);
1724 else if (y >= output->y + output->height)
1725 *fy = wl_fixed_from_int(output->y +
1726 output->height - 1);
1727}
1728
Rob Bradford806d8c02013-06-25 18:56:41 +01001729WL_EXPORT void
1730weston_pointer_clamp(struct weston_pointer *pointer, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001731{
Rob Bradford806d8c02013-06-25 18:56:41 +01001732 struct weston_compositor *ec = pointer->seat->compositor;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001733 struct weston_output *output, *prev = NULL;
1734 int x, y, old_x, old_y, valid = 0;
1735
1736 x = wl_fixed_to_int(*fx);
1737 y = wl_fixed_to_int(*fy);
Rob Bradford806d8c02013-06-25 18:56:41 +01001738 old_x = wl_fixed_to_int(pointer->x);
1739 old_y = wl_fixed_to_int(pointer->y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001740
1741 wl_list_for_each(output, &ec->output_list, link) {
Rob Bradford66bd9f52013-06-25 18:56:42 +01001742 if (pointer->seat->output && pointer->seat->output != output)
1743 continue;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001744 if (pixman_region32_contains_point(&output->region,
1745 x, y, NULL))
1746 valid = 1;
1747 if (pixman_region32_contains_point(&output->region,
1748 old_x, old_y, NULL))
1749 prev = output;
1750 }
1751
Rob Bradford66bd9f52013-06-25 18:56:42 +01001752 if (!prev)
1753 prev = pointer->seat->output;
1754
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001755 if (prev && !valid)
1756 weston_pointer_clamp_for_output(pointer, prev, fx, fy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001757}
1758
Jonas Ådahld2510102014-10-05 21:39:14 +02001759static void
1760weston_pointer_move_to(struct weston_pointer *pointer,
1761 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001762{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001763 int32_t ix, iy;
1764
Rob Bradford806d8c02013-06-25 18:56:41 +01001765 weston_pointer_clamp (pointer, &x, &y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001766
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001767 pointer->x = x;
1768 pointer->y = y;
1769
1770 ix = wl_fixed_to_int(x);
1771 iy = wl_fixed_to_int(y);
1772
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001773 if (pointer->sprite) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001774 weston_view_set_position(pointer->sprite,
1775 ix - pointer->hotspot_x,
1776 iy - pointer->hotspot_y);
1777 weston_view_schedule_repaint(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001778 }
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001779
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001780 pointer->grab->interface->focus(pointer->grab);
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001781 wl_signal_emit(&pointer->motion_signal, pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001782}
1783
Jonas Ådahld2510102014-10-05 21:39:14 +02001784WL_EXPORT void
Jonas Ådahld2510102014-10-05 21:39:14 +02001785weston_pointer_move(struct weston_pointer *pointer,
1786 struct weston_pointer_motion_event *event)
1787{
1788 wl_fixed_t x, y;
1789
1790 weston_pointer_motion_to_abs(pointer, event, &x, &y);
1791 weston_pointer_move_to(pointer, x, y);
1792}
1793
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001794/** Verify if the pointer is in a valid position and move it if it isn't.
1795 */
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001796static void
1797weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001798{
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001799 struct weston_pointer *pointer;
1800 struct weston_compositor *ec;
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001801 struct weston_output *output, *closest = NULL;
1802 int x, y, distance, min = INT_MAX;
1803 wl_fixed_t fx, fy;
1804
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001805 pointer = container_of(listener, struct weston_pointer,
1806 output_destroy_listener);
1807 ec = pointer->seat->compositor;
1808
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001809 x = wl_fixed_to_int(pointer->x);
1810 y = wl_fixed_to_int(pointer->y);
1811
1812 wl_list_for_each(output, &ec->output_list, link) {
1813 if (pixman_region32_contains_point(&output->region,
1814 x, y, NULL))
1815 return;
1816
1817 /* Aproximante the distance from the pointer to the center of
1818 * the output. */
1819 distance = abs(output->x + output->width / 2 - x) +
1820 abs(output->y + output->height / 2 - y);
1821 if (distance < min) {
1822 min = distance;
1823 closest = output;
1824 }
1825 }
1826
1827 /* Nothing to do if there's no output left. */
1828 if (!closest)
1829 return;
1830
1831 fx = pointer->x;
1832 fy = pointer->y;
1833
1834 weston_pointer_clamp_for_output(pointer, closest, &fx, &fy);
Jonas Ådahld2510102014-10-05 21:39:14 +02001835 weston_pointer_move_to(pointer, fx, fy);
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001836}
1837
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001838WL_EXPORT void
1839notify_motion(struct weston_seat *seat,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001840 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001841 struct weston_pointer_motion_event *event)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001842{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001843 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001844 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001845
1846 weston_compositor_wake(ec);
Jonas Ådahld2510102014-10-05 21:39:14 +02001847 pointer->grab->interface->motion(pointer->grab, time, event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001848}
1849
Daniel Stone96d47c02013-11-19 11:37:12 +01001850static void
1851run_modifier_bindings(struct weston_seat *seat, uint32_t old, uint32_t new)
1852{
1853 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001854 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Daniel Stone96d47c02013-11-19 11:37:12 +01001855 uint32_t diff;
1856 unsigned int i;
1857 struct {
1858 uint32_t xkb;
1859 enum weston_keyboard_modifier weston;
1860 } mods[] = {
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001861 { keyboard->xkb_info->ctrl_mod, MODIFIER_CTRL },
1862 { keyboard->xkb_info->alt_mod, MODIFIER_ALT },
1863 { keyboard->xkb_info->super_mod, MODIFIER_SUPER },
1864 { keyboard->xkb_info->shift_mod, MODIFIER_SHIFT },
Daniel Stone96d47c02013-11-19 11:37:12 +01001865 };
1866
1867 diff = new & ~old;
1868 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
1869 if (diff & (1 << mods[i].xkb))
1870 weston_compositor_run_modifier_binding(compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001871 keyboard,
Daniel Stone96d47c02013-11-19 11:37:12 +01001872 mods[i].weston,
1873 WL_KEYBOARD_KEY_STATE_PRESSED);
1874 }
1875
1876 diff = old & ~new;
1877 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
1878 if (diff & (1 << mods[i].xkb))
1879 weston_compositor_run_modifier_binding(compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001880 keyboard,
Daniel Stone96d47c02013-11-19 11:37:12 +01001881 mods[i].weston,
1882 WL_KEYBOARD_KEY_STATE_RELEASED);
1883 }
1884}
1885
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001886WL_EXPORT void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001887notify_motion_absolute(struct weston_seat *seat, const struct timespec *time,
1888 double x, double y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001889{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001890 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001891 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Jonas Ådahld2510102014-10-05 21:39:14 +02001892 struct weston_pointer_motion_event event = { 0 };
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001893
1894 weston_compositor_wake(ec);
Jonas Ådahld2510102014-10-05 21:39:14 +02001895
1896 event = (struct weston_pointer_motion_event) {
1897 .mask = WESTON_POINTER_MOTION_ABS,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001898 .x = x,
1899 .y = y,
Jonas Ådahld2510102014-10-05 21:39:14 +02001900 };
1901
1902 pointer->grab->interface->motion(pointer->grab, time, &event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001903}
1904
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02001905static unsigned int
1906peek_next_activate_serial(struct weston_compositor *c)
1907{
1908 unsigned serial = c->activate_serial + 1;
1909
1910 return serial == 0 ? 1 : serial;
1911}
1912
1913static void
1914inc_activate_serial(struct weston_compositor *c)
1915{
1916 c->activate_serial = peek_next_activate_serial (c);
1917}
1918
1919WL_EXPORT void
Marius Vladd6ccc8b2021-03-05 22:07:30 +02001920weston_view_activate_input(struct weston_view *view,
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02001921 struct weston_seat *seat,
1922 uint32_t flags)
1923{
1924 struct weston_compositor *compositor = seat->compositor;
1925
1926 if (flags & WESTON_ACTIVATE_FLAG_CLICKED) {
1927 view->click_to_activate_serial =
1928 peek_next_activate_serial(compositor);
1929 }
1930
1931 weston_seat_set_keyboard_focus(seat, view->surface);
1932}
1933
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001934WL_EXPORT void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001935notify_button(struct weston_seat *seat, const struct timespec *time,
1936 int32_t button, enum wl_pointer_button_state state)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001937{
1938 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001939 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001940
1941 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001942 weston_compositor_idle_inhibit(compositor);
1943 if (pointer->button_count == 0) {
1944 pointer->grab_button = button;
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001945 pointer->grab_time = *time;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001946 pointer->grab_x = pointer->x;
1947 pointer->grab_y = pointer->y;
1948 }
1949 pointer->button_count++;
1950 } else {
1951 weston_compositor_idle_release(compositor);
1952 pointer->button_count--;
1953 }
1954
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001955 weston_compositor_run_button_binding(compositor, pointer, time, button,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001956 state);
1957
1958 pointer->grab->interface->button(pointer->grab, time, button, state);
1959
1960 if (pointer->button_count == 1)
1961 pointer->grab_serial =
1962 wl_display_get_serial(compositor->wl_display);
1963}
1964
1965WL_EXPORT void
Alexandros Frantzis80321942017-11-16 18:20:56 +02001966notify_axis(struct weston_seat *seat, const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001967 struct weston_pointer_axis_event *event)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001968{
1969 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001970 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001971
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001972 weston_compositor_wake(compositor);
1973
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001974 if (weston_compositor_run_axis_binding(compositor, pointer,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001975 time, event))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001976 return;
1977
Peter Hutterer89b6a492016-01-18 15:58:17 +10001978 pointer->grab->interface->axis(pointer->grab, time, event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001979}
1980
Peter Hutterer87743e92016-01-18 16:38:22 +10001981WL_EXPORT void
1982notify_axis_source(struct weston_seat *seat, uint32_t source)
1983{
1984 struct weston_compositor *compositor = seat->compositor;
1985 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1986
1987 weston_compositor_wake(compositor);
1988
1989 pointer->grab->interface->axis_source(pointer->grab, source);
1990}
1991
1992WL_EXPORT void
1993notify_pointer_frame(struct weston_seat *seat)
1994{
1995 struct weston_compositor *compositor = seat->compositor;
1996 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1997
1998 weston_compositor_wake(compositor);
1999
2000 pointer->grab->interface->frame(pointer->grab);
2001}
2002
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03002003WL_EXPORT int
2004weston_keyboard_set_locks(struct weston_keyboard *keyboard,
2005 uint32_t mask, uint32_t value)
2006{
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03002007 uint32_t serial;
2008 xkb_mod_mask_t mods_depressed, mods_latched, mods_locked, group;
2009 xkb_mod_mask_t num, caps;
2010
2011 /* We don't want the leds to go out of sync with the actual state
2012 * so if the backend has no way to change the leds don't try to
2013 * change the state */
2014 if (!keyboard->seat->led_update)
2015 return -1;
2016
2017 mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
2018 XKB_STATE_DEPRESSED);
2019 mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
2020 XKB_STATE_LATCHED);
2021 mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
2022 XKB_STATE_LOCKED);
2023 group = xkb_state_serialize_group(keyboard->xkb_state.state,
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02002024 XKB_STATE_EFFECTIVE);
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03002025
2026 num = (1 << keyboard->xkb_info->mod2_mod);
2027 caps = (1 << keyboard->xkb_info->caps_mod);
2028 if (mask & WESTON_NUM_LOCK) {
2029 if (value & WESTON_NUM_LOCK)
2030 mods_locked |= num;
2031 else
2032 mods_locked &= ~num;
2033 }
2034 if (mask & WESTON_CAPS_LOCK) {
2035 if (value & WESTON_CAPS_LOCK)
2036 mods_locked |= caps;
2037 else
2038 mods_locked &= ~caps;
2039 }
2040
2041 xkb_state_update_mask(keyboard->xkb_state.state, mods_depressed,
2042 mods_latched, mods_locked, 0, 0, group);
2043
2044 serial = wl_display_next_serial(
2045 keyboard->seat->compositor->wl_display);
2046 notify_modifiers(keyboard->seat, serial);
2047
2048 return 0;
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03002049}
2050
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002051WL_EXPORT void
2052notify_modifiers(struct weston_seat *seat, uint32_t serial)
2053{
Derek Foreman1281a362015-07-31 16:55:32 -05002054 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002055 struct weston_keyboard_grab *grab = keyboard->grab;
2056 uint32_t mods_depressed, mods_latched, mods_locked, group;
2057 uint32_t mods_lookup;
2058 enum weston_led leds = 0;
2059 int changed = 0;
2060
2061 /* Serialize and update our internal state, checking to see if it's
2062 * different to the previous state. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002063 mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03002064 XKB_STATE_MODS_DEPRESSED);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002065 mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03002066 XKB_STATE_MODS_LATCHED);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002067 mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03002068 XKB_STATE_MODS_LOCKED);
2069 group = xkb_state_serialize_layout(keyboard->xkb_state.state,
2070 XKB_STATE_LAYOUT_EFFECTIVE);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002071
Derek Foreman244e99e2015-06-03 15:53:26 -05002072 if (mods_depressed != keyboard->modifiers.mods_depressed ||
2073 mods_latched != keyboard->modifiers.mods_latched ||
2074 mods_locked != keyboard->modifiers.mods_locked ||
2075 group != keyboard->modifiers.group)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002076 changed = 1;
2077
Derek Foreman244e99e2015-06-03 15:53:26 -05002078 run_modifier_bindings(seat, keyboard->modifiers.mods_depressed,
Daniel Stone96d47c02013-11-19 11:37:12 +01002079 mods_depressed);
2080
Derek Foreman244e99e2015-06-03 15:53:26 -05002081 keyboard->modifiers.mods_depressed = mods_depressed;
2082 keyboard->modifiers.mods_latched = mods_latched;
2083 keyboard->modifiers.mods_locked = mods_locked;
2084 keyboard->modifiers.group = group;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002085
2086 /* And update the modifier_state for bindings. */
2087 mods_lookup = mods_depressed | mods_latched;
2088 seat->modifier_state = 0;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002089 if (mods_lookup & (1 << keyboard->xkb_info->ctrl_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002090 seat->modifier_state |= MODIFIER_CTRL;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002091 if (mods_lookup & (1 << keyboard->xkb_info->alt_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002092 seat->modifier_state |= MODIFIER_ALT;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002093 if (mods_lookup & (1 << keyboard->xkb_info->super_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002094 seat->modifier_state |= MODIFIER_SUPER;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002095 if (mods_lookup & (1 << keyboard->xkb_info->shift_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002096 seat->modifier_state |= MODIFIER_SHIFT;
2097
2098 /* Finally, notify the compositor that LEDs have changed. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002099 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
2100 keyboard->xkb_info->num_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002101 leds |= LED_NUM_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002102 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
2103 keyboard->xkb_info->caps_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002104 leds |= LED_CAPS_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002105 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
2106 keyboard->xkb_info->scroll_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002107 leds |= LED_SCROLL_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002108 if (leds != keyboard->xkb_state.leds && seat->led_update)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002109 seat->led_update(seat, leds);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002110 keyboard->xkb_state.leds = leds;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002111
2112 if (changed) {
2113 grab->interface->modifiers(grab,
2114 serial,
2115 keyboard->modifiers.mods_depressed,
2116 keyboard->modifiers.mods_latched,
2117 keyboard->modifiers.mods_locked,
2118 keyboard->modifiers.group);
2119 }
2120}
2121
2122static void
2123update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
2124 enum wl_keyboard_key_state state)
2125{
Derek Foreman1281a362015-07-31 16:55:32 -05002126 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002127 enum xkb_key_direction direction;
2128
2129 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2130 direction = XKB_KEY_DOWN;
2131 else
2132 direction = XKB_KEY_UP;
2133
2134 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
2135 * broken keycode system, which starts at 8. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002136 xkb_state_update_key(keyboard->xkb_state.state, key + 8, direction);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002137
2138 notify_modifiers(seat, serial);
2139}
Rui Matos65196bc2013-10-10 19:44:19 +02002140
Derek Foremanf8f7fd62017-06-28 11:41:43 -05002141WL_EXPORT void
2142weston_keyboard_send_keymap(struct weston_keyboard *kbd, struct wl_resource *resource)
Rui Matos65196bc2013-10-10 19:44:19 +02002143{
Derek Foremanf8f7fd62017-06-28 11:41:43 -05002144 struct weston_xkb_info *xkb_info = kbd->xkb_info;
Derek Foreman76829fc2017-06-28 12:17:46 -05002145 int fd;
Sebastian Wickabec5122019-11-01 02:38:45 +01002146 size_t size;
2147 enum ro_anonymous_file_mapmode mapmode;
Derek Foremanf8f7fd62017-06-28 11:41:43 -05002148
Sebastian Wickabec5122019-11-01 02:38:45 +01002149 if (wl_resource_get_version(resource) < 7)
2150 mapmode = RO_ANONYMOUS_FILE_MAPMODE_SHARED;
2151 else
2152 mapmode = RO_ANONYMOUS_FILE_MAPMODE_PRIVATE;
2153
2154 fd = os_ro_anonymous_file_get_fd(xkb_info->keymap_rofile, mapmode);
2155 size = os_ro_anonymous_file_size(xkb_info->keymap_rofile);
2156
2157 if (fd == -1) {
2158 weston_log("creating a keymap file failed: %s\n",
Antonio Borneo39578632019-04-26 23:57:31 +02002159 strerror(errno));
Derek Foreman76829fc2017-06-28 12:17:46 -05002160 return;
2161 }
2162
Rui Matos65196bc2013-10-10 19:44:19 +02002163 wl_keyboard_send_keymap(resource,
2164 WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
Derek Foreman76829fc2017-06-28 12:17:46 -05002165 fd,
Sebastian Wickabec5122019-11-01 02:38:45 +01002166 size);
2167
2168 os_ro_anonymous_file_put_fd(fd);
Rui Matos65196bc2013-10-10 19:44:19 +02002169}
2170
2171static void
2172send_modifiers(struct wl_resource *resource, uint32_t serial, struct weston_keyboard *keyboard)
2173{
2174 wl_keyboard_send_modifiers(resource, serial,
2175 keyboard->modifiers.mods_depressed,
2176 keyboard->modifiers.mods_latched,
2177 keyboard->modifiers.mods_locked,
2178 keyboard->modifiers.group);
2179}
2180
2181static struct weston_xkb_info *
2182weston_xkb_info_create(struct xkb_keymap *keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02002183
2184static void
2185update_keymap(struct weston_seat *seat)
2186{
Derek Foreman1281a362015-07-31 16:55:32 -05002187 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Rui Matos65196bc2013-10-10 19:44:19 +02002188 struct wl_resource *resource;
2189 struct weston_xkb_info *xkb_info;
2190 struct xkb_state *state;
2191 xkb_mod_mask_t latched_mods;
2192 xkb_mod_mask_t locked_mods;
2193
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002194 xkb_info = weston_xkb_info_create(keyboard->pending_keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02002195
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002196 xkb_keymap_unref(keyboard->pending_keymap);
2197 keyboard->pending_keymap = NULL;
Rui Matos65196bc2013-10-10 19:44:19 +02002198
2199 if (!xkb_info) {
2200 weston_log("failed to create XKB info\n");
2201 return;
2202 }
2203
2204 state = xkb_state_new(xkb_info->keymap);
2205 if (!state) {
2206 weston_log("failed to initialise XKB state\n");
2207 weston_xkb_info_destroy(xkb_info);
2208 return;
2209 }
2210
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002211 latched_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
2212 XKB_STATE_MODS_LATCHED);
2213 locked_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
2214 XKB_STATE_MODS_LOCKED);
Rui Matos65196bc2013-10-10 19:44:19 +02002215 xkb_state_update_mask(state,
2216 0, /* depressed */
2217 latched_mods,
2218 locked_mods,
2219 0, 0, 0);
2220
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002221 weston_xkb_info_destroy(keyboard->xkb_info);
2222 keyboard->xkb_info = xkb_info;
Rui Matos65196bc2013-10-10 19:44:19 +02002223
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002224 xkb_state_unref(keyboard->xkb_state.state);
2225 keyboard->xkb_state.state = state;
Rui Matos65196bc2013-10-10 19:44:19 +02002226
Derek Foremanbc91e542015-06-03 15:53:27 -05002227 wl_resource_for_each(resource, &keyboard->resource_list)
Derek Foremanf8f7fd62017-06-28 11:41:43 -05002228 weston_keyboard_send_keymap(keyboard, resource);
Derek Foremanbc91e542015-06-03 15:53:27 -05002229 wl_resource_for_each(resource, &keyboard->focus_resource_list)
Derek Foremanf8f7fd62017-06-28 11:41:43 -05002230 weston_keyboard_send_keymap(keyboard, resource);
Rui Matos65196bc2013-10-10 19:44:19 +02002231
2232 notify_modifiers(seat, wl_display_next_serial(seat->compositor->wl_display));
2233
2234 if (!latched_mods && !locked_mods)
2235 return;
2236
Derek Foremanbc91e542015-06-03 15:53:27 -05002237 wl_resource_for_each(resource, &keyboard->resource_list)
2238 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), keyboard);
2239 wl_resource_for_each(resource, &keyboard->focus_resource_list)
2240 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), keyboard);
Rui Matos65196bc2013-10-10 19:44:19 +02002241}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002242
2243WL_EXPORT void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02002244notify_key(struct weston_seat *seat, const struct timespec *time, uint32_t key,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002245 enum wl_keyboard_key_state state,
2246 enum weston_key_state_update update_state)
2247{
2248 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002249 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002250 struct weston_keyboard_grab *grab = keyboard->grab;
Pekka Paalanen86b53962014-11-19 13:43:32 +02002251 uint32_t *k, *end;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002252
2253 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002254 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002255 } else {
2256 weston_compositor_idle_release(compositor);
2257 }
2258
Pekka Paalanen86b53962014-11-19 13:43:32 +02002259 end = keyboard->keys.data + keyboard->keys.size;
2260 for (k = keyboard->keys.data; k < end; k++) {
2261 if (*k == key) {
2262 /* Ignore server-generated repeats. */
2263 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2264 return;
2265 *k = *--end;
2266 }
2267 }
2268 keyboard->keys.size = (void *) end - keyboard->keys.data;
2269 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2270 k = wl_array_add(&keyboard->keys, sizeof *k);
2271 *k = key;
2272 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002273
2274 if (grab == &keyboard->default_grab ||
2275 grab == &keyboard->input_method_grab) {
Derek Foreman99a6a2d2015-07-15 13:00:43 -05002276 weston_compositor_run_key_binding(compositor, keyboard, time,
2277 key, state);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002278 grab = keyboard->grab;
2279 }
2280
2281 grab->interface->key(grab, time, key, state);
2282
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002283 if (keyboard->pending_keymap &&
Pekka Paalanen86b53962014-11-19 13:43:32 +02002284 keyboard->keys.size == 0)
Rui Matos65196bc2013-10-10 19:44:19 +02002285 update_keymap(seat);
2286
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002287 if (update_state == STATE_UPDATE_AUTOMATIC) {
2288 update_modifier_state(seat,
2289 wl_display_get_serial(compositor->wl_display),
2290 key,
2291 state);
2292 }
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002293
Olivier Fourdandf84dbe2016-06-30 16:01:56 +02002294 keyboard->grab_serial = wl_display_get_serial(compositor->wl_display);
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002295 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02002296 keyboard->grab_time = *time;
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002297 keyboard->grab_key = key;
2298 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002299}
2300
2301WL_EXPORT void
2302notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002303 double x, double y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002304{
Derek Foreman1281a362015-07-31 16:55:32 -05002305 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2306
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002307 if (output) {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002308 weston_pointer_move_to(pointer,
2309 wl_fixed_from_double(x),
2310 wl_fixed_from_double(y));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002311 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002312 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002313 * NULL) here, but somehow that breaks re-entry... */
2314 }
2315}
2316
2317static void
2318destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
2319{
2320 struct weston_seat *ws;
2321
2322 ws = container_of(listener, struct weston_seat,
2323 saved_kbd_focus_listener);
2324
2325 ws->saved_kbd_focus = NULL;
Derek Foremana822afa2021-08-10 17:17:24 -05002326
2327 wl_list_remove(&ws->saved_kbd_focus_listener.link);
2328 ws->saved_kbd_focus_listener.notify = NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002329}
2330
2331WL_EXPORT void
2332notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
2333 enum weston_key_state_update update_state)
2334{
2335 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002336 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04002337 struct weston_surface *surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002338 uint32_t *k, serial;
2339
2340 serial = wl_display_next_serial(compositor->wl_display);
2341 wl_array_copy(&keyboard->keys, keys);
2342 wl_array_for_each(k, &keyboard->keys) {
2343 weston_compositor_idle_inhibit(compositor);
2344 if (update_state == STATE_UPDATE_AUTOMATIC)
2345 update_modifier_state(seat, serial, *k,
2346 WL_KEYBOARD_KEY_STATE_PRESSED);
2347 }
2348
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002349 surface = seat->saved_kbd_focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002350 if (surface) {
Derek Foremana822afa2021-08-10 17:17:24 -05002351 wl_list_remove(&seat->saved_kbd_focus_listener.link);
2352 seat->saved_kbd_focus_listener.notify = NULL;
2353 seat->saved_kbd_focus = NULL;
2354 if (seat->use_saved_kbd_focus)
2355 weston_keyboard_set_focus(keyboard, surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002356 }
2357}
2358
2359WL_EXPORT void
2360notify_keyboard_focus_out(struct weston_seat *seat)
2361{
2362 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002363 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2364 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Quentin Glidic85d55542017-07-21 14:02:40 +02002365 struct weston_surface *focus = keyboard->focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002366 uint32_t *k, serial;
2367
2368 serial = wl_display_next_serial(compositor->wl_display);
2369 wl_array_for_each(k, &keyboard->keys) {
2370 weston_compositor_idle_release(compositor);
2371 update_modifier_state(seat, serial, *k,
2372 WL_KEYBOARD_KEY_STATE_RELEASED);
2373 }
2374
2375 seat->modifier_state = 0;
2376
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002377 weston_keyboard_set_focus(keyboard, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002378 weston_keyboard_cancel_grab(keyboard);
Derek Foreman1281a362015-07-31 16:55:32 -05002379 if (pointer)
2380 weston_pointer_cancel_grab(pointer);
Quentin Glidic85d55542017-07-21 14:02:40 +02002381
2382 if (focus) {
Derek Foremana822afa2021-08-10 17:17:24 -05002383 seat->use_saved_kbd_focus = true;
Quentin Glidic85d55542017-07-21 14:02:40 +02002384 seat->saved_kbd_focus = focus;
2385 seat->saved_kbd_focus_listener.notify =
2386 destroy_device_saved_kbd_focus;
2387 wl_signal_add(&focus->destroy_signal,
2388 &seat->saved_kbd_focus_listener);
2389 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002390}
2391
Michael Fua2bb7912013-07-23 15:51:06 +08002392WL_EXPORT void
Derek Foreman4c93c082015-04-30 16:45:41 -05002393weston_touch_set_focus(struct weston_touch *touch, struct weston_view *view)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002394{
Neil Roberts96d790e2013-09-19 17:32:00 +01002395 struct wl_list *focus_resource_list;
2396
Derek Foreman4c93c082015-04-30 16:45:41 -05002397 focus_resource_list = &touch->focus_resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002398
Derek Foreman4c93c082015-04-30 16:45:41 -05002399 if (view && touch->focus &&
2400 touch->focus->surface == view->surface) {
2401 touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002402 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002403 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002404
Derek Foreman4c93c082015-04-30 16:45:41 -05002405 wl_list_remove(&touch->focus_resource_listener.link);
2406 wl_list_init(&touch->focus_resource_listener.link);
2407 wl_list_remove(&touch->focus_view_listener.link);
2408 wl_list_init(&touch->focus_view_listener.link);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01002409
Neil Roberts96d790e2013-09-19 17:32:00 +01002410 if (!wl_list_empty(focus_resource_list)) {
Derek Foreman4c93c082015-04-30 16:45:41 -05002411 move_resources(&touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002412 focus_resource_list);
2413 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002414
Jason Ekstranda7af7042013-10-12 22:38:11 -05002415 if (view) {
Derek Foreman362656b2014-09-04 10:23:05 -05002416 struct wl_client *surface_client;
2417
2418 if (!view->surface->resource) {
Derek Foreman4c93c082015-04-30 16:45:41 -05002419 touch->focus = NULL;
Derek Foreman362656b2014-09-04 10:23:05 -05002420 return;
2421 }
2422
2423 surface_client = wl_resource_get_client(view->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +01002424 move_resources_for_client(focus_resource_list,
Derek Foreman4c93c082015-04-30 16:45:41 -05002425 &touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002426 surface_client);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01002427 wl_resource_add_destroy_listener(view->surface->resource,
Derek Foreman4c93c082015-04-30 16:45:41 -05002428 &touch->focus_resource_listener);
2429 wl_signal_add(&view->destroy_signal, &touch->focus_view_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002430 }
Derek Foreman4c93c082015-04-30 16:45:41 -05002431 touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002432}
2433
Pekka Paalanenf4062532018-02-26 16:18:29 +02002434static void
2435process_touch_normal(struct weston_touch_device *device,
2436 const struct timespec *time, int touch_id,
2437 double double_x, double double_y, int touch_type)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002438{
Pekka Paalanenbcbce332018-02-26 14:43:00 +02002439 struct weston_touch *touch = device->aggregate;
2440 struct weston_touch_grab *grab = device->aggregate->grab;
2441 struct weston_compositor *ec = device->aggregate->seat->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002442 struct weston_view *ev;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002443 wl_fixed_t sx, sy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002444 wl_fixed_t x = wl_fixed_from_double(double_x);
2445 wl_fixed_t y = wl_fixed_from_double(double_y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002446
2447 /* Update grab's global coordinates. */
Neil Roberts306fe082013-10-03 16:43:06 +01002448 if (touch_id == touch->grab_touch_id && touch_type != WL_TOUCH_UP) {
2449 touch->grab_x = x;
2450 touch->grab_y = y;
2451 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002452
2453 switch (touch_type) {
2454 case WL_TOUCH_DOWN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002455 /* the first finger down picks the view, and all further go
2456 * to that view for the remainder of the touch session i.e.
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002457 * until all touch points are up again. */
Jonas Ådahl9484b692013-12-02 22:05:03 +01002458 if (touch->num_tp == 1) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002459 ev = weston_compositor_pick_view(ec, x, y, &sx, &sy);
Derek Foreman4c93c082015-04-30 16:45:41 -05002460 weston_touch_set_focus(touch, ev);
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002461 } else if (!touch->focus) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002462 /* Unexpected condition: We have non-initial touch but
2463 * there is no focused surface.
2464 */
Chris Michael3f607d32015-10-07 11:59:49 -04002465 weston_log("touch event received with %d points down "
Jonas Ådahl9484b692013-12-02 22:05:03 +01002466 "but no surface focused\n", touch->num_tp);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002467 return;
2468 }
2469
Derek Foreman99a6a2d2015-07-15 13:00:43 -05002470 weston_compositor_run_touch_binding(ec, touch,
Kristian Høgsbergc8964012014-02-05 14:25:18 -08002471 time, touch_type);
2472
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002473 grab->interface->down(grab, time, touch_id, x, y);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002474 if (touch->num_tp == 1) {
Rusty Lynchf1407ff2013-08-08 21:13:57 -07002475 touch->grab_serial =
2476 wl_display_get_serial(ec->wl_display);
Neil Roberts306fe082013-10-03 16:43:06 +01002477 touch->grab_touch_id = touch_id;
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02002478 touch->grab_time = *time;
Rusty Lynchf1407ff2013-08-08 21:13:57 -07002479 touch->grab_x = x;
2480 touch->grab_y = y;
2481 }
2482
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002483 break;
2484 case WL_TOUCH_MOTION:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002485 ev = touch->focus;
2486 if (!ev)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002487 break;
2488
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +02002489 grab->interface->motion(grab, time, touch_id, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002490 break;
2491 case WL_TOUCH_UP:
Alexandros Frantzis27a51b82017-11-16 18:20:59 +02002492 grab->interface->up(grab, time, touch_id);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002493 if (touch->num_tp == 0)
Derek Foreman4c93c082015-04-30 16:45:41 -05002494 weston_touch_set_focus(touch, NULL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002495 break;
2496 }
2497}
2498
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002499static enum weston_touch_mode
2500get_next_touch_mode(enum weston_touch_mode from)
2501{
2502 switch (from) {
2503 case WESTON_TOUCH_MODE_PREP_NORMAL:
2504 return WESTON_TOUCH_MODE_NORMAL;
2505
2506 case WESTON_TOUCH_MODE_PREP_CALIB:
2507 return WESTON_TOUCH_MODE_CALIB;
2508
2509 case WESTON_TOUCH_MODE_NORMAL:
2510 case WESTON_TOUCH_MODE_CALIB:
2511 return from;
2512 }
2513
2514 return WESTON_TOUCH_MODE_NORMAL;
2515}
2516
2517/** Global touch mode update
2518 *
2519 * If no seat has a touch down and the compositor is in a PREP touch mode,
2520 * set the compositor to the goal touch mode.
2521 *
2522 * Calls calibrator if touch mode changed.
2523 */
2524static void
2525weston_compositor_update_touch_mode(struct weston_compositor *compositor)
2526{
2527 struct weston_seat *seat;
2528 struct weston_touch *touch;
2529 enum weston_touch_mode goal;
2530
2531 wl_list_for_each(seat, &compositor->seat_list, link) {
2532 touch = weston_seat_get_touch(seat);
2533 if (!touch)
2534 continue;
2535
2536 if (touch->num_tp > 0)
2537 return;
2538 }
2539
2540 goal = get_next_touch_mode(compositor->touch_mode);
2541 if (compositor->touch_mode != goal) {
2542 compositor->touch_mode = goal;
2543 touch_calibrator_mode_changed(compositor);
2544 }
2545}
2546
2547/** Start transition to normal touch event handling
2548 *
2549 * The touch event mode changes when all touches on all touch devices have
2550 * been lifted. If no touches are currently down, the transition is immediate.
2551 *
2552 * \sa weston_touch_mode
2553 */
2554void
2555weston_compositor_set_touch_mode_normal(struct weston_compositor *compositor)
2556{
2557 switch (compositor->touch_mode) {
2558 case WESTON_TOUCH_MODE_PREP_NORMAL:
2559 case WESTON_TOUCH_MODE_NORMAL:
2560 return;
2561 case WESTON_TOUCH_MODE_PREP_CALIB:
2562 compositor->touch_mode = WESTON_TOUCH_MODE_NORMAL;
2563 touch_calibrator_mode_changed(compositor);
2564 return;
2565 case WESTON_TOUCH_MODE_CALIB:
2566 compositor->touch_mode = WESTON_TOUCH_MODE_PREP_NORMAL;
2567 }
2568
2569 weston_compositor_update_touch_mode(compositor);
2570}
2571
2572/** Start transition to calibrator touch event handling
2573 *
2574 * The touch event mode changes when all touches on all touch devices have
2575 * been lifted. If no touches are currently down, the transition is immediate.
2576 *
2577 * \sa weston_touch_mode
2578 */
2579void
2580weston_compositor_set_touch_mode_calib(struct weston_compositor *compositor)
2581{
2582 switch (compositor->touch_mode) {
2583 case WESTON_TOUCH_MODE_PREP_CALIB:
2584 case WESTON_TOUCH_MODE_CALIB:
2585 assert(0);
2586 return;
2587 case WESTON_TOUCH_MODE_PREP_NORMAL:
2588 compositor->touch_mode = WESTON_TOUCH_MODE_CALIB;
2589 touch_calibrator_mode_changed(compositor);
2590 return;
2591 case WESTON_TOUCH_MODE_NORMAL:
2592 compositor->touch_mode = WESTON_TOUCH_MODE_PREP_CALIB;
2593 }
2594
2595 weston_compositor_update_touch_mode(compositor);
2596}
2597
Pekka Paalanenf4062532018-02-26 16:18:29 +02002598/** Feed in touch down, motion, and up events, calibratable device.
2599 *
2600 * It assumes always the correct cycle sequence until it gets here: touch_down
2601 * → touch_update → ... → touch_update → touch_end. The driver is responsible
2602 * for sending along such order.
2603 *
2604 * \param device The physical device that generated the event.
2605 * \param time The event timestamp.
2606 * \param touch_id ID for the touch point of this event (multi-touch).
Marius Vlada2dace22019-06-12 16:05:44 +03002607 * \param x X coordinate in compositor global space.
2608 * \param y Y coordinate in compositor global space.
Pekka Paalanenf4062532018-02-26 16:18:29 +02002609 * \param norm Normalized device X, Y coordinates in calibration space, or NULL.
2610 * \param touch_type Either WL_TOUCH_DOWN, WL_TOUCH_UP, or WL_TOUCH_MOTION.
2611 *
2612 * Coordinates double_x and double_y are used for normal operation.
2613 *
2614 * Coordinates norm are only used for touch device calibration. If and only if
2615 * the weston_touch_device does not support calibrating, norm must be NULL.
2616 *
2617 * The calibration space is the normalized coordinate space
2618 * [0.0, 1.0]×[0.0, 1.0] of the weston_touch_device. This is assumed to
2619 * map to the similar normalized coordinate space of the associated
2620 * weston_output.
2621 */
2622WL_EXPORT void
2623notify_touch_normalized(struct weston_touch_device *device,
2624 const struct timespec *time,
2625 int touch_id,
2626 double x, double y,
2627 const struct weston_point2d_device_normalized *norm,
2628 int touch_type)
2629{
Louis-Francis Ratté-Boulianne813a06e2017-11-28 20:42:47 -05002630 struct weston_seat *seat = device->aggregate->seat;
2631 struct weston_touch *touch = device->aggregate;
2632
Pekka Paalanenf4062532018-02-26 16:18:29 +02002633 if (touch_type != WL_TOUCH_UP) {
2634 if (weston_touch_device_can_calibrate(device))
2635 assert(norm != NULL);
2636 else
2637 assert(norm == NULL);
2638 }
2639
Louis-Francis Ratté-Boulianne813a06e2017-11-28 20:42:47 -05002640 /* Update touchpoints count regardless of the current mode. */
2641 switch (touch_type) {
2642 case WL_TOUCH_DOWN:
2643 weston_compositor_idle_inhibit(seat->compositor);
2644
2645 touch->num_tp++;
2646 break;
2647 case WL_TOUCH_UP:
2648 if (touch->num_tp == 0) {
2649 /* This can happen if we start out with one or
2650 * more fingers on the touch screen, in which
2651 * case we didn't get the corresponding down
2652 * event. */
Pekka Paalanen332a45e2018-03-02 14:20:59 +02002653 weston_log("Unmatched touch up event on seat %s, device %s\n",
2654 seat->seat_name, device->syspath);
2655 return;
Louis-Francis Ratté-Boulianne813a06e2017-11-28 20:42:47 -05002656 }
2657 weston_compositor_idle_release(seat->compositor);
2658
2659 touch->num_tp--;
2660 break;
2661 default:
2662 break;
2663 }
2664
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002665 /* Properly forward the touch event */
2666 switch (weston_touch_device_get_mode(device)) {
2667 case WESTON_TOUCH_MODE_NORMAL:
2668 case WESTON_TOUCH_MODE_PREP_CALIB:
2669 process_touch_normal(device, time, touch_id, x, y, touch_type);
2670 break;
2671 case WESTON_TOUCH_MODE_CALIB:
2672 case WESTON_TOUCH_MODE_PREP_NORMAL:
Louis-Francis Ratté-Boulianne83630982017-11-28 20:42:47 -05002673 notify_touch_calibrator(device, time, touch_id,
2674 norm, touch_type);
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002675 break;
2676 }
Pekka Paalanenf4062532018-02-26 16:18:29 +02002677}
2678
Jonas Ådahl1679f232014-04-12 09:39:51 +02002679WL_EXPORT void
Pekka Paalanenbcbce332018-02-26 14:43:00 +02002680notify_touch_frame(struct weston_touch_device *device)
Jonas Ådahl1679f232014-04-12 09:39:51 +02002681{
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002682 struct weston_touch_grab *grab;
Jonas Ådahl1679f232014-04-12 09:39:51 +02002683
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002684 switch (weston_touch_device_get_mode(device)) {
2685 case WESTON_TOUCH_MODE_NORMAL:
2686 case WESTON_TOUCH_MODE_PREP_CALIB:
2687 grab = device->aggregate->grab;
2688 grab->interface->frame(grab);
2689 break;
2690 case WESTON_TOUCH_MODE_CALIB:
2691 case WESTON_TOUCH_MODE_PREP_NORMAL:
Louis-Francis Ratté-Boulianne83630982017-11-28 20:42:47 -05002692 notify_touch_calibrator_frame(device);
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002693 break;
2694 }
2695
2696 weston_compositor_update_touch_mode(device->aggregate->seat->compositor);
Jonas Ådahl1679f232014-04-12 09:39:51 +02002697}
2698
Derek Foreman3cc004a2015-11-06 15:56:09 -06002699WL_EXPORT void
Pekka Paalanenbcbce332018-02-26 14:43:00 +02002700notify_touch_cancel(struct weston_touch_device *device)
Derek Foreman3cc004a2015-11-06 15:56:09 -06002701{
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002702 struct weston_touch_grab *grab;
Derek Foreman3cc004a2015-11-06 15:56:09 -06002703
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002704 switch (weston_touch_device_get_mode(device)) {
2705 case WESTON_TOUCH_MODE_NORMAL:
2706 case WESTON_TOUCH_MODE_PREP_CALIB:
2707 grab = device->aggregate->grab;
2708 grab->interface->cancel(grab);
2709 break;
2710 case WESTON_TOUCH_MODE_CALIB:
2711 case WESTON_TOUCH_MODE_PREP_NORMAL:
Louis-Francis Ratté-Boulianne83630982017-11-28 20:42:47 -05002712 notify_touch_calibrator_cancel(device);
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002713 break;
2714 }
2715
2716 weston_compositor_update_touch_mode(device->aggregate->seat->compositor);
Derek Foreman3cc004a2015-11-06 15:56:09 -06002717}
2718
Pekka Paalanen8274d902014-08-06 19:36:51 +03002719static int
2720pointer_cursor_surface_get_label(struct weston_surface *surface,
2721 char *buf, size_t len)
2722{
2723 return snprintf(buf, len, "cursor");
2724}
2725
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002726static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002727pointer_cursor_surface_committed(struct weston_surface *es,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002728 int32_t dx, int32_t dy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002729{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002730 struct weston_pointer *pointer = es->committed_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002731 int x, y;
2732
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002733 if (es->width == 0)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002734 return;
2735
Jason Ekstranda7af7042013-10-12 22:38:11 -05002736 assert(es == pointer->sprite->surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002737
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002738 pointer->hotspot_x -= dx;
2739 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002740
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002741 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
2742 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002743
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002744 weston_view_set_position(pointer->sprite, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002745
2746 empty_region(&es->pending.input);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02002747 empty_region(&es->input);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002748
2749 if (!weston_surface_is_mapped(es)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002750 weston_layer_entry_insert(&es->compositor->cursor_layer.view_list,
2751 &pointer->sprite->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002752 weston_view_update_transform(pointer->sprite);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002753 es->is_mapped = true;
2754 pointer->sprite->is_mapped = true;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002755 }
2756}
2757
2758static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002759pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2760 uint32_t serial, struct wl_resource *surface_resource,
2761 int32_t x, int32_t y)
2762{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002763 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002764 struct weston_surface *surface = NULL;
2765
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +02002766 if (!pointer)
2767 return;
2768
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002769 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002770 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002771
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002772 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002773 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002774 /* pointer->focus->surface->resource can be NULL. Surfaces like the
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02002775 black_surface used in shell.c for fullscreen don't have
2776 a resource, but can still have focus */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002777 if (pointer->focus->surface->resource == NULL)
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02002778 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002779 if (wl_resource_get_client(pointer->focus->surface->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002780 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002781 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002782 return;
2783
Derek Foreman4e53c532015-03-23 10:55:32 -05002784 if (!surface) {
2785 if (pointer->sprite)
2786 pointer_unmap_sprite(pointer);
2787 return;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002788 }
2789
Jonas Ådahlb4070242015-03-18 15:08:03 +08002790 if (pointer->sprite && pointer->sprite->surface == surface &&
2791 pointer->hotspot_x == x && pointer->hotspot_y == y)
2792 return;
2793
Derek Foreman4e53c532015-03-23 10:55:32 -05002794 if (!pointer->sprite || pointer->sprite->surface != surface) {
2795 if (weston_surface_set_role(surface, "wl_pointer-cursor",
2796 resource,
2797 WL_POINTER_ERROR_ROLE) < 0)
2798 return;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002799
Derek Foreman4e53c532015-03-23 10:55:32 -05002800 if (pointer->sprite)
2801 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002802
Derek Foreman4e53c532015-03-23 10:55:32 -05002803 wl_signal_add(&surface->destroy_signal,
2804 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002805
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002806 surface->committed = pointer_cursor_surface_committed;
2807 surface->committed_private = pointer;
Derek Foreman4e53c532015-03-23 10:55:32 -05002808 weston_surface_set_label_func(surface,
2809 pointer_cursor_surface_get_label);
2810 pointer->sprite = weston_view_create(surface);
2811 }
2812
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002813 pointer->hotspot_x = x;
2814 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002815
Alexandros Frantzis28d66342021-06-09 10:56:26 +03002816 if (surface->width != 0) {
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002817 pointer_cursor_surface_committed(surface, 0, 0);
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +02002818 weston_view_schedule_repaint(pointer->sprite);
2819 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002820}
2821
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002822static void
2823pointer_release(struct wl_client *client, struct wl_resource *resource)
2824{
2825 wl_resource_destroy(resource);
2826}
2827
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002828static const struct wl_pointer_interface pointer_interface = {
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002829 pointer_set_cursor,
2830 pointer_release
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002831};
2832
2833static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002834seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2835 uint32_t id)
2836{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002837 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002838 /* We use the pointer_state directly, which means we'll
2839 * give a wl_pointer if the seat has ever had one - even though
2840 * the spec explicitly states that this request only takes effect
2841 * if the seat has the pointer capability.
2842 *
2843 * This prevents a race between the compositor sending new
2844 * capabilities and the client trying to use the old ones.
2845 */
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002846 struct weston_pointer *pointer = seat ? seat->pointer_state : NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002847 struct wl_resource *cr;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002848 struct weston_pointer_client *pointer_client;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002849
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02002850 cr = wl_resource_create(client, &wl_pointer_interface,
Jason Ekstranda85118c2013-06-27 20:17:02 -05002851 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002852 if (cr == NULL) {
2853 wl_client_post_no_memory(client);
2854 return;
2855 }
2856
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002857 wl_list_init(wl_resource_get_link(cr));
2858 wl_resource_set_implementation(cr, &pointer_interface, pointer,
2859 unbind_pointer_client_resource);
2860
2861 /* If we don't have a pointer_state, the resource is inert, so there
2862 * is nothing more to set up */
2863 if (!pointer)
2864 return;
2865
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002866 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
2867 if (!pointer_client) {
2868 wl_client_post_no_memory(client);
2869 return;
2870 }
2871
2872 wl_list_insert(&pointer_client->pointer_resources,
2873 wl_resource_get_link(cr));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002874
Derek Foreman1281a362015-07-31 16:55:32 -05002875 if (pointer->focus && pointer->focus->surface->resource &&
2876 wl_resource_get_client(pointer->focus->surface->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002877 wl_fixed_t sx, sy;
2878
Derek Foreman1281a362015-07-31 16:55:32 -05002879 weston_view_from_global_fixed(pointer->focus,
2880 pointer->x,
2881 pointer->y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002882 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002883
Neil Roberts96d790e2013-09-19 17:32:00 +01002884 wl_pointer_send_enter(cr,
Derek Foreman1281a362015-07-31 16:55:32 -05002885 pointer->focus_serial,
2886 pointer->focus->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01002887 sx, sy);
Peter Hutterer87743e92016-01-18 16:38:22 +10002888 pointer_send_frame(cr);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002889 }
2890}
2891
2892static void
Alexandros Frantzis2b442482018-02-20 14:05:50 +02002893destroy_keyboard_resource(struct wl_resource *resource)
2894{
2895 struct weston_keyboard *keyboard = wl_resource_get_user_data(resource);
2896
2897 wl_list_remove(wl_resource_get_link(resource));
2898
2899 if (keyboard) {
2900 remove_input_resource_from_timestamps(resource,
2901 &keyboard->timestamps_list);
leng.fangdbaf6fa2024-06-20 19:31:04 +08002902 weston_keyboard_clear_key_info_for_resource(keyboard, resource);
Alexandros Frantzis2b442482018-02-20 14:05:50 +02002903 }
2904}
2905
2906static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002907keyboard_release(struct wl_client *client, struct wl_resource *resource)
2908{
2909 wl_resource_destroy(resource);
2910}
2911
2912static const struct wl_keyboard_interface keyboard_interface = {
2913 keyboard_release
2914};
2915
2916static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002917seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2918 uint32_t id)
2919{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002920 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002921 /* We use the keyboard_state directly, which means we'll
2922 * give a wl_keyboard if the seat has ever had one - even though
2923 * the spec explicitly states that this request only takes effect
2924 * if the seat has the keyboard capability.
2925 *
2926 * This prevents a race between the compositor sending new
2927 * capabilities and the client trying to use the old ones.
2928 */
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002929 struct weston_keyboard *keyboard = seat ? seat->keyboard_state : NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002930 struct wl_resource *cr;
2931
leng.fange9d800e2024-07-12 13:31:59 +08002932 if (weston_keyboard_need_skip_get_keyboard(client, keyboard))
2933 return;
2934
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02002935 cr = wl_resource_create(client, &wl_keyboard_interface,
Jason Ekstranda85118c2013-06-27 20:17:02 -05002936 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002937 if (cr == NULL) {
2938 wl_client_post_no_memory(client);
2939 return;
2940 }
2941
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002942 wl_list_init(wl_resource_get_link(cr));
2943 wl_resource_set_implementation(cr, &keyboard_interface,
Alexandros Frantzis2b442482018-02-20 14:05:50 +02002944 keyboard, destroy_keyboard_resource);
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002945
2946 /* If we don't have a keyboard_state, the resource is inert, so there
2947 * is nothing more to set up */
2948 if (!keyboard)
2949 return;
2950
Neil Roberts96d790e2013-09-19 17:32:00 +01002951 /* May be moved to focused list later by either
2952 * weston_keyboard_set_focus or directly if this client is already
2953 * focused */
Derek Foreman345c9f32015-06-03 15:53:28 -05002954 wl_list_insert(&keyboard->resource_list, wl_resource_get_link(cr));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002955
Jonny Lamb66a41a02014-08-12 14:58:25 +02002956 if (wl_resource_get_version(cr) >= WL_KEYBOARD_REPEAT_INFO_SINCE_VERSION) {
2957 wl_keyboard_send_repeat_info(cr,
2958 seat->compositor->kb_repeat_rate,
2959 seat->compositor->kb_repeat_delay);
2960 }
Jasper St. Pierred8c6aeb2014-08-04 13:43:24 -04002961
Derek Foremanf8f7fd62017-06-28 11:41:43 -05002962 weston_keyboard_send_keymap(keyboard, cr);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002963
Derek Foreman345c9f32015-06-03 15:53:28 -05002964 if (keyboard->focus && keyboard->focus->resource &&
2965 wl_resource_get_client(keyboard->focus->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01002966 struct weston_surface *surface =
Derek Foreman345c9f32015-06-03 15:53:28 -05002967 (struct weston_surface *)keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +01002968
2969 wl_list_remove(wl_resource_get_link(cr));
Derek Foreman345c9f32015-06-03 15:53:28 -05002970 wl_list_insert(&keyboard->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002971 wl_resource_get_link(cr));
2972 wl_keyboard_send_enter(cr,
Derek Foreman345c9f32015-06-03 15:53:28 -05002973 keyboard->focus_serial,
Neil Roberts96d790e2013-09-19 17:32:00 +01002974 surface->resource,
Derek Foreman345c9f32015-06-03 15:53:28 -05002975 &keyboard->keys);
Neil Roberts96d790e2013-09-19 17:32:00 +01002976
Kirill Chibisovc46c70d2020-06-17 01:43:57 +03002977 send_modifiers_to_resource(keyboard,
2978 cr,
2979 keyboard->focus_serial);
2980
Neil Roberts96d790e2013-09-19 17:32:00 +01002981 /* If this is the first keyboard resource for this
2982 * client... */
Derek Foreman345c9f32015-06-03 15:53:28 -05002983 if (keyboard->focus_resource_list.prev ==
Neil Roberts96d790e2013-09-19 17:32:00 +01002984 wl_resource_get_link(cr))
2985 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002986 }
2987}
2988
2989static void
Alexandros Frantzisd7157842018-02-20 14:07:03 +02002990destroy_touch_resource(struct wl_resource *resource)
2991{
2992 struct weston_touch *touch = wl_resource_get_user_data(resource);
2993
2994 wl_list_remove(wl_resource_get_link(resource));
2995
2996 if (touch) {
2997 remove_input_resource_from_timestamps(resource,
2998 &touch->timestamps_list);
2999 }
3000}
3001
3002static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01003003touch_release(struct wl_client *client, struct wl_resource *resource)
3004{
3005 wl_resource_destroy(resource);
3006}
3007
3008static const struct wl_touch_interface touch_interface = {
3009 touch_release
3010};
3011
3012static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003013seat_get_touch(struct wl_client *client, struct wl_resource *resource,
3014 uint32_t id)
3015{
Jason Ekstrand44a38632013-06-14 10:08:00 -05003016 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05003017 /* We use the touch_state directly, which means we'll
3018 * give a wl_touch if the seat has ever had one - even though
3019 * the spec explicitly states that this request only takes effect
3020 * if the seat has the touch capability.
3021 *
3022 * This prevents a race between the compositor sending new
3023 * capabilities and the client trying to use the old ones.
3024 */
Alexandros Frantzis8480d132018-02-15 13:07:09 +02003025 struct weston_touch *touch = seat ? seat->touch_state : NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003026 struct wl_resource *cr;
3027
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02003028 cr = wl_resource_create(client, &wl_touch_interface,
Jason Ekstranda85118c2013-06-27 20:17:02 -05003029 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003030 if (cr == NULL) {
3031 wl_client_post_no_memory(client);
3032 return;
3033 }
3034
Alexandros Frantzis8480d132018-02-15 13:07:09 +02003035 wl_list_init(wl_resource_get_link(cr));
3036 wl_resource_set_implementation(cr, &touch_interface,
Alexandros Frantzisd7157842018-02-20 14:07:03 +02003037 touch, destroy_touch_resource);
Alexandros Frantzis8480d132018-02-15 13:07:09 +02003038
3039 /* If we don't have a touch_state, the resource is inert, so there
3040 * is nothing more to set up */
3041 if (!touch)
3042 return;
3043
Derek Foreman1281a362015-07-31 16:55:32 -05003044 if (touch->focus &&
3045 wl_resource_get_client(touch->focus->surface->resource) == client) {
Chokshi, Mituld6697142015-10-09 08:28:47 +00003046 wl_list_insert(&touch->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01003047 wl_resource_get_link(cr));
3048 } else {
Chokshi, Mituld6697142015-10-09 08:28:47 +00003049 wl_list_insert(&touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01003050 wl_resource_get_link(cr));
3051 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003052}
3053
Quentin Glidicaab1d362016-03-13 17:49:08 +01003054static void
3055seat_release(struct wl_client *client, struct wl_resource *resource)
3056{
3057 wl_resource_destroy(resource);
3058}
3059
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003060static const struct wl_seat_interface seat_interface = {
3061 seat_get_pointer,
3062 seat_get_keyboard,
3063 seat_get_touch,
Quentin Glidicaab1d362016-03-13 17:49:08 +01003064 seat_release,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003065};
3066
3067static void
3068bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3069{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003070 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003071 struct wl_resource *resource;
3072 enum wl_seat_capability caps = 0;
3073
Jason Ekstranda85118c2013-06-27 20:17:02 -05003074 resource = wl_resource_create(client,
Derek Foreman1909c102015-11-26 14:17:47 -06003075 &wl_seat_interface, version, id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05003076 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003077 wl_resource_set_implementation(resource, &seat_interface, data,
3078 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003079
Derek Foreman1281a362015-07-31 16:55:32 -05003080 if (weston_seat_get_pointer(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003081 caps |= WL_SEAT_CAPABILITY_POINTER;
Derek Foreman1281a362015-07-31 16:55:32 -05003082 if (weston_seat_get_keyboard(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003083 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Derek Foreman1281a362015-07-31 16:55:32 -05003084 if (weston_seat_get_touch(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003085 caps |= WL_SEAT_CAPABILITY_TOUCH;
3086
3087 wl_seat_send_capabilities(resource, caps);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003088 if (version >= WL_SEAT_NAME_SINCE_VERSION)
Rob Bradforde445ae62013-05-31 18:09:51 +01003089 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003090}
3091
Jonas Ådahl30d61d82014-10-22 21:21:17 +02003092static void
3093relative_pointer_destroy(struct wl_client *client,
3094 struct wl_resource *resource)
3095{
3096 wl_resource_destroy(resource);
3097}
3098
3099static const struct zwp_relative_pointer_v1_interface relative_pointer_interface = {
3100 relative_pointer_destroy
3101};
3102
3103static void
3104relative_pointer_manager_destroy(struct wl_client *client,
3105 struct wl_resource *resource)
3106{
3107 wl_resource_destroy(resource);
3108}
3109
3110static void
3111relative_pointer_manager_get_relative_pointer(struct wl_client *client,
3112 struct wl_resource *resource,
3113 uint32_t id,
3114 struct wl_resource *pointer_resource)
3115{
3116 struct weston_pointer *pointer =
3117 wl_resource_get_user_data(pointer_resource);
3118 struct weston_pointer_client *pointer_client;
3119 struct wl_resource *cr;
3120
3121 cr = wl_resource_create(client, &zwp_relative_pointer_v1_interface,
3122 wl_resource_get_version(resource), id);
3123 if (cr == NULL) {
3124 wl_client_post_no_memory(client);
3125 return;
3126 }
3127
3128 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
3129 if (!pointer_client) {
3130 wl_client_post_no_memory(client);
3131 return;
3132 }
3133
3134 wl_list_insert(&pointer_client->relative_pointer_resources,
3135 wl_resource_get_link(cr));
3136 wl_resource_set_implementation(cr, &relative_pointer_interface,
3137 pointer,
3138 unbind_pointer_client_resource);
3139}
3140
3141static const struct zwp_relative_pointer_manager_v1_interface relative_pointer_manager = {
3142 relative_pointer_manager_destroy,
3143 relative_pointer_manager_get_relative_pointer,
3144};
3145
3146static void
3147bind_relative_pointer_manager(struct wl_client *client, void *data,
3148 uint32_t version, uint32_t id)
3149{
3150 struct weston_compositor *compositor = data;
3151 struct wl_resource *resource;
3152
3153 resource = wl_resource_create(client,
3154 &zwp_relative_pointer_manager_v1_interface,
3155 1, id);
3156
3157 wl_resource_set_implementation(resource, &relative_pointer_manager,
3158 compositor,
3159 NULL);
3160}
3161
Giulio Camuffo0358af42016-06-02 21:48:08 +03003162WL_EXPORT int
3163weston_compositor_set_xkb_rule_names(struct weston_compositor *ec,
3164 struct xkb_rule_names *names)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003165{
3166 if (ec->xkb_context == NULL) {
Peter Hutterera2086bb2020-05-13 15:24:27 +10003167 ec->xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003168 if (ec->xkb_context == NULL) {
3169 weston_log("failed to create XKB context\n");
3170 return -1;
3171 }
3172 }
3173
3174 if (names)
3175 ec->xkb_names = *names;
3176 if (!ec->xkb_names.rules)
3177 ec->xkb_names.rules = strdup("evdev");
3178 if (!ec->xkb_names.model)
3179 ec->xkb_names.model = strdup("pc105");
3180 if (!ec->xkb_names.layout)
3181 ec->xkb_names.layout = strdup("us");
3182
3183 return 0;
3184}
3185
Stefan Schmidtfda26522013-09-17 10:54:09 +01003186static void
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003187weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003188{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003189 if (--xkb_info->ref_count > 0)
3190 return;
3191
Ran Benitac9c74152014-08-19 23:59:52 +03003192 xkb_keymap_unref(xkb_info->keymap);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003193
Sebastian Wickabec5122019-11-01 02:38:45 +01003194 os_ro_anonymous_file_destroy(xkb_info->keymap_rofile);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003195 free(xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003196}
3197
3198void
3199weston_compositor_xkb_destroy(struct weston_compositor *ec)
3200{
3201 free((char *) ec->xkb_names.rules);
3202 free((char *) ec->xkb_names.model);
3203 free((char *) ec->xkb_names.layout);
3204 free((char *) ec->xkb_names.variant);
3205 free((char *) ec->xkb_names.options);
Stefan Schmidtfda26522013-09-17 10:54:09 +01003206
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003207 if (ec->xkb_info)
3208 weston_xkb_info_destroy(ec->xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003209 xkb_context_unref(ec->xkb_context);
3210}
3211
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003212static struct weston_xkb_info *
3213weston_xkb_info_create(struct xkb_keymap *keymap)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003214{
Sebastian Wickabec5122019-11-01 02:38:45 +01003215 char *keymap_string;
3216 size_t keymap_size;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003217 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
3218 if (xkb_info == NULL)
3219 return NULL;
3220
Ran Benita2e1968f2014-08-19 23:59:51 +03003221 xkb_info->keymap = xkb_keymap_ref(keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003222 xkb_info->ref_count = 1;
3223
Ran Benita2e1968f2014-08-19 23:59:51 +03003224 xkb_info->shift_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3225 XKB_MOD_NAME_SHIFT);
3226 xkb_info->caps_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3227 XKB_MOD_NAME_CAPS);
3228 xkb_info->ctrl_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3229 XKB_MOD_NAME_CTRL);
3230 xkb_info->alt_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3231 XKB_MOD_NAME_ALT);
3232 xkb_info->mod2_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3233 "Mod2");
3234 xkb_info->mod3_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3235 "Mod3");
3236 xkb_info->super_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3237 XKB_MOD_NAME_LOGO);
3238 xkb_info->mod5_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3239 "Mod5");
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003240
Ran Benita2e1968f2014-08-19 23:59:51 +03003241 xkb_info->num_led = xkb_keymap_led_get_index(xkb_info->keymap,
3242 XKB_LED_NAME_NUM);
3243 xkb_info->caps_led = xkb_keymap_led_get_index(xkb_info->keymap,
3244 XKB_LED_NAME_CAPS);
3245 xkb_info->scroll_led = xkb_keymap_led_get_index(xkb_info->keymap,
3246 XKB_LED_NAME_SCROLL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003247
Sebastian Wickabec5122019-11-01 02:38:45 +01003248 keymap_string = xkb_keymap_get_as_string(xkb_info->keymap,
Derek Foreman76829fc2017-06-28 12:17:46 -05003249 XKB_KEYMAP_FORMAT_TEXT_V1);
Sebastian Wickabec5122019-11-01 02:38:45 +01003250 if (keymap_string == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003251 weston_log("failed to get string version of keymap\n");
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003252 goto err_keymap;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003253 }
Sebastian Wickabec5122019-11-01 02:38:45 +01003254 keymap_size = strlen(keymap_string) + 1;
3255
3256 xkb_info->keymap_rofile = os_ro_anonymous_file_create(keymap_size,
3257 keymap_string);
3258 free(keymap_string);
3259
3260 if (!xkb_info->keymap_rofile) {
3261 weston_log("failed to create anonymous file for keymap\n");
3262 goto err_keymap;
3263 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003264
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003265 return xkb_info;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003266
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003267err_keymap:
Ran Benita2e1968f2014-08-19 23:59:51 +03003268 xkb_keymap_unref(xkb_info->keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003269 free(xkb_info);
3270 return NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003271}
3272
3273static int
3274weston_compositor_build_global_keymap(struct weston_compositor *ec)
3275{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003276 struct xkb_keymap *keymap;
3277
3278 if (ec->xkb_info != NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003279 return 0;
3280
Ran Benita2e1968f2014-08-19 23:59:51 +03003281 keymap = xkb_keymap_new_from_names(ec->xkb_context,
3282 &ec->xkb_names,
3283 0);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003284 if (keymap == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003285 weston_log("failed to compile global XKB keymap\n");
3286 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
3287 "options %s\n",
3288 ec->xkb_names.rules, ec->xkb_names.model,
3289 ec->xkb_names.layout, ec->xkb_names.variant,
3290 ec->xkb_names.options);
3291 return -1;
3292 }
3293
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003294 ec->xkb_info = weston_xkb_info_create(keymap);
Rui Matos73d93952013-10-24 19:28:41 +02003295 xkb_keymap_unref(keymap);
Stefan Schmidtfda26522013-09-17 10:54:09 +01003296 if (ec->xkb_info == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003297 return -1;
3298
3299 return 0;
3300}
3301
Rui Matos65196bc2013-10-10 19:44:19 +02003302WL_EXPORT void
3303weston_seat_update_keymap(struct weston_seat *seat, struct xkb_keymap *keymap)
3304{
Derek Foreman1281a362015-07-31 16:55:32 -05003305 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
3306
3307 if (!keyboard || !keymap)
Rui Matos65196bc2013-10-10 19:44:19 +02003308 return;
3309
Derek Foreman1281a362015-07-31 16:55:32 -05003310 xkb_keymap_unref(keyboard->pending_keymap);
3311 keyboard->pending_keymap = xkb_keymap_ref(keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02003312
Derek Foreman1281a362015-07-31 16:55:32 -05003313 if (keyboard->keys.size == 0)
Rui Matos65196bc2013-10-10 19:44:19 +02003314 update_keymap(seat);
Rui Matos65196bc2013-10-10 19:44:19 +02003315}
3316
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003317WL_EXPORT int
3318weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
3319{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003320 struct weston_keyboard *keyboard;
3321
Derek Foreman1281a362015-07-31 16:55:32 -05003322 if (seat->keyboard_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003323 seat->keyboard_device_count += 1;
3324 if (seat->keyboard_device_count == 1)
3325 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003326 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003327 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003328
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003329 keyboard = weston_keyboard_create();
3330 if (keyboard == NULL) {
3331 weston_log("failed to allocate weston keyboard struct\n");
3332 return -1;
3333 }
3334
Derek Foreman185d1582017-06-28 11:17:23 -05003335 if (keymap != NULL) {
3336 keyboard->xkb_info = weston_xkb_info_create(keymap);
3337 if (keyboard->xkb_info == NULL)
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02003338 goto err;
Derek Foreman185d1582017-06-28 11:17:23 -05003339 } else {
3340 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
3341 goto err;
3342 keyboard->xkb_info = seat->compositor->xkb_info;
3343 keyboard->xkb_info->ref_count++;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01003344 }
Derek Foreman185d1582017-06-28 11:17:23 -05003345
3346 keyboard->xkb_state.state = xkb_state_new(keyboard->xkb_info->keymap);
3347 if (keyboard->xkb_state.state == NULL) {
3348 weston_log("failed to initialise XKB state\n");
3349 goto err;
3350 }
3351
3352 keyboard->xkb_state.leds = 0;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01003353
Derek Foreman1281a362015-07-31 16:55:32 -05003354 seat->keyboard_state = keyboard;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02003355 seat->keyboard_device_count = 1;
3356 keyboard->seat = seat;
3357
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003358 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003359
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003360 return 0;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02003361
3362err:
3363 if (keyboard->xkb_info)
3364 weston_xkb_info_destroy(keyboard->xkb_info);
3365 free(keyboard);
3366
3367 return -1;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003368}
3369
Jonas Ådahl91fed542013-12-03 09:14:27 +01003370static void
3371weston_keyboard_reset_state(struct weston_keyboard *keyboard)
3372{
3373 struct weston_seat *seat = keyboard->seat;
3374 struct xkb_state *state;
3375
Derek Foreman185d1582017-06-28 11:17:23 -05003376 state = xkb_state_new(keyboard->xkb_info->keymap);
3377 if (!state) {
3378 weston_log("failed to reset XKB state\n");
3379 return;
Jonas Ådahl91fed542013-12-03 09:14:27 +01003380 }
Derek Foreman185d1582017-06-28 11:17:23 -05003381 xkb_state_unref(keyboard->xkb_state.state);
3382 keyboard->xkb_state.state = state;
3383
3384 keyboard->xkb_state.leds = 0;
Jonas Ådahl91fed542013-12-03 09:14:27 +01003385
3386 seat->modifier_state = 0;
3387}
3388
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003389WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003390weston_seat_release_keyboard(struct weston_seat *seat)
3391{
3392 seat->keyboard_device_count--;
Derek Foremand621df22014-11-19 11:04:12 -06003393 assert(seat->keyboard_device_count >= 0);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003394 if (seat->keyboard_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05003395 weston_keyboard_set_focus(seat->keyboard_state, NULL);
3396 weston_keyboard_cancel_grab(seat->keyboard_state);
3397 weston_keyboard_reset_state(seat->keyboard_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003398 seat_send_updated_caps(seat);
3399 }
3400}
3401
Marius Vladeb34f822021-03-30 23:09:05 +03003402WL_EXPORT int
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003403weston_seat_init_pointer(struct weston_seat *seat)
3404{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003405 struct weston_pointer *pointer;
3406
Derek Foreman1281a362015-07-31 16:55:32 -05003407 if (seat->pointer_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003408 seat->pointer_device_count += 1;
3409 if (seat->pointer_device_count == 1)
3410 seat_send_updated_caps(seat);
Marius Vladeb34f822021-03-30 23:09:05 +03003411 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003412 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003413
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003414 pointer = weston_pointer_create(seat);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003415 if (pointer == NULL)
Marius Vladeb34f822021-03-30 23:09:05 +03003416 return -1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003417
Derek Foreman1281a362015-07-31 16:55:32 -05003418 seat->pointer_state = pointer;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003419 seat->pointer_device_count = 1;
3420 pointer->seat = seat;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003421
3422 seat_send_updated_caps(seat);
Marius Vladeb34f822021-03-30 23:09:05 +03003423
3424 return 0;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003425}
3426
3427WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003428weston_seat_release_pointer(struct weston_seat *seat)
3429{
Derek Foreman1281a362015-07-31 16:55:32 -05003430 struct weston_pointer *pointer = seat->pointer_state;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003431
3432 seat->pointer_device_count--;
3433 if (seat->pointer_device_count == 0) {
Derek Foremanf9318d12015-05-11 15:40:11 -05003434 weston_pointer_clear_focus(pointer);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003435 weston_pointer_cancel_grab(pointer);
Jonas Ådahl630bae82013-10-17 23:04:06 +02003436
Jonas Ådahla4932742013-10-17 23:04:07 +02003437 if (pointer->sprite)
3438 pointer_unmap_sprite(pointer);
3439
Jonas Ådahl3e12e632013-12-02 22:05:05 +01003440 weston_pointer_reset_state(pointer);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003441 seat_send_updated_caps(seat);
Derek Foremanfd5ca512015-01-07 15:00:25 -06003442
3443 /* seat->pointer is intentionally not destroyed so that
3444 * a newly attached pointer on this seat will retain
3445 * the previous cursor co-ordinates.
3446 */
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003447 }
3448}
3449
Marius Vladeb34f822021-03-30 23:09:05 +03003450WL_EXPORT int
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003451weston_seat_init_touch(struct weston_seat *seat)
3452{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003453 struct weston_touch *touch;
3454
Derek Foreman1281a362015-07-31 16:55:32 -05003455 if (seat->touch_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003456 seat->touch_device_count += 1;
3457 if (seat->touch_device_count == 1)
3458 seat_send_updated_caps(seat);
Marius Vladeb34f822021-03-30 23:09:05 +03003459 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003460 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003461
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003462 touch = weston_touch_create();
3463 if (touch == NULL)
Marius Vladeb34f822021-03-30 23:09:05 +03003464 return -1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003465
Derek Foreman1281a362015-07-31 16:55:32 -05003466 seat->touch_state = touch;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003467 seat->touch_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003468 touch->seat = seat;
3469
3470 seat_send_updated_caps(seat);
Marius Vladeb34f822021-03-30 23:09:05 +03003471
3472 return 0;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003473}
3474
3475WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003476weston_seat_release_touch(struct weston_seat *seat)
3477{
3478 seat->touch_device_count--;
3479 if (seat->touch_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05003480 weston_touch_set_focus(seat->touch_state, NULL);
3481 weston_touch_cancel_grab(seat->touch_state);
3482 weston_touch_reset_state(seat->touch_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003483 seat_send_updated_caps(seat);
3484 }
3485}
3486
3487WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003488weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
3489 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003490{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003491 memset(seat, 0, sizeof *seat);
3492
Kristian Høgsberge3148752013-05-06 23:19:49 -04003493 seat->selection_data_source = NULL;
3494 wl_list_init(&seat->base_resource_list);
3495 wl_signal_init(&seat->selection_signal);
3496 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04003497 wl_signal_init(&seat->destroy_signal);
Jason Ekstranda4ab5422014-04-02 19:53:45 -05003498 wl_signal_init(&seat->updated_caps_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003499
Sebastian Wickabec5122019-11-01 02:38:45 +01003500 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface,
3501 MIN(wl_seat_interface.version, 7),
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003502 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003503
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003504 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003505 seat->modifier_state = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003506 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003507
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003508 wl_list_insert(ec->seat_list.prev, &seat->link);
3509
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003510 clipboard_create(seat);
3511
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003512 wl_signal_emit(&ec->seat_created_signal, seat);
3513}
3514
3515WL_EXPORT void
3516weston_seat_release(struct weston_seat *seat)
3517{
Alexandros Frantzis8480d132018-02-15 13:07:09 +02003518 struct wl_resource *resource;
3519
3520 wl_resource_for_each(resource, &seat->base_resource_list) {
3521 wl_resource_set_user_data(resource, NULL);
3522 }
3523
3524 wl_resource_for_each(resource, &seat->drag_resource_list) {
3525 wl_resource_set_user_data(resource, NULL);
3526 }
3527
3528 wl_list_remove(&seat->base_resource_list);
3529 wl_list_remove(&seat->drag_resource_list);
3530
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003531 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003532
Jonas Ådahl1afb2382014-01-03 19:46:51 +01003533 if (seat->saved_kbd_focus)
3534 wl_list_remove(&seat->saved_kbd_focus_listener.link);
3535
Derek Foreman1281a362015-07-31 16:55:32 -05003536 if (seat->pointer_state)
3537 weston_pointer_destroy(seat->pointer_state);
3538 if (seat->keyboard_state)
3539 weston_keyboard_destroy(seat->keyboard_state);
3540 if (seat->touch_state)
3541 weston_touch_destroy(seat->touch_state);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003542
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003543 free (seat->seat_name);
3544
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003545 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04003546
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003547 wl_signal_emit(&seat->destroy_signal, seat);
3548}
Derek Foreman1281a362015-07-31 16:55:32 -05003549
3550/** Get a seat's keyboard pointer
3551 *
3552 * \param seat The seat to query
3553 * \return The seat's keyboard pointer, or NULL if no keyboard is present
3554 *
3555 * The keyboard pointer for a seat isn't freed when all keyboards are removed,
3556 * so it should only be used when the seat's keyboard_device_count is greater
3557 * than zero. This function does that test and only returns a pointer
3558 * when a keyboard is present.
3559 */
3560WL_EXPORT struct weston_keyboard *
3561weston_seat_get_keyboard(struct weston_seat *seat)
3562{
3563 if (!seat)
3564 return NULL;
3565
3566 if (seat->keyboard_device_count)
3567 return seat->keyboard_state;
3568
3569 return NULL;
3570}
3571
3572/** Get a seat's pointer pointer
3573 *
3574 * \param seat The seat to query
3575 * \return The seat's pointer pointer, or NULL if no pointer device is present
3576 *
3577 * The pointer pointer for a seat isn't freed when all mice are removed,
3578 * so it should only be used when the seat's pointer_device_count is greater
3579 * than zero. This function does that test and only returns a pointer
3580 * when a pointing device is present.
3581 */
3582WL_EXPORT struct weston_pointer *
3583weston_seat_get_pointer(struct weston_seat *seat)
3584{
3585 if (!seat)
3586 return NULL;
3587
3588 if (seat->pointer_device_count)
3589 return seat->pointer_state;
3590
3591 return NULL;
3592}
3593
Jonas Ådahld3414f22016-07-22 17:56:31 +08003594static const struct zwp_locked_pointer_v1_interface locked_pointer_interface;
3595static const struct zwp_confined_pointer_v1_interface confined_pointer_interface;
3596
3597static enum pointer_constraint_type
3598pointer_constraint_get_type(struct weston_pointer_constraint *constraint)
3599{
3600 if (wl_resource_instance_of(constraint->resource,
3601 &zwp_locked_pointer_v1_interface,
3602 &locked_pointer_interface)) {
3603 return POINTER_CONSTRAINT_TYPE_LOCK;
3604 } else if (wl_resource_instance_of(constraint->resource,
3605 &zwp_confined_pointer_v1_interface,
3606 &confined_pointer_interface)) {
3607 return POINTER_CONSTRAINT_TYPE_CONFINE;
3608 }
3609
3610 abort();
3611 return 0;
3612}
3613
3614static void
3615pointer_constraint_notify_activated(struct weston_pointer_constraint *constraint)
3616{
3617 struct wl_resource *resource = constraint->resource;
3618
3619 switch (pointer_constraint_get_type(constraint)) {
3620 case POINTER_CONSTRAINT_TYPE_LOCK:
3621 zwp_locked_pointer_v1_send_locked(resource);
3622 break;
3623 case POINTER_CONSTRAINT_TYPE_CONFINE:
3624 zwp_confined_pointer_v1_send_confined(resource);
3625 break;
3626 }
3627}
3628
3629static void
3630pointer_constraint_notify_deactivated(struct weston_pointer_constraint *constraint)
3631{
3632 struct wl_resource *resource = constraint->resource;
3633
3634 switch (pointer_constraint_get_type(constraint)) {
3635 case POINTER_CONSTRAINT_TYPE_LOCK:
3636 zwp_locked_pointer_v1_send_unlocked(resource);
3637 break;
3638 case POINTER_CONSTRAINT_TYPE_CONFINE:
3639 zwp_confined_pointer_v1_send_unconfined(resource);
3640 break;
3641 }
3642}
3643
3644static struct weston_pointer_constraint *
3645get_pointer_constraint_for_pointer(struct weston_surface *surface,
3646 struct weston_pointer *pointer)
3647{
3648 struct weston_pointer_constraint *constraint;
3649
3650 wl_list_for_each(constraint, &surface->pointer_constraints, link) {
3651 if (constraint->pointer == pointer)
3652 return constraint;
3653 }
3654
3655 return NULL;
3656}
3657
Derek Foreman1281a362015-07-31 16:55:32 -05003658/** Get a seat's touch pointer
3659 *
3660 * \param seat The seat to query
3661 * \return The seat's touch pointer, or NULL if no touch device is present
3662 *
3663 * The touch pointer for a seat isn't freed when all touch devices are removed,
3664 * so it should only be used when the seat's touch_device_count is greater
3665 * than zero. This function does that test and only returns a pointer
3666 * when a touch device is present.
3667 */
3668WL_EXPORT struct weston_touch *
3669weston_seat_get_touch(struct weston_seat *seat)
3670{
3671 if (!seat)
3672 return NULL;
3673
3674 if (seat->touch_device_count)
3675 return seat->touch_state;
3676
3677 return NULL;
3678}
Bryce Harrington24f917e2016-06-29 19:04:07 -07003679
3680/** Sets the keyboard focus to the given surface
3681 *
Marius Vlada2dace22019-06-12 16:05:44 +03003682 * \param surface the surface to focus on
Bryce Harrington24f917e2016-06-29 19:04:07 -07003683 * \param seat The seat to query
3684 */
3685WL_EXPORT void
3686weston_seat_set_keyboard_focus(struct weston_seat *seat,
3687 struct weston_surface *surface)
3688{
3689 struct weston_compositor *compositor = seat->compositor;
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003690 struct weston_surface_activation_data activation_data;
Bryce Harrington24f917e2016-06-29 19:04:07 -07003691
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003692 inc_activate_serial(compositor);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003693
3694 activation_data = (struct weston_surface_activation_data) {
3695 .surface = surface,
3696 .seat = seat,
3697 };
3698 wl_signal_emit(&compositor->activate_signal, &activation_data);
Bryce Harrington24f917e2016-06-29 19:04:07 -07003699}
Jonas Ådahl30d61d82014-10-22 21:21:17 +02003700
Jonas Ådahld3414f22016-07-22 17:56:31 +08003701static void
3702enable_pointer_constraint(struct weston_pointer_constraint *constraint,
3703 struct weston_view *view)
3704{
3705 assert(constraint->view == NULL);
3706 constraint->view = view;
3707 pointer_constraint_notify_activated(constraint);
3708 weston_pointer_start_grab(constraint->pointer, &constraint->grab);
3709 wl_list_remove(&constraint->surface_destroy_listener.link);
3710 wl_list_init(&constraint->surface_destroy_listener.link);
3711}
3712
3713static bool
3714is_pointer_constraint_enabled(struct weston_pointer_constraint *constraint)
3715{
3716 return constraint->view != NULL;
3717}
3718
3719static void
3720weston_pointer_constraint_disable(struct weston_pointer_constraint *constraint)
3721{
3722 constraint->view = NULL;
3723 pointer_constraint_notify_deactivated(constraint);
3724 weston_pointer_end_grab(constraint->grab.pointer);
3725}
3726
3727void
3728weston_pointer_constraint_destroy(struct weston_pointer_constraint *constraint)
3729{
3730 if (is_pointer_constraint_enabled(constraint))
3731 weston_pointer_constraint_disable(constraint);
3732
3733 wl_list_remove(&constraint->pointer_destroy_listener.link);
3734 wl_list_remove(&constraint->surface_destroy_listener.link);
3735 wl_list_remove(&constraint->surface_commit_listener.link);
3736 wl_list_remove(&constraint->surface_activate_listener.link);
3737
3738 wl_resource_set_user_data(constraint->resource, NULL);
3739 pixman_region32_fini(&constraint->region);
3740 wl_list_remove(&constraint->link);
3741 free(constraint);
3742}
3743
3744static void
3745disable_pointer_constraint(struct weston_pointer_constraint *constraint)
3746{
3747 switch (constraint->lifetime) {
3748 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT:
3749 weston_pointer_constraint_destroy(constraint);
3750 break;
3751 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT:
3752 weston_pointer_constraint_disable(constraint);
3753 break;
3754 }
3755}
3756
3757static bool
3758is_within_constraint_region(struct weston_pointer_constraint *constraint,
3759 wl_fixed_t sx, wl_fixed_t sy)
3760{
3761 struct weston_surface *surface = constraint->surface;
3762 pixman_region32_t constraint_region;
3763 bool result;
3764
3765 pixman_region32_init(&constraint_region);
3766 pixman_region32_intersect(&constraint_region,
3767 &surface->input,
3768 &constraint->region);
3769 result = pixman_region32_contains_point(&constraint_region,
3770 wl_fixed_to_int(sx),
3771 wl_fixed_to_int(sy),
3772 NULL);
3773 pixman_region32_fini(&constraint_region);
3774
3775 return result;
3776}
3777
3778static void
3779maybe_enable_pointer_constraint(struct weston_pointer_constraint *constraint)
3780{
3781 struct weston_surface *surface = constraint->surface;
3782 struct weston_view *vit;
3783 struct weston_view *view = NULL;
3784 struct weston_pointer *pointer = constraint->pointer;
3785 struct weston_keyboard *keyboard;
3786 struct weston_seat *seat = pointer->seat;
3787 int32_t x, y;
3788
3789 /* Postpone if no view of the surface was most recently clicked. */
3790 wl_list_for_each(vit, &surface->views, surface_link) {
3791 if (vit->click_to_activate_serial ==
3792 surface->compositor->activate_serial) {
3793 view = vit;
3794 }
3795 }
3796 if (view == NULL)
3797 return;
3798
3799 /* Postpone if surface doesn't have keyboard focus. */
3800 keyboard = weston_seat_get_keyboard(seat);
3801 if (!keyboard || keyboard->focus != surface)
3802 return;
3803
3804 /* Postpone constraint if the pointer is not within the
3805 * constraint region.
3806 */
3807 weston_view_from_global(view,
3808 wl_fixed_to_int(pointer->x),
3809 wl_fixed_to_int(pointer->y),
3810 &x, &y);
3811 if (!is_within_constraint_region(constraint,
3812 wl_fixed_from_int(x),
3813 wl_fixed_from_int(y)))
3814 return;
3815
3816 enable_pointer_constraint(constraint, view);
3817}
3818
3819static void
3820locked_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
3821{
3822}
3823
3824static void
3825locked_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02003826 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003827 struct weston_pointer_motion_event *event)
3828{
Quentin Glidiccde13452016-08-12 10:41:32 +02003829 pointer_send_relative_motion(grab->pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08003830}
3831
3832static void
3833locked_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003834 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003835 uint32_t button,
3836 uint32_t state_w)
3837{
3838 weston_pointer_send_button(grab->pointer, time, button, state_w);
3839}
3840
3841static void
3842locked_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02003843 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003844 struct weston_pointer_axis_event *event)
3845{
3846 weston_pointer_send_axis(grab->pointer, time, event);
3847}
3848
3849static void
3850locked_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
3851 uint32_t source)
3852{
3853 weston_pointer_send_axis_source(grab->pointer, source);
3854}
3855
3856static void
3857locked_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
3858{
3859 weston_pointer_send_frame(grab->pointer);
3860}
3861
3862static void
3863locked_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
3864{
3865 struct weston_pointer_constraint *constraint =
3866 container_of(grab, struct weston_pointer_constraint, grab);
3867
3868 disable_pointer_constraint(constraint);
3869}
3870
3871static const struct weston_pointer_grab_interface
3872 locked_pointer_grab_interface = {
3873 locked_pointer_grab_pointer_focus,
3874 locked_pointer_grab_pointer_motion,
3875 locked_pointer_grab_pointer_button,
3876 locked_pointer_grab_pointer_axis,
3877 locked_pointer_grab_pointer_axis_source,
3878 locked_pointer_grab_pointer_frame,
3879 locked_pointer_grab_pointer_cancel,
3880};
3881
3882static void
3883pointer_constraint_constrain_resource_destroyed(struct wl_resource *resource)
3884{
3885 struct weston_pointer_constraint *constraint =
3886 wl_resource_get_user_data(resource);
3887
3888 if (!constraint)
3889 return;
3890
3891 weston_pointer_constraint_destroy(constraint);
3892}
3893
3894static void
3895pointer_constraint_surface_activate(struct wl_listener *listener, void *data)
3896{
3897 struct weston_surface_activation_data *activation = data;
3898 struct weston_pointer *pointer;
3899 struct weston_surface *focus = activation->surface;
3900 struct weston_pointer_constraint *constraint =
3901 container_of(listener, struct weston_pointer_constraint,
3902 surface_activate_listener);
3903 bool is_constraint_surface;
3904
3905 pointer = weston_seat_get_pointer(activation->seat);
3906 if (!pointer)
3907 return;
3908
3909 is_constraint_surface =
3910 get_pointer_constraint_for_pointer(focus, pointer) == constraint;
3911
3912 if (is_constraint_surface &&
3913 !is_pointer_constraint_enabled(constraint))
3914 maybe_enable_pointer_constraint(constraint);
3915 else if (!is_constraint_surface &&
3916 is_pointer_constraint_enabled(constraint))
3917 disable_pointer_constraint(constraint);
3918}
3919
3920static void
3921pointer_constraint_pointer_destroyed(struct wl_listener *listener, void *data)
3922{
3923 struct weston_pointer_constraint *constraint =
3924 container_of(listener, struct weston_pointer_constraint,
3925 pointer_destroy_listener);
3926
3927 weston_pointer_constraint_destroy(constraint);
3928}
3929
3930static void
3931pointer_constraint_surface_destroyed(struct wl_listener *listener, void *data)
3932{
3933 struct weston_pointer_constraint *constraint =
3934 container_of(listener, struct weston_pointer_constraint,
3935 surface_destroy_listener);
3936
3937 weston_pointer_constraint_destroy(constraint);
3938}
3939
3940static void
3941pointer_constraint_surface_committed(struct wl_listener *listener, void *data)
3942{
3943 struct weston_pointer_constraint *constraint =
3944 container_of(listener, struct weston_pointer_constraint,
3945 surface_commit_listener);
3946
3947 if (constraint->region_is_pending) {
3948 constraint->region_is_pending = false;
3949 pixman_region32_copy(&constraint->region,
3950 &constraint->region_pending);
3951 pixman_region32_fini(&constraint->region_pending);
3952 pixman_region32_init(&constraint->region_pending);
3953 }
3954
3955 if (constraint->hint_is_pending) {
3956 constraint->hint_is_pending = false;
3957
3958 constraint->hint_is_pending = true;
3959 constraint->hint_x = constraint->hint_x_pending;
3960 constraint->hint_y = constraint->hint_y_pending;
3961 }
3962
3963 if (pointer_constraint_get_type(constraint) ==
3964 POINTER_CONSTRAINT_TYPE_CONFINE &&
3965 is_pointer_constraint_enabled(constraint))
3966 maybe_warp_confined_pointer(constraint);
3967}
3968
3969static struct weston_pointer_constraint *
3970weston_pointer_constraint_create(struct weston_surface *surface,
3971 struct weston_pointer *pointer,
3972 struct weston_region *region,
3973 enum zwp_pointer_constraints_v1_lifetime lifetime,
3974 struct wl_resource *cr,
3975 const struct weston_pointer_grab_interface *grab_interface)
3976{
3977 struct weston_pointer_constraint *constraint;
3978
3979 constraint = zalloc(sizeof *constraint);
3980 if (!constraint)
3981 return NULL;
3982
3983 constraint->lifetime = lifetime;
3984 pixman_region32_init(&constraint->region);
3985 pixman_region32_init(&constraint->region_pending);
3986 wl_list_insert(&surface->pointer_constraints, &constraint->link);
3987 constraint->surface = surface;
3988 constraint->pointer = pointer;
3989 constraint->resource = cr;
3990 constraint->grab.interface = grab_interface;
3991 if (region) {
3992 pixman_region32_copy(&constraint->region,
3993 &region->region);
3994 } else {
3995 pixman_region32_fini(&constraint->region);
3996 region_init_infinite(&constraint->region);
3997 }
3998
3999 constraint->surface_activate_listener.notify =
4000 pointer_constraint_surface_activate;
4001 constraint->surface_destroy_listener.notify =
4002 pointer_constraint_surface_destroyed;
4003 constraint->surface_commit_listener.notify =
4004 pointer_constraint_surface_committed;
4005 constraint->pointer_destroy_listener.notify =
4006 pointer_constraint_pointer_destroyed;
4007
4008 wl_signal_add(&surface->compositor->activate_signal,
4009 &constraint->surface_activate_listener);
4010 wl_signal_add(&pointer->destroy_signal,
4011 &constraint->pointer_destroy_listener);
4012 wl_signal_add(&surface->destroy_signal,
4013 &constraint->surface_destroy_listener);
4014 wl_signal_add(&surface->commit_signal,
4015 &constraint->surface_commit_listener);
4016
4017 return constraint;
4018}
4019
4020static void
4021init_pointer_constraint(struct wl_resource *pointer_constraints_resource,
4022 uint32_t id,
4023 struct weston_surface *surface,
4024 struct weston_pointer *pointer,
4025 struct weston_region *region,
4026 enum zwp_pointer_constraints_v1_lifetime lifetime,
4027 const struct wl_interface *interface,
4028 const void *implementation,
4029 const struct weston_pointer_grab_interface *grab_interface)
4030{
4031 struct wl_client *client =
4032 wl_resource_get_client(pointer_constraints_resource);
4033 struct wl_resource *cr;
4034 struct weston_pointer_constraint *constraint;
4035
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02004036 if (pointer && get_pointer_constraint_for_pointer(surface, pointer)) {
Jonas Ådahld3414f22016-07-22 17:56:31 +08004037 wl_resource_post_error(pointer_constraints_resource,
4038 ZWP_POINTER_CONSTRAINTS_V1_ERROR_ALREADY_CONSTRAINED,
4039 "the pointer has a lock/confine request on this surface");
4040 return;
4041 }
4042
4043 cr = wl_resource_create(client, interface,
4044 wl_resource_get_version(pointer_constraints_resource),
4045 id);
4046 if (cr == NULL) {
4047 wl_client_post_no_memory(client);
4048 return;
4049 }
4050
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02004051 if (pointer) {
4052 constraint = weston_pointer_constraint_create(surface, pointer,
4053 region, lifetime,
4054 cr, grab_interface);
4055 if (constraint == NULL) {
4056 wl_client_post_no_memory(client);
4057 return;
4058 }
4059 } else {
4060 constraint = NULL;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004061 }
4062
4063 wl_resource_set_implementation(cr, implementation, constraint,
4064 pointer_constraint_constrain_resource_destroyed);
4065
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02004066 if (constraint)
4067 maybe_enable_pointer_constraint(constraint);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004068}
4069
4070static void
4071pointer_constraints_destroy(struct wl_client *client,
4072 struct wl_resource *resource)
4073{
4074 wl_resource_destroy(resource);
4075}
4076
4077static void
4078locked_pointer_destroy(struct wl_client *client,
4079 struct wl_resource *resource)
4080{
4081 struct weston_pointer_constraint *constraint =
4082 wl_resource_get_user_data(resource);
4083 wl_fixed_t x, y;
4084
4085 if (constraint && constraint->view && constraint->hint_is_pending &&
4086 is_within_constraint_region(constraint,
4087 constraint->hint_x,
4088 constraint->hint_y)) {
4089 weston_view_to_global_fixed(constraint->view,
4090 constraint->hint_x,
4091 constraint->hint_y,
4092 &x, &y);
4093 weston_pointer_move_to(constraint->pointer, x, y);
4094 }
4095 wl_resource_destroy(resource);
4096}
4097
4098static void
4099locked_pointer_set_cursor_position_hint(struct wl_client *client,
4100 struct wl_resource *resource,
4101 wl_fixed_t surface_x,
4102 wl_fixed_t surface_y)
4103{
4104 struct weston_pointer_constraint *constraint =
4105 wl_resource_get_user_data(resource);
4106
4107 /* Ignore a set cursor hint that was sent after the lock was cancelled.
4108 */
4109 if (!constraint ||
4110 !constraint->resource ||
4111 constraint->resource != resource)
4112 return;
4113
4114 constraint->hint_is_pending = true;
4115 constraint->hint_x_pending = surface_x;
4116 constraint->hint_y_pending = surface_y;
4117}
4118
4119static void
4120locked_pointer_set_region(struct wl_client *client,
4121 struct wl_resource *resource,
4122 struct wl_resource *region_resource)
4123{
4124 struct weston_pointer_constraint *constraint =
4125 wl_resource_get_user_data(resource);
4126 struct weston_region *region = region_resource ?
4127 wl_resource_get_user_data(region_resource) : NULL;
4128
4129 if (!constraint)
4130 return;
4131
4132 if (region) {
4133 pixman_region32_copy(&constraint->region_pending,
4134 &region->region);
4135 } else {
4136 pixman_region32_fini(&constraint->region_pending);
4137 region_init_infinite(&constraint->region_pending);
4138 }
4139 constraint->region_is_pending = true;
4140}
4141
4142
4143static const struct zwp_locked_pointer_v1_interface locked_pointer_interface = {
4144 locked_pointer_destroy,
4145 locked_pointer_set_cursor_position_hint,
4146 locked_pointer_set_region,
4147};
4148
4149static void
4150pointer_constraints_lock_pointer(struct wl_client *client,
4151 struct wl_resource *resource,
4152 uint32_t id,
4153 struct wl_resource *surface_resource,
4154 struct wl_resource *pointer_resource,
4155 struct wl_resource *region_resource,
4156 uint32_t lifetime)
4157{
4158 struct weston_surface *surface =
4159 wl_resource_get_user_data(surface_resource);
4160 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
4161 struct weston_region *region = region_resource ?
4162 wl_resource_get_user_data(region_resource) : NULL;
4163
4164 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
4165 &zwp_locked_pointer_v1_interface,
4166 &locked_pointer_interface,
4167 &locked_pointer_grab_interface);
4168}
4169
4170static void
4171confined_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
4172{
4173}
4174
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004175static double
4176vec2d_cross_product(struct vec2d a, struct vec2d b)
4177{
4178 return a.x * b.y - a.y * b.x;
4179}
4180
4181static struct vec2d
4182vec2d_add(struct vec2d a, struct vec2d b)
4183{
4184 return (struct vec2d) {
4185 .x = a.x + b.x,
4186 .y = a.y + b.y,
4187 };
4188}
4189
4190static struct vec2d
4191vec2d_subtract(struct vec2d a, struct vec2d b)
4192{
4193 return (struct vec2d) {
4194 .x = a.x - b.x,
4195 .y = a.y - b.y,
4196 };
4197}
4198
4199static struct vec2d
4200vec2d_multiply_constant(double c, struct vec2d a)
4201{
4202 return (struct vec2d) {
4203 .x = c * a.x,
4204 .y = c * a.y,
4205 };
4206}
4207
4208static bool
4209lines_intersect(struct line *line1, struct line *line2,
4210 struct vec2d *intersection)
4211{
4212 struct vec2d p = line1->a;
4213 struct vec2d r = vec2d_subtract(line1->b, line1->a);
4214 struct vec2d q = line2->a;
4215 struct vec2d s = vec2d_subtract(line2->b, line2->a);
4216 double rxs;
4217 double sxr;
4218 double t;
4219 double u;
4220
4221 /*
4222 * The line (p, r) and (q, s) intersects where
4223 *
4224 * p + t r = q + u s
4225 *
4226 * Calculate t:
4227 *
4228 * (p + t r) × s = (q + u s) × s
4229 * p × s + t (r × s) = q × s + u (s × s)
4230 * p × s + t (r × s) = q × s
4231 * t (r × s) = q × s - p × s
4232 * t (r × s) = (q - p) × s
4233 * t = ((q - p) × s) / (r × s)
4234 *
4235 * Using the same method, for u we get:
4236 *
4237 * u = ((p - q) × r) / (s × r)
4238 */
4239
4240 rxs = vec2d_cross_product(r, s);
4241 sxr = vec2d_cross_product(s, r);
4242
4243 /* If r × s = 0 then the lines are either parallel or collinear. */
4244 if (fabs(rxs) < DBL_MIN)
4245 return false;
4246
4247 t = vec2d_cross_product(vec2d_subtract(q, p), s) / rxs;
4248 u = vec2d_cross_product(vec2d_subtract(p, q), r) / sxr;
4249
4250 /* The lines only intersect if 0 ≤ t ≤ 1 and 0 ≤ u ≤ 1. */
4251 if (t < 0.0 || t > 1.0 || u < 0.0 || u > 1.0)
4252 return false;
4253
4254 *intersection = vec2d_add(p, vec2d_multiply_constant(t, r));
4255 return true;
4256}
4257
4258static struct border *
4259add_border(struct wl_array *array,
4260 double x1, double y1,
4261 double x2, double y2,
4262 enum motion_direction blocking_dir)
4263{
4264 struct border *border = wl_array_add(array, sizeof *border);
4265
4266 *border = (struct border) {
4267 .line = (struct line) {
4268 .a = (struct vec2d) {
4269 .x = x1,
4270 .y = y1,
4271 },
4272 .b = (struct vec2d) {
4273 .x = x2,
4274 .y = y2,
4275 },
4276 },
4277 .blocking_dir = blocking_dir,
4278 };
4279
4280 return border;
4281}
4282
4283static int
4284compare_lines_x(const void *a, const void *b)
4285{
4286 const struct border *border_a = a;
4287 const struct border *border_b = b;
4288
4289
4290 if (border_a->line.a.x == border_b->line.a.x)
4291 return border_a->line.b.x < border_b->line.b.x;
4292 else
4293 return border_a->line.a.x > border_b->line.a.x;
4294}
4295
4296static void
4297add_non_overlapping_edges(pixman_box32_t *boxes,
4298 int band_above_start,
4299 int band_below_start,
4300 int band_below_end,
4301 struct wl_array *borders)
4302{
4303 int i;
4304 struct wl_array band_merge;
4305 struct border *border;
4306 struct border *prev_border;
4307 struct border *new_border;
4308
4309 wl_array_init(&band_merge);
4310
4311 /* Add bottom band of previous row, and top band of current row, and
4312 * sort them so lower left x coordinate comes first. If there are two
4313 * borders with the same left x coordinate, the wider one comes first.
4314 */
4315 for (i = band_above_start; i < band_below_start; i++) {
4316 pixman_box32_t *box = &boxes[i];
4317 add_border(&band_merge, box->x1, box->y2, box->x2, box->y2,
4318 MOTION_DIRECTION_POSITIVE_Y);
4319 }
4320 for (i = band_below_start; i < band_below_end; i++) {
4321 pixman_box32_t *box= &boxes[i];
4322 add_border(&band_merge, box->x1, box->y1, box->x2, box->y1,
4323 MOTION_DIRECTION_NEGATIVE_Y);
4324 }
4325 qsort(band_merge.data,
4326 band_merge.size / sizeof *border,
4327 sizeof *border,
4328 compare_lines_x);
4329
4330 /* Combine the two combined bands so that any overlapping border is
4331 * eliminated. */
4332 prev_border = NULL;
4333 wl_array_for_each(border, &band_merge) {
4334 assert(border->line.a.y == border->line.b.y);
4335 assert(!prev_border ||
4336 prev_border->line.a.y == border->line.a.y);
4337 assert(!prev_border ||
4338 (prev_border->line.a.x != border->line.a.x ||
4339 prev_border->line.b.x != border->line.b.x));
4340 assert(!prev_border ||
4341 prev_border->line.a.x <= border->line.a.x);
4342
4343 if (prev_border &&
4344 prev_border->line.a.x == border->line.a.x) {
4345 /*
4346 * ------------ +
4347 * ------- =
4348 * [ ]-----
4349 */
4350 prev_border->line.a.x = border->line.b.x;
4351 } else if (prev_border &&
4352 prev_border->line.b.x == border->line.b.x) {
4353 /*
4354 * ------------ +
4355 * ------ =
4356 * ------[ ]
4357 */
4358 prev_border->line.b.x = border->line.a.x;
4359 } else if (prev_border &&
4360 prev_border->line.b.x == border->line.a.x) {
4361 /*
4362 * -------- +
4363 * ------ =
4364 * --------------
4365 */
4366 prev_border->line.b.x = border->line.b.x;
4367 } else if (prev_border &&
4368 prev_border->line.b.x >= border->line.a.x) {
4369 /*
4370 * --------------- +
4371 * ------ =
4372 * -----[ ]----
4373 */
4374 new_border = add_border(borders,
4375 border->line.b.x,
4376 border->line.b.y,
4377 prev_border->line.b.x,
4378 prev_border->line.b.y,
4379 prev_border->blocking_dir);
4380 prev_border->line.b.x = border->line.a.x;
4381 prev_border = new_border;
4382 } else {
4383 assert(!prev_border ||
4384 prev_border->line.b.x < border->line.a.x);
4385 /*
4386 * First border or non-overlapping.
4387 *
4388 * ----- +
4389 * ----- =
4390 * ----- -----
4391 */
4392 new_border = wl_array_add(borders, sizeof *border);
4393 *new_border = *border;
4394 prev_border = new_border;
4395 }
4396 }
4397
4398 wl_array_release(&band_merge);
4399}
4400
4401static void
4402add_band_bottom_edges(pixman_box32_t *boxes,
4403 int band_start,
4404 int band_end,
4405 struct wl_array *borders)
4406{
4407 int i;
4408
4409 for (i = band_start; i < band_end; i++) {
4410 add_border(borders,
4411 boxes[i].x1, boxes[i].y2,
4412 boxes[i].x2, boxes[i].y2,
4413 MOTION_DIRECTION_POSITIVE_Y);
4414 }
4415}
4416
4417static void
4418region_to_outline(pixman_region32_t *region, struct wl_array *borders)
4419{
4420 pixman_box32_t *boxes;
4421 int num_boxes;
4422 int i;
4423 int top_most, bottom_most;
4424 int current_roof;
4425 int prev_top;
4426 int band_start, prev_band_start;
4427
4428 /*
4429 * Remove any overlapping lines from the set of rectangles. Note that
4430 * pixman regions are grouped as rows of rectangles, where rectangles
4431 * in one row never touch or overlap and are all of the same height.
4432 *
4433 * -------- --- -------- ---
4434 * | | | | | | | |
4435 * ----------====---- --- ----------- ----- ---
4436 * | | => | |
4437 * ----==========--------- ----- ----------
4438 * | | | |
4439 * ------------------- -------------------
4440 *
4441 */
4442
4443 boxes = pixman_region32_rectangles(region, &num_boxes);
4444 prev_top = 0;
4445 top_most = boxes[0].y1;
4446 current_roof = top_most;
4447 bottom_most = boxes[num_boxes - 1].y2;
4448 band_start = 0;
4449 prev_band_start = 0;
4450 for (i = 0; i < num_boxes; i++) {
4451 /* Detect if there is a vertical empty space, and add the lower
4452 * level of the previous band if so was the case. */
4453 if (i > 0 &&
4454 boxes[i].y1 != prev_top &&
4455 boxes[i].y1 != boxes[i - 1].y2) {
4456 current_roof = boxes[i].y1;
4457 add_band_bottom_edges(boxes,
4458 band_start,
4459 i,
4460 borders);
4461 }
4462
4463 /* Special case adding the last band, since it won't be handled
4464 * by the band change detection below. */
4465 if (boxes[i].y1 != current_roof && i == num_boxes - 1) {
4466 if (boxes[i].y1 != prev_top) {
4467 /* The last band is a single box, so we don't
4468 * have a prev_band_start to tell us when the
4469 * previous band started. */
4470 add_non_overlapping_edges(boxes,
4471 band_start,
4472 i,
4473 i + 1,
4474 borders);
4475 } else {
4476 add_non_overlapping_edges(boxes,
4477 prev_band_start,
4478 band_start,
4479 i + 1,
4480 borders);
4481 }
4482 }
4483
4484 /* Detect when passing a band and combine the top border of the
4485 * just passed band with the bottom band of the previous band.
4486 */
4487 if (boxes[i].y1 != top_most && boxes[i].y1 != prev_top) {
4488 /* Combine the two passed bands. */
4489 if (prev_top != current_roof) {
4490 add_non_overlapping_edges(boxes,
4491 prev_band_start,
4492 band_start,
4493 i,
4494 borders);
4495 }
4496
4497 prev_band_start = band_start;
4498 band_start = i;
4499 }
4500
4501 /* Add the top border if the box is part of the current roof. */
4502 if (boxes[i].y1 == current_roof) {
4503 add_border(borders,
4504 boxes[i].x1, boxes[i].y1,
4505 boxes[i].x2, boxes[i].y1,
4506 MOTION_DIRECTION_NEGATIVE_Y);
4507 }
4508
4509 /* Add the bottom border of the last band. */
4510 if (boxes[i].y2 == bottom_most) {
4511 add_border(borders,
4512 boxes[i].x1, boxes[i].y2,
4513 boxes[i].x2, boxes[i].y2,
4514 MOTION_DIRECTION_POSITIVE_Y);
4515 }
4516
4517 /* Always add the left border. */
4518 add_border(borders,
4519 boxes[i].x1, boxes[i].y1,
4520 boxes[i].x1, boxes[i].y2,
4521 MOTION_DIRECTION_NEGATIVE_X);
4522
4523 /* Always add the right border. */
4524 add_border(borders,
4525 boxes[i].x2, boxes[i].y1,
4526 boxes[i].x2, boxes[i].y2,
4527 MOTION_DIRECTION_POSITIVE_X);
4528
4529 prev_top = boxes[i].y1;
4530 }
4531}
4532
4533static bool
4534is_border_horizontal (struct border *border)
4535{
4536 return border->line.a.y == border->line.b.y;
4537}
4538
4539static bool
4540is_border_blocking_directions(struct border *border,
4541 uint32_t directions)
4542{
4543 /* Don't block parallel motions. */
4544 if (is_border_horizontal(border)) {
4545 if ((directions & (MOTION_DIRECTION_POSITIVE_Y |
4546 MOTION_DIRECTION_NEGATIVE_Y)) == 0)
4547 return false;
4548 } else {
4549 if ((directions & (MOTION_DIRECTION_POSITIVE_X |
4550 MOTION_DIRECTION_NEGATIVE_X)) == 0)
4551 return false;
4552 }
4553
4554 return (~border->blocking_dir & directions) != directions;
4555}
4556
4557static struct border *
4558get_closest_border(struct wl_array *borders,
4559 struct line *motion,
4560 uint32_t directions)
4561{
4562 struct border *border;
4563 struct vec2d intersection;
4564 struct vec2d delta;
4565 double distance_2;
4566 struct border *closest_border = NULL;
4567 double closest_distance_2 = DBL_MAX;
4568
4569 wl_array_for_each(border, borders) {
4570 if (!is_border_blocking_directions(border, directions))
4571 continue;
4572
4573 if (!lines_intersect(&border->line, motion, &intersection))
4574 continue;
4575
4576 delta = vec2d_subtract(intersection, motion->a);
4577 distance_2 = delta.x*delta.x + delta.y*delta.y;
4578 if (distance_2 < closest_distance_2) {
4579 closest_border = border;
4580 closest_distance_2 = distance_2;
4581 }
4582 }
4583
4584 return closest_border;
4585}
4586
4587static void
4588clamp_to_border(struct border *border,
4589 struct line *motion,
4590 uint32_t *motion_dir)
4591{
4592 /*
4593 * When clamping either rightward or downward motions, the motion needs
4594 * to be clamped so that the destination coordinate does not end up on
4595 * the border (see weston_pointer_clamp_event_to_region). Do this by
4596 * clamping such motions to the border minus the smallest possible
4597 * wl_fixed_t value.
4598 */
4599 if (is_border_horizontal(border)) {
4600 if (*motion_dir & MOTION_DIRECTION_POSITIVE_Y)
4601 motion->b.y = border->line.a.y - wl_fixed_to_double(1);
4602 else
4603 motion->b.y = border->line.a.y;
4604 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_Y |
4605 MOTION_DIRECTION_NEGATIVE_Y);
4606 } else {
4607 if (*motion_dir & MOTION_DIRECTION_POSITIVE_X)
4608 motion->b.x = border->line.a.x - wl_fixed_to_double(1);
4609 else
4610 motion->b.x = border->line.a.x;
4611 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_X |
4612 MOTION_DIRECTION_NEGATIVE_X);
4613 }
4614}
4615
4616static uint32_t
4617get_motion_directions(struct line *motion)
4618{
4619 uint32_t directions = 0;
4620
4621 if (motion->a.x < motion->b.x)
4622 directions |= MOTION_DIRECTION_POSITIVE_X;
4623 else if (motion->a.x > motion->b.x)
4624 directions |= MOTION_DIRECTION_NEGATIVE_X;
4625 if (motion->a.y < motion->b.y)
4626 directions |= MOTION_DIRECTION_POSITIVE_Y;
4627 else if (motion->a.y > motion->b.y)
4628 directions |= MOTION_DIRECTION_NEGATIVE_Y;
4629
4630 return directions;
4631}
4632
Jonas Ådahld3414f22016-07-22 17:56:31 +08004633static void
4634weston_pointer_clamp_event_to_region(struct weston_pointer *pointer,
4635 struct weston_pointer_motion_event *event,
4636 pixman_region32_t *region,
4637 wl_fixed_t *clamped_x,
4638 wl_fixed_t *clamped_y)
4639{
4640 wl_fixed_t x, y;
4641 wl_fixed_t sx, sy;
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004642 wl_fixed_t old_sx = pointer->sx;
4643 wl_fixed_t old_sy = pointer->sy;
4644 struct wl_array borders;
4645 struct line motion;
4646 struct border *closest_border;
4647 float new_x_f, new_y_f;
4648 uint32_t directions;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004649
4650 weston_pointer_motion_to_abs(pointer, event, &x, &y);
4651 weston_view_from_global_fixed(pointer->focus, x, y, &sx, &sy);
4652
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004653 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004654
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004655 /*
4656 * Generate borders given the confine region we are to use. The borders
4657 * are defined to be the outer region of the allowed area. This means
4658 * top/left borders are "within" the allowed area, while bottom/right
4659 * borders are outside. This needs to be considered when clamping
4660 * confined motion vectors.
4661 */
4662 region_to_outline(region, &borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004663
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004664 motion = (struct line) {
4665 .a = (struct vec2d) {
4666 .x = wl_fixed_to_double(old_sx),
4667 .y = wl_fixed_to_double(old_sy),
4668 },
4669 .b = (struct vec2d) {
4670 .x = wl_fixed_to_double(sx),
4671 .y = wl_fixed_to_double(sy),
4672 },
4673 };
4674 directions = get_motion_directions(&motion);
4675
4676 while (directions) {
4677 closest_border = get_closest_border(&borders,
4678 &motion,
4679 directions);
4680 if (closest_border)
4681 clamp_to_border(closest_border, &motion, &directions);
4682 else
4683 break;
4684 }
4685
4686 weston_view_to_global_float(pointer->focus,
4687 (float) motion.b.x, (float) motion.b.y,
4688 &new_x_f, &new_y_f);
4689 *clamped_x = wl_fixed_from_double(new_x_f);
4690 *clamped_y = wl_fixed_from_double(new_y_f);
4691
4692 wl_array_release(&borders);
4693}
4694
4695static double
4696point_to_border_distance_2(struct border *border, double x, double y)
4697{
4698 double orig_x, orig_y;
4699 double dx, dy;
4700
4701 if (is_border_horizontal(border)) {
4702 if (x < border->line.a.x)
4703 orig_x = border->line.a.x;
4704 else if (x > border->line.b.x)
4705 orig_x = border->line.b.x;
4706 else
4707 orig_x = x;
4708 orig_y = border->line.a.y;
4709 } else {
4710 if (y < border->line.a.y)
4711 orig_y = border->line.a.y;
4712 else if (y > border->line.b.y)
4713 orig_y = border->line.b.y;
4714 else
4715 orig_y = y;
4716 orig_x = border->line.a.x;
4717 }
4718
4719
4720 dx = fabs(orig_x - x);
4721 dy = fabs(orig_y - y);
4722 return dx*dx + dy*dy;
4723}
4724
4725static void
4726warp_to_behind_border(struct border *border, wl_fixed_t *sx, wl_fixed_t *sy)
4727{
4728 switch (border->blocking_dir) {
4729 case MOTION_DIRECTION_POSITIVE_X:
4730 case MOTION_DIRECTION_NEGATIVE_X:
4731 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_X)
4732 *sx = wl_fixed_from_double(border->line.a.x) - 1;
4733 else
4734 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4735 if (*sy < wl_fixed_from_double(border->line.a.y))
4736 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4737 else if (*sy > wl_fixed_from_double(border->line.b.y))
4738 *sy = wl_fixed_from_double(border->line.b.y) - 1;
4739 break;
4740 case MOTION_DIRECTION_POSITIVE_Y:
4741 case MOTION_DIRECTION_NEGATIVE_Y:
4742 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_Y)
4743 *sy = wl_fixed_from_double(border->line.a.y) - 1;
4744 else
4745 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4746 if (*sx < wl_fixed_from_double(border->line.a.x))
4747 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4748 else if (*sx > wl_fixed_from_double(border->line.b.x))
4749 *sx = wl_fixed_from_double(border->line.b.x) - 1;
4750 break;
4751 }
Jonas Ådahld3414f22016-07-22 17:56:31 +08004752}
4753
4754static void
4755maybe_warp_confined_pointer(struct weston_pointer_constraint *constraint)
4756{
4757 wl_fixed_t x;
4758 wl_fixed_t y;
4759 wl_fixed_t sx;
4760 wl_fixed_t sy;
4761
4762 weston_view_from_global_fixed(constraint->view,
4763 constraint->pointer->x,
4764 constraint->pointer->y,
4765 &sx,
4766 &sy);
4767
4768 if (!is_within_constraint_region(constraint, sx, sy)) {
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004769 double xf = wl_fixed_to_double(sx);
4770 double yf = wl_fixed_to_double(sy);
4771 pixman_region32_t confine_region;
4772 struct wl_array borders;
4773 struct border *border;
4774 double closest_distance_2 = DBL_MAX;
4775 struct border *closest_border = NULL;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004776
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004777 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004778
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004779 pixman_region32_init(&confine_region);
4780 pixman_region32_intersect(&confine_region,
4781 &constraint->view->surface->input,
4782 &constraint->region);
4783 region_to_outline(&confine_region, &borders);
4784 pixman_region32_fini(&confine_region);
4785
4786 wl_array_for_each(border, &borders) {
4787 double distance_2;
4788
4789 distance_2 = point_to_border_distance_2(border, xf, yf);
4790 if (distance_2 < closest_distance_2) {
4791 closest_border = border;
4792 closest_distance_2 = distance_2;
4793 }
4794 }
4795 assert(closest_border);
4796
4797 warp_to_behind_border(closest_border, &sx, &sy);
4798
4799 wl_array_release(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004800
4801 weston_view_to_global_fixed(constraint->view, sx, sy, &x, &y);
4802 weston_pointer_move_to(constraint->pointer, x, y);
4803 }
4804}
4805
4806static void
4807confined_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02004808 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004809 struct weston_pointer_motion_event *event)
4810{
4811 struct weston_pointer_constraint *constraint =
4812 container_of(grab, struct weston_pointer_constraint, grab);
4813 struct weston_pointer *pointer = grab->pointer;
4814 struct weston_surface *surface;
4815 wl_fixed_t x, y;
4816 wl_fixed_t old_sx = pointer->sx;
4817 wl_fixed_t old_sy = pointer->sy;
4818 pixman_region32_t confine_region;
4819
4820 assert(pointer->focus);
4821 assert(pointer->focus->surface == constraint->surface);
4822
4823 surface = pointer->focus->surface;
4824
4825 pixman_region32_init(&confine_region);
4826 pixman_region32_intersect(&confine_region,
4827 &surface->input,
4828 &constraint->region);
4829 weston_pointer_clamp_event_to_region(pointer, event,
4830 &confine_region, &x, &y);
4831 weston_pointer_move_to(pointer, x, y);
4832 pixman_region32_fini(&confine_region);
4833
4834 weston_view_from_global_fixed(pointer->focus, x, y,
4835 &pointer->sx, &pointer->sy);
4836
4837 if (old_sx != pointer->sx || old_sy != pointer->sy) {
Quentin Glidiccde13452016-08-12 10:41:32 +02004838 pointer_send_motion(pointer, time,
4839 pointer->sx, pointer->sy);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004840 }
4841
Quentin Glidiccde13452016-08-12 10:41:32 +02004842 pointer_send_relative_motion(pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004843}
4844
4845static void
4846confined_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02004847 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004848 uint32_t button,
4849 uint32_t state_w)
4850{
4851 weston_pointer_send_button(grab->pointer, time, button, state_w);
4852}
4853
4854static void
4855confined_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02004856 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004857 struct weston_pointer_axis_event *event)
4858{
4859 weston_pointer_send_axis(grab->pointer, time, event);
4860}
4861
4862static void
4863confined_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
4864 uint32_t source)
4865{
4866 weston_pointer_send_axis_source(grab->pointer, source);
4867}
4868
4869static void
4870confined_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
4871{
4872 weston_pointer_send_frame(grab->pointer);
4873}
4874
4875static void
4876confined_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
4877{
4878 struct weston_pointer_constraint *constraint =
4879 container_of(grab, struct weston_pointer_constraint, grab);
4880
4881 disable_pointer_constraint(constraint);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004882}
4883
4884static const struct weston_pointer_grab_interface
4885 confined_pointer_grab_interface = {
4886 confined_pointer_grab_pointer_focus,
4887 confined_pointer_grab_pointer_motion,
4888 confined_pointer_grab_pointer_button,
4889 confined_pointer_grab_pointer_axis,
4890 confined_pointer_grab_pointer_axis_source,
4891 confined_pointer_grab_pointer_frame,
4892 confined_pointer_grab_pointer_cancel,
4893};
4894
4895static void
4896confined_pointer_destroy(struct wl_client *client,
4897 struct wl_resource *resource)
4898{
4899 wl_resource_destroy(resource);
4900}
4901
4902static void
4903confined_pointer_set_region(struct wl_client *client,
4904 struct wl_resource *resource,
4905 struct wl_resource *region_resource)
4906{
4907 struct weston_pointer_constraint *constraint =
4908 wl_resource_get_user_data(resource);
4909 struct weston_region *region = region_resource ?
4910 wl_resource_get_user_data(region_resource) : NULL;
4911
4912 if (!constraint)
4913 return;
4914
4915 if (region) {
4916 pixman_region32_copy(&constraint->region_pending,
4917 &region->region);
4918 } else {
4919 pixman_region32_fini(&constraint->region_pending);
4920 region_init_infinite(&constraint->region_pending);
4921 }
4922 constraint->region_is_pending = true;
4923}
4924
4925static const struct zwp_confined_pointer_v1_interface confined_pointer_interface = {
4926 confined_pointer_destroy,
4927 confined_pointer_set_region,
4928};
4929
4930static void
4931pointer_constraints_confine_pointer(struct wl_client *client,
4932 struct wl_resource *resource,
4933 uint32_t id,
4934 struct wl_resource *surface_resource,
4935 struct wl_resource *pointer_resource,
4936 struct wl_resource *region_resource,
4937 uint32_t lifetime)
4938{
4939 struct weston_surface *surface =
4940 wl_resource_get_user_data(surface_resource);
4941 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
4942 struct weston_region *region = region_resource ?
4943 wl_resource_get_user_data(region_resource) : NULL;
4944
4945 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
4946 &zwp_confined_pointer_v1_interface,
4947 &confined_pointer_interface,
4948 &confined_pointer_grab_interface);
4949}
4950
4951static const struct zwp_pointer_constraints_v1_interface pointer_constraints_interface = {
4952 pointer_constraints_destroy,
4953 pointer_constraints_lock_pointer,
4954 pointer_constraints_confine_pointer,
4955};
4956
4957static void
4958bind_pointer_constraints(struct wl_client *client, void *data,
4959 uint32_t version, uint32_t id)
4960{
4961 struct wl_resource *resource;
4962
4963 resource = wl_resource_create(client,
4964 &zwp_pointer_constraints_v1_interface,
4965 1, id);
4966
4967 wl_resource_set_implementation(resource, &pointer_constraints_interface,
4968 NULL, NULL);
4969}
4970
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004971static void
Alexandros Frantzis2b442482018-02-20 14:05:50 +02004972input_timestamps_destroy(struct wl_client *client,
4973 struct wl_resource *resource)
4974{
4975 wl_resource_destroy(resource);
4976}
4977
4978static const struct zwp_input_timestamps_v1_interface
4979 input_timestamps_interface = {
4980 input_timestamps_destroy,
4981};
4982
4983static void
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004984input_timestamps_manager_destroy(struct wl_client *client,
4985 struct wl_resource *resource)
4986{
4987 wl_resource_destroy(resource);
4988}
4989
4990static void
4991input_timestamps_manager_get_keyboard_timestamps(struct wl_client *client,
4992 struct wl_resource *resource,
4993 uint32_t id,
4994 struct wl_resource *keyboard_resource)
4995{
Alexandros Frantzis2b442482018-02-20 14:05:50 +02004996 struct weston_keyboard *keyboard =
4997 wl_resource_get_user_data(keyboard_resource);
4998 struct wl_resource *input_ts;
4999
5000 input_ts = wl_resource_create(client,
5001 &zwp_input_timestamps_v1_interface,
5002 1, id);
5003 if (!input_ts) {
5004 wl_client_post_no_memory(client);
5005 return;
5006 }
5007
5008 if (keyboard) {
5009 wl_list_insert(&keyboard->timestamps_list,
5010 wl_resource_get_link(input_ts));
5011 } else {
5012 wl_list_init(wl_resource_get_link(input_ts));
5013 }
5014
5015 wl_resource_set_implementation(input_ts,
5016 &input_timestamps_interface,
5017 keyboard_resource,
5018 unbind_resource);
Alexandros Frantzis538749d2018-02-16 18:44:16 +02005019}
5020
5021static void
5022input_timestamps_manager_get_pointer_timestamps(struct wl_client *client,
5023 struct wl_resource *resource,
5024 uint32_t id,
5025 struct wl_resource *pointer_resource)
5026{
Alexandros Frantzisdb907b72018-02-20 14:06:26 +02005027 struct weston_pointer *pointer =
5028 wl_resource_get_user_data(pointer_resource);
5029 struct wl_resource *input_ts;
5030
5031 input_ts = wl_resource_create(client,
5032 &zwp_input_timestamps_v1_interface,
5033 1, id);
5034 if (!input_ts) {
5035 wl_client_post_no_memory(client);
5036 return;
5037 }
5038
5039 if (pointer) {
5040 wl_list_insert(&pointer->timestamps_list,
5041 wl_resource_get_link(input_ts));
5042 } else {
5043 wl_list_init(wl_resource_get_link(input_ts));
5044 }
5045
5046 wl_resource_set_implementation(input_ts,
5047 &input_timestamps_interface,
5048 pointer_resource,
5049 unbind_resource);
Alexandros Frantzis538749d2018-02-16 18:44:16 +02005050}
5051
5052static void
5053input_timestamps_manager_get_touch_timestamps(struct wl_client *client,
5054 struct wl_resource *resource,
5055 uint32_t id,
5056 struct wl_resource *touch_resource)
5057{
Alexandros Frantzisd7157842018-02-20 14:07:03 +02005058 struct weston_touch *touch = wl_resource_get_user_data(touch_resource);
5059 struct wl_resource *input_ts;
5060
5061 input_ts = wl_resource_create(client,
5062 &zwp_input_timestamps_v1_interface,
5063 1, id);
5064 if (!input_ts) {
5065 wl_client_post_no_memory(client);
5066 return;
5067 }
5068
5069 if (touch) {
5070 wl_list_insert(&touch->timestamps_list,
5071 wl_resource_get_link(input_ts));
5072 } else {
5073 wl_list_init(wl_resource_get_link(input_ts));
5074 }
5075
5076 wl_resource_set_implementation(input_ts,
5077 &input_timestamps_interface,
5078 touch_resource,
5079 unbind_resource);
Alexandros Frantzis538749d2018-02-16 18:44:16 +02005080}
5081
5082static const struct zwp_input_timestamps_manager_v1_interface
5083 input_timestamps_manager_interface = {
5084 input_timestamps_manager_destroy,
5085 input_timestamps_manager_get_keyboard_timestamps,
5086 input_timestamps_manager_get_pointer_timestamps,
5087 input_timestamps_manager_get_touch_timestamps,
5088};
5089
5090static void
5091bind_input_timestamps_manager(struct wl_client *client, void *data,
5092 uint32_t version, uint32_t id)
5093{
5094 struct wl_resource *resource =
5095 wl_resource_create(client,
5096 &zwp_input_timestamps_manager_v1_interface,
5097 1, id);
5098
5099 if (resource == NULL) {
5100 wl_client_post_no_memory(client);
5101 return;
5102 }
5103
5104 wl_resource_set_implementation(resource,
5105 &input_timestamps_manager_interface,
5106 NULL, NULL);
5107}
5108
Jonas Ådahl30d61d82014-10-22 21:21:17 +02005109int
5110weston_input_init(struct weston_compositor *compositor)
5111{
5112 if (!wl_global_create(compositor->wl_display,
5113 &zwp_relative_pointer_manager_v1_interface, 1,
5114 compositor, bind_relative_pointer_manager))
5115 return -1;
5116
Jonas Ådahld3414f22016-07-22 17:56:31 +08005117 if (!wl_global_create(compositor->wl_display,
5118 &zwp_pointer_constraints_v1_interface, 1,
5119 NULL, bind_pointer_constraints))
5120 return -1;
5121
Alexandros Frantzis538749d2018-02-16 18:44:16 +02005122 if (!wl_global_create(compositor->wl_display,
5123 &zwp_input_timestamps_manager_v1_interface, 1,
5124 NULL, bind_input_timestamps_manager))
5125 return -1;
5126
Jonas Ådahl30d61d82014-10-22 21:21:17 +02005127 return 0;
5128}