blob: ba211f38328ee877de3f99e63885d030e80c84f6 [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:
2586 break;
2587 }
Pekka Paalanenf4062532018-02-26 16:18:29 +02002588}
2589
Jonas Ådahl1679f232014-04-12 09:39:51 +02002590WL_EXPORT void
Pekka Paalanenbcbce332018-02-26 14:43:00 +02002591notify_touch_frame(struct weston_touch_device *device)
Jonas Ådahl1679f232014-04-12 09:39:51 +02002592{
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002593 struct weston_touch_grab *grab;
Jonas Ådahl1679f232014-04-12 09:39:51 +02002594
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002595 switch (weston_touch_device_get_mode(device)) {
2596 case WESTON_TOUCH_MODE_NORMAL:
2597 case WESTON_TOUCH_MODE_PREP_CALIB:
2598 grab = device->aggregate->grab;
2599 grab->interface->frame(grab);
2600 break;
2601 case WESTON_TOUCH_MODE_CALIB:
2602 case WESTON_TOUCH_MODE_PREP_NORMAL:
2603 break;
2604 }
2605
2606 weston_compositor_update_touch_mode(device->aggregate->seat->compositor);
Jonas Ådahl1679f232014-04-12 09:39:51 +02002607}
2608
Derek Foreman3cc004a2015-11-06 15:56:09 -06002609WL_EXPORT void
Pekka Paalanenbcbce332018-02-26 14:43:00 +02002610notify_touch_cancel(struct weston_touch_device *device)
Derek Foreman3cc004a2015-11-06 15:56:09 -06002611{
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002612 struct weston_touch_grab *grab;
Derek Foreman3cc004a2015-11-06 15:56:09 -06002613
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05002614 switch (weston_touch_device_get_mode(device)) {
2615 case WESTON_TOUCH_MODE_NORMAL:
2616 case WESTON_TOUCH_MODE_PREP_CALIB:
2617 grab = device->aggregate->grab;
2618 grab->interface->cancel(grab);
2619 break;
2620 case WESTON_TOUCH_MODE_CALIB:
2621 case WESTON_TOUCH_MODE_PREP_NORMAL:
2622 break;
2623 }
2624
2625 weston_compositor_update_touch_mode(device->aggregate->seat->compositor);
Derek Foreman3cc004a2015-11-06 15:56:09 -06002626}
2627
Pekka Paalanen8274d902014-08-06 19:36:51 +03002628static int
2629pointer_cursor_surface_get_label(struct weston_surface *surface,
2630 char *buf, size_t len)
2631{
2632 return snprintf(buf, len, "cursor");
2633}
2634
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002635static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002636pointer_cursor_surface_committed(struct weston_surface *es,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002637 int32_t dx, int32_t dy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002638{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002639 struct weston_pointer *pointer = es->committed_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002640 int x, y;
2641
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002642 if (es->width == 0)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002643 return;
2644
Jason Ekstranda7af7042013-10-12 22:38:11 -05002645 assert(es == pointer->sprite->surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002646
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002647 pointer->hotspot_x -= dx;
2648 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002649
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002650 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
2651 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002652
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002653 weston_view_set_position(pointer->sprite, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002654
2655 empty_region(&es->pending.input);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02002656 empty_region(&es->input);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002657
2658 if (!weston_surface_is_mapped(es)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002659 weston_layer_entry_insert(&es->compositor->cursor_layer.view_list,
2660 &pointer->sprite->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002661 weston_view_update_transform(pointer->sprite);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002662 es->is_mapped = true;
2663 pointer->sprite->is_mapped = true;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002664 }
2665}
2666
2667static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002668pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2669 uint32_t serial, struct wl_resource *surface_resource,
2670 int32_t x, int32_t y)
2671{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002672 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002673 struct weston_surface *surface = NULL;
2674
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +02002675 if (!pointer)
2676 return;
2677
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002678 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002679 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002680
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002681 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002682 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002683 /* pointer->focus->surface->resource can be NULL. Surfaces like the
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02002684 black_surface used in shell.c for fullscreen don't have
2685 a resource, but can still have focus */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002686 if (pointer->focus->surface->resource == NULL)
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02002687 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002688 if (wl_resource_get_client(pointer->focus->surface->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002689 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002690 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002691 return;
2692
Derek Foreman4e53c532015-03-23 10:55:32 -05002693 if (!surface) {
2694 if (pointer->sprite)
2695 pointer_unmap_sprite(pointer);
2696 return;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002697 }
2698
Jonas Ådahlb4070242015-03-18 15:08:03 +08002699 if (pointer->sprite && pointer->sprite->surface == surface &&
2700 pointer->hotspot_x == x && pointer->hotspot_y == y)
2701 return;
2702
Derek Foreman4e53c532015-03-23 10:55:32 -05002703 if (!pointer->sprite || pointer->sprite->surface != surface) {
2704 if (weston_surface_set_role(surface, "wl_pointer-cursor",
2705 resource,
2706 WL_POINTER_ERROR_ROLE) < 0)
2707 return;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002708
Derek Foreman4e53c532015-03-23 10:55:32 -05002709 if (pointer->sprite)
2710 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002711
Derek Foreman4e53c532015-03-23 10:55:32 -05002712 wl_signal_add(&surface->destroy_signal,
2713 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002714
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002715 surface->committed = pointer_cursor_surface_committed;
2716 surface->committed_private = pointer;
Derek Foreman4e53c532015-03-23 10:55:32 -05002717 weston_surface_set_label_func(surface,
2718 pointer_cursor_surface_get_label);
2719 pointer->sprite = weston_view_create(surface);
2720 }
2721
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002722 pointer->hotspot_x = x;
2723 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002724
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +02002725 if (surface->buffer_ref.buffer) {
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002726 pointer_cursor_surface_committed(surface, 0, 0);
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +02002727 weston_view_schedule_repaint(pointer->sprite);
2728 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002729}
2730
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002731static void
2732pointer_release(struct wl_client *client, struct wl_resource *resource)
2733{
2734 wl_resource_destroy(resource);
2735}
2736
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002737static const struct wl_pointer_interface pointer_interface = {
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002738 pointer_set_cursor,
2739 pointer_release
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002740};
2741
2742static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002743seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2744 uint32_t id)
2745{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002746 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002747 /* We use the pointer_state directly, which means we'll
2748 * give a wl_pointer if the seat has ever had one - even though
2749 * the spec explicitly states that this request only takes effect
2750 * if the seat has the pointer capability.
2751 *
2752 * This prevents a race between the compositor sending new
2753 * capabilities and the client trying to use the old ones.
2754 */
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002755 struct weston_pointer *pointer = seat ? seat->pointer_state : NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002756 struct wl_resource *cr;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002757 struct weston_pointer_client *pointer_client;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002758
Jason Ekstranda85118c2013-06-27 20:17:02 -05002759 cr = wl_resource_create(client, &wl_pointer_interface,
2760 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002761 if (cr == NULL) {
2762 wl_client_post_no_memory(client);
2763 return;
2764 }
2765
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002766 wl_list_init(wl_resource_get_link(cr));
2767 wl_resource_set_implementation(cr, &pointer_interface, pointer,
2768 unbind_pointer_client_resource);
2769
2770 /* If we don't have a pointer_state, the resource is inert, so there
2771 * is nothing more to set up */
2772 if (!pointer)
2773 return;
2774
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002775 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
2776 if (!pointer_client) {
2777 wl_client_post_no_memory(client);
2778 return;
2779 }
2780
2781 wl_list_insert(&pointer_client->pointer_resources,
2782 wl_resource_get_link(cr));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002783
Derek Foreman1281a362015-07-31 16:55:32 -05002784 if (pointer->focus && pointer->focus->surface->resource &&
2785 wl_resource_get_client(pointer->focus->surface->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002786 wl_fixed_t sx, sy;
2787
Derek Foreman1281a362015-07-31 16:55:32 -05002788 weston_view_from_global_fixed(pointer->focus,
2789 pointer->x,
2790 pointer->y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002791 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002792
Neil Roberts96d790e2013-09-19 17:32:00 +01002793 wl_pointer_send_enter(cr,
Derek Foreman1281a362015-07-31 16:55:32 -05002794 pointer->focus_serial,
2795 pointer->focus->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01002796 sx, sy);
Peter Hutterer87743e92016-01-18 16:38:22 +10002797 pointer_send_frame(cr);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002798 }
2799}
2800
2801static void
Alexandros Frantzis2b442482018-02-20 14:05:50 +02002802destroy_keyboard_resource(struct wl_resource *resource)
2803{
2804 struct weston_keyboard *keyboard = wl_resource_get_user_data(resource);
2805
2806 wl_list_remove(wl_resource_get_link(resource));
2807
2808 if (keyboard) {
2809 remove_input_resource_from_timestamps(resource,
2810 &keyboard->timestamps_list);
2811 }
2812}
2813
2814static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002815keyboard_release(struct wl_client *client, struct wl_resource *resource)
2816{
2817 wl_resource_destroy(resource);
2818}
2819
2820static const struct wl_keyboard_interface keyboard_interface = {
2821 keyboard_release
2822};
2823
Derek Foreman280e7dd2014-10-03 13:13:42 -05002824static bool
Neil Roberts96d790e2013-09-19 17:32:00 +01002825should_send_modifiers_to_client(struct weston_seat *seat,
2826 struct wl_client *client)
2827{
Derek Foreman1281a362015-07-31 16:55:32 -05002828 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2829 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2830
2831 if (keyboard &&
2832 keyboard->focus &&
2833 keyboard->focus->resource &&
2834 wl_resource_get_client(keyboard->focus->resource) == client)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002835 return true;
Neil Roberts96d790e2013-09-19 17:32:00 +01002836
Derek Foreman1281a362015-07-31 16:55:32 -05002837 if (pointer &&
2838 pointer->focus &&
2839 pointer->focus->surface->resource &&
2840 wl_resource_get_client(pointer->focus->surface->resource) == client)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002841 return true;
Neil Roberts96d790e2013-09-19 17:32:00 +01002842
Derek Foreman280e7dd2014-10-03 13:13:42 -05002843 return false;
Neil Roberts96d790e2013-09-19 17:32:00 +01002844}
2845
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002846static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002847seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2848 uint32_t id)
2849{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002850 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002851 /* We use the keyboard_state directly, which means we'll
2852 * give a wl_keyboard if the seat has ever had one - even though
2853 * the spec explicitly states that this request only takes effect
2854 * if the seat has the keyboard capability.
2855 *
2856 * This prevents a race between the compositor sending new
2857 * capabilities and the client trying to use the old ones.
2858 */
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002859 struct weston_keyboard *keyboard = seat ? seat->keyboard_state : NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002860 struct wl_resource *cr;
2861
Jason Ekstranda85118c2013-06-27 20:17:02 -05002862 cr = wl_resource_create(client, &wl_keyboard_interface,
2863 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002864 if (cr == NULL) {
2865 wl_client_post_no_memory(client);
2866 return;
2867 }
2868
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002869 wl_list_init(wl_resource_get_link(cr));
2870 wl_resource_set_implementation(cr, &keyboard_interface,
Alexandros Frantzis2b442482018-02-20 14:05:50 +02002871 keyboard, destroy_keyboard_resource);
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002872
2873 /* If we don't have a keyboard_state, the resource is inert, so there
2874 * is nothing more to set up */
2875 if (!keyboard)
2876 return;
2877
Neil Roberts96d790e2013-09-19 17:32:00 +01002878 /* May be moved to focused list later by either
2879 * weston_keyboard_set_focus or directly if this client is already
2880 * focused */
Derek Foreman345c9f32015-06-03 15:53:28 -05002881 wl_list_insert(&keyboard->resource_list, wl_resource_get_link(cr));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002882
Jonny Lamb66a41a02014-08-12 14:58:25 +02002883 if (wl_resource_get_version(cr) >= WL_KEYBOARD_REPEAT_INFO_SINCE_VERSION) {
2884 wl_keyboard_send_repeat_info(cr,
2885 seat->compositor->kb_repeat_rate,
2886 seat->compositor->kb_repeat_delay);
2887 }
Jasper St. Pierred8c6aeb2014-08-04 13:43:24 -04002888
Derek Foreman185d1582017-06-28 11:17:23 -05002889 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2890 keyboard->xkb_info->keymap_fd,
2891 keyboard->xkb_info->keymap_size);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002892
Neil Roberts96d790e2013-09-19 17:32:00 +01002893 if (should_send_modifiers_to_client(seat, client)) {
Derek Foreman345c9f32015-06-03 15:53:28 -05002894 send_modifiers_to_resource(keyboard,
Neil Roberts96d790e2013-09-19 17:32:00 +01002895 cr,
Derek Foreman345c9f32015-06-03 15:53:28 -05002896 keyboard->focus_serial);
Neil Roberts96d790e2013-09-19 17:32:00 +01002897 }
2898
Derek Foreman345c9f32015-06-03 15:53:28 -05002899 if (keyboard->focus && keyboard->focus->resource &&
2900 wl_resource_get_client(keyboard->focus->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01002901 struct weston_surface *surface =
Derek Foreman345c9f32015-06-03 15:53:28 -05002902 (struct weston_surface *)keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +01002903
2904 wl_list_remove(wl_resource_get_link(cr));
Derek Foreman345c9f32015-06-03 15:53:28 -05002905 wl_list_insert(&keyboard->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002906 wl_resource_get_link(cr));
2907 wl_keyboard_send_enter(cr,
Derek Foreman345c9f32015-06-03 15:53:28 -05002908 keyboard->focus_serial,
Neil Roberts96d790e2013-09-19 17:32:00 +01002909 surface->resource,
Derek Foreman345c9f32015-06-03 15:53:28 -05002910 &keyboard->keys);
Neil Roberts96d790e2013-09-19 17:32:00 +01002911
2912 /* If this is the first keyboard resource for this
2913 * client... */
Derek Foreman345c9f32015-06-03 15:53:28 -05002914 if (keyboard->focus_resource_list.prev ==
Neil Roberts96d790e2013-09-19 17:32:00 +01002915 wl_resource_get_link(cr))
2916 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002917 }
2918}
2919
2920static void
Alexandros Frantzisd7157842018-02-20 14:07:03 +02002921destroy_touch_resource(struct wl_resource *resource)
2922{
2923 struct weston_touch *touch = wl_resource_get_user_data(resource);
2924
2925 wl_list_remove(wl_resource_get_link(resource));
2926
2927 if (touch) {
2928 remove_input_resource_from_timestamps(resource,
2929 &touch->timestamps_list);
2930 }
2931}
2932
2933static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002934touch_release(struct wl_client *client, struct wl_resource *resource)
2935{
2936 wl_resource_destroy(resource);
2937}
2938
2939static const struct wl_touch_interface touch_interface = {
2940 touch_release
2941};
2942
2943static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002944seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2945 uint32_t id)
2946{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002947 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002948 /* We use the touch_state directly, which means we'll
2949 * give a wl_touch if the seat has ever had one - even though
2950 * the spec explicitly states that this request only takes effect
2951 * if the seat has the touch capability.
2952 *
2953 * This prevents a race between the compositor sending new
2954 * capabilities and the client trying to use the old ones.
2955 */
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002956 struct weston_touch *touch = seat ? seat->touch_state : NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002957 struct wl_resource *cr;
2958
Jason Ekstranda85118c2013-06-27 20:17:02 -05002959 cr = wl_resource_create(client, &wl_touch_interface,
2960 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002961 if (cr == NULL) {
2962 wl_client_post_no_memory(client);
2963 return;
2964 }
2965
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002966 wl_list_init(wl_resource_get_link(cr));
2967 wl_resource_set_implementation(cr, &touch_interface,
Alexandros Frantzisd7157842018-02-20 14:07:03 +02002968 touch, destroy_touch_resource);
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002969
2970 /* If we don't have a touch_state, the resource is inert, so there
2971 * is nothing more to set up */
2972 if (!touch)
2973 return;
2974
Derek Foreman1281a362015-07-31 16:55:32 -05002975 if (touch->focus &&
2976 wl_resource_get_client(touch->focus->surface->resource) == client) {
Chokshi, Mituld6697142015-10-09 08:28:47 +00002977 wl_list_insert(&touch->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002978 wl_resource_get_link(cr));
2979 } else {
Chokshi, Mituld6697142015-10-09 08:28:47 +00002980 wl_list_insert(&touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002981 wl_resource_get_link(cr));
2982 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002983}
2984
Quentin Glidicaab1d362016-03-13 17:49:08 +01002985static void
2986seat_release(struct wl_client *client, struct wl_resource *resource)
2987{
2988 wl_resource_destroy(resource);
2989}
2990
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002991static const struct wl_seat_interface seat_interface = {
2992 seat_get_pointer,
2993 seat_get_keyboard,
2994 seat_get_touch,
Quentin Glidicaab1d362016-03-13 17:49:08 +01002995 seat_release,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002996};
2997
2998static void
2999bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3000{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003001 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003002 struct wl_resource *resource;
3003 enum wl_seat_capability caps = 0;
3004
Jason Ekstranda85118c2013-06-27 20:17:02 -05003005 resource = wl_resource_create(client,
Derek Foreman1909c102015-11-26 14:17:47 -06003006 &wl_seat_interface, version, id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05003007 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003008 wl_resource_set_implementation(resource, &seat_interface, data,
3009 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003010
Derek Foreman1281a362015-07-31 16:55:32 -05003011 if (weston_seat_get_pointer(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003012 caps |= WL_SEAT_CAPABILITY_POINTER;
Derek Foreman1281a362015-07-31 16:55:32 -05003013 if (weston_seat_get_keyboard(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003014 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Derek Foreman1281a362015-07-31 16:55:32 -05003015 if (weston_seat_get_touch(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003016 caps |= WL_SEAT_CAPABILITY_TOUCH;
3017
3018 wl_seat_send_capabilities(resource, caps);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003019 if (version >= WL_SEAT_NAME_SINCE_VERSION)
Rob Bradforde445ae62013-05-31 18:09:51 +01003020 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003021}
3022
Jonas Ådahl30d61d82014-10-22 21:21:17 +02003023static void
3024relative_pointer_destroy(struct wl_client *client,
3025 struct wl_resource *resource)
3026{
3027 wl_resource_destroy(resource);
3028}
3029
3030static const struct zwp_relative_pointer_v1_interface relative_pointer_interface = {
3031 relative_pointer_destroy
3032};
3033
3034static void
3035relative_pointer_manager_destroy(struct wl_client *client,
3036 struct wl_resource *resource)
3037{
3038 wl_resource_destroy(resource);
3039}
3040
3041static void
3042relative_pointer_manager_get_relative_pointer(struct wl_client *client,
3043 struct wl_resource *resource,
3044 uint32_t id,
3045 struct wl_resource *pointer_resource)
3046{
3047 struct weston_pointer *pointer =
3048 wl_resource_get_user_data(pointer_resource);
3049 struct weston_pointer_client *pointer_client;
3050 struct wl_resource *cr;
3051
3052 cr = wl_resource_create(client, &zwp_relative_pointer_v1_interface,
3053 wl_resource_get_version(resource), id);
3054 if (cr == NULL) {
3055 wl_client_post_no_memory(client);
3056 return;
3057 }
3058
3059 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
3060 if (!pointer_client) {
3061 wl_client_post_no_memory(client);
3062 return;
3063 }
3064
3065 wl_list_insert(&pointer_client->relative_pointer_resources,
3066 wl_resource_get_link(cr));
3067 wl_resource_set_implementation(cr, &relative_pointer_interface,
3068 pointer,
3069 unbind_pointer_client_resource);
3070}
3071
3072static const struct zwp_relative_pointer_manager_v1_interface relative_pointer_manager = {
3073 relative_pointer_manager_destroy,
3074 relative_pointer_manager_get_relative_pointer,
3075};
3076
3077static void
3078bind_relative_pointer_manager(struct wl_client *client, void *data,
3079 uint32_t version, uint32_t id)
3080{
3081 struct weston_compositor *compositor = data;
3082 struct wl_resource *resource;
3083
3084 resource = wl_resource_create(client,
3085 &zwp_relative_pointer_manager_v1_interface,
3086 1, id);
3087
3088 wl_resource_set_implementation(resource, &relative_pointer_manager,
3089 compositor,
3090 NULL);
3091}
3092
Giulio Camuffo0358af42016-06-02 21:48:08 +03003093WL_EXPORT int
3094weston_compositor_set_xkb_rule_names(struct weston_compositor *ec,
3095 struct xkb_rule_names *names)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003096{
3097 if (ec->xkb_context == NULL) {
3098 ec->xkb_context = xkb_context_new(0);
3099 if (ec->xkb_context == NULL) {
3100 weston_log("failed to create XKB context\n");
3101 return -1;
3102 }
3103 }
3104
3105 if (names)
3106 ec->xkb_names = *names;
3107 if (!ec->xkb_names.rules)
3108 ec->xkb_names.rules = strdup("evdev");
3109 if (!ec->xkb_names.model)
3110 ec->xkb_names.model = strdup("pc105");
3111 if (!ec->xkb_names.layout)
3112 ec->xkb_names.layout = strdup("us");
3113
3114 return 0;
3115}
3116
Stefan Schmidtfda26522013-09-17 10:54:09 +01003117static void
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003118weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003119{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003120 if (--xkb_info->ref_count > 0)
3121 return;
3122
Ran Benitac9c74152014-08-19 23:59:52 +03003123 xkb_keymap_unref(xkb_info->keymap);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003124
3125 if (xkb_info->keymap_area)
3126 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
3127 if (xkb_info->keymap_fd >= 0)
3128 close(xkb_info->keymap_fd);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003129 free(xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003130}
3131
3132void
3133weston_compositor_xkb_destroy(struct weston_compositor *ec)
3134{
3135 free((char *) ec->xkb_names.rules);
3136 free((char *) ec->xkb_names.model);
3137 free((char *) ec->xkb_names.layout);
3138 free((char *) ec->xkb_names.variant);
3139 free((char *) ec->xkb_names.options);
Stefan Schmidtfda26522013-09-17 10:54:09 +01003140
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003141 if (ec->xkb_info)
3142 weston_xkb_info_destroy(ec->xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003143 xkb_context_unref(ec->xkb_context);
3144}
3145
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003146static struct weston_xkb_info *
3147weston_xkb_info_create(struct xkb_keymap *keymap)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003148{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003149 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
3150 if (xkb_info == NULL)
3151 return NULL;
3152
Ran Benita2e1968f2014-08-19 23:59:51 +03003153 xkb_info->keymap = xkb_keymap_ref(keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003154 xkb_info->ref_count = 1;
3155
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003156 char *keymap_str;
3157
Ran Benita2e1968f2014-08-19 23:59:51 +03003158 xkb_info->shift_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3159 XKB_MOD_NAME_SHIFT);
3160 xkb_info->caps_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3161 XKB_MOD_NAME_CAPS);
3162 xkb_info->ctrl_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3163 XKB_MOD_NAME_CTRL);
3164 xkb_info->alt_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3165 XKB_MOD_NAME_ALT);
3166 xkb_info->mod2_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3167 "Mod2");
3168 xkb_info->mod3_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3169 "Mod3");
3170 xkb_info->super_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3171 XKB_MOD_NAME_LOGO);
3172 xkb_info->mod5_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
3173 "Mod5");
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003174
Ran Benita2e1968f2014-08-19 23:59:51 +03003175 xkb_info->num_led = xkb_keymap_led_get_index(xkb_info->keymap,
3176 XKB_LED_NAME_NUM);
3177 xkb_info->caps_led = xkb_keymap_led_get_index(xkb_info->keymap,
3178 XKB_LED_NAME_CAPS);
3179 xkb_info->scroll_led = xkb_keymap_led_get_index(xkb_info->keymap,
3180 XKB_LED_NAME_SCROLL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003181
Ran Benita2e1968f2014-08-19 23:59:51 +03003182 keymap_str = xkb_keymap_get_as_string(xkb_info->keymap,
3183 XKB_KEYMAP_FORMAT_TEXT_V1);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003184 if (keymap_str == NULL) {
3185 weston_log("failed to get string version of keymap\n");
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003186 goto err_keymap;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003187 }
3188 xkb_info->keymap_size = strlen(keymap_str) + 1;
3189
3190 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
3191 if (xkb_info->keymap_fd < 0) {
3192 weston_log("creating a keymap file for %lu bytes failed: %m\n",
3193 (unsigned long) xkb_info->keymap_size);
3194 goto err_keymap_str;
3195 }
3196
3197 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
3198 PROT_READ | PROT_WRITE,
3199 MAP_SHARED, xkb_info->keymap_fd, 0);
3200 if (xkb_info->keymap_area == MAP_FAILED) {
3201 weston_log("failed to mmap() %lu bytes\n",
3202 (unsigned long) xkb_info->keymap_size);
3203 goto err_dev_zero;
3204 }
3205 strcpy(xkb_info->keymap_area, keymap_str);
3206 free(keymap_str);
3207
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003208 return xkb_info;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003209
3210err_dev_zero:
3211 close(xkb_info->keymap_fd);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003212err_keymap_str:
3213 free(keymap_str);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003214err_keymap:
Ran Benita2e1968f2014-08-19 23:59:51 +03003215 xkb_keymap_unref(xkb_info->keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003216 free(xkb_info);
3217 return NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003218}
3219
3220static int
3221weston_compositor_build_global_keymap(struct weston_compositor *ec)
3222{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003223 struct xkb_keymap *keymap;
3224
3225 if (ec->xkb_info != NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003226 return 0;
3227
Ran Benita2e1968f2014-08-19 23:59:51 +03003228 keymap = xkb_keymap_new_from_names(ec->xkb_context,
3229 &ec->xkb_names,
3230 0);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003231 if (keymap == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003232 weston_log("failed to compile global XKB keymap\n");
3233 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
3234 "options %s\n",
3235 ec->xkb_names.rules, ec->xkb_names.model,
3236 ec->xkb_names.layout, ec->xkb_names.variant,
3237 ec->xkb_names.options);
3238 return -1;
3239 }
3240
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00003241 ec->xkb_info = weston_xkb_info_create(keymap);
Rui Matos73d93952013-10-24 19:28:41 +02003242 xkb_keymap_unref(keymap);
Stefan Schmidtfda26522013-09-17 10:54:09 +01003243 if (ec->xkb_info == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003244 return -1;
3245
3246 return 0;
3247}
3248
Rui Matos65196bc2013-10-10 19:44:19 +02003249WL_EXPORT void
3250weston_seat_update_keymap(struct weston_seat *seat, struct xkb_keymap *keymap)
3251{
Derek Foreman1281a362015-07-31 16:55:32 -05003252 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
3253
3254 if (!keyboard || !keymap)
Rui Matos65196bc2013-10-10 19:44:19 +02003255 return;
3256
Derek Foreman1281a362015-07-31 16:55:32 -05003257 xkb_keymap_unref(keyboard->pending_keymap);
3258 keyboard->pending_keymap = xkb_keymap_ref(keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02003259
Derek Foreman1281a362015-07-31 16:55:32 -05003260 if (keyboard->keys.size == 0)
Rui Matos65196bc2013-10-10 19:44:19 +02003261 update_keymap(seat);
Rui Matos65196bc2013-10-10 19:44:19 +02003262}
3263
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003264WL_EXPORT int
3265weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
3266{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003267 struct weston_keyboard *keyboard;
3268
Derek Foreman1281a362015-07-31 16:55:32 -05003269 if (seat->keyboard_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003270 seat->keyboard_device_count += 1;
3271 if (seat->keyboard_device_count == 1)
3272 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003273 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003274 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003275
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003276 keyboard = weston_keyboard_create();
3277 if (keyboard == NULL) {
3278 weston_log("failed to allocate weston keyboard struct\n");
3279 return -1;
3280 }
3281
Derek Foreman185d1582017-06-28 11:17:23 -05003282 if (keymap != NULL) {
3283 keyboard->xkb_info = weston_xkb_info_create(keymap);
3284 if (keyboard->xkb_info == NULL)
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02003285 goto err;
Derek Foreman185d1582017-06-28 11:17:23 -05003286 } else {
3287 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
3288 goto err;
3289 keyboard->xkb_info = seat->compositor->xkb_info;
3290 keyboard->xkb_info->ref_count++;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01003291 }
Derek Foreman185d1582017-06-28 11:17:23 -05003292
3293 keyboard->xkb_state.state = xkb_state_new(keyboard->xkb_info->keymap);
3294 if (keyboard->xkb_state.state == NULL) {
3295 weston_log("failed to initialise XKB state\n");
3296 goto err;
3297 }
3298
3299 keyboard->xkb_state.leds = 0;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01003300
Derek Foreman1281a362015-07-31 16:55:32 -05003301 seat->keyboard_state = keyboard;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02003302 seat->keyboard_device_count = 1;
3303 keyboard->seat = seat;
3304
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003305 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003306
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003307 return 0;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02003308
3309err:
3310 if (keyboard->xkb_info)
3311 weston_xkb_info_destroy(keyboard->xkb_info);
3312 free(keyboard);
3313
3314 return -1;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003315}
3316
Jonas Ådahl91fed542013-12-03 09:14:27 +01003317static void
3318weston_keyboard_reset_state(struct weston_keyboard *keyboard)
3319{
3320 struct weston_seat *seat = keyboard->seat;
3321 struct xkb_state *state;
3322
Derek Foreman185d1582017-06-28 11:17:23 -05003323 state = xkb_state_new(keyboard->xkb_info->keymap);
3324 if (!state) {
3325 weston_log("failed to reset XKB state\n");
3326 return;
Jonas Ådahl91fed542013-12-03 09:14:27 +01003327 }
Derek Foreman185d1582017-06-28 11:17:23 -05003328 xkb_state_unref(keyboard->xkb_state.state);
3329 keyboard->xkb_state.state = state;
3330
3331 keyboard->xkb_state.leds = 0;
Jonas Ådahl91fed542013-12-03 09:14:27 +01003332
3333 seat->modifier_state = 0;
3334}
3335
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003336WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003337weston_seat_release_keyboard(struct weston_seat *seat)
3338{
3339 seat->keyboard_device_count--;
Derek Foremand621df22014-11-19 11:04:12 -06003340 assert(seat->keyboard_device_count >= 0);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003341 if (seat->keyboard_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05003342 weston_keyboard_set_focus(seat->keyboard_state, NULL);
3343 weston_keyboard_cancel_grab(seat->keyboard_state);
3344 weston_keyboard_reset_state(seat->keyboard_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003345 seat_send_updated_caps(seat);
3346 }
3347}
3348
3349WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003350weston_seat_init_pointer(struct weston_seat *seat)
3351{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003352 struct weston_pointer *pointer;
3353
Derek Foreman1281a362015-07-31 16:55:32 -05003354 if (seat->pointer_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003355 seat->pointer_device_count += 1;
3356 if (seat->pointer_device_count == 1)
3357 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003358 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003359 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003360
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003361 pointer = weston_pointer_create(seat);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003362 if (pointer == NULL)
3363 return;
3364
Derek Foreman1281a362015-07-31 16:55:32 -05003365 seat->pointer_state = pointer;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003366 seat->pointer_device_count = 1;
3367 pointer->seat = seat;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003368
3369 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003370}
3371
3372WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003373weston_seat_release_pointer(struct weston_seat *seat)
3374{
Derek Foreman1281a362015-07-31 16:55:32 -05003375 struct weston_pointer *pointer = seat->pointer_state;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003376
3377 seat->pointer_device_count--;
3378 if (seat->pointer_device_count == 0) {
Derek Foremanf9318d12015-05-11 15:40:11 -05003379 weston_pointer_clear_focus(pointer);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003380 weston_pointer_cancel_grab(pointer);
Jonas Ådahl630bae82013-10-17 23:04:06 +02003381
Jonas Ådahla4932742013-10-17 23:04:07 +02003382 if (pointer->sprite)
3383 pointer_unmap_sprite(pointer);
3384
Jonas Ådahl3e12e632013-12-02 22:05:05 +01003385 weston_pointer_reset_state(pointer);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003386 seat_send_updated_caps(seat);
Derek Foremanfd5ca512015-01-07 15:00:25 -06003387
3388 /* seat->pointer is intentionally not destroyed so that
3389 * a newly attached pointer on this seat will retain
3390 * the previous cursor co-ordinates.
3391 */
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003392 }
3393}
3394
3395WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003396weston_seat_init_touch(struct weston_seat *seat)
3397{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003398 struct weston_touch *touch;
3399
Derek Foreman1281a362015-07-31 16:55:32 -05003400 if (seat->touch_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003401 seat->touch_device_count += 1;
3402 if (seat->touch_device_count == 1)
3403 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003404 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003405 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003406
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003407 touch = weston_touch_create();
3408 if (touch == NULL)
3409 return;
3410
Derek Foreman1281a362015-07-31 16:55:32 -05003411 seat->touch_state = touch;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003412 seat->touch_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003413 touch->seat = seat;
3414
3415 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003416}
3417
3418WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003419weston_seat_release_touch(struct weston_seat *seat)
3420{
3421 seat->touch_device_count--;
3422 if (seat->touch_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05003423 weston_touch_set_focus(seat->touch_state, NULL);
3424 weston_touch_cancel_grab(seat->touch_state);
3425 weston_touch_reset_state(seat->touch_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003426 seat_send_updated_caps(seat);
3427 }
3428}
3429
3430WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003431weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
3432 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003433{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003434 memset(seat, 0, sizeof *seat);
3435
Kristian Høgsberge3148752013-05-06 23:19:49 -04003436 seat->selection_data_source = NULL;
3437 wl_list_init(&seat->base_resource_list);
3438 wl_signal_init(&seat->selection_signal);
3439 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04003440 wl_signal_init(&seat->destroy_signal);
Jason Ekstranda4ab5422014-04-02 19:53:45 -05003441 wl_signal_init(&seat->updated_caps_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003442
Peter Hutterer87743e92016-01-18 16:38:22 +10003443 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 5,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003444 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003445
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003446 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003447 seat->modifier_state = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003448 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003449
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003450 wl_list_insert(ec->seat_list.prev, &seat->link);
3451
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003452 clipboard_create(seat);
3453
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003454 wl_signal_emit(&ec->seat_created_signal, seat);
3455}
3456
3457WL_EXPORT void
3458weston_seat_release(struct weston_seat *seat)
3459{
Alexandros Frantzis8480d132018-02-15 13:07:09 +02003460 struct wl_resource *resource;
3461
3462 wl_resource_for_each(resource, &seat->base_resource_list) {
3463 wl_resource_set_user_data(resource, NULL);
3464 }
3465
3466 wl_resource_for_each(resource, &seat->drag_resource_list) {
3467 wl_resource_set_user_data(resource, NULL);
3468 }
3469
3470 wl_list_remove(&seat->base_resource_list);
3471 wl_list_remove(&seat->drag_resource_list);
3472
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003473 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003474
Jonas Ådahl1afb2382014-01-03 19:46:51 +01003475 if (seat->saved_kbd_focus)
3476 wl_list_remove(&seat->saved_kbd_focus_listener.link);
3477
Derek Foreman1281a362015-07-31 16:55:32 -05003478 if (seat->pointer_state)
3479 weston_pointer_destroy(seat->pointer_state);
3480 if (seat->keyboard_state)
3481 weston_keyboard_destroy(seat->keyboard_state);
3482 if (seat->touch_state)
3483 weston_touch_destroy(seat->touch_state);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003484
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003485 free (seat->seat_name);
3486
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003487 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04003488
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003489 wl_signal_emit(&seat->destroy_signal, seat);
3490}
Derek Foreman1281a362015-07-31 16:55:32 -05003491
3492/** Get a seat's keyboard pointer
3493 *
3494 * \param seat The seat to query
3495 * \return The seat's keyboard pointer, or NULL if no keyboard is present
3496 *
3497 * The keyboard pointer for a seat isn't freed when all keyboards are removed,
3498 * so it should only be used when the seat's keyboard_device_count is greater
3499 * than zero. This function does that test and only returns a pointer
3500 * when a keyboard is present.
3501 */
3502WL_EXPORT struct weston_keyboard *
3503weston_seat_get_keyboard(struct weston_seat *seat)
3504{
3505 if (!seat)
3506 return NULL;
3507
3508 if (seat->keyboard_device_count)
3509 return seat->keyboard_state;
3510
3511 return NULL;
3512}
3513
3514/** Get a seat's pointer pointer
3515 *
3516 * \param seat The seat to query
3517 * \return The seat's pointer pointer, or NULL if no pointer device is present
3518 *
3519 * The pointer pointer for a seat isn't freed when all mice are removed,
3520 * so it should only be used when the seat's pointer_device_count is greater
3521 * than zero. This function does that test and only returns a pointer
3522 * when a pointing device is present.
3523 */
3524WL_EXPORT struct weston_pointer *
3525weston_seat_get_pointer(struct weston_seat *seat)
3526{
3527 if (!seat)
3528 return NULL;
3529
3530 if (seat->pointer_device_count)
3531 return seat->pointer_state;
3532
3533 return NULL;
3534}
3535
Jonas Ådahld3414f22016-07-22 17:56:31 +08003536static const struct zwp_locked_pointer_v1_interface locked_pointer_interface;
3537static const struct zwp_confined_pointer_v1_interface confined_pointer_interface;
3538
3539static enum pointer_constraint_type
3540pointer_constraint_get_type(struct weston_pointer_constraint *constraint)
3541{
3542 if (wl_resource_instance_of(constraint->resource,
3543 &zwp_locked_pointer_v1_interface,
3544 &locked_pointer_interface)) {
3545 return POINTER_CONSTRAINT_TYPE_LOCK;
3546 } else if (wl_resource_instance_of(constraint->resource,
3547 &zwp_confined_pointer_v1_interface,
3548 &confined_pointer_interface)) {
3549 return POINTER_CONSTRAINT_TYPE_CONFINE;
3550 }
3551
3552 abort();
3553 return 0;
3554}
3555
3556static void
3557pointer_constraint_notify_activated(struct weston_pointer_constraint *constraint)
3558{
3559 struct wl_resource *resource = constraint->resource;
3560
3561 switch (pointer_constraint_get_type(constraint)) {
3562 case POINTER_CONSTRAINT_TYPE_LOCK:
3563 zwp_locked_pointer_v1_send_locked(resource);
3564 break;
3565 case POINTER_CONSTRAINT_TYPE_CONFINE:
3566 zwp_confined_pointer_v1_send_confined(resource);
3567 break;
3568 }
3569}
3570
3571static void
3572pointer_constraint_notify_deactivated(struct weston_pointer_constraint *constraint)
3573{
3574 struct wl_resource *resource = constraint->resource;
3575
3576 switch (pointer_constraint_get_type(constraint)) {
3577 case POINTER_CONSTRAINT_TYPE_LOCK:
3578 zwp_locked_pointer_v1_send_unlocked(resource);
3579 break;
3580 case POINTER_CONSTRAINT_TYPE_CONFINE:
3581 zwp_confined_pointer_v1_send_unconfined(resource);
3582 break;
3583 }
3584}
3585
3586static struct weston_pointer_constraint *
3587get_pointer_constraint_for_pointer(struct weston_surface *surface,
3588 struct weston_pointer *pointer)
3589{
3590 struct weston_pointer_constraint *constraint;
3591
3592 wl_list_for_each(constraint, &surface->pointer_constraints, link) {
3593 if (constraint->pointer == pointer)
3594 return constraint;
3595 }
3596
3597 return NULL;
3598}
3599
Derek Foreman1281a362015-07-31 16:55:32 -05003600/** Get a seat's touch pointer
3601 *
3602 * \param seat The seat to query
3603 * \return The seat's touch pointer, or NULL if no touch device is present
3604 *
3605 * The touch pointer for a seat isn't freed when all touch devices are removed,
3606 * so it should only be used when the seat's touch_device_count is greater
3607 * than zero. This function does that test and only returns a pointer
3608 * when a touch device is present.
3609 */
3610WL_EXPORT struct weston_touch *
3611weston_seat_get_touch(struct weston_seat *seat)
3612{
3613 if (!seat)
3614 return NULL;
3615
3616 if (seat->touch_device_count)
3617 return seat->touch_state;
3618
3619 return NULL;
3620}
Bryce Harrington24f917e2016-06-29 19:04:07 -07003621
3622/** Sets the keyboard focus to the given surface
3623 *
3624 * \param seat The seat to query
3625 */
3626WL_EXPORT void
3627weston_seat_set_keyboard_focus(struct weston_seat *seat,
3628 struct weston_surface *surface)
3629{
3630 struct weston_compositor *compositor = seat->compositor;
3631 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003632 struct weston_surface_activation_data activation_data;
Bryce Harrington24f917e2016-06-29 19:04:07 -07003633
Jonas Ådahlef8e1c32016-03-15 20:28:51 +08003634 if (keyboard && keyboard->focus != surface) {
Bryce Harrington24f917e2016-06-29 19:04:07 -07003635 weston_keyboard_set_focus(keyboard, surface);
3636 wl_data_device_set_keyboard_focus(seat);
3637 }
3638
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003639 inc_activate_serial(compositor);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003640
3641 activation_data = (struct weston_surface_activation_data) {
3642 .surface = surface,
3643 .seat = seat,
3644 };
3645 wl_signal_emit(&compositor->activate_signal, &activation_data);
Bryce Harrington24f917e2016-06-29 19:04:07 -07003646}
Jonas Ådahl30d61d82014-10-22 21:21:17 +02003647
Jonas Ådahld3414f22016-07-22 17:56:31 +08003648static void
3649enable_pointer_constraint(struct weston_pointer_constraint *constraint,
3650 struct weston_view *view)
3651{
3652 assert(constraint->view == NULL);
3653 constraint->view = view;
3654 pointer_constraint_notify_activated(constraint);
3655 weston_pointer_start_grab(constraint->pointer, &constraint->grab);
3656 wl_list_remove(&constraint->surface_destroy_listener.link);
3657 wl_list_init(&constraint->surface_destroy_listener.link);
3658}
3659
3660static bool
3661is_pointer_constraint_enabled(struct weston_pointer_constraint *constraint)
3662{
3663 return constraint->view != NULL;
3664}
3665
3666static void
3667weston_pointer_constraint_disable(struct weston_pointer_constraint *constraint)
3668{
3669 constraint->view = NULL;
3670 pointer_constraint_notify_deactivated(constraint);
3671 weston_pointer_end_grab(constraint->grab.pointer);
3672}
3673
3674void
3675weston_pointer_constraint_destroy(struct weston_pointer_constraint *constraint)
3676{
3677 if (is_pointer_constraint_enabled(constraint))
3678 weston_pointer_constraint_disable(constraint);
3679
3680 wl_list_remove(&constraint->pointer_destroy_listener.link);
3681 wl_list_remove(&constraint->surface_destroy_listener.link);
3682 wl_list_remove(&constraint->surface_commit_listener.link);
3683 wl_list_remove(&constraint->surface_activate_listener.link);
3684
3685 wl_resource_set_user_data(constraint->resource, NULL);
3686 pixman_region32_fini(&constraint->region);
3687 wl_list_remove(&constraint->link);
3688 free(constraint);
3689}
3690
3691static void
3692disable_pointer_constraint(struct weston_pointer_constraint *constraint)
3693{
3694 switch (constraint->lifetime) {
3695 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT:
3696 weston_pointer_constraint_destroy(constraint);
3697 break;
3698 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT:
3699 weston_pointer_constraint_disable(constraint);
3700 break;
3701 }
3702}
3703
3704static bool
3705is_within_constraint_region(struct weston_pointer_constraint *constraint,
3706 wl_fixed_t sx, wl_fixed_t sy)
3707{
3708 struct weston_surface *surface = constraint->surface;
3709 pixman_region32_t constraint_region;
3710 bool result;
3711
3712 pixman_region32_init(&constraint_region);
3713 pixman_region32_intersect(&constraint_region,
3714 &surface->input,
3715 &constraint->region);
3716 result = pixman_region32_contains_point(&constraint_region,
3717 wl_fixed_to_int(sx),
3718 wl_fixed_to_int(sy),
3719 NULL);
3720 pixman_region32_fini(&constraint_region);
3721
3722 return result;
3723}
3724
3725static void
3726maybe_enable_pointer_constraint(struct weston_pointer_constraint *constraint)
3727{
3728 struct weston_surface *surface = constraint->surface;
3729 struct weston_view *vit;
3730 struct weston_view *view = NULL;
3731 struct weston_pointer *pointer = constraint->pointer;
3732 struct weston_keyboard *keyboard;
3733 struct weston_seat *seat = pointer->seat;
3734 int32_t x, y;
3735
3736 /* Postpone if no view of the surface was most recently clicked. */
3737 wl_list_for_each(vit, &surface->views, surface_link) {
3738 if (vit->click_to_activate_serial ==
3739 surface->compositor->activate_serial) {
3740 view = vit;
3741 }
3742 }
3743 if (view == NULL)
3744 return;
3745
3746 /* Postpone if surface doesn't have keyboard focus. */
3747 keyboard = weston_seat_get_keyboard(seat);
3748 if (!keyboard || keyboard->focus != surface)
3749 return;
3750
3751 /* Postpone constraint if the pointer is not within the
3752 * constraint region.
3753 */
3754 weston_view_from_global(view,
3755 wl_fixed_to_int(pointer->x),
3756 wl_fixed_to_int(pointer->y),
3757 &x, &y);
3758 if (!is_within_constraint_region(constraint,
3759 wl_fixed_from_int(x),
3760 wl_fixed_from_int(y)))
3761 return;
3762
3763 enable_pointer_constraint(constraint, view);
3764}
3765
3766static void
3767locked_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
3768{
3769}
3770
3771static void
3772locked_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02003773 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003774 struct weston_pointer_motion_event *event)
3775{
Quentin Glidiccde13452016-08-12 10:41:32 +02003776 pointer_send_relative_motion(grab->pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08003777}
3778
3779static void
3780locked_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003781 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003782 uint32_t button,
3783 uint32_t state_w)
3784{
3785 weston_pointer_send_button(grab->pointer, time, button, state_w);
3786}
3787
3788static void
3789locked_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02003790 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003791 struct weston_pointer_axis_event *event)
3792{
3793 weston_pointer_send_axis(grab->pointer, time, event);
3794}
3795
3796static void
3797locked_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
3798 uint32_t source)
3799{
3800 weston_pointer_send_axis_source(grab->pointer, source);
3801}
3802
3803static void
3804locked_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
3805{
3806 weston_pointer_send_frame(grab->pointer);
3807}
3808
3809static void
3810locked_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
3811{
3812 struct weston_pointer_constraint *constraint =
3813 container_of(grab, struct weston_pointer_constraint, grab);
3814
3815 disable_pointer_constraint(constraint);
3816}
3817
3818static const struct weston_pointer_grab_interface
3819 locked_pointer_grab_interface = {
3820 locked_pointer_grab_pointer_focus,
3821 locked_pointer_grab_pointer_motion,
3822 locked_pointer_grab_pointer_button,
3823 locked_pointer_grab_pointer_axis,
3824 locked_pointer_grab_pointer_axis_source,
3825 locked_pointer_grab_pointer_frame,
3826 locked_pointer_grab_pointer_cancel,
3827};
3828
3829static void
3830pointer_constraint_constrain_resource_destroyed(struct wl_resource *resource)
3831{
3832 struct weston_pointer_constraint *constraint =
3833 wl_resource_get_user_data(resource);
3834
3835 if (!constraint)
3836 return;
3837
3838 weston_pointer_constraint_destroy(constraint);
3839}
3840
3841static void
3842pointer_constraint_surface_activate(struct wl_listener *listener, void *data)
3843{
3844 struct weston_surface_activation_data *activation = data;
3845 struct weston_pointer *pointer;
3846 struct weston_surface *focus = activation->surface;
3847 struct weston_pointer_constraint *constraint =
3848 container_of(listener, struct weston_pointer_constraint,
3849 surface_activate_listener);
3850 bool is_constraint_surface;
3851
3852 pointer = weston_seat_get_pointer(activation->seat);
3853 if (!pointer)
3854 return;
3855
3856 is_constraint_surface =
3857 get_pointer_constraint_for_pointer(focus, pointer) == constraint;
3858
3859 if (is_constraint_surface &&
3860 !is_pointer_constraint_enabled(constraint))
3861 maybe_enable_pointer_constraint(constraint);
3862 else if (!is_constraint_surface &&
3863 is_pointer_constraint_enabled(constraint))
3864 disable_pointer_constraint(constraint);
3865}
3866
3867static void
3868pointer_constraint_pointer_destroyed(struct wl_listener *listener, void *data)
3869{
3870 struct weston_pointer_constraint *constraint =
3871 container_of(listener, struct weston_pointer_constraint,
3872 pointer_destroy_listener);
3873
3874 weston_pointer_constraint_destroy(constraint);
3875}
3876
3877static void
3878pointer_constraint_surface_destroyed(struct wl_listener *listener, void *data)
3879{
3880 struct weston_pointer_constraint *constraint =
3881 container_of(listener, struct weston_pointer_constraint,
3882 surface_destroy_listener);
3883
3884 weston_pointer_constraint_destroy(constraint);
3885}
3886
3887static void
3888pointer_constraint_surface_committed(struct wl_listener *listener, void *data)
3889{
3890 struct weston_pointer_constraint *constraint =
3891 container_of(listener, struct weston_pointer_constraint,
3892 surface_commit_listener);
3893
3894 if (constraint->region_is_pending) {
3895 constraint->region_is_pending = false;
3896 pixman_region32_copy(&constraint->region,
3897 &constraint->region_pending);
3898 pixman_region32_fini(&constraint->region_pending);
3899 pixman_region32_init(&constraint->region_pending);
3900 }
3901
3902 if (constraint->hint_is_pending) {
3903 constraint->hint_is_pending = false;
3904
3905 constraint->hint_is_pending = true;
3906 constraint->hint_x = constraint->hint_x_pending;
3907 constraint->hint_y = constraint->hint_y_pending;
3908 }
3909
3910 if (pointer_constraint_get_type(constraint) ==
3911 POINTER_CONSTRAINT_TYPE_CONFINE &&
3912 is_pointer_constraint_enabled(constraint))
3913 maybe_warp_confined_pointer(constraint);
3914}
3915
3916static struct weston_pointer_constraint *
3917weston_pointer_constraint_create(struct weston_surface *surface,
3918 struct weston_pointer *pointer,
3919 struct weston_region *region,
3920 enum zwp_pointer_constraints_v1_lifetime lifetime,
3921 struct wl_resource *cr,
3922 const struct weston_pointer_grab_interface *grab_interface)
3923{
3924 struct weston_pointer_constraint *constraint;
3925
3926 constraint = zalloc(sizeof *constraint);
3927 if (!constraint)
3928 return NULL;
3929
3930 constraint->lifetime = lifetime;
3931 pixman_region32_init(&constraint->region);
3932 pixman_region32_init(&constraint->region_pending);
3933 wl_list_insert(&surface->pointer_constraints, &constraint->link);
3934 constraint->surface = surface;
3935 constraint->pointer = pointer;
3936 constraint->resource = cr;
3937 constraint->grab.interface = grab_interface;
3938 if (region) {
3939 pixman_region32_copy(&constraint->region,
3940 &region->region);
3941 } else {
3942 pixman_region32_fini(&constraint->region);
3943 region_init_infinite(&constraint->region);
3944 }
3945
3946 constraint->surface_activate_listener.notify =
3947 pointer_constraint_surface_activate;
3948 constraint->surface_destroy_listener.notify =
3949 pointer_constraint_surface_destroyed;
3950 constraint->surface_commit_listener.notify =
3951 pointer_constraint_surface_committed;
3952 constraint->pointer_destroy_listener.notify =
3953 pointer_constraint_pointer_destroyed;
3954
3955 wl_signal_add(&surface->compositor->activate_signal,
3956 &constraint->surface_activate_listener);
3957 wl_signal_add(&pointer->destroy_signal,
3958 &constraint->pointer_destroy_listener);
3959 wl_signal_add(&surface->destroy_signal,
3960 &constraint->surface_destroy_listener);
3961 wl_signal_add(&surface->commit_signal,
3962 &constraint->surface_commit_listener);
3963
3964 return constraint;
3965}
3966
3967static void
3968init_pointer_constraint(struct wl_resource *pointer_constraints_resource,
3969 uint32_t id,
3970 struct weston_surface *surface,
3971 struct weston_pointer *pointer,
3972 struct weston_region *region,
3973 enum zwp_pointer_constraints_v1_lifetime lifetime,
3974 const struct wl_interface *interface,
3975 const void *implementation,
3976 const struct weston_pointer_grab_interface *grab_interface)
3977{
3978 struct wl_client *client =
3979 wl_resource_get_client(pointer_constraints_resource);
3980 struct wl_resource *cr;
3981 struct weston_pointer_constraint *constraint;
3982
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02003983 if (pointer && get_pointer_constraint_for_pointer(surface, pointer)) {
Jonas Ådahld3414f22016-07-22 17:56:31 +08003984 wl_resource_post_error(pointer_constraints_resource,
3985 ZWP_POINTER_CONSTRAINTS_V1_ERROR_ALREADY_CONSTRAINED,
3986 "the pointer has a lock/confine request on this surface");
3987 return;
3988 }
3989
3990 cr = wl_resource_create(client, interface,
3991 wl_resource_get_version(pointer_constraints_resource),
3992 id);
3993 if (cr == NULL) {
3994 wl_client_post_no_memory(client);
3995 return;
3996 }
3997
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02003998 if (pointer) {
3999 constraint = weston_pointer_constraint_create(surface, pointer,
4000 region, lifetime,
4001 cr, grab_interface);
4002 if (constraint == NULL) {
4003 wl_client_post_no_memory(client);
4004 return;
4005 }
4006 } else {
4007 constraint = NULL;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004008 }
4009
4010 wl_resource_set_implementation(cr, implementation, constraint,
4011 pointer_constraint_constrain_resource_destroyed);
4012
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02004013 if (constraint)
4014 maybe_enable_pointer_constraint(constraint);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004015}
4016
4017static void
4018pointer_constraints_destroy(struct wl_client *client,
4019 struct wl_resource *resource)
4020{
4021 wl_resource_destroy(resource);
4022}
4023
4024static void
4025locked_pointer_destroy(struct wl_client *client,
4026 struct wl_resource *resource)
4027{
4028 struct weston_pointer_constraint *constraint =
4029 wl_resource_get_user_data(resource);
4030 wl_fixed_t x, y;
4031
4032 if (constraint && constraint->view && constraint->hint_is_pending &&
4033 is_within_constraint_region(constraint,
4034 constraint->hint_x,
4035 constraint->hint_y)) {
4036 weston_view_to_global_fixed(constraint->view,
4037 constraint->hint_x,
4038 constraint->hint_y,
4039 &x, &y);
4040 weston_pointer_move_to(constraint->pointer, x, y);
4041 }
4042 wl_resource_destroy(resource);
4043}
4044
4045static void
4046locked_pointer_set_cursor_position_hint(struct wl_client *client,
4047 struct wl_resource *resource,
4048 wl_fixed_t surface_x,
4049 wl_fixed_t surface_y)
4050{
4051 struct weston_pointer_constraint *constraint =
4052 wl_resource_get_user_data(resource);
4053
4054 /* Ignore a set cursor hint that was sent after the lock was cancelled.
4055 */
4056 if (!constraint ||
4057 !constraint->resource ||
4058 constraint->resource != resource)
4059 return;
4060
4061 constraint->hint_is_pending = true;
4062 constraint->hint_x_pending = surface_x;
4063 constraint->hint_y_pending = surface_y;
4064}
4065
4066static void
4067locked_pointer_set_region(struct wl_client *client,
4068 struct wl_resource *resource,
4069 struct wl_resource *region_resource)
4070{
4071 struct weston_pointer_constraint *constraint =
4072 wl_resource_get_user_data(resource);
4073 struct weston_region *region = region_resource ?
4074 wl_resource_get_user_data(region_resource) : NULL;
4075
4076 if (!constraint)
4077 return;
4078
4079 if (region) {
4080 pixman_region32_copy(&constraint->region_pending,
4081 &region->region);
4082 } else {
4083 pixman_region32_fini(&constraint->region_pending);
4084 region_init_infinite(&constraint->region_pending);
4085 }
4086 constraint->region_is_pending = true;
4087}
4088
4089
4090static const struct zwp_locked_pointer_v1_interface locked_pointer_interface = {
4091 locked_pointer_destroy,
4092 locked_pointer_set_cursor_position_hint,
4093 locked_pointer_set_region,
4094};
4095
4096static void
4097pointer_constraints_lock_pointer(struct wl_client *client,
4098 struct wl_resource *resource,
4099 uint32_t id,
4100 struct wl_resource *surface_resource,
4101 struct wl_resource *pointer_resource,
4102 struct wl_resource *region_resource,
4103 uint32_t lifetime)
4104{
4105 struct weston_surface *surface =
4106 wl_resource_get_user_data(surface_resource);
4107 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
4108 struct weston_region *region = region_resource ?
4109 wl_resource_get_user_data(region_resource) : NULL;
4110
4111 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
4112 &zwp_locked_pointer_v1_interface,
4113 &locked_pointer_interface,
4114 &locked_pointer_grab_interface);
4115}
4116
4117static void
4118confined_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
4119{
4120}
4121
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004122static double
4123vec2d_cross_product(struct vec2d a, struct vec2d b)
4124{
4125 return a.x * b.y - a.y * b.x;
4126}
4127
4128static struct vec2d
4129vec2d_add(struct vec2d a, struct vec2d b)
4130{
4131 return (struct vec2d) {
4132 .x = a.x + b.x,
4133 .y = a.y + b.y,
4134 };
4135}
4136
4137static struct vec2d
4138vec2d_subtract(struct vec2d a, struct vec2d b)
4139{
4140 return (struct vec2d) {
4141 .x = a.x - b.x,
4142 .y = a.y - b.y,
4143 };
4144}
4145
4146static struct vec2d
4147vec2d_multiply_constant(double c, struct vec2d a)
4148{
4149 return (struct vec2d) {
4150 .x = c * a.x,
4151 .y = c * a.y,
4152 };
4153}
4154
4155static bool
4156lines_intersect(struct line *line1, struct line *line2,
4157 struct vec2d *intersection)
4158{
4159 struct vec2d p = line1->a;
4160 struct vec2d r = vec2d_subtract(line1->b, line1->a);
4161 struct vec2d q = line2->a;
4162 struct vec2d s = vec2d_subtract(line2->b, line2->a);
4163 double rxs;
4164 double sxr;
4165 double t;
4166 double u;
4167
4168 /*
4169 * The line (p, r) and (q, s) intersects where
4170 *
4171 * p + t r = q + u s
4172 *
4173 * Calculate t:
4174 *
4175 * (p + t r) × s = (q + u s) × s
4176 * p × s + t (r × s) = q × s + u (s × s)
4177 * p × s + t (r × s) = q × s
4178 * t (r × s) = q × s - p × s
4179 * t (r × s) = (q - p) × s
4180 * t = ((q - p) × s) / (r × s)
4181 *
4182 * Using the same method, for u we get:
4183 *
4184 * u = ((p - q) × r) / (s × r)
4185 */
4186
4187 rxs = vec2d_cross_product(r, s);
4188 sxr = vec2d_cross_product(s, r);
4189
4190 /* If r × s = 0 then the lines are either parallel or collinear. */
4191 if (fabs(rxs) < DBL_MIN)
4192 return false;
4193
4194 t = vec2d_cross_product(vec2d_subtract(q, p), s) / rxs;
4195 u = vec2d_cross_product(vec2d_subtract(p, q), r) / sxr;
4196
4197 /* The lines only intersect if 0 ≤ t ≤ 1 and 0 ≤ u ≤ 1. */
4198 if (t < 0.0 || t > 1.0 || u < 0.0 || u > 1.0)
4199 return false;
4200
4201 *intersection = vec2d_add(p, vec2d_multiply_constant(t, r));
4202 return true;
4203}
4204
4205static struct border *
4206add_border(struct wl_array *array,
4207 double x1, double y1,
4208 double x2, double y2,
4209 enum motion_direction blocking_dir)
4210{
4211 struct border *border = wl_array_add(array, sizeof *border);
4212
4213 *border = (struct border) {
4214 .line = (struct line) {
4215 .a = (struct vec2d) {
4216 .x = x1,
4217 .y = y1,
4218 },
4219 .b = (struct vec2d) {
4220 .x = x2,
4221 .y = y2,
4222 },
4223 },
4224 .blocking_dir = blocking_dir,
4225 };
4226
4227 return border;
4228}
4229
4230static int
4231compare_lines_x(const void *a, const void *b)
4232{
4233 const struct border *border_a = a;
4234 const struct border *border_b = b;
4235
4236
4237 if (border_a->line.a.x == border_b->line.a.x)
4238 return border_a->line.b.x < border_b->line.b.x;
4239 else
4240 return border_a->line.a.x > border_b->line.a.x;
4241}
4242
4243static void
4244add_non_overlapping_edges(pixman_box32_t *boxes,
4245 int band_above_start,
4246 int band_below_start,
4247 int band_below_end,
4248 struct wl_array *borders)
4249{
4250 int i;
4251 struct wl_array band_merge;
4252 struct border *border;
4253 struct border *prev_border;
4254 struct border *new_border;
4255
4256 wl_array_init(&band_merge);
4257
4258 /* Add bottom band of previous row, and top band of current row, and
4259 * sort them so lower left x coordinate comes first. If there are two
4260 * borders with the same left x coordinate, the wider one comes first.
4261 */
4262 for (i = band_above_start; i < band_below_start; i++) {
4263 pixman_box32_t *box = &boxes[i];
4264 add_border(&band_merge, box->x1, box->y2, box->x2, box->y2,
4265 MOTION_DIRECTION_POSITIVE_Y);
4266 }
4267 for (i = band_below_start; i < band_below_end; i++) {
4268 pixman_box32_t *box= &boxes[i];
4269 add_border(&band_merge, box->x1, box->y1, box->x2, box->y1,
4270 MOTION_DIRECTION_NEGATIVE_Y);
4271 }
4272 qsort(band_merge.data,
4273 band_merge.size / sizeof *border,
4274 sizeof *border,
4275 compare_lines_x);
4276
4277 /* Combine the two combined bands so that any overlapping border is
4278 * eliminated. */
4279 prev_border = NULL;
4280 wl_array_for_each(border, &band_merge) {
4281 assert(border->line.a.y == border->line.b.y);
4282 assert(!prev_border ||
4283 prev_border->line.a.y == border->line.a.y);
4284 assert(!prev_border ||
4285 (prev_border->line.a.x != border->line.a.x ||
4286 prev_border->line.b.x != border->line.b.x));
4287 assert(!prev_border ||
4288 prev_border->line.a.x <= border->line.a.x);
4289
4290 if (prev_border &&
4291 prev_border->line.a.x == border->line.a.x) {
4292 /*
4293 * ------------ +
4294 * ------- =
4295 * [ ]-----
4296 */
4297 prev_border->line.a.x = border->line.b.x;
4298 } else if (prev_border &&
4299 prev_border->line.b.x == border->line.b.x) {
4300 /*
4301 * ------------ +
4302 * ------ =
4303 * ------[ ]
4304 */
4305 prev_border->line.b.x = border->line.a.x;
4306 } else if (prev_border &&
4307 prev_border->line.b.x == border->line.a.x) {
4308 /*
4309 * -------- +
4310 * ------ =
4311 * --------------
4312 */
4313 prev_border->line.b.x = border->line.b.x;
4314 } else if (prev_border &&
4315 prev_border->line.b.x >= border->line.a.x) {
4316 /*
4317 * --------------- +
4318 * ------ =
4319 * -----[ ]----
4320 */
4321 new_border = add_border(borders,
4322 border->line.b.x,
4323 border->line.b.y,
4324 prev_border->line.b.x,
4325 prev_border->line.b.y,
4326 prev_border->blocking_dir);
4327 prev_border->line.b.x = border->line.a.x;
4328 prev_border = new_border;
4329 } else {
4330 assert(!prev_border ||
4331 prev_border->line.b.x < border->line.a.x);
4332 /*
4333 * First border or non-overlapping.
4334 *
4335 * ----- +
4336 * ----- =
4337 * ----- -----
4338 */
4339 new_border = wl_array_add(borders, sizeof *border);
4340 *new_border = *border;
4341 prev_border = new_border;
4342 }
4343 }
4344
4345 wl_array_release(&band_merge);
4346}
4347
4348static void
4349add_band_bottom_edges(pixman_box32_t *boxes,
4350 int band_start,
4351 int band_end,
4352 struct wl_array *borders)
4353{
4354 int i;
4355
4356 for (i = band_start; i < band_end; i++) {
4357 add_border(borders,
4358 boxes[i].x1, boxes[i].y2,
4359 boxes[i].x2, boxes[i].y2,
4360 MOTION_DIRECTION_POSITIVE_Y);
4361 }
4362}
4363
4364static void
4365region_to_outline(pixman_region32_t *region, struct wl_array *borders)
4366{
4367 pixman_box32_t *boxes;
4368 int num_boxes;
4369 int i;
4370 int top_most, bottom_most;
4371 int current_roof;
4372 int prev_top;
4373 int band_start, prev_band_start;
4374
4375 /*
4376 * Remove any overlapping lines from the set of rectangles. Note that
4377 * pixman regions are grouped as rows of rectangles, where rectangles
4378 * in one row never touch or overlap and are all of the same height.
4379 *
4380 * -------- --- -------- ---
4381 * | | | | | | | |
4382 * ----------====---- --- ----------- ----- ---
4383 * | | => | |
4384 * ----==========--------- ----- ----------
4385 * | | | |
4386 * ------------------- -------------------
4387 *
4388 */
4389
4390 boxes = pixman_region32_rectangles(region, &num_boxes);
4391 prev_top = 0;
4392 top_most = boxes[0].y1;
4393 current_roof = top_most;
4394 bottom_most = boxes[num_boxes - 1].y2;
4395 band_start = 0;
4396 prev_band_start = 0;
4397 for (i = 0; i < num_boxes; i++) {
4398 /* Detect if there is a vertical empty space, and add the lower
4399 * level of the previous band if so was the case. */
4400 if (i > 0 &&
4401 boxes[i].y1 != prev_top &&
4402 boxes[i].y1 != boxes[i - 1].y2) {
4403 current_roof = boxes[i].y1;
4404 add_band_bottom_edges(boxes,
4405 band_start,
4406 i,
4407 borders);
4408 }
4409
4410 /* Special case adding the last band, since it won't be handled
4411 * by the band change detection below. */
4412 if (boxes[i].y1 != current_roof && i == num_boxes - 1) {
4413 if (boxes[i].y1 != prev_top) {
4414 /* The last band is a single box, so we don't
4415 * have a prev_band_start to tell us when the
4416 * previous band started. */
4417 add_non_overlapping_edges(boxes,
4418 band_start,
4419 i,
4420 i + 1,
4421 borders);
4422 } else {
4423 add_non_overlapping_edges(boxes,
4424 prev_band_start,
4425 band_start,
4426 i + 1,
4427 borders);
4428 }
4429 }
4430
4431 /* Detect when passing a band and combine the top border of the
4432 * just passed band with the bottom band of the previous band.
4433 */
4434 if (boxes[i].y1 != top_most && boxes[i].y1 != prev_top) {
4435 /* Combine the two passed bands. */
4436 if (prev_top != current_roof) {
4437 add_non_overlapping_edges(boxes,
4438 prev_band_start,
4439 band_start,
4440 i,
4441 borders);
4442 }
4443
4444 prev_band_start = band_start;
4445 band_start = i;
4446 }
4447
4448 /* Add the top border if the box is part of the current roof. */
4449 if (boxes[i].y1 == current_roof) {
4450 add_border(borders,
4451 boxes[i].x1, boxes[i].y1,
4452 boxes[i].x2, boxes[i].y1,
4453 MOTION_DIRECTION_NEGATIVE_Y);
4454 }
4455
4456 /* Add the bottom border of the last band. */
4457 if (boxes[i].y2 == bottom_most) {
4458 add_border(borders,
4459 boxes[i].x1, boxes[i].y2,
4460 boxes[i].x2, boxes[i].y2,
4461 MOTION_DIRECTION_POSITIVE_Y);
4462 }
4463
4464 /* Always add the left border. */
4465 add_border(borders,
4466 boxes[i].x1, boxes[i].y1,
4467 boxes[i].x1, boxes[i].y2,
4468 MOTION_DIRECTION_NEGATIVE_X);
4469
4470 /* Always add the right border. */
4471 add_border(borders,
4472 boxes[i].x2, boxes[i].y1,
4473 boxes[i].x2, boxes[i].y2,
4474 MOTION_DIRECTION_POSITIVE_X);
4475
4476 prev_top = boxes[i].y1;
4477 }
4478}
4479
4480static bool
4481is_border_horizontal (struct border *border)
4482{
4483 return border->line.a.y == border->line.b.y;
4484}
4485
4486static bool
4487is_border_blocking_directions(struct border *border,
4488 uint32_t directions)
4489{
4490 /* Don't block parallel motions. */
4491 if (is_border_horizontal(border)) {
4492 if ((directions & (MOTION_DIRECTION_POSITIVE_Y |
4493 MOTION_DIRECTION_NEGATIVE_Y)) == 0)
4494 return false;
4495 } else {
4496 if ((directions & (MOTION_DIRECTION_POSITIVE_X |
4497 MOTION_DIRECTION_NEGATIVE_X)) == 0)
4498 return false;
4499 }
4500
4501 return (~border->blocking_dir & directions) != directions;
4502}
4503
4504static struct border *
4505get_closest_border(struct wl_array *borders,
4506 struct line *motion,
4507 uint32_t directions)
4508{
4509 struct border *border;
4510 struct vec2d intersection;
4511 struct vec2d delta;
4512 double distance_2;
4513 struct border *closest_border = NULL;
4514 double closest_distance_2 = DBL_MAX;
4515
4516 wl_array_for_each(border, borders) {
4517 if (!is_border_blocking_directions(border, directions))
4518 continue;
4519
4520 if (!lines_intersect(&border->line, motion, &intersection))
4521 continue;
4522
4523 delta = vec2d_subtract(intersection, motion->a);
4524 distance_2 = delta.x*delta.x + delta.y*delta.y;
4525 if (distance_2 < closest_distance_2) {
4526 closest_border = border;
4527 closest_distance_2 = distance_2;
4528 }
4529 }
4530
4531 return closest_border;
4532}
4533
4534static void
4535clamp_to_border(struct border *border,
4536 struct line *motion,
4537 uint32_t *motion_dir)
4538{
4539 /*
4540 * When clamping either rightward or downward motions, the motion needs
4541 * to be clamped so that the destination coordinate does not end up on
4542 * the border (see weston_pointer_clamp_event_to_region). Do this by
4543 * clamping such motions to the border minus the smallest possible
4544 * wl_fixed_t value.
4545 */
4546 if (is_border_horizontal(border)) {
4547 if (*motion_dir & MOTION_DIRECTION_POSITIVE_Y)
4548 motion->b.y = border->line.a.y - wl_fixed_to_double(1);
4549 else
4550 motion->b.y = border->line.a.y;
4551 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_Y |
4552 MOTION_DIRECTION_NEGATIVE_Y);
4553 } else {
4554 if (*motion_dir & MOTION_DIRECTION_POSITIVE_X)
4555 motion->b.x = border->line.a.x - wl_fixed_to_double(1);
4556 else
4557 motion->b.x = border->line.a.x;
4558 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_X |
4559 MOTION_DIRECTION_NEGATIVE_X);
4560 }
4561}
4562
4563static uint32_t
4564get_motion_directions(struct line *motion)
4565{
4566 uint32_t directions = 0;
4567
4568 if (motion->a.x < motion->b.x)
4569 directions |= MOTION_DIRECTION_POSITIVE_X;
4570 else if (motion->a.x > motion->b.x)
4571 directions |= MOTION_DIRECTION_NEGATIVE_X;
4572 if (motion->a.y < motion->b.y)
4573 directions |= MOTION_DIRECTION_POSITIVE_Y;
4574 else if (motion->a.y > motion->b.y)
4575 directions |= MOTION_DIRECTION_NEGATIVE_Y;
4576
4577 return directions;
4578}
4579
Jonas Ådahld3414f22016-07-22 17:56:31 +08004580static void
4581weston_pointer_clamp_event_to_region(struct weston_pointer *pointer,
4582 struct weston_pointer_motion_event *event,
4583 pixman_region32_t *region,
4584 wl_fixed_t *clamped_x,
4585 wl_fixed_t *clamped_y)
4586{
4587 wl_fixed_t x, y;
4588 wl_fixed_t sx, sy;
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004589 wl_fixed_t old_sx = pointer->sx;
4590 wl_fixed_t old_sy = pointer->sy;
4591 struct wl_array borders;
4592 struct line motion;
4593 struct border *closest_border;
4594 float new_x_f, new_y_f;
4595 uint32_t directions;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004596
4597 weston_pointer_motion_to_abs(pointer, event, &x, &y);
4598 weston_view_from_global_fixed(pointer->focus, x, y, &sx, &sy);
4599
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004600 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004601
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004602 /*
4603 * Generate borders given the confine region we are to use. The borders
4604 * are defined to be the outer region of the allowed area. This means
4605 * top/left borders are "within" the allowed area, while bottom/right
4606 * borders are outside. This needs to be considered when clamping
4607 * confined motion vectors.
4608 */
4609 region_to_outline(region, &borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004610
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004611 motion = (struct line) {
4612 .a = (struct vec2d) {
4613 .x = wl_fixed_to_double(old_sx),
4614 .y = wl_fixed_to_double(old_sy),
4615 },
4616 .b = (struct vec2d) {
4617 .x = wl_fixed_to_double(sx),
4618 .y = wl_fixed_to_double(sy),
4619 },
4620 };
4621 directions = get_motion_directions(&motion);
4622
4623 while (directions) {
4624 closest_border = get_closest_border(&borders,
4625 &motion,
4626 directions);
4627 if (closest_border)
4628 clamp_to_border(closest_border, &motion, &directions);
4629 else
4630 break;
4631 }
4632
4633 weston_view_to_global_float(pointer->focus,
4634 (float) motion.b.x, (float) motion.b.y,
4635 &new_x_f, &new_y_f);
4636 *clamped_x = wl_fixed_from_double(new_x_f);
4637 *clamped_y = wl_fixed_from_double(new_y_f);
4638
4639 wl_array_release(&borders);
4640}
4641
4642static double
4643point_to_border_distance_2(struct border *border, double x, double y)
4644{
4645 double orig_x, orig_y;
4646 double dx, dy;
4647
4648 if (is_border_horizontal(border)) {
4649 if (x < border->line.a.x)
4650 orig_x = border->line.a.x;
4651 else if (x > border->line.b.x)
4652 orig_x = border->line.b.x;
4653 else
4654 orig_x = x;
4655 orig_y = border->line.a.y;
4656 } else {
4657 if (y < border->line.a.y)
4658 orig_y = border->line.a.y;
4659 else if (y > border->line.b.y)
4660 orig_y = border->line.b.y;
4661 else
4662 orig_y = y;
4663 orig_x = border->line.a.x;
4664 }
4665
4666
4667 dx = fabs(orig_x - x);
4668 dy = fabs(orig_y - y);
4669 return dx*dx + dy*dy;
4670}
4671
4672static void
4673warp_to_behind_border(struct border *border, wl_fixed_t *sx, wl_fixed_t *sy)
4674{
4675 switch (border->blocking_dir) {
4676 case MOTION_DIRECTION_POSITIVE_X:
4677 case MOTION_DIRECTION_NEGATIVE_X:
4678 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_X)
4679 *sx = wl_fixed_from_double(border->line.a.x) - 1;
4680 else
4681 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4682 if (*sy < wl_fixed_from_double(border->line.a.y))
4683 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4684 else if (*sy > wl_fixed_from_double(border->line.b.y))
4685 *sy = wl_fixed_from_double(border->line.b.y) - 1;
4686 break;
4687 case MOTION_DIRECTION_POSITIVE_Y:
4688 case MOTION_DIRECTION_NEGATIVE_Y:
4689 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_Y)
4690 *sy = wl_fixed_from_double(border->line.a.y) - 1;
4691 else
4692 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4693 if (*sx < wl_fixed_from_double(border->line.a.x))
4694 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4695 else if (*sx > wl_fixed_from_double(border->line.b.x))
4696 *sx = wl_fixed_from_double(border->line.b.x) - 1;
4697 break;
4698 }
Jonas Ådahld3414f22016-07-22 17:56:31 +08004699}
4700
4701static void
4702maybe_warp_confined_pointer(struct weston_pointer_constraint *constraint)
4703{
4704 wl_fixed_t x;
4705 wl_fixed_t y;
4706 wl_fixed_t sx;
4707 wl_fixed_t sy;
4708
4709 weston_view_from_global_fixed(constraint->view,
4710 constraint->pointer->x,
4711 constraint->pointer->y,
4712 &sx,
4713 &sy);
4714
4715 if (!is_within_constraint_region(constraint, sx, sy)) {
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004716 double xf = wl_fixed_to_double(sx);
4717 double yf = wl_fixed_to_double(sy);
4718 pixman_region32_t confine_region;
4719 struct wl_array borders;
4720 struct border *border;
4721 double closest_distance_2 = DBL_MAX;
4722 struct border *closest_border = NULL;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004723
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004724 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004725
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004726 pixman_region32_init(&confine_region);
4727 pixman_region32_intersect(&confine_region,
4728 &constraint->view->surface->input,
4729 &constraint->region);
4730 region_to_outline(&confine_region, &borders);
4731 pixman_region32_fini(&confine_region);
4732
4733 wl_array_for_each(border, &borders) {
4734 double distance_2;
4735
4736 distance_2 = point_to_border_distance_2(border, xf, yf);
4737 if (distance_2 < closest_distance_2) {
4738 closest_border = border;
4739 closest_distance_2 = distance_2;
4740 }
4741 }
4742 assert(closest_border);
4743
4744 warp_to_behind_border(closest_border, &sx, &sy);
4745
4746 wl_array_release(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004747
4748 weston_view_to_global_fixed(constraint->view, sx, sy, &x, &y);
4749 weston_pointer_move_to(constraint->pointer, x, y);
4750 }
4751}
4752
4753static void
4754confined_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02004755 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004756 struct weston_pointer_motion_event *event)
4757{
4758 struct weston_pointer_constraint *constraint =
4759 container_of(grab, struct weston_pointer_constraint, grab);
4760 struct weston_pointer *pointer = grab->pointer;
4761 struct weston_surface *surface;
4762 wl_fixed_t x, y;
4763 wl_fixed_t old_sx = pointer->sx;
4764 wl_fixed_t old_sy = pointer->sy;
4765 pixman_region32_t confine_region;
4766
4767 assert(pointer->focus);
4768 assert(pointer->focus->surface == constraint->surface);
4769
4770 surface = pointer->focus->surface;
4771
4772 pixman_region32_init(&confine_region);
4773 pixman_region32_intersect(&confine_region,
4774 &surface->input,
4775 &constraint->region);
4776 weston_pointer_clamp_event_to_region(pointer, event,
4777 &confine_region, &x, &y);
4778 weston_pointer_move_to(pointer, x, y);
4779 pixman_region32_fini(&confine_region);
4780
4781 weston_view_from_global_fixed(pointer->focus, x, y,
4782 &pointer->sx, &pointer->sy);
4783
4784 if (old_sx != pointer->sx || old_sy != pointer->sy) {
Quentin Glidiccde13452016-08-12 10:41:32 +02004785 pointer_send_motion(pointer, time,
4786 pointer->sx, pointer->sy);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004787 }
4788
Quentin Glidiccde13452016-08-12 10:41:32 +02004789 pointer_send_relative_motion(pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004790}
4791
4792static void
4793confined_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02004794 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004795 uint32_t button,
4796 uint32_t state_w)
4797{
4798 weston_pointer_send_button(grab->pointer, time, button, state_w);
4799}
4800
4801static void
4802confined_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02004803 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004804 struct weston_pointer_axis_event *event)
4805{
4806 weston_pointer_send_axis(grab->pointer, time, event);
4807}
4808
4809static void
4810confined_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
4811 uint32_t source)
4812{
4813 weston_pointer_send_axis_source(grab->pointer, source);
4814}
4815
4816static void
4817confined_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
4818{
4819 weston_pointer_send_frame(grab->pointer);
4820}
4821
4822static void
4823confined_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
4824{
4825 struct weston_pointer_constraint *constraint =
4826 container_of(grab, struct weston_pointer_constraint, grab);
4827
4828 disable_pointer_constraint(constraint);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004829}
4830
4831static const struct weston_pointer_grab_interface
4832 confined_pointer_grab_interface = {
4833 confined_pointer_grab_pointer_focus,
4834 confined_pointer_grab_pointer_motion,
4835 confined_pointer_grab_pointer_button,
4836 confined_pointer_grab_pointer_axis,
4837 confined_pointer_grab_pointer_axis_source,
4838 confined_pointer_grab_pointer_frame,
4839 confined_pointer_grab_pointer_cancel,
4840};
4841
4842static void
4843confined_pointer_destroy(struct wl_client *client,
4844 struct wl_resource *resource)
4845{
4846 wl_resource_destroy(resource);
4847}
4848
4849static void
4850confined_pointer_set_region(struct wl_client *client,
4851 struct wl_resource *resource,
4852 struct wl_resource *region_resource)
4853{
4854 struct weston_pointer_constraint *constraint =
4855 wl_resource_get_user_data(resource);
4856 struct weston_region *region = region_resource ?
4857 wl_resource_get_user_data(region_resource) : NULL;
4858
4859 if (!constraint)
4860 return;
4861
4862 if (region) {
4863 pixman_region32_copy(&constraint->region_pending,
4864 &region->region);
4865 } else {
4866 pixman_region32_fini(&constraint->region_pending);
4867 region_init_infinite(&constraint->region_pending);
4868 }
4869 constraint->region_is_pending = true;
4870}
4871
4872static const struct zwp_confined_pointer_v1_interface confined_pointer_interface = {
4873 confined_pointer_destroy,
4874 confined_pointer_set_region,
4875};
4876
4877static void
4878pointer_constraints_confine_pointer(struct wl_client *client,
4879 struct wl_resource *resource,
4880 uint32_t id,
4881 struct wl_resource *surface_resource,
4882 struct wl_resource *pointer_resource,
4883 struct wl_resource *region_resource,
4884 uint32_t lifetime)
4885{
4886 struct weston_surface *surface =
4887 wl_resource_get_user_data(surface_resource);
4888 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
4889 struct weston_region *region = region_resource ?
4890 wl_resource_get_user_data(region_resource) : NULL;
4891
4892 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
4893 &zwp_confined_pointer_v1_interface,
4894 &confined_pointer_interface,
4895 &confined_pointer_grab_interface);
4896}
4897
4898static const struct zwp_pointer_constraints_v1_interface pointer_constraints_interface = {
4899 pointer_constraints_destroy,
4900 pointer_constraints_lock_pointer,
4901 pointer_constraints_confine_pointer,
4902};
4903
4904static void
4905bind_pointer_constraints(struct wl_client *client, void *data,
4906 uint32_t version, uint32_t id)
4907{
4908 struct wl_resource *resource;
4909
4910 resource = wl_resource_create(client,
4911 &zwp_pointer_constraints_v1_interface,
4912 1, id);
4913
4914 wl_resource_set_implementation(resource, &pointer_constraints_interface,
4915 NULL, NULL);
4916}
4917
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004918static void
Alexandros Frantzis2b442482018-02-20 14:05:50 +02004919input_timestamps_destroy(struct wl_client *client,
4920 struct wl_resource *resource)
4921{
4922 wl_resource_destroy(resource);
4923}
4924
4925static const struct zwp_input_timestamps_v1_interface
4926 input_timestamps_interface = {
4927 input_timestamps_destroy,
4928};
4929
4930static void
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004931input_timestamps_manager_destroy(struct wl_client *client,
4932 struct wl_resource *resource)
4933{
4934 wl_resource_destroy(resource);
4935}
4936
4937static void
4938input_timestamps_manager_get_keyboard_timestamps(struct wl_client *client,
4939 struct wl_resource *resource,
4940 uint32_t id,
4941 struct wl_resource *keyboard_resource)
4942{
Alexandros Frantzis2b442482018-02-20 14:05:50 +02004943 struct weston_keyboard *keyboard =
4944 wl_resource_get_user_data(keyboard_resource);
4945 struct wl_resource *input_ts;
4946
4947 input_ts = wl_resource_create(client,
4948 &zwp_input_timestamps_v1_interface,
4949 1, id);
4950 if (!input_ts) {
4951 wl_client_post_no_memory(client);
4952 return;
4953 }
4954
4955 if (keyboard) {
4956 wl_list_insert(&keyboard->timestamps_list,
4957 wl_resource_get_link(input_ts));
4958 } else {
4959 wl_list_init(wl_resource_get_link(input_ts));
4960 }
4961
4962 wl_resource_set_implementation(input_ts,
4963 &input_timestamps_interface,
4964 keyboard_resource,
4965 unbind_resource);
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004966}
4967
4968static void
4969input_timestamps_manager_get_pointer_timestamps(struct wl_client *client,
4970 struct wl_resource *resource,
4971 uint32_t id,
4972 struct wl_resource *pointer_resource)
4973{
Alexandros Frantzisdb907b72018-02-20 14:06:26 +02004974 struct weston_pointer *pointer =
4975 wl_resource_get_user_data(pointer_resource);
4976 struct wl_resource *input_ts;
4977
4978 input_ts = wl_resource_create(client,
4979 &zwp_input_timestamps_v1_interface,
4980 1, id);
4981 if (!input_ts) {
4982 wl_client_post_no_memory(client);
4983 return;
4984 }
4985
4986 if (pointer) {
4987 wl_list_insert(&pointer->timestamps_list,
4988 wl_resource_get_link(input_ts));
4989 } else {
4990 wl_list_init(wl_resource_get_link(input_ts));
4991 }
4992
4993 wl_resource_set_implementation(input_ts,
4994 &input_timestamps_interface,
4995 pointer_resource,
4996 unbind_resource);
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004997}
4998
4999static void
5000input_timestamps_manager_get_touch_timestamps(struct wl_client *client,
5001 struct wl_resource *resource,
5002 uint32_t id,
5003 struct wl_resource *touch_resource)
5004{
Alexandros Frantzisd7157842018-02-20 14:07:03 +02005005 struct weston_touch *touch = wl_resource_get_user_data(touch_resource);
5006 struct wl_resource *input_ts;
5007
5008 input_ts = wl_resource_create(client,
5009 &zwp_input_timestamps_v1_interface,
5010 1, id);
5011 if (!input_ts) {
5012 wl_client_post_no_memory(client);
5013 return;
5014 }
5015
5016 if (touch) {
5017 wl_list_insert(&touch->timestamps_list,
5018 wl_resource_get_link(input_ts));
5019 } else {
5020 wl_list_init(wl_resource_get_link(input_ts));
5021 }
5022
5023 wl_resource_set_implementation(input_ts,
5024 &input_timestamps_interface,
5025 touch_resource,
5026 unbind_resource);
Alexandros Frantzis538749d2018-02-16 18:44:16 +02005027}
5028
5029static const struct zwp_input_timestamps_manager_v1_interface
5030 input_timestamps_manager_interface = {
5031 input_timestamps_manager_destroy,
5032 input_timestamps_manager_get_keyboard_timestamps,
5033 input_timestamps_manager_get_pointer_timestamps,
5034 input_timestamps_manager_get_touch_timestamps,
5035};
5036
5037static void
5038bind_input_timestamps_manager(struct wl_client *client, void *data,
5039 uint32_t version, uint32_t id)
5040{
5041 struct wl_resource *resource =
5042 wl_resource_create(client,
5043 &zwp_input_timestamps_manager_v1_interface,
5044 1, id);
5045
5046 if (resource == NULL) {
5047 wl_client_post_no_memory(client);
5048 return;
5049 }
5050
5051 wl_resource_set_implementation(resource,
5052 &input_timestamps_manager_interface,
5053 NULL, NULL);
5054}
5055
Jonas Ådahl30d61d82014-10-22 21:21:17 +02005056int
5057weston_input_init(struct weston_compositor *compositor)
5058{
5059 if (!wl_global_create(compositor->wl_display,
5060 &zwp_relative_pointer_manager_v1_interface, 1,
5061 compositor, bind_relative_pointer_manager))
5062 return -1;
5063
Jonas Ådahld3414f22016-07-22 17:56:31 +08005064 if (!wl_global_create(compositor->wl_display,
5065 &zwp_pointer_constraints_v1_interface, 1,
5066 NULL, bind_pointer_constraints))
5067 return -1;
5068
Alexandros Frantzis538749d2018-02-16 18:44:16 +02005069 if (!wl_global_create(compositor->wl_display,
5070 &zwp_input_timestamps_manager_v1_interface, 1,
5071 NULL, bind_input_timestamps_manager))
5072 return -1;
5073
Jonas Ådahl30d61d82014-10-22 21:21:17 +02005074 return 0;
5075}