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