blob: 8f6abd51ffe3882a272007c825fb6f9c65b3ed2c [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,
526 &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,
612 &pointer->timestamps_list,
613 time);
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200614 wl_pointer_send_button(resource, serial, msecs, button, state);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200615 }
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800616}
617
618static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700619default_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200620 const struct timespec *time, uint32_t button,
Quentin Glidiccde13452016-08-12 10:41:32 +0200621 enum wl_pointer_button_state state)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400622{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400623 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400624 struct weston_compositor *compositor = pointer->seat->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500625 struct weston_view *view;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400626 wl_fixed_t sx, sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400627
Quentin Glidiccde13452016-08-12 10:41:32 +0200628 weston_pointer_send_button(pointer, time, button, state);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400629
630 if (pointer->button_count == 0 &&
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400631 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500632 view = weston_compositor_pick_view(compositor,
633 pointer->x, pointer->y,
634 &sx, &sy);
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400635
Jason Ekstranda7af7042013-10-12 22:38:11 -0500636 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400637 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400638}
639
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200640/** Send wl_pointer.axis events to focused resources.
641 *
642 * \param pointer The pointer where the axis events originates from.
643 * \param time The timestamp of the event
Marius Vlada2dace22019-06-12 16:05:44 +0300644 * \param event The axis value of the event
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200645 *
646 * For every resource that is currently in focus, send a wl_pointer.axis event
647 * with the passed parameters. The focused resources are the wl_pointer
648 * resources of the client which currently has the surface with pointer focus.
649 */
650WL_EXPORT void
651weston_pointer_send_axis(struct weston_pointer *pointer,
Alexandros Frantzis80321942017-11-16 18:20:56 +0200652 const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +1000653 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200654{
655 struct wl_resource *resource;
656 struct wl_list *resource_list;
Alexandros Frantzis80321942017-11-16 18:20:56 +0200657 uint32_t msecs;
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200658
Quentin Glidiccde13452016-08-12 10:41:32 +0200659 if (!weston_pointer_has_focus_resource(pointer))
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800660 return;
661
662 resource_list = &pointer->focus_client->pointer_resources;
Alexandros Frantzis80321942017-11-16 18:20:56 +0200663 msecs = timespec_to_msec(time);
Peter Hutterer87743e92016-01-18 16:38:22 +1000664 wl_resource_for_each(resource, resource_list) {
665 if (event->has_discrete &&
666 wl_resource_get_version(resource) >=
667 WL_POINTER_AXIS_DISCRETE_SINCE_VERSION)
668 wl_pointer_send_axis_discrete(resource, event->axis,
669 event->discrete);
670
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200671 if (event->value) {
672 send_timestamps_for_input_resource(resource,
673 &pointer->timestamps_list,
674 time);
Alexandros Frantzis80321942017-11-16 18:20:56 +0200675 wl_pointer_send_axis(resource, msecs,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200676 event->axis,
677 wl_fixed_from_double(event->value));
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200678 } else if (wl_resource_get_version(resource) >=
679 WL_POINTER_AXIS_STOP_SINCE_VERSION) {
680 send_timestamps_for_input_resource(resource,
681 &pointer->timestamps_list,
682 time);
Alexandros Frantzis80321942017-11-16 18:20:56 +0200683 wl_pointer_send_axis_stop(resource, msecs,
Peter Hutterer87743e92016-01-18 16:38:22 +1000684 event->axis);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200685 }
Peter Hutterer87743e92016-01-18 16:38:22 +1000686 }
687}
688
Quentin Glidiccde13452016-08-12 10:41:32 +0200689/** Send wl_pointer.axis_source events to focused resources.
690 *
691 * \param pointer The pointer where the axis_source events originates from.
692 * \param source The axis_source enum value of the event
693 *
694 * For every resource that is currently in focus, send a wl_pointer.axis_source
695 * event with the passed parameter. The focused resources are the wl_pointer
696 * resources of the client which currently has the surface with pointer focus.
697 */
Peter Hutterer87743e92016-01-18 16:38:22 +1000698WL_EXPORT void
Quentin Glidiccde13452016-08-12 10:41:32 +0200699weston_pointer_send_axis_source(struct weston_pointer *pointer,
700 enum wl_pointer_axis_source source)
Peter Hutterer87743e92016-01-18 16:38:22 +1000701{
702 struct wl_resource *resource;
703 struct wl_list *resource_list;
704
Quentin Glidiccde13452016-08-12 10:41:32 +0200705 if (!weston_pointer_has_focus_resource(pointer))
Jonas Ådahled6014a2016-04-21 10:21:48 +0800706 return;
707
Peter Hutterer87743e92016-01-18 16:38:22 +1000708 resource_list = &pointer->focus_client->pointer_resources;
709 wl_resource_for_each(resource, resource_list) {
710 if (wl_resource_get_version(resource) >=
711 WL_POINTER_AXIS_SOURCE_SINCE_VERSION) {
712 wl_pointer_send_axis_source(resource, source);
713 }
714 }
715}
716
717static void
718pointer_send_frame(struct wl_resource *resource)
719{
720 if (wl_resource_get_version(resource) >=
721 WL_POINTER_FRAME_SINCE_VERSION) {
722 wl_pointer_send_frame(resource);
723 }
724}
725
Quentin Glidiccde13452016-08-12 10:41:32 +0200726/** Send wl_pointer.frame events to focused resources.
727 *
728 * \param pointer The pointer where the frame events originates from.
729 *
730 * For every resource that is currently in focus, send a wl_pointer.frame event.
731 * The focused resources are the wl_pointer resources of the client which
732 * currently has the surface with pointer focus.
733 */
Peter Hutterer87743e92016-01-18 16:38:22 +1000734WL_EXPORT void
735weston_pointer_send_frame(struct weston_pointer *pointer)
736{
737 struct wl_resource *resource;
738 struct wl_list *resource_list;
739
Quentin Glidiccde13452016-08-12 10:41:32 +0200740 if (!weston_pointer_has_focus_resource(pointer))
Derek Foreman8efa31b2016-01-29 10:29:46 -0600741 return;
742
Peter Hutterer87743e92016-01-18 16:38:22 +1000743 resource_list = &pointer->focus_client->pointer_resources;
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200744 wl_resource_for_each(resource, resource_list)
Peter Hutterer87743e92016-01-18 16:38:22 +1000745 pointer_send_frame(resource);
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200746}
747
748static void
749default_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +0200750 const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +1000751 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200752{
Peter Hutterer89b6a492016-01-18 15:58:17 +1000753 weston_pointer_send_axis(grab->pointer, time, event);
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200754}
755
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200756static void
Peter Hutterer87743e92016-01-18 16:38:22 +1000757default_grab_pointer_axis_source(struct weston_pointer_grab *grab,
Quentin Glidiccde13452016-08-12 10:41:32 +0200758 enum wl_pointer_axis_source source)
Peter Hutterer87743e92016-01-18 16:38:22 +1000759{
760 weston_pointer_send_axis_source(grab->pointer, source);
761}
762
763static void
764default_grab_pointer_frame(struct weston_pointer_grab *grab)
765{
766 weston_pointer_send_frame(grab->pointer);
767}
768
769static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200770default_grab_pointer_cancel(struct weston_pointer_grab *grab)
771{
772}
773
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400774static const struct weston_pointer_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400775 default_pointer_grab_interface = {
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700776 default_grab_pointer_focus,
777 default_grab_pointer_motion,
778 default_grab_pointer_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200779 default_grab_pointer_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +1000780 default_grab_pointer_axis_source,
781 default_grab_pointer_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200782 default_grab_pointer_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400783};
784
Quentin Glidiccde13452016-08-12 10:41:32 +0200785/** Check if the touch has focused resources.
786 *
787 * \param touch The touch to check for focused resources.
788 * \return Whether or not this touch has focused resources
789 */
790WL_EXPORT bool
791weston_touch_has_focus_resource(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400792{
Quentin Glidiccde13452016-08-12 10:41:32 +0200793 if (!touch->focus)
794 return false;
795
796 if (wl_list_empty(&touch->focus_resource_list))
797 return false;
798
799 return true;
800}
801
802/** Send wl_touch.down events to focused resources.
803 *
804 * \param touch The touch where the down events originates from.
805 * \param time The timestamp of the event
806 * \param touch_id The touch_id value of the event
807 * \param x The x value of the event
808 * \param y The y value of the event
809 *
810 * For every resource that is currently in focus, send a wl_touch.down event
811 * with the passed parameters. The focused resources are the wl_touch
812 * resources of the client which currently has the surface with touch focus.
813 */
814WL_EXPORT void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200815weston_touch_send_down(struct weston_touch *touch, const struct timespec *time,
Quentin Glidiccde13452016-08-12 10:41:32 +0200816 int touch_id, wl_fixed_t x, wl_fixed_t y)
817{
Rob Bradford880ebc72013-07-22 17:31:38 +0100818 struct wl_display *display = touch->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400819 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100820 struct wl_resource *resource;
821 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300822 wl_fixed_t sx, sy;
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200823 uint32_t msecs;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300824
Quentin Glidiccde13452016-08-12 10:41:32 +0200825 if (!weston_touch_has_focus_resource(touch))
Bryce Harrington2c40d1d2016-02-02 10:18:48 -0800826 return;
827
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300828 weston_view_from_global_fixed(touch->focus, x, y, &sx, &sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400829
Neil Roberts96d790e2013-09-19 17:32:00 +0100830 resource_list = &touch->focus_resource_list;
Quentin Glidiccde13452016-08-12 10:41:32 +0200831 serial = wl_display_next_serial(display);
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200832 msecs = timespec_to_msec(time);
Alexandros Frantzisd7157842018-02-20 14:07:03 +0200833 wl_resource_for_each(resource, resource_list) {
834 send_timestamps_for_input_resource(resource,
835 &touch->timestamps_list,
836 time);
837 wl_touch_send_down(resource, serial, msecs,
838 touch->focus->surface->resource,
839 touch_id, sx, sy);
840 }
Quentin Glidiccde13452016-08-12 10:41:32 +0200841}
Neil Roberts96d790e2013-09-19 17:32:00 +0100842
Quentin Glidiccde13452016-08-12 10:41:32 +0200843static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200844default_grab_touch_down(struct weston_touch_grab *grab,
845 const struct timespec *time, int touch_id,
846 wl_fixed_t x, wl_fixed_t y)
Quentin Glidiccde13452016-08-12 10:41:32 +0200847{
848 weston_touch_send_down(grab->touch, time, touch_id, x, y);
849}
850
851/** Send wl_touch.up events to focused resources.
852 *
853 * \param touch The touch where the up events originates from.
854 * \param time The timestamp of the event
855 * \param touch_id The touch_id value of the event
856 *
857 * For every resource that is currently in focus, send a wl_touch.up event
858 * with the passed parameters. The focused resources are the wl_touch
859 * resources of the client which currently has the surface with touch focus.
860 */
861WL_EXPORT void
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200862weston_touch_send_up(struct weston_touch *touch, const struct timespec *time,
863 int touch_id)
Quentin Glidiccde13452016-08-12 10:41:32 +0200864{
865 struct wl_display *display = touch->seat->compositor->wl_display;
866 uint32_t serial;
867 struct wl_resource *resource;
868 struct wl_list *resource_list;
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200869 uint32_t msecs;
Quentin Glidiccde13452016-08-12 10:41:32 +0200870
871 if (!weston_touch_has_focus_resource(touch))
872 return;
873
874 resource_list = &touch->focus_resource_list;
875 serial = wl_display_next_serial(display);
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200876 msecs = timespec_to_msec(time);
Alexandros Frantzisd7157842018-02-20 14:07:03 +0200877 wl_resource_for_each(resource, resource_list) {
878 send_timestamps_for_input_resource(resource,
879 &touch->timestamps_list,
880 time);
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200881 wl_touch_send_up(resource, serial, msecs, touch_id);
Alexandros Frantzisd7157842018-02-20 14:07:03 +0200882 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400883}
884
Kristian Høgsberge329f362013-05-06 22:19:57 -0400885static void
886default_grab_touch_up(struct weston_touch_grab *grab,
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200887 const struct timespec *time, int touch_id)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400888{
Quentin Glidiccde13452016-08-12 10:41:32 +0200889 weston_touch_send_up(grab->touch, time, touch_id);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400890}
891
Quentin Glidiccde13452016-08-12 10:41:32 +0200892/** Send wl_touch.motion events to focused resources.
893 *
894 * \param touch The touch where the motion events originates from.
895 * \param time The timestamp of the event
896 * \param touch_id The touch_id value of the event
897 * \param x The x value of the event
898 * \param y The y value of the event
899 *
900 * For every resource that is currently in focus, send a wl_touch.motion event
901 * with the passed parameters. The focused resources are the wl_touch
902 * resources of the client which currently has the surface with touch focus.
903 */
904WL_EXPORT void
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200905weston_touch_send_motion(struct weston_touch *touch,
906 const struct timespec *time, int touch_id,
907 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400908{
Neil Roberts96d790e2013-09-19 17:32:00 +0100909 struct wl_resource *resource;
910 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300911 wl_fixed_t sx, sy;
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200912 uint32_t msecs;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300913
Quentin Glidiccde13452016-08-12 10:41:32 +0200914 if (!weston_touch_has_focus_resource(touch))
915 return;
916
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300917 weston_view_from_global_fixed(touch->focus, x, y, &sx, &sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400918
Neil Roberts96d790e2013-09-19 17:32:00 +0100919 resource_list = &touch->focus_resource_list;
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200920 msecs = timespec_to_msec(time);
Neil Roberts96d790e2013-09-19 17:32:00 +0100921 wl_resource_for_each(resource, resource_list) {
Alexandros Frantzisd7157842018-02-20 14:07:03 +0200922 send_timestamps_for_input_resource(resource,
923 &touch->timestamps_list,
924 time);
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200925 wl_touch_send_motion(resource, msecs,
Kristian Høgsberge329f362013-05-06 22:19:57 -0400926 touch_id, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400927 }
928}
929
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200930static void
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200931default_grab_touch_motion(struct weston_touch_grab *grab,
932 const struct timespec *time, int touch_id,
933 wl_fixed_t x, wl_fixed_t y)
Quentin Glidiccde13452016-08-12 10:41:32 +0200934{
935 weston_touch_send_motion(grab->touch, time, touch_id, x, y);
936}
937
938
939/** Send wl_touch.frame events to focused resources.
940 *
941 * \param touch The touch where the frame events originates from.
942 *
943 * For every resource that is currently in focus, send a wl_touch.frame event.
944 * The focused resources are the wl_touch resources of the client which
945 * currently has the surface with touch focus.
946 */
947WL_EXPORT void
948weston_touch_send_frame(struct weston_touch *touch)
Jonas Ådahl1679f232014-04-12 09:39:51 +0200949{
950 struct wl_resource *resource;
951
Quentin Glidiccde13452016-08-12 10:41:32 +0200952 if (!weston_touch_has_focus_resource(touch))
953 return;
954
955 wl_resource_for_each(resource, &touch->focus_resource_list)
Jonas Ådahl1679f232014-04-12 09:39:51 +0200956 wl_touch_send_frame(resource);
957}
958
959static void
Quentin Glidiccde13452016-08-12 10:41:32 +0200960default_grab_touch_frame(struct weston_touch_grab *grab)
961{
962 weston_touch_send_frame(grab->touch);
963}
964
965static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200966default_grab_touch_cancel(struct weston_touch_grab *grab)
967{
968}
969
Kristian Høgsberge329f362013-05-06 22:19:57 -0400970static const struct weston_touch_grab_interface default_touch_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400971 default_grab_touch_down,
972 default_grab_touch_up,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200973 default_grab_touch_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +0200974 default_grab_touch_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200975 default_grab_touch_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400976};
977
Quentin Glidiccde13452016-08-12 10:41:32 +0200978/** Check if the keyboard has focused resources.
979 *
980 * \param keyboard The keyboard to check for focused resources.
981 * \return Whether or not this keyboard has focused resources
982 */
983WL_EXPORT bool
984weston_keyboard_has_focus_resource(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400985{
Quentin Glidiccde13452016-08-12 10:41:32 +0200986 if (!keyboard->focus)
987 return false;
988
989 if (wl_list_empty(&keyboard->focus_resource_list))
990 return false;
991
992 return true;
993}
994
995/** Send wl_keyboard.key events to focused resources.
996 *
997 * \param keyboard The keyboard where the key events originates from.
998 * \param time The timestamp of the event
999 * \param key The key value of the event
1000 * \param state The state enum value of the event
1001 *
1002 * For every resource that is currently in focus, send a wl_keyboard.key event
1003 * with the passed parameters. The focused resources are the wl_keyboard
1004 * resources of the client which currently has the surface with keyboard focus.
1005 */
1006WL_EXPORT void
1007weston_keyboard_send_key(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02001008 const struct timespec *time, uint32_t key,
Quentin Glidiccde13452016-08-12 10:41:32 +02001009 enum wl_keyboard_key_state state)
1010{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001011 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +01001012 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001013 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01001014 struct wl_list *resource_list;
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02001015 uint32_t msecs;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001016
Quentin Glidiccde13452016-08-12 10:41:32 +02001017 if (!weston_keyboard_has_focus_resource(keyboard))
1018 return;
1019
Neil Roberts96d790e2013-09-19 17:32:00 +01001020 resource_list = &keyboard->focus_resource_list;
Quentin Glidiccde13452016-08-12 10:41:32 +02001021 serial = wl_display_next_serial(display);
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02001022 msecs = timespec_to_msec(time);
Alexandros Frantzis2b442482018-02-20 14:05:50 +02001023 wl_resource_for_each(resource, resource_list) {
1024 send_timestamps_for_input_resource(resource,
1025 &keyboard->timestamps_list,
1026 time);
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02001027 wl_keyboard_send_key(resource, serial, msecs, key, state);
Alexandros Frantzis2b442482018-02-20 14:05:50 +02001028 }
Quentin Glidiccde13452016-08-12 10:41:32 +02001029};
1030
1031static void
1032default_grab_keyboard_key(struct weston_keyboard_grab *grab,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02001033 const struct timespec *time, uint32_t key,
1034 uint32_t state)
Quentin Glidiccde13452016-08-12 10:41:32 +02001035{
1036 weston_keyboard_send_key(grab->keyboard, time, key, state);
Neil Roberts96d790e2013-09-19 17:32:00 +01001037}
1038
1039static void
1040send_modifiers_to_resource(struct weston_keyboard *keyboard,
1041 struct wl_resource *resource,
1042 uint32_t serial)
1043{
1044 wl_keyboard_send_modifiers(resource,
1045 serial,
1046 keyboard->modifiers.mods_depressed,
1047 keyboard->modifiers.mods_latched,
1048 keyboard->modifiers.mods_locked,
1049 keyboard->modifiers.group);
1050}
1051
1052static void
1053send_modifiers_to_client_in_list(struct wl_client *client,
1054 struct wl_list *list,
1055 uint32_t serial,
1056 struct weston_keyboard *keyboard)
1057{
1058 struct wl_resource *resource;
1059
1060 wl_resource_for_each(resource, list) {
1061 if (wl_resource_get_client(resource) == client)
1062 send_modifiers_to_resource(keyboard,
1063 resource,
1064 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001065 }
1066}
1067
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001068static struct weston_pointer_client *
1069find_pointer_client_for_surface(struct weston_pointer *pointer,
1070 struct weston_surface *surface)
1071{
1072 struct wl_client *client;
1073
1074 if (!surface)
1075 return NULL;
1076
1077 if (!surface->resource)
1078 return NULL;
1079
1080 client = wl_resource_get_client(surface->resource);
1081 return weston_pointer_get_pointer_client(pointer, client);
1082}
1083
1084static struct weston_pointer_client *
1085find_pointer_client_for_view(struct weston_pointer *pointer, struct weston_view *view)
1086{
1087 if (!view)
1088 return NULL;
1089
1090 return find_pointer_client_for_surface(pointer, view->surface);
1091}
1092
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001093static struct wl_resource *
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001094find_resource_for_surface(struct wl_list *list, struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001095{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001096 if (!surface)
1097 return NULL;
1098
Jason Ekstrand44a38632013-06-14 10:08:00 -05001099 if (!surface->resource)
1100 return NULL;
Stefan Schmidtfda26522013-09-17 10:54:09 +01001101
Jason Ekstrand44a38632013-06-14 10:08:00 -05001102 return wl_resource_find_for_client(list, wl_resource_get_client(surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001103}
1104
Quentin Glidiccde13452016-08-12 10:41:32 +02001105/** Send wl_keyboard.modifiers events to focused resources and pointer
1106 * focused resources.
1107 *
1108 * \param keyboard The keyboard where the modifiers events originates from.
1109 * \param serial The serial of the event
1110 * \param mods_depressed The mods_depressed value of the event
1111 * \param mods_latched The mods_latched value of the event
1112 * \param mods_locked The mods_locked value of the event
1113 * \param group The group value of the event
1114 *
1115 * For every resource that is currently in focus, send a wl_keyboard.modifiers
1116 * event with the passed parameters. The focused resources are the wl_keyboard
1117 * resources of the client which currently has the surface with keyboard focus.
1118 * This also sends wl_keyboard.modifiers events to the wl_keyboard resources of
1119 * the client having pointer focus (if different from the keyboard focus client).
1120 */
1121WL_EXPORT void
1122weston_keyboard_send_modifiers(struct weston_keyboard *keyboard,
1123 uint32_t serial, uint32_t mods_depressed,
1124 uint32_t mods_latched,
1125 uint32_t mods_locked, uint32_t group)
1126{
1127 struct weston_pointer *pointer =
1128 weston_seat_get_pointer(keyboard->seat);
1129
1130 if (weston_keyboard_has_focus_resource(keyboard)) {
1131 struct wl_list *resource_list;
1132 struct wl_resource *resource;
1133
1134 resource_list = &keyboard->focus_resource_list;
1135 wl_resource_for_each(resource, resource_list) {
1136 wl_keyboard_send_modifiers(resource, serial,
1137 mods_depressed, mods_latched,
1138 mods_locked, group);
1139 }
1140 }
1141
1142 if (pointer && pointer->focus && pointer->focus->surface->resource &&
1143 pointer->focus->surface != keyboard->focus) {
1144 struct wl_client *pointer_client =
1145 wl_resource_get_client(pointer->focus->surface->resource);
1146
1147 send_modifiers_to_client_in_list(pointer_client,
1148 &keyboard->resource_list,
1149 serial,
1150 keyboard);
1151 }
1152}
1153
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001154static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -07001155default_grab_keyboard_modifiers(struct weston_keyboard_grab *grab,
1156 uint32_t serial, uint32_t mods_depressed,
1157 uint32_t mods_latched,
1158 uint32_t mods_locked, uint32_t group)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001159{
Quentin Glidiccde13452016-08-12 10:41:32 +02001160 weston_keyboard_send_modifiers(grab->keyboard, serial, mods_depressed,
1161 mods_latched, mods_locked, group);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001162}
1163
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001164static void
1165default_grab_keyboard_cancel(struct weston_keyboard_grab *grab)
1166{
1167}
1168
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001169static const struct weston_keyboard_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001170 default_keyboard_grab_interface = {
Kristian Høgsbergb27901c2013-10-28 15:32:02 -07001171 default_grab_keyboard_key,
1172 default_grab_keyboard_modifiers,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001173 default_grab_keyboard_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001174};
1175
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001176static void
1177pointer_unmap_sprite(struct weston_pointer *pointer)
1178{
Pekka Paalanenc557ff72014-11-12 16:42:52 +02001179 struct weston_surface *surface = pointer->sprite->surface;
1180
1181 if (weston_surface_is_mapped(surface))
1182 weston_surface_unmap(surface);
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001183
1184 wl_list_remove(&pointer->sprite_destroy_listener.link);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02001185 surface->committed = NULL;
1186 surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03001187 weston_surface_set_label_func(surface, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001188 weston_view_destroy(pointer->sprite);
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001189 pointer->sprite = NULL;
1190}
1191
1192static void
1193pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
1194{
1195 struct weston_pointer *pointer =
1196 container_of(listener, struct weston_pointer,
1197 sprite_destroy_listener);
1198
1199 pointer->sprite = NULL;
1200}
1201
Jonas Ådahl3e12e632013-12-02 22:05:05 +01001202static void
1203weston_pointer_reset_state(struct weston_pointer *pointer)
1204{
1205 pointer->button_count = 0;
1206}
1207
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001208static void
1209weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data);
1210
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001211static struct weston_pointer *
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001212weston_pointer_create(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001213{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001214 struct weston_pointer *pointer;
1215
Peter Huttererf3d62272013-08-08 11:57:05 +10001216 pointer = zalloc(sizeof *pointer);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001217 if (pointer == NULL)
1218 return NULL;
1219
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001220 wl_list_init(&pointer->pointer_clients);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001221 weston_pointer_set_default_grab(pointer,
1222 seat->compositor->default_pointer_grab);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001223 wl_list_init(&pointer->focus_resource_listener.link);
1224 pointer->focus_resource_listener.notify = pointer_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001225 pointer->default_grab.pointer = pointer;
1226 pointer->grab = &pointer->default_grab;
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001227 wl_signal_init(&pointer->motion_signal);
Emilio Pozuelo Monfortaa7a4762013-11-19 11:37:15 +01001228 wl_signal_init(&pointer->focus_signal);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001229 wl_list_init(&pointer->focus_view_listener.link);
Jonas Ådahl3eb4ddd2016-07-22 17:52:58 +08001230 wl_signal_init(&pointer->destroy_signal);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +02001231 wl_list_init(&pointer->timestamps_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001232
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001233 pointer->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
1234
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001235 /* FIXME: Pick better co-ords. */
1236 pointer->x = wl_fixed_from_int(100);
1237 pointer->y = wl_fixed_from_int(100);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001238
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001239 pointer->output_destroy_listener.notify =
1240 weston_pointer_handle_output_destroy;
1241 wl_signal_add(&seat->compositor->output_destroyed_signal,
1242 &pointer->output_destroy_listener);
1243
Derek Foremanf9318d12015-05-11 15:40:11 -05001244 pointer->sx = wl_fixed_from_int(-1000000);
1245 pointer->sy = wl_fixed_from_int(-1000000);
1246
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001247 return pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001248}
1249
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001250static void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001251weston_pointer_destroy(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001252{
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +02001253 struct weston_pointer_client *pointer_client, *tmp;
1254
Jonas Ådahl3eb4ddd2016-07-22 17:52:58 +08001255 wl_signal_emit(&pointer->destroy_signal, pointer);
1256
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001257 if (pointer->sprite)
1258 pointer_unmap_sprite(pointer);
1259
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +02001260 wl_list_for_each_safe(pointer_client, tmp, &pointer->pointer_clients,
1261 link) {
1262 wl_list_remove(&pointer_client->link);
1263 weston_pointer_client_destroy(pointer_client);
1264 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001265
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001266 wl_list_remove(&pointer->focus_resource_listener.link);
1267 wl_list_remove(&pointer->focus_view_listener.link);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001268 wl_list_remove(&pointer->output_destroy_listener.link);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +02001269 wl_list_remove(&pointer->timestamps_list);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001270 free(pointer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001271}
1272
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001273void
1274weston_pointer_set_default_grab(struct weston_pointer *pointer,
1275 const struct weston_pointer_grab_interface *interface)
1276{
1277 if (interface)
1278 pointer->default_grab.interface = interface;
1279 else
1280 pointer->default_grab.interface =
1281 &default_pointer_grab_interface;
1282}
1283
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001284static struct weston_keyboard *
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001285weston_keyboard_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001286{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001287 struct weston_keyboard *keyboard;
1288
Peter Huttererf3d62272013-08-08 11:57:05 +10001289 keyboard = zalloc(sizeof *keyboard);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001290 if (keyboard == NULL)
Neil Roberts96d790e2013-09-19 17:32:00 +01001291 return NULL;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001292
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001293 wl_list_init(&keyboard->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +01001294 wl_list_init(&keyboard->focus_resource_list);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001295 wl_list_init(&keyboard->focus_resource_listener.link);
1296 keyboard->focus_resource_listener.notify = keyboard_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001297 wl_array_init(&keyboard->keys);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001298 keyboard->default_grab.interface = &default_keyboard_grab_interface;
1299 keyboard->default_grab.keyboard = keyboard;
1300 keyboard->grab = &keyboard->default_grab;
1301 wl_signal_init(&keyboard->focus_signal);
Alexandros Frantzis2b442482018-02-20 14:05:50 +02001302 wl_list_init(&keyboard->timestamps_list);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001303
1304 return keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001305}
1306
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001307static void
1308weston_xkb_info_destroy(struct weston_xkb_info *xkb_info);
1309
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001310static void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001311weston_keyboard_destroy(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001312{
Alexandros Frantzis7a314d62018-01-26 18:47:56 +02001313 struct wl_resource *resource;
1314
1315 wl_resource_for_each(resource, &keyboard->resource_list) {
1316 wl_resource_set_user_data(resource, NULL);
1317 }
1318
1319 wl_resource_for_each(resource, &keyboard->focus_resource_list) {
1320 wl_resource_set_user_data(resource, NULL);
1321 }
1322
1323 wl_list_remove(&keyboard->resource_list);
1324 wl_list_remove(&keyboard->focus_resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +01001325
Derek Foreman185d1582017-06-28 11:17:23 -05001326 xkb_state_unref(keyboard->xkb_state.state);
1327 if (keyboard->xkb_info)
1328 weston_xkb_info_destroy(keyboard->xkb_info);
1329 xkb_keymap_unref(keyboard->pending_keymap);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001330
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001331 wl_array_release(&keyboard->keys);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001332 wl_list_remove(&keyboard->focus_resource_listener.link);
Alexandros Frantzis2b442482018-02-20 14:05:50 +02001333 wl_list_remove(&keyboard->timestamps_list);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001334 free(keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001335}
1336
Jonas Ådahlcbfa7f72013-12-02 22:05:04 +01001337static void
1338weston_touch_reset_state(struct weston_touch *touch)
1339{
1340 touch->num_tp = 0;
1341}
1342
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001343static struct weston_touch *
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001344weston_touch_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001345{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001346 struct weston_touch *touch;
1347
Peter Huttererf3d62272013-08-08 11:57:05 +10001348 touch = zalloc(sizeof *touch);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001349 if (touch == NULL)
1350 return NULL;
1351
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -05001352 wl_list_init(&touch->device_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001353 wl_list_init(&touch->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +01001354 wl_list_init(&touch->focus_resource_list);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001355 wl_list_init(&touch->focus_view_listener.link);
1356 touch->focus_view_listener.notify = touch_focus_view_destroyed;
1357 wl_list_init(&touch->focus_resource_listener.link);
1358 touch->focus_resource_listener.notify = touch_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001359 touch->default_grab.interface = &default_touch_grab_interface;
1360 touch->default_grab.touch = touch;
1361 touch->grab = &touch->default_grab;
1362 wl_signal_init(&touch->focus_signal);
Alexandros Frantzisd7157842018-02-20 14:07:03 +02001363 wl_list_init(&touch->timestamps_list);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001364
1365 return touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001366}
1367
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001368static void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001369weston_touch_destroy(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001370{
Alexandros Frantzisb0b598c2018-01-26 18:47:57 +02001371 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01001372
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -05001373 assert(wl_list_empty(&touch->device_list));
1374
Alexandros Frantzisb0b598c2018-01-26 18:47:57 +02001375 wl_resource_for_each(resource, &touch->resource_list) {
1376 wl_resource_set_user_data(resource, NULL);
1377 }
1378
1379 wl_resource_for_each(resource, &touch->focus_resource_list) {
1380 wl_resource_set_user_data(resource, NULL);
1381 }
1382
1383 wl_list_remove(&touch->resource_list);
1384 wl_list_remove(&touch->focus_resource_list);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001385 wl_list_remove(&touch->focus_view_listener.link);
1386 wl_list_remove(&touch->focus_resource_listener.link);
Alexandros Frantzisd7157842018-02-20 14:07:03 +02001387 wl_list_remove(&touch->timestamps_list);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001388 free(touch);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001389}
1390
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001391static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04001392seat_send_updated_caps(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001393{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001394 enum wl_seat_capability caps = 0;
Rob Bradford6e737f52013-09-06 17:48:19 +01001395 struct wl_resource *resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001396
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001397 if (seat->pointer_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001398 caps |= WL_SEAT_CAPABILITY_POINTER;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001399 if (seat->keyboard_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001400 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001401 if (seat->touch_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001402 caps |= WL_SEAT_CAPABILITY_TOUCH;
1403
Rob Bradford6e737f52013-09-06 17:48:19 +01001404 wl_resource_for_each(resource, &seat->base_resource_list) {
1405 wl_seat_send_capabilities(resource, caps);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001406 }
Jason Ekstranda4ab5422014-04-02 19:53:45 -05001407 wl_signal_emit(&seat->updated_caps_signal, seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001408}
1409
Derek Foremanf9318d12015-05-11 15:40:11 -05001410
1411/** Clear the pointer focus
1412 *
1413 * \param pointer the pointer to clear focus for.
1414 *
1415 * This can be used to unset pointer focus and set the co-ordinates to the
1416 * arbitrary values we use for the no focus case.
1417 *
1418 * There's no requirement to use this function. For example, passing the
1419 * results of a weston_compositor_pick_view() directly to
1420 * weston_pointer_set_focus() will do the right thing when no view is found.
1421 */
1422WL_EXPORT void
1423weston_pointer_clear_focus(struct weston_pointer *pointer)
1424{
1425 weston_pointer_set_focus(pointer, NULL,
1426 wl_fixed_from_int(-1000000),
1427 wl_fixed_from_int(-1000000));
1428}
1429
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001430WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001431weston_pointer_set_focus(struct weston_pointer *pointer,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001432 struct weston_view *view,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001433 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001434{
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001435 struct weston_pointer_client *pointer_client;
Derek Foreman1281a362015-07-31 16:55:32 -05001436 struct weston_keyboard *kbd = weston_seat_get_keyboard(pointer->seat);
Neil Roberts96d790e2013-09-19 17:32:00 +01001437 struct wl_resource *resource;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001438 struct wl_resource *surface_resource;
Rob Bradford880ebc72013-07-22 17:31:38 +01001439 struct wl_display *display = pointer->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001440 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01001441 struct wl_list *focus_resource_list;
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001442 int refocus = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001443
1444 if ((!pointer->focus && view) ||
1445 (pointer->focus && !view) ||
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001446 (pointer->focus && pointer->focus->surface != view->surface) ||
1447 pointer->sx != sx || pointer->sy != sy)
1448 refocus = 1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001449
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001450 if (pointer->focus_client && refocus) {
1451 focus_resource_list = &pointer->focus_client->pointer_resources;
1452 if (!wl_list_empty(focus_resource_list)) {
1453 serial = wl_display_next_serial(display);
1454 surface_resource = pointer->focus->surface->resource;
1455 wl_resource_for_each(resource, focus_resource_list) {
1456 wl_pointer_send_leave(resource, serial,
1457 surface_resource);
Peter Hutterer87743e92016-01-18 16:38:22 +10001458 pointer_send_frame(resource);
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001459 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001460 }
1461
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001462 pointer->focus_client = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001463 }
1464
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001465 pointer_client = find_pointer_client_for_view(pointer, view);
1466 if (pointer_client && refocus) {
1467 struct wl_client *surface_client = pointer_client->client;
Neil Roberts96d790e2013-09-19 17:32:00 +01001468
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001469 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001470
Jason Ekstranda7af7042013-10-12 22:38:11 -05001471 if (kbd && kbd->focus != view->surface)
Kristian Høgsbergcb406f12013-10-09 10:54:03 -07001472 send_modifiers_to_client_in_list(surface_client,
1473 &kbd->resource_list,
1474 serial,
1475 kbd);
1476
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001477 pointer->focus_client = pointer_client;
Neil Roberts96d790e2013-09-19 17:32:00 +01001478
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001479 focus_resource_list = &pointer->focus_client->pointer_resources;
Neil Roberts96d790e2013-09-19 17:32:00 +01001480 wl_resource_for_each(resource, focus_resource_list) {
1481 wl_pointer_send_enter(resource,
1482 serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001483 view->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01001484 sx, sy);
Peter Hutterer87743e92016-01-18 16:38:22 +10001485 pointer_send_frame(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001486 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001487
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001488 pointer->focus_serial = serial;
1489 }
1490
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001491 wl_list_remove(&pointer->focus_view_listener.link);
1492 wl_list_init(&pointer->focus_view_listener.link);
1493 wl_list_remove(&pointer->focus_resource_listener.link);
1494 wl_list_init(&pointer->focus_resource_listener.link);
Emilio Pozuelo Monfortaa7a4762013-11-19 11:37:15 +01001495 if (view)
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001496 wl_signal_add(&view->destroy_signal, &pointer->focus_view_listener);
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001497 if (view && view->surface->resource)
1498 wl_resource_add_destroy_listener(view->surface->resource,
1499 &pointer->focus_resource_listener);
1500
1501 pointer->focus = view;
1502 pointer->focus_view_listener.notify = pointer_focus_view_destroyed;
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001503 pointer->sx = sx;
1504 pointer->sy = sy;
1505
Derek Foremanf9318d12015-05-11 15:40:11 -05001506 assert(view || sx == wl_fixed_from_int(-1000000));
1507 assert(view || sy == wl_fixed_from_int(-1000000));
1508
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001509 wl_signal_emit(&pointer->focus_signal, pointer);
1510}
1511
Neil Roberts96d790e2013-09-19 17:32:00 +01001512static void
1513send_enter_to_resource_list(struct wl_list *list,
1514 struct weston_keyboard *keyboard,
1515 struct weston_surface *surface,
1516 uint32_t serial)
1517{
1518 struct wl_resource *resource;
1519
1520 wl_resource_for_each(resource, list) {
Neil Roberts96d790e2013-09-19 17:32:00 +01001521 wl_keyboard_send_enter(resource, serial,
1522 surface->resource,
1523 &keyboard->keys);
Kirill Chibisovc46c70d2020-06-17 01:43:57 +03001524 send_modifiers_to_resource(keyboard, resource, serial);
Neil Roberts96d790e2013-09-19 17:32:00 +01001525 }
1526}
1527
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001528WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001529weston_keyboard_set_focus(struct weston_keyboard *keyboard,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001530 struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001531{
Quentin Glidic85d55542017-07-21 14:02:40 +02001532 struct weston_seat *seat = keyboard->seat;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001533 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +01001534 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001535 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01001536 struct wl_list *focus_resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001537
Pekka Paalanen72e183b2018-02-22 16:55:15 +02001538 /* Keyboard focus on a surface without a client is equivalent to NULL
1539 * focus as nothing would react to the keyboard events anyway.
1540 * Just set focus to NULL instead - the destroy listener hangs on the
1541 * wl_resource anyway.
1542 */
1543 if (surface && !surface->resource)
1544 surface = NULL;
1545
Neil Roberts96d790e2013-09-19 17:32:00 +01001546 focus_resource_list = &keyboard->focus_resource_list;
1547
1548 if (!wl_list_empty(focus_resource_list) && keyboard->focus != surface) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001549 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001550 wl_resource_for_each(resource, focus_resource_list) {
1551 wl_keyboard_send_leave(resource, serial,
1552 keyboard->focus->resource);
1553 }
1554 move_resources(&keyboard->resource_list, focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001555 }
1556
Neil Roberts96d790e2013-09-19 17:32:00 +01001557 if (find_resource_for_surface(&keyboard->resource_list, surface) &&
1558 keyboard->focus != surface) {
1559 struct wl_client *surface_client =
1560 wl_resource_get_client(surface->resource);
1561
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001562 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001563
1564 move_resources_for_client(focus_resource_list,
1565 &keyboard->resource_list,
1566 surface_client);
1567 send_enter_to_resource_list(focus_resource_list,
1568 keyboard,
1569 surface,
1570 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001571 keyboard->focus_serial = serial;
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001572 }
1573
Quentin Glidic85d55542017-07-21 14:02:40 +02001574 if (seat->saved_kbd_focus) {
1575 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1576 seat->saved_kbd_focus = NULL;
1577 }
1578
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001579 wl_list_remove(&keyboard->focus_resource_listener.link);
1580 wl_list_init(&keyboard->focus_resource_listener.link);
Pekka Paalanen72e183b2018-02-22 16:55:15 +02001581 if (surface)
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001582 wl_resource_add_destroy_listener(surface->resource,
1583 &keyboard->focus_resource_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001584
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001585 keyboard->focus = surface;
1586 wl_signal_emit(&keyboard->focus_signal, keyboard);
1587}
1588
Giulio Camuffoa20ca812014-11-22 11:16:56 +02001589/* Users of this function must manually manage the keyboard focus */
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001590WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001591weston_keyboard_start_grab(struct weston_keyboard *keyboard,
1592 struct weston_keyboard_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001593{
1594 keyboard->grab = grab;
1595 grab->keyboard = keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001596}
1597
1598WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001599weston_keyboard_end_grab(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001600{
1601 keyboard->grab = &keyboard->default_grab;
1602}
1603
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001604static void
1605weston_keyboard_cancel_grab(struct weston_keyboard *keyboard)
1606{
1607 keyboard->grab->interface->cancel(keyboard->grab);
1608}
1609
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001610WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001611weston_pointer_start_grab(struct weston_pointer *pointer,
1612 struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001613{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001614 pointer->grab = grab;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001615 grab->pointer = pointer;
Kristian Høgsbergda751b82013-07-04 00:58:07 -04001616 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001617}
1618
1619WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001620weston_pointer_end_grab(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001621{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001622 pointer->grab = &pointer->default_grab;
Kristian Høgsbergda751b82013-07-04 00:58:07 -04001623 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001624}
1625
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001626static void
1627weston_pointer_cancel_grab(struct weston_pointer *pointer)
1628{
1629 pointer->grab->interface->cancel(pointer->grab);
1630}
1631
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001632WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -04001633weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001634{
1635 touch->grab = grab;
1636 grab->touch = touch;
1637}
1638
1639WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -04001640weston_touch_end_grab(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001641{
1642 touch->grab = &touch->default_grab;
1643}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001644
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001645static void
1646weston_touch_cancel_grab(struct weston_touch *touch)
1647{
1648 touch->grab->interface->cancel(touch->grab);
1649}
1650
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001651static void
1652weston_pointer_clamp_for_output(struct weston_pointer *pointer,
1653 struct weston_output *output,
1654 wl_fixed_t *fx, wl_fixed_t *fy)
1655{
1656 int x, y;
1657
1658 x = wl_fixed_to_int(*fx);
1659 y = wl_fixed_to_int(*fy);
1660
1661 if (x < output->x)
1662 *fx = wl_fixed_from_int(output->x);
1663 else if (x >= output->x + output->width)
1664 *fx = wl_fixed_from_int(output->x +
1665 output->width - 1);
1666 if (y < output->y)
1667 *fy = wl_fixed_from_int(output->y);
1668 else if (y >= output->y + output->height)
1669 *fy = wl_fixed_from_int(output->y +
1670 output->height - 1);
1671}
1672
Rob Bradford806d8c02013-06-25 18:56:41 +01001673WL_EXPORT void
1674weston_pointer_clamp(struct weston_pointer *pointer, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001675{
Rob Bradford806d8c02013-06-25 18:56:41 +01001676 struct weston_compositor *ec = pointer->seat->compositor;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001677 struct weston_output *output, *prev = NULL;
1678 int x, y, old_x, old_y, valid = 0;
1679
1680 x = wl_fixed_to_int(*fx);
1681 y = wl_fixed_to_int(*fy);
Rob Bradford806d8c02013-06-25 18:56:41 +01001682 old_x = wl_fixed_to_int(pointer->x);
1683 old_y = wl_fixed_to_int(pointer->y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001684
1685 wl_list_for_each(output, &ec->output_list, link) {
Rob Bradford66bd9f52013-06-25 18:56:42 +01001686 if (pointer->seat->output && pointer->seat->output != output)
1687 continue;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001688 if (pixman_region32_contains_point(&output->region,
1689 x, y, NULL))
1690 valid = 1;
1691 if (pixman_region32_contains_point(&output->region,
1692 old_x, old_y, NULL))
1693 prev = output;
1694 }
1695
Rob Bradford66bd9f52013-06-25 18:56:42 +01001696 if (!prev)
1697 prev = pointer->seat->output;
1698
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001699 if (prev && !valid)
1700 weston_pointer_clamp_for_output(pointer, prev, fx, fy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001701}
1702
Jonas Ådahld2510102014-10-05 21:39:14 +02001703static void
1704weston_pointer_move_to(struct weston_pointer *pointer,
1705 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001706{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001707 int32_t ix, iy;
1708
Rob Bradford806d8c02013-06-25 18:56:41 +01001709 weston_pointer_clamp (pointer, &x, &y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001710
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001711 pointer->x = x;
1712 pointer->y = y;
1713
1714 ix = wl_fixed_to_int(x);
1715 iy = wl_fixed_to_int(y);
1716
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001717 if (pointer->sprite) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001718 weston_view_set_position(pointer->sprite,
1719 ix - pointer->hotspot_x,
1720 iy - pointer->hotspot_y);
1721 weston_view_schedule_repaint(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001722 }
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001723
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001724 pointer->grab->interface->focus(pointer->grab);
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001725 wl_signal_emit(&pointer->motion_signal, pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001726}
1727
Jonas Ådahld2510102014-10-05 21:39:14 +02001728WL_EXPORT void
Jonas Ådahld2510102014-10-05 21:39:14 +02001729weston_pointer_move(struct weston_pointer *pointer,
1730 struct weston_pointer_motion_event *event)
1731{
1732 wl_fixed_t x, y;
1733
1734 weston_pointer_motion_to_abs(pointer, event, &x, &y);
1735 weston_pointer_move_to(pointer, x, y);
1736}
1737
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001738/** Verify if the pointer is in a valid position and move it if it isn't.
1739 */
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001740static void
1741weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001742{
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001743 struct weston_pointer *pointer;
1744 struct weston_compositor *ec;
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001745 struct weston_output *output, *closest = NULL;
1746 int x, y, distance, min = INT_MAX;
1747 wl_fixed_t fx, fy;
1748
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001749 pointer = container_of(listener, struct weston_pointer,
1750 output_destroy_listener);
1751 ec = pointer->seat->compositor;
1752
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001753 x = wl_fixed_to_int(pointer->x);
1754 y = wl_fixed_to_int(pointer->y);
1755
1756 wl_list_for_each(output, &ec->output_list, link) {
1757 if (pixman_region32_contains_point(&output->region,
1758 x, y, NULL))
1759 return;
1760
1761 /* Aproximante the distance from the pointer to the center of
1762 * the output. */
1763 distance = abs(output->x + output->width / 2 - x) +
1764 abs(output->y + output->height / 2 - y);
1765 if (distance < min) {
1766 min = distance;
1767 closest = output;
1768 }
1769 }
1770
1771 /* Nothing to do if there's no output left. */
1772 if (!closest)
1773 return;
1774
1775 fx = pointer->x;
1776 fy = pointer->y;
1777
1778 weston_pointer_clamp_for_output(pointer, closest, &fx, &fy);
Jonas Ådahld2510102014-10-05 21:39:14 +02001779 weston_pointer_move_to(pointer, fx, fy);
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001780}
1781
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001782WL_EXPORT void
1783notify_motion(struct weston_seat *seat,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001784 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001785 struct weston_pointer_motion_event *event)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001786{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001787 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001788 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001789
1790 weston_compositor_wake(ec);
Jonas Ådahld2510102014-10-05 21:39:14 +02001791 pointer->grab->interface->motion(pointer->grab, time, event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001792}
1793
Daniel Stone96d47c02013-11-19 11:37:12 +01001794static void
1795run_modifier_bindings(struct weston_seat *seat, uint32_t old, uint32_t new)
1796{
1797 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001798 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Daniel Stone96d47c02013-11-19 11:37:12 +01001799 uint32_t diff;
1800 unsigned int i;
1801 struct {
1802 uint32_t xkb;
1803 enum weston_keyboard_modifier weston;
1804 } mods[] = {
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001805 { keyboard->xkb_info->ctrl_mod, MODIFIER_CTRL },
1806 { keyboard->xkb_info->alt_mod, MODIFIER_ALT },
1807 { keyboard->xkb_info->super_mod, MODIFIER_SUPER },
1808 { keyboard->xkb_info->shift_mod, MODIFIER_SHIFT },
Daniel Stone96d47c02013-11-19 11:37:12 +01001809 };
1810
1811 diff = new & ~old;
1812 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
1813 if (diff & (1 << mods[i].xkb))
1814 weston_compositor_run_modifier_binding(compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001815 keyboard,
Daniel Stone96d47c02013-11-19 11:37:12 +01001816 mods[i].weston,
1817 WL_KEYBOARD_KEY_STATE_PRESSED);
1818 }
1819
1820 diff = old & ~new;
1821 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
1822 if (diff & (1 << mods[i].xkb))
1823 weston_compositor_run_modifier_binding(compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001824 keyboard,
Daniel Stone96d47c02013-11-19 11:37:12 +01001825 mods[i].weston,
1826 WL_KEYBOARD_KEY_STATE_RELEASED);
1827 }
1828}
1829
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001830WL_EXPORT void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001831notify_motion_absolute(struct weston_seat *seat, const struct timespec *time,
1832 double x, double y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001833{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001834 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001835 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Jonas Ådahld2510102014-10-05 21:39:14 +02001836 struct weston_pointer_motion_event event = { 0 };
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001837
1838 weston_compositor_wake(ec);
Jonas Ådahld2510102014-10-05 21:39:14 +02001839
1840 event = (struct weston_pointer_motion_event) {
1841 .mask = WESTON_POINTER_MOTION_ABS,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001842 .x = x,
1843 .y = y,
Jonas Ådahld2510102014-10-05 21:39:14 +02001844 };
1845
1846 pointer->grab->interface->motion(pointer->grab, time, &event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001847}
1848
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02001849static unsigned int
1850peek_next_activate_serial(struct weston_compositor *c)
1851{
1852 unsigned serial = c->activate_serial + 1;
1853
1854 return serial == 0 ? 1 : serial;
1855}
1856
1857static void
1858inc_activate_serial(struct weston_compositor *c)
1859{
1860 c->activate_serial = peek_next_activate_serial (c);
1861}
1862
1863WL_EXPORT void
1864weston_view_activate(struct weston_view *view,
1865 struct weston_seat *seat,
1866 uint32_t flags)
1867{
1868 struct weston_compositor *compositor = seat->compositor;
1869
1870 if (flags & WESTON_ACTIVATE_FLAG_CLICKED) {
1871 view->click_to_activate_serial =
1872 peek_next_activate_serial(compositor);
1873 }
1874
1875 weston_seat_set_keyboard_focus(seat, view->surface);
1876}
1877
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001878WL_EXPORT void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001879notify_button(struct weston_seat *seat, const struct timespec *time,
1880 int32_t button, enum wl_pointer_button_state state)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001881{
1882 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001883 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001884
1885 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001886 weston_compositor_idle_inhibit(compositor);
1887 if (pointer->button_count == 0) {
1888 pointer->grab_button = button;
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001889 pointer->grab_time = *time;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001890 pointer->grab_x = pointer->x;
1891 pointer->grab_y = pointer->y;
1892 }
1893 pointer->button_count++;
1894 } else {
1895 weston_compositor_idle_release(compositor);
1896 pointer->button_count--;
1897 }
1898
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001899 weston_compositor_run_button_binding(compositor, pointer, time, button,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001900 state);
1901
1902 pointer->grab->interface->button(pointer->grab, time, button, state);
1903
1904 if (pointer->button_count == 1)
1905 pointer->grab_serial =
1906 wl_display_get_serial(compositor->wl_display);
1907}
1908
1909WL_EXPORT void
Alexandros Frantzis80321942017-11-16 18:20:56 +02001910notify_axis(struct weston_seat *seat, const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001911 struct weston_pointer_axis_event *event)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001912{
1913 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001914 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001915
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001916 weston_compositor_wake(compositor);
1917
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001918 if (weston_compositor_run_axis_binding(compositor, pointer,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001919 time, event))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001920 return;
1921
Peter Hutterer89b6a492016-01-18 15:58:17 +10001922 pointer->grab->interface->axis(pointer->grab, time, event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001923}
1924
Peter Hutterer87743e92016-01-18 16:38:22 +10001925WL_EXPORT void
1926notify_axis_source(struct weston_seat *seat, uint32_t source)
1927{
1928 struct weston_compositor *compositor = seat->compositor;
1929 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1930
1931 weston_compositor_wake(compositor);
1932
1933 pointer->grab->interface->axis_source(pointer->grab, source);
1934}
1935
1936WL_EXPORT void
1937notify_pointer_frame(struct weston_seat *seat)
1938{
1939 struct weston_compositor *compositor = seat->compositor;
1940 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1941
1942 weston_compositor_wake(compositor);
1943
1944 pointer->grab->interface->frame(pointer->grab);
1945}
1946
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001947WL_EXPORT int
1948weston_keyboard_set_locks(struct weston_keyboard *keyboard,
1949 uint32_t mask, uint32_t value)
1950{
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001951 uint32_t serial;
1952 xkb_mod_mask_t mods_depressed, mods_latched, mods_locked, group;
1953 xkb_mod_mask_t num, caps;
1954
1955 /* We don't want the leds to go out of sync with the actual state
1956 * so if the backend has no way to change the leds don't try to
1957 * change the state */
1958 if (!keyboard->seat->led_update)
1959 return -1;
1960
1961 mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
1962 XKB_STATE_DEPRESSED);
1963 mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
1964 XKB_STATE_LATCHED);
1965 mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
1966 XKB_STATE_LOCKED);
1967 group = xkb_state_serialize_group(keyboard->xkb_state.state,
1968 XKB_STATE_EFFECTIVE);
1969
1970 num = (1 << keyboard->xkb_info->mod2_mod);
1971 caps = (1 << keyboard->xkb_info->caps_mod);
1972 if (mask & WESTON_NUM_LOCK) {
1973 if (value & WESTON_NUM_LOCK)
1974 mods_locked |= num;
1975 else
1976 mods_locked &= ~num;
1977 }
1978 if (mask & WESTON_CAPS_LOCK) {
1979 if (value & WESTON_CAPS_LOCK)
1980 mods_locked |= caps;
1981 else
1982 mods_locked &= ~caps;
1983 }
1984
1985 xkb_state_update_mask(keyboard->xkb_state.state, mods_depressed,
1986 mods_latched, mods_locked, 0, 0, group);
1987
1988 serial = wl_display_next_serial(
1989 keyboard->seat->compositor->wl_display);
1990 notify_modifiers(keyboard->seat, serial);
1991
1992 return 0;
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001993}
1994
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001995WL_EXPORT void
1996notify_modifiers(struct weston_seat *seat, uint32_t serial)
1997{
Derek Foreman1281a362015-07-31 16:55:32 -05001998 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001999 struct weston_keyboard_grab *grab = keyboard->grab;
2000 uint32_t mods_depressed, mods_latched, mods_locked, group;
2001 uint32_t mods_lookup;
2002 enum weston_led leds = 0;
2003 int changed = 0;
2004
2005 /* Serialize and update our internal state, checking to see if it's
2006 * different to the previous state. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002007 mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03002008 XKB_STATE_MODS_DEPRESSED);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002009 mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03002010 XKB_STATE_MODS_LATCHED);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002011 mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03002012 XKB_STATE_MODS_LOCKED);
2013 group = xkb_state_serialize_layout(keyboard->xkb_state.state,
2014 XKB_STATE_LAYOUT_EFFECTIVE);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002015
Derek Foreman244e99e2015-06-03 15:53:26 -05002016 if (mods_depressed != keyboard->modifiers.mods_depressed ||
2017 mods_latched != keyboard->modifiers.mods_latched ||
2018 mods_locked != keyboard->modifiers.mods_locked ||
2019 group != keyboard->modifiers.group)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002020 changed = 1;
2021
Derek Foreman244e99e2015-06-03 15:53:26 -05002022 run_modifier_bindings(seat, keyboard->modifiers.mods_depressed,
Daniel Stone96d47c02013-11-19 11:37:12 +01002023 mods_depressed);
2024
Derek Foreman244e99e2015-06-03 15:53:26 -05002025 keyboard->modifiers.mods_depressed = mods_depressed;
2026 keyboard->modifiers.mods_latched = mods_latched;
2027 keyboard->modifiers.mods_locked = mods_locked;
2028 keyboard->modifiers.group = group;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002029
2030 /* And update the modifier_state for bindings. */
2031 mods_lookup = mods_depressed | mods_latched;
2032 seat->modifier_state = 0;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002033 if (mods_lookup & (1 << keyboard->xkb_info->ctrl_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002034 seat->modifier_state |= MODIFIER_CTRL;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002035 if (mods_lookup & (1 << keyboard->xkb_info->alt_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002036 seat->modifier_state |= MODIFIER_ALT;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002037 if (mods_lookup & (1 << keyboard->xkb_info->super_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002038 seat->modifier_state |= MODIFIER_SUPER;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002039 if (mods_lookup & (1 << keyboard->xkb_info->shift_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002040 seat->modifier_state |= MODIFIER_SHIFT;
2041
2042 /* Finally, notify the compositor that LEDs have changed. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002043 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
2044 keyboard->xkb_info->num_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002045 leds |= LED_NUM_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002046 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
2047 keyboard->xkb_info->caps_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002048 leds |= LED_CAPS_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002049 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
2050 keyboard->xkb_info->scroll_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002051 leds |= LED_SCROLL_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002052 if (leds != keyboard->xkb_state.leds && seat->led_update)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002053 seat->led_update(seat, leds);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002054 keyboard->xkb_state.leds = leds;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002055
2056 if (changed) {
2057 grab->interface->modifiers(grab,
2058 serial,
2059 keyboard->modifiers.mods_depressed,
2060 keyboard->modifiers.mods_latched,
2061 keyboard->modifiers.mods_locked,
2062 keyboard->modifiers.group);
2063 }
2064}
2065
2066static void
2067update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
2068 enum wl_keyboard_key_state state)
2069{
Derek Foreman1281a362015-07-31 16:55:32 -05002070 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002071 enum xkb_key_direction direction;
2072
2073 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2074 direction = XKB_KEY_DOWN;
2075 else
2076 direction = XKB_KEY_UP;
2077
2078 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
2079 * broken keycode system, which starts at 8. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002080 xkb_state_update_key(keyboard->xkb_state.state, key + 8, direction);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002081
2082 notify_modifiers(seat, serial);
2083}
Rui Matos65196bc2013-10-10 19:44:19 +02002084
Derek Foremanf8f7fd62017-06-28 11:41:43 -05002085WL_EXPORT void
2086weston_keyboard_send_keymap(struct weston_keyboard *kbd, struct wl_resource *resource)
Rui Matos65196bc2013-10-10 19:44:19 +02002087{
Derek Foremanf8f7fd62017-06-28 11:41:43 -05002088 struct weston_xkb_info *xkb_info = kbd->xkb_info;
Derek Foreman76829fc2017-06-28 12:17:46 -05002089 int fd;
Sebastian Wickabec5122019-11-01 02:38:45 +01002090 size_t size;
2091 enum ro_anonymous_file_mapmode mapmode;
Derek Foremanf8f7fd62017-06-28 11:41:43 -05002092
Sebastian Wickabec5122019-11-01 02:38:45 +01002093 if (wl_resource_get_version(resource) < 7)
2094 mapmode = RO_ANONYMOUS_FILE_MAPMODE_SHARED;
2095 else
2096 mapmode = RO_ANONYMOUS_FILE_MAPMODE_PRIVATE;
2097
2098 fd = os_ro_anonymous_file_get_fd(xkb_info->keymap_rofile, mapmode);
2099 size = os_ro_anonymous_file_size(xkb_info->keymap_rofile);
2100
2101 if (fd == -1) {
2102 weston_log("creating a keymap file failed: %s\n",
Antonio Borneo39578632019-04-26 23:57:31 +02002103 strerror(errno));
Derek Foreman76829fc2017-06-28 12:17:46 -05002104 return;
2105 }
2106
Rui Matos65196bc2013-10-10 19:44:19 +02002107 wl_keyboard_send_keymap(resource,
2108 WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
Derek Foreman76829fc2017-06-28 12:17:46 -05002109 fd,
Sebastian Wickabec5122019-11-01 02:38:45 +01002110 size);
2111
2112 os_ro_anonymous_file_put_fd(fd);
Rui Matos65196bc2013-10-10 19:44:19 +02002113}
2114
2115static void
2116send_modifiers(struct wl_resource *resource, uint32_t serial, struct weston_keyboard *keyboard)
2117{
2118 wl_keyboard_send_modifiers(resource, serial,
2119 keyboard->modifiers.mods_depressed,
2120 keyboard->modifiers.mods_latched,
2121 keyboard->modifiers.mods_locked,
2122 keyboard->modifiers.group);
2123}
2124
2125static struct weston_xkb_info *
2126weston_xkb_info_create(struct xkb_keymap *keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02002127
2128static void
2129update_keymap(struct weston_seat *seat)
2130{
Derek Foreman1281a362015-07-31 16:55:32 -05002131 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Rui Matos65196bc2013-10-10 19:44:19 +02002132 struct wl_resource *resource;
2133 struct weston_xkb_info *xkb_info;
2134 struct xkb_state *state;
2135 xkb_mod_mask_t latched_mods;
2136 xkb_mod_mask_t locked_mods;
2137
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002138 xkb_info = weston_xkb_info_create(keyboard->pending_keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02002139
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002140 xkb_keymap_unref(keyboard->pending_keymap);
2141 keyboard->pending_keymap = NULL;
Rui Matos65196bc2013-10-10 19:44:19 +02002142
2143 if (!xkb_info) {
2144 weston_log("failed to create XKB info\n");
2145 return;
2146 }
2147
2148 state = xkb_state_new(xkb_info->keymap);
2149 if (!state) {
2150 weston_log("failed to initialise XKB state\n");
2151 weston_xkb_info_destroy(xkb_info);
2152 return;
2153 }
2154
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002155 latched_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
2156 XKB_STATE_MODS_LATCHED);
2157 locked_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
2158 XKB_STATE_MODS_LOCKED);
Rui Matos65196bc2013-10-10 19:44:19 +02002159 xkb_state_update_mask(state,
2160 0, /* depressed */
2161 latched_mods,
2162 locked_mods,
2163 0, 0, 0);
2164
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002165 weston_xkb_info_destroy(keyboard->xkb_info);
2166 keyboard->xkb_info = xkb_info;
Rui Matos65196bc2013-10-10 19:44:19 +02002167
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002168 xkb_state_unref(keyboard->xkb_state.state);
2169 keyboard->xkb_state.state = state;
Rui Matos65196bc2013-10-10 19:44:19 +02002170
Derek Foremanbc91e542015-06-03 15:53:27 -05002171 wl_resource_for_each(resource, &keyboard->resource_list)
Derek Foremanf8f7fd62017-06-28 11:41:43 -05002172 weston_keyboard_send_keymap(keyboard, resource);
Derek Foremanbc91e542015-06-03 15:53:27 -05002173 wl_resource_for_each(resource, &keyboard->focus_resource_list)
Derek Foremanf8f7fd62017-06-28 11:41:43 -05002174 weston_keyboard_send_keymap(keyboard, resource);
Rui Matos65196bc2013-10-10 19:44:19 +02002175
2176 notify_modifiers(seat, wl_display_next_serial(seat->compositor->wl_display));
2177
2178 if (!latched_mods && !locked_mods)
2179 return;
2180
Derek Foremanbc91e542015-06-03 15:53:27 -05002181 wl_resource_for_each(resource, &keyboard->resource_list)
2182 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), keyboard);
2183 wl_resource_for_each(resource, &keyboard->focus_resource_list)
2184 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), keyboard);
Rui Matos65196bc2013-10-10 19:44:19 +02002185}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002186
2187WL_EXPORT void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02002188notify_key(struct weston_seat *seat, const struct timespec *time, uint32_t key,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002189 enum wl_keyboard_key_state state,
2190 enum weston_key_state_update update_state)
2191{
2192 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002193 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002194 struct weston_keyboard_grab *grab = keyboard->grab;
Pekka Paalanen86b53962014-11-19 13:43:32 +02002195 uint32_t *k, *end;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002196
2197 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002198 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002199 } else {
2200 weston_compositor_idle_release(compositor);
2201 }
2202
Pekka Paalanen86b53962014-11-19 13:43:32 +02002203 end = keyboard->keys.data + keyboard->keys.size;
2204 for (k = keyboard->keys.data; k < end; k++) {
2205 if (*k == key) {
2206 /* Ignore server-generated repeats. */
2207 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2208 return;
2209 *k = *--end;
2210 }
2211 }
2212 keyboard->keys.size = (void *) end - keyboard->keys.data;
2213 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2214 k = wl_array_add(&keyboard->keys, sizeof *k);
2215 *k = key;
2216 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002217
2218 if (grab == &keyboard->default_grab ||
2219 grab == &keyboard->input_method_grab) {
Derek Foreman99a6a2d2015-07-15 13:00:43 -05002220 weston_compositor_run_key_binding(compositor, keyboard, time,
2221 key, state);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002222 grab = keyboard->grab;
2223 }
2224
2225 grab->interface->key(grab, time, key, state);
2226
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002227 if (keyboard->pending_keymap &&
Pekka Paalanen86b53962014-11-19 13:43:32 +02002228 keyboard->keys.size == 0)
Rui Matos65196bc2013-10-10 19:44:19 +02002229 update_keymap(seat);
2230
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002231 if (update_state == STATE_UPDATE_AUTOMATIC) {
2232 update_modifier_state(seat,
2233 wl_display_get_serial(compositor->wl_display),
2234 key,
2235 state);
2236 }
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002237
Olivier Fourdandf84dbe2016-06-30 16:01:56 +02002238 keyboard->grab_serial = wl_display_get_serial(compositor->wl_display);
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002239 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02002240 keyboard->grab_time = *time;
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002241 keyboard->grab_key = key;
2242 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002243}
2244
2245WL_EXPORT void
2246notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002247 double x, double y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002248{
Derek Foreman1281a362015-07-31 16:55:32 -05002249 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2250
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002251 if (output) {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002252 weston_pointer_move_to(pointer,
2253 wl_fixed_from_double(x),
2254 wl_fixed_from_double(y));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002255 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002256 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002257 * NULL) here, but somehow that breaks re-entry... */
2258 }
2259}
2260
2261static void
2262destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
2263{
2264 struct weston_seat *ws;
2265
2266 ws = container_of(listener, struct weston_seat,
2267 saved_kbd_focus_listener);
2268
2269 ws->saved_kbd_focus = NULL;
2270}
2271
2272WL_EXPORT void
2273notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
2274 enum weston_key_state_update update_state)
2275{
2276 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002277 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04002278 struct weston_surface *surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002279 uint32_t *k, serial;
2280
2281 serial = wl_display_next_serial(compositor->wl_display);
2282 wl_array_copy(&keyboard->keys, keys);
2283 wl_array_for_each(k, &keyboard->keys) {
2284 weston_compositor_idle_inhibit(compositor);
2285 if (update_state == STATE_UPDATE_AUTOMATIC)
2286 update_modifier_state(seat, serial, *k,
2287 WL_KEYBOARD_KEY_STATE_PRESSED);
2288 }
2289
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002290 surface = seat->saved_kbd_focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002291 if (surface) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002292 weston_keyboard_set_focus(keyboard, surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002293 }
2294}
2295
2296WL_EXPORT void
2297notify_keyboard_focus_out(struct weston_seat *seat)
2298{
2299 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002300 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2301 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Quentin Glidic85d55542017-07-21 14:02:40 +02002302 struct weston_surface *focus = keyboard->focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002303 uint32_t *k, serial;
2304
2305 serial = wl_display_next_serial(compositor->wl_display);
2306 wl_array_for_each(k, &keyboard->keys) {
2307 weston_compositor_idle_release(compositor);
2308 update_modifier_state(seat, serial, *k,
2309 WL_KEYBOARD_KEY_STATE_RELEASED);
2310 }
2311
2312 seat->modifier_state = 0;
2313
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002314 weston_keyboard_set_focus(keyboard, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002315 weston_keyboard_cancel_grab(keyboard);
Derek Foreman1281a362015-07-31 16:55:32 -05002316 if (pointer)
2317 weston_pointer_cancel_grab(pointer);
Quentin Glidic85d55542017-07-21 14:02:40 +02002318
2319 if (focus) {
2320 seat->saved_kbd_focus = focus;
2321 seat->saved_kbd_focus_listener.notify =
2322 destroy_device_saved_kbd_focus;
2323 wl_signal_add(&focus->destroy_signal,
2324 &seat->saved_kbd_focus_listener);
2325 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002326}
2327
Michael Fua2bb7912013-07-23 15:51:06 +08002328WL_EXPORT void
Derek Foreman4c93c082015-04-30 16:45:41 -05002329weston_touch_set_focus(struct weston_touch *touch, struct weston_view *view)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002330{
Neil Roberts96d790e2013-09-19 17:32:00 +01002331 struct wl_list *focus_resource_list;
2332
Derek Foreman4c93c082015-04-30 16:45:41 -05002333 focus_resource_list = &touch->focus_resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002334
Derek Foreman4c93c082015-04-30 16:45:41 -05002335 if (view && touch->focus &&
2336 touch->focus->surface == view->surface) {
2337 touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002338 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002339 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002340
Derek Foreman4c93c082015-04-30 16:45:41 -05002341 wl_list_remove(&touch->focus_resource_listener.link);
2342 wl_list_init(&touch->focus_resource_listener.link);
2343 wl_list_remove(&touch->focus_view_listener.link);
2344 wl_list_init(&touch->focus_view_listener.link);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01002345
Neil Roberts96d790e2013-09-19 17:32:00 +01002346 if (!wl_list_empty(focus_resource_list)) {
Derek Foreman4c93c082015-04-30 16:45:41 -05002347 move_resources(&touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002348 focus_resource_list);
2349 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002350
Jason Ekstranda7af7042013-10-12 22:38:11 -05002351 if (view) {
Derek Foreman362656b2014-09-04 10:23:05 -05002352 struct wl_client *surface_client;
2353
2354 if (!view->surface->resource) {
Derek Foreman4c93c082015-04-30 16:45:41 -05002355 touch->focus = NULL;
Derek Foreman362656b2014-09-04 10:23:05 -05002356 return;
2357 }
2358
2359 surface_client = wl_resource_get_client(view->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +01002360 move_resources_for_client(focus_resource_list,
Derek Foreman4c93c082015-04-30 16:45:41 -05002361 &touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002362 surface_client);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01002363 wl_resource_add_destroy_listener(view->surface->resource,
Derek Foreman4c93c082015-04-30 16:45:41 -05002364 &touch->focus_resource_listener);
2365 wl_signal_add(&view->destroy_signal, &touch->focus_view_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002366 }
Derek Foreman4c93c082015-04-30 16:45:41 -05002367 touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002368}
2369
Pekka Paalanenf4062532018-02-26 16:18:29 +02002370static void
2371process_touch_normal(struct weston_touch_device *device,
2372 const struct timespec *time, int touch_id,
2373 double double_x, double double_y, int touch_type)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002374{
Pekka Paalanenbcbce332018-02-26 14:43:00 +02002375 struct weston_touch *touch = device->aggregate;
2376 struct weston_touch_grab *grab = device->aggregate->grab;
2377 struct weston_compositor *ec = device->aggregate->seat->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002378 struct weston_view *ev;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002379 wl_fixed_t sx, sy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002380 wl_fixed_t x = wl_fixed_from_double(double_x);
2381 wl_fixed_t y = wl_fixed_from_double(double_y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002382
2383 /* Update grab's global coordinates. */
Neil Roberts306fe082013-10-03 16:43:06 +01002384 if (touch_id == touch->grab_touch_id && touch_type != WL_TOUCH_UP) {
2385 touch->grab_x = x;
2386 touch->grab_y = y;
2387 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002388
2389 switch (touch_type) {
2390 case WL_TOUCH_DOWN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002391 /* the first finger down picks the view, and all further go
2392 * to that view for the remainder of the touch session i.e.
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002393 * until all touch points are up again. */
Jonas Ådahl9484b692013-12-02 22:05:03 +01002394 if (touch->num_tp == 1) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002395 ev = weston_compositor_pick_view(ec, x, y, &sx, &sy);
Derek Foreman4c93c082015-04-30 16:45:41 -05002396 weston_touch_set_focus(touch, ev);
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002397 } else if (!touch->focus) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002398 /* Unexpected condition: We have non-initial touch but
2399 * there is no focused surface.
2400 */
Chris Michael3f607d32015-10-07 11:59:49 -04002401 weston_log("touch event received with %d points down "
Jonas Ådahl9484b692013-12-02 22:05:03 +01002402 "but no surface focused\n", touch->num_tp);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002403 return;
2404 }
2405
Derek Foreman99a6a2d2015-07-15 13:00:43 -05002406 weston_compositor_run_touch_binding(ec, touch,
Kristian Høgsbergc8964012014-02-05 14:25:18 -08002407 time, touch_type);
2408
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002409 grab->interface->down(grab, time, touch_id, x, y);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002410 if (touch->num_tp == 1) {
Rusty Lynchf1407ff2013-08-08 21:13:57 -07002411 touch->grab_serial =
2412 wl_display_get_serial(ec->wl_display);
Neil Roberts306fe082013-10-03 16:43:06 +01002413 touch->grab_touch_id = touch_id;
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02002414 touch->grab_time = *time;
Rusty Lynchf1407ff2013-08-08 21:13:57 -07002415 touch->grab_x = x;
2416 touch->grab_y = y;
2417 }
2418
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002419 break;
2420 case WL_TOUCH_MOTION:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002421 ev = touch->focus;
2422 if (!ev)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002423 break;
2424
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +02002425 grab->interface->motion(grab, time, touch_id, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002426 break;
2427 case WL_TOUCH_UP:
Alexandros Frantzis27a51b82017-11-16 18:20:59 +02002428 grab->interface->up(grab, time, touch_id);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002429 if (touch->num_tp == 0)
Derek Foreman4c93c082015-04-30 16:45:41 -05002430 weston_touch_set_focus(touch, NULL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002431 break;
2432 }
2433}
2434
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002435static enum weston_touch_mode
2436get_next_touch_mode(enum weston_touch_mode from)
2437{
2438 switch (from) {
2439 case WESTON_TOUCH_MODE_PREP_NORMAL:
2440 return WESTON_TOUCH_MODE_NORMAL;
2441
2442 case WESTON_TOUCH_MODE_PREP_CALIB:
2443 return WESTON_TOUCH_MODE_CALIB;
2444
2445 case WESTON_TOUCH_MODE_NORMAL:
2446 case WESTON_TOUCH_MODE_CALIB:
2447 return from;
2448 }
2449
2450 return WESTON_TOUCH_MODE_NORMAL;
2451}
2452
2453/** Global touch mode update
2454 *
2455 * If no seat has a touch down and the compositor is in a PREP touch mode,
2456 * set the compositor to the goal touch mode.
2457 *
2458 * Calls calibrator if touch mode changed.
2459 */
2460static void
2461weston_compositor_update_touch_mode(struct weston_compositor *compositor)
2462{
2463 struct weston_seat *seat;
2464 struct weston_touch *touch;
2465 enum weston_touch_mode goal;
2466
2467 wl_list_for_each(seat, &compositor->seat_list, link) {
2468 touch = weston_seat_get_touch(seat);
2469 if (!touch)
2470 continue;
2471
2472 if (touch->num_tp > 0)
2473 return;
2474 }
2475
2476 goal = get_next_touch_mode(compositor->touch_mode);
2477 if (compositor->touch_mode != goal) {
2478 compositor->touch_mode = goal;
2479 touch_calibrator_mode_changed(compositor);
2480 }
2481}
2482
2483/** Start transition to normal touch event handling
2484 *
2485 * The touch event mode changes when all touches on all touch devices have
2486 * been lifted. If no touches are currently down, the transition is immediate.
2487 *
2488 * \sa weston_touch_mode
2489 */
2490void
2491weston_compositor_set_touch_mode_normal(struct weston_compositor *compositor)
2492{
2493 switch (compositor->touch_mode) {
2494 case WESTON_TOUCH_MODE_PREP_NORMAL:
2495 case WESTON_TOUCH_MODE_NORMAL:
2496 return;
2497 case WESTON_TOUCH_MODE_PREP_CALIB:
2498 compositor->touch_mode = WESTON_TOUCH_MODE_NORMAL;
2499 touch_calibrator_mode_changed(compositor);
2500 return;
2501 case WESTON_TOUCH_MODE_CALIB:
2502 compositor->touch_mode = WESTON_TOUCH_MODE_PREP_NORMAL;
2503 }
2504
2505 weston_compositor_update_touch_mode(compositor);
2506}
2507
2508/** Start transition to calibrator touch event handling
2509 *
2510 * The touch event mode changes when all touches on all touch devices have
2511 * been lifted. If no touches are currently down, the transition is immediate.
2512 *
2513 * \sa weston_touch_mode
2514 */
2515void
2516weston_compositor_set_touch_mode_calib(struct weston_compositor *compositor)
2517{
2518 switch (compositor->touch_mode) {
2519 case WESTON_TOUCH_MODE_PREP_CALIB:
2520 case WESTON_TOUCH_MODE_CALIB:
2521 assert(0);
2522 return;
2523 case WESTON_TOUCH_MODE_PREP_NORMAL:
2524 compositor->touch_mode = WESTON_TOUCH_MODE_CALIB;
2525 touch_calibrator_mode_changed(compositor);
2526 return;
2527 case WESTON_TOUCH_MODE_NORMAL:
2528 compositor->touch_mode = WESTON_TOUCH_MODE_PREP_CALIB;
2529 }
2530
2531 weston_compositor_update_touch_mode(compositor);
2532}
2533
Pekka Paalanenf4062532018-02-26 16:18:29 +02002534/** Feed in touch down, motion, and up events, calibratable device.
2535 *
2536 * It assumes always the correct cycle sequence until it gets here: touch_down
2537 * → touch_update → ... → touch_update → touch_end. The driver is responsible
2538 * for sending along such order.
2539 *
2540 * \param device The physical device that generated the event.
2541 * \param time The event timestamp.
2542 * \param touch_id ID for the touch point of this event (multi-touch).
Marius Vlada2dace22019-06-12 16:05:44 +03002543 * \param x X coordinate in compositor global space.
2544 * \param y Y coordinate in compositor global space.
Pekka Paalanenf4062532018-02-26 16:18:29 +02002545 * \param norm Normalized device X, Y coordinates in calibration space, or NULL.
2546 * \param touch_type Either WL_TOUCH_DOWN, WL_TOUCH_UP, or WL_TOUCH_MOTION.
2547 *
2548 * Coordinates double_x and double_y are used for normal operation.
2549 *
2550 * Coordinates norm are only used for touch device calibration. If and only if
2551 * the weston_touch_device does not support calibrating, norm must be NULL.
2552 *
2553 * The calibration space is the normalized coordinate space
2554 * [0.0, 1.0]×[0.0, 1.0] of the weston_touch_device. This is assumed to
2555 * map to the similar normalized coordinate space of the associated
2556 * weston_output.
2557 */
2558WL_EXPORT void
2559notify_touch_normalized(struct weston_touch_device *device,
2560 const struct timespec *time,
2561 int touch_id,
2562 double x, double y,
2563 const struct weston_point2d_device_normalized *norm,
2564 int touch_type)
2565{
Louis-Francis Ratté-Boulianne813a06e2017-11-28 20:42:47 -05002566 struct weston_seat *seat = device->aggregate->seat;
2567 struct weston_touch *touch = device->aggregate;
2568
Pekka Paalanenf4062532018-02-26 16:18:29 +02002569 if (touch_type != WL_TOUCH_UP) {
2570 if (weston_touch_device_can_calibrate(device))
2571 assert(norm != NULL);
2572 else
2573 assert(norm == NULL);
2574 }
2575
Louis-Francis Ratté-Boulianne813a06e2017-11-28 20:42:47 -05002576 /* Update touchpoints count regardless of the current mode. */
2577 switch (touch_type) {
2578 case WL_TOUCH_DOWN:
2579 weston_compositor_idle_inhibit(seat->compositor);
2580
2581 touch->num_tp++;
2582 break;
2583 case WL_TOUCH_UP:
2584 if (touch->num_tp == 0) {
2585 /* This can happen if we start out with one or
2586 * more fingers on the touch screen, in which
2587 * case we didn't get the corresponding down
2588 * event. */
Pekka Paalanen332a45e2018-03-02 14:20:59 +02002589 weston_log("Unmatched touch up event on seat %s, device %s\n",
2590 seat->seat_name, device->syspath);
2591 return;
Louis-Francis Ratté-Boulianne813a06e2017-11-28 20:42:47 -05002592 }
2593 weston_compositor_idle_release(seat->compositor);
2594
2595 touch->num_tp--;
2596 break;
2597 default:
2598 break;
2599 }
2600
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002601 /* Properly forward the touch event */
2602 switch (weston_touch_device_get_mode(device)) {
2603 case WESTON_TOUCH_MODE_NORMAL:
2604 case WESTON_TOUCH_MODE_PREP_CALIB:
2605 process_touch_normal(device, time, touch_id, x, y, touch_type);
2606 break;
2607 case WESTON_TOUCH_MODE_CALIB:
2608 case WESTON_TOUCH_MODE_PREP_NORMAL:
Louis-Francis Ratté-Boulianne83630982017-11-28 20:42:47 -05002609 notify_touch_calibrator(device, time, touch_id,
2610 norm, touch_type);
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002611 break;
2612 }
Pekka Paalanenf4062532018-02-26 16:18:29 +02002613}
2614
Jonas Ådahl1679f232014-04-12 09:39:51 +02002615WL_EXPORT void
Pekka Paalanenbcbce332018-02-26 14:43:00 +02002616notify_touch_frame(struct weston_touch_device *device)
Jonas Ådahl1679f232014-04-12 09:39:51 +02002617{
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002618 struct weston_touch_grab *grab;
Jonas Ådahl1679f232014-04-12 09:39:51 +02002619
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002620 switch (weston_touch_device_get_mode(device)) {
2621 case WESTON_TOUCH_MODE_NORMAL:
2622 case WESTON_TOUCH_MODE_PREP_CALIB:
2623 grab = device->aggregate->grab;
2624 grab->interface->frame(grab);
2625 break;
2626 case WESTON_TOUCH_MODE_CALIB:
2627 case WESTON_TOUCH_MODE_PREP_NORMAL:
Louis-Francis Ratté-Boulianne83630982017-11-28 20:42:47 -05002628 notify_touch_calibrator_frame(device);
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002629 break;
2630 }
2631
2632 weston_compositor_update_touch_mode(device->aggregate->seat->compositor);
Jonas Ådahl1679f232014-04-12 09:39:51 +02002633}
2634
Derek Foreman3cc004a2015-11-06 15:56:09 -06002635WL_EXPORT void
Pekka Paalanenbcbce332018-02-26 14:43:00 +02002636notify_touch_cancel(struct weston_touch_device *device)
Derek Foreman3cc004a2015-11-06 15:56:09 -06002637{
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002638 struct weston_touch_grab *grab;
Derek Foreman3cc004a2015-11-06 15:56:09 -06002639
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002640 switch (weston_touch_device_get_mode(device)) {
2641 case WESTON_TOUCH_MODE_NORMAL:
2642 case WESTON_TOUCH_MODE_PREP_CALIB:
2643 grab = device->aggregate->grab;
2644 grab->interface->cancel(grab);
2645 break;
2646 case WESTON_TOUCH_MODE_CALIB:
2647 case WESTON_TOUCH_MODE_PREP_NORMAL:
Louis-Francis Ratté-Boulianne83630982017-11-28 20:42:47 -05002648 notify_touch_calibrator_cancel(device);
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002649 break;
2650 }
2651
2652 weston_compositor_update_touch_mode(device->aggregate->seat->compositor);
Derek Foreman3cc004a2015-11-06 15:56:09 -06002653}
2654
Pekka Paalanen8274d902014-08-06 19:36:51 +03002655static int
2656pointer_cursor_surface_get_label(struct weston_surface *surface,
2657 char *buf, size_t len)
2658{
2659 return snprintf(buf, len, "cursor");
2660}
2661
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002662static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002663pointer_cursor_surface_committed(struct weston_surface *es,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002664 int32_t dx, int32_t dy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002665{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002666 struct weston_pointer *pointer = es->committed_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002667 int x, y;
2668
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002669 if (es->width == 0)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002670 return;
2671
Jason Ekstranda7af7042013-10-12 22:38:11 -05002672 assert(es == pointer->sprite->surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002673
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002674 pointer->hotspot_x -= dx;
2675 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002676
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002677 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
2678 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002679
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002680 weston_view_set_position(pointer->sprite, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002681
2682 empty_region(&es->pending.input);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02002683 empty_region(&es->input);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002684
2685 if (!weston_surface_is_mapped(es)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002686 weston_layer_entry_insert(&es->compositor->cursor_layer.view_list,
2687 &pointer->sprite->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002688 weston_view_update_transform(pointer->sprite);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002689 es->is_mapped = true;
2690 pointer->sprite->is_mapped = true;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002691 }
2692}
2693
2694static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002695pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2696 uint32_t serial, struct wl_resource *surface_resource,
2697 int32_t x, int32_t y)
2698{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002699 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002700 struct weston_surface *surface = NULL;
2701
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +02002702 if (!pointer)
2703 return;
2704
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002705 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002706 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002707
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002708 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002709 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002710 /* pointer->focus->surface->resource can be NULL. Surfaces like the
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02002711 black_surface used in shell.c for fullscreen don't have
2712 a resource, but can still have focus */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002713 if (pointer->focus->surface->resource == NULL)
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02002714 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002715 if (wl_resource_get_client(pointer->focus->surface->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002716 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002717 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002718 return;
2719
Derek Foreman4e53c532015-03-23 10:55:32 -05002720 if (!surface) {
2721 if (pointer->sprite)
2722 pointer_unmap_sprite(pointer);
2723 return;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002724 }
2725
Jonas Ådahlb4070242015-03-18 15:08:03 +08002726 if (pointer->sprite && pointer->sprite->surface == surface &&
2727 pointer->hotspot_x == x && pointer->hotspot_y == y)
2728 return;
2729
Derek Foreman4e53c532015-03-23 10:55:32 -05002730 if (!pointer->sprite || pointer->sprite->surface != surface) {
2731 if (weston_surface_set_role(surface, "wl_pointer-cursor",
2732 resource,
2733 WL_POINTER_ERROR_ROLE) < 0)
2734 return;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002735
Derek Foreman4e53c532015-03-23 10:55:32 -05002736 if (pointer->sprite)
2737 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002738
Derek Foreman4e53c532015-03-23 10:55:32 -05002739 wl_signal_add(&surface->destroy_signal,
2740 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002741
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002742 surface->committed = pointer_cursor_surface_committed;
2743 surface->committed_private = pointer;
Derek Foreman4e53c532015-03-23 10:55:32 -05002744 weston_surface_set_label_func(surface,
2745 pointer_cursor_surface_get_label);
2746 pointer->sprite = weston_view_create(surface);
2747 }
2748
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002749 pointer->hotspot_x = x;
2750 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002751
Alexandros Frantzis28d66342021-06-09 10:56:26 +03002752 if (surface->width != 0) {
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002753 pointer_cursor_surface_committed(surface, 0, 0);
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +02002754 weston_view_schedule_repaint(pointer->sprite);
2755 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002756}
2757
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002758static void
2759pointer_release(struct wl_client *client, struct wl_resource *resource)
2760{
2761 wl_resource_destroy(resource);
2762}
2763
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002764static const struct wl_pointer_interface pointer_interface = {
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002765 pointer_set_cursor,
2766 pointer_release
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002767};
2768
2769static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002770seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2771 uint32_t id)
2772{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002773 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002774 /* We use the pointer_state directly, which means we'll
2775 * give a wl_pointer if the seat has ever had one - even though
2776 * the spec explicitly states that this request only takes effect
2777 * if the seat has the pointer capability.
2778 *
2779 * This prevents a race between the compositor sending new
2780 * capabilities and the client trying to use the old ones.
2781 */
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002782 struct weston_pointer *pointer = seat ? seat->pointer_state : NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002783 struct wl_resource *cr;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002784 struct weston_pointer_client *pointer_client;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002785
Jason Ekstranda85118c2013-06-27 20:17:02 -05002786 cr = wl_resource_create(client, &wl_pointer_interface,
2787 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002788 if (cr == NULL) {
2789 wl_client_post_no_memory(client);
2790 return;
2791 }
2792
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002793 wl_list_init(wl_resource_get_link(cr));
2794 wl_resource_set_implementation(cr, &pointer_interface, pointer,
2795 unbind_pointer_client_resource);
2796
2797 /* If we don't have a pointer_state, the resource is inert, so there
2798 * is nothing more to set up */
2799 if (!pointer)
2800 return;
2801
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002802 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
2803 if (!pointer_client) {
2804 wl_client_post_no_memory(client);
2805 return;
2806 }
2807
2808 wl_list_insert(&pointer_client->pointer_resources,
2809 wl_resource_get_link(cr));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002810
Derek Foreman1281a362015-07-31 16:55:32 -05002811 if (pointer->focus && pointer->focus->surface->resource &&
2812 wl_resource_get_client(pointer->focus->surface->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002813 wl_fixed_t sx, sy;
2814
Derek Foreman1281a362015-07-31 16:55:32 -05002815 weston_view_from_global_fixed(pointer->focus,
2816 pointer->x,
2817 pointer->y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002818 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002819
Neil Roberts96d790e2013-09-19 17:32:00 +01002820 wl_pointer_send_enter(cr,
Derek Foreman1281a362015-07-31 16:55:32 -05002821 pointer->focus_serial,
2822 pointer->focus->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01002823 sx, sy);
Peter Hutterer87743e92016-01-18 16:38:22 +10002824 pointer_send_frame(cr);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002825 }
2826}
2827
2828static void
Alexandros Frantzis2b442482018-02-20 14:05:50 +02002829destroy_keyboard_resource(struct wl_resource *resource)
2830{
2831 struct weston_keyboard *keyboard = wl_resource_get_user_data(resource);
2832
2833 wl_list_remove(wl_resource_get_link(resource));
2834
2835 if (keyboard) {
2836 remove_input_resource_from_timestamps(resource,
2837 &keyboard->timestamps_list);
2838 }
2839}
2840
2841static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002842keyboard_release(struct wl_client *client, struct wl_resource *resource)
2843{
2844 wl_resource_destroy(resource);
2845}
2846
2847static const struct wl_keyboard_interface keyboard_interface = {
2848 keyboard_release
2849};
2850
2851static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002852seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2853 uint32_t id)
2854{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002855 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002856 /* We use the keyboard_state directly, which means we'll
2857 * give a wl_keyboard if the seat has ever had one - even though
2858 * the spec explicitly states that this request only takes effect
2859 * if the seat has the keyboard capability.
2860 *
2861 * This prevents a race between the compositor sending new
2862 * capabilities and the client trying to use the old ones.
2863 */
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002864 struct weston_keyboard *keyboard = seat ? seat->keyboard_state : NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002865 struct wl_resource *cr;
2866
Jason Ekstranda85118c2013-06-27 20:17:02 -05002867 cr = wl_resource_create(client, &wl_keyboard_interface,
2868 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002869 if (cr == NULL) {
2870 wl_client_post_no_memory(client);
2871 return;
2872 }
2873
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002874 wl_list_init(wl_resource_get_link(cr));
2875 wl_resource_set_implementation(cr, &keyboard_interface,
Alexandros Frantzis2b442482018-02-20 14:05:50 +02002876 keyboard, destroy_keyboard_resource);
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002877
2878 /* If we don't have a keyboard_state, the resource is inert, so there
2879 * is nothing more to set up */
2880 if (!keyboard)
2881 return;
2882
Neil Roberts96d790e2013-09-19 17:32:00 +01002883 /* May be moved to focused list later by either
2884 * weston_keyboard_set_focus or directly if this client is already
2885 * focused */
Derek Foreman345c9f32015-06-03 15:53:28 -05002886 wl_list_insert(&keyboard->resource_list, wl_resource_get_link(cr));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002887
Jonny Lamb66a41a02014-08-12 14:58:25 +02002888 if (wl_resource_get_version(cr) >= WL_KEYBOARD_REPEAT_INFO_SINCE_VERSION) {
2889 wl_keyboard_send_repeat_info(cr,
2890 seat->compositor->kb_repeat_rate,
2891 seat->compositor->kb_repeat_delay);
2892 }
Jasper St. Pierred8c6aeb2014-08-04 13:43:24 -04002893
Derek Foremanf8f7fd62017-06-28 11:41:43 -05002894 weston_keyboard_send_keymap(keyboard, cr);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002895
Derek Foreman345c9f32015-06-03 15:53:28 -05002896 if (keyboard->focus && keyboard->focus->resource &&
2897 wl_resource_get_client(keyboard->focus->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01002898 struct weston_surface *surface =
Derek Foreman345c9f32015-06-03 15:53:28 -05002899 (struct weston_surface *)keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +01002900
2901 wl_list_remove(wl_resource_get_link(cr));
Derek Foreman345c9f32015-06-03 15:53:28 -05002902 wl_list_insert(&keyboard->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002903 wl_resource_get_link(cr));
2904 wl_keyboard_send_enter(cr,
Derek Foreman345c9f32015-06-03 15:53:28 -05002905 keyboard->focus_serial,
Neil Roberts96d790e2013-09-19 17:32:00 +01002906 surface->resource,
Derek Foreman345c9f32015-06-03 15:53:28 -05002907 &keyboard->keys);
Neil Roberts96d790e2013-09-19 17:32:00 +01002908
Kirill Chibisovc46c70d2020-06-17 01:43:57 +03002909 send_modifiers_to_resource(keyboard,
2910 cr,
2911 keyboard->focus_serial);
2912
Neil Roberts96d790e2013-09-19 17:32:00 +01002913 /* If this is the first keyboard resource for this
2914 * client... */
Derek Foreman345c9f32015-06-03 15:53:28 -05002915 if (keyboard->focus_resource_list.prev ==
Neil Roberts96d790e2013-09-19 17:32:00 +01002916 wl_resource_get_link(cr))
2917 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002918 }
2919}
2920
2921static void
Alexandros Frantzisd7157842018-02-20 14:07:03 +02002922destroy_touch_resource(struct wl_resource *resource)
2923{
2924 struct weston_touch *touch = wl_resource_get_user_data(resource);
2925
2926 wl_list_remove(wl_resource_get_link(resource));
2927
2928 if (touch) {
2929 remove_input_resource_from_timestamps(resource,
2930 &touch->timestamps_list);
2931 }
2932}
2933
2934static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002935touch_release(struct wl_client *client, struct wl_resource *resource)
2936{
2937 wl_resource_destroy(resource);
2938}
2939
2940static const struct wl_touch_interface touch_interface = {
2941 touch_release
2942};
2943
2944static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002945seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2946 uint32_t id)
2947{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002948 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002949 /* We use the touch_state directly, which means we'll
2950 * give a wl_touch if the seat has ever had one - even though
2951 * the spec explicitly states that this request only takes effect
2952 * if the seat has the touch capability.
2953 *
2954 * This prevents a race between the compositor sending new
2955 * capabilities and the client trying to use the old ones.
2956 */
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002957 struct weston_touch *touch = seat ? seat->touch_state : NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002958 struct wl_resource *cr;
2959
Jason Ekstranda85118c2013-06-27 20:17:02 -05002960 cr = wl_resource_create(client, &wl_touch_interface,
2961 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002962 if (cr == NULL) {
2963 wl_client_post_no_memory(client);
2964 return;
2965 }
2966
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002967 wl_list_init(wl_resource_get_link(cr));
2968 wl_resource_set_implementation(cr, &touch_interface,
Alexandros Frantzisd7157842018-02-20 14:07:03 +02002969 touch, destroy_touch_resource);
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002970
2971 /* If we don't have a touch_state, the resource is inert, so there
2972 * is nothing more to set up */
2973 if (!touch)
2974 return;
2975
Derek Foreman1281a362015-07-31 16:55:32 -05002976 if (touch->focus &&
2977 wl_resource_get_client(touch->focus->surface->resource) == client) {
Chokshi, Mituld6697142015-10-09 08:28:47 +00002978 wl_list_insert(&touch->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002979 wl_resource_get_link(cr));
2980 } else {
Chokshi, Mituld6697142015-10-09 08:28:47 +00002981 wl_list_insert(&touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002982 wl_resource_get_link(cr));
2983 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002984}
2985
Quentin Glidicaab1d362016-03-13 17:49:08 +01002986static void
2987seat_release(struct wl_client *client, struct wl_resource *resource)
2988{
2989 wl_resource_destroy(resource);
2990}
2991
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002992static const struct wl_seat_interface seat_interface = {
2993 seat_get_pointer,
2994 seat_get_keyboard,
2995 seat_get_touch,
Quentin Glidicaab1d362016-03-13 17:49:08 +01002996 seat_release,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002997};
2998
2999static void
3000bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3001{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003002 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003003 struct wl_resource *resource;
3004 enum wl_seat_capability caps = 0;
3005
Jason Ekstranda85118c2013-06-27 20:17:02 -05003006 resource = wl_resource_create(client,
Derek Foreman1909c102015-11-26 14:17:47 -06003007 &wl_seat_interface, version, id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05003008 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003009 wl_resource_set_implementation(resource, &seat_interface, data,
3010 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003011
Derek Foreman1281a362015-07-31 16:55:32 -05003012 if (weston_seat_get_pointer(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003013 caps |= WL_SEAT_CAPABILITY_POINTER;
Derek Foreman1281a362015-07-31 16:55:32 -05003014 if (weston_seat_get_keyboard(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003015 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Derek Foreman1281a362015-07-31 16:55:32 -05003016 if (weston_seat_get_touch(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003017 caps |= WL_SEAT_CAPABILITY_TOUCH;
3018
3019 wl_seat_send_capabilities(resource, caps);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003020 if (version >= WL_SEAT_NAME_SINCE_VERSION)
Rob Bradforde445ae62013-05-31 18:09:51 +01003021 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003022}
3023
Jonas Ådahl30d61d82014-10-22 21:21:17 +02003024static void
3025relative_pointer_destroy(struct wl_client *client,
3026 struct wl_resource *resource)
3027{
3028 wl_resource_destroy(resource);
3029}
3030
3031static const struct zwp_relative_pointer_v1_interface relative_pointer_interface = {
3032 relative_pointer_destroy
3033};
3034
3035static void
3036relative_pointer_manager_destroy(struct wl_client *client,
3037 struct wl_resource *resource)
3038{
3039 wl_resource_destroy(resource);
3040}
3041
3042static void
3043relative_pointer_manager_get_relative_pointer(struct wl_client *client,
3044 struct wl_resource *resource,
3045 uint32_t id,
3046 struct wl_resource *pointer_resource)
3047{
3048 struct weston_pointer *pointer =
3049 wl_resource_get_user_data(pointer_resource);
3050 struct weston_pointer_client *pointer_client;
3051 struct wl_resource *cr;
3052
3053 cr = wl_resource_create(client, &zwp_relative_pointer_v1_interface,
3054 wl_resource_get_version(resource), id);
3055 if (cr == NULL) {
3056 wl_client_post_no_memory(client);
3057 return;
3058 }
3059
3060 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
3061 if (!pointer_client) {
3062 wl_client_post_no_memory(client);
3063 return;
3064 }
3065
3066 wl_list_insert(&pointer_client->relative_pointer_resources,
3067 wl_resource_get_link(cr));
3068 wl_resource_set_implementation(cr, &relative_pointer_interface,
3069 pointer,
3070 unbind_pointer_client_resource);
3071}
3072
3073static const struct zwp_relative_pointer_manager_v1_interface relative_pointer_manager = {
3074 relative_pointer_manager_destroy,
3075 relative_pointer_manager_get_relative_pointer,
3076};
3077
3078static void
3079bind_relative_pointer_manager(struct wl_client *client, void *data,
3080 uint32_t version, uint32_t id)
3081{
3082 struct weston_compositor *compositor = data;
3083 struct wl_resource *resource;
3084
3085 resource = wl_resource_create(client,
3086 &zwp_relative_pointer_manager_v1_interface,
3087 1, id);
3088
3089 wl_resource_set_implementation(resource, &relative_pointer_manager,
3090 compositor,
3091 NULL);
3092}
3093
Giulio Camuffo0358af42016-06-02 21:48:08 +03003094WL_EXPORT int
3095weston_compositor_set_xkb_rule_names(struct weston_compositor *ec,
3096 struct xkb_rule_names *names)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003097{
3098 if (ec->xkb_context == NULL) {
Peter Hutterera2086bb2020-05-13 15:24:27 +10003099 ec->xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003100 if (ec->xkb_context == NULL) {
3101 weston_log("failed to create XKB context\n");
3102 return -1;
3103 }
3104 }
3105
3106 if (names)
3107 ec->xkb_names = *names;
3108 if (!ec->xkb_names.rules)
3109 ec->xkb_names.rules = strdup("evdev");
3110 if (!ec->xkb_names.model)
3111 ec->xkb_names.model = strdup("pc105");
3112 if (!ec->xkb_names.layout)
3113 ec->xkb_names.layout = strdup("us");
3114
3115 return 0;
3116}
3117
Stefan Schmidtfda26522013-09-17 10:54:09 +01003118static void
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003119weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003120{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003121 if (--xkb_info->ref_count > 0)
3122 return;
3123
Ran Benitac9c74152014-08-19 23:59:52 +03003124 xkb_keymap_unref(xkb_info->keymap);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003125
Sebastian Wickabec5122019-11-01 02:38:45 +01003126 os_ro_anonymous_file_destroy(xkb_info->keymap_rofile);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003127 free(xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003128}
3129
3130void
3131weston_compositor_xkb_destroy(struct weston_compositor *ec)
3132{
3133 free((char *) ec->xkb_names.rules);
3134 free((char *) ec->xkb_names.model);
3135 free((char *) ec->xkb_names.layout);
3136 free((char *) ec->xkb_names.variant);
3137 free((char *) ec->xkb_names.options);
Stefan Schmidtfda26522013-09-17 10:54:09 +01003138
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003139 if (ec->xkb_info)
3140 weston_xkb_info_destroy(ec->xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003141 xkb_context_unref(ec->xkb_context);
3142}
3143
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003144static struct weston_xkb_info *
3145weston_xkb_info_create(struct xkb_keymap *keymap)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003146{
Sebastian Wickabec5122019-11-01 02:38:45 +01003147 char *keymap_string;
3148 size_t keymap_size;
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003149 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
3150 if (xkb_info == NULL)
3151 return NULL;
3152
Ran Benita2e1968f2014-08-19 23:59:51 +03003153 xkb_info->keymap = xkb_keymap_ref(keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003154 xkb_info->ref_count = 1;
3155
Ran Benita2e1968f2014-08-19 23:59:51 +03003156 xkb_info->shift_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3157 XKB_MOD_NAME_SHIFT);
3158 xkb_info->caps_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3159 XKB_MOD_NAME_CAPS);
3160 xkb_info->ctrl_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3161 XKB_MOD_NAME_CTRL);
3162 xkb_info->alt_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3163 XKB_MOD_NAME_ALT);
3164 xkb_info->mod2_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3165 "Mod2");
3166 xkb_info->mod3_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3167 "Mod3");
3168 xkb_info->super_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3169 XKB_MOD_NAME_LOGO);
3170 xkb_info->mod5_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3171 "Mod5");
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003172
Ran Benita2e1968f2014-08-19 23:59:51 +03003173 xkb_info->num_led = xkb_keymap_led_get_index(xkb_info->keymap,
3174 XKB_LED_NAME_NUM);
3175 xkb_info->caps_led = xkb_keymap_led_get_index(xkb_info->keymap,
3176 XKB_LED_NAME_CAPS);
3177 xkb_info->scroll_led = xkb_keymap_led_get_index(xkb_info->keymap,
3178 XKB_LED_NAME_SCROLL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003179
Sebastian Wickabec5122019-11-01 02:38:45 +01003180 keymap_string = xkb_keymap_get_as_string(xkb_info->keymap,
Derek Foreman76829fc2017-06-28 12:17:46 -05003181 XKB_KEYMAP_FORMAT_TEXT_V1);
Sebastian Wickabec5122019-11-01 02:38:45 +01003182 if (keymap_string == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003183 weston_log("failed to get string version of keymap\n");
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003184 goto err_keymap;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003185 }
Sebastian Wickabec5122019-11-01 02:38:45 +01003186 keymap_size = strlen(keymap_string) + 1;
3187
3188 xkb_info->keymap_rofile = os_ro_anonymous_file_create(keymap_size,
3189 keymap_string);
3190 free(keymap_string);
3191
3192 if (!xkb_info->keymap_rofile) {
3193 weston_log("failed to create anonymous file for keymap\n");
3194 goto err_keymap;
3195 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003196
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003197 return xkb_info;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003198
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003199err_keymap:
Ran Benita2e1968f2014-08-19 23:59:51 +03003200 xkb_keymap_unref(xkb_info->keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003201 free(xkb_info);
3202 return NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003203}
3204
3205static int
3206weston_compositor_build_global_keymap(struct weston_compositor *ec)
3207{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003208 struct xkb_keymap *keymap;
3209
3210 if (ec->xkb_info != NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003211 return 0;
3212
Ran Benita2e1968f2014-08-19 23:59:51 +03003213 keymap = xkb_keymap_new_from_names(ec->xkb_context,
3214 &ec->xkb_names,
3215 0);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003216 if (keymap == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003217 weston_log("failed to compile global XKB keymap\n");
3218 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
3219 "options %s\n",
3220 ec->xkb_names.rules, ec->xkb_names.model,
3221 ec->xkb_names.layout, ec->xkb_names.variant,
3222 ec->xkb_names.options);
3223 return -1;
3224 }
3225
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003226 ec->xkb_info = weston_xkb_info_create(keymap);
Rui Matos73d93952013-10-24 19:28:41 +02003227 xkb_keymap_unref(keymap);
Stefan Schmidtfda26522013-09-17 10:54:09 +01003228 if (ec->xkb_info == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003229 return -1;
3230
3231 return 0;
3232}
3233
Rui Matos65196bc2013-10-10 19:44:19 +02003234WL_EXPORT void
3235weston_seat_update_keymap(struct weston_seat *seat, struct xkb_keymap *keymap)
3236{
Derek Foreman1281a362015-07-31 16:55:32 -05003237 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
3238
3239 if (!keyboard || !keymap)
Rui Matos65196bc2013-10-10 19:44:19 +02003240 return;
3241
Derek Foreman1281a362015-07-31 16:55:32 -05003242 xkb_keymap_unref(keyboard->pending_keymap);
3243 keyboard->pending_keymap = xkb_keymap_ref(keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02003244
Derek Foreman1281a362015-07-31 16:55:32 -05003245 if (keyboard->keys.size == 0)
Rui Matos65196bc2013-10-10 19:44:19 +02003246 update_keymap(seat);
Rui Matos65196bc2013-10-10 19:44:19 +02003247}
3248
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003249WL_EXPORT int
3250weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
3251{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003252 struct weston_keyboard *keyboard;
3253
Derek Foreman1281a362015-07-31 16:55:32 -05003254 if (seat->keyboard_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003255 seat->keyboard_device_count += 1;
3256 if (seat->keyboard_device_count == 1)
3257 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003258 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003259 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003260
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003261 keyboard = weston_keyboard_create();
3262 if (keyboard == NULL) {
3263 weston_log("failed to allocate weston keyboard struct\n");
3264 return -1;
3265 }
3266
Derek Foreman185d1582017-06-28 11:17:23 -05003267 if (keymap != NULL) {
3268 keyboard->xkb_info = weston_xkb_info_create(keymap);
3269 if (keyboard->xkb_info == NULL)
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02003270 goto err;
Derek Foreman185d1582017-06-28 11:17:23 -05003271 } else {
3272 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
3273 goto err;
3274 keyboard->xkb_info = seat->compositor->xkb_info;
3275 keyboard->xkb_info->ref_count++;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01003276 }
Derek Foreman185d1582017-06-28 11:17:23 -05003277
3278 keyboard->xkb_state.state = xkb_state_new(keyboard->xkb_info->keymap);
3279 if (keyboard->xkb_state.state == NULL) {
3280 weston_log("failed to initialise XKB state\n");
3281 goto err;
3282 }
3283
3284 keyboard->xkb_state.leds = 0;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01003285
Derek Foreman1281a362015-07-31 16:55:32 -05003286 seat->keyboard_state = keyboard;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02003287 seat->keyboard_device_count = 1;
3288 keyboard->seat = seat;
3289
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003290 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003291
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003292 return 0;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02003293
3294err:
3295 if (keyboard->xkb_info)
3296 weston_xkb_info_destroy(keyboard->xkb_info);
3297 free(keyboard);
3298
3299 return -1;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003300}
3301
Jonas Ådahl91fed542013-12-03 09:14:27 +01003302static void
3303weston_keyboard_reset_state(struct weston_keyboard *keyboard)
3304{
3305 struct weston_seat *seat = keyboard->seat;
3306 struct xkb_state *state;
3307
Derek Foreman185d1582017-06-28 11:17:23 -05003308 state = xkb_state_new(keyboard->xkb_info->keymap);
3309 if (!state) {
3310 weston_log("failed to reset XKB state\n");
3311 return;
Jonas Ådahl91fed542013-12-03 09:14:27 +01003312 }
Derek Foreman185d1582017-06-28 11:17:23 -05003313 xkb_state_unref(keyboard->xkb_state.state);
3314 keyboard->xkb_state.state = state;
3315
3316 keyboard->xkb_state.leds = 0;
Jonas Ådahl91fed542013-12-03 09:14:27 +01003317
3318 seat->modifier_state = 0;
3319}
3320
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003321WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003322weston_seat_release_keyboard(struct weston_seat *seat)
3323{
3324 seat->keyboard_device_count--;
Derek Foremand621df22014-11-19 11:04:12 -06003325 assert(seat->keyboard_device_count >= 0);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003326 if (seat->keyboard_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05003327 weston_keyboard_set_focus(seat->keyboard_state, NULL);
3328 weston_keyboard_cancel_grab(seat->keyboard_state);
3329 weston_keyboard_reset_state(seat->keyboard_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003330 seat_send_updated_caps(seat);
3331 }
3332}
3333
3334WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003335weston_seat_init_pointer(struct weston_seat *seat)
3336{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003337 struct weston_pointer *pointer;
3338
Derek Foreman1281a362015-07-31 16:55:32 -05003339 if (seat->pointer_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003340 seat->pointer_device_count += 1;
3341 if (seat->pointer_device_count == 1)
3342 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003343 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003344 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003345
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003346 pointer = weston_pointer_create(seat);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003347 if (pointer == NULL)
3348 return;
3349
Derek Foreman1281a362015-07-31 16:55:32 -05003350 seat->pointer_state = pointer;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003351 seat->pointer_device_count = 1;
3352 pointer->seat = seat;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003353
3354 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003355}
3356
3357WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003358weston_seat_release_pointer(struct weston_seat *seat)
3359{
Derek Foreman1281a362015-07-31 16:55:32 -05003360 struct weston_pointer *pointer = seat->pointer_state;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003361
3362 seat->pointer_device_count--;
3363 if (seat->pointer_device_count == 0) {
Derek Foremanf9318d12015-05-11 15:40:11 -05003364 weston_pointer_clear_focus(pointer);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003365 weston_pointer_cancel_grab(pointer);
Jonas Ådahl630bae82013-10-17 23:04:06 +02003366
Jonas Ådahla4932742013-10-17 23:04:07 +02003367 if (pointer->sprite)
3368 pointer_unmap_sprite(pointer);
3369
Jonas Ådahl3e12e632013-12-02 22:05:05 +01003370 weston_pointer_reset_state(pointer);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003371 seat_send_updated_caps(seat);
Derek Foremanfd5ca512015-01-07 15:00:25 -06003372
3373 /* seat->pointer is intentionally not destroyed so that
3374 * a newly attached pointer on this seat will retain
3375 * the previous cursor co-ordinates.
3376 */
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003377 }
3378}
3379
3380WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003381weston_seat_init_touch(struct weston_seat *seat)
3382{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003383 struct weston_touch *touch;
3384
Derek Foreman1281a362015-07-31 16:55:32 -05003385 if (seat->touch_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003386 seat->touch_device_count += 1;
3387 if (seat->touch_device_count == 1)
3388 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003389 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003390 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003391
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003392 touch = weston_touch_create();
3393 if (touch == NULL)
3394 return;
3395
Derek Foreman1281a362015-07-31 16:55:32 -05003396 seat->touch_state = touch;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003397 seat->touch_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003398 touch->seat = seat;
3399
3400 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003401}
3402
3403WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003404weston_seat_release_touch(struct weston_seat *seat)
3405{
3406 seat->touch_device_count--;
3407 if (seat->touch_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05003408 weston_touch_set_focus(seat->touch_state, NULL);
3409 weston_touch_cancel_grab(seat->touch_state);
3410 weston_touch_reset_state(seat->touch_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003411 seat_send_updated_caps(seat);
3412 }
3413}
3414
3415WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003416weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
3417 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003418{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003419 memset(seat, 0, sizeof *seat);
3420
Kristian Høgsberge3148752013-05-06 23:19:49 -04003421 seat->selection_data_source = NULL;
3422 wl_list_init(&seat->base_resource_list);
3423 wl_signal_init(&seat->selection_signal);
3424 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04003425 wl_signal_init(&seat->destroy_signal);
Jason Ekstranda4ab5422014-04-02 19:53:45 -05003426 wl_signal_init(&seat->updated_caps_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003427
Sebastian Wickabec5122019-11-01 02:38:45 +01003428 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface,
3429 MIN(wl_seat_interface.version, 7),
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003430 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003431
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003432 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003433 seat->modifier_state = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003434 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003435
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003436 wl_list_insert(ec->seat_list.prev, &seat->link);
3437
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003438 clipboard_create(seat);
3439
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003440 wl_signal_emit(&ec->seat_created_signal, seat);
3441}
3442
3443WL_EXPORT void
3444weston_seat_release(struct weston_seat *seat)
3445{
Alexandros Frantzis8480d132018-02-15 13:07:09 +02003446 struct wl_resource *resource;
3447
3448 wl_resource_for_each(resource, &seat->base_resource_list) {
3449 wl_resource_set_user_data(resource, NULL);
3450 }
3451
3452 wl_resource_for_each(resource, &seat->drag_resource_list) {
3453 wl_resource_set_user_data(resource, NULL);
3454 }
3455
3456 wl_list_remove(&seat->base_resource_list);
3457 wl_list_remove(&seat->drag_resource_list);
3458
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003459 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003460
Jonas Ådahl1afb2382014-01-03 19:46:51 +01003461 if (seat->saved_kbd_focus)
3462 wl_list_remove(&seat->saved_kbd_focus_listener.link);
3463
Derek Foreman1281a362015-07-31 16:55:32 -05003464 if (seat->pointer_state)
3465 weston_pointer_destroy(seat->pointer_state);
3466 if (seat->keyboard_state)
3467 weston_keyboard_destroy(seat->keyboard_state);
3468 if (seat->touch_state)
3469 weston_touch_destroy(seat->touch_state);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003470
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003471 free (seat->seat_name);
3472
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003473 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04003474
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003475 wl_signal_emit(&seat->destroy_signal, seat);
3476}
Derek Foreman1281a362015-07-31 16:55:32 -05003477
3478/** Get a seat's keyboard pointer
3479 *
3480 * \param seat The seat to query
3481 * \return The seat's keyboard pointer, or NULL if no keyboard is present
3482 *
3483 * The keyboard pointer for a seat isn't freed when all keyboards are removed,
3484 * so it should only be used when the seat's keyboard_device_count is greater
3485 * than zero. This function does that test and only returns a pointer
3486 * when a keyboard is present.
3487 */
3488WL_EXPORT struct weston_keyboard *
3489weston_seat_get_keyboard(struct weston_seat *seat)
3490{
3491 if (!seat)
3492 return NULL;
3493
3494 if (seat->keyboard_device_count)
3495 return seat->keyboard_state;
3496
3497 return NULL;
3498}
3499
3500/** Get a seat's pointer pointer
3501 *
3502 * \param seat The seat to query
3503 * \return The seat's pointer pointer, or NULL if no pointer device is present
3504 *
3505 * The pointer pointer for a seat isn't freed when all mice are removed,
3506 * so it should only be used when the seat's pointer_device_count is greater
3507 * than zero. This function does that test and only returns a pointer
3508 * when a pointing device is present.
3509 */
3510WL_EXPORT struct weston_pointer *
3511weston_seat_get_pointer(struct weston_seat *seat)
3512{
3513 if (!seat)
3514 return NULL;
3515
3516 if (seat->pointer_device_count)
3517 return seat->pointer_state;
3518
3519 return NULL;
3520}
3521
Jonas Ådahld3414f22016-07-22 17:56:31 +08003522static const struct zwp_locked_pointer_v1_interface locked_pointer_interface;
3523static const struct zwp_confined_pointer_v1_interface confined_pointer_interface;
3524
3525static enum pointer_constraint_type
3526pointer_constraint_get_type(struct weston_pointer_constraint *constraint)
3527{
3528 if (wl_resource_instance_of(constraint->resource,
3529 &zwp_locked_pointer_v1_interface,
3530 &locked_pointer_interface)) {
3531 return POINTER_CONSTRAINT_TYPE_LOCK;
3532 } else if (wl_resource_instance_of(constraint->resource,
3533 &zwp_confined_pointer_v1_interface,
3534 &confined_pointer_interface)) {
3535 return POINTER_CONSTRAINT_TYPE_CONFINE;
3536 }
3537
3538 abort();
3539 return 0;
3540}
3541
3542static void
3543pointer_constraint_notify_activated(struct weston_pointer_constraint *constraint)
3544{
3545 struct wl_resource *resource = constraint->resource;
3546
3547 switch (pointer_constraint_get_type(constraint)) {
3548 case POINTER_CONSTRAINT_TYPE_LOCK:
3549 zwp_locked_pointer_v1_send_locked(resource);
3550 break;
3551 case POINTER_CONSTRAINT_TYPE_CONFINE:
3552 zwp_confined_pointer_v1_send_confined(resource);
3553 break;
3554 }
3555}
3556
3557static void
3558pointer_constraint_notify_deactivated(struct weston_pointer_constraint *constraint)
3559{
3560 struct wl_resource *resource = constraint->resource;
3561
3562 switch (pointer_constraint_get_type(constraint)) {
3563 case POINTER_CONSTRAINT_TYPE_LOCK:
3564 zwp_locked_pointer_v1_send_unlocked(resource);
3565 break;
3566 case POINTER_CONSTRAINT_TYPE_CONFINE:
3567 zwp_confined_pointer_v1_send_unconfined(resource);
3568 break;
3569 }
3570}
3571
3572static struct weston_pointer_constraint *
3573get_pointer_constraint_for_pointer(struct weston_surface *surface,
3574 struct weston_pointer *pointer)
3575{
3576 struct weston_pointer_constraint *constraint;
3577
3578 wl_list_for_each(constraint, &surface->pointer_constraints, link) {
3579 if (constraint->pointer == pointer)
3580 return constraint;
3581 }
3582
3583 return NULL;
3584}
3585
Derek Foreman1281a362015-07-31 16:55:32 -05003586/** Get a seat's touch pointer
3587 *
3588 * \param seat The seat to query
3589 * \return The seat's touch pointer, or NULL if no touch device is present
3590 *
3591 * The touch pointer for a seat isn't freed when all touch devices are removed,
3592 * so it should only be used when the seat's touch_device_count is greater
3593 * than zero. This function does that test and only returns a pointer
3594 * when a touch device is present.
3595 */
3596WL_EXPORT struct weston_touch *
3597weston_seat_get_touch(struct weston_seat *seat)
3598{
3599 if (!seat)
3600 return NULL;
3601
3602 if (seat->touch_device_count)
3603 return seat->touch_state;
3604
3605 return NULL;
3606}
Bryce Harrington24f917e2016-06-29 19:04:07 -07003607
3608/** Sets the keyboard focus to the given surface
3609 *
Marius Vlada2dace22019-06-12 16:05:44 +03003610 * \param surface the surface to focus on
Bryce Harrington24f917e2016-06-29 19:04:07 -07003611 * \param seat The seat to query
3612 */
3613WL_EXPORT void
3614weston_seat_set_keyboard_focus(struct weston_seat *seat,
3615 struct weston_surface *surface)
3616{
3617 struct weston_compositor *compositor = seat->compositor;
3618 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003619 struct weston_surface_activation_data activation_data;
Bryce Harrington24f917e2016-06-29 19:04:07 -07003620
Jonas Ådahlef8e1c32016-03-15 20:28:51 +08003621 if (keyboard && keyboard->focus != surface) {
Bryce Harrington24f917e2016-06-29 19:04:07 -07003622 weston_keyboard_set_focus(keyboard, surface);
3623 wl_data_device_set_keyboard_focus(seat);
3624 }
3625
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003626 inc_activate_serial(compositor);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003627
3628 activation_data = (struct weston_surface_activation_data) {
3629 .surface = surface,
3630 .seat = seat,
3631 };
3632 wl_signal_emit(&compositor->activate_signal, &activation_data);
Bryce Harrington24f917e2016-06-29 19:04:07 -07003633}
Jonas Ådahl30d61d82014-10-22 21:21:17 +02003634
Jonas Ådahld3414f22016-07-22 17:56:31 +08003635static void
3636enable_pointer_constraint(struct weston_pointer_constraint *constraint,
3637 struct weston_view *view)
3638{
3639 assert(constraint->view == NULL);
3640 constraint->view = view;
3641 pointer_constraint_notify_activated(constraint);
3642 weston_pointer_start_grab(constraint->pointer, &constraint->grab);
3643 wl_list_remove(&constraint->surface_destroy_listener.link);
3644 wl_list_init(&constraint->surface_destroy_listener.link);
3645}
3646
3647static bool
3648is_pointer_constraint_enabled(struct weston_pointer_constraint *constraint)
3649{
3650 return constraint->view != NULL;
3651}
3652
3653static void
3654weston_pointer_constraint_disable(struct weston_pointer_constraint *constraint)
3655{
3656 constraint->view = NULL;
3657 pointer_constraint_notify_deactivated(constraint);
3658 weston_pointer_end_grab(constraint->grab.pointer);
3659}
3660
3661void
3662weston_pointer_constraint_destroy(struct weston_pointer_constraint *constraint)
3663{
3664 if (is_pointer_constraint_enabled(constraint))
3665 weston_pointer_constraint_disable(constraint);
3666
3667 wl_list_remove(&constraint->pointer_destroy_listener.link);
3668 wl_list_remove(&constraint->surface_destroy_listener.link);
3669 wl_list_remove(&constraint->surface_commit_listener.link);
3670 wl_list_remove(&constraint->surface_activate_listener.link);
3671
3672 wl_resource_set_user_data(constraint->resource, NULL);
3673 pixman_region32_fini(&constraint->region);
3674 wl_list_remove(&constraint->link);
3675 free(constraint);
3676}
3677
3678static void
3679disable_pointer_constraint(struct weston_pointer_constraint *constraint)
3680{
3681 switch (constraint->lifetime) {
3682 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT:
3683 weston_pointer_constraint_destroy(constraint);
3684 break;
3685 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT:
3686 weston_pointer_constraint_disable(constraint);
3687 break;
3688 }
3689}
3690
3691static bool
3692is_within_constraint_region(struct weston_pointer_constraint *constraint,
3693 wl_fixed_t sx, wl_fixed_t sy)
3694{
3695 struct weston_surface *surface = constraint->surface;
3696 pixman_region32_t constraint_region;
3697 bool result;
3698
3699 pixman_region32_init(&constraint_region);
3700 pixman_region32_intersect(&constraint_region,
3701 &surface->input,
3702 &constraint->region);
3703 result = pixman_region32_contains_point(&constraint_region,
3704 wl_fixed_to_int(sx),
3705 wl_fixed_to_int(sy),
3706 NULL);
3707 pixman_region32_fini(&constraint_region);
3708
3709 return result;
3710}
3711
3712static void
3713maybe_enable_pointer_constraint(struct weston_pointer_constraint *constraint)
3714{
3715 struct weston_surface *surface = constraint->surface;
3716 struct weston_view *vit;
3717 struct weston_view *view = NULL;
3718 struct weston_pointer *pointer = constraint->pointer;
3719 struct weston_keyboard *keyboard;
3720 struct weston_seat *seat = pointer->seat;
3721 int32_t x, y;
3722
3723 /* Postpone if no view of the surface was most recently clicked. */
3724 wl_list_for_each(vit, &surface->views, surface_link) {
3725 if (vit->click_to_activate_serial ==
3726 surface->compositor->activate_serial) {
3727 view = vit;
3728 }
3729 }
3730 if (view == NULL)
3731 return;
3732
3733 /* Postpone if surface doesn't have keyboard focus. */
3734 keyboard = weston_seat_get_keyboard(seat);
3735 if (!keyboard || keyboard->focus != surface)
3736 return;
3737
3738 /* Postpone constraint if the pointer is not within the
3739 * constraint region.
3740 */
3741 weston_view_from_global(view,
3742 wl_fixed_to_int(pointer->x),
3743 wl_fixed_to_int(pointer->y),
3744 &x, &y);
3745 if (!is_within_constraint_region(constraint,
3746 wl_fixed_from_int(x),
3747 wl_fixed_from_int(y)))
3748 return;
3749
3750 enable_pointer_constraint(constraint, view);
3751}
3752
3753static void
3754locked_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
3755{
3756}
3757
3758static void
3759locked_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02003760 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003761 struct weston_pointer_motion_event *event)
3762{
Quentin Glidiccde13452016-08-12 10:41:32 +02003763 pointer_send_relative_motion(grab->pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08003764}
3765
3766static void
3767locked_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003768 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003769 uint32_t button,
3770 uint32_t state_w)
3771{
3772 weston_pointer_send_button(grab->pointer, time, button, state_w);
3773}
3774
3775static void
3776locked_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02003777 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003778 struct weston_pointer_axis_event *event)
3779{
3780 weston_pointer_send_axis(grab->pointer, time, event);
3781}
3782
3783static void
3784locked_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
3785 uint32_t source)
3786{
3787 weston_pointer_send_axis_source(grab->pointer, source);
3788}
3789
3790static void
3791locked_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
3792{
3793 weston_pointer_send_frame(grab->pointer);
3794}
3795
3796static void
3797locked_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
3798{
3799 struct weston_pointer_constraint *constraint =
3800 container_of(grab, struct weston_pointer_constraint, grab);
3801
3802 disable_pointer_constraint(constraint);
3803}
3804
3805static const struct weston_pointer_grab_interface
3806 locked_pointer_grab_interface = {
3807 locked_pointer_grab_pointer_focus,
3808 locked_pointer_grab_pointer_motion,
3809 locked_pointer_grab_pointer_button,
3810 locked_pointer_grab_pointer_axis,
3811 locked_pointer_grab_pointer_axis_source,
3812 locked_pointer_grab_pointer_frame,
3813 locked_pointer_grab_pointer_cancel,
3814};
3815
3816static void
3817pointer_constraint_constrain_resource_destroyed(struct wl_resource *resource)
3818{
3819 struct weston_pointer_constraint *constraint =
3820 wl_resource_get_user_data(resource);
3821
3822 if (!constraint)
3823 return;
3824
3825 weston_pointer_constraint_destroy(constraint);
3826}
3827
3828static void
3829pointer_constraint_surface_activate(struct wl_listener *listener, void *data)
3830{
3831 struct weston_surface_activation_data *activation = data;
3832 struct weston_pointer *pointer;
3833 struct weston_surface *focus = activation->surface;
3834 struct weston_pointer_constraint *constraint =
3835 container_of(listener, struct weston_pointer_constraint,
3836 surface_activate_listener);
3837 bool is_constraint_surface;
3838
3839 pointer = weston_seat_get_pointer(activation->seat);
3840 if (!pointer)
3841 return;
3842
3843 is_constraint_surface =
3844 get_pointer_constraint_for_pointer(focus, pointer) == constraint;
3845
3846 if (is_constraint_surface &&
3847 !is_pointer_constraint_enabled(constraint))
3848 maybe_enable_pointer_constraint(constraint);
3849 else if (!is_constraint_surface &&
3850 is_pointer_constraint_enabled(constraint))
3851 disable_pointer_constraint(constraint);
3852}
3853
3854static void
3855pointer_constraint_pointer_destroyed(struct wl_listener *listener, void *data)
3856{
3857 struct weston_pointer_constraint *constraint =
3858 container_of(listener, struct weston_pointer_constraint,
3859 pointer_destroy_listener);
3860
3861 weston_pointer_constraint_destroy(constraint);
3862}
3863
3864static void
3865pointer_constraint_surface_destroyed(struct wl_listener *listener, void *data)
3866{
3867 struct weston_pointer_constraint *constraint =
3868 container_of(listener, struct weston_pointer_constraint,
3869 surface_destroy_listener);
3870
3871 weston_pointer_constraint_destroy(constraint);
3872}
3873
3874static void
3875pointer_constraint_surface_committed(struct wl_listener *listener, void *data)
3876{
3877 struct weston_pointer_constraint *constraint =
3878 container_of(listener, struct weston_pointer_constraint,
3879 surface_commit_listener);
3880
3881 if (constraint->region_is_pending) {
3882 constraint->region_is_pending = false;
3883 pixman_region32_copy(&constraint->region,
3884 &constraint->region_pending);
3885 pixman_region32_fini(&constraint->region_pending);
3886 pixman_region32_init(&constraint->region_pending);
3887 }
3888
3889 if (constraint->hint_is_pending) {
3890 constraint->hint_is_pending = false;
3891
3892 constraint->hint_is_pending = true;
3893 constraint->hint_x = constraint->hint_x_pending;
3894 constraint->hint_y = constraint->hint_y_pending;
3895 }
3896
3897 if (pointer_constraint_get_type(constraint) ==
3898 POINTER_CONSTRAINT_TYPE_CONFINE &&
3899 is_pointer_constraint_enabled(constraint))
3900 maybe_warp_confined_pointer(constraint);
3901}
3902
3903static struct weston_pointer_constraint *
3904weston_pointer_constraint_create(struct weston_surface *surface,
3905 struct weston_pointer *pointer,
3906 struct weston_region *region,
3907 enum zwp_pointer_constraints_v1_lifetime lifetime,
3908 struct wl_resource *cr,
3909 const struct weston_pointer_grab_interface *grab_interface)
3910{
3911 struct weston_pointer_constraint *constraint;
3912
3913 constraint = zalloc(sizeof *constraint);
3914 if (!constraint)
3915 return NULL;
3916
3917 constraint->lifetime = lifetime;
3918 pixman_region32_init(&constraint->region);
3919 pixman_region32_init(&constraint->region_pending);
3920 wl_list_insert(&surface->pointer_constraints, &constraint->link);
3921 constraint->surface = surface;
3922 constraint->pointer = pointer;
3923 constraint->resource = cr;
3924 constraint->grab.interface = grab_interface;
3925 if (region) {
3926 pixman_region32_copy(&constraint->region,
3927 &region->region);
3928 } else {
3929 pixman_region32_fini(&constraint->region);
3930 region_init_infinite(&constraint->region);
3931 }
3932
3933 constraint->surface_activate_listener.notify =
3934 pointer_constraint_surface_activate;
3935 constraint->surface_destroy_listener.notify =
3936 pointer_constraint_surface_destroyed;
3937 constraint->surface_commit_listener.notify =
3938 pointer_constraint_surface_committed;
3939 constraint->pointer_destroy_listener.notify =
3940 pointer_constraint_pointer_destroyed;
3941
3942 wl_signal_add(&surface->compositor->activate_signal,
3943 &constraint->surface_activate_listener);
3944 wl_signal_add(&pointer->destroy_signal,
3945 &constraint->pointer_destroy_listener);
3946 wl_signal_add(&surface->destroy_signal,
3947 &constraint->surface_destroy_listener);
3948 wl_signal_add(&surface->commit_signal,
3949 &constraint->surface_commit_listener);
3950
3951 return constraint;
3952}
3953
3954static void
3955init_pointer_constraint(struct wl_resource *pointer_constraints_resource,
3956 uint32_t id,
3957 struct weston_surface *surface,
3958 struct weston_pointer *pointer,
3959 struct weston_region *region,
3960 enum zwp_pointer_constraints_v1_lifetime lifetime,
3961 const struct wl_interface *interface,
3962 const void *implementation,
3963 const struct weston_pointer_grab_interface *grab_interface)
3964{
3965 struct wl_client *client =
3966 wl_resource_get_client(pointer_constraints_resource);
3967 struct wl_resource *cr;
3968 struct weston_pointer_constraint *constraint;
3969
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02003970 if (pointer && get_pointer_constraint_for_pointer(surface, pointer)) {
Jonas Ådahld3414f22016-07-22 17:56:31 +08003971 wl_resource_post_error(pointer_constraints_resource,
3972 ZWP_POINTER_CONSTRAINTS_V1_ERROR_ALREADY_CONSTRAINED,
3973 "the pointer has a lock/confine request on this surface");
3974 return;
3975 }
3976
3977 cr = wl_resource_create(client, interface,
3978 wl_resource_get_version(pointer_constraints_resource),
3979 id);
3980 if (cr == NULL) {
3981 wl_client_post_no_memory(client);
3982 return;
3983 }
3984
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02003985 if (pointer) {
3986 constraint = weston_pointer_constraint_create(surface, pointer,
3987 region, lifetime,
3988 cr, grab_interface);
3989 if (constraint == NULL) {
3990 wl_client_post_no_memory(client);
3991 return;
3992 }
3993 } else {
3994 constraint = NULL;
Jonas Ådahld3414f22016-07-22 17:56:31 +08003995 }
3996
3997 wl_resource_set_implementation(cr, implementation, constraint,
3998 pointer_constraint_constrain_resource_destroyed);
3999
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02004000 if (constraint)
4001 maybe_enable_pointer_constraint(constraint);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004002}
4003
4004static void
4005pointer_constraints_destroy(struct wl_client *client,
4006 struct wl_resource *resource)
4007{
4008 wl_resource_destroy(resource);
4009}
4010
4011static void
4012locked_pointer_destroy(struct wl_client *client,
4013 struct wl_resource *resource)
4014{
4015 struct weston_pointer_constraint *constraint =
4016 wl_resource_get_user_data(resource);
4017 wl_fixed_t x, y;
4018
4019 if (constraint && constraint->view && constraint->hint_is_pending &&
4020 is_within_constraint_region(constraint,
4021 constraint->hint_x,
4022 constraint->hint_y)) {
4023 weston_view_to_global_fixed(constraint->view,
4024 constraint->hint_x,
4025 constraint->hint_y,
4026 &x, &y);
4027 weston_pointer_move_to(constraint->pointer, x, y);
4028 }
4029 wl_resource_destroy(resource);
4030}
4031
4032static void
4033locked_pointer_set_cursor_position_hint(struct wl_client *client,
4034 struct wl_resource *resource,
4035 wl_fixed_t surface_x,
4036 wl_fixed_t surface_y)
4037{
4038 struct weston_pointer_constraint *constraint =
4039 wl_resource_get_user_data(resource);
4040
4041 /* Ignore a set cursor hint that was sent after the lock was cancelled.
4042 */
4043 if (!constraint ||
4044 !constraint->resource ||
4045 constraint->resource != resource)
4046 return;
4047
4048 constraint->hint_is_pending = true;
4049 constraint->hint_x_pending = surface_x;
4050 constraint->hint_y_pending = surface_y;
4051}
4052
4053static void
4054locked_pointer_set_region(struct wl_client *client,
4055 struct wl_resource *resource,
4056 struct wl_resource *region_resource)
4057{
4058 struct weston_pointer_constraint *constraint =
4059 wl_resource_get_user_data(resource);
4060 struct weston_region *region = region_resource ?
4061 wl_resource_get_user_data(region_resource) : NULL;
4062
4063 if (!constraint)
4064 return;
4065
4066 if (region) {
4067 pixman_region32_copy(&constraint->region_pending,
4068 &region->region);
4069 } else {
4070 pixman_region32_fini(&constraint->region_pending);
4071 region_init_infinite(&constraint->region_pending);
4072 }
4073 constraint->region_is_pending = true;
4074}
4075
4076
4077static const struct zwp_locked_pointer_v1_interface locked_pointer_interface = {
4078 locked_pointer_destroy,
4079 locked_pointer_set_cursor_position_hint,
4080 locked_pointer_set_region,
4081};
4082
4083static void
4084pointer_constraints_lock_pointer(struct wl_client *client,
4085 struct wl_resource *resource,
4086 uint32_t id,
4087 struct wl_resource *surface_resource,
4088 struct wl_resource *pointer_resource,
4089 struct wl_resource *region_resource,
4090 uint32_t lifetime)
4091{
4092 struct weston_surface *surface =
4093 wl_resource_get_user_data(surface_resource);
4094 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
4095 struct weston_region *region = region_resource ?
4096 wl_resource_get_user_data(region_resource) : NULL;
4097
4098 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
4099 &zwp_locked_pointer_v1_interface,
4100 &locked_pointer_interface,
4101 &locked_pointer_grab_interface);
4102}
4103
4104static void
4105confined_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
4106{
4107}
4108
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004109static double
4110vec2d_cross_product(struct vec2d a, struct vec2d b)
4111{
4112 return a.x * b.y - a.y * b.x;
4113}
4114
4115static struct vec2d
4116vec2d_add(struct vec2d a, struct vec2d b)
4117{
4118 return (struct vec2d) {
4119 .x = a.x + b.x,
4120 .y = a.y + b.y,
4121 };
4122}
4123
4124static struct vec2d
4125vec2d_subtract(struct vec2d a, struct vec2d b)
4126{
4127 return (struct vec2d) {
4128 .x = a.x - b.x,
4129 .y = a.y - b.y,
4130 };
4131}
4132
4133static struct vec2d
4134vec2d_multiply_constant(double c, struct vec2d a)
4135{
4136 return (struct vec2d) {
4137 .x = c * a.x,
4138 .y = c * a.y,
4139 };
4140}
4141
4142static bool
4143lines_intersect(struct line *line1, struct line *line2,
4144 struct vec2d *intersection)
4145{
4146 struct vec2d p = line1->a;
4147 struct vec2d r = vec2d_subtract(line1->b, line1->a);
4148 struct vec2d q = line2->a;
4149 struct vec2d s = vec2d_subtract(line2->b, line2->a);
4150 double rxs;
4151 double sxr;
4152 double t;
4153 double u;
4154
4155 /*
4156 * The line (p, r) and (q, s) intersects where
4157 *
4158 * p + t r = q + u s
4159 *
4160 * Calculate t:
4161 *
4162 * (p + t r) × s = (q + u s) × s
4163 * p × s + t (r × s) = q × s + u (s × s)
4164 * p × s + t (r × s) = q × s
4165 * t (r × s) = q × s - p × s
4166 * t (r × s) = (q - p) × s
4167 * t = ((q - p) × s) / (r × s)
4168 *
4169 * Using the same method, for u we get:
4170 *
4171 * u = ((p - q) × r) / (s × r)
4172 */
4173
4174 rxs = vec2d_cross_product(r, s);
4175 sxr = vec2d_cross_product(s, r);
4176
4177 /* If r × s = 0 then the lines are either parallel or collinear. */
4178 if (fabs(rxs) < DBL_MIN)
4179 return false;
4180
4181 t = vec2d_cross_product(vec2d_subtract(q, p), s) / rxs;
4182 u = vec2d_cross_product(vec2d_subtract(p, q), r) / sxr;
4183
4184 /* The lines only intersect if 0 ≤ t ≤ 1 and 0 ≤ u ≤ 1. */
4185 if (t < 0.0 || t > 1.0 || u < 0.0 || u > 1.0)
4186 return false;
4187
4188 *intersection = vec2d_add(p, vec2d_multiply_constant(t, r));
4189 return true;
4190}
4191
4192static struct border *
4193add_border(struct wl_array *array,
4194 double x1, double y1,
4195 double x2, double y2,
4196 enum motion_direction blocking_dir)
4197{
4198 struct border *border = wl_array_add(array, sizeof *border);
4199
4200 *border = (struct border) {
4201 .line = (struct line) {
4202 .a = (struct vec2d) {
4203 .x = x1,
4204 .y = y1,
4205 },
4206 .b = (struct vec2d) {
4207 .x = x2,
4208 .y = y2,
4209 },
4210 },
4211 .blocking_dir = blocking_dir,
4212 };
4213
4214 return border;
4215}
4216
4217static int
4218compare_lines_x(const void *a, const void *b)
4219{
4220 const struct border *border_a = a;
4221 const struct border *border_b = b;
4222
4223
4224 if (border_a->line.a.x == border_b->line.a.x)
4225 return border_a->line.b.x < border_b->line.b.x;
4226 else
4227 return border_a->line.a.x > border_b->line.a.x;
4228}
4229
4230static void
4231add_non_overlapping_edges(pixman_box32_t *boxes,
4232 int band_above_start,
4233 int band_below_start,
4234 int band_below_end,
4235 struct wl_array *borders)
4236{
4237 int i;
4238 struct wl_array band_merge;
4239 struct border *border;
4240 struct border *prev_border;
4241 struct border *new_border;
4242
4243 wl_array_init(&band_merge);
4244
4245 /* Add bottom band of previous row, and top band of current row, and
4246 * sort them so lower left x coordinate comes first. If there are two
4247 * borders with the same left x coordinate, the wider one comes first.
4248 */
4249 for (i = band_above_start; i < band_below_start; i++) {
4250 pixman_box32_t *box = &boxes[i];
4251 add_border(&band_merge, box->x1, box->y2, box->x2, box->y2,
4252 MOTION_DIRECTION_POSITIVE_Y);
4253 }
4254 for (i = band_below_start; i < band_below_end; i++) {
4255 pixman_box32_t *box= &boxes[i];
4256 add_border(&band_merge, box->x1, box->y1, box->x2, box->y1,
4257 MOTION_DIRECTION_NEGATIVE_Y);
4258 }
4259 qsort(band_merge.data,
4260 band_merge.size / sizeof *border,
4261 sizeof *border,
4262 compare_lines_x);
4263
4264 /* Combine the two combined bands so that any overlapping border is
4265 * eliminated. */
4266 prev_border = NULL;
4267 wl_array_for_each(border, &band_merge) {
4268 assert(border->line.a.y == border->line.b.y);
4269 assert(!prev_border ||
4270 prev_border->line.a.y == border->line.a.y);
4271 assert(!prev_border ||
4272 (prev_border->line.a.x != border->line.a.x ||
4273 prev_border->line.b.x != border->line.b.x));
4274 assert(!prev_border ||
4275 prev_border->line.a.x <= border->line.a.x);
4276
4277 if (prev_border &&
4278 prev_border->line.a.x == border->line.a.x) {
4279 /*
4280 * ------------ +
4281 * ------- =
4282 * [ ]-----
4283 */
4284 prev_border->line.a.x = border->line.b.x;
4285 } else if (prev_border &&
4286 prev_border->line.b.x == border->line.b.x) {
4287 /*
4288 * ------------ +
4289 * ------ =
4290 * ------[ ]
4291 */
4292 prev_border->line.b.x = border->line.a.x;
4293 } else if (prev_border &&
4294 prev_border->line.b.x == border->line.a.x) {
4295 /*
4296 * -------- +
4297 * ------ =
4298 * --------------
4299 */
4300 prev_border->line.b.x = border->line.b.x;
4301 } else if (prev_border &&
4302 prev_border->line.b.x >= border->line.a.x) {
4303 /*
4304 * --------------- +
4305 * ------ =
4306 * -----[ ]----
4307 */
4308 new_border = add_border(borders,
4309 border->line.b.x,
4310 border->line.b.y,
4311 prev_border->line.b.x,
4312 prev_border->line.b.y,
4313 prev_border->blocking_dir);
4314 prev_border->line.b.x = border->line.a.x;
4315 prev_border = new_border;
4316 } else {
4317 assert(!prev_border ||
4318 prev_border->line.b.x < border->line.a.x);
4319 /*
4320 * First border or non-overlapping.
4321 *
4322 * ----- +
4323 * ----- =
4324 * ----- -----
4325 */
4326 new_border = wl_array_add(borders, sizeof *border);
4327 *new_border = *border;
4328 prev_border = new_border;
4329 }
4330 }
4331
4332 wl_array_release(&band_merge);
4333}
4334
4335static void
4336add_band_bottom_edges(pixman_box32_t *boxes,
4337 int band_start,
4338 int band_end,
4339 struct wl_array *borders)
4340{
4341 int i;
4342
4343 for (i = band_start; i < band_end; i++) {
4344 add_border(borders,
4345 boxes[i].x1, boxes[i].y2,
4346 boxes[i].x2, boxes[i].y2,
4347 MOTION_DIRECTION_POSITIVE_Y);
4348 }
4349}
4350
4351static void
4352region_to_outline(pixman_region32_t *region, struct wl_array *borders)
4353{
4354 pixman_box32_t *boxes;
4355 int num_boxes;
4356 int i;
4357 int top_most, bottom_most;
4358 int current_roof;
4359 int prev_top;
4360 int band_start, prev_band_start;
4361
4362 /*
4363 * Remove any overlapping lines from the set of rectangles. Note that
4364 * pixman regions are grouped as rows of rectangles, where rectangles
4365 * in one row never touch or overlap and are all of the same height.
4366 *
4367 * -------- --- -------- ---
4368 * | | | | | | | |
4369 * ----------====---- --- ----------- ----- ---
4370 * | | => | |
4371 * ----==========--------- ----- ----------
4372 * | | | |
4373 * ------------------- -------------------
4374 *
4375 */
4376
4377 boxes = pixman_region32_rectangles(region, &num_boxes);
4378 prev_top = 0;
4379 top_most = boxes[0].y1;
4380 current_roof = top_most;
4381 bottom_most = boxes[num_boxes - 1].y2;
4382 band_start = 0;
4383 prev_band_start = 0;
4384 for (i = 0; i < num_boxes; i++) {
4385 /* Detect if there is a vertical empty space, and add the lower
4386 * level of the previous band if so was the case. */
4387 if (i > 0 &&
4388 boxes[i].y1 != prev_top &&
4389 boxes[i].y1 != boxes[i - 1].y2) {
4390 current_roof = boxes[i].y1;
4391 add_band_bottom_edges(boxes,
4392 band_start,
4393 i,
4394 borders);
4395 }
4396
4397 /* Special case adding the last band, since it won't be handled
4398 * by the band change detection below. */
4399 if (boxes[i].y1 != current_roof && i == num_boxes - 1) {
4400 if (boxes[i].y1 != prev_top) {
4401 /* The last band is a single box, so we don't
4402 * have a prev_band_start to tell us when the
4403 * previous band started. */
4404 add_non_overlapping_edges(boxes,
4405 band_start,
4406 i,
4407 i + 1,
4408 borders);
4409 } else {
4410 add_non_overlapping_edges(boxes,
4411 prev_band_start,
4412 band_start,
4413 i + 1,
4414 borders);
4415 }
4416 }
4417
4418 /* Detect when passing a band and combine the top border of the
4419 * just passed band with the bottom band of the previous band.
4420 */
4421 if (boxes[i].y1 != top_most && boxes[i].y1 != prev_top) {
4422 /* Combine the two passed bands. */
4423 if (prev_top != current_roof) {
4424 add_non_overlapping_edges(boxes,
4425 prev_band_start,
4426 band_start,
4427 i,
4428 borders);
4429 }
4430
4431 prev_band_start = band_start;
4432 band_start = i;
4433 }
4434
4435 /* Add the top border if the box is part of the current roof. */
4436 if (boxes[i].y1 == current_roof) {
4437 add_border(borders,
4438 boxes[i].x1, boxes[i].y1,
4439 boxes[i].x2, boxes[i].y1,
4440 MOTION_DIRECTION_NEGATIVE_Y);
4441 }
4442
4443 /* Add the bottom border of the last band. */
4444 if (boxes[i].y2 == bottom_most) {
4445 add_border(borders,
4446 boxes[i].x1, boxes[i].y2,
4447 boxes[i].x2, boxes[i].y2,
4448 MOTION_DIRECTION_POSITIVE_Y);
4449 }
4450
4451 /* Always add the left border. */
4452 add_border(borders,
4453 boxes[i].x1, boxes[i].y1,
4454 boxes[i].x1, boxes[i].y2,
4455 MOTION_DIRECTION_NEGATIVE_X);
4456
4457 /* Always add the right border. */
4458 add_border(borders,
4459 boxes[i].x2, boxes[i].y1,
4460 boxes[i].x2, boxes[i].y2,
4461 MOTION_DIRECTION_POSITIVE_X);
4462
4463 prev_top = boxes[i].y1;
4464 }
4465}
4466
4467static bool
4468is_border_horizontal (struct border *border)
4469{
4470 return border->line.a.y == border->line.b.y;
4471}
4472
4473static bool
4474is_border_blocking_directions(struct border *border,
4475 uint32_t directions)
4476{
4477 /* Don't block parallel motions. */
4478 if (is_border_horizontal(border)) {
4479 if ((directions & (MOTION_DIRECTION_POSITIVE_Y |
4480 MOTION_DIRECTION_NEGATIVE_Y)) == 0)
4481 return false;
4482 } else {
4483 if ((directions & (MOTION_DIRECTION_POSITIVE_X |
4484 MOTION_DIRECTION_NEGATIVE_X)) == 0)
4485 return false;
4486 }
4487
4488 return (~border->blocking_dir & directions) != directions;
4489}
4490
4491static struct border *
4492get_closest_border(struct wl_array *borders,
4493 struct line *motion,
4494 uint32_t directions)
4495{
4496 struct border *border;
4497 struct vec2d intersection;
4498 struct vec2d delta;
4499 double distance_2;
4500 struct border *closest_border = NULL;
4501 double closest_distance_2 = DBL_MAX;
4502
4503 wl_array_for_each(border, borders) {
4504 if (!is_border_blocking_directions(border, directions))
4505 continue;
4506
4507 if (!lines_intersect(&border->line, motion, &intersection))
4508 continue;
4509
4510 delta = vec2d_subtract(intersection, motion->a);
4511 distance_2 = delta.x*delta.x + delta.y*delta.y;
4512 if (distance_2 < closest_distance_2) {
4513 closest_border = border;
4514 closest_distance_2 = distance_2;
4515 }
4516 }
4517
4518 return closest_border;
4519}
4520
4521static void
4522clamp_to_border(struct border *border,
4523 struct line *motion,
4524 uint32_t *motion_dir)
4525{
4526 /*
4527 * When clamping either rightward or downward motions, the motion needs
4528 * to be clamped so that the destination coordinate does not end up on
4529 * the border (see weston_pointer_clamp_event_to_region). Do this by
4530 * clamping such motions to the border minus the smallest possible
4531 * wl_fixed_t value.
4532 */
4533 if (is_border_horizontal(border)) {
4534 if (*motion_dir & MOTION_DIRECTION_POSITIVE_Y)
4535 motion->b.y = border->line.a.y - wl_fixed_to_double(1);
4536 else
4537 motion->b.y = border->line.a.y;
4538 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_Y |
4539 MOTION_DIRECTION_NEGATIVE_Y);
4540 } else {
4541 if (*motion_dir & MOTION_DIRECTION_POSITIVE_X)
4542 motion->b.x = border->line.a.x - wl_fixed_to_double(1);
4543 else
4544 motion->b.x = border->line.a.x;
4545 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_X |
4546 MOTION_DIRECTION_NEGATIVE_X);
4547 }
4548}
4549
4550static uint32_t
4551get_motion_directions(struct line *motion)
4552{
4553 uint32_t directions = 0;
4554
4555 if (motion->a.x < motion->b.x)
4556 directions |= MOTION_DIRECTION_POSITIVE_X;
4557 else if (motion->a.x > motion->b.x)
4558 directions |= MOTION_DIRECTION_NEGATIVE_X;
4559 if (motion->a.y < motion->b.y)
4560 directions |= MOTION_DIRECTION_POSITIVE_Y;
4561 else if (motion->a.y > motion->b.y)
4562 directions |= MOTION_DIRECTION_NEGATIVE_Y;
4563
4564 return directions;
4565}
4566
Jonas Ådahld3414f22016-07-22 17:56:31 +08004567static void
4568weston_pointer_clamp_event_to_region(struct weston_pointer *pointer,
4569 struct weston_pointer_motion_event *event,
4570 pixman_region32_t *region,
4571 wl_fixed_t *clamped_x,
4572 wl_fixed_t *clamped_y)
4573{
4574 wl_fixed_t x, y;
4575 wl_fixed_t sx, sy;
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004576 wl_fixed_t old_sx = pointer->sx;
4577 wl_fixed_t old_sy = pointer->sy;
4578 struct wl_array borders;
4579 struct line motion;
4580 struct border *closest_border;
4581 float new_x_f, new_y_f;
4582 uint32_t directions;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004583
4584 weston_pointer_motion_to_abs(pointer, event, &x, &y);
4585 weston_view_from_global_fixed(pointer->focus, x, y, &sx, &sy);
4586
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004587 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004588
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004589 /*
4590 * Generate borders given the confine region we are to use. The borders
4591 * are defined to be the outer region of the allowed area. This means
4592 * top/left borders are "within" the allowed area, while bottom/right
4593 * borders are outside. This needs to be considered when clamping
4594 * confined motion vectors.
4595 */
4596 region_to_outline(region, &borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004597
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004598 motion = (struct line) {
4599 .a = (struct vec2d) {
4600 .x = wl_fixed_to_double(old_sx),
4601 .y = wl_fixed_to_double(old_sy),
4602 },
4603 .b = (struct vec2d) {
4604 .x = wl_fixed_to_double(sx),
4605 .y = wl_fixed_to_double(sy),
4606 },
4607 };
4608 directions = get_motion_directions(&motion);
4609
4610 while (directions) {
4611 closest_border = get_closest_border(&borders,
4612 &motion,
4613 directions);
4614 if (closest_border)
4615 clamp_to_border(closest_border, &motion, &directions);
4616 else
4617 break;
4618 }
4619
4620 weston_view_to_global_float(pointer->focus,
4621 (float) motion.b.x, (float) motion.b.y,
4622 &new_x_f, &new_y_f);
4623 *clamped_x = wl_fixed_from_double(new_x_f);
4624 *clamped_y = wl_fixed_from_double(new_y_f);
4625
4626 wl_array_release(&borders);
4627}
4628
4629static double
4630point_to_border_distance_2(struct border *border, double x, double y)
4631{
4632 double orig_x, orig_y;
4633 double dx, dy;
4634
4635 if (is_border_horizontal(border)) {
4636 if (x < border->line.a.x)
4637 orig_x = border->line.a.x;
4638 else if (x > border->line.b.x)
4639 orig_x = border->line.b.x;
4640 else
4641 orig_x = x;
4642 orig_y = border->line.a.y;
4643 } else {
4644 if (y < border->line.a.y)
4645 orig_y = border->line.a.y;
4646 else if (y > border->line.b.y)
4647 orig_y = border->line.b.y;
4648 else
4649 orig_y = y;
4650 orig_x = border->line.a.x;
4651 }
4652
4653
4654 dx = fabs(orig_x - x);
4655 dy = fabs(orig_y - y);
4656 return dx*dx + dy*dy;
4657}
4658
4659static void
4660warp_to_behind_border(struct border *border, wl_fixed_t *sx, wl_fixed_t *sy)
4661{
4662 switch (border->blocking_dir) {
4663 case MOTION_DIRECTION_POSITIVE_X:
4664 case MOTION_DIRECTION_NEGATIVE_X:
4665 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_X)
4666 *sx = wl_fixed_from_double(border->line.a.x) - 1;
4667 else
4668 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4669 if (*sy < wl_fixed_from_double(border->line.a.y))
4670 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4671 else if (*sy > wl_fixed_from_double(border->line.b.y))
4672 *sy = wl_fixed_from_double(border->line.b.y) - 1;
4673 break;
4674 case MOTION_DIRECTION_POSITIVE_Y:
4675 case MOTION_DIRECTION_NEGATIVE_Y:
4676 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_Y)
4677 *sy = wl_fixed_from_double(border->line.a.y) - 1;
4678 else
4679 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4680 if (*sx < wl_fixed_from_double(border->line.a.x))
4681 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4682 else if (*sx > wl_fixed_from_double(border->line.b.x))
4683 *sx = wl_fixed_from_double(border->line.b.x) - 1;
4684 break;
4685 }
Jonas Ådahld3414f22016-07-22 17:56:31 +08004686}
4687
4688static void
4689maybe_warp_confined_pointer(struct weston_pointer_constraint *constraint)
4690{
4691 wl_fixed_t x;
4692 wl_fixed_t y;
4693 wl_fixed_t sx;
4694 wl_fixed_t sy;
4695
4696 weston_view_from_global_fixed(constraint->view,
4697 constraint->pointer->x,
4698 constraint->pointer->y,
4699 &sx,
4700 &sy);
4701
4702 if (!is_within_constraint_region(constraint, sx, sy)) {
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004703 double xf = wl_fixed_to_double(sx);
4704 double yf = wl_fixed_to_double(sy);
4705 pixman_region32_t confine_region;
4706 struct wl_array borders;
4707 struct border *border;
4708 double closest_distance_2 = DBL_MAX;
4709 struct border *closest_border = NULL;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004710
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004711 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004712
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004713 pixman_region32_init(&confine_region);
4714 pixman_region32_intersect(&confine_region,
4715 &constraint->view->surface->input,
4716 &constraint->region);
4717 region_to_outline(&confine_region, &borders);
4718 pixman_region32_fini(&confine_region);
4719
4720 wl_array_for_each(border, &borders) {
4721 double distance_2;
4722
4723 distance_2 = point_to_border_distance_2(border, xf, yf);
4724 if (distance_2 < closest_distance_2) {
4725 closest_border = border;
4726 closest_distance_2 = distance_2;
4727 }
4728 }
4729 assert(closest_border);
4730
4731 warp_to_behind_border(closest_border, &sx, &sy);
4732
4733 wl_array_release(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004734
4735 weston_view_to_global_fixed(constraint->view, sx, sy, &x, &y);
4736 weston_pointer_move_to(constraint->pointer, x, y);
4737 }
4738}
4739
4740static void
4741confined_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02004742 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004743 struct weston_pointer_motion_event *event)
4744{
4745 struct weston_pointer_constraint *constraint =
4746 container_of(grab, struct weston_pointer_constraint, grab);
4747 struct weston_pointer *pointer = grab->pointer;
4748 struct weston_surface *surface;
4749 wl_fixed_t x, y;
4750 wl_fixed_t old_sx = pointer->sx;
4751 wl_fixed_t old_sy = pointer->sy;
4752 pixman_region32_t confine_region;
4753
4754 assert(pointer->focus);
4755 assert(pointer->focus->surface == constraint->surface);
4756
4757 surface = pointer->focus->surface;
4758
4759 pixman_region32_init(&confine_region);
4760 pixman_region32_intersect(&confine_region,
4761 &surface->input,
4762 &constraint->region);
4763 weston_pointer_clamp_event_to_region(pointer, event,
4764 &confine_region, &x, &y);
4765 weston_pointer_move_to(pointer, x, y);
4766 pixman_region32_fini(&confine_region);
4767
4768 weston_view_from_global_fixed(pointer->focus, x, y,
4769 &pointer->sx, &pointer->sy);
4770
4771 if (old_sx != pointer->sx || old_sy != pointer->sy) {
Quentin Glidiccde13452016-08-12 10:41:32 +02004772 pointer_send_motion(pointer, time,
4773 pointer->sx, pointer->sy);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004774 }
4775
Quentin Glidiccde13452016-08-12 10:41:32 +02004776 pointer_send_relative_motion(pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004777}
4778
4779static void
4780confined_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02004781 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004782 uint32_t button,
4783 uint32_t state_w)
4784{
4785 weston_pointer_send_button(grab->pointer, time, button, state_w);
4786}
4787
4788static void
4789confined_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02004790 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004791 struct weston_pointer_axis_event *event)
4792{
4793 weston_pointer_send_axis(grab->pointer, time, event);
4794}
4795
4796static void
4797confined_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
4798 uint32_t source)
4799{
4800 weston_pointer_send_axis_source(grab->pointer, source);
4801}
4802
4803static void
4804confined_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
4805{
4806 weston_pointer_send_frame(grab->pointer);
4807}
4808
4809static void
4810confined_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
4811{
4812 struct weston_pointer_constraint *constraint =
4813 container_of(grab, struct weston_pointer_constraint, grab);
4814
4815 disable_pointer_constraint(constraint);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004816}
4817
4818static const struct weston_pointer_grab_interface
4819 confined_pointer_grab_interface = {
4820 confined_pointer_grab_pointer_focus,
4821 confined_pointer_grab_pointer_motion,
4822 confined_pointer_grab_pointer_button,
4823 confined_pointer_grab_pointer_axis,
4824 confined_pointer_grab_pointer_axis_source,
4825 confined_pointer_grab_pointer_frame,
4826 confined_pointer_grab_pointer_cancel,
4827};
4828
4829static void
4830confined_pointer_destroy(struct wl_client *client,
4831 struct wl_resource *resource)
4832{
4833 wl_resource_destroy(resource);
4834}
4835
4836static void
4837confined_pointer_set_region(struct wl_client *client,
4838 struct wl_resource *resource,
4839 struct wl_resource *region_resource)
4840{
4841 struct weston_pointer_constraint *constraint =
4842 wl_resource_get_user_data(resource);
4843 struct weston_region *region = region_resource ?
4844 wl_resource_get_user_data(region_resource) : NULL;
4845
4846 if (!constraint)
4847 return;
4848
4849 if (region) {
4850 pixman_region32_copy(&constraint->region_pending,
4851 &region->region);
4852 } else {
4853 pixman_region32_fini(&constraint->region_pending);
4854 region_init_infinite(&constraint->region_pending);
4855 }
4856 constraint->region_is_pending = true;
4857}
4858
4859static const struct zwp_confined_pointer_v1_interface confined_pointer_interface = {
4860 confined_pointer_destroy,
4861 confined_pointer_set_region,
4862};
4863
4864static void
4865pointer_constraints_confine_pointer(struct wl_client *client,
4866 struct wl_resource *resource,
4867 uint32_t id,
4868 struct wl_resource *surface_resource,
4869 struct wl_resource *pointer_resource,
4870 struct wl_resource *region_resource,
4871 uint32_t lifetime)
4872{
4873 struct weston_surface *surface =
4874 wl_resource_get_user_data(surface_resource);
4875 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
4876 struct weston_region *region = region_resource ?
4877 wl_resource_get_user_data(region_resource) : NULL;
4878
4879 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
4880 &zwp_confined_pointer_v1_interface,
4881 &confined_pointer_interface,
4882 &confined_pointer_grab_interface);
4883}
4884
4885static const struct zwp_pointer_constraints_v1_interface pointer_constraints_interface = {
4886 pointer_constraints_destroy,
4887 pointer_constraints_lock_pointer,
4888 pointer_constraints_confine_pointer,
4889};
4890
4891static void
4892bind_pointer_constraints(struct wl_client *client, void *data,
4893 uint32_t version, uint32_t id)
4894{
4895 struct wl_resource *resource;
4896
4897 resource = wl_resource_create(client,
4898 &zwp_pointer_constraints_v1_interface,
4899 1, id);
4900
4901 wl_resource_set_implementation(resource, &pointer_constraints_interface,
4902 NULL, NULL);
4903}
4904
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004905static void
Alexandros Frantzis2b442482018-02-20 14:05:50 +02004906input_timestamps_destroy(struct wl_client *client,
4907 struct wl_resource *resource)
4908{
4909 wl_resource_destroy(resource);
4910}
4911
4912static const struct zwp_input_timestamps_v1_interface
4913 input_timestamps_interface = {
4914 input_timestamps_destroy,
4915};
4916
4917static void
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004918input_timestamps_manager_destroy(struct wl_client *client,
4919 struct wl_resource *resource)
4920{
4921 wl_resource_destroy(resource);
4922}
4923
4924static void
4925input_timestamps_manager_get_keyboard_timestamps(struct wl_client *client,
4926 struct wl_resource *resource,
4927 uint32_t id,
4928 struct wl_resource *keyboard_resource)
4929{
Alexandros Frantzis2b442482018-02-20 14:05:50 +02004930 struct weston_keyboard *keyboard =
4931 wl_resource_get_user_data(keyboard_resource);
4932 struct wl_resource *input_ts;
4933
4934 input_ts = wl_resource_create(client,
4935 &zwp_input_timestamps_v1_interface,
4936 1, id);
4937 if (!input_ts) {
4938 wl_client_post_no_memory(client);
4939 return;
4940 }
4941
4942 if (keyboard) {
4943 wl_list_insert(&keyboard->timestamps_list,
4944 wl_resource_get_link(input_ts));
4945 } else {
4946 wl_list_init(wl_resource_get_link(input_ts));
4947 }
4948
4949 wl_resource_set_implementation(input_ts,
4950 &input_timestamps_interface,
4951 keyboard_resource,
4952 unbind_resource);
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004953}
4954
4955static void
4956input_timestamps_manager_get_pointer_timestamps(struct wl_client *client,
4957 struct wl_resource *resource,
4958 uint32_t id,
4959 struct wl_resource *pointer_resource)
4960{
Alexandros Frantzisdb907b72018-02-20 14:06:26 +02004961 struct weston_pointer *pointer =
4962 wl_resource_get_user_data(pointer_resource);
4963 struct wl_resource *input_ts;
4964
4965 input_ts = wl_resource_create(client,
4966 &zwp_input_timestamps_v1_interface,
4967 1, id);
4968 if (!input_ts) {
4969 wl_client_post_no_memory(client);
4970 return;
4971 }
4972
4973 if (pointer) {
4974 wl_list_insert(&pointer->timestamps_list,
4975 wl_resource_get_link(input_ts));
4976 } else {
4977 wl_list_init(wl_resource_get_link(input_ts));
4978 }
4979
4980 wl_resource_set_implementation(input_ts,
4981 &input_timestamps_interface,
4982 pointer_resource,
4983 unbind_resource);
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004984}
4985
4986static void
4987input_timestamps_manager_get_touch_timestamps(struct wl_client *client,
4988 struct wl_resource *resource,
4989 uint32_t id,
4990 struct wl_resource *touch_resource)
4991{
Alexandros Frantzisd7157842018-02-20 14:07:03 +02004992 struct weston_touch *touch = wl_resource_get_user_data(touch_resource);
4993 struct wl_resource *input_ts;
4994
4995 input_ts = wl_resource_create(client,
4996 &zwp_input_timestamps_v1_interface,
4997 1, id);
4998 if (!input_ts) {
4999 wl_client_post_no_memory(client);
5000 return;
5001 }
5002
5003 if (touch) {
5004 wl_list_insert(&touch->timestamps_list,
5005 wl_resource_get_link(input_ts));
5006 } else {
5007 wl_list_init(wl_resource_get_link(input_ts));
5008 }
5009
5010 wl_resource_set_implementation(input_ts,
5011 &input_timestamps_interface,
5012 touch_resource,
5013 unbind_resource);
Alexandros Frantzis538749d2018-02-16 18:44:16 +02005014}
5015
5016static const struct zwp_input_timestamps_manager_v1_interface
5017 input_timestamps_manager_interface = {
5018 input_timestamps_manager_destroy,
5019 input_timestamps_manager_get_keyboard_timestamps,
5020 input_timestamps_manager_get_pointer_timestamps,
5021 input_timestamps_manager_get_touch_timestamps,
5022};
5023
5024static void
5025bind_input_timestamps_manager(struct wl_client *client, void *data,
5026 uint32_t version, uint32_t id)
5027{
5028 struct wl_resource *resource =
5029 wl_resource_create(client,
5030 &zwp_input_timestamps_manager_v1_interface,
5031 1, id);
5032
5033 if (resource == NULL) {
5034 wl_client_post_no_memory(client);
5035 return;
5036 }
5037
5038 wl_resource_set_implementation(resource,
5039 &input_timestamps_manager_interface,
5040 NULL, NULL);
5041}
5042
Jonas Ådahl30d61d82014-10-22 21:21:17 +02005043int
5044weston_input_init(struct weston_compositor *compositor)
5045{
5046 if (!wl_global_create(compositor->wl_display,
5047 &zwp_relative_pointer_manager_v1_interface, 1,
5048 compositor, bind_relative_pointer_manager))
5049 return -1;
5050
Jonas Ådahld3414f22016-07-22 17:56:31 +08005051 if (!wl_global_create(compositor->wl_display,
5052 &zwp_pointer_constraints_v1_interface, 1,
5053 NULL, bind_pointer_constraints))
5054 return -1;
5055
Alexandros Frantzis538749d2018-02-16 18:44:16 +02005056 if (!wl_global_create(compositor->wl_display,
5057 &zwp_input_timestamps_manager_v1_interface, 1,
5058 NULL, bind_input_timestamps_manager))
5059 return -1;
5060
Jonas Ådahl30d61d82014-10-22 21:21:17 +02005061 return 0;
5062}