blob: 0e87a4bbc5e98d7466e665adadb6e382d318d23a [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) {
1521 send_modifiers_to_resource(keyboard, resource, serial);
1522 wl_keyboard_send_enter(resource, serial,
1523 surface->resource,
1524 &keyboard->keys);
1525 }
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 void *area;
2090 int fd;
Derek Foremanf8f7fd62017-06-28 11:41:43 -05002091
Derek Foreman76829fc2017-06-28 12:17:46 -05002092 fd = os_create_anonymous_file(xkb_info->keymap_size);
2093 if (fd < 0) {
Antonio Borneo39578632019-04-26 23:57:31 +02002094 weston_log("creating a keymap file for %lu bytes failed: %s\n",
2095 (unsigned long) xkb_info->keymap_size,
2096 strerror(errno));
Derek Foreman76829fc2017-06-28 12:17:46 -05002097 return;
2098 }
2099
2100 area = mmap(NULL, xkb_info->keymap_size, PROT_READ | PROT_WRITE,
2101 MAP_SHARED, fd, 0);
2102 if (area == MAP_FAILED) {
2103 weston_log("failed to mmap() %lu bytes\n",
2104 (unsigned long) xkb_info->keymap_size);
2105 goto err_mmap;
2106 }
2107 strcpy(area, xkb_info->keymap_string);
2108 munmap(area, xkb_info->keymap_size);
Rui Matos65196bc2013-10-10 19:44:19 +02002109 wl_keyboard_send_keymap(resource,
2110 WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
Derek Foreman76829fc2017-06-28 12:17:46 -05002111 fd,
Rui Matos65196bc2013-10-10 19:44:19 +02002112 xkb_info->keymap_size);
Derek Foreman76829fc2017-06-28 12:17:46 -05002113err_mmap:
2114 close(fd);
Rui Matos65196bc2013-10-10 19:44:19 +02002115}
2116
2117static void
2118send_modifiers(struct wl_resource *resource, uint32_t serial, struct weston_keyboard *keyboard)
2119{
2120 wl_keyboard_send_modifiers(resource, serial,
2121 keyboard->modifiers.mods_depressed,
2122 keyboard->modifiers.mods_latched,
2123 keyboard->modifiers.mods_locked,
2124 keyboard->modifiers.group);
2125}
2126
2127static struct weston_xkb_info *
2128weston_xkb_info_create(struct xkb_keymap *keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02002129
2130static void
2131update_keymap(struct weston_seat *seat)
2132{
Derek Foreman1281a362015-07-31 16:55:32 -05002133 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Rui Matos65196bc2013-10-10 19:44:19 +02002134 struct wl_resource *resource;
2135 struct weston_xkb_info *xkb_info;
2136 struct xkb_state *state;
2137 xkb_mod_mask_t latched_mods;
2138 xkb_mod_mask_t locked_mods;
2139
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002140 xkb_info = weston_xkb_info_create(keyboard->pending_keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02002141
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002142 xkb_keymap_unref(keyboard->pending_keymap);
2143 keyboard->pending_keymap = NULL;
Rui Matos65196bc2013-10-10 19:44:19 +02002144
2145 if (!xkb_info) {
2146 weston_log("failed to create XKB info\n");
2147 return;
2148 }
2149
2150 state = xkb_state_new(xkb_info->keymap);
2151 if (!state) {
2152 weston_log("failed to initialise XKB state\n");
2153 weston_xkb_info_destroy(xkb_info);
2154 return;
2155 }
2156
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002157 latched_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
2158 XKB_STATE_MODS_LATCHED);
2159 locked_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
2160 XKB_STATE_MODS_LOCKED);
Rui Matos65196bc2013-10-10 19:44:19 +02002161 xkb_state_update_mask(state,
2162 0, /* depressed */
2163 latched_mods,
2164 locked_mods,
2165 0, 0, 0);
2166
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002167 weston_xkb_info_destroy(keyboard->xkb_info);
2168 keyboard->xkb_info = xkb_info;
Rui Matos65196bc2013-10-10 19:44:19 +02002169
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002170 xkb_state_unref(keyboard->xkb_state.state);
2171 keyboard->xkb_state.state = state;
Rui Matos65196bc2013-10-10 19:44:19 +02002172
Derek Foremanbc91e542015-06-03 15:53:27 -05002173 wl_resource_for_each(resource, &keyboard->resource_list)
Derek Foremanf8f7fd62017-06-28 11:41:43 -05002174 weston_keyboard_send_keymap(keyboard, resource);
Derek Foremanbc91e542015-06-03 15:53:27 -05002175 wl_resource_for_each(resource, &keyboard->focus_resource_list)
Derek Foremanf8f7fd62017-06-28 11:41:43 -05002176 weston_keyboard_send_keymap(keyboard, resource);
Rui Matos65196bc2013-10-10 19:44:19 +02002177
2178 notify_modifiers(seat, wl_display_next_serial(seat->compositor->wl_display));
2179
2180 if (!latched_mods && !locked_mods)
2181 return;
2182
Derek Foremanbc91e542015-06-03 15:53:27 -05002183 wl_resource_for_each(resource, &keyboard->resource_list)
2184 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), keyboard);
2185 wl_resource_for_each(resource, &keyboard->focus_resource_list)
2186 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), keyboard);
Rui Matos65196bc2013-10-10 19:44:19 +02002187}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002188
2189WL_EXPORT void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02002190notify_key(struct weston_seat *seat, const struct timespec *time, uint32_t key,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002191 enum wl_keyboard_key_state state,
2192 enum weston_key_state_update update_state)
2193{
2194 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002195 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002196 struct weston_keyboard_grab *grab = keyboard->grab;
Pekka Paalanen86b53962014-11-19 13:43:32 +02002197 uint32_t *k, *end;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002198
2199 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002200 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002201 } else {
2202 weston_compositor_idle_release(compositor);
2203 }
2204
Pekka Paalanen86b53962014-11-19 13:43:32 +02002205 end = keyboard->keys.data + keyboard->keys.size;
2206 for (k = keyboard->keys.data; k < end; k++) {
2207 if (*k == key) {
2208 /* Ignore server-generated repeats. */
2209 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2210 return;
2211 *k = *--end;
2212 }
2213 }
2214 keyboard->keys.size = (void *) end - keyboard->keys.data;
2215 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2216 k = wl_array_add(&keyboard->keys, sizeof *k);
2217 *k = key;
2218 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002219
2220 if (grab == &keyboard->default_grab ||
2221 grab == &keyboard->input_method_grab) {
Derek Foreman99a6a2d2015-07-15 13:00:43 -05002222 weston_compositor_run_key_binding(compositor, keyboard, time,
2223 key, state);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002224 grab = keyboard->grab;
2225 }
2226
2227 grab->interface->key(grab, time, key, state);
2228
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002229 if (keyboard->pending_keymap &&
Pekka Paalanen86b53962014-11-19 13:43:32 +02002230 keyboard->keys.size == 0)
Rui Matos65196bc2013-10-10 19:44:19 +02002231 update_keymap(seat);
2232
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002233 if (update_state == STATE_UPDATE_AUTOMATIC) {
2234 update_modifier_state(seat,
2235 wl_display_get_serial(compositor->wl_display),
2236 key,
2237 state);
2238 }
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002239
Olivier Fourdandf84dbe2016-06-30 16:01:56 +02002240 keyboard->grab_serial = wl_display_get_serial(compositor->wl_display);
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002241 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02002242 keyboard->grab_time = *time;
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002243 keyboard->grab_key = key;
2244 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002245}
2246
2247WL_EXPORT void
2248notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002249 double x, double y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002250{
Derek Foreman1281a362015-07-31 16:55:32 -05002251 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2252
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002253 if (output) {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002254 weston_pointer_move_to(pointer,
2255 wl_fixed_from_double(x),
2256 wl_fixed_from_double(y));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002257 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002258 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002259 * NULL) here, but somehow that breaks re-entry... */
2260 }
2261}
2262
2263static void
2264destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
2265{
2266 struct weston_seat *ws;
2267
2268 ws = container_of(listener, struct weston_seat,
2269 saved_kbd_focus_listener);
2270
2271 ws->saved_kbd_focus = NULL;
2272}
2273
2274WL_EXPORT void
2275notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
2276 enum weston_key_state_update update_state)
2277{
2278 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002279 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04002280 struct weston_surface *surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002281 uint32_t *k, serial;
2282
2283 serial = wl_display_next_serial(compositor->wl_display);
2284 wl_array_copy(&keyboard->keys, keys);
2285 wl_array_for_each(k, &keyboard->keys) {
2286 weston_compositor_idle_inhibit(compositor);
2287 if (update_state == STATE_UPDATE_AUTOMATIC)
2288 update_modifier_state(seat, serial, *k,
2289 WL_KEYBOARD_KEY_STATE_PRESSED);
2290 }
2291
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002292 surface = seat->saved_kbd_focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002293 if (surface) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002294 weston_keyboard_set_focus(keyboard, surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002295 }
2296}
2297
2298WL_EXPORT void
2299notify_keyboard_focus_out(struct weston_seat *seat)
2300{
2301 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002302 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2303 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Quentin Glidic85d55542017-07-21 14:02:40 +02002304 struct weston_surface *focus = keyboard->focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002305 uint32_t *k, serial;
2306
2307 serial = wl_display_next_serial(compositor->wl_display);
2308 wl_array_for_each(k, &keyboard->keys) {
2309 weston_compositor_idle_release(compositor);
2310 update_modifier_state(seat, serial, *k,
2311 WL_KEYBOARD_KEY_STATE_RELEASED);
2312 }
2313
2314 seat->modifier_state = 0;
2315
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002316 weston_keyboard_set_focus(keyboard, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002317 weston_keyboard_cancel_grab(keyboard);
Derek Foreman1281a362015-07-31 16:55:32 -05002318 if (pointer)
2319 weston_pointer_cancel_grab(pointer);
Quentin Glidic85d55542017-07-21 14:02:40 +02002320
2321 if (focus) {
2322 seat->saved_kbd_focus = focus;
2323 seat->saved_kbd_focus_listener.notify =
2324 destroy_device_saved_kbd_focus;
2325 wl_signal_add(&focus->destroy_signal,
2326 &seat->saved_kbd_focus_listener);
2327 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002328}
2329
Michael Fua2bb7912013-07-23 15:51:06 +08002330WL_EXPORT void
Derek Foreman4c93c082015-04-30 16:45:41 -05002331weston_touch_set_focus(struct weston_touch *touch, struct weston_view *view)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002332{
Neil Roberts96d790e2013-09-19 17:32:00 +01002333 struct wl_list *focus_resource_list;
2334
Derek Foreman4c93c082015-04-30 16:45:41 -05002335 focus_resource_list = &touch->focus_resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002336
Derek Foreman4c93c082015-04-30 16:45:41 -05002337 if (view && touch->focus &&
2338 touch->focus->surface == view->surface) {
2339 touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002340 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002341 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002342
Derek Foreman4c93c082015-04-30 16:45:41 -05002343 wl_list_remove(&touch->focus_resource_listener.link);
2344 wl_list_init(&touch->focus_resource_listener.link);
2345 wl_list_remove(&touch->focus_view_listener.link);
2346 wl_list_init(&touch->focus_view_listener.link);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01002347
Neil Roberts96d790e2013-09-19 17:32:00 +01002348 if (!wl_list_empty(focus_resource_list)) {
Derek Foreman4c93c082015-04-30 16:45:41 -05002349 move_resources(&touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002350 focus_resource_list);
2351 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002352
Jason Ekstranda7af7042013-10-12 22:38:11 -05002353 if (view) {
Derek Foreman362656b2014-09-04 10:23:05 -05002354 struct wl_client *surface_client;
2355
2356 if (!view->surface->resource) {
Derek Foreman4c93c082015-04-30 16:45:41 -05002357 touch->focus = NULL;
Derek Foreman362656b2014-09-04 10:23:05 -05002358 return;
2359 }
2360
2361 surface_client = wl_resource_get_client(view->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +01002362 move_resources_for_client(focus_resource_list,
Derek Foreman4c93c082015-04-30 16:45:41 -05002363 &touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002364 surface_client);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01002365 wl_resource_add_destroy_listener(view->surface->resource,
Derek Foreman4c93c082015-04-30 16:45:41 -05002366 &touch->focus_resource_listener);
2367 wl_signal_add(&view->destroy_signal, &touch->focus_view_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002368 }
Derek Foreman4c93c082015-04-30 16:45:41 -05002369 touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002370}
2371
Pekka Paalanenf4062532018-02-26 16:18:29 +02002372static void
2373process_touch_normal(struct weston_touch_device *device,
2374 const struct timespec *time, int touch_id,
2375 double double_x, double double_y, int touch_type)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002376{
Pekka Paalanenbcbce332018-02-26 14:43:00 +02002377 struct weston_touch *touch = device->aggregate;
2378 struct weston_touch_grab *grab = device->aggregate->grab;
2379 struct weston_compositor *ec = device->aggregate->seat->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002380 struct weston_view *ev;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002381 wl_fixed_t sx, sy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002382 wl_fixed_t x = wl_fixed_from_double(double_x);
2383 wl_fixed_t y = wl_fixed_from_double(double_y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002384
2385 /* Update grab's global coordinates. */
Neil Roberts306fe082013-10-03 16:43:06 +01002386 if (touch_id == touch->grab_touch_id && touch_type != WL_TOUCH_UP) {
2387 touch->grab_x = x;
2388 touch->grab_y = y;
2389 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002390
2391 switch (touch_type) {
2392 case WL_TOUCH_DOWN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002393 /* the first finger down picks the view, and all further go
2394 * to that view for the remainder of the touch session i.e.
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002395 * until all touch points are up again. */
Jonas Ådahl9484b692013-12-02 22:05:03 +01002396 if (touch->num_tp == 1) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002397 ev = weston_compositor_pick_view(ec, x, y, &sx, &sy);
Derek Foreman4c93c082015-04-30 16:45:41 -05002398 weston_touch_set_focus(touch, ev);
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002399 } else if (!touch->focus) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002400 /* Unexpected condition: We have non-initial touch but
2401 * there is no focused surface.
2402 */
Chris Michael3f607d32015-10-07 11:59:49 -04002403 weston_log("touch event received with %d points down "
Jonas Ådahl9484b692013-12-02 22:05:03 +01002404 "but no surface focused\n", touch->num_tp);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002405 return;
2406 }
2407
Derek Foreman99a6a2d2015-07-15 13:00:43 -05002408 weston_compositor_run_touch_binding(ec, touch,
Kristian Høgsbergc8964012014-02-05 14:25:18 -08002409 time, touch_type);
2410
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002411 grab->interface->down(grab, time, touch_id, x, y);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002412 if (touch->num_tp == 1) {
Rusty Lynchf1407ff2013-08-08 21:13:57 -07002413 touch->grab_serial =
2414 wl_display_get_serial(ec->wl_display);
Neil Roberts306fe082013-10-03 16:43:06 +01002415 touch->grab_touch_id = touch_id;
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02002416 touch->grab_time = *time;
Rusty Lynchf1407ff2013-08-08 21:13:57 -07002417 touch->grab_x = x;
2418 touch->grab_y = y;
2419 }
2420
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002421 break;
2422 case WL_TOUCH_MOTION:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002423 ev = touch->focus;
2424 if (!ev)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002425 break;
2426
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +02002427 grab->interface->motion(grab, time, touch_id, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002428 break;
2429 case WL_TOUCH_UP:
Alexandros Frantzis27a51b82017-11-16 18:20:59 +02002430 grab->interface->up(grab, time, touch_id);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002431 if (touch->num_tp == 0)
Derek Foreman4c93c082015-04-30 16:45:41 -05002432 weston_touch_set_focus(touch, NULL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002433 break;
2434 }
2435}
2436
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002437static enum weston_touch_mode
2438get_next_touch_mode(enum weston_touch_mode from)
2439{
2440 switch (from) {
2441 case WESTON_TOUCH_MODE_PREP_NORMAL:
2442 return WESTON_TOUCH_MODE_NORMAL;
2443
2444 case WESTON_TOUCH_MODE_PREP_CALIB:
2445 return WESTON_TOUCH_MODE_CALIB;
2446
2447 case WESTON_TOUCH_MODE_NORMAL:
2448 case WESTON_TOUCH_MODE_CALIB:
2449 return from;
2450 }
2451
2452 return WESTON_TOUCH_MODE_NORMAL;
2453}
2454
2455/** Global touch mode update
2456 *
2457 * If no seat has a touch down and the compositor is in a PREP touch mode,
2458 * set the compositor to the goal touch mode.
2459 *
2460 * Calls calibrator if touch mode changed.
2461 */
2462static void
2463weston_compositor_update_touch_mode(struct weston_compositor *compositor)
2464{
2465 struct weston_seat *seat;
2466 struct weston_touch *touch;
2467 enum weston_touch_mode goal;
2468
2469 wl_list_for_each(seat, &compositor->seat_list, link) {
2470 touch = weston_seat_get_touch(seat);
2471 if (!touch)
2472 continue;
2473
2474 if (touch->num_tp > 0)
2475 return;
2476 }
2477
2478 goal = get_next_touch_mode(compositor->touch_mode);
2479 if (compositor->touch_mode != goal) {
2480 compositor->touch_mode = goal;
2481 touch_calibrator_mode_changed(compositor);
2482 }
2483}
2484
2485/** Start transition to normal touch event handling
2486 *
2487 * The touch event mode changes when all touches on all touch devices have
2488 * been lifted. If no touches are currently down, the transition is immediate.
2489 *
2490 * \sa weston_touch_mode
2491 */
2492void
2493weston_compositor_set_touch_mode_normal(struct weston_compositor *compositor)
2494{
2495 switch (compositor->touch_mode) {
2496 case WESTON_TOUCH_MODE_PREP_NORMAL:
2497 case WESTON_TOUCH_MODE_NORMAL:
2498 return;
2499 case WESTON_TOUCH_MODE_PREP_CALIB:
2500 compositor->touch_mode = WESTON_TOUCH_MODE_NORMAL;
2501 touch_calibrator_mode_changed(compositor);
2502 return;
2503 case WESTON_TOUCH_MODE_CALIB:
2504 compositor->touch_mode = WESTON_TOUCH_MODE_PREP_NORMAL;
2505 }
2506
2507 weston_compositor_update_touch_mode(compositor);
2508}
2509
2510/** Start transition to calibrator touch event handling
2511 *
2512 * The touch event mode changes when all touches on all touch devices have
2513 * been lifted. If no touches are currently down, the transition is immediate.
2514 *
2515 * \sa weston_touch_mode
2516 */
2517void
2518weston_compositor_set_touch_mode_calib(struct weston_compositor *compositor)
2519{
2520 switch (compositor->touch_mode) {
2521 case WESTON_TOUCH_MODE_PREP_CALIB:
2522 case WESTON_TOUCH_MODE_CALIB:
2523 assert(0);
2524 return;
2525 case WESTON_TOUCH_MODE_PREP_NORMAL:
2526 compositor->touch_mode = WESTON_TOUCH_MODE_CALIB;
2527 touch_calibrator_mode_changed(compositor);
2528 return;
2529 case WESTON_TOUCH_MODE_NORMAL:
2530 compositor->touch_mode = WESTON_TOUCH_MODE_PREP_CALIB;
2531 }
2532
2533 weston_compositor_update_touch_mode(compositor);
2534}
2535
Pekka Paalanenf4062532018-02-26 16:18:29 +02002536/** Feed in touch down, motion, and up events, calibratable device.
2537 *
2538 * It assumes always the correct cycle sequence until it gets here: touch_down
2539 * → touch_update → ... → touch_update → touch_end. The driver is responsible
2540 * for sending along such order.
2541 *
2542 * \param device The physical device that generated the event.
2543 * \param time The event timestamp.
2544 * \param touch_id ID for the touch point of this event (multi-touch).
Marius Vlada2dace22019-06-12 16:05:44 +03002545 * \param x X coordinate in compositor global space.
2546 * \param y Y coordinate in compositor global space.
Pekka Paalanenf4062532018-02-26 16:18:29 +02002547 * \param norm Normalized device X, Y coordinates in calibration space, or NULL.
2548 * \param touch_type Either WL_TOUCH_DOWN, WL_TOUCH_UP, or WL_TOUCH_MOTION.
2549 *
2550 * Coordinates double_x and double_y are used for normal operation.
2551 *
2552 * Coordinates norm are only used for touch device calibration. If and only if
2553 * the weston_touch_device does not support calibrating, norm must be NULL.
2554 *
2555 * The calibration space is the normalized coordinate space
2556 * [0.0, 1.0]×[0.0, 1.0] of the weston_touch_device. This is assumed to
2557 * map to the similar normalized coordinate space of the associated
2558 * weston_output.
2559 */
2560WL_EXPORT void
2561notify_touch_normalized(struct weston_touch_device *device,
2562 const struct timespec *time,
2563 int touch_id,
2564 double x, double y,
2565 const struct weston_point2d_device_normalized *norm,
2566 int touch_type)
2567{
Louis-Francis Ratté-Boulianne813a06e2017-11-28 20:42:47 -05002568 struct weston_seat *seat = device->aggregate->seat;
2569 struct weston_touch *touch = device->aggregate;
2570
Pekka Paalanenf4062532018-02-26 16:18:29 +02002571 if (touch_type != WL_TOUCH_UP) {
2572 if (weston_touch_device_can_calibrate(device))
2573 assert(norm != NULL);
2574 else
2575 assert(norm == NULL);
2576 }
2577
Louis-Francis Ratté-Boulianne813a06e2017-11-28 20:42:47 -05002578 /* Update touchpoints count regardless of the current mode. */
2579 switch (touch_type) {
2580 case WL_TOUCH_DOWN:
2581 weston_compositor_idle_inhibit(seat->compositor);
2582
2583 touch->num_tp++;
2584 break;
2585 case WL_TOUCH_UP:
2586 if (touch->num_tp == 0) {
2587 /* This can happen if we start out with one or
2588 * more fingers on the touch screen, in which
2589 * case we didn't get the corresponding down
2590 * event. */
Pekka Paalanen332a45e2018-03-02 14:20:59 +02002591 weston_log("Unmatched touch up event on seat %s, device %s\n",
2592 seat->seat_name, device->syspath);
2593 return;
Louis-Francis Ratté-Boulianne813a06e2017-11-28 20:42:47 -05002594 }
2595 weston_compositor_idle_release(seat->compositor);
2596
2597 touch->num_tp--;
2598 break;
2599 default:
2600 break;
2601 }
2602
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002603 /* Properly forward the touch event */
2604 switch (weston_touch_device_get_mode(device)) {
2605 case WESTON_TOUCH_MODE_NORMAL:
2606 case WESTON_TOUCH_MODE_PREP_CALIB:
2607 process_touch_normal(device, time, touch_id, x, y, touch_type);
2608 break;
2609 case WESTON_TOUCH_MODE_CALIB:
2610 case WESTON_TOUCH_MODE_PREP_NORMAL:
Louis-Francis Ratté-Boulianne83630982017-11-28 20:42:47 -05002611 notify_touch_calibrator(device, time, touch_id,
2612 norm, touch_type);
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002613 break;
2614 }
Pekka Paalanenf4062532018-02-26 16:18:29 +02002615}
2616
Jonas Ådahl1679f232014-04-12 09:39:51 +02002617WL_EXPORT void
Pekka Paalanenbcbce332018-02-26 14:43:00 +02002618notify_touch_frame(struct weston_touch_device *device)
Jonas Ådahl1679f232014-04-12 09:39:51 +02002619{
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002620 struct weston_touch_grab *grab;
Jonas Ådahl1679f232014-04-12 09:39:51 +02002621
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002622 switch (weston_touch_device_get_mode(device)) {
2623 case WESTON_TOUCH_MODE_NORMAL:
2624 case WESTON_TOUCH_MODE_PREP_CALIB:
2625 grab = device->aggregate->grab;
2626 grab->interface->frame(grab);
2627 break;
2628 case WESTON_TOUCH_MODE_CALIB:
2629 case WESTON_TOUCH_MODE_PREP_NORMAL:
Louis-Francis Ratté-Boulianne83630982017-11-28 20:42:47 -05002630 notify_touch_calibrator_frame(device);
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002631 break;
2632 }
2633
2634 weston_compositor_update_touch_mode(device->aggregate->seat->compositor);
Jonas Ådahl1679f232014-04-12 09:39:51 +02002635}
2636
Derek Foreman3cc004a2015-11-06 15:56:09 -06002637WL_EXPORT void
Pekka Paalanenbcbce332018-02-26 14:43:00 +02002638notify_touch_cancel(struct weston_touch_device *device)
Derek Foreman3cc004a2015-11-06 15:56:09 -06002639{
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002640 struct weston_touch_grab *grab;
Derek Foreman3cc004a2015-11-06 15:56:09 -06002641
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002642 switch (weston_touch_device_get_mode(device)) {
2643 case WESTON_TOUCH_MODE_NORMAL:
2644 case WESTON_TOUCH_MODE_PREP_CALIB:
2645 grab = device->aggregate->grab;
2646 grab->interface->cancel(grab);
2647 break;
2648 case WESTON_TOUCH_MODE_CALIB:
2649 case WESTON_TOUCH_MODE_PREP_NORMAL:
Louis-Francis Ratté-Boulianne83630982017-11-28 20:42:47 -05002650 notify_touch_calibrator_cancel(device);
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002651 break;
2652 }
2653
2654 weston_compositor_update_touch_mode(device->aggregate->seat->compositor);
Derek Foreman3cc004a2015-11-06 15:56:09 -06002655}
2656
Pekka Paalanen8274d902014-08-06 19:36:51 +03002657static int
2658pointer_cursor_surface_get_label(struct weston_surface *surface,
2659 char *buf, size_t len)
2660{
2661 return snprintf(buf, len, "cursor");
2662}
2663
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002664static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002665pointer_cursor_surface_committed(struct weston_surface *es,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002666 int32_t dx, int32_t dy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002667{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002668 struct weston_pointer *pointer = es->committed_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002669 int x, y;
2670
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002671 if (es->width == 0)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002672 return;
2673
Jason Ekstranda7af7042013-10-12 22:38:11 -05002674 assert(es == pointer->sprite->surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002675
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002676 pointer->hotspot_x -= dx;
2677 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002678
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002679 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
2680 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002681
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002682 weston_view_set_position(pointer->sprite, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002683
2684 empty_region(&es->pending.input);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02002685 empty_region(&es->input);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002686
2687 if (!weston_surface_is_mapped(es)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002688 weston_layer_entry_insert(&es->compositor->cursor_layer.view_list,
2689 &pointer->sprite->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002690 weston_view_update_transform(pointer->sprite);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002691 es->is_mapped = true;
2692 pointer->sprite->is_mapped = true;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002693 }
2694}
2695
2696static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002697pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2698 uint32_t serial, struct wl_resource *surface_resource,
2699 int32_t x, int32_t y)
2700{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002701 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002702 struct weston_surface *surface = NULL;
2703
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +02002704 if (!pointer)
2705 return;
2706
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002707 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002708 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002709
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002710 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002711 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002712 /* pointer->focus->surface->resource can be NULL. Surfaces like the
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02002713 black_surface used in shell.c for fullscreen don't have
2714 a resource, but can still have focus */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002715 if (pointer->focus->surface->resource == NULL)
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02002716 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002717 if (wl_resource_get_client(pointer->focus->surface->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002718 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002719 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002720 return;
2721
Derek Foreman4e53c532015-03-23 10:55:32 -05002722 if (!surface) {
2723 if (pointer->sprite)
2724 pointer_unmap_sprite(pointer);
2725 return;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002726 }
2727
Jonas Ådahlb4070242015-03-18 15:08:03 +08002728 if (pointer->sprite && pointer->sprite->surface == surface &&
2729 pointer->hotspot_x == x && pointer->hotspot_y == y)
2730 return;
2731
Derek Foreman4e53c532015-03-23 10:55:32 -05002732 if (!pointer->sprite || pointer->sprite->surface != surface) {
2733 if (weston_surface_set_role(surface, "wl_pointer-cursor",
2734 resource,
2735 WL_POINTER_ERROR_ROLE) < 0)
2736 return;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002737
Derek Foreman4e53c532015-03-23 10:55:32 -05002738 if (pointer->sprite)
2739 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002740
Derek Foreman4e53c532015-03-23 10:55:32 -05002741 wl_signal_add(&surface->destroy_signal,
2742 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002743
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002744 surface->committed = pointer_cursor_surface_committed;
2745 surface->committed_private = pointer;
Derek Foreman4e53c532015-03-23 10:55:32 -05002746 weston_surface_set_label_func(surface,
2747 pointer_cursor_surface_get_label);
2748 pointer->sprite = weston_view_create(surface);
2749 }
2750
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002751 pointer->hotspot_x = x;
2752 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002753
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +02002754 if (surface->buffer_ref.buffer) {
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002755 pointer_cursor_surface_committed(surface, 0, 0);
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +02002756 weston_view_schedule_repaint(pointer->sprite);
2757 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002758}
2759
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002760static void
2761pointer_release(struct wl_client *client, struct wl_resource *resource)
2762{
2763 wl_resource_destroy(resource);
2764}
2765
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002766static const struct wl_pointer_interface pointer_interface = {
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002767 pointer_set_cursor,
2768 pointer_release
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002769};
2770
2771static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002772seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2773 uint32_t id)
2774{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002775 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002776 /* We use the pointer_state directly, which means we'll
2777 * give a wl_pointer if the seat has ever had one - even though
2778 * the spec explicitly states that this request only takes effect
2779 * if the seat has the pointer capability.
2780 *
2781 * This prevents a race between the compositor sending new
2782 * capabilities and the client trying to use the old ones.
2783 */
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002784 struct weston_pointer *pointer = seat ? seat->pointer_state : NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002785 struct wl_resource *cr;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002786 struct weston_pointer_client *pointer_client;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002787
Jason Ekstranda85118c2013-06-27 20:17:02 -05002788 cr = wl_resource_create(client, &wl_pointer_interface,
2789 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002790 if (cr == NULL) {
2791 wl_client_post_no_memory(client);
2792 return;
2793 }
2794
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002795 wl_list_init(wl_resource_get_link(cr));
2796 wl_resource_set_implementation(cr, &pointer_interface, pointer,
2797 unbind_pointer_client_resource);
2798
2799 /* If we don't have a pointer_state, the resource is inert, so there
2800 * is nothing more to set up */
2801 if (!pointer)
2802 return;
2803
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002804 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
2805 if (!pointer_client) {
2806 wl_client_post_no_memory(client);
2807 return;
2808 }
2809
2810 wl_list_insert(&pointer_client->pointer_resources,
2811 wl_resource_get_link(cr));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002812
Derek Foreman1281a362015-07-31 16:55:32 -05002813 if (pointer->focus && pointer->focus->surface->resource &&
2814 wl_resource_get_client(pointer->focus->surface->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002815 wl_fixed_t sx, sy;
2816
Derek Foreman1281a362015-07-31 16:55:32 -05002817 weston_view_from_global_fixed(pointer->focus,
2818 pointer->x,
2819 pointer->y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002820 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002821
Neil Roberts96d790e2013-09-19 17:32:00 +01002822 wl_pointer_send_enter(cr,
Derek Foreman1281a362015-07-31 16:55:32 -05002823 pointer->focus_serial,
2824 pointer->focus->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01002825 sx, sy);
Peter Hutterer87743e92016-01-18 16:38:22 +10002826 pointer_send_frame(cr);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002827 }
2828}
2829
2830static void
Alexandros Frantzis2b442482018-02-20 14:05:50 +02002831destroy_keyboard_resource(struct wl_resource *resource)
2832{
2833 struct weston_keyboard *keyboard = wl_resource_get_user_data(resource);
2834
2835 wl_list_remove(wl_resource_get_link(resource));
2836
2837 if (keyboard) {
2838 remove_input_resource_from_timestamps(resource,
2839 &keyboard->timestamps_list);
2840 }
2841}
2842
2843static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002844keyboard_release(struct wl_client *client, struct wl_resource *resource)
2845{
2846 wl_resource_destroy(resource);
2847}
2848
2849static const struct wl_keyboard_interface keyboard_interface = {
2850 keyboard_release
2851};
2852
Derek Foreman280e7dd2014-10-03 13:13:42 -05002853static bool
Neil Roberts96d790e2013-09-19 17:32:00 +01002854should_send_modifiers_to_client(struct weston_seat *seat,
2855 struct wl_client *client)
2856{
Derek Foreman1281a362015-07-31 16:55:32 -05002857 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2858 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2859
2860 if (keyboard &&
2861 keyboard->focus &&
2862 keyboard->focus->resource &&
2863 wl_resource_get_client(keyboard->focus->resource) == client)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002864 return true;
Neil Roberts96d790e2013-09-19 17:32:00 +01002865
Derek Foreman1281a362015-07-31 16:55:32 -05002866 if (pointer &&
2867 pointer->focus &&
2868 pointer->focus->surface->resource &&
2869 wl_resource_get_client(pointer->focus->surface->resource) == client)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002870 return true;
Neil Roberts96d790e2013-09-19 17:32:00 +01002871
Derek Foreman280e7dd2014-10-03 13:13:42 -05002872 return false;
Neil Roberts96d790e2013-09-19 17:32:00 +01002873}
2874
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002875static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002876seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2877 uint32_t id)
2878{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002879 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002880 /* We use the keyboard_state directly, which means we'll
2881 * give a wl_keyboard if the seat has ever had one - even though
2882 * the spec explicitly states that this request only takes effect
2883 * if the seat has the keyboard capability.
2884 *
2885 * This prevents a race between the compositor sending new
2886 * capabilities and the client trying to use the old ones.
2887 */
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002888 struct weston_keyboard *keyboard = seat ? seat->keyboard_state : NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002889 struct wl_resource *cr;
2890
Jason Ekstranda85118c2013-06-27 20:17:02 -05002891 cr = wl_resource_create(client, &wl_keyboard_interface,
2892 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002893 if (cr == NULL) {
2894 wl_client_post_no_memory(client);
2895 return;
2896 }
2897
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002898 wl_list_init(wl_resource_get_link(cr));
2899 wl_resource_set_implementation(cr, &keyboard_interface,
Alexandros Frantzis2b442482018-02-20 14:05:50 +02002900 keyboard, destroy_keyboard_resource);
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002901
2902 /* If we don't have a keyboard_state, the resource is inert, so there
2903 * is nothing more to set up */
2904 if (!keyboard)
2905 return;
2906
Neil Roberts96d790e2013-09-19 17:32:00 +01002907 /* May be moved to focused list later by either
2908 * weston_keyboard_set_focus or directly if this client is already
2909 * focused */
Derek Foreman345c9f32015-06-03 15:53:28 -05002910 wl_list_insert(&keyboard->resource_list, wl_resource_get_link(cr));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002911
Jonny Lamb66a41a02014-08-12 14:58:25 +02002912 if (wl_resource_get_version(cr) >= WL_KEYBOARD_REPEAT_INFO_SINCE_VERSION) {
2913 wl_keyboard_send_repeat_info(cr,
2914 seat->compositor->kb_repeat_rate,
2915 seat->compositor->kb_repeat_delay);
2916 }
Jasper St. Pierred8c6aeb2014-08-04 13:43:24 -04002917
Derek Foremanf8f7fd62017-06-28 11:41:43 -05002918 weston_keyboard_send_keymap(keyboard, cr);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002919
Neil Roberts96d790e2013-09-19 17:32:00 +01002920 if (should_send_modifiers_to_client(seat, client)) {
Derek Foreman345c9f32015-06-03 15:53:28 -05002921 send_modifiers_to_resource(keyboard,
Neil Roberts96d790e2013-09-19 17:32:00 +01002922 cr,
Derek Foreman345c9f32015-06-03 15:53:28 -05002923 keyboard->focus_serial);
Neil Roberts96d790e2013-09-19 17:32:00 +01002924 }
2925
Derek Foreman345c9f32015-06-03 15:53:28 -05002926 if (keyboard->focus && keyboard->focus->resource &&
2927 wl_resource_get_client(keyboard->focus->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01002928 struct weston_surface *surface =
Derek Foreman345c9f32015-06-03 15:53:28 -05002929 (struct weston_surface *)keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +01002930
2931 wl_list_remove(wl_resource_get_link(cr));
Derek Foreman345c9f32015-06-03 15:53:28 -05002932 wl_list_insert(&keyboard->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002933 wl_resource_get_link(cr));
2934 wl_keyboard_send_enter(cr,
Derek Foreman345c9f32015-06-03 15:53:28 -05002935 keyboard->focus_serial,
Neil Roberts96d790e2013-09-19 17:32:00 +01002936 surface->resource,
Derek Foreman345c9f32015-06-03 15:53:28 -05002937 &keyboard->keys);
Neil Roberts96d790e2013-09-19 17:32:00 +01002938
2939 /* If this is the first keyboard resource for this
2940 * client... */
Derek Foreman345c9f32015-06-03 15:53:28 -05002941 if (keyboard->focus_resource_list.prev ==
Neil Roberts96d790e2013-09-19 17:32:00 +01002942 wl_resource_get_link(cr))
2943 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002944 }
2945}
2946
2947static void
Alexandros Frantzisd7157842018-02-20 14:07:03 +02002948destroy_touch_resource(struct wl_resource *resource)
2949{
2950 struct weston_touch *touch = wl_resource_get_user_data(resource);
2951
2952 wl_list_remove(wl_resource_get_link(resource));
2953
2954 if (touch) {
2955 remove_input_resource_from_timestamps(resource,
2956 &touch->timestamps_list);
2957 }
2958}
2959
2960static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002961touch_release(struct wl_client *client, struct wl_resource *resource)
2962{
2963 wl_resource_destroy(resource);
2964}
2965
2966static const struct wl_touch_interface touch_interface = {
2967 touch_release
2968};
2969
2970static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002971seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2972 uint32_t id)
2973{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002974 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002975 /* We use the touch_state directly, which means we'll
2976 * give a wl_touch if the seat has ever had one - even though
2977 * the spec explicitly states that this request only takes effect
2978 * if the seat has the touch capability.
2979 *
2980 * This prevents a race between the compositor sending new
2981 * capabilities and the client trying to use the old ones.
2982 */
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002983 struct weston_touch *touch = seat ? seat->touch_state : NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002984 struct wl_resource *cr;
2985
Jason Ekstranda85118c2013-06-27 20:17:02 -05002986 cr = wl_resource_create(client, &wl_touch_interface,
2987 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002988 if (cr == NULL) {
2989 wl_client_post_no_memory(client);
2990 return;
2991 }
2992
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002993 wl_list_init(wl_resource_get_link(cr));
2994 wl_resource_set_implementation(cr, &touch_interface,
Alexandros Frantzisd7157842018-02-20 14:07:03 +02002995 touch, destroy_touch_resource);
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002996
2997 /* If we don't have a touch_state, the resource is inert, so there
2998 * is nothing more to set up */
2999 if (!touch)
3000 return;
3001
Derek Foreman1281a362015-07-31 16:55:32 -05003002 if (touch->focus &&
3003 wl_resource_get_client(touch->focus->surface->resource) == client) {
Chokshi, Mituld6697142015-10-09 08:28:47 +00003004 wl_list_insert(&touch->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01003005 wl_resource_get_link(cr));
3006 } else {
Chokshi, Mituld6697142015-10-09 08:28:47 +00003007 wl_list_insert(&touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01003008 wl_resource_get_link(cr));
3009 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003010}
3011
Quentin Glidicaab1d362016-03-13 17:49:08 +01003012static void
3013seat_release(struct wl_client *client, struct wl_resource *resource)
3014{
3015 wl_resource_destroy(resource);
3016}
3017
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003018static const struct wl_seat_interface seat_interface = {
3019 seat_get_pointer,
3020 seat_get_keyboard,
3021 seat_get_touch,
Quentin Glidicaab1d362016-03-13 17:49:08 +01003022 seat_release,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003023};
3024
3025static void
3026bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3027{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003028 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003029 struct wl_resource *resource;
3030 enum wl_seat_capability caps = 0;
3031
Jason Ekstranda85118c2013-06-27 20:17:02 -05003032 resource = wl_resource_create(client,
Derek Foreman1909c102015-11-26 14:17:47 -06003033 &wl_seat_interface, version, id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05003034 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003035 wl_resource_set_implementation(resource, &seat_interface, data,
3036 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003037
Derek Foreman1281a362015-07-31 16:55:32 -05003038 if (weston_seat_get_pointer(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003039 caps |= WL_SEAT_CAPABILITY_POINTER;
Derek Foreman1281a362015-07-31 16:55:32 -05003040 if (weston_seat_get_keyboard(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003041 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Derek Foreman1281a362015-07-31 16:55:32 -05003042 if (weston_seat_get_touch(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003043 caps |= WL_SEAT_CAPABILITY_TOUCH;
3044
3045 wl_seat_send_capabilities(resource, caps);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003046 if (version >= WL_SEAT_NAME_SINCE_VERSION)
Rob Bradforde445ae62013-05-31 18:09:51 +01003047 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003048}
3049
Jonas Ådahl30d61d82014-10-22 21:21:17 +02003050static void
3051relative_pointer_destroy(struct wl_client *client,
3052 struct wl_resource *resource)
3053{
3054 wl_resource_destroy(resource);
3055}
3056
3057static const struct zwp_relative_pointer_v1_interface relative_pointer_interface = {
3058 relative_pointer_destroy
3059};
3060
3061static void
3062relative_pointer_manager_destroy(struct wl_client *client,
3063 struct wl_resource *resource)
3064{
3065 wl_resource_destroy(resource);
3066}
3067
3068static void
3069relative_pointer_manager_get_relative_pointer(struct wl_client *client,
3070 struct wl_resource *resource,
3071 uint32_t id,
3072 struct wl_resource *pointer_resource)
3073{
3074 struct weston_pointer *pointer =
3075 wl_resource_get_user_data(pointer_resource);
3076 struct weston_pointer_client *pointer_client;
3077 struct wl_resource *cr;
3078
3079 cr = wl_resource_create(client, &zwp_relative_pointer_v1_interface,
3080 wl_resource_get_version(resource), id);
3081 if (cr == NULL) {
3082 wl_client_post_no_memory(client);
3083 return;
3084 }
3085
3086 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
3087 if (!pointer_client) {
3088 wl_client_post_no_memory(client);
3089 return;
3090 }
3091
3092 wl_list_insert(&pointer_client->relative_pointer_resources,
3093 wl_resource_get_link(cr));
3094 wl_resource_set_implementation(cr, &relative_pointer_interface,
3095 pointer,
3096 unbind_pointer_client_resource);
3097}
3098
3099static const struct zwp_relative_pointer_manager_v1_interface relative_pointer_manager = {
3100 relative_pointer_manager_destroy,
3101 relative_pointer_manager_get_relative_pointer,
3102};
3103
3104static void
3105bind_relative_pointer_manager(struct wl_client *client, void *data,
3106 uint32_t version, uint32_t id)
3107{
3108 struct weston_compositor *compositor = data;
3109 struct wl_resource *resource;
3110
3111 resource = wl_resource_create(client,
3112 &zwp_relative_pointer_manager_v1_interface,
3113 1, id);
3114
3115 wl_resource_set_implementation(resource, &relative_pointer_manager,
3116 compositor,
3117 NULL);
3118}
3119
Giulio Camuffo0358af42016-06-02 21:48:08 +03003120WL_EXPORT int
3121weston_compositor_set_xkb_rule_names(struct weston_compositor *ec,
3122 struct xkb_rule_names *names)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003123{
3124 if (ec->xkb_context == NULL) {
3125 ec->xkb_context = xkb_context_new(0);
3126 if (ec->xkb_context == NULL) {
3127 weston_log("failed to create XKB context\n");
3128 return -1;
3129 }
3130 }
3131
3132 if (names)
3133 ec->xkb_names = *names;
3134 if (!ec->xkb_names.rules)
3135 ec->xkb_names.rules = strdup("evdev");
3136 if (!ec->xkb_names.model)
3137 ec->xkb_names.model = strdup("pc105");
3138 if (!ec->xkb_names.layout)
3139 ec->xkb_names.layout = strdup("us");
3140
3141 return 0;
3142}
3143
Stefan Schmidtfda26522013-09-17 10:54:09 +01003144static void
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003145weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003146{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003147 if (--xkb_info->ref_count > 0)
3148 return;
3149
Ran Benitac9c74152014-08-19 23:59:52 +03003150 xkb_keymap_unref(xkb_info->keymap);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003151
Derek Foreman76829fc2017-06-28 12:17:46 -05003152 if (xkb_info->keymap_string)
3153 free(xkb_info->keymap_string);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003154 free(xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003155}
3156
3157void
3158weston_compositor_xkb_destroy(struct weston_compositor *ec)
3159{
3160 free((char *) ec->xkb_names.rules);
3161 free((char *) ec->xkb_names.model);
3162 free((char *) ec->xkb_names.layout);
3163 free((char *) ec->xkb_names.variant);
3164 free((char *) ec->xkb_names.options);
Stefan Schmidtfda26522013-09-17 10:54:09 +01003165
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003166 if (ec->xkb_info)
3167 weston_xkb_info_destroy(ec->xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003168 xkb_context_unref(ec->xkb_context);
3169}
3170
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003171static struct weston_xkb_info *
3172weston_xkb_info_create(struct xkb_keymap *keymap)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003173{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003174 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
3175 if (xkb_info == NULL)
3176 return NULL;
3177
Ran Benita2e1968f2014-08-19 23:59:51 +03003178 xkb_info->keymap = xkb_keymap_ref(keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003179 xkb_info->ref_count = 1;
3180
Ran Benita2e1968f2014-08-19 23:59:51 +03003181 xkb_info->shift_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3182 XKB_MOD_NAME_SHIFT);
3183 xkb_info->caps_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3184 XKB_MOD_NAME_CAPS);
3185 xkb_info->ctrl_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3186 XKB_MOD_NAME_CTRL);
3187 xkb_info->alt_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3188 XKB_MOD_NAME_ALT);
3189 xkb_info->mod2_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3190 "Mod2");
3191 xkb_info->mod3_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3192 "Mod3");
3193 xkb_info->super_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3194 XKB_MOD_NAME_LOGO);
3195 xkb_info->mod5_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3196 "Mod5");
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003197
Ran Benita2e1968f2014-08-19 23:59:51 +03003198 xkb_info->num_led = xkb_keymap_led_get_index(xkb_info->keymap,
3199 XKB_LED_NAME_NUM);
3200 xkb_info->caps_led = xkb_keymap_led_get_index(xkb_info->keymap,
3201 XKB_LED_NAME_CAPS);
3202 xkb_info->scroll_led = xkb_keymap_led_get_index(xkb_info->keymap,
3203 XKB_LED_NAME_SCROLL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003204
Derek Foreman76829fc2017-06-28 12:17:46 -05003205 xkb_info->keymap_string = xkb_keymap_get_as_string(xkb_info->keymap,
3206 XKB_KEYMAP_FORMAT_TEXT_V1);
3207 if (xkb_info->keymap_string == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003208 weston_log("failed to get string version of keymap\n");
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003209 goto err_keymap;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003210 }
Derek Foreman76829fc2017-06-28 12:17:46 -05003211 xkb_info->keymap_size = strlen(xkb_info->keymap_string) + 1;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003212
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003213 return xkb_info;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003214
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003215err_keymap:
Ran Benita2e1968f2014-08-19 23:59:51 +03003216 xkb_keymap_unref(xkb_info->keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003217 free(xkb_info);
3218 return NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003219}
3220
3221static int
3222weston_compositor_build_global_keymap(struct weston_compositor *ec)
3223{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003224 struct xkb_keymap *keymap;
3225
3226 if (ec->xkb_info != NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003227 return 0;
3228
Ran Benita2e1968f2014-08-19 23:59:51 +03003229 keymap = xkb_keymap_new_from_names(ec->xkb_context,
3230 &ec->xkb_names,
3231 0);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003232 if (keymap == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003233 weston_log("failed to compile global XKB keymap\n");
3234 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
3235 "options %s\n",
3236 ec->xkb_names.rules, ec->xkb_names.model,
3237 ec->xkb_names.layout, ec->xkb_names.variant,
3238 ec->xkb_names.options);
3239 return -1;
3240 }
3241
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003242 ec->xkb_info = weston_xkb_info_create(keymap);
Rui Matos73d93952013-10-24 19:28:41 +02003243 xkb_keymap_unref(keymap);
Stefan Schmidtfda26522013-09-17 10:54:09 +01003244 if (ec->xkb_info == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003245 return -1;
3246
3247 return 0;
3248}
3249
Rui Matos65196bc2013-10-10 19:44:19 +02003250WL_EXPORT void
3251weston_seat_update_keymap(struct weston_seat *seat, struct xkb_keymap *keymap)
3252{
Derek Foreman1281a362015-07-31 16:55:32 -05003253 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
3254
3255 if (!keyboard || !keymap)
Rui Matos65196bc2013-10-10 19:44:19 +02003256 return;
3257
Derek Foreman1281a362015-07-31 16:55:32 -05003258 xkb_keymap_unref(keyboard->pending_keymap);
3259 keyboard->pending_keymap = xkb_keymap_ref(keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02003260
Derek Foreman1281a362015-07-31 16:55:32 -05003261 if (keyboard->keys.size == 0)
Rui Matos65196bc2013-10-10 19:44:19 +02003262 update_keymap(seat);
Rui Matos65196bc2013-10-10 19:44:19 +02003263}
3264
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003265WL_EXPORT int
3266weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
3267{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003268 struct weston_keyboard *keyboard;
3269
Derek Foreman1281a362015-07-31 16:55:32 -05003270 if (seat->keyboard_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003271 seat->keyboard_device_count += 1;
3272 if (seat->keyboard_device_count == 1)
3273 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003274 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003275 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003276
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003277 keyboard = weston_keyboard_create();
3278 if (keyboard == NULL) {
3279 weston_log("failed to allocate weston keyboard struct\n");
3280 return -1;
3281 }
3282
Derek Foreman185d1582017-06-28 11:17:23 -05003283 if (keymap != NULL) {
3284 keyboard->xkb_info = weston_xkb_info_create(keymap);
3285 if (keyboard->xkb_info == NULL)
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02003286 goto err;
Derek Foreman185d1582017-06-28 11:17:23 -05003287 } else {
3288 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
3289 goto err;
3290 keyboard->xkb_info = seat->compositor->xkb_info;
3291 keyboard->xkb_info->ref_count++;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01003292 }
Derek Foreman185d1582017-06-28 11:17:23 -05003293
3294 keyboard->xkb_state.state = xkb_state_new(keyboard->xkb_info->keymap);
3295 if (keyboard->xkb_state.state == NULL) {
3296 weston_log("failed to initialise XKB state\n");
3297 goto err;
3298 }
3299
3300 keyboard->xkb_state.leds = 0;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01003301
Derek Foreman1281a362015-07-31 16:55:32 -05003302 seat->keyboard_state = keyboard;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02003303 seat->keyboard_device_count = 1;
3304 keyboard->seat = seat;
3305
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003306 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003307
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003308 return 0;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02003309
3310err:
3311 if (keyboard->xkb_info)
3312 weston_xkb_info_destroy(keyboard->xkb_info);
3313 free(keyboard);
3314
3315 return -1;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003316}
3317
Jonas Ådahl91fed542013-12-03 09:14:27 +01003318static void
3319weston_keyboard_reset_state(struct weston_keyboard *keyboard)
3320{
3321 struct weston_seat *seat = keyboard->seat;
3322 struct xkb_state *state;
3323
Derek Foreman185d1582017-06-28 11:17:23 -05003324 state = xkb_state_new(keyboard->xkb_info->keymap);
3325 if (!state) {
3326 weston_log("failed to reset XKB state\n");
3327 return;
Jonas Ådahl91fed542013-12-03 09:14:27 +01003328 }
Derek Foreman185d1582017-06-28 11:17:23 -05003329 xkb_state_unref(keyboard->xkb_state.state);
3330 keyboard->xkb_state.state = state;
3331
3332 keyboard->xkb_state.leds = 0;
Jonas Ådahl91fed542013-12-03 09:14:27 +01003333
3334 seat->modifier_state = 0;
3335}
3336
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003337WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003338weston_seat_release_keyboard(struct weston_seat *seat)
3339{
3340 seat->keyboard_device_count--;
Derek Foremand621df22014-11-19 11:04:12 -06003341 assert(seat->keyboard_device_count >= 0);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003342 if (seat->keyboard_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05003343 weston_keyboard_set_focus(seat->keyboard_state, NULL);
3344 weston_keyboard_cancel_grab(seat->keyboard_state);
3345 weston_keyboard_reset_state(seat->keyboard_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003346 seat_send_updated_caps(seat);
3347 }
3348}
3349
3350WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003351weston_seat_init_pointer(struct weston_seat *seat)
3352{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003353 struct weston_pointer *pointer;
3354
Derek Foreman1281a362015-07-31 16:55:32 -05003355 if (seat->pointer_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003356 seat->pointer_device_count += 1;
3357 if (seat->pointer_device_count == 1)
3358 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003359 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003360 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003361
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003362 pointer = weston_pointer_create(seat);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003363 if (pointer == NULL)
3364 return;
3365
Derek Foreman1281a362015-07-31 16:55:32 -05003366 seat->pointer_state = pointer;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003367 seat->pointer_device_count = 1;
3368 pointer->seat = seat;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003369
3370 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003371}
3372
3373WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003374weston_seat_release_pointer(struct weston_seat *seat)
3375{
Derek Foreman1281a362015-07-31 16:55:32 -05003376 struct weston_pointer *pointer = seat->pointer_state;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003377
3378 seat->pointer_device_count--;
3379 if (seat->pointer_device_count == 0) {
Derek Foremanf9318d12015-05-11 15:40:11 -05003380 weston_pointer_clear_focus(pointer);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003381 weston_pointer_cancel_grab(pointer);
Jonas Ådahl630bae82013-10-17 23:04:06 +02003382
Jonas Ådahla4932742013-10-17 23:04:07 +02003383 if (pointer->sprite)
3384 pointer_unmap_sprite(pointer);
3385
Jonas Ådahl3e12e632013-12-02 22:05:05 +01003386 weston_pointer_reset_state(pointer);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003387 seat_send_updated_caps(seat);
Derek Foremanfd5ca512015-01-07 15:00:25 -06003388
3389 /* seat->pointer is intentionally not destroyed so that
3390 * a newly attached pointer on this seat will retain
3391 * the previous cursor co-ordinates.
3392 */
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003393 }
3394}
3395
3396WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003397weston_seat_init_touch(struct weston_seat *seat)
3398{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003399 struct weston_touch *touch;
3400
Derek Foreman1281a362015-07-31 16:55:32 -05003401 if (seat->touch_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003402 seat->touch_device_count += 1;
3403 if (seat->touch_device_count == 1)
3404 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003405 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003406 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003407
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003408 touch = weston_touch_create();
3409 if (touch == NULL)
3410 return;
3411
Derek Foreman1281a362015-07-31 16:55:32 -05003412 seat->touch_state = touch;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003413 seat->touch_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003414 touch->seat = seat;
3415
3416 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003417}
3418
3419WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003420weston_seat_release_touch(struct weston_seat *seat)
3421{
3422 seat->touch_device_count--;
3423 if (seat->touch_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05003424 weston_touch_set_focus(seat->touch_state, NULL);
3425 weston_touch_cancel_grab(seat->touch_state);
3426 weston_touch_reset_state(seat->touch_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003427 seat_send_updated_caps(seat);
3428 }
3429}
3430
3431WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003432weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
3433 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003434{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003435 memset(seat, 0, sizeof *seat);
3436
Kristian Høgsberge3148752013-05-06 23:19:49 -04003437 seat->selection_data_source = NULL;
3438 wl_list_init(&seat->base_resource_list);
3439 wl_signal_init(&seat->selection_signal);
3440 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04003441 wl_signal_init(&seat->destroy_signal);
Jason Ekstranda4ab5422014-04-02 19:53:45 -05003442 wl_signal_init(&seat->updated_caps_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003443
Peter Hutterer87743e92016-01-18 16:38:22 +10003444 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 5,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003445 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003446
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003447 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003448 seat->modifier_state = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003449 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003450
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003451 wl_list_insert(ec->seat_list.prev, &seat->link);
3452
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003453 clipboard_create(seat);
3454
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003455 wl_signal_emit(&ec->seat_created_signal, seat);
3456}
3457
3458WL_EXPORT void
3459weston_seat_release(struct weston_seat *seat)
3460{
Alexandros Frantzis8480d132018-02-15 13:07:09 +02003461 struct wl_resource *resource;
3462
3463 wl_resource_for_each(resource, &seat->base_resource_list) {
3464 wl_resource_set_user_data(resource, NULL);
3465 }
3466
3467 wl_resource_for_each(resource, &seat->drag_resource_list) {
3468 wl_resource_set_user_data(resource, NULL);
3469 }
3470
3471 wl_list_remove(&seat->base_resource_list);
3472 wl_list_remove(&seat->drag_resource_list);
3473
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003474 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003475
Jonas Ådahl1afb2382014-01-03 19:46:51 +01003476 if (seat->saved_kbd_focus)
3477 wl_list_remove(&seat->saved_kbd_focus_listener.link);
3478
Derek Foreman1281a362015-07-31 16:55:32 -05003479 if (seat->pointer_state)
3480 weston_pointer_destroy(seat->pointer_state);
3481 if (seat->keyboard_state)
3482 weston_keyboard_destroy(seat->keyboard_state);
3483 if (seat->touch_state)
3484 weston_touch_destroy(seat->touch_state);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003485
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003486 free (seat->seat_name);
3487
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003488 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04003489
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003490 wl_signal_emit(&seat->destroy_signal, seat);
3491}
Derek Foreman1281a362015-07-31 16:55:32 -05003492
3493/** Get a seat's keyboard pointer
3494 *
3495 * \param seat The seat to query
3496 * \return The seat's keyboard pointer, or NULL if no keyboard is present
3497 *
3498 * The keyboard pointer for a seat isn't freed when all keyboards are removed,
3499 * so it should only be used when the seat's keyboard_device_count is greater
3500 * than zero. This function does that test and only returns a pointer
3501 * when a keyboard is present.
3502 */
3503WL_EXPORT struct weston_keyboard *
3504weston_seat_get_keyboard(struct weston_seat *seat)
3505{
3506 if (!seat)
3507 return NULL;
3508
3509 if (seat->keyboard_device_count)
3510 return seat->keyboard_state;
3511
3512 return NULL;
3513}
3514
3515/** Get a seat's pointer pointer
3516 *
3517 * \param seat The seat to query
3518 * \return The seat's pointer pointer, or NULL if no pointer device is present
3519 *
3520 * The pointer pointer for a seat isn't freed when all mice are removed,
3521 * so it should only be used when the seat's pointer_device_count is greater
3522 * than zero. This function does that test and only returns a pointer
3523 * when a pointing device is present.
3524 */
3525WL_EXPORT struct weston_pointer *
3526weston_seat_get_pointer(struct weston_seat *seat)
3527{
3528 if (!seat)
3529 return NULL;
3530
3531 if (seat->pointer_device_count)
3532 return seat->pointer_state;
3533
3534 return NULL;
3535}
3536
Jonas Ådahld3414f22016-07-22 17:56:31 +08003537static const struct zwp_locked_pointer_v1_interface locked_pointer_interface;
3538static const struct zwp_confined_pointer_v1_interface confined_pointer_interface;
3539
3540static enum pointer_constraint_type
3541pointer_constraint_get_type(struct weston_pointer_constraint *constraint)
3542{
3543 if (wl_resource_instance_of(constraint->resource,
3544 &zwp_locked_pointer_v1_interface,
3545 &locked_pointer_interface)) {
3546 return POINTER_CONSTRAINT_TYPE_LOCK;
3547 } else if (wl_resource_instance_of(constraint->resource,
3548 &zwp_confined_pointer_v1_interface,
3549 &confined_pointer_interface)) {
3550 return POINTER_CONSTRAINT_TYPE_CONFINE;
3551 }
3552
3553 abort();
3554 return 0;
3555}
3556
3557static void
3558pointer_constraint_notify_activated(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_locked(resource);
3565 break;
3566 case POINTER_CONSTRAINT_TYPE_CONFINE:
3567 zwp_confined_pointer_v1_send_confined(resource);
3568 break;
3569 }
3570}
3571
3572static void
3573pointer_constraint_notify_deactivated(struct weston_pointer_constraint *constraint)
3574{
3575 struct wl_resource *resource = constraint->resource;
3576
3577 switch (pointer_constraint_get_type(constraint)) {
3578 case POINTER_CONSTRAINT_TYPE_LOCK:
3579 zwp_locked_pointer_v1_send_unlocked(resource);
3580 break;
3581 case POINTER_CONSTRAINT_TYPE_CONFINE:
3582 zwp_confined_pointer_v1_send_unconfined(resource);
3583 break;
3584 }
3585}
3586
3587static struct weston_pointer_constraint *
3588get_pointer_constraint_for_pointer(struct weston_surface *surface,
3589 struct weston_pointer *pointer)
3590{
3591 struct weston_pointer_constraint *constraint;
3592
3593 wl_list_for_each(constraint, &surface->pointer_constraints, link) {
3594 if (constraint->pointer == pointer)
3595 return constraint;
3596 }
3597
3598 return NULL;
3599}
3600
Derek Foreman1281a362015-07-31 16:55:32 -05003601/** Get a seat's touch pointer
3602 *
3603 * \param seat The seat to query
3604 * \return The seat's touch pointer, or NULL if no touch device is present
3605 *
3606 * The touch pointer for a seat isn't freed when all touch devices are removed,
3607 * so it should only be used when the seat's touch_device_count is greater
3608 * than zero. This function does that test and only returns a pointer
3609 * when a touch device is present.
3610 */
3611WL_EXPORT struct weston_touch *
3612weston_seat_get_touch(struct weston_seat *seat)
3613{
3614 if (!seat)
3615 return NULL;
3616
3617 if (seat->touch_device_count)
3618 return seat->touch_state;
3619
3620 return NULL;
3621}
Bryce Harrington24f917e2016-06-29 19:04:07 -07003622
3623/** Sets the keyboard focus to the given surface
3624 *
Marius Vlada2dace22019-06-12 16:05:44 +03003625 * \param surface the surface to focus on
Bryce Harrington24f917e2016-06-29 19:04:07 -07003626 * \param seat The seat to query
3627 */
3628WL_EXPORT void
3629weston_seat_set_keyboard_focus(struct weston_seat *seat,
3630 struct weston_surface *surface)
3631{
3632 struct weston_compositor *compositor = seat->compositor;
3633 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003634 struct weston_surface_activation_data activation_data;
Bryce Harrington24f917e2016-06-29 19:04:07 -07003635
Jonas Ådahlef8e1c32016-03-15 20:28:51 +08003636 if (keyboard && keyboard->focus != surface) {
Bryce Harrington24f917e2016-06-29 19:04:07 -07003637 weston_keyboard_set_focus(keyboard, surface);
3638 wl_data_device_set_keyboard_focus(seat);
3639 }
3640
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003641 inc_activate_serial(compositor);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003642
3643 activation_data = (struct weston_surface_activation_data) {
3644 .surface = surface,
3645 .seat = seat,
3646 };
3647 wl_signal_emit(&compositor->activate_signal, &activation_data);
Bryce Harrington24f917e2016-06-29 19:04:07 -07003648}
Jonas Ådahl30d61d82014-10-22 21:21:17 +02003649
Jonas Ådahld3414f22016-07-22 17:56:31 +08003650static void
3651enable_pointer_constraint(struct weston_pointer_constraint *constraint,
3652 struct weston_view *view)
3653{
3654 assert(constraint->view == NULL);
3655 constraint->view = view;
3656 pointer_constraint_notify_activated(constraint);
3657 weston_pointer_start_grab(constraint->pointer, &constraint->grab);
3658 wl_list_remove(&constraint->surface_destroy_listener.link);
3659 wl_list_init(&constraint->surface_destroy_listener.link);
3660}
3661
3662static bool
3663is_pointer_constraint_enabled(struct weston_pointer_constraint *constraint)
3664{
3665 return constraint->view != NULL;
3666}
3667
3668static void
3669weston_pointer_constraint_disable(struct weston_pointer_constraint *constraint)
3670{
3671 constraint->view = NULL;
3672 pointer_constraint_notify_deactivated(constraint);
3673 weston_pointer_end_grab(constraint->grab.pointer);
3674}
3675
3676void
3677weston_pointer_constraint_destroy(struct weston_pointer_constraint *constraint)
3678{
3679 if (is_pointer_constraint_enabled(constraint))
3680 weston_pointer_constraint_disable(constraint);
3681
3682 wl_list_remove(&constraint->pointer_destroy_listener.link);
3683 wl_list_remove(&constraint->surface_destroy_listener.link);
3684 wl_list_remove(&constraint->surface_commit_listener.link);
3685 wl_list_remove(&constraint->surface_activate_listener.link);
3686
3687 wl_resource_set_user_data(constraint->resource, NULL);
3688 pixman_region32_fini(&constraint->region);
3689 wl_list_remove(&constraint->link);
3690 free(constraint);
3691}
3692
3693static void
3694disable_pointer_constraint(struct weston_pointer_constraint *constraint)
3695{
3696 switch (constraint->lifetime) {
3697 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT:
3698 weston_pointer_constraint_destroy(constraint);
3699 break;
3700 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT:
3701 weston_pointer_constraint_disable(constraint);
3702 break;
3703 }
3704}
3705
3706static bool
3707is_within_constraint_region(struct weston_pointer_constraint *constraint,
3708 wl_fixed_t sx, wl_fixed_t sy)
3709{
3710 struct weston_surface *surface = constraint->surface;
3711 pixman_region32_t constraint_region;
3712 bool result;
3713
3714 pixman_region32_init(&constraint_region);
3715 pixman_region32_intersect(&constraint_region,
3716 &surface->input,
3717 &constraint->region);
3718 result = pixman_region32_contains_point(&constraint_region,
3719 wl_fixed_to_int(sx),
3720 wl_fixed_to_int(sy),
3721 NULL);
3722 pixman_region32_fini(&constraint_region);
3723
3724 return result;
3725}
3726
3727static void
3728maybe_enable_pointer_constraint(struct weston_pointer_constraint *constraint)
3729{
3730 struct weston_surface *surface = constraint->surface;
3731 struct weston_view *vit;
3732 struct weston_view *view = NULL;
3733 struct weston_pointer *pointer = constraint->pointer;
3734 struct weston_keyboard *keyboard;
3735 struct weston_seat *seat = pointer->seat;
3736 int32_t x, y;
3737
3738 /* Postpone if no view of the surface was most recently clicked. */
3739 wl_list_for_each(vit, &surface->views, surface_link) {
3740 if (vit->click_to_activate_serial ==
3741 surface->compositor->activate_serial) {
3742 view = vit;
3743 }
3744 }
3745 if (view == NULL)
3746 return;
3747
3748 /* Postpone if surface doesn't have keyboard focus. */
3749 keyboard = weston_seat_get_keyboard(seat);
3750 if (!keyboard || keyboard->focus != surface)
3751 return;
3752
3753 /* Postpone constraint if the pointer is not within the
3754 * constraint region.
3755 */
3756 weston_view_from_global(view,
3757 wl_fixed_to_int(pointer->x),
3758 wl_fixed_to_int(pointer->y),
3759 &x, &y);
3760 if (!is_within_constraint_region(constraint,
3761 wl_fixed_from_int(x),
3762 wl_fixed_from_int(y)))
3763 return;
3764
3765 enable_pointer_constraint(constraint, view);
3766}
3767
3768static void
3769locked_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
3770{
3771}
3772
3773static void
3774locked_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02003775 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003776 struct weston_pointer_motion_event *event)
3777{
Quentin Glidiccde13452016-08-12 10:41:32 +02003778 pointer_send_relative_motion(grab->pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08003779}
3780
3781static void
3782locked_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003783 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003784 uint32_t button,
3785 uint32_t state_w)
3786{
3787 weston_pointer_send_button(grab->pointer, time, button, state_w);
3788}
3789
3790static void
3791locked_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02003792 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003793 struct weston_pointer_axis_event *event)
3794{
3795 weston_pointer_send_axis(grab->pointer, time, event);
3796}
3797
3798static void
3799locked_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
3800 uint32_t source)
3801{
3802 weston_pointer_send_axis_source(grab->pointer, source);
3803}
3804
3805static void
3806locked_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
3807{
3808 weston_pointer_send_frame(grab->pointer);
3809}
3810
3811static void
3812locked_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
3813{
3814 struct weston_pointer_constraint *constraint =
3815 container_of(grab, struct weston_pointer_constraint, grab);
3816
3817 disable_pointer_constraint(constraint);
3818}
3819
3820static const struct weston_pointer_grab_interface
3821 locked_pointer_grab_interface = {
3822 locked_pointer_grab_pointer_focus,
3823 locked_pointer_grab_pointer_motion,
3824 locked_pointer_grab_pointer_button,
3825 locked_pointer_grab_pointer_axis,
3826 locked_pointer_grab_pointer_axis_source,
3827 locked_pointer_grab_pointer_frame,
3828 locked_pointer_grab_pointer_cancel,
3829};
3830
3831static void
3832pointer_constraint_constrain_resource_destroyed(struct wl_resource *resource)
3833{
3834 struct weston_pointer_constraint *constraint =
3835 wl_resource_get_user_data(resource);
3836
3837 if (!constraint)
3838 return;
3839
3840 weston_pointer_constraint_destroy(constraint);
3841}
3842
3843static void
3844pointer_constraint_surface_activate(struct wl_listener *listener, void *data)
3845{
3846 struct weston_surface_activation_data *activation = data;
3847 struct weston_pointer *pointer;
3848 struct weston_surface *focus = activation->surface;
3849 struct weston_pointer_constraint *constraint =
3850 container_of(listener, struct weston_pointer_constraint,
3851 surface_activate_listener);
3852 bool is_constraint_surface;
3853
3854 pointer = weston_seat_get_pointer(activation->seat);
3855 if (!pointer)
3856 return;
3857
3858 is_constraint_surface =
3859 get_pointer_constraint_for_pointer(focus, pointer) == constraint;
3860
3861 if (is_constraint_surface &&
3862 !is_pointer_constraint_enabled(constraint))
3863 maybe_enable_pointer_constraint(constraint);
3864 else if (!is_constraint_surface &&
3865 is_pointer_constraint_enabled(constraint))
3866 disable_pointer_constraint(constraint);
3867}
3868
3869static void
3870pointer_constraint_pointer_destroyed(struct wl_listener *listener, void *data)
3871{
3872 struct weston_pointer_constraint *constraint =
3873 container_of(listener, struct weston_pointer_constraint,
3874 pointer_destroy_listener);
3875
3876 weston_pointer_constraint_destroy(constraint);
3877}
3878
3879static void
3880pointer_constraint_surface_destroyed(struct wl_listener *listener, void *data)
3881{
3882 struct weston_pointer_constraint *constraint =
3883 container_of(listener, struct weston_pointer_constraint,
3884 surface_destroy_listener);
3885
3886 weston_pointer_constraint_destroy(constraint);
3887}
3888
3889static void
3890pointer_constraint_surface_committed(struct wl_listener *listener, void *data)
3891{
3892 struct weston_pointer_constraint *constraint =
3893 container_of(listener, struct weston_pointer_constraint,
3894 surface_commit_listener);
3895
3896 if (constraint->region_is_pending) {
3897 constraint->region_is_pending = false;
3898 pixman_region32_copy(&constraint->region,
3899 &constraint->region_pending);
3900 pixman_region32_fini(&constraint->region_pending);
3901 pixman_region32_init(&constraint->region_pending);
3902 }
3903
3904 if (constraint->hint_is_pending) {
3905 constraint->hint_is_pending = false;
3906
3907 constraint->hint_is_pending = true;
3908 constraint->hint_x = constraint->hint_x_pending;
3909 constraint->hint_y = constraint->hint_y_pending;
3910 }
3911
3912 if (pointer_constraint_get_type(constraint) ==
3913 POINTER_CONSTRAINT_TYPE_CONFINE &&
3914 is_pointer_constraint_enabled(constraint))
3915 maybe_warp_confined_pointer(constraint);
3916}
3917
3918static struct weston_pointer_constraint *
3919weston_pointer_constraint_create(struct weston_surface *surface,
3920 struct weston_pointer *pointer,
3921 struct weston_region *region,
3922 enum zwp_pointer_constraints_v1_lifetime lifetime,
3923 struct wl_resource *cr,
3924 const struct weston_pointer_grab_interface *grab_interface)
3925{
3926 struct weston_pointer_constraint *constraint;
3927
3928 constraint = zalloc(sizeof *constraint);
3929 if (!constraint)
3930 return NULL;
3931
3932 constraint->lifetime = lifetime;
3933 pixman_region32_init(&constraint->region);
3934 pixman_region32_init(&constraint->region_pending);
3935 wl_list_insert(&surface->pointer_constraints, &constraint->link);
3936 constraint->surface = surface;
3937 constraint->pointer = pointer;
3938 constraint->resource = cr;
3939 constraint->grab.interface = grab_interface;
3940 if (region) {
3941 pixman_region32_copy(&constraint->region,
3942 &region->region);
3943 } else {
3944 pixman_region32_fini(&constraint->region);
3945 region_init_infinite(&constraint->region);
3946 }
3947
3948 constraint->surface_activate_listener.notify =
3949 pointer_constraint_surface_activate;
3950 constraint->surface_destroy_listener.notify =
3951 pointer_constraint_surface_destroyed;
3952 constraint->surface_commit_listener.notify =
3953 pointer_constraint_surface_committed;
3954 constraint->pointer_destroy_listener.notify =
3955 pointer_constraint_pointer_destroyed;
3956
3957 wl_signal_add(&surface->compositor->activate_signal,
3958 &constraint->surface_activate_listener);
3959 wl_signal_add(&pointer->destroy_signal,
3960 &constraint->pointer_destroy_listener);
3961 wl_signal_add(&surface->destroy_signal,
3962 &constraint->surface_destroy_listener);
3963 wl_signal_add(&surface->commit_signal,
3964 &constraint->surface_commit_listener);
3965
3966 return constraint;
3967}
3968
3969static void
3970init_pointer_constraint(struct wl_resource *pointer_constraints_resource,
3971 uint32_t id,
3972 struct weston_surface *surface,
3973 struct weston_pointer *pointer,
3974 struct weston_region *region,
3975 enum zwp_pointer_constraints_v1_lifetime lifetime,
3976 const struct wl_interface *interface,
3977 const void *implementation,
3978 const struct weston_pointer_grab_interface *grab_interface)
3979{
3980 struct wl_client *client =
3981 wl_resource_get_client(pointer_constraints_resource);
3982 struct wl_resource *cr;
3983 struct weston_pointer_constraint *constraint;
3984
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02003985 if (pointer && get_pointer_constraint_for_pointer(surface, pointer)) {
Jonas Ådahld3414f22016-07-22 17:56:31 +08003986 wl_resource_post_error(pointer_constraints_resource,
3987 ZWP_POINTER_CONSTRAINTS_V1_ERROR_ALREADY_CONSTRAINED,
3988 "the pointer has a lock/confine request on this surface");
3989 return;
3990 }
3991
3992 cr = wl_resource_create(client, interface,
3993 wl_resource_get_version(pointer_constraints_resource),
3994 id);
3995 if (cr == NULL) {
3996 wl_client_post_no_memory(client);
3997 return;
3998 }
3999
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02004000 if (pointer) {
4001 constraint = weston_pointer_constraint_create(surface, pointer,
4002 region, lifetime,
4003 cr, grab_interface);
4004 if (constraint == NULL) {
4005 wl_client_post_no_memory(client);
4006 return;
4007 }
4008 } else {
4009 constraint = NULL;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004010 }
4011
4012 wl_resource_set_implementation(cr, implementation, constraint,
4013 pointer_constraint_constrain_resource_destroyed);
4014
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02004015 if (constraint)
4016 maybe_enable_pointer_constraint(constraint);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004017}
4018
4019static void
4020pointer_constraints_destroy(struct wl_client *client,
4021 struct wl_resource *resource)
4022{
4023 wl_resource_destroy(resource);
4024}
4025
4026static void
4027locked_pointer_destroy(struct wl_client *client,
4028 struct wl_resource *resource)
4029{
4030 struct weston_pointer_constraint *constraint =
4031 wl_resource_get_user_data(resource);
4032 wl_fixed_t x, y;
4033
4034 if (constraint && constraint->view && constraint->hint_is_pending &&
4035 is_within_constraint_region(constraint,
4036 constraint->hint_x,
4037 constraint->hint_y)) {
4038 weston_view_to_global_fixed(constraint->view,
4039 constraint->hint_x,
4040 constraint->hint_y,
4041 &x, &y);
4042 weston_pointer_move_to(constraint->pointer, x, y);
4043 }
4044 wl_resource_destroy(resource);
4045}
4046
4047static void
4048locked_pointer_set_cursor_position_hint(struct wl_client *client,
4049 struct wl_resource *resource,
4050 wl_fixed_t surface_x,
4051 wl_fixed_t surface_y)
4052{
4053 struct weston_pointer_constraint *constraint =
4054 wl_resource_get_user_data(resource);
4055
4056 /* Ignore a set cursor hint that was sent after the lock was cancelled.
4057 */
4058 if (!constraint ||
4059 !constraint->resource ||
4060 constraint->resource != resource)
4061 return;
4062
4063 constraint->hint_is_pending = true;
4064 constraint->hint_x_pending = surface_x;
4065 constraint->hint_y_pending = surface_y;
4066}
4067
4068static void
4069locked_pointer_set_region(struct wl_client *client,
4070 struct wl_resource *resource,
4071 struct wl_resource *region_resource)
4072{
4073 struct weston_pointer_constraint *constraint =
4074 wl_resource_get_user_data(resource);
4075 struct weston_region *region = region_resource ?
4076 wl_resource_get_user_data(region_resource) : NULL;
4077
4078 if (!constraint)
4079 return;
4080
4081 if (region) {
4082 pixman_region32_copy(&constraint->region_pending,
4083 &region->region);
4084 } else {
4085 pixman_region32_fini(&constraint->region_pending);
4086 region_init_infinite(&constraint->region_pending);
4087 }
4088 constraint->region_is_pending = true;
4089}
4090
4091
4092static const struct zwp_locked_pointer_v1_interface locked_pointer_interface = {
4093 locked_pointer_destroy,
4094 locked_pointer_set_cursor_position_hint,
4095 locked_pointer_set_region,
4096};
4097
4098static void
4099pointer_constraints_lock_pointer(struct wl_client *client,
4100 struct wl_resource *resource,
4101 uint32_t id,
4102 struct wl_resource *surface_resource,
4103 struct wl_resource *pointer_resource,
4104 struct wl_resource *region_resource,
4105 uint32_t lifetime)
4106{
4107 struct weston_surface *surface =
4108 wl_resource_get_user_data(surface_resource);
4109 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
4110 struct weston_region *region = region_resource ?
4111 wl_resource_get_user_data(region_resource) : NULL;
4112
4113 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
4114 &zwp_locked_pointer_v1_interface,
4115 &locked_pointer_interface,
4116 &locked_pointer_grab_interface);
4117}
4118
4119static void
4120confined_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
4121{
4122}
4123
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004124static double
4125vec2d_cross_product(struct vec2d a, struct vec2d b)
4126{
4127 return a.x * b.y - a.y * b.x;
4128}
4129
4130static struct vec2d
4131vec2d_add(struct vec2d a, struct vec2d b)
4132{
4133 return (struct vec2d) {
4134 .x = a.x + b.x,
4135 .y = a.y + b.y,
4136 };
4137}
4138
4139static struct vec2d
4140vec2d_subtract(struct vec2d a, struct vec2d b)
4141{
4142 return (struct vec2d) {
4143 .x = a.x - b.x,
4144 .y = a.y - b.y,
4145 };
4146}
4147
4148static struct vec2d
4149vec2d_multiply_constant(double c, struct vec2d a)
4150{
4151 return (struct vec2d) {
4152 .x = c * a.x,
4153 .y = c * a.y,
4154 };
4155}
4156
4157static bool
4158lines_intersect(struct line *line1, struct line *line2,
4159 struct vec2d *intersection)
4160{
4161 struct vec2d p = line1->a;
4162 struct vec2d r = vec2d_subtract(line1->b, line1->a);
4163 struct vec2d q = line2->a;
4164 struct vec2d s = vec2d_subtract(line2->b, line2->a);
4165 double rxs;
4166 double sxr;
4167 double t;
4168 double u;
4169
4170 /*
4171 * The line (p, r) and (q, s) intersects where
4172 *
4173 * p + t r = q + u s
4174 *
4175 * Calculate t:
4176 *
4177 * (p + t r) × s = (q + u s) × s
4178 * p × s + t (r × s) = q × s + u (s × s)
4179 * p × s + t (r × s) = q × s
4180 * t (r × s) = q × s - p × s
4181 * t (r × s) = (q - p) × s
4182 * t = ((q - p) × s) / (r × s)
4183 *
4184 * Using the same method, for u we get:
4185 *
4186 * u = ((p - q) × r) / (s × r)
4187 */
4188
4189 rxs = vec2d_cross_product(r, s);
4190 sxr = vec2d_cross_product(s, r);
4191
4192 /* If r × s = 0 then the lines are either parallel or collinear. */
4193 if (fabs(rxs) < DBL_MIN)
4194 return false;
4195
4196 t = vec2d_cross_product(vec2d_subtract(q, p), s) / rxs;
4197 u = vec2d_cross_product(vec2d_subtract(p, q), r) / sxr;
4198
4199 /* The lines only intersect if 0 ≤ t ≤ 1 and 0 ≤ u ≤ 1. */
4200 if (t < 0.0 || t > 1.0 || u < 0.0 || u > 1.0)
4201 return false;
4202
4203 *intersection = vec2d_add(p, vec2d_multiply_constant(t, r));
4204 return true;
4205}
4206
4207static struct border *
4208add_border(struct wl_array *array,
4209 double x1, double y1,
4210 double x2, double y2,
4211 enum motion_direction blocking_dir)
4212{
4213 struct border *border = wl_array_add(array, sizeof *border);
4214
4215 *border = (struct border) {
4216 .line = (struct line) {
4217 .a = (struct vec2d) {
4218 .x = x1,
4219 .y = y1,
4220 },
4221 .b = (struct vec2d) {
4222 .x = x2,
4223 .y = y2,
4224 },
4225 },
4226 .blocking_dir = blocking_dir,
4227 };
4228
4229 return border;
4230}
4231
4232static int
4233compare_lines_x(const void *a, const void *b)
4234{
4235 const struct border *border_a = a;
4236 const struct border *border_b = b;
4237
4238
4239 if (border_a->line.a.x == border_b->line.a.x)
4240 return border_a->line.b.x < border_b->line.b.x;
4241 else
4242 return border_a->line.a.x > border_b->line.a.x;
4243}
4244
4245static void
4246add_non_overlapping_edges(pixman_box32_t *boxes,
4247 int band_above_start,
4248 int band_below_start,
4249 int band_below_end,
4250 struct wl_array *borders)
4251{
4252 int i;
4253 struct wl_array band_merge;
4254 struct border *border;
4255 struct border *prev_border;
4256 struct border *new_border;
4257
4258 wl_array_init(&band_merge);
4259
4260 /* Add bottom band of previous row, and top band of current row, and
4261 * sort them so lower left x coordinate comes first. If there are two
4262 * borders with the same left x coordinate, the wider one comes first.
4263 */
4264 for (i = band_above_start; i < band_below_start; i++) {
4265 pixman_box32_t *box = &boxes[i];
4266 add_border(&band_merge, box->x1, box->y2, box->x2, box->y2,
4267 MOTION_DIRECTION_POSITIVE_Y);
4268 }
4269 for (i = band_below_start; i < band_below_end; i++) {
4270 pixman_box32_t *box= &boxes[i];
4271 add_border(&band_merge, box->x1, box->y1, box->x2, box->y1,
4272 MOTION_DIRECTION_NEGATIVE_Y);
4273 }
4274 qsort(band_merge.data,
4275 band_merge.size / sizeof *border,
4276 sizeof *border,
4277 compare_lines_x);
4278
4279 /* Combine the two combined bands so that any overlapping border is
4280 * eliminated. */
4281 prev_border = NULL;
4282 wl_array_for_each(border, &band_merge) {
4283 assert(border->line.a.y == border->line.b.y);
4284 assert(!prev_border ||
4285 prev_border->line.a.y == border->line.a.y);
4286 assert(!prev_border ||
4287 (prev_border->line.a.x != border->line.a.x ||
4288 prev_border->line.b.x != border->line.b.x));
4289 assert(!prev_border ||
4290 prev_border->line.a.x <= border->line.a.x);
4291
4292 if (prev_border &&
4293 prev_border->line.a.x == border->line.a.x) {
4294 /*
4295 * ------------ +
4296 * ------- =
4297 * [ ]-----
4298 */
4299 prev_border->line.a.x = border->line.b.x;
4300 } else if (prev_border &&
4301 prev_border->line.b.x == border->line.b.x) {
4302 /*
4303 * ------------ +
4304 * ------ =
4305 * ------[ ]
4306 */
4307 prev_border->line.b.x = border->line.a.x;
4308 } else if (prev_border &&
4309 prev_border->line.b.x == border->line.a.x) {
4310 /*
4311 * -------- +
4312 * ------ =
4313 * --------------
4314 */
4315 prev_border->line.b.x = border->line.b.x;
4316 } else if (prev_border &&
4317 prev_border->line.b.x >= border->line.a.x) {
4318 /*
4319 * --------------- +
4320 * ------ =
4321 * -----[ ]----
4322 */
4323 new_border = add_border(borders,
4324 border->line.b.x,
4325 border->line.b.y,
4326 prev_border->line.b.x,
4327 prev_border->line.b.y,
4328 prev_border->blocking_dir);
4329 prev_border->line.b.x = border->line.a.x;
4330 prev_border = new_border;
4331 } else {
4332 assert(!prev_border ||
4333 prev_border->line.b.x < border->line.a.x);
4334 /*
4335 * First border or non-overlapping.
4336 *
4337 * ----- +
4338 * ----- =
4339 * ----- -----
4340 */
4341 new_border = wl_array_add(borders, sizeof *border);
4342 *new_border = *border;
4343 prev_border = new_border;
4344 }
4345 }
4346
4347 wl_array_release(&band_merge);
4348}
4349
4350static void
4351add_band_bottom_edges(pixman_box32_t *boxes,
4352 int band_start,
4353 int band_end,
4354 struct wl_array *borders)
4355{
4356 int i;
4357
4358 for (i = band_start; i < band_end; i++) {
4359 add_border(borders,
4360 boxes[i].x1, boxes[i].y2,
4361 boxes[i].x2, boxes[i].y2,
4362 MOTION_DIRECTION_POSITIVE_Y);
4363 }
4364}
4365
4366static void
4367region_to_outline(pixman_region32_t *region, struct wl_array *borders)
4368{
4369 pixman_box32_t *boxes;
4370 int num_boxes;
4371 int i;
4372 int top_most, bottom_most;
4373 int current_roof;
4374 int prev_top;
4375 int band_start, prev_band_start;
4376
4377 /*
4378 * Remove any overlapping lines from the set of rectangles. Note that
4379 * pixman regions are grouped as rows of rectangles, where rectangles
4380 * in one row never touch or overlap and are all of the same height.
4381 *
4382 * -------- --- -------- ---
4383 * | | | | | | | |
4384 * ----------====---- --- ----------- ----- ---
4385 * | | => | |
4386 * ----==========--------- ----- ----------
4387 * | | | |
4388 * ------------------- -------------------
4389 *
4390 */
4391
4392 boxes = pixman_region32_rectangles(region, &num_boxes);
4393 prev_top = 0;
4394 top_most = boxes[0].y1;
4395 current_roof = top_most;
4396 bottom_most = boxes[num_boxes - 1].y2;
4397 band_start = 0;
4398 prev_band_start = 0;
4399 for (i = 0; i < num_boxes; i++) {
4400 /* Detect if there is a vertical empty space, and add the lower
4401 * level of the previous band if so was the case. */
4402 if (i > 0 &&
4403 boxes[i].y1 != prev_top &&
4404 boxes[i].y1 != boxes[i - 1].y2) {
4405 current_roof = boxes[i].y1;
4406 add_band_bottom_edges(boxes,
4407 band_start,
4408 i,
4409 borders);
4410 }
4411
4412 /* Special case adding the last band, since it won't be handled
4413 * by the band change detection below. */
4414 if (boxes[i].y1 != current_roof && i == num_boxes - 1) {
4415 if (boxes[i].y1 != prev_top) {
4416 /* The last band is a single box, so we don't
4417 * have a prev_band_start to tell us when the
4418 * previous band started. */
4419 add_non_overlapping_edges(boxes,
4420 band_start,
4421 i,
4422 i + 1,
4423 borders);
4424 } else {
4425 add_non_overlapping_edges(boxes,
4426 prev_band_start,
4427 band_start,
4428 i + 1,
4429 borders);
4430 }
4431 }
4432
4433 /* Detect when passing a band and combine the top border of the
4434 * just passed band with the bottom band of the previous band.
4435 */
4436 if (boxes[i].y1 != top_most && boxes[i].y1 != prev_top) {
4437 /* Combine the two passed bands. */
4438 if (prev_top != current_roof) {
4439 add_non_overlapping_edges(boxes,
4440 prev_band_start,
4441 band_start,
4442 i,
4443 borders);
4444 }
4445
4446 prev_band_start = band_start;
4447 band_start = i;
4448 }
4449
4450 /* Add the top border if the box is part of the current roof. */
4451 if (boxes[i].y1 == current_roof) {
4452 add_border(borders,
4453 boxes[i].x1, boxes[i].y1,
4454 boxes[i].x2, boxes[i].y1,
4455 MOTION_DIRECTION_NEGATIVE_Y);
4456 }
4457
4458 /* Add the bottom border of the last band. */
4459 if (boxes[i].y2 == bottom_most) {
4460 add_border(borders,
4461 boxes[i].x1, boxes[i].y2,
4462 boxes[i].x2, boxes[i].y2,
4463 MOTION_DIRECTION_POSITIVE_Y);
4464 }
4465
4466 /* Always add the left border. */
4467 add_border(borders,
4468 boxes[i].x1, boxes[i].y1,
4469 boxes[i].x1, boxes[i].y2,
4470 MOTION_DIRECTION_NEGATIVE_X);
4471
4472 /* Always add the right border. */
4473 add_border(borders,
4474 boxes[i].x2, boxes[i].y1,
4475 boxes[i].x2, boxes[i].y2,
4476 MOTION_DIRECTION_POSITIVE_X);
4477
4478 prev_top = boxes[i].y1;
4479 }
4480}
4481
4482static bool
4483is_border_horizontal (struct border *border)
4484{
4485 return border->line.a.y == border->line.b.y;
4486}
4487
4488static bool
4489is_border_blocking_directions(struct border *border,
4490 uint32_t directions)
4491{
4492 /* Don't block parallel motions. */
4493 if (is_border_horizontal(border)) {
4494 if ((directions & (MOTION_DIRECTION_POSITIVE_Y |
4495 MOTION_DIRECTION_NEGATIVE_Y)) == 0)
4496 return false;
4497 } else {
4498 if ((directions & (MOTION_DIRECTION_POSITIVE_X |
4499 MOTION_DIRECTION_NEGATIVE_X)) == 0)
4500 return false;
4501 }
4502
4503 return (~border->blocking_dir & directions) != directions;
4504}
4505
4506static struct border *
4507get_closest_border(struct wl_array *borders,
4508 struct line *motion,
4509 uint32_t directions)
4510{
4511 struct border *border;
4512 struct vec2d intersection;
4513 struct vec2d delta;
4514 double distance_2;
4515 struct border *closest_border = NULL;
4516 double closest_distance_2 = DBL_MAX;
4517
4518 wl_array_for_each(border, borders) {
4519 if (!is_border_blocking_directions(border, directions))
4520 continue;
4521
4522 if (!lines_intersect(&border->line, motion, &intersection))
4523 continue;
4524
4525 delta = vec2d_subtract(intersection, motion->a);
4526 distance_2 = delta.x*delta.x + delta.y*delta.y;
4527 if (distance_2 < closest_distance_2) {
4528 closest_border = border;
4529 closest_distance_2 = distance_2;
4530 }
4531 }
4532
4533 return closest_border;
4534}
4535
4536static void
4537clamp_to_border(struct border *border,
4538 struct line *motion,
4539 uint32_t *motion_dir)
4540{
4541 /*
4542 * When clamping either rightward or downward motions, the motion needs
4543 * to be clamped so that the destination coordinate does not end up on
4544 * the border (see weston_pointer_clamp_event_to_region). Do this by
4545 * clamping such motions to the border minus the smallest possible
4546 * wl_fixed_t value.
4547 */
4548 if (is_border_horizontal(border)) {
4549 if (*motion_dir & MOTION_DIRECTION_POSITIVE_Y)
4550 motion->b.y = border->line.a.y - wl_fixed_to_double(1);
4551 else
4552 motion->b.y = border->line.a.y;
4553 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_Y |
4554 MOTION_DIRECTION_NEGATIVE_Y);
4555 } else {
4556 if (*motion_dir & MOTION_DIRECTION_POSITIVE_X)
4557 motion->b.x = border->line.a.x - wl_fixed_to_double(1);
4558 else
4559 motion->b.x = border->line.a.x;
4560 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_X |
4561 MOTION_DIRECTION_NEGATIVE_X);
4562 }
4563}
4564
4565static uint32_t
4566get_motion_directions(struct line *motion)
4567{
4568 uint32_t directions = 0;
4569
4570 if (motion->a.x < motion->b.x)
4571 directions |= MOTION_DIRECTION_POSITIVE_X;
4572 else if (motion->a.x > motion->b.x)
4573 directions |= MOTION_DIRECTION_NEGATIVE_X;
4574 if (motion->a.y < motion->b.y)
4575 directions |= MOTION_DIRECTION_POSITIVE_Y;
4576 else if (motion->a.y > motion->b.y)
4577 directions |= MOTION_DIRECTION_NEGATIVE_Y;
4578
4579 return directions;
4580}
4581
Jonas Ådahld3414f22016-07-22 17:56:31 +08004582static void
4583weston_pointer_clamp_event_to_region(struct weston_pointer *pointer,
4584 struct weston_pointer_motion_event *event,
4585 pixman_region32_t *region,
4586 wl_fixed_t *clamped_x,
4587 wl_fixed_t *clamped_y)
4588{
4589 wl_fixed_t x, y;
4590 wl_fixed_t sx, sy;
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004591 wl_fixed_t old_sx = pointer->sx;
4592 wl_fixed_t old_sy = pointer->sy;
4593 struct wl_array borders;
4594 struct line motion;
4595 struct border *closest_border;
4596 float new_x_f, new_y_f;
4597 uint32_t directions;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004598
4599 weston_pointer_motion_to_abs(pointer, event, &x, &y);
4600 weston_view_from_global_fixed(pointer->focus, x, y, &sx, &sy);
4601
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004602 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004603
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004604 /*
4605 * Generate borders given the confine region we are to use. The borders
4606 * are defined to be the outer region of the allowed area. This means
4607 * top/left borders are "within" the allowed area, while bottom/right
4608 * borders are outside. This needs to be considered when clamping
4609 * confined motion vectors.
4610 */
4611 region_to_outline(region, &borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004612
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004613 motion = (struct line) {
4614 .a = (struct vec2d) {
4615 .x = wl_fixed_to_double(old_sx),
4616 .y = wl_fixed_to_double(old_sy),
4617 },
4618 .b = (struct vec2d) {
4619 .x = wl_fixed_to_double(sx),
4620 .y = wl_fixed_to_double(sy),
4621 },
4622 };
4623 directions = get_motion_directions(&motion);
4624
4625 while (directions) {
4626 closest_border = get_closest_border(&borders,
4627 &motion,
4628 directions);
4629 if (closest_border)
4630 clamp_to_border(closest_border, &motion, &directions);
4631 else
4632 break;
4633 }
4634
4635 weston_view_to_global_float(pointer->focus,
4636 (float) motion.b.x, (float) motion.b.y,
4637 &new_x_f, &new_y_f);
4638 *clamped_x = wl_fixed_from_double(new_x_f);
4639 *clamped_y = wl_fixed_from_double(new_y_f);
4640
4641 wl_array_release(&borders);
4642}
4643
4644static double
4645point_to_border_distance_2(struct border *border, double x, double y)
4646{
4647 double orig_x, orig_y;
4648 double dx, dy;
4649
4650 if (is_border_horizontal(border)) {
4651 if (x < border->line.a.x)
4652 orig_x = border->line.a.x;
4653 else if (x > border->line.b.x)
4654 orig_x = border->line.b.x;
4655 else
4656 orig_x = x;
4657 orig_y = border->line.a.y;
4658 } else {
4659 if (y < border->line.a.y)
4660 orig_y = border->line.a.y;
4661 else if (y > border->line.b.y)
4662 orig_y = border->line.b.y;
4663 else
4664 orig_y = y;
4665 orig_x = border->line.a.x;
4666 }
4667
4668
4669 dx = fabs(orig_x - x);
4670 dy = fabs(orig_y - y);
4671 return dx*dx + dy*dy;
4672}
4673
4674static void
4675warp_to_behind_border(struct border *border, wl_fixed_t *sx, wl_fixed_t *sy)
4676{
4677 switch (border->blocking_dir) {
4678 case MOTION_DIRECTION_POSITIVE_X:
4679 case MOTION_DIRECTION_NEGATIVE_X:
4680 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_X)
4681 *sx = wl_fixed_from_double(border->line.a.x) - 1;
4682 else
4683 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4684 if (*sy < wl_fixed_from_double(border->line.a.y))
4685 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4686 else if (*sy > wl_fixed_from_double(border->line.b.y))
4687 *sy = wl_fixed_from_double(border->line.b.y) - 1;
4688 break;
4689 case MOTION_DIRECTION_POSITIVE_Y:
4690 case MOTION_DIRECTION_NEGATIVE_Y:
4691 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_Y)
4692 *sy = wl_fixed_from_double(border->line.a.y) - 1;
4693 else
4694 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4695 if (*sx < wl_fixed_from_double(border->line.a.x))
4696 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4697 else if (*sx > wl_fixed_from_double(border->line.b.x))
4698 *sx = wl_fixed_from_double(border->line.b.x) - 1;
4699 break;
4700 }
Jonas Ådahld3414f22016-07-22 17:56:31 +08004701}
4702
4703static void
4704maybe_warp_confined_pointer(struct weston_pointer_constraint *constraint)
4705{
4706 wl_fixed_t x;
4707 wl_fixed_t y;
4708 wl_fixed_t sx;
4709 wl_fixed_t sy;
4710
4711 weston_view_from_global_fixed(constraint->view,
4712 constraint->pointer->x,
4713 constraint->pointer->y,
4714 &sx,
4715 &sy);
4716
4717 if (!is_within_constraint_region(constraint, sx, sy)) {
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004718 double xf = wl_fixed_to_double(sx);
4719 double yf = wl_fixed_to_double(sy);
4720 pixman_region32_t confine_region;
4721 struct wl_array borders;
4722 struct border *border;
4723 double closest_distance_2 = DBL_MAX;
4724 struct border *closest_border = NULL;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004725
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004726 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004727
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004728 pixman_region32_init(&confine_region);
4729 pixman_region32_intersect(&confine_region,
4730 &constraint->view->surface->input,
4731 &constraint->region);
4732 region_to_outline(&confine_region, &borders);
4733 pixman_region32_fini(&confine_region);
4734
4735 wl_array_for_each(border, &borders) {
4736 double distance_2;
4737
4738 distance_2 = point_to_border_distance_2(border, xf, yf);
4739 if (distance_2 < closest_distance_2) {
4740 closest_border = border;
4741 closest_distance_2 = distance_2;
4742 }
4743 }
4744 assert(closest_border);
4745
4746 warp_to_behind_border(closest_border, &sx, &sy);
4747
4748 wl_array_release(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004749
4750 weston_view_to_global_fixed(constraint->view, sx, sy, &x, &y);
4751 weston_pointer_move_to(constraint->pointer, x, y);
4752 }
4753}
4754
4755static void
4756confined_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02004757 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004758 struct weston_pointer_motion_event *event)
4759{
4760 struct weston_pointer_constraint *constraint =
4761 container_of(grab, struct weston_pointer_constraint, grab);
4762 struct weston_pointer *pointer = grab->pointer;
4763 struct weston_surface *surface;
4764 wl_fixed_t x, y;
4765 wl_fixed_t old_sx = pointer->sx;
4766 wl_fixed_t old_sy = pointer->sy;
4767 pixman_region32_t confine_region;
4768
4769 assert(pointer->focus);
4770 assert(pointer->focus->surface == constraint->surface);
4771
4772 surface = pointer->focus->surface;
4773
4774 pixman_region32_init(&confine_region);
4775 pixman_region32_intersect(&confine_region,
4776 &surface->input,
4777 &constraint->region);
4778 weston_pointer_clamp_event_to_region(pointer, event,
4779 &confine_region, &x, &y);
4780 weston_pointer_move_to(pointer, x, y);
4781 pixman_region32_fini(&confine_region);
4782
4783 weston_view_from_global_fixed(pointer->focus, x, y,
4784 &pointer->sx, &pointer->sy);
4785
4786 if (old_sx != pointer->sx || old_sy != pointer->sy) {
Quentin Glidiccde13452016-08-12 10:41:32 +02004787 pointer_send_motion(pointer, time,
4788 pointer->sx, pointer->sy);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004789 }
4790
Quentin Glidiccde13452016-08-12 10:41:32 +02004791 pointer_send_relative_motion(pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004792}
4793
4794static void
4795confined_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02004796 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004797 uint32_t button,
4798 uint32_t state_w)
4799{
4800 weston_pointer_send_button(grab->pointer, time, button, state_w);
4801}
4802
4803static void
4804confined_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02004805 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004806 struct weston_pointer_axis_event *event)
4807{
4808 weston_pointer_send_axis(grab->pointer, time, event);
4809}
4810
4811static void
4812confined_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
4813 uint32_t source)
4814{
4815 weston_pointer_send_axis_source(grab->pointer, source);
4816}
4817
4818static void
4819confined_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
4820{
4821 weston_pointer_send_frame(grab->pointer);
4822}
4823
4824static void
4825confined_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
4826{
4827 struct weston_pointer_constraint *constraint =
4828 container_of(grab, struct weston_pointer_constraint, grab);
4829
4830 disable_pointer_constraint(constraint);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004831}
4832
4833static const struct weston_pointer_grab_interface
4834 confined_pointer_grab_interface = {
4835 confined_pointer_grab_pointer_focus,
4836 confined_pointer_grab_pointer_motion,
4837 confined_pointer_grab_pointer_button,
4838 confined_pointer_grab_pointer_axis,
4839 confined_pointer_grab_pointer_axis_source,
4840 confined_pointer_grab_pointer_frame,
4841 confined_pointer_grab_pointer_cancel,
4842};
4843
4844static void
4845confined_pointer_destroy(struct wl_client *client,
4846 struct wl_resource *resource)
4847{
4848 wl_resource_destroy(resource);
4849}
4850
4851static void
4852confined_pointer_set_region(struct wl_client *client,
4853 struct wl_resource *resource,
4854 struct wl_resource *region_resource)
4855{
4856 struct weston_pointer_constraint *constraint =
4857 wl_resource_get_user_data(resource);
4858 struct weston_region *region = region_resource ?
4859 wl_resource_get_user_data(region_resource) : NULL;
4860
4861 if (!constraint)
4862 return;
4863
4864 if (region) {
4865 pixman_region32_copy(&constraint->region_pending,
4866 &region->region);
4867 } else {
4868 pixman_region32_fini(&constraint->region_pending);
4869 region_init_infinite(&constraint->region_pending);
4870 }
4871 constraint->region_is_pending = true;
4872}
4873
4874static const struct zwp_confined_pointer_v1_interface confined_pointer_interface = {
4875 confined_pointer_destroy,
4876 confined_pointer_set_region,
4877};
4878
4879static void
4880pointer_constraints_confine_pointer(struct wl_client *client,
4881 struct wl_resource *resource,
4882 uint32_t id,
4883 struct wl_resource *surface_resource,
4884 struct wl_resource *pointer_resource,
4885 struct wl_resource *region_resource,
4886 uint32_t lifetime)
4887{
4888 struct weston_surface *surface =
4889 wl_resource_get_user_data(surface_resource);
4890 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
4891 struct weston_region *region = region_resource ?
4892 wl_resource_get_user_data(region_resource) : NULL;
4893
4894 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
4895 &zwp_confined_pointer_v1_interface,
4896 &confined_pointer_interface,
4897 &confined_pointer_grab_interface);
4898}
4899
4900static const struct zwp_pointer_constraints_v1_interface pointer_constraints_interface = {
4901 pointer_constraints_destroy,
4902 pointer_constraints_lock_pointer,
4903 pointer_constraints_confine_pointer,
4904};
4905
4906static void
4907bind_pointer_constraints(struct wl_client *client, void *data,
4908 uint32_t version, uint32_t id)
4909{
4910 struct wl_resource *resource;
4911
4912 resource = wl_resource_create(client,
4913 &zwp_pointer_constraints_v1_interface,
4914 1, id);
4915
4916 wl_resource_set_implementation(resource, &pointer_constraints_interface,
4917 NULL, NULL);
4918}
4919
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004920static void
Alexandros Frantzis2b442482018-02-20 14:05:50 +02004921input_timestamps_destroy(struct wl_client *client,
4922 struct wl_resource *resource)
4923{
4924 wl_resource_destroy(resource);
4925}
4926
4927static const struct zwp_input_timestamps_v1_interface
4928 input_timestamps_interface = {
4929 input_timestamps_destroy,
4930};
4931
4932static void
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004933input_timestamps_manager_destroy(struct wl_client *client,
4934 struct wl_resource *resource)
4935{
4936 wl_resource_destroy(resource);
4937}
4938
4939static void
4940input_timestamps_manager_get_keyboard_timestamps(struct wl_client *client,
4941 struct wl_resource *resource,
4942 uint32_t id,
4943 struct wl_resource *keyboard_resource)
4944{
Alexandros Frantzis2b442482018-02-20 14:05:50 +02004945 struct weston_keyboard *keyboard =
4946 wl_resource_get_user_data(keyboard_resource);
4947 struct wl_resource *input_ts;
4948
4949 input_ts = wl_resource_create(client,
4950 &zwp_input_timestamps_v1_interface,
4951 1, id);
4952 if (!input_ts) {
4953 wl_client_post_no_memory(client);
4954 return;
4955 }
4956
4957 if (keyboard) {
4958 wl_list_insert(&keyboard->timestamps_list,
4959 wl_resource_get_link(input_ts));
4960 } else {
4961 wl_list_init(wl_resource_get_link(input_ts));
4962 }
4963
4964 wl_resource_set_implementation(input_ts,
4965 &input_timestamps_interface,
4966 keyboard_resource,
4967 unbind_resource);
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004968}
4969
4970static void
4971input_timestamps_manager_get_pointer_timestamps(struct wl_client *client,
4972 struct wl_resource *resource,
4973 uint32_t id,
4974 struct wl_resource *pointer_resource)
4975{
Alexandros Frantzisdb907b72018-02-20 14:06:26 +02004976 struct weston_pointer *pointer =
4977 wl_resource_get_user_data(pointer_resource);
4978 struct wl_resource *input_ts;
4979
4980 input_ts = wl_resource_create(client,
4981 &zwp_input_timestamps_v1_interface,
4982 1, id);
4983 if (!input_ts) {
4984 wl_client_post_no_memory(client);
4985 return;
4986 }
4987
4988 if (pointer) {
4989 wl_list_insert(&pointer->timestamps_list,
4990 wl_resource_get_link(input_ts));
4991 } else {
4992 wl_list_init(wl_resource_get_link(input_ts));
4993 }
4994
4995 wl_resource_set_implementation(input_ts,
4996 &input_timestamps_interface,
4997 pointer_resource,
4998 unbind_resource);
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004999}
5000
5001static void
5002input_timestamps_manager_get_touch_timestamps(struct wl_client *client,
5003 struct wl_resource *resource,
5004 uint32_t id,
5005 struct wl_resource *touch_resource)
5006{
Alexandros Frantzisd7157842018-02-20 14:07:03 +02005007 struct weston_touch *touch = wl_resource_get_user_data(touch_resource);
5008 struct wl_resource *input_ts;
5009
5010 input_ts = wl_resource_create(client,
5011 &zwp_input_timestamps_v1_interface,
5012 1, id);
5013 if (!input_ts) {
5014 wl_client_post_no_memory(client);
5015 return;
5016 }
5017
5018 if (touch) {
5019 wl_list_insert(&touch->timestamps_list,
5020 wl_resource_get_link(input_ts));
5021 } else {
5022 wl_list_init(wl_resource_get_link(input_ts));
5023 }
5024
5025 wl_resource_set_implementation(input_ts,
5026 &input_timestamps_interface,
5027 touch_resource,
5028 unbind_resource);
Alexandros Frantzis538749d2018-02-16 18:44:16 +02005029}
5030
5031static const struct zwp_input_timestamps_manager_v1_interface
5032 input_timestamps_manager_interface = {
5033 input_timestamps_manager_destroy,
5034 input_timestamps_manager_get_keyboard_timestamps,
5035 input_timestamps_manager_get_pointer_timestamps,
5036 input_timestamps_manager_get_touch_timestamps,
5037};
5038
5039static void
5040bind_input_timestamps_manager(struct wl_client *client, void *data,
5041 uint32_t version, uint32_t id)
5042{
5043 struct wl_resource *resource =
5044 wl_resource_create(client,
5045 &zwp_input_timestamps_manager_v1_interface,
5046 1, id);
5047
5048 if (resource == NULL) {
5049 wl_client_post_no_memory(client);
5050 return;
5051 }
5052
5053 wl_resource_set_implementation(resource,
5054 &input_timestamps_manager_interface,
5055 NULL, NULL);
5056}
5057
Jonas Ådahl30d61d82014-10-22 21:21:17 +02005058int
5059weston_input_init(struct weston_compositor *compositor)
5060{
5061 if (!wl_global_create(compositor->wl_display,
5062 &zwp_relative_pointer_manager_v1_interface, 1,
5063 compositor, bind_relative_pointer_manager))
5064 return -1;
5065
Jonas Ådahld3414f22016-07-22 17:56:31 +08005066 if (!wl_global_create(compositor->wl_display,
5067 &zwp_pointer_constraints_v1_interface, 1,
5068 NULL, bind_pointer_constraints))
5069 return -1;
5070
Alexandros Frantzis538749d2018-02-16 18:44:16 +02005071 if (!wl_global_create(compositor->wl_display,
5072 &zwp_input_timestamps_manager_v1_interface, 1,
5073 NULL, bind_input_timestamps_manager))
5074 return -1;
5075
Jonas Ådahl30d61d82014-10-22 21:21:17 +02005076 return 0;
5077}