blob: acb564e0ce1699083c88c213baaa7ab70fe89283 [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>
Kristian Høgsberg2158a882013-04-18 15:07:39 -040040
Jon Cruz35b2eaa2015-06-15 15:37:08 -070041#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070042#include "shared/os-compatibility.h"
Alexandros Frantzis84b31f82017-11-24 18:01:46 +020043#include "shared/timespec-util.h"
Kristian Høgsberg2158a882013-04-18 15:07:39 -040044#include "compositor.h"
Daniel Stone7dbb0e12016-11-24 15:30:41 +000045#include "relative-pointer-unstable-v1-server-protocol.h"
46#include "pointer-constraints-unstable-v1-server-protocol.h"
Alexandros Frantzis538749d2018-02-16 18:44:16 +020047#include "input-timestamps-unstable-v1-server-protocol.h"
Jonas Ådahld3414f22016-07-22 17:56:31 +080048
49enum pointer_constraint_type {
50 POINTER_CONSTRAINT_TYPE_LOCK,
51 POINTER_CONSTRAINT_TYPE_CONFINE,
52};
53
Jonas Ådahld0be2bb2015-04-30 17:56:37 +080054enum motion_direction {
55 MOTION_DIRECTION_POSITIVE_X = 1 << 0,
56 MOTION_DIRECTION_NEGATIVE_X = 1 << 1,
57 MOTION_DIRECTION_POSITIVE_Y = 1 << 2,
58 MOTION_DIRECTION_NEGATIVE_Y = 1 << 3,
59};
60
61struct vec2d {
62 double x, y;
63};
64
65struct line {
66 struct vec2d a;
67 struct vec2d b;
68};
69
70struct border {
71 struct line line;
72 enum motion_direction blocking_dir;
73};
74
Jonas Ådahld3414f22016-07-22 17:56:31 +080075static void
76maybe_warp_confined_pointer(struct weston_pointer_constraint *constraint);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040077
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040078static void
79empty_region(pixman_region32_t *region)
80{
81 pixman_region32_fini(region);
82 pixman_region32_init(region);
83}
84
Jonas Ådahld3414f22016-07-22 17:56:31 +080085static void
86region_init_infinite(pixman_region32_t *region)
87{
88 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
89 UINT32_MAX, UINT32_MAX);
90}
91
Alexandros Frantzis2b442482018-02-20 14:05:50 +020092static void
93send_timestamp(struct wl_resource *resource,
94 const struct timespec *time)
95{
96 uint32_t tv_sec_hi, tv_sec_lo, tv_nsec;
97
98 timespec_to_proto(time, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
99 zwp_input_timestamps_v1_send_timestamp(resource, tv_sec_hi, tv_sec_lo,
100 tv_nsec);
101}
102
103static void
104send_timestamps_for_input_resource(struct wl_resource *input_resource,
105 struct wl_list *list,
106 const struct timespec *time)
107{
108 struct wl_resource *resource;
109
110 wl_resource_for_each(resource, list) {
111 if (wl_resource_get_user_data(resource) == input_resource)
112 send_timestamp(resource, time);
113 }
114}
115
116static void
117remove_input_resource_from_timestamps(struct wl_resource *input_resource,
118 struct wl_list *list)
119{
120 struct wl_resource *resource;
121
122 wl_resource_for_each(resource, list) {
123 if (wl_resource_get_user_data(resource) == input_resource)
124 wl_resource_set_user_data(resource, NULL);
125 }
126}
127
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -0500128/** Register a touchscreen input device
129 *
130 * \param touch The parent weston_touch that identifies the seat.
131 * \param syspath Unique device name.
132 * \param backend_data Backend private data if necessary.
133 * \param ops Calibration operations, or NULL for not able to run calibration.
134 * \return New touch device, or NULL on failure.
135 */
136WL_EXPORT struct weston_touch_device *
137weston_touch_create_touch_device(struct weston_touch *touch,
138 const char *syspath,
139 void *backend_data,
140 const struct weston_touch_device_ops *ops)
141{
142 struct weston_touch_device *device;
143
144 assert(syspath);
145 if (ops) {
146 assert(ops->get_output);
147 assert(ops->get_calibration_head_name);
148 assert(ops->get_calibration);
149 assert(ops->set_calibration);
150 }
151
152 device = zalloc(sizeof *device);
153 if (!device)
154 return NULL;
155
156 wl_signal_init(&device->destroy_signal);
157
158 device->syspath = strdup(syspath);
159 if (!device->syspath) {
160 free(device);
161 return NULL;
162 }
163
164 device->backend_data = backend_data;
165 device->ops = ops;
166
167 device->aggregate = touch;
168 wl_list_insert(touch->device_list.prev, &device->link);
169
170 return device;
171}
172
173/** Destroy the touch device. */
174WL_EXPORT void
175weston_touch_device_destroy(struct weston_touch_device *device)
176{
177 wl_list_remove(&device->link);
178 wl_signal_emit(&device->destroy_signal, device);
179 free(device->syspath);
180 free(device);
181}
182
183/** Is it possible to run calibration on this touch device? */
184WL_EXPORT bool
185weston_touch_device_can_calibrate(struct weston_touch_device *device)
186{
187 return !!device->ops;
188}
189
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800190static struct weston_pointer_client *
191weston_pointer_client_create(struct wl_client *client)
192{
193 struct weston_pointer_client *pointer_client;
194
195 pointer_client = zalloc(sizeof *pointer_client);
196 if (!pointer_client)
197 return NULL;
198
199 pointer_client->client = client;
200 wl_list_init(&pointer_client->pointer_resources);
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200201 wl_list_init(&pointer_client->relative_pointer_resources);
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800202
203 return pointer_client;
204}
205
206static void
207weston_pointer_client_destroy(struct weston_pointer_client *pointer_client)
208{
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +0200209 struct wl_resource *resource;
210
211 wl_resource_for_each(resource, &pointer_client->pointer_resources) {
212 wl_resource_set_user_data(resource, NULL);
213 }
214
215 wl_resource_for_each(resource,
216 &pointer_client->relative_pointer_resources) {
217 wl_resource_set_user_data(resource, NULL);
218 }
219
220 wl_list_remove(&pointer_client->pointer_resources);
221 wl_list_remove(&pointer_client->relative_pointer_resources);
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800222 free(pointer_client);
223}
224
225static bool
226weston_pointer_client_is_empty(struct weston_pointer_client *pointer_client)
227{
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200228 return (wl_list_empty(&pointer_client->pointer_resources) &&
229 wl_list_empty(&pointer_client->relative_pointer_resources));
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800230}
231
232static struct weston_pointer_client *
233weston_pointer_get_pointer_client(struct weston_pointer *pointer,
234 struct wl_client *client)
235{
236 struct weston_pointer_client *pointer_client;
237
238 wl_list_for_each(pointer_client, &pointer->pointer_clients, link) {
239 if (pointer_client->client == client)
240 return pointer_client;
241 }
242
243 return NULL;
244}
245
246static struct weston_pointer_client *
247weston_pointer_ensure_pointer_client(struct weston_pointer *pointer,
248 struct wl_client *client)
249{
250 struct weston_pointer_client *pointer_client;
251
252 pointer_client = weston_pointer_get_pointer_client(pointer, client);
253 if (pointer_client)
254 return pointer_client;
255
256 pointer_client = weston_pointer_client_create(client);
257 wl_list_insert(&pointer->pointer_clients, &pointer_client->link);
258
259 if (pointer->focus &&
260 pointer->focus->surface->resource &&
261 wl_resource_get_client(pointer->focus->surface->resource) == client) {
262 pointer->focus_client = pointer_client;
263 }
264
265 return pointer_client;
266}
267
268static void
269weston_pointer_cleanup_pointer_client(struct weston_pointer *pointer,
270 struct weston_pointer_client *pointer_client)
271{
272 if (weston_pointer_client_is_empty(pointer_client)) {
273 if (pointer->focus_client == pointer_client)
274 pointer->focus_client = NULL;
275 wl_list_remove(&pointer_client->link);
276 weston_pointer_client_destroy(pointer_client);
277 }
278}
279
280static void
281unbind_pointer_client_resource(struct wl_resource *resource)
282{
283 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
284 struct wl_client *client = wl_resource_get_client(resource);
285 struct weston_pointer_client *pointer_client;
286
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800287 wl_list_remove(wl_resource_get_link(resource));
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +0200288
289 if (pointer) {
290 pointer_client = weston_pointer_get_pointer_client(pointer,
291 client);
292 assert(pointer_client);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200293 remove_input_resource_from_timestamps(resource,
294 &pointer->timestamps_list);
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +0200295 weston_pointer_cleanup_pointer_client(pointer, pointer_client);
296 }
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800297}
298
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400299static void unbind_resource(struct wl_resource *resource)
300{
Jason Ekstrand44a38632013-06-14 10:08:00 -0500301 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400302}
303
Jonas Ådahl3042ffe2013-10-17 23:04:08 +0200304WL_EXPORT void
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200305weston_pointer_motion_to_abs(struct weston_pointer *pointer,
306 struct weston_pointer_motion_event *event,
307 wl_fixed_t *x, wl_fixed_t *y)
308{
309 if (event->mask & WESTON_POINTER_MOTION_ABS) {
310 *x = wl_fixed_from_double(event->x);
311 *y = wl_fixed_from_double(event->y);
312 } else if (event->mask & WESTON_POINTER_MOTION_REL) {
313 *x = pointer->x + wl_fixed_from_double(event->dx);
314 *y = pointer->y + wl_fixed_from_double(event->dy);
315 } else {
316 assert(!"invalid motion event");
317 *x = *y = 0;
318 }
319}
320
321static bool
322weston_pointer_motion_to_rel(struct weston_pointer *pointer,
323 struct weston_pointer_motion_event *event,
324 double *dx, double *dy,
325 double *dx_unaccel, double *dy_unaccel)
326{
327 if (event->mask & WESTON_POINTER_MOTION_REL &&
328 event->mask & WESTON_POINTER_MOTION_REL_UNACCEL) {
329 *dx = event->dx;
330 *dy = event->dy;
331 *dx_unaccel = event->dx_unaccel;
332 *dy_unaccel = event->dy_unaccel;
333 return true;
334 } else if (event->mask & WESTON_POINTER_MOTION_REL) {
335 *dx_unaccel = *dx = event->dx;
336 *dy_unaccel = *dy = event->dy;
337 return true;
338 } else if (event->mask & WESTON_POINTER_MOTION_REL_UNACCEL) {
339 *dx_unaccel = *dx = event->dx_unaccel;
340 *dy_unaccel = *dy = event->dy_unaccel;
341 return true;
342 } else {
343 return false;
344 }
345}
346
347WL_EXPORT void
Kristian Høgsberga71e8b22013-05-06 21:51:21 -0400348weston_seat_repick(struct weston_seat *seat)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400349{
Derek Foreman1281a362015-07-31 16:55:32 -0500350 const struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400351
Derek Foreman1b786ee2015-06-03 15:53:23 -0500352 if (!pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400353 return;
354
Derek Foreman1b786ee2015-06-03 15:53:23 -0500355 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400356}
357
358static void
359weston_compositor_idle_inhibit(struct weston_compositor *compositor)
360{
361 weston_compositor_wake(compositor);
362 compositor->idle_inhibit++;
363}
364
365static void
366weston_compositor_idle_release(struct weston_compositor *compositor)
367{
368 compositor->idle_inhibit--;
369 weston_compositor_wake(compositor);
370}
371
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400372static void
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100373pointer_focus_view_destroyed(struct wl_listener *listener, void *data)
374{
375 struct weston_pointer *pointer =
376 container_of(listener, struct weston_pointer,
377 focus_view_listener);
378
Derek Foremanf9318d12015-05-11 15:40:11 -0500379 weston_pointer_clear_focus(pointer);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100380}
381
382static void
383pointer_focus_resource_destroyed(struct wl_listener *listener, void *data)
384{
385 struct weston_pointer *pointer =
386 container_of(listener, struct weston_pointer,
387 focus_resource_listener);
388
Derek Foremanf9318d12015-05-11 15:40:11 -0500389 weston_pointer_clear_focus(pointer);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100390}
391
392static void
393keyboard_focus_resource_destroyed(struct wl_listener *listener, void *data)
394{
395 struct weston_keyboard *keyboard =
396 container_of(listener, struct weston_keyboard,
397 focus_resource_listener);
398
399 weston_keyboard_set_focus(keyboard, NULL);
400}
401
402static void
403touch_focus_view_destroyed(struct wl_listener *listener, void *data)
404{
405 struct weston_touch *touch =
406 container_of(listener, struct weston_touch,
407 focus_view_listener);
408
Derek Foreman4c93c082015-04-30 16:45:41 -0500409 weston_touch_set_focus(touch, NULL);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100410}
411
412static void
413touch_focus_resource_destroyed(struct wl_listener *listener, void *data)
414{
415 struct weston_touch *touch =
416 container_of(listener, struct weston_touch,
417 focus_resource_listener);
418
Derek Foreman4c93c082015-04-30 16:45:41 -0500419 weston_touch_set_focus(touch, NULL);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100420}
421
422static void
Neil Roberts96d790e2013-09-19 17:32:00 +0100423move_resources(struct wl_list *destination, struct wl_list *source)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400424{
Neil Roberts96d790e2013-09-19 17:32:00 +0100425 wl_list_insert_list(destination, source);
426 wl_list_init(source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400427}
428
429static void
Neil Roberts96d790e2013-09-19 17:32:00 +0100430move_resources_for_client(struct wl_list *destination,
431 struct wl_list *source,
432 struct wl_client *client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400433{
Neil Roberts96d790e2013-09-19 17:32:00 +0100434 struct wl_resource *resource, *tmp;
435 wl_resource_for_each_safe(resource, tmp, source) {
436 if (wl_resource_get_client(resource) == client) {
437 wl_list_remove(wl_resource_get_link(resource));
438 wl_list_insert(destination,
439 wl_resource_get_link(resource));
440 }
441 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400442}
443
444static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700445default_grab_pointer_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400446{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400447 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500448 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400449 wl_fixed_t sx, sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400450
451 if (pointer->button_count > 0)
452 return;
453
Jason Ekstranda7af7042013-10-12 22:38:11 -0500454 view = weston_compositor_pick_view(pointer->seat->compositor,
455 pointer->x, pointer->y,
456 &sx, &sy);
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400457
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -0800458 if (pointer->focus != view || pointer->sx != sx || pointer->sy != sy)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500459 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400460}
461
462static void
Quentin Glidiccde13452016-08-12 10:41:32 +0200463pointer_send_relative_motion(struct weston_pointer *pointer,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200464 const struct timespec *time,
Quentin Glidiccde13452016-08-12 10:41:32 +0200465 struct weston_pointer_motion_event *event)
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200466{
467 uint64_t time_usec;
468 double dx, dy, dx_unaccel, dy_unaccel;
469 wl_fixed_t dxf, dyf, dxf_unaccel, dyf_unaccel;
470 struct wl_list *resource_list;
471 struct wl_resource *resource;
472
473 if (!pointer->focus_client)
474 return;
475
476 if (!weston_pointer_motion_to_rel(pointer, event,
477 &dx, &dy,
478 &dx_unaccel, &dy_unaccel))
479 return;
480
481 resource_list = &pointer->focus_client->relative_pointer_resources;
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200482 time_usec = timespec_to_usec(&event->time);
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200483 if (time_usec == 0)
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200484 time_usec = timespec_to_usec(time);
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200485
486 dxf = wl_fixed_from_double(dx);
487 dyf = wl_fixed_from_double(dy);
488 dxf_unaccel = wl_fixed_from_double(dx_unaccel);
489 dyf_unaccel = wl_fixed_from_double(dy_unaccel);
490
491 wl_resource_for_each(resource, resource_list) {
492 zwp_relative_pointer_v1_send_relative_motion(
493 resource,
494 (uint32_t) (time_usec >> 32),
495 (uint32_t) time_usec,
496 dxf, dyf,
497 dxf_unaccel, dyf_unaccel);
498 }
499}
500
501static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200502pointer_send_motion(struct weston_pointer *pointer,
503 const struct timespec *time,
Quentin Glidiccde13452016-08-12 10:41:32 +0200504 wl_fixed_t sx, wl_fixed_t sy)
Jonas Ådahlf44942e2016-07-22 17:54:55 +0800505{
506 struct wl_list *resource_list;
507 struct wl_resource *resource;
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200508 uint32_t msecs;
Jonas Ådahlf44942e2016-07-22 17:54:55 +0800509
510 if (!pointer->focus_client)
511 return;
512
513 resource_list = &pointer->focus_client->pointer_resources;
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200514 msecs = timespec_to_msec(time);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200515 wl_resource_for_each(resource, resource_list) {
516 send_timestamps_for_input_resource(resource,
517 &pointer->timestamps_list,
518 time);
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200519 wl_pointer_send_motion(resource, msecs, sx, sy);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200520 }
Jonas Ådahlf44942e2016-07-22 17:54:55 +0800521}
522
Quentin Glidiccde13452016-08-12 10:41:32 +0200523WL_EXPORT void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200524weston_pointer_send_motion(struct weston_pointer *pointer,
525 const struct timespec *time,
Quentin Glidiccde13452016-08-12 10:41:32 +0200526 struct weston_pointer_motion_event *event)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400527{
Jonas Ådahld2510102014-10-05 21:39:14 +0200528 wl_fixed_t x, y;
Jonas Ådahl8283c342015-04-24 15:26:17 +0800529 wl_fixed_t old_sx = pointer->sx;
530 wl_fixed_t old_sy = pointer->sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400531
Jonas Ådahld2510102014-10-05 21:39:14 +0200532 if (pointer->focus) {
533 weston_pointer_motion_to_abs(pointer, event, &x, &y);
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -0800534 weston_view_from_global_fixed(pointer->focus, x, y,
535 &pointer->sx, &pointer->sy);
Jonas Ådahld2510102014-10-05 21:39:14 +0200536 }
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -0800537
Jonas Ådahld2510102014-10-05 21:39:14 +0200538 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100539
Jonas Ådahlf44942e2016-07-22 17:54:55 +0800540 if (old_sx != pointer->sx || old_sy != pointer->sy) {
Quentin Glidiccde13452016-08-12 10:41:32 +0200541 pointer_send_motion(pointer, time,
542 pointer->sx, pointer->sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400543 }
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200544
Quentin Glidiccde13452016-08-12 10:41:32 +0200545 pointer_send_relative_motion(pointer, time, event);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400546}
547
548static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200549default_grab_pointer_motion(struct weston_pointer_grab *grab,
550 const struct timespec *time,
Quentin Glidiccde13452016-08-12 10:41:32 +0200551 struct weston_pointer_motion_event *event)
552{
553 weston_pointer_send_motion(grab->pointer, time, event);
554}
555
556/** Check if the pointer has focused resources.
557 *
558 * \param pointer The pointer to check for focused resources.
559 * \return Whether or not this pointer has focused resources
560 */
561WL_EXPORT bool
562weston_pointer_has_focus_resource(struct weston_pointer *pointer)
563{
564 if (!pointer->focus_client)
565 return false;
566
567 if (wl_list_empty(&pointer->focus_client->pointer_resources))
568 return false;
569
570 return true;
571}
572
573/** Send wl_pointer.button events to focused resources.
574 *
575 * \param pointer The pointer where the button events originates from.
576 * \param time The timestamp of the event
577 * \param button The button value of the event
578 * \param value The state enum value of the event
579 *
580 * For every resource that is currently in focus, send a wl_pointer.button event
581 * with the passed parameters. The focused resources are the wl_pointer
582 * resources of the client which currently has the surface with pointer focus.
583 */
584WL_EXPORT void
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800585weston_pointer_send_button(struct weston_pointer *pointer,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200586 const struct timespec *time, uint32_t button,
Quentin Glidiccde13452016-08-12 10:41:32 +0200587 enum wl_pointer_button_state state)
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800588{
589 struct wl_display *display = pointer->seat->compositor->wl_display;
590 struct wl_list *resource_list;
591 struct wl_resource *resource;
592 uint32_t serial;
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200593 uint32_t msecs;
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800594
Quentin Glidiccde13452016-08-12 10:41:32 +0200595 if (!weston_pointer_has_focus_resource(pointer))
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800596 return;
597
598 resource_list = &pointer->focus_client->pointer_resources;
Quentin Glidiccde13452016-08-12 10:41:32 +0200599 serial = wl_display_next_serial(display);
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200600 msecs = timespec_to_msec(time);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200601 wl_resource_for_each(resource, resource_list) {
602 send_timestamps_for_input_resource(resource,
603 &pointer->timestamps_list,
604 time);
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200605 wl_pointer_send_button(resource, serial, msecs, button, state);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200606 }
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800607}
608
609static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700610default_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200611 const struct timespec *time, uint32_t button,
Quentin Glidiccde13452016-08-12 10:41:32 +0200612 enum wl_pointer_button_state state)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400613{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400614 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400615 struct weston_compositor *compositor = pointer->seat->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500616 struct weston_view *view;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400617 wl_fixed_t sx, sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400618
Quentin Glidiccde13452016-08-12 10:41:32 +0200619 weston_pointer_send_button(pointer, time, button, state);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400620
621 if (pointer->button_count == 0 &&
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400622 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500623 view = weston_compositor_pick_view(compositor,
624 pointer->x, pointer->y,
625 &sx, &sy);
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400626
Jason Ekstranda7af7042013-10-12 22:38:11 -0500627 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400628 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400629}
630
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200631/** Send wl_pointer.axis events to focused resources.
632 *
633 * \param pointer The pointer where the axis events originates from.
634 * \param time The timestamp of the event
635 * \param axis The axis enum value of the event
636 * \param value The axis value of the event
637 *
638 * For every resource that is currently in focus, send a wl_pointer.axis event
639 * with the passed parameters. The focused resources are the wl_pointer
640 * resources of the client which currently has the surface with pointer focus.
641 */
642WL_EXPORT void
643weston_pointer_send_axis(struct weston_pointer *pointer,
Alexandros Frantzis80321942017-11-16 18:20:56 +0200644 const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +1000645 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200646{
647 struct wl_resource *resource;
648 struct wl_list *resource_list;
Alexandros Frantzis80321942017-11-16 18:20:56 +0200649 uint32_t msecs;
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200650
Quentin Glidiccde13452016-08-12 10:41:32 +0200651 if (!weston_pointer_has_focus_resource(pointer))
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800652 return;
653
654 resource_list = &pointer->focus_client->pointer_resources;
Alexandros Frantzis80321942017-11-16 18:20:56 +0200655 msecs = timespec_to_msec(time);
Peter Hutterer87743e92016-01-18 16:38:22 +1000656 wl_resource_for_each(resource, resource_list) {
657 if (event->has_discrete &&
658 wl_resource_get_version(resource) >=
659 WL_POINTER_AXIS_DISCRETE_SINCE_VERSION)
660 wl_pointer_send_axis_discrete(resource, event->axis,
661 event->discrete);
662
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200663 if (event->value) {
664 send_timestamps_for_input_resource(resource,
665 &pointer->timestamps_list,
666 time);
Alexandros Frantzis80321942017-11-16 18:20:56 +0200667 wl_pointer_send_axis(resource, msecs,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200668 event->axis,
669 wl_fixed_from_double(event->value));
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200670 } else if (wl_resource_get_version(resource) >=
671 WL_POINTER_AXIS_STOP_SINCE_VERSION) {
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_stop(resource, msecs,
Peter Hutterer87743e92016-01-18 16:38:22 +1000676 event->axis);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200677 }
Peter Hutterer87743e92016-01-18 16:38:22 +1000678 }
679}
680
Quentin Glidiccde13452016-08-12 10:41:32 +0200681/** Send wl_pointer.axis_source events to focused resources.
682 *
683 * \param pointer The pointer where the axis_source events originates from.
684 * \param source The axis_source enum value of the event
685 *
686 * For every resource that is currently in focus, send a wl_pointer.axis_source
687 * event with the passed parameter. The focused resources are the wl_pointer
688 * resources of the client which currently has the surface with pointer focus.
689 */
Peter Hutterer87743e92016-01-18 16:38:22 +1000690WL_EXPORT void
Quentin Glidiccde13452016-08-12 10:41:32 +0200691weston_pointer_send_axis_source(struct weston_pointer *pointer,
692 enum wl_pointer_axis_source source)
Peter Hutterer87743e92016-01-18 16:38:22 +1000693{
694 struct wl_resource *resource;
695 struct wl_list *resource_list;
696
Quentin Glidiccde13452016-08-12 10:41:32 +0200697 if (!weston_pointer_has_focus_resource(pointer))
Jonas Ådahled6014a2016-04-21 10:21:48 +0800698 return;
699
Peter Hutterer87743e92016-01-18 16:38:22 +1000700 resource_list = &pointer->focus_client->pointer_resources;
701 wl_resource_for_each(resource, resource_list) {
702 if (wl_resource_get_version(resource) >=
703 WL_POINTER_AXIS_SOURCE_SINCE_VERSION) {
704 wl_pointer_send_axis_source(resource, source);
705 }
706 }
707}
708
709static void
710pointer_send_frame(struct wl_resource *resource)
711{
712 if (wl_resource_get_version(resource) >=
713 WL_POINTER_FRAME_SINCE_VERSION) {
714 wl_pointer_send_frame(resource);
715 }
716}
717
Quentin Glidiccde13452016-08-12 10:41:32 +0200718/** Send wl_pointer.frame events to focused resources.
719 *
720 * \param pointer The pointer where the frame events originates from.
721 *
722 * For every resource that is currently in focus, send a wl_pointer.frame event.
723 * The focused resources are the wl_pointer resources of the client which
724 * currently has the surface with pointer focus.
725 */
Peter Hutterer87743e92016-01-18 16:38:22 +1000726WL_EXPORT void
727weston_pointer_send_frame(struct weston_pointer *pointer)
728{
729 struct wl_resource *resource;
730 struct wl_list *resource_list;
731
Quentin Glidiccde13452016-08-12 10:41:32 +0200732 if (!weston_pointer_has_focus_resource(pointer))
Derek Foreman8efa31b2016-01-29 10:29:46 -0600733 return;
734
Peter Hutterer87743e92016-01-18 16:38:22 +1000735 resource_list = &pointer->focus_client->pointer_resources;
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200736 wl_resource_for_each(resource, resource_list)
Peter Hutterer87743e92016-01-18 16:38:22 +1000737 pointer_send_frame(resource);
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200738}
739
740static void
741default_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +0200742 const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +1000743 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200744{
Peter Hutterer89b6a492016-01-18 15:58:17 +1000745 weston_pointer_send_axis(grab->pointer, time, event);
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200746}
747
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200748static void
Peter Hutterer87743e92016-01-18 16:38:22 +1000749default_grab_pointer_axis_source(struct weston_pointer_grab *grab,
Quentin Glidiccde13452016-08-12 10:41:32 +0200750 enum wl_pointer_axis_source source)
Peter Hutterer87743e92016-01-18 16:38:22 +1000751{
752 weston_pointer_send_axis_source(grab->pointer, source);
753}
754
755static void
756default_grab_pointer_frame(struct weston_pointer_grab *grab)
757{
758 weston_pointer_send_frame(grab->pointer);
759}
760
761static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200762default_grab_pointer_cancel(struct weston_pointer_grab *grab)
763{
764}
765
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400766static const struct weston_pointer_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400767 default_pointer_grab_interface = {
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700768 default_grab_pointer_focus,
769 default_grab_pointer_motion,
770 default_grab_pointer_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200771 default_grab_pointer_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +1000772 default_grab_pointer_axis_source,
773 default_grab_pointer_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200774 default_grab_pointer_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400775};
776
Quentin Glidiccde13452016-08-12 10:41:32 +0200777/** Check if the touch has focused resources.
778 *
779 * \param touch The touch to check for focused resources.
780 * \return Whether or not this touch has focused resources
781 */
782WL_EXPORT bool
783weston_touch_has_focus_resource(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400784{
Quentin Glidiccde13452016-08-12 10:41:32 +0200785 if (!touch->focus)
786 return false;
787
788 if (wl_list_empty(&touch->focus_resource_list))
789 return false;
790
791 return true;
792}
793
794/** Send wl_touch.down events to focused resources.
795 *
796 * \param touch The touch where the down events originates from.
797 * \param time The timestamp of the event
798 * \param touch_id The touch_id value of the event
799 * \param x The x value of the event
800 * \param y The y value of the event
801 *
802 * For every resource that is currently in focus, send a wl_touch.down event
803 * with the passed parameters. The focused resources are the wl_touch
804 * resources of the client which currently has the surface with touch focus.
805 */
806WL_EXPORT void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200807weston_touch_send_down(struct weston_touch *touch, const struct timespec *time,
Quentin Glidiccde13452016-08-12 10:41:32 +0200808 int touch_id, wl_fixed_t x, wl_fixed_t y)
809{
Rob Bradford880ebc72013-07-22 17:31:38 +0100810 struct wl_display *display = touch->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400811 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100812 struct wl_resource *resource;
813 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300814 wl_fixed_t sx, sy;
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200815 uint32_t msecs;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300816
Quentin Glidiccde13452016-08-12 10:41:32 +0200817 if (!weston_touch_has_focus_resource(touch))
Bryce Harrington2c40d1d2016-02-02 10:18:48 -0800818 return;
819
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300820 weston_view_from_global_fixed(touch->focus, x, y, &sx, &sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400821
Neil Roberts96d790e2013-09-19 17:32:00 +0100822 resource_list = &touch->focus_resource_list;
Quentin Glidiccde13452016-08-12 10:41:32 +0200823 serial = wl_display_next_serial(display);
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200824 msecs = timespec_to_msec(time);
Alexandros Frantzisd7157842018-02-20 14:07:03 +0200825 wl_resource_for_each(resource, resource_list) {
826 send_timestamps_for_input_resource(resource,
827 &touch->timestamps_list,
828 time);
829 wl_touch_send_down(resource, serial, msecs,
830 touch->focus->surface->resource,
831 touch_id, sx, sy);
832 }
Quentin Glidiccde13452016-08-12 10:41:32 +0200833}
Neil Roberts96d790e2013-09-19 17:32:00 +0100834
Quentin Glidiccde13452016-08-12 10:41:32 +0200835static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200836default_grab_touch_down(struct weston_touch_grab *grab,
837 const struct timespec *time, int touch_id,
838 wl_fixed_t x, wl_fixed_t y)
Quentin Glidiccde13452016-08-12 10:41:32 +0200839{
840 weston_touch_send_down(grab->touch, time, touch_id, x, y);
841}
842
843/** Send wl_touch.up events to focused resources.
844 *
845 * \param touch The touch where the up events originates from.
846 * \param time The timestamp of the event
847 * \param touch_id The touch_id value of the event
848 *
849 * For every resource that is currently in focus, send a wl_touch.up event
850 * with the passed parameters. The focused resources are the wl_touch
851 * resources of the client which currently has the surface with touch focus.
852 */
853WL_EXPORT void
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200854weston_touch_send_up(struct weston_touch *touch, const struct timespec *time,
855 int touch_id)
Quentin Glidiccde13452016-08-12 10:41:32 +0200856{
857 struct wl_display *display = touch->seat->compositor->wl_display;
858 uint32_t serial;
859 struct wl_resource *resource;
860 struct wl_list *resource_list;
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200861 uint32_t msecs;
Quentin Glidiccde13452016-08-12 10:41:32 +0200862
863 if (!weston_touch_has_focus_resource(touch))
864 return;
865
866 resource_list = &touch->focus_resource_list;
867 serial = wl_display_next_serial(display);
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200868 msecs = timespec_to_msec(time);
Alexandros Frantzisd7157842018-02-20 14:07:03 +0200869 wl_resource_for_each(resource, resource_list) {
870 send_timestamps_for_input_resource(resource,
871 &touch->timestamps_list,
872 time);
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200873 wl_touch_send_up(resource, serial, msecs, touch_id);
Alexandros Frantzisd7157842018-02-20 14:07:03 +0200874 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400875}
876
Kristian Høgsberge329f362013-05-06 22:19:57 -0400877static void
878default_grab_touch_up(struct weston_touch_grab *grab,
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200879 const struct timespec *time, int touch_id)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400880{
Quentin Glidiccde13452016-08-12 10:41:32 +0200881 weston_touch_send_up(grab->touch, time, touch_id);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400882}
883
Quentin Glidiccde13452016-08-12 10:41:32 +0200884/** Send wl_touch.motion events to focused resources.
885 *
886 * \param touch The touch where the motion events originates from.
887 * \param time The timestamp of the event
888 * \param touch_id The touch_id value of the event
889 * \param x The x value of the event
890 * \param y The y value of the event
891 *
892 * For every resource that is currently in focus, send a wl_touch.motion event
893 * with the passed parameters. The focused resources are the wl_touch
894 * resources of the client which currently has the surface with touch focus.
895 */
896WL_EXPORT void
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200897weston_touch_send_motion(struct weston_touch *touch,
898 const struct timespec *time, int touch_id,
899 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400900{
Neil Roberts96d790e2013-09-19 17:32:00 +0100901 struct wl_resource *resource;
902 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300903 wl_fixed_t sx, sy;
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200904 uint32_t msecs;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300905
Quentin Glidiccde13452016-08-12 10:41:32 +0200906 if (!weston_touch_has_focus_resource(touch))
907 return;
908
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300909 weston_view_from_global_fixed(touch->focus, x, y, &sx, &sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400910
Neil Roberts96d790e2013-09-19 17:32:00 +0100911 resource_list = &touch->focus_resource_list;
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200912 msecs = timespec_to_msec(time);
Neil Roberts96d790e2013-09-19 17:32:00 +0100913 wl_resource_for_each(resource, resource_list) {
Alexandros Frantzisd7157842018-02-20 14:07:03 +0200914 send_timestamps_for_input_resource(resource,
915 &touch->timestamps_list,
916 time);
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200917 wl_touch_send_motion(resource, msecs,
Kristian Høgsberge329f362013-05-06 22:19:57 -0400918 touch_id, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400919 }
920}
921
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200922static void
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200923default_grab_touch_motion(struct weston_touch_grab *grab,
924 const struct timespec *time, int touch_id,
925 wl_fixed_t x, wl_fixed_t y)
Quentin Glidiccde13452016-08-12 10:41:32 +0200926{
927 weston_touch_send_motion(grab->touch, time, touch_id, x, y);
928}
929
930
931/** Send wl_touch.frame events to focused resources.
932 *
933 * \param touch The touch where the frame events originates from.
934 *
935 * For every resource that is currently in focus, send a wl_touch.frame event.
936 * The focused resources are the wl_touch resources of the client which
937 * currently has the surface with touch focus.
938 */
939WL_EXPORT void
940weston_touch_send_frame(struct weston_touch *touch)
Jonas Ådahl1679f232014-04-12 09:39:51 +0200941{
942 struct wl_resource *resource;
943
Quentin Glidiccde13452016-08-12 10:41:32 +0200944 if (!weston_touch_has_focus_resource(touch))
945 return;
946
947 wl_resource_for_each(resource, &touch->focus_resource_list)
Jonas Ådahl1679f232014-04-12 09:39:51 +0200948 wl_touch_send_frame(resource);
949}
950
951static void
Quentin Glidiccde13452016-08-12 10:41:32 +0200952default_grab_touch_frame(struct weston_touch_grab *grab)
953{
954 weston_touch_send_frame(grab->touch);
955}
956
957static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200958default_grab_touch_cancel(struct weston_touch_grab *grab)
959{
960}
961
Kristian Høgsberge329f362013-05-06 22:19:57 -0400962static const struct weston_touch_grab_interface default_touch_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400963 default_grab_touch_down,
964 default_grab_touch_up,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200965 default_grab_touch_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +0200966 default_grab_touch_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200967 default_grab_touch_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400968};
969
Quentin Glidiccde13452016-08-12 10:41:32 +0200970/** Check if the keyboard has focused resources.
971 *
972 * \param keyboard The keyboard to check for focused resources.
973 * \return Whether or not this keyboard has focused resources
974 */
975WL_EXPORT bool
976weston_keyboard_has_focus_resource(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400977{
Quentin Glidiccde13452016-08-12 10:41:32 +0200978 if (!keyboard->focus)
979 return false;
980
981 if (wl_list_empty(&keyboard->focus_resource_list))
982 return false;
983
984 return true;
985}
986
987/** Send wl_keyboard.key events to focused resources.
988 *
989 * \param keyboard The keyboard where the key events originates from.
990 * \param time The timestamp of the event
991 * \param key The key value of the event
992 * \param state The state enum value of the event
993 *
994 * For every resource that is currently in focus, send a wl_keyboard.key event
995 * with the passed parameters. The focused resources are the wl_keyboard
996 * resources of the client which currently has the surface with keyboard focus.
997 */
998WL_EXPORT void
999weston_keyboard_send_key(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02001000 const struct timespec *time, uint32_t key,
Quentin Glidiccde13452016-08-12 10:41:32 +02001001 enum wl_keyboard_key_state state)
1002{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001003 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +01001004 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001005 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01001006 struct wl_list *resource_list;
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02001007 uint32_t msecs;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001008
Quentin Glidiccde13452016-08-12 10:41:32 +02001009 if (!weston_keyboard_has_focus_resource(keyboard))
1010 return;
1011
Neil Roberts96d790e2013-09-19 17:32:00 +01001012 resource_list = &keyboard->focus_resource_list;
Quentin Glidiccde13452016-08-12 10:41:32 +02001013 serial = wl_display_next_serial(display);
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02001014 msecs = timespec_to_msec(time);
Alexandros Frantzis2b442482018-02-20 14:05:50 +02001015 wl_resource_for_each(resource, resource_list) {
1016 send_timestamps_for_input_resource(resource,
1017 &keyboard->timestamps_list,
1018 time);
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02001019 wl_keyboard_send_key(resource, serial, msecs, key, state);
Alexandros Frantzis2b442482018-02-20 14:05:50 +02001020 }
Quentin Glidiccde13452016-08-12 10:41:32 +02001021};
1022
1023static void
1024default_grab_keyboard_key(struct weston_keyboard_grab *grab,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02001025 const struct timespec *time, uint32_t key,
1026 uint32_t state)
Quentin Glidiccde13452016-08-12 10:41:32 +02001027{
1028 weston_keyboard_send_key(grab->keyboard, time, key, state);
Neil Roberts96d790e2013-09-19 17:32:00 +01001029}
1030
1031static void
1032send_modifiers_to_resource(struct weston_keyboard *keyboard,
1033 struct wl_resource *resource,
1034 uint32_t serial)
1035{
1036 wl_keyboard_send_modifiers(resource,
1037 serial,
1038 keyboard->modifiers.mods_depressed,
1039 keyboard->modifiers.mods_latched,
1040 keyboard->modifiers.mods_locked,
1041 keyboard->modifiers.group);
1042}
1043
1044static void
1045send_modifiers_to_client_in_list(struct wl_client *client,
1046 struct wl_list *list,
1047 uint32_t serial,
1048 struct weston_keyboard *keyboard)
1049{
1050 struct wl_resource *resource;
1051
1052 wl_resource_for_each(resource, list) {
1053 if (wl_resource_get_client(resource) == client)
1054 send_modifiers_to_resource(keyboard,
1055 resource,
1056 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001057 }
1058}
1059
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001060static struct weston_pointer_client *
1061find_pointer_client_for_surface(struct weston_pointer *pointer,
1062 struct weston_surface *surface)
1063{
1064 struct wl_client *client;
1065
1066 if (!surface)
1067 return NULL;
1068
1069 if (!surface->resource)
1070 return NULL;
1071
1072 client = wl_resource_get_client(surface->resource);
1073 return weston_pointer_get_pointer_client(pointer, client);
1074}
1075
1076static struct weston_pointer_client *
1077find_pointer_client_for_view(struct weston_pointer *pointer, struct weston_view *view)
1078{
1079 if (!view)
1080 return NULL;
1081
1082 return find_pointer_client_for_surface(pointer, view->surface);
1083}
1084
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001085static struct wl_resource *
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001086find_resource_for_surface(struct wl_list *list, struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001087{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001088 if (!surface)
1089 return NULL;
1090
Jason Ekstrand44a38632013-06-14 10:08:00 -05001091 if (!surface->resource)
1092 return NULL;
Stefan Schmidtfda26522013-09-17 10:54:09 +01001093
Jason Ekstrand44a38632013-06-14 10:08:00 -05001094 return wl_resource_find_for_client(list, wl_resource_get_client(surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001095}
1096
Quentin Glidiccde13452016-08-12 10:41:32 +02001097/** Send wl_keyboard.modifiers events to focused resources and pointer
1098 * focused resources.
1099 *
1100 * \param keyboard The keyboard where the modifiers events originates from.
1101 * \param serial The serial of the event
1102 * \param mods_depressed The mods_depressed value of the event
1103 * \param mods_latched The mods_latched value of the event
1104 * \param mods_locked The mods_locked value of the event
1105 * \param group The group value of the event
1106 *
1107 * For every resource that is currently in focus, send a wl_keyboard.modifiers
1108 * event with the passed parameters. The focused resources are the wl_keyboard
1109 * resources of the client which currently has the surface with keyboard focus.
1110 * This also sends wl_keyboard.modifiers events to the wl_keyboard resources of
1111 * the client having pointer focus (if different from the keyboard focus client).
1112 */
1113WL_EXPORT void
1114weston_keyboard_send_modifiers(struct weston_keyboard *keyboard,
1115 uint32_t serial, uint32_t mods_depressed,
1116 uint32_t mods_latched,
1117 uint32_t mods_locked, uint32_t group)
1118{
1119 struct weston_pointer *pointer =
1120 weston_seat_get_pointer(keyboard->seat);
1121
1122 if (weston_keyboard_has_focus_resource(keyboard)) {
1123 struct wl_list *resource_list;
1124 struct wl_resource *resource;
1125
1126 resource_list = &keyboard->focus_resource_list;
1127 wl_resource_for_each(resource, resource_list) {
1128 wl_keyboard_send_modifiers(resource, serial,
1129 mods_depressed, mods_latched,
1130 mods_locked, group);
1131 }
1132 }
1133
1134 if (pointer && pointer->focus && pointer->focus->surface->resource &&
1135 pointer->focus->surface != keyboard->focus) {
1136 struct wl_client *pointer_client =
1137 wl_resource_get_client(pointer->focus->surface->resource);
1138
1139 send_modifiers_to_client_in_list(pointer_client,
1140 &keyboard->resource_list,
1141 serial,
1142 keyboard);
1143 }
1144}
1145
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001146static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -07001147default_grab_keyboard_modifiers(struct weston_keyboard_grab *grab,
1148 uint32_t serial, uint32_t mods_depressed,
1149 uint32_t mods_latched,
1150 uint32_t mods_locked, uint32_t group)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001151{
Quentin Glidiccde13452016-08-12 10:41:32 +02001152 weston_keyboard_send_modifiers(grab->keyboard, serial, mods_depressed,
1153 mods_latched, mods_locked, group);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001154}
1155
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001156static void
1157default_grab_keyboard_cancel(struct weston_keyboard_grab *grab)
1158{
1159}
1160
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001161static const struct weston_keyboard_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001162 default_keyboard_grab_interface = {
Kristian Høgsbergb27901c2013-10-28 15:32:02 -07001163 default_grab_keyboard_key,
1164 default_grab_keyboard_modifiers,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001165 default_grab_keyboard_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001166};
1167
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001168static void
1169pointer_unmap_sprite(struct weston_pointer *pointer)
1170{
Pekka Paalanenc557ff72014-11-12 16:42:52 +02001171 struct weston_surface *surface = pointer->sprite->surface;
1172
1173 if (weston_surface_is_mapped(surface))
1174 weston_surface_unmap(surface);
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001175
1176 wl_list_remove(&pointer->sprite_destroy_listener.link);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02001177 surface->committed = NULL;
1178 surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03001179 weston_surface_set_label_func(surface, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001180 weston_view_destroy(pointer->sprite);
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001181 pointer->sprite = NULL;
1182}
1183
1184static void
1185pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
1186{
1187 struct weston_pointer *pointer =
1188 container_of(listener, struct weston_pointer,
1189 sprite_destroy_listener);
1190
1191 pointer->sprite = NULL;
1192}
1193
Jonas Ådahl3e12e632013-12-02 22:05:05 +01001194static void
1195weston_pointer_reset_state(struct weston_pointer *pointer)
1196{
1197 pointer->button_count = 0;
1198}
1199
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001200static void
1201weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data);
1202
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001203static struct weston_pointer *
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001204weston_pointer_create(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001205{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001206 struct weston_pointer *pointer;
1207
Peter Huttererf3d62272013-08-08 11:57:05 +10001208 pointer = zalloc(sizeof *pointer);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001209 if (pointer == NULL)
1210 return NULL;
1211
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001212 wl_list_init(&pointer->pointer_clients);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001213 weston_pointer_set_default_grab(pointer,
1214 seat->compositor->default_pointer_grab);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001215 wl_list_init(&pointer->focus_resource_listener.link);
1216 pointer->focus_resource_listener.notify = pointer_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001217 pointer->default_grab.pointer = pointer;
1218 pointer->grab = &pointer->default_grab;
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001219 wl_signal_init(&pointer->motion_signal);
Emilio Pozuelo Monfortaa7a4762013-11-19 11:37:15 +01001220 wl_signal_init(&pointer->focus_signal);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001221 wl_list_init(&pointer->focus_view_listener.link);
Jonas Ådahl3eb4ddd2016-07-22 17:52:58 +08001222 wl_signal_init(&pointer->destroy_signal);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +02001223 wl_list_init(&pointer->timestamps_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001224
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001225 pointer->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
1226
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001227 /* FIXME: Pick better co-ords. */
1228 pointer->x = wl_fixed_from_int(100);
1229 pointer->y = wl_fixed_from_int(100);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001230
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001231 pointer->output_destroy_listener.notify =
1232 weston_pointer_handle_output_destroy;
1233 wl_signal_add(&seat->compositor->output_destroyed_signal,
1234 &pointer->output_destroy_listener);
1235
Derek Foremanf9318d12015-05-11 15:40:11 -05001236 pointer->sx = wl_fixed_from_int(-1000000);
1237 pointer->sy = wl_fixed_from_int(-1000000);
1238
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001239 return pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001240}
1241
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001242static void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001243weston_pointer_destroy(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001244{
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +02001245 struct weston_pointer_client *pointer_client, *tmp;
1246
Jonas Ådahl3eb4ddd2016-07-22 17:52:58 +08001247 wl_signal_emit(&pointer->destroy_signal, pointer);
1248
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001249 if (pointer->sprite)
1250 pointer_unmap_sprite(pointer);
1251
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +02001252 wl_list_for_each_safe(pointer_client, tmp, &pointer->pointer_clients,
1253 link) {
1254 wl_list_remove(&pointer_client->link);
1255 weston_pointer_client_destroy(pointer_client);
1256 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001257
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001258 wl_list_remove(&pointer->focus_resource_listener.link);
1259 wl_list_remove(&pointer->focus_view_listener.link);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001260 wl_list_remove(&pointer->output_destroy_listener.link);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +02001261 wl_list_remove(&pointer->timestamps_list);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001262 free(pointer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001263}
1264
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001265void
1266weston_pointer_set_default_grab(struct weston_pointer *pointer,
1267 const struct weston_pointer_grab_interface *interface)
1268{
1269 if (interface)
1270 pointer->default_grab.interface = interface;
1271 else
1272 pointer->default_grab.interface =
1273 &default_pointer_grab_interface;
1274}
1275
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001276static struct weston_keyboard *
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001277weston_keyboard_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001278{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001279 struct weston_keyboard *keyboard;
1280
Peter Huttererf3d62272013-08-08 11:57:05 +10001281 keyboard = zalloc(sizeof *keyboard);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001282 if (keyboard == NULL)
Neil Roberts96d790e2013-09-19 17:32:00 +01001283 return NULL;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001284
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001285 wl_list_init(&keyboard->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +01001286 wl_list_init(&keyboard->focus_resource_list);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001287 wl_list_init(&keyboard->focus_resource_listener.link);
1288 keyboard->focus_resource_listener.notify = keyboard_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001289 wl_array_init(&keyboard->keys);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001290 keyboard->default_grab.interface = &default_keyboard_grab_interface;
1291 keyboard->default_grab.keyboard = keyboard;
1292 keyboard->grab = &keyboard->default_grab;
1293 wl_signal_init(&keyboard->focus_signal);
Alexandros Frantzis2b442482018-02-20 14:05:50 +02001294 wl_list_init(&keyboard->timestamps_list);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001295
1296 return keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001297}
1298
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001299static void
1300weston_xkb_info_destroy(struct weston_xkb_info *xkb_info);
1301
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001302static void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001303weston_keyboard_destroy(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001304{
Alexandros Frantzis7a314d62018-01-26 18:47:56 +02001305 struct wl_resource *resource;
1306
1307 wl_resource_for_each(resource, &keyboard->resource_list) {
1308 wl_resource_set_user_data(resource, NULL);
1309 }
1310
1311 wl_resource_for_each(resource, &keyboard->focus_resource_list) {
1312 wl_resource_set_user_data(resource, NULL);
1313 }
1314
1315 wl_list_remove(&keyboard->resource_list);
1316 wl_list_remove(&keyboard->focus_resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +01001317
Derek Foreman185d1582017-06-28 11:17:23 -05001318 xkb_state_unref(keyboard->xkb_state.state);
1319 if (keyboard->xkb_info)
1320 weston_xkb_info_destroy(keyboard->xkb_info);
1321 xkb_keymap_unref(keyboard->pending_keymap);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001322
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001323 wl_array_release(&keyboard->keys);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001324 wl_list_remove(&keyboard->focus_resource_listener.link);
Alexandros Frantzis2b442482018-02-20 14:05:50 +02001325 wl_list_remove(&keyboard->timestamps_list);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001326 free(keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001327}
1328
Jonas Ådahlcbfa7f72013-12-02 22:05:04 +01001329static void
1330weston_touch_reset_state(struct weston_touch *touch)
1331{
1332 touch->num_tp = 0;
1333}
1334
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001335static struct weston_touch *
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001336weston_touch_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001337{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001338 struct weston_touch *touch;
1339
Peter Huttererf3d62272013-08-08 11:57:05 +10001340 touch = zalloc(sizeof *touch);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001341 if (touch == NULL)
1342 return NULL;
1343
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -05001344 wl_list_init(&touch->device_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001345 wl_list_init(&touch->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +01001346 wl_list_init(&touch->focus_resource_list);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001347 wl_list_init(&touch->focus_view_listener.link);
1348 touch->focus_view_listener.notify = touch_focus_view_destroyed;
1349 wl_list_init(&touch->focus_resource_listener.link);
1350 touch->focus_resource_listener.notify = touch_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001351 touch->default_grab.interface = &default_touch_grab_interface;
1352 touch->default_grab.touch = touch;
1353 touch->grab = &touch->default_grab;
1354 wl_signal_init(&touch->focus_signal);
Alexandros Frantzisd7157842018-02-20 14:07:03 +02001355 wl_list_init(&touch->timestamps_list);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001356
1357 return touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001358}
1359
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001360static void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001361weston_touch_destroy(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001362{
Alexandros Frantzisb0b598c2018-01-26 18:47:57 +02001363 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01001364
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -05001365 assert(wl_list_empty(&touch->device_list));
1366
Alexandros Frantzisb0b598c2018-01-26 18:47:57 +02001367 wl_resource_for_each(resource, &touch->resource_list) {
1368 wl_resource_set_user_data(resource, NULL);
1369 }
1370
1371 wl_resource_for_each(resource, &touch->focus_resource_list) {
1372 wl_resource_set_user_data(resource, NULL);
1373 }
1374
1375 wl_list_remove(&touch->resource_list);
1376 wl_list_remove(&touch->focus_resource_list);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001377 wl_list_remove(&touch->focus_view_listener.link);
1378 wl_list_remove(&touch->focus_resource_listener.link);
Alexandros Frantzisd7157842018-02-20 14:07:03 +02001379 wl_list_remove(&touch->timestamps_list);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001380 free(touch);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001381}
1382
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001383static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04001384seat_send_updated_caps(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001385{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001386 enum wl_seat_capability caps = 0;
Rob Bradford6e737f52013-09-06 17:48:19 +01001387 struct wl_resource *resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001388
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001389 if (seat->pointer_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001390 caps |= WL_SEAT_CAPABILITY_POINTER;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001391 if (seat->keyboard_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001392 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001393 if (seat->touch_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001394 caps |= WL_SEAT_CAPABILITY_TOUCH;
1395
Rob Bradford6e737f52013-09-06 17:48:19 +01001396 wl_resource_for_each(resource, &seat->base_resource_list) {
1397 wl_seat_send_capabilities(resource, caps);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001398 }
Jason Ekstranda4ab5422014-04-02 19:53:45 -05001399 wl_signal_emit(&seat->updated_caps_signal, seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001400}
1401
Derek Foremanf9318d12015-05-11 15:40:11 -05001402
1403/** Clear the pointer focus
1404 *
1405 * \param pointer the pointer to clear focus for.
1406 *
1407 * This can be used to unset pointer focus and set the co-ordinates to the
1408 * arbitrary values we use for the no focus case.
1409 *
1410 * There's no requirement to use this function. For example, passing the
1411 * results of a weston_compositor_pick_view() directly to
1412 * weston_pointer_set_focus() will do the right thing when no view is found.
1413 */
1414WL_EXPORT void
1415weston_pointer_clear_focus(struct weston_pointer *pointer)
1416{
1417 weston_pointer_set_focus(pointer, NULL,
1418 wl_fixed_from_int(-1000000),
1419 wl_fixed_from_int(-1000000));
1420}
1421
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001422WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001423weston_pointer_set_focus(struct weston_pointer *pointer,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001424 struct weston_view *view,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001425 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001426{
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001427 struct weston_pointer_client *pointer_client;
Derek Foreman1281a362015-07-31 16:55:32 -05001428 struct weston_keyboard *kbd = weston_seat_get_keyboard(pointer->seat);
Neil Roberts96d790e2013-09-19 17:32:00 +01001429 struct wl_resource *resource;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001430 struct wl_resource *surface_resource;
Rob Bradford880ebc72013-07-22 17:31:38 +01001431 struct wl_display *display = pointer->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001432 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01001433 struct wl_list *focus_resource_list;
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001434 int refocus = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001435
1436 if ((!pointer->focus && view) ||
1437 (pointer->focus && !view) ||
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001438 (pointer->focus && pointer->focus->surface != view->surface) ||
1439 pointer->sx != sx || pointer->sy != sy)
1440 refocus = 1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001441
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001442 if (pointer->focus_client && refocus) {
1443 focus_resource_list = &pointer->focus_client->pointer_resources;
1444 if (!wl_list_empty(focus_resource_list)) {
1445 serial = wl_display_next_serial(display);
1446 surface_resource = pointer->focus->surface->resource;
1447 wl_resource_for_each(resource, focus_resource_list) {
1448 wl_pointer_send_leave(resource, serial,
1449 surface_resource);
Peter Hutterer87743e92016-01-18 16:38:22 +10001450 pointer_send_frame(resource);
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001451 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001452 }
1453
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001454 pointer->focus_client = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001455 }
1456
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001457 pointer_client = find_pointer_client_for_view(pointer, view);
1458 if (pointer_client && refocus) {
1459 struct wl_client *surface_client = pointer_client->client;
Neil Roberts96d790e2013-09-19 17:32:00 +01001460
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001461 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001462
Jason Ekstranda7af7042013-10-12 22:38:11 -05001463 if (kbd && kbd->focus != view->surface)
Kristian Høgsbergcb406f12013-10-09 10:54:03 -07001464 send_modifiers_to_client_in_list(surface_client,
1465 &kbd->resource_list,
1466 serial,
1467 kbd);
1468
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001469 pointer->focus_client = pointer_client;
Neil Roberts96d790e2013-09-19 17:32:00 +01001470
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001471 focus_resource_list = &pointer->focus_client->pointer_resources;
Neil Roberts96d790e2013-09-19 17:32:00 +01001472 wl_resource_for_each(resource, focus_resource_list) {
1473 wl_pointer_send_enter(resource,
1474 serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001475 view->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01001476 sx, sy);
Peter Hutterer87743e92016-01-18 16:38:22 +10001477 pointer_send_frame(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001478 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001479
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001480 pointer->focus_serial = serial;
1481 }
1482
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001483 wl_list_remove(&pointer->focus_view_listener.link);
1484 wl_list_init(&pointer->focus_view_listener.link);
1485 wl_list_remove(&pointer->focus_resource_listener.link);
1486 wl_list_init(&pointer->focus_resource_listener.link);
Emilio Pozuelo Monfortaa7a4762013-11-19 11:37:15 +01001487 if (view)
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001488 wl_signal_add(&view->destroy_signal, &pointer->focus_view_listener);
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001489 if (view && view->surface->resource)
1490 wl_resource_add_destroy_listener(view->surface->resource,
1491 &pointer->focus_resource_listener);
1492
1493 pointer->focus = view;
1494 pointer->focus_view_listener.notify = pointer_focus_view_destroyed;
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001495 pointer->sx = sx;
1496 pointer->sy = sy;
1497
Derek Foremanf9318d12015-05-11 15:40:11 -05001498 assert(view || sx == wl_fixed_from_int(-1000000));
1499 assert(view || sy == wl_fixed_from_int(-1000000));
1500
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001501 wl_signal_emit(&pointer->focus_signal, pointer);
1502}
1503
Neil Roberts96d790e2013-09-19 17:32:00 +01001504static void
1505send_enter_to_resource_list(struct wl_list *list,
1506 struct weston_keyboard *keyboard,
1507 struct weston_surface *surface,
1508 uint32_t serial)
1509{
1510 struct wl_resource *resource;
1511
1512 wl_resource_for_each(resource, list) {
1513 send_modifiers_to_resource(keyboard, resource, serial);
1514 wl_keyboard_send_enter(resource, serial,
1515 surface->resource,
1516 &keyboard->keys);
1517 }
1518}
1519
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001520WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001521weston_keyboard_set_focus(struct weston_keyboard *keyboard,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001522 struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001523{
Quentin Glidic85d55542017-07-21 14:02:40 +02001524 struct weston_seat *seat = keyboard->seat;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001525 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +01001526 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001527 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01001528 struct wl_list *focus_resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001529
Pekka Paalanen72e183b2018-02-22 16:55:15 +02001530 /* Keyboard focus on a surface without a client is equivalent to NULL
1531 * focus as nothing would react to the keyboard events anyway.
1532 * Just set focus to NULL instead - the destroy listener hangs on the
1533 * wl_resource anyway.
1534 */
1535 if (surface && !surface->resource)
1536 surface = NULL;
1537
Neil Roberts96d790e2013-09-19 17:32:00 +01001538 focus_resource_list = &keyboard->focus_resource_list;
1539
1540 if (!wl_list_empty(focus_resource_list) && keyboard->focus != surface) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001541 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001542 wl_resource_for_each(resource, focus_resource_list) {
1543 wl_keyboard_send_leave(resource, serial,
1544 keyboard->focus->resource);
1545 }
1546 move_resources(&keyboard->resource_list, focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001547 }
1548
Neil Roberts96d790e2013-09-19 17:32:00 +01001549 if (find_resource_for_surface(&keyboard->resource_list, surface) &&
1550 keyboard->focus != surface) {
1551 struct wl_client *surface_client =
1552 wl_resource_get_client(surface->resource);
1553
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001554 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001555
1556 move_resources_for_client(focus_resource_list,
1557 &keyboard->resource_list,
1558 surface_client);
1559 send_enter_to_resource_list(focus_resource_list,
1560 keyboard,
1561 surface,
1562 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001563 keyboard->focus_serial = serial;
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001564 }
1565
Quentin Glidic85d55542017-07-21 14:02:40 +02001566 if (seat->saved_kbd_focus) {
1567 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1568 seat->saved_kbd_focus = NULL;
1569 }
1570
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001571 wl_list_remove(&keyboard->focus_resource_listener.link);
1572 wl_list_init(&keyboard->focus_resource_listener.link);
Pekka Paalanen72e183b2018-02-22 16:55:15 +02001573 if (surface)
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001574 wl_resource_add_destroy_listener(surface->resource,
1575 &keyboard->focus_resource_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001576
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001577 keyboard->focus = surface;
1578 wl_signal_emit(&keyboard->focus_signal, keyboard);
1579}
1580
Giulio Camuffoa20ca812014-11-22 11:16:56 +02001581/* Users of this function must manually manage the keyboard focus */
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001582WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001583weston_keyboard_start_grab(struct weston_keyboard *keyboard,
1584 struct weston_keyboard_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001585{
1586 keyboard->grab = grab;
1587 grab->keyboard = keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001588}
1589
1590WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001591weston_keyboard_end_grab(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001592{
1593 keyboard->grab = &keyboard->default_grab;
1594}
1595
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001596static void
1597weston_keyboard_cancel_grab(struct weston_keyboard *keyboard)
1598{
1599 keyboard->grab->interface->cancel(keyboard->grab);
1600}
1601
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001602WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001603weston_pointer_start_grab(struct weston_pointer *pointer,
1604 struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001605{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001606 pointer->grab = grab;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001607 grab->pointer = pointer;
Kristian Høgsbergda751b82013-07-04 00:58:07 -04001608 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001609}
1610
1611WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001612weston_pointer_end_grab(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001613{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001614 pointer->grab = &pointer->default_grab;
Kristian Høgsbergda751b82013-07-04 00:58:07 -04001615 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001616}
1617
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001618static void
1619weston_pointer_cancel_grab(struct weston_pointer *pointer)
1620{
1621 pointer->grab->interface->cancel(pointer->grab);
1622}
1623
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001624WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -04001625weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001626{
1627 touch->grab = grab;
1628 grab->touch = touch;
1629}
1630
1631WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -04001632weston_touch_end_grab(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001633{
1634 touch->grab = &touch->default_grab;
1635}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001636
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001637static void
1638weston_touch_cancel_grab(struct weston_touch *touch)
1639{
1640 touch->grab->interface->cancel(touch->grab);
1641}
1642
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001643static void
1644weston_pointer_clamp_for_output(struct weston_pointer *pointer,
1645 struct weston_output *output,
1646 wl_fixed_t *fx, wl_fixed_t *fy)
1647{
1648 int x, y;
1649
1650 x = wl_fixed_to_int(*fx);
1651 y = wl_fixed_to_int(*fy);
1652
1653 if (x < output->x)
1654 *fx = wl_fixed_from_int(output->x);
1655 else if (x >= output->x + output->width)
1656 *fx = wl_fixed_from_int(output->x +
1657 output->width - 1);
1658 if (y < output->y)
1659 *fy = wl_fixed_from_int(output->y);
1660 else if (y >= output->y + output->height)
1661 *fy = wl_fixed_from_int(output->y +
1662 output->height - 1);
1663}
1664
Rob Bradford806d8c02013-06-25 18:56:41 +01001665WL_EXPORT void
1666weston_pointer_clamp(struct weston_pointer *pointer, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001667{
Rob Bradford806d8c02013-06-25 18:56:41 +01001668 struct weston_compositor *ec = pointer->seat->compositor;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001669 struct weston_output *output, *prev = NULL;
1670 int x, y, old_x, old_y, valid = 0;
1671
1672 x = wl_fixed_to_int(*fx);
1673 y = wl_fixed_to_int(*fy);
Rob Bradford806d8c02013-06-25 18:56:41 +01001674 old_x = wl_fixed_to_int(pointer->x);
1675 old_y = wl_fixed_to_int(pointer->y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001676
1677 wl_list_for_each(output, &ec->output_list, link) {
Rob Bradford66bd9f52013-06-25 18:56:42 +01001678 if (pointer->seat->output && pointer->seat->output != output)
1679 continue;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001680 if (pixman_region32_contains_point(&output->region,
1681 x, y, NULL))
1682 valid = 1;
1683 if (pixman_region32_contains_point(&output->region,
1684 old_x, old_y, NULL))
1685 prev = output;
1686 }
1687
Rob Bradford66bd9f52013-06-25 18:56:42 +01001688 if (!prev)
1689 prev = pointer->seat->output;
1690
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001691 if (prev && !valid)
1692 weston_pointer_clamp_for_output(pointer, prev, fx, fy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001693}
1694
Jonas Ådahld2510102014-10-05 21:39:14 +02001695static void
1696weston_pointer_move_to(struct weston_pointer *pointer,
1697 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001698{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001699 int32_t ix, iy;
1700
Rob Bradford806d8c02013-06-25 18:56:41 +01001701 weston_pointer_clamp (pointer, &x, &y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001702
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001703 pointer->x = x;
1704 pointer->y = y;
1705
1706 ix = wl_fixed_to_int(x);
1707 iy = wl_fixed_to_int(y);
1708
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001709 if (pointer->sprite) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001710 weston_view_set_position(pointer->sprite,
1711 ix - pointer->hotspot_x,
1712 iy - pointer->hotspot_y);
1713 weston_view_schedule_repaint(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001714 }
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001715
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001716 pointer->grab->interface->focus(pointer->grab);
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001717 wl_signal_emit(&pointer->motion_signal, pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001718}
1719
Jonas Ådahld2510102014-10-05 21:39:14 +02001720WL_EXPORT void
Jonas Ådahld2510102014-10-05 21:39:14 +02001721weston_pointer_move(struct weston_pointer *pointer,
1722 struct weston_pointer_motion_event *event)
1723{
1724 wl_fixed_t x, y;
1725
1726 weston_pointer_motion_to_abs(pointer, event, &x, &y);
1727 weston_pointer_move_to(pointer, x, y);
1728}
1729
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001730/** Verify if the pointer is in a valid position and move it if it isn't.
1731 */
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001732static void
1733weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001734{
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001735 struct weston_pointer *pointer;
1736 struct weston_compositor *ec;
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001737 struct weston_output *output, *closest = NULL;
1738 int x, y, distance, min = INT_MAX;
1739 wl_fixed_t fx, fy;
1740
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001741 pointer = container_of(listener, struct weston_pointer,
1742 output_destroy_listener);
1743 ec = pointer->seat->compositor;
1744
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001745 x = wl_fixed_to_int(pointer->x);
1746 y = wl_fixed_to_int(pointer->y);
1747
1748 wl_list_for_each(output, &ec->output_list, link) {
1749 if (pixman_region32_contains_point(&output->region,
1750 x, y, NULL))
1751 return;
1752
1753 /* Aproximante the distance from the pointer to the center of
1754 * the output. */
1755 distance = abs(output->x + output->width / 2 - x) +
1756 abs(output->y + output->height / 2 - y);
1757 if (distance < min) {
1758 min = distance;
1759 closest = output;
1760 }
1761 }
1762
1763 /* Nothing to do if there's no output left. */
1764 if (!closest)
1765 return;
1766
1767 fx = pointer->x;
1768 fy = pointer->y;
1769
1770 weston_pointer_clamp_for_output(pointer, closest, &fx, &fy);
Jonas Ådahld2510102014-10-05 21:39:14 +02001771 weston_pointer_move_to(pointer, fx, fy);
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001772}
1773
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001774WL_EXPORT void
1775notify_motion(struct weston_seat *seat,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001776 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001777 struct weston_pointer_motion_event *event)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001778{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001779 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001780 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001781
1782 weston_compositor_wake(ec);
Jonas Ådahld2510102014-10-05 21:39:14 +02001783 pointer->grab->interface->motion(pointer->grab, time, event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001784}
1785
Daniel Stone96d47c02013-11-19 11:37:12 +01001786static void
1787run_modifier_bindings(struct weston_seat *seat, uint32_t old, uint32_t new)
1788{
1789 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001790 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Daniel Stone96d47c02013-11-19 11:37:12 +01001791 uint32_t diff;
1792 unsigned int i;
1793 struct {
1794 uint32_t xkb;
1795 enum weston_keyboard_modifier weston;
1796 } mods[] = {
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001797 { keyboard->xkb_info->ctrl_mod, MODIFIER_CTRL },
1798 { keyboard->xkb_info->alt_mod, MODIFIER_ALT },
1799 { keyboard->xkb_info->super_mod, MODIFIER_SUPER },
1800 { keyboard->xkb_info->shift_mod, MODIFIER_SHIFT },
Daniel Stone96d47c02013-11-19 11:37:12 +01001801 };
1802
1803 diff = new & ~old;
1804 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
1805 if (diff & (1 << mods[i].xkb))
1806 weston_compositor_run_modifier_binding(compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001807 keyboard,
Daniel Stone96d47c02013-11-19 11:37:12 +01001808 mods[i].weston,
1809 WL_KEYBOARD_KEY_STATE_PRESSED);
1810 }
1811
1812 diff = old & ~new;
1813 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
1814 if (diff & (1 << mods[i].xkb))
1815 weston_compositor_run_modifier_binding(compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001816 keyboard,
Daniel Stone96d47c02013-11-19 11:37:12 +01001817 mods[i].weston,
1818 WL_KEYBOARD_KEY_STATE_RELEASED);
1819 }
1820}
1821
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001822WL_EXPORT void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001823notify_motion_absolute(struct weston_seat *seat, const struct timespec *time,
1824 double x, double y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001825{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001826 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001827 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Jonas Ådahld2510102014-10-05 21:39:14 +02001828 struct weston_pointer_motion_event event = { 0 };
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001829
1830 weston_compositor_wake(ec);
Jonas Ådahld2510102014-10-05 21:39:14 +02001831
1832 event = (struct weston_pointer_motion_event) {
1833 .mask = WESTON_POINTER_MOTION_ABS,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001834 .x = x,
1835 .y = y,
Jonas Ådahld2510102014-10-05 21:39:14 +02001836 };
1837
1838 pointer->grab->interface->motion(pointer->grab, time, &event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001839}
1840
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02001841static unsigned int
1842peek_next_activate_serial(struct weston_compositor *c)
1843{
1844 unsigned serial = c->activate_serial + 1;
1845
1846 return serial == 0 ? 1 : serial;
1847}
1848
1849static void
1850inc_activate_serial(struct weston_compositor *c)
1851{
1852 c->activate_serial = peek_next_activate_serial (c);
1853}
1854
1855WL_EXPORT void
1856weston_view_activate(struct weston_view *view,
1857 struct weston_seat *seat,
1858 uint32_t flags)
1859{
1860 struct weston_compositor *compositor = seat->compositor;
1861
1862 if (flags & WESTON_ACTIVATE_FLAG_CLICKED) {
1863 view->click_to_activate_serial =
1864 peek_next_activate_serial(compositor);
1865 }
1866
1867 weston_seat_set_keyboard_focus(seat, view->surface);
1868}
1869
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001870WL_EXPORT void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001871notify_button(struct weston_seat *seat, const struct timespec *time,
1872 int32_t button, enum wl_pointer_button_state state)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001873{
1874 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001875 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001876
1877 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001878 weston_compositor_idle_inhibit(compositor);
1879 if (pointer->button_count == 0) {
1880 pointer->grab_button = button;
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001881 pointer->grab_time = *time;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001882 pointer->grab_x = pointer->x;
1883 pointer->grab_y = pointer->y;
1884 }
1885 pointer->button_count++;
1886 } else {
1887 weston_compositor_idle_release(compositor);
1888 pointer->button_count--;
1889 }
1890
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001891 weston_compositor_run_button_binding(compositor, pointer, time, button,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001892 state);
1893
1894 pointer->grab->interface->button(pointer->grab, time, button, state);
1895
1896 if (pointer->button_count == 1)
1897 pointer->grab_serial =
1898 wl_display_get_serial(compositor->wl_display);
1899}
1900
1901WL_EXPORT void
Alexandros Frantzis80321942017-11-16 18:20:56 +02001902notify_axis(struct weston_seat *seat, const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001903 struct weston_pointer_axis_event *event)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001904{
1905 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001906 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001907
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001908 weston_compositor_wake(compositor);
1909
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001910 if (weston_compositor_run_axis_binding(compositor, pointer,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001911 time, event))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001912 return;
1913
Peter Hutterer89b6a492016-01-18 15:58:17 +10001914 pointer->grab->interface->axis(pointer->grab, time, event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001915}
1916
Peter Hutterer87743e92016-01-18 16:38:22 +10001917WL_EXPORT void
1918notify_axis_source(struct weston_seat *seat, uint32_t source)
1919{
1920 struct weston_compositor *compositor = seat->compositor;
1921 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1922
1923 weston_compositor_wake(compositor);
1924
1925 pointer->grab->interface->axis_source(pointer->grab, source);
1926}
1927
1928WL_EXPORT void
1929notify_pointer_frame(struct weston_seat *seat)
1930{
1931 struct weston_compositor *compositor = seat->compositor;
1932 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1933
1934 weston_compositor_wake(compositor);
1935
1936 pointer->grab->interface->frame(pointer->grab);
1937}
1938
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001939WL_EXPORT int
1940weston_keyboard_set_locks(struct weston_keyboard *keyboard,
1941 uint32_t mask, uint32_t value)
1942{
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001943 uint32_t serial;
1944 xkb_mod_mask_t mods_depressed, mods_latched, mods_locked, group;
1945 xkb_mod_mask_t num, caps;
1946
1947 /* We don't want the leds to go out of sync with the actual state
1948 * so if the backend has no way to change the leds don't try to
1949 * change the state */
1950 if (!keyboard->seat->led_update)
1951 return -1;
1952
1953 mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
1954 XKB_STATE_DEPRESSED);
1955 mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
1956 XKB_STATE_LATCHED);
1957 mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
1958 XKB_STATE_LOCKED);
1959 group = xkb_state_serialize_group(keyboard->xkb_state.state,
1960 XKB_STATE_EFFECTIVE);
1961
1962 num = (1 << keyboard->xkb_info->mod2_mod);
1963 caps = (1 << keyboard->xkb_info->caps_mod);
1964 if (mask & WESTON_NUM_LOCK) {
1965 if (value & WESTON_NUM_LOCK)
1966 mods_locked |= num;
1967 else
1968 mods_locked &= ~num;
1969 }
1970 if (mask & WESTON_CAPS_LOCK) {
1971 if (value & WESTON_CAPS_LOCK)
1972 mods_locked |= caps;
1973 else
1974 mods_locked &= ~caps;
1975 }
1976
1977 xkb_state_update_mask(keyboard->xkb_state.state, mods_depressed,
1978 mods_latched, mods_locked, 0, 0, group);
1979
1980 serial = wl_display_next_serial(
1981 keyboard->seat->compositor->wl_display);
1982 notify_modifiers(keyboard->seat, serial);
1983
1984 return 0;
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001985}
1986
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001987WL_EXPORT void
1988notify_modifiers(struct weston_seat *seat, uint32_t serial)
1989{
Derek Foreman1281a362015-07-31 16:55:32 -05001990 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001991 struct weston_keyboard_grab *grab = keyboard->grab;
1992 uint32_t mods_depressed, mods_latched, mods_locked, group;
1993 uint32_t mods_lookup;
1994 enum weston_led leds = 0;
1995 int changed = 0;
1996
1997 /* Serialize and update our internal state, checking to see if it's
1998 * different to the previous state. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001999 mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03002000 XKB_STATE_MODS_DEPRESSED);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002001 mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03002002 XKB_STATE_MODS_LATCHED);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002003 mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03002004 XKB_STATE_MODS_LOCKED);
2005 group = xkb_state_serialize_layout(keyboard->xkb_state.state,
2006 XKB_STATE_LAYOUT_EFFECTIVE);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002007
Derek Foreman244e99e2015-06-03 15:53:26 -05002008 if (mods_depressed != keyboard->modifiers.mods_depressed ||
2009 mods_latched != keyboard->modifiers.mods_latched ||
2010 mods_locked != keyboard->modifiers.mods_locked ||
2011 group != keyboard->modifiers.group)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002012 changed = 1;
2013
Derek Foreman244e99e2015-06-03 15:53:26 -05002014 run_modifier_bindings(seat, keyboard->modifiers.mods_depressed,
Daniel Stone96d47c02013-11-19 11:37:12 +01002015 mods_depressed);
2016
Derek Foreman244e99e2015-06-03 15:53:26 -05002017 keyboard->modifiers.mods_depressed = mods_depressed;
2018 keyboard->modifiers.mods_latched = mods_latched;
2019 keyboard->modifiers.mods_locked = mods_locked;
2020 keyboard->modifiers.group = group;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002021
2022 /* And update the modifier_state for bindings. */
2023 mods_lookup = mods_depressed | mods_latched;
2024 seat->modifier_state = 0;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002025 if (mods_lookup & (1 << keyboard->xkb_info->ctrl_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002026 seat->modifier_state |= MODIFIER_CTRL;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002027 if (mods_lookup & (1 << keyboard->xkb_info->alt_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002028 seat->modifier_state |= MODIFIER_ALT;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002029 if (mods_lookup & (1 << keyboard->xkb_info->super_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002030 seat->modifier_state |= MODIFIER_SUPER;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002031 if (mods_lookup & (1 << keyboard->xkb_info->shift_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002032 seat->modifier_state |= MODIFIER_SHIFT;
2033
2034 /* Finally, notify the compositor that LEDs have changed. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002035 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
2036 keyboard->xkb_info->num_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002037 leds |= LED_NUM_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002038 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
2039 keyboard->xkb_info->caps_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002040 leds |= LED_CAPS_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002041 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
2042 keyboard->xkb_info->scroll_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002043 leds |= LED_SCROLL_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002044 if (leds != keyboard->xkb_state.leds && seat->led_update)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002045 seat->led_update(seat, leds);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002046 keyboard->xkb_state.leds = leds;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002047
2048 if (changed) {
2049 grab->interface->modifiers(grab,
2050 serial,
2051 keyboard->modifiers.mods_depressed,
2052 keyboard->modifiers.mods_latched,
2053 keyboard->modifiers.mods_locked,
2054 keyboard->modifiers.group);
2055 }
2056}
2057
2058static void
2059update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
2060 enum wl_keyboard_key_state state)
2061{
Derek Foreman1281a362015-07-31 16:55:32 -05002062 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002063 enum xkb_key_direction direction;
2064
2065 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2066 direction = XKB_KEY_DOWN;
2067 else
2068 direction = XKB_KEY_UP;
2069
2070 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
2071 * broken keycode system, which starts at 8. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002072 xkb_state_update_key(keyboard->xkb_state.state, key + 8, direction);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002073
2074 notify_modifiers(seat, serial);
2075}
Rui Matos65196bc2013-10-10 19:44:19 +02002076
2077static void
2078send_keymap(struct wl_resource *resource, struct weston_xkb_info *xkb_info)
2079{
2080 wl_keyboard_send_keymap(resource,
2081 WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2082 xkb_info->keymap_fd,
2083 xkb_info->keymap_size);
2084}
2085
2086static void
2087send_modifiers(struct wl_resource *resource, uint32_t serial, struct weston_keyboard *keyboard)
2088{
2089 wl_keyboard_send_modifiers(resource, serial,
2090 keyboard->modifiers.mods_depressed,
2091 keyboard->modifiers.mods_latched,
2092 keyboard->modifiers.mods_locked,
2093 keyboard->modifiers.group);
2094}
2095
2096static struct weston_xkb_info *
2097weston_xkb_info_create(struct xkb_keymap *keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02002098
2099static void
2100update_keymap(struct weston_seat *seat)
2101{
Derek Foreman1281a362015-07-31 16:55:32 -05002102 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Rui Matos65196bc2013-10-10 19:44:19 +02002103 struct wl_resource *resource;
2104 struct weston_xkb_info *xkb_info;
2105 struct xkb_state *state;
2106 xkb_mod_mask_t latched_mods;
2107 xkb_mod_mask_t locked_mods;
2108
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002109 xkb_info = weston_xkb_info_create(keyboard->pending_keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02002110
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002111 xkb_keymap_unref(keyboard->pending_keymap);
2112 keyboard->pending_keymap = NULL;
Rui Matos65196bc2013-10-10 19:44:19 +02002113
2114 if (!xkb_info) {
2115 weston_log("failed to create XKB info\n");
2116 return;
2117 }
2118
2119 state = xkb_state_new(xkb_info->keymap);
2120 if (!state) {
2121 weston_log("failed to initialise XKB state\n");
2122 weston_xkb_info_destroy(xkb_info);
2123 return;
2124 }
2125
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002126 latched_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
2127 XKB_STATE_MODS_LATCHED);
2128 locked_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
2129 XKB_STATE_MODS_LOCKED);
Rui Matos65196bc2013-10-10 19:44:19 +02002130 xkb_state_update_mask(state,
2131 0, /* depressed */
2132 latched_mods,
2133 locked_mods,
2134 0, 0, 0);
2135
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002136 weston_xkb_info_destroy(keyboard->xkb_info);
2137 keyboard->xkb_info = xkb_info;
Rui Matos65196bc2013-10-10 19:44:19 +02002138
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002139 xkb_state_unref(keyboard->xkb_state.state);
2140 keyboard->xkb_state.state = state;
Rui Matos65196bc2013-10-10 19:44:19 +02002141
Derek Foremanbc91e542015-06-03 15:53:27 -05002142 wl_resource_for_each(resource, &keyboard->resource_list)
Rui Matos65196bc2013-10-10 19:44:19 +02002143 send_keymap(resource, xkb_info);
Derek Foremanbc91e542015-06-03 15:53:27 -05002144 wl_resource_for_each(resource, &keyboard->focus_resource_list)
Rui Matos65196bc2013-10-10 19:44:19 +02002145 send_keymap(resource, xkb_info);
2146
2147 notify_modifiers(seat, wl_display_next_serial(seat->compositor->wl_display));
2148
2149 if (!latched_mods && !locked_mods)
2150 return;
2151
Derek Foremanbc91e542015-06-03 15:53:27 -05002152 wl_resource_for_each(resource, &keyboard->resource_list)
2153 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), keyboard);
2154 wl_resource_for_each(resource, &keyboard->focus_resource_list)
2155 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), keyboard);
Rui Matos65196bc2013-10-10 19:44:19 +02002156}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002157
2158WL_EXPORT void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02002159notify_key(struct weston_seat *seat, const struct timespec *time, uint32_t key,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002160 enum wl_keyboard_key_state state,
2161 enum weston_key_state_update update_state)
2162{
2163 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002164 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002165 struct weston_keyboard_grab *grab = keyboard->grab;
Pekka Paalanen86b53962014-11-19 13:43:32 +02002166 uint32_t *k, *end;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002167
2168 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002169 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002170 } else {
2171 weston_compositor_idle_release(compositor);
2172 }
2173
Pekka Paalanen86b53962014-11-19 13:43:32 +02002174 end = keyboard->keys.data + keyboard->keys.size;
2175 for (k = keyboard->keys.data; k < end; k++) {
2176 if (*k == key) {
2177 /* Ignore server-generated repeats. */
2178 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2179 return;
2180 *k = *--end;
2181 }
2182 }
2183 keyboard->keys.size = (void *) end - keyboard->keys.data;
2184 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2185 k = wl_array_add(&keyboard->keys, sizeof *k);
2186 *k = key;
2187 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002188
2189 if (grab == &keyboard->default_grab ||
2190 grab == &keyboard->input_method_grab) {
Derek Foreman99a6a2d2015-07-15 13:00:43 -05002191 weston_compositor_run_key_binding(compositor, keyboard, time,
2192 key, state);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002193 grab = keyboard->grab;
2194 }
2195
2196 grab->interface->key(grab, time, key, state);
2197
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002198 if (keyboard->pending_keymap &&
Pekka Paalanen86b53962014-11-19 13:43:32 +02002199 keyboard->keys.size == 0)
Rui Matos65196bc2013-10-10 19:44:19 +02002200 update_keymap(seat);
2201
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002202 if (update_state == STATE_UPDATE_AUTOMATIC) {
2203 update_modifier_state(seat,
2204 wl_display_get_serial(compositor->wl_display),
2205 key,
2206 state);
2207 }
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002208
Olivier Fourdandf84dbe2016-06-30 16:01:56 +02002209 keyboard->grab_serial = wl_display_get_serial(compositor->wl_display);
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002210 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02002211 keyboard->grab_time = *time;
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002212 keyboard->grab_key = key;
2213 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002214}
2215
2216WL_EXPORT void
2217notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002218 double x, double y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002219{
Derek Foreman1281a362015-07-31 16:55:32 -05002220 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2221
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002222 if (output) {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002223 weston_pointer_move_to(pointer,
2224 wl_fixed_from_double(x),
2225 wl_fixed_from_double(y));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002226 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002227 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002228 * NULL) here, but somehow that breaks re-entry... */
2229 }
2230}
2231
2232static void
2233destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
2234{
2235 struct weston_seat *ws;
2236
2237 ws = container_of(listener, struct weston_seat,
2238 saved_kbd_focus_listener);
2239
2240 ws->saved_kbd_focus = NULL;
2241}
2242
2243WL_EXPORT void
2244notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
2245 enum weston_key_state_update update_state)
2246{
2247 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002248 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04002249 struct weston_surface *surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002250 uint32_t *k, serial;
2251
2252 serial = wl_display_next_serial(compositor->wl_display);
2253 wl_array_copy(&keyboard->keys, keys);
2254 wl_array_for_each(k, &keyboard->keys) {
2255 weston_compositor_idle_inhibit(compositor);
2256 if (update_state == STATE_UPDATE_AUTOMATIC)
2257 update_modifier_state(seat, serial, *k,
2258 WL_KEYBOARD_KEY_STATE_PRESSED);
2259 }
2260
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002261 surface = seat->saved_kbd_focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002262 if (surface) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002263 weston_keyboard_set_focus(keyboard, surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002264 }
2265}
2266
2267WL_EXPORT void
2268notify_keyboard_focus_out(struct weston_seat *seat)
2269{
2270 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002271 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2272 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Quentin Glidic85d55542017-07-21 14:02:40 +02002273 struct weston_surface *focus = keyboard->focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002274 uint32_t *k, serial;
2275
2276 serial = wl_display_next_serial(compositor->wl_display);
2277 wl_array_for_each(k, &keyboard->keys) {
2278 weston_compositor_idle_release(compositor);
2279 update_modifier_state(seat, serial, *k,
2280 WL_KEYBOARD_KEY_STATE_RELEASED);
2281 }
2282
2283 seat->modifier_state = 0;
2284
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002285 weston_keyboard_set_focus(keyboard, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002286 weston_keyboard_cancel_grab(keyboard);
Derek Foreman1281a362015-07-31 16:55:32 -05002287 if (pointer)
2288 weston_pointer_cancel_grab(pointer);
Quentin Glidic85d55542017-07-21 14:02:40 +02002289
2290 if (focus) {
2291 seat->saved_kbd_focus = focus;
2292 seat->saved_kbd_focus_listener.notify =
2293 destroy_device_saved_kbd_focus;
2294 wl_signal_add(&focus->destroy_signal,
2295 &seat->saved_kbd_focus_listener);
2296 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002297}
2298
Michael Fua2bb7912013-07-23 15:51:06 +08002299WL_EXPORT void
Derek Foreman4c93c082015-04-30 16:45:41 -05002300weston_touch_set_focus(struct weston_touch *touch, struct weston_view *view)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002301{
Neil Roberts96d790e2013-09-19 17:32:00 +01002302 struct wl_list *focus_resource_list;
2303
Derek Foreman4c93c082015-04-30 16:45:41 -05002304 focus_resource_list = &touch->focus_resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002305
Derek Foreman4c93c082015-04-30 16:45:41 -05002306 if (view && touch->focus &&
2307 touch->focus->surface == view->surface) {
2308 touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002309 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002310 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002311
Derek Foreman4c93c082015-04-30 16:45:41 -05002312 wl_list_remove(&touch->focus_resource_listener.link);
2313 wl_list_init(&touch->focus_resource_listener.link);
2314 wl_list_remove(&touch->focus_view_listener.link);
2315 wl_list_init(&touch->focus_view_listener.link);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01002316
Neil Roberts96d790e2013-09-19 17:32:00 +01002317 if (!wl_list_empty(focus_resource_list)) {
Derek Foreman4c93c082015-04-30 16:45:41 -05002318 move_resources(&touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002319 focus_resource_list);
2320 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002321
Jason Ekstranda7af7042013-10-12 22:38:11 -05002322 if (view) {
Derek Foreman362656b2014-09-04 10:23:05 -05002323 struct wl_client *surface_client;
2324
2325 if (!view->surface->resource) {
Derek Foreman4c93c082015-04-30 16:45:41 -05002326 touch->focus = NULL;
Derek Foreman362656b2014-09-04 10:23:05 -05002327 return;
2328 }
2329
2330 surface_client = wl_resource_get_client(view->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +01002331 move_resources_for_client(focus_resource_list,
Derek Foreman4c93c082015-04-30 16:45:41 -05002332 &touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002333 surface_client);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01002334 wl_resource_add_destroy_listener(view->surface->resource,
Derek Foreman4c93c082015-04-30 16:45:41 -05002335 &touch->focus_resource_listener);
2336 wl_signal_add(&view->destroy_signal, &touch->focus_view_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002337 }
Derek Foreman4c93c082015-04-30 16:45:41 -05002338 touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002339}
2340
2341/**
2342 * notify_touch - emulates button touches and notifies surfaces accordingly.
2343 *
2344 * It assumes always the correct cycle sequence until it gets here: touch_down
2345 * → touch_update → ... → touch_update → touch_end. The driver is responsible
2346 * for sending along such order.
2347 *
2348 */
2349WL_EXPORT void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02002350notify_touch(struct weston_seat *seat, const struct timespec *time,
2351 int touch_id, double double_x, double double_y, int touch_type)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002352{
2353 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002354 struct weston_touch *touch = weston_seat_get_touch(seat);
Kristian Høgsberge329f362013-05-06 22:19:57 -04002355 struct weston_touch_grab *grab = touch->grab;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002356 struct weston_view *ev;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002357 wl_fixed_t sx, sy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002358 wl_fixed_t x = wl_fixed_from_double(double_x);
2359 wl_fixed_t y = wl_fixed_from_double(double_y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002360
2361 /* Update grab's global coordinates. */
Neil Roberts306fe082013-10-03 16:43:06 +01002362 if (touch_id == touch->grab_touch_id && touch_type != WL_TOUCH_UP) {
2363 touch->grab_x = x;
2364 touch->grab_y = y;
2365 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002366
2367 switch (touch_type) {
2368 case WL_TOUCH_DOWN:
2369 weston_compositor_idle_inhibit(ec);
2370
Jonas Ådahl9484b692013-12-02 22:05:03 +01002371 touch->num_tp++;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002372
Jason Ekstranda7af7042013-10-12 22:38:11 -05002373 /* the first finger down picks the view, and all further go
2374 * to that view for the remainder of the touch session i.e.
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002375 * until all touch points are up again. */
Jonas Ådahl9484b692013-12-02 22:05:03 +01002376 if (touch->num_tp == 1) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002377 ev = weston_compositor_pick_view(ec, x, y, &sx, &sy);
Derek Foreman4c93c082015-04-30 16:45:41 -05002378 weston_touch_set_focus(touch, ev);
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002379 } else if (!touch->focus) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002380 /* Unexpected condition: We have non-initial touch but
2381 * there is no focused surface.
2382 */
Chris Michael3f607d32015-10-07 11:59:49 -04002383 weston_log("touch event received with %d points down "
Jonas Ådahl9484b692013-12-02 22:05:03 +01002384 "but no surface focused\n", touch->num_tp);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002385 return;
2386 }
2387
Derek Foreman99a6a2d2015-07-15 13:00:43 -05002388 weston_compositor_run_touch_binding(ec, touch,
Kristian Høgsbergc8964012014-02-05 14:25:18 -08002389 time, touch_type);
2390
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002391 grab->interface->down(grab, time, touch_id, x, y);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002392 if (touch->num_tp == 1) {
Rusty Lynchf1407ff2013-08-08 21:13:57 -07002393 touch->grab_serial =
2394 wl_display_get_serial(ec->wl_display);
Neil Roberts306fe082013-10-03 16:43:06 +01002395 touch->grab_touch_id = touch_id;
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02002396 touch->grab_time = *time;
Rusty Lynchf1407ff2013-08-08 21:13:57 -07002397 touch->grab_x = x;
2398 touch->grab_y = y;
2399 }
2400
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002401 break;
2402 case WL_TOUCH_MOTION:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002403 ev = touch->focus;
2404 if (!ev)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002405 break;
2406
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +02002407 grab->interface->motion(grab, time, touch_id, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002408 break;
2409 case WL_TOUCH_UP:
Kristian Høgsberga30e29a2014-01-08 22:29:20 -08002410 if (touch->num_tp == 0) {
2411 /* This can happen if we start out with one or
2412 * more fingers on the touch screen, in which
2413 * case we didn't get the corresponding down
2414 * event. */
2415 weston_log("unmatched touch up event\n");
2416 break;
2417 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002418 weston_compositor_idle_release(ec);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002419 touch->num_tp--;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002420
Alexandros Frantzis27a51b82017-11-16 18:20:59 +02002421 grab->interface->up(grab, time, touch_id);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002422 if (touch->num_tp == 0)
Derek Foreman4c93c082015-04-30 16:45:41 -05002423 weston_touch_set_focus(touch, NULL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002424 break;
2425 }
2426}
2427
Jonas Ådahl1679f232014-04-12 09:39:51 +02002428WL_EXPORT void
2429notify_touch_frame(struct weston_seat *seat)
2430{
Derek Foreman1281a362015-07-31 16:55:32 -05002431 struct weston_touch *touch = weston_seat_get_touch(seat);
Jonas Ådahl1679f232014-04-12 09:39:51 +02002432 struct weston_touch_grab *grab = touch->grab;
2433
2434 grab->interface->frame(grab);
2435}
2436
Derek Foreman3cc004a2015-11-06 15:56:09 -06002437WL_EXPORT void
2438notify_touch_cancel(struct weston_seat *seat)
2439{
2440 struct weston_touch *touch = weston_seat_get_touch(seat);
2441 struct weston_touch_grab *grab = touch->grab;
2442
2443 grab->interface->cancel(grab);
2444}
2445
Pekka Paalanen8274d902014-08-06 19:36:51 +03002446static int
2447pointer_cursor_surface_get_label(struct weston_surface *surface,
2448 char *buf, size_t len)
2449{
2450 return snprintf(buf, len, "cursor");
2451}
2452
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002453static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002454pointer_cursor_surface_committed(struct weston_surface *es,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002455 int32_t dx, int32_t dy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002456{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002457 struct weston_pointer *pointer = es->committed_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002458 int x, y;
2459
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002460 if (es->width == 0)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002461 return;
2462
Jason Ekstranda7af7042013-10-12 22:38:11 -05002463 assert(es == pointer->sprite->surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002464
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002465 pointer->hotspot_x -= dx;
2466 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002467
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002468 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
2469 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002470
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002471 weston_view_set_position(pointer->sprite, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002472
2473 empty_region(&es->pending.input);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02002474 empty_region(&es->input);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002475
2476 if (!weston_surface_is_mapped(es)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002477 weston_layer_entry_insert(&es->compositor->cursor_layer.view_list,
2478 &pointer->sprite->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002479 weston_view_update_transform(pointer->sprite);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002480 es->is_mapped = true;
2481 pointer->sprite->is_mapped = true;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002482 }
2483}
2484
2485static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002486pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2487 uint32_t serial, struct wl_resource *surface_resource,
2488 int32_t x, int32_t y)
2489{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002490 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002491 struct weston_surface *surface = NULL;
2492
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +02002493 if (!pointer)
2494 return;
2495
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002496 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002497 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002498
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002499 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002500 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002501 /* pointer->focus->surface->resource can be NULL. Surfaces like the
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02002502 black_surface used in shell.c for fullscreen don't have
2503 a resource, but can still have focus */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002504 if (pointer->focus->surface->resource == NULL)
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02002505 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002506 if (wl_resource_get_client(pointer->focus->surface->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002507 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002508 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002509 return;
2510
Derek Foreman4e53c532015-03-23 10:55:32 -05002511 if (!surface) {
2512 if (pointer->sprite)
2513 pointer_unmap_sprite(pointer);
2514 return;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002515 }
2516
Jonas Ådahlb4070242015-03-18 15:08:03 +08002517 if (pointer->sprite && pointer->sprite->surface == surface &&
2518 pointer->hotspot_x == x && pointer->hotspot_y == y)
2519 return;
2520
Derek Foreman4e53c532015-03-23 10:55:32 -05002521 if (!pointer->sprite || pointer->sprite->surface != surface) {
2522 if (weston_surface_set_role(surface, "wl_pointer-cursor",
2523 resource,
2524 WL_POINTER_ERROR_ROLE) < 0)
2525 return;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002526
Derek Foreman4e53c532015-03-23 10:55:32 -05002527 if (pointer->sprite)
2528 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002529
Derek Foreman4e53c532015-03-23 10:55:32 -05002530 wl_signal_add(&surface->destroy_signal,
2531 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002532
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002533 surface->committed = pointer_cursor_surface_committed;
2534 surface->committed_private = pointer;
Derek Foreman4e53c532015-03-23 10:55:32 -05002535 weston_surface_set_label_func(surface,
2536 pointer_cursor_surface_get_label);
2537 pointer->sprite = weston_view_create(surface);
2538 }
2539
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002540 pointer->hotspot_x = x;
2541 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002542
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +02002543 if (surface->buffer_ref.buffer) {
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002544 pointer_cursor_surface_committed(surface, 0, 0);
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +02002545 weston_view_schedule_repaint(pointer->sprite);
2546 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002547}
2548
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002549static void
2550pointer_release(struct wl_client *client, struct wl_resource *resource)
2551{
2552 wl_resource_destroy(resource);
2553}
2554
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002555static const struct wl_pointer_interface pointer_interface = {
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002556 pointer_set_cursor,
2557 pointer_release
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002558};
2559
2560static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002561seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2562 uint32_t id)
2563{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002564 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002565 /* We use the pointer_state directly, which means we'll
2566 * give a wl_pointer if the seat has ever had one - even though
2567 * the spec explicitly states that this request only takes effect
2568 * if the seat has the pointer capability.
2569 *
2570 * This prevents a race between the compositor sending new
2571 * capabilities and the client trying to use the old ones.
2572 */
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002573 struct weston_pointer *pointer = seat ? seat->pointer_state : NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002574 struct wl_resource *cr;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002575 struct weston_pointer_client *pointer_client;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002576
Jason Ekstranda85118c2013-06-27 20:17:02 -05002577 cr = wl_resource_create(client, &wl_pointer_interface,
2578 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002579 if (cr == NULL) {
2580 wl_client_post_no_memory(client);
2581 return;
2582 }
2583
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002584 wl_list_init(wl_resource_get_link(cr));
2585 wl_resource_set_implementation(cr, &pointer_interface, pointer,
2586 unbind_pointer_client_resource);
2587
2588 /* If we don't have a pointer_state, the resource is inert, so there
2589 * is nothing more to set up */
2590 if (!pointer)
2591 return;
2592
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002593 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
2594 if (!pointer_client) {
2595 wl_client_post_no_memory(client);
2596 return;
2597 }
2598
2599 wl_list_insert(&pointer_client->pointer_resources,
2600 wl_resource_get_link(cr));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002601
Derek Foreman1281a362015-07-31 16:55:32 -05002602 if (pointer->focus && pointer->focus->surface->resource &&
2603 wl_resource_get_client(pointer->focus->surface->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002604 wl_fixed_t sx, sy;
2605
Derek Foreman1281a362015-07-31 16:55:32 -05002606 weston_view_from_global_fixed(pointer->focus,
2607 pointer->x,
2608 pointer->y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002609 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002610
Neil Roberts96d790e2013-09-19 17:32:00 +01002611 wl_pointer_send_enter(cr,
Derek Foreman1281a362015-07-31 16:55:32 -05002612 pointer->focus_serial,
2613 pointer->focus->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01002614 sx, sy);
Peter Hutterer87743e92016-01-18 16:38:22 +10002615 pointer_send_frame(cr);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002616 }
2617}
2618
2619static void
Alexandros Frantzis2b442482018-02-20 14:05:50 +02002620destroy_keyboard_resource(struct wl_resource *resource)
2621{
2622 struct weston_keyboard *keyboard = wl_resource_get_user_data(resource);
2623
2624 wl_list_remove(wl_resource_get_link(resource));
2625
2626 if (keyboard) {
2627 remove_input_resource_from_timestamps(resource,
2628 &keyboard->timestamps_list);
2629 }
2630}
2631
2632static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002633keyboard_release(struct wl_client *client, struct wl_resource *resource)
2634{
2635 wl_resource_destroy(resource);
2636}
2637
2638static const struct wl_keyboard_interface keyboard_interface = {
2639 keyboard_release
2640};
2641
Derek Foreman280e7dd2014-10-03 13:13:42 -05002642static bool
Neil Roberts96d790e2013-09-19 17:32:00 +01002643should_send_modifiers_to_client(struct weston_seat *seat,
2644 struct wl_client *client)
2645{
Derek Foreman1281a362015-07-31 16:55:32 -05002646 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2647 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2648
2649 if (keyboard &&
2650 keyboard->focus &&
2651 keyboard->focus->resource &&
2652 wl_resource_get_client(keyboard->focus->resource) == client)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002653 return true;
Neil Roberts96d790e2013-09-19 17:32:00 +01002654
Derek Foreman1281a362015-07-31 16:55:32 -05002655 if (pointer &&
2656 pointer->focus &&
2657 pointer->focus->surface->resource &&
2658 wl_resource_get_client(pointer->focus->surface->resource) == client)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002659 return true;
Neil Roberts96d790e2013-09-19 17:32:00 +01002660
Derek Foreman280e7dd2014-10-03 13:13:42 -05002661 return false;
Neil Roberts96d790e2013-09-19 17:32:00 +01002662}
2663
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002664static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002665seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2666 uint32_t id)
2667{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002668 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002669 /* We use the keyboard_state directly, which means we'll
2670 * give a wl_keyboard if the seat has ever had one - even though
2671 * the spec explicitly states that this request only takes effect
2672 * if the seat has the keyboard capability.
2673 *
2674 * This prevents a race between the compositor sending new
2675 * capabilities and the client trying to use the old ones.
2676 */
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002677 struct weston_keyboard *keyboard = seat ? seat->keyboard_state : NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002678 struct wl_resource *cr;
2679
Jason Ekstranda85118c2013-06-27 20:17:02 -05002680 cr = wl_resource_create(client, &wl_keyboard_interface,
2681 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002682 if (cr == NULL) {
2683 wl_client_post_no_memory(client);
2684 return;
2685 }
2686
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002687 wl_list_init(wl_resource_get_link(cr));
2688 wl_resource_set_implementation(cr, &keyboard_interface,
Alexandros Frantzis2b442482018-02-20 14:05:50 +02002689 keyboard, destroy_keyboard_resource);
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002690
2691 /* If we don't have a keyboard_state, the resource is inert, so there
2692 * is nothing more to set up */
2693 if (!keyboard)
2694 return;
2695
Neil Roberts96d790e2013-09-19 17:32:00 +01002696 /* May be moved to focused list later by either
2697 * weston_keyboard_set_focus or directly if this client is already
2698 * focused */
Derek Foreman345c9f32015-06-03 15:53:28 -05002699 wl_list_insert(&keyboard->resource_list, wl_resource_get_link(cr));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002700
Jonny Lamb66a41a02014-08-12 14:58:25 +02002701 if (wl_resource_get_version(cr) >= WL_KEYBOARD_REPEAT_INFO_SINCE_VERSION) {
2702 wl_keyboard_send_repeat_info(cr,
2703 seat->compositor->kb_repeat_rate,
2704 seat->compositor->kb_repeat_delay);
2705 }
Jasper St. Pierred8c6aeb2014-08-04 13:43:24 -04002706
Derek Foreman185d1582017-06-28 11:17:23 -05002707 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2708 keyboard->xkb_info->keymap_fd,
2709 keyboard->xkb_info->keymap_size);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002710
Neil Roberts96d790e2013-09-19 17:32:00 +01002711 if (should_send_modifiers_to_client(seat, client)) {
Derek Foreman345c9f32015-06-03 15:53:28 -05002712 send_modifiers_to_resource(keyboard,
Neil Roberts96d790e2013-09-19 17:32:00 +01002713 cr,
Derek Foreman345c9f32015-06-03 15:53:28 -05002714 keyboard->focus_serial);
Neil Roberts96d790e2013-09-19 17:32:00 +01002715 }
2716
Derek Foreman345c9f32015-06-03 15:53:28 -05002717 if (keyboard->focus && keyboard->focus->resource &&
2718 wl_resource_get_client(keyboard->focus->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01002719 struct weston_surface *surface =
Derek Foreman345c9f32015-06-03 15:53:28 -05002720 (struct weston_surface *)keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +01002721
2722 wl_list_remove(wl_resource_get_link(cr));
Derek Foreman345c9f32015-06-03 15:53:28 -05002723 wl_list_insert(&keyboard->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002724 wl_resource_get_link(cr));
2725 wl_keyboard_send_enter(cr,
Derek Foreman345c9f32015-06-03 15:53:28 -05002726 keyboard->focus_serial,
Neil Roberts96d790e2013-09-19 17:32:00 +01002727 surface->resource,
Derek Foreman345c9f32015-06-03 15:53:28 -05002728 &keyboard->keys);
Neil Roberts96d790e2013-09-19 17:32:00 +01002729
2730 /* If this is the first keyboard resource for this
2731 * client... */
Derek Foreman345c9f32015-06-03 15:53:28 -05002732 if (keyboard->focus_resource_list.prev ==
Neil Roberts96d790e2013-09-19 17:32:00 +01002733 wl_resource_get_link(cr))
2734 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002735 }
2736}
2737
2738static void
Alexandros Frantzisd7157842018-02-20 14:07:03 +02002739destroy_touch_resource(struct wl_resource *resource)
2740{
2741 struct weston_touch *touch = wl_resource_get_user_data(resource);
2742
2743 wl_list_remove(wl_resource_get_link(resource));
2744
2745 if (touch) {
2746 remove_input_resource_from_timestamps(resource,
2747 &touch->timestamps_list);
2748 }
2749}
2750
2751static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002752touch_release(struct wl_client *client, struct wl_resource *resource)
2753{
2754 wl_resource_destroy(resource);
2755}
2756
2757static const struct wl_touch_interface touch_interface = {
2758 touch_release
2759};
2760
2761static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002762seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2763 uint32_t id)
2764{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002765 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002766 /* We use the touch_state directly, which means we'll
2767 * give a wl_touch if the seat has ever had one - even though
2768 * the spec explicitly states that this request only takes effect
2769 * if the seat has the touch capability.
2770 *
2771 * This prevents a race between the compositor sending new
2772 * capabilities and the client trying to use the old ones.
2773 */
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002774 struct weston_touch *touch = seat ? seat->touch_state : NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002775 struct wl_resource *cr;
2776
Jason Ekstranda85118c2013-06-27 20:17:02 -05002777 cr = wl_resource_create(client, &wl_touch_interface,
2778 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002779 if (cr == NULL) {
2780 wl_client_post_no_memory(client);
2781 return;
2782 }
2783
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002784 wl_list_init(wl_resource_get_link(cr));
2785 wl_resource_set_implementation(cr, &touch_interface,
Alexandros Frantzisd7157842018-02-20 14:07:03 +02002786 touch, destroy_touch_resource);
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002787
2788 /* If we don't have a touch_state, the resource is inert, so there
2789 * is nothing more to set up */
2790 if (!touch)
2791 return;
2792
Derek Foreman1281a362015-07-31 16:55:32 -05002793 if (touch->focus &&
2794 wl_resource_get_client(touch->focus->surface->resource) == client) {
Chokshi, Mituld6697142015-10-09 08:28:47 +00002795 wl_list_insert(&touch->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002796 wl_resource_get_link(cr));
2797 } else {
Chokshi, Mituld6697142015-10-09 08:28:47 +00002798 wl_list_insert(&touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002799 wl_resource_get_link(cr));
2800 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002801}
2802
Quentin Glidicaab1d362016-03-13 17:49:08 +01002803static void
2804seat_release(struct wl_client *client, struct wl_resource *resource)
2805{
2806 wl_resource_destroy(resource);
2807}
2808
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002809static const struct wl_seat_interface seat_interface = {
2810 seat_get_pointer,
2811 seat_get_keyboard,
2812 seat_get_touch,
Quentin Glidicaab1d362016-03-13 17:49:08 +01002813 seat_release,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002814};
2815
2816static void
2817bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2818{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002819 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002820 struct wl_resource *resource;
2821 enum wl_seat_capability caps = 0;
2822
Jason Ekstranda85118c2013-06-27 20:17:02 -05002823 resource = wl_resource_create(client,
Derek Foreman1909c102015-11-26 14:17:47 -06002824 &wl_seat_interface, version, id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05002825 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05002826 wl_resource_set_implementation(resource, &seat_interface, data,
2827 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002828
Derek Foreman1281a362015-07-31 16:55:32 -05002829 if (weston_seat_get_pointer(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002830 caps |= WL_SEAT_CAPABILITY_POINTER;
Derek Foreman1281a362015-07-31 16:55:32 -05002831 if (weston_seat_get_keyboard(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002832 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Derek Foreman1281a362015-07-31 16:55:32 -05002833 if (weston_seat_get_touch(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002834 caps |= WL_SEAT_CAPABILITY_TOUCH;
2835
2836 wl_seat_send_capabilities(resource, caps);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04002837 if (version >= WL_SEAT_NAME_SINCE_VERSION)
Rob Bradforde445ae62013-05-31 18:09:51 +01002838 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002839}
2840
Jonas Ådahl30d61d82014-10-22 21:21:17 +02002841static void
2842relative_pointer_destroy(struct wl_client *client,
2843 struct wl_resource *resource)
2844{
2845 wl_resource_destroy(resource);
2846}
2847
2848static const struct zwp_relative_pointer_v1_interface relative_pointer_interface = {
2849 relative_pointer_destroy
2850};
2851
2852static void
2853relative_pointer_manager_destroy(struct wl_client *client,
2854 struct wl_resource *resource)
2855{
2856 wl_resource_destroy(resource);
2857}
2858
2859static void
2860relative_pointer_manager_get_relative_pointer(struct wl_client *client,
2861 struct wl_resource *resource,
2862 uint32_t id,
2863 struct wl_resource *pointer_resource)
2864{
2865 struct weston_pointer *pointer =
2866 wl_resource_get_user_data(pointer_resource);
2867 struct weston_pointer_client *pointer_client;
2868 struct wl_resource *cr;
2869
2870 cr = wl_resource_create(client, &zwp_relative_pointer_v1_interface,
2871 wl_resource_get_version(resource), id);
2872 if (cr == NULL) {
2873 wl_client_post_no_memory(client);
2874 return;
2875 }
2876
2877 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
2878 if (!pointer_client) {
2879 wl_client_post_no_memory(client);
2880 return;
2881 }
2882
2883 wl_list_insert(&pointer_client->relative_pointer_resources,
2884 wl_resource_get_link(cr));
2885 wl_resource_set_implementation(cr, &relative_pointer_interface,
2886 pointer,
2887 unbind_pointer_client_resource);
2888}
2889
2890static const struct zwp_relative_pointer_manager_v1_interface relative_pointer_manager = {
2891 relative_pointer_manager_destroy,
2892 relative_pointer_manager_get_relative_pointer,
2893};
2894
2895static void
2896bind_relative_pointer_manager(struct wl_client *client, void *data,
2897 uint32_t version, uint32_t id)
2898{
2899 struct weston_compositor *compositor = data;
2900 struct wl_resource *resource;
2901
2902 resource = wl_resource_create(client,
2903 &zwp_relative_pointer_manager_v1_interface,
2904 1, id);
2905
2906 wl_resource_set_implementation(resource, &relative_pointer_manager,
2907 compositor,
2908 NULL);
2909}
2910
Giulio Camuffo0358af42016-06-02 21:48:08 +03002911WL_EXPORT int
2912weston_compositor_set_xkb_rule_names(struct weston_compositor *ec,
2913 struct xkb_rule_names *names)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002914{
2915 if (ec->xkb_context == NULL) {
2916 ec->xkb_context = xkb_context_new(0);
2917 if (ec->xkb_context == NULL) {
2918 weston_log("failed to create XKB context\n");
2919 return -1;
2920 }
2921 }
2922
2923 if (names)
2924 ec->xkb_names = *names;
2925 if (!ec->xkb_names.rules)
2926 ec->xkb_names.rules = strdup("evdev");
2927 if (!ec->xkb_names.model)
2928 ec->xkb_names.model = strdup("pc105");
2929 if (!ec->xkb_names.layout)
2930 ec->xkb_names.layout = strdup("us");
2931
2932 return 0;
2933}
2934
Stefan Schmidtfda26522013-09-17 10:54:09 +01002935static void
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002936weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002937{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002938 if (--xkb_info->ref_count > 0)
2939 return;
2940
Ran Benitac9c74152014-08-19 23:59:52 +03002941 xkb_keymap_unref(xkb_info->keymap);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002942
2943 if (xkb_info->keymap_area)
2944 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2945 if (xkb_info->keymap_fd >= 0)
2946 close(xkb_info->keymap_fd);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002947 free(xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002948}
2949
2950void
2951weston_compositor_xkb_destroy(struct weston_compositor *ec)
2952{
2953 free((char *) ec->xkb_names.rules);
2954 free((char *) ec->xkb_names.model);
2955 free((char *) ec->xkb_names.layout);
2956 free((char *) ec->xkb_names.variant);
2957 free((char *) ec->xkb_names.options);
Stefan Schmidtfda26522013-09-17 10:54:09 +01002958
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002959 if (ec->xkb_info)
2960 weston_xkb_info_destroy(ec->xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002961 xkb_context_unref(ec->xkb_context);
2962}
2963
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002964static struct weston_xkb_info *
2965weston_xkb_info_create(struct xkb_keymap *keymap)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002966{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002967 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
2968 if (xkb_info == NULL)
2969 return NULL;
2970
Ran Benita2e1968f2014-08-19 23:59:51 +03002971 xkb_info->keymap = xkb_keymap_ref(keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002972 xkb_info->ref_count = 1;
2973
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002974 char *keymap_str;
2975
Ran Benita2e1968f2014-08-19 23:59:51 +03002976 xkb_info->shift_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2977 XKB_MOD_NAME_SHIFT);
2978 xkb_info->caps_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2979 XKB_MOD_NAME_CAPS);
2980 xkb_info->ctrl_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2981 XKB_MOD_NAME_CTRL);
2982 xkb_info->alt_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2983 XKB_MOD_NAME_ALT);
2984 xkb_info->mod2_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2985 "Mod2");
2986 xkb_info->mod3_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2987 "Mod3");
2988 xkb_info->super_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2989 XKB_MOD_NAME_LOGO);
2990 xkb_info->mod5_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2991 "Mod5");
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002992
Ran Benita2e1968f2014-08-19 23:59:51 +03002993 xkb_info->num_led = xkb_keymap_led_get_index(xkb_info->keymap,
2994 XKB_LED_NAME_NUM);
2995 xkb_info->caps_led = xkb_keymap_led_get_index(xkb_info->keymap,
2996 XKB_LED_NAME_CAPS);
2997 xkb_info->scroll_led = xkb_keymap_led_get_index(xkb_info->keymap,
2998 XKB_LED_NAME_SCROLL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002999
Ran Benita2e1968f2014-08-19 23:59:51 +03003000 keymap_str = xkb_keymap_get_as_string(xkb_info->keymap,
3001 XKB_KEYMAP_FORMAT_TEXT_V1);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003002 if (keymap_str == NULL) {
3003 weston_log("failed to get string version of keymap\n");
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003004 goto err_keymap;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003005 }
3006 xkb_info->keymap_size = strlen(keymap_str) + 1;
3007
3008 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
3009 if (xkb_info->keymap_fd < 0) {
3010 weston_log("creating a keymap file for %lu bytes failed: %m\n",
3011 (unsigned long) xkb_info->keymap_size);
3012 goto err_keymap_str;
3013 }
3014
3015 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
3016 PROT_READ | PROT_WRITE,
3017 MAP_SHARED, xkb_info->keymap_fd, 0);
3018 if (xkb_info->keymap_area == MAP_FAILED) {
3019 weston_log("failed to mmap() %lu bytes\n",
3020 (unsigned long) xkb_info->keymap_size);
3021 goto err_dev_zero;
3022 }
3023 strcpy(xkb_info->keymap_area, keymap_str);
3024 free(keymap_str);
3025
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003026 return xkb_info;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003027
3028err_dev_zero:
3029 close(xkb_info->keymap_fd);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003030err_keymap_str:
3031 free(keymap_str);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003032err_keymap:
Ran Benita2e1968f2014-08-19 23:59:51 +03003033 xkb_keymap_unref(xkb_info->keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003034 free(xkb_info);
3035 return NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003036}
3037
3038static int
3039weston_compositor_build_global_keymap(struct weston_compositor *ec)
3040{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003041 struct xkb_keymap *keymap;
3042
3043 if (ec->xkb_info != NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003044 return 0;
3045
Ran Benita2e1968f2014-08-19 23:59:51 +03003046 keymap = xkb_keymap_new_from_names(ec->xkb_context,
3047 &ec->xkb_names,
3048 0);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003049 if (keymap == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003050 weston_log("failed to compile global XKB keymap\n");
3051 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
3052 "options %s\n",
3053 ec->xkb_names.rules, ec->xkb_names.model,
3054 ec->xkb_names.layout, ec->xkb_names.variant,
3055 ec->xkb_names.options);
3056 return -1;
3057 }
3058
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003059 ec->xkb_info = weston_xkb_info_create(keymap);
Rui Matos73d93952013-10-24 19:28:41 +02003060 xkb_keymap_unref(keymap);
Stefan Schmidtfda26522013-09-17 10:54:09 +01003061 if (ec->xkb_info == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003062 return -1;
3063
3064 return 0;
3065}
3066
Rui Matos65196bc2013-10-10 19:44:19 +02003067WL_EXPORT void
3068weston_seat_update_keymap(struct weston_seat *seat, struct xkb_keymap *keymap)
3069{
Derek Foreman1281a362015-07-31 16:55:32 -05003070 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
3071
3072 if (!keyboard || !keymap)
Rui Matos65196bc2013-10-10 19:44:19 +02003073 return;
3074
Derek Foreman1281a362015-07-31 16:55:32 -05003075 xkb_keymap_unref(keyboard->pending_keymap);
3076 keyboard->pending_keymap = xkb_keymap_ref(keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02003077
Derek Foreman1281a362015-07-31 16:55:32 -05003078 if (keyboard->keys.size == 0)
Rui Matos65196bc2013-10-10 19:44:19 +02003079 update_keymap(seat);
Rui Matos65196bc2013-10-10 19:44:19 +02003080}
3081
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003082WL_EXPORT int
3083weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
3084{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003085 struct weston_keyboard *keyboard;
3086
Derek Foreman1281a362015-07-31 16:55:32 -05003087 if (seat->keyboard_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003088 seat->keyboard_device_count += 1;
3089 if (seat->keyboard_device_count == 1)
3090 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003091 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003092 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003093
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003094 keyboard = weston_keyboard_create();
3095 if (keyboard == NULL) {
3096 weston_log("failed to allocate weston keyboard struct\n");
3097 return -1;
3098 }
3099
Derek Foreman185d1582017-06-28 11:17:23 -05003100 if (keymap != NULL) {
3101 keyboard->xkb_info = weston_xkb_info_create(keymap);
3102 if (keyboard->xkb_info == NULL)
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02003103 goto err;
Derek Foreman185d1582017-06-28 11:17:23 -05003104 } else {
3105 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
3106 goto err;
3107 keyboard->xkb_info = seat->compositor->xkb_info;
3108 keyboard->xkb_info->ref_count++;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01003109 }
Derek Foreman185d1582017-06-28 11:17:23 -05003110
3111 keyboard->xkb_state.state = xkb_state_new(keyboard->xkb_info->keymap);
3112 if (keyboard->xkb_state.state == NULL) {
3113 weston_log("failed to initialise XKB state\n");
3114 goto err;
3115 }
3116
3117 keyboard->xkb_state.leds = 0;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01003118
Derek Foreman1281a362015-07-31 16:55:32 -05003119 seat->keyboard_state = keyboard;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02003120 seat->keyboard_device_count = 1;
3121 keyboard->seat = seat;
3122
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003123 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003124
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003125 return 0;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02003126
3127err:
3128 if (keyboard->xkb_info)
3129 weston_xkb_info_destroy(keyboard->xkb_info);
3130 free(keyboard);
3131
3132 return -1;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003133}
3134
Jonas Ådahl91fed542013-12-03 09:14:27 +01003135static void
3136weston_keyboard_reset_state(struct weston_keyboard *keyboard)
3137{
3138 struct weston_seat *seat = keyboard->seat;
3139 struct xkb_state *state;
3140
Derek Foreman185d1582017-06-28 11:17:23 -05003141 state = xkb_state_new(keyboard->xkb_info->keymap);
3142 if (!state) {
3143 weston_log("failed to reset XKB state\n");
3144 return;
Jonas Ådahl91fed542013-12-03 09:14:27 +01003145 }
Derek Foreman185d1582017-06-28 11:17:23 -05003146 xkb_state_unref(keyboard->xkb_state.state);
3147 keyboard->xkb_state.state = state;
3148
3149 keyboard->xkb_state.leds = 0;
Jonas Ådahl91fed542013-12-03 09:14:27 +01003150
3151 seat->modifier_state = 0;
3152}
3153
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003154WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003155weston_seat_release_keyboard(struct weston_seat *seat)
3156{
3157 seat->keyboard_device_count--;
Derek Foremand621df22014-11-19 11:04:12 -06003158 assert(seat->keyboard_device_count >= 0);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003159 if (seat->keyboard_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05003160 weston_keyboard_set_focus(seat->keyboard_state, NULL);
3161 weston_keyboard_cancel_grab(seat->keyboard_state);
3162 weston_keyboard_reset_state(seat->keyboard_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003163 seat_send_updated_caps(seat);
3164 }
3165}
3166
3167WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003168weston_seat_init_pointer(struct weston_seat *seat)
3169{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003170 struct weston_pointer *pointer;
3171
Derek Foreman1281a362015-07-31 16:55:32 -05003172 if (seat->pointer_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003173 seat->pointer_device_count += 1;
3174 if (seat->pointer_device_count == 1)
3175 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003176 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003177 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003178
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003179 pointer = weston_pointer_create(seat);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003180 if (pointer == NULL)
3181 return;
3182
Derek Foreman1281a362015-07-31 16:55:32 -05003183 seat->pointer_state = pointer;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003184 seat->pointer_device_count = 1;
3185 pointer->seat = seat;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003186
3187 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003188}
3189
3190WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003191weston_seat_release_pointer(struct weston_seat *seat)
3192{
Derek Foreman1281a362015-07-31 16:55:32 -05003193 struct weston_pointer *pointer = seat->pointer_state;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003194
3195 seat->pointer_device_count--;
3196 if (seat->pointer_device_count == 0) {
Derek Foremanf9318d12015-05-11 15:40:11 -05003197 weston_pointer_clear_focus(pointer);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003198 weston_pointer_cancel_grab(pointer);
Jonas Ådahl630bae82013-10-17 23:04:06 +02003199
Jonas Ådahla4932742013-10-17 23:04:07 +02003200 if (pointer->sprite)
3201 pointer_unmap_sprite(pointer);
3202
Jonas Ådahl3e12e632013-12-02 22:05:05 +01003203 weston_pointer_reset_state(pointer);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003204 seat_send_updated_caps(seat);
Derek Foremanfd5ca512015-01-07 15:00:25 -06003205
3206 /* seat->pointer is intentionally not destroyed so that
3207 * a newly attached pointer on this seat will retain
3208 * the previous cursor co-ordinates.
3209 */
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003210 }
3211}
3212
3213WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003214weston_seat_init_touch(struct weston_seat *seat)
3215{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003216 struct weston_touch *touch;
3217
Derek Foreman1281a362015-07-31 16:55:32 -05003218 if (seat->touch_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003219 seat->touch_device_count += 1;
3220 if (seat->touch_device_count == 1)
3221 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003222 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003223 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003224
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003225 touch = weston_touch_create();
3226 if (touch == NULL)
3227 return;
3228
Derek Foreman1281a362015-07-31 16:55:32 -05003229 seat->touch_state = touch;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003230 seat->touch_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003231 touch->seat = seat;
3232
3233 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003234}
3235
3236WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003237weston_seat_release_touch(struct weston_seat *seat)
3238{
3239 seat->touch_device_count--;
3240 if (seat->touch_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05003241 weston_touch_set_focus(seat->touch_state, NULL);
3242 weston_touch_cancel_grab(seat->touch_state);
3243 weston_touch_reset_state(seat->touch_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003244 seat_send_updated_caps(seat);
3245 }
3246}
3247
3248WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003249weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
3250 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003251{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003252 memset(seat, 0, sizeof *seat);
3253
Kristian Høgsberge3148752013-05-06 23:19:49 -04003254 seat->selection_data_source = NULL;
3255 wl_list_init(&seat->base_resource_list);
3256 wl_signal_init(&seat->selection_signal);
3257 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04003258 wl_signal_init(&seat->destroy_signal);
Jason Ekstranda4ab5422014-04-02 19:53:45 -05003259 wl_signal_init(&seat->updated_caps_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003260
Peter Hutterer87743e92016-01-18 16:38:22 +10003261 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 5,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003262 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003263
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003264 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003265 seat->modifier_state = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003266 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003267
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003268 wl_list_insert(ec->seat_list.prev, &seat->link);
3269
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003270 clipboard_create(seat);
3271
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003272 wl_signal_emit(&ec->seat_created_signal, seat);
3273}
3274
3275WL_EXPORT void
3276weston_seat_release(struct weston_seat *seat)
3277{
Alexandros Frantzis8480d132018-02-15 13:07:09 +02003278 struct wl_resource *resource;
3279
3280 wl_resource_for_each(resource, &seat->base_resource_list) {
3281 wl_resource_set_user_data(resource, NULL);
3282 }
3283
3284 wl_resource_for_each(resource, &seat->drag_resource_list) {
3285 wl_resource_set_user_data(resource, NULL);
3286 }
3287
3288 wl_list_remove(&seat->base_resource_list);
3289 wl_list_remove(&seat->drag_resource_list);
3290
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003291 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003292
Jonas Ådahl1afb2382014-01-03 19:46:51 +01003293 if (seat->saved_kbd_focus)
3294 wl_list_remove(&seat->saved_kbd_focus_listener.link);
3295
Derek Foreman1281a362015-07-31 16:55:32 -05003296 if (seat->pointer_state)
3297 weston_pointer_destroy(seat->pointer_state);
3298 if (seat->keyboard_state)
3299 weston_keyboard_destroy(seat->keyboard_state);
3300 if (seat->touch_state)
3301 weston_touch_destroy(seat->touch_state);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003302
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003303 free (seat->seat_name);
3304
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003305 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04003306
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003307 wl_signal_emit(&seat->destroy_signal, seat);
3308}
Derek Foreman1281a362015-07-31 16:55:32 -05003309
3310/** Get a seat's keyboard pointer
3311 *
3312 * \param seat The seat to query
3313 * \return The seat's keyboard pointer, or NULL if no keyboard is present
3314 *
3315 * The keyboard pointer for a seat isn't freed when all keyboards are removed,
3316 * so it should only be used when the seat's keyboard_device_count is greater
3317 * than zero. This function does that test and only returns a pointer
3318 * when a keyboard is present.
3319 */
3320WL_EXPORT struct weston_keyboard *
3321weston_seat_get_keyboard(struct weston_seat *seat)
3322{
3323 if (!seat)
3324 return NULL;
3325
3326 if (seat->keyboard_device_count)
3327 return seat->keyboard_state;
3328
3329 return NULL;
3330}
3331
3332/** Get a seat's pointer pointer
3333 *
3334 * \param seat The seat to query
3335 * \return The seat's pointer pointer, or NULL if no pointer device is present
3336 *
3337 * The pointer pointer for a seat isn't freed when all mice are removed,
3338 * so it should only be used when the seat's pointer_device_count is greater
3339 * than zero. This function does that test and only returns a pointer
3340 * when a pointing device is present.
3341 */
3342WL_EXPORT struct weston_pointer *
3343weston_seat_get_pointer(struct weston_seat *seat)
3344{
3345 if (!seat)
3346 return NULL;
3347
3348 if (seat->pointer_device_count)
3349 return seat->pointer_state;
3350
3351 return NULL;
3352}
3353
Jonas Ådahld3414f22016-07-22 17:56:31 +08003354static const struct zwp_locked_pointer_v1_interface locked_pointer_interface;
3355static const struct zwp_confined_pointer_v1_interface confined_pointer_interface;
3356
3357static enum pointer_constraint_type
3358pointer_constraint_get_type(struct weston_pointer_constraint *constraint)
3359{
3360 if (wl_resource_instance_of(constraint->resource,
3361 &zwp_locked_pointer_v1_interface,
3362 &locked_pointer_interface)) {
3363 return POINTER_CONSTRAINT_TYPE_LOCK;
3364 } else if (wl_resource_instance_of(constraint->resource,
3365 &zwp_confined_pointer_v1_interface,
3366 &confined_pointer_interface)) {
3367 return POINTER_CONSTRAINT_TYPE_CONFINE;
3368 }
3369
3370 abort();
3371 return 0;
3372}
3373
3374static void
3375pointer_constraint_notify_activated(struct weston_pointer_constraint *constraint)
3376{
3377 struct wl_resource *resource = constraint->resource;
3378
3379 switch (pointer_constraint_get_type(constraint)) {
3380 case POINTER_CONSTRAINT_TYPE_LOCK:
3381 zwp_locked_pointer_v1_send_locked(resource);
3382 break;
3383 case POINTER_CONSTRAINT_TYPE_CONFINE:
3384 zwp_confined_pointer_v1_send_confined(resource);
3385 break;
3386 }
3387}
3388
3389static void
3390pointer_constraint_notify_deactivated(struct weston_pointer_constraint *constraint)
3391{
3392 struct wl_resource *resource = constraint->resource;
3393
3394 switch (pointer_constraint_get_type(constraint)) {
3395 case POINTER_CONSTRAINT_TYPE_LOCK:
3396 zwp_locked_pointer_v1_send_unlocked(resource);
3397 break;
3398 case POINTER_CONSTRAINT_TYPE_CONFINE:
3399 zwp_confined_pointer_v1_send_unconfined(resource);
3400 break;
3401 }
3402}
3403
3404static struct weston_pointer_constraint *
3405get_pointer_constraint_for_pointer(struct weston_surface *surface,
3406 struct weston_pointer *pointer)
3407{
3408 struct weston_pointer_constraint *constraint;
3409
3410 wl_list_for_each(constraint, &surface->pointer_constraints, link) {
3411 if (constraint->pointer == pointer)
3412 return constraint;
3413 }
3414
3415 return NULL;
3416}
3417
Derek Foreman1281a362015-07-31 16:55:32 -05003418/** Get a seat's touch pointer
3419 *
3420 * \param seat The seat to query
3421 * \return The seat's touch pointer, or NULL if no touch device is present
3422 *
3423 * The touch pointer for a seat isn't freed when all touch devices are removed,
3424 * so it should only be used when the seat's touch_device_count is greater
3425 * than zero. This function does that test and only returns a pointer
3426 * when a touch device is present.
3427 */
3428WL_EXPORT struct weston_touch *
3429weston_seat_get_touch(struct weston_seat *seat)
3430{
3431 if (!seat)
3432 return NULL;
3433
3434 if (seat->touch_device_count)
3435 return seat->touch_state;
3436
3437 return NULL;
3438}
Bryce Harrington24f917e2016-06-29 19:04:07 -07003439
3440/** Sets the keyboard focus to the given surface
3441 *
3442 * \param seat The seat to query
3443 */
3444WL_EXPORT void
3445weston_seat_set_keyboard_focus(struct weston_seat *seat,
3446 struct weston_surface *surface)
3447{
3448 struct weston_compositor *compositor = seat->compositor;
3449 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003450 struct weston_surface_activation_data activation_data;
Bryce Harrington24f917e2016-06-29 19:04:07 -07003451
Jonas Ådahlef8e1c32016-03-15 20:28:51 +08003452 if (keyboard && keyboard->focus != surface) {
Bryce Harrington24f917e2016-06-29 19:04:07 -07003453 weston_keyboard_set_focus(keyboard, surface);
3454 wl_data_device_set_keyboard_focus(seat);
3455 }
3456
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003457 inc_activate_serial(compositor);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003458
3459 activation_data = (struct weston_surface_activation_data) {
3460 .surface = surface,
3461 .seat = seat,
3462 };
3463 wl_signal_emit(&compositor->activate_signal, &activation_data);
Bryce Harrington24f917e2016-06-29 19:04:07 -07003464}
Jonas Ådahl30d61d82014-10-22 21:21:17 +02003465
Jonas Ådahld3414f22016-07-22 17:56:31 +08003466static void
3467enable_pointer_constraint(struct weston_pointer_constraint *constraint,
3468 struct weston_view *view)
3469{
3470 assert(constraint->view == NULL);
3471 constraint->view = view;
3472 pointer_constraint_notify_activated(constraint);
3473 weston_pointer_start_grab(constraint->pointer, &constraint->grab);
3474 wl_list_remove(&constraint->surface_destroy_listener.link);
3475 wl_list_init(&constraint->surface_destroy_listener.link);
3476}
3477
3478static bool
3479is_pointer_constraint_enabled(struct weston_pointer_constraint *constraint)
3480{
3481 return constraint->view != NULL;
3482}
3483
3484static void
3485weston_pointer_constraint_disable(struct weston_pointer_constraint *constraint)
3486{
3487 constraint->view = NULL;
3488 pointer_constraint_notify_deactivated(constraint);
3489 weston_pointer_end_grab(constraint->grab.pointer);
3490}
3491
3492void
3493weston_pointer_constraint_destroy(struct weston_pointer_constraint *constraint)
3494{
3495 if (is_pointer_constraint_enabled(constraint))
3496 weston_pointer_constraint_disable(constraint);
3497
3498 wl_list_remove(&constraint->pointer_destroy_listener.link);
3499 wl_list_remove(&constraint->surface_destroy_listener.link);
3500 wl_list_remove(&constraint->surface_commit_listener.link);
3501 wl_list_remove(&constraint->surface_activate_listener.link);
3502
3503 wl_resource_set_user_data(constraint->resource, NULL);
3504 pixman_region32_fini(&constraint->region);
3505 wl_list_remove(&constraint->link);
3506 free(constraint);
3507}
3508
3509static void
3510disable_pointer_constraint(struct weston_pointer_constraint *constraint)
3511{
3512 switch (constraint->lifetime) {
3513 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT:
3514 weston_pointer_constraint_destroy(constraint);
3515 break;
3516 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT:
3517 weston_pointer_constraint_disable(constraint);
3518 break;
3519 }
3520}
3521
3522static bool
3523is_within_constraint_region(struct weston_pointer_constraint *constraint,
3524 wl_fixed_t sx, wl_fixed_t sy)
3525{
3526 struct weston_surface *surface = constraint->surface;
3527 pixman_region32_t constraint_region;
3528 bool result;
3529
3530 pixman_region32_init(&constraint_region);
3531 pixman_region32_intersect(&constraint_region,
3532 &surface->input,
3533 &constraint->region);
3534 result = pixman_region32_contains_point(&constraint_region,
3535 wl_fixed_to_int(sx),
3536 wl_fixed_to_int(sy),
3537 NULL);
3538 pixman_region32_fini(&constraint_region);
3539
3540 return result;
3541}
3542
3543static void
3544maybe_enable_pointer_constraint(struct weston_pointer_constraint *constraint)
3545{
3546 struct weston_surface *surface = constraint->surface;
3547 struct weston_view *vit;
3548 struct weston_view *view = NULL;
3549 struct weston_pointer *pointer = constraint->pointer;
3550 struct weston_keyboard *keyboard;
3551 struct weston_seat *seat = pointer->seat;
3552 int32_t x, y;
3553
3554 /* Postpone if no view of the surface was most recently clicked. */
3555 wl_list_for_each(vit, &surface->views, surface_link) {
3556 if (vit->click_to_activate_serial ==
3557 surface->compositor->activate_serial) {
3558 view = vit;
3559 }
3560 }
3561 if (view == NULL)
3562 return;
3563
3564 /* Postpone if surface doesn't have keyboard focus. */
3565 keyboard = weston_seat_get_keyboard(seat);
3566 if (!keyboard || keyboard->focus != surface)
3567 return;
3568
3569 /* Postpone constraint if the pointer is not within the
3570 * constraint region.
3571 */
3572 weston_view_from_global(view,
3573 wl_fixed_to_int(pointer->x),
3574 wl_fixed_to_int(pointer->y),
3575 &x, &y);
3576 if (!is_within_constraint_region(constraint,
3577 wl_fixed_from_int(x),
3578 wl_fixed_from_int(y)))
3579 return;
3580
3581 enable_pointer_constraint(constraint, view);
3582}
3583
3584static void
3585locked_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
3586{
3587}
3588
3589static void
3590locked_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02003591 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003592 struct weston_pointer_motion_event *event)
3593{
Quentin Glidiccde13452016-08-12 10:41:32 +02003594 pointer_send_relative_motion(grab->pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08003595}
3596
3597static void
3598locked_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003599 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003600 uint32_t button,
3601 uint32_t state_w)
3602{
3603 weston_pointer_send_button(grab->pointer, time, button, state_w);
3604}
3605
3606static void
3607locked_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02003608 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003609 struct weston_pointer_axis_event *event)
3610{
3611 weston_pointer_send_axis(grab->pointer, time, event);
3612}
3613
3614static void
3615locked_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
3616 uint32_t source)
3617{
3618 weston_pointer_send_axis_source(grab->pointer, source);
3619}
3620
3621static void
3622locked_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
3623{
3624 weston_pointer_send_frame(grab->pointer);
3625}
3626
3627static void
3628locked_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
3629{
3630 struct weston_pointer_constraint *constraint =
3631 container_of(grab, struct weston_pointer_constraint, grab);
3632
3633 disable_pointer_constraint(constraint);
3634}
3635
3636static const struct weston_pointer_grab_interface
3637 locked_pointer_grab_interface = {
3638 locked_pointer_grab_pointer_focus,
3639 locked_pointer_grab_pointer_motion,
3640 locked_pointer_grab_pointer_button,
3641 locked_pointer_grab_pointer_axis,
3642 locked_pointer_grab_pointer_axis_source,
3643 locked_pointer_grab_pointer_frame,
3644 locked_pointer_grab_pointer_cancel,
3645};
3646
3647static void
3648pointer_constraint_constrain_resource_destroyed(struct wl_resource *resource)
3649{
3650 struct weston_pointer_constraint *constraint =
3651 wl_resource_get_user_data(resource);
3652
3653 if (!constraint)
3654 return;
3655
3656 weston_pointer_constraint_destroy(constraint);
3657}
3658
3659static void
3660pointer_constraint_surface_activate(struct wl_listener *listener, void *data)
3661{
3662 struct weston_surface_activation_data *activation = data;
3663 struct weston_pointer *pointer;
3664 struct weston_surface *focus = activation->surface;
3665 struct weston_pointer_constraint *constraint =
3666 container_of(listener, struct weston_pointer_constraint,
3667 surface_activate_listener);
3668 bool is_constraint_surface;
3669
3670 pointer = weston_seat_get_pointer(activation->seat);
3671 if (!pointer)
3672 return;
3673
3674 is_constraint_surface =
3675 get_pointer_constraint_for_pointer(focus, pointer) == constraint;
3676
3677 if (is_constraint_surface &&
3678 !is_pointer_constraint_enabled(constraint))
3679 maybe_enable_pointer_constraint(constraint);
3680 else if (!is_constraint_surface &&
3681 is_pointer_constraint_enabled(constraint))
3682 disable_pointer_constraint(constraint);
3683}
3684
3685static void
3686pointer_constraint_pointer_destroyed(struct wl_listener *listener, void *data)
3687{
3688 struct weston_pointer_constraint *constraint =
3689 container_of(listener, struct weston_pointer_constraint,
3690 pointer_destroy_listener);
3691
3692 weston_pointer_constraint_destroy(constraint);
3693}
3694
3695static void
3696pointer_constraint_surface_destroyed(struct wl_listener *listener, void *data)
3697{
3698 struct weston_pointer_constraint *constraint =
3699 container_of(listener, struct weston_pointer_constraint,
3700 surface_destroy_listener);
3701
3702 weston_pointer_constraint_destroy(constraint);
3703}
3704
3705static void
3706pointer_constraint_surface_committed(struct wl_listener *listener, void *data)
3707{
3708 struct weston_pointer_constraint *constraint =
3709 container_of(listener, struct weston_pointer_constraint,
3710 surface_commit_listener);
3711
3712 if (constraint->region_is_pending) {
3713 constraint->region_is_pending = false;
3714 pixman_region32_copy(&constraint->region,
3715 &constraint->region_pending);
3716 pixman_region32_fini(&constraint->region_pending);
3717 pixman_region32_init(&constraint->region_pending);
3718 }
3719
3720 if (constraint->hint_is_pending) {
3721 constraint->hint_is_pending = false;
3722
3723 constraint->hint_is_pending = true;
3724 constraint->hint_x = constraint->hint_x_pending;
3725 constraint->hint_y = constraint->hint_y_pending;
3726 }
3727
3728 if (pointer_constraint_get_type(constraint) ==
3729 POINTER_CONSTRAINT_TYPE_CONFINE &&
3730 is_pointer_constraint_enabled(constraint))
3731 maybe_warp_confined_pointer(constraint);
3732}
3733
3734static struct weston_pointer_constraint *
3735weston_pointer_constraint_create(struct weston_surface *surface,
3736 struct weston_pointer *pointer,
3737 struct weston_region *region,
3738 enum zwp_pointer_constraints_v1_lifetime lifetime,
3739 struct wl_resource *cr,
3740 const struct weston_pointer_grab_interface *grab_interface)
3741{
3742 struct weston_pointer_constraint *constraint;
3743
3744 constraint = zalloc(sizeof *constraint);
3745 if (!constraint)
3746 return NULL;
3747
3748 constraint->lifetime = lifetime;
3749 pixman_region32_init(&constraint->region);
3750 pixman_region32_init(&constraint->region_pending);
3751 wl_list_insert(&surface->pointer_constraints, &constraint->link);
3752 constraint->surface = surface;
3753 constraint->pointer = pointer;
3754 constraint->resource = cr;
3755 constraint->grab.interface = grab_interface;
3756 if (region) {
3757 pixman_region32_copy(&constraint->region,
3758 &region->region);
3759 } else {
3760 pixman_region32_fini(&constraint->region);
3761 region_init_infinite(&constraint->region);
3762 }
3763
3764 constraint->surface_activate_listener.notify =
3765 pointer_constraint_surface_activate;
3766 constraint->surface_destroy_listener.notify =
3767 pointer_constraint_surface_destroyed;
3768 constraint->surface_commit_listener.notify =
3769 pointer_constraint_surface_committed;
3770 constraint->pointer_destroy_listener.notify =
3771 pointer_constraint_pointer_destroyed;
3772
3773 wl_signal_add(&surface->compositor->activate_signal,
3774 &constraint->surface_activate_listener);
3775 wl_signal_add(&pointer->destroy_signal,
3776 &constraint->pointer_destroy_listener);
3777 wl_signal_add(&surface->destroy_signal,
3778 &constraint->surface_destroy_listener);
3779 wl_signal_add(&surface->commit_signal,
3780 &constraint->surface_commit_listener);
3781
3782 return constraint;
3783}
3784
3785static void
3786init_pointer_constraint(struct wl_resource *pointer_constraints_resource,
3787 uint32_t id,
3788 struct weston_surface *surface,
3789 struct weston_pointer *pointer,
3790 struct weston_region *region,
3791 enum zwp_pointer_constraints_v1_lifetime lifetime,
3792 const struct wl_interface *interface,
3793 const void *implementation,
3794 const struct weston_pointer_grab_interface *grab_interface)
3795{
3796 struct wl_client *client =
3797 wl_resource_get_client(pointer_constraints_resource);
3798 struct wl_resource *cr;
3799 struct weston_pointer_constraint *constraint;
3800
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02003801 if (pointer && get_pointer_constraint_for_pointer(surface, pointer)) {
Jonas Ådahld3414f22016-07-22 17:56:31 +08003802 wl_resource_post_error(pointer_constraints_resource,
3803 ZWP_POINTER_CONSTRAINTS_V1_ERROR_ALREADY_CONSTRAINED,
3804 "the pointer has a lock/confine request on this surface");
3805 return;
3806 }
3807
3808 cr = wl_resource_create(client, interface,
3809 wl_resource_get_version(pointer_constraints_resource),
3810 id);
3811 if (cr == NULL) {
3812 wl_client_post_no_memory(client);
3813 return;
3814 }
3815
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02003816 if (pointer) {
3817 constraint = weston_pointer_constraint_create(surface, pointer,
3818 region, lifetime,
3819 cr, grab_interface);
3820 if (constraint == NULL) {
3821 wl_client_post_no_memory(client);
3822 return;
3823 }
3824 } else {
3825 constraint = NULL;
Jonas Ådahld3414f22016-07-22 17:56:31 +08003826 }
3827
3828 wl_resource_set_implementation(cr, implementation, constraint,
3829 pointer_constraint_constrain_resource_destroyed);
3830
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02003831 if (constraint)
3832 maybe_enable_pointer_constraint(constraint);
Jonas Ådahld3414f22016-07-22 17:56:31 +08003833}
3834
3835static void
3836pointer_constraints_destroy(struct wl_client *client,
3837 struct wl_resource *resource)
3838{
3839 wl_resource_destroy(resource);
3840}
3841
3842static void
3843locked_pointer_destroy(struct wl_client *client,
3844 struct wl_resource *resource)
3845{
3846 struct weston_pointer_constraint *constraint =
3847 wl_resource_get_user_data(resource);
3848 wl_fixed_t x, y;
3849
3850 if (constraint && constraint->view && constraint->hint_is_pending &&
3851 is_within_constraint_region(constraint,
3852 constraint->hint_x,
3853 constraint->hint_y)) {
3854 weston_view_to_global_fixed(constraint->view,
3855 constraint->hint_x,
3856 constraint->hint_y,
3857 &x, &y);
3858 weston_pointer_move_to(constraint->pointer, x, y);
3859 }
3860 wl_resource_destroy(resource);
3861}
3862
3863static void
3864locked_pointer_set_cursor_position_hint(struct wl_client *client,
3865 struct wl_resource *resource,
3866 wl_fixed_t surface_x,
3867 wl_fixed_t surface_y)
3868{
3869 struct weston_pointer_constraint *constraint =
3870 wl_resource_get_user_data(resource);
3871
3872 /* Ignore a set cursor hint that was sent after the lock was cancelled.
3873 */
3874 if (!constraint ||
3875 !constraint->resource ||
3876 constraint->resource != resource)
3877 return;
3878
3879 constraint->hint_is_pending = true;
3880 constraint->hint_x_pending = surface_x;
3881 constraint->hint_y_pending = surface_y;
3882}
3883
3884static void
3885locked_pointer_set_region(struct wl_client *client,
3886 struct wl_resource *resource,
3887 struct wl_resource *region_resource)
3888{
3889 struct weston_pointer_constraint *constraint =
3890 wl_resource_get_user_data(resource);
3891 struct weston_region *region = region_resource ?
3892 wl_resource_get_user_data(region_resource) : NULL;
3893
3894 if (!constraint)
3895 return;
3896
3897 if (region) {
3898 pixman_region32_copy(&constraint->region_pending,
3899 &region->region);
3900 } else {
3901 pixman_region32_fini(&constraint->region_pending);
3902 region_init_infinite(&constraint->region_pending);
3903 }
3904 constraint->region_is_pending = true;
3905}
3906
3907
3908static const struct zwp_locked_pointer_v1_interface locked_pointer_interface = {
3909 locked_pointer_destroy,
3910 locked_pointer_set_cursor_position_hint,
3911 locked_pointer_set_region,
3912};
3913
3914static void
3915pointer_constraints_lock_pointer(struct wl_client *client,
3916 struct wl_resource *resource,
3917 uint32_t id,
3918 struct wl_resource *surface_resource,
3919 struct wl_resource *pointer_resource,
3920 struct wl_resource *region_resource,
3921 uint32_t lifetime)
3922{
3923 struct weston_surface *surface =
3924 wl_resource_get_user_data(surface_resource);
3925 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
3926 struct weston_region *region = region_resource ?
3927 wl_resource_get_user_data(region_resource) : NULL;
3928
3929 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
3930 &zwp_locked_pointer_v1_interface,
3931 &locked_pointer_interface,
3932 &locked_pointer_grab_interface);
3933}
3934
3935static void
3936confined_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
3937{
3938}
3939
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08003940static double
3941vec2d_cross_product(struct vec2d a, struct vec2d b)
3942{
3943 return a.x * b.y - a.y * b.x;
3944}
3945
3946static struct vec2d
3947vec2d_add(struct vec2d a, struct vec2d b)
3948{
3949 return (struct vec2d) {
3950 .x = a.x + b.x,
3951 .y = a.y + b.y,
3952 };
3953}
3954
3955static struct vec2d
3956vec2d_subtract(struct vec2d a, struct vec2d b)
3957{
3958 return (struct vec2d) {
3959 .x = a.x - b.x,
3960 .y = a.y - b.y,
3961 };
3962}
3963
3964static struct vec2d
3965vec2d_multiply_constant(double c, struct vec2d a)
3966{
3967 return (struct vec2d) {
3968 .x = c * a.x,
3969 .y = c * a.y,
3970 };
3971}
3972
3973static bool
3974lines_intersect(struct line *line1, struct line *line2,
3975 struct vec2d *intersection)
3976{
3977 struct vec2d p = line1->a;
3978 struct vec2d r = vec2d_subtract(line1->b, line1->a);
3979 struct vec2d q = line2->a;
3980 struct vec2d s = vec2d_subtract(line2->b, line2->a);
3981 double rxs;
3982 double sxr;
3983 double t;
3984 double u;
3985
3986 /*
3987 * The line (p, r) and (q, s) intersects where
3988 *
3989 * p + t r = q + u s
3990 *
3991 * Calculate t:
3992 *
3993 * (p + t r) × s = (q + u s) × s
3994 * p × s + t (r × s) = q × s + u (s × s)
3995 * p × s + t (r × s) = q × s
3996 * t (r × s) = q × s - p × s
3997 * t (r × s) = (q - p) × s
3998 * t = ((q - p) × s) / (r × s)
3999 *
4000 * Using the same method, for u we get:
4001 *
4002 * u = ((p - q) × r) / (s × r)
4003 */
4004
4005 rxs = vec2d_cross_product(r, s);
4006 sxr = vec2d_cross_product(s, r);
4007
4008 /* If r × s = 0 then the lines are either parallel or collinear. */
4009 if (fabs(rxs) < DBL_MIN)
4010 return false;
4011
4012 t = vec2d_cross_product(vec2d_subtract(q, p), s) / rxs;
4013 u = vec2d_cross_product(vec2d_subtract(p, q), r) / sxr;
4014
4015 /* The lines only intersect if 0 ≤ t ≤ 1 and 0 ≤ u ≤ 1. */
4016 if (t < 0.0 || t > 1.0 || u < 0.0 || u > 1.0)
4017 return false;
4018
4019 *intersection = vec2d_add(p, vec2d_multiply_constant(t, r));
4020 return true;
4021}
4022
4023static struct border *
4024add_border(struct wl_array *array,
4025 double x1, double y1,
4026 double x2, double y2,
4027 enum motion_direction blocking_dir)
4028{
4029 struct border *border = wl_array_add(array, sizeof *border);
4030
4031 *border = (struct border) {
4032 .line = (struct line) {
4033 .a = (struct vec2d) {
4034 .x = x1,
4035 .y = y1,
4036 },
4037 .b = (struct vec2d) {
4038 .x = x2,
4039 .y = y2,
4040 },
4041 },
4042 .blocking_dir = blocking_dir,
4043 };
4044
4045 return border;
4046}
4047
4048static int
4049compare_lines_x(const void *a, const void *b)
4050{
4051 const struct border *border_a = a;
4052 const struct border *border_b = b;
4053
4054
4055 if (border_a->line.a.x == border_b->line.a.x)
4056 return border_a->line.b.x < border_b->line.b.x;
4057 else
4058 return border_a->line.a.x > border_b->line.a.x;
4059}
4060
4061static void
4062add_non_overlapping_edges(pixman_box32_t *boxes,
4063 int band_above_start,
4064 int band_below_start,
4065 int band_below_end,
4066 struct wl_array *borders)
4067{
4068 int i;
4069 struct wl_array band_merge;
4070 struct border *border;
4071 struct border *prev_border;
4072 struct border *new_border;
4073
4074 wl_array_init(&band_merge);
4075
4076 /* Add bottom band of previous row, and top band of current row, and
4077 * sort them so lower left x coordinate comes first. If there are two
4078 * borders with the same left x coordinate, the wider one comes first.
4079 */
4080 for (i = band_above_start; i < band_below_start; i++) {
4081 pixman_box32_t *box = &boxes[i];
4082 add_border(&band_merge, box->x1, box->y2, box->x2, box->y2,
4083 MOTION_DIRECTION_POSITIVE_Y);
4084 }
4085 for (i = band_below_start; i < band_below_end; i++) {
4086 pixman_box32_t *box= &boxes[i];
4087 add_border(&band_merge, box->x1, box->y1, box->x2, box->y1,
4088 MOTION_DIRECTION_NEGATIVE_Y);
4089 }
4090 qsort(band_merge.data,
4091 band_merge.size / sizeof *border,
4092 sizeof *border,
4093 compare_lines_x);
4094
4095 /* Combine the two combined bands so that any overlapping border is
4096 * eliminated. */
4097 prev_border = NULL;
4098 wl_array_for_each(border, &band_merge) {
4099 assert(border->line.a.y == border->line.b.y);
4100 assert(!prev_border ||
4101 prev_border->line.a.y == border->line.a.y);
4102 assert(!prev_border ||
4103 (prev_border->line.a.x != border->line.a.x ||
4104 prev_border->line.b.x != border->line.b.x));
4105 assert(!prev_border ||
4106 prev_border->line.a.x <= border->line.a.x);
4107
4108 if (prev_border &&
4109 prev_border->line.a.x == border->line.a.x) {
4110 /*
4111 * ------------ +
4112 * ------- =
4113 * [ ]-----
4114 */
4115 prev_border->line.a.x = border->line.b.x;
4116 } else if (prev_border &&
4117 prev_border->line.b.x == border->line.b.x) {
4118 /*
4119 * ------------ +
4120 * ------ =
4121 * ------[ ]
4122 */
4123 prev_border->line.b.x = border->line.a.x;
4124 } else if (prev_border &&
4125 prev_border->line.b.x == border->line.a.x) {
4126 /*
4127 * -------- +
4128 * ------ =
4129 * --------------
4130 */
4131 prev_border->line.b.x = border->line.b.x;
4132 } else if (prev_border &&
4133 prev_border->line.b.x >= border->line.a.x) {
4134 /*
4135 * --------------- +
4136 * ------ =
4137 * -----[ ]----
4138 */
4139 new_border = add_border(borders,
4140 border->line.b.x,
4141 border->line.b.y,
4142 prev_border->line.b.x,
4143 prev_border->line.b.y,
4144 prev_border->blocking_dir);
4145 prev_border->line.b.x = border->line.a.x;
4146 prev_border = new_border;
4147 } else {
4148 assert(!prev_border ||
4149 prev_border->line.b.x < border->line.a.x);
4150 /*
4151 * First border or non-overlapping.
4152 *
4153 * ----- +
4154 * ----- =
4155 * ----- -----
4156 */
4157 new_border = wl_array_add(borders, sizeof *border);
4158 *new_border = *border;
4159 prev_border = new_border;
4160 }
4161 }
4162
4163 wl_array_release(&band_merge);
4164}
4165
4166static void
4167add_band_bottom_edges(pixman_box32_t *boxes,
4168 int band_start,
4169 int band_end,
4170 struct wl_array *borders)
4171{
4172 int i;
4173
4174 for (i = band_start; i < band_end; i++) {
4175 add_border(borders,
4176 boxes[i].x1, boxes[i].y2,
4177 boxes[i].x2, boxes[i].y2,
4178 MOTION_DIRECTION_POSITIVE_Y);
4179 }
4180}
4181
4182static void
4183region_to_outline(pixman_region32_t *region, struct wl_array *borders)
4184{
4185 pixman_box32_t *boxes;
4186 int num_boxes;
4187 int i;
4188 int top_most, bottom_most;
4189 int current_roof;
4190 int prev_top;
4191 int band_start, prev_band_start;
4192
4193 /*
4194 * Remove any overlapping lines from the set of rectangles. Note that
4195 * pixman regions are grouped as rows of rectangles, where rectangles
4196 * in one row never touch or overlap and are all of the same height.
4197 *
4198 * -------- --- -------- ---
4199 * | | | | | | | |
4200 * ----------====---- --- ----------- ----- ---
4201 * | | => | |
4202 * ----==========--------- ----- ----------
4203 * | | | |
4204 * ------------------- -------------------
4205 *
4206 */
4207
4208 boxes = pixman_region32_rectangles(region, &num_boxes);
4209 prev_top = 0;
4210 top_most = boxes[0].y1;
4211 current_roof = top_most;
4212 bottom_most = boxes[num_boxes - 1].y2;
4213 band_start = 0;
4214 prev_band_start = 0;
4215 for (i = 0; i < num_boxes; i++) {
4216 /* Detect if there is a vertical empty space, and add the lower
4217 * level of the previous band if so was the case. */
4218 if (i > 0 &&
4219 boxes[i].y1 != prev_top &&
4220 boxes[i].y1 != boxes[i - 1].y2) {
4221 current_roof = boxes[i].y1;
4222 add_band_bottom_edges(boxes,
4223 band_start,
4224 i,
4225 borders);
4226 }
4227
4228 /* Special case adding the last band, since it won't be handled
4229 * by the band change detection below. */
4230 if (boxes[i].y1 != current_roof && i == num_boxes - 1) {
4231 if (boxes[i].y1 != prev_top) {
4232 /* The last band is a single box, so we don't
4233 * have a prev_band_start to tell us when the
4234 * previous band started. */
4235 add_non_overlapping_edges(boxes,
4236 band_start,
4237 i,
4238 i + 1,
4239 borders);
4240 } else {
4241 add_non_overlapping_edges(boxes,
4242 prev_band_start,
4243 band_start,
4244 i + 1,
4245 borders);
4246 }
4247 }
4248
4249 /* Detect when passing a band and combine the top border of the
4250 * just passed band with the bottom band of the previous band.
4251 */
4252 if (boxes[i].y1 != top_most && boxes[i].y1 != prev_top) {
4253 /* Combine the two passed bands. */
4254 if (prev_top != current_roof) {
4255 add_non_overlapping_edges(boxes,
4256 prev_band_start,
4257 band_start,
4258 i,
4259 borders);
4260 }
4261
4262 prev_band_start = band_start;
4263 band_start = i;
4264 }
4265
4266 /* Add the top border if the box is part of the current roof. */
4267 if (boxes[i].y1 == current_roof) {
4268 add_border(borders,
4269 boxes[i].x1, boxes[i].y1,
4270 boxes[i].x2, boxes[i].y1,
4271 MOTION_DIRECTION_NEGATIVE_Y);
4272 }
4273
4274 /* Add the bottom border of the last band. */
4275 if (boxes[i].y2 == bottom_most) {
4276 add_border(borders,
4277 boxes[i].x1, boxes[i].y2,
4278 boxes[i].x2, boxes[i].y2,
4279 MOTION_DIRECTION_POSITIVE_Y);
4280 }
4281
4282 /* Always add the left border. */
4283 add_border(borders,
4284 boxes[i].x1, boxes[i].y1,
4285 boxes[i].x1, boxes[i].y2,
4286 MOTION_DIRECTION_NEGATIVE_X);
4287
4288 /* Always add the right border. */
4289 add_border(borders,
4290 boxes[i].x2, boxes[i].y1,
4291 boxes[i].x2, boxes[i].y2,
4292 MOTION_DIRECTION_POSITIVE_X);
4293
4294 prev_top = boxes[i].y1;
4295 }
4296}
4297
4298static bool
4299is_border_horizontal (struct border *border)
4300{
4301 return border->line.a.y == border->line.b.y;
4302}
4303
4304static bool
4305is_border_blocking_directions(struct border *border,
4306 uint32_t directions)
4307{
4308 /* Don't block parallel motions. */
4309 if (is_border_horizontal(border)) {
4310 if ((directions & (MOTION_DIRECTION_POSITIVE_Y |
4311 MOTION_DIRECTION_NEGATIVE_Y)) == 0)
4312 return false;
4313 } else {
4314 if ((directions & (MOTION_DIRECTION_POSITIVE_X |
4315 MOTION_DIRECTION_NEGATIVE_X)) == 0)
4316 return false;
4317 }
4318
4319 return (~border->blocking_dir & directions) != directions;
4320}
4321
4322static struct border *
4323get_closest_border(struct wl_array *borders,
4324 struct line *motion,
4325 uint32_t directions)
4326{
4327 struct border *border;
4328 struct vec2d intersection;
4329 struct vec2d delta;
4330 double distance_2;
4331 struct border *closest_border = NULL;
4332 double closest_distance_2 = DBL_MAX;
4333
4334 wl_array_for_each(border, borders) {
4335 if (!is_border_blocking_directions(border, directions))
4336 continue;
4337
4338 if (!lines_intersect(&border->line, motion, &intersection))
4339 continue;
4340
4341 delta = vec2d_subtract(intersection, motion->a);
4342 distance_2 = delta.x*delta.x + delta.y*delta.y;
4343 if (distance_2 < closest_distance_2) {
4344 closest_border = border;
4345 closest_distance_2 = distance_2;
4346 }
4347 }
4348
4349 return closest_border;
4350}
4351
4352static void
4353clamp_to_border(struct border *border,
4354 struct line *motion,
4355 uint32_t *motion_dir)
4356{
4357 /*
4358 * When clamping either rightward or downward motions, the motion needs
4359 * to be clamped so that the destination coordinate does not end up on
4360 * the border (see weston_pointer_clamp_event_to_region). Do this by
4361 * clamping such motions to the border minus the smallest possible
4362 * wl_fixed_t value.
4363 */
4364 if (is_border_horizontal(border)) {
4365 if (*motion_dir & MOTION_DIRECTION_POSITIVE_Y)
4366 motion->b.y = border->line.a.y - wl_fixed_to_double(1);
4367 else
4368 motion->b.y = border->line.a.y;
4369 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_Y |
4370 MOTION_DIRECTION_NEGATIVE_Y);
4371 } else {
4372 if (*motion_dir & MOTION_DIRECTION_POSITIVE_X)
4373 motion->b.x = border->line.a.x - wl_fixed_to_double(1);
4374 else
4375 motion->b.x = border->line.a.x;
4376 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_X |
4377 MOTION_DIRECTION_NEGATIVE_X);
4378 }
4379}
4380
4381static uint32_t
4382get_motion_directions(struct line *motion)
4383{
4384 uint32_t directions = 0;
4385
4386 if (motion->a.x < motion->b.x)
4387 directions |= MOTION_DIRECTION_POSITIVE_X;
4388 else if (motion->a.x > motion->b.x)
4389 directions |= MOTION_DIRECTION_NEGATIVE_X;
4390 if (motion->a.y < motion->b.y)
4391 directions |= MOTION_DIRECTION_POSITIVE_Y;
4392 else if (motion->a.y > motion->b.y)
4393 directions |= MOTION_DIRECTION_NEGATIVE_Y;
4394
4395 return directions;
4396}
4397
Jonas Ådahld3414f22016-07-22 17:56:31 +08004398static void
4399weston_pointer_clamp_event_to_region(struct weston_pointer *pointer,
4400 struct weston_pointer_motion_event *event,
4401 pixman_region32_t *region,
4402 wl_fixed_t *clamped_x,
4403 wl_fixed_t *clamped_y)
4404{
4405 wl_fixed_t x, y;
4406 wl_fixed_t sx, sy;
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004407 wl_fixed_t old_sx = pointer->sx;
4408 wl_fixed_t old_sy = pointer->sy;
4409 struct wl_array borders;
4410 struct line motion;
4411 struct border *closest_border;
4412 float new_x_f, new_y_f;
4413 uint32_t directions;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004414
4415 weston_pointer_motion_to_abs(pointer, event, &x, &y);
4416 weston_view_from_global_fixed(pointer->focus, x, y, &sx, &sy);
4417
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004418 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004419
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004420 /*
4421 * Generate borders given the confine region we are to use. The borders
4422 * are defined to be the outer region of the allowed area. This means
4423 * top/left borders are "within" the allowed area, while bottom/right
4424 * borders are outside. This needs to be considered when clamping
4425 * confined motion vectors.
4426 */
4427 region_to_outline(region, &borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004428
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004429 motion = (struct line) {
4430 .a = (struct vec2d) {
4431 .x = wl_fixed_to_double(old_sx),
4432 .y = wl_fixed_to_double(old_sy),
4433 },
4434 .b = (struct vec2d) {
4435 .x = wl_fixed_to_double(sx),
4436 .y = wl_fixed_to_double(sy),
4437 },
4438 };
4439 directions = get_motion_directions(&motion);
4440
4441 while (directions) {
4442 closest_border = get_closest_border(&borders,
4443 &motion,
4444 directions);
4445 if (closest_border)
4446 clamp_to_border(closest_border, &motion, &directions);
4447 else
4448 break;
4449 }
4450
4451 weston_view_to_global_float(pointer->focus,
4452 (float) motion.b.x, (float) motion.b.y,
4453 &new_x_f, &new_y_f);
4454 *clamped_x = wl_fixed_from_double(new_x_f);
4455 *clamped_y = wl_fixed_from_double(new_y_f);
4456
4457 wl_array_release(&borders);
4458}
4459
4460static double
4461point_to_border_distance_2(struct border *border, double x, double y)
4462{
4463 double orig_x, orig_y;
4464 double dx, dy;
4465
4466 if (is_border_horizontal(border)) {
4467 if (x < border->line.a.x)
4468 orig_x = border->line.a.x;
4469 else if (x > border->line.b.x)
4470 orig_x = border->line.b.x;
4471 else
4472 orig_x = x;
4473 orig_y = border->line.a.y;
4474 } else {
4475 if (y < border->line.a.y)
4476 orig_y = border->line.a.y;
4477 else if (y > border->line.b.y)
4478 orig_y = border->line.b.y;
4479 else
4480 orig_y = y;
4481 orig_x = border->line.a.x;
4482 }
4483
4484
4485 dx = fabs(orig_x - x);
4486 dy = fabs(orig_y - y);
4487 return dx*dx + dy*dy;
4488}
4489
4490static void
4491warp_to_behind_border(struct border *border, wl_fixed_t *sx, wl_fixed_t *sy)
4492{
4493 switch (border->blocking_dir) {
4494 case MOTION_DIRECTION_POSITIVE_X:
4495 case MOTION_DIRECTION_NEGATIVE_X:
4496 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_X)
4497 *sx = wl_fixed_from_double(border->line.a.x) - 1;
4498 else
4499 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4500 if (*sy < wl_fixed_from_double(border->line.a.y))
4501 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4502 else if (*sy > wl_fixed_from_double(border->line.b.y))
4503 *sy = wl_fixed_from_double(border->line.b.y) - 1;
4504 break;
4505 case MOTION_DIRECTION_POSITIVE_Y:
4506 case MOTION_DIRECTION_NEGATIVE_Y:
4507 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_Y)
4508 *sy = wl_fixed_from_double(border->line.a.y) - 1;
4509 else
4510 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4511 if (*sx < wl_fixed_from_double(border->line.a.x))
4512 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4513 else if (*sx > wl_fixed_from_double(border->line.b.x))
4514 *sx = wl_fixed_from_double(border->line.b.x) - 1;
4515 break;
4516 }
Jonas Ådahld3414f22016-07-22 17:56:31 +08004517}
4518
4519static void
4520maybe_warp_confined_pointer(struct weston_pointer_constraint *constraint)
4521{
4522 wl_fixed_t x;
4523 wl_fixed_t y;
4524 wl_fixed_t sx;
4525 wl_fixed_t sy;
4526
4527 weston_view_from_global_fixed(constraint->view,
4528 constraint->pointer->x,
4529 constraint->pointer->y,
4530 &sx,
4531 &sy);
4532
4533 if (!is_within_constraint_region(constraint, sx, sy)) {
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004534 double xf = wl_fixed_to_double(sx);
4535 double yf = wl_fixed_to_double(sy);
4536 pixman_region32_t confine_region;
4537 struct wl_array borders;
4538 struct border *border;
4539 double closest_distance_2 = DBL_MAX;
4540 struct border *closest_border = NULL;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004541
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004542 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004543
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004544 pixman_region32_init(&confine_region);
4545 pixman_region32_intersect(&confine_region,
4546 &constraint->view->surface->input,
4547 &constraint->region);
4548 region_to_outline(&confine_region, &borders);
4549 pixman_region32_fini(&confine_region);
4550
4551 wl_array_for_each(border, &borders) {
4552 double distance_2;
4553
4554 distance_2 = point_to_border_distance_2(border, xf, yf);
4555 if (distance_2 < closest_distance_2) {
4556 closest_border = border;
4557 closest_distance_2 = distance_2;
4558 }
4559 }
4560 assert(closest_border);
4561
4562 warp_to_behind_border(closest_border, &sx, &sy);
4563
4564 wl_array_release(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004565
4566 weston_view_to_global_fixed(constraint->view, sx, sy, &x, &y);
4567 weston_pointer_move_to(constraint->pointer, x, y);
4568 }
4569}
4570
4571static void
4572confined_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02004573 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004574 struct weston_pointer_motion_event *event)
4575{
4576 struct weston_pointer_constraint *constraint =
4577 container_of(grab, struct weston_pointer_constraint, grab);
4578 struct weston_pointer *pointer = grab->pointer;
4579 struct weston_surface *surface;
4580 wl_fixed_t x, y;
4581 wl_fixed_t old_sx = pointer->sx;
4582 wl_fixed_t old_sy = pointer->sy;
4583 pixman_region32_t confine_region;
4584
4585 assert(pointer->focus);
4586 assert(pointer->focus->surface == constraint->surface);
4587
4588 surface = pointer->focus->surface;
4589
4590 pixman_region32_init(&confine_region);
4591 pixman_region32_intersect(&confine_region,
4592 &surface->input,
4593 &constraint->region);
4594 weston_pointer_clamp_event_to_region(pointer, event,
4595 &confine_region, &x, &y);
4596 weston_pointer_move_to(pointer, x, y);
4597 pixman_region32_fini(&confine_region);
4598
4599 weston_view_from_global_fixed(pointer->focus, x, y,
4600 &pointer->sx, &pointer->sy);
4601
4602 if (old_sx != pointer->sx || old_sy != pointer->sy) {
Quentin Glidiccde13452016-08-12 10:41:32 +02004603 pointer_send_motion(pointer, time,
4604 pointer->sx, pointer->sy);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004605 }
4606
Quentin Glidiccde13452016-08-12 10:41:32 +02004607 pointer_send_relative_motion(pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004608}
4609
4610static void
4611confined_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02004612 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004613 uint32_t button,
4614 uint32_t state_w)
4615{
4616 weston_pointer_send_button(grab->pointer, time, button, state_w);
4617}
4618
4619static void
4620confined_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02004621 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004622 struct weston_pointer_axis_event *event)
4623{
4624 weston_pointer_send_axis(grab->pointer, time, event);
4625}
4626
4627static void
4628confined_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
4629 uint32_t source)
4630{
4631 weston_pointer_send_axis_source(grab->pointer, source);
4632}
4633
4634static void
4635confined_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
4636{
4637 weston_pointer_send_frame(grab->pointer);
4638}
4639
4640static void
4641confined_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
4642{
4643 struct weston_pointer_constraint *constraint =
4644 container_of(grab, struct weston_pointer_constraint, grab);
4645
4646 disable_pointer_constraint(constraint);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004647}
4648
4649static const struct weston_pointer_grab_interface
4650 confined_pointer_grab_interface = {
4651 confined_pointer_grab_pointer_focus,
4652 confined_pointer_grab_pointer_motion,
4653 confined_pointer_grab_pointer_button,
4654 confined_pointer_grab_pointer_axis,
4655 confined_pointer_grab_pointer_axis_source,
4656 confined_pointer_grab_pointer_frame,
4657 confined_pointer_grab_pointer_cancel,
4658};
4659
4660static void
4661confined_pointer_destroy(struct wl_client *client,
4662 struct wl_resource *resource)
4663{
4664 wl_resource_destroy(resource);
4665}
4666
4667static void
4668confined_pointer_set_region(struct wl_client *client,
4669 struct wl_resource *resource,
4670 struct wl_resource *region_resource)
4671{
4672 struct weston_pointer_constraint *constraint =
4673 wl_resource_get_user_data(resource);
4674 struct weston_region *region = region_resource ?
4675 wl_resource_get_user_data(region_resource) : NULL;
4676
4677 if (!constraint)
4678 return;
4679
4680 if (region) {
4681 pixman_region32_copy(&constraint->region_pending,
4682 &region->region);
4683 } else {
4684 pixman_region32_fini(&constraint->region_pending);
4685 region_init_infinite(&constraint->region_pending);
4686 }
4687 constraint->region_is_pending = true;
4688}
4689
4690static const struct zwp_confined_pointer_v1_interface confined_pointer_interface = {
4691 confined_pointer_destroy,
4692 confined_pointer_set_region,
4693};
4694
4695static void
4696pointer_constraints_confine_pointer(struct wl_client *client,
4697 struct wl_resource *resource,
4698 uint32_t id,
4699 struct wl_resource *surface_resource,
4700 struct wl_resource *pointer_resource,
4701 struct wl_resource *region_resource,
4702 uint32_t lifetime)
4703{
4704 struct weston_surface *surface =
4705 wl_resource_get_user_data(surface_resource);
4706 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
4707 struct weston_region *region = region_resource ?
4708 wl_resource_get_user_data(region_resource) : NULL;
4709
4710 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
4711 &zwp_confined_pointer_v1_interface,
4712 &confined_pointer_interface,
4713 &confined_pointer_grab_interface);
4714}
4715
4716static const struct zwp_pointer_constraints_v1_interface pointer_constraints_interface = {
4717 pointer_constraints_destroy,
4718 pointer_constraints_lock_pointer,
4719 pointer_constraints_confine_pointer,
4720};
4721
4722static void
4723bind_pointer_constraints(struct wl_client *client, void *data,
4724 uint32_t version, uint32_t id)
4725{
4726 struct wl_resource *resource;
4727
4728 resource = wl_resource_create(client,
4729 &zwp_pointer_constraints_v1_interface,
4730 1, id);
4731
4732 wl_resource_set_implementation(resource, &pointer_constraints_interface,
4733 NULL, NULL);
4734}
4735
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004736static void
Alexandros Frantzis2b442482018-02-20 14:05:50 +02004737input_timestamps_destroy(struct wl_client *client,
4738 struct wl_resource *resource)
4739{
4740 wl_resource_destroy(resource);
4741}
4742
4743static const struct zwp_input_timestamps_v1_interface
4744 input_timestamps_interface = {
4745 input_timestamps_destroy,
4746};
4747
4748static void
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004749input_timestamps_manager_destroy(struct wl_client *client,
4750 struct wl_resource *resource)
4751{
4752 wl_resource_destroy(resource);
4753}
4754
4755static void
4756input_timestamps_manager_get_keyboard_timestamps(struct wl_client *client,
4757 struct wl_resource *resource,
4758 uint32_t id,
4759 struct wl_resource *keyboard_resource)
4760{
Alexandros Frantzis2b442482018-02-20 14:05:50 +02004761 struct weston_keyboard *keyboard =
4762 wl_resource_get_user_data(keyboard_resource);
4763 struct wl_resource *input_ts;
4764
4765 input_ts = wl_resource_create(client,
4766 &zwp_input_timestamps_v1_interface,
4767 1, id);
4768 if (!input_ts) {
4769 wl_client_post_no_memory(client);
4770 return;
4771 }
4772
4773 if (keyboard) {
4774 wl_list_insert(&keyboard->timestamps_list,
4775 wl_resource_get_link(input_ts));
4776 } else {
4777 wl_list_init(wl_resource_get_link(input_ts));
4778 }
4779
4780 wl_resource_set_implementation(input_ts,
4781 &input_timestamps_interface,
4782 keyboard_resource,
4783 unbind_resource);
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004784}
4785
4786static void
4787input_timestamps_manager_get_pointer_timestamps(struct wl_client *client,
4788 struct wl_resource *resource,
4789 uint32_t id,
4790 struct wl_resource *pointer_resource)
4791{
Alexandros Frantzisdb907b72018-02-20 14:06:26 +02004792 struct weston_pointer *pointer =
4793 wl_resource_get_user_data(pointer_resource);
4794 struct wl_resource *input_ts;
4795
4796 input_ts = wl_resource_create(client,
4797 &zwp_input_timestamps_v1_interface,
4798 1, id);
4799 if (!input_ts) {
4800 wl_client_post_no_memory(client);
4801 return;
4802 }
4803
4804 if (pointer) {
4805 wl_list_insert(&pointer->timestamps_list,
4806 wl_resource_get_link(input_ts));
4807 } else {
4808 wl_list_init(wl_resource_get_link(input_ts));
4809 }
4810
4811 wl_resource_set_implementation(input_ts,
4812 &input_timestamps_interface,
4813 pointer_resource,
4814 unbind_resource);
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004815}
4816
4817static void
4818input_timestamps_manager_get_touch_timestamps(struct wl_client *client,
4819 struct wl_resource *resource,
4820 uint32_t id,
4821 struct wl_resource *touch_resource)
4822{
Alexandros Frantzisd7157842018-02-20 14:07:03 +02004823 struct weston_touch *touch = wl_resource_get_user_data(touch_resource);
4824 struct wl_resource *input_ts;
4825
4826 input_ts = wl_resource_create(client,
4827 &zwp_input_timestamps_v1_interface,
4828 1, id);
4829 if (!input_ts) {
4830 wl_client_post_no_memory(client);
4831 return;
4832 }
4833
4834 if (touch) {
4835 wl_list_insert(&touch->timestamps_list,
4836 wl_resource_get_link(input_ts));
4837 } else {
4838 wl_list_init(wl_resource_get_link(input_ts));
4839 }
4840
4841 wl_resource_set_implementation(input_ts,
4842 &input_timestamps_interface,
4843 touch_resource,
4844 unbind_resource);
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004845}
4846
4847static const struct zwp_input_timestamps_manager_v1_interface
4848 input_timestamps_manager_interface = {
4849 input_timestamps_manager_destroy,
4850 input_timestamps_manager_get_keyboard_timestamps,
4851 input_timestamps_manager_get_pointer_timestamps,
4852 input_timestamps_manager_get_touch_timestamps,
4853};
4854
4855static void
4856bind_input_timestamps_manager(struct wl_client *client, void *data,
4857 uint32_t version, uint32_t id)
4858{
4859 struct wl_resource *resource =
4860 wl_resource_create(client,
4861 &zwp_input_timestamps_manager_v1_interface,
4862 1, id);
4863
4864 if (resource == NULL) {
4865 wl_client_post_no_memory(client);
4866 return;
4867 }
4868
4869 wl_resource_set_implementation(resource,
4870 &input_timestamps_manager_interface,
4871 NULL, NULL);
4872}
4873
Jonas Ådahl30d61d82014-10-22 21:21:17 +02004874int
4875weston_input_init(struct weston_compositor *compositor)
4876{
4877 if (!wl_global_create(compositor->wl_display,
4878 &zwp_relative_pointer_manager_v1_interface, 1,
4879 compositor, bind_relative_pointer_manager))
4880 return -1;
4881
Jonas Ådahld3414f22016-07-22 17:56:31 +08004882 if (!wl_global_create(compositor->wl_display,
4883 &zwp_pointer_constraints_v1_interface, 1,
4884 NULL, bind_pointer_constraints))
4885 return -1;
4886
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004887 if (!wl_global_create(compositor->wl_display,
4888 &zwp_input_timestamps_manager_v1_interface, 1,
4889 NULL, bind_input_timestamps_manager))
4890 return -1;
4891
Jonas Ådahl30d61d82014-10-22 21:21:17 +02004892 return 0;
4893}