blob: e1a2efe791e871d44ab5f77aa9a2bae82fa029f6 [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;
limin.tian1a73c602025-01-22 08:06:40 +00003691 if (!compositor->simple_shell) {
3692 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
3693 if (keyboard && keyboard->focus != surface) {
3694 weston_keyboard_set_focus(keyboard, surface);
3695 wl_data_device_set_keyboard_focus(seat);
3696 }
3697 }
Bryce Harrington24f917e2016-06-29 19:04:07 -07003698
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003699 inc_activate_serial(compositor);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003700
3701 activation_data = (struct weston_surface_activation_data) {
3702 .surface = surface,
3703 .seat = seat,
3704 };
3705 wl_signal_emit(&compositor->activate_signal, &activation_data);
Bryce Harrington24f917e2016-06-29 19:04:07 -07003706}
Jonas Ådahl30d61d82014-10-22 21:21:17 +02003707
Jonas Ådahld3414f22016-07-22 17:56:31 +08003708static void
3709enable_pointer_constraint(struct weston_pointer_constraint *constraint,
3710 struct weston_view *view)
3711{
3712 assert(constraint->view == NULL);
3713 constraint->view = view;
3714 pointer_constraint_notify_activated(constraint);
3715 weston_pointer_start_grab(constraint->pointer, &constraint->grab);
3716 wl_list_remove(&constraint->surface_destroy_listener.link);
3717 wl_list_init(&constraint->surface_destroy_listener.link);
3718}
3719
3720static bool
3721is_pointer_constraint_enabled(struct weston_pointer_constraint *constraint)
3722{
3723 return constraint->view != NULL;
3724}
3725
3726static void
3727weston_pointer_constraint_disable(struct weston_pointer_constraint *constraint)
3728{
3729 constraint->view = NULL;
3730 pointer_constraint_notify_deactivated(constraint);
3731 weston_pointer_end_grab(constraint->grab.pointer);
3732}
3733
3734void
3735weston_pointer_constraint_destroy(struct weston_pointer_constraint *constraint)
3736{
3737 if (is_pointer_constraint_enabled(constraint))
3738 weston_pointer_constraint_disable(constraint);
3739
3740 wl_list_remove(&constraint->pointer_destroy_listener.link);
3741 wl_list_remove(&constraint->surface_destroy_listener.link);
3742 wl_list_remove(&constraint->surface_commit_listener.link);
3743 wl_list_remove(&constraint->surface_activate_listener.link);
3744
3745 wl_resource_set_user_data(constraint->resource, NULL);
3746 pixman_region32_fini(&constraint->region);
3747 wl_list_remove(&constraint->link);
3748 free(constraint);
3749}
3750
3751static void
3752disable_pointer_constraint(struct weston_pointer_constraint *constraint)
3753{
3754 switch (constraint->lifetime) {
3755 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT:
3756 weston_pointer_constraint_destroy(constraint);
3757 break;
3758 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT:
3759 weston_pointer_constraint_disable(constraint);
3760 break;
3761 }
3762}
3763
3764static bool
3765is_within_constraint_region(struct weston_pointer_constraint *constraint,
3766 wl_fixed_t sx, wl_fixed_t sy)
3767{
3768 struct weston_surface *surface = constraint->surface;
3769 pixman_region32_t constraint_region;
3770 bool result;
3771
3772 pixman_region32_init(&constraint_region);
3773 pixman_region32_intersect(&constraint_region,
3774 &surface->input,
3775 &constraint->region);
3776 result = pixman_region32_contains_point(&constraint_region,
3777 wl_fixed_to_int(sx),
3778 wl_fixed_to_int(sy),
3779 NULL);
3780 pixman_region32_fini(&constraint_region);
3781
3782 return result;
3783}
3784
3785static void
3786maybe_enable_pointer_constraint(struct weston_pointer_constraint *constraint)
3787{
3788 struct weston_surface *surface = constraint->surface;
3789 struct weston_view *vit;
3790 struct weston_view *view = NULL;
3791 struct weston_pointer *pointer = constraint->pointer;
3792 struct weston_keyboard *keyboard;
3793 struct weston_seat *seat = pointer->seat;
3794 int32_t x, y;
3795
3796 /* Postpone if no view of the surface was most recently clicked. */
3797 wl_list_for_each(vit, &surface->views, surface_link) {
3798 if (vit->click_to_activate_serial ==
3799 surface->compositor->activate_serial) {
3800 view = vit;
3801 }
3802 }
3803 if (view == NULL)
3804 return;
3805
3806 /* Postpone if surface doesn't have keyboard focus. */
3807 keyboard = weston_seat_get_keyboard(seat);
3808 if (!keyboard || keyboard->focus != surface)
3809 return;
3810
3811 /* Postpone constraint if the pointer is not within the
3812 * constraint region.
3813 */
3814 weston_view_from_global(view,
3815 wl_fixed_to_int(pointer->x),
3816 wl_fixed_to_int(pointer->y),
3817 &x, &y);
3818 if (!is_within_constraint_region(constraint,
3819 wl_fixed_from_int(x),
3820 wl_fixed_from_int(y)))
3821 return;
3822
3823 enable_pointer_constraint(constraint, view);
3824}
3825
3826static void
3827locked_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
3828{
3829}
3830
3831static void
3832locked_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02003833 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003834 struct weston_pointer_motion_event *event)
3835{
Quentin Glidiccde13452016-08-12 10:41:32 +02003836 pointer_send_relative_motion(grab->pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08003837}
3838
3839static void
3840locked_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003841 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003842 uint32_t button,
3843 uint32_t state_w)
3844{
3845 weston_pointer_send_button(grab->pointer, time, button, state_w);
3846}
3847
3848static void
3849locked_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02003850 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003851 struct weston_pointer_axis_event *event)
3852{
3853 weston_pointer_send_axis(grab->pointer, time, event);
3854}
3855
3856static void
3857locked_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
3858 uint32_t source)
3859{
3860 weston_pointer_send_axis_source(grab->pointer, source);
3861}
3862
3863static void
3864locked_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
3865{
3866 weston_pointer_send_frame(grab->pointer);
3867}
3868
3869static void
3870locked_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
3871{
3872 struct weston_pointer_constraint *constraint =
3873 container_of(grab, struct weston_pointer_constraint, grab);
3874
3875 disable_pointer_constraint(constraint);
3876}
3877
3878static const struct weston_pointer_grab_interface
3879 locked_pointer_grab_interface = {
3880 locked_pointer_grab_pointer_focus,
3881 locked_pointer_grab_pointer_motion,
3882 locked_pointer_grab_pointer_button,
3883 locked_pointer_grab_pointer_axis,
3884 locked_pointer_grab_pointer_axis_source,
3885 locked_pointer_grab_pointer_frame,
3886 locked_pointer_grab_pointer_cancel,
3887};
3888
3889static void
3890pointer_constraint_constrain_resource_destroyed(struct wl_resource *resource)
3891{
3892 struct weston_pointer_constraint *constraint =
3893 wl_resource_get_user_data(resource);
3894
3895 if (!constraint)
3896 return;
3897
3898 weston_pointer_constraint_destroy(constraint);
3899}
3900
3901static void
3902pointer_constraint_surface_activate(struct wl_listener *listener, void *data)
3903{
3904 struct weston_surface_activation_data *activation = data;
3905 struct weston_pointer *pointer;
3906 struct weston_surface *focus = activation->surface;
3907 struct weston_pointer_constraint *constraint =
3908 container_of(listener, struct weston_pointer_constraint,
3909 surface_activate_listener);
3910 bool is_constraint_surface;
3911
3912 pointer = weston_seat_get_pointer(activation->seat);
3913 if (!pointer)
3914 return;
3915
3916 is_constraint_surface =
3917 get_pointer_constraint_for_pointer(focus, pointer) == constraint;
3918
3919 if (is_constraint_surface &&
3920 !is_pointer_constraint_enabled(constraint))
3921 maybe_enable_pointer_constraint(constraint);
3922 else if (!is_constraint_surface &&
3923 is_pointer_constraint_enabled(constraint))
3924 disable_pointer_constraint(constraint);
3925}
3926
3927static void
3928pointer_constraint_pointer_destroyed(struct wl_listener *listener, void *data)
3929{
3930 struct weston_pointer_constraint *constraint =
3931 container_of(listener, struct weston_pointer_constraint,
3932 pointer_destroy_listener);
3933
3934 weston_pointer_constraint_destroy(constraint);
3935}
3936
3937static void
3938pointer_constraint_surface_destroyed(struct wl_listener *listener, void *data)
3939{
3940 struct weston_pointer_constraint *constraint =
3941 container_of(listener, struct weston_pointer_constraint,
3942 surface_destroy_listener);
3943
3944 weston_pointer_constraint_destroy(constraint);
3945}
3946
3947static void
3948pointer_constraint_surface_committed(struct wl_listener *listener, void *data)
3949{
3950 struct weston_pointer_constraint *constraint =
3951 container_of(listener, struct weston_pointer_constraint,
3952 surface_commit_listener);
3953
3954 if (constraint->region_is_pending) {
3955 constraint->region_is_pending = false;
3956 pixman_region32_copy(&constraint->region,
3957 &constraint->region_pending);
3958 pixman_region32_fini(&constraint->region_pending);
3959 pixman_region32_init(&constraint->region_pending);
3960 }
3961
3962 if (constraint->hint_is_pending) {
3963 constraint->hint_is_pending = false;
3964
3965 constraint->hint_is_pending = true;
3966 constraint->hint_x = constraint->hint_x_pending;
3967 constraint->hint_y = constraint->hint_y_pending;
3968 }
3969
3970 if (pointer_constraint_get_type(constraint) ==
3971 POINTER_CONSTRAINT_TYPE_CONFINE &&
3972 is_pointer_constraint_enabled(constraint))
3973 maybe_warp_confined_pointer(constraint);
3974}
3975
3976static struct weston_pointer_constraint *
3977weston_pointer_constraint_create(struct weston_surface *surface,
3978 struct weston_pointer *pointer,
3979 struct weston_region *region,
3980 enum zwp_pointer_constraints_v1_lifetime lifetime,
3981 struct wl_resource *cr,
3982 const struct weston_pointer_grab_interface *grab_interface)
3983{
3984 struct weston_pointer_constraint *constraint;
3985
3986 constraint = zalloc(sizeof *constraint);
3987 if (!constraint)
3988 return NULL;
3989
3990 constraint->lifetime = lifetime;
3991 pixman_region32_init(&constraint->region);
3992 pixman_region32_init(&constraint->region_pending);
3993 wl_list_insert(&surface->pointer_constraints, &constraint->link);
3994 constraint->surface = surface;
3995 constraint->pointer = pointer;
3996 constraint->resource = cr;
3997 constraint->grab.interface = grab_interface;
3998 if (region) {
3999 pixman_region32_copy(&constraint->region,
4000 &region->region);
4001 } else {
4002 pixman_region32_fini(&constraint->region);
4003 region_init_infinite(&constraint->region);
4004 }
4005
4006 constraint->surface_activate_listener.notify =
4007 pointer_constraint_surface_activate;
4008 constraint->surface_destroy_listener.notify =
4009 pointer_constraint_surface_destroyed;
4010 constraint->surface_commit_listener.notify =
4011 pointer_constraint_surface_committed;
4012 constraint->pointer_destroy_listener.notify =
4013 pointer_constraint_pointer_destroyed;
4014
4015 wl_signal_add(&surface->compositor->activate_signal,
4016 &constraint->surface_activate_listener);
4017 wl_signal_add(&pointer->destroy_signal,
4018 &constraint->pointer_destroy_listener);
4019 wl_signal_add(&surface->destroy_signal,
4020 &constraint->surface_destroy_listener);
4021 wl_signal_add(&surface->commit_signal,
4022 &constraint->surface_commit_listener);
4023
4024 return constraint;
4025}
4026
4027static void
4028init_pointer_constraint(struct wl_resource *pointer_constraints_resource,
4029 uint32_t id,
4030 struct weston_surface *surface,
4031 struct weston_pointer *pointer,
4032 struct weston_region *region,
4033 enum zwp_pointer_constraints_v1_lifetime lifetime,
4034 const struct wl_interface *interface,
4035 const void *implementation,
4036 const struct weston_pointer_grab_interface *grab_interface)
4037{
4038 struct wl_client *client =
4039 wl_resource_get_client(pointer_constraints_resource);
4040 struct wl_resource *cr;
4041 struct weston_pointer_constraint *constraint;
4042
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02004043 if (pointer && get_pointer_constraint_for_pointer(surface, pointer)) {
Jonas Ådahld3414f22016-07-22 17:56:31 +08004044 wl_resource_post_error(pointer_constraints_resource,
4045 ZWP_POINTER_CONSTRAINTS_V1_ERROR_ALREADY_CONSTRAINED,
4046 "the pointer has a lock/confine request on this surface");
4047 return;
4048 }
4049
4050 cr = wl_resource_create(client, interface,
4051 wl_resource_get_version(pointer_constraints_resource),
4052 id);
4053 if (cr == NULL) {
4054 wl_client_post_no_memory(client);
4055 return;
4056 }
4057
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02004058 if (pointer) {
4059 constraint = weston_pointer_constraint_create(surface, pointer,
4060 region, lifetime,
4061 cr, grab_interface);
4062 if (constraint == NULL) {
4063 wl_client_post_no_memory(client);
4064 return;
4065 }
4066 } else {
4067 constraint = NULL;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004068 }
4069
4070 wl_resource_set_implementation(cr, implementation, constraint,
4071 pointer_constraint_constrain_resource_destroyed);
4072
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02004073 if (constraint)
4074 maybe_enable_pointer_constraint(constraint);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004075}
4076
4077static void
4078pointer_constraints_destroy(struct wl_client *client,
4079 struct wl_resource *resource)
4080{
4081 wl_resource_destroy(resource);
4082}
4083
4084static void
4085locked_pointer_destroy(struct wl_client *client,
4086 struct wl_resource *resource)
4087{
4088 struct weston_pointer_constraint *constraint =
4089 wl_resource_get_user_data(resource);
4090 wl_fixed_t x, y;
4091
4092 if (constraint && constraint->view && constraint->hint_is_pending &&
4093 is_within_constraint_region(constraint,
4094 constraint->hint_x,
4095 constraint->hint_y)) {
4096 weston_view_to_global_fixed(constraint->view,
4097 constraint->hint_x,
4098 constraint->hint_y,
4099 &x, &y);
4100 weston_pointer_move_to(constraint->pointer, x, y);
4101 }
4102 wl_resource_destroy(resource);
4103}
4104
4105static void
4106locked_pointer_set_cursor_position_hint(struct wl_client *client,
4107 struct wl_resource *resource,
4108 wl_fixed_t surface_x,
4109 wl_fixed_t surface_y)
4110{
4111 struct weston_pointer_constraint *constraint =
4112 wl_resource_get_user_data(resource);
4113
4114 /* Ignore a set cursor hint that was sent after the lock was cancelled.
4115 */
4116 if (!constraint ||
4117 !constraint->resource ||
4118 constraint->resource != resource)
4119 return;
4120
4121 constraint->hint_is_pending = true;
4122 constraint->hint_x_pending = surface_x;
4123 constraint->hint_y_pending = surface_y;
4124}
4125
4126static void
4127locked_pointer_set_region(struct wl_client *client,
4128 struct wl_resource *resource,
4129 struct wl_resource *region_resource)
4130{
4131 struct weston_pointer_constraint *constraint =
4132 wl_resource_get_user_data(resource);
4133 struct weston_region *region = region_resource ?
4134 wl_resource_get_user_data(region_resource) : NULL;
4135
4136 if (!constraint)
4137 return;
4138
4139 if (region) {
4140 pixman_region32_copy(&constraint->region_pending,
4141 &region->region);
4142 } else {
4143 pixman_region32_fini(&constraint->region_pending);
4144 region_init_infinite(&constraint->region_pending);
4145 }
4146 constraint->region_is_pending = true;
4147}
4148
4149
4150static const struct zwp_locked_pointer_v1_interface locked_pointer_interface = {
4151 locked_pointer_destroy,
4152 locked_pointer_set_cursor_position_hint,
4153 locked_pointer_set_region,
4154};
4155
4156static void
4157pointer_constraints_lock_pointer(struct wl_client *client,
4158 struct wl_resource *resource,
4159 uint32_t id,
4160 struct wl_resource *surface_resource,
4161 struct wl_resource *pointer_resource,
4162 struct wl_resource *region_resource,
4163 uint32_t lifetime)
4164{
4165 struct weston_surface *surface =
4166 wl_resource_get_user_data(surface_resource);
4167 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
4168 struct weston_region *region = region_resource ?
4169 wl_resource_get_user_data(region_resource) : NULL;
4170
4171 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
4172 &zwp_locked_pointer_v1_interface,
4173 &locked_pointer_interface,
4174 &locked_pointer_grab_interface);
4175}
4176
4177static void
4178confined_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
4179{
4180}
4181
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004182static double
4183vec2d_cross_product(struct vec2d a, struct vec2d b)
4184{
4185 return a.x * b.y - a.y * b.x;
4186}
4187
4188static struct vec2d
4189vec2d_add(struct vec2d a, struct vec2d b)
4190{
4191 return (struct vec2d) {
4192 .x = a.x + b.x,
4193 .y = a.y + b.y,
4194 };
4195}
4196
4197static struct vec2d
4198vec2d_subtract(struct vec2d a, struct vec2d b)
4199{
4200 return (struct vec2d) {
4201 .x = a.x - b.x,
4202 .y = a.y - b.y,
4203 };
4204}
4205
4206static struct vec2d
4207vec2d_multiply_constant(double c, struct vec2d a)
4208{
4209 return (struct vec2d) {
4210 .x = c * a.x,
4211 .y = c * a.y,
4212 };
4213}
4214
4215static bool
4216lines_intersect(struct line *line1, struct line *line2,
4217 struct vec2d *intersection)
4218{
4219 struct vec2d p = line1->a;
4220 struct vec2d r = vec2d_subtract(line1->b, line1->a);
4221 struct vec2d q = line2->a;
4222 struct vec2d s = vec2d_subtract(line2->b, line2->a);
4223 double rxs;
4224 double sxr;
4225 double t;
4226 double u;
4227
4228 /*
4229 * The line (p, r) and (q, s) intersects where
4230 *
4231 * p + t r = q + u s
4232 *
4233 * Calculate t:
4234 *
4235 * (p + t r) × s = (q + u s) × s
4236 * p × s + t (r × s) = q × s + u (s × s)
4237 * p × s + t (r × s) = q × s
4238 * t (r × s) = q × s - p × s
4239 * t (r × s) = (q - p) × s
4240 * t = ((q - p) × s) / (r × s)
4241 *
4242 * Using the same method, for u we get:
4243 *
4244 * u = ((p - q) × r) / (s × r)
4245 */
4246
4247 rxs = vec2d_cross_product(r, s);
4248 sxr = vec2d_cross_product(s, r);
4249
4250 /* If r × s = 0 then the lines are either parallel or collinear. */
4251 if (fabs(rxs) < DBL_MIN)
4252 return false;
4253
4254 t = vec2d_cross_product(vec2d_subtract(q, p), s) / rxs;
4255 u = vec2d_cross_product(vec2d_subtract(p, q), r) / sxr;
4256
4257 /* The lines only intersect if 0 ≤ t ≤ 1 and 0 ≤ u ≤ 1. */
4258 if (t < 0.0 || t > 1.0 || u < 0.0 || u > 1.0)
4259 return false;
4260
4261 *intersection = vec2d_add(p, vec2d_multiply_constant(t, r));
4262 return true;
4263}
4264
4265static struct border *
4266add_border(struct wl_array *array,
4267 double x1, double y1,
4268 double x2, double y2,
4269 enum motion_direction blocking_dir)
4270{
4271 struct border *border = wl_array_add(array, sizeof *border);
4272
4273 *border = (struct border) {
4274 .line = (struct line) {
4275 .a = (struct vec2d) {
4276 .x = x1,
4277 .y = y1,
4278 },
4279 .b = (struct vec2d) {
4280 .x = x2,
4281 .y = y2,
4282 },
4283 },
4284 .blocking_dir = blocking_dir,
4285 };
4286
4287 return border;
4288}
4289
4290static int
4291compare_lines_x(const void *a, const void *b)
4292{
4293 const struct border *border_a = a;
4294 const struct border *border_b = b;
4295
4296
4297 if (border_a->line.a.x == border_b->line.a.x)
4298 return border_a->line.b.x < border_b->line.b.x;
4299 else
4300 return border_a->line.a.x > border_b->line.a.x;
4301}
4302
4303static void
4304add_non_overlapping_edges(pixman_box32_t *boxes,
4305 int band_above_start,
4306 int band_below_start,
4307 int band_below_end,
4308 struct wl_array *borders)
4309{
4310 int i;
4311 struct wl_array band_merge;
4312 struct border *border;
4313 struct border *prev_border;
4314 struct border *new_border;
4315
4316 wl_array_init(&band_merge);
4317
4318 /* Add bottom band of previous row, and top band of current row, and
4319 * sort them so lower left x coordinate comes first. If there are two
4320 * borders with the same left x coordinate, the wider one comes first.
4321 */
4322 for (i = band_above_start; i < band_below_start; i++) {
4323 pixman_box32_t *box = &boxes[i];
4324 add_border(&band_merge, box->x1, box->y2, box->x2, box->y2,
4325 MOTION_DIRECTION_POSITIVE_Y);
4326 }
4327 for (i = band_below_start; i < band_below_end; i++) {
4328 pixman_box32_t *box= &boxes[i];
4329 add_border(&band_merge, box->x1, box->y1, box->x2, box->y1,
4330 MOTION_DIRECTION_NEGATIVE_Y);
4331 }
4332 qsort(band_merge.data,
4333 band_merge.size / sizeof *border,
4334 sizeof *border,
4335 compare_lines_x);
4336
4337 /* Combine the two combined bands so that any overlapping border is
4338 * eliminated. */
4339 prev_border = NULL;
4340 wl_array_for_each(border, &band_merge) {
4341 assert(border->line.a.y == border->line.b.y);
4342 assert(!prev_border ||
4343 prev_border->line.a.y == border->line.a.y);
4344 assert(!prev_border ||
4345 (prev_border->line.a.x != border->line.a.x ||
4346 prev_border->line.b.x != border->line.b.x));
4347 assert(!prev_border ||
4348 prev_border->line.a.x <= border->line.a.x);
4349
4350 if (prev_border &&
4351 prev_border->line.a.x == border->line.a.x) {
4352 /*
4353 * ------------ +
4354 * ------- =
4355 * [ ]-----
4356 */
4357 prev_border->line.a.x = border->line.b.x;
4358 } else if (prev_border &&
4359 prev_border->line.b.x == border->line.b.x) {
4360 /*
4361 * ------------ +
4362 * ------ =
4363 * ------[ ]
4364 */
4365 prev_border->line.b.x = border->line.a.x;
4366 } else if (prev_border &&
4367 prev_border->line.b.x == border->line.a.x) {
4368 /*
4369 * -------- +
4370 * ------ =
4371 * --------------
4372 */
4373 prev_border->line.b.x = border->line.b.x;
4374 } else if (prev_border &&
4375 prev_border->line.b.x >= border->line.a.x) {
4376 /*
4377 * --------------- +
4378 * ------ =
4379 * -----[ ]----
4380 */
4381 new_border = add_border(borders,
4382 border->line.b.x,
4383 border->line.b.y,
4384 prev_border->line.b.x,
4385 prev_border->line.b.y,
4386 prev_border->blocking_dir);
4387 prev_border->line.b.x = border->line.a.x;
4388 prev_border = new_border;
4389 } else {
4390 assert(!prev_border ||
4391 prev_border->line.b.x < border->line.a.x);
4392 /*
4393 * First border or non-overlapping.
4394 *
4395 * ----- +
4396 * ----- =
4397 * ----- -----
4398 */
4399 new_border = wl_array_add(borders, sizeof *border);
4400 *new_border = *border;
4401 prev_border = new_border;
4402 }
4403 }
4404
4405 wl_array_release(&band_merge);
4406}
4407
4408static void
4409add_band_bottom_edges(pixman_box32_t *boxes,
4410 int band_start,
4411 int band_end,
4412 struct wl_array *borders)
4413{
4414 int i;
4415
4416 for (i = band_start; i < band_end; i++) {
4417 add_border(borders,
4418 boxes[i].x1, boxes[i].y2,
4419 boxes[i].x2, boxes[i].y2,
4420 MOTION_DIRECTION_POSITIVE_Y);
4421 }
4422}
4423
4424static void
4425region_to_outline(pixman_region32_t *region, struct wl_array *borders)
4426{
4427 pixman_box32_t *boxes;
4428 int num_boxes;
4429 int i;
4430 int top_most, bottom_most;
4431 int current_roof;
4432 int prev_top;
4433 int band_start, prev_band_start;
4434
4435 /*
4436 * Remove any overlapping lines from the set of rectangles. Note that
4437 * pixman regions are grouped as rows of rectangles, where rectangles
4438 * in one row never touch or overlap and are all of the same height.
4439 *
4440 * -------- --- -------- ---
4441 * | | | | | | | |
4442 * ----------====---- --- ----------- ----- ---
4443 * | | => | |
4444 * ----==========--------- ----- ----------
4445 * | | | |
4446 * ------------------- -------------------
4447 *
4448 */
4449
4450 boxes = pixman_region32_rectangles(region, &num_boxes);
4451 prev_top = 0;
4452 top_most = boxes[0].y1;
4453 current_roof = top_most;
4454 bottom_most = boxes[num_boxes - 1].y2;
4455 band_start = 0;
4456 prev_band_start = 0;
4457 for (i = 0; i < num_boxes; i++) {
4458 /* Detect if there is a vertical empty space, and add the lower
4459 * level of the previous band if so was the case. */
4460 if (i > 0 &&
4461 boxes[i].y1 != prev_top &&
4462 boxes[i].y1 != boxes[i - 1].y2) {
4463 current_roof = boxes[i].y1;
4464 add_band_bottom_edges(boxes,
4465 band_start,
4466 i,
4467 borders);
4468 }
4469
4470 /* Special case adding the last band, since it won't be handled
4471 * by the band change detection below. */
4472 if (boxes[i].y1 != current_roof && i == num_boxes - 1) {
4473 if (boxes[i].y1 != prev_top) {
4474 /* The last band is a single box, so we don't
4475 * have a prev_band_start to tell us when the
4476 * previous band started. */
4477 add_non_overlapping_edges(boxes,
4478 band_start,
4479 i,
4480 i + 1,
4481 borders);
4482 } else {
4483 add_non_overlapping_edges(boxes,
4484 prev_band_start,
4485 band_start,
4486 i + 1,
4487 borders);
4488 }
4489 }
4490
4491 /* Detect when passing a band and combine the top border of the
4492 * just passed band with the bottom band of the previous band.
4493 */
4494 if (boxes[i].y1 != top_most && boxes[i].y1 != prev_top) {
4495 /* Combine the two passed bands. */
4496 if (prev_top != current_roof) {
4497 add_non_overlapping_edges(boxes,
4498 prev_band_start,
4499 band_start,
4500 i,
4501 borders);
4502 }
4503
4504 prev_band_start = band_start;
4505 band_start = i;
4506 }
4507
4508 /* Add the top border if the box is part of the current roof. */
4509 if (boxes[i].y1 == current_roof) {
4510 add_border(borders,
4511 boxes[i].x1, boxes[i].y1,
4512 boxes[i].x2, boxes[i].y1,
4513 MOTION_DIRECTION_NEGATIVE_Y);
4514 }
4515
4516 /* Add the bottom border of the last band. */
4517 if (boxes[i].y2 == bottom_most) {
4518 add_border(borders,
4519 boxes[i].x1, boxes[i].y2,
4520 boxes[i].x2, boxes[i].y2,
4521 MOTION_DIRECTION_POSITIVE_Y);
4522 }
4523
4524 /* Always add the left border. */
4525 add_border(borders,
4526 boxes[i].x1, boxes[i].y1,
4527 boxes[i].x1, boxes[i].y2,
4528 MOTION_DIRECTION_NEGATIVE_X);
4529
4530 /* Always add the right border. */
4531 add_border(borders,
4532 boxes[i].x2, boxes[i].y1,
4533 boxes[i].x2, boxes[i].y2,
4534 MOTION_DIRECTION_POSITIVE_X);
4535
4536 prev_top = boxes[i].y1;
4537 }
4538}
4539
4540static bool
4541is_border_horizontal (struct border *border)
4542{
4543 return border->line.a.y == border->line.b.y;
4544}
4545
4546static bool
4547is_border_blocking_directions(struct border *border,
4548 uint32_t directions)
4549{
4550 /* Don't block parallel motions. */
4551 if (is_border_horizontal(border)) {
4552 if ((directions & (MOTION_DIRECTION_POSITIVE_Y |
4553 MOTION_DIRECTION_NEGATIVE_Y)) == 0)
4554 return false;
4555 } else {
4556 if ((directions & (MOTION_DIRECTION_POSITIVE_X |
4557 MOTION_DIRECTION_NEGATIVE_X)) == 0)
4558 return false;
4559 }
4560
4561 return (~border->blocking_dir & directions) != directions;
4562}
4563
4564static struct border *
4565get_closest_border(struct wl_array *borders,
4566 struct line *motion,
4567 uint32_t directions)
4568{
4569 struct border *border;
4570 struct vec2d intersection;
4571 struct vec2d delta;
4572 double distance_2;
4573 struct border *closest_border = NULL;
4574 double closest_distance_2 = DBL_MAX;
4575
4576 wl_array_for_each(border, borders) {
4577 if (!is_border_blocking_directions(border, directions))
4578 continue;
4579
4580 if (!lines_intersect(&border->line, motion, &intersection))
4581 continue;
4582
4583 delta = vec2d_subtract(intersection, motion->a);
4584 distance_2 = delta.x*delta.x + delta.y*delta.y;
4585 if (distance_2 < closest_distance_2) {
4586 closest_border = border;
4587 closest_distance_2 = distance_2;
4588 }
4589 }
4590
4591 return closest_border;
4592}
4593
4594static void
4595clamp_to_border(struct border *border,
4596 struct line *motion,
4597 uint32_t *motion_dir)
4598{
4599 /*
4600 * When clamping either rightward or downward motions, the motion needs
4601 * to be clamped so that the destination coordinate does not end up on
4602 * the border (see weston_pointer_clamp_event_to_region). Do this by
4603 * clamping such motions to the border minus the smallest possible
4604 * wl_fixed_t value.
4605 */
4606 if (is_border_horizontal(border)) {
4607 if (*motion_dir & MOTION_DIRECTION_POSITIVE_Y)
4608 motion->b.y = border->line.a.y - wl_fixed_to_double(1);
4609 else
4610 motion->b.y = border->line.a.y;
4611 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_Y |
4612 MOTION_DIRECTION_NEGATIVE_Y);
4613 } else {
4614 if (*motion_dir & MOTION_DIRECTION_POSITIVE_X)
4615 motion->b.x = border->line.a.x - wl_fixed_to_double(1);
4616 else
4617 motion->b.x = border->line.a.x;
4618 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_X |
4619 MOTION_DIRECTION_NEGATIVE_X);
4620 }
4621}
4622
4623static uint32_t
4624get_motion_directions(struct line *motion)
4625{
4626 uint32_t directions = 0;
4627
4628 if (motion->a.x < motion->b.x)
4629 directions |= MOTION_DIRECTION_POSITIVE_X;
4630 else if (motion->a.x > motion->b.x)
4631 directions |= MOTION_DIRECTION_NEGATIVE_X;
4632 if (motion->a.y < motion->b.y)
4633 directions |= MOTION_DIRECTION_POSITIVE_Y;
4634 else if (motion->a.y > motion->b.y)
4635 directions |= MOTION_DIRECTION_NEGATIVE_Y;
4636
4637 return directions;
4638}
4639
Jonas Ådahld3414f22016-07-22 17:56:31 +08004640static void
4641weston_pointer_clamp_event_to_region(struct weston_pointer *pointer,
4642 struct weston_pointer_motion_event *event,
4643 pixman_region32_t *region,
4644 wl_fixed_t *clamped_x,
4645 wl_fixed_t *clamped_y)
4646{
4647 wl_fixed_t x, y;
4648 wl_fixed_t sx, sy;
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004649 wl_fixed_t old_sx = pointer->sx;
4650 wl_fixed_t old_sy = pointer->sy;
4651 struct wl_array borders;
4652 struct line motion;
4653 struct border *closest_border;
4654 float new_x_f, new_y_f;
4655 uint32_t directions;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004656
4657 weston_pointer_motion_to_abs(pointer, event, &x, &y);
4658 weston_view_from_global_fixed(pointer->focus, x, y, &sx, &sy);
4659
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004660 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004661
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004662 /*
4663 * Generate borders given the confine region we are to use. The borders
4664 * are defined to be the outer region of the allowed area. This means
4665 * top/left borders are "within" the allowed area, while bottom/right
4666 * borders are outside. This needs to be considered when clamping
4667 * confined motion vectors.
4668 */
4669 region_to_outline(region, &borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004670
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004671 motion = (struct line) {
4672 .a = (struct vec2d) {
4673 .x = wl_fixed_to_double(old_sx),
4674 .y = wl_fixed_to_double(old_sy),
4675 },
4676 .b = (struct vec2d) {
4677 .x = wl_fixed_to_double(sx),
4678 .y = wl_fixed_to_double(sy),
4679 },
4680 };
4681 directions = get_motion_directions(&motion);
4682
4683 while (directions) {
4684 closest_border = get_closest_border(&borders,
4685 &motion,
4686 directions);
4687 if (closest_border)
4688 clamp_to_border(closest_border, &motion, &directions);
4689 else
4690 break;
4691 }
4692
4693 weston_view_to_global_float(pointer->focus,
4694 (float) motion.b.x, (float) motion.b.y,
4695 &new_x_f, &new_y_f);
4696 *clamped_x = wl_fixed_from_double(new_x_f);
4697 *clamped_y = wl_fixed_from_double(new_y_f);
4698
4699 wl_array_release(&borders);
4700}
4701
4702static double
4703point_to_border_distance_2(struct border *border, double x, double y)
4704{
4705 double orig_x, orig_y;
4706 double dx, dy;
4707
4708 if (is_border_horizontal(border)) {
4709 if (x < border->line.a.x)
4710 orig_x = border->line.a.x;
4711 else if (x > border->line.b.x)
4712 orig_x = border->line.b.x;
4713 else
4714 orig_x = x;
4715 orig_y = border->line.a.y;
4716 } else {
4717 if (y < border->line.a.y)
4718 orig_y = border->line.a.y;
4719 else if (y > border->line.b.y)
4720 orig_y = border->line.b.y;
4721 else
4722 orig_y = y;
4723 orig_x = border->line.a.x;
4724 }
4725
4726
4727 dx = fabs(orig_x - x);
4728 dy = fabs(orig_y - y);
4729 return dx*dx + dy*dy;
4730}
4731
4732static void
4733warp_to_behind_border(struct border *border, wl_fixed_t *sx, wl_fixed_t *sy)
4734{
4735 switch (border->blocking_dir) {
4736 case MOTION_DIRECTION_POSITIVE_X:
4737 case MOTION_DIRECTION_NEGATIVE_X:
4738 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_X)
4739 *sx = wl_fixed_from_double(border->line.a.x) - 1;
4740 else
4741 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4742 if (*sy < wl_fixed_from_double(border->line.a.y))
4743 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4744 else if (*sy > wl_fixed_from_double(border->line.b.y))
4745 *sy = wl_fixed_from_double(border->line.b.y) - 1;
4746 break;
4747 case MOTION_DIRECTION_POSITIVE_Y:
4748 case MOTION_DIRECTION_NEGATIVE_Y:
4749 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_Y)
4750 *sy = wl_fixed_from_double(border->line.a.y) - 1;
4751 else
4752 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4753 if (*sx < wl_fixed_from_double(border->line.a.x))
4754 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4755 else if (*sx > wl_fixed_from_double(border->line.b.x))
4756 *sx = wl_fixed_from_double(border->line.b.x) - 1;
4757 break;
4758 }
Jonas Ådahld3414f22016-07-22 17:56:31 +08004759}
4760
4761static void
4762maybe_warp_confined_pointer(struct weston_pointer_constraint *constraint)
4763{
4764 wl_fixed_t x;
4765 wl_fixed_t y;
4766 wl_fixed_t sx;
4767 wl_fixed_t sy;
4768
4769 weston_view_from_global_fixed(constraint->view,
4770 constraint->pointer->x,
4771 constraint->pointer->y,
4772 &sx,
4773 &sy);
4774
4775 if (!is_within_constraint_region(constraint, sx, sy)) {
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004776 double xf = wl_fixed_to_double(sx);
4777 double yf = wl_fixed_to_double(sy);
4778 pixman_region32_t confine_region;
4779 struct wl_array borders;
4780 struct border *border;
4781 double closest_distance_2 = DBL_MAX;
4782 struct border *closest_border = NULL;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004783
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004784 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004785
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004786 pixman_region32_init(&confine_region);
4787 pixman_region32_intersect(&confine_region,
4788 &constraint->view->surface->input,
4789 &constraint->region);
4790 region_to_outline(&confine_region, &borders);
4791 pixman_region32_fini(&confine_region);
4792
4793 wl_array_for_each(border, &borders) {
4794 double distance_2;
4795
4796 distance_2 = point_to_border_distance_2(border, xf, yf);
4797 if (distance_2 < closest_distance_2) {
4798 closest_border = border;
4799 closest_distance_2 = distance_2;
4800 }
4801 }
4802 assert(closest_border);
4803
4804 warp_to_behind_border(closest_border, &sx, &sy);
4805
4806 wl_array_release(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004807
4808 weston_view_to_global_fixed(constraint->view, sx, sy, &x, &y);
4809 weston_pointer_move_to(constraint->pointer, x, y);
4810 }
4811}
4812
4813static void
4814confined_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02004815 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004816 struct weston_pointer_motion_event *event)
4817{
4818 struct weston_pointer_constraint *constraint =
4819 container_of(grab, struct weston_pointer_constraint, grab);
4820 struct weston_pointer *pointer = grab->pointer;
4821 struct weston_surface *surface;
4822 wl_fixed_t x, y;
4823 wl_fixed_t old_sx = pointer->sx;
4824 wl_fixed_t old_sy = pointer->sy;
4825 pixman_region32_t confine_region;
4826
4827 assert(pointer->focus);
4828 assert(pointer->focus->surface == constraint->surface);
4829
4830 surface = pointer->focus->surface;
4831
4832 pixman_region32_init(&confine_region);
4833 pixman_region32_intersect(&confine_region,
4834 &surface->input,
4835 &constraint->region);
4836 weston_pointer_clamp_event_to_region(pointer, event,
4837 &confine_region, &x, &y);
4838 weston_pointer_move_to(pointer, x, y);
4839 pixman_region32_fini(&confine_region);
4840
4841 weston_view_from_global_fixed(pointer->focus, x, y,
4842 &pointer->sx, &pointer->sy);
4843
4844 if (old_sx != pointer->sx || old_sy != pointer->sy) {
Quentin Glidiccde13452016-08-12 10:41:32 +02004845 pointer_send_motion(pointer, time,
4846 pointer->sx, pointer->sy);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004847 }
4848
Quentin Glidiccde13452016-08-12 10:41:32 +02004849 pointer_send_relative_motion(pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004850}
4851
4852static void
4853confined_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02004854 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004855 uint32_t button,
4856 uint32_t state_w)
4857{
4858 weston_pointer_send_button(grab->pointer, time, button, state_w);
4859}
4860
4861static void
4862confined_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02004863 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004864 struct weston_pointer_axis_event *event)
4865{
4866 weston_pointer_send_axis(grab->pointer, time, event);
4867}
4868
4869static void
4870confined_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
4871 uint32_t source)
4872{
4873 weston_pointer_send_axis_source(grab->pointer, source);
4874}
4875
4876static void
4877confined_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
4878{
4879 weston_pointer_send_frame(grab->pointer);
4880}
4881
4882static void
4883confined_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
4884{
4885 struct weston_pointer_constraint *constraint =
4886 container_of(grab, struct weston_pointer_constraint, grab);
4887
4888 disable_pointer_constraint(constraint);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004889}
4890
4891static const struct weston_pointer_grab_interface
4892 confined_pointer_grab_interface = {
4893 confined_pointer_grab_pointer_focus,
4894 confined_pointer_grab_pointer_motion,
4895 confined_pointer_grab_pointer_button,
4896 confined_pointer_grab_pointer_axis,
4897 confined_pointer_grab_pointer_axis_source,
4898 confined_pointer_grab_pointer_frame,
4899 confined_pointer_grab_pointer_cancel,
4900};
4901
4902static void
4903confined_pointer_destroy(struct wl_client *client,
4904 struct wl_resource *resource)
4905{
4906 wl_resource_destroy(resource);
4907}
4908
4909static void
4910confined_pointer_set_region(struct wl_client *client,
4911 struct wl_resource *resource,
4912 struct wl_resource *region_resource)
4913{
4914 struct weston_pointer_constraint *constraint =
4915 wl_resource_get_user_data(resource);
4916 struct weston_region *region = region_resource ?
4917 wl_resource_get_user_data(region_resource) : NULL;
4918
4919 if (!constraint)
4920 return;
4921
4922 if (region) {
4923 pixman_region32_copy(&constraint->region_pending,
4924 &region->region);
4925 } else {
4926 pixman_region32_fini(&constraint->region_pending);
4927 region_init_infinite(&constraint->region_pending);
4928 }
4929 constraint->region_is_pending = true;
4930}
4931
4932static const struct zwp_confined_pointer_v1_interface confined_pointer_interface = {
4933 confined_pointer_destroy,
4934 confined_pointer_set_region,
4935};
4936
4937static void
4938pointer_constraints_confine_pointer(struct wl_client *client,
4939 struct wl_resource *resource,
4940 uint32_t id,
4941 struct wl_resource *surface_resource,
4942 struct wl_resource *pointer_resource,
4943 struct wl_resource *region_resource,
4944 uint32_t lifetime)
4945{
4946 struct weston_surface *surface =
4947 wl_resource_get_user_data(surface_resource);
4948 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
4949 struct weston_region *region = region_resource ?
4950 wl_resource_get_user_data(region_resource) : NULL;
4951
4952 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
4953 &zwp_confined_pointer_v1_interface,
4954 &confined_pointer_interface,
4955 &confined_pointer_grab_interface);
4956}
4957
4958static const struct zwp_pointer_constraints_v1_interface pointer_constraints_interface = {
4959 pointer_constraints_destroy,
4960 pointer_constraints_lock_pointer,
4961 pointer_constraints_confine_pointer,
4962};
4963
4964static void
4965bind_pointer_constraints(struct wl_client *client, void *data,
4966 uint32_t version, uint32_t id)
4967{
4968 struct wl_resource *resource;
4969
4970 resource = wl_resource_create(client,
4971 &zwp_pointer_constraints_v1_interface,
4972 1, id);
4973
4974 wl_resource_set_implementation(resource, &pointer_constraints_interface,
4975 NULL, NULL);
4976}
4977
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004978static void
Alexandros Frantzis2b442482018-02-20 14:05:50 +02004979input_timestamps_destroy(struct wl_client *client,
4980 struct wl_resource *resource)
4981{
4982 wl_resource_destroy(resource);
4983}
4984
4985static const struct zwp_input_timestamps_v1_interface
4986 input_timestamps_interface = {
4987 input_timestamps_destroy,
4988};
4989
4990static void
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004991input_timestamps_manager_destroy(struct wl_client *client,
4992 struct wl_resource *resource)
4993{
4994 wl_resource_destroy(resource);
4995}
4996
4997static void
4998input_timestamps_manager_get_keyboard_timestamps(struct wl_client *client,
4999 struct wl_resource *resource,
5000 uint32_t id,
5001 struct wl_resource *keyboard_resource)
5002{
Alexandros Frantzis2b442482018-02-20 14:05:50 +02005003 struct weston_keyboard *keyboard =
5004 wl_resource_get_user_data(keyboard_resource);
5005 struct wl_resource *input_ts;
5006
5007 input_ts = wl_resource_create(client,
5008 &zwp_input_timestamps_v1_interface,
5009 1, id);
5010 if (!input_ts) {
5011 wl_client_post_no_memory(client);
5012 return;
5013 }
5014
5015 if (keyboard) {
5016 wl_list_insert(&keyboard->timestamps_list,
5017 wl_resource_get_link(input_ts));
5018 } else {
5019 wl_list_init(wl_resource_get_link(input_ts));
5020 }
5021
5022 wl_resource_set_implementation(input_ts,
5023 &input_timestamps_interface,
5024 keyboard_resource,
5025 unbind_resource);
Alexandros Frantzis538749d2018-02-16 18:44:16 +02005026}
5027
5028static void
5029input_timestamps_manager_get_pointer_timestamps(struct wl_client *client,
5030 struct wl_resource *resource,
5031 uint32_t id,
5032 struct wl_resource *pointer_resource)
5033{
Alexandros Frantzisdb907b72018-02-20 14:06:26 +02005034 struct weston_pointer *pointer =
5035 wl_resource_get_user_data(pointer_resource);
5036 struct wl_resource *input_ts;
5037
5038 input_ts = wl_resource_create(client,
5039 &zwp_input_timestamps_v1_interface,
5040 1, id);
5041 if (!input_ts) {
5042 wl_client_post_no_memory(client);
5043 return;
5044 }
5045
5046 if (pointer) {
5047 wl_list_insert(&pointer->timestamps_list,
5048 wl_resource_get_link(input_ts));
5049 } else {
5050 wl_list_init(wl_resource_get_link(input_ts));
5051 }
5052
5053 wl_resource_set_implementation(input_ts,
5054 &input_timestamps_interface,
5055 pointer_resource,
5056 unbind_resource);
Alexandros Frantzis538749d2018-02-16 18:44:16 +02005057}
5058
5059static void
5060input_timestamps_manager_get_touch_timestamps(struct wl_client *client,
5061 struct wl_resource *resource,
5062 uint32_t id,
5063 struct wl_resource *touch_resource)
5064{
Alexandros Frantzisd7157842018-02-20 14:07:03 +02005065 struct weston_touch *touch = wl_resource_get_user_data(touch_resource);
5066 struct wl_resource *input_ts;
5067
5068 input_ts = wl_resource_create(client,
5069 &zwp_input_timestamps_v1_interface,
5070 1, id);
5071 if (!input_ts) {
5072 wl_client_post_no_memory(client);
5073 return;
5074 }
5075
5076 if (touch) {
5077 wl_list_insert(&touch->timestamps_list,
5078 wl_resource_get_link(input_ts));
5079 } else {
5080 wl_list_init(wl_resource_get_link(input_ts));
5081 }
5082
5083 wl_resource_set_implementation(input_ts,
5084 &input_timestamps_interface,
5085 touch_resource,
5086 unbind_resource);
Alexandros Frantzis538749d2018-02-16 18:44:16 +02005087}
5088
5089static const struct zwp_input_timestamps_manager_v1_interface
5090 input_timestamps_manager_interface = {
5091 input_timestamps_manager_destroy,
5092 input_timestamps_manager_get_keyboard_timestamps,
5093 input_timestamps_manager_get_pointer_timestamps,
5094 input_timestamps_manager_get_touch_timestamps,
5095};
5096
5097static void
5098bind_input_timestamps_manager(struct wl_client *client, void *data,
5099 uint32_t version, uint32_t id)
5100{
5101 struct wl_resource *resource =
5102 wl_resource_create(client,
5103 &zwp_input_timestamps_manager_v1_interface,
5104 1, id);
5105
5106 if (resource == NULL) {
5107 wl_client_post_no_memory(client);
5108 return;
5109 }
5110
5111 wl_resource_set_implementation(resource,
5112 &input_timestamps_manager_interface,
5113 NULL, NULL);
5114}
5115
Jonas Ådahl30d61d82014-10-22 21:21:17 +02005116int
5117weston_input_init(struct weston_compositor *compositor)
5118{
5119 if (!wl_global_create(compositor->wl_display,
5120 &zwp_relative_pointer_manager_v1_interface, 1,
5121 compositor, bind_relative_pointer_manager))
5122 return -1;
5123
Jonas Ådahld3414f22016-07-22 17:56:31 +08005124 if (!wl_global_create(compositor->wl_display,
5125 &zwp_pointer_constraints_v1_interface, 1,
5126 NULL, bind_pointer_constraints))
5127 return -1;
5128
Alexandros Frantzis538749d2018-02-16 18:44:16 +02005129 if (!wl_global_create(compositor->wl_display,
5130 &zwp_input_timestamps_manager_v1_interface, 1,
5131 NULL, bind_input_timestamps_manager))
5132 return -1;
5133
Jonas Ådahl30d61d82014-10-22 21:21:17 +02005134 return 0;
5135}