blob: 04c11419997e8040b33fd76a0767562a05085f6a [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
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -0500190static enum weston_touch_mode
191weston_touch_device_get_mode(struct weston_touch_device *device)
192{
193 return device->aggregate->seat->compositor->touch_mode;
194}
195
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800196static struct weston_pointer_client *
197weston_pointer_client_create(struct wl_client *client)
198{
199 struct weston_pointer_client *pointer_client;
200
201 pointer_client = zalloc(sizeof *pointer_client);
202 if (!pointer_client)
203 return NULL;
204
205 pointer_client->client = client;
206 wl_list_init(&pointer_client->pointer_resources);
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200207 wl_list_init(&pointer_client->relative_pointer_resources);
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800208
209 return pointer_client;
210}
211
212static void
213weston_pointer_client_destroy(struct weston_pointer_client *pointer_client)
214{
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +0200215 struct wl_resource *resource;
216
217 wl_resource_for_each(resource, &pointer_client->pointer_resources) {
218 wl_resource_set_user_data(resource, NULL);
219 }
220
221 wl_resource_for_each(resource,
222 &pointer_client->relative_pointer_resources) {
223 wl_resource_set_user_data(resource, NULL);
224 }
225
226 wl_list_remove(&pointer_client->pointer_resources);
227 wl_list_remove(&pointer_client->relative_pointer_resources);
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800228 free(pointer_client);
229}
230
231static bool
232weston_pointer_client_is_empty(struct weston_pointer_client *pointer_client)
233{
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200234 return (wl_list_empty(&pointer_client->pointer_resources) &&
235 wl_list_empty(&pointer_client->relative_pointer_resources));
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800236}
237
238static struct weston_pointer_client *
239weston_pointer_get_pointer_client(struct weston_pointer *pointer,
240 struct wl_client *client)
241{
242 struct weston_pointer_client *pointer_client;
243
244 wl_list_for_each(pointer_client, &pointer->pointer_clients, link) {
245 if (pointer_client->client == client)
246 return pointer_client;
247 }
248
249 return NULL;
250}
251
252static struct weston_pointer_client *
253weston_pointer_ensure_pointer_client(struct weston_pointer *pointer,
254 struct wl_client *client)
255{
256 struct weston_pointer_client *pointer_client;
257
258 pointer_client = weston_pointer_get_pointer_client(pointer, client);
259 if (pointer_client)
260 return pointer_client;
261
262 pointer_client = weston_pointer_client_create(client);
263 wl_list_insert(&pointer->pointer_clients, &pointer_client->link);
264
265 if (pointer->focus &&
266 pointer->focus->surface->resource &&
267 wl_resource_get_client(pointer->focus->surface->resource) == client) {
268 pointer->focus_client = pointer_client;
269 }
270
271 return pointer_client;
272}
273
274static void
275weston_pointer_cleanup_pointer_client(struct weston_pointer *pointer,
276 struct weston_pointer_client *pointer_client)
277{
278 if (weston_pointer_client_is_empty(pointer_client)) {
279 if (pointer->focus_client == pointer_client)
280 pointer->focus_client = NULL;
281 wl_list_remove(&pointer_client->link);
282 weston_pointer_client_destroy(pointer_client);
283 }
284}
285
286static void
287unbind_pointer_client_resource(struct wl_resource *resource)
288{
289 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
290 struct wl_client *client = wl_resource_get_client(resource);
291 struct weston_pointer_client *pointer_client;
292
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800293 wl_list_remove(wl_resource_get_link(resource));
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +0200294
295 if (pointer) {
296 pointer_client = weston_pointer_get_pointer_client(pointer,
297 client);
298 assert(pointer_client);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200299 remove_input_resource_from_timestamps(resource,
300 &pointer->timestamps_list);
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +0200301 weston_pointer_cleanup_pointer_client(pointer, pointer_client);
302 }
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800303}
304
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400305static void unbind_resource(struct wl_resource *resource)
306{
Jason Ekstrand44a38632013-06-14 10:08:00 -0500307 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400308}
309
Jonas Ådahl3042ffe2013-10-17 23:04:08 +0200310WL_EXPORT void
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200311weston_pointer_motion_to_abs(struct weston_pointer *pointer,
312 struct weston_pointer_motion_event *event,
313 wl_fixed_t *x, wl_fixed_t *y)
314{
315 if (event->mask & WESTON_POINTER_MOTION_ABS) {
316 *x = wl_fixed_from_double(event->x);
317 *y = wl_fixed_from_double(event->y);
318 } else if (event->mask & WESTON_POINTER_MOTION_REL) {
319 *x = pointer->x + wl_fixed_from_double(event->dx);
320 *y = pointer->y + wl_fixed_from_double(event->dy);
321 } else {
322 assert(!"invalid motion event");
323 *x = *y = 0;
324 }
325}
326
327static bool
328weston_pointer_motion_to_rel(struct weston_pointer *pointer,
329 struct weston_pointer_motion_event *event,
330 double *dx, double *dy,
331 double *dx_unaccel, double *dy_unaccel)
332{
333 if (event->mask & WESTON_POINTER_MOTION_REL &&
334 event->mask & WESTON_POINTER_MOTION_REL_UNACCEL) {
335 *dx = event->dx;
336 *dy = event->dy;
337 *dx_unaccel = event->dx_unaccel;
338 *dy_unaccel = event->dy_unaccel;
339 return true;
340 } else if (event->mask & WESTON_POINTER_MOTION_REL) {
341 *dx_unaccel = *dx = event->dx;
342 *dy_unaccel = *dy = event->dy;
343 return true;
344 } else if (event->mask & WESTON_POINTER_MOTION_REL_UNACCEL) {
345 *dx_unaccel = *dx = event->dx_unaccel;
346 *dy_unaccel = *dy = event->dy_unaccel;
347 return true;
348 } else {
349 return false;
350 }
351}
352
353WL_EXPORT void
Kristian Høgsberga71e8b22013-05-06 21:51:21 -0400354weston_seat_repick(struct weston_seat *seat)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400355{
Derek Foreman1281a362015-07-31 16:55:32 -0500356 const struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400357
Derek Foreman1b786ee2015-06-03 15:53:23 -0500358 if (!pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400359 return;
360
Derek Foreman1b786ee2015-06-03 15:53:23 -0500361 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400362}
363
364static void
365weston_compositor_idle_inhibit(struct weston_compositor *compositor)
366{
367 weston_compositor_wake(compositor);
368 compositor->idle_inhibit++;
369}
370
371static void
372weston_compositor_idle_release(struct weston_compositor *compositor)
373{
374 compositor->idle_inhibit--;
375 weston_compositor_wake(compositor);
376}
377
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400378static void
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100379pointer_focus_view_destroyed(struct wl_listener *listener, void *data)
380{
381 struct weston_pointer *pointer =
382 container_of(listener, struct weston_pointer,
383 focus_view_listener);
384
Derek Foremanf9318d12015-05-11 15:40:11 -0500385 weston_pointer_clear_focus(pointer);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100386}
387
388static void
389pointer_focus_resource_destroyed(struct wl_listener *listener, void *data)
390{
391 struct weston_pointer *pointer =
392 container_of(listener, struct weston_pointer,
393 focus_resource_listener);
394
Derek Foremanf9318d12015-05-11 15:40:11 -0500395 weston_pointer_clear_focus(pointer);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100396}
397
398static void
399keyboard_focus_resource_destroyed(struct wl_listener *listener, void *data)
400{
401 struct weston_keyboard *keyboard =
402 container_of(listener, struct weston_keyboard,
403 focus_resource_listener);
404
405 weston_keyboard_set_focus(keyboard, NULL);
406}
407
408static void
409touch_focus_view_destroyed(struct wl_listener *listener, void *data)
410{
411 struct weston_touch *touch =
412 container_of(listener, struct weston_touch,
413 focus_view_listener);
414
Derek Foreman4c93c082015-04-30 16:45:41 -0500415 weston_touch_set_focus(touch, NULL);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100416}
417
418static void
419touch_focus_resource_destroyed(struct wl_listener *listener, void *data)
420{
421 struct weston_touch *touch =
422 container_of(listener, struct weston_touch,
423 focus_resource_listener);
424
Derek Foreman4c93c082015-04-30 16:45:41 -0500425 weston_touch_set_focus(touch, NULL);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100426}
427
428static void
Neil Roberts96d790e2013-09-19 17:32:00 +0100429move_resources(struct wl_list *destination, struct wl_list *source)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400430{
Neil Roberts96d790e2013-09-19 17:32:00 +0100431 wl_list_insert_list(destination, source);
432 wl_list_init(source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400433}
434
435static void
Neil Roberts96d790e2013-09-19 17:32:00 +0100436move_resources_for_client(struct wl_list *destination,
437 struct wl_list *source,
438 struct wl_client *client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400439{
Neil Roberts96d790e2013-09-19 17:32:00 +0100440 struct wl_resource *resource, *tmp;
441 wl_resource_for_each_safe(resource, tmp, source) {
442 if (wl_resource_get_client(resource) == client) {
443 wl_list_remove(wl_resource_get_link(resource));
444 wl_list_insert(destination,
445 wl_resource_get_link(resource));
446 }
447 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400448}
449
450static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700451default_grab_pointer_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400452{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400453 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500454 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400455 wl_fixed_t sx, sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400456
457 if (pointer->button_count > 0)
458 return;
459
Jason Ekstranda7af7042013-10-12 22:38:11 -0500460 view = weston_compositor_pick_view(pointer->seat->compositor,
461 pointer->x, pointer->y,
462 &sx, &sy);
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400463
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -0800464 if (pointer->focus != view || pointer->sx != sx || pointer->sy != sy)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500465 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400466}
467
468static void
Quentin Glidiccde13452016-08-12 10:41:32 +0200469pointer_send_relative_motion(struct weston_pointer *pointer,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200470 const struct timespec *time,
Quentin Glidiccde13452016-08-12 10:41:32 +0200471 struct weston_pointer_motion_event *event)
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200472{
473 uint64_t time_usec;
474 double dx, dy, dx_unaccel, dy_unaccel;
475 wl_fixed_t dxf, dyf, dxf_unaccel, dyf_unaccel;
476 struct wl_list *resource_list;
477 struct wl_resource *resource;
478
479 if (!pointer->focus_client)
480 return;
481
482 if (!weston_pointer_motion_to_rel(pointer, event,
483 &dx, &dy,
484 &dx_unaccel, &dy_unaccel))
485 return;
486
487 resource_list = &pointer->focus_client->relative_pointer_resources;
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200488 time_usec = timespec_to_usec(&event->time);
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200489 if (time_usec == 0)
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200490 time_usec = timespec_to_usec(time);
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200491
492 dxf = wl_fixed_from_double(dx);
493 dyf = wl_fixed_from_double(dy);
494 dxf_unaccel = wl_fixed_from_double(dx_unaccel);
495 dyf_unaccel = wl_fixed_from_double(dy_unaccel);
496
497 wl_resource_for_each(resource, resource_list) {
498 zwp_relative_pointer_v1_send_relative_motion(
499 resource,
500 (uint32_t) (time_usec >> 32),
501 (uint32_t) time_usec,
502 dxf, dyf,
503 dxf_unaccel, dyf_unaccel);
504 }
505}
506
507static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200508pointer_send_motion(struct weston_pointer *pointer,
509 const struct timespec *time,
Quentin Glidiccde13452016-08-12 10:41:32 +0200510 wl_fixed_t sx, wl_fixed_t sy)
Jonas Ådahlf44942e2016-07-22 17:54:55 +0800511{
512 struct wl_list *resource_list;
513 struct wl_resource *resource;
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200514 uint32_t msecs;
Jonas Ådahlf44942e2016-07-22 17:54:55 +0800515
516 if (!pointer->focus_client)
517 return;
518
519 resource_list = &pointer->focus_client->pointer_resources;
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200520 msecs = timespec_to_msec(time);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200521 wl_resource_for_each(resource, resource_list) {
522 send_timestamps_for_input_resource(resource,
523 &pointer->timestamps_list,
524 time);
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200525 wl_pointer_send_motion(resource, msecs, sx, sy);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200526 }
Jonas Ådahlf44942e2016-07-22 17:54:55 +0800527}
528
Quentin Glidiccde13452016-08-12 10:41:32 +0200529WL_EXPORT void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200530weston_pointer_send_motion(struct weston_pointer *pointer,
531 const struct timespec *time,
Quentin Glidiccde13452016-08-12 10:41:32 +0200532 struct weston_pointer_motion_event *event)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400533{
Jonas Ådahld2510102014-10-05 21:39:14 +0200534 wl_fixed_t x, y;
Jonas Ådahl8283c342015-04-24 15:26:17 +0800535 wl_fixed_t old_sx = pointer->sx;
536 wl_fixed_t old_sy = pointer->sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400537
Jonas Ådahld2510102014-10-05 21:39:14 +0200538 if (pointer->focus) {
539 weston_pointer_motion_to_abs(pointer, event, &x, &y);
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -0800540 weston_view_from_global_fixed(pointer->focus, x, y,
541 &pointer->sx, &pointer->sy);
Jonas Ådahld2510102014-10-05 21:39:14 +0200542 }
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -0800543
Jonas Ådahld2510102014-10-05 21:39:14 +0200544 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100545
Jonas Ådahlf44942e2016-07-22 17:54:55 +0800546 if (old_sx != pointer->sx || old_sy != pointer->sy) {
Quentin Glidiccde13452016-08-12 10:41:32 +0200547 pointer_send_motion(pointer, time,
548 pointer->sx, pointer->sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400549 }
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200550
Quentin Glidiccde13452016-08-12 10:41:32 +0200551 pointer_send_relative_motion(pointer, time, event);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400552}
553
554static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200555default_grab_pointer_motion(struct weston_pointer_grab *grab,
556 const struct timespec *time,
Quentin Glidiccde13452016-08-12 10:41:32 +0200557 struct weston_pointer_motion_event *event)
558{
559 weston_pointer_send_motion(grab->pointer, time, event);
560}
561
562/** Check if the pointer has focused resources.
563 *
564 * \param pointer The pointer to check for focused resources.
565 * \return Whether or not this pointer has focused resources
566 */
567WL_EXPORT bool
568weston_pointer_has_focus_resource(struct weston_pointer *pointer)
569{
570 if (!pointer->focus_client)
571 return false;
572
573 if (wl_list_empty(&pointer->focus_client->pointer_resources))
574 return false;
575
576 return true;
577}
578
579/** Send wl_pointer.button events to focused resources.
580 *
581 * \param pointer The pointer where the button events originates from.
582 * \param time The timestamp of the event
583 * \param button The button value of the event
584 * \param value The state enum value of the event
585 *
586 * For every resource that is currently in focus, send a wl_pointer.button event
587 * with the passed parameters. The focused resources are the wl_pointer
588 * resources of the client which currently has the surface with pointer focus.
589 */
590WL_EXPORT void
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800591weston_pointer_send_button(struct weston_pointer *pointer,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200592 const struct timespec *time, uint32_t button,
Quentin Glidiccde13452016-08-12 10:41:32 +0200593 enum wl_pointer_button_state state)
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800594{
595 struct wl_display *display = pointer->seat->compositor->wl_display;
596 struct wl_list *resource_list;
597 struct wl_resource *resource;
598 uint32_t serial;
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200599 uint32_t msecs;
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800600
Quentin Glidiccde13452016-08-12 10:41:32 +0200601 if (!weston_pointer_has_focus_resource(pointer))
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800602 return;
603
604 resource_list = &pointer->focus_client->pointer_resources;
Quentin Glidiccde13452016-08-12 10:41:32 +0200605 serial = wl_display_next_serial(display);
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200606 msecs = timespec_to_msec(time);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200607 wl_resource_for_each(resource, resource_list) {
608 send_timestamps_for_input_resource(resource,
609 &pointer->timestamps_list,
610 time);
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200611 wl_pointer_send_button(resource, serial, msecs, button, state);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200612 }
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800613}
614
615static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700616default_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200617 const struct timespec *time, uint32_t button,
Quentin Glidiccde13452016-08-12 10:41:32 +0200618 enum wl_pointer_button_state state)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400619{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400620 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400621 struct weston_compositor *compositor = pointer->seat->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500622 struct weston_view *view;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400623 wl_fixed_t sx, sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400624
Quentin Glidiccde13452016-08-12 10:41:32 +0200625 weston_pointer_send_button(pointer, time, button, state);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400626
627 if (pointer->button_count == 0 &&
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400628 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500629 view = weston_compositor_pick_view(compositor,
630 pointer->x, pointer->y,
631 &sx, &sy);
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400632
Jason Ekstranda7af7042013-10-12 22:38:11 -0500633 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400634 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400635}
636
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200637/** Send wl_pointer.axis events to focused resources.
638 *
639 * \param pointer The pointer where the axis events originates from.
640 * \param time The timestamp of the event
641 * \param axis The axis enum value of the event
642 * \param value The axis value of the event
643 *
644 * For every resource that is currently in focus, send a wl_pointer.axis event
645 * with the passed parameters. The focused resources are the wl_pointer
646 * resources of the client which currently has the surface with pointer focus.
647 */
648WL_EXPORT void
649weston_pointer_send_axis(struct weston_pointer *pointer,
Alexandros Frantzis80321942017-11-16 18:20:56 +0200650 const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +1000651 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200652{
653 struct wl_resource *resource;
654 struct wl_list *resource_list;
Alexandros Frantzis80321942017-11-16 18:20:56 +0200655 uint32_t msecs;
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200656
Quentin Glidiccde13452016-08-12 10:41:32 +0200657 if (!weston_pointer_has_focus_resource(pointer))
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800658 return;
659
660 resource_list = &pointer->focus_client->pointer_resources;
Alexandros Frantzis80321942017-11-16 18:20:56 +0200661 msecs = timespec_to_msec(time);
Peter Hutterer87743e92016-01-18 16:38:22 +1000662 wl_resource_for_each(resource, resource_list) {
663 if (event->has_discrete &&
664 wl_resource_get_version(resource) >=
665 WL_POINTER_AXIS_DISCRETE_SINCE_VERSION)
666 wl_pointer_send_axis_discrete(resource, event->axis,
667 event->discrete);
668
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200669 if (event->value) {
670 send_timestamps_for_input_resource(resource,
671 &pointer->timestamps_list,
672 time);
Alexandros Frantzis80321942017-11-16 18:20:56 +0200673 wl_pointer_send_axis(resource, msecs,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200674 event->axis,
675 wl_fixed_from_double(event->value));
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200676 } else if (wl_resource_get_version(resource) >=
677 WL_POINTER_AXIS_STOP_SINCE_VERSION) {
678 send_timestamps_for_input_resource(resource,
679 &pointer->timestamps_list,
680 time);
Alexandros Frantzis80321942017-11-16 18:20:56 +0200681 wl_pointer_send_axis_stop(resource, msecs,
Peter Hutterer87743e92016-01-18 16:38:22 +1000682 event->axis);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200683 }
Peter Hutterer87743e92016-01-18 16:38:22 +1000684 }
685}
686
Quentin Glidiccde13452016-08-12 10:41:32 +0200687/** Send wl_pointer.axis_source events to focused resources.
688 *
689 * \param pointer The pointer where the axis_source events originates from.
690 * \param source The axis_source enum value of the event
691 *
692 * For every resource that is currently in focus, send a wl_pointer.axis_source
693 * event with the passed parameter. The focused resources are the wl_pointer
694 * resources of the client which currently has the surface with pointer focus.
695 */
Peter Hutterer87743e92016-01-18 16:38:22 +1000696WL_EXPORT void
Quentin Glidiccde13452016-08-12 10:41:32 +0200697weston_pointer_send_axis_source(struct weston_pointer *pointer,
698 enum wl_pointer_axis_source source)
Peter Hutterer87743e92016-01-18 16:38:22 +1000699{
700 struct wl_resource *resource;
701 struct wl_list *resource_list;
702
Quentin Glidiccde13452016-08-12 10:41:32 +0200703 if (!weston_pointer_has_focus_resource(pointer))
Jonas Ådahled6014a2016-04-21 10:21:48 +0800704 return;
705
Peter Hutterer87743e92016-01-18 16:38:22 +1000706 resource_list = &pointer->focus_client->pointer_resources;
707 wl_resource_for_each(resource, resource_list) {
708 if (wl_resource_get_version(resource) >=
709 WL_POINTER_AXIS_SOURCE_SINCE_VERSION) {
710 wl_pointer_send_axis_source(resource, source);
711 }
712 }
713}
714
715static void
716pointer_send_frame(struct wl_resource *resource)
717{
718 if (wl_resource_get_version(resource) >=
719 WL_POINTER_FRAME_SINCE_VERSION) {
720 wl_pointer_send_frame(resource);
721 }
722}
723
Quentin Glidiccde13452016-08-12 10:41:32 +0200724/** Send wl_pointer.frame events to focused resources.
725 *
726 * \param pointer The pointer where the frame events originates from.
727 *
728 * For every resource that is currently in focus, send a wl_pointer.frame event.
729 * The focused resources are the wl_pointer resources of the client which
730 * currently has the surface with pointer focus.
731 */
Peter Hutterer87743e92016-01-18 16:38:22 +1000732WL_EXPORT void
733weston_pointer_send_frame(struct weston_pointer *pointer)
734{
735 struct wl_resource *resource;
736 struct wl_list *resource_list;
737
Quentin Glidiccde13452016-08-12 10:41:32 +0200738 if (!weston_pointer_has_focus_resource(pointer))
Derek Foreman8efa31b2016-01-29 10:29:46 -0600739 return;
740
Peter Hutterer87743e92016-01-18 16:38:22 +1000741 resource_list = &pointer->focus_client->pointer_resources;
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200742 wl_resource_for_each(resource, resource_list)
Peter Hutterer87743e92016-01-18 16:38:22 +1000743 pointer_send_frame(resource);
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200744}
745
746static void
747default_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +0200748 const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +1000749 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200750{
Peter Hutterer89b6a492016-01-18 15:58:17 +1000751 weston_pointer_send_axis(grab->pointer, time, event);
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200752}
753
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200754static void
Peter Hutterer87743e92016-01-18 16:38:22 +1000755default_grab_pointer_axis_source(struct weston_pointer_grab *grab,
Quentin Glidiccde13452016-08-12 10:41:32 +0200756 enum wl_pointer_axis_source source)
Peter Hutterer87743e92016-01-18 16:38:22 +1000757{
758 weston_pointer_send_axis_source(grab->pointer, source);
759}
760
761static void
762default_grab_pointer_frame(struct weston_pointer_grab *grab)
763{
764 weston_pointer_send_frame(grab->pointer);
765}
766
767static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200768default_grab_pointer_cancel(struct weston_pointer_grab *grab)
769{
770}
771
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400772static const struct weston_pointer_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400773 default_pointer_grab_interface = {
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700774 default_grab_pointer_focus,
775 default_grab_pointer_motion,
776 default_grab_pointer_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200777 default_grab_pointer_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +1000778 default_grab_pointer_axis_source,
779 default_grab_pointer_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200780 default_grab_pointer_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400781};
782
Quentin Glidiccde13452016-08-12 10:41:32 +0200783/** Check if the touch has focused resources.
784 *
785 * \param touch The touch to check for focused resources.
786 * \return Whether or not this touch has focused resources
787 */
788WL_EXPORT bool
789weston_touch_has_focus_resource(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400790{
Quentin Glidiccde13452016-08-12 10:41:32 +0200791 if (!touch->focus)
792 return false;
793
794 if (wl_list_empty(&touch->focus_resource_list))
795 return false;
796
797 return true;
798}
799
800/** Send wl_touch.down events to focused resources.
801 *
802 * \param touch The touch where the down events originates from.
803 * \param time The timestamp of the event
804 * \param touch_id The touch_id value of the event
805 * \param x The x value of the event
806 * \param y The y value of the event
807 *
808 * For every resource that is currently in focus, send a wl_touch.down event
809 * with the passed parameters. The focused resources are the wl_touch
810 * resources of the client which currently has the surface with touch focus.
811 */
812WL_EXPORT void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200813weston_touch_send_down(struct weston_touch *touch, const struct timespec *time,
Quentin Glidiccde13452016-08-12 10:41:32 +0200814 int touch_id, wl_fixed_t x, wl_fixed_t y)
815{
Rob Bradford880ebc72013-07-22 17:31:38 +0100816 struct wl_display *display = touch->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400817 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100818 struct wl_resource *resource;
819 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300820 wl_fixed_t sx, sy;
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200821 uint32_t msecs;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300822
Quentin Glidiccde13452016-08-12 10:41:32 +0200823 if (!weston_touch_has_focus_resource(touch))
Bryce Harrington2c40d1d2016-02-02 10:18:48 -0800824 return;
825
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300826 weston_view_from_global_fixed(touch->focus, x, y, &sx, &sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400827
Neil Roberts96d790e2013-09-19 17:32:00 +0100828 resource_list = &touch->focus_resource_list;
Quentin Glidiccde13452016-08-12 10:41:32 +0200829 serial = wl_display_next_serial(display);
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200830 msecs = timespec_to_msec(time);
Alexandros Frantzisd7157842018-02-20 14:07:03 +0200831 wl_resource_for_each(resource, resource_list) {
832 send_timestamps_for_input_resource(resource,
833 &touch->timestamps_list,
834 time);
835 wl_touch_send_down(resource, serial, msecs,
836 touch->focus->surface->resource,
837 touch_id, sx, sy);
838 }
Quentin Glidiccde13452016-08-12 10:41:32 +0200839}
Neil Roberts96d790e2013-09-19 17:32:00 +0100840
Quentin Glidiccde13452016-08-12 10:41:32 +0200841static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200842default_grab_touch_down(struct weston_touch_grab *grab,
843 const struct timespec *time, int touch_id,
844 wl_fixed_t x, wl_fixed_t y)
Quentin Glidiccde13452016-08-12 10:41:32 +0200845{
846 weston_touch_send_down(grab->touch, time, touch_id, x, y);
847}
848
849/** Send wl_touch.up events to focused resources.
850 *
851 * \param touch The touch where the up events originates from.
852 * \param time The timestamp of the event
853 * \param touch_id The touch_id value of the event
854 *
855 * For every resource that is currently in focus, send a wl_touch.up event
856 * with the passed parameters. The focused resources are the wl_touch
857 * resources of the client which currently has the surface with touch focus.
858 */
859WL_EXPORT void
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200860weston_touch_send_up(struct weston_touch *touch, const struct timespec *time,
861 int touch_id)
Quentin Glidiccde13452016-08-12 10:41:32 +0200862{
863 struct wl_display *display = touch->seat->compositor->wl_display;
864 uint32_t serial;
865 struct wl_resource *resource;
866 struct wl_list *resource_list;
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200867 uint32_t msecs;
Quentin Glidiccde13452016-08-12 10:41:32 +0200868
869 if (!weston_touch_has_focus_resource(touch))
870 return;
871
872 resource_list = &touch->focus_resource_list;
873 serial = wl_display_next_serial(display);
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200874 msecs = timespec_to_msec(time);
Alexandros Frantzisd7157842018-02-20 14:07:03 +0200875 wl_resource_for_each(resource, resource_list) {
876 send_timestamps_for_input_resource(resource,
877 &touch->timestamps_list,
878 time);
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200879 wl_touch_send_up(resource, serial, msecs, touch_id);
Alexandros Frantzisd7157842018-02-20 14:07:03 +0200880 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400881}
882
Kristian Høgsberge329f362013-05-06 22:19:57 -0400883static void
884default_grab_touch_up(struct weston_touch_grab *grab,
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200885 const struct timespec *time, int touch_id)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400886{
Quentin Glidiccde13452016-08-12 10:41:32 +0200887 weston_touch_send_up(grab->touch, time, touch_id);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400888}
889
Quentin Glidiccde13452016-08-12 10:41:32 +0200890/** Send wl_touch.motion events to focused resources.
891 *
892 * \param touch The touch where the motion events originates from.
893 * \param time The timestamp of the event
894 * \param touch_id The touch_id value of the event
895 * \param x The x value of the event
896 * \param y The y value of the event
897 *
898 * For every resource that is currently in focus, send a wl_touch.motion event
899 * with the passed parameters. The focused resources are the wl_touch
900 * resources of the client which currently has the surface with touch focus.
901 */
902WL_EXPORT void
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200903weston_touch_send_motion(struct weston_touch *touch,
904 const struct timespec *time, int touch_id,
905 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400906{
Neil Roberts96d790e2013-09-19 17:32:00 +0100907 struct wl_resource *resource;
908 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300909 wl_fixed_t sx, sy;
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200910 uint32_t msecs;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300911
Quentin Glidiccde13452016-08-12 10:41:32 +0200912 if (!weston_touch_has_focus_resource(touch))
913 return;
914
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300915 weston_view_from_global_fixed(touch->focus, x, y, &sx, &sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400916
Neil Roberts96d790e2013-09-19 17:32:00 +0100917 resource_list = &touch->focus_resource_list;
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200918 msecs = timespec_to_msec(time);
Neil Roberts96d790e2013-09-19 17:32:00 +0100919 wl_resource_for_each(resource, resource_list) {
Alexandros Frantzisd7157842018-02-20 14:07:03 +0200920 send_timestamps_for_input_resource(resource,
921 &touch->timestamps_list,
922 time);
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200923 wl_touch_send_motion(resource, msecs,
Kristian Høgsberge329f362013-05-06 22:19:57 -0400924 touch_id, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400925 }
926}
927
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200928static void
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200929default_grab_touch_motion(struct weston_touch_grab *grab,
930 const struct timespec *time, int touch_id,
931 wl_fixed_t x, wl_fixed_t y)
Quentin Glidiccde13452016-08-12 10:41:32 +0200932{
933 weston_touch_send_motion(grab->touch, time, touch_id, x, y);
934}
935
936
937/** Send wl_touch.frame events to focused resources.
938 *
939 * \param touch The touch where the frame events originates from.
940 *
941 * For every resource that is currently in focus, send a wl_touch.frame event.
942 * The focused resources are the wl_touch resources of the client which
943 * currently has the surface with touch focus.
944 */
945WL_EXPORT void
946weston_touch_send_frame(struct weston_touch *touch)
Jonas Ådahl1679f232014-04-12 09:39:51 +0200947{
948 struct wl_resource *resource;
949
Quentin Glidiccde13452016-08-12 10:41:32 +0200950 if (!weston_touch_has_focus_resource(touch))
951 return;
952
953 wl_resource_for_each(resource, &touch->focus_resource_list)
Jonas Ådahl1679f232014-04-12 09:39:51 +0200954 wl_touch_send_frame(resource);
955}
956
957static void
Quentin Glidiccde13452016-08-12 10:41:32 +0200958default_grab_touch_frame(struct weston_touch_grab *grab)
959{
960 weston_touch_send_frame(grab->touch);
961}
962
963static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200964default_grab_touch_cancel(struct weston_touch_grab *grab)
965{
966}
967
Kristian Høgsberge329f362013-05-06 22:19:57 -0400968static const struct weston_touch_grab_interface default_touch_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400969 default_grab_touch_down,
970 default_grab_touch_up,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200971 default_grab_touch_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +0200972 default_grab_touch_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200973 default_grab_touch_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400974};
975
Quentin Glidiccde13452016-08-12 10:41:32 +0200976/** Check if the keyboard has focused resources.
977 *
978 * \param keyboard The keyboard to check for focused resources.
979 * \return Whether or not this keyboard has focused resources
980 */
981WL_EXPORT bool
982weston_keyboard_has_focus_resource(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400983{
Quentin Glidiccde13452016-08-12 10:41:32 +0200984 if (!keyboard->focus)
985 return false;
986
987 if (wl_list_empty(&keyboard->focus_resource_list))
988 return false;
989
990 return true;
991}
992
993/** Send wl_keyboard.key events to focused resources.
994 *
995 * \param keyboard The keyboard where the key events originates from.
996 * \param time The timestamp of the event
997 * \param key The key value of the event
998 * \param state The state enum value of the event
999 *
1000 * For every resource that is currently in focus, send a wl_keyboard.key event
1001 * with the passed parameters. The focused resources are the wl_keyboard
1002 * resources of the client which currently has the surface with keyboard focus.
1003 */
1004WL_EXPORT void
1005weston_keyboard_send_key(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02001006 const struct timespec *time, uint32_t key,
Quentin Glidiccde13452016-08-12 10:41:32 +02001007 enum wl_keyboard_key_state state)
1008{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001009 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +01001010 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001011 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01001012 struct wl_list *resource_list;
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02001013 uint32_t msecs;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001014
Quentin Glidiccde13452016-08-12 10:41:32 +02001015 if (!weston_keyboard_has_focus_resource(keyboard))
1016 return;
1017
Neil Roberts96d790e2013-09-19 17:32:00 +01001018 resource_list = &keyboard->focus_resource_list;
Quentin Glidiccde13452016-08-12 10:41:32 +02001019 serial = wl_display_next_serial(display);
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02001020 msecs = timespec_to_msec(time);
Alexandros Frantzis2b442482018-02-20 14:05:50 +02001021 wl_resource_for_each(resource, resource_list) {
1022 send_timestamps_for_input_resource(resource,
1023 &keyboard->timestamps_list,
1024 time);
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02001025 wl_keyboard_send_key(resource, serial, msecs, key, state);
Alexandros Frantzis2b442482018-02-20 14:05:50 +02001026 }
Quentin Glidiccde13452016-08-12 10:41:32 +02001027};
1028
1029static void
1030default_grab_keyboard_key(struct weston_keyboard_grab *grab,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02001031 const struct timespec *time, uint32_t key,
1032 uint32_t state)
Quentin Glidiccde13452016-08-12 10:41:32 +02001033{
1034 weston_keyboard_send_key(grab->keyboard, time, key, state);
Neil Roberts96d790e2013-09-19 17:32:00 +01001035}
1036
1037static void
1038send_modifiers_to_resource(struct weston_keyboard *keyboard,
1039 struct wl_resource *resource,
1040 uint32_t serial)
1041{
1042 wl_keyboard_send_modifiers(resource,
1043 serial,
1044 keyboard->modifiers.mods_depressed,
1045 keyboard->modifiers.mods_latched,
1046 keyboard->modifiers.mods_locked,
1047 keyboard->modifiers.group);
1048}
1049
1050static void
1051send_modifiers_to_client_in_list(struct wl_client *client,
1052 struct wl_list *list,
1053 uint32_t serial,
1054 struct weston_keyboard *keyboard)
1055{
1056 struct wl_resource *resource;
1057
1058 wl_resource_for_each(resource, list) {
1059 if (wl_resource_get_client(resource) == client)
1060 send_modifiers_to_resource(keyboard,
1061 resource,
1062 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001063 }
1064}
1065
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001066static struct weston_pointer_client *
1067find_pointer_client_for_surface(struct weston_pointer *pointer,
1068 struct weston_surface *surface)
1069{
1070 struct wl_client *client;
1071
1072 if (!surface)
1073 return NULL;
1074
1075 if (!surface->resource)
1076 return NULL;
1077
1078 client = wl_resource_get_client(surface->resource);
1079 return weston_pointer_get_pointer_client(pointer, client);
1080}
1081
1082static struct weston_pointer_client *
1083find_pointer_client_for_view(struct weston_pointer *pointer, struct weston_view *view)
1084{
1085 if (!view)
1086 return NULL;
1087
1088 return find_pointer_client_for_surface(pointer, view->surface);
1089}
1090
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001091static struct wl_resource *
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001092find_resource_for_surface(struct wl_list *list, struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001093{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001094 if (!surface)
1095 return NULL;
1096
Jason Ekstrand44a38632013-06-14 10:08:00 -05001097 if (!surface->resource)
1098 return NULL;
Stefan Schmidtfda26522013-09-17 10:54:09 +01001099
Jason Ekstrand44a38632013-06-14 10:08:00 -05001100 return wl_resource_find_for_client(list, wl_resource_get_client(surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001101}
1102
Quentin Glidiccde13452016-08-12 10:41:32 +02001103/** Send wl_keyboard.modifiers events to focused resources and pointer
1104 * focused resources.
1105 *
1106 * \param keyboard The keyboard where the modifiers events originates from.
1107 * \param serial The serial of the event
1108 * \param mods_depressed The mods_depressed value of the event
1109 * \param mods_latched The mods_latched value of the event
1110 * \param mods_locked The mods_locked value of the event
1111 * \param group The group value of the event
1112 *
1113 * For every resource that is currently in focus, send a wl_keyboard.modifiers
1114 * event with the passed parameters. The focused resources are the wl_keyboard
1115 * resources of the client which currently has the surface with keyboard focus.
1116 * This also sends wl_keyboard.modifiers events to the wl_keyboard resources of
1117 * the client having pointer focus (if different from the keyboard focus client).
1118 */
1119WL_EXPORT void
1120weston_keyboard_send_modifiers(struct weston_keyboard *keyboard,
1121 uint32_t serial, uint32_t mods_depressed,
1122 uint32_t mods_latched,
1123 uint32_t mods_locked, uint32_t group)
1124{
1125 struct weston_pointer *pointer =
1126 weston_seat_get_pointer(keyboard->seat);
1127
1128 if (weston_keyboard_has_focus_resource(keyboard)) {
1129 struct wl_list *resource_list;
1130 struct wl_resource *resource;
1131
1132 resource_list = &keyboard->focus_resource_list;
1133 wl_resource_for_each(resource, resource_list) {
1134 wl_keyboard_send_modifiers(resource, serial,
1135 mods_depressed, mods_latched,
1136 mods_locked, group);
1137 }
1138 }
1139
1140 if (pointer && pointer->focus && pointer->focus->surface->resource &&
1141 pointer->focus->surface != keyboard->focus) {
1142 struct wl_client *pointer_client =
1143 wl_resource_get_client(pointer->focus->surface->resource);
1144
1145 send_modifiers_to_client_in_list(pointer_client,
1146 &keyboard->resource_list,
1147 serial,
1148 keyboard);
1149 }
1150}
1151
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001152static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -07001153default_grab_keyboard_modifiers(struct weston_keyboard_grab *grab,
1154 uint32_t serial, uint32_t mods_depressed,
1155 uint32_t mods_latched,
1156 uint32_t mods_locked, uint32_t group)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001157{
Quentin Glidiccde13452016-08-12 10:41:32 +02001158 weston_keyboard_send_modifiers(grab->keyboard, serial, mods_depressed,
1159 mods_latched, mods_locked, group);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001160}
1161
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001162static void
1163default_grab_keyboard_cancel(struct weston_keyboard_grab *grab)
1164{
1165}
1166
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001167static const struct weston_keyboard_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001168 default_keyboard_grab_interface = {
Kristian Høgsbergb27901c2013-10-28 15:32:02 -07001169 default_grab_keyboard_key,
1170 default_grab_keyboard_modifiers,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001171 default_grab_keyboard_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001172};
1173
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001174static void
1175pointer_unmap_sprite(struct weston_pointer *pointer)
1176{
Pekka Paalanenc557ff72014-11-12 16:42:52 +02001177 struct weston_surface *surface = pointer->sprite->surface;
1178
1179 if (weston_surface_is_mapped(surface))
1180 weston_surface_unmap(surface);
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001181
1182 wl_list_remove(&pointer->sprite_destroy_listener.link);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02001183 surface->committed = NULL;
1184 surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03001185 weston_surface_set_label_func(surface, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001186 weston_view_destroy(pointer->sprite);
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001187 pointer->sprite = NULL;
1188}
1189
1190static void
1191pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
1192{
1193 struct weston_pointer *pointer =
1194 container_of(listener, struct weston_pointer,
1195 sprite_destroy_listener);
1196
1197 pointer->sprite = NULL;
1198}
1199
Jonas Ådahl3e12e632013-12-02 22:05:05 +01001200static void
1201weston_pointer_reset_state(struct weston_pointer *pointer)
1202{
1203 pointer->button_count = 0;
1204}
1205
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001206static void
1207weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data);
1208
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001209static struct weston_pointer *
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001210weston_pointer_create(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001211{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001212 struct weston_pointer *pointer;
1213
Peter Huttererf3d62272013-08-08 11:57:05 +10001214 pointer = zalloc(sizeof *pointer);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001215 if (pointer == NULL)
1216 return NULL;
1217
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001218 wl_list_init(&pointer->pointer_clients);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001219 weston_pointer_set_default_grab(pointer,
1220 seat->compositor->default_pointer_grab);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001221 wl_list_init(&pointer->focus_resource_listener.link);
1222 pointer->focus_resource_listener.notify = pointer_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001223 pointer->default_grab.pointer = pointer;
1224 pointer->grab = &pointer->default_grab;
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001225 wl_signal_init(&pointer->motion_signal);
Emilio Pozuelo Monfortaa7a4762013-11-19 11:37:15 +01001226 wl_signal_init(&pointer->focus_signal);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001227 wl_list_init(&pointer->focus_view_listener.link);
Jonas Ådahl3eb4ddd2016-07-22 17:52:58 +08001228 wl_signal_init(&pointer->destroy_signal);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +02001229 wl_list_init(&pointer->timestamps_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001230
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001231 pointer->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
1232
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001233 /* FIXME: Pick better co-ords. */
1234 pointer->x = wl_fixed_from_int(100);
1235 pointer->y = wl_fixed_from_int(100);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001236
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001237 pointer->output_destroy_listener.notify =
1238 weston_pointer_handle_output_destroy;
1239 wl_signal_add(&seat->compositor->output_destroyed_signal,
1240 &pointer->output_destroy_listener);
1241
Derek Foremanf9318d12015-05-11 15:40:11 -05001242 pointer->sx = wl_fixed_from_int(-1000000);
1243 pointer->sy = wl_fixed_from_int(-1000000);
1244
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001245 return pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001246}
1247
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001248static void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001249weston_pointer_destroy(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001250{
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +02001251 struct weston_pointer_client *pointer_client, *tmp;
1252
Jonas Ådahl3eb4ddd2016-07-22 17:52:58 +08001253 wl_signal_emit(&pointer->destroy_signal, pointer);
1254
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001255 if (pointer->sprite)
1256 pointer_unmap_sprite(pointer);
1257
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +02001258 wl_list_for_each_safe(pointer_client, tmp, &pointer->pointer_clients,
1259 link) {
1260 wl_list_remove(&pointer_client->link);
1261 weston_pointer_client_destroy(pointer_client);
1262 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001263
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001264 wl_list_remove(&pointer->focus_resource_listener.link);
1265 wl_list_remove(&pointer->focus_view_listener.link);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001266 wl_list_remove(&pointer->output_destroy_listener.link);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +02001267 wl_list_remove(&pointer->timestamps_list);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001268 free(pointer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001269}
1270
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001271void
1272weston_pointer_set_default_grab(struct weston_pointer *pointer,
1273 const struct weston_pointer_grab_interface *interface)
1274{
1275 if (interface)
1276 pointer->default_grab.interface = interface;
1277 else
1278 pointer->default_grab.interface =
1279 &default_pointer_grab_interface;
1280}
1281
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001282static struct weston_keyboard *
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001283weston_keyboard_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001284{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001285 struct weston_keyboard *keyboard;
1286
Peter Huttererf3d62272013-08-08 11:57:05 +10001287 keyboard = zalloc(sizeof *keyboard);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001288 if (keyboard == NULL)
Neil Roberts96d790e2013-09-19 17:32:00 +01001289 return NULL;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001290
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001291 wl_list_init(&keyboard->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +01001292 wl_list_init(&keyboard->focus_resource_list);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001293 wl_list_init(&keyboard->focus_resource_listener.link);
1294 keyboard->focus_resource_listener.notify = keyboard_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001295 wl_array_init(&keyboard->keys);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001296 keyboard->default_grab.interface = &default_keyboard_grab_interface;
1297 keyboard->default_grab.keyboard = keyboard;
1298 keyboard->grab = &keyboard->default_grab;
1299 wl_signal_init(&keyboard->focus_signal);
Alexandros Frantzis2b442482018-02-20 14:05:50 +02001300 wl_list_init(&keyboard->timestamps_list);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001301
1302 return keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001303}
1304
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001305static void
1306weston_xkb_info_destroy(struct weston_xkb_info *xkb_info);
1307
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001308static void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001309weston_keyboard_destroy(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001310{
Alexandros Frantzis7a314d62018-01-26 18:47:56 +02001311 struct wl_resource *resource;
1312
1313 wl_resource_for_each(resource, &keyboard->resource_list) {
1314 wl_resource_set_user_data(resource, NULL);
1315 }
1316
1317 wl_resource_for_each(resource, &keyboard->focus_resource_list) {
1318 wl_resource_set_user_data(resource, NULL);
1319 }
1320
1321 wl_list_remove(&keyboard->resource_list);
1322 wl_list_remove(&keyboard->focus_resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +01001323
Derek Foreman185d1582017-06-28 11:17:23 -05001324 xkb_state_unref(keyboard->xkb_state.state);
1325 if (keyboard->xkb_info)
1326 weston_xkb_info_destroy(keyboard->xkb_info);
1327 xkb_keymap_unref(keyboard->pending_keymap);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001328
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001329 wl_array_release(&keyboard->keys);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001330 wl_list_remove(&keyboard->focus_resource_listener.link);
Alexandros Frantzis2b442482018-02-20 14:05:50 +02001331 wl_list_remove(&keyboard->timestamps_list);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001332 free(keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001333}
1334
Jonas Ådahlcbfa7f72013-12-02 22:05:04 +01001335static void
1336weston_touch_reset_state(struct weston_touch *touch)
1337{
1338 touch->num_tp = 0;
1339}
1340
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001341static struct weston_touch *
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001342weston_touch_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001343{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001344 struct weston_touch *touch;
1345
Peter Huttererf3d62272013-08-08 11:57:05 +10001346 touch = zalloc(sizeof *touch);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001347 if (touch == NULL)
1348 return NULL;
1349
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -05001350 wl_list_init(&touch->device_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001351 wl_list_init(&touch->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +01001352 wl_list_init(&touch->focus_resource_list);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001353 wl_list_init(&touch->focus_view_listener.link);
1354 touch->focus_view_listener.notify = touch_focus_view_destroyed;
1355 wl_list_init(&touch->focus_resource_listener.link);
1356 touch->focus_resource_listener.notify = touch_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001357 touch->default_grab.interface = &default_touch_grab_interface;
1358 touch->default_grab.touch = touch;
1359 touch->grab = &touch->default_grab;
1360 wl_signal_init(&touch->focus_signal);
Alexandros Frantzisd7157842018-02-20 14:07:03 +02001361 wl_list_init(&touch->timestamps_list);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001362
1363 return touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001364}
1365
Pekka Paalanen1b043b12018-02-26 14:55:32 +02001366static void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001367weston_touch_destroy(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001368{
Alexandros Frantzisb0b598c2018-01-26 18:47:57 +02001369 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01001370
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -05001371 assert(wl_list_empty(&touch->device_list));
1372
Alexandros Frantzisb0b598c2018-01-26 18:47:57 +02001373 wl_resource_for_each(resource, &touch->resource_list) {
1374 wl_resource_set_user_data(resource, NULL);
1375 }
1376
1377 wl_resource_for_each(resource, &touch->focus_resource_list) {
1378 wl_resource_set_user_data(resource, NULL);
1379 }
1380
1381 wl_list_remove(&touch->resource_list);
1382 wl_list_remove(&touch->focus_resource_list);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001383 wl_list_remove(&touch->focus_view_listener.link);
1384 wl_list_remove(&touch->focus_resource_listener.link);
Alexandros Frantzisd7157842018-02-20 14:07:03 +02001385 wl_list_remove(&touch->timestamps_list);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001386 free(touch);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001387}
1388
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001389static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04001390seat_send_updated_caps(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001391{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001392 enum wl_seat_capability caps = 0;
Rob Bradford6e737f52013-09-06 17:48:19 +01001393 struct wl_resource *resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001394
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001395 if (seat->pointer_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001396 caps |= WL_SEAT_CAPABILITY_POINTER;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001397 if (seat->keyboard_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001398 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001399 if (seat->touch_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001400 caps |= WL_SEAT_CAPABILITY_TOUCH;
1401
Rob Bradford6e737f52013-09-06 17:48:19 +01001402 wl_resource_for_each(resource, &seat->base_resource_list) {
1403 wl_seat_send_capabilities(resource, caps);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001404 }
Jason Ekstranda4ab5422014-04-02 19:53:45 -05001405 wl_signal_emit(&seat->updated_caps_signal, seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001406}
1407
Derek Foremanf9318d12015-05-11 15:40:11 -05001408
1409/** Clear the pointer focus
1410 *
1411 * \param pointer the pointer to clear focus for.
1412 *
1413 * This can be used to unset pointer focus and set the co-ordinates to the
1414 * arbitrary values we use for the no focus case.
1415 *
1416 * There's no requirement to use this function. For example, passing the
1417 * results of a weston_compositor_pick_view() directly to
1418 * weston_pointer_set_focus() will do the right thing when no view is found.
1419 */
1420WL_EXPORT void
1421weston_pointer_clear_focus(struct weston_pointer *pointer)
1422{
1423 weston_pointer_set_focus(pointer, NULL,
1424 wl_fixed_from_int(-1000000),
1425 wl_fixed_from_int(-1000000));
1426}
1427
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001428WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001429weston_pointer_set_focus(struct weston_pointer *pointer,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001430 struct weston_view *view,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001431 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001432{
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001433 struct weston_pointer_client *pointer_client;
Derek Foreman1281a362015-07-31 16:55:32 -05001434 struct weston_keyboard *kbd = weston_seat_get_keyboard(pointer->seat);
Neil Roberts96d790e2013-09-19 17:32:00 +01001435 struct wl_resource *resource;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001436 struct wl_resource *surface_resource;
Rob Bradford880ebc72013-07-22 17:31:38 +01001437 struct wl_display *display = pointer->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001438 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01001439 struct wl_list *focus_resource_list;
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001440 int refocus = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001441
1442 if ((!pointer->focus && view) ||
1443 (pointer->focus && !view) ||
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001444 (pointer->focus && pointer->focus->surface != view->surface) ||
1445 pointer->sx != sx || pointer->sy != sy)
1446 refocus = 1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001447
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001448 if (pointer->focus_client && refocus) {
1449 focus_resource_list = &pointer->focus_client->pointer_resources;
1450 if (!wl_list_empty(focus_resource_list)) {
1451 serial = wl_display_next_serial(display);
1452 surface_resource = pointer->focus->surface->resource;
1453 wl_resource_for_each(resource, focus_resource_list) {
1454 wl_pointer_send_leave(resource, serial,
1455 surface_resource);
Peter Hutterer87743e92016-01-18 16:38:22 +10001456 pointer_send_frame(resource);
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001457 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001458 }
1459
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001460 pointer->focus_client = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001461 }
1462
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001463 pointer_client = find_pointer_client_for_view(pointer, view);
1464 if (pointer_client && refocus) {
1465 struct wl_client *surface_client = pointer_client->client;
Neil Roberts96d790e2013-09-19 17:32:00 +01001466
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001467 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001468
Jason Ekstranda7af7042013-10-12 22:38:11 -05001469 if (kbd && kbd->focus != view->surface)
Kristian Høgsbergcb406f12013-10-09 10:54:03 -07001470 send_modifiers_to_client_in_list(surface_client,
1471 &kbd->resource_list,
1472 serial,
1473 kbd);
1474
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001475 pointer->focus_client = pointer_client;
Neil Roberts96d790e2013-09-19 17:32:00 +01001476
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001477 focus_resource_list = &pointer->focus_client->pointer_resources;
Neil Roberts96d790e2013-09-19 17:32:00 +01001478 wl_resource_for_each(resource, focus_resource_list) {
1479 wl_pointer_send_enter(resource,
1480 serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001481 view->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01001482 sx, sy);
Peter Hutterer87743e92016-01-18 16:38:22 +10001483 pointer_send_frame(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001484 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001485
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001486 pointer->focus_serial = serial;
1487 }
1488
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001489 wl_list_remove(&pointer->focus_view_listener.link);
1490 wl_list_init(&pointer->focus_view_listener.link);
1491 wl_list_remove(&pointer->focus_resource_listener.link);
1492 wl_list_init(&pointer->focus_resource_listener.link);
Emilio Pozuelo Monfortaa7a4762013-11-19 11:37:15 +01001493 if (view)
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001494 wl_signal_add(&view->destroy_signal, &pointer->focus_view_listener);
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001495 if (view && view->surface->resource)
1496 wl_resource_add_destroy_listener(view->surface->resource,
1497 &pointer->focus_resource_listener);
1498
1499 pointer->focus = view;
1500 pointer->focus_view_listener.notify = pointer_focus_view_destroyed;
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001501 pointer->sx = sx;
1502 pointer->sy = sy;
1503
Derek Foremanf9318d12015-05-11 15:40:11 -05001504 assert(view || sx == wl_fixed_from_int(-1000000));
1505 assert(view || sy == wl_fixed_from_int(-1000000));
1506
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001507 wl_signal_emit(&pointer->focus_signal, pointer);
1508}
1509
Neil Roberts96d790e2013-09-19 17:32:00 +01001510static void
1511send_enter_to_resource_list(struct wl_list *list,
1512 struct weston_keyboard *keyboard,
1513 struct weston_surface *surface,
1514 uint32_t serial)
1515{
1516 struct wl_resource *resource;
1517
1518 wl_resource_for_each(resource, list) {
1519 send_modifiers_to_resource(keyboard, resource, serial);
1520 wl_keyboard_send_enter(resource, serial,
1521 surface->resource,
1522 &keyboard->keys);
1523 }
1524}
1525
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001526WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001527weston_keyboard_set_focus(struct weston_keyboard *keyboard,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001528 struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001529{
Quentin Glidic85d55542017-07-21 14:02:40 +02001530 struct weston_seat *seat = keyboard->seat;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001531 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +01001532 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001533 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01001534 struct wl_list *focus_resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001535
Pekka Paalanen72e183b2018-02-22 16:55:15 +02001536 /* Keyboard focus on a surface without a client is equivalent to NULL
1537 * focus as nothing would react to the keyboard events anyway.
1538 * Just set focus to NULL instead - the destroy listener hangs on the
1539 * wl_resource anyway.
1540 */
1541 if (surface && !surface->resource)
1542 surface = NULL;
1543
Neil Roberts96d790e2013-09-19 17:32:00 +01001544 focus_resource_list = &keyboard->focus_resource_list;
1545
1546 if (!wl_list_empty(focus_resource_list) && keyboard->focus != surface) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001547 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001548 wl_resource_for_each(resource, focus_resource_list) {
1549 wl_keyboard_send_leave(resource, serial,
1550 keyboard->focus->resource);
1551 }
1552 move_resources(&keyboard->resource_list, focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001553 }
1554
Neil Roberts96d790e2013-09-19 17:32:00 +01001555 if (find_resource_for_surface(&keyboard->resource_list, surface) &&
1556 keyboard->focus != surface) {
1557 struct wl_client *surface_client =
1558 wl_resource_get_client(surface->resource);
1559
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001560 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001561
1562 move_resources_for_client(focus_resource_list,
1563 &keyboard->resource_list,
1564 surface_client);
1565 send_enter_to_resource_list(focus_resource_list,
1566 keyboard,
1567 surface,
1568 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001569 keyboard->focus_serial = serial;
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001570 }
1571
Quentin Glidic85d55542017-07-21 14:02:40 +02001572 if (seat->saved_kbd_focus) {
1573 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1574 seat->saved_kbd_focus = NULL;
1575 }
1576
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001577 wl_list_remove(&keyboard->focus_resource_listener.link);
1578 wl_list_init(&keyboard->focus_resource_listener.link);
Pekka Paalanen72e183b2018-02-22 16:55:15 +02001579 if (surface)
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001580 wl_resource_add_destroy_listener(surface->resource,
1581 &keyboard->focus_resource_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001582
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001583 keyboard->focus = surface;
1584 wl_signal_emit(&keyboard->focus_signal, keyboard);
1585}
1586
Giulio Camuffoa20ca812014-11-22 11:16:56 +02001587/* Users of this function must manually manage the keyboard focus */
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001588WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001589weston_keyboard_start_grab(struct weston_keyboard *keyboard,
1590 struct weston_keyboard_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001591{
1592 keyboard->grab = grab;
1593 grab->keyboard = keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001594}
1595
1596WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001597weston_keyboard_end_grab(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001598{
1599 keyboard->grab = &keyboard->default_grab;
1600}
1601
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001602static void
1603weston_keyboard_cancel_grab(struct weston_keyboard *keyboard)
1604{
1605 keyboard->grab->interface->cancel(keyboard->grab);
1606}
1607
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001608WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001609weston_pointer_start_grab(struct weston_pointer *pointer,
1610 struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001611{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001612 pointer->grab = grab;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001613 grab->pointer = pointer;
Kristian Høgsbergda751b82013-07-04 00:58:07 -04001614 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001615}
1616
1617WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001618weston_pointer_end_grab(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001619{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001620 pointer->grab = &pointer->default_grab;
Kristian Høgsbergda751b82013-07-04 00:58:07 -04001621 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001622}
1623
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001624static void
1625weston_pointer_cancel_grab(struct weston_pointer *pointer)
1626{
1627 pointer->grab->interface->cancel(pointer->grab);
1628}
1629
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001630WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -04001631weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001632{
1633 touch->grab = grab;
1634 grab->touch = touch;
1635}
1636
1637WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -04001638weston_touch_end_grab(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001639{
1640 touch->grab = &touch->default_grab;
1641}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001642
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001643static void
1644weston_touch_cancel_grab(struct weston_touch *touch)
1645{
1646 touch->grab->interface->cancel(touch->grab);
1647}
1648
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001649static void
1650weston_pointer_clamp_for_output(struct weston_pointer *pointer,
1651 struct weston_output *output,
1652 wl_fixed_t *fx, wl_fixed_t *fy)
1653{
1654 int x, y;
1655
1656 x = wl_fixed_to_int(*fx);
1657 y = wl_fixed_to_int(*fy);
1658
1659 if (x < output->x)
1660 *fx = wl_fixed_from_int(output->x);
1661 else if (x >= output->x + output->width)
1662 *fx = wl_fixed_from_int(output->x +
1663 output->width - 1);
1664 if (y < output->y)
1665 *fy = wl_fixed_from_int(output->y);
1666 else if (y >= output->y + output->height)
1667 *fy = wl_fixed_from_int(output->y +
1668 output->height - 1);
1669}
1670
Rob Bradford806d8c02013-06-25 18:56:41 +01001671WL_EXPORT void
1672weston_pointer_clamp(struct weston_pointer *pointer, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001673{
Rob Bradford806d8c02013-06-25 18:56:41 +01001674 struct weston_compositor *ec = pointer->seat->compositor;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001675 struct weston_output *output, *prev = NULL;
1676 int x, y, old_x, old_y, valid = 0;
1677
1678 x = wl_fixed_to_int(*fx);
1679 y = wl_fixed_to_int(*fy);
Rob Bradford806d8c02013-06-25 18:56:41 +01001680 old_x = wl_fixed_to_int(pointer->x);
1681 old_y = wl_fixed_to_int(pointer->y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001682
1683 wl_list_for_each(output, &ec->output_list, link) {
Rob Bradford66bd9f52013-06-25 18:56:42 +01001684 if (pointer->seat->output && pointer->seat->output != output)
1685 continue;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001686 if (pixman_region32_contains_point(&output->region,
1687 x, y, NULL))
1688 valid = 1;
1689 if (pixman_region32_contains_point(&output->region,
1690 old_x, old_y, NULL))
1691 prev = output;
1692 }
1693
Rob Bradford66bd9f52013-06-25 18:56:42 +01001694 if (!prev)
1695 prev = pointer->seat->output;
1696
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001697 if (prev && !valid)
1698 weston_pointer_clamp_for_output(pointer, prev, fx, fy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001699}
1700
Jonas Ådahld2510102014-10-05 21:39:14 +02001701static void
1702weston_pointer_move_to(struct weston_pointer *pointer,
1703 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001704{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001705 int32_t ix, iy;
1706
Rob Bradford806d8c02013-06-25 18:56:41 +01001707 weston_pointer_clamp (pointer, &x, &y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001708
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001709 pointer->x = x;
1710 pointer->y = y;
1711
1712 ix = wl_fixed_to_int(x);
1713 iy = wl_fixed_to_int(y);
1714
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001715 if (pointer->sprite) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001716 weston_view_set_position(pointer->sprite,
1717 ix - pointer->hotspot_x,
1718 iy - pointer->hotspot_y);
1719 weston_view_schedule_repaint(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001720 }
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001721
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001722 pointer->grab->interface->focus(pointer->grab);
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001723 wl_signal_emit(&pointer->motion_signal, pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001724}
1725
Jonas Ådahld2510102014-10-05 21:39:14 +02001726WL_EXPORT void
Jonas Ådahld2510102014-10-05 21:39:14 +02001727weston_pointer_move(struct weston_pointer *pointer,
1728 struct weston_pointer_motion_event *event)
1729{
1730 wl_fixed_t x, y;
1731
1732 weston_pointer_motion_to_abs(pointer, event, &x, &y);
1733 weston_pointer_move_to(pointer, x, y);
1734}
1735
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001736/** Verify if the pointer is in a valid position and move it if it isn't.
1737 */
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001738static void
1739weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001740{
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001741 struct weston_pointer *pointer;
1742 struct weston_compositor *ec;
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001743 struct weston_output *output, *closest = NULL;
1744 int x, y, distance, min = INT_MAX;
1745 wl_fixed_t fx, fy;
1746
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001747 pointer = container_of(listener, struct weston_pointer,
1748 output_destroy_listener);
1749 ec = pointer->seat->compositor;
1750
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001751 x = wl_fixed_to_int(pointer->x);
1752 y = wl_fixed_to_int(pointer->y);
1753
1754 wl_list_for_each(output, &ec->output_list, link) {
1755 if (pixman_region32_contains_point(&output->region,
1756 x, y, NULL))
1757 return;
1758
1759 /* Aproximante the distance from the pointer to the center of
1760 * the output. */
1761 distance = abs(output->x + output->width / 2 - x) +
1762 abs(output->y + output->height / 2 - y);
1763 if (distance < min) {
1764 min = distance;
1765 closest = output;
1766 }
1767 }
1768
1769 /* Nothing to do if there's no output left. */
1770 if (!closest)
1771 return;
1772
1773 fx = pointer->x;
1774 fy = pointer->y;
1775
1776 weston_pointer_clamp_for_output(pointer, closest, &fx, &fy);
Jonas Ådahld2510102014-10-05 21:39:14 +02001777 weston_pointer_move_to(pointer, fx, fy);
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001778}
1779
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001780WL_EXPORT void
1781notify_motion(struct weston_seat *seat,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001782 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001783 struct weston_pointer_motion_event *event)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001784{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001785 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001786 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001787
1788 weston_compositor_wake(ec);
Jonas Ådahld2510102014-10-05 21:39:14 +02001789 pointer->grab->interface->motion(pointer->grab, time, event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001790}
1791
Daniel Stone96d47c02013-11-19 11:37:12 +01001792static void
1793run_modifier_bindings(struct weston_seat *seat, uint32_t old, uint32_t new)
1794{
1795 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001796 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Daniel Stone96d47c02013-11-19 11:37:12 +01001797 uint32_t diff;
1798 unsigned int i;
1799 struct {
1800 uint32_t xkb;
1801 enum weston_keyboard_modifier weston;
1802 } mods[] = {
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001803 { keyboard->xkb_info->ctrl_mod, MODIFIER_CTRL },
1804 { keyboard->xkb_info->alt_mod, MODIFIER_ALT },
1805 { keyboard->xkb_info->super_mod, MODIFIER_SUPER },
1806 { keyboard->xkb_info->shift_mod, MODIFIER_SHIFT },
Daniel Stone96d47c02013-11-19 11:37:12 +01001807 };
1808
1809 diff = new & ~old;
1810 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
1811 if (diff & (1 << mods[i].xkb))
1812 weston_compositor_run_modifier_binding(compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001813 keyboard,
Daniel Stone96d47c02013-11-19 11:37:12 +01001814 mods[i].weston,
1815 WL_KEYBOARD_KEY_STATE_PRESSED);
1816 }
1817
1818 diff = old & ~new;
1819 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
1820 if (diff & (1 << mods[i].xkb))
1821 weston_compositor_run_modifier_binding(compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001822 keyboard,
Daniel Stone96d47c02013-11-19 11:37:12 +01001823 mods[i].weston,
1824 WL_KEYBOARD_KEY_STATE_RELEASED);
1825 }
1826}
1827
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001828WL_EXPORT void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001829notify_motion_absolute(struct weston_seat *seat, const struct timespec *time,
1830 double x, double y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001831{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001832 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001833 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Jonas Ådahld2510102014-10-05 21:39:14 +02001834 struct weston_pointer_motion_event event = { 0 };
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001835
1836 weston_compositor_wake(ec);
Jonas Ådahld2510102014-10-05 21:39:14 +02001837
1838 event = (struct weston_pointer_motion_event) {
1839 .mask = WESTON_POINTER_MOTION_ABS,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001840 .x = x,
1841 .y = y,
Jonas Ådahld2510102014-10-05 21:39:14 +02001842 };
1843
1844 pointer->grab->interface->motion(pointer->grab, time, &event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001845}
1846
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02001847static unsigned int
1848peek_next_activate_serial(struct weston_compositor *c)
1849{
1850 unsigned serial = c->activate_serial + 1;
1851
1852 return serial == 0 ? 1 : serial;
1853}
1854
1855static void
1856inc_activate_serial(struct weston_compositor *c)
1857{
1858 c->activate_serial = peek_next_activate_serial (c);
1859}
1860
1861WL_EXPORT void
1862weston_view_activate(struct weston_view *view,
1863 struct weston_seat *seat,
1864 uint32_t flags)
1865{
1866 struct weston_compositor *compositor = seat->compositor;
1867
1868 if (flags & WESTON_ACTIVATE_FLAG_CLICKED) {
1869 view->click_to_activate_serial =
1870 peek_next_activate_serial(compositor);
1871 }
1872
1873 weston_seat_set_keyboard_focus(seat, view->surface);
1874}
1875
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001876WL_EXPORT void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001877notify_button(struct weston_seat *seat, const struct timespec *time,
1878 int32_t button, enum wl_pointer_button_state state)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001879{
1880 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001881 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001882
1883 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001884 weston_compositor_idle_inhibit(compositor);
1885 if (pointer->button_count == 0) {
1886 pointer->grab_button = button;
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001887 pointer->grab_time = *time;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001888 pointer->grab_x = pointer->x;
1889 pointer->grab_y = pointer->y;
1890 }
1891 pointer->button_count++;
1892 } else {
1893 weston_compositor_idle_release(compositor);
1894 pointer->button_count--;
1895 }
1896
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001897 weston_compositor_run_button_binding(compositor, pointer, time, button,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001898 state);
1899
1900 pointer->grab->interface->button(pointer->grab, time, button, state);
1901
1902 if (pointer->button_count == 1)
1903 pointer->grab_serial =
1904 wl_display_get_serial(compositor->wl_display);
1905}
1906
1907WL_EXPORT void
Alexandros Frantzis80321942017-11-16 18:20:56 +02001908notify_axis(struct weston_seat *seat, const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001909 struct weston_pointer_axis_event *event)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001910{
1911 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001912 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001913
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001914 weston_compositor_wake(compositor);
1915
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001916 if (weston_compositor_run_axis_binding(compositor, pointer,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001917 time, event))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001918 return;
1919
Peter Hutterer89b6a492016-01-18 15:58:17 +10001920 pointer->grab->interface->axis(pointer->grab, time, event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001921}
1922
Peter Hutterer87743e92016-01-18 16:38:22 +10001923WL_EXPORT void
1924notify_axis_source(struct weston_seat *seat, uint32_t source)
1925{
1926 struct weston_compositor *compositor = seat->compositor;
1927 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1928
1929 weston_compositor_wake(compositor);
1930
1931 pointer->grab->interface->axis_source(pointer->grab, source);
1932}
1933
1934WL_EXPORT void
1935notify_pointer_frame(struct weston_seat *seat)
1936{
1937 struct weston_compositor *compositor = seat->compositor;
1938 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1939
1940 weston_compositor_wake(compositor);
1941
1942 pointer->grab->interface->frame(pointer->grab);
1943}
1944
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001945WL_EXPORT int
1946weston_keyboard_set_locks(struct weston_keyboard *keyboard,
1947 uint32_t mask, uint32_t value)
1948{
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001949 uint32_t serial;
1950 xkb_mod_mask_t mods_depressed, mods_latched, mods_locked, group;
1951 xkb_mod_mask_t num, caps;
1952
1953 /* We don't want the leds to go out of sync with the actual state
1954 * so if the backend has no way to change the leds don't try to
1955 * change the state */
1956 if (!keyboard->seat->led_update)
1957 return -1;
1958
1959 mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
1960 XKB_STATE_DEPRESSED);
1961 mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
1962 XKB_STATE_LATCHED);
1963 mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
1964 XKB_STATE_LOCKED);
1965 group = xkb_state_serialize_group(keyboard->xkb_state.state,
1966 XKB_STATE_EFFECTIVE);
1967
1968 num = (1 << keyboard->xkb_info->mod2_mod);
1969 caps = (1 << keyboard->xkb_info->caps_mod);
1970 if (mask & WESTON_NUM_LOCK) {
1971 if (value & WESTON_NUM_LOCK)
1972 mods_locked |= num;
1973 else
1974 mods_locked &= ~num;
1975 }
1976 if (mask & WESTON_CAPS_LOCK) {
1977 if (value & WESTON_CAPS_LOCK)
1978 mods_locked |= caps;
1979 else
1980 mods_locked &= ~caps;
1981 }
1982
1983 xkb_state_update_mask(keyboard->xkb_state.state, mods_depressed,
1984 mods_latched, mods_locked, 0, 0, group);
1985
1986 serial = wl_display_next_serial(
1987 keyboard->seat->compositor->wl_display);
1988 notify_modifiers(keyboard->seat, serial);
1989
1990 return 0;
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001991}
1992
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001993WL_EXPORT void
1994notify_modifiers(struct weston_seat *seat, uint32_t serial)
1995{
Derek Foreman1281a362015-07-31 16:55:32 -05001996 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001997 struct weston_keyboard_grab *grab = keyboard->grab;
1998 uint32_t mods_depressed, mods_latched, mods_locked, group;
1999 uint32_t mods_lookup;
2000 enum weston_led leds = 0;
2001 int changed = 0;
2002
2003 /* Serialize and update our internal state, checking to see if it's
2004 * different to the previous state. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002005 mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03002006 XKB_STATE_MODS_DEPRESSED);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002007 mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03002008 XKB_STATE_MODS_LATCHED);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002009 mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03002010 XKB_STATE_MODS_LOCKED);
2011 group = xkb_state_serialize_layout(keyboard->xkb_state.state,
2012 XKB_STATE_LAYOUT_EFFECTIVE);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002013
Derek Foreman244e99e2015-06-03 15:53:26 -05002014 if (mods_depressed != keyboard->modifiers.mods_depressed ||
2015 mods_latched != keyboard->modifiers.mods_latched ||
2016 mods_locked != keyboard->modifiers.mods_locked ||
2017 group != keyboard->modifiers.group)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002018 changed = 1;
2019
Derek Foreman244e99e2015-06-03 15:53:26 -05002020 run_modifier_bindings(seat, keyboard->modifiers.mods_depressed,
Daniel Stone96d47c02013-11-19 11:37:12 +01002021 mods_depressed);
2022
Derek Foreman244e99e2015-06-03 15:53:26 -05002023 keyboard->modifiers.mods_depressed = mods_depressed;
2024 keyboard->modifiers.mods_latched = mods_latched;
2025 keyboard->modifiers.mods_locked = mods_locked;
2026 keyboard->modifiers.group = group;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002027
2028 /* And update the modifier_state for bindings. */
2029 mods_lookup = mods_depressed | mods_latched;
2030 seat->modifier_state = 0;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002031 if (mods_lookup & (1 << keyboard->xkb_info->ctrl_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002032 seat->modifier_state |= MODIFIER_CTRL;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002033 if (mods_lookup & (1 << keyboard->xkb_info->alt_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002034 seat->modifier_state |= MODIFIER_ALT;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002035 if (mods_lookup & (1 << keyboard->xkb_info->super_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002036 seat->modifier_state |= MODIFIER_SUPER;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002037 if (mods_lookup & (1 << keyboard->xkb_info->shift_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002038 seat->modifier_state |= MODIFIER_SHIFT;
2039
2040 /* Finally, notify the compositor that LEDs have changed. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002041 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
2042 keyboard->xkb_info->num_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002043 leds |= LED_NUM_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002044 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
2045 keyboard->xkb_info->caps_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002046 leds |= LED_CAPS_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002047 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
2048 keyboard->xkb_info->scroll_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002049 leds |= LED_SCROLL_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002050 if (leds != keyboard->xkb_state.leds && seat->led_update)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002051 seat->led_update(seat, leds);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002052 keyboard->xkb_state.leds = leds;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002053
2054 if (changed) {
2055 grab->interface->modifiers(grab,
2056 serial,
2057 keyboard->modifiers.mods_depressed,
2058 keyboard->modifiers.mods_latched,
2059 keyboard->modifiers.mods_locked,
2060 keyboard->modifiers.group);
2061 }
2062}
2063
2064static void
2065update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
2066 enum wl_keyboard_key_state state)
2067{
Derek Foreman1281a362015-07-31 16:55:32 -05002068 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002069 enum xkb_key_direction direction;
2070
2071 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2072 direction = XKB_KEY_DOWN;
2073 else
2074 direction = XKB_KEY_UP;
2075
2076 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
2077 * broken keycode system, which starts at 8. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002078 xkb_state_update_key(keyboard->xkb_state.state, key + 8, direction);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002079
2080 notify_modifiers(seat, serial);
2081}
Rui Matos65196bc2013-10-10 19:44:19 +02002082
2083static void
2084send_keymap(struct wl_resource *resource, struct weston_xkb_info *xkb_info)
2085{
2086 wl_keyboard_send_keymap(resource,
2087 WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2088 xkb_info->keymap_fd,
2089 xkb_info->keymap_size);
2090}
2091
2092static void
2093send_modifiers(struct wl_resource *resource, uint32_t serial, struct weston_keyboard *keyboard)
2094{
2095 wl_keyboard_send_modifiers(resource, serial,
2096 keyboard->modifiers.mods_depressed,
2097 keyboard->modifiers.mods_latched,
2098 keyboard->modifiers.mods_locked,
2099 keyboard->modifiers.group);
2100}
2101
2102static struct weston_xkb_info *
2103weston_xkb_info_create(struct xkb_keymap *keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02002104
2105static void
2106update_keymap(struct weston_seat *seat)
2107{
Derek Foreman1281a362015-07-31 16:55:32 -05002108 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Rui Matos65196bc2013-10-10 19:44:19 +02002109 struct wl_resource *resource;
2110 struct weston_xkb_info *xkb_info;
2111 struct xkb_state *state;
2112 xkb_mod_mask_t latched_mods;
2113 xkb_mod_mask_t locked_mods;
2114
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002115 xkb_info = weston_xkb_info_create(keyboard->pending_keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02002116
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002117 xkb_keymap_unref(keyboard->pending_keymap);
2118 keyboard->pending_keymap = NULL;
Rui Matos65196bc2013-10-10 19:44:19 +02002119
2120 if (!xkb_info) {
2121 weston_log("failed to create XKB info\n");
2122 return;
2123 }
2124
2125 state = xkb_state_new(xkb_info->keymap);
2126 if (!state) {
2127 weston_log("failed to initialise XKB state\n");
2128 weston_xkb_info_destroy(xkb_info);
2129 return;
2130 }
2131
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002132 latched_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
2133 XKB_STATE_MODS_LATCHED);
2134 locked_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
2135 XKB_STATE_MODS_LOCKED);
Rui Matos65196bc2013-10-10 19:44:19 +02002136 xkb_state_update_mask(state,
2137 0, /* depressed */
2138 latched_mods,
2139 locked_mods,
2140 0, 0, 0);
2141
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002142 weston_xkb_info_destroy(keyboard->xkb_info);
2143 keyboard->xkb_info = xkb_info;
Rui Matos65196bc2013-10-10 19:44:19 +02002144
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002145 xkb_state_unref(keyboard->xkb_state.state);
2146 keyboard->xkb_state.state = state;
Rui Matos65196bc2013-10-10 19:44:19 +02002147
Derek Foremanbc91e542015-06-03 15:53:27 -05002148 wl_resource_for_each(resource, &keyboard->resource_list)
Rui Matos65196bc2013-10-10 19:44:19 +02002149 send_keymap(resource, xkb_info);
Derek Foremanbc91e542015-06-03 15:53:27 -05002150 wl_resource_for_each(resource, &keyboard->focus_resource_list)
Rui Matos65196bc2013-10-10 19:44:19 +02002151 send_keymap(resource, xkb_info);
2152
2153 notify_modifiers(seat, wl_display_next_serial(seat->compositor->wl_display));
2154
2155 if (!latched_mods && !locked_mods)
2156 return;
2157
Derek Foremanbc91e542015-06-03 15:53:27 -05002158 wl_resource_for_each(resource, &keyboard->resource_list)
2159 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), keyboard);
2160 wl_resource_for_each(resource, &keyboard->focus_resource_list)
2161 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), keyboard);
Rui Matos65196bc2013-10-10 19:44:19 +02002162}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002163
2164WL_EXPORT void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02002165notify_key(struct weston_seat *seat, const struct timespec *time, uint32_t key,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002166 enum wl_keyboard_key_state state,
2167 enum weston_key_state_update update_state)
2168{
2169 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002170 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002171 struct weston_keyboard_grab *grab = keyboard->grab;
Pekka Paalanen86b53962014-11-19 13:43:32 +02002172 uint32_t *k, *end;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002173
2174 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002175 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002176 } else {
2177 weston_compositor_idle_release(compositor);
2178 }
2179
Pekka Paalanen86b53962014-11-19 13:43:32 +02002180 end = keyboard->keys.data + keyboard->keys.size;
2181 for (k = keyboard->keys.data; k < end; k++) {
2182 if (*k == key) {
2183 /* Ignore server-generated repeats. */
2184 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2185 return;
2186 *k = *--end;
2187 }
2188 }
2189 keyboard->keys.size = (void *) end - keyboard->keys.data;
2190 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2191 k = wl_array_add(&keyboard->keys, sizeof *k);
2192 *k = key;
2193 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002194
2195 if (grab == &keyboard->default_grab ||
2196 grab == &keyboard->input_method_grab) {
Derek Foreman99a6a2d2015-07-15 13:00:43 -05002197 weston_compositor_run_key_binding(compositor, keyboard, time,
2198 key, state);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002199 grab = keyboard->grab;
2200 }
2201
2202 grab->interface->key(grab, time, key, state);
2203
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002204 if (keyboard->pending_keymap &&
Pekka Paalanen86b53962014-11-19 13:43:32 +02002205 keyboard->keys.size == 0)
Rui Matos65196bc2013-10-10 19:44:19 +02002206 update_keymap(seat);
2207
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002208 if (update_state == STATE_UPDATE_AUTOMATIC) {
2209 update_modifier_state(seat,
2210 wl_display_get_serial(compositor->wl_display),
2211 key,
2212 state);
2213 }
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002214
Olivier Fourdandf84dbe2016-06-30 16:01:56 +02002215 keyboard->grab_serial = wl_display_get_serial(compositor->wl_display);
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002216 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02002217 keyboard->grab_time = *time;
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002218 keyboard->grab_key = key;
2219 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002220}
2221
2222WL_EXPORT void
2223notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002224 double x, double y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002225{
Derek Foreman1281a362015-07-31 16:55:32 -05002226 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2227
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002228 if (output) {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002229 weston_pointer_move_to(pointer,
2230 wl_fixed_from_double(x),
2231 wl_fixed_from_double(y));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002232 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002233 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002234 * NULL) here, but somehow that breaks re-entry... */
2235 }
2236}
2237
2238static void
2239destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
2240{
2241 struct weston_seat *ws;
2242
2243 ws = container_of(listener, struct weston_seat,
2244 saved_kbd_focus_listener);
2245
2246 ws->saved_kbd_focus = NULL;
2247}
2248
2249WL_EXPORT void
2250notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
2251 enum weston_key_state_update update_state)
2252{
2253 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002254 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04002255 struct weston_surface *surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002256 uint32_t *k, serial;
2257
2258 serial = wl_display_next_serial(compositor->wl_display);
2259 wl_array_copy(&keyboard->keys, keys);
2260 wl_array_for_each(k, &keyboard->keys) {
2261 weston_compositor_idle_inhibit(compositor);
2262 if (update_state == STATE_UPDATE_AUTOMATIC)
2263 update_modifier_state(seat, serial, *k,
2264 WL_KEYBOARD_KEY_STATE_PRESSED);
2265 }
2266
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002267 surface = seat->saved_kbd_focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002268 if (surface) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002269 weston_keyboard_set_focus(keyboard, surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002270 }
2271}
2272
2273WL_EXPORT void
2274notify_keyboard_focus_out(struct weston_seat *seat)
2275{
2276 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002277 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2278 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Quentin Glidic85d55542017-07-21 14:02:40 +02002279 struct weston_surface *focus = keyboard->focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002280 uint32_t *k, serial;
2281
2282 serial = wl_display_next_serial(compositor->wl_display);
2283 wl_array_for_each(k, &keyboard->keys) {
2284 weston_compositor_idle_release(compositor);
2285 update_modifier_state(seat, serial, *k,
2286 WL_KEYBOARD_KEY_STATE_RELEASED);
2287 }
2288
2289 seat->modifier_state = 0;
2290
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002291 weston_keyboard_set_focus(keyboard, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002292 weston_keyboard_cancel_grab(keyboard);
Derek Foreman1281a362015-07-31 16:55:32 -05002293 if (pointer)
2294 weston_pointer_cancel_grab(pointer);
Quentin Glidic85d55542017-07-21 14:02:40 +02002295
2296 if (focus) {
2297 seat->saved_kbd_focus = focus;
2298 seat->saved_kbd_focus_listener.notify =
2299 destroy_device_saved_kbd_focus;
2300 wl_signal_add(&focus->destroy_signal,
2301 &seat->saved_kbd_focus_listener);
2302 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002303}
2304
Michael Fua2bb7912013-07-23 15:51:06 +08002305WL_EXPORT void
Derek Foreman4c93c082015-04-30 16:45:41 -05002306weston_touch_set_focus(struct weston_touch *touch, struct weston_view *view)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002307{
Neil Roberts96d790e2013-09-19 17:32:00 +01002308 struct wl_list *focus_resource_list;
2309
Derek Foreman4c93c082015-04-30 16:45:41 -05002310 focus_resource_list = &touch->focus_resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002311
Derek Foreman4c93c082015-04-30 16:45:41 -05002312 if (view && touch->focus &&
2313 touch->focus->surface == view->surface) {
2314 touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002315 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002316 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002317
Derek Foreman4c93c082015-04-30 16:45:41 -05002318 wl_list_remove(&touch->focus_resource_listener.link);
2319 wl_list_init(&touch->focus_resource_listener.link);
2320 wl_list_remove(&touch->focus_view_listener.link);
2321 wl_list_init(&touch->focus_view_listener.link);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01002322
Neil Roberts96d790e2013-09-19 17:32:00 +01002323 if (!wl_list_empty(focus_resource_list)) {
Derek Foreman4c93c082015-04-30 16:45:41 -05002324 move_resources(&touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002325 focus_resource_list);
2326 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002327
Jason Ekstranda7af7042013-10-12 22:38:11 -05002328 if (view) {
Derek Foreman362656b2014-09-04 10:23:05 -05002329 struct wl_client *surface_client;
2330
2331 if (!view->surface->resource) {
Derek Foreman4c93c082015-04-30 16:45:41 -05002332 touch->focus = NULL;
Derek Foreman362656b2014-09-04 10:23:05 -05002333 return;
2334 }
2335
2336 surface_client = wl_resource_get_client(view->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +01002337 move_resources_for_client(focus_resource_list,
Derek Foreman4c93c082015-04-30 16:45:41 -05002338 &touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002339 surface_client);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01002340 wl_resource_add_destroy_listener(view->surface->resource,
Derek Foreman4c93c082015-04-30 16:45:41 -05002341 &touch->focus_resource_listener);
2342 wl_signal_add(&view->destroy_signal, &touch->focus_view_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002343 }
Derek Foreman4c93c082015-04-30 16:45:41 -05002344 touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002345}
2346
Pekka Paalanenf4062532018-02-26 16:18:29 +02002347static void
2348process_touch_normal(struct weston_touch_device *device,
2349 const struct timespec *time, int touch_id,
2350 double double_x, double double_y, int touch_type)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002351{
Pekka Paalanenbcbce332018-02-26 14:43:00 +02002352 struct weston_touch *touch = device->aggregate;
2353 struct weston_touch_grab *grab = device->aggregate->grab;
2354 struct weston_compositor *ec = device->aggregate->seat->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002355 struct weston_view *ev;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002356 wl_fixed_t sx, sy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002357 wl_fixed_t x = wl_fixed_from_double(double_x);
2358 wl_fixed_t y = wl_fixed_from_double(double_y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002359
2360 /* Update grab's global coordinates. */
Neil Roberts306fe082013-10-03 16:43:06 +01002361 if (touch_id == touch->grab_touch_id && touch_type != WL_TOUCH_UP) {
2362 touch->grab_x = x;
2363 touch->grab_y = y;
2364 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002365
2366 switch (touch_type) {
2367 case WL_TOUCH_DOWN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002368 /* the first finger down picks the view, and all further go
2369 * to that view for the remainder of the touch session i.e.
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002370 * until all touch points are up again. */
Jonas Ådahl9484b692013-12-02 22:05:03 +01002371 if (touch->num_tp == 1) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002372 ev = weston_compositor_pick_view(ec, x, y, &sx, &sy);
Derek Foreman4c93c082015-04-30 16:45:41 -05002373 weston_touch_set_focus(touch, ev);
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002374 } else if (!touch->focus) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002375 /* Unexpected condition: We have non-initial touch but
2376 * there is no focused surface.
2377 */
Chris Michael3f607d32015-10-07 11:59:49 -04002378 weston_log("touch event received with %d points down "
Jonas Ådahl9484b692013-12-02 22:05:03 +01002379 "but no surface focused\n", touch->num_tp);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002380 return;
2381 }
2382
Derek Foreman99a6a2d2015-07-15 13:00:43 -05002383 weston_compositor_run_touch_binding(ec, touch,
Kristian Høgsbergc8964012014-02-05 14:25:18 -08002384 time, touch_type);
2385
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002386 grab->interface->down(grab, time, touch_id, x, y);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002387 if (touch->num_tp == 1) {
Rusty Lynchf1407ff2013-08-08 21:13:57 -07002388 touch->grab_serial =
2389 wl_display_get_serial(ec->wl_display);
Neil Roberts306fe082013-10-03 16:43:06 +01002390 touch->grab_touch_id = touch_id;
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02002391 touch->grab_time = *time;
Rusty Lynchf1407ff2013-08-08 21:13:57 -07002392 touch->grab_x = x;
2393 touch->grab_y = y;
2394 }
2395
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002396 break;
2397 case WL_TOUCH_MOTION:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002398 ev = touch->focus;
2399 if (!ev)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002400 break;
2401
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +02002402 grab->interface->motion(grab, time, touch_id, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002403 break;
2404 case WL_TOUCH_UP:
Alexandros Frantzis27a51b82017-11-16 18:20:59 +02002405 grab->interface->up(grab, time, touch_id);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002406 if (touch->num_tp == 0)
Derek Foreman4c93c082015-04-30 16:45:41 -05002407 weston_touch_set_focus(touch, NULL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002408 break;
2409 }
2410}
2411
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002412static enum weston_touch_mode
2413get_next_touch_mode(enum weston_touch_mode from)
2414{
2415 switch (from) {
2416 case WESTON_TOUCH_MODE_PREP_NORMAL:
2417 return WESTON_TOUCH_MODE_NORMAL;
2418
2419 case WESTON_TOUCH_MODE_PREP_CALIB:
2420 return WESTON_TOUCH_MODE_CALIB;
2421
2422 case WESTON_TOUCH_MODE_NORMAL:
2423 case WESTON_TOUCH_MODE_CALIB:
2424 return from;
2425 }
2426
2427 return WESTON_TOUCH_MODE_NORMAL;
2428}
2429
2430/** Global touch mode update
2431 *
2432 * If no seat has a touch down and the compositor is in a PREP touch mode,
2433 * set the compositor to the goal touch mode.
2434 *
2435 * Calls calibrator if touch mode changed.
2436 */
2437static void
2438weston_compositor_update_touch_mode(struct weston_compositor *compositor)
2439{
2440 struct weston_seat *seat;
2441 struct weston_touch *touch;
2442 enum weston_touch_mode goal;
2443
2444 wl_list_for_each(seat, &compositor->seat_list, link) {
2445 touch = weston_seat_get_touch(seat);
2446 if (!touch)
2447 continue;
2448
2449 if (touch->num_tp > 0)
2450 return;
2451 }
2452
2453 goal = get_next_touch_mode(compositor->touch_mode);
2454 if (compositor->touch_mode != goal) {
2455 compositor->touch_mode = goal;
2456 touch_calibrator_mode_changed(compositor);
2457 }
2458}
2459
2460/** Start transition to normal touch event handling
2461 *
2462 * The touch event mode changes when all touches on all touch devices have
2463 * been lifted. If no touches are currently down, the transition is immediate.
2464 *
2465 * \sa weston_touch_mode
2466 */
2467void
2468weston_compositor_set_touch_mode_normal(struct weston_compositor *compositor)
2469{
2470 switch (compositor->touch_mode) {
2471 case WESTON_TOUCH_MODE_PREP_NORMAL:
2472 case WESTON_TOUCH_MODE_NORMAL:
2473 return;
2474 case WESTON_TOUCH_MODE_PREP_CALIB:
2475 compositor->touch_mode = WESTON_TOUCH_MODE_NORMAL;
2476 touch_calibrator_mode_changed(compositor);
2477 return;
2478 case WESTON_TOUCH_MODE_CALIB:
2479 compositor->touch_mode = WESTON_TOUCH_MODE_PREP_NORMAL;
2480 }
2481
2482 weston_compositor_update_touch_mode(compositor);
2483}
2484
2485/** Start transition to calibrator touch event handling
2486 *
2487 * The touch event mode changes when all touches on all touch devices have
2488 * been lifted. If no touches are currently down, the transition is immediate.
2489 *
2490 * \sa weston_touch_mode
2491 */
2492void
2493weston_compositor_set_touch_mode_calib(struct weston_compositor *compositor)
2494{
2495 switch (compositor->touch_mode) {
2496 case WESTON_TOUCH_MODE_PREP_CALIB:
2497 case WESTON_TOUCH_MODE_CALIB:
2498 assert(0);
2499 return;
2500 case WESTON_TOUCH_MODE_PREP_NORMAL:
2501 compositor->touch_mode = WESTON_TOUCH_MODE_CALIB;
2502 touch_calibrator_mode_changed(compositor);
2503 return;
2504 case WESTON_TOUCH_MODE_NORMAL:
2505 compositor->touch_mode = WESTON_TOUCH_MODE_PREP_CALIB;
2506 }
2507
2508 weston_compositor_update_touch_mode(compositor);
2509}
2510
Pekka Paalanenf4062532018-02-26 16:18:29 +02002511/** Feed in touch down, motion, and up events, calibratable device.
2512 *
2513 * It assumes always the correct cycle sequence until it gets here: touch_down
2514 * → touch_update → ... → touch_update → touch_end. The driver is responsible
2515 * for sending along such order.
2516 *
2517 * \param device The physical device that generated the event.
2518 * \param time The event timestamp.
2519 * \param touch_id ID for the touch point of this event (multi-touch).
2520 * \param double_x X coordinate in compositor global space.
2521 * \param double_y Y coordinate in compositor global space.
2522 * \param norm Normalized device X, Y coordinates in calibration space, or NULL.
2523 * \param touch_type Either WL_TOUCH_DOWN, WL_TOUCH_UP, or WL_TOUCH_MOTION.
2524 *
2525 * Coordinates double_x and double_y are used for normal operation.
2526 *
2527 * Coordinates norm are only used for touch device calibration. If and only if
2528 * the weston_touch_device does not support calibrating, norm must be NULL.
2529 *
2530 * The calibration space is the normalized coordinate space
2531 * [0.0, 1.0]×[0.0, 1.0] of the weston_touch_device. This is assumed to
2532 * map to the similar normalized coordinate space of the associated
2533 * weston_output.
2534 */
2535WL_EXPORT void
2536notify_touch_normalized(struct weston_touch_device *device,
2537 const struct timespec *time,
2538 int touch_id,
2539 double x, double y,
2540 const struct weston_point2d_device_normalized *norm,
2541 int touch_type)
2542{
Louis-Francis Ratté-Boulianne813a06e2017-11-28 20:42:47 -05002543 struct weston_seat *seat = device->aggregate->seat;
2544 struct weston_touch *touch = device->aggregate;
2545
Pekka Paalanenf4062532018-02-26 16:18:29 +02002546 if (touch_type != WL_TOUCH_UP) {
2547 if (weston_touch_device_can_calibrate(device))
2548 assert(norm != NULL);
2549 else
2550 assert(norm == NULL);
2551 }
2552
Louis-Francis Ratté-Boulianne813a06e2017-11-28 20:42:47 -05002553 /* Update touchpoints count regardless of the current mode. */
2554 switch (touch_type) {
2555 case WL_TOUCH_DOWN:
2556 weston_compositor_idle_inhibit(seat->compositor);
2557
2558 touch->num_tp++;
2559 break;
2560 case WL_TOUCH_UP:
2561 if (touch->num_tp == 0) {
2562 /* This can happen if we start out with one or
2563 * more fingers on the touch screen, in which
2564 * case we didn't get the corresponding down
2565 * event. */
Pekka Paalanen332a45e2018-03-02 14:20:59 +02002566 weston_log("Unmatched touch up event on seat %s, device %s\n",
2567 seat->seat_name, device->syspath);
2568 return;
Louis-Francis Ratté-Boulianne813a06e2017-11-28 20:42:47 -05002569 }
2570 weston_compositor_idle_release(seat->compositor);
2571
2572 touch->num_tp--;
2573 break;
2574 default:
2575 break;
2576 }
2577
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002578 /* Properly forward the touch event */
2579 switch (weston_touch_device_get_mode(device)) {
2580 case WESTON_TOUCH_MODE_NORMAL:
2581 case WESTON_TOUCH_MODE_PREP_CALIB:
2582 process_touch_normal(device, time, touch_id, x, y, touch_type);
2583 break;
2584 case WESTON_TOUCH_MODE_CALIB:
2585 case WESTON_TOUCH_MODE_PREP_NORMAL:
Louis-Francis Ratté-Boulianne83630982017-11-28 20:42:47 -05002586 notify_touch_calibrator(device, time, touch_id,
2587 norm, touch_type);
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002588 break;
2589 }
Pekka Paalanenf4062532018-02-26 16:18:29 +02002590}
2591
Jonas Ådahl1679f232014-04-12 09:39:51 +02002592WL_EXPORT void
Pekka Paalanenbcbce332018-02-26 14:43:00 +02002593notify_touch_frame(struct weston_touch_device *device)
Jonas Ådahl1679f232014-04-12 09:39:51 +02002594{
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002595 struct weston_touch_grab *grab;
Jonas Ådahl1679f232014-04-12 09:39:51 +02002596
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002597 switch (weston_touch_device_get_mode(device)) {
2598 case WESTON_TOUCH_MODE_NORMAL:
2599 case WESTON_TOUCH_MODE_PREP_CALIB:
2600 grab = device->aggregate->grab;
2601 grab->interface->frame(grab);
2602 break;
2603 case WESTON_TOUCH_MODE_CALIB:
2604 case WESTON_TOUCH_MODE_PREP_NORMAL:
Louis-Francis Ratté-Boulianne83630982017-11-28 20:42:47 -05002605 notify_touch_calibrator_frame(device);
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002606 break;
2607 }
2608
2609 weston_compositor_update_touch_mode(device->aggregate->seat->compositor);
Jonas Ådahl1679f232014-04-12 09:39:51 +02002610}
2611
Derek Foreman3cc004a2015-11-06 15:56:09 -06002612WL_EXPORT void
Pekka Paalanenbcbce332018-02-26 14:43:00 +02002613notify_touch_cancel(struct weston_touch_device *device)
Derek Foreman3cc004a2015-11-06 15:56:09 -06002614{
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002615 struct weston_touch_grab *grab;
Derek Foreman3cc004a2015-11-06 15:56:09 -06002616
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002617 switch (weston_touch_device_get_mode(device)) {
2618 case WESTON_TOUCH_MODE_NORMAL:
2619 case WESTON_TOUCH_MODE_PREP_CALIB:
2620 grab = device->aggregate->grab;
2621 grab->interface->cancel(grab);
2622 break;
2623 case WESTON_TOUCH_MODE_CALIB:
2624 case WESTON_TOUCH_MODE_PREP_NORMAL:
Louis-Francis Ratté-Boulianne83630982017-11-28 20:42:47 -05002625 notify_touch_calibrator_cancel(device);
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002626 break;
2627 }
2628
2629 weston_compositor_update_touch_mode(device->aggregate->seat->compositor);
Derek Foreman3cc004a2015-11-06 15:56:09 -06002630}
2631
Pekka Paalanen8274d902014-08-06 19:36:51 +03002632static int
2633pointer_cursor_surface_get_label(struct weston_surface *surface,
2634 char *buf, size_t len)
2635{
2636 return snprintf(buf, len, "cursor");
2637}
2638
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002639static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002640pointer_cursor_surface_committed(struct weston_surface *es,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002641 int32_t dx, int32_t dy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002642{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002643 struct weston_pointer *pointer = es->committed_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002644 int x, y;
2645
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002646 if (es->width == 0)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002647 return;
2648
Jason Ekstranda7af7042013-10-12 22:38:11 -05002649 assert(es == pointer->sprite->surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002650
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002651 pointer->hotspot_x -= dx;
2652 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002653
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002654 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
2655 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002656
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002657 weston_view_set_position(pointer->sprite, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002658
2659 empty_region(&es->pending.input);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02002660 empty_region(&es->input);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002661
2662 if (!weston_surface_is_mapped(es)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002663 weston_layer_entry_insert(&es->compositor->cursor_layer.view_list,
2664 &pointer->sprite->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002665 weston_view_update_transform(pointer->sprite);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002666 es->is_mapped = true;
2667 pointer->sprite->is_mapped = true;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002668 }
2669}
2670
2671static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002672pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2673 uint32_t serial, struct wl_resource *surface_resource,
2674 int32_t x, int32_t y)
2675{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002676 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002677 struct weston_surface *surface = NULL;
2678
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +02002679 if (!pointer)
2680 return;
2681
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002682 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002683 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002684
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002685 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002686 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002687 /* pointer->focus->surface->resource can be NULL. Surfaces like the
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02002688 black_surface used in shell.c for fullscreen don't have
2689 a resource, but can still have focus */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002690 if (pointer->focus->surface->resource == NULL)
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02002691 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002692 if (wl_resource_get_client(pointer->focus->surface->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002693 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002694 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002695 return;
2696
Derek Foreman4e53c532015-03-23 10:55:32 -05002697 if (!surface) {
2698 if (pointer->sprite)
2699 pointer_unmap_sprite(pointer);
2700 return;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002701 }
2702
Jonas Ådahlb4070242015-03-18 15:08:03 +08002703 if (pointer->sprite && pointer->sprite->surface == surface &&
2704 pointer->hotspot_x == x && pointer->hotspot_y == y)
2705 return;
2706
Derek Foreman4e53c532015-03-23 10:55:32 -05002707 if (!pointer->sprite || pointer->sprite->surface != surface) {
2708 if (weston_surface_set_role(surface, "wl_pointer-cursor",
2709 resource,
2710 WL_POINTER_ERROR_ROLE) < 0)
2711 return;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002712
Derek Foreman4e53c532015-03-23 10:55:32 -05002713 if (pointer->sprite)
2714 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002715
Derek Foreman4e53c532015-03-23 10:55:32 -05002716 wl_signal_add(&surface->destroy_signal,
2717 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002718
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002719 surface->committed = pointer_cursor_surface_committed;
2720 surface->committed_private = pointer;
Derek Foreman4e53c532015-03-23 10:55:32 -05002721 weston_surface_set_label_func(surface,
2722 pointer_cursor_surface_get_label);
2723 pointer->sprite = weston_view_create(surface);
2724 }
2725
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002726 pointer->hotspot_x = x;
2727 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002728
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +02002729 if (surface->buffer_ref.buffer) {
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002730 pointer_cursor_surface_committed(surface, 0, 0);
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +02002731 weston_view_schedule_repaint(pointer->sprite);
2732 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002733}
2734
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002735static void
2736pointer_release(struct wl_client *client, struct wl_resource *resource)
2737{
2738 wl_resource_destroy(resource);
2739}
2740
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002741static const struct wl_pointer_interface pointer_interface = {
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002742 pointer_set_cursor,
2743 pointer_release
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002744};
2745
2746static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002747seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2748 uint32_t id)
2749{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002750 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002751 /* We use the pointer_state directly, which means we'll
2752 * give a wl_pointer if the seat has ever had one - even though
2753 * the spec explicitly states that this request only takes effect
2754 * if the seat has the pointer capability.
2755 *
2756 * This prevents a race between the compositor sending new
2757 * capabilities and the client trying to use the old ones.
2758 */
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002759 struct weston_pointer *pointer = seat ? seat->pointer_state : NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002760 struct wl_resource *cr;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002761 struct weston_pointer_client *pointer_client;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002762
Jason Ekstranda85118c2013-06-27 20:17:02 -05002763 cr = wl_resource_create(client, &wl_pointer_interface,
2764 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002765 if (cr == NULL) {
2766 wl_client_post_no_memory(client);
2767 return;
2768 }
2769
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002770 wl_list_init(wl_resource_get_link(cr));
2771 wl_resource_set_implementation(cr, &pointer_interface, pointer,
2772 unbind_pointer_client_resource);
2773
2774 /* If we don't have a pointer_state, the resource is inert, so there
2775 * is nothing more to set up */
2776 if (!pointer)
2777 return;
2778
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002779 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
2780 if (!pointer_client) {
2781 wl_client_post_no_memory(client);
2782 return;
2783 }
2784
2785 wl_list_insert(&pointer_client->pointer_resources,
2786 wl_resource_get_link(cr));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002787
Derek Foreman1281a362015-07-31 16:55:32 -05002788 if (pointer->focus && pointer->focus->surface->resource &&
2789 wl_resource_get_client(pointer->focus->surface->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002790 wl_fixed_t sx, sy;
2791
Derek Foreman1281a362015-07-31 16:55:32 -05002792 weston_view_from_global_fixed(pointer->focus,
2793 pointer->x,
2794 pointer->y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002795 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002796
Neil Roberts96d790e2013-09-19 17:32:00 +01002797 wl_pointer_send_enter(cr,
Derek Foreman1281a362015-07-31 16:55:32 -05002798 pointer->focus_serial,
2799 pointer->focus->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01002800 sx, sy);
Peter Hutterer87743e92016-01-18 16:38:22 +10002801 pointer_send_frame(cr);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002802 }
2803}
2804
2805static void
Alexandros Frantzis2b442482018-02-20 14:05:50 +02002806destroy_keyboard_resource(struct wl_resource *resource)
2807{
2808 struct weston_keyboard *keyboard = wl_resource_get_user_data(resource);
2809
2810 wl_list_remove(wl_resource_get_link(resource));
2811
2812 if (keyboard) {
2813 remove_input_resource_from_timestamps(resource,
2814 &keyboard->timestamps_list);
2815 }
2816}
2817
2818static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002819keyboard_release(struct wl_client *client, struct wl_resource *resource)
2820{
2821 wl_resource_destroy(resource);
2822}
2823
2824static const struct wl_keyboard_interface keyboard_interface = {
2825 keyboard_release
2826};
2827
Derek Foreman280e7dd2014-10-03 13:13:42 -05002828static bool
Neil Roberts96d790e2013-09-19 17:32:00 +01002829should_send_modifiers_to_client(struct weston_seat *seat,
2830 struct wl_client *client)
2831{
Derek Foreman1281a362015-07-31 16:55:32 -05002832 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2833 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2834
2835 if (keyboard &&
2836 keyboard->focus &&
2837 keyboard->focus->resource &&
2838 wl_resource_get_client(keyboard->focus->resource) == client)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002839 return true;
Neil Roberts96d790e2013-09-19 17:32:00 +01002840
Derek Foreman1281a362015-07-31 16:55:32 -05002841 if (pointer &&
2842 pointer->focus &&
2843 pointer->focus->surface->resource &&
2844 wl_resource_get_client(pointer->focus->surface->resource) == client)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002845 return true;
Neil Roberts96d790e2013-09-19 17:32:00 +01002846
Derek Foreman280e7dd2014-10-03 13:13:42 -05002847 return false;
Neil Roberts96d790e2013-09-19 17:32:00 +01002848}
2849
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002850static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002851seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2852 uint32_t id)
2853{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002854 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002855 /* We use the keyboard_state directly, which means we'll
2856 * give a wl_keyboard if the seat has ever had one - even though
2857 * the spec explicitly states that this request only takes effect
2858 * if the seat has the keyboard capability.
2859 *
2860 * This prevents a race between the compositor sending new
2861 * capabilities and the client trying to use the old ones.
2862 */
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002863 struct weston_keyboard *keyboard = seat ? seat->keyboard_state : NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002864 struct wl_resource *cr;
2865
Jason Ekstranda85118c2013-06-27 20:17:02 -05002866 cr = wl_resource_create(client, &wl_keyboard_interface,
2867 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002868 if (cr == NULL) {
2869 wl_client_post_no_memory(client);
2870 return;
2871 }
2872
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002873 wl_list_init(wl_resource_get_link(cr));
2874 wl_resource_set_implementation(cr, &keyboard_interface,
Alexandros Frantzis2b442482018-02-20 14:05:50 +02002875 keyboard, destroy_keyboard_resource);
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002876
2877 /* If we don't have a keyboard_state, the resource is inert, so there
2878 * is nothing more to set up */
2879 if (!keyboard)
2880 return;
2881
Neil Roberts96d790e2013-09-19 17:32:00 +01002882 /* May be moved to focused list later by either
2883 * weston_keyboard_set_focus or directly if this client is already
2884 * focused */
Derek Foreman345c9f32015-06-03 15:53:28 -05002885 wl_list_insert(&keyboard->resource_list, wl_resource_get_link(cr));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002886
Jonny Lamb66a41a02014-08-12 14:58:25 +02002887 if (wl_resource_get_version(cr) >= WL_KEYBOARD_REPEAT_INFO_SINCE_VERSION) {
2888 wl_keyboard_send_repeat_info(cr,
2889 seat->compositor->kb_repeat_rate,
2890 seat->compositor->kb_repeat_delay);
2891 }
Jasper St. Pierred8c6aeb2014-08-04 13:43:24 -04002892
Derek Foreman185d1582017-06-28 11:17:23 -05002893 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2894 keyboard->xkb_info->keymap_fd,
2895 keyboard->xkb_info->keymap_size);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002896
Neil Roberts96d790e2013-09-19 17:32:00 +01002897 if (should_send_modifiers_to_client(seat, client)) {
Derek Foreman345c9f32015-06-03 15:53:28 -05002898 send_modifiers_to_resource(keyboard,
Neil Roberts96d790e2013-09-19 17:32:00 +01002899 cr,
Derek Foreman345c9f32015-06-03 15:53:28 -05002900 keyboard->focus_serial);
Neil Roberts96d790e2013-09-19 17:32:00 +01002901 }
2902
Derek Foreman345c9f32015-06-03 15:53:28 -05002903 if (keyboard->focus && keyboard->focus->resource &&
2904 wl_resource_get_client(keyboard->focus->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01002905 struct weston_surface *surface =
Derek Foreman345c9f32015-06-03 15:53:28 -05002906 (struct weston_surface *)keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +01002907
2908 wl_list_remove(wl_resource_get_link(cr));
Derek Foreman345c9f32015-06-03 15:53:28 -05002909 wl_list_insert(&keyboard->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002910 wl_resource_get_link(cr));
2911 wl_keyboard_send_enter(cr,
Derek Foreman345c9f32015-06-03 15:53:28 -05002912 keyboard->focus_serial,
Neil Roberts96d790e2013-09-19 17:32:00 +01002913 surface->resource,
Derek Foreman345c9f32015-06-03 15:53:28 -05002914 &keyboard->keys);
Neil Roberts96d790e2013-09-19 17:32:00 +01002915
2916 /* If this is the first keyboard resource for this
2917 * client... */
Derek Foreman345c9f32015-06-03 15:53:28 -05002918 if (keyboard->focus_resource_list.prev ==
Neil Roberts96d790e2013-09-19 17:32:00 +01002919 wl_resource_get_link(cr))
2920 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002921 }
2922}
2923
2924static void
Alexandros Frantzisd7157842018-02-20 14:07:03 +02002925destroy_touch_resource(struct wl_resource *resource)
2926{
2927 struct weston_touch *touch = wl_resource_get_user_data(resource);
2928
2929 wl_list_remove(wl_resource_get_link(resource));
2930
2931 if (touch) {
2932 remove_input_resource_from_timestamps(resource,
2933 &touch->timestamps_list);
2934 }
2935}
2936
2937static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002938touch_release(struct wl_client *client, struct wl_resource *resource)
2939{
2940 wl_resource_destroy(resource);
2941}
2942
2943static const struct wl_touch_interface touch_interface = {
2944 touch_release
2945};
2946
2947static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002948seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2949 uint32_t id)
2950{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002951 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002952 /* We use the touch_state directly, which means we'll
2953 * give a wl_touch if the seat has ever had one - even though
2954 * the spec explicitly states that this request only takes effect
2955 * if the seat has the touch capability.
2956 *
2957 * This prevents a race between the compositor sending new
2958 * capabilities and the client trying to use the old ones.
2959 */
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002960 struct weston_touch *touch = seat ? seat->touch_state : NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002961 struct wl_resource *cr;
2962
Jason Ekstranda85118c2013-06-27 20:17:02 -05002963 cr = wl_resource_create(client, &wl_touch_interface,
2964 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002965 if (cr == NULL) {
2966 wl_client_post_no_memory(client);
2967 return;
2968 }
2969
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002970 wl_list_init(wl_resource_get_link(cr));
2971 wl_resource_set_implementation(cr, &touch_interface,
Alexandros Frantzisd7157842018-02-20 14:07:03 +02002972 touch, destroy_touch_resource);
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002973
2974 /* If we don't have a touch_state, the resource is inert, so there
2975 * is nothing more to set up */
2976 if (!touch)
2977 return;
2978
Derek Foreman1281a362015-07-31 16:55:32 -05002979 if (touch->focus &&
2980 wl_resource_get_client(touch->focus->surface->resource) == client) {
Chokshi, Mituld6697142015-10-09 08:28:47 +00002981 wl_list_insert(&touch->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002982 wl_resource_get_link(cr));
2983 } else {
Chokshi, Mituld6697142015-10-09 08:28:47 +00002984 wl_list_insert(&touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002985 wl_resource_get_link(cr));
2986 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002987}
2988
Quentin Glidicaab1d362016-03-13 17:49:08 +01002989static void
2990seat_release(struct wl_client *client, struct wl_resource *resource)
2991{
2992 wl_resource_destroy(resource);
2993}
2994
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002995static const struct wl_seat_interface seat_interface = {
2996 seat_get_pointer,
2997 seat_get_keyboard,
2998 seat_get_touch,
Quentin Glidicaab1d362016-03-13 17:49:08 +01002999 seat_release,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003000};
3001
3002static void
3003bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3004{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003005 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003006 struct wl_resource *resource;
3007 enum wl_seat_capability caps = 0;
3008
Jason Ekstranda85118c2013-06-27 20:17:02 -05003009 resource = wl_resource_create(client,
Derek Foreman1909c102015-11-26 14:17:47 -06003010 &wl_seat_interface, version, id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05003011 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003012 wl_resource_set_implementation(resource, &seat_interface, data,
3013 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003014
Derek Foreman1281a362015-07-31 16:55:32 -05003015 if (weston_seat_get_pointer(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003016 caps |= WL_SEAT_CAPABILITY_POINTER;
Derek Foreman1281a362015-07-31 16:55:32 -05003017 if (weston_seat_get_keyboard(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003018 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Derek Foreman1281a362015-07-31 16:55:32 -05003019 if (weston_seat_get_touch(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003020 caps |= WL_SEAT_CAPABILITY_TOUCH;
3021
3022 wl_seat_send_capabilities(resource, caps);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003023 if (version >= WL_SEAT_NAME_SINCE_VERSION)
Rob Bradforde445ae62013-05-31 18:09:51 +01003024 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003025}
3026
Jonas Ådahl30d61d82014-10-22 21:21:17 +02003027static void
3028relative_pointer_destroy(struct wl_client *client,
3029 struct wl_resource *resource)
3030{
3031 wl_resource_destroy(resource);
3032}
3033
3034static const struct zwp_relative_pointer_v1_interface relative_pointer_interface = {
3035 relative_pointer_destroy
3036};
3037
3038static void
3039relative_pointer_manager_destroy(struct wl_client *client,
3040 struct wl_resource *resource)
3041{
3042 wl_resource_destroy(resource);
3043}
3044
3045static void
3046relative_pointer_manager_get_relative_pointer(struct wl_client *client,
3047 struct wl_resource *resource,
3048 uint32_t id,
3049 struct wl_resource *pointer_resource)
3050{
3051 struct weston_pointer *pointer =
3052 wl_resource_get_user_data(pointer_resource);
3053 struct weston_pointer_client *pointer_client;
3054 struct wl_resource *cr;
3055
3056 cr = wl_resource_create(client, &zwp_relative_pointer_v1_interface,
3057 wl_resource_get_version(resource), id);
3058 if (cr == NULL) {
3059 wl_client_post_no_memory(client);
3060 return;
3061 }
3062
3063 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
3064 if (!pointer_client) {
3065 wl_client_post_no_memory(client);
3066 return;
3067 }
3068
3069 wl_list_insert(&pointer_client->relative_pointer_resources,
3070 wl_resource_get_link(cr));
3071 wl_resource_set_implementation(cr, &relative_pointer_interface,
3072 pointer,
3073 unbind_pointer_client_resource);
3074}
3075
3076static const struct zwp_relative_pointer_manager_v1_interface relative_pointer_manager = {
3077 relative_pointer_manager_destroy,
3078 relative_pointer_manager_get_relative_pointer,
3079};
3080
3081static void
3082bind_relative_pointer_manager(struct wl_client *client, void *data,
3083 uint32_t version, uint32_t id)
3084{
3085 struct weston_compositor *compositor = data;
3086 struct wl_resource *resource;
3087
3088 resource = wl_resource_create(client,
3089 &zwp_relative_pointer_manager_v1_interface,
3090 1, id);
3091
3092 wl_resource_set_implementation(resource, &relative_pointer_manager,
3093 compositor,
3094 NULL);
3095}
3096
Giulio Camuffo0358af42016-06-02 21:48:08 +03003097WL_EXPORT int
3098weston_compositor_set_xkb_rule_names(struct weston_compositor *ec,
3099 struct xkb_rule_names *names)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003100{
3101 if (ec->xkb_context == NULL) {
3102 ec->xkb_context = xkb_context_new(0);
3103 if (ec->xkb_context == NULL) {
3104 weston_log("failed to create XKB context\n");
3105 return -1;
3106 }
3107 }
3108
3109 if (names)
3110 ec->xkb_names = *names;
3111 if (!ec->xkb_names.rules)
3112 ec->xkb_names.rules = strdup("evdev");
3113 if (!ec->xkb_names.model)
3114 ec->xkb_names.model = strdup("pc105");
3115 if (!ec->xkb_names.layout)
3116 ec->xkb_names.layout = strdup("us");
3117
3118 return 0;
3119}
3120
Stefan Schmidtfda26522013-09-17 10:54:09 +01003121static void
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003122weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003123{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003124 if (--xkb_info->ref_count > 0)
3125 return;
3126
Ran Benitac9c74152014-08-19 23:59:52 +03003127 xkb_keymap_unref(xkb_info->keymap);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003128
3129 if (xkb_info->keymap_area)
3130 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
3131 if (xkb_info->keymap_fd >= 0)
3132 close(xkb_info->keymap_fd);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003133 free(xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003134}
3135
3136void
3137weston_compositor_xkb_destroy(struct weston_compositor *ec)
3138{
3139 free((char *) ec->xkb_names.rules);
3140 free((char *) ec->xkb_names.model);
3141 free((char *) ec->xkb_names.layout);
3142 free((char *) ec->xkb_names.variant);
3143 free((char *) ec->xkb_names.options);
Stefan Schmidtfda26522013-09-17 10:54:09 +01003144
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003145 if (ec->xkb_info)
3146 weston_xkb_info_destroy(ec->xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003147 xkb_context_unref(ec->xkb_context);
3148}
3149
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003150static struct weston_xkb_info *
3151weston_xkb_info_create(struct xkb_keymap *keymap)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003152{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003153 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
3154 if (xkb_info == NULL)
3155 return NULL;
3156
Ran Benita2e1968f2014-08-19 23:59:51 +03003157 xkb_info->keymap = xkb_keymap_ref(keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003158 xkb_info->ref_count = 1;
3159
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003160 char *keymap_str;
3161
Ran Benita2e1968f2014-08-19 23:59:51 +03003162 xkb_info->shift_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3163 XKB_MOD_NAME_SHIFT);
3164 xkb_info->caps_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3165 XKB_MOD_NAME_CAPS);
3166 xkb_info->ctrl_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3167 XKB_MOD_NAME_CTRL);
3168 xkb_info->alt_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3169 XKB_MOD_NAME_ALT);
3170 xkb_info->mod2_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3171 "Mod2");
3172 xkb_info->mod3_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3173 "Mod3");
3174 xkb_info->super_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3175 XKB_MOD_NAME_LOGO);
3176 xkb_info->mod5_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3177 "Mod5");
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003178
Ran Benita2e1968f2014-08-19 23:59:51 +03003179 xkb_info->num_led = xkb_keymap_led_get_index(xkb_info->keymap,
3180 XKB_LED_NAME_NUM);
3181 xkb_info->caps_led = xkb_keymap_led_get_index(xkb_info->keymap,
3182 XKB_LED_NAME_CAPS);
3183 xkb_info->scroll_led = xkb_keymap_led_get_index(xkb_info->keymap,
3184 XKB_LED_NAME_SCROLL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003185
Ran Benita2e1968f2014-08-19 23:59:51 +03003186 keymap_str = xkb_keymap_get_as_string(xkb_info->keymap,
3187 XKB_KEYMAP_FORMAT_TEXT_V1);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003188 if (keymap_str == NULL) {
3189 weston_log("failed to get string version of keymap\n");
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003190 goto err_keymap;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003191 }
3192 xkb_info->keymap_size = strlen(keymap_str) + 1;
3193
3194 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
3195 if (xkb_info->keymap_fd < 0) {
3196 weston_log("creating a keymap file for %lu bytes failed: %m\n",
3197 (unsigned long) xkb_info->keymap_size);
3198 goto err_keymap_str;
3199 }
3200
3201 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
3202 PROT_READ | PROT_WRITE,
3203 MAP_SHARED, xkb_info->keymap_fd, 0);
3204 if (xkb_info->keymap_area == MAP_FAILED) {
3205 weston_log("failed to mmap() %lu bytes\n",
3206 (unsigned long) xkb_info->keymap_size);
3207 goto err_dev_zero;
3208 }
3209 strcpy(xkb_info->keymap_area, keymap_str);
3210 free(keymap_str);
3211
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003212 return xkb_info;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003213
3214err_dev_zero:
3215 close(xkb_info->keymap_fd);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003216err_keymap_str:
3217 free(keymap_str);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003218err_keymap:
Ran Benita2e1968f2014-08-19 23:59:51 +03003219 xkb_keymap_unref(xkb_info->keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003220 free(xkb_info);
3221 return NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003222}
3223
3224static int
3225weston_compositor_build_global_keymap(struct weston_compositor *ec)
3226{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003227 struct xkb_keymap *keymap;
3228
3229 if (ec->xkb_info != NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003230 return 0;
3231
Ran Benita2e1968f2014-08-19 23:59:51 +03003232 keymap = xkb_keymap_new_from_names(ec->xkb_context,
3233 &ec->xkb_names,
3234 0);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003235 if (keymap == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003236 weston_log("failed to compile global XKB keymap\n");
3237 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
3238 "options %s\n",
3239 ec->xkb_names.rules, ec->xkb_names.model,
3240 ec->xkb_names.layout, ec->xkb_names.variant,
3241 ec->xkb_names.options);
3242 return -1;
3243 }
3244
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003245 ec->xkb_info = weston_xkb_info_create(keymap);
Rui Matos73d93952013-10-24 19:28:41 +02003246 xkb_keymap_unref(keymap);
Stefan Schmidtfda26522013-09-17 10:54:09 +01003247 if (ec->xkb_info == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003248 return -1;
3249
3250 return 0;
3251}
3252
Rui Matos65196bc2013-10-10 19:44:19 +02003253WL_EXPORT void
3254weston_seat_update_keymap(struct weston_seat *seat, struct xkb_keymap *keymap)
3255{
Derek Foreman1281a362015-07-31 16:55:32 -05003256 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
3257
3258 if (!keyboard || !keymap)
Rui Matos65196bc2013-10-10 19:44:19 +02003259 return;
3260
Derek Foreman1281a362015-07-31 16:55:32 -05003261 xkb_keymap_unref(keyboard->pending_keymap);
3262 keyboard->pending_keymap = xkb_keymap_ref(keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02003263
Derek Foreman1281a362015-07-31 16:55:32 -05003264 if (keyboard->keys.size == 0)
Rui Matos65196bc2013-10-10 19:44:19 +02003265 update_keymap(seat);
Rui Matos65196bc2013-10-10 19:44:19 +02003266}
3267
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003268WL_EXPORT int
3269weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
3270{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003271 struct weston_keyboard *keyboard;
3272
Derek Foreman1281a362015-07-31 16:55:32 -05003273 if (seat->keyboard_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003274 seat->keyboard_device_count += 1;
3275 if (seat->keyboard_device_count == 1)
3276 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003277 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003278 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003279
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003280 keyboard = weston_keyboard_create();
3281 if (keyboard == NULL) {
3282 weston_log("failed to allocate weston keyboard struct\n");
3283 return -1;
3284 }
3285
Derek Foreman185d1582017-06-28 11:17:23 -05003286 if (keymap != NULL) {
3287 keyboard->xkb_info = weston_xkb_info_create(keymap);
3288 if (keyboard->xkb_info == NULL)
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02003289 goto err;
Derek Foreman185d1582017-06-28 11:17:23 -05003290 } else {
3291 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
3292 goto err;
3293 keyboard->xkb_info = seat->compositor->xkb_info;
3294 keyboard->xkb_info->ref_count++;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01003295 }
Derek Foreman185d1582017-06-28 11:17:23 -05003296
3297 keyboard->xkb_state.state = xkb_state_new(keyboard->xkb_info->keymap);
3298 if (keyboard->xkb_state.state == NULL) {
3299 weston_log("failed to initialise XKB state\n");
3300 goto err;
3301 }
3302
3303 keyboard->xkb_state.leds = 0;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01003304
Derek Foreman1281a362015-07-31 16:55:32 -05003305 seat->keyboard_state = keyboard;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02003306 seat->keyboard_device_count = 1;
3307 keyboard->seat = seat;
3308
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003309 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003310
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003311 return 0;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02003312
3313err:
3314 if (keyboard->xkb_info)
3315 weston_xkb_info_destroy(keyboard->xkb_info);
3316 free(keyboard);
3317
3318 return -1;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003319}
3320
Jonas Ådahl91fed542013-12-03 09:14:27 +01003321static void
3322weston_keyboard_reset_state(struct weston_keyboard *keyboard)
3323{
3324 struct weston_seat *seat = keyboard->seat;
3325 struct xkb_state *state;
3326
Derek Foreman185d1582017-06-28 11:17:23 -05003327 state = xkb_state_new(keyboard->xkb_info->keymap);
3328 if (!state) {
3329 weston_log("failed to reset XKB state\n");
3330 return;
Jonas Ådahl91fed542013-12-03 09:14:27 +01003331 }
Derek Foreman185d1582017-06-28 11:17:23 -05003332 xkb_state_unref(keyboard->xkb_state.state);
3333 keyboard->xkb_state.state = state;
3334
3335 keyboard->xkb_state.leds = 0;
Jonas Ådahl91fed542013-12-03 09:14:27 +01003336
3337 seat->modifier_state = 0;
3338}
3339
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003340WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003341weston_seat_release_keyboard(struct weston_seat *seat)
3342{
3343 seat->keyboard_device_count--;
Derek Foremand621df22014-11-19 11:04:12 -06003344 assert(seat->keyboard_device_count >= 0);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003345 if (seat->keyboard_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05003346 weston_keyboard_set_focus(seat->keyboard_state, NULL);
3347 weston_keyboard_cancel_grab(seat->keyboard_state);
3348 weston_keyboard_reset_state(seat->keyboard_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003349 seat_send_updated_caps(seat);
3350 }
3351}
3352
3353WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003354weston_seat_init_pointer(struct weston_seat *seat)
3355{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003356 struct weston_pointer *pointer;
3357
Derek Foreman1281a362015-07-31 16:55:32 -05003358 if (seat->pointer_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003359 seat->pointer_device_count += 1;
3360 if (seat->pointer_device_count == 1)
3361 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003362 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003363 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003364
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003365 pointer = weston_pointer_create(seat);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003366 if (pointer == NULL)
3367 return;
3368
Derek Foreman1281a362015-07-31 16:55:32 -05003369 seat->pointer_state = pointer;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003370 seat->pointer_device_count = 1;
3371 pointer->seat = seat;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003372
3373 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003374}
3375
3376WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003377weston_seat_release_pointer(struct weston_seat *seat)
3378{
Derek Foreman1281a362015-07-31 16:55:32 -05003379 struct weston_pointer *pointer = seat->pointer_state;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003380
3381 seat->pointer_device_count--;
3382 if (seat->pointer_device_count == 0) {
Derek Foremanf9318d12015-05-11 15:40:11 -05003383 weston_pointer_clear_focus(pointer);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003384 weston_pointer_cancel_grab(pointer);
Jonas Ådahl630bae82013-10-17 23:04:06 +02003385
Jonas Ådahla4932742013-10-17 23:04:07 +02003386 if (pointer->sprite)
3387 pointer_unmap_sprite(pointer);
3388
Jonas Ådahl3e12e632013-12-02 22:05:05 +01003389 weston_pointer_reset_state(pointer);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003390 seat_send_updated_caps(seat);
Derek Foremanfd5ca512015-01-07 15:00:25 -06003391
3392 /* seat->pointer is intentionally not destroyed so that
3393 * a newly attached pointer on this seat will retain
3394 * the previous cursor co-ordinates.
3395 */
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003396 }
3397}
3398
3399WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003400weston_seat_init_touch(struct weston_seat *seat)
3401{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003402 struct weston_touch *touch;
3403
Derek Foreman1281a362015-07-31 16:55:32 -05003404 if (seat->touch_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003405 seat->touch_device_count += 1;
3406 if (seat->touch_device_count == 1)
3407 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003408 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003409 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003410
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003411 touch = weston_touch_create();
3412 if (touch == NULL)
3413 return;
3414
Derek Foreman1281a362015-07-31 16:55:32 -05003415 seat->touch_state = touch;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003416 seat->touch_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003417 touch->seat = seat;
3418
3419 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003420}
3421
3422WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003423weston_seat_release_touch(struct weston_seat *seat)
3424{
3425 seat->touch_device_count--;
3426 if (seat->touch_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05003427 weston_touch_set_focus(seat->touch_state, NULL);
3428 weston_touch_cancel_grab(seat->touch_state);
3429 weston_touch_reset_state(seat->touch_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003430 seat_send_updated_caps(seat);
3431 }
3432}
3433
3434WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003435weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
3436 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003437{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003438 memset(seat, 0, sizeof *seat);
3439
Kristian Høgsberge3148752013-05-06 23:19:49 -04003440 seat->selection_data_source = NULL;
3441 wl_list_init(&seat->base_resource_list);
3442 wl_signal_init(&seat->selection_signal);
3443 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04003444 wl_signal_init(&seat->destroy_signal);
Jason Ekstranda4ab5422014-04-02 19:53:45 -05003445 wl_signal_init(&seat->updated_caps_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003446
Peter Hutterer87743e92016-01-18 16:38:22 +10003447 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 5,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003448 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003449
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003450 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003451 seat->modifier_state = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003452 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003453
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003454 wl_list_insert(ec->seat_list.prev, &seat->link);
3455
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003456 clipboard_create(seat);
3457
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003458 wl_signal_emit(&ec->seat_created_signal, seat);
3459}
3460
3461WL_EXPORT void
3462weston_seat_release(struct weston_seat *seat)
3463{
Alexandros Frantzis8480d132018-02-15 13:07:09 +02003464 struct wl_resource *resource;
3465
3466 wl_resource_for_each(resource, &seat->base_resource_list) {
3467 wl_resource_set_user_data(resource, NULL);
3468 }
3469
3470 wl_resource_for_each(resource, &seat->drag_resource_list) {
3471 wl_resource_set_user_data(resource, NULL);
3472 }
3473
3474 wl_list_remove(&seat->base_resource_list);
3475 wl_list_remove(&seat->drag_resource_list);
3476
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003477 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003478
Jonas Ådahl1afb2382014-01-03 19:46:51 +01003479 if (seat->saved_kbd_focus)
3480 wl_list_remove(&seat->saved_kbd_focus_listener.link);
3481
Derek Foreman1281a362015-07-31 16:55:32 -05003482 if (seat->pointer_state)
3483 weston_pointer_destroy(seat->pointer_state);
3484 if (seat->keyboard_state)
3485 weston_keyboard_destroy(seat->keyboard_state);
3486 if (seat->touch_state)
3487 weston_touch_destroy(seat->touch_state);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003488
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003489 free (seat->seat_name);
3490
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003491 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04003492
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003493 wl_signal_emit(&seat->destroy_signal, seat);
3494}
Derek Foreman1281a362015-07-31 16:55:32 -05003495
3496/** Get a seat's keyboard pointer
3497 *
3498 * \param seat The seat to query
3499 * \return The seat's keyboard pointer, or NULL if no keyboard is present
3500 *
3501 * The keyboard pointer for a seat isn't freed when all keyboards are removed,
3502 * so it should only be used when the seat's keyboard_device_count is greater
3503 * than zero. This function does that test and only returns a pointer
3504 * when a keyboard is present.
3505 */
3506WL_EXPORT struct weston_keyboard *
3507weston_seat_get_keyboard(struct weston_seat *seat)
3508{
3509 if (!seat)
3510 return NULL;
3511
3512 if (seat->keyboard_device_count)
3513 return seat->keyboard_state;
3514
3515 return NULL;
3516}
3517
3518/** Get a seat's pointer pointer
3519 *
3520 * \param seat The seat to query
3521 * \return The seat's pointer pointer, or NULL if no pointer device is present
3522 *
3523 * The pointer pointer for a seat isn't freed when all mice are removed,
3524 * so it should only be used when the seat's pointer_device_count is greater
3525 * than zero. This function does that test and only returns a pointer
3526 * when a pointing device is present.
3527 */
3528WL_EXPORT struct weston_pointer *
3529weston_seat_get_pointer(struct weston_seat *seat)
3530{
3531 if (!seat)
3532 return NULL;
3533
3534 if (seat->pointer_device_count)
3535 return seat->pointer_state;
3536
3537 return NULL;
3538}
3539
Jonas Ådahld3414f22016-07-22 17:56:31 +08003540static const struct zwp_locked_pointer_v1_interface locked_pointer_interface;
3541static const struct zwp_confined_pointer_v1_interface confined_pointer_interface;
3542
3543static enum pointer_constraint_type
3544pointer_constraint_get_type(struct weston_pointer_constraint *constraint)
3545{
3546 if (wl_resource_instance_of(constraint->resource,
3547 &zwp_locked_pointer_v1_interface,
3548 &locked_pointer_interface)) {
3549 return POINTER_CONSTRAINT_TYPE_LOCK;
3550 } else if (wl_resource_instance_of(constraint->resource,
3551 &zwp_confined_pointer_v1_interface,
3552 &confined_pointer_interface)) {
3553 return POINTER_CONSTRAINT_TYPE_CONFINE;
3554 }
3555
3556 abort();
3557 return 0;
3558}
3559
3560static void
3561pointer_constraint_notify_activated(struct weston_pointer_constraint *constraint)
3562{
3563 struct wl_resource *resource = constraint->resource;
3564
3565 switch (pointer_constraint_get_type(constraint)) {
3566 case POINTER_CONSTRAINT_TYPE_LOCK:
3567 zwp_locked_pointer_v1_send_locked(resource);
3568 break;
3569 case POINTER_CONSTRAINT_TYPE_CONFINE:
3570 zwp_confined_pointer_v1_send_confined(resource);
3571 break;
3572 }
3573}
3574
3575static void
3576pointer_constraint_notify_deactivated(struct weston_pointer_constraint *constraint)
3577{
3578 struct wl_resource *resource = constraint->resource;
3579
3580 switch (pointer_constraint_get_type(constraint)) {
3581 case POINTER_CONSTRAINT_TYPE_LOCK:
3582 zwp_locked_pointer_v1_send_unlocked(resource);
3583 break;
3584 case POINTER_CONSTRAINT_TYPE_CONFINE:
3585 zwp_confined_pointer_v1_send_unconfined(resource);
3586 break;
3587 }
3588}
3589
3590static struct weston_pointer_constraint *
3591get_pointer_constraint_for_pointer(struct weston_surface *surface,
3592 struct weston_pointer *pointer)
3593{
3594 struct weston_pointer_constraint *constraint;
3595
3596 wl_list_for_each(constraint, &surface->pointer_constraints, link) {
3597 if (constraint->pointer == pointer)
3598 return constraint;
3599 }
3600
3601 return NULL;
3602}
3603
Derek Foreman1281a362015-07-31 16:55:32 -05003604/** Get a seat's touch pointer
3605 *
3606 * \param seat The seat to query
3607 * \return The seat's touch pointer, or NULL if no touch device is present
3608 *
3609 * The touch pointer for a seat isn't freed when all touch devices are removed,
3610 * so it should only be used when the seat's touch_device_count is greater
3611 * than zero. This function does that test and only returns a pointer
3612 * when a touch device is present.
3613 */
3614WL_EXPORT struct weston_touch *
3615weston_seat_get_touch(struct weston_seat *seat)
3616{
3617 if (!seat)
3618 return NULL;
3619
3620 if (seat->touch_device_count)
3621 return seat->touch_state;
3622
3623 return NULL;
3624}
Bryce Harrington24f917e2016-06-29 19:04:07 -07003625
3626/** Sets the keyboard focus to the given surface
3627 *
3628 * \param seat The seat to query
3629 */
3630WL_EXPORT void
3631weston_seat_set_keyboard_focus(struct weston_seat *seat,
3632 struct weston_surface *surface)
3633{
3634 struct weston_compositor *compositor = seat->compositor;
3635 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003636 struct weston_surface_activation_data activation_data;
Bryce Harrington24f917e2016-06-29 19:04:07 -07003637
Jonas Ådahlef8e1c32016-03-15 20:28:51 +08003638 if (keyboard && keyboard->focus != surface) {
Bryce Harrington24f917e2016-06-29 19:04:07 -07003639 weston_keyboard_set_focus(keyboard, surface);
3640 wl_data_device_set_keyboard_focus(seat);
3641 }
3642
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003643 inc_activate_serial(compositor);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003644
3645 activation_data = (struct weston_surface_activation_data) {
3646 .surface = surface,
3647 .seat = seat,
3648 };
3649 wl_signal_emit(&compositor->activate_signal, &activation_data);
Bryce Harrington24f917e2016-06-29 19:04:07 -07003650}
Jonas Ådahl30d61d82014-10-22 21:21:17 +02003651
Jonas Ådahld3414f22016-07-22 17:56:31 +08003652static void
3653enable_pointer_constraint(struct weston_pointer_constraint *constraint,
3654 struct weston_view *view)
3655{
3656 assert(constraint->view == NULL);
3657 constraint->view = view;
3658 pointer_constraint_notify_activated(constraint);
3659 weston_pointer_start_grab(constraint->pointer, &constraint->grab);
3660 wl_list_remove(&constraint->surface_destroy_listener.link);
3661 wl_list_init(&constraint->surface_destroy_listener.link);
3662}
3663
3664static bool
3665is_pointer_constraint_enabled(struct weston_pointer_constraint *constraint)
3666{
3667 return constraint->view != NULL;
3668}
3669
3670static void
3671weston_pointer_constraint_disable(struct weston_pointer_constraint *constraint)
3672{
3673 constraint->view = NULL;
3674 pointer_constraint_notify_deactivated(constraint);
3675 weston_pointer_end_grab(constraint->grab.pointer);
3676}
3677
3678void
3679weston_pointer_constraint_destroy(struct weston_pointer_constraint *constraint)
3680{
3681 if (is_pointer_constraint_enabled(constraint))
3682 weston_pointer_constraint_disable(constraint);
3683
3684 wl_list_remove(&constraint->pointer_destroy_listener.link);
3685 wl_list_remove(&constraint->surface_destroy_listener.link);
3686 wl_list_remove(&constraint->surface_commit_listener.link);
3687 wl_list_remove(&constraint->surface_activate_listener.link);
3688
3689 wl_resource_set_user_data(constraint->resource, NULL);
3690 pixman_region32_fini(&constraint->region);
3691 wl_list_remove(&constraint->link);
3692 free(constraint);
3693}
3694
3695static void
3696disable_pointer_constraint(struct weston_pointer_constraint *constraint)
3697{
3698 switch (constraint->lifetime) {
3699 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT:
3700 weston_pointer_constraint_destroy(constraint);
3701 break;
3702 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT:
3703 weston_pointer_constraint_disable(constraint);
3704 break;
3705 }
3706}
3707
3708static bool
3709is_within_constraint_region(struct weston_pointer_constraint *constraint,
3710 wl_fixed_t sx, wl_fixed_t sy)
3711{
3712 struct weston_surface *surface = constraint->surface;
3713 pixman_region32_t constraint_region;
3714 bool result;
3715
3716 pixman_region32_init(&constraint_region);
3717 pixman_region32_intersect(&constraint_region,
3718 &surface->input,
3719 &constraint->region);
3720 result = pixman_region32_contains_point(&constraint_region,
3721 wl_fixed_to_int(sx),
3722 wl_fixed_to_int(sy),
3723 NULL);
3724 pixman_region32_fini(&constraint_region);
3725
3726 return result;
3727}
3728
3729static void
3730maybe_enable_pointer_constraint(struct weston_pointer_constraint *constraint)
3731{
3732 struct weston_surface *surface = constraint->surface;
3733 struct weston_view *vit;
3734 struct weston_view *view = NULL;
3735 struct weston_pointer *pointer = constraint->pointer;
3736 struct weston_keyboard *keyboard;
3737 struct weston_seat *seat = pointer->seat;
3738 int32_t x, y;
3739
3740 /* Postpone if no view of the surface was most recently clicked. */
3741 wl_list_for_each(vit, &surface->views, surface_link) {
3742 if (vit->click_to_activate_serial ==
3743 surface->compositor->activate_serial) {
3744 view = vit;
3745 }
3746 }
3747 if (view == NULL)
3748 return;
3749
3750 /* Postpone if surface doesn't have keyboard focus. */
3751 keyboard = weston_seat_get_keyboard(seat);
3752 if (!keyboard || keyboard->focus != surface)
3753 return;
3754
3755 /* Postpone constraint if the pointer is not within the
3756 * constraint region.
3757 */
3758 weston_view_from_global(view,
3759 wl_fixed_to_int(pointer->x),
3760 wl_fixed_to_int(pointer->y),
3761 &x, &y);
3762 if (!is_within_constraint_region(constraint,
3763 wl_fixed_from_int(x),
3764 wl_fixed_from_int(y)))
3765 return;
3766
3767 enable_pointer_constraint(constraint, view);
3768}
3769
3770static void
3771locked_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
3772{
3773}
3774
3775static void
3776locked_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02003777 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003778 struct weston_pointer_motion_event *event)
3779{
Quentin Glidiccde13452016-08-12 10:41:32 +02003780 pointer_send_relative_motion(grab->pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08003781}
3782
3783static void
3784locked_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003785 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003786 uint32_t button,
3787 uint32_t state_w)
3788{
3789 weston_pointer_send_button(grab->pointer, time, button, state_w);
3790}
3791
3792static void
3793locked_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02003794 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003795 struct weston_pointer_axis_event *event)
3796{
3797 weston_pointer_send_axis(grab->pointer, time, event);
3798}
3799
3800static void
3801locked_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
3802 uint32_t source)
3803{
3804 weston_pointer_send_axis_source(grab->pointer, source);
3805}
3806
3807static void
3808locked_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
3809{
3810 weston_pointer_send_frame(grab->pointer);
3811}
3812
3813static void
3814locked_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
3815{
3816 struct weston_pointer_constraint *constraint =
3817 container_of(grab, struct weston_pointer_constraint, grab);
3818
3819 disable_pointer_constraint(constraint);
3820}
3821
3822static const struct weston_pointer_grab_interface
3823 locked_pointer_grab_interface = {
3824 locked_pointer_grab_pointer_focus,
3825 locked_pointer_grab_pointer_motion,
3826 locked_pointer_grab_pointer_button,
3827 locked_pointer_grab_pointer_axis,
3828 locked_pointer_grab_pointer_axis_source,
3829 locked_pointer_grab_pointer_frame,
3830 locked_pointer_grab_pointer_cancel,
3831};
3832
3833static void
3834pointer_constraint_constrain_resource_destroyed(struct wl_resource *resource)
3835{
3836 struct weston_pointer_constraint *constraint =
3837 wl_resource_get_user_data(resource);
3838
3839 if (!constraint)
3840 return;
3841
3842 weston_pointer_constraint_destroy(constraint);
3843}
3844
3845static void
3846pointer_constraint_surface_activate(struct wl_listener *listener, void *data)
3847{
3848 struct weston_surface_activation_data *activation = data;
3849 struct weston_pointer *pointer;
3850 struct weston_surface *focus = activation->surface;
3851 struct weston_pointer_constraint *constraint =
3852 container_of(listener, struct weston_pointer_constraint,
3853 surface_activate_listener);
3854 bool is_constraint_surface;
3855
3856 pointer = weston_seat_get_pointer(activation->seat);
3857 if (!pointer)
3858 return;
3859
3860 is_constraint_surface =
3861 get_pointer_constraint_for_pointer(focus, pointer) == constraint;
3862
3863 if (is_constraint_surface &&
3864 !is_pointer_constraint_enabled(constraint))
3865 maybe_enable_pointer_constraint(constraint);
3866 else if (!is_constraint_surface &&
3867 is_pointer_constraint_enabled(constraint))
3868 disable_pointer_constraint(constraint);
3869}
3870
3871static void
3872pointer_constraint_pointer_destroyed(struct wl_listener *listener, void *data)
3873{
3874 struct weston_pointer_constraint *constraint =
3875 container_of(listener, struct weston_pointer_constraint,
3876 pointer_destroy_listener);
3877
3878 weston_pointer_constraint_destroy(constraint);
3879}
3880
3881static void
3882pointer_constraint_surface_destroyed(struct wl_listener *listener, void *data)
3883{
3884 struct weston_pointer_constraint *constraint =
3885 container_of(listener, struct weston_pointer_constraint,
3886 surface_destroy_listener);
3887
3888 weston_pointer_constraint_destroy(constraint);
3889}
3890
3891static void
3892pointer_constraint_surface_committed(struct wl_listener *listener, void *data)
3893{
3894 struct weston_pointer_constraint *constraint =
3895 container_of(listener, struct weston_pointer_constraint,
3896 surface_commit_listener);
3897
3898 if (constraint->region_is_pending) {
3899 constraint->region_is_pending = false;
3900 pixman_region32_copy(&constraint->region,
3901 &constraint->region_pending);
3902 pixman_region32_fini(&constraint->region_pending);
3903 pixman_region32_init(&constraint->region_pending);
3904 }
3905
3906 if (constraint->hint_is_pending) {
3907 constraint->hint_is_pending = false;
3908
3909 constraint->hint_is_pending = true;
3910 constraint->hint_x = constraint->hint_x_pending;
3911 constraint->hint_y = constraint->hint_y_pending;
3912 }
3913
3914 if (pointer_constraint_get_type(constraint) ==
3915 POINTER_CONSTRAINT_TYPE_CONFINE &&
3916 is_pointer_constraint_enabled(constraint))
3917 maybe_warp_confined_pointer(constraint);
3918}
3919
3920static struct weston_pointer_constraint *
3921weston_pointer_constraint_create(struct weston_surface *surface,
3922 struct weston_pointer *pointer,
3923 struct weston_region *region,
3924 enum zwp_pointer_constraints_v1_lifetime lifetime,
3925 struct wl_resource *cr,
3926 const struct weston_pointer_grab_interface *grab_interface)
3927{
3928 struct weston_pointer_constraint *constraint;
3929
3930 constraint = zalloc(sizeof *constraint);
3931 if (!constraint)
3932 return NULL;
3933
3934 constraint->lifetime = lifetime;
3935 pixman_region32_init(&constraint->region);
3936 pixman_region32_init(&constraint->region_pending);
3937 wl_list_insert(&surface->pointer_constraints, &constraint->link);
3938 constraint->surface = surface;
3939 constraint->pointer = pointer;
3940 constraint->resource = cr;
3941 constraint->grab.interface = grab_interface;
3942 if (region) {
3943 pixman_region32_copy(&constraint->region,
3944 &region->region);
3945 } else {
3946 pixman_region32_fini(&constraint->region);
3947 region_init_infinite(&constraint->region);
3948 }
3949
3950 constraint->surface_activate_listener.notify =
3951 pointer_constraint_surface_activate;
3952 constraint->surface_destroy_listener.notify =
3953 pointer_constraint_surface_destroyed;
3954 constraint->surface_commit_listener.notify =
3955 pointer_constraint_surface_committed;
3956 constraint->pointer_destroy_listener.notify =
3957 pointer_constraint_pointer_destroyed;
3958
3959 wl_signal_add(&surface->compositor->activate_signal,
3960 &constraint->surface_activate_listener);
3961 wl_signal_add(&pointer->destroy_signal,
3962 &constraint->pointer_destroy_listener);
3963 wl_signal_add(&surface->destroy_signal,
3964 &constraint->surface_destroy_listener);
3965 wl_signal_add(&surface->commit_signal,
3966 &constraint->surface_commit_listener);
3967
3968 return constraint;
3969}
3970
3971static void
3972init_pointer_constraint(struct wl_resource *pointer_constraints_resource,
3973 uint32_t id,
3974 struct weston_surface *surface,
3975 struct weston_pointer *pointer,
3976 struct weston_region *region,
3977 enum zwp_pointer_constraints_v1_lifetime lifetime,
3978 const struct wl_interface *interface,
3979 const void *implementation,
3980 const struct weston_pointer_grab_interface *grab_interface)
3981{
3982 struct wl_client *client =
3983 wl_resource_get_client(pointer_constraints_resource);
3984 struct wl_resource *cr;
3985 struct weston_pointer_constraint *constraint;
3986
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02003987 if (pointer && get_pointer_constraint_for_pointer(surface, pointer)) {
Jonas Ådahld3414f22016-07-22 17:56:31 +08003988 wl_resource_post_error(pointer_constraints_resource,
3989 ZWP_POINTER_CONSTRAINTS_V1_ERROR_ALREADY_CONSTRAINED,
3990 "the pointer has a lock/confine request on this surface");
3991 return;
3992 }
3993
3994 cr = wl_resource_create(client, interface,
3995 wl_resource_get_version(pointer_constraints_resource),
3996 id);
3997 if (cr == NULL) {
3998 wl_client_post_no_memory(client);
3999 return;
4000 }
4001
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02004002 if (pointer) {
4003 constraint = weston_pointer_constraint_create(surface, pointer,
4004 region, lifetime,
4005 cr, grab_interface);
4006 if (constraint == NULL) {
4007 wl_client_post_no_memory(client);
4008 return;
4009 }
4010 } else {
4011 constraint = NULL;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004012 }
4013
4014 wl_resource_set_implementation(cr, implementation, constraint,
4015 pointer_constraint_constrain_resource_destroyed);
4016
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02004017 if (constraint)
4018 maybe_enable_pointer_constraint(constraint);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004019}
4020
4021static void
4022pointer_constraints_destroy(struct wl_client *client,
4023 struct wl_resource *resource)
4024{
4025 wl_resource_destroy(resource);
4026}
4027
4028static void
4029locked_pointer_destroy(struct wl_client *client,
4030 struct wl_resource *resource)
4031{
4032 struct weston_pointer_constraint *constraint =
4033 wl_resource_get_user_data(resource);
4034 wl_fixed_t x, y;
4035
4036 if (constraint && constraint->view && constraint->hint_is_pending &&
4037 is_within_constraint_region(constraint,
4038 constraint->hint_x,
4039 constraint->hint_y)) {
4040 weston_view_to_global_fixed(constraint->view,
4041 constraint->hint_x,
4042 constraint->hint_y,
4043 &x, &y);
4044 weston_pointer_move_to(constraint->pointer, x, y);
4045 }
4046 wl_resource_destroy(resource);
4047}
4048
4049static void
4050locked_pointer_set_cursor_position_hint(struct wl_client *client,
4051 struct wl_resource *resource,
4052 wl_fixed_t surface_x,
4053 wl_fixed_t surface_y)
4054{
4055 struct weston_pointer_constraint *constraint =
4056 wl_resource_get_user_data(resource);
4057
4058 /* Ignore a set cursor hint that was sent after the lock was cancelled.
4059 */
4060 if (!constraint ||
4061 !constraint->resource ||
4062 constraint->resource != resource)
4063 return;
4064
4065 constraint->hint_is_pending = true;
4066 constraint->hint_x_pending = surface_x;
4067 constraint->hint_y_pending = surface_y;
4068}
4069
4070static void
4071locked_pointer_set_region(struct wl_client *client,
4072 struct wl_resource *resource,
4073 struct wl_resource *region_resource)
4074{
4075 struct weston_pointer_constraint *constraint =
4076 wl_resource_get_user_data(resource);
4077 struct weston_region *region = region_resource ?
4078 wl_resource_get_user_data(region_resource) : NULL;
4079
4080 if (!constraint)
4081 return;
4082
4083 if (region) {
4084 pixman_region32_copy(&constraint->region_pending,
4085 &region->region);
4086 } else {
4087 pixman_region32_fini(&constraint->region_pending);
4088 region_init_infinite(&constraint->region_pending);
4089 }
4090 constraint->region_is_pending = true;
4091}
4092
4093
4094static const struct zwp_locked_pointer_v1_interface locked_pointer_interface = {
4095 locked_pointer_destroy,
4096 locked_pointer_set_cursor_position_hint,
4097 locked_pointer_set_region,
4098};
4099
4100static void
4101pointer_constraints_lock_pointer(struct wl_client *client,
4102 struct wl_resource *resource,
4103 uint32_t id,
4104 struct wl_resource *surface_resource,
4105 struct wl_resource *pointer_resource,
4106 struct wl_resource *region_resource,
4107 uint32_t lifetime)
4108{
4109 struct weston_surface *surface =
4110 wl_resource_get_user_data(surface_resource);
4111 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
4112 struct weston_region *region = region_resource ?
4113 wl_resource_get_user_data(region_resource) : NULL;
4114
4115 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
4116 &zwp_locked_pointer_v1_interface,
4117 &locked_pointer_interface,
4118 &locked_pointer_grab_interface);
4119}
4120
4121static void
4122confined_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
4123{
4124}
4125
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004126static double
4127vec2d_cross_product(struct vec2d a, struct vec2d b)
4128{
4129 return a.x * b.y - a.y * b.x;
4130}
4131
4132static struct vec2d
4133vec2d_add(struct vec2d a, struct vec2d b)
4134{
4135 return (struct vec2d) {
4136 .x = a.x + b.x,
4137 .y = a.y + b.y,
4138 };
4139}
4140
4141static struct vec2d
4142vec2d_subtract(struct vec2d a, struct vec2d b)
4143{
4144 return (struct vec2d) {
4145 .x = a.x - b.x,
4146 .y = a.y - b.y,
4147 };
4148}
4149
4150static struct vec2d
4151vec2d_multiply_constant(double c, struct vec2d a)
4152{
4153 return (struct vec2d) {
4154 .x = c * a.x,
4155 .y = c * a.y,
4156 };
4157}
4158
4159static bool
4160lines_intersect(struct line *line1, struct line *line2,
4161 struct vec2d *intersection)
4162{
4163 struct vec2d p = line1->a;
4164 struct vec2d r = vec2d_subtract(line1->b, line1->a);
4165 struct vec2d q = line2->a;
4166 struct vec2d s = vec2d_subtract(line2->b, line2->a);
4167 double rxs;
4168 double sxr;
4169 double t;
4170 double u;
4171
4172 /*
4173 * The line (p, r) and (q, s) intersects where
4174 *
4175 * p + t r = q + u s
4176 *
4177 * Calculate t:
4178 *
4179 * (p + t r) × s = (q + u s) × s
4180 * p × s + t (r × s) = q × s + u (s × s)
4181 * p × s + t (r × s) = q × s
4182 * t (r × s) = q × s - p × s
4183 * t (r × s) = (q - p) × s
4184 * t = ((q - p) × s) / (r × s)
4185 *
4186 * Using the same method, for u we get:
4187 *
4188 * u = ((p - q) × r) / (s × r)
4189 */
4190
4191 rxs = vec2d_cross_product(r, s);
4192 sxr = vec2d_cross_product(s, r);
4193
4194 /* If r × s = 0 then the lines are either parallel or collinear. */
4195 if (fabs(rxs) < DBL_MIN)
4196 return false;
4197
4198 t = vec2d_cross_product(vec2d_subtract(q, p), s) / rxs;
4199 u = vec2d_cross_product(vec2d_subtract(p, q), r) / sxr;
4200
4201 /* The lines only intersect if 0 ≤ t ≤ 1 and 0 ≤ u ≤ 1. */
4202 if (t < 0.0 || t > 1.0 || u < 0.0 || u > 1.0)
4203 return false;
4204
4205 *intersection = vec2d_add(p, vec2d_multiply_constant(t, r));
4206 return true;
4207}
4208
4209static struct border *
4210add_border(struct wl_array *array,
4211 double x1, double y1,
4212 double x2, double y2,
4213 enum motion_direction blocking_dir)
4214{
4215 struct border *border = wl_array_add(array, sizeof *border);
4216
4217 *border = (struct border) {
4218 .line = (struct line) {
4219 .a = (struct vec2d) {
4220 .x = x1,
4221 .y = y1,
4222 },
4223 .b = (struct vec2d) {
4224 .x = x2,
4225 .y = y2,
4226 },
4227 },
4228 .blocking_dir = blocking_dir,
4229 };
4230
4231 return border;
4232}
4233
4234static int
4235compare_lines_x(const void *a, const void *b)
4236{
4237 const struct border *border_a = a;
4238 const struct border *border_b = b;
4239
4240
4241 if (border_a->line.a.x == border_b->line.a.x)
4242 return border_a->line.b.x < border_b->line.b.x;
4243 else
4244 return border_a->line.a.x > border_b->line.a.x;
4245}
4246
4247static void
4248add_non_overlapping_edges(pixman_box32_t *boxes,
4249 int band_above_start,
4250 int band_below_start,
4251 int band_below_end,
4252 struct wl_array *borders)
4253{
4254 int i;
4255 struct wl_array band_merge;
4256 struct border *border;
4257 struct border *prev_border;
4258 struct border *new_border;
4259
4260 wl_array_init(&band_merge);
4261
4262 /* Add bottom band of previous row, and top band of current row, and
4263 * sort them so lower left x coordinate comes first. If there are two
4264 * borders with the same left x coordinate, the wider one comes first.
4265 */
4266 for (i = band_above_start; i < band_below_start; i++) {
4267 pixman_box32_t *box = &boxes[i];
4268 add_border(&band_merge, box->x1, box->y2, box->x2, box->y2,
4269 MOTION_DIRECTION_POSITIVE_Y);
4270 }
4271 for (i = band_below_start; i < band_below_end; i++) {
4272 pixman_box32_t *box= &boxes[i];
4273 add_border(&band_merge, box->x1, box->y1, box->x2, box->y1,
4274 MOTION_DIRECTION_NEGATIVE_Y);
4275 }
4276 qsort(band_merge.data,
4277 band_merge.size / sizeof *border,
4278 sizeof *border,
4279 compare_lines_x);
4280
4281 /* Combine the two combined bands so that any overlapping border is
4282 * eliminated. */
4283 prev_border = NULL;
4284 wl_array_for_each(border, &band_merge) {
4285 assert(border->line.a.y == border->line.b.y);
4286 assert(!prev_border ||
4287 prev_border->line.a.y == border->line.a.y);
4288 assert(!prev_border ||
4289 (prev_border->line.a.x != border->line.a.x ||
4290 prev_border->line.b.x != border->line.b.x));
4291 assert(!prev_border ||
4292 prev_border->line.a.x <= border->line.a.x);
4293
4294 if (prev_border &&
4295 prev_border->line.a.x == border->line.a.x) {
4296 /*
4297 * ------------ +
4298 * ------- =
4299 * [ ]-----
4300 */
4301 prev_border->line.a.x = border->line.b.x;
4302 } else if (prev_border &&
4303 prev_border->line.b.x == border->line.b.x) {
4304 /*
4305 * ------------ +
4306 * ------ =
4307 * ------[ ]
4308 */
4309 prev_border->line.b.x = border->line.a.x;
4310 } else if (prev_border &&
4311 prev_border->line.b.x == border->line.a.x) {
4312 /*
4313 * -------- +
4314 * ------ =
4315 * --------------
4316 */
4317 prev_border->line.b.x = border->line.b.x;
4318 } else if (prev_border &&
4319 prev_border->line.b.x >= border->line.a.x) {
4320 /*
4321 * --------------- +
4322 * ------ =
4323 * -----[ ]----
4324 */
4325 new_border = add_border(borders,
4326 border->line.b.x,
4327 border->line.b.y,
4328 prev_border->line.b.x,
4329 prev_border->line.b.y,
4330 prev_border->blocking_dir);
4331 prev_border->line.b.x = border->line.a.x;
4332 prev_border = new_border;
4333 } else {
4334 assert(!prev_border ||
4335 prev_border->line.b.x < border->line.a.x);
4336 /*
4337 * First border or non-overlapping.
4338 *
4339 * ----- +
4340 * ----- =
4341 * ----- -----
4342 */
4343 new_border = wl_array_add(borders, sizeof *border);
4344 *new_border = *border;
4345 prev_border = new_border;
4346 }
4347 }
4348
4349 wl_array_release(&band_merge);
4350}
4351
4352static void
4353add_band_bottom_edges(pixman_box32_t *boxes,
4354 int band_start,
4355 int band_end,
4356 struct wl_array *borders)
4357{
4358 int i;
4359
4360 for (i = band_start; i < band_end; i++) {
4361 add_border(borders,
4362 boxes[i].x1, boxes[i].y2,
4363 boxes[i].x2, boxes[i].y2,
4364 MOTION_DIRECTION_POSITIVE_Y);
4365 }
4366}
4367
4368static void
4369region_to_outline(pixman_region32_t *region, struct wl_array *borders)
4370{
4371 pixman_box32_t *boxes;
4372 int num_boxes;
4373 int i;
4374 int top_most, bottom_most;
4375 int current_roof;
4376 int prev_top;
4377 int band_start, prev_band_start;
4378
4379 /*
4380 * Remove any overlapping lines from the set of rectangles. Note that
4381 * pixman regions are grouped as rows of rectangles, where rectangles
4382 * in one row never touch or overlap and are all of the same height.
4383 *
4384 * -------- --- -------- ---
4385 * | | | | | | | |
4386 * ----------====---- --- ----------- ----- ---
4387 * | | => | |
4388 * ----==========--------- ----- ----------
4389 * | | | |
4390 * ------------------- -------------------
4391 *
4392 */
4393
4394 boxes = pixman_region32_rectangles(region, &num_boxes);
4395 prev_top = 0;
4396 top_most = boxes[0].y1;
4397 current_roof = top_most;
4398 bottom_most = boxes[num_boxes - 1].y2;
4399 band_start = 0;
4400 prev_band_start = 0;
4401 for (i = 0; i < num_boxes; i++) {
4402 /* Detect if there is a vertical empty space, and add the lower
4403 * level of the previous band if so was the case. */
4404 if (i > 0 &&
4405 boxes[i].y1 != prev_top &&
4406 boxes[i].y1 != boxes[i - 1].y2) {
4407 current_roof = boxes[i].y1;
4408 add_band_bottom_edges(boxes,
4409 band_start,
4410 i,
4411 borders);
4412 }
4413
4414 /* Special case adding the last band, since it won't be handled
4415 * by the band change detection below. */
4416 if (boxes[i].y1 != current_roof && i == num_boxes - 1) {
4417 if (boxes[i].y1 != prev_top) {
4418 /* The last band is a single box, so we don't
4419 * have a prev_band_start to tell us when the
4420 * previous band started. */
4421 add_non_overlapping_edges(boxes,
4422 band_start,
4423 i,
4424 i + 1,
4425 borders);
4426 } else {
4427 add_non_overlapping_edges(boxes,
4428 prev_band_start,
4429 band_start,
4430 i + 1,
4431 borders);
4432 }
4433 }
4434
4435 /* Detect when passing a band and combine the top border of the
4436 * just passed band with the bottom band of the previous band.
4437 */
4438 if (boxes[i].y1 != top_most && boxes[i].y1 != prev_top) {
4439 /* Combine the two passed bands. */
4440 if (prev_top != current_roof) {
4441 add_non_overlapping_edges(boxes,
4442 prev_band_start,
4443 band_start,
4444 i,
4445 borders);
4446 }
4447
4448 prev_band_start = band_start;
4449 band_start = i;
4450 }
4451
4452 /* Add the top border if the box is part of the current roof. */
4453 if (boxes[i].y1 == current_roof) {
4454 add_border(borders,
4455 boxes[i].x1, boxes[i].y1,
4456 boxes[i].x2, boxes[i].y1,
4457 MOTION_DIRECTION_NEGATIVE_Y);
4458 }
4459
4460 /* Add the bottom border of the last band. */
4461 if (boxes[i].y2 == bottom_most) {
4462 add_border(borders,
4463 boxes[i].x1, boxes[i].y2,
4464 boxes[i].x2, boxes[i].y2,
4465 MOTION_DIRECTION_POSITIVE_Y);
4466 }
4467
4468 /* Always add the left border. */
4469 add_border(borders,
4470 boxes[i].x1, boxes[i].y1,
4471 boxes[i].x1, boxes[i].y2,
4472 MOTION_DIRECTION_NEGATIVE_X);
4473
4474 /* Always add the right border. */
4475 add_border(borders,
4476 boxes[i].x2, boxes[i].y1,
4477 boxes[i].x2, boxes[i].y2,
4478 MOTION_DIRECTION_POSITIVE_X);
4479
4480 prev_top = boxes[i].y1;
4481 }
4482}
4483
4484static bool
4485is_border_horizontal (struct border *border)
4486{
4487 return border->line.a.y == border->line.b.y;
4488}
4489
4490static bool
4491is_border_blocking_directions(struct border *border,
4492 uint32_t directions)
4493{
4494 /* Don't block parallel motions. */
4495 if (is_border_horizontal(border)) {
4496 if ((directions & (MOTION_DIRECTION_POSITIVE_Y |
4497 MOTION_DIRECTION_NEGATIVE_Y)) == 0)
4498 return false;
4499 } else {
4500 if ((directions & (MOTION_DIRECTION_POSITIVE_X |
4501 MOTION_DIRECTION_NEGATIVE_X)) == 0)
4502 return false;
4503 }
4504
4505 return (~border->blocking_dir & directions) != directions;
4506}
4507
4508static struct border *
4509get_closest_border(struct wl_array *borders,
4510 struct line *motion,
4511 uint32_t directions)
4512{
4513 struct border *border;
4514 struct vec2d intersection;
4515 struct vec2d delta;
4516 double distance_2;
4517 struct border *closest_border = NULL;
4518 double closest_distance_2 = DBL_MAX;
4519
4520 wl_array_for_each(border, borders) {
4521 if (!is_border_blocking_directions(border, directions))
4522 continue;
4523
4524 if (!lines_intersect(&border->line, motion, &intersection))
4525 continue;
4526
4527 delta = vec2d_subtract(intersection, motion->a);
4528 distance_2 = delta.x*delta.x + delta.y*delta.y;
4529 if (distance_2 < closest_distance_2) {
4530 closest_border = border;
4531 closest_distance_2 = distance_2;
4532 }
4533 }
4534
4535 return closest_border;
4536}
4537
4538static void
4539clamp_to_border(struct border *border,
4540 struct line *motion,
4541 uint32_t *motion_dir)
4542{
4543 /*
4544 * When clamping either rightward or downward motions, the motion needs
4545 * to be clamped so that the destination coordinate does not end up on
4546 * the border (see weston_pointer_clamp_event_to_region). Do this by
4547 * clamping such motions to the border minus the smallest possible
4548 * wl_fixed_t value.
4549 */
4550 if (is_border_horizontal(border)) {
4551 if (*motion_dir & MOTION_DIRECTION_POSITIVE_Y)
4552 motion->b.y = border->line.a.y - wl_fixed_to_double(1);
4553 else
4554 motion->b.y = border->line.a.y;
4555 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_Y |
4556 MOTION_DIRECTION_NEGATIVE_Y);
4557 } else {
4558 if (*motion_dir & MOTION_DIRECTION_POSITIVE_X)
4559 motion->b.x = border->line.a.x - wl_fixed_to_double(1);
4560 else
4561 motion->b.x = border->line.a.x;
4562 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_X |
4563 MOTION_DIRECTION_NEGATIVE_X);
4564 }
4565}
4566
4567static uint32_t
4568get_motion_directions(struct line *motion)
4569{
4570 uint32_t directions = 0;
4571
4572 if (motion->a.x < motion->b.x)
4573 directions |= MOTION_DIRECTION_POSITIVE_X;
4574 else if (motion->a.x > motion->b.x)
4575 directions |= MOTION_DIRECTION_NEGATIVE_X;
4576 if (motion->a.y < motion->b.y)
4577 directions |= MOTION_DIRECTION_POSITIVE_Y;
4578 else if (motion->a.y > motion->b.y)
4579 directions |= MOTION_DIRECTION_NEGATIVE_Y;
4580
4581 return directions;
4582}
4583
Jonas Ådahld3414f22016-07-22 17:56:31 +08004584static void
4585weston_pointer_clamp_event_to_region(struct weston_pointer *pointer,
4586 struct weston_pointer_motion_event *event,
4587 pixman_region32_t *region,
4588 wl_fixed_t *clamped_x,
4589 wl_fixed_t *clamped_y)
4590{
4591 wl_fixed_t x, y;
4592 wl_fixed_t sx, sy;
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004593 wl_fixed_t old_sx = pointer->sx;
4594 wl_fixed_t old_sy = pointer->sy;
4595 struct wl_array borders;
4596 struct line motion;
4597 struct border *closest_border;
4598 float new_x_f, new_y_f;
4599 uint32_t directions;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004600
4601 weston_pointer_motion_to_abs(pointer, event, &x, &y);
4602 weston_view_from_global_fixed(pointer->focus, x, y, &sx, &sy);
4603
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004604 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004605
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004606 /*
4607 * Generate borders given the confine region we are to use. The borders
4608 * are defined to be the outer region of the allowed area. This means
4609 * top/left borders are "within" the allowed area, while bottom/right
4610 * borders are outside. This needs to be considered when clamping
4611 * confined motion vectors.
4612 */
4613 region_to_outline(region, &borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004614
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004615 motion = (struct line) {
4616 .a = (struct vec2d) {
4617 .x = wl_fixed_to_double(old_sx),
4618 .y = wl_fixed_to_double(old_sy),
4619 },
4620 .b = (struct vec2d) {
4621 .x = wl_fixed_to_double(sx),
4622 .y = wl_fixed_to_double(sy),
4623 },
4624 };
4625 directions = get_motion_directions(&motion);
4626
4627 while (directions) {
4628 closest_border = get_closest_border(&borders,
4629 &motion,
4630 directions);
4631 if (closest_border)
4632 clamp_to_border(closest_border, &motion, &directions);
4633 else
4634 break;
4635 }
4636
4637 weston_view_to_global_float(pointer->focus,
4638 (float) motion.b.x, (float) motion.b.y,
4639 &new_x_f, &new_y_f);
4640 *clamped_x = wl_fixed_from_double(new_x_f);
4641 *clamped_y = wl_fixed_from_double(new_y_f);
4642
4643 wl_array_release(&borders);
4644}
4645
4646static double
4647point_to_border_distance_2(struct border *border, double x, double y)
4648{
4649 double orig_x, orig_y;
4650 double dx, dy;
4651
4652 if (is_border_horizontal(border)) {
4653 if (x < border->line.a.x)
4654 orig_x = border->line.a.x;
4655 else if (x > border->line.b.x)
4656 orig_x = border->line.b.x;
4657 else
4658 orig_x = x;
4659 orig_y = border->line.a.y;
4660 } else {
4661 if (y < border->line.a.y)
4662 orig_y = border->line.a.y;
4663 else if (y > border->line.b.y)
4664 orig_y = border->line.b.y;
4665 else
4666 orig_y = y;
4667 orig_x = border->line.a.x;
4668 }
4669
4670
4671 dx = fabs(orig_x - x);
4672 dy = fabs(orig_y - y);
4673 return dx*dx + dy*dy;
4674}
4675
4676static void
4677warp_to_behind_border(struct border *border, wl_fixed_t *sx, wl_fixed_t *sy)
4678{
4679 switch (border->blocking_dir) {
4680 case MOTION_DIRECTION_POSITIVE_X:
4681 case MOTION_DIRECTION_NEGATIVE_X:
4682 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_X)
4683 *sx = wl_fixed_from_double(border->line.a.x) - 1;
4684 else
4685 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4686 if (*sy < wl_fixed_from_double(border->line.a.y))
4687 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4688 else if (*sy > wl_fixed_from_double(border->line.b.y))
4689 *sy = wl_fixed_from_double(border->line.b.y) - 1;
4690 break;
4691 case MOTION_DIRECTION_POSITIVE_Y:
4692 case MOTION_DIRECTION_NEGATIVE_Y:
4693 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_Y)
4694 *sy = wl_fixed_from_double(border->line.a.y) - 1;
4695 else
4696 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4697 if (*sx < wl_fixed_from_double(border->line.a.x))
4698 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4699 else if (*sx > wl_fixed_from_double(border->line.b.x))
4700 *sx = wl_fixed_from_double(border->line.b.x) - 1;
4701 break;
4702 }
Jonas Ådahld3414f22016-07-22 17:56:31 +08004703}
4704
4705static void
4706maybe_warp_confined_pointer(struct weston_pointer_constraint *constraint)
4707{
4708 wl_fixed_t x;
4709 wl_fixed_t y;
4710 wl_fixed_t sx;
4711 wl_fixed_t sy;
4712
4713 weston_view_from_global_fixed(constraint->view,
4714 constraint->pointer->x,
4715 constraint->pointer->y,
4716 &sx,
4717 &sy);
4718
4719 if (!is_within_constraint_region(constraint, sx, sy)) {
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004720 double xf = wl_fixed_to_double(sx);
4721 double yf = wl_fixed_to_double(sy);
4722 pixman_region32_t confine_region;
4723 struct wl_array borders;
4724 struct border *border;
4725 double closest_distance_2 = DBL_MAX;
4726 struct border *closest_border = NULL;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004727
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004728 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004729
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004730 pixman_region32_init(&confine_region);
4731 pixman_region32_intersect(&confine_region,
4732 &constraint->view->surface->input,
4733 &constraint->region);
4734 region_to_outline(&confine_region, &borders);
4735 pixman_region32_fini(&confine_region);
4736
4737 wl_array_for_each(border, &borders) {
4738 double distance_2;
4739
4740 distance_2 = point_to_border_distance_2(border, xf, yf);
4741 if (distance_2 < closest_distance_2) {
4742 closest_border = border;
4743 closest_distance_2 = distance_2;
4744 }
4745 }
4746 assert(closest_border);
4747
4748 warp_to_behind_border(closest_border, &sx, &sy);
4749
4750 wl_array_release(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004751
4752 weston_view_to_global_fixed(constraint->view, sx, sy, &x, &y);
4753 weston_pointer_move_to(constraint->pointer, x, y);
4754 }
4755}
4756
4757static void
4758confined_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02004759 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004760 struct weston_pointer_motion_event *event)
4761{
4762 struct weston_pointer_constraint *constraint =
4763 container_of(grab, struct weston_pointer_constraint, grab);
4764 struct weston_pointer *pointer = grab->pointer;
4765 struct weston_surface *surface;
4766 wl_fixed_t x, y;
4767 wl_fixed_t old_sx = pointer->sx;
4768 wl_fixed_t old_sy = pointer->sy;
4769 pixman_region32_t confine_region;
4770
4771 assert(pointer->focus);
4772 assert(pointer->focus->surface == constraint->surface);
4773
4774 surface = pointer->focus->surface;
4775
4776 pixman_region32_init(&confine_region);
4777 pixman_region32_intersect(&confine_region,
4778 &surface->input,
4779 &constraint->region);
4780 weston_pointer_clamp_event_to_region(pointer, event,
4781 &confine_region, &x, &y);
4782 weston_pointer_move_to(pointer, x, y);
4783 pixman_region32_fini(&confine_region);
4784
4785 weston_view_from_global_fixed(pointer->focus, x, y,
4786 &pointer->sx, &pointer->sy);
4787
4788 if (old_sx != pointer->sx || old_sy != pointer->sy) {
Quentin Glidiccde13452016-08-12 10:41:32 +02004789 pointer_send_motion(pointer, time,
4790 pointer->sx, pointer->sy);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004791 }
4792
Quentin Glidiccde13452016-08-12 10:41:32 +02004793 pointer_send_relative_motion(pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004794}
4795
4796static void
4797confined_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02004798 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004799 uint32_t button,
4800 uint32_t state_w)
4801{
4802 weston_pointer_send_button(grab->pointer, time, button, state_w);
4803}
4804
4805static void
4806confined_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02004807 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004808 struct weston_pointer_axis_event *event)
4809{
4810 weston_pointer_send_axis(grab->pointer, time, event);
4811}
4812
4813static void
4814confined_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
4815 uint32_t source)
4816{
4817 weston_pointer_send_axis_source(grab->pointer, source);
4818}
4819
4820static void
4821confined_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
4822{
4823 weston_pointer_send_frame(grab->pointer);
4824}
4825
4826static void
4827confined_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
4828{
4829 struct weston_pointer_constraint *constraint =
4830 container_of(grab, struct weston_pointer_constraint, grab);
4831
4832 disable_pointer_constraint(constraint);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004833}
4834
4835static const struct weston_pointer_grab_interface
4836 confined_pointer_grab_interface = {
4837 confined_pointer_grab_pointer_focus,
4838 confined_pointer_grab_pointer_motion,
4839 confined_pointer_grab_pointer_button,
4840 confined_pointer_grab_pointer_axis,
4841 confined_pointer_grab_pointer_axis_source,
4842 confined_pointer_grab_pointer_frame,
4843 confined_pointer_grab_pointer_cancel,
4844};
4845
4846static void
4847confined_pointer_destroy(struct wl_client *client,
4848 struct wl_resource *resource)
4849{
4850 wl_resource_destroy(resource);
4851}
4852
4853static void
4854confined_pointer_set_region(struct wl_client *client,
4855 struct wl_resource *resource,
4856 struct wl_resource *region_resource)
4857{
4858 struct weston_pointer_constraint *constraint =
4859 wl_resource_get_user_data(resource);
4860 struct weston_region *region = region_resource ?
4861 wl_resource_get_user_data(region_resource) : NULL;
4862
4863 if (!constraint)
4864 return;
4865
4866 if (region) {
4867 pixman_region32_copy(&constraint->region_pending,
4868 &region->region);
4869 } else {
4870 pixman_region32_fini(&constraint->region_pending);
4871 region_init_infinite(&constraint->region_pending);
4872 }
4873 constraint->region_is_pending = true;
4874}
4875
4876static const struct zwp_confined_pointer_v1_interface confined_pointer_interface = {
4877 confined_pointer_destroy,
4878 confined_pointer_set_region,
4879};
4880
4881static void
4882pointer_constraints_confine_pointer(struct wl_client *client,
4883 struct wl_resource *resource,
4884 uint32_t id,
4885 struct wl_resource *surface_resource,
4886 struct wl_resource *pointer_resource,
4887 struct wl_resource *region_resource,
4888 uint32_t lifetime)
4889{
4890 struct weston_surface *surface =
4891 wl_resource_get_user_data(surface_resource);
4892 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
4893 struct weston_region *region = region_resource ?
4894 wl_resource_get_user_data(region_resource) : NULL;
4895
4896 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
4897 &zwp_confined_pointer_v1_interface,
4898 &confined_pointer_interface,
4899 &confined_pointer_grab_interface);
4900}
4901
4902static const struct zwp_pointer_constraints_v1_interface pointer_constraints_interface = {
4903 pointer_constraints_destroy,
4904 pointer_constraints_lock_pointer,
4905 pointer_constraints_confine_pointer,
4906};
4907
4908static void
4909bind_pointer_constraints(struct wl_client *client, void *data,
4910 uint32_t version, uint32_t id)
4911{
4912 struct wl_resource *resource;
4913
4914 resource = wl_resource_create(client,
4915 &zwp_pointer_constraints_v1_interface,
4916 1, id);
4917
4918 wl_resource_set_implementation(resource, &pointer_constraints_interface,
4919 NULL, NULL);
4920}
4921
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004922static void
Alexandros Frantzis2b442482018-02-20 14:05:50 +02004923input_timestamps_destroy(struct wl_client *client,
4924 struct wl_resource *resource)
4925{
4926 wl_resource_destroy(resource);
4927}
4928
4929static const struct zwp_input_timestamps_v1_interface
4930 input_timestamps_interface = {
4931 input_timestamps_destroy,
4932};
4933
4934static void
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004935input_timestamps_manager_destroy(struct wl_client *client,
4936 struct wl_resource *resource)
4937{
4938 wl_resource_destroy(resource);
4939}
4940
4941static void
4942input_timestamps_manager_get_keyboard_timestamps(struct wl_client *client,
4943 struct wl_resource *resource,
4944 uint32_t id,
4945 struct wl_resource *keyboard_resource)
4946{
Alexandros Frantzis2b442482018-02-20 14:05:50 +02004947 struct weston_keyboard *keyboard =
4948 wl_resource_get_user_data(keyboard_resource);
4949 struct wl_resource *input_ts;
4950
4951 input_ts = wl_resource_create(client,
4952 &zwp_input_timestamps_v1_interface,
4953 1, id);
4954 if (!input_ts) {
4955 wl_client_post_no_memory(client);
4956 return;
4957 }
4958
4959 if (keyboard) {
4960 wl_list_insert(&keyboard->timestamps_list,
4961 wl_resource_get_link(input_ts));
4962 } else {
4963 wl_list_init(wl_resource_get_link(input_ts));
4964 }
4965
4966 wl_resource_set_implementation(input_ts,
4967 &input_timestamps_interface,
4968 keyboard_resource,
4969 unbind_resource);
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004970}
4971
4972static void
4973input_timestamps_manager_get_pointer_timestamps(struct wl_client *client,
4974 struct wl_resource *resource,
4975 uint32_t id,
4976 struct wl_resource *pointer_resource)
4977{
Alexandros Frantzisdb907b72018-02-20 14:06:26 +02004978 struct weston_pointer *pointer =
4979 wl_resource_get_user_data(pointer_resource);
4980 struct wl_resource *input_ts;
4981
4982 input_ts = wl_resource_create(client,
4983 &zwp_input_timestamps_v1_interface,
4984 1, id);
4985 if (!input_ts) {
4986 wl_client_post_no_memory(client);
4987 return;
4988 }
4989
4990 if (pointer) {
4991 wl_list_insert(&pointer->timestamps_list,
4992 wl_resource_get_link(input_ts));
4993 } else {
4994 wl_list_init(wl_resource_get_link(input_ts));
4995 }
4996
4997 wl_resource_set_implementation(input_ts,
4998 &input_timestamps_interface,
4999 pointer_resource,
5000 unbind_resource);
Alexandros Frantzis538749d2018-02-16 18:44:16 +02005001}
5002
5003static void
5004input_timestamps_manager_get_touch_timestamps(struct wl_client *client,
5005 struct wl_resource *resource,
5006 uint32_t id,
5007 struct wl_resource *touch_resource)
5008{
Alexandros Frantzisd7157842018-02-20 14:07:03 +02005009 struct weston_touch *touch = wl_resource_get_user_data(touch_resource);
5010 struct wl_resource *input_ts;
5011
5012 input_ts = wl_resource_create(client,
5013 &zwp_input_timestamps_v1_interface,
5014 1, id);
5015 if (!input_ts) {
5016 wl_client_post_no_memory(client);
5017 return;
5018 }
5019
5020 if (touch) {
5021 wl_list_insert(&touch->timestamps_list,
5022 wl_resource_get_link(input_ts));
5023 } else {
5024 wl_list_init(wl_resource_get_link(input_ts));
5025 }
5026
5027 wl_resource_set_implementation(input_ts,
5028 &input_timestamps_interface,
5029 touch_resource,
5030 unbind_resource);
Alexandros Frantzis538749d2018-02-16 18:44:16 +02005031}
5032
5033static const struct zwp_input_timestamps_manager_v1_interface
5034 input_timestamps_manager_interface = {
5035 input_timestamps_manager_destroy,
5036 input_timestamps_manager_get_keyboard_timestamps,
5037 input_timestamps_manager_get_pointer_timestamps,
5038 input_timestamps_manager_get_touch_timestamps,
5039};
5040
5041static void
5042bind_input_timestamps_manager(struct wl_client *client, void *data,
5043 uint32_t version, uint32_t id)
5044{
5045 struct wl_resource *resource =
5046 wl_resource_create(client,
5047 &zwp_input_timestamps_manager_v1_interface,
5048 1, id);
5049
5050 if (resource == NULL) {
5051 wl_client_post_no_memory(client);
5052 return;
5053 }
5054
5055 wl_resource_set_implementation(resource,
5056 &input_timestamps_manager_interface,
5057 NULL, NULL);
5058}
5059
Jonas Ådahl30d61d82014-10-22 21:21:17 +02005060int
5061weston_input_init(struct weston_compositor *compositor)
5062{
5063 if (!wl_global_create(compositor->wl_display,
5064 &zwp_relative_pointer_manager_v1_interface, 1,
5065 compositor, bind_relative_pointer_manager))
5066 return -1;
5067
Jonas Ådahld3414f22016-07-22 17:56:31 +08005068 if (!wl_global_create(compositor->wl_display,
5069 &zwp_pointer_constraints_v1_interface, 1,
5070 NULL, bind_pointer_constraints))
5071 return -1;
5072
Alexandros Frantzis538749d2018-02-16 18:44:16 +02005073 if (!wl_global_create(compositor->wl_display,
5074 &zwp_input_timestamps_manager_v1_interface, 1,
5075 NULL, bind_input_timestamps_manager))
5076 return -1;
5077
Jonas Ådahl30d61d82014-10-22 21:21:17 +02005078 return 0;
5079}