blob: 6816cc32db3ef6c585cb1de5ab5ffc12d0f53507 [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
Pekka Paalanenf4062532018-02-26 16:18:29 +02002341static void
2342process_touch_normal(struct weston_touch_device *device,
2343 const struct timespec *time, int touch_id,
2344 double double_x, double double_y, int touch_type)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002345{
Pekka Paalanenbcbce332018-02-26 14:43:00 +02002346 struct weston_touch *touch = device->aggregate;
2347 struct weston_touch_grab *grab = device->aggregate->grab;
2348 struct weston_compositor *ec = device->aggregate->seat->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002349 struct weston_view *ev;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002350 wl_fixed_t sx, sy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002351 wl_fixed_t x = wl_fixed_from_double(double_x);
2352 wl_fixed_t y = wl_fixed_from_double(double_y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002353
2354 /* Update grab's global coordinates. */
Neil Roberts306fe082013-10-03 16:43:06 +01002355 if (touch_id == touch->grab_touch_id && touch_type != WL_TOUCH_UP) {
2356 touch->grab_x = x;
2357 touch->grab_y = y;
2358 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002359
2360 switch (touch_type) {
2361 case WL_TOUCH_DOWN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002362 /* the first finger down picks the view, and all further go
2363 * to that view for the remainder of the touch session i.e.
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002364 * until all touch points are up again. */
Jonas Ådahl9484b692013-12-02 22:05:03 +01002365 if (touch->num_tp == 1) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002366 ev = weston_compositor_pick_view(ec, x, y, &sx, &sy);
Derek Foreman4c93c082015-04-30 16:45:41 -05002367 weston_touch_set_focus(touch, ev);
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002368 } else if (!touch->focus) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002369 /* Unexpected condition: We have non-initial touch but
2370 * there is no focused surface.
2371 */
Chris Michael3f607d32015-10-07 11:59:49 -04002372 weston_log("touch event received with %d points down "
Jonas Ådahl9484b692013-12-02 22:05:03 +01002373 "but no surface focused\n", touch->num_tp);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002374 return;
2375 }
2376
Derek Foreman99a6a2d2015-07-15 13:00:43 -05002377 weston_compositor_run_touch_binding(ec, touch,
Kristian Høgsbergc8964012014-02-05 14:25:18 -08002378 time, touch_type);
2379
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002380 grab->interface->down(grab, time, touch_id, x, y);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002381 if (touch->num_tp == 1) {
Rusty Lynchf1407ff2013-08-08 21:13:57 -07002382 touch->grab_serial =
2383 wl_display_get_serial(ec->wl_display);
Neil Roberts306fe082013-10-03 16:43:06 +01002384 touch->grab_touch_id = touch_id;
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02002385 touch->grab_time = *time;
Rusty Lynchf1407ff2013-08-08 21:13:57 -07002386 touch->grab_x = x;
2387 touch->grab_y = y;
2388 }
2389
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002390 break;
2391 case WL_TOUCH_MOTION:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002392 ev = touch->focus;
2393 if (!ev)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002394 break;
2395
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +02002396 grab->interface->motion(grab, time, touch_id, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002397 break;
2398 case WL_TOUCH_UP:
Alexandros Frantzis27a51b82017-11-16 18:20:59 +02002399 grab->interface->up(grab, time, touch_id);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002400 if (touch->num_tp == 0)
Derek Foreman4c93c082015-04-30 16:45:41 -05002401 weston_touch_set_focus(touch, NULL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002402 break;
2403 }
2404}
2405
Pekka Paalanenf4062532018-02-26 16:18:29 +02002406/** Feed in touch down, motion, and up events, calibratable device.
2407 *
2408 * It assumes always the correct cycle sequence until it gets here: touch_down
2409 * → touch_update → ... → touch_update → touch_end. The driver is responsible
2410 * for sending along such order.
2411 *
2412 * \param device The physical device that generated the event.
2413 * \param time The event timestamp.
2414 * \param touch_id ID for the touch point of this event (multi-touch).
2415 * \param double_x X coordinate in compositor global space.
2416 * \param double_y Y coordinate in compositor global space.
2417 * \param norm Normalized device X, Y coordinates in calibration space, or NULL.
2418 * \param touch_type Either WL_TOUCH_DOWN, WL_TOUCH_UP, or WL_TOUCH_MOTION.
2419 *
2420 * Coordinates double_x and double_y are used for normal operation.
2421 *
2422 * Coordinates norm are only used for touch device calibration. If and only if
2423 * the weston_touch_device does not support calibrating, norm must be NULL.
2424 *
2425 * The calibration space is the normalized coordinate space
2426 * [0.0, 1.0]×[0.0, 1.0] of the weston_touch_device. This is assumed to
2427 * map to the similar normalized coordinate space of the associated
2428 * weston_output.
2429 */
2430WL_EXPORT void
2431notify_touch_normalized(struct weston_touch_device *device,
2432 const struct timespec *time,
2433 int touch_id,
2434 double x, double y,
2435 const struct weston_point2d_device_normalized *norm,
2436 int touch_type)
2437{
Louis-Francis Ratté-Boulianne813a06e2017-11-28 20:42:47 -05002438 struct weston_seat *seat = device->aggregate->seat;
2439 struct weston_touch *touch = device->aggregate;
2440
Pekka Paalanenf4062532018-02-26 16:18:29 +02002441 if (touch_type != WL_TOUCH_UP) {
2442 if (weston_touch_device_can_calibrate(device))
2443 assert(norm != NULL);
2444 else
2445 assert(norm == NULL);
2446 }
2447
Louis-Francis Ratté-Boulianne813a06e2017-11-28 20:42:47 -05002448 /* Update touchpoints count regardless of the current mode. */
2449 switch (touch_type) {
2450 case WL_TOUCH_DOWN:
2451 weston_compositor_idle_inhibit(seat->compositor);
2452
2453 touch->num_tp++;
2454 break;
2455 case WL_TOUCH_UP:
2456 if (touch->num_tp == 0) {
2457 /* This can happen if we start out with one or
2458 * more fingers on the touch screen, in which
2459 * case we didn't get the corresponding down
2460 * event. */
2461 weston_log("unmatched touch up event\n");
2462 break;
2463 }
2464 weston_compositor_idle_release(seat->compositor);
2465
2466 touch->num_tp--;
2467 break;
2468 default:
2469 break;
2470 }
2471
Pekka Paalanenf4062532018-02-26 16:18:29 +02002472 process_touch_normal(device, time, touch_id, x, y, touch_type);
2473}
2474
Jonas Ådahl1679f232014-04-12 09:39:51 +02002475WL_EXPORT void
Pekka Paalanenbcbce332018-02-26 14:43:00 +02002476notify_touch_frame(struct weston_touch_device *device)
Jonas Ådahl1679f232014-04-12 09:39:51 +02002477{
Pekka Paalanenbcbce332018-02-26 14:43:00 +02002478 struct weston_touch_grab *grab = device->aggregate->grab;
Jonas Ådahl1679f232014-04-12 09:39:51 +02002479
2480 grab->interface->frame(grab);
2481}
2482
Derek Foreman3cc004a2015-11-06 15:56:09 -06002483WL_EXPORT void
Pekka Paalanenbcbce332018-02-26 14:43:00 +02002484notify_touch_cancel(struct weston_touch_device *device)
Derek Foreman3cc004a2015-11-06 15:56:09 -06002485{
Pekka Paalanenbcbce332018-02-26 14:43:00 +02002486 struct weston_touch_grab *grab = device->aggregate->grab;
Derek Foreman3cc004a2015-11-06 15:56:09 -06002487
2488 grab->interface->cancel(grab);
2489}
2490
Pekka Paalanen8274d902014-08-06 19:36:51 +03002491static int
2492pointer_cursor_surface_get_label(struct weston_surface *surface,
2493 char *buf, size_t len)
2494{
2495 return snprintf(buf, len, "cursor");
2496}
2497
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002498static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002499pointer_cursor_surface_committed(struct weston_surface *es,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002500 int32_t dx, int32_t dy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002501{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002502 struct weston_pointer *pointer = es->committed_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002503 int x, y;
2504
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002505 if (es->width == 0)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002506 return;
2507
Jason Ekstranda7af7042013-10-12 22:38:11 -05002508 assert(es == pointer->sprite->surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002509
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002510 pointer->hotspot_x -= dx;
2511 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002512
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002513 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
2514 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002515
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002516 weston_view_set_position(pointer->sprite, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002517
2518 empty_region(&es->pending.input);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02002519 empty_region(&es->input);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002520
2521 if (!weston_surface_is_mapped(es)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002522 weston_layer_entry_insert(&es->compositor->cursor_layer.view_list,
2523 &pointer->sprite->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002524 weston_view_update_transform(pointer->sprite);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002525 es->is_mapped = true;
2526 pointer->sprite->is_mapped = true;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002527 }
2528}
2529
2530static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002531pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2532 uint32_t serial, struct wl_resource *surface_resource,
2533 int32_t x, int32_t y)
2534{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002535 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002536 struct weston_surface *surface = NULL;
2537
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +02002538 if (!pointer)
2539 return;
2540
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002541 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002542 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002543
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002544 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002545 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002546 /* pointer->focus->surface->resource can be NULL. Surfaces like the
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02002547 black_surface used in shell.c for fullscreen don't have
2548 a resource, but can still have focus */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002549 if (pointer->focus->surface->resource == NULL)
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02002550 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002551 if (wl_resource_get_client(pointer->focus->surface->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002552 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002553 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002554 return;
2555
Derek Foreman4e53c532015-03-23 10:55:32 -05002556 if (!surface) {
2557 if (pointer->sprite)
2558 pointer_unmap_sprite(pointer);
2559 return;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002560 }
2561
Jonas Ådahlb4070242015-03-18 15:08:03 +08002562 if (pointer->sprite && pointer->sprite->surface == surface &&
2563 pointer->hotspot_x == x && pointer->hotspot_y == y)
2564 return;
2565
Derek Foreman4e53c532015-03-23 10:55:32 -05002566 if (!pointer->sprite || pointer->sprite->surface != surface) {
2567 if (weston_surface_set_role(surface, "wl_pointer-cursor",
2568 resource,
2569 WL_POINTER_ERROR_ROLE) < 0)
2570 return;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002571
Derek Foreman4e53c532015-03-23 10:55:32 -05002572 if (pointer->sprite)
2573 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002574
Derek Foreman4e53c532015-03-23 10:55:32 -05002575 wl_signal_add(&surface->destroy_signal,
2576 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002577
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002578 surface->committed = pointer_cursor_surface_committed;
2579 surface->committed_private = pointer;
Derek Foreman4e53c532015-03-23 10:55:32 -05002580 weston_surface_set_label_func(surface,
2581 pointer_cursor_surface_get_label);
2582 pointer->sprite = weston_view_create(surface);
2583 }
2584
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002585 pointer->hotspot_x = x;
2586 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002587
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +02002588 if (surface->buffer_ref.buffer) {
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002589 pointer_cursor_surface_committed(surface, 0, 0);
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +02002590 weston_view_schedule_repaint(pointer->sprite);
2591 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002592}
2593
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002594static void
2595pointer_release(struct wl_client *client, struct wl_resource *resource)
2596{
2597 wl_resource_destroy(resource);
2598}
2599
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002600static const struct wl_pointer_interface pointer_interface = {
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002601 pointer_set_cursor,
2602 pointer_release
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002603};
2604
2605static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002606seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2607 uint32_t id)
2608{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002609 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002610 /* We use the pointer_state directly, which means we'll
2611 * give a wl_pointer if the seat has ever had one - even though
2612 * the spec explicitly states that this request only takes effect
2613 * if the seat has the pointer capability.
2614 *
2615 * This prevents a race between the compositor sending new
2616 * capabilities and the client trying to use the old ones.
2617 */
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002618 struct weston_pointer *pointer = seat ? seat->pointer_state : NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002619 struct wl_resource *cr;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002620 struct weston_pointer_client *pointer_client;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002621
Jason Ekstranda85118c2013-06-27 20:17:02 -05002622 cr = wl_resource_create(client, &wl_pointer_interface,
2623 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002624 if (cr == NULL) {
2625 wl_client_post_no_memory(client);
2626 return;
2627 }
2628
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002629 wl_list_init(wl_resource_get_link(cr));
2630 wl_resource_set_implementation(cr, &pointer_interface, pointer,
2631 unbind_pointer_client_resource);
2632
2633 /* If we don't have a pointer_state, the resource is inert, so there
2634 * is nothing more to set up */
2635 if (!pointer)
2636 return;
2637
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002638 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
2639 if (!pointer_client) {
2640 wl_client_post_no_memory(client);
2641 return;
2642 }
2643
2644 wl_list_insert(&pointer_client->pointer_resources,
2645 wl_resource_get_link(cr));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002646
Derek Foreman1281a362015-07-31 16:55:32 -05002647 if (pointer->focus && pointer->focus->surface->resource &&
2648 wl_resource_get_client(pointer->focus->surface->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002649 wl_fixed_t sx, sy;
2650
Derek Foreman1281a362015-07-31 16:55:32 -05002651 weston_view_from_global_fixed(pointer->focus,
2652 pointer->x,
2653 pointer->y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002654 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002655
Neil Roberts96d790e2013-09-19 17:32:00 +01002656 wl_pointer_send_enter(cr,
Derek Foreman1281a362015-07-31 16:55:32 -05002657 pointer->focus_serial,
2658 pointer->focus->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01002659 sx, sy);
Peter Hutterer87743e92016-01-18 16:38:22 +10002660 pointer_send_frame(cr);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002661 }
2662}
2663
2664static void
Alexandros Frantzis2b442482018-02-20 14:05:50 +02002665destroy_keyboard_resource(struct wl_resource *resource)
2666{
2667 struct weston_keyboard *keyboard = wl_resource_get_user_data(resource);
2668
2669 wl_list_remove(wl_resource_get_link(resource));
2670
2671 if (keyboard) {
2672 remove_input_resource_from_timestamps(resource,
2673 &keyboard->timestamps_list);
2674 }
2675}
2676
2677static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002678keyboard_release(struct wl_client *client, struct wl_resource *resource)
2679{
2680 wl_resource_destroy(resource);
2681}
2682
2683static const struct wl_keyboard_interface keyboard_interface = {
2684 keyboard_release
2685};
2686
Derek Foreman280e7dd2014-10-03 13:13:42 -05002687static bool
Neil Roberts96d790e2013-09-19 17:32:00 +01002688should_send_modifiers_to_client(struct weston_seat *seat,
2689 struct wl_client *client)
2690{
Derek Foreman1281a362015-07-31 16:55:32 -05002691 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2692 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2693
2694 if (keyboard &&
2695 keyboard->focus &&
2696 keyboard->focus->resource &&
2697 wl_resource_get_client(keyboard->focus->resource) == client)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002698 return true;
Neil Roberts96d790e2013-09-19 17:32:00 +01002699
Derek Foreman1281a362015-07-31 16:55:32 -05002700 if (pointer &&
2701 pointer->focus &&
2702 pointer->focus->surface->resource &&
2703 wl_resource_get_client(pointer->focus->surface->resource) == client)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002704 return true;
Neil Roberts96d790e2013-09-19 17:32:00 +01002705
Derek Foreman280e7dd2014-10-03 13:13:42 -05002706 return false;
Neil Roberts96d790e2013-09-19 17:32:00 +01002707}
2708
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002709static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002710seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2711 uint32_t id)
2712{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002713 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002714 /* We use the keyboard_state directly, which means we'll
2715 * give a wl_keyboard if the seat has ever had one - even though
2716 * the spec explicitly states that this request only takes effect
2717 * if the seat has the keyboard capability.
2718 *
2719 * This prevents a race between the compositor sending new
2720 * capabilities and the client trying to use the old ones.
2721 */
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002722 struct weston_keyboard *keyboard = seat ? seat->keyboard_state : NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002723 struct wl_resource *cr;
2724
Jason Ekstranda85118c2013-06-27 20:17:02 -05002725 cr = wl_resource_create(client, &wl_keyboard_interface,
2726 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002727 if (cr == NULL) {
2728 wl_client_post_no_memory(client);
2729 return;
2730 }
2731
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002732 wl_list_init(wl_resource_get_link(cr));
2733 wl_resource_set_implementation(cr, &keyboard_interface,
Alexandros Frantzis2b442482018-02-20 14:05:50 +02002734 keyboard, destroy_keyboard_resource);
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002735
2736 /* If we don't have a keyboard_state, the resource is inert, so there
2737 * is nothing more to set up */
2738 if (!keyboard)
2739 return;
2740
Neil Roberts96d790e2013-09-19 17:32:00 +01002741 /* May be moved to focused list later by either
2742 * weston_keyboard_set_focus or directly if this client is already
2743 * focused */
Derek Foreman345c9f32015-06-03 15:53:28 -05002744 wl_list_insert(&keyboard->resource_list, wl_resource_get_link(cr));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002745
Jonny Lamb66a41a02014-08-12 14:58:25 +02002746 if (wl_resource_get_version(cr) >= WL_KEYBOARD_REPEAT_INFO_SINCE_VERSION) {
2747 wl_keyboard_send_repeat_info(cr,
2748 seat->compositor->kb_repeat_rate,
2749 seat->compositor->kb_repeat_delay);
2750 }
Jasper St. Pierred8c6aeb2014-08-04 13:43:24 -04002751
Derek Foreman185d1582017-06-28 11:17:23 -05002752 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2753 keyboard->xkb_info->keymap_fd,
2754 keyboard->xkb_info->keymap_size);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002755
Neil Roberts96d790e2013-09-19 17:32:00 +01002756 if (should_send_modifiers_to_client(seat, client)) {
Derek Foreman345c9f32015-06-03 15:53:28 -05002757 send_modifiers_to_resource(keyboard,
Neil Roberts96d790e2013-09-19 17:32:00 +01002758 cr,
Derek Foreman345c9f32015-06-03 15:53:28 -05002759 keyboard->focus_serial);
Neil Roberts96d790e2013-09-19 17:32:00 +01002760 }
2761
Derek Foreman345c9f32015-06-03 15:53:28 -05002762 if (keyboard->focus && keyboard->focus->resource &&
2763 wl_resource_get_client(keyboard->focus->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01002764 struct weston_surface *surface =
Derek Foreman345c9f32015-06-03 15:53:28 -05002765 (struct weston_surface *)keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +01002766
2767 wl_list_remove(wl_resource_get_link(cr));
Derek Foreman345c9f32015-06-03 15:53:28 -05002768 wl_list_insert(&keyboard->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002769 wl_resource_get_link(cr));
2770 wl_keyboard_send_enter(cr,
Derek Foreman345c9f32015-06-03 15:53:28 -05002771 keyboard->focus_serial,
Neil Roberts96d790e2013-09-19 17:32:00 +01002772 surface->resource,
Derek Foreman345c9f32015-06-03 15:53:28 -05002773 &keyboard->keys);
Neil Roberts96d790e2013-09-19 17:32:00 +01002774
2775 /* If this is the first keyboard resource for this
2776 * client... */
Derek Foreman345c9f32015-06-03 15:53:28 -05002777 if (keyboard->focus_resource_list.prev ==
Neil Roberts96d790e2013-09-19 17:32:00 +01002778 wl_resource_get_link(cr))
2779 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002780 }
2781}
2782
2783static void
Alexandros Frantzisd7157842018-02-20 14:07:03 +02002784destroy_touch_resource(struct wl_resource *resource)
2785{
2786 struct weston_touch *touch = wl_resource_get_user_data(resource);
2787
2788 wl_list_remove(wl_resource_get_link(resource));
2789
2790 if (touch) {
2791 remove_input_resource_from_timestamps(resource,
2792 &touch->timestamps_list);
2793 }
2794}
2795
2796static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002797touch_release(struct wl_client *client, struct wl_resource *resource)
2798{
2799 wl_resource_destroy(resource);
2800}
2801
2802static const struct wl_touch_interface touch_interface = {
2803 touch_release
2804};
2805
2806static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002807seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2808 uint32_t id)
2809{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002810 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002811 /* We use the touch_state directly, which means we'll
2812 * give a wl_touch if the seat has ever had one - even though
2813 * the spec explicitly states that this request only takes effect
2814 * if the seat has the touch capability.
2815 *
2816 * This prevents a race between the compositor sending new
2817 * capabilities and the client trying to use the old ones.
2818 */
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002819 struct weston_touch *touch = seat ? seat->touch_state : NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002820 struct wl_resource *cr;
2821
Jason Ekstranda85118c2013-06-27 20:17:02 -05002822 cr = wl_resource_create(client, &wl_touch_interface,
2823 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002824 if (cr == NULL) {
2825 wl_client_post_no_memory(client);
2826 return;
2827 }
2828
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002829 wl_list_init(wl_resource_get_link(cr));
2830 wl_resource_set_implementation(cr, &touch_interface,
Alexandros Frantzisd7157842018-02-20 14:07:03 +02002831 touch, destroy_touch_resource);
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002832
2833 /* If we don't have a touch_state, the resource is inert, so there
2834 * is nothing more to set up */
2835 if (!touch)
2836 return;
2837
Derek Foreman1281a362015-07-31 16:55:32 -05002838 if (touch->focus &&
2839 wl_resource_get_client(touch->focus->surface->resource) == client) {
Chokshi, Mituld6697142015-10-09 08:28:47 +00002840 wl_list_insert(&touch->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002841 wl_resource_get_link(cr));
2842 } else {
Chokshi, Mituld6697142015-10-09 08:28:47 +00002843 wl_list_insert(&touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002844 wl_resource_get_link(cr));
2845 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002846}
2847
Quentin Glidicaab1d362016-03-13 17:49:08 +01002848static void
2849seat_release(struct wl_client *client, struct wl_resource *resource)
2850{
2851 wl_resource_destroy(resource);
2852}
2853
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002854static const struct wl_seat_interface seat_interface = {
2855 seat_get_pointer,
2856 seat_get_keyboard,
2857 seat_get_touch,
Quentin Glidicaab1d362016-03-13 17:49:08 +01002858 seat_release,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002859};
2860
2861static void
2862bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2863{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002864 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002865 struct wl_resource *resource;
2866 enum wl_seat_capability caps = 0;
2867
Jason Ekstranda85118c2013-06-27 20:17:02 -05002868 resource = wl_resource_create(client,
Derek Foreman1909c102015-11-26 14:17:47 -06002869 &wl_seat_interface, version, id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05002870 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05002871 wl_resource_set_implementation(resource, &seat_interface, data,
2872 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002873
Derek Foreman1281a362015-07-31 16:55:32 -05002874 if (weston_seat_get_pointer(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002875 caps |= WL_SEAT_CAPABILITY_POINTER;
Derek Foreman1281a362015-07-31 16:55:32 -05002876 if (weston_seat_get_keyboard(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002877 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Derek Foreman1281a362015-07-31 16:55:32 -05002878 if (weston_seat_get_touch(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002879 caps |= WL_SEAT_CAPABILITY_TOUCH;
2880
2881 wl_seat_send_capabilities(resource, caps);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04002882 if (version >= WL_SEAT_NAME_SINCE_VERSION)
Rob Bradforde445ae62013-05-31 18:09:51 +01002883 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002884}
2885
Jonas Ådahl30d61d82014-10-22 21:21:17 +02002886static void
2887relative_pointer_destroy(struct wl_client *client,
2888 struct wl_resource *resource)
2889{
2890 wl_resource_destroy(resource);
2891}
2892
2893static const struct zwp_relative_pointer_v1_interface relative_pointer_interface = {
2894 relative_pointer_destroy
2895};
2896
2897static void
2898relative_pointer_manager_destroy(struct wl_client *client,
2899 struct wl_resource *resource)
2900{
2901 wl_resource_destroy(resource);
2902}
2903
2904static void
2905relative_pointer_manager_get_relative_pointer(struct wl_client *client,
2906 struct wl_resource *resource,
2907 uint32_t id,
2908 struct wl_resource *pointer_resource)
2909{
2910 struct weston_pointer *pointer =
2911 wl_resource_get_user_data(pointer_resource);
2912 struct weston_pointer_client *pointer_client;
2913 struct wl_resource *cr;
2914
2915 cr = wl_resource_create(client, &zwp_relative_pointer_v1_interface,
2916 wl_resource_get_version(resource), id);
2917 if (cr == NULL) {
2918 wl_client_post_no_memory(client);
2919 return;
2920 }
2921
2922 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
2923 if (!pointer_client) {
2924 wl_client_post_no_memory(client);
2925 return;
2926 }
2927
2928 wl_list_insert(&pointer_client->relative_pointer_resources,
2929 wl_resource_get_link(cr));
2930 wl_resource_set_implementation(cr, &relative_pointer_interface,
2931 pointer,
2932 unbind_pointer_client_resource);
2933}
2934
2935static const struct zwp_relative_pointer_manager_v1_interface relative_pointer_manager = {
2936 relative_pointer_manager_destroy,
2937 relative_pointer_manager_get_relative_pointer,
2938};
2939
2940static void
2941bind_relative_pointer_manager(struct wl_client *client, void *data,
2942 uint32_t version, uint32_t id)
2943{
2944 struct weston_compositor *compositor = data;
2945 struct wl_resource *resource;
2946
2947 resource = wl_resource_create(client,
2948 &zwp_relative_pointer_manager_v1_interface,
2949 1, id);
2950
2951 wl_resource_set_implementation(resource, &relative_pointer_manager,
2952 compositor,
2953 NULL);
2954}
2955
Giulio Camuffo0358af42016-06-02 21:48:08 +03002956WL_EXPORT int
2957weston_compositor_set_xkb_rule_names(struct weston_compositor *ec,
2958 struct xkb_rule_names *names)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002959{
2960 if (ec->xkb_context == NULL) {
2961 ec->xkb_context = xkb_context_new(0);
2962 if (ec->xkb_context == NULL) {
2963 weston_log("failed to create XKB context\n");
2964 return -1;
2965 }
2966 }
2967
2968 if (names)
2969 ec->xkb_names = *names;
2970 if (!ec->xkb_names.rules)
2971 ec->xkb_names.rules = strdup("evdev");
2972 if (!ec->xkb_names.model)
2973 ec->xkb_names.model = strdup("pc105");
2974 if (!ec->xkb_names.layout)
2975 ec->xkb_names.layout = strdup("us");
2976
2977 return 0;
2978}
2979
Stefan Schmidtfda26522013-09-17 10:54:09 +01002980static void
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002981weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002982{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002983 if (--xkb_info->ref_count > 0)
2984 return;
2985
Ran Benitac9c74152014-08-19 23:59:52 +03002986 xkb_keymap_unref(xkb_info->keymap);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002987
2988 if (xkb_info->keymap_area)
2989 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2990 if (xkb_info->keymap_fd >= 0)
2991 close(xkb_info->keymap_fd);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002992 free(xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002993}
2994
2995void
2996weston_compositor_xkb_destroy(struct weston_compositor *ec)
2997{
2998 free((char *) ec->xkb_names.rules);
2999 free((char *) ec->xkb_names.model);
3000 free((char *) ec->xkb_names.layout);
3001 free((char *) ec->xkb_names.variant);
3002 free((char *) ec->xkb_names.options);
Stefan Schmidtfda26522013-09-17 10:54:09 +01003003
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003004 if (ec->xkb_info)
3005 weston_xkb_info_destroy(ec->xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003006 xkb_context_unref(ec->xkb_context);
3007}
3008
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003009static struct weston_xkb_info *
3010weston_xkb_info_create(struct xkb_keymap *keymap)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003011{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003012 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
3013 if (xkb_info == NULL)
3014 return NULL;
3015
Ran Benita2e1968f2014-08-19 23:59:51 +03003016 xkb_info->keymap = xkb_keymap_ref(keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003017 xkb_info->ref_count = 1;
3018
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003019 char *keymap_str;
3020
Ran Benita2e1968f2014-08-19 23:59:51 +03003021 xkb_info->shift_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3022 XKB_MOD_NAME_SHIFT);
3023 xkb_info->caps_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3024 XKB_MOD_NAME_CAPS);
3025 xkb_info->ctrl_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3026 XKB_MOD_NAME_CTRL);
3027 xkb_info->alt_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3028 XKB_MOD_NAME_ALT);
3029 xkb_info->mod2_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3030 "Mod2");
3031 xkb_info->mod3_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3032 "Mod3");
3033 xkb_info->super_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3034 XKB_MOD_NAME_LOGO);
3035 xkb_info->mod5_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3036 "Mod5");
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003037
Ran Benita2e1968f2014-08-19 23:59:51 +03003038 xkb_info->num_led = xkb_keymap_led_get_index(xkb_info->keymap,
3039 XKB_LED_NAME_NUM);
3040 xkb_info->caps_led = xkb_keymap_led_get_index(xkb_info->keymap,
3041 XKB_LED_NAME_CAPS);
3042 xkb_info->scroll_led = xkb_keymap_led_get_index(xkb_info->keymap,
3043 XKB_LED_NAME_SCROLL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003044
Ran Benita2e1968f2014-08-19 23:59:51 +03003045 keymap_str = xkb_keymap_get_as_string(xkb_info->keymap,
3046 XKB_KEYMAP_FORMAT_TEXT_V1);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003047 if (keymap_str == NULL) {
3048 weston_log("failed to get string version of keymap\n");
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003049 goto err_keymap;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003050 }
3051 xkb_info->keymap_size = strlen(keymap_str) + 1;
3052
3053 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
3054 if (xkb_info->keymap_fd < 0) {
3055 weston_log("creating a keymap file for %lu bytes failed: %m\n",
3056 (unsigned long) xkb_info->keymap_size);
3057 goto err_keymap_str;
3058 }
3059
3060 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
3061 PROT_READ | PROT_WRITE,
3062 MAP_SHARED, xkb_info->keymap_fd, 0);
3063 if (xkb_info->keymap_area == MAP_FAILED) {
3064 weston_log("failed to mmap() %lu bytes\n",
3065 (unsigned long) xkb_info->keymap_size);
3066 goto err_dev_zero;
3067 }
3068 strcpy(xkb_info->keymap_area, keymap_str);
3069 free(keymap_str);
3070
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003071 return xkb_info;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003072
3073err_dev_zero:
3074 close(xkb_info->keymap_fd);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003075err_keymap_str:
3076 free(keymap_str);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003077err_keymap:
Ran Benita2e1968f2014-08-19 23:59:51 +03003078 xkb_keymap_unref(xkb_info->keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003079 free(xkb_info);
3080 return NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003081}
3082
3083static int
3084weston_compositor_build_global_keymap(struct weston_compositor *ec)
3085{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003086 struct xkb_keymap *keymap;
3087
3088 if (ec->xkb_info != NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003089 return 0;
3090
Ran Benita2e1968f2014-08-19 23:59:51 +03003091 keymap = xkb_keymap_new_from_names(ec->xkb_context,
3092 &ec->xkb_names,
3093 0);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003094 if (keymap == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003095 weston_log("failed to compile global XKB keymap\n");
3096 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
3097 "options %s\n",
3098 ec->xkb_names.rules, ec->xkb_names.model,
3099 ec->xkb_names.layout, ec->xkb_names.variant,
3100 ec->xkb_names.options);
3101 return -1;
3102 }
3103
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003104 ec->xkb_info = weston_xkb_info_create(keymap);
Rui Matos73d93952013-10-24 19:28:41 +02003105 xkb_keymap_unref(keymap);
Stefan Schmidtfda26522013-09-17 10:54:09 +01003106 if (ec->xkb_info == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003107 return -1;
3108
3109 return 0;
3110}
3111
Rui Matos65196bc2013-10-10 19:44:19 +02003112WL_EXPORT void
3113weston_seat_update_keymap(struct weston_seat *seat, struct xkb_keymap *keymap)
3114{
Derek Foreman1281a362015-07-31 16:55:32 -05003115 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
3116
3117 if (!keyboard || !keymap)
Rui Matos65196bc2013-10-10 19:44:19 +02003118 return;
3119
Derek Foreman1281a362015-07-31 16:55:32 -05003120 xkb_keymap_unref(keyboard->pending_keymap);
3121 keyboard->pending_keymap = xkb_keymap_ref(keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02003122
Derek Foreman1281a362015-07-31 16:55:32 -05003123 if (keyboard->keys.size == 0)
Rui Matos65196bc2013-10-10 19:44:19 +02003124 update_keymap(seat);
Rui Matos65196bc2013-10-10 19:44:19 +02003125}
3126
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003127WL_EXPORT int
3128weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
3129{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003130 struct weston_keyboard *keyboard;
3131
Derek Foreman1281a362015-07-31 16:55:32 -05003132 if (seat->keyboard_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003133 seat->keyboard_device_count += 1;
3134 if (seat->keyboard_device_count == 1)
3135 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003136 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003137 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003138
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003139 keyboard = weston_keyboard_create();
3140 if (keyboard == NULL) {
3141 weston_log("failed to allocate weston keyboard struct\n");
3142 return -1;
3143 }
3144
Derek Foreman185d1582017-06-28 11:17:23 -05003145 if (keymap != NULL) {
3146 keyboard->xkb_info = weston_xkb_info_create(keymap);
3147 if (keyboard->xkb_info == NULL)
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02003148 goto err;
Derek Foreman185d1582017-06-28 11:17:23 -05003149 } else {
3150 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
3151 goto err;
3152 keyboard->xkb_info = seat->compositor->xkb_info;
3153 keyboard->xkb_info->ref_count++;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01003154 }
Derek Foreman185d1582017-06-28 11:17:23 -05003155
3156 keyboard->xkb_state.state = xkb_state_new(keyboard->xkb_info->keymap);
3157 if (keyboard->xkb_state.state == NULL) {
3158 weston_log("failed to initialise XKB state\n");
3159 goto err;
3160 }
3161
3162 keyboard->xkb_state.leds = 0;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01003163
Derek Foreman1281a362015-07-31 16:55:32 -05003164 seat->keyboard_state = keyboard;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02003165 seat->keyboard_device_count = 1;
3166 keyboard->seat = seat;
3167
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003168 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003169
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003170 return 0;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02003171
3172err:
3173 if (keyboard->xkb_info)
3174 weston_xkb_info_destroy(keyboard->xkb_info);
3175 free(keyboard);
3176
3177 return -1;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003178}
3179
Jonas Ådahl91fed542013-12-03 09:14:27 +01003180static void
3181weston_keyboard_reset_state(struct weston_keyboard *keyboard)
3182{
3183 struct weston_seat *seat = keyboard->seat;
3184 struct xkb_state *state;
3185
Derek Foreman185d1582017-06-28 11:17:23 -05003186 state = xkb_state_new(keyboard->xkb_info->keymap);
3187 if (!state) {
3188 weston_log("failed to reset XKB state\n");
3189 return;
Jonas Ådahl91fed542013-12-03 09:14:27 +01003190 }
Derek Foreman185d1582017-06-28 11:17:23 -05003191 xkb_state_unref(keyboard->xkb_state.state);
3192 keyboard->xkb_state.state = state;
3193
3194 keyboard->xkb_state.leds = 0;
Jonas Ådahl91fed542013-12-03 09:14:27 +01003195
3196 seat->modifier_state = 0;
3197}
3198
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003199WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003200weston_seat_release_keyboard(struct weston_seat *seat)
3201{
3202 seat->keyboard_device_count--;
Derek Foremand621df22014-11-19 11:04:12 -06003203 assert(seat->keyboard_device_count >= 0);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003204 if (seat->keyboard_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05003205 weston_keyboard_set_focus(seat->keyboard_state, NULL);
3206 weston_keyboard_cancel_grab(seat->keyboard_state);
3207 weston_keyboard_reset_state(seat->keyboard_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003208 seat_send_updated_caps(seat);
3209 }
3210}
3211
3212WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003213weston_seat_init_pointer(struct weston_seat *seat)
3214{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003215 struct weston_pointer *pointer;
3216
Derek Foreman1281a362015-07-31 16:55:32 -05003217 if (seat->pointer_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003218 seat->pointer_device_count += 1;
3219 if (seat->pointer_device_count == 1)
3220 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003221 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003222 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003223
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003224 pointer = weston_pointer_create(seat);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003225 if (pointer == NULL)
3226 return;
3227
Derek Foreman1281a362015-07-31 16:55:32 -05003228 seat->pointer_state = pointer;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003229 seat->pointer_device_count = 1;
3230 pointer->seat = seat;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003231
3232 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003233}
3234
3235WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003236weston_seat_release_pointer(struct weston_seat *seat)
3237{
Derek Foreman1281a362015-07-31 16:55:32 -05003238 struct weston_pointer *pointer = seat->pointer_state;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003239
3240 seat->pointer_device_count--;
3241 if (seat->pointer_device_count == 0) {
Derek Foremanf9318d12015-05-11 15:40:11 -05003242 weston_pointer_clear_focus(pointer);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003243 weston_pointer_cancel_grab(pointer);
Jonas Ådahl630bae82013-10-17 23:04:06 +02003244
Jonas Ådahla4932742013-10-17 23:04:07 +02003245 if (pointer->sprite)
3246 pointer_unmap_sprite(pointer);
3247
Jonas Ådahl3e12e632013-12-02 22:05:05 +01003248 weston_pointer_reset_state(pointer);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003249 seat_send_updated_caps(seat);
Derek Foremanfd5ca512015-01-07 15:00:25 -06003250
3251 /* seat->pointer is intentionally not destroyed so that
3252 * a newly attached pointer on this seat will retain
3253 * the previous cursor co-ordinates.
3254 */
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003255 }
3256}
3257
3258WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003259weston_seat_init_touch(struct weston_seat *seat)
3260{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003261 struct weston_touch *touch;
3262
Derek Foreman1281a362015-07-31 16:55:32 -05003263 if (seat->touch_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003264 seat->touch_device_count += 1;
3265 if (seat->touch_device_count == 1)
3266 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003267 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003268 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003269
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003270 touch = weston_touch_create();
3271 if (touch == NULL)
3272 return;
3273
Derek Foreman1281a362015-07-31 16:55:32 -05003274 seat->touch_state = touch;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003275 seat->touch_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003276 touch->seat = seat;
3277
3278 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003279}
3280
3281WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003282weston_seat_release_touch(struct weston_seat *seat)
3283{
3284 seat->touch_device_count--;
3285 if (seat->touch_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05003286 weston_touch_set_focus(seat->touch_state, NULL);
3287 weston_touch_cancel_grab(seat->touch_state);
3288 weston_touch_reset_state(seat->touch_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003289 seat_send_updated_caps(seat);
3290 }
3291}
3292
3293WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003294weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
3295 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003296{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003297 memset(seat, 0, sizeof *seat);
3298
Kristian Høgsberge3148752013-05-06 23:19:49 -04003299 seat->selection_data_source = NULL;
3300 wl_list_init(&seat->base_resource_list);
3301 wl_signal_init(&seat->selection_signal);
3302 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04003303 wl_signal_init(&seat->destroy_signal);
Jason Ekstranda4ab5422014-04-02 19:53:45 -05003304 wl_signal_init(&seat->updated_caps_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003305
Peter Hutterer87743e92016-01-18 16:38:22 +10003306 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 5,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003307 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003308
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003309 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003310 seat->modifier_state = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003311 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003312
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003313 wl_list_insert(ec->seat_list.prev, &seat->link);
3314
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003315 clipboard_create(seat);
3316
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003317 wl_signal_emit(&ec->seat_created_signal, seat);
3318}
3319
3320WL_EXPORT void
3321weston_seat_release(struct weston_seat *seat)
3322{
Alexandros Frantzis8480d132018-02-15 13:07:09 +02003323 struct wl_resource *resource;
3324
3325 wl_resource_for_each(resource, &seat->base_resource_list) {
3326 wl_resource_set_user_data(resource, NULL);
3327 }
3328
3329 wl_resource_for_each(resource, &seat->drag_resource_list) {
3330 wl_resource_set_user_data(resource, NULL);
3331 }
3332
3333 wl_list_remove(&seat->base_resource_list);
3334 wl_list_remove(&seat->drag_resource_list);
3335
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003336 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003337
Jonas Ådahl1afb2382014-01-03 19:46:51 +01003338 if (seat->saved_kbd_focus)
3339 wl_list_remove(&seat->saved_kbd_focus_listener.link);
3340
Derek Foreman1281a362015-07-31 16:55:32 -05003341 if (seat->pointer_state)
3342 weston_pointer_destroy(seat->pointer_state);
3343 if (seat->keyboard_state)
3344 weston_keyboard_destroy(seat->keyboard_state);
3345 if (seat->touch_state)
3346 weston_touch_destroy(seat->touch_state);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003347
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003348 free (seat->seat_name);
3349
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003350 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04003351
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003352 wl_signal_emit(&seat->destroy_signal, seat);
3353}
Derek Foreman1281a362015-07-31 16:55:32 -05003354
3355/** Get a seat's keyboard pointer
3356 *
3357 * \param seat The seat to query
3358 * \return The seat's keyboard pointer, or NULL if no keyboard is present
3359 *
3360 * The keyboard pointer for a seat isn't freed when all keyboards are removed,
3361 * so it should only be used when the seat's keyboard_device_count is greater
3362 * than zero. This function does that test and only returns a pointer
3363 * when a keyboard is present.
3364 */
3365WL_EXPORT struct weston_keyboard *
3366weston_seat_get_keyboard(struct weston_seat *seat)
3367{
3368 if (!seat)
3369 return NULL;
3370
3371 if (seat->keyboard_device_count)
3372 return seat->keyboard_state;
3373
3374 return NULL;
3375}
3376
3377/** Get a seat's pointer pointer
3378 *
3379 * \param seat The seat to query
3380 * \return The seat's pointer pointer, or NULL if no pointer device is present
3381 *
3382 * The pointer pointer for a seat isn't freed when all mice are removed,
3383 * so it should only be used when the seat's pointer_device_count is greater
3384 * than zero. This function does that test and only returns a pointer
3385 * when a pointing device is present.
3386 */
3387WL_EXPORT struct weston_pointer *
3388weston_seat_get_pointer(struct weston_seat *seat)
3389{
3390 if (!seat)
3391 return NULL;
3392
3393 if (seat->pointer_device_count)
3394 return seat->pointer_state;
3395
3396 return NULL;
3397}
3398
Jonas Ådahld3414f22016-07-22 17:56:31 +08003399static const struct zwp_locked_pointer_v1_interface locked_pointer_interface;
3400static const struct zwp_confined_pointer_v1_interface confined_pointer_interface;
3401
3402static enum pointer_constraint_type
3403pointer_constraint_get_type(struct weston_pointer_constraint *constraint)
3404{
3405 if (wl_resource_instance_of(constraint->resource,
3406 &zwp_locked_pointer_v1_interface,
3407 &locked_pointer_interface)) {
3408 return POINTER_CONSTRAINT_TYPE_LOCK;
3409 } else if (wl_resource_instance_of(constraint->resource,
3410 &zwp_confined_pointer_v1_interface,
3411 &confined_pointer_interface)) {
3412 return POINTER_CONSTRAINT_TYPE_CONFINE;
3413 }
3414
3415 abort();
3416 return 0;
3417}
3418
3419static void
3420pointer_constraint_notify_activated(struct weston_pointer_constraint *constraint)
3421{
3422 struct wl_resource *resource = constraint->resource;
3423
3424 switch (pointer_constraint_get_type(constraint)) {
3425 case POINTER_CONSTRAINT_TYPE_LOCK:
3426 zwp_locked_pointer_v1_send_locked(resource);
3427 break;
3428 case POINTER_CONSTRAINT_TYPE_CONFINE:
3429 zwp_confined_pointer_v1_send_confined(resource);
3430 break;
3431 }
3432}
3433
3434static void
3435pointer_constraint_notify_deactivated(struct weston_pointer_constraint *constraint)
3436{
3437 struct wl_resource *resource = constraint->resource;
3438
3439 switch (pointer_constraint_get_type(constraint)) {
3440 case POINTER_CONSTRAINT_TYPE_LOCK:
3441 zwp_locked_pointer_v1_send_unlocked(resource);
3442 break;
3443 case POINTER_CONSTRAINT_TYPE_CONFINE:
3444 zwp_confined_pointer_v1_send_unconfined(resource);
3445 break;
3446 }
3447}
3448
3449static struct weston_pointer_constraint *
3450get_pointer_constraint_for_pointer(struct weston_surface *surface,
3451 struct weston_pointer *pointer)
3452{
3453 struct weston_pointer_constraint *constraint;
3454
3455 wl_list_for_each(constraint, &surface->pointer_constraints, link) {
3456 if (constraint->pointer == pointer)
3457 return constraint;
3458 }
3459
3460 return NULL;
3461}
3462
Derek Foreman1281a362015-07-31 16:55:32 -05003463/** Get a seat's touch pointer
3464 *
3465 * \param seat The seat to query
3466 * \return The seat's touch pointer, or NULL if no touch device is present
3467 *
3468 * The touch pointer for a seat isn't freed when all touch devices are removed,
3469 * so it should only be used when the seat's touch_device_count is greater
3470 * than zero. This function does that test and only returns a pointer
3471 * when a touch device is present.
3472 */
3473WL_EXPORT struct weston_touch *
3474weston_seat_get_touch(struct weston_seat *seat)
3475{
3476 if (!seat)
3477 return NULL;
3478
3479 if (seat->touch_device_count)
3480 return seat->touch_state;
3481
3482 return NULL;
3483}
Bryce Harrington24f917e2016-06-29 19:04:07 -07003484
3485/** Sets the keyboard focus to the given surface
3486 *
3487 * \param seat The seat to query
3488 */
3489WL_EXPORT void
3490weston_seat_set_keyboard_focus(struct weston_seat *seat,
3491 struct weston_surface *surface)
3492{
3493 struct weston_compositor *compositor = seat->compositor;
3494 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003495 struct weston_surface_activation_data activation_data;
Bryce Harrington24f917e2016-06-29 19:04:07 -07003496
Jonas Ådahlef8e1c32016-03-15 20:28:51 +08003497 if (keyboard && keyboard->focus != surface) {
Bryce Harrington24f917e2016-06-29 19:04:07 -07003498 weston_keyboard_set_focus(keyboard, surface);
3499 wl_data_device_set_keyboard_focus(seat);
3500 }
3501
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003502 inc_activate_serial(compositor);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003503
3504 activation_data = (struct weston_surface_activation_data) {
3505 .surface = surface,
3506 .seat = seat,
3507 };
3508 wl_signal_emit(&compositor->activate_signal, &activation_data);
Bryce Harrington24f917e2016-06-29 19:04:07 -07003509}
Jonas Ådahl30d61d82014-10-22 21:21:17 +02003510
Jonas Ådahld3414f22016-07-22 17:56:31 +08003511static void
3512enable_pointer_constraint(struct weston_pointer_constraint *constraint,
3513 struct weston_view *view)
3514{
3515 assert(constraint->view == NULL);
3516 constraint->view = view;
3517 pointer_constraint_notify_activated(constraint);
3518 weston_pointer_start_grab(constraint->pointer, &constraint->grab);
3519 wl_list_remove(&constraint->surface_destroy_listener.link);
3520 wl_list_init(&constraint->surface_destroy_listener.link);
3521}
3522
3523static bool
3524is_pointer_constraint_enabled(struct weston_pointer_constraint *constraint)
3525{
3526 return constraint->view != NULL;
3527}
3528
3529static void
3530weston_pointer_constraint_disable(struct weston_pointer_constraint *constraint)
3531{
3532 constraint->view = NULL;
3533 pointer_constraint_notify_deactivated(constraint);
3534 weston_pointer_end_grab(constraint->grab.pointer);
3535}
3536
3537void
3538weston_pointer_constraint_destroy(struct weston_pointer_constraint *constraint)
3539{
3540 if (is_pointer_constraint_enabled(constraint))
3541 weston_pointer_constraint_disable(constraint);
3542
3543 wl_list_remove(&constraint->pointer_destroy_listener.link);
3544 wl_list_remove(&constraint->surface_destroy_listener.link);
3545 wl_list_remove(&constraint->surface_commit_listener.link);
3546 wl_list_remove(&constraint->surface_activate_listener.link);
3547
3548 wl_resource_set_user_data(constraint->resource, NULL);
3549 pixman_region32_fini(&constraint->region);
3550 wl_list_remove(&constraint->link);
3551 free(constraint);
3552}
3553
3554static void
3555disable_pointer_constraint(struct weston_pointer_constraint *constraint)
3556{
3557 switch (constraint->lifetime) {
3558 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT:
3559 weston_pointer_constraint_destroy(constraint);
3560 break;
3561 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT:
3562 weston_pointer_constraint_disable(constraint);
3563 break;
3564 }
3565}
3566
3567static bool
3568is_within_constraint_region(struct weston_pointer_constraint *constraint,
3569 wl_fixed_t sx, wl_fixed_t sy)
3570{
3571 struct weston_surface *surface = constraint->surface;
3572 pixman_region32_t constraint_region;
3573 bool result;
3574
3575 pixman_region32_init(&constraint_region);
3576 pixman_region32_intersect(&constraint_region,
3577 &surface->input,
3578 &constraint->region);
3579 result = pixman_region32_contains_point(&constraint_region,
3580 wl_fixed_to_int(sx),
3581 wl_fixed_to_int(sy),
3582 NULL);
3583 pixman_region32_fini(&constraint_region);
3584
3585 return result;
3586}
3587
3588static void
3589maybe_enable_pointer_constraint(struct weston_pointer_constraint *constraint)
3590{
3591 struct weston_surface *surface = constraint->surface;
3592 struct weston_view *vit;
3593 struct weston_view *view = NULL;
3594 struct weston_pointer *pointer = constraint->pointer;
3595 struct weston_keyboard *keyboard;
3596 struct weston_seat *seat = pointer->seat;
3597 int32_t x, y;
3598
3599 /* Postpone if no view of the surface was most recently clicked. */
3600 wl_list_for_each(vit, &surface->views, surface_link) {
3601 if (vit->click_to_activate_serial ==
3602 surface->compositor->activate_serial) {
3603 view = vit;
3604 }
3605 }
3606 if (view == NULL)
3607 return;
3608
3609 /* Postpone if surface doesn't have keyboard focus. */
3610 keyboard = weston_seat_get_keyboard(seat);
3611 if (!keyboard || keyboard->focus != surface)
3612 return;
3613
3614 /* Postpone constraint if the pointer is not within the
3615 * constraint region.
3616 */
3617 weston_view_from_global(view,
3618 wl_fixed_to_int(pointer->x),
3619 wl_fixed_to_int(pointer->y),
3620 &x, &y);
3621 if (!is_within_constraint_region(constraint,
3622 wl_fixed_from_int(x),
3623 wl_fixed_from_int(y)))
3624 return;
3625
3626 enable_pointer_constraint(constraint, view);
3627}
3628
3629static void
3630locked_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
3631{
3632}
3633
3634static void
3635locked_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02003636 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003637 struct weston_pointer_motion_event *event)
3638{
Quentin Glidiccde13452016-08-12 10:41:32 +02003639 pointer_send_relative_motion(grab->pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08003640}
3641
3642static void
3643locked_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003644 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003645 uint32_t button,
3646 uint32_t state_w)
3647{
3648 weston_pointer_send_button(grab->pointer, time, button, state_w);
3649}
3650
3651static void
3652locked_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02003653 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003654 struct weston_pointer_axis_event *event)
3655{
3656 weston_pointer_send_axis(grab->pointer, time, event);
3657}
3658
3659static void
3660locked_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
3661 uint32_t source)
3662{
3663 weston_pointer_send_axis_source(grab->pointer, source);
3664}
3665
3666static void
3667locked_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
3668{
3669 weston_pointer_send_frame(grab->pointer);
3670}
3671
3672static void
3673locked_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
3674{
3675 struct weston_pointer_constraint *constraint =
3676 container_of(grab, struct weston_pointer_constraint, grab);
3677
3678 disable_pointer_constraint(constraint);
3679}
3680
3681static const struct weston_pointer_grab_interface
3682 locked_pointer_grab_interface = {
3683 locked_pointer_grab_pointer_focus,
3684 locked_pointer_grab_pointer_motion,
3685 locked_pointer_grab_pointer_button,
3686 locked_pointer_grab_pointer_axis,
3687 locked_pointer_grab_pointer_axis_source,
3688 locked_pointer_grab_pointer_frame,
3689 locked_pointer_grab_pointer_cancel,
3690};
3691
3692static void
3693pointer_constraint_constrain_resource_destroyed(struct wl_resource *resource)
3694{
3695 struct weston_pointer_constraint *constraint =
3696 wl_resource_get_user_data(resource);
3697
3698 if (!constraint)
3699 return;
3700
3701 weston_pointer_constraint_destroy(constraint);
3702}
3703
3704static void
3705pointer_constraint_surface_activate(struct wl_listener *listener, void *data)
3706{
3707 struct weston_surface_activation_data *activation = data;
3708 struct weston_pointer *pointer;
3709 struct weston_surface *focus = activation->surface;
3710 struct weston_pointer_constraint *constraint =
3711 container_of(listener, struct weston_pointer_constraint,
3712 surface_activate_listener);
3713 bool is_constraint_surface;
3714
3715 pointer = weston_seat_get_pointer(activation->seat);
3716 if (!pointer)
3717 return;
3718
3719 is_constraint_surface =
3720 get_pointer_constraint_for_pointer(focus, pointer) == constraint;
3721
3722 if (is_constraint_surface &&
3723 !is_pointer_constraint_enabled(constraint))
3724 maybe_enable_pointer_constraint(constraint);
3725 else if (!is_constraint_surface &&
3726 is_pointer_constraint_enabled(constraint))
3727 disable_pointer_constraint(constraint);
3728}
3729
3730static void
3731pointer_constraint_pointer_destroyed(struct wl_listener *listener, void *data)
3732{
3733 struct weston_pointer_constraint *constraint =
3734 container_of(listener, struct weston_pointer_constraint,
3735 pointer_destroy_listener);
3736
3737 weston_pointer_constraint_destroy(constraint);
3738}
3739
3740static void
3741pointer_constraint_surface_destroyed(struct wl_listener *listener, void *data)
3742{
3743 struct weston_pointer_constraint *constraint =
3744 container_of(listener, struct weston_pointer_constraint,
3745 surface_destroy_listener);
3746
3747 weston_pointer_constraint_destroy(constraint);
3748}
3749
3750static void
3751pointer_constraint_surface_committed(struct wl_listener *listener, void *data)
3752{
3753 struct weston_pointer_constraint *constraint =
3754 container_of(listener, struct weston_pointer_constraint,
3755 surface_commit_listener);
3756
3757 if (constraint->region_is_pending) {
3758 constraint->region_is_pending = false;
3759 pixman_region32_copy(&constraint->region,
3760 &constraint->region_pending);
3761 pixman_region32_fini(&constraint->region_pending);
3762 pixman_region32_init(&constraint->region_pending);
3763 }
3764
3765 if (constraint->hint_is_pending) {
3766 constraint->hint_is_pending = false;
3767
3768 constraint->hint_is_pending = true;
3769 constraint->hint_x = constraint->hint_x_pending;
3770 constraint->hint_y = constraint->hint_y_pending;
3771 }
3772
3773 if (pointer_constraint_get_type(constraint) ==
3774 POINTER_CONSTRAINT_TYPE_CONFINE &&
3775 is_pointer_constraint_enabled(constraint))
3776 maybe_warp_confined_pointer(constraint);
3777}
3778
3779static struct weston_pointer_constraint *
3780weston_pointer_constraint_create(struct weston_surface *surface,
3781 struct weston_pointer *pointer,
3782 struct weston_region *region,
3783 enum zwp_pointer_constraints_v1_lifetime lifetime,
3784 struct wl_resource *cr,
3785 const struct weston_pointer_grab_interface *grab_interface)
3786{
3787 struct weston_pointer_constraint *constraint;
3788
3789 constraint = zalloc(sizeof *constraint);
3790 if (!constraint)
3791 return NULL;
3792
3793 constraint->lifetime = lifetime;
3794 pixman_region32_init(&constraint->region);
3795 pixman_region32_init(&constraint->region_pending);
3796 wl_list_insert(&surface->pointer_constraints, &constraint->link);
3797 constraint->surface = surface;
3798 constraint->pointer = pointer;
3799 constraint->resource = cr;
3800 constraint->grab.interface = grab_interface;
3801 if (region) {
3802 pixman_region32_copy(&constraint->region,
3803 &region->region);
3804 } else {
3805 pixman_region32_fini(&constraint->region);
3806 region_init_infinite(&constraint->region);
3807 }
3808
3809 constraint->surface_activate_listener.notify =
3810 pointer_constraint_surface_activate;
3811 constraint->surface_destroy_listener.notify =
3812 pointer_constraint_surface_destroyed;
3813 constraint->surface_commit_listener.notify =
3814 pointer_constraint_surface_committed;
3815 constraint->pointer_destroy_listener.notify =
3816 pointer_constraint_pointer_destroyed;
3817
3818 wl_signal_add(&surface->compositor->activate_signal,
3819 &constraint->surface_activate_listener);
3820 wl_signal_add(&pointer->destroy_signal,
3821 &constraint->pointer_destroy_listener);
3822 wl_signal_add(&surface->destroy_signal,
3823 &constraint->surface_destroy_listener);
3824 wl_signal_add(&surface->commit_signal,
3825 &constraint->surface_commit_listener);
3826
3827 return constraint;
3828}
3829
3830static void
3831init_pointer_constraint(struct wl_resource *pointer_constraints_resource,
3832 uint32_t id,
3833 struct weston_surface *surface,
3834 struct weston_pointer *pointer,
3835 struct weston_region *region,
3836 enum zwp_pointer_constraints_v1_lifetime lifetime,
3837 const struct wl_interface *interface,
3838 const void *implementation,
3839 const struct weston_pointer_grab_interface *grab_interface)
3840{
3841 struct wl_client *client =
3842 wl_resource_get_client(pointer_constraints_resource);
3843 struct wl_resource *cr;
3844 struct weston_pointer_constraint *constraint;
3845
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02003846 if (pointer && get_pointer_constraint_for_pointer(surface, pointer)) {
Jonas Ådahld3414f22016-07-22 17:56:31 +08003847 wl_resource_post_error(pointer_constraints_resource,
3848 ZWP_POINTER_CONSTRAINTS_V1_ERROR_ALREADY_CONSTRAINED,
3849 "the pointer has a lock/confine request on this surface");
3850 return;
3851 }
3852
3853 cr = wl_resource_create(client, interface,
3854 wl_resource_get_version(pointer_constraints_resource),
3855 id);
3856 if (cr == NULL) {
3857 wl_client_post_no_memory(client);
3858 return;
3859 }
3860
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02003861 if (pointer) {
3862 constraint = weston_pointer_constraint_create(surface, pointer,
3863 region, lifetime,
3864 cr, grab_interface);
3865 if (constraint == NULL) {
3866 wl_client_post_no_memory(client);
3867 return;
3868 }
3869 } else {
3870 constraint = NULL;
Jonas Ådahld3414f22016-07-22 17:56:31 +08003871 }
3872
3873 wl_resource_set_implementation(cr, implementation, constraint,
3874 pointer_constraint_constrain_resource_destroyed);
3875
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02003876 if (constraint)
3877 maybe_enable_pointer_constraint(constraint);
Jonas Ådahld3414f22016-07-22 17:56:31 +08003878}
3879
3880static void
3881pointer_constraints_destroy(struct wl_client *client,
3882 struct wl_resource *resource)
3883{
3884 wl_resource_destroy(resource);
3885}
3886
3887static void
3888locked_pointer_destroy(struct wl_client *client,
3889 struct wl_resource *resource)
3890{
3891 struct weston_pointer_constraint *constraint =
3892 wl_resource_get_user_data(resource);
3893 wl_fixed_t x, y;
3894
3895 if (constraint && constraint->view && constraint->hint_is_pending &&
3896 is_within_constraint_region(constraint,
3897 constraint->hint_x,
3898 constraint->hint_y)) {
3899 weston_view_to_global_fixed(constraint->view,
3900 constraint->hint_x,
3901 constraint->hint_y,
3902 &x, &y);
3903 weston_pointer_move_to(constraint->pointer, x, y);
3904 }
3905 wl_resource_destroy(resource);
3906}
3907
3908static void
3909locked_pointer_set_cursor_position_hint(struct wl_client *client,
3910 struct wl_resource *resource,
3911 wl_fixed_t surface_x,
3912 wl_fixed_t surface_y)
3913{
3914 struct weston_pointer_constraint *constraint =
3915 wl_resource_get_user_data(resource);
3916
3917 /* Ignore a set cursor hint that was sent after the lock was cancelled.
3918 */
3919 if (!constraint ||
3920 !constraint->resource ||
3921 constraint->resource != resource)
3922 return;
3923
3924 constraint->hint_is_pending = true;
3925 constraint->hint_x_pending = surface_x;
3926 constraint->hint_y_pending = surface_y;
3927}
3928
3929static void
3930locked_pointer_set_region(struct wl_client *client,
3931 struct wl_resource *resource,
3932 struct wl_resource *region_resource)
3933{
3934 struct weston_pointer_constraint *constraint =
3935 wl_resource_get_user_data(resource);
3936 struct weston_region *region = region_resource ?
3937 wl_resource_get_user_data(region_resource) : NULL;
3938
3939 if (!constraint)
3940 return;
3941
3942 if (region) {
3943 pixman_region32_copy(&constraint->region_pending,
3944 &region->region);
3945 } else {
3946 pixman_region32_fini(&constraint->region_pending);
3947 region_init_infinite(&constraint->region_pending);
3948 }
3949 constraint->region_is_pending = true;
3950}
3951
3952
3953static const struct zwp_locked_pointer_v1_interface locked_pointer_interface = {
3954 locked_pointer_destroy,
3955 locked_pointer_set_cursor_position_hint,
3956 locked_pointer_set_region,
3957};
3958
3959static void
3960pointer_constraints_lock_pointer(struct wl_client *client,
3961 struct wl_resource *resource,
3962 uint32_t id,
3963 struct wl_resource *surface_resource,
3964 struct wl_resource *pointer_resource,
3965 struct wl_resource *region_resource,
3966 uint32_t lifetime)
3967{
3968 struct weston_surface *surface =
3969 wl_resource_get_user_data(surface_resource);
3970 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
3971 struct weston_region *region = region_resource ?
3972 wl_resource_get_user_data(region_resource) : NULL;
3973
3974 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
3975 &zwp_locked_pointer_v1_interface,
3976 &locked_pointer_interface,
3977 &locked_pointer_grab_interface);
3978}
3979
3980static void
3981confined_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
3982{
3983}
3984
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08003985static double
3986vec2d_cross_product(struct vec2d a, struct vec2d b)
3987{
3988 return a.x * b.y - a.y * b.x;
3989}
3990
3991static struct vec2d
3992vec2d_add(struct vec2d a, struct vec2d b)
3993{
3994 return (struct vec2d) {
3995 .x = a.x + b.x,
3996 .y = a.y + b.y,
3997 };
3998}
3999
4000static struct vec2d
4001vec2d_subtract(struct vec2d a, struct vec2d b)
4002{
4003 return (struct vec2d) {
4004 .x = a.x - b.x,
4005 .y = a.y - b.y,
4006 };
4007}
4008
4009static struct vec2d
4010vec2d_multiply_constant(double c, struct vec2d a)
4011{
4012 return (struct vec2d) {
4013 .x = c * a.x,
4014 .y = c * a.y,
4015 };
4016}
4017
4018static bool
4019lines_intersect(struct line *line1, struct line *line2,
4020 struct vec2d *intersection)
4021{
4022 struct vec2d p = line1->a;
4023 struct vec2d r = vec2d_subtract(line1->b, line1->a);
4024 struct vec2d q = line2->a;
4025 struct vec2d s = vec2d_subtract(line2->b, line2->a);
4026 double rxs;
4027 double sxr;
4028 double t;
4029 double u;
4030
4031 /*
4032 * The line (p, r) and (q, s) intersects where
4033 *
4034 * p + t r = q + u s
4035 *
4036 * Calculate t:
4037 *
4038 * (p + t r) × s = (q + u s) × s
4039 * p × s + t (r × s) = q × s + u (s × s)
4040 * p × s + t (r × s) = q × s
4041 * t (r × s) = q × s - p × s
4042 * t (r × s) = (q - p) × s
4043 * t = ((q - p) × s) / (r × s)
4044 *
4045 * Using the same method, for u we get:
4046 *
4047 * u = ((p - q) × r) / (s × r)
4048 */
4049
4050 rxs = vec2d_cross_product(r, s);
4051 sxr = vec2d_cross_product(s, r);
4052
4053 /* If r × s = 0 then the lines are either parallel or collinear. */
4054 if (fabs(rxs) < DBL_MIN)
4055 return false;
4056
4057 t = vec2d_cross_product(vec2d_subtract(q, p), s) / rxs;
4058 u = vec2d_cross_product(vec2d_subtract(p, q), r) / sxr;
4059
4060 /* The lines only intersect if 0 ≤ t ≤ 1 and 0 ≤ u ≤ 1. */
4061 if (t < 0.0 || t > 1.0 || u < 0.0 || u > 1.0)
4062 return false;
4063
4064 *intersection = vec2d_add(p, vec2d_multiply_constant(t, r));
4065 return true;
4066}
4067
4068static struct border *
4069add_border(struct wl_array *array,
4070 double x1, double y1,
4071 double x2, double y2,
4072 enum motion_direction blocking_dir)
4073{
4074 struct border *border = wl_array_add(array, sizeof *border);
4075
4076 *border = (struct border) {
4077 .line = (struct line) {
4078 .a = (struct vec2d) {
4079 .x = x1,
4080 .y = y1,
4081 },
4082 .b = (struct vec2d) {
4083 .x = x2,
4084 .y = y2,
4085 },
4086 },
4087 .blocking_dir = blocking_dir,
4088 };
4089
4090 return border;
4091}
4092
4093static int
4094compare_lines_x(const void *a, const void *b)
4095{
4096 const struct border *border_a = a;
4097 const struct border *border_b = b;
4098
4099
4100 if (border_a->line.a.x == border_b->line.a.x)
4101 return border_a->line.b.x < border_b->line.b.x;
4102 else
4103 return border_a->line.a.x > border_b->line.a.x;
4104}
4105
4106static void
4107add_non_overlapping_edges(pixman_box32_t *boxes,
4108 int band_above_start,
4109 int band_below_start,
4110 int band_below_end,
4111 struct wl_array *borders)
4112{
4113 int i;
4114 struct wl_array band_merge;
4115 struct border *border;
4116 struct border *prev_border;
4117 struct border *new_border;
4118
4119 wl_array_init(&band_merge);
4120
4121 /* Add bottom band of previous row, and top band of current row, and
4122 * sort them so lower left x coordinate comes first. If there are two
4123 * borders with the same left x coordinate, the wider one comes first.
4124 */
4125 for (i = band_above_start; i < band_below_start; i++) {
4126 pixman_box32_t *box = &boxes[i];
4127 add_border(&band_merge, box->x1, box->y2, box->x2, box->y2,
4128 MOTION_DIRECTION_POSITIVE_Y);
4129 }
4130 for (i = band_below_start; i < band_below_end; i++) {
4131 pixman_box32_t *box= &boxes[i];
4132 add_border(&band_merge, box->x1, box->y1, box->x2, box->y1,
4133 MOTION_DIRECTION_NEGATIVE_Y);
4134 }
4135 qsort(band_merge.data,
4136 band_merge.size / sizeof *border,
4137 sizeof *border,
4138 compare_lines_x);
4139
4140 /* Combine the two combined bands so that any overlapping border is
4141 * eliminated. */
4142 prev_border = NULL;
4143 wl_array_for_each(border, &band_merge) {
4144 assert(border->line.a.y == border->line.b.y);
4145 assert(!prev_border ||
4146 prev_border->line.a.y == border->line.a.y);
4147 assert(!prev_border ||
4148 (prev_border->line.a.x != border->line.a.x ||
4149 prev_border->line.b.x != border->line.b.x));
4150 assert(!prev_border ||
4151 prev_border->line.a.x <= border->line.a.x);
4152
4153 if (prev_border &&
4154 prev_border->line.a.x == border->line.a.x) {
4155 /*
4156 * ------------ +
4157 * ------- =
4158 * [ ]-----
4159 */
4160 prev_border->line.a.x = border->line.b.x;
4161 } else if (prev_border &&
4162 prev_border->line.b.x == border->line.b.x) {
4163 /*
4164 * ------------ +
4165 * ------ =
4166 * ------[ ]
4167 */
4168 prev_border->line.b.x = border->line.a.x;
4169 } else if (prev_border &&
4170 prev_border->line.b.x == border->line.a.x) {
4171 /*
4172 * -------- +
4173 * ------ =
4174 * --------------
4175 */
4176 prev_border->line.b.x = border->line.b.x;
4177 } else if (prev_border &&
4178 prev_border->line.b.x >= border->line.a.x) {
4179 /*
4180 * --------------- +
4181 * ------ =
4182 * -----[ ]----
4183 */
4184 new_border = add_border(borders,
4185 border->line.b.x,
4186 border->line.b.y,
4187 prev_border->line.b.x,
4188 prev_border->line.b.y,
4189 prev_border->blocking_dir);
4190 prev_border->line.b.x = border->line.a.x;
4191 prev_border = new_border;
4192 } else {
4193 assert(!prev_border ||
4194 prev_border->line.b.x < border->line.a.x);
4195 /*
4196 * First border or non-overlapping.
4197 *
4198 * ----- +
4199 * ----- =
4200 * ----- -----
4201 */
4202 new_border = wl_array_add(borders, sizeof *border);
4203 *new_border = *border;
4204 prev_border = new_border;
4205 }
4206 }
4207
4208 wl_array_release(&band_merge);
4209}
4210
4211static void
4212add_band_bottom_edges(pixman_box32_t *boxes,
4213 int band_start,
4214 int band_end,
4215 struct wl_array *borders)
4216{
4217 int i;
4218
4219 for (i = band_start; i < band_end; i++) {
4220 add_border(borders,
4221 boxes[i].x1, boxes[i].y2,
4222 boxes[i].x2, boxes[i].y2,
4223 MOTION_DIRECTION_POSITIVE_Y);
4224 }
4225}
4226
4227static void
4228region_to_outline(pixman_region32_t *region, struct wl_array *borders)
4229{
4230 pixman_box32_t *boxes;
4231 int num_boxes;
4232 int i;
4233 int top_most, bottom_most;
4234 int current_roof;
4235 int prev_top;
4236 int band_start, prev_band_start;
4237
4238 /*
4239 * Remove any overlapping lines from the set of rectangles. Note that
4240 * pixman regions are grouped as rows of rectangles, where rectangles
4241 * in one row never touch or overlap and are all of the same height.
4242 *
4243 * -------- --- -------- ---
4244 * | | | | | | | |
4245 * ----------====---- --- ----------- ----- ---
4246 * | | => | |
4247 * ----==========--------- ----- ----------
4248 * | | | |
4249 * ------------------- -------------------
4250 *
4251 */
4252
4253 boxes = pixman_region32_rectangles(region, &num_boxes);
4254 prev_top = 0;
4255 top_most = boxes[0].y1;
4256 current_roof = top_most;
4257 bottom_most = boxes[num_boxes - 1].y2;
4258 band_start = 0;
4259 prev_band_start = 0;
4260 for (i = 0; i < num_boxes; i++) {
4261 /* Detect if there is a vertical empty space, and add the lower
4262 * level of the previous band if so was the case. */
4263 if (i > 0 &&
4264 boxes[i].y1 != prev_top &&
4265 boxes[i].y1 != boxes[i - 1].y2) {
4266 current_roof = boxes[i].y1;
4267 add_band_bottom_edges(boxes,
4268 band_start,
4269 i,
4270 borders);
4271 }
4272
4273 /* Special case adding the last band, since it won't be handled
4274 * by the band change detection below. */
4275 if (boxes[i].y1 != current_roof && i == num_boxes - 1) {
4276 if (boxes[i].y1 != prev_top) {
4277 /* The last band is a single box, so we don't
4278 * have a prev_band_start to tell us when the
4279 * previous band started. */
4280 add_non_overlapping_edges(boxes,
4281 band_start,
4282 i,
4283 i + 1,
4284 borders);
4285 } else {
4286 add_non_overlapping_edges(boxes,
4287 prev_band_start,
4288 band_start,
4289 i + 1,
4290 borders);
4291 }
4292 }
4293
4294 /* Detect when passing a band and combine the top border of the
4295 * just passed band with the bottom band of the previous band.
4296 */
4297 if (boxes[i].y1 != top_most && boxes[i].y1 != prev_top) {
4298 /* Combine the two passed bands. */
4299 if (prev_top != current_roof) {
4300 add_non_overlapping_edges(boxes,
4301 prev_band_start,
4302 band_start,
4303 i,
4304 borders);
4305 }
4306
4307 prev_band_start = band_start;
4308 band_start = i;
4309 }
4310
4311 /* Add the top border if the box is part of the current roof. */
4312 if (boxes[i].y1 == current_roof) {
4313 add_border(borders,
4314 boxes[i].x1, boxes[i].y1,
4315 boxes[i].x2, boxes[i].y1,
4316 MOTION_DIRECTION_NEGATIVE_Y);
4317 }
4318
4319 /* Add the bottom border of the last band. */
4320 if (boxes[i].y2 == bottom_most) {
4321 add_border(borders,
4322 boxes[i].x1, boxes[i].y2,
4323 boxes[i].x2, boxes[i].y2,
4324 MOTION_DIRECTION_POSITIVE_Y);
4325 }
4326
4327 /* Always add the left border. */
4328 add_border(borders,
4329 boxes[i].x1, boxes[i].y1,
4330 boxes[i].x1, boxes[i].y2,
4331 MOTION_DIRECTION_NEGATIVE_X);
4332
4333 /* Always add the right border. */
4334 add_border(borders,
4335 boxes[i].x2, boxes[i].y1,
4336 boxes[i].x2, boxes[i].y2,
4337 MOTION_DIRECTION_POSITIVE_X);
4338
4339 prev_top = boxes[i].y1;
4340 }
4341}
4342
4343static bool
4344is_border_horizontal (struct border *border)
4345{
4346 return border->line.a.y == border->line.b.y;
4347}
4348
4349static bool
4350is_border_blocking_directions(struct border *border,
4351 uint32_t directions)
4352{
4353 /* Don't block parallel motions. */
4354 if (is_border_horizontal(border)) {
4355 if ((directions & (MOTION_DIRECTION_POSITIVE_Y |
4356 MOTION_DIRECTION_NEGATIVE_Y)) == 0)
4357 return false;
4358 } else {
4359 if ((directions & (MOTION_DIRECTION_POSITIVE_X |
4360 MOTION_DIRECTION_NEGATIVE_X)) == 0)
4361 return false;
4362 }
4363
4364 return (~border->blocking_dir & directions) != directions;
4365}
4366
4367static struct border *
4368get_closest_border(struct wl_array *borders,
4369 struct line *motion,
4370 uint32_t directions)
4371{
4372 struct border *border;
4373 struct vec2d intersection;
4374 struct vec2d delta;
4375 double distance_2;
4376 struct border *closest_border = NULL;
4377 double closest_distance_2 = DBL_MAX;
4378
4379 wl_array_for_each(border, borders) {
4380 if (!is_border_blocking_directions(border, directions))
4381 continue;
4382
4383 if (!lines_intersect(&border->line, motion, &intersection))
4384 continue;
4385
4386 delta = vec2d_subtract(intersection, motion->a);
4387 distance_2 = delta.x*delta.x + delta.y*delta.y;
4388 if (distance_2 < closest_distance_2) {
4389 closest_border = border;
4390 closest_distance_2 = distance_2;
4391 }
4392 }
4393
4394 return closest_border;
4395}
4396
4397static void
4398clamp_to_border(struct border *border,
4399 struct line *motion,
4400 uint32_t *motion_dir)
4401{
4402 /*
4403 * When clamping either rightward or downward motions, the motion needs
4404 * to be clamped so that the destination coordinate does not end up on
4405 * the border (see weston_pointer_clamp_event_to_region). Do this by
4406 * clamping such motions to the border minus the smallest possible
4407 * wl_fixed_t value.
4408 */
4409 if (is_border_horizontal(border)) {
4410 if (*motion_dir & MOTION_DIRECTION_POSITIVE_Y)
4411 motion->b.y = border->line.a.y - wl_fixed_to_double(1);
4412 else
4413 motion->b.y = border->line.a.y;
4414 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_Y |
4415 MOTION_DIRECTION_NEGATIVE_Y);
4416 } else {
4417 if (*motion_dir & MOTION_DIRECTION_POSITIVE_X)
4418 motion->b.x = border->line.a.x - wl_fixed_to_double(1);
4419 else
4420 motion->b.x = border->line.a.x;
4421 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_X |
4422 MOTION_DIRECTION_NEGATIVE_X);
4423 }
4424}
4425
4426static uint32_t
4427get_motion_directions(struct line *motion)
4428{
4429 uint32_t directions = 0;
4430
4431 if (motion->a.x < motion->b.x)
4432 directions |= MOTION_DIRECTION_POSITIVE_X;
4433 else if (motion->a.x > motion->b.x)
4434 directions |= MOTION_DIRECTION_NEGATIVE_X;
4435 if (motion->a.y < motion->b.y)
4436 directions |= MOTION_DIRECTION_POSITIVE_Y;
4437 else if (motion->a.y > motion->b.y)
4438 directions |= MOTION_DIRECTION_NEGATIVE_Y;
4439
4440 return directions;
4441}
4442
Jonas Ådahld3414f22016-07-22 17:56:31 +08004443static void
4444weston_pointer_clamp_event_to_region(struct weston_pointer *pointer,
4445 struct weston_pointer_motion_event *event,
4446 pixman_region32_t *region,
4447 wl_fixed_t *clamped_x,
4448 wl_fixed_t *clamped_y)
4449{
4450 wl_fixed_t x, y;
4451 wl_fixed_t sx, sy;
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004452 wl_fixed_t old_sx = pointer->sx;
4453 wl_fixed_t old_sy = pointer->sy;
4454 struct wl_array borders;
4455 struct line motion;
4456 struct border *closest_border;
4457 float new_x_f, new_y_f;
4458 uint32_t directions;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004459
4460 weston_pointer_motion_to_abs(pointer, event, &x, &y);
4461 weston_view_from_global_fixed(pointer->focus, x, y, &sx, &sy);
4462
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004463 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004464
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004465 /*
4466 * Generate borders given the confine region we are to use. The borders
4467 * are defined to be the outer region of the allowed area. This means
4468 * top/left borders are "within" the allowed area, while bottom/right
4469 * borders are outside. This needs to be considered when clamping
4470 * confined motion vectors.
4471 */
4472 region_to_outline(region, &borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004473
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004474 motion = (struct line) {
4475 .a = (struct vec2d) {
4476 .x = wl_fixed_to_double(old_sx),
4477 .y = wl_fixed_to_double(old_sy),
4478 },
4479 .b = (struct vec2d) {
4480 .x = wl_fixed_to_double(sx),
4481 .y = wl_fixed_to_double(sy),
4482 },
4483 };
4484 directions = get_motion_directions(&motion);
4485
4486 while (directions) {
4487 closest_border = get_closest_border(&borders,
4488 &motion,
4489 directions);
4490 if (closest_border)
4491 clamp_to_border(closest_border, &motion, &directions);
4492 else
4493 break;
4494 }
4495
4496 weston_view_to_global_float(pointer->focus,
4497 (float) motion.b.x, (float) motion.b.y,
4498 &new_x_f, &new_y_f);
4499 *clamped_x = wl_fixed_from_double(new_x_f);
4500 *clamped_y = wl_fixed_from_double(new_y_f);
4501
4502 wl_array_release(&borders);
4503}
4504
4505static double
4506point_to_border_distance_2(struct border *border, double x, double y)
4507{
4508 double orig_x, orig_y;
4509 double dx, dy;
4510
4511 if (is_border_horizontal(border)) {
4512 if (x < border->line.a.x)
4513 orig_x = border->line.a.x;
4514 else if (x > border->line.b.x)
4515 orig_x = border->line.b.x;
4516 else
4517 orig_x = x;
4518 orig_y = border->line.a.y;
4519 } else {
4520 if (y < border->line.a.y)
4521 orig_y = border->line.a.y;
4522 else if (y > border->line.b.y)
4523 orig_y = border->line.b.y;
4524 else
4525 orig_y = y;
4526 orig_x = border->line.a.x;
4527 }
4528
4529
4530 dx = fabs(orig_x - x);
4531 dy = fabs(orig_y - y);
4532 return dx*dx + dy*dy;
4533}
4534
4535static void
4536warp_to_behind_border(struct border *border, wl_fixed_t *sx, wl_fixed_t *sy)
4537{
4538 switch (border->blocking_dir) {
4539 case MOTION_DIRECTION_POSITIVE_X:
4540 case MOTION_DIRECTION_NEGATIVE_X:
4541 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_X)
4542 *sx = wl_fixed_from_double(border->line.a.x) - 1;
4543 else
4544 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4545 if (*sy < wl_fixed_from_double(border->line.a.y))
4546 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4547 else if (*sy > wl_fixed_from_double(border->line.b.y))
4548 *sy = wl_fixed_from_double(border->line.b.y) - 1;
4549 break;
4550 case MOTION_DIRECTION_POSITIVE_Y:
4551 case MOTION_DIRECTION_NEGATIVE_Y:
4552 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_Y)
4553 *sy = wl_fixed_from_double(border->line.a.y) - 1;
4554 else
4555 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4556 if (*sx < wl_fixed_from_double(border->line.a.x))
4557 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4558 else if (*sx > wl_fixed_from_double(border->line.b.x))
4559 *sx = wl_fixed_from_double(border->line.b.x) - 1;
4560 break;
4561 }
Jonas Ådahld3414f22016-07-22 17:56:31 +08004562}
4563
4564static void
4565maybe_warp_confined_pointer(struct weston_pointer_constraint *constraint)
4566{
4567 wl_fixed_t x;
4568 wl_fixed_t y;
4569 wl_fixed_t sx;
4570 wl_fixed_t sy;
4571
4572 weston_view_from_global_fixed(constraint->view,
4573 constraint->pointer->x,
4574 constraint->pointer->y,
4575 &sx,
4576 &sy);
4577
4578 if (!is_within_constraint_region(constraint, sx, sy)) {
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004579 double xf = wl_fixed_to_double(sx);
4580 double yf = wl_fixed_to_double(sy);
4581 pixman_region32_t confine_region;
4582 struct wl_array borders;
4583 struct border *border;
4584 double closest_distance_2 = DBL_MAX;
4585 struct border *closest_border = NULL;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004586
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004587 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004588
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004589 pixman_region32_init(&confine_region);
4590 pixman_region32_intersect(&confine_region,
4591 &constraint->view->surface->input,
4592 &constraint->region);
4593 region_to_outline(&confine_region, &borders);
4594 pixman_region32_fini(&confine_region);
4595
4596 wl_array_for_each(border, &borders) {
4597 double distance_2;
4598
4599 distance_2 = point_to_border_distance_2(border, xf, yf);
4600 if (distance_2 < closest_distance_2) {
4601 closest_border = border;
4602 closest_distance_2 = distance_2;
4603 }
4604 }
4605 assert(closest_border);
4606
4607 warp_to_behind_border(closest_border, &sx, &sy);
4608
4609 wl_array_release(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004610
4611 weston_view_to_global_fixed(constraint->view, sx, sy, &x, &y);
4612 weston_pointer_move_to(constraint->pointer, x, y);
4613 }
4614}
4615
4616static void
4617confined_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02004618 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004619 struct weston_pointer_motion_event *event)
4620{
4621 struct weston_pointer_constraint *constraint =
4622 container_of(grab, struct weston_pointer_constraint, grab);
4623 struct weston_pointer *pointer = grab->pointer;
4624 struct weston_surface *surface;
4625 wl_fixed_t x, y;
4626 wl_fixed_t old_sx = pointer->sx;
4627 wl_fixed_t old_sy = pointer->sy;
4628 pixman_region32_t confine_region;
4629
4630 assert(pointer->focus);
4631 assert(pointer->focus->surface == constraint->surface);
4632
4633 surface = pointer->focus->surface;
4634
4635 pixman_region32_init(&confine_region);
4636 pixman_region32_intersect(&confine_region,
4637 &surface->input,
4638 &constraint->region);
4639 weston_pointer_clamp_event_to_region(pointer, event,
4640 &confine_region, &x, &y);
4641 weston_pointer_move_to(pointer, x, y);
4642 pixman_region32_fini(&confine_region);
4643
4644 weston_view_from_global_fixed(pointer->focus, x, y,
4645 &pointer->sx, &pointer->sy);
4646
4647 if (old_sx != pointer->sx || old_sy != pointer->sy) {
Quentin Glidiccde13452016-08-12 10:41:32 +02004648 pointer_send_motion(pointer, time,
4649 pointer->sx, pointer->sy);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004650 }
4651
Quentin Glidiccde13452016-08-12 10:41:32 +02004652 pointer_send_relative_motion(pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004653}
4654
4655static void
4656confined_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02004657 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004658 uint32_t button,
4659 uint32_t state_w)
4660{
4661 weston_pointer_send_button(grab->pointer, time, button, state_w);
4662}
4663
4664static void
4665confined_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02004666 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004667 struct weston_pointer_axis_event *event)
4668{
4669 weston_pointer_send_axis(grab->pointer, time, event);
4670}
4671
4672static void
4673confined_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
4674 uint32_t source)
4675{
4676 weston_pointer_send_axis_source(grab->pointer, source);
4677}
4678
4679static void
4680confined_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
4681{
4682 weston_pointer_send_frame(grab->pointer);
4683}
4684
4685static void
4686confined_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
4687{
4688 struct weston_pointer_constraint *constraint =
4689 container_of(grab, struct weston_pointer_constraint, grab);
4690
4691 disable_pointer_constraint(constraint);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004692}
4693
4694static const struct weston_pointer_grab_interface
4695 confined_pointer_grab_interface = {
4696 confined_pointer_grab_pointer_focus,
4697 confined_pointer_grab_pointer_motion,
4698 confined_pointer_grab_pointer_button,
4699 confined_pointer_grab_pointer_axis,
4700 confined_pointer_grab_pointer_axis_source,
4701 confined_pointer_grab_pointer_frame,
4702 confined_pointer_grab_pointer_cancel,
4703};
4704
4705static void
4706confined_pointer_destroy(struct wl_client *client,
4707 struct wl_resource *resource)
4708{
4709 wl_resource_destroy(resource);
4710}
4711
4712static void
4713confined_pointer_set_region(struct wl_client *client,
4714 struct wl_resource *resource,
4715 struct wl_resource *region_resource)
4716{
4717 struct weston_pointer_constraint *constraint =
4718 wl_resource_get_user_data(resource);
4719 struct weston_region *region = region_resource ?
4720 wl_resource_get_user_data(region_resource) : NULL;
4721
4722 if (!constraint)
4723 return;
4724
4725 if (region) {
4726 pixman_region32_copy(&constraint->region_pending,
4727 &region->region);
4728 } else {
4729 pixman_region32_fini(&constraint->region_pending);
4730 region_init_infinite(&constraint->region_pending);
4731 }
4732 constraint->region_is_pending = true;
4733}
4734
4735static const struct zwp_confined_pointer_v1_interface confined_pointer_interface = {
4736 confined_pointer_destroy,
4737 confined_pointer_set_region,
4738};
4739
4740static void
4741pointer_constraints_confine_pointer(struct wl_client *client,
4742 struct wl_resource *resource,
4743 uint32_t id,
4744 struct wl_resource *surface_resource,
4745 struct wl_resource *pointer_resource,
4746 struct wl_resource *region_resource,
4747 uint32_t lifetime)
4748{
4749 struct weston_surface *surface =
4750 wl_resource_get_user_data(surface_resource);
4751 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
4752 struct weston_region *region = region_resource ?
4753 wl_resource_get_user_data(region_resource) : NULL;
4754
4755 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
4756 &zwp_confined_pointer_v1_interface,
4757 &confined_pointer_interface,
4758 &confined_pointer_grab_interface);
4759}
4760
4761static const struct zwp_pointer_constraints_v1_interface pointer_constraints_interface = {
4762 pointer_constraints_destroy,
4763 pointer_constraints_lock_pointer,
4764 pointer_constraints_confine_pointer,
4765};
4766
4767static void
4768bind_pointer_constraints(struct wl_client *client, void *data,
4769 uint32_t version, uint32_t id)
4770{
4771 struct wl_resource *resource;
4772
4773 resource = wl_resource_create(client,
4774 &zwp_pointer_constraints_v1_interface,
4775 1, id);
4776
4777 wl_resource_set_implementation(resource, &pointer_constraints_interface,
4778 NULL, NULL);
4779}
4780
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004781static void
Alexandros Frantzis2b442482018-02-20 14:05:50 +02004782input_timestamps_destroy(struct wl_client *client,
4783 struct wl_resource *resource)
4784{
4785 wl_resource_destroy(resource);
4786}
4787
4788static const struct zwp_input_timestamps_v1_interface
4789 input_timestamps_interface = {
4790 input_timestamps_destroy,
4791};
4792
4793static void
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004794input_timestamps_manager_destroy(struct wl_client *client,
4795 struct wl_resource *resource)
4796{
4797 wl_resource_destroy(resource);
4798}
4799
4800static void
4801input_timestamps_manager_get_keyboard_timestamps(struct wl_client *client,
4802 struct wl_resource *resource,
4803 uint32_t id,
4804 struct wl_resource *keyboard_resource)
4805{
Alexandros Frantzis2b442482018-02-20 14:05:50 +02004806 struct weston_keyboard *keyboard =
4807 wl_resource_get_user_data(keyboard_resource);
4808 struct wl_resource *input_ts;
4809
4810 input_ts = wl_resource_create(client,
4811 &zwp_input_timestamps_v1_interface,
4812 1, id);
4813 if (!input_ts) {
4814 wl_client_post_no_memory(client);
4815 return;
4816 }
4817
4818 if (keyboard) {
4819 wl_list_insert(&keyboard->timestamps_list,
4820 wl_resource_get_link(input_ts));
4821 } else {
4822 wl_list_init(wl_resource_get_link(input_ts));
4823 }
4824
4825 wl_resource_set_implementation(input_ts,
4826 &input_timestamps_interface,
4827 keyboard_resource,
4828 unbind_resource);
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004829}
4830
4831static void
4832input_timestamps_manager_get_pointer_timestamps(struct wl_client *client,
4833 struct wl_resource *resource,
4834 uint32_t id,
4835 struct wl_resource *pointer_resource)
4836{
Alexandros Frantzisdb907b72018-02-20 14:06:26 +02004837 struct weston_pointer *pointer =
4838 wl_resource_get_user_data(pointer_resource);
4839 struct wl_resource *input_ts;
4840
4841 input_ts = wl_resource_create(client,
4842 &zwp_input_timestamps_v1_interface,
4843 1, id);
4844 if (!input_ts) {
4845 wl_client_post_no_memory(client);
4846 return;
4847 }
4848
4849 if (pointer) {
4850 wl_list_insert(&pointer->timestamps_list,
4851 wl_resource_get_link(input_ts));
4852 } else {
4853 wl_list_init(wl_resource_get_link(input_ts));
4854 }
4855
4856 wl_resource_set_implementation(input_ts,
4857 &input_timestamps_interface,
4858 pointer_resource,
4859 unbind_resource);
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004860}
4861
4862static void
4863input_timestamps_manager_get_touch_timestamps(struct wl_client *client,
4864 struct wl_resource *resource,
4865 uint32_t id,
4866 struct wl_resource *touch_resource)
4867{
Alexandros Frantzisd7157842018-02-20 14:07:03 +02004868 struct weston_touch *touch = wl_resource_get_user_data(touch_resource);
4869 struct wl_resource *input_ts;
4870
4871 input_ts = wl_resource_create(client,
4872 &zwp_input_timestamps_v1_interface,
4873 1, id);
4874 if (!input_ts) {
4875 wl_client_post_no_memory(client);
4876 return;
4877 }
4878
4879 if (touch) {
4880 wl_list_insert(&touch->timestamps_list,
4881 wl_resource_get_link(input_ts));
4882 } else {
4883 wl_list_init(wl_resource_get_link(input_ts));
4884 }
4885
4886 wl_resource_set_implementation(input_ts,
4887 &input_timestamps_interface,
4888 touch_resource,
4889 unbind_resource);
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004890}
4891
4892static const struct zwp_input_timestamps_manager_v1_interface
4893 input_timestamps_manager_interface = {
4894 input_timestamps_manager_destroy,
4895 input_timestamps_manager_get_keyboard_timestamps,
4896 input_timestamps_manager_get_pointer_timestamps,
4897 input_timestamps_manager_get_touch_timestamps,
4898};
4899
4900static void
4901bind_input_timestamps_manager(struct wl_client *client, void *data,
4902 uint32_t version, uint32_t id)
4903{
4904 struct wl_resource *resource =
4905 wl_resource_create(client,
4906 &zwp_input_timestamps_manager_v1_interface,
4907 1, id);
4908
4909 if (resource == NULL) {
4910 wl_client_post_no_memory(client);
4911 return;
4912 }
4913
4914 wl_resource_set_implementation(resource,
4915 &input_timestamps_manager_interface,
4916 NULL, NULL);
4917}
4918
Jonas Ådahl30d61d82014-10-22 21:21:17 +02004919int
4920weston_input_init(struct weston_compositor *compositor)
4921{
4922 if (!wl_global_create(compositor->wl_display,
4923 &zwp_relative_pointer_manager_v1_interface, 1,
4924 compositor, bind_relative_pointer_manager))
4925 return -1;
4926
Jonas Ådahld3414f22016-07-22 17:56:31 +08004927 if (!wl_global_create(compositor->wl_display,
4928 &zwp_pointer_constraints_v1_interface, 1,
4929 NULL, bind_pointer_constraints))
4930 return -1;
4931
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004932 if (!wl_global_create(compositor->wl_display,
4933 &zwp_input_timestamps_manager_v1_interface, 1,
4934 NULL, bind_input_timestamps_manager))
4935 return -1;
4936
Jonas Ådahl30d61d82014-10-22 21:21:17 +02004937 return 0;
4938}