blob: 632c9c3c15ea941d7288c3494291a5ec5aaeaf77 [file] [log] [blame]
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001/*
2 * Copyright © 2013 Intel Corporation
3 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07004 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
Kristian Høgsberg2158a882013-04-18 15:07:39 -040011 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070012 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
Kristian Høgsberg2158a882013-04-18 15:07:39 -040024 */
25
Daniel Stone8e7a8bd2013-08-15 01:10:24 +010026#include "config.h"
27
Jonas Ådahld3414f22016-07-22 17:56:31 +080028#include <stdbool.h>
Kristian Høgsberg2158a882013-04-18 15:07:39 -040029#include <stdlib.h>
30#include <stdint.h>
31#include <string.h>
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040032#include <sys/mman.h>
33#include <assert.h>
34#include <unistd.h>
Jonas Ådahld0be2bb2015-04-30 17:56:37 +080035#include <values.h>
Matt Roper01a92732013-06-24 16:52:44 +010036#include <fcntl.h>
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +020037#include <limits.h>
Kristian Høgsberg2158a882013-04-18 15:07:39 -040038
Jon Cruz35b2eaa2015-06-15 15:37:08 -070039#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070040#include "shared/os-compatibility.h"
Alexandros Frantzis84b31f82017-11-24 18:01:46 +020041#include "shared/timespec-util.h"
Kristian Høgsberg2158a882013-04-18 15:07:39 -040042#include "compositor.h"
Daniel Stone7dbb0e12016-11-24 15:30:41 +000043#include "relative-pointer-unstable-v1-server-protocol.h"
44#include "pointer-constraints-unstable-v1-server-protocol.h"
Alexandros Frantzis538749d2018-02-16 18:44:16 +020045#include "input-timestamps-unstable-v1-server-protocol.h"
Jonas Ådahld3414f22016-07-22 17:56:31 +080046
47enum pointer_constraint_type {
48 POINTER_CONSTRAINT_TYPE_LOCK,
49 POINTER_CONSTRAINT_TYPE_CONFINE,
50};
51
Jonas Ådahld0be2bb2015-04-30 17:56:37 +080052enum motion_direction {
53 MOTION_DIRECTION_POSITIVE_X = 1 << 0,
54 MOTION_DIRECTION_NEGATIVE_X = 1 << 1,
55 MOTION_DIRECTION_POSITIVE_Y = 1 << 2,
56 MOTION_DIRECTION_NEGATIVE_Y = 1 << 3,
57};
58
59struct vec2d {
60 double x, y;
61};
62
63struct line {
64 struct vec2d a;
65 struct vec2d b;
66};
67
68struct border {
69 struct line line;
70 enum motion_direction blocking_dir;
71};
72
Jonas Ådahld3414f22016-07-22 17:56:31 +080073static void
74maybe_warp_confined_pointer(struct weston_pointer_constraint *constraint);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040075
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040076static void
77empty_region(pixman_region32_t *region)
78{
79 pixman_region32_fini(region);
80 pixman_region32_init(region);
81}
82
Jonas Ådahld3414f22016-07-22 17:56:31 +080083static void
84region_init_infinite(pixman_region32_t *region)
85{
86 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
87 UINT32_MAX, UINT32_MAX);
88}
89
Alexandros Frantzis2b442482018-02-20 14:05:50 +020090static void
91send_timestamp(struct wl_resource *resource,
92 const struct timespec *time)
93{
94 uint32_t tv_sec_hi, tv_sec_lo, tv_nsec;
95
96 timespec_to_proto(time, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
97 zwp_input_timestamps_v1_send_timestamp(resource, tv_sec_hi, tv_sec_lo,
98 tv_nsec);
99}
100
101static void
102send_timestamps_for_input_resource(struct wl_resource *input_resource,
103 struct wl_list *list,
104 const struct timespec *time)
105{
106 struct wl_resource *resource;
107
108 wl_resource_for_each(resource, list) {
109 if (wl_resource_get_user_data(resource) == input_resource)
110 send_timestamp(resource, time);
111 }
112}
113
114static void
115remove_input_resource_from_timestamps(struct wl_resource *input_resource,
116 struct wl_list *list)
117{
118 struct wl_resource *resource;
119
120 wl_resource_for_each(resource, list) {
121 if (wl_resource_get_user_data(resource) == input_resource)
122 wl_resource_set_user_data(resource, NULL);
123 }
124}
125
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800126static struct weston_pointer_client *
127weston_pointer_client_create(struct wl_client *client)
128{
129 struct weston_pointer_client *pointer_client;
130
131 pointer_client = zalloc(sizeof *pointer_client);
132 if (!pointer_client)
133 return NULL;
134
135 pointer_client->client = client;
136 wl_list_init(&pointer_client->pointer_resources);
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200137 wl_list_init(&pointer_client->relative_pointer_resources);
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800138
139 return pointer_client;
140}
141
142static void
143weston_pointer_client_destroy(struct weston_pointer_client *pointer_client)
144{
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +0200145 struct wl_resource *resource;
146
147 wl_resource_for_each(resource, &pointer_client->pointer_resources) {
148 wl_resource_set_user_data(resource, NULL);
149 }
150
151 wl_resource_for_each(resource,
152 &pointer_client->relative_pointer_resources) {
153 wl_resource_set_user_data(resource, NULL);
154 }
155
156 wl_list_remove(&pointer_client->pointer_resources);
157 wl_list_remove(&pointer_client->relative_pointer_resources);
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800158 free(pointer_client);
159}
160
161static bool
162weston_pointer_client_is_empty(struct weston_pointer_client *pointer_client)
163{
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200164 return (wl_list_empty(&pointer_client->pointer_resources) &&
165 wl_list_empty(&pointer_client->relative_pointer_resources));
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800166}
167
168static struct weston_pointer_client *
169weston_pointer_get_pointer_client(struct weston_pointer *pointer,
170 struct wl_client *client)
171{
172 struct weston_pointer_client *pointer_client;
173
174 wl_list_for_each(pointer_client, &pointer->pointer_clients, link) {
175 if (pointer_client->client == client)
176 return pointer_client;
177 }
178
179 return NULL;
180}
181
182static struct weston_pointer_client *
183weston_pointer_ensure_pointer_client(struct weston_pointer *pointer,
184 struct wl_client *client)
185{
186 struct weston_pointer_client *pointer_client;
187
188 pointer_client = weston_pointer_get_pointer_client(pointer, client);
189 if (pointer_client)
190 return pointer_client;
191
192 pointer_client = weston_pointer_client_create(client);
193 wl_list_insert(&pointer->pointer_clients, &pointer_client->link);
194
195 if (pointer->focus &&
196 pointer->focus->surface->resource &&
197 wl_resource_get_client(pointer->focus->surface->resource) == client) {
198 pointer->focus_client = pointer_client;
199 }
200
201 return pointer_client;
202}
203
204static void
205weston_pointer_cleanup_pointer_client(struct weston_pointer *pointer,
206 struct weston_pointer_client *pointer_client)
207{
208 if (weston_pointer_client_is_empty(pointer_client)) {
209 if (pointer->focus_client == pointer_client)
210 pointer->focus_client = NULL;
211 wl_list_remove(&pointer_client->link);
212 weston_pointer_client_destroy(pointer_client);
213 }
214}
215
216static void
217unbind_pointer_client_resource(struct wl_resource *resource)
218{
219 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
220 struct wl_client *client = wl_resource_get_client(resource);
221 struct weston_pointer_client *pointer_client;
222
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800223 wl_list_remove(wl_resource_get_link(resource));
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +0200224
225 if (pointer) {
226 pointer_client = weston_pointer_get_pointer_client(pointer,
227 client);
228 assert(pointer_client);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200229 remove_input_resource_from_timestamps(resource,
230 &pointer->timestamps_list);
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +0200231 weston_pointer_cleanup_pointer_client(pointer, pointer_client);
232 }
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800233}
234
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400235static void unbind_resource(struct wl_resource *resource)
236{
Jason Ekstrand44a38632013-06-14 10:08:00 -0500237 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400238}
239
Jonas Ådahl3042ffe2013-10-17 23:04:08 +0200240WL_EXPORT void
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200241weston_pointer_motion_to_abs(struct weston_pointer *pointer,
242 struct weston_pointer_motion_event *event,
243 wl_fixed_t *x, wl_fixed_t *y)
244{
245 if (event->mask & WESTON_POINTER_MOTION_ABS) {
246 *x = wl_fixed_from_double(event->x);
247 *y = wl_fixed_from_double(event->y);
248 } else if (event->mask & WESTON_POINTER_MOTION_REL) {
249 *x = pointer->x + wl_fixed_from_double(event->dx);
250 *y = pointer->y + wl_fixed_from_double(event->dy);
251 } else {
252 assert(!"invalid motion event");
253 *x = *y = 0;
254 }
255}
256
257static bool
258weston_pointer_motion_to_rel(struct weston_pointer *pointer,
259 struct weston_pointer_motion_event *event,
260 double *dx, double *dy,
261 double *dx_unaccel, double *dy_unaccel)
262{
263 if (event->mask & WESTON_POINTER_MOTION_REL &&
264 event->mask & WESTON_POINTER_MOTION_REL_UNACCEL) {
265 *dx = event->dx;
266 *dy = event->dy;
267 *dx_unaccel = event->dx_unaccel;
268 *dy_unaccel = event->dy_unaccel;
269 return true;
270 } else if (event->mask & WESTON_POINTER_MOTION_REL) {
271 *dx_unaccel = *dx = event->dx;
272 *dy_unaccel = *dy = event->dy;
273 return true;
274 } else if (event->mask & WESTON_POINTER_MOTION_REL_UNACCEL) {
275 *dx_unaccel = *dx = event->dx_unaccel;
276 *dy_unaccel = *dy = event->dy_unaccel;
277 return true;
278 } else {
279 return false;
280 }
281}
282
283WL_EXPORT void
Kristian Høgsberga71e8b22013-05-06 21:51:21 -0400284weston_seat_repick(struct weston_seat *seat)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400285{
Derek Foreman1281a362015-07-31 16:55:32 -0500286 const struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400287
Derek Foreman1b786ee2015-06-03 15:53:23 -0500288 if (!pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400289 return;
290
Derek Foreman1b786ee2015-06-03 15:53:23 -0500291 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400292}
293
294static void
295weston_compositor_idle_inhibit(struct weston_compositor *compositor)
296{
297 weston_compositor_wake(compositor);
298 compositor->idle_inhibit++;
299}
300
301static void
302weston_compositor_idle_release(struct weston_compositor *compositor)
303{
304 compositor->idle_inhibit--;
305 weston_compositor_wake(compositor);
306}
307
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400308static void
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100309pointer_focus_view_destroyed(struct wl_listener *listener, void *data)
310{
311 struct weston_pointer *pointer =
312 container_of(listener, struct weston_pointer,
313 focus_view_listener);
314
Derek Foremanf9318d12015-05-11 15:40:11 -0500315 weston_pointer_clear_focus(pointer);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100316}
317
318static void
319pointer_focus_resource_destroyed(struct wl_listener *listener, void *data)
320{
321 struct weston_pointer *pointer =
322 container_of(listener, struct weston_pointer,
323 focus_resource_listener);
324
Derek Foremanf9318d12015-05-11 15:40:11 -0500325 weston_pointer_clear_focus(pointer);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100326}
327
328static void
329keyboard_focus_resource_destroyed(struct wl_listener *listener, void *data)
330{
331 struct weston_keyboard *keyboard =
332 container_of(listener, struct weston_keyboard,
333 focus_resource_listener);
334
335 weston_keyboard_set_focus(keyboard, NULL);
336}
337
338static void
339touch_focus_view_destroyed(struct wl_listener *listener, void *data)
340{
341 struct weston_touch *touch =
342 container_of(listener, struct weston_touch,
343 focus_view_listener);
344
Derek Foreman4c93c082015-04-30 16:45:41 -0500345 weston_touch_set_focus(touch, NULL);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100346}
347
348static void
349touch_focus_resource_destroyed(struct wl_listener *listener, void *data)
350{
351 struct weston_touch *touch =
352 container_of(listener, struct weston_touch,
353 focus_resource_listener);
354
Derek Foreman4c93c082015-04-30 16:45:41 -0500355 weston_touch_set_focus(touch, NULL);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100356}
357
358static void
Neil Roberts96d790e2013-09-19 17:32:00 +0100359move_resources(struct wl_list *destination, struct wl_list *source)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400360{
Neil Roberts96d790e2013-09-19 17:32:00 +0100361 wl_list_insert_list(destination, source);
362 wl_list_init(source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400363}
364
365static void
Neil Roberts96d790e2013-09-19 17:32:00 +0100366move_resources_for_client(struct wl_list *destination,
367 struct wl_list *source,
368 struct wl_client *client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400369{
Neil Roberts96d790e2013-09-19 17:32:00 +0100370 struct wl_resource *resource, *tmp;
371 wl_resource_for_each_safe(resource, tmp, source) {
372 if (wl_resource_get_client(resource) == client) {
373 wl_list_remove(wl_resource_get_link(resource));
374 wl_list_insert(destination,
375 wl_resource_get_link(resource));
376 }
377 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400378}
379
380static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700381default_grab_pointer_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400382{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400383 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500384 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400385 wl_fixed_t sx, sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400386
387 if (pointer->button_count > 0)
388 return;
389
Jason Ekstranda7af7042013-10-12 22:38:11 -0500390 view = weston_compositor_pick_view(pointer->seat->compositor,
391 pointer->x, pointer->y,
392 &sx, &sy);
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400393
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -0800394 if (pointer->focus != view || pointer->sx != sx || pointer->sy != sy)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500395 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400396}
397
398static void
Quentin Glidiccde13452016-08-12 10:41:32 +0200399pointer_send_relative_motion(struct weston_pointer *pointer,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200400 const struct timespec *time,
Quentin Glidiccde13452016-08-12 10:41:32 +0200401 struct weston_pointer_motion_event *event)
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200402{
403 uint64_t time_usec;
404 double dx, dy, dx_unaccel, dy_unaccel;
405 wl_fixed_t dxf, dyf, dxf_unaccel, dyf_unaccel;
406 struct wl_list *resource_list;
407 struct wl_resource *resource;
408
409 if (!pointer->focus_client)
410 return;
411
412 if (!weston_pointer_motion_to_rel(pointer, event,
413 &dx, &dy,
414 &dx_unaccel, &dy_unaccel))
415 return;
416
417 resource_list = &pointer->focus_client->relative_pointer_resources;
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200418 time_usec = timespec_to_usec(&event->time);
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200419 if (time_usec == 0)
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200420 time_usec = timespec_to_usec(time);
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200421
422 dxf = wl_fixed_from_double(dx);
423 dyf = wl_fixed_from_double(dy);
424 dxf_unaccel = wl_fixed_from_double(dx_unaccel);
425 dyf_unaccel = wl_fixed_from_double(dy_unaccel);
426
427 wl_resource_for_each(resource, resource_list) {
428 zwp_relative_pointer_v1_send_relative_motion(
429 resource,
430 (uint32_t) (time_usec >> 32),
431 (uint32_t) time_usec,
432 dxf, dyf,
433 dxf_unaccel, dyf_unaccel);
434 }
435}
436
437static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200438pointer_send_motion(struct weston_pointer *pointer,
439 const struct timespec *time,
Quentin Glidiccde13452016-08-12 10:41:32 +0200440 wl_fixed_t sx, wl_fixed_t sy)
Jonas Ådahlf44942e2016-07-22 17:54:55 +0800441{
442 struct wl_list *resource_list;
443 struct wl_resource *resource;
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200444 uint32_t msecs;
Jonas Ådahlf44942e2016-07-22 17:54:55 +0800445
446 if (!pointer->focus_client)
447 return;
448
449 resource_list = &pointer->focus_client->pointer_resources;
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200450 msecs = timespec_to_msec(time);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200451 wl_resource_for_each(resource, resource_list) {
452 send_timestamps_for_input_resource(resource,
453 &pointer->timestamps_list,
454 time);
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200455 wl_pointer_send_motion(resource, msecs, sx, sy);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200456 }
Jonas Ådahlf44942e2016-07-22 17:54:55 +0800457}
458
Quentin Glidiccde13452016-08-12 10:41:32 +0200459WL_EXPORT void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200460weston_pointer_send_motion(struct weston_pointer *pointer,
461 const struct timespec *time,
Quentin Glidiccde13452016-08-12 10:41:32 +0200462 struct weston_pointer_motion_event *event)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400463{
Jonas Ådahld2510102014-10-05 21:39:14 +0200464 wl_fixed_t x, y;
Jonas Ådahl8283c342015-04-24 15:26:17 +0800465 wl_fixed_t old_sx = pointer->sx;
466 wl_fixed_t old_sy = pointer->sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400467
Jonas Ådahld2510102014-10-05 21:39:14 +0200468 if (pointer->focus) {
469 weston_pointer_motion_to_abs(pointer, event, &x, &y);
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -0800470 weston_view_from_global_fixed(pointer->focus, x, y,
471 &pointer->sx, &pointer->sy);
Jonas Ådahld2510102014-10-05 21:39:14 +0200472 }
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -0800473
Jonas Ådahld2510102014-10-05 21:39:14 +0200474 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100475
Jonas Ådahlf44942e2016-07-22 17:54:55 +0800476 if (old_sx != pointer->sx || old_sy != pointer->sy) {
Quentin Glidiccde13452016-08-12 10:41:32 +0200477 pointer_send_motion(pointer, time,
478 pointer->sx, pointer->sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400479 }
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200480
Quentin Glidiccde13452016-08-12 10:41:32 +0200481 pointer_send_relative_motion(pointer, time, event);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400482}
483
484static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200485default_grab_pointer_motion(struct weston_pointer_grab *grab,
486 const struct timespec *time,
Quentin Glidiccde13452016-08-12 10:41:32 +0200487 struct weston_pointer_motion_event *event)
488{
489 weston_pointer_send_motion(grab->pointer, time, event);
490}
491
492/** Check if the pointer has focused resources.
493 *
494 * \param pointer The pointer to check for focused resources.
495 * \return Whether or not this pointer has focused resources
496 */
497WL_EXPORT bool
498weston_pointer_has_focus_resource(struct weston_pointer *pointer)
499{
500 if (!pointer->focus_client)
501 return false;
502
503 if (wl_list_empty(&pointer->focus_client->pointer_resources))
504 return false;
505
506 return true;
507}
508
509/** Send wl_pointer.button events to focused resources.
510 *
511 * \param pointer The pointer where the button events originates from.
512 * \param time The timestamp of the event
513 * \param button The button value of the event
514 * \param value The state enum value of the event
515 *
516 * For every resource that is currently in focus, send a wl_pointer.button event
517 * with the passed parameters. The focused resources are the wl_pointer
518 * resources of the client which currently has the surface with pointer focus.
519 */
520WL_EXPORT void
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800521weston_pointer_send_button(struct weston_pointer *pointer,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200522 const struct timespec *time, uint32_t button,
Quentin Glidiccde13452016-08-12 10:41:32 +0200523 enum wl_pointer_button_state state)
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800524{
525 struct wl_display *display = pointer->seat->compositor->wl_display;
526 struct wl_list *resource_list;
527 struct wl_resource *resource;
528 uint32_t serial;
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200529 uint32_t msecs;
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800530
Quentin Glidiccde13452016-08-12 10:41:32 +0200531 if (!weston_pointer_has_focus_resource(pointer))
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800532 return;
533
534 resource_list = &pointer->focus_client->pointer_resources;
Quentin Glidiccde13452016-08-12 10:41:32 +0200535 serial = wl_display_next_serial(display);
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200536 msecs = timespec_to_msec(time);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200537 wl_resource_for_each(resource, resource_list) {
538 send_timestamps_for_input_resource(resource,
539 &pointer->timestamps_list,
540 time);
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200541 wl_pointer_send_button(resource, serial, msecs, button, state);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200542 }
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800543}
544
545static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700546default_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200547 const struct timespec *time, uint32_t button,
Quentin Glidiccde13452016-08-12 10:41:32 +0200548 enum wl_pointer_button_state state)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400549{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400550 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400551 struct weston_compositor *compositor = pointer->seat->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500552 struct weston_view *view;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400553 wl_fixed_t sx, sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400554
Quentin Glidiccde13452016-08-12 10:41:32 +0200555 weston_pointer_send_button(pointer, time, button, state);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400556
557 if (pointer->button_count == 0 &&
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400558 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500559 view = weston_compositor_pick_view(compositor,
560 pointer->x, pointer->y,
561 &sx, &sy);
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400562
Jason Ekstranda7af7042013-10-12 22:38:11 -0500563 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400564 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400565}
566
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200567/** Send wl_pointer.axis events to focused resources.
568 *
569 * \param pointer The pointer where the axis events originates from.
570 * \param time The timestamp of the event
571 * \param axis The axis enum value of the event
572 * \param value The axis value of the event
573 *
574 * For every resource that is currently in focus, send a wl_pointer.axis event
575 * with the passed parameters. The focused resources are the wl_pointer
576 * resources of the client which currently has the surface with pointer focus.
577 */
578WL_EXPORT void
579weston_pointer_send_axis(struct weston_pointer *pointer,
Alexandros Frantzis80321942017-11-16 18:20:56 +0200580 const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +1000581 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200582{
583 struct wl_resource *resource;
584 struct wl_list *resource_list;
Alexandros Frantzis80321942017-11-16 18:20:56 +0200585 uint32_t msecs;
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200586
Quentin Glidiccde13452016-08-12 10:41:32 +0200587 if (!weston_pointer_has_focus_resource(pointer))
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800588 return;
589
590 resource_list = &pointer->focus_client->pointer_resources;
Alexandros Frantzis80321942017-11-16 18:20:56 +0200591 msecs = timespec_to_msec(time);
Peter Hutterer87743e92016-01-18 16:38:22 +1000592 wl_resource_for_each(resource, resource_list) {
593 if (event->has_discrete &&
594 wl_resource_get_version(resource) >=
595 WL_POINTER_AXIS_DISCRETE_SINCE_VERSION)
596 wl_pointer_send_axis_discrete(resource, event->axis,
597 event->discrete);
598
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200599 if (event->value) {
600 send_timestamps_for_input_resource(resource,
601 &pointer->timestamps_list,
602 time);
Alexandros Frantzis80321942017-11-16 18:20:56 +0200603 wl_pointer_send_axis(resource, msecs,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200604 event->axis,
605 wl_fixed_from_double(event->value));
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200606 } else if (wl_resource_get_version(resource) >=
607 WL_POINTER_AXIS_STOP_SINCE_VERSION) {
608 send_timestamps_for_input_resource(resource,
609 &pointer->timestamps_list,
610 time);
Alexandros Frantzis80321942017-11-16 18:20:56 +0200611 wl_pointer_send_axis_stop(resource, msecs,
Peter Hutterer87743e92016-01-18 16:38:22 +1000612 event->axis);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200613 }
Peter Hutterer87743e92016-01-18 16:38:22 +1000614 }
615}
616
Quentin Glidiccde13452016-08-12 10:41:32 +0200617/** Send wl_pointer.axis_source events to focused resources.
618 *
619 * \param pointer The pointer where the axis_source events originates from.
620 * \param source The axis_source enum value of the event
621 *
622 * For every resource that is currently in focus, send a wl_pointer.axis_source
623 * event with the passed parameter. The focused resources are the wl_pointer
624 * resources of the client which currently has the surface with pointer focus.
625 */
Peter Hutterer87743e92016-01-18 16:38:22 +1000626WL_EXPORT void
Quentin Glidiccde13452016-08-12 10:41:32 +0200627weston_pointer_send_axis_source(struct weston_pointer *pointer,
628 enum wl_pointer_axis_source source)
Peter Hutterer87743e92016-01-18 16:38:22 +1000629{
630 struct wl_resource *resource;
631 struct wl_list *resource_list;
632
Quentin Glidiccde13452016-08-12 10:41:32 +0200633 if (!weston_pointer_has_focus_resource(pointer))
Jonas Ådahled6014a2016-04-21 10:21:48 +0800634 return;
635
Peter Hutterer87743e92016-01-18 16:38:22 +1000636 resource_list = &pointer->focus_client->pointer_resources;
637 wl_resource_for_each(resource, resource_list) {
638 if (wl_resource_get_version(resource) >=
639 WL_POINTER_AXIS_SOURCE_SINCE_VERSION) {
640 wl_pointer_send_axis_source(resource, source);
641 }
642 }
643}
644
645static void
646pointer_send_frame(struct wl_resource *resource)
647{
648 if (wl_resource_get_version(resource) >=
649 WL_POINTER_FRAME_SINCE_VERSION) {
650 wl_pointer_send_frame(resource);
651 }
652}
653
Quentin Glidiccde13452016-08-12 10:41:32 +0200654/** Send wl_pointer.frame events to focused resources.
655 *
656 * \param pointer The pointer where the frame events originates from.
657 *
658 * For every resource that is currently in focus, send a wl_pointer.frame event.
659 * The focused resources are the wl_pointer resources of the client which
660 * currently has the surface with pointer focus.
661 */
Peter Hutterer87743e92016-01-18 16:38:22 +1000662WL_EXPORT void
663weston_pointer_send_frame(struct weston_pointer *pointer)
664{
665 struct wl_resource *resource;
666 struct wl_list *resource_list;
667
Quentin Glidiccde13452016-08-12 10:41:32 +0200668 if (!weston_pointer_has_focus_resource(pointer))
Derek Foreman8efa31b2016-01-29 10:29:46 -0600669 return;
670
Peter Hutterer87743e92016-01-18 16:38:22 +1000671 resource_list = &pointer->focus_client->pointer_resources;
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200672 wl_resource_for_each(resource, resource_list)
Peter Hutterer87743e92016-01-18 16:38:22 +1000673 pointer_send_frame(resource);
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200674}
675
676static void
677default_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +0200678 const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +1000679 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200680{
Peter Hutterer89b6a492016-01-18 15:58:17 +1000681 weston_pointer_send_axis(grab->pointer, time, event);
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200682}
683
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200684static void
Peter Hutterer87743e92016-01-18 16:38:22 +1000685default_grab_pointer_axis_source(struct weston_pointer_grab *grab,
Quentin Glidiccde13452016-08-12 10:41:32 +0200686 enum wl_pointer_axis_source source)
Peter Hutterer87743e92016-01-18 16:38:22 +1000687{
688 weston_pointer_send_axis_source(grab->pointer, source);
689}
690
691static void
692default_grab_pointer_frame(struct weston_pointer_grab *grab)
693{
694 weston_pointer_send_frame(grab->pointer);
695}
696
697static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200698default_grab_pointer_cancel(struct weston_pointer_grab *grab)
699{
700}
701
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400702static const struct weston_pointer_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400703 default_pointer_grab_interface = {
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700704 default_grab_pointer_focus,
705 default_grab_pointer_motion,
706 default_grab_pointer_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200707 default_grab_pointer_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +1000708 default_grab_pointer_axis_source,
709 default_grab_pointer_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200710 default_grab_pointer_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400711};
712
Quentin Glidiccde13452016-08-12 10:41:32 +0200713/** Check if the touch has focused resources.
714 *
715 * \param touch The touch to check for focused resources.
716 * \return Whether or not this touch has focused resources
717 */
718WL_EXPORT bool
719weston_touch_has_focus_resource(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400720{
Quentin Glidiccde13452016-08-12 10:41:32 +0200721 if (!touch->focus)
722 return false;
723
724 if (wl_list_empty(&touch->focus_resource_list))
725 return false;
726
727 return true;
728}
729
730/** Send wl_touch.down events to focused resources.
731 *
732 * \param touch The touch where the down events originates from.
733 * \param time The timestamp of the event
734 * \param touch_id The touch_id value of the event
735 * \param x The x value of the event
736 * \param y The y value of the event
737 *
738 * For every resource that is currently in focus, send a wl_touch.down event
739 * with the passed parameters. The focused resources are the wl_touch
740 * resources of the client which currently has the surface with touch focus.
741 */
742WL_EXPORT void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200743weston_touch_send_down(struct weston_touch *touch, const struct timespec *time,
Quentin Glidiccde13452016-08-12 10:41:32 +0200744 int touch_id, wl_fixed_t x, wl_fixed_t y)
745{
Rob Bradford880ebc72013-07-22 17:31:38 +0100746 struct wl_display *display = touch->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400747 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100748 struct wl_resource *resource;
749 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300750 wl_fixed_t sx, sy;
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200751 uint32_t msecs;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300752
Quentin Glidiccde13452016-08-12 10:41:32 +0200753 if (!weston_touch_has_focus_resource(touch))
Bryce Harrington2c40d1d2016-02-02 10:18:48 -0800754 return;
755
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300756 weston_view_from_global_fixed(touch->focus, x, y, &sx, &sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400757
Neil Roberts96d790e2013-09-19 17:32:00 +0100758 resource_list = &touch->focus_resource_list;
Quentin Glidiccde13452016-08-12 10:41:32 +0200759 serial = wl_display_next_serial(display);
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200760 msecs = timespec_to_msec(time);
Quentin Glidiccde13452016-08-12 10:41:32 +0200761 wl_resource_for_each(resource, resource_list)
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200762 wl_touch_send_down(resource, serial, msecs,
Quentin Glidiccde13452016-08-12 10:41:32 +0200763 touch->focus->surface->resource,
764 touch_id, sx, sy);
765}
Neil Roberts96d790e2013-09-19 17:32:00 +0100766
Quentin Glidiccde13452016-08-12 10:41:32 +0200767static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200768default_grab_touch_down(struct weston_touch_grab *grab,
769 const struct timespec *time, int touch_id,
770 wl_fixed_t x, wl_fixed_t y)
Quentin Glidiccde13452016-08-12 10:41:32 +0200771{
772 weston_touch_send_down(grab->touch, time, touch_id, x, y);
773}
774
775/** Send wl_touch.up events to focused resources.
776 *
777 * \param touch The touch where the up events originates from.
778 * \param time The timestamp of the event
779 * \param touch_id The touch_id value of the event
780 *
781 * For every resource that is currently in focus, send a wl_touch.up event
782 * with the passed parameters. The focused resources are the wl_touch
783 * resources of the client which currently has the surface with touch focus.
784 */
785WL_EXPORT void
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200786weston_touch_send_up(struct weston_touch *touch, const struct timespec *time,
787 int touch_id)
Quentin Glidiccde13452016-08-12 10:41:32 +0200788{
789 struct wl_display *display = touch->seat->compositor->wl_display;
790 uint32_t serial;
791 struct wl_resource *resource;
792 struct wl_list *resource_list;
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200793 uint32_t msecs;
Quentin Glidiccde13452016-08-12 10:41:32 +0200794
795 if (!weston_touch_has_focus_resource(touch))
796 return;
797
798 resource_list = &touch->focus_resource_list;
799 serial = wl_display_next_serial(display);
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200800 msecs = timespec_to_msec(time);
Quentin Glidiccde13452016-08-12 10:41:32 +0200801 wl_resource_for_each(resource, resource_list)
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200802 wl_touch_send_up(resource, serial, msecs, touch_id);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400803}
804
Kristian Høgsberge329f362013-05-06 22:19:57 -0400805static void
806default_grab_touch_up(struct weston_touch_grab *grab,
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200807 const struct timespec *time, int touch_id)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400808{
Quentin Glidiccde13452016-08-12 10:41:32 +0200809 weston_touch_send_up(grab->touch, time, touch_id);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400810}
811
Quentin Glidiccde13452016-08-12 10:41:32 +0200812/** Send wl_touch.motion events to focused resources.
813 *
814 * \param touch The touch where the motion events originates from.
815 * \param time The timestamp of the event
816 * \param touch_id The touch_id value of the event
817 * \param x The x value of the event
818 * \param y The y value of the event
819 *
820 * For every resource that is currently in focus, send a wl_touch.motion event
821 * with the passed parameters. The focused resources are the wl_touch
822 * resources of the client which currently has the surface with touch focus.
823 */
824WL_EXPORT void
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200825weston_touch_send_motion(struct weston_touch *touch,
826 const struct timespec *time, int touch_id,
827 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400828{
Neil Roberts96d790e2013-09-19 17:32:00 +0100829 struct wl_resource *resource;
830 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300831 wl_fixed_t sx, sy;
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200832 uint32_t msecs;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300833
Quentin Glidiccde13452016-08-12 10:41:32 +0200834 if (!weston_touch_has_focus_resource(touch))
835 return;
836
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300837 weston_view_from_global_fixed(touch->focus, x, y, &sx, &sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400838
Neil Roberts96d790e2013-09-19 17:32:00 +0100839 resource_list = &touch->focus_resource_list;
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200840 msecs = timespec_to_msec(time);
Neil Roberts96d790e2013-09-19 17:32:00 +0100841 wl_resource_for_each(resource, resource_list) {
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200842 wl_touch_send_motion(resource, msecs,
Kristian Høgsberge329f362013-05-06 22:19:57 -0400843 touch_id, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400844 }
845}
846
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200847static void
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200848default_grab_touch_motion(struct weston_touch_grab *grab,
849 const struct timespec *time, int touch_id,
850 wl_fixed_t x, wl_fixed_t y)
Quentin Glidiccde13452016-08-12 10:41:32 +0200851{
852 weston_touch_send_motion(grab->touch, time, touch_id, x, y);
853}
854
855
856/** Send wl_touch.frame events to focused resources.
857 *
858 * \param touch The touch where the frame events originates from.
859 *
860 * For every resource that is currently in focus, send a wl_touch.frame event.
861 * The focused resources are the wl_touch resources of the client which
862 * currently has the surface with touch focus.
863 */
864WL_EXPORT void
865weston_touch_send_frame(struct weston_touch *touch)
Jonas Ådahl1679f232014-04-12 09:39:51 +0200866{
867 struct wl_resource *resource;
868
Quentin Glidiccde13452016-08-12 10:41:32 +0200869 if (!weston_touch_has_focus_resource(touch))
870 return;
871
872 wl_resource_for_each(resource, &touch->focus_resource_list)
Jonas Ådahl1679f232014-04-12 09:39:51 +0200873 wl_touch_send_frame(resource);
874}
875
876static void
Quentin Glidiccde13452016-08-12 10:41:32 +0200877default_grab_touch_frame(struct weston_touch_grab *grab)
878{
879 weston_touch_send_frame(grab->touch);
880}
881
882static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200883default_grab_touch_cancel(struct weston_touch_grab *grab)
884{
885}
886
Kristian Høgsberge329f362013-05-06 22:19:57 -0400887static const struct weston_touch_grab_interface default_touch_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400888 default_grab_touch_down,
889 default_grab_touch_up,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200890 default_grab_touch_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +0200891 default_grab_touch_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200892 default_grab_touch_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400893};
894
Quentin Glidiccde13452016-08-12 10:41:32 +0200895/** Check if the keyboard has focused resources.
896 *
897 * \param keyboard The keyboard to check for focused resources.
898 * \return Whether or not this keyboard has focused resources
899 */
900WL_EXPORT bool
901weston_keyboard_has_focus_resource(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400902{
Quentin Glidiccde13452016-08-12 10:41:32 +0200903 if (!keyboard->focus)
904 return false;
905
906 if (wl_list_empty(&keyboard->focus_resource_list))
907 return false;
908
909 return true;
910}
911
912/** Send wl_keyboard.key events to focused resources.
913 *
914 * \param keyboard The keyboard where the key events originates from.
915 * \param time The timestamp of the event
916 * \param key The key value of the event
917 * \param state The state enum value of the event
918 *
919 * For every resource that is currently in focus, send a wl_keyboard.key event
920 * with the passed parameters. The focused resources are the wl_keyboard
921 * resources of the client which currently has the surface with keyboard focus.
922 */
923WL_EXPORT void
924weston_keyboard_send_key(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200925 const struct timespec *time, uint32_t key,
Quentin Glidiccde13452016-08-12 10:41:32 +0200926 enum wl_keyboard_key_state state)
927{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400928 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +0100929 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400930 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100931 struct wl_list *resource_list;
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200932 uint32_t msecs;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400933
Quentin Glidiccde13452016-08-12 10:41:32 +0200934 if (!weston_keyboard_has_focus_resource(keyboard))
935 return;
936
Neil Roberts96d790e2013-09-19 17:32:00 +0100937 resource_list = &keyboard->focus_resource_list;
Quentin Glidiccde13452016-08-12 10:41:32 +0200938 serial = wl_display_next_serial(display);
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200939 msecs = timespec_to_msec(time);
Alexandros Frantzis2b442482018-02-20 14:05:50 +0200940 wl_resource_for_each(resource, resource_list) {
941 send_timestamps_for_input_resource(resource,
942 &keyboard->timestamps_list,
943 time);
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200944 wl_keyboard_send_key(resource, serial, msecs, key, state);
Alexandros Frantzis2b442482018-02-20 14:05:50 +0200945 }
Quentin Glidiccde13452016-08-12 10:41:32 +0200946};
947
948static void
949default_grab_keyboard_key(struct weston_keyboard_grab *grab,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200950 const struct timespec *time, uint32_t key,
951 uint32_t state)
Quentin Glidiccde13452016-08-12 10:41:32 +0200952{
953 weston_keyboard_send_key(grab->keyboard, time, key, state);
Neil Roberts96d790e2013-09-19 17:32:00 +0100954}
955
956static void
957send_modifiers_to_resource(struct weston_keyboard *keyboard,
958 struct wl_resource *resource,
959 uint32_t serial)
960{
961 wl_keyboard_send_modifiers(resource,
962 serial,
963 keyboard->modifiers.mods_depressed,
964 keyboard->modifiers.mods_latched,
965 keyboard->modifiers.mods_locked,
966 keyboard->modifiers.group);
967}
968
969static void
970send_modifiers_to_client_in_list(struct wl_client *client,
971 struct wl_list *list,
972 uint32_t serial,
973 struct weston_keyboard *keyboard)
974{
975 struct wl_resource *resource;
976
977 wl_resource_for_each(resource, list) {
978 if (wl_resource_get_client(resource) == client)
979 send_modifiers_to_resource(keyboard,
980 resource,
981 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400982 }
983}
984
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800985static struct weston_pointer_client *
986find_pointer_client_for_surface(struct weston_pointer *pointer,
987 struct weston_surface *surface)
988{
989 struct wl_client *client;
990
991 if (!surface)
992 return NULL;
993
994 if (!surface->resource)
995 return NULL;
996
997 client = wl_resource_get_client(surface->resource);
998 return weston_pointer_get_pointer_client(pointer, client);
999}
1000
1001static struct weston_pointer_client *
1002find_pointer_client_for_view(struct weston_pointer *pointer, struct weston_view *view)
1003{
1004 if (!view)
1005 return NULL;
1006
1007 return find_pointer_client_for_surface(pointer, view->surface);
1008}
1009
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001010static struct wl_resource *
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001011find_resource_for_surface(struct wl_list *list, struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001012{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001013 if (!surface)
1014 return NULL;
1015
Jason Ekstrand44a38632013-06-14 10:08:00 -05001016 if (!surface->resource)
1017 return NULL;
Stefan Schmidtfda26522013-09-17 10:54:09 +01001018
Jason Ekstrand44a38632013-06-14 10:08:00 -05001019 return wl_resource_find_for_client(list, wl_resource_get_client(surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001020}
1021
Quentin Glidiccde13452016-08-12 10:41:32 +02001022/** Send wl_keyboard.modifiers events to focused resources and pointer
1023 * focused resources.
1024 *
1025 * \param keyboard The keyboard where the modifiers events originates from.
1026 * \param serial The serial of the event
1027 * \param mods_depressed The mods_depressed value of the event
1028 * \param mods_latched The mods_latched value of the event
1029 * \param mods_locked The mods_locked value of the event
1030 * \param group The group value of the event
1031 *
1032 * For every resource that is currently in focus, send a wl_keyboard.modifiers
1033 * event with the passed parameters. The focused resources are the wl_keyboard
1034 * resources of the client which currently has the surface with keyboard focus.
1035 * This also sends wl_keyboard.modifiers events to the wl_keyboard resources of
1036 * the client having pointer focus (if different from the keyboard focus client).
1037 */
1038WL_EXPORT void
1039weston_keyboard_send_modifiers(struct weston_keyboard *keyboard,
1040 uint32_t serial, uint32_t mods_depressed,
1041 uint32_t mods_latched,
1042 uint32_t mods_locked, uint32_t group)
1043{
1044 struct weston_pointer *pointer =
1045 weston_seat_get_pointer(keyboard->seat);
1046
1047 if (weston_keyboard_has_focus_resource(keyboard)) {
1048 struct wl_list *resource_list;
1049 struct wl_resource *resource;
1050
1051 resource_list = &keyboard->focus_resource_list;
1052 wl_resource_for_each(resource, resource_list) {
1053 wl_keyboard_send_modifiers(resource, serial,
1054 mods_depressed, mods_latched,
1055 mods_locked, group);
1056 }
1057 }
1058
1059 if (pointer && pointer->focus && pointer->focus->surface->resource &&
1060 pointer->focus->surface != keyboard->focus) {
1061 struct wl_client *pointer_client =
1062 wl_resource_get_client(pointer->focus->surface->resource);
1063
1064 send_modifiers_to_client_in_list(pointer_client,
1065 &keyboard->resource_list,
1066 serial,
1067 keyboard);
1068 }
1069}
1070
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001071static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -07001072default_grab_keyboard_modifiers(struct weston_keyboard_grab *grab,
1073 uint32_t serial, uint32_t mods_depressed,
1074 uint32_t mods_latched,
1075 uint32_t mods_locked, uint32_t group)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001076{
Quentin Glidiccde13452016-08-12 10:41:32 +02001077 weston_keyboard_send_modifiers(grab->keyboard, serial, mods_depressed,
1078 mods_latched, mods_locked, group);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001079}
1080
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001081static void
1082default_grab_keyboard_cancel(struct weston_keyboard_grab *grab)
1083{
1084}
1085
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001086static const struct weston_keyboard_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001087 default_keyboard_grab_interface = {
Kristian Høgsbergb27901c2013-10-28 15:32:02 -07001088 default_grab_keyboard_key,
1089 default_grab_keyboard_modifiers,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001090 default_grab_keyboard_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001091};
1092
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001093static void
1094pointer_unmap_sprite(struct weston_pointer *pointer)
1095{
Pekka Paalanenc557ff72014-11-12 16:42:52 +02001096 struct weston_surface *surface = pointer->sprite->surface;
1097
1098 if (weston_surface_is_mapped(surface))
1099 weston_surface_unmap(surface);
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001100
1101 wl_list_remove(&pointer->sprite_destroy_listener.link);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02001102 surface->committed = NULL;
1103 surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03001104 weston_surface_set_label_func(surface, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001105 weston_view_destroy(pointer->sprite);
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001106 pointer->sprite = NULL;
1107}
1108
1109static void
1110pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
1111{
1112 struct weston_pointer *pointer =
1113 container_of(listener, struct weston_pointer,
1114 sprite_destroy_listener);
1115
1116 pointer->sprite = NULL;
1117}
1118
Jonas Ådahl3e12e632013-12-02 22:05:05 +01001119static void
1120weston_pointer_reset_state(struct weston_pointer *pointer)
1121{
1122 pointer->button_count = 0;
1123}
1124
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001125static void
1126weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data);
1127
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001128WL_EXPORT struct weston_pointer *
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001129weston_pointer_create(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001130{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001131 struct weston_pointer *pointer;
1132
Peter Huttererf3d62272013-08-08 11:57:05 +10001133 pointer = zalloc(sizeof *pointer);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001134 if (pointer == NULL)
1135 return NULL;
1136
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001137 wl_list_init(&pointer->pointer_clients);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001138 weston_pointer_set_default_grab(pointer,
1139 seat->compositor->default_pointer_grab);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001140 wl_list_init(&pointer->focus_resource_listener.link);
1141 pointer->focus_resource_listener.notify = pointer_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001142 pointer->default_grab.pointer = pointer;
1143 pointer->grab = &pointer->default_grab;
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001144 wl_signal_init(&pointer->motion_signal);
Emilio Pozuelo Monfortaa7a4762013-11-19 11:37:15 +01001145 wl_signal_init(&pointer->focus_signal);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001146 wl_list_init(&pointer->focus_view_listener.link);
Jonas Ådahl3eb4ddd2016-07-22 17:52:58 +08001147 wl_signal_init(&pointer->destroy_signal);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +02001148 wl_list_init(&pointer->timestamps_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001149
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001150 pointer->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
1151
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001152 /* FIXME: Pick better co-ords. */
1153 pointer->x = wl_fixed_from_int(100);
1154 pointer->y = wl_fixed_from_int(100);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001155
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001156 pointer->output_destroy_listener.notify =
1157 weston_pointer_handle_output_destroy;
1158 wl_signal_add(&seat->compositor->output_destroyed_signal,
1159 &pointer->output_destroy_listener);
1160
Derek Foremanf9318d12015-05-11 15:40:11 -05001161 pointer->sx = wl_fixed_from_int(-1000000);
1162 pointer->sy = wl_fixed_from_int(-1000000);
1163
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001164 return pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001165}
1166
1167WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001168weston_pointer_destroy(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001169{
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +02001170 struct weston_pointer_client *pointer_client, *tmp;
1171
Jonas Ådahl3eb4ddd2016-07-22 17:52:58 +08001172 wl_signal_emit(&pointer->destroy_signal, pointer);
1173
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001174 if (pointer->sprite)
1175 pointer_unmap_sprite(pointer);
1176
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +02001177 wl_list_for_each_safe(pointer_client, tmp, &pointer->pointer_clients,
1178 link) {
1179 wl_list_remove(&pointer_client->link);
1180 weston_pointer_client_destroy(pointer_client);
1181 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001182
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001183 wl_list_remove(&pointer->focus_resource_listener.link);
1184 wl_list_remove(&pointer->focus_view_listener.link);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001185 wl_list_remove(&pointer->output_destroy_listener.link);
Alexandros Frantzisdb907b72018-02-20 14:06:26 +02001186 wl_list_remove(&pointer->timestamps_list);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001187 free(pointer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001188}
1189
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001190void
1191weston_pointer_set_default_grab(struct weston_pointer *pointer,
1192 const struct weston_pointer_grab_interface *interface)
1193{
1194 if (interface)
1195 pointer->default_grab.interface = interface;
1196 else
1197 pointer->default_grab.interface =
1198 &default_pointer_grab_interface;
1199}
1200
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001201WL_EXPORT struct weston_keyboard *
1202weston_keyboard_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001203{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001204 struct weston_keyboard *keyboard;
1205
Peter Huttererf3d62272013-08-08 11:57:05 +10001206 keyboard = zalloc(sizeof *keyboard);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001207 if (keyboard == NULL)
Neil Roberts96d790e2013-09-19 17:32:00 +01001208 return NULL;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001209
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001210 wl_list_init(&keyboard->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +01001211 wl_list_init(&keyboard->focus_resource_list);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001212 wl_list_init(&keyboard->focus_resource_listener.link);
1213 keyboard->focus_resource_listener.notify = keyboard_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001214 wl_array_init(&keyboard->keys);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001215 keyboard->default_grab.interface = &default_keyboard_grab_interface;
1216 keyboard->default_grab.keyboard = keyboard;
1217 keyboard->grab = &keyboard->default_grab;
1218 wl_signal_init(&keyboard->focus_signal);
Alexandros Frantzis2b442482018-02-20 14:05:50 +02001219 wl_list_init(&keyboard->timestamps_list);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001220
1221 return keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001222}
1223
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001224static void
1225weston_xkb_info_destroy(struct weston_xkb_info *xkb_info);
1226
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001227WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001228weston_keyboard_destroy(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001229{
Alexandros Frantzis7a314d62018-01-26 18:47:56 +02001230 struct wl_resource *resource;
1231
1232 wl_resource_for_each(resource, &keyboard->resource_list) {
1233 wl_resource_set_user_data(resource, NULL);
1234 }
1235
1236 wl_resource_for_each(resource, &keyboard->focus_resource_list) {
1237 wl_resource_set_user_data(resource, NULL);
1238 }
1239
1240 wl_list_remove(&keyboard->resource_list);
1241 wl_list_remove(&keyboard->focus_resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +01001242
Derek Foreman185d1582017-06-28 11:17:23 -05001243 xkb_state_unref(keyboard->xkb_state.state);
1244 if (keyboard->xkb_info)
1245 weston_xkb_info_destroy(keyboard->xkb_info);
1246 xkb_keymap_unref(keyboard->pending_keymap);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001247
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001248 wl_array_release(&keyboard->keys);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001249 wl_list_remove(&keyboard->focus_resource_listener.link);
Alexandros Frantzis2b442482018-02-20 14:05:50 +02001250 wl_list_remove(&keyboard->timestamps_list);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001251 free(keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001252}
1253
Jonas Ådahlcbfa7f72013-12-02 22:05:04 +01001254static void
1255weston_touch_reset_state(struct weston_touch *touch)
1256{
1257 touch->num_tp = 0;
1258}
1259
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001260WL_EXPORT struct weston_touch *
1261weston_touch_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001262{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001263 struct weston_touch *touch;
1264
Peter Huttererf3d62272013-08-08 11:57:05 +10001265 touch = zalloc(sizeof *touch);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001266 if (touch == NULL)
1267 return NULL;
1268
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001269 wl_list_init(&touch->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +01001270 wl_list_init(&touch->focus_resource_list);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001271 wl_list_init(&touch->focus_view_listener.link);
1272 touch->focus_view_listener.notify = touch_focus_view_destroyed;
1273 wl_list_init(&touch->focus_resource_listener.link);
1274 touch->focus_resource_listener.notify = touch_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001275 touch->default_grab.interface = &default_touch_grab_interface;
1276 touch->default_grab.touch = touch;
1277 touch->grab = &touch->default_grab;
1278 wl_signal_init(&touch->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001279
1280 return touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001281}
1282
1283WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001284weston_touch_destroy(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001285{
Alexandros Frantzisb0b598c2018-01-26 18:47:57 +02001286 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01001287
Alexandros Frantzisb0b598c2018-01-26 18:47:57 +02001288 wl_resource_for_each(resource, &touch->resource_list) {
1289 wl_resource_set_user_data(resource, NULL);
1290 }
1291
1292 wl_resource_for_each(resource, &touch->focus_resource_list) {
1293 wl_resource_set_user_data(resource, NULL);
1294 }
1295
1296 wl_list_remove(&touch->resource_list);
1297 wl_list_remove(&touch->focus_resource_list);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001298 wl_list_remove(&touch->focus_view_listener.link);
1299 wl_list_remove(&touch->focus_resource_listener.link);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001300 free(touch);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001301}
1302
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001303static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04001304seat_send_updated_caps(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001305{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001306 enum wl_seat_capability caps = 0;
Rob Bradford6e737f52013-09-06 17:48:19 +01001307 struct wl_resource *resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001308
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001309 if (seat->pointer_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001310 caps |= WL_SEAT_CAPABILITY_POINTER;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001311 if (seat->keyboard_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001312 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001313 if (seat->touch_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001314 caps |= WL_SEAT_CAPABILITY_TOUCH;
1315
Rob Bradford6e737f52013-09-06 17:48:19 +01001316 wl_resource_for_each(resource, &seat->base_resource_list) {
1317 wl_seat_send_capabilities(resource, caps);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001318 }
Jason Ekstranda4ab5422014-04-02 19:53:45 -05001319 wl_signal_emit(&seat->updated_caps_signal, seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001320}
1321
Derek Foremanf9318d12015-05-11 15:40:11 -05001322
1323/** Clear the pointer focus
1324 *
1325 * \param pointer the pointer to clear focus for.
1326 *
1327 * This can be used to unset pointer focus and set the co-ordinates to the
1328 * arbitrary values we use for the no focus case.
1329 *
1330 * There's no requirement to use this function. For example, passing the
1331 * results of a weston_compositor_pick_view() directly to
1332 * weston_pointer_set_focus() will do the right thing when no view is found.
1333 */
1334WL_EXPORT void
1335weston_pointer_clear_focus(struct weston_pointer *pointer)
1336{
1337 weston_pointer_set_focus(pointer, NULL,
1338 wl_fixed_from_int(-1000000),
1339 wl_fixed_from_int(-1000000));
1340}
1341
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001342WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001343weston_pointer_set_focus(struct weston_pointer *pointer,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001344 struct weston_view *view,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001345 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001346{
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001347 struct weston_pointer_client *pointer_client;
Derek Foreman1281a362015-07-31 16:55:32 -05001348 struct weston_keyboard *kbd = weston_seat_get_keyboard(pointer->seat);
Neil Roberts96d790e2013-09-19 17:32:00 +01001349 struct wl_resource *resource;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001350 struct wl_resource *surface_resource;
Rob Bradford880ebc72013-07-22 17:31:38 +01001351 struct wl_display *display = pointer->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001352 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01001353 struct wl_list *focus_resource_list;
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001354 int refocus = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001355
1356 if ((!pointer->focus && view) ||
1357 (pointer->focus && !view) ||
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001358 (pointer->focus && pointer->focus->surface != view->surface) ||
1359 pointer->sx != sx || pointer->sy != sy)
1360 refocus = 1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001361
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001362 if (pointer->focus_client && refocus) {
1363 focus_resource_list = &pointer->focus_client->pointer_resources;
1364 if (!wl_list_empty(focus_resource_list)) {
1365 serial = wl_display_next_serial(display);
1366 surface_resource = pointer->focus->surface->resource;
1367 wl_resource_for_each(resource, focus_resource_list) {
1368 wl_pointer_send_leave(resource, serial,
1369 surface_resource);
Peter Hutterer87743e92016-01-18 16:38:22 +10001370 pointer_send_frame(resource);
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001371 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001372 }
1373
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001374 pointer->focus_client = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001375 }
1376
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001377 pointer_client = find_pointer_client_for_view(pointer, view);
1378 if (pointer_client && refocus) {
1379 struct wl_client *surface_client = pointer_client->client;
Neil Roberts96d790e2013-09-19 17:32:00 +01001380
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001381 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001382
Jason Ekstranda7af7042013-10-12 22:38:11 -05001383 if (kbd && kbd->focus != view->surface)
Kristian Høgsbergcb406f12013-10-09 10:54:03 -07001384 send_modifiers_to_client_in_list(surface_client,
1385 &kbd->resource_list,
1386 serial,
1387 kbd);
1388
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001389 pointer->focus_client = pointer_client;
Neil Roberts96d790e2013-09-19 17:32:00 +01001390
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001391 focus_resource_list = &pointer->focus_client->pointer_resources;
Neil Roberts96d790e2013-09-19 17:32:00 +01001392 wl_resource_for_each(resource, focus_resource_list) {
1393 wl_pointer_send_enter(resource,
1394 serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001395 view->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01001396 sx, sy);
Peter Hutterer87743e92016-01-18 16:38:22 +10001397 pointer_send_frame(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001398 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001399
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001400 pointer->focus_serial = serial;
1401 }
1402
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001403 wl_list_remove(&pointer->focus_view_listener.link);
1404 wl_list_init(&pointer->focus_view_listener.link);
1405 wl_list_remove(&pointer->focus_resource_listener.link);
1406 wl_list_init(&pointer->focus_resource_listener.link);
Emilio Pozuelo Monfortaa7a4762013-11-19 11:37:15 +01001407 if (view)
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001408 wl_signal_add(&view->destroy_signal, &pointer->focus_view_listener);
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001409 if (view && view->surface->resource)
1410 wl_resource_add_destroy_listener(view->surface->resource,
1411 &pointer->focus_resource_listener);
1412
1413 pointer->focus = view;
1414 pointer->focus_view_listener.notify = pointer_focus_view_destroyed;
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001415 pointer->sx = sx;
1416 pointer->sy = sy;
1417
Derek Foremanf9318d12015-05-11 15:40:11 -05001418 assert(view || sx == wl_fixed_from_int(-1000000));
1419 assert(view || sy == wl_fixed_from_int(-1000000));
1420
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001421 wl_signal_emit(&pointer->focus_signal, pointer);
1422}
1423
Neil Roberts96d790e2013-09-19 17:32:00 +01001424static void
1425send_enter_to_resource_list(struct wl_list *list,
1426 struct weston_keyboard *keyboard,
1427 struct weston_surface *surface,
1428 uint32_t serial)
1429{
1430 struct wl_resource *resource;
1431
1432 wl_resource_for_each(resource, list) {
1433 send_modifiers_to_resource(keyboard, resource, serial);
1434 wl_keyboard_send_enter(resource, serial,
1435 surface->resource,
1436 &keyboard->keys);
1437 }
1438}
1439
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001440WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001441weston_keyboard_set_focus(struct weston_keyboard *keyboard,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001442 struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001443{
Quentin Glidic85d55542017-07-21 14:02:40 +02001444 struct weston_seat *seat = keyboard->seat;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001445 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +01001446 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001447 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01001448 struct wl_list *focus_resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001449
Neil Roberts96d790e2013-09-19 17:32:00 +01001450 focus_resource_list = &keyboard->focus_resource_list;
1451
1452 if (!wl_list_empty(focus_resource_list) && keyboard->focus != surface) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001453 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001454 wl_resource_for_each(resource, focus_resource_list) {
1455 wl_keyboard_send_leave(resource, serial,
1456 keyboard->focus->resource);
1457 }
1458 move_resources(&keyboard->resource_list, focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001459 }
1460
Neil Roberts96d790e2013-09-19 17:32:00 +01001461 if (find_resource_for_surface(&keyboard->resource_list, surface) &&
1462 keyboard->focus != surface) {
1463 struct wl_client *surface_client =
1464 wl_resource_get_client(surface->resource);
1465
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001466 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001467
1468 move_resources_for_client(focus_resource_list,
1469 &keyboard->resource_list,
1470 surface_client);
1471 send_enter_to_resource_list(focus_resource_list,
1472 keyboard,
1473 surface,
1474 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001475 keyboard->focus_serial = serial;
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001476 }
1477
Quentin Glidic85d55542017-07-21 14:02:40 +02001478 if (seat->saved_kbd_focus) {
1479 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1480 seat->saved_kbd_focus = NULL;
1481 }
1482
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001483 wl_list_remove(&keyboard->focus_resource_listener.link);
1484 wl_list_init(&keyboard->focus_resource_listener.link);
1485 if (surface && surface->resource)
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001486 wl_resource_add_destroy_listener(surface->resource,
1487 &keyboard->focus_resource_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001488
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001489 keyboard->focus = surface;
1490 wl_signal_emit(&keyboard->focus_signal, keyboard);
1491}
1492
Giulio Camuffoa20ca812014-11-22 11:16:56 +02001493/* Users of this function must manually manage the keyboard focus */
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001494WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001495weston_keyboard_start_grab(struct weston_keyboard *keyboard,
1496 struct weston_keyboard_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001497{
1498 keyboard->grab = grab;
1499 grab->keyboard = keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001500}
1501
1502WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001503weston_keyboard_end_grab(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001504{
1505 keyboard->grab = &keyboard->default_grab;
1506}
1507
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001508static void
1509weston_keyboard_cancel_grab(struct weston_keyboard *keyboard)
1510{
1511 keyboard->grab->interface->cancel(keyboard->grab);
1512}
1513
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001514WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001515weston_pointer_start_grab(struct weston_pointer *pointer,
1516 struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001517{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001518 pointer->grab = grab;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001519 grab->pointer = pointer;
Kristian Høgsbergda751b82013-07-04 00:58:07 -04001520 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001521}
1522
1523WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001524weston_pointer_end_grab(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001525{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001526 pointer->grab = &pointer->default_grab;
Kristian Høgsbergda751b82013-07-04 00:58:07 -04001527 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001528}
1529
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001530static void
1531weston_pointer_cancel_grab(struct weston_pointer *pointer)
1532{
1533 pointer->grab->interface->cancel(pointer->grab);
1534}
1535
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001536WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -04001537weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001538{
1539 touch->grab = grab;
1540 grab->touch = touch;
1541}
1542
1543WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -04001544weston_touch_end_grab(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001545{
1546 touch->grab = &touch->default_grab;
1547}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001548
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001549static void
1550weston_touch_cancel_grab(struct weston_touch *touch)
1551{
1552 touch->grab->interface->cancel(touch->grab);
1553}
1554
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001555static void
1556weston_pointer_clamp_for_output(struct weston_pointer *pointer,
1557 struct weston_output *output,
1558 wl_fixed_t *fx, wl_fixed_t *fy)
1559{
1560 int x, y;
1561
1562 x = wl_fixed_to_int(*fx);
1563 y = wl_fixed_to_int(*fy);
1564
1565 if (x < output->x)
1566 *fx = wl_fixed_from_int(output->x);
1567 else if (x >= output->x + output->width)
1568 *fx = wl_fixed_from_int(output->x +
1569 output->width - 1);
1570 if (y < output->y)
1571 *fy = wl_fixed_from_int(output->y);
1572 else if (y >= output->y + output->height)
1573 *fy = wl_fixed_from_int(output->y +
1574 output->height - 1);
1575}
1576
Rob Bradford806d8c02013-06-25 18:56:41 +01001577WL_EXPORT void
1578weston_pointer_clamp(struct weston_pointer *pointer, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001579{
Rob Bradford806d8c02013-06-25 18:56:41 +01001580 struct weston_compositor *ec = pointer->seat->compositor;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001581 struct weston_output *output, *prev = NULL;
1582 int x, y, old_x, old_y, valid = 0;
1583
1584 x = wl_fixed_to_int(*fx);
1585 y = wl_fixed_to_int(*fy);
Rob Bradford806d8c02013-06-25 18:56:41 +01001586 old_x = wl_fixed_to_int(pointer->x);
1587 old_y = wl_fixed_to_int(pointer->y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001588
1589 wl_list_for_each(output, &ec->output_list, link) {
Rob Bradford66bd9f52013-06-25 18:56:42 +01001590 if (pointer->seat->output && pointer->seat->output != output)
1591 continue;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001592 if (pixman_region32_contains_point(&output->region,
1593 x, y, NULL))
1594 valid = 1;
1595 if (pixman_region32_contains_point(&output->region,
1596 old_x, old_y, NULL))
1597 prev = output;
1598 }
1599
Rob Bradford66bd9f52013-06-25 18:56:42 +01001600 if (!prev)
1601 prev = pointer->seat->output;
1602
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001603 if (prev && !valid)
1604 weston_pointer_clamp_for_output(pointer, prev, fx, fy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001605}
1606
Jonas Ådahld2510102014-10-05 21:39:14 +02001607static void
1608weston_pointer_move_to(struct weston_pointer *pointer,
1609 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001610{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001611 int32_t ix, iy;
1612
Rob Bradford806d8c02013-06-25 18:56:41 +01001613 weston_pointer_clamp (pointer, &x, &y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001614
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001615 pointer->x = x;
1616 pointer->y = y;
1617
1618 ix = wl_fixed_to_int(x);
1619 iy = wl_fixed_to_int(y);
1620
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001621 if (pointer->sprite) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001622 weston_view_set_position(pointer->sprite,
1623 ix - pointer->hotspot_x,
1624 iy - pointer->hotspot_y);
1625 weston_view_schedule_repaint(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001626 }
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001627
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001628 pointer->grab->interface->focus(pointer->grab);
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001629 wl_signal_emit(&pointer->motion_signal, pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001630}
1631
Jonas Ådahld2510102014-10-05 21:39:14 +02001632WL_EXPORT void
Jonas Ådahld2510102014-10-05 21:39:14 +02001633weston_pointer_move(struct weston_pointer *pointer,
1634 struct weston_pointer_motion_event *event)
1635{
1636 wl_fixed_t x, y;
1637
1638 weston_pointer_motion_to_abs(pointer, event, &x, &y);
1639 weston_pointer_move_to(pointer, x, y);
1640}
1641
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001642/** Verify if the pointer is in a valid position and move it if it isn't.
1643 */
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001644static void
1645weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001646{
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001647 struct weston_pointer *pointer;
1648 struct weston_compositor *ec;
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001649 struct weston_output *output, *closest = NULL;
1650 int x, y, distance, min = INT_MAX;
1651 wl_fixed_t fx, fy;
1652
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001653 pointer = container_of(listener, struct weston_pointer,
1654 output_destroy_listener);
1655 ec = pointer->seat->compositor;
1656
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001657 x = wl_fixed_to_int(pointer->x);
1658 y = wl_fixed_to_int(pointer->y);
1659
1660 wl_list_for_each(output, &ec->output_list, link) {
1661 if (pixman_region32_contains_point(&output->region,
1662 x, y, NULL))
1663 return;
1664
1665 /* Aproximante the distance from the pointer to the center of
1666 * the output. */
1667 distance = abs(output->x + output->width / 2 - x) +
1668 abs(output->y + output->height / 2 - y);
1669 if (distance < min) {
1670 min = distance;
1671 closest = output;
1672 }
1673 }
1674
1675 /* Nothing to do if there's no output left. */
1676 if (!closest)
1677 return;
1678
1679 fx = pointer->x;
1680 fy = pointer->y;
1681
1682 weston_pointer_clamp_for_output(pointer, closest, &fx, &fy);
Jonas Ådahld2510102014-10-05 21:39:14 +02001683 weston_pointer_move_to(pointer, fx, fy);
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001684}
1685
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001686WL_EXPORT void
1687notify_motion(struct weston_seat *seat,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001688 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001689 struct weston_pointer_motion_event *event)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001690{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001691 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001692 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001693
1694 weston_compositor_wake(ec);
Jonas Ådahld2510102014-10-05 21:39:14 +02001695 pointer->grab->interface->motion(pointer->grab, time, event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001696}
1697
Daniel Stone96d47c02013-11-19 11:37:12 +01001698static void
1699run_modifier_bindings(struct weston_seat *seat, uint32_t old, uint32_t new)
1700{
1701 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001702 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Daniel Stone96d47c02013-11-19 11:37:12 +01001703 uint32_t diff;
1704 unsigned int i;
1705 struct {
1706 uint32_t xkb;
1707 enum weston_keyboard_modifier weston;
1708 } mods[] = {
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001709 { keyboard->xkb_info->ctrl_mod, MODIFIER_CTRL },
1710 { keyboard->xkb_info->alt_mod, MODIFIER_ALT },
1711 { keyboard->xkb_info->super_mod, MODIFIER_SUPER },
1712 { keyboard->xkb_info->shift_mod, MODIFIER_SHIFT },
Daniel Stone96d47c02013-11-19 11:37:12 +01001713 };
1714
1715 diff = new & ~old;
1716 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
1717 if (diff & (1 << mods[i].xkb))
1718 weston_compositor_run_modifier_binding(compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001719 keyboard,
Daniel Stone96d47c02013-11-19 11:37:12 +01001720 mods[i].weston,
1721 WL_KEYBOARD_KEY_STATE_PRESSED);
1722 }
1723
1724 diff = old & ~new;
1725 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
1726 if (diff & (1 << mods[i].xkb))
1727 weston_compositor_run_modifier_binding(compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001728 keyboard,
Daniel Stone96d47c02013-11-19 11:37:12 +01001729 mods[i].weston,
1730 WL_KEYBOARD_KEY_STATE_RELEASED);
1731 }
1732}
1733
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001734WL_EXPORT void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001735notify_motion_absolute(struct weston_seat *seat, const struct timespec *time,
1736 double x, double y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001737{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001738 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001739 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Jonas Ådahld2510102014-10-05 21:39:14 +02001740 struct weston_pointer_motion_event event = { 0 };
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001741
1742 weston_compositor_wake(ec);
Jonas Ådahld2510102014-10-05 21:39:14 +02001743
1744 event = (struct weston_pointer_motion_event) {
1745 .mask = WESTON_POINTER_MOTION_ABS,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001746 .x = x,
1747 .y = y,
Jonas Ådahld2510102014-10-05 21:39:14 +02001748 };
1749
1750 pointer->grab->interface->motion(pointer->grab, time, &event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001751}
1752
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02001753static unsigned int
1754peek_next_activate_serial(struct weston_compositor *c)
1755{
1756 unsigned serial = c->activate_serial + 1;
1757
1758 return serial == 0 ? 1 : serial;
1759}
1760
1761static void
1762inc_activate_serial(struct weston_compositor *c)
1763{
1764 c->activate_serial = peek_next_activate_serial (c);
1765}
1766
1767WL_EXPORT void
1768weston_view_activate(struct weston_view *view,
1769 struct weston_seat *seat,
1770 uint32_t flags)
1771{
1772 struct weston_compositor *compositor = seat->compositor;
1773
1774 if (flags & WESTON_ACTIVATE_FLAG_CLICKED) {
1775 view->click_to_activate_serial =
1776 peek_next_activate_serial(compositor);
1777 }
1778
1779 weston_seat_set_keyboard_focus(seat, view->surface);
1780}
1781
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001782WL_EXPORT void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001783notify_button(struct weston_seat *seat, const struct timespec *time,
1784 int32_t button, enum wl_pointer_button_state state)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001785{
1786 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001787 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001788
1789 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001790 weston_compositor_idle_inhibit(compositor);
1791 if (pointer->button_count == 0) {
1792 pointer->grab_button = button;
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001793 pointer->grab_time = *time;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001794 pointer->grab_x = pointer->x;
1795 pointer->grab_y = pointer->y;
1796 }
1797 pointer->button_count++;
1798 } else {
1799 weston_compositor_idle_release(compositor);
1800 pointer->button_count--;
1801 }
1802
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001803 weston_compositor_run_button_binding(compositor, pointer, time, button,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001804 state);
1805
1806 pointer->grab->interface->button(pointer->grab, time, button, state);
1807
1808 if (pointer->button_count == 1)
1809 pointer->grab_serial =
1810 wl_display_get_serial(compositor->wl_display);
1811}
1812
1813WL_EXPORT void
Alexandros Frantzis80321942017-11-16 18:20:56 +02001814notify_axis(struct weston_seat *seat, const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001815 struct weston_pointer_axis_event *event)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001816{
1817 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001818 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001819
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001820 weston_compositor_wake(compositor);
1821
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001822 if (weston_compositor_run_axis_binding(compositor, pointer,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001823 time, event))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001824 return;
1825
Peter Hutterer89b6a492016-01-18 15:58:17 +10001826 pointer->grab->interface->axis(pointer->grab, time, event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001827}
1828
Peter Hutterer87743e92016-01-18 16:38:22 +10001829WL_EXPORT void
1830notify_axis_source(struct weston_seat *seat, uint32_t source)
1831{
1832 struct weston_compositor *compositor = seat->compositor;
1833 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1834
1835 weston_compositor_wake(compositor);
1836
1837 pointer->grab->interface->axis_source(pointer->grab, source);
1838}
1839
1840WL_EXPORT void
1841notify_pointer_frame(struct weston_seat *seat)
1842{
1843 struct weston_compositor *compositor = seat->compositor;
1844 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1845
1846 weston_compositor_wake(compositor);
1847
1848 pointer->grab->interface->frame(pointer->grab);
1849}
1850
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001851WL_EXPORT int
1852weston_keyboard_set_locks(struct weston_keyboard *keyboard,
1853 uint32_t mask, uint32_t value)
1854{
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001855 uint32_t serial;
1856 xkb_mod_mask_t mods_depressed, mods_latched, mods_locked, group;
1857 xkb_mod_mask_t num, caps;
1858
1859 /* We don't want the leds to go out of sync with the actual state
1860 * so if the backend has no way to change the leds don't try to
1861 * change the state */
1862 if (!keyboard->seat->led_update)
1863 return -1;
1864
1865 mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
1866 XKB_STATE_DEPRESSED);
1867 mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
1868 XKB_STATE_LATCHED);
1869 mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
1870 XKB_STATE_LOCKED);
1871 group = xkb_state_serialize_group(keyboard->xkb_state.state,
1872 XKB_STATE_EFFECTIVE);
1873
1874 num = (1 << keyboard->xkb_info->mod2_mod);
1875 caps = (1 << keyboard->xkb_info->caps_mod);
1876 if (mask & WESTON_NUM_LOCK) {
1877 if (value & WESTON_NUM_LOCK)
1878 mods_locked |= num;
1879 else
1880 mods_locked &= ~num;
1881 }
1882 if (mask & WESTON_CAPS_LOCK) {
1883 if (value & WESTON_CAPS_LOCK)
1884 mods_locked |= caps;
1885 else
1886 mods_locked &= ~caps;
1887 }
1888
1889 xkb_state_update_mask(keyboard->xkb_state.state, mods_depressed,
1890 mods_latched, mods_locked, 0, 0, group);
1891
1892 serial = wl_display_next_serial(
1893 keyboard->seat->compositor->wl_display);
1894 notify_modifiers(keyboard->seat, serial);
1895
1896 return 0;
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001897}
1898
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001899WL_EXPORT void
1900notify_modifiers(struct weston_seat *seat, uint32_t serial)
1901{
Derek Foreman1281a362015-07-31 16:55:32 -05001902 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001903 struct weston_keyboard_grab *grab = keyboard->grab;
1904 uint32_t mods_depressed, mods_latched, mods_locked, group;
1905 uint32_t mods_lookup;
1906 enum weston_led leds = 0;
1907 int changed = 0;
1908
1909 /* Serialize and update our internal state, checking to see if it's
1910 * different to the previous state. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001911 mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03001912 XKB_STATE_MODS_DEPRESSED);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001913 mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03001914 XKB_STATE_MODS_LATCHED);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001915 mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03001916 XKB_STATE_MODS_LOCKED);
1917 group = xkb_state_serialize_layout(keyboard->xkb_state.state,
1918 XKB_STATE_LAYOUT_EFFECTIVE);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001919
Derek Foreman244e99e2015-06-03 15:53:26 -05001920 if (mods_depressed != keyboard->modifiers.mods_depressed ||
1921 mods_latched != keyboard->modifiers.mods_latched ||
1922 mods_locked != keyboard->modifiers.mods_locked ||
1923 group != keyboard->modifiers.group)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001924 changed = 1;
1925
Derek Foreman244e99e2015-06-03 15:53:26 -05001926 run_modifier_bindings(seat, keyboard->modifiers.mods_depressed,
Daniel Stone96d47c02013-11-19 11:37:12 +01001927 mods_depressed);
1928
Derek Foreman244e99e2015-06-03 15:53:26 -05001929 keyboard->modifiers.mods_depressed = mods_depressed;
1930 keyboard->modifiers.mods_latched = mods_latched;
1931 keyboard->modifiers.mods_locked = mods_locked;
1932 keyboard->modifiers.group = group;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001933
1934 /* And update the modifier_state for bindings. */
1935 mods_lookup = mods_depressed | mods_latched;
1936 seat->modifier_state = 0;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001937 if (mods_lookup & (1 << keyboard->xkb_info->ctrl_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001938 seat->modifier_state |= MODIFIER_CTRL;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001939 if (mods_lookup & (1 << keyboard->xkb_info->alt_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001940 seat->modifier_state |= MODIFIER_ALT;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001941 if (mods_lookup & (1 << keyboard->xkb_info->super_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001942 seat->modifier_state |= MODIFIER_SUPER;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001943 if (mods_lookup & (1 << keyboard->xkb_info->shift_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001944 seat->modifier_state |= MODIFIER_SHIFT;
1945
1946 /* Finally, notify the compositor that LEDs have changed. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001947 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
1948 keyboard->xkb_info->num_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001949 leds |= LED_NUM_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001950 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
1951 keyboard->xkb_info->caps_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001952 leds |= LED_CAPS_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001953 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
1954 keyboard->xkb_info->scroll_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001955 leds |= LED_SCROLL_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001956 if (leds != keyboard->xkb_state.leds && seat->led_update)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001957 seat->led_update(seat, leds);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001958 keyboard->xkb_state.leds = leds;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001959
1960 if (changed) {
1961 grab->interface->modifiers(grab,
1962 serial,
1963 keyboard->modifiers.mods_depressed,
1964 keyboard->modifiers.mods_latched,
1965 keyboard->modifiers.mods_locked,
1966 keyboard->modifiers.group);
1967 }
1968}
1969
1970static void
1971update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
1972 enum wl_keyboard_key_state state)
1973{
Derek Foreman1281a362015-07-31 16:55:32 -05001974 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001975 enum xkb_key_direction direction;
1976
1977 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1978 direction = XKB_KEY_DOWN;
1979 else
1980 direction = XKB_KEY_UP;
1981
1982 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
1983 * broken keycode system, which starts at 8. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001984 xkb_state_update_key(keyboard->xkb_state.state, key + 8, direction);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001985
1986 notify_modifiers(seat, serial);
1987}
Rui Matos65196bc2013-10-10 19:44:19 +02001988
1989static void
1990send_keymap(struct wl_resource *resource, struct weston_xkb_info *xkb_info)
1991{
1992 wl_keyboard_send_keymap(resource,
1993 WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
1994 xkb_info->keymap_fd,
1995 xkb_info->keymap_size);
1996}
1997
1998static void
1999send_modifiers(struct wl_resource *resource, uint32_t serial, struct weston_keyboard *keyboard)
2000{
2001 wl_keyboard_send_modifiers(resource, serial,
2002 keyboard->modifiers.mods_depressed,
2003 keyboard->modifiers.mods_latched,
2004 keyboard->modifiers.mods_locked,
2005 keyboard->modifiers.group);
2006}
2007
2008static struct weston_xkb_info *
2009weston_xkb_info_create(struct xkb_keymap *keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02002010
2011static void
2012update_keymap(struct weston_seat *seat)
2013{
Derek Foreman1281a362015-07-31 16:55:32 -05002014 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Rui Matos65196bc2013-10-10 19:44:19 +02002015 struct wl_resource *resource;
2016 struct weston_xkb_info *xkb_info;
2017 struct xkb_state *state;
2018 xkb_mod_mask_t latched_mods;
2019 xkb_mod_mask_t locked_mods;
2020
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002021 xkb_info = weston_xkb_info_create(keyboard->pending_keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02002022
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002023 xkb_keymap_unref(keyboard->pending_keymap);
2024 keyboard->pending_keymap = NULL;
Rui Matos65196bc2013-10-10 19:44:19 +02002025
2026 if (!xkb_info) {
2027 weston_log("failed to create XKB info\n");
2028 return;
2029 }
2030
2031 state = xkb_state_new(xkb_info->keymap);
2032 if (!state) {
2033 weston_log("failed to initialise XKB state\n");
2034 weston_xkb_info_destroy(xkb_info);
2035 return;
2036 }
2037
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002038 latched_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
2039 XKB_STATE_MODS_LATCHED);
2040 locked_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
2041 XKB_STATE_MODS_LOCKED);
Rui Matos65196bc2013-10-10 19:44:19 +02002042 xkb_state_update_mask(state,
2043 0, /* depressed */
2044 latched_mods,
2045 locked_mods,
2046 0, 0, 0);
2047
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002048 weston_xkb_info_destroy(keyboard->xkb_info);
2049 keyboard->xkb_info = xkb_info;
Rui Matos65196bc2013-10-10 19:44:19 +02002050
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002051 xkb_state_unref(keyboard->xkb_state.state);
2052 keyboard->xkb_state.state = state;
Rui Matos65196bc2013-10-10 19:44:19 +02002053
Derek Foremanbc91e542015-06-03 15:53:27 -05002054 wl_resource_for_each(resource, &keyboard->resource_list)
Rui Matos65196bc2013-10-10 19:44:19 +02002055 send_keymap(resource, xkb_info);
Derek Foremanbc91e542015-06-03 15:53:27 -05002056 wl_resource_for_each(resource, &keyboard->focus_resource_list)
Rui Matos65196bc2013-10-10 19:44:19 +02002057 send_keymap(resource, xkb_info);
2058
2059 notify_modifiers(seat, wl_display_next_serial(seat->compositor->wl_display));
2060
2061 if (!latched_mods && !locked_mods)
2062 return;
2063
Derek Foremanbc91e542015-06-03 15:53:27 -05002064 wl_resource_for_each(resource, &keyboard->resource_list)
2065 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), keyboard);
2066 wl_resource_for_each(resource, &keyboard->focus_resource_list)
2067 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), keyboard);
Rui Matos65196bc2013-10-10 19:44:19 +02002068}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002069
2070WL_EXPORT void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02002071notify_key(struct weston_seat *seat, const struct timespec *time, uint32_t key,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002072 enum wl_keyboard_key_state state,
2073 enum weston_key_state_update update_state)
2074{
2075 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002076 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002077 struct weston_keyboard_grab *grab = keyboard->grab;
Pekka Paalanen86b53962014-11-19 13:43:32 +02002078 uint32_t *k, *end;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002079
2080 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002081 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002082 } else {
2083 weston_compositor_idle_release(compositor);
2084 }
2085
Pekka Paalanen86b53962014-11-19 13:43:32 +02002086 end = keyboard->keys.data + keyboard->keys.size;
2087 for (k = keyboard->keys.data; k < end; k++) {
2088 if (*k == key) {
2089 /* Ignore server-generated repeats. */
2090 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2091 return;
2092 *k = *--end;
2093 }
2094 }
2095 keyboard->keys.size = (void *) end - keyboard->keys.data;
2096 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2097 k = wl_array_add(&keyboard->keys, sizeof *k);
2098 *k = key;
2099 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002100
2101 if (grab == &keyboard->default_grab ||
2102 grab == &keyboard->input_method_grab) {
Derek Foreman99a6a2d2015-07-15 13:00:43 -05002103 weston_compositor_run_key_binding(compositor, keyboard, time,
2104 key, state);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002105 grab = keyboard->grab;
2106 }
2107
2108 grab->interface->key(grab, time, key, state);
2109
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002110 if (keyboard->pending_keymap &&
Pekka Paalanen86b53962014-11-19 13:43:32 +02002111 keyboard->keys.size == 0)
Rui Matos65196bc2013-10-10 19:44:19 +02002112 update_keymap(seat);
2113
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002114 if (update_state == STATE_UPDATE_AUTOMATIC) {
2115 update_modifier_state(seat,
2116 wl_display_get_serial(compositor->wl_display),
2117 key,
2118 state);
2119 }
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002120
Olivier Fourdandf84dbe2016-06-30 16:01:56 +02002121 keyboard->grab_serial = wl_display_get_serial(compositor->wl_display);
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002122 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02002123 keyboard->grab_time = *time;
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002124 keyboard->grab_key = key;
2125 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002126}
2127
2128WL_EXPORT void
2129notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002130 double x, double y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002131{
Derek Foreman1281a362015-07-31 16:55:32 -05002132 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2133
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002134 if (output) {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002135 weston_pointer_move_to(pointer,
2136 wl_fixed_from_double(x),
2137 wl_fixed_from_double(y));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002138 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002139 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002140 * NULL) here, but somehow that breaks re-entry... */
2141 }
2142}
2143
2144static void
2145destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
2146{
2147 struct weston_seat *ws;
2148
2149 ws = container_of(listener, struct weston_seat,
2150 saved_kbd_focus_listener);
2151
2152 ws->saved_kbd_focus = NULL;
2153}
2154
2155WL_EXPORT void
2156notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
2157 enum weston_key_state_update update_state)
2158{
2159 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002160 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04002161 struct weston_surface *surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002162 uint32_t *k, serial;
2163
2164 serial = wl_display_next_serial(compositor->wl_display);
2165 wl_array_copy(&keyboard->keys, keys);
2166 wl_array_for_each(k, &keyboard->keys) {
2167 weston_compositor_idle_inhibit(compositor);
2168 if (update_state == STATE_UPDATE_AUTOMATIC)
2169 update_modifier_state(seat, serial, *k,
2170 WL_KEYBOARD_KEY_STATE_PRESSED);
2171 }
2172
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002173 surface = seat->saved_kbd_focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002174 if (surface) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002175 weston_keyboard_set_focus(keyboard, surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002176 }
2177}
2178
2179WL_EXPORT void
2180notify_keyboard_focus_out(struct weston_seat *seat)
2181{
2182 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002183 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2184 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Quentin Glidic85d55542017-07-21 14:02:40 +02002185 struct weston_surface *focus = keyboard->focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002186 uint32_t *k, serial;
2187
2188 serial = wl_display_next_serial(compositor->wl_display);
2189 wl_array_for_each(k, &keyboard->keys) {
2190 weston_compositor_idle_release(compositor);
2191 update_modifier_state(seat, serial, *k,
2192 WL_KEYBOARD_KEY_STATE_RELEASED);
2193 }
2194
2195 seat->modifier_state = 0;
2196
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002197 weston_keyboard_set_focus(keyboard, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002198 weston_keyboard_cancel_grab(keyboard);
Derek Foreman1281a362015-07-31 16:55:32 -05002199 if (pointer)
2200 weston_pointer_cancel_grab(pointer);
Quentin Glidic85d55542017-07-21 14:02:40 +02002201
2202 if (focus) {
2203 seat->saved_kbd_focus = focus;
2204 seat->saved_kbd_focus_listener.notify =
2205 destroy_device_saved_kbd_focus;
2206 wl_signal_add(&focus->destroy_signal,
2207 &seat->saved_kbd_focus_listener);
2208 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002209}
2210
Michael Fua2bb7912013-07-23 15:51:06 +08002211WL_EXPORT void
Derek Foreman4c93c082015-04-30 16:45:41 -05002212weston_touch_set_focus(struct weston_touch *touch, struct weston_view *view)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002213{
Neil Roberts96d790e2013-09-19 17:32:00 +01002214 struct wl_list *focus_resource_list;
2215
Derek Foreman4c93c082015-04-30 16:45:41 -05002216 focus_resource_list = &touch->focus_resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002217
Derek Foreman4c93c082015-04-30 16:45:41 -05002218 if (view && touch->focus &&
2219 touch->focus->surface == view->surface) {
2220 touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002221 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002222 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002223
Derek Foreman4c93c082015-04-30 16:45:41 -05002224 wl_list_remove(&touch->focus_resource_listener.link);
2225 wl_list_init(&touch->focus_resource_listener.link);
2226 wl_list_remove(&touch->focus_view_listener.link);
2227 wl_list_init(&touch->focus_view_listener.link);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01002228
Neil Roberts96d790e2013-09-19 17:32:00 +01002229 if (!wl_list_empty(focus_resource_list)) {
Derek Foreman4c93c082015-04-30 16:45:41 -05002230 move_resources(&touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002231 focus_resource_list);
2232 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002233
Jason Ekstranda7af7042013-10-12 22:38:11 -05002234 if (view) {
Derek Foreman362656b2014-09-04 10:23:05 -05002235 struct wl_client *surface_client;
2236
2237 if (!view->surface->resource) {
Derek Foreman4c93c082015-04-30 16:45:41 -05002238 touch->focus = NULL;
Derek Foreman362656b2014-09-04 10:23:05 -05002239 return;
2240 }
2241
2242 surface_client = wl_resource_get_client(view->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +01002243 move_resources_for_client(focus_resource_list,
Derek Foreman4c93c082015-04-30 16:45:41 -05002244 &touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002245 surface_client);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01002246 wl_resource_add_destroy_listener(view->surface->resource,
Derek Foreman4c93c082015-04-30 16:45:41 -05002247 &touch->focus_resource_listener);
2248 wl_signal_add(&view->destroy_signal, &touch->focus_view_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002249 }
Derek Foreman4c93c082015-04-30 16:45:41 -05002250 touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002251}
2252
2253/**
2254 * notify_touch - emulates button touches and notifies surfaces accordingly.
2255 *
2256 * It assumes always the correct cycle sequence until it gets here: touch_down
2257 * → touch_update → ... → touch_update → touch_end. The driver is responsible
2258 * for sending along such order.
2259 *
2260 */
2261WL_EXPORT void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02002262notify_touch(struct weston_seat *seat, const struct timespec *time,
2263 int touch_id, double double_x, double double_y, int touch_type)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002264{
2265 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002266 struct weston_touch *touch = weston_seat_get_touch(seat);
Kristian Høgsberge329f362013-05-06 22:19:57 -04002267 struct weston_touch_grab *grab = touch->grab;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002268 struct weston_view *ev;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002269 wl_fixed_t sx, sy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002270 wl_fixed_t x = wl_fixed_from_double(double_x);
2271 wl_fixed_t y = wl_fixed_from_double(double_y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002272
2273 /* Update grab's global coordinates. */
Neil Roberts306fe082013-10-03 16:43:06 +01002274 if (touch_id == touch->grab_touch_id && touch_type != WL_TOUCH_UP) {
2275 touch->grab_x = x;
2276 touch->grab_y = y;
2277 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002278
2279 switch (touch_type) {
2280 case WL_TOUCH_DOWN:
2281 weston_compositor_idle_inhibit(ec);
2282
Jonas Ådahl9484b692013-12-02 22:05:03 +01002283 touch->num_tp++;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002284
Jason Ekstranda7af7042013-10-12 22:38:11 -05002285 /* the first finger down picks the view, and all further go
2286 * to that view for the remainder of the touch session i.e.
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002287 * until all touch points are up again. */
Jonas Ådahl9484b692013-12-02 22:05:03 +01002288 if (touch->num_tp == 1) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002289 ev = weston_compositor_pick_view(ec, x, y, &sx, &sy);
Derek Foreman4c93c082015-04-30 16:45:41 -05002290 weston_touch_set_focus(touch, ev);
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002291 } else if (!touch->focus) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002292 /* Unexpected condition: We have non-initial touch but
2293 * there is no focused surface.
2294 */
Chris Michael3f607d32015-10-07 11:59:49 -04002295 weston_log("touch event received with %d points down "
Jonas Ådahl9484b692013-12-02 22:05:03 +01002296 "but no surface focused\n", touch->num_tp);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002297 return;
2298 }
2299
Derek Foreman99a6a2d2015-07-15 13:00:43 -05002300 weston_compositor_run_touch_binding(ec, touch,
Kristian Høgsbergc8964012014-02-05 14:25:18 -08002301 time, touch_type);
2302
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002303 grab->interface->down(grab, time, touch_id, x, y);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002304 if (touch->num_tp == 1) {
Rusty Lynchf1407ff2013-08-08 21:13:57 -07002305 touch->grab_serial =
2306 wl_display_get_serial(ec->wl_display);
Neil Roberts306fe082013-10-03 16:43:06 +01002307 touch->grab_touch_id = touch_id;
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02002308 touch->grab_time = *time;
Rusty Lynchf1407ff2013-08-08 21:13:57 -07002309 touch->grab_x = x;
2310 touch->grab_y = y;
2311 }
2312
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002313 break;
2314 case WL_TOUCH_MOTION:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002315 ev = touch->focus;
2316 if (!ev)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002317 break;
2318
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +02002319 grab->interface->motion(grab, time, touch_id, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002320 break;
2321 case WL_TOUCH_UP:
Kristian Høgsberga30e29a2014-01-08 22:29:20 -08002322 if (touch->num_tp == 0) {
2323 /* This can happen if we start out with one or
2324 * more fingers on the touch screen, in which
2325 * case we didn't get the corresponding down
2326 * event. */
2327 weston_log("unmatched touch up event\n");
2328 break;
2329 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002330 weston_compositor_idle_release(ec);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002331 touch->num_tp--;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002332
Alexandros Frantzis27a51b82017-11-16 18:20:59 +02002333 grab->interface->up(grab, time, touch_id);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002334 if (touch->num_tp == 0)
Derek Foreman4c93c082015-04-30 16:45:41 -05002335 weston_touch_set_focus(touch, NULL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002336 break;
2337 }
2338}
2339
Jonas Ådahl1679f232014-04-12 09:39:51 +02002340WL_EXPORT void
2341notify_touch_frame(struct weston_seat *seat)
2342{
Derek Foreman1281a362015-07-31 16:55:32 -05002343 struct weston_touch *touch = weston_seat_get_touch(seat);
Jonas Ådahl1679f232014-04-12 09:39:51 +02002344 struct weston_touch_grab *grab = touch->grab;
2345
2346 grab->interface->frame(grab);
2347}
2348
Derek Foreman3cc004a2015-11-06 15:56:09 -06002349WL_EXPORT void
2350notify_touch_cancel(struct weston_seat *seat)
2351{
2352 struct weston_touch *touch = weston_seat_get_touch(seat);
2353 struct weston_touch_grab *grab = touch->grab;
2354
2355 grab->interface->cancel(grab);
2356}
2357
Pekka Paalanen8274d902014-08-06 19:36:51 +03002358static int
2359pointer_cursor_surface_get_label(struct weston_surface *surface,
2360 char *buf, size_t len)
2361{
2362 return snprintf(buf, len, "cursor");
2363}
2364
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002365static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002366pointer_cursor_surface_committed(struct weston_surface *es,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002367 int32_t dx, int32_t dy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002368{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002369 struct weston_pointer *pointer = es->committed_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002370 int x, y;
2371
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002372 if (es->width == 0)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002373 return;
2374
Jason Ekstranda7af7042013-10-12 22:38:11 -05002375 assert(es == pointer->sprite->surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002376
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002377 pointer->hotspot_x -= dx;
2378 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002379
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002380 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
2381 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002382
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002383 weston_view_set_position(pointer->sprite, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002384
2385 empty_region(&es->pending.input);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02002386 empty_region(&es->input);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002387
2388 if (!weston_surface_is_mapped(es)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002389 weston_layer_entry_insert(&es->compositor->cursor_layer.view_list,
2390 &pointer->sprite->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002391 weston_view_update_transform(pointer->sprite);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002392 es->is_mapped = true;
2393 pointer->sprite->is_mapped = true;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002394 }
2395}
2396
2397static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002398pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2399 uint32_t serial, struct wl_resource *surface_resource,
2400 int32_t x, int32_t y)
2401{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002402 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002403 struct weston_surface *surface = NULL;
2404
Alexandros Frantzis1c3a40e2018-02-08 15:37:53 +02002405 if (!pointer)
2406 return;
2407
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002408 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002409 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002410
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002411 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002412 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002413 /* pointer->focus->surface->resource can be NULL. Surfaces like the
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02002414 black_surface used in shell.c for fullscreen don't have
2415 a resource, but can still have focus */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002416 if (pointer->focus->surface->resource == NULL)
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02002417 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002418 if (wl_resource_get_client(pointer->focus->surface->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002419 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002420 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002421 return;
2422
Derek Foreman4e53c532015-03-23 10:55:32 -05002423 if (!surface) {
2424 if (pointer->sprite)
2425 pointer_unmap_sprite(pointer);
2426 return;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002427 }
2428
Jonas Ådahlb4070242015-03-18 15:08:03 +08002429 if (pointer->sprite && pointer->sprite->surface == surface &&
2430 pointer->hotspot_x == x && pointer->hotspot_y == y)
2431 return;
2432
Derek Foreman4e53c532015-03-23 10:55:32 -05002433 if (!pointer->sprite || pointer->sprite->surface != surface) {
2434 if (weston_surface_set_role(surface, "wl_pointer-cursor",
2435 resource,
2436 WL_POINTER_ERROR_ROLE) < 0)
2437 return;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002438
Derek Foreman4e53c532015-03-23 10:55:32 -05002439 if (pointer->sprite)
2440 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002441
Derek Foreman4e53c532015-03-23 10:55:32 -05002442 wl_signal_add(&surface->destroy_signal,
2443 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002444
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002445 surface->committed = pointer_cursor_surface_committed;
2446 surface->committed_private = pointer;
Derek Foreman4e53c532015-03-23 10:55:32 -05002447 weston_surface_set_label_func(surface,
2448 pointer_cursor_surface_get_label);
2449 pointer->sprite = weston_view_create(surface);
2450 }
2451
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002452 pointer->hotspot_x = x;
2453 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002454
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +02002455 if (surface->buffer_ref.buffer) {
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002456 pointer_cursor_surface_committed(surface, 0, 0);
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +02002457 weston_view_schedule_repaint(pointer->sprite);
2458 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002459}
2460
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002461static void
2462pointer_release(struct wl_client *client, struct wl_resource *resource)
2463{
2464 wl_resource_destroy(resource);
2465}
2466
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002467static const struct wl_pointer_interface pointer_interface = {
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002468 pointer_set_cursor,
2469 pointer_release
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002470};
2471
2472static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002473seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2474 uint32_t id)
2475{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002476 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002477 /* We use the pointer_state directly, which means we'll
2478 * give a wl_pointer if the seat has ever had one - even though
2479 * the spec explicitly states that this request only takes effect
2480 * if the seat has the pointer capability.
2481 *
2482 * This prevents a race between the compositor sending new
2483 * capabilities and the client trying to use the old ones.
2484 */
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002485 struct weston_pointer *pointer = seat ? seat->pointer_state : NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002486 struct wl_resource *cr;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002487 struct weston_pointer_client *pointer_client;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002488
Jason Ekstranda85118c2013-06-27 20:17:02 -05002489 cr = wl_resource_create(client, &wl_pointer_interface,
2490 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002491 if (cr == NULL) {
2492 wl_client_post_no_memory(client);
2493 return;
2494 }
2495
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002496 wl_list_init(wl_resource_get_link(cr));
2497 wl_resource_set_implementation(cr, &pointer_interface, pointer,
2498 unbind_pointer_client_resource);
2499
2500 /* If we don't have a pointer_state, the resource is inert, so there
2501 * is nothing more to set up */
2502 if (!pointer)
2503 return;
2504
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002505 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
2506 if (!pointer_client) {
2507 wl_client_post_no_memory(client);
2508 return;
2509 }
2510
2511 wl_list_insert(&pointer_client->pointer_resources,
2512 wl_resource_get_link(cr));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002513
Derek Foreman1281a362015-07-31 16:55:32 -05002514 if (pointer->focus && pointer->focus->surface->resource &&
2515 wl_resource_get_client(pointer->focus->surface->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002516 wl_fixed_t sx, sy;
2517
Derek Foreman1281a362015-07-31 16:55:32 -05002518 weston_view_from_global_fixed(pointer->focus,
2519 pointer->x,
2520 pointer->y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002521 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002522
Neil Roberts96d790e2013-09-19 17:32:00 +01002523 wl_pointer_send_enter(cr,
Derek Foreman1281a362015-07-31 16:55:32 -05002524 pointer->focus_serial,
2525 pointer->focus->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01002526 sx, sy);
Peter Hutterer87743e92016-01-18 16:38:22 +10002527 pointer_send_frame(cr);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002528 }
2529}
2530
2531static void
Alexandros Frantzis2b442482018-02-20 14:05:50 +02002532destroy_keyboard_resource(struct wl_resource *resource)
2533{
2534 struct weston_keyboard *keyboard = wl_resource_get_user_data(resource);
2535
2536 wl_list_remove(wl_resource_get_link(resource));
2537
2538 if (keyboard) {
2539 remove_input_resource_from_timestamps(resource,
2540 &keyboard->timestamps_list);
2541 }
2542}
2543
2544static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002545keyboard_release(struct wl_client *client, struct wl_resource *resource)
2546{
2547 wl_resource_destroy(resource);
2548}
2549
2550static const struct wl_keyboard_interface keyboard_interface = {
2551 keyboard_release
2552};
2553
Derek Foreman280e7dd2014-10-03 13:13:42 -05002554static bool
Neil Roberts96d790e2013-09-19 17:32:00 +01002555should_send_modifiers_to_client(struct weston_seat *seat,
2556 struct wl_client *client)
2557{
Derek Foreman1281a362015-07-31 16:55:32 -05002558 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2559 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2560
2561 if (keyboard &&
2562 keyboard->focus &&
2563 keyboard->focus->resource &&
2564 wl_resource_get_client(keyboard->focus->resource) == client)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002565 return true;
Neil Roberts96d790e2013-09-19 17:32:00 +01002566
Derek Foreman1281a362015-07-31 16:55:32 -05002567 if (pointer &&
2568 pointer->focus &&
2569 pointer->focus->surface->resource &&
2570 wl_resource_get_client(pointer->focus->surface->resource) == client)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002571 return true;
Neil Roberts96d790e2013-09-19 17:32:00 +01002572
Derek Foreman280e7dd2014-10-03 13:13:42 -05002573 return false;
Neil Roberts96d790e2013-09-19 17:32:00 +01002574}
2575
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002576static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002577seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2578 uint32_t id)
2579{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002580 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002581 /* We use the keyboard_state directly, which means we'll
2582 * give a wl_keyboard if the seat has ever had one - even though
2583 * the spec explicitly states that this request only takes effect
2584 * if the seat has the keyboard capability.
2585 *
2586 * This prevents a race between the compositor sending new
2587 * capabilities and the client trying to use the old ones.
2588 */
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002589 struct weston_keyboard *keyboard = seat ? seat->keyboard_state : NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002590 struct wl_resource *cr;
2591
Jason Ekstranda85118c2013-06-27 20:17:02 -05002592 cr = wl_resource_create(client, &wl_keyboard_interface,
2593 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002594 if (cr == NULL) {
2595 wl_client_post_no_memory(client);
2596 return;
2597 }
2598
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002599 wl_list_init(wl_resource_get_link(cr));
2600 wl_resource_set_implementation(cr, &keyboard_interface,
Alexandros Frantzis2b442482018-02-20 14:05:50 +02002601 keyboard, destroy_keyboard_resource);
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002602
2603 /* If we don't have a keyboard_state, the resource is inert, so there
2604 * is nothing more to set up */
2605 if (!keyboard)
2606 return;
2607
Neil Roberts96d790e2013-09-19 17:32:00 +01002608 /* May be moved to focused list later by either
2609 * weston_keyboard_set_focus or directly if this client is already
2610 * focused */
Derek Foreman345c9f32015-06-03 15:53:28 -05002611 wl_list_insert(&keyboard->resource_list, wl_resource_get_link(cr));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002612
Jonny Lamb66a41a02014-08-12 14:58:25 +02002613 if (wl_resource_get_version(cr) >= WL_KEYBOARD_REPEAT_INFO_SINCE_VERSION) {
2614 wl_keyboard_send_repeat_info(cr,
2615 seat->compositor->kb_repeat_rate,
2616 seat->compositor->kb_repeat_delay);
2617 }
Jasper St. Pierred8c6aeb2014-08-04 13:43:24 -04002618
Derek Foreman185d1582017-06-28 11:17:23 -05002619 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2620 keyboard->xkb_info->keymap_fd,
2621 keyboard->xkb_info->keymap_size);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002622
Neil Roberts96d790e2013-09-19 17:32:00 +01002623 if (should_send_modifiers_to_client(seat, client)) {
Derek Foreman345c9f32015-06-03 15:53:28 -05002624 send_modifiers_to_resource(keyboard,
Neil Roberts96d790e2013-09-19 17:32:00 +01002625 cr,
Derek Foreman345c9f32015-06-03 15:53:28 -05002626 keyboard->focus_serial);
Neil Roberts96d790e2013-09-19 17:32:00 +01002627 }
2628
Derek Foreman345c9f32015-06-03 15:53:28 -05002629 if (keyboard->focus && keyboard->focus->resource &&
2630 wl_resource_get_client(keyboard->focus->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01002631 struct weston_surface *surface =
Derek Foreman345c9f32015-06-03 15:53:28 -05002632 (struct weston_surface *)keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +01002633
2634 wl_list_remove(wl_resource_get_link(cr));
Derek Foreman345c9f32015-06-03 15:53:28 -05002635 wl_list_insert(&keyboard->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002636 wl_resource_get_link(cr));
2637 wl_keyboard_send_enter(cr,
Derek Foreman345c9f32015-06-03 15:53:28 -05002638 keyboard->focus_serial,
Neil Roberts96d790e2013-09-19 17:32:00 +01002639 surface->resource,
Derek Foreman345c9f32015-06-03 15:53:28 -05002640 &keyboard->keys);
Neil Roberts96d790e2013-09-19 17:32:00 +01002641
2642 /* If this is the first keyboard resource for this
2643 * client... */
Derek Foreman345c9f32015-06-03 15:53:28 -05002644 if (keyboard->focus_resource_list.prev ==
Neil Roberts96d790e2013-09-19 17:32:00 +01002645 wl_resource_get_link(cr))
2646 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002647 }
2648}
2649
2650static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002651touch_release(struct wl_client *client, struct wl_resource *resource)
2652{
2653 wl_resource_destroy(resource);
2654}
2655
2656static const struct wl_touch_interface touch_interface = {
2657 touch_release
2658};
2659
2660static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002661seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2662 uint32_t id)
2663{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002664 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002665 /* We use the touch_state directly, which means we'll
2666 * give a wl_touch if the seat has ever had one - even though
2667 * the spec explicitly states that this request only takes effect
2668 * if the seat has the touch capability.
2669 *
2670 * This prevents a race between the compositor sending new
2671 * capabilities and the client trying to use the old ones.
2672 */
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002673 struct weston_touch *touch = seat ? seat->touch_state : NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002674 struct wl_resource *cr;
2675
Jason Ekstranda85118c2013-06-27 20:17:02 -05002676 cr = wl_resource_create(client, &wl_touch_interface,
2677 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002678 if (cr == NULL) {
2679 wl_client_post_no_memory(client);
2680 return;
2681 }
2682
Alexandros Frantzis8480d132018-02-15 13:07:09 +02002683 wl_list_init(wl_resource_get_link(cr));
2684 wl_resource_set_implementation(cr, &touch_interface,
2685 touch, unbind_resource);
2686
2687 /* If we don't have a touch_state, the resource is inert, so there
2688 * is nothing more to set up */
2689 if (!touch)
2690 return;
2691
Derek Foreman1281a362015-07-31 16:55:32 -05002692 if (touch->focus &&
2693 wl_resource_get_client(touch->focus->surface->resource) == client) {
Chokshi, Mituld6697142015-10-09 08:28:47 +00002694 wl_list_insert(&touch->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002695 wl_resource_get_link(cr));
2696 } else {
Chokshi, Mituld6697142015-10-09 08:28:47 +00002697 wl_list_insert(&touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002698 wl_resource_get_link(cr));
2699 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002700}
2701
Quentin Glidicaab1d362016-03-13 17:49:08 +01002702static void
2703seat_release(struct wl_client *client, struct wl_resource *resource)
2704{
2705 wl_resource_destroy(resource);
2706}
2707
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002708static const struct wl_seat_interface seat_interface = {
2709 seat_get_pointer,
2710 seat_get_keyboard,
2711 seat_get_touch,
Quentin Glidicaab1d362016-03-13 17:49:08 +01002712 seat_release,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002713};
2714
2715static void
2716bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2717{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002718 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002719 struct wl_resource *resource;
2720 enum wl_seat_capability caps = 0;
2721
Jason Ekstranda85118c2013-06-27 20:17:02 -05002722 resource = wl_resource_create(client,
Derek Foreman1909c102015-11-26 14:17:47 -06002723 &wl_seat_interface, version, id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05002724 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05002725 wl_resource_set_implementation(resource, &seat_interface, data,
2726 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002727
Derek Foreman1281a362015-07-31 16:55:32 -05002728 if (weston_seat_get_pointer(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002729 caps |= WL_SEAT_CAPABILITY_POINTER;
Derek Foreman1281a362015-07-31 16:55:32 -05002730 if (weston_seat_get_keyboard(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002731 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Derek Foreman1281a362015-07-31 16:55:32 -05002732 if (weston_seat_get_touch(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002733 caps |= WL_SEAT_CAPABILITY_TOUCH;
2734
2735 wl_seat_send_capabilities(resource, caps);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04002736 if (version >= WL_SEAT_NAME_SINCE_VERSION)
Rob Bradforde445ae62013-05-31 18:09:51 +01002737 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002738}
2739
Jonas Ådahl30d61d82014-10-22 21:21:17 +02002740static void
2741relative_pointer_destroy(struct wl_client *client,
2742 struct wl_resource *resource)
2743{
2744 wl_resource_destroy(resource);
2745}
2746
2747static const struct zwp_relative_pointer_v1_interface relative_pointer_interface = {
2748 relative_pointer_destroy
2749};
2750
2751static void
2752relative_pointer_manager_destroy(struct wl_client *client,
2753 struct wl_resource *resource)
2754{
2755 wl_resource_destroy(resource);
2756}
2757
2758static void
2759relative_pointer_manager_get_relative_pointer(struct wl_client *client,
2760 struct wl_resource *resource,
2761 uint32_t id,
2762 struct wl_resource *pointer_resource)
2763{
2764 struct weston_pointer *pointer =
2765 wl_resource_get_user_data(pointer_resource);
2766 struct weston_pointer_client *pointer_client;
2767 struct wl_resource *cr;
2768
2769 cr = wl_resource_create(client, &zwp_relative_pointer_v1_interface,
2770 wl_resource_get_version(resource), id);
2771 if (cr == NULL) {
2772 wl_client_post_no_memory(client);
2773 return;
2774 }
2775
2776 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
2777 if (!pointer_client) {
2778 wl_client_post_no_memory(client);
2779 return;
2780 }
2781
2782 wl_list_insert(&pointer_client->relative_pointer_resources,
2783 wl_resource_get_link(cr));
2784 wl_resource_set_implementation(cr, &relative_pointer_interface,
2785 pointer,
2786 unbind_pointer_client_resource);
2787}
2788
2789static const struct zwp_relative_pointer_manager_v1_interface relative_pointer_manager = {
2790 relative_pointer_manager_destroy,
2791 relative_pointer_manager_get_relative_pointer,
2792};
2793
2794static void
2795bind_relative_pointer_manager(struct wl_client *client, void *data,
2796 uint32_t version, uint32_t id)
2797{
2798 struct weston_compositor *compositor = data;
2799 struct wl_resource *resource;
2800
2801 resource = wl_resource_create(client,
2802 &zwp_relative_pointer_manager_v1_interface,
2803 1, id);
2804
2805 wl_resource_set_implementation(resource, &relative_pointer_manager,
2806 compositor,
2807 NULL);
2808}
2809
Giulio Camuffo0358af42016-06-02 21:48:08 +03002810WL_EXPORT int
2811weston_compositor_set_xkb_rule_names(struct weston_compositor *ec,
2812 struct xkb_rule_names *names)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002813{
2814 if (ec->xkb_context == NULL) {
2815 ec->xkb_context = xkb_context_new(0);
2816 if (ec->xkb_context == NULL) {
2817 weston_log("failed to create XKB context\n");
2818 return -1;
2819 }
2820 }
2821
2822 if (names)
2823 ec->xkb_names = *names;
2824 if (!ec->xkb_names.rules)
2825 ec->xkb_names.rules = strdup("evdev");
2826 if (!ec->xkb_names.model)
2827 ec->xkb_names.model = strdup("pc105");
2828 if (!ec->xkb_names.layout)
2829 ec->xkb_names.layout = strdup("us");
2830
2831 return 0;
2832}
2833
Stefan Schmidtfda26522013-09-17 10:54:09 +01002834static void
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002835weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002836{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002837 if (--xkb_info->ref_count > 0)
2838 return;
2839
Ran Benitac9c74152014-08-19 23:59:52 +03002840 xkb_keymap_unref(xkb_info->keymap);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002841
2842 if (xkb_info->keymap_area)
2843 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2844 if (xkb_info->keymap_fd >= 0)
2845 close(xkb_info->keymap_fd);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002846 free(xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002847}
2848
2849void
2850weston_compositor_xkb_destroy(struct weston_compositor *ec)
2851{
2852 free((char *) ec->xkb_names.rules);
2853 free((char *) ec->xkb_names.model);
2854 free((char *) ec->xkb_names.layout);
2855 free((char *) ec->xkb_names.variant);
2856 free((char *) ec->xkb_names.options);
Stefan Schmidtfda26522013-09-17 10:54:09 +01002857
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002858 if (ec->xkb_info)
2859 weston_xkb_info_destroy(ec->xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002860 xkb_context_unref(ec->xkb_context);
2861}
2862
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002863static struct weston_xkb_info *
2864weston_xkb_info_create(struct xkb_keymap *keymap)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002865{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002866 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
2867 if (xkb_info == NULL)
2868 return NULL;
2869
Ran Benita2e1968f2014-08-19 23:59:51 +03002870 xkb_info->keymap = xkb_keymap_ref(keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002871 xkb_info->ref_count = 1;
2872
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002873 char *keymap_str;
2874
Ran Benita2e1968f2014-08-19 23:59:51 +03002875 xkb_info->shift_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2876 XKB_MOD_NAME_SHIFT);
2877 xkb_info->caps_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2878 XKB_MOD_NAME_CAPS);
2879 xkb_info->ctrl_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2880 XKB_MOD_NAME_CTRL);
2881 xkb_info->alt_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2882 XKB_MOD_NAME_ALT);
2883 xkb_info->mod2_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2884 "Mod2");
2885 xkb_info->mod3_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2886 "Mod3");
2887 xkb_info->super_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2888 XKB_MOD_NAME_LOGO);
2889 xkb_info->mod5_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2890 "Mod5");
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002891
Ran Benita2e1968f2014-08-19 23:59:51 +03002892 xkb_info->num_led = xkb_keymap_led_get_index(xkb_info->keymap,
2893 XKB_LED_NAME_NUM);
2894 xkb_info->caps_led = xkb_keymap_led_get_index(xkb_info->keymap,
2895 XKB_LED_NAME_CAPS);
2896 xkb_info->scroll_led = xkb_keymap_led_get_index(xkb_info->keymap,
2897 XKB_LED_NAME_SCROLL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002898
Ran Benita2e1968f2014-08-19 23:59:51 +03002899 keymap_str = xkb_keymap_get_as_string(xkb_info->keymap,
2900 XKB_KEYMAP_FORMAT_TEXT_V1);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002901 if (keymap_str == NULL) {
2902 weston_log("failed to get string version of keymap\n");
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002903 goto err_keymap;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002904 }
2905 xkb_info->keymap_size = strlen(keymap_str) + 1;
2906
2907 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
2908 if (xkb_info->keymap_fd < 0) {
2909 weston_log("creating a keymap file for %lu bytes failed: %m\n",
2910 (unsigned long) xkb_info->keymap_size);
2911 goto err_keymap_str;
2912 }
2913
2914 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2915 PROT_READ | PROT_WRITE,
2916 MAP_SHARED, xkb_info->keymap_fd, 0);
2917 if (xkb_info->keymap_area == MAP_FAILED) {
2918 weston_log("failed to mmap() %lu bytes\n",
2919 (unsigned long) xkb_info->keymap_size);
2920 goto err_dev_zero;
2921 }
2922 strcpy(xkb_info->keymap_area, keymap_str);
2923 free(keymap_str);
2924
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002925 return xkb_info;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002926
2927err_dev_zero:
2928 close(xkb_info->keymap_fd);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002929err_keymap_str:
2930 free(keymap_str);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002931err_keymap:
Ran Benita2e1968f2014-08-19 23:59:51 +03002932 xkb_keymap_unref(xkb_info->keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002933 free(xkb_info);
2934 return NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002935}
2936
2937static int
2938weston_compositor_build_global_keymap(struct weston_compositor *ec)
2939{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002940 struct xkb_keymap *keymap;
2941
2942 if (ec->xkb_info != NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002943 return 0;
2944
Ran Benita2e1968f2014-08-19 23:59:51 +03002945 keymap = xkb_keymap_new_from_names(ec->xkb_context,
2946 &ec->xkb_names,
2947 0);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002948 if (keymap == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002949 weston_log("failed to compile global XKB keymap\n");
2950 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
2951 "options %s\n",
2952 ec->xkb_names.rules, ec->xkb_names.model,
2953 ec->xkb_names.layout, ec->xkb_names.variant,
2954 ec->xkb_names.options);
2955 return -1;
2956 }
2957
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002958 ec->xkb_info = weston_xkb_info_create(keymap);
Rui Matos73d93952013-10-24 19:28:41 +02002959 xkb_keymap_unref(keymap);
Stefan Schmidtfda26522013-09-17 10:54:09 +01002960 if (ec->xkb_info == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002961 return -1;
2962
2963 return 0;
2964}
2965
Rui Matos65196bc2013-10-10 19:44:19 +02002966WL_EXPORT void
2967weston_seat_update_keymap(struct weston_seat *seat, struct xkb_keymap *keymap)
2968{
Derek Foreman1281a362015-07-31 16:55:32 -05002969 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2970
2971 if (!keyboard || !keymap)
Rui Matos65196bc2013-10-10 19:44:19 +02002972 return;
2973
Derek Foreman1281a362015-07-31 16:55:32 -05002974 xkb_keymap_unref(keyboard->pending_keymap);
2975 keyboard->pending_keymap = xkb_keymap_ref(keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02002976
Derek Foreman1281a362015-07-31 16:55:32 -05002977 if (keyboard->keys.size == 0)
Rui Matos65196bc2013-10-10 19:44:19 +02002978 update_keymap(seat);
Rui Matos65196bc2013-10-10 19:44:19 +02002979}
2980
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002981WL_EXPORT int
2982weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
2983{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002984 struct weston_keyboard *keyboard;
2985
Derek Foreman1281a362015-07-31 16:55:32 -05002986 if (seat->keyboard_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002987 seat->keyboard_device_count += 1;
2988 if (seat->keyboard_device_count == 1)
2989 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002990 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002991 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002992
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002993 keyboard = weston_keyboard_create();
2994 if (keyboard == NULL) {
2995 weston_log("failed to allocate weston keyboard struct\n");
2996 return -1;
2997 }
2998
Derek Foreman185d1582017-06-28 11:17:23 -05002999 if (keymap != NULL) {
3000 keyboard->xkb_info = weston_xkb_info_create(keymap);
3001 if (keyboard->xkb_info == NULL)
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02003002 goto err;
Derek Foreman185d1582017-06-28 11:17:23 -05003003 } else {
3004 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
3005 goto err;
3006 keyboard->xkb_info = seat->compositor->xkb_info;
3007 keyboard->xkb_info->ref_count++;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01003008 }
Derek Foreman185d1582017-06-28 11:17:23 -05003009
3010 keyboard->xkb_state.state = xkb_state_new(keyboard->xkb_info->keymap);
3011 if (keyboard->xkb_state.state == NULL) {
3012 weston_log("failed to initialise XKB state\n");
3013 goto err;
3014 }
3015
3016 keyboard->xkb_state.leds = 0;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01003017
Derek Foreman1281a362015-07-31 16:55:32 -05003018 seat->keyboard_state = keyboard;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02003019 seat->keyboard_device_count = 1;
3020 keyboard->seat = seat;
3021
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003022 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003023
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003024 return 0;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02003025
3026err:
3027 if (keyboard->xkb_info)
3028 weston_xkb_info_destroy(keyboard->xkb_info);
3029 free(keyboard);
3030
3031 return -1;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003032}
3033
Jonas Ådahl91fed542013-12-03 09:14:27 +01003034static void
3035weston_keyboard_reset_state(struct weston_keyboard *keyboard)
3036{
3037 struct weston_seat *seat = keyboard->seat;
3038 struct xkb_state *state;
3039
Derek Foreman185d1582017-06-28 11:17:23 -05003040 state = xkb_state_new(keyboard->xkb_info->keymap);
3041 if (!state) {
3042 weston_log("failed to reset XKB state\n");
3043 return;
Jonas Ådahl91fed542013-12-03 09:14:27 +01003044 }
Derek Foreman185d1582017-06-28 11:17:23 -05003045 xkb_state_unref(keyboard->xkb_state.state);
3046 keyboard->xkb_state.state = state;
3047
3048 keyboard->xkb_state.leds = 0;
Jonas Ådahl91fed542013-12-03 09:14:27 +01003049
3050 seat->modifier_state = 0;
3051}
3052
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003053WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003054weston_seat_release_keyboard(struct weston_seat *seat)
3055{
3056 seat->keyboard_device_count--;
Derek Foremand621df22014-11-19 11:04:12 -06003057 assert(seat->keyboard_device_count >= 0);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003058 if (seat->keyboard_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05003059 weston_keyboard_set_focus(seat->keyboard_state, NULL);
3060 weston_keyboard_cancel_grab(seat->keyboard_state);
3061 weston_keyboard_reset_state(seat->keyboard_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003062 seat_send_updated_caps(seat);
3063 }
3064}
3065
3066WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003067weston_seat_init_pointer(struct weston_seat *seat)
3068{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003069 struct weston_pointer *pointer;
3070
Derek Foreman1281a362015-07-31 16:55:32 -05003071 if (seat->pointer_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003072 seat->pointer_device_count += 1;
3073 if (seat->pointer_device_count == 1)
3074 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003075 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003076 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003077
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003078 pointer = weston_pointer_create(seat);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003079 if (pointer == NULL)
3080 return;
3081
Derek Foreman1281a362015-07-31 16:55:32 -05003082 seat->pointer_state = pointer;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003083 seat->pointer_device_count = 1;
3084 pointer->seat = seat;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003085
3086 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003087}
3088
3089WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003090weston_seat_release_pointer(struct weston_seat *seat)
3091{
Derek Foreman1281a362015-07-31 16:55:32 -05003092 struct weston_pointer *pointer = seat->pointer_state;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003093
3094 seat->pointer_device_count--;
3095 if (seat->pointer_device_count == 0) {
Derek Foremanf9318d12015-05-11 15:40:11 -05003096 weston_pointer_clear_focus(pointer);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003097 weston_pointer_cancel_grab(pointer);
Jonas Ådahl630bae82013-10-17 23:04:06 +02003098
Jonas Ådahla4932742013-10-17 23:04:07 +02003099 if (pointer->sprite)
3100 pointer_unmap_sprite(pointer);
3101
Jonas Ådahl3e12e632013-12-02 22:05:05 +01003102 weston_pointer_reset_state(pointer);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003103 seat_send_updated_caps(seat);
Derek Foremanfd5ca512015-01-07 15:00:25 -06003104
3105 /* seat->pointer is intentionally not destroyed so that
3106 * a newly attached pointer on this seat will retain
3107 * the previous cursor co-ordinates.
3108 */
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003109 }
3110}
3111
3112WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003113weston_seat_init_touch(struct weston_seat *seat)
3114{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003115 struct weston_touch *touch;
3116
Derek Foreman1281a362015-07-31 16:55:32 -05003117 if (seat->touch_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003118 seat->touch_device_count += 1;
3119 if (seat->touch_device_count == 1)
3120 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003121 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003122 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003123
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003124 touch = weston_touch_create();
3125 if (touch == NULL)
3126 return;
3127
Derek Foreman1281a362015-07-31 16:55:32 -05003128 seat->touch_state = touch;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003129 seat->touch_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003130 touch->seat = seat;
3131
3132 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003133}
3134
3135WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003136weston_seat_release_touch(struct weston_seat *seat)
3137{
3138 seat->touch_device_count--;
3139 if (seat->touch_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05003140 weston_touch_set_focus(seat->touch_state, NULL);
3141 weston_touch_cancel_grab(seat->touch_state);
3142 weston_touch_reset_state(seat->touch_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003143 seat_send_updated_caps(seat);
3144 }
3145}
3146
3147WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003148weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
3149 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003150{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003151 memset(seat, 0, sizeof *seat);
3152
Kristian Høgsberge3148752013-05-06 23:19:49 -04003153 seat->selection_data_source = NULL;
3154 wl_list_init(&seat->base_resource_list);
3155 wl_signal_init(&seat->selection_signal);
3156 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04003157 wl_signal_init(&seat->destroy_signal);
Jason Ekstranda4ab5422014-04-02 19:53:45 -05003158 wl_signal_init(&seat->updated_caps_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003159
Peter Hutterer87743e92016-01-18 16:38:22 +10003160 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 5,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003161 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003162
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003163 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003164 seat->modifier_state = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003165 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003166
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003167 wl_list_insert(ec->seat_list.prev, &seat->link);
3168
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003169 clipboard_create(seat);
3170
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003171 wl_signal_emit(&ec->seat_created_signal, seat);
3172}
3173
3174WL_EXPORT void
3175weston_seat_release(struct weston_seat *seat)
3176{
Alexandros Frantzis8480d132018-02-15 13:07:09 +02003177 struct wl_resource *resource;
3178
3179 wl_resource_for_each(resource, &seat->base_resource_list) {
3180 wl_resource_set_user_data(resource, NULL);
3181 }
3182
3183 wl_resource_for_each(resource, &seat->drag_resource_list) {
3184 wl_resource_set_user_data(resource, NULL);
3185 }
3186
3187 wl_list_remove(&seat->base_resource_list);
3188 wl_list_remove(&seat->drag_resource_list);
3189
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003190 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003191
Jonas Ådahl1afb2382014-01-03 19:46:51 +01003192 if (seat->saved_kbd_focus)
3193 wl_list_remove(&seat->saved_kbd_focus_listener.link);
3194
Derek Foreman1281a362015-07-31 16:55:32 -05003195 if (seat->pointer_state)
3196 weston_pointer_destroy(seat->pointer_state);
3197 if (seat->keyboard_state)
3198 weston_keyboard_destroy(seat->keyboard_state);
3199 if (seat->touch_state)
3200 weston_touch_destroy(seat->touch_state);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003201
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003202 free (seat->seat_name);
3203
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003204 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04003205
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003206 wl_signal_emit(&seat->destroy_signal, seat);
3207}
Derek Foreman1281a362015-07-31 16:55:32 -05003208
3209/** Get a seat's keyboard pointer
3210 *
3211 * \param seat The seat to query
3212 * \return The seat's keyboard pointer, or NULL if no keyboard is present
3213 *
3214 * The keyboard pointer for a seat isn't freed when all keyboards are removed,
3215 * so it should only be used when the seat's keyboard_device_count is greater
3216 * than zero. This function does that test and only returns a pointer
3217 * when a keyboard is present.
3218 */
3219WL_EXPORT struct weston_keyboard *
3220weston_seat_get_keyboard(struct weston_seat *seat)
3221{
3222 if (!seat)
3223 return NULL;
3224
3225 if (seat->keyboard_device_count)
3226 return seat->keyboard_state;
3227
3228 return NULL;
3229}
3230
3231/** Get a seat's pointer pointer
3232 *
3233 * \param seat The seat to query
3234 * \return The seat's pointer pointer, or NULL if no pointer device is present
3235 *
3236 * The pointer pointer for a seat isn't freed when all mice are removed,
3237 * so it should only be used when the seat's pointer_device_count is greater
3238 * than zero. This function does that test and only returns a pointer
3239 * when a pointing device is present.
3240 */
3241WL_EXPORT struct weston_pointer *
3242weston_seat_get_pointer(struct weston_seat *seat)
3243{
3244 if (!seat)
3245 return NULL;
3246
3247 if (seat->pointer_device_count)
3248 return seat->pointer_state;
3249
3250 return NULL;
3251}
3252
Jonas Ådahld3414f22016-07-22 17:56:31 +08003253static const struct zwp_locked_pointer_v1_interface locked_pointer_interface;
3254static const struct zwp_confined_pointer_v1_interface confined_pointer_interface;
3255
3256static enum pointer_constraint_type
3257pointer_constraint_get_type(struct weston_pointer_constraint *constraint)
3258{
3259 if (wl_resource_instance_of(constraint->resource,
3260 &zwp_locked_pointer_v1_interface,
3261 &locked_pointer_interface)) {
3262 return POINTER_CONSTRAINT_TYPE_LOCK;
3263 } else if (wl_resource_instance_of(constraint->resource,
3264 &zwp_confined_pointer_v1_interface,
3265 &confined_pointer_interface)) {
3266 return POINTER_CONSTRAINT_TYPE_CONFINE;
3267 }
3268
3269 abort();
3270 return 0;
3271}
3272
3273static void
3274pointer_constraint_notify_activated(struct weston_pointer_constraint *constraint)
3275{
3276 struct wl_resource *resource = constraint->resource;
3277
3278 switch (pointer_constraint_get_type(constraint)) {
3279 case POINTER_CONSTRAINT_TYPE_LOCK:
3280 zwp_locked_pointer_v1_send_locked(resource);
3281 break;
3282 case POINTER_CONSTRAINT_TYPE_CONFINE:
3283 zwp_confined_pointer_v1_send_confined(resource);
3284 break;
3285 }
3286}
3287
3288static void
3289pointer_constraint_notify_deactivated(struct weston_pointer_constraint *constraint)
3290{
3291 struct wl_resource *resource = constraint->resource;
3292
3293 switch (pointer_constraint_get_type(constraint)) {
3294 case POINTER_CONSTRAINT_TYPE_LOCK:
3295 zwp_locked_pointer_v1_send_unlocked(resource);
3296 break;
3297 case POINTER_CONSTRAINT_TYPE_CONFINE:
3298 zwp_confined_pointer_v1_send_unconfined(resource);
3299 break;
3300 }
3301}
3302
3303static struct weston_pointer_constraint *
3304get_pointer_constraint_for_pointer(struct weston_surface *surface,
3305 struct weston_pointer *pointer)
3306{
3307 struct weston_pointer_constraint *constraint;
3308
3309 wl_list_for_each(constraint, &surface->pointer_constraints, link) {
3310 if (constraint->pointer == pointer)
3311 return constraint;
3312 }
3313
3314 return NULL;
3315}
3316
Derek Foreman1281a362015-07-31 16:55:32 -05003317/** Get a seat's touch pointer
3318 *
3319 * \param seat The seat to query
3320 * \return The seat's touch pointer, or NULL if no touch device is present
3321 *
3322 * The touch pointer for a seat isn't freed when all touch devices are removed,
3323 * so it should only be used when the seat's touch_device_count is greater
3324 * than zero. This function does that test and only returns a pointer
3325 * when a touch device is present.
3326 */
3327WL_EXPORT struct weston_touch *
3328weston_seat_get_touch(struct weston_seat *seat)
3329{
3330 if (!seat)
3331 return NULL;
3332
3333 if (seat->touch_device_count)
3334 return seat->touch_state;
3335
3336 return NULL;
3337}
Bryce Harrington24f917e2016-06-29 19:04:07 -07003338
3339/** Sets the keyboard focus to the given surface
3340 *
3341 * \param seat The seat to query
3342 */
3343WL_EXPORT void
3344weston_seat_set_keyboard_focus(struct weston_seat *seat,
3345 struct weston_surface *surface)
3346{
3347 struct weston_compositor *compositor = seat->compositor;
3348 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003349 struct weston_surface_activation_data activation_data;
Bryce Harrington24f917e2016-06-29 19:04:07 -07003350
Jonas Ådahlef8e1c32016-03-15 20:28:51 +08003351 if (keyboard && keyboard->focus != surface) {
Bryce Harrington24f917e2016-06-29 19:04:07 -07003352 weston_keyboard_set_focus(keyboard, surface);
3353 wl_data_device_set_keyboard_focus(seat);
3354 }
3355
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003356 inc_activate_serial(compositor);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003357
3358 activation_data = (struct weston_surface_activation_data) {
3359 .surface = surface,
3360 .seat = seat,
3361 };
3362 wl_signal_emit(&compositor->activate_signal, &activation_data);
Bryce Harrington24f917e2016-06-29 19:04:07 -07003363}
Jonas Ådahl30d61d82014-10-22 21:21:17 +02003364
Jonas Ådahld3414f22016-07-22 17:56:31 +08003365static void
3366enable_pointer_constraint(struct weston_pointer_constraint *constraint,
3367 struct weston_view *view)
3368{
3369 assert(constraint->view == NULL);
3370 constraint->view = view;
3371 pointer_constraint_notify_activated(constraint);
3372 weston_pointer_start_grab(constraint->pointer, &constraint->grab);
3373 wl_list_remove(&constraint->surface_destroy_listener.link);
3374 wl_list_init(&constraint->surface_destroy_listener.link);
3375}
3376
3377static bool
3378is_pointer_constraint_enabled(struct weston_pointer_constraint *constraint)
3379{
3380 return constraint->view != NULL;
3381}
3382
3383static void
3384weston_pointer_constraint_disable(struct weston_pointer_constraint *constraint)
3385{
3386 constraint->view = NULL;
3387 pointer_constraint_notify_deactivated(constraint);
3388 weston_pointer_end_grab(constraint->grab.pointer);
3389}
3390
3391void
3392weston_pointer_constraint_destroy(struct weston_pointer_constraint *constraint)
3393{
3394 if (is_pointer_constraint_enabled(constraint))
3395 weston_pointer_constraint_disable(constraint);
3396
3397 wl_list_remove(&constraint->pointer_destroy_listener.link);
3398 wl_list_remove(&constraint->surface_destroy_listener.link);
3399 wl_list_remove(&constraint->surface_commit_listener.link);
3400 wl_list_remove(&constraint->surface_activate_listener.link);
3401
3402 wl_resource_set_user_data(constraint->resource, NULL);
3403 pixman_region32_fini(&constraint->region);
3404 wl_list_remove(&constraint->link);
3405 free(constraint);
3406}
3407
3408static void
3409disable_pointer_constraint(struct weston_pointer_constraint *constraint)
3410{
3411 switch (constraint->lifetime) {
3412 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT:
3413 weston_pointer_constraint_destroy(constraint);
3414 break;
3415 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT:
3416 weston_pointer_constraint_disable(constraint);
3417 break;
3418 }
3419}
3420
3421static bool
3422is_within_constraint_region(struct weston_pointer_constraint *constraint,
3423 wl_fixed_t sx, wl_fixed_t sy)
3424{
3425 struct weston_surface *surface = constraint->surface;
3426 pixman_region32_t constraint_region;
3427 bool result;
3428
3429 pixman_region32_init(&constraint_region);
3430 pixman_region32_intersect(&constraint_region,
3431 &surface->input,
3432 &constraint->region);
3433 result = pixman_region32_contains_point(&constraint_region,
3434 wl_fixed_to_int(sx),
3435 wl_fixed_to_int(sy),
3436 NULL);
3437 pixman_region32_fini(&constraint_region);
3438
3439 return result;
3440}
3441
3442static void
3443maybe_enable_pointer_constraint(struct weston_pointer_constraint *constraint)
3444{
3445 struct weston_surface *surface = constraint->surface;
3446 struct weston_view *vit;
3447 struct weston_view *view = NULL;
3448 struct weston_pointer *pointer = constraint->pointer;
3449 struct weston_keyboard *keyboard;
3450 struct weston_seat *seat = pointer->seat;
3451 int32_t x, y;
3452
3453 /* Postpone if no view of the surface was most recently clicked. */
3454 wl_list_for_each(vit, &surface->views, surface_link) {
3455 if (vit->click_to_activate_serial ==
3456 surface->compositor->activate_serial) {
3457 view = vit;
3458 }
3459 }
3460 if (view == NULL)
3461 return;
3462
3463 /* Postpone if surface doesn't have keyboard focus. */
3464 keyboard = weston_seat_get_keyboard(seat);
3465 if (!keyboard || keyboard->focus != surface)
3466 return;
3467
3468 /* Postpone constraint if the pointer is not within the
3469 * constraint region.
3470 */
3471 weston_view_from_global(view,
3472 wl_fixed_to_int(pointer->x),
3473 wl_fixed_to_int(pointer->y),
3474 &x, &y);
3475 if (!is_within_constraint_region(constraint,
3476 wl_fixed_from_int(x),
3477 wl_fixed_from_int(y)))
3478 return;
3479
3480 enable_pointer_constraint(constraint, view);
3481}
3482
3483static void
3484locked_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
3485{
3486}
3487
3488static void
3489locked_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02003490 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003491 struct weston_pointer_motion_event *event)
3492{
Quentin Glidiccde13452016-08-12 10:41:32 +02003493 pointer_send_relative_motion(grab->pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08003494}
3495
3496static void
3497locked_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003498 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003499 uint32_t button,
3500 uint32_t state_w)
3501{
3502 weston_pointer_send_button(grab->pointer, time, button, state_w);
3503}
3504
3505static void
3506locked_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02003507 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003508 struct weston_pointer_axis_event *event)
3509{
3510 weston_pointer_send_axis(grab->pointer, time, event);
3511}
3512
3513static void
3514locked_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
3515 uint32_t source)
3516{
3517 weston_pointer_send_axis_source(grab->pointer, source);
3518}
3519
3520static void
3521locked_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
3522{
3523 weston_pointer_send_frame(grab->pointer);
3524}
3525
3526static void
3527locked_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
3528{
3529 struct weston_pointer_constraint *constraint =
3530 container_of(grab, struct weston_pointer_constraint, grab);
3531
3532 disable_pointer_constraint(constraint);
3533}
3534
3535static const struct weston_pointer_grab_interface
3536 locked_pointer_grab_interface = {
3537 locked_pointer_grab_pointer_focus,
3538 locked_pointer_grab_pointer_motion,
3539 locked_pointer_grab_pointer_button,
3540 locked_pointer_grab_pointer_axis,
3541 locked_pointer_grab_pointer_axis_source,
3542 locked_pointer_grab_pointer_frame,
3543 locked_pointer_grab_pointer_cancel,
3544};
3545
3546static void
3547pointer_constraint_constrain_resource_destroyed(struct wl_resource *resource)
3548{
3549 struct weston_pointer_constraint *constraint =
3550 wl_resource_get_user_data(resource);
3551
3552 if (!constraint)
3553 return;
3554
3555 weston_pointer_constraint_destroy(constraint);
3556}
3557
3558static void
3559pointer_constraint_surface_activate(struct wl_listener *listener, void *data)
3560{
3561 struct weston_surface_activation_data *activation = data;
3562 struct weston_pointer *pointer;
3563 struct weston_surface *focus = activation->surface;
3564 struct weston_pointer_constraint *constraint =
3565 container_of(listener, struct weston_pointer_constraint,
3566 surface_activate_listener);
3567 bool is_constraint_surface;
3568
3569 pointer = weston_seat_get_pointer(activation->seat);
3570 if (!pointer)
3571 return;
3572
3573 is_constraint_surface =
3574 get_pointer_constraint_for_pointer(focus, pointer) == constraint;
3575
3576 if (is_constraint_surface &&
3577 !is_pointer_constraint_enabled(constraint))
3578 maybe_enable_pointer_constraint(constraint);
3579 else if (!is_constraint_surface &&
3580 is_pointer_constraint_enabled(constraint))
3581 disable_pointer_constraint(constraint);
3582}
3583
3584static void
3585pointer_constraint_pointer_destroyed(struct wl_listener *listener, void *data)
3586{
3587 struct weston_pointer_constraint *constraint =
3588 container_of(listener, struct weston_pointer_constraint,
3589 pointer_destroy_listener);
3590
3591 weston_pointer_constraint_destroy(constraint);
3592}
3593
3594static void
3595pointer_constraint_surface_destroyed(struct wl_listener *listener, void *data)
3596{
3597 struct weston_pointer_constraint *constraint =
3598 container_of(listener, struct weston_pointer_constraint,
3599 surface_destroy_listener);
3600
3601 weston_pointer_constraint_destroy(constraint);
3602}
3603
3604static void
3605pointer_constraint_surface_committed(struct wl_listener *listener, void *data)
3606{
3607 struct weston_pointer_constraint *constraint =
3608 container_of(listener, struct weston_pointer_constraint,
3609 surface_commit_listener);
3610
3611 if (constraint->region_is_pending) {
3612 constraint->region_is_pending = false;
3613 pixman_region32_copy(&constraint->region,
3614 &constraint->region_pending);
3615 pixman_region32_fini(&constraint->region_pending);
3616 pixman_region32_init(&constraint->region_pending);
3617 }
3618
3619 if (constraint->hint_is_pending) {
3620 constraint->hint_is_pending = false;
3621
3622 constraint->hint_is_pending = true;
3623 constraint->hint_x = constraint->hint_x_pending;
3624 constraint->hint_y = constraint->hint_y_pending;
3625 }
3626
3627 if (pointer_constraint_get_type(constraint) ==
3628 POINTER_CONSTRAINT_TYPE_CONFINE &&
3629 is_pointer_constraint_enabled(constraint))
3630 maybe_warp_confined_pointer(constraint);
3631}
3632
3633static struct weston_pointer_constraint *
3634weston_pointer_constraint_create(struct weston_surface *surface,
3635 struct weston_pointer *pointer,
3636 struct weston_region *region,
3637 enum zwp_pointer_constraints_v1_lifetime lifetime,
3638 struct wl_resource *cr,
3639 const struct weston_pointer_grab_interface *grab_interface)
3640{
3641 struct weston_pointer_constraint *constraint;
3642
3643 constraint = zalloc(sizeof *constraint);
3644 if (!constraint)
3645 return NULL;
3646
3647 constraint->lifetime = lifetime;
3648 pixman_region32_init(&constraint->region);
3649 pixman_region32_init(&constraint->region_pending);
3650 wl_list_insert(&surface->pointer_constraints, &constraint->link);
3651 constraint->surface = surface;
3652 constraint->pointer = pointer;
3653 constraint->resource = cr;
3654 constraint->grab.interface = grab_interface;
3655 if (region) {
3656 pixman_region32_copy(&constraint->region,
3657 &region->region);
3658 } else {
3659 pixman_region32_fini(&constraint->region);
3660 region_init_infinite(&constraint->region);
3661 }
3662
3663 constraint->surface_activate_listener.notify =
3664 pointer_constraint_surface_activate;
3665 constraint->surface_destroy_listener.notify =
3666 pointer_constraint_surface_destroyed;
3667 constraint->surface_commit_listener.notify =
3668 pointer_constraint_surface_committed;
3669 constraint->pointer_destroy_listener.notify =
3670 pointer_constraint_pointer_destroyed;
3671
3672 wl_signal_add(&surface->compositor->activate_signal,
3673 &constraint->surface_activate_listener);
3674 wl_signal_add(&pointer->destroy_signal,
3675 &constraint->pointer_destroy_listener);
3676 wl_signal_add(&surface->destroy_signal,
3677 &constraint->surface_destroy_listener);
3678 wl_signal_add(&surface->commit_signal,
3679 &constraint->surface_commit_listener);
3680
3681 return constraint;
3682}
3683
3684static void
3685init_pointer_constraint(struct wl_resource *pointer_constraints_resource,
3686 uint32_t id,
3687 struct weston_surface *surface,
3688 struct weston_pointer *pointer,
3689 struct weston_region *region,
3690 enum zwp_pointer_constraints_v1_lifetime lifetime,
3691 const struct wl_interface *interface,
3692 const void *implementation,
3693 const struct weston_pointer_grab_interface *grab_interface)
3694{
3695 struct wl_client *client =
3696 wl_resource_get_client(pointer_constraints_resource);
3697 struct wl_resource *cr;
3698 struct weston_pointer_constraint *constraint;
3699
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02003700 if (pointer && get_pointer_constraint_for_pointer(surface, pointer)) {
Jonas Ådahld3414f22016-07-22 17:56:31 +08003701 wl_resource_post_error(pointer_constraints_resource,
3702 ZWP_POINTER_CONSTRAINTS_V1_ERROR_ALREADY_CONSTRAINED,
3703 "the pointer has a lock/confine request on this surface");
3704 return;
3705 }
3706
3707 cr = wl_resource_create(client, interface,
3708 wl_resource_get_version(pointer_constraints_resource),
3709 id);
3710 if (cr == NULL) {
3711 wl_client_post_no_memory(client);
3712 return;
3713 }
3714
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02003715 if (pointer) {
3716 constraint = weston_pointer_constraint_create(surface, pointer,
3717 region, lifetime,
3718 cr, grab_interface);
3719 if (constraint == NULL) {
3720 wl_client_post_no_memory(client);
3721 return;
3722 }
3723 } else {
3724 constraint = NULL;
Jonas Ådahld3414f22016-07-22 17:56:31 +08003725 }
3726
3727 wl_resource_set_implementation(cr, implementation, constraint,
3728 pointer_constraint_constrain_resource_destroyed);
3729
Alexandros Frantzis0f14ae92018-02-08 15:37:52 +02003730 if (constraint)
3731 maybe_enable_pointer_constraint(constraint);
Jonas Ådahld3414f22016-07-22 17:56:31 +08003732}
3733
3734static void
3735pointer_constraints_destroy(struct wl_client *client,
3736 struct wl_resource *resource)
3737{
3738 wl_resource_destroy(resource);
3739}
3740
3741static void
3742locked_pointer_destroy(struct wl_client *client,
3743 struct wl_resource *resource)
3744{
3745 struct weston_pointer_constraint *constraint =
3746 wl_resource_get_user_data(resource);
3747 wl_fixed_t x, y;
3748
3749 if (constraint && constraint->view && constraint->hint_is_pending &&
3750 is_within_constraint_region(constraint,
3751 constraint->hint_x,
3752 constraint->hint_y)) {
3753 weston_view_to_global_fixed(constraint->view,
3754 constraint->hint_x,
3755 constraint->hint_y,
3756 &x, &y);
3757 weston_pointer_move_to(constraint->pointer, x, y);
3758 }
3759 wl_resource_destroy(resource);
3760}
3761
3762static void
3763locked_pointer_set_cursor_position_hint(struct wl_client *client,
3764 struct wl_resource *resource,
3765 wl_fixed_t surface_x,
3766 wl_fixed_t surface_y)
3767{
3768 struct weston_pointer_constraint *constraint =
3769 wl_resource_get_user_data(resource);
3770
3771 /* Ignore a set cursor hint that was sent after the lock was cancelled.
3772 */
3773 if (!constraint ||
3774 !constraint->resource ||
3775 constraint->resource != resource)
3776 return;
3777
3778 constraint->hint_is_pending = true;
3779 constraint->hint_x_pending = surface_x;
3780 constraint->hint_y_pending = surface_y;
3781}
3782
3783static void
3784locked_pointer_set_region(struct wl_client *client,
3785 struct wl_resource *resource,
3786 struct wl_resource *region_resource)
3787{
3788 struct weston_pointer_constraint *constraint =
3789 wl_resource_get_user_data(resource);
3790 struct weston_region *region = region_resource ?
3791 wl_resource_get_user_data(region_resource) : NULL;
3792
3793 if (!constraint)
3794 return;
3795
3796 if (region) {
3797 pixman_region32_copy(&constraint->region_pending,
3798 &region->region);
3799 } else {
3800 pixman_region32_fini(&constraint->region_pending);
3801 region_init_infinite(&constraint->region_pending);
3802 }
3803 constraint->region_is_pending = true;
3804}
3805
3806
3807static const struct zwp_locked_pointer_v1_interface locked_pointer_interface = {
3808 locked_pointer_destroy,
3809 locked_pointer_set_cursor_position_hint,
3810 locked_pointer_set_region,
3811};
3812
3813static void
3814pointer_constraints_lock_pointer(struct wl_client *client,
3815 struct wl_resource *resource,
3816 uint32_t id,
3817 struct wl_resource *surface_resource,
3818 struct wl_resource *pointer_resource,
3819 struct wl_resource *region_resource,
3820 uint32_t lifetime)
3821{
3822 struct weston_surface *surface =
3823 wl_resource_get_user_data(surface_resource);
3824 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
3825 struct weston_region *region = region_resource ?
3826 wl_resource_get_user_data(region_resource) : NULL;
3827
3828 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
3829 &zwp_locked_pointer_v1_interface,
3830 &locked_pointer_interface,
3831 &locked_pointer_grab_interface);
3832}
3833
3834static void
3835confined_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
3836{
3837}
3838
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08003839static double
3840vec2d_cross_product(struct vec2d a, struct vec2d b)
3841{
3842 return a.x * b.y - a.y * b.x;
3843}
3844
3845static struct vec2d
3846vec2d_add(struct vec2d a, struct vec2d b)
3847{
3848 return (struct vec2d) {
3849 .x = a.x + b.x,
3850 .y = a.y + b.y,
3851 };
3852}
3853
3854static struct vec2d
3855vec2d_subtract(struct vec2d a, struct vec2d b)
3856{
3857 return (struct vec2d) {
3858 .x = a.x - b.x,
3859 .y = a.y - b.y,
3860 };
3861}
3862
3863static struct vec2d
3864vec2d_multiply_constant(double c, struct vec2d a)
3865{
3866 return (struct vec2d) {
3867 .x = c * a.x,
3868 .y = c * a.y,
3869 };
3870}
3871
3872static bool
3873lines_intersect(struct line *line1, struct line *line2,
3874 struct vec2d *intersection)
3875{
3876 struct vec2d p = line1->a;
3877 struct vec2d r = vec2d_subtract(line1->b, line1->a);
3878 struct vec2d q = line2->a;
3879 struct vec2d s = vec2d_subtract(line2->b, line2->a);
3880 double rxs;
3881 double sxr;
3882 double t;
3883 double u;
3884
3885 /*
3886 * The line (p, r) and (q, s) intersects where
3887 *
3888 * p + t r = q + u s
3889 *
3890 * Calculate t:
3891 *
3892 * (p + t r) × s = (q + u s) × s
3893 * p × s + t (r × s) = q × s + u (s × s)
3894 * p × s + t (r × s) = q × s
3895 * t (r × s) = q × s - p × s
3896 * t (r × s) = (q - p) × s
3897 * t = ((q - p) × s) / (r × s)
3898 *
3899 * Using the same method, for u we get:
3900 *
3901 * u = ((p - q) × r) / (s × r)
3902 */
3903
3904 rxs = vec2d_cross_product(r, s);
3905 sxr = vec2d_cross_product(s, r);
3906
3907 /* If r × s = 0 then the lines are either parallel or collinear. */
3908 if (fabs(rxs) < DBL_MIN)
3909 return false;
3910
3911 t = vec2d_cross_product(vec2d_subtract(q, p), s) / rxs;
3912 u = vec2d_cross_product(vec2d_subtract(p, q), r) / sxr;
3913
3914 /* The lines only intersect if 0 ≤ t ≤ 1 and 0 ≤ u ≤ 1. */
3915 if (t < 0.0 || t > 1.0 || u < 0.0 || u > 1.0)
3916 return false;
3917
3918 *intersection = vec2d_add(p, vec2d_multiply_constant(t, r));
3919 return true;
3920}
3921
3922static struct border *
3923add_border(struct wl_array *array,
3924 double x1, double y1,
3925 double x2, double y2,
3926 enum motion_direction blocking_dir)
3927{
3928 struct border *border = wl_array_add(array, sizeof *border);
3929
3930 *border = (struct border) {
3931 .line = (struct line) {
3932 .a = (struct vec2d) {
3933 .x = x1,
3934 .y = y1,
3935 },
3936 .b = (struct vec2d) {
3937 .x = x2,
3938 .y = y2,
3939 },
3940 },
3941 .blocking_dir = blocking_dir,
3942 };
3943
3944 return border;
3945}
3946
3947static int
3948compare_lines_x(const void *a, const void *b)
3949{
3950 const struct border *border_a = a;
3951 const struct border *border_b = b;
3952
3953
3954 if (border_a->line.a.x == border_b->line.a.x)
3955 return border_a->line.b.x < border_b->line.b.x;
3956 else
3957 return border_a->line.a.x > border_b->line.a.x;
3958}
3959
3960static void
3961add_non_overlapping_edges(pixman_box32_t *boxes,
3962 int band_above_start,
3963 int band_below_start,
3964 int band_below_end,
3965 struct wl_array *borders)
3966{
3967 int i;
3968 struct wl_array band_merge;
3969 struct border *border;
3970 struct border *prev_border;
3971 struct border *new_border;
3972
3973 wl_array_init(&band_merge);
3974
3975 /* Add bottom band of previous row, and top band of current row, and
3976 * sort them so lower left x coordinate comes first. If there are two
3977 * borders with the same left x coordinate, the wider one comes first.
3978 */
3979 for (i = band_above_start; i < band_below_start; i++) {
3980 pixman_box32_t *box = &boxes[i];
3981 add_border(&band_merge, box->x1, box->y2, box->x2, box->y2,
3982 MOTION_DIRECTION_POSITIVE_Y);
3983 }
3984 for (i = band_below_start; i < band_below_end; i++) {
3985 pixman_box32_t *box= &boxes[i];
3986 add_border(&band_merge, box->x1, box->y1, box->x2, box->y1,
3987 MOTION_DIRECTION_NEGATIVE_Y);
3988 }
3989 qsort(band_merge.data,
3990 band_merge.size / sizeof *border,
3991 sizeof *border,
3992 compare_lines_x);
3993
3994 /* Combine the two combined bands so that any overlapping border is
3995 * eliminated. */
3996 prev_border = NULL;
3997 wl_array_for_each(border, &band_merge) {
3998 assert(border->line.a.y == border->line.b.y);
3999 assert(!prev_border ||
4000 prev_border->line.a.y == border->line.a.y);
4001 assert(!prev_border ||
4002 (prev_border->line.a.x != border->line.a.x ||
4003 prev_border->line.b.x != border->line.b.x));
4004 assert(!prev_border ||
4005 prev_border->line.a.x <= border->line.a.x);
4006
4007 if (prev_border &&
4008 prev_border->line.a.x == border->line.a.x) {
4009 /*
4010 * ------------ +
4011 * ------- =
4012 * [ ]-----
4013 */
4014 prev_border->line.a.x = border->line.b.x;
4015 } else if (prev_border &&
4016 prev_border->line.b.x == border->line.b.x) {
4017 /*
4018 * ------------ +
4019 * ------ =
4020 * ------[ ]
4021 */
4022 prev_border->line.b.x = border->line.a.x;
4023 } else if (prev_border &&
4024 prev_border->line.b.x == border->line.a.x) {
4025 /*
4026 * -------- +
4027 * ------ =
4028 * --------------
4029 */
4030 prev_border->line.b.x = border->line.b.x;
4031 } else if (prev_border &&
4032 prev_border->line.b.x >= border->line.a.x) {
4033 /*
4034 * --------------- +
4035 * ------ =
4036 * -----[ ]----
4037 */
4038 new_border = add_border(borders,
4039 border->line.b.x,
4040 border->line.b.y,
4041 prev_border->line.b.x,
4042 prev_border->line.b.y,
4043 prev_border->blocking_dir);
4044 prev_border->line.b.x = border->line.a.x;
4045 prev_border = new_border;
4046 } else {
4047 assert(!prev_border ||
4048 prev_border->line.b.x < border->line.a.x);
4049 /*
4050 * First border or non-overlapping.
4051 *
4052 * ----- +
4053 * ----- =
4054 * ----- -----
4055 */
4056 new_border = wl_array_add(borders, sizeof *border);
4057 *new_border = *border;
4058 prev_border = new_border;
4059 }
4060 }
4061
4062 wl_array_release(&band_merge);
4063}
4064
4065static void
4066add_band_bottom_edges(pixman_box32_t *boxes,
4067 int band_start,
4068 int band_end,
4069 struct wl_array *borders)
4070{
4071 int i;
4072
4073 for (i = band_start; i < band_end; i++) {
4074 add_border(borders,
4075 boxes[i].x1, boxes[i].y2,
4076 boxes[i].x2, boxes[i].y2,
4077 MOTION_DIRECTION_POSITIVE_Y);
4078 }
4079}
4080
4081static void
4082region_to_outline(pixman_region32_t *region, struct wl_array *borders)
4083{
4084 pixman_box32_t *boxes;
4085 int num_boxes;
4086 int i;
4087 int top_most, bottom_most;
4088 int current_roof;
4089 int prev_top;
4090 int band_start, prev_band_start;
4091
4092 /*
4093 * Remove any overlapping lines from the set of rectangles. Note that
4094 * pixman regions are grouped as rows of rectangles, where rectangles
4095 * in one row never touch or overlap and are all of the same height.
4096 *
4097 * -------- --- -------- ---
4098 * | | | | | | | |
4099 * ----------====---- --- ----------- ----- ---
4100 * | | => | |
4101 * ----==========--------- ----- ----------
4102 * | | | |
4103 * ------------------- -------------------
4104 *
4105 */
4106
4107 boxes = pixman_region32_rectangles(region, &num_boxes);
4108 prev_top = 0;
4109 top_most = boxes[0].y1;
4110 current_roof = top_most;
4111 bottom_most = boxes[num_boxes - 1].y2;
4112 band_start = 0;
4113 prev_band_start = 0;
4114 for (i = 0; i < num_boxes; i++) {
4115 /* Detect if there is a vertical empty space, and add the lower
4116 * level of the previous band if so was the case. */
4117 if (i > 0 &&
4118 boxes[i].y1 != prev_top &&
4119 boxes[i].y1 != boxes[i - 1].y2) {
4120 current_roof = boxes[i].y1;
4121 add_band_bottom_edges(boxes,
4122 band_start,
4123 i,
4124 borders);
4125 }
4126
4127 /* Special case adding the last band, since it won't be handled
4128 * by the band change detection below. */
4129 if (boxes[i].y1 != current_roof && i == num_boxes - 1) {
4130 if (boxes[i].y1 != prev_top) {
4131 /* The last band is a single box, so we don't
4132 * have a prev_band_start to tell us when the
4133 * previous band started. */
4134 add_non_overlapping_edges(boxes,
4135 band_start,
4136 i,
4137 i + 1,
4138 borders);
4139 } else {
4140 add_non_overlapping_edges(boxes,
4141 prev_band_start,
4142 band_start,
4143 i + 1,
4144 borders);
4145 }
4146 }
4147
4148 /* Detect when passing a band and combine the top border of the
4149 * just passed band with the bottom band of the previous band.
4150 */
4151 if (boxes[i].y1 != top_most && boxes[i].y1 != prev_top) {
4152 /* Combine the two passed bands. */
4153 if (prev_top != current_roof) {
4154 add_non_overlapping_edges(boxes,
4155 prev_band_start,
4156 band_start,
4157 i,
4158 borders);
4159 }
4160
4161 prev_band_start = band_start;
4162 band_start = i;
4163 }
4164
4165 /* Add the top border if the box is part of the current roof. */
4166 if (boxes[i].y1 == current_roof) {
4167 add_border(borders,
4168 boxes[i].x1, boxes[i].y1,
4169 boxes[i].x2, boxes[i].y1,
4170 MOTION_DIRECTION_NEGATIVE_Y);
4171 }
4172
4173 /* Add the bottom border of the last band. */
4174 if (boxes[i].y2 == bottom_most) {
4175 add_border(borders,
4176 boxes[i].x1, boxes[i].y2,
4177 boxes[i].x2, boxes[i].y2,
4178 MOTION_DIRECTION_POSITIVE_Y);
4179 }
4180
4181 /* Always add the left border. */
4182 add_border(borders,
4183 boxes[i].x1, boxes[i].y1,
4184 boxes[i].x1, boxes[i].y2,
4185 MOTION_DIRECTION_NEGATIVE_X);
4186
4187 /* Always add the right border. */
4188 add_border(borders,
4189 boxes[i].x2, boxes[i].y1,
4190 boxes[i].x2, boxes[i].y2,
4191 MOTION_DIRECTION_POSITIVE_X);
4192
4193 prev_top = boxes[i].y1;
4194 }
4195}
4196
4197static bool
4198is_border_horizontal (struct border *border)
4199{
4200 return border->line.a.y == border->line.b.y;
4201}
4202
4203static bool
4204is_border_blocking_directions(struct border *border,
4205 uint32_t directions)
4206{
4207 /* Don't block parallel motions. */
4208 if (is_border_horizontal(border)) {
4209 if ((directions & (MOTION_DIRECTION_POSITIVE_Y |
4210 MOTION_DIRECTION_NEGATIVE_Y)) == 0)
4211 return false;
4212 } else {
4213 if ((directions & (MOTION_DIRECTION_POSITIVE_X |
4214 MOTION_DIRECTION_NEGATIVE_X)) == 0)
4215 return false;
4216 }
4217
4218 return (~border->blocking_dir & directions) != directions;
4219}
4220
4221static struct border *
4222get_closest_border(struct wl_array *borders,
4223 struct line *motion,
4224 uint32_t directions)
4225{
4226 struct border *border;
4227 struct vec2d intersection;
4228 struct vec2d delta;
4229 double distance_2;
4230 struct border *closest_border = NULL;
4231 double closest_distance_2 = DBL_MAX;
4232
4233 wl_array_for_each(border, borders) {
4234 if (!is_border_blocking_directions(border, directions))
4235 continue;
4236
4237 if (!lines_intersect(&border->line, motion, &intersection))
4238 continue;
4239
4240 delta = vec2d_subtract(intersection, motion->a);
4241 distance_2 = delta.x*delta.x + delta.y*delta.y;
4242 if (distance_2 < closest_distance_2) {
4243 closest_border = border;
4244 closest_distance_2 = distance_2;
4245 }
4246 }
4247
4248 return closest_border;
4249}
4250
4251static void
4252clamp_to_border(struct border *border,
4253 struct line *motion,
4254 uint32_t *motion_dir)
4255{
4256 /*
4257 * When clamping either rightward or downward motions, the motion needs
4258 * to be clamped so that the destination coordinate does not end up on
4259 * the border (see weston_pointer_clamp_event_to_region). Do this by
4260 * clamping such motions to the border minus the smallest possible
4261 * wl_fixed_t value.
4262 */
4263 if (is_border_horizontal(border)) {
4264 if (*motion_dir & MOTION_DIRECTION_POSITIVE_Y)
4265 motion->b.y = border->line.a.y - wl_fixed_to_double(1);
4266 else
4267 motion->b.y = border->line.a.y;
4268 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_Y |
4269 MOTION_DIRECTION_NEGATIVE_Y);
4270 } else {
4271 if (*motion_dir & MOTION_DIRECTION_POSITIVE_X)
4272 motion->b.x = border->line.a.x - wl_fixed_to_double(1);
4273 else
4274 motion->b.x = border->line.a.x;
4275 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_X |
4276 MOTION_DIRECTION_NEGATIVE_X);
4277 }
4278}
4279
4280static uint32_t
4281get_motion_directions(struct line *motion)
4282{
4283 uint32_t directions = 0;
4284
4285 if (motion->a.x < motion->b.x)
4286 directions |= MOTION_DIRECTION_POSITIVE_X;
4287 else if (motion->a.x > motion->b.x)
4288 directions |= MOTION_DIRECTION_NEGATIVE_X;
4289 if (motion->a.y < motion->b.y)
4290 directions |= MOTION_DIRECTION_POSITIVE_Y;
4291 else if (motion->a.y > motion->b.y)
4292 directions |= MOTION_DIRECTION_NEGATIVE_Y;
4293
4294 return directions;
4295}
4296
Jonas Ådahld3414f22016-07-22 17:56:31 +08004297static void
4298weston_pointer_clamp_event_to_region(struct weston_pointer *pointer,
4299 struct weston_pointer_motion_event *event,
4300 pixman_region32_t *region,
4301 wl_fixed_t *clamped_x,
4302 wl_fixed_t *clamped_y)
4303{
4304 wl_fixed_t x, y;
4305 wl_fixed_t sx, sy;
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004306 wl_fixed_t old_sx = pointer->sx;
4307 wl_fixed_t old_sy = pointer->sy;
4308 struct wl_array borders;
4309 struct line motion;
4310 struct border *closest_border;
4311 float new_x_f, new_y_f;
4312 uint32_t directions;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004313
4314 weston_pointer_motion_to_abs(pointer, event, &x, &y);
4315 weston_view_from_global_fixed(pointer->focus, x, y, &sx, &sy);
4316
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004317 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004318
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004319 /*
4320 * Generate borders given the confine region we are to use. The borders
4321 * are defined to be the outer region of the allowed area. This means
4322 * top/left borders are "within" the allowed area, while bottom/right
4323 * borders are outside. This needs to be considered when clamping
4324 * confined motion vectors.
4325 */
4326 region_to_outline(region, &borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004327
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004328 motion = (struct line) {
4329 .a = (struct vec2d) {
4330 .x = wl_fixed_to_double(old_sx),
4331 .y = wl_fixed_to_double(old_sy),
4332 },
4333 .b = (struct vec2d) {
4334 .x = wl_fixed_to_double(sx),
4335 .y = wl_fixed_to_double(sy),
4336 },
4337 };
4338 directions = get_motion_directions(&motion);
4339
4340 while (directions) {
4341 closest_border = get_closest_border(&borders,
4342 &motion,
4343 directions);
4344 if (closest_border)
4345 clamp_to_border(closest_border, &motion, &directions);
4346 else
4347 break;
4348 }
4349
4350 weston_view_to_global_float(pointer->focus,
4351 (float) motion.b.x, (float) motion.b.y,
4352 &new_x_f, &new_y_f);
4353 *clamped_x = wl_fixed_from_double(new_x_f);
4354 *clamped_y = wl_fixed_from_double(new_y_f);
4355
4356 wl_array_release(&borders);
4357}
4358
4359static double
4360point_to_border_distance_2(struct border *border, double x, double y)
4361{
4362 double orig_x, orig_y;
4363 double dx, dy;
4364
4365 if (is_border_horizontal(border)) {
4366 if (x < border->line.a.x)
4367 orig_x = border->line.a.x;
4368 else if (x > border->line.b.x)
4369 orig_x = border->line.b.x;
4370 else
4371 orig_x = x;
4372 orig_y = border->line.a.y;
4373 } else {
4374 if (y < border->line.a.y)
4375 orig_y = border->line.a.y;
4376 else if (y > border->line.b.y)
4377 orig_y = border->line.b.y;
4378 else
4379 orig_y = y;
4380 orig_x = border->line.a.x;
4381 }
4382
4383
4384 dx = fabs(orig_x - x);
4385 dy = fabs(orig_y - y);
4386 return dx*dx + dy*dy;
4387}
4388
4389static void
4390warp_to_behind_border(struct border *border, wl_fixed_t *sx, wl_fixed_t *sy)
4391{
4392 switch (border->blocking_dir) {
4393 case MOTION_DIRECTION_POSITIVE_X:
4394 case MOTION_DIRECTION_NEGATIVE_X:
4395 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_X)
4396 *sx = wl_fixed_from_double(border->line.a.x) - 1;
4397 else
4398 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4399 if (*sy < wl_fixed_from_double(border->line.a.y))
4400 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4401 else if (*sy > wl_fixed_from_double(border->line.b.y))
4402 *sy = wl_fixed_from_double(border->line.b.y) - 1;
4403 break;
4404 case MOTION_DIRECTION_POSITIVE_Y:
4405 case MOTION_DIRECTION_NEGATIVE_Y:
4406 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_Y)
4407 *sy = wl_fixed_from_double(border->line.a.y) - 1;
4408 else
4409 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4410 if (*sx < wl_fixed_from_double(border->line.a.x))
4411 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4412 else if (*sx > wl_fixed_from_double(border->line.b.x))
4413 *sx = wl_fixed_from_double(border->line.b.x) - 1;
4414 break;
4415 }
Jonas Ådahld3414f22016-07-22 17:56:31 +08004416}
4417
4418static void
4419maybe_warp_confined_pointer(struct weston_pointer_constraint *constraint)
4420{
4421 wl_fixed_t x;
4422 wl_fixed_t y;
4423 wl_fixed_t sx;
4424 wl_fixed_t sy;
4425
4426 weston_view_from_global_fixed(constraint->view,
4427 constraint->pointer->x,
4428 constraint->pointer->y,
4429 &sx,
4430 &sy);
4431
4432 if (!is_within_constraint_region(constraint, sx, sy)) {
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004433 double xf = wl_fixed_to_double(sx);
4434 double yf = wl_fixed_to_double(sy);
4435 pixman_region32_t confine_region;
4436 struct wl_array borders;
4437 struct border *border;
4438 double closest_distance_2 = DBL_MAX;
4439 struct border *closest_border = NULL;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004440
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004441 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004442
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004443 pixman_region32_init(&confine_region);
4444 pixman_region32_intersect(&confine_region,
4445 &constraint->view->surface->input,
4446 &constraint->region);
4447 region_to_outline(&confine_region, &borders);
4448 pixman_region32_fini(&confine_region);
4449
4450 wl_array_for_each(border, &borders) {
4451 double distance_2;
4452
4453 distance_2 = point_to_border_distance_2(border, xf, yf);
4454 if (distance_2 < closest_distance_2) {
4455 closest_border = border;
4456 closest_distance_2 = distance_2;
4457 }
4458 }
4459 assert(closest_border);
4460
4461 warp_to_behind_border(closest_border, &sx, &sy);
4462
4463 wl_array_release(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004464
4465 weston_view_to_global_fixed(constraint->view, sx, sy, &x, &y);
4466 weston_pointer_move_to(constraint->pointer, x, y);
4467 }
4468}
4469
4470static void
4471confined_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02004472 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004473 struct weston_pointer_motion_event *event)
4474{
4475 struct weston_pointer_constraint *constraint =
4476 container_of(grab, struct weston_pointer_constraint, grab);
4477 struct weston_pointer *pointer = grab->pointer;
4478 struct weston_surface *surface;
4479 wl_fixed_t x, y;
4480 wl_fixed_t old_sx = pointer->sx;
4481 wl_fixed_t old_sy = pointer->sy;
4482 pixman_region32_t confine_region;
4483
4484 assert(pointer->focus);
4485 assert(pointer->focus->surface == constraint->surface);
4486
4487 surface = pointer->focus->surface;
4488
4489 pixman_region32_init(&confine_region);
4490 pixman_region32_intersect(&confine_region,
4491 &surface->input,
4492 &constraint->region);
4493 weston_pointer_clamp_event_to_region(pointer, event,
4494 &confine_region, &x, &y);
4495 weston_pointer_move_to(pointer, x, y);
4496 pixman_region32_fini(&confine_region);
4497
4498 weston_view_from_global_fixed(pointer->focus, x, y,
4499 &pointer->sx, &pointer->sy);
4500
4501 if (old_sx != pointer->sx || old_sy != pointer->sy) {
Quentin Glidiccde13452016-08-12 10:41:32 +02004502 pointer_send_motion(pointer, time,
4503 pointer->sx, pointer->sy);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004504 }
4505
Quentin Glidiccde13452016-08-12 10:41:32 +02004506 pointer_send_relative_motion(pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004507}
4508
4509static void
4510confined_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02004511 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004512 uint32_t button,
4513 uint32_t state_w)
4514{
4515 weston_pointer_send_button(grab->pointer, time, button, state_w);
4516}
4517
4518static void
4519confined_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02004520 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004521 struct weston_pointer_axis_event *event)
4522{
4523 weston_pointer_send_axis(grab->pointer, time, event);
4524}
4525
4526static void
4527confined_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
4528 uint32_t source)
4529{
4530 weston_pointer_send_axis_source(grab->pointer, source);
4531}
4532
4533static void
4534confined_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
4535{
4536 weston_pointer_send_frame(grab->pointer);
4537}
4538
4539static void
4540confined_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
4541{
4542 struct weston_pointer_constraint *constraint =
4543 container_of(grab, struct weston_pointer_constraint, grab);
4544
4545 disable_pointer_constraint(constraint);
4546
4547 /* If this is a persistent constraint, re-add the surface destroy signal
4548 * listener only if we are currently not destroying the surface. */
4549 switch (constraint->lifetime) {
4550 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT:
4551 if (constraint->surface->resource)
4552 wl_signal_add(&constraint->surface->destroy_signal,
4553 &constraint->surface_destroy_listener);
4554 break;
4555 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT:
4556 break;
4557 }
4558}
4559
4560static const struct weston_pointer_grab_interface
4561 confined_pointer_grab_interface = {
4562 confined_pointer_grab_pointer_focus,
4563 confined_pointer_grab_pointer_motion,
4564 confined_pointer_grab_pointer_button,
4565 confined_pointer_grab_pointer_axis,
4566 confined_pointer_grab_pointer_axis_source,
4567 confined_pointer_grab_pointer_frame,
4568 confined_pointer_grab_pointer_cancel,
4569};
4570
4571static void
4572confined_pointer_destroy(struct wl_client *client,
4573 struct wl_resource *resource)
4574{
4575 wl_resource_destroy(resource);
4576}
4577
4578static void
4579confined_pointer_set_region(struct wl_client *client,
4580 struct wl_resource *resource,
4581 struct wl_resource *region_resource)
4582{
4583 struct weston_pointer_constraint *constraint =
4584 wl_resource_get_user_data(resource);
4585 struct weston_region *region = region_resource ?
4586 wl_resource_get_user_data(region_resource) : NULL;
4587
4588 if (!constraint)
4589 return;
4590
4591 if (region) {
4592 pixman_region32_copy(&constraint->region_pending,
4593 &region->region);
4594 } else {
4595 pixman_region32_fini(&constraint->region_pending);
4596 region_init_infinite(&constraint->region_pending);
4597 }
4598 constraint->region_is_pending = true;
4599}
4600
4601static const struct zwp_confined_pointer_v1_interface confined_pointer_interface = {
4602 confined_pointer_destroy,
4603 confined_pointer_set_region,
4604};
4605
4606static void
4607pointer_constraints_confine_pointer(struct wl_client *client,
4608 struct wl_resource *resource,
4609 uint32_t id,
4610 struct wl_resource *surface_resource,
4611 struct wl_resource *pointer_resource,
4612 struct wl_resource *region_resource,
4613 uint32_t lifetime)
4614{
4615 struct weston_surface *surface =
4616 wl_resource_get_user_data(surface_resource);
4617 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
4618 struct weston_region *region = region_resource ?
4619 wl_resource_get_user_data(region_resource) : NULL;
4620
4621 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
4622 &zwp_confined_pointer_v1_interface,
4623 &confined_pointer_interface,
4624 &confined_pointer_grab_interface);
4625}
4626
4627static const struct zwp_pointer_constraints_v1_interface pointer_constraints_interface = {
4628 pointer_constraints_destroy,
4629 pointer_constraints_lock_pointer,
4630 pointer_constraints_confine_pointer,
4631};
4632
4633static void
4634bind_pointer_constraints(struct wl_client *client, void *data,
4635 uint32_t version, uint32_t id)
4636{
4637 struct wl_resource *resource;
4638
4639 resource = wl_resource_create(client,
4640 &zwp_pointer_constraints_v1_interface,
4641 1, id);
4642
4643 wl_resource_set_implementation(resource, &pointer_constraints_interface,
4644 NULL, NULL);
4645}
4646
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004647static void
Alexandros Frantzis2b442482018-02-20 14:05:50 +02004648input_timestamps_destroy(struct wl_client *client,
4649 struct wl_resource *resource)
4650{
4651 wl_resource_destroy(resource);
4652}
4653
4654static const struct zwp_input_timestamps_v1_interface
4655 input_timestamps_interface = {
4656 input_timestamps_destroy,
4657};
4658
4659static void
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004660input_timestamps_manager_destroy(struct wl_client *client,
4661 struct wl_resource *resource)
4662{
4663 wl_resource_destroy(resource);
4664}
4665
4666static void
4667input_timestamps_manager_get_keyboard_timestamps(struct wl_client *client,
4668 struct wl_resource *resource,
4669 uint32_t id,
4670 struct wl_resource *keyboard_resource)
4671{
Alexandros Frantzis2b442482018-02-20 14:05:50 +02004672 struct weston_keyboard *keyboard =
4673 wl_resource_get_user_data(keyboard_resource);
4674 struct wl_resource *input_ts;
4675
4676 input_ts = wl_resource_create(client,
4677 &zwp_input_timestamps_v1_interface,
4678 1, id);
4679 if (!input_ts) {
4680 wl_client_post_no_memory(client);
4681 return;
4682 }
4683
4684 if (keyboard) {
4685 wl_list_insert(&keyboard->timestamps_list,
4686 wl_resource_get_link(input_ts));
4687 } else {
4688 wl_list_init(wl_resource_get_link(input_ts));
4689 }
4690
4691 wl_resource_set_implementation(input_ts,
4692 &input_timestamps_interface,
4693 keyboard_resource,
4694 unbind_resource);
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004695}
4696
4697static void
4698input_timestamps_manager_get_pointer_timestamps(struct wl_client *client,
4699 struct wl_resource *resource,
4700 uint32_t id,
4701 struct wl_resource *pointer_resource)
4702{
Alexandros Frantzisdb907b72018-02-20 14:06:26 +02004703 struct weston_pointer *pointer =
4704 wl_resource_get_user_data(pointer_resource);
4705 struct wl_resource *input_ts;
4706
4707 input_ts = wl_resource_create(client,
4708 &zwp_input_timestamps_v1_interface,
4709 1, id);
4710 if (!input_ts) {
4711 wl_client_post_no_memory(client);
4712 return;
4713 }
4714
4715 if (pointer) {
4716 wl_list_insert(&pointer->timestamps_list,
4717 wl_resource_get_link(input_ts));
4718 } else {
4719 wl_list_init(wl_resource_get_link(input_ts));
4720 }
4721
4722 wl_resource_set_implementation(input_ts,
4723 &input_timestamps_interface,
4724 pointer_resource,
4725 unbind_resource);
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004726}
4727
4728static void
4729input_timestamps_manager_get_touch_timestamps(struct wl_client *client,
4730 struct wl_resource *resource,
4731 uint32_t id,
4732 struct wl_resource *touch_resource)
4733{
4734 wl_client_post_no_memory(client);
4735}
4736
4737static const struct zwp_input_timestamps_manager_v1_interface
4738 input_timestamps_manager_interface = {
4739 input_timestamps_manager_destroy,
4740 input_timestamps_manager_get_keyboard_timestamps,
4741 input_timestamps_manager_get_pointer_timestamps,
4742 input_timestamps_manager_get_touch_timestamps,
4743};
4744
4745static void
4746bind_input_timestamps_manager(struct wl_client *client, void *data,
4747 uint32_t version, uint32_t id)
4748{
4749 struct wl_resource *resource =
4750 wl_resource_create(client,
4751 &zwp_input_timestamps_manager_v1_interface,
4752 1, id);
4753
4754 if (resource == NULL) {
4755 wl_client_post_no_memory(client);
4756 return;
4757 }
4758
4759 wl_resource_set_implementation(resource,
4760 &input_timestamps_manager_interface,
4761 NULL, NULL);
4762}
4763
Jonas Ådahl30d61d82014-10-22 21:21:17 +02004764int
4765weston_input_init(struct weston_compositor *compositor)
4766{
4767 if (!wl_global_create(compositor->wl_display,
4768 &zwp_relative_pointer_manager_v1_interface, 1,
4769 compositor, bind_relative_pointer_manager))
4770 return -1;
4771
Jonas Ådahld3414f22016-07-22 17:56:31 +08004772 if (!wl_global_create(compositor->wl_display,
4773 &zwp_pointer_constraints_v1_interface, 1,
4774 NULL, bind_pointer_constraints))
4775 return -1;
4776
Alexandros Frantzis538749d2018-02-16 18:44:16 +02004777 if (!wl_global_create(compositor->wl_display,
4778 &zwp_input_timestamps_manager_v1_interface, 1,
4779 NULL, bind_input_timestamps_manager))
4780 return -1;
4781
Jonas Ådahl30d61d82014-10-22 21:21:17 +02004782 return 0;
4783}