blob: 7f789333272ee963aebac6a0ddcbc0ded9314619 [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"
Jonas Ådahld3414f22016-07-22 17:56:31 +080045
46enum pointer_constraint_type {
47 POINTER_CONSTRAINT_TYPE_LOCK,
48 POINTER_CONSTRAINT_TYPE_CONFINE,
49};
50
Jonas Ådahld0be2bb2015-04-30 17:56:37 +080051enum motion_direction {
52 MOTION_DIRECTION_POSITIVE_X = 1 << 0,
53 MOTION_DIRECTION_NEGATIVE_X = 1 << 1,
54 MOTION_DIRECTION_POSITIVE_Y = 1 << 2,
55 MOTION_DIRECTION_NEGATIVE_Y = 1 << 3,
56};
57
58struct vec2d {
59 double x, y;
60};
61
62struct line {
63 struct vec2d a;
64 struct vec2d b;
65};
66
67struct border {
68 struct line line;
69 enum motion_direction blocking_dir;
70};
71
Jonas Ådahld3414f22016-07-22 17:56:31 +080072static void
73maybe_warp_confined_pointer(struct weston_pointer_constraint *constraint);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040074
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040075static void
76empty_region(pixman_region32_t *region)
77{
78 pixman_region32_fini(region);
79 pixman_region32_init(region);
80}
81
Jonas Ådahld3414f22016-07-22 17:56:31 +080082static void
83region_init_infinite(pixman_region32_t *region)
84{
85 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
86 UINT32_MAX, UINT32_MAX);
87}
88
Jonas Ådahl2cbf2932015-07-22 12:05:38 +080089static struct weston_pointer_client *
90weston_pointer_client_create(struct wl_client *client)
91{
92 struct weston_pointer_client *pointer_client;
93
94 pointer_client = zalloc(sizeof *pointer_client);
95 if (!pointer_client)
96 return NULL;
97
98 pointer_client->client = client;
99 wl_list_init(&pointer_client->pointer_resources);
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200100 wl_list_init(&pointer_client->relative_pointer_resources);
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800101
102 return pointer_client;
103}
104
105static void
106weston_pointer_client_destroy(struct weston_pointer_client *pointer_client)
107{
108 free(pointer_client);
109}
110
111static bool
112weston_pointer_client_is_empty(struct weston_pointer_client *pointer_client)
113{
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200114 return (wl_list_empty(&pointer_client->pointer_resources) &&
115 wl_list_empty(&pointer_client->relative_pointer_resources));
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800116}
117
118static struct weston_pointer_client *
119weston_pointer_get_pointer_client(struct weston_pointer *pointer,
120 struct wl_client *client)
121{
122 struct weston_pointer_client *pointer_client;
123
124 wl_list_for_each(pointer_client, &pointer->pointer_clients, link) {
125 if (pointer_client->client == client)
126 return pointer_client;
127 }
128
129 return NULL;
130}
131
132static struct weston_pointer_client *
133weston_pointer_ensure_pointer_client(struct weston_pointer *pointer,
134 struct wl_client *client)
135{
136 struct weston_pointer_client *pointer_client;
137
138 pointer_client = weston_pointer_get_pointer_client(pointer, client);
139 if (pointer_client)
140 return pointer_client;
141
142 pointer_client = weston_pointer_client_create(client);
143 wl_list_insert(&pointer->pointer_clients, &pointer_client->link);
144
145 if (pointer->focus &&
146 pointer->focus->surface->resource &&
147 wl_resource_get_client(pointer->focus->surface->resource) == client) {
148 pointer->focus_client = pointer_client;
149 }
150
151 return pointer_client;
152}
153
154static void
155weston_pointer_cleanup_pointer_client(struct weston_pointer *pointer,
156 struct weston_pointer_client *pointer_client)
157{
158 if (weston_pointer_client_is_empty(pointer_client)) {
159 if (pointer->focus_client == pointer_client)
160 pointer->focus_client = NULL;
161 wl_list_remove(&pointer_client->link);
162 weston_pointer_client_destroy(pointer_client);
163 }
164}
165
166static void
167unbind_pointer_client_resource(struct wl_resource *resource)
168{
169 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
170 struct wl_client *client = wl_resource_get_client(resource);
171 struct weston_pointer_client *pointer_client;
172
173 pointer_client = weston_pointer_get_pointer_client(pointer, client);
174 assert(pointer_client);
175
176 wl_list_remove(wl_resource_get_link(resource));
177 weston_pointer_cleanup_pointer_client(pointer, pointer_client);
178}
179
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400180static void unbind_resource(struct wl_resource *resource)
181{
Jason Ekstrand44a38632013-06-14 10:08:00 -0500182 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400183}
184
Jonas Ådahl3042ffe2013-10-17 23:04:08 +0200185WL_EXPORT void
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200186weston_pointer_motion_to_abs(struct weston_pointer *pointer,
187 struct weston_pointer_motion_event *event,
188 wl_fixed_t *x, wl_fixed_t *y)
189{
190 if (event->mask & WESTON_POINTER_MOTION_ABS) {
191 *x = wl_fixed_from_double(event->x);
192 *y = wl_fixed_from_double(event->y);
193 } else if (event->mask & WESTON_POINTER_MOTION_REL) {
194 *x = pointer->x + wl_fixed_from_double(event->dx);
195 *y = pointer->y + wl_fixed_from_double(event->dy);
196 } else {
197 assert(!"invalid motion event");
198 *x = *y = 0;
199 }
200}
201
202static bool
203weston_pointer_motion_to_rel(struct weston_pointer *pointer,
204 struct weston_pointer_motion_event *event,
205 double *dx, double *dy,
206 double *dx_unaccel, double *dy_unaccel)
207{
208 if (event->mask & WESTON_POINTER_MOTION_REL &&
209 event->mask & WESTON_POINTER_MOTION_REL_UNACCEL) {
210 *dx = event->dx;
211 *dy = event->dy;
212 *dx_unaccel = event->dx_unaccel;
213 *dy_unaccel = event->dy_unaccel;
214 return true;
215 } else if (event->mask & WESTON_POINTER_MOTION_REL) {
216 *dx_unaccel = *dx = event->dx;
217 *dy_unaccel = *dy = event->dy;
218 return true;
219 } else if (event->mask & WESTON_POINTER_MOTION_REL_UNACCEL) {
220 *dx_unaccel = *dx = event->dx_unaccel;
221 *dy_unaccel = *dy = event->dy_unaccel;
222 return true;
223 } else {
224 return false;
225 }
226}
227
228WL_EXPORT void
Kristian Høgsberga71e8b22013-05-06 21:51:21 -0400229weston_seat_repick(struct weston_seat *seat)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400230{
Derek Foreman1281a362015-07-31 16:55:32 -0500231 const struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400232
Derek Foreman1b786ee2015-06-03 15:53:23 -0500233 if (!pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400234 return;
235
Derek Foreman1b786ee2015-06-03 15:53:23 -0500236 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400237}
238
239static void
240weston_compositor_idle_inhibit(struct weston_compositor *compositor)
241{
242 weston_compositor_wake(compositor);
243 compositor->idle_inhibit++;
244}
245
246static void
247weston_compositor_idle_release(struct weston_compositor *compositor)
248{
249 compositor->idle_inhibit--;
250 weston_compositor_wake(compositor);
251}
252
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400253static void
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100254pointer_focus_view_destroyed(struct wl_listener *listener, void *data)
255{
256 struct weston_pointer *pointer =
257 container_of(listener, struct weston_pointer,
258 focus_view_listener);
259
Derek Foremanf9318d12015-05-11 15:40:11 -0500260 weston_pointer_clear_focus(pointer);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100261}
262
263static void
264pointer_focus_resource_destroyed(struct wl_listener *listener, void *data)
265{
266 struct weston_pointer *pointer =
267 container_of(listener, struct weston_pointer,
268 focus_resource_listener);
269
Derek Foremanf9318d12015-05-11 15:40:11 -0500270 weston_pointer_clear_focus(pointer);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100271}
272
273static void
274keyboard_focus_resource_destroyed(struct wl_listener *listener, void *data)
275{
276 struct weston_keyboard *keyboard =
277 container_of(listener, struct weston_keyboard,
278 focus_resource_listener);
279
280 weston_keyboard_set_focus(keyboard, NULL);
281}
282
283static void
284touch_focus_view_destroyed(struct wl_listener *listener, void *data)
285{
286 struct weston_touch *touch =
287 container_of(listener, struct weston_touch,
288 focus_view_listener);
289
Derek Foreman4c93c082015-04-30 16:45:41 -0500290 weston_touch_set_focus(touch, NULL);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100291}
292
293static void
294touch_focus_resource_destroyed(struct wl_listener *listener, void *data)
295{
296 struct weston_touch *touch =
297 container_of(listener, struct weston_touch,
298 focus_resource_listener);
299
Derek Foreman4c93c082015-04-30 16:45:41 -0500300 weston_touch_set_focus(touch, NULL);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100301}
302
303static void
Neil Roberts96d790e2013-09-19 17:32:00 +0100304move_resources(struct wl_list *destination, struct wl_list *source)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400305{
Neil Roberts96d790e2013-09-19 17:32:00 +0100306 wl_list_insert_list(destination, source);
307 wl_list_init(source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400308}
309
310static void
Neil Roberts96d790e2013-09-19 17:32:00 +0100311move_resources_for_client(struct wl_list *destination,
312 struct wl_list *source,
313 struct wl_client *client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400314{
Neil Roberts96d790e2013-09-19 17:32:00 +0100315 struct wl_resource *resource, *tmp;
316 wl_resource_for_each_safe(resource, tmp, source) {
317 if (wl_resource_get_client(resource) == client) {
318 wl_list_remove(wl_resource_get_link(resource));
319 wl_list_insert(destination,
320 wl_resource_get_link(resource));
321 }
322 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400323}
324
325static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700326default_grab_pointer_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400327{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400328 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500329 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400330 wl_fixed_t sx, sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400331
332 if (pointer->button_count > 0)
333 return;
334
Jason Ekstranda7af7042013-10-12 22:38:11 -0500335 view = weston_compositor_pick_view(pointer->seat->compositor,
336 pointer->x, pointer->y,
337 &sx, &sy);
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400338
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -0800339 if (pointer->focus != view || pointer->sx != sx || pointer->sy != sy)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500340 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400341}
342
343static void
Quentin Glidiccde13452016-08-12 10:41:32 +0200344pointer_send_relative_motion(struct weston_pointer *pointer,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200345 const struct timespec *time,
Quentin Glidiccde13452016-08-12 10:41:32 +0200346 struct weston_pointer_motion_event *event)
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200347{
348 uint64_t time_usec;
349 double dx, dy, dx_unaccel, dy_unaccel;
350 wl_fixed_t dxf, dyf, dxf_unaccel, dyf_unaccel;
351 struct wl_list *resource_list;
352 struct wl_resource *resource;
353
354 if (!pointer->focus_client)
355 return;
356
357 if (!weston_pointer_motion_to_rel(pointer, event,
358 &dx, &dy,
359 &dx_unaccel, &dy_unaccel))
360 return;
361
362 resource_list = &pointer->focus_client->relative_pointer_resources;
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200363 time_usec = timespec_to_usec(&event->time);
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200364 if (time_usec == 0)
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200365 time_usec = timespec_to_usec(time);
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200366
367 dxf = wl_fixed_from_double(dx);
368 dyf = wl_fixed_from_double(dy);
369 dxf_unaccel = wl_fixed_from_double(dx_unaccel);
370 dyf_unaccel = wl_fixed_from_double(dy_unaccel);
371
372 wl_resource_for_each(resource, resource_list) {
373 zwp_relative_pointer_v1_send_relative_motion(
374 resource,
375 (uint32_t) (time_usec >> 32),
376 (uint32_t) time_usec,
377 dxf, dyf,
378 dxf_unaccel, dyf_unaccel);
379 }
380}
381
382static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200383pointer_send_motion(struct weston_pointer *pointer,
384 const struct timespec *time,
Quentin Glidiccde13452016-08-12 10:41:32 +0200385 wl_fixed_t sx, wl_fixed_t sy)
Jonas Ådahlf44942e2016-07-22 17:54:55 +0800386{
387 struct wl_list *resource_list;
388 struct wl_resource *resource;
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200389 uint32_t msecs;
Jonas Ådahlf44942e2016-07-22 17:54:55 +0800390
391 if (!pointer->focus_client)
392 return;
393
394 resource_list = &pointer->focus_client->pointer_resources;
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200395 msecs = timespec_to_msec(time);
Jonas Ådahlf44942e2016-07-22 17:54:55 +0800396 wl_resource_for_each(resource, resource_list)
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200397 wl_pointer_send_motion(resource, msecs, sx, sy);
Jonas Ådahlf44942e2016-07-22 17:54:55 +0800398}
399
Quentin Glidiccde13452016-08-12 10:41:32 +0200400WL_EXPORT void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200401weston_pointer_send_motion(struct weston_pointer *pointer,
402 const struct timespec *time,
Quentin Glidiccde13452016-08-12 10:41:32 +0200403 struct weston_pointer_motion_event *event)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400404{
Jonas Ådahld2510102014-10-05 21:39:14 +0200405 wl_fixed_t x, y;
Jonas Ådahl8283c342015-04-24 15:26:17 +0800406 wl_fixed_t old_sx = pointer->sx;
407 wl_fixed_t old_sy = pointer->sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400408
Jonas Ådahld2510102014-10-05 21:39:14 +0200409 if (pointer->focus) {
410 weston_pointer_motion_to_abs(pointer, event, &x, &y);
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -0800411 weston_view_from_global_fixed(pointer->focus, x, y,
412 &pointer->sx, &pointer->sy);
Jonas Ådahld2510102014-10-05 21:39:14 +0200413 }
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -0800414
Jonas Ådahld2510102014-10-05 21:39:14 +0200415 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100416
Jonas Ådahlf44942e2016-07-22 17:54:55 +0800417 if (old_sx != pointer->sx || old_sy != pointer->sy) {
Quentin Glidiccde13452016-08-12 10:41:32 +0200418 pointer_send_motion(pointer, time,
419 pointer->sx, pointer->sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400420 }
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200421
Quentin Glidiccde13452016-08-12 10:41:32 +0200422 pointer_send_relative_motion(pointer, time, event);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400423}
424
425static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200426default_grab_pointer_motion(struct weston_pointer_grab *grab,
427 const struct timespec *time,
Quentin Glidiccde13452016-08-12 10:41:32 +0200428 struct weston_pointer_motion_event *event)
429{
430 weston_pointer_send_motion(grab->pointer, time, event);
431}
432
433/** Check if the pointer has focused resources.
434 *
435 * \param pointer The pointer to check for focused resources.
436 * \return Whether or not this pointer has focused resources
437 */
438WL_EXPORT bool
439weston_pointer_has_focus_resource(struct weston_pointer *pointer)
440{
441 if (!pointer->focus_client)
442 return false;
443
444 if (wl_list_empty(&pointer->focus_client->pointer_resources))
445 return false;
446
447 return true;
448}
449
450/** Send wl_pointer.button events to focused resources.
451 *
452 * \param pointer The pointer where the button events originates from.
453 * \param time The timestamp of the event
454 * \param button The button value of the event
455 * \param value The state enum value of the event
456 *
457 * For every resource that is currently in focus, send a wl_pointer.button event
458 * with the passed parameters. The focused resources are the wl_pointer
459 * resources of the client which currently has the surface with pointer focus.
460 */
461WL_EXPORT void
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800462weston_pointer_send_button(struct weston_pointer *pointer,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200463 const struct timespec *time, uint32_t button,
Quentin Glidiccde13452016-08-12 10:41:32 +0200464 enum wl_pointer_button_state state)
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800465{
466 struct wl_display *display = pointer->seat->compositor->wl_display;
467 struct wl_list *resource_list;
468 struct wl_resource *resource;
469 uint32_t serial;
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200470 uint32_t msecs;
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800471
Quentin Glidiccde13452016-08-12 10:41:32 +0200472 if (!weston_pointer_has_focus_resource(pointer))
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800473 return;
474
475 resource_list = &pointer->focus_client->pointer_resources;
Quentin Glidiccde13452016-08-12 10:41:32 +0200476 serial = wl_display_next_serial(display);
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200477 msecs = timespec_to_msec(time);
Quentin Glidiccde13452016-08-12 10:41:32 +0200478 wl_resource_for_each(resource, resource_list)
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200479 wl_pointer_send_button(resource, serial, msecs, button, state);
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800480}
481
482static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700483default_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200484 const struct timespec *time, uint32_t button,
Quentin Glidiccde13452016-08-12 10:41:32 +0200485 enum wl_pointer_button_state state)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400486{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400487 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400488 struct weston_compositor *compositor = pointer->seat->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500489 struct weston_view *view;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400490 wl_fixed_t sx, sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400491
Quentin Glidiccde13452016-08-12 10:41:32 +0200492 weston_pointer_send_button(pointer, time, button, state);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400493
494 if (pointer->button_count == 0 &&
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400495 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500496 view = weston_compositor_pick_view(compositor,
497 pointer->x, pointer->y,
498 &sx, &sy);
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400499
Jason Ekstranda7af7042013-10-12 22:38:11 -0500500 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400501 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400502}
503
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200504/** Send wl_pointer.axis events to focused resources.
505 *
506 * \param pointer The pointer where the axis events originates from.
507 * \param time The timestamp of the event
508 * \param axis The axis enum value of the event
509 * \param value The axis value of the event
510 *
511 * For every resource that is currently in focus, send a wl_pointer.axis event
512 * with the passed parameters. The focused resources are the wl_pointer
513 * resources of the client which currently has the surface with pointer focus.
514 */
515WL_EXPORT void
516weston_pointer_send_axis(struct weston_pointer *pointer,
Peter Hutterer89b6a492016-01-18 15:58:17 +1000517 uint32_t time,
518 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200519{
520 struct wl_resource *resource;
521 struct wl_list *resource_list;
522
Quentin Glidiccde13452016-08-12 10:41:32 +0200523 if (!weston_pointer_has_focus_resource(pointer))
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800524 return;
525
526 resource_list = &pointer->focus_client->pointer_resources;
Peter Hutterer87743e92016-01-18 16:38:22 +1000527 wl_resource_for_each(resource, resource_list) {
528 if (event->has_discrete &&
529 wl_resource_get_version(resource) >=
530 WL_POINTER_AXIS_DISCRETE_SINCE_VERSION)
531 wl_pointer_send_axis_discrete(resource, event->axis,
532 event->discrete);
533
534 if (event->value)
535 wl_pointer_send_axis(resource, time,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200536 event->axis,
537 wl_fixed_from_double(event->value));
Peter Hutterer87743e92016-01-18 16:38:22 +1000538 else if (wl_resource_get_version(resource) >=
539 WL_POINTER_AXIS_STOP_SINCE_VERSION)
540 wl_pointer_send_axis_stop(resource, time,
541 event->axis);
542 }
543}
544
Quentin Glidiccde13452016-08-12 10:41:32 +0200545/** Send wl_pointer.axis_source events to focused resources.
546 *
547 * \param pointer The pointer where the axis_source events originates from.
548 * \param source The axis_source enum value of the event
549 *
550 * For every resource that is currently in focus, send a wl_pointer.axis_source
551 * event with the passed parameter. The focused resources are the wl_pointer
552 * resources of the client which currently has the surface with pointer focus.
553 */
Peter Hutterer87743e92016-01-18 16:38:22 +1000554WL_EXPORT void
Quentin Glidiccde13452016-08-12 10:41:32 +0200555weston_pointer_send_axis_source(struct weston_pointer *pointer,
556 enum wl_pointer_axis_source source)
Peter Hutterer87743e92016-01-18 16:38:22 +1000557{
558 struct wl_resource *resource;
559 struct wl_list *resource_list;
560
Quentin Glidiccde13452016-08-12 10:41:32 +0200561 if (!weston_pointer_has_focus_resource(pointer))
Jonas Ådahled6014a2016-04-21 10:21:48 +0800562 return;
563
Peter Hutterer87743e92016-01-18 16:38:22 +1000564 resource_list = &pointer->focus_client->pointer_resources;
565 wl_resource_for_each(resource, resource_list) {
566 if (wl_resource_get_version(resource) >=
567 WL_POINTER_AXIS_SOURCE_SINCE_VERSION) {
568 wl_pointer_send_axis_source(resource, source);
569 }
570 }
571}
572
573static void
574pointer_send_frame(struct wl_resource *resource)
575{
576 if (wl_resource_get_version(resource) >=
577 WL_POINTER_FRAME_SINCE_VERSION) {
578 wl_pointer_send_frame(resource);
579 }
580}
581
Quentin Glidiccde13452016-08-12 10:41:32 +0200582/** Send wl_pointer.frame events to focused resources.
583 *
584 * \param pointer The pointer where the frame events originates from.
585 *
586 * For every resource that is currently in focus, send a wl_pointer.frame event.
587 * The focused resources are the wl_pointer resources of the client which
588 * currently has the surface with pointer focus.
589 */
Peter Hutterer87743e92016-01-18 16:38:22 +1000590WL_EXPORT void
591weston_pointer_send_frame(struct weston_pointer *pointer)
592{
593 struct wl_resource *resource;
594 struct wl_list *resource_list;
595
Quentin Glidiccde13452016-08-12 10:41:32 +0200596 if (!weston_pointer_has_focus_resource(pointer))
Derek Foreman8efa31b2016-01-29 10:29:46 -0600597 return;
598
Peter Hutterer87743e92016-01-18 16:38:22 +1000599 resource_list = &pointer->focus_client->pointer_resources;
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200600 wl_resource_for_each(resource, resource_list)
Peter Hutterer87743e92016-01-18 16:38:22 +1000601 pointer_send_frame(resource);
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200602}
603
604static void
605default_grab_pointer_axis(struct weston_pointer_grab *grab,
Peter Hutterer89b6a492016-01-18 15:58:17 +1000606 uint32_t time,
607 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200608{
Peter Hutterer89b6a492016-01-18 15:58:17 +1000609 weston_pointer_send_axis(grab->pointer, time, event);
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200610}
611
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200612static void
Peter Hutterer87743e92016-01-18 16:38:22 +1000613default_grab_pointer_axis_source(struct weston_pointer_grab *grab,
Quentin Glidiccde13452016-08-12 10:41:32 +0200614 enum wl_pointer_axis_source source)
Peter Hutterer87743e92016-01-18 16:38:22 +1000615{
616 weston_pointer_send_axis_source(grab->pointer, source);
617}
618
619static void
620default_grab_pointer_frame(struct weston_pointer_grab *grab)
621{
622 weston_pointer_send_frame(grab->pointer);
623}
624
625static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200626default_grab_pointer_cancel(struct weston_pointer_grab *grab)
627{
628}
629
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400630static const struct weston_pointer_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400631 default_pointer_grab_interface = {
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700632 default_grab_pointer_focus,
633 default_grab_pointer_motion,
634 default_grab_pointer_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200635 default_grab_pointer_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +1000636 default_grab_pointer_axis_source,
637 default_grab_pointer_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200638 default_grab_pointer_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400639};
640
Quentin Glidiccde13452016-08-12 10:41:32 +0200641/** Check if the touch has focused resources.
642 *
643 * \param touch The touch to check for focused resources.
644 * \return Whether or not this touch has focused resources
645 */
646WL_EXPORT bool
647weston_touch_has_focus_resource(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400648{
Quentin Glidiccde13452016-08-12 10:41:32 +0200649 if (!touch->focus)
650 return false;
651
652 if (wl_list_empty(&touch->focus_resource_list))
653 return false;
654
655 return true;
656}
657
658/** Send wl_touch.down events to focused resources.
659 *
660 * \param touch The touch where the down events originates from.
661 * \param time The timestamp of the event
662 * \param touch_id The touch_id value of the event
663 * \param x The x value of the event
664 * \param y The y value of the event
665 *
666 * For every resource that is currently in focus, send a wl_touch.down event
667 * with the passed parameters. The focused resources are the wl_touch
668 * resources of the client which currently has the surface with touch focus.
669 */
670WL_EXPORT void
671weston_touch_send_down(struct weston_touch *touch, uint32_t time,
672 int touch_id, wl_fixed_t x, wl_fixed_t y)
673{
Rob Bradford880ebc72013-07-22 17:31:38 +0100674 struct wl_display *display = touch->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400675 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100676 struct wl_resource *resource;
677 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300678 wl_fixed_t sx, sy;
679
Quentin Glidiccde13452016-08-12 10:41:32 +0200680 if (!weston_touch_has_focus_resource(touch))
Bryce Harrington2c40d1d2016-02-02 10:18:48 -0800681 return;
682
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300683 weston_view_from_global_fixed(touch->focus, x, y, &sx, &sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400684
Neil Roberts96d790e2013-09-19 17:32:00 +0100685 resource_list = &touch->focus_resource_list;
Quentin Glidiccde13452016-08-12 10:41:32 +0200686 serial = wl_display_next_serial(display);
687 wl_resource_for_each(resource, resource_list)
688 wl_touch_send_down(resource, serial, time,
689 touch->focus->surface->resource,
690 touch_id, sx, sy);
691}
Neil Roberts96d790e2013-09-19 17:32:00 +0100692
Quentin Glidiccde13452016-08-12 10:41:32 +0200693static void
694default_grab_touch_down(struct weston_touch_grab *grab, uint32_t time,
695 int touch_id, wl_fixed_t x, wl_fixed_t y)
696{
697 weston_touch_send_down(grab->touch, time, touch_id, x, y);
698}
699
700/** Send wl_touch.up events to focused resources.
701 *
702 * \param touch The touch where the up events originates from.
703 * \param time The timestamp of the event
704 * \param touch_id The touch_id value of the event
705 *
706 * For every resource that is currently in focus, send a wl_touch.up event
707 * with the passed parameters. The focused resources are the wl_touch
708 * resources of the client which currently has the surface with touch focus.
709 */
710WL_EXPORT void
711weston_touch_send_up(struct weston_touch *touch, uint32_t time, int touch_id)
712{
713 struct wl_display *display = touch->seat->compositor->wl_display;
714 uint32_t serial;
715 struct wl_resource *resource;
716 struct wl_list *resource_list;
717
718 if (!weston_touch_has_focus_resource(touch))
719 return;
720
721 resource_list = &touch->focus_resource_list;
722 serial = wl_display_next_serial(display);
723 wl_resource_for_each(resource, resource_list)
724 wl_touch_send_up(resource, serial, time, touch_id);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400725}
726
Kristian Høgsberge329f362013-05-06 22:19:57 -0400727static void
728default_grab_touch_up(struct weston_touch_grab *grab,
729 uint32_t time, int touch_id)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400730{
Quentin Glidiccde13452016-08-12 10:41:32 +0200731 weston_touch_send_up(grab->touch, time, touch_id);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400732}
733
Quentin Glidiccde13452016-08-12 10:41:32 +0200734/** Send wl_touch.motion events to focused resources.
735 *
736 * \param touch The touch where the motion events originates from.
737 * \param time The timestamp of the event
738 * \param touch_id The touch_id value of the event
739 * \param x The x value of the event
740 * \param y The y value of the event
741 *
742 * For every resource that is currently in focus, send a wl_touch.motion event
743 * with the passed parameters. The focused resources are the wl_touch
744 * resources of the client which currently has the surface with touch focus.
745 */
746WL_EXPORT void
747weston_touch_send_motion(struct weston_touch *touch, uint32_t time,
748 int touch_id, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400749{
Neil Roberts96d790e2013-09-19 17:32:00 +0100750 struct wl_resource *resource;
751 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300752 wl_fixed_t sx, sy;
753
Quentin Glidiccde13452016-08-12 10:41:32 +0200754 if (!weston_touch_has_focus_resource(touch))
755 return;
756
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300757 weston_view_from_global_fixed(touch->focus, x, y, &sx, &sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400758
Neil Roberts96d790e2013-09-19 17:32:00 +0100759 resource_list = &touch->focus_resource_list;
Neil Roberts96d790e2013-09-19 17:32:00 +0100760 wl_resource_for_each(resource, resource_list) {
761 wl_touch_send_motion(resource, time,
Kristian Høgsberge329f362013-05-06 22:19:57 -0400762 touch_id, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400763 }
764}
765
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200766static void
Quentin Glidiccde13452016-08-12 10:41:32 +0200767default_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time,
768 int touch_id, wl_fixed_t x, wl_fixed_t y)
769{
770 weston_touch_send_motion(grab->touch, time, touch_id, x, y);
771}
772
773
774/** Send wl_touch.frame events to focused resources.
775 *
776 * \param touch The touch where the frame events originates from.
777 *
778 * For every resource that is currently in focus, send a wl_touch.frame event.
779 * The focused resources are the wl_touch resources of the client which
780 * currently has the surface with touch focus.
781 */
782WL_EXPORT void
783weston_touch_send_frame(struct weston_touch *touch)
Jonas Ådahl1679f232014-04-12 09:39:51 +0200784{
785 struct wl_resource *resource;
786
Quentin Glidiccde13452016-08-12 10:41:32 +0200787 if (!weston_touch_has_focus_resource(touch))
788 return;
789
790 wl_resource_for_each(resource, &touch->focus_resource_list)
Jonas Ådahl1679f232014-04-12 09:39:51 +0200791 wl_touch_send_frame(resource);
792}
793
794static void
Quentin Glidiccde13452016-08-12 10:41:32 +0200795default_grab_touch_frame(struct weston_touch_grab *grab)
796{
797 weston_touch_send_frame(grab->touch);
798}
799
800static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200801default_grab_touch_cancel(struct weston_touch_grab *grab)
802{
803}
804
Kristian Høgsberge329f362013-05-06 22:19:57 -0400805static const struct weston_touch_grab_interface default_touch_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400806 default_grab_touch_down,
807 default_grab_touch_up,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200808 default_grab_touch_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +0200809 default_grab_touch_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200810 default_grab_touch_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400811};
812
Quentin Glidiccde13452016-08-12 10:41:32 +0200813/** Check if the keyboard has focused resources.
814 *
815 * \param keyboard The keyboard to check for focused resources.
816 * \return Whether or not this keyboard has focused resources
817 */
818WL_EXPORT bool
819weston_keyboard_has_focus_resource(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400820{
Quentin Glidiccde13452016-08-12 10:41:32 +0200821 if (!keyboard->focus)
822 return false;
823
824 if (wl_list_empty(&keyboard->focus_resource_list))
825 return false;
826
827 return true;
828}
829
830/** Send wl_keyboard.key events to focused resources.
831 *
832 * \param keyboard The keyboard where the key events originates from.
833 * \param time The timestamp of the event
834 * \param key The key value of the event
835 * \param state The state enum value of the event
836 *
837 * For every resource that is currently in focus, send a wl_keyboard.key event
838 * with the passed parameters. The focused resources are the wl_keyboard
839 * resources of the client which currently has the surface with keyboard focus.
840 */
841WL_EXPORT void
842weston_keyboard_send_key(struct weston_keyboard *keyboard,
843 uint32_t time, uint32_t key,
844 enum wl_keyboard_key_state state)
845{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400846 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +0100847 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400848 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100849 struct wl_list *resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400850
Quentin Glidiccde13452016-08-12 10:41:32 +0200851 if (!weston_keyboard_has_focus_resource(keyboard))
852 return;
853
Neil Roberts96d790e2013-09-19 17:32:00 +0100854 resource_list = &keyboard->focus_resource_list;
Quentin Glidiccde13452016-08-12 10:41:32 +0200855 serial = wl_display_next_serial(display);
856 wl_resource_for_each(resource, resource_list)
857 wl_keyboard_send_key(resource, serial, time, key, state);
858};
859
860static void
861default_grab_keyboard_key(struct weston_keyboard_grab *grab,
862 uint32_t time, uint32_t key, uint32_t state)
863{
864 weston_keyboard_send_key(grab->keyboard, time, key, state);
Neil Roberts96d790e2013-09-19 17:32:00 +0100865}
866
867static void
868send_modifiers_to_resource(struct weston_keyboard *keyboard,
869 struct wl_resource *resource,
870 uint32_t serial)
871{
872 wl_keyboard_send_modifiers(resource,
873 serial,
874 keyboard->modifiers.mods_depressed,
875 keyboard->modifiers.mods_latched,
876 keyboard->modifiers.mods_locked,
877 keyboard->modifiers.group);
878}
879
880static void
881send_modifiers_to_client_in_list(struct wl_client *client,
882 struct wl_list *list,
883 uint32_t serial,
884 struct weston_keyboard *keyboard)
885{
886 struct wl_resource *resource;
887
888 wl_resource_for_each(resource, list) {
889 if (wl_resource_get_client(resource) == client)
890 send_modifiers_to_resource(keyboard,
891 resource,
892 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400893 }
894}
895
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800896static struct weston_pointer_client *
897find_pointer_client_for_surface(struct weston_pointer *pointer,
898 struct weston_surface *surface)
899{
900 struct wl_client *client;
901
902 if (!surface)
903 return NULL;
904
905 if (!surface->resource)
906 return NULL;
907
908 client = wl_resource_get_client(surface->resource);
909 return weston_pointer_get_pointer_client(pointer, client);
910}
911
912static struct weston_pointer_client *
913find_pointer_client_for_view(struct weston_pointer *pointer, struct weston_view *view)
914{
915 if (!view)
916 return NULL;
917
918 return find_pointer_client_for_surface(pointer, view->surface);
919}
920
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400921static struct wl_resource *
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400922find_resource_for_surface(struct wl_list *list, struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400923{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400924 if (!surface)
925 return NULL;
926
Jason Ekstrand44a38632013-06-14 10:08:00 -0500927 if (!surface->resource)
928 return NULL;
Stefan Schmidtfda26522013-09-17 10:54:09 +0100929
Jason Ekstrand44a38632013-06-14 10:08:00 -0500930 return wl_resource_find_for_client(list, wl_resource_get_client(surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400931}
932
Quentin Glidiccde13452016-08-12 10:41:32 +0200933/** Send wl_keyboard.modifiers events to focused resources and pointer
934 * focused resources.
935 *
936 * \param keyboard The keyboard where the modifiers events originates from.
937 * \param serial The serial of the event
938 * \param mods_depressed The mods_depressed value of the event
939 * \param mods_latched The mods_latched value of the event
940 * \param mods_locked The mods_locked value of the event
941 * \param group The group value of the event
942 *
943 * For every resource that is currently in focus, send a wl_keyboard.modifiers
944 * event with the passed parameters. The focused resources are the wl_keyboard
945 * resources of the client which currently has the surface with keyboard focus.
946 * This also sends wl_keyboard.modifiers events to the wl_keyboard resources of
947 * the client having pointer focus (if different from the keyboard focus client).
948 */
949WL_EXPORT void
950weston_keyboard_send_modifiers(struct weston_keyboard *keyboard,
951 uint32_t serial, uint32_t mods_depressed,
952 uint32_t mods_latched,
953 uint32_t mods_locked, uint32_t group)
954{
955 struct weston_pointer *pointer =
956 weston_seat_get_pointer(keyboard->seat);
957
958 if (weston_keyboard_has_focus_resource(keyboard)) {
959 struct wl_list *resource_list;
960 struct wl_resource *resource;
961
962 resource_list = &keyboard->focus_resource_list;
963 wl_resource_for_each(resource, resource_list) {
964 wl_keyboard_send_modifiers(resource, serial,
965 mods_depressed, mods_latched,
966 mods_locked, group);
967 }
968 }
969
970 if (pointer && pointer->focus && pointer->focus->surface->resource &&
971 pointer->focus->surface != keyboard->focus) {
972 struct wl_client *pointer_client =
973 wl_resource_get_client(pointer->focus->surface->resource);
974
975 send_modifiers_to_client_in_list(pointer_client,
976 &keyboard->resource_list,
977 serial,
978 keyboard);
979 }
980}
981
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400982static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700983default_grab_keyboard_modifiers(struct weston_keyboard_grab *grab,
984 uint32_t serial, uint32_t mods_depressed,
985 uint32_t mods_latched,
986 uint32_t mods_locked, uint32_t group)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400987{
Quentin Glidiccde13452016-08-12 10:41:32 +0200988 weston_keyboard_send_modifiers(grab->keyboard, serial, mods_depressed,
989 mods_latched, mods_locked, group);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400990}
991
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200992static void
993default_grab_keyboard_cancel(struct weston_keyboard_grab *grab)
994{
995}
996
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400997static const struct weston_keyboard_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400998 default_keyboard_grab_interface = {
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700999 default_grab_keyboard_key,
1000 default_grab_keyboard_modifiers,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001001 default_grab_keyboard_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001002};
1003
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001004static void
1005pointer_unmap_sprite(struct weston_pointer *pointer)
1006{
Pekka Paalanenc557ff72014-11-12 16:42:52 +02001007 struct weston_surface *surface = pointer->sprite->surface;
1008
1009 if (weston_surface_is_mapped(surface))
1010 weston_surface_unmap(surface);
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001011
1012 wl_list_remove(&pointer->sprite_destroy_listener.link);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02001013 surface->committed = NULL;
1014 surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03001015 weston_surface_set_label_func(surface, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001016 weston_view_destroy(pointer->sprite);
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001017 pointer->sprite = NULL;
1018}
1019
1020static void
1021pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
1022{
1023 struct weston_pointer *pointer =
1024 container_of(listener, struct weston_pointer,
1025 sprite_destroy_listener);
1026
1027 pointer->sprite = NULL;
1028}
1029
Jonas Ådahl3e12e632013-12-02 22:05:05 +01001030static void
1031weston_pointer_reset_state(struct weston_pointer *pointer)
1032{
1033 pointer->button_count = 0;
1034}
1035
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001036static void
1037weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data);
1038
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001039WL_EXPORT struct weston_pointer *
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001040weston_pointer_create(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001041{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001042 struct weston_pointer *pointer;
1043
Peter Huttererf3d62272013-08-08 11:57:05 +10001044 pointer = zalloc(sizeof *pointer);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001045 if (pointer == NULL)
1046 return NULL;
1047
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001048 wl_list_init(&pointer->pointer_clients);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001049 weston_pointer_set_default_grab(pointer,
1050 seat->compositor->default_pointer_grab);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001051 wl_list_init(&pointer->focus_resource_listener.link);
1052 pointer->focus_resource_listener.notify = pointer_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001053 pointer->default_grab.pointer = pointer;
1054 pointer->grab = &pointer->default_grab;
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001055 wl_signal_init(&pointer->motion_signal);
Emilio Pozuelo Monfortaa7a4762013-11-19 11:37:15 +01001056 wl_signal_init(&pointer->focus_signal);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001057 wl_list_init(&pointer->focus_view_listener.link);
Jonas Ådahl3eb4ddd2016-07-22 17:52:58 +08001058 wl_signal_init(&pointer->destroy_signal);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001059
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001060 pointer->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
1061
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001062 /* FIXME: Pick better co-ords. */
1063 pointer->x = wl_fixed_from_int(100);
1064 pointer->y = wl_fixed_from_int(100);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001065
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001066 pointer->output_destroy_listener.notify =
1067 weston_pointer_handle_output_destroy;
1068 wl_signal_add(&seat->compositor->output_destroyed_signal,
1069 &pointer->output_destroy_listener);
1070
Derek Foremanf9318d12015-05-11 15:40:11 -05001071 pointer->sx = wl_fixed_from_int(-1000000);
1072 pointer->sy = wl_fixed_from_int(-1000000);
1073
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001074 return pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001075}
1076
1077WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001078weston_pointer_destroy(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001079{
Jonas Ådahl3eb4ddd2016-07-22 17:52:58 +08001080 wl_signal_emit(&pointer->destroy_signal, pointer);
1081
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001082 if (pointer->sprite)
1083 pointer_unmap_sprite(pointer);
1084
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001085 /* XXX: What about pointer->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +01001086
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001087 wl_list_remove(&pointer->focus_resource_listener.link);
1088 wl_list_remove(&pointer->focus_view_listener.link);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001089 wl_list_remove(&pointer->output_destroy_listener.link);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001090 free(pointer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001091}
1092
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001093void
1094weston_pointer_set_default_grab(struct weston_pointer *pointer,
1095 const struct weston_pointer_grab_interface *interface)
1096{
1097 if (interface)
1098 pointer->default_grab.interface = interface;
1099 else
1100 pointer->default_grab.interface =
1101 &default_pointer_grab_interface;
1102}
1103
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001104WL_EXPORT struct weston_keyboard *
1105weston_keyboard_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001106{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001107 struct weston_keyboard *keyboard;
1108
Peter Huttererf3d62272013-08-08 11:57:05 +10001109 keyboard = zalloc(sizeof *keyboard);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001110 if (keyboard == NULL)
Neil Roberts96d790e2013-09-19 17:32:00 +01001111 return NULL;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001112
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001113 wl_list_init(&keyboard->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +01001114 wl_list_init(&keyboard->focus_resource_list);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001115 wl_list_init(&keyboard->focus_resource_listener.link);
1116 keyboard->focus_resource_listener.notify = keyboard_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001117 wl_array_init(&keyboard->keys);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001118 keyboard->default_grab.interface = &default_keyboard_grab_interface;
1119 keyboard->default_grab.keyboard = keyboard;
1120 keyboard->grab = &keyboard->default_grab;
1121 wl_signal_init(&keyboard->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001122
1123 return keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001124}
1125
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001126static void
1127weston_xkb_info_destroy(struct weston_xkb_info *xkb_info);
1128
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001129WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001130weston_keyboard_destroy(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001131{
1132 /* XXX: What about keyboard->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +01001133
Derek Foreman185d1582017-06-28 11:17:23 -05001134 xkb_state_unref(keyboard->xkb_state.state);
1135 if (keyboard->xkb_info)
1136 weston_xkb_info_destroy(keyboard->xkb_info);
1137 xkb_keymap_unref(keyboard->pending_keymap);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001138
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001139 wl_array_release(&keyboard->keys);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001140 wl_list_remove(&keyboard->focus_resource_listener.link);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001141 free(keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001142}
1143
Jonas Ådahlcbfa7f72013-12-02 22:05:04 +01001144static void
1145weston_touch_reset_state(struct weston_touch *touch)
1146{
1147 touch->num_tp = 0;
1148}
1149
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001150WL_EXPORT struct weston_touch *
1151weston_touch_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001152{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001153 struct weston_touch *touch;
1154
Peter Huttererf3d62272013-08-08 11:57:05 +10001155 touch = zalloc(sizeof *touch);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001156 if (touch == NULL)
1157 return NULL;
1158
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001159 wl_list_init(&touch->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +01001160 wl_list_init(&touch->focus_resource_list);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001161 wl_list_init(&touch->focus_view_listener.link);
1162 touch->focus_view_listener.notify = touch_focus_view_destroyed;
1163 wl_list_init(&touch->focus_resource_listener.link);
1164 touch->focus_resource_listener.notify = touch_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001165 touch->default_grab.interface = &default_touch_grab_interface;
1166 touch->default_grab.touch = touch;
1167 touch->grab = &touch->default_grab;
1168 wl_signal_init(&touch->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001169
1170 return touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001171}
1172
1173WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001174weston_touch_destroy(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001175{
1176 /* XXX: What about touch->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +01001177
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001178 wl_list_remove(&touch->focus_view_listener.link);
1179 wl_list_remove(&touch->focus_resource_listener.link);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001180 free(touch);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001181}
1182
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001183static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04001184seat_send_updated_caps(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001185{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001186 enum wl_seat_capability caps = 0;
Rob Bradford6e737f52013-09-06 17:48:19 +01001187 struct wl_resource *resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001188
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001189 if (seat->pointer_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001190 caps |= WL_SEAT_CAPABILITY_POINTER;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001191 if (seat->keyboard_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001192 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001193 if (seat->touch_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001194 caps |= WL_SEAT_CAPABILITY_TOUCH;
1195
Rob Bradford6e737f52013-09-06 17:48:19 +01001196 wl_resource_for_each(resource, &seat->base_resource_list) {
1197 wl_seat_send_capabilities(resource, caps);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001198 }
Jason Ekstranda4ab5422014-04-02 19:53:45 -05001199 wl_signal_emit(&seat->updated_caps_signal, seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001200}
1201
Derek Foremanf9318d12015-05-11 15:40:11 -05001202
1203/** Clear the pointer focus
1204 *
1205 * \param pointer the pointer to clear focus for.
1206 *
1207 * This can be used to unset pointer focus and set the co-ordinates to the
1208 * arbitrary values we use for the no focus case.
1209 *
1210 * There's no requirement to use this function. For example, passing the
1211 * results of a weston_compositor_pick_view() directly to
1212 * weston_pointer_set_focus() will do the right thing when no view is found.
1213 */
1214WL_EXPORT void
1215weston_pointer_clear_focus(struct weston_pointer *pointer)
1216{
1217 weston_pointer_set_focus(pointer, NULL,
1218 wl_fixed_from_int(-1000000),
1219 wl_fixed_from_int(-1000000));
1220}
1221
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001222WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001223weston_pointer_set_focus(struct weston_pointer *pointer,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001224 struct weston_view *view,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001225 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001226{
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001227 struct weston_pointer_client *pointer_client;
Derek Foreman1281a362015-07-31 16:55:32 -05001228 struct weston_keyboard *kbd = weston_seat_get_keyboard(pointer->seat);
Neil Roberts96d790e2013-09-19 17:32:00 +01001229 struct wl_resource *resource;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001230 struct wl_resource *surface_resource;
Rob Bradford880ebc72013-07-22 17:31:38 +01001231 struct wl_display *display = pointer->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001232 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01001233 struct wl_list *focus_resource_list;
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001234 int refocus = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001235
1236 if ((!pointer->focus && view) ||
1237 (pointer->focus && !view) ||
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001238 (pointer->focus && pointer->focus->surface != view->surface) ||
1239 pointer->sx != sx || pointer->sy != sy)
1240 refocus = 1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001241
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001242 if (pointer->focus_client && refocus) {
1243 focus_resource_list = &pointer->focus_client->pointer_resources;
1244 if (!wl_list_empty(focus_resource_list)) {
1245 serial = wl_display_next_serial(display);
1246 surface_resource = pointer->focus->surface->resource;
1247 wl_resource_for_each(resource, focus_resource_list) {
1248 wl_pointer_send_leave(resource, serial,
1249 surface_resource);
Peter Hutterer87743e92016-01-18 16:38:22 +10001250 pointer_send_frame(resource);
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001251 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001252 }
1253
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001254 pointer->focus_client = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001255 }
1256
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001257 pointer_client = find_pointer_client_for_view(pointer, view);
1258 if (pointer_client && refocus) {
1259 struct wl_client *surface_client = pointer_client->client;
Neil Roberts96d790e2013-09-19 17:32:00 +01001260
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001261 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001262
Jason Ekstranda7af7042013-10-12 22:38:11 -05001263 if (kbd && kbd->focus != view->surface)
Kristian Høgsbergcb406f12013-10-09 10:54:03 -07001264 send_modifiers_to_client_in_list(surface_client,
1265 &kbd->resource_list,
1266 serial,
1267 kbd);
1268
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001269 pointer->focus_client = pointer_client;
Neil Roberts96d790e2013-09-19 17:32:00 +01001270
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001271 focus_resource_list = &pointer->focus_client->pointer_resources;
Neil Roberts96d790e2013-09-19 17:32:00 +01001272 wl_resource_for_each(resource, focus_resource_list) {
1273 wl_pointer_send_enter(resource,
1274 serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001275 view->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01001276 sx, sy);
Peter Hutterer87743e92016-01-18 16:38:22 +10001277 pointer_send_frame(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001278 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001279
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001280 pointer->focus_serial = serial;
1281 }
1282
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001283 wl_list_remove(&pointer->focus_view_listener.link);
1284 wl_list_init(&pointer->focus_view_listener.link);
1285 wl_list_remove(&pointer->focus_resource_listener.link);
1286 wl_list_init(&pointer->focus_resource_listener.link);
Emilio Pozuelo Monfortaa7a4762013-11-19 11:37:15 +01001287 if (view)
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001288 wl_signal_add(&view->destroy_signal, &pointer->focus_view_listener);
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001289 if (view && view->surface->resource)
1290 wl_resource_add_destroy_listener(view->surface->resource,
1291 &pointer->focus_resource_listener);
1292
1293 pointer->focus = view;
1294 pointer->focus_view_listener.notify = pointer_focus_view_destroyed;
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001295 pointer->sx = sx;
1296 pointer->sy = sy;
1297
Derek Foremanf9318d12015-05-11 15:40:11 -05001298 assert(view || sx == wl_fixed_from_int(-1000000));
1299 assert(view || sy == wl_fixed_from_int(-1000000));
1300
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001301 wl_signal_emit(&pointer->focus_signal, pointer);
1302}
1303
Neil Roberts96d790e2013-09-19 17:32:00 +01001304static void
1305send_enter_to_resource_list(struct wl_list *list,
1306 struct weston_keyboard *keyboard,
1307 struct weston_surface *surface,
1308 uint32_t serial)
1309{
1310 struct wl_resource *resource;
1311
1312 wl_resource_for_each(resource, list) {
1313 send_modifiers_to_resource(keyboard, resource, serial);
1314 wl_keyboard_send_enter(resource, serial,
1315 surface->resource,
1316 &keyboard->keys);
1317 }
1318}
1319
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001320WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001321weston_keyboard_set_focus(struct weston_keyboard *keyboard,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001322 struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001323{
1324 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +01001325 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001326 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01001327 struct wl_list *focus_resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001328
Neil Roberts96d790e2013-09-19 17:32:00 +01001329 focus_resource_list = &keyboard->focus_resource_list;
1330
1331 if (!wl_list_empty(focus_resource_list) && keyboard->focus != surface) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001332 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001333 wl_resource_for_each(resource, focus_resource_list) {
1334 wl_keyboard_send_leave(resource, serial,
1335 keyboard->focus->resource);
1336 }
1337 move_resources(&keyboard->resource_list, focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001338 }
1339
Neil Roberts96d790e2013-09-19 17:32:00 +01001340 if (find_resource_for_surface(&keyboard->resource_list, surface) &&
1341 keyboard->focus != surface) {
1342 struct wl_client *surface_client =
1343 wl_resource_get_client(surface->resource);
1344
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001345 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001346
1347 move_resources_for_client(focus_resource_list,
1348 &keyboard->resource_list,
1349 surface_client);
1350 send_enter_to_resource_list(focus_resource_list,
1351 keyboard,
1352 surface,
1353 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001354 keyboard->focus_serial = serial;
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001355 }
1356
1357 wl_list_remove(&keyboard->focus_resource_listener.link);
1358 wl_list_init(&keyboard->focus_resource_listener.link);
1359 if (surface && surface->resource)
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001360 wl_resource_add_destroy_listener(surface->resource,
1361 &keyboard->focus_resource_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001362
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001363 keyboard->focus = surface;
1364 wl_signal_emit(&keyboard->focus_signal, keyboard);
1365}
1366
Giulio Camuffoa20ca812014-11-22 11:16:56 +02001367/* Users of this function must manually manage the keyboard focus */
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001368WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001369weston_keyboard_start_grab(struct weston_keyboard *keyboard,
1370 struct weston_keyboard_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001371{
1372 keyboard->grab = grab;
1373 grab->keyboard = keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001374}
1375
1376WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001377weston_keyboard_end_grab(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001378{
1379 keyboard->grab = &keyboard->default_grab;
1380}
1381
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001382static void
1383weston_keyboard_cancel_grab(struct weston_keyboard *keyboard)
1384{
1385 keyboard->grab->interface->cancel(keyboard->grab);
1386}
1387
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001388WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001389weston_pointer_start_grab(struct weston_pointer *pointer,
1390 struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001391{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001392 pointer->grab = grab;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001393 grab->pointer = pointer;
Kristian Høgsbergda751b82013-07-04 00:58:07 -04001394 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001395}
1396
1397WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001398weston_pointer_end_grab(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001399{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001400 pointer->grab = &pointer->default_grab;
Kristian Høgsbergda751b82013-07-04 00:58:07 -04001401 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001402}
1403
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001404static void
1405weston_pointer_cancel_grab(struct weston_pointer *pointer)
1406{
1407 pointer->grab->interface->cancel(pointer->grab);
1408}
1409
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001410WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -04001411weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001412{
1413 touch->grab = grab;
1414 grab->touch = touch;
1415}
1416
1417WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -04001418weston_touch_end_grab(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001419{
1420 touch->grab = &touch->default_grab;
1421}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001422
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001423static void
1424weston_touch_cancel_grab(struct weston_touch *touch)
1425{
1426 touch->grab->interface->cancel(touch->grab);
1427}
1428
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001429static void
1430weston_pointer_clamp_for_output(struct weston_pointer *pointer,
1431 struct weston_output *output,
1432 wl_fixed_t *fx, wl_fixed_t *fy)
1433{
1434 int x, y;
1435
1436 x = wl_fixed_to_int(*fx);
1437 y = wl_fixed_to_int(*fy);
1438
1439 if (x < output->x)
1440 *fx = wl_fixed_from_int(output->x);
1441 else if (x >= output->x + output->width)
1442 *fx = wl_fixed_from_int(output->x +
1443 output->width - 1);
1444 if (y < output->y)
1445 *fy = wl_fixed_from_int(output->y);
1446 else if (y >= output->y + output->height)
1447 *fy = wl_fixed_from_int(output->y +
1448 output->height - 1);
1449}
1450
Rob Bradford806d8c02013-06-25 18:56:41 +01001451WL_EXPORT void
1452weston_pointer_clamp(struct weston_pointer *pointer, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001453{
Rob Bradford806d8c02013-06-25 18:56:41 +01001454 struct weston_compositor *ec = pointer->seat->compositor;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001455 struct weston_output *output, *prev = NULL;
1456 int x, y, old_x, old_y, valid = 0;
1457
1458 x = wl_fixed_to_int(*fx);
1459 y = wl_fixed_to_int(*fy);
Rob Bradford806d8c02013-06-25 18:56:41 +01001460 old_x = wl_fixed_to_int(pointer->x);
1461 old_y = wl_fixed_to_int(pointer->y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001462
1463 wl_list_for_each(output, &ec->output_list, link) {
Rob Bradford66bd9f52013-06-25 18:56:42 +01001464 if (pointer->seat->output && pointer->seat->output != output)
1465 continue;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001466 if (pixman_region32_contains_point(&output->region,
1467 x, y, NULL))
1468 valid = 1;
1469 if (pixman_region32_contains_point(&output->region,
1470 old_x, old_y, NULL))
1471 prev = output;
1472 }
1473
Rob Bradford66bd9f52013-06-25 18:56:42 +01001474 if (!prev)
1475 prev = pointer->seat->output;
1476
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001477 if (prev && !valid)
1478 weston_pointer_clamp_for_output(pointer, prev, fx, fy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001479}
1480
Jonas Ådahld2510102014-10-05 21:39:14 +02001481static void
1482weston_pointer_move_to(struct weston_pointer *pointer,
1483 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001484{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001485 int32_t ix, iy;
1486
Rob Bradford806d8c02013-06-25 18:56:41 +01001487 weston_pointer_clamp (pointer, &x, &y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001488
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001489 pointer->x = x;
1490 pointer->y = y;
1491
1492 ix = wl_fixed_to_int(x);
1493 iy = wl_fixed_to_int(y);
1494
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001495 if (pointer->sprite) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001496 weston_view_set_position(pointer->sprite,
1497 ix - pointer->hotspot_x,
1498 iy - pointer->hotspot_y);
1499 weston_view_schedule_repaint(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001500 }
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001501
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001502 pointer->grab->interface->focus(pointer->grab);
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001503 wl_signal_emit(&pointer->motion_signal, pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001504}
1505
Jonas Ådahld2510102014-10-05 21:39:14 +02001506WL_EXPORT void
Jonas Ådahld2510102014-10-05 21:39:14 +02001507weston_pointer_move(struct weston_pointer *pointer,
1508 struct weston_pointer_motion_event *event)
1509{
1510 wl_fixed_t x, y;
1511
1512 weston_pointer_motion_to_abs(pointer, event, &x, &y);
1513 weston_pointer_move_to(pointer, x, y);
1514}
1515
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001516/** Verify if the pointer is in a valid position and move it if it isn't.
1517 */
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001518static void
1519weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001520{
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001521 struct weston_pointer *pointer;
1522 struct weston_compositor *ec;
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001523 struct weston_output *output, *closest = NULL;
1524 int x, y, distance, min = INT_MAX;
1525 wl_fixed_t fx, fy;
1526
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001527 pointer = container_of(listener, struct weston_pointer,
1528 output_destroy_listener);
1529 ec = pointer->seat->compositor;
1530
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001531 x = wl_fixed_to_int(pointer->x);
1532 y = wl_fixed_to_int(pointer->y);
1533
1534 wl_list_for_each(output, &ec->output_list, link) {
1535 if (pixman_region32_contains_point(&output->region,
1536 x, y, NULL))
1537 return;
1538
1539 /* Aproximante the distance from the pointer to the center of
1540 * the output. */
1541 distance = abs(output->x + output->width / 2 - x) +
1542 abs(output->y + output->height / 2 - y);
1543 if (distance < min) {
1544 min = distance;
1545 closest = output;
1546 }
1547 }
1548
1549 /* Nothing to do if there's no output left. */
1550 if (!closest)
1551 return;
1552
1553 fx = pointer->x;
1554 fy = pointer->y;
1555
1556 weston_pointer_clamp_for_output(pointer, closest, &fx, &fy);
Jonas Ådahld2510102014-10-05 21:39:14 +02001557 weston_pointer_move_to(pointer, fx, fy);
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001558}
1559
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001560WL_EXPORT void
1561notify_motion(struct weston_seat *seat,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001562 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001563 struct weston_pointer_motion_event *event)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001564{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001565 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001566 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001567
1568 weston_compositor_wake(ec);
Jonas Ådahld2510102014-10-05 21:39:14 +02001569 pointer->grab->interface->motion(pointer->grab, time, event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001570}
1571
Daniel Stone96d47c02013-11-19 11:37:12 +01001572static void
1573run_modifier_bindings(struct weston_seat *seat, uint32_t old, uint32_t new)
1574{
1575 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001576 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Daniel Stone96d47c02013-11-19 11:37:12 +01001577 uint32_t diff;
1578 unsigned int i;
1579 struct {
1580 uint32_t xkb;
1581 enum weston_keyboard_modifier weston;
1582 } mods[] = {
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001583 { keyboard->xkb_info->ctrl_mod, MODIFIER_CTRL },
1584 { keyboard->xkb_info->alt_mod, MODIFIER_ALT },
1585 { keyboard->xkb_info->super_mod, MODIFIER_SUPER },
1586 { keyboard->xkb_info->shift_mod, MODIFIER_SHIFT },
Daniel Stone96d47c02013-11-19 11:37:12 +01001587 };
1588
1589 diff = new & ~old;
1590 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
1591 if (diff & (1 << mods[i].xkb))
1592 weston_compositor_run_modifier_binding(compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001593 keyboard,
Daniel Stone96d47c02013-11-19 11:37:12 +01001594 mods[i].weston,
1595 WL_KEYBOARD_KEY_STATE_PRESSED);
1596 }
1597
1598 diff = old & ~new;
1599 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
1600 if (diff & (1 << mods[i].xkb))
1601 weston_compositor_run_modifier_binding(compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001602 keyboard,
Daniel Stone96d47c02013-11-19 11:37:12 +01001603 mods[i].weston,
1604 WL_KEYBOARD_KEY_STATE_RELEASED);
1605 }
1606}
1607
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001608WL_EXPORT void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001609notify_motion_absolute(struct weston_seat *seat, const struct timespec *time,
1610 double x, double y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001611{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001612 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001613 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Jonas Ådahld2510102014-10-05 21:39:14 +02001614 struct weston_pointer_motion_event event = { 0 };
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001615
1616 weston_compositor_wake(ec);
Jonas Ådahld2510102014-10-05 21:39:14 +02001617
1618 event = (struct weston_pointer_motion_event) {
1619 .mask = WESTON_POINTER_MOTION_ABS,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001620 .x = x,
1621 .y = y,
Jonas Ådahld2510102014-10-05 21:39:14 +02001622 };
1623
1624 pointer->grab->interface->motion(pointer->grab, time, &event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001625}
1626
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02001627static unsigned int
1628peek_next_activate_serial(struct weston_compositor *c)
1629{
1630 unsigned serial = c->activate_serial + 1;
1631
1632 return serial == 0 ? 1 : serial;
1633}
1634
1635static void
1636inc_activate_serial(struct weston_compositor *c)
1637{
1638 c->activate_serial = peek_next_activate_serial (c);
1639}
1640
1641WL_EXPORT void
1642weston_view_activate(struct weston_view *view,
1643 struct weston_seat *seat,
1644 uint32_t flags)
1645{
1646 struct weston_compositor *compositor = seat->compositor;
1647
1648 if (flags & WESTON_ACTIVATE_FLAG_CLICKED) {
1649 view->click_to_activate_serial =
1650 peek_next_activate_serial(compositor);
1651 }
1652
1653 weston_seat_set_keyboard_focus(seat, view->surface);
1654}
1655
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001656WL_EXPORT void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001657notify_button(struct weston_seat *seat, const struct timespec *time,
1658 int32_t button, enum wl_pointer_button_state state)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001659{
1660 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001661 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001662
1663 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001664 weston_compositor_idle_inhibit(compositor);
1665 if (pointer->button_count == 0) {
1666 pointer->grab_button = button;
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001667 pointer->grab_time = *time;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001668 pointer->grab_x = pointer->x;
1669 pointer->grab_y = pointer->y;
1670 }
1671 pointer->button_count++;
1672 } else {
1673 weston_compositor_idle_release(compositor);
1674 pointer->button_count--;
1675 }
1676
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001677 weston_compositor_run_button_binding(compositor, pointer, time, button,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001678 state);
1679
1680 pointer->grab->interface->button(pointer->grab, time, button, state);
1681
1682 if (pointer->button_count == 1)
1683 pointer->grab_serial =
1684 wl_display_get_serial(compositor->wl_display);
1685}
1686
1687WL_EXPORT void
Peter Hutterer89b6a492016-01-18 15:58:17 +10001688notify_axis(struct weston_seat *seat, uint32_t time,
1689 struct weston_pointer_axis_event *event)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001690{
1691 struct weston_compositor *compositor = 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
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001694 weston_compositor_wake(compositor);
1695
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001696 if (weston_compositor_run_axis_binding(compositor, pointer,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001697 time, event))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001698 return;
1699
Peter Hutterer89b6a492016-01-18 15:58:17 +10001700 pointer->grab->interface->axis(pointer->grab, time, event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001701}
1702
Peter Hutterer87743e92016-01-18 16:38:22 +10001703WL_EXPORT void
1704notify_axis_source(struct weston_seat *seat, uint32_t source)
1705{
1706 struct weston_compositor *compositor = seat->compositor;
1707 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1708
1709 weston_compositor_wake(compositor);
1710
1711 pointer->grab->interface->axis_source(pointer->grab, source);
1712}
1713
1714WL_EXPORT void
1715notify_pointer_frame(struct weston_seat *seat)
1716{
1717 struct weston_compositor *compositor = seat->compositor;
1718 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1719
1720 weston_compositor_wake(compositor);
1721
1722 pointer->grab->interface->frame(pointer->grab);
1723}
1724
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001725WL_EXPORT int
1726weston_keyboard_set_locks(struct weston_keyboard *keyboard,
1727 uint32_t mask, uint32_t value)
1728{
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001729 uint32_t serial;
1730 xkb_mod_mask_t mods_depressed, mods_latched, mods_locked, group;
1731 xkb_mod_mask_t num, caps;
1732
1733 /* We don't want the leds to go out of sync with the actual state
1734 * so if the backend has no way to change the leds don't try to
1735 * change the state */
1736 if (!keyboard->seat->led_update)
1737 return -1;
1738
1739 mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
1740 XKB_STATE_DEPRESSED);
1741 mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
1742 XKB_STATE_LATCHED);
1743 mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
1744 XKB_STATE_LOCKED);
1745 group = xkb_state_serialize_group(keyboard->xkb_state.state,
1746 XKB_STATE_EFFECTIVE);
1747
1748 num = (1 << keyboard->xkb_info->mod2_mod);
1749 caps = (1 << keyboard->xkb_info->caps_mod);
1750 if (mask & WESTON_NUM_LOCK) {
1751 if (value & WESTON_NUM_LOCK)
1752 mods_locked |= num;
1753 else
1754 mods_locked &= ~num;
1755 }
1756 if (mask & WESTON_CAPS_LOCK) {
1757 if (value & WESTON_CAPS_LOCK)
1758 mods_locked |= caps;
1759 else
1760 mods_locked &= ~caps;
1761 }
1762
1763 xkb_state_update_mask(keyboard->xkb_state.state, mods_depressed,
1764 mods_latched, mods_locked, 0, 0, group);
1765
1766 serial = wl_display_next_serial(
1767 keyboard->seat->compositor->wl_display);
1768 notify_modifiers(keyboard->seat, serial);
1769
1770 return 0;
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001771}
1772
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001773WL_EXPORT void
1774notify_modifiers(struct weston_seat *seat, uint32_t serial)
1775{
Derek Foreman1281a362015-07-31 16:55:32 -05001776 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001777 struct weston_keyboard_grab *grab = keyboard->grab;
1778 uint32_t mods_depressed, mods_latched, mods_locked, group;
1779 uint32_t mods_lookup;
1780 enum weston_led leds = 0;
1781 int changed = 0;
1782
1783 /* Serialize and update our internal state, checking to see if it's
1784 * different to the previous state. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001785 mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03001786 XKB_STATE_MODS_DEPRESSED);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001787 mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03001788 XKB_STATE_MODS_LATCHED);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001789 mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03001790 XKB_STATE_MODS_LOCKED);
1791 group = xkb_state_serialize_layout(keyboard->xkb_state.state,
1792 XKB_STATE_LAYOUT_EFFECTIVE);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001793
Derek Foreman244e99e2015-06-03 15:53:26 -05001794 if (mods_depressed != keyboard->modifiers.mods_depressed ||
1795 mods_latched != keyboard->modifiers.mods_latched ||
1796 mods_locked != keyboard->modifiers.mods_locked ||
1797 group != keyboard->modifiers.group)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001798 changed = 1;
1799
Derek Foreman244e99e2015-06-03 15:53:26 -05001800 run_modifier_bindings(seat, keyboard->modifiers.mods_depressed,
Daniel Stone96d47c02013-11-19 11:37:12 +01001801 mods_depressed);
1802
Derek Foreman244e99e2015-06-03 15:53:26 -05001803 keyboard->modifiers.mods_depressed = mods_depressed;
1804 keyboard->modifiers.mods_latched = mods_latched;
1805 keyboard->modifiers.mods_locked = mods_locked;
1806 keyboard->modifiers.group = group;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001807
1808 /* And update the modifier_state for bindings. */
1809 mods_lookup = mods_depressed | mods_latched;
1810 seat->modifier_state = 0;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001811 if (mods_lookup & (1 << keyboard->xkb_info->ctrl_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001812 seat->modifier_state |= MODIFIER_CTRL;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001813 if (mods_lookup & (1 << keyboard->xkb_info->alt_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001814 seat->modifier_state |= MODIFIER_ALT;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001815 if (mods_lookup & (1 << keyboard->xkb_info->super_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001816 seat->modifier_state |= MODIFIER_SUPER;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001817 if (mods_lookup & (1 << keyboard->xkb_info->shift_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001818 seat->modifier_state |= MODIFIER_SHIFT;
1819
1820 /* Finally, notify the compositor that LEDs have changed. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001821 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
1822 keyboard->xkb_info->num_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001823 leds |= LED_NUM_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001824 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
1825 keyboard->xkb_info->caps_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001826 leds |= LED_CAPS_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001827 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
1828 keyboard->xkb_info->scroll_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001829 leds |= LED_SCROLL_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001830 if (leds != keyboard->xkb_state.leds && seat->led_update)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001831 seat->led_update(seat, leds);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001832 keyboard->xkb_state.leds = leds;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001833
1834 if (changed) {
1835 grab->interface->modifiers(grab,
1836 serial,
1837 keyboard->modifiers.mods_depressed,
1838 keyboard->modifiers.mods_latched,
1839 keyboard->modifiers.mods_locked,
1840 keyboard->modifiers.group);
1841 }
1842}
1843
1844static void
1845update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
1846 enum wl_keyboard_key_state state)
1847{
Derek Foreman1281a362015-07-31 16:55:32 -05001848 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001849 enum xkb_key_direction direction;
1850
1851 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1852 direction = XKB_KEY_DOWN;
1853 else
1854 direction = XKB_KEY_UP;
1855
1856 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
1857 * broken keycode system, which starts at 8. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001858 xkb_state_update_key(keyboard->xkb_state.state, key + 8, direction);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001859
1860 notify_modifiers(seat, serial);
1861}
Rui Matos65196bc2013-10-10 19:44:19 +02001862
1863static void
1864send_keymap(struct wl_resource *resource, struct weston_xkb_info *xkb_info)
1865{
1866 wl_keyboard_send_keymap(resource,
1867 WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
1868 xkb_info->keymap_fd,
1869 xkb_info->keymap_size);
1870}
1871
1872static void
1873send_modifiers(struct wl_resource *resource, uint32_t serial, struct weston_keyboard *keyboard)
1874{
1875 wl_keyboard_send_modifiers(resource, serial,
1876 keyboard->modifiers.mods_depressed,
1877 keyboard->modifiers.mods_latched,
1878 keyboard->modifiers.mods_locked,
1879 keyboard->modifiers.group);
1880}
1881
1882static struct weston_xkb_info *
1883weston_xkb_info_create(struct xkb_keymap *keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02001884
1885static void
1886update_keymap(struct weston_seat *seat)
1887{
Derek Foreman1281a362015-07-31 16:55:32 -05001888 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Rui Matos65196bc2013-10-10 19:44:19 +02001889 struct wl_resource *resource;
1890 struct weston_xkb_info *xkb_info;
1891 struct xkb_state *state;
1892 xkb_mod_mask_t latched_mods;
1893 xkb_mod_mask_t locked_mods;
1894
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001895 xkb_info = weston_xkb_info_create(keyboard->pending_keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02001896
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001897 xkb_keymap_unref(keyboard->pending_keymap);
1898 keyboard->pending_keymap = NULL;
Rui Matos65196bc2013-10-10 19:44:19 +02001899
1900 if (!xkb_info) {
1901 weston_log("failed to create XKB info\n");
1902 return;
1903 }
1904
1905 state = xkb_state_new(xkb_info->keymap);
1906 if (!state) {
1907 weston_log("failed to initialise XKB state\n");
1908 weston_xkb_info_destroy(xkb_info);
1909 return;
1910 }
1911
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001912 latched_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
1913 XKB_STATE_MODS_LATCHED);
1914 locked_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
1915 XKB_STATE_MODS_LOCKED);
Rui Matos65196bc2013-10-10 19:44:19 +02001916 xkb_state_update_mask(state,
1917 0, /* depressed */
1918 latched_mods,
1919 locked_mods,
1920 0, 0, 0);
1921
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001922 weston_xkb_info_destroy(keyboard->xkb_info);
1923 keyboard->xkb_info = xkb_info;
Rui Matos65196bc2013-10-10 19:44:19 +02001924
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001925 xkb_state_unref(keyboard->xkb_state.state);
1926 keyboard->xkb_state.state = state;
Rui Matos65196bc2013-10-10 19:44:19 +02001927
Derek Foremanbc91e542015-06-03 15:53:27 -05001928 wl_resource_for_each(resource, &keyboard->resource_list)
Rui Matos65196bc2013-10-10 19:44:19 +02001929 send_keymap(resource, xkb_info);
Derek Foremanbc91e542015-06-03 15:53:27 -05001930 wl_resource_for_each(resource, &keyboard->focus_resource_list)
Rui Matos65196bc2013-10-10 19:44:19 +02001931 send_keymap(resource, xkb_info);
1932
1933 notify_modifiers(seat, wl_display_next_serial(seat->compositor->wl_display));
1934
1935 if (!latched_mods && !locked_mods)
1936 return;
1937
Derek Foremanbc91e542015-06-03 15:53:27 -05001938 wl_resource_for_each(resource, &keyboard->resource_list)
1939 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), keyboard);
1940 wl_resource_for_each(resource, &keyboard->focus_resource_list)
1941 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), keyboard);
Rui Matos65196bc2013-10-10 19:44:19 +02001942}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001943
1944WL_EXPORT void
1945notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
1946 enum wl_keyboard_key_state state,
1947 enum weston_key_state_update update_state)
1948{
1949 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001950 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001951 struct weston_keyboard_grab *grab = keyboard->grab;
Pekka Paalanen86b53962014-11-19 13:43:32 +02001952 uint32_t *k, *end;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001953
1954 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001955 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001956 } else {
1957 weston_compositor_idle_release(compositor);
1958 }
1959
Pekka Paalanen86b53962014-11-19 13:43:32 +02001960 end = keyboard->keys.data + keyboard->keys.size;
1961 for (k = keyboard->keys.data; k < end; k++) {
1962 if (*k == key) {
1963 /* Ignore server-generated repeats. */
1964 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1965 return;
1966 *k = *--end;
1967 }
1968 }
1969 keyboard->keys.size = (void *) end - keyboard->keys.data;
1970 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1971 k = wl_array_add(&keyboard->keys, sizeof *k);
1972 *k = key;
1973 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001974
1975 if (grab == &keyboard->default_grab ||
1976 grab == &keyboard->input_method_grab) {
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001977 weston_compositor_run_key_binding(compositor, keyboard, time,
1978 key, state);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001979 grab = keyboard->grab;
1980 }
1981
1982 grab->interface->key(grab, time, key, state);
1983
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001984 if (keyboard->pending_keymap &&
Pekka Paalanen86b53962014-11-19 13:43:32 +02001985 keyboard->keys.size == 0)
Rui Matos65196bc2013-10-10 19:44:19 +02001986 update_keymap(seat);
1987
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001988 if (update_state == STATE_UPDATE_AUTOMATIC) {
1989 update_modifier_state(seat,
1990 wl_display_get_serial(compositor->wl_display),
1991 key,
1992 state);
1993 }
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02001994
Olivier Fourdandf84dbe2016-06-30 16:01:56 +02001995 keyboard->grab_serial = wl_display_get_serial(compositor->wl_display);
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02001996 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02001997 keyboard->grab_time = time;
1998 keyboard->grab_key = key;
1999 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002000}
2001
2002WL_EXPORT void
2003notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002004 double x, double y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002005{
Derek Foreman1281a362015-07-31 16:55:32 -05002006 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2007
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002008 if (output) {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002009 weston_pointer_move_to(pointer,
2010 wl_fixed_from_double(x),
2011 wl_fixed_from_double(y));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002012 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002013 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002014 * NULL) here, but somehow that breaks re-entry... */
2015 }
2016}
2017
2018static void
2019destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
2020{
2021 struct weston_seat *ws;
2022
2023 ws = container_of(listener, struct weston_seat,
2024 saved_kbd_focus_listener);
2025
2026 ws->saved_kbd_focus = NULL;
2027}
2028
2029WL_EXPORT void
2030notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
2031 enum weston_key_state_update update_state)
2032{
2033 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002034 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04002035 struct weston_surface *surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002036 uint32_t *k, serial;
2037
2038 serial = wl_display_next_serial(compositor->wl_display);
2039 wl_array_copy(&keyboard->keys, keys);
2040 wl_array_for_each(k, &keyboard->keys) {
2041 weston_compositor_idle_inhibit(compositor);
2042 if (update_state == STATE_UPDATE_AUTOMATIC)
2043 update_modifier_state(seat, serial, *k,
2044 WL_KEYBOARD_KEY_STATE_PRESSED);
2045 }
2046
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002047 surface = seat->saved_kbd_focus;
2048
2049 if (surface) {
2050 wl_list_remove(&seat->saved_kbd_focus_listener.link);
2051 weston_keyboard_set_focus(keyboard, surface);
2052 seat->saved_kbd_focus = NULL;
2053 }
2054}
2055
2056WL_EXPORT void
2057notify_keyboard_focus_out(struct weston_seat *seat)
2058{
2059 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002060 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2061 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002062 uint32_t *k, serial;
2063
2064 serial = wl_display_next_serial(compositor->wl_display);
2065 wl_array_for_each(k, &keyboard->keys) {
2066 weston_compositor_idle_release(compositor);
2067 update_modifier_state(seat, serial, *k,
2068 WL_KEYBOARD_KEY_STATE_RELEASED);
2069 }
2070
2071 seat->modifier_state = 0;
2072
2073 if (keyboard->focus) {
2074 seat->saved_kbd_focus = keyboard->focus;
2075 seat->saved_kbd_focus_listener.notify =
2076 destroy_device_saved_kbd_focus;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002077 wl_signal_add(&keyboard->focus->destroy_signal,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002078 &seat->saved_kbd_focus_listener);
2079 }
2080
2081 weston_keyboard_set_focus(keyboard, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002082 weston_keyboard_cancel_grab(keyboard);
Derek Foreman1281a362015-07-31 16:55:32 -05002083 if (pointer)
2084 weston_pointer_cancel_grab(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002085}
2086
Michael Fua2bb7912013-07-23 15:51:06 +08002087WL_EXPORT void
Derek Foreman4c93c082015-04-30 16:45:41 -05002088weston_touch_set_focus(struct weston_touch *touch, struct weston_view *view)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002089{
Neil Roberts96d790e2013-09-19 17:32:00 +01002090 struct wl_list *focus_resource_list;
2091
Derek Foreman4c93c082015-04-30 16:45:41 -05002092 focus_resource_list = &touch->focus_resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002093
Derek Foreman4c93c082015-04-30 16:45:41 -05002094 if (view && touch->focus &&
2095 touch->focus->surface == view->surface) {
2096 touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002097 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002098 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002099
Derek Foreman4c93c082015-04-30 16:45:41 -05002100 wl_list_remove(&touch->focus_resource_listener.link);
2101 wl_list_init(&touch->focus_resource_listener.link);
2102 wl_list_remove(&touch->focus_view_listener.link);
2103 wl_list_init(&touch->focus_view_listener.link);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01002104
Neil Roberts96d790e2013-09-19 17:32:00 +01002105 if (!wl_list_empty(focus_resource_list)) {
Derek Foreman4c93c082015-04-30 16:45:41 -05002106 move_resources(&touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002107 focus_resource_list);
2108 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002109
Jason Ekstranda7af7042013-10-12 22:38:11 -05002110 if (view) {
Derek Foreman362656b2014-09-04 10:23:05 -05002111 struct wl_client *surface_client;
2112
2113 if (!view->surface->resource) {
Derek Foreman4c93c082015-04-30 16:45:41 -05002114 touch->focus = NULL;
Derek Foreman362656b2014-09-04 10:23:05 -05002115 return;
2116 }
2117
2118 surface_client = wl_resource_get_client(view->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +01002119 move_resources_for_client(focus_resource_list,
Derek Foreman4c93c082015-04-30 16:45:41 -05002120 &touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002121 surface_client);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01002122 wl_resource_add_destroy_listener(view->surface->resource,
Derek Foreman4c93c082015-04-30 16:45:41 -05002123 &touch->focus_resource_listener);
2124 wl_signal_add(&view->destroy_signal, &touch->focus_view_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002125 }
Derek Foreman4c93c082015-04-30 16:45:41 -05002126 touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002127}
2128
2129/**
2130 * notify_touch - emulates button touches and notifies surfaces accordingly.
2131 *
2132 * It assumes always the correct cycle sequence until it gets here: touch_down
2133 * → touch_update → ... → touch_update → touch_end. The driver is responsible
2134 * for sending along such order.
2135 *
2136 */
2137WL_EXPORT void
2138notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002139 double double_x, double double_y, int touch_type)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002140{
2141 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002142 struct weston_touch *touch = weston_seat_get_touch(seat);
Kristian Høgsberge329f362013-05-06 22:19:57 -04002143 struct weston_touch_grab *grab = touch->grab;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002144 struct weston_view *ev;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002145 wl_fixed_t sx, sy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002146 wl_fixed_t x = wl_fixed_from_double(double_x);
2147 wl_fixed_t y = wl_fixed_from_double(double_y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002148
2149 /* Update grab's global coordinates. */
Neil Roberts306fe082013-10-03 16:43:06 +01002150 if (touch_id == touch->grab_touch_id && touch_type != WL_TOUCH_UP) {
2151 touch->grab_x = x;
2152 touch->grab_y = y;
2153 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002154
2155 switch (touch_type) {
2156 case WL_TOUCH_DOWN:
2157 weston_compositor_idle_inhibit(ec);
2158
Jonas Ådahl9484b692013-12-02 22:05:03 +01002159 touch->num_tp++;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002160
Jason Ekstranda7af7042013-10-12 22:38:11 -05002161 /* the first finger down picks the view, and all further go
2162 * to that view for the remainder of the touch session i.e.
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002163 * until all touch points are up again. */
Jonas Ådahl9484b692013-12-02 22:05:03 +01002164 if (touch->num_tp == 1) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002165 ev = weston_compositor_pick_view(ec, x, y, &sx, &sy);
Derek Foreman4c93c082015-04-30 16:45:41 -05002166 weston_touch_set_focus(touch, ev);
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002167 } else if (!touch->focus) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002168 /* Unexpected condition: We have non-initial touch but
2169 * there is no focused surface.
2170 */
Chris Michael3f607d32015-10-07 11:59:49 -04002171 weston_log("touch event received with %d points down "
Jonas Ådahl9484b692013-12-02 22:05:03 +01002172 "but no surface focused\n", touch->num_tp);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002173 return;
2174 }
2175
Derek Foreman99a6a2d2015-07-15 13:00:43 -05002176 weston_compositor_run_touch_binding(ec, touch,
Kristian Høgsbergc8964012014-02-05 14:25:18 -08002177 time, touch_type);
2178
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002179 grab->interface->down(grab, time, touch_id, x, y);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002180 if (touch->num_tp == 1) {
Rusty Lynchf1407ff2013-08-08 21:13:57 -07002181 touch->grab_serial =
2182 wl_display_get_serial(ec->wl_display);
Neil Roberts306fe082013-10-03 16:43:06 +01002183 touch->grab_touch_id = touch_id;
Rusty Lynchf1407ff2013-08-08 21:13:57 -07002184 touch->grab_time = time;
2185 touch->grab_x = x;
2186 touch->grab_y = y;
2187 }
2188
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002189 break;
2190 case WL_TOUCH_MOTION:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002191 ev = touch->focus;
2192 if (!ev)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002193 break;
2194
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002195 grab->interface->motion(grab, time, touch_id, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002196 break;
2197 case WL_TOUCH_UP:
Kristian Høgsberga30e29a2014-01-08 22:29:20 -08002198 if (touch->num_tp == 0) {
2199 /* This can happen if we start out with one or
2200 * more fingers on the touch screen, in which
2201 * case we didn't get the corresponding down
2202 * event. */
2203 weston_log("unmatched touch up event\n");
2204 break;
2205 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002206 weston_compositor_idle_release(ec);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002207 touch->num_tp--;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002208
2209 grab->interface->up(grab, time, touch_id);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002210 if (touch->num_tp == 0)
Derek Foreman4c93c082015-04-30 16:45:41 -05002211 weston_touch_set_focus(touch, NULL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002212 break;
2213 }
2214}
2215
Jonas Ådahl1679f232014-04-12 09:39:51 +02002216WL_EXPORT void
2217notify_touch_frame(struct weston_seat *seat)
2218{
Derek Foreman1281a362015-07-31 16:55:32 -05002219 struct weston_touch *touch = weston_seat_get_touch(seat);
Jonas Ådahl1679f232014-04-12 09:39:51 +02002220 struct weston_touch_grab *grab = touch->grab;
2221
2222 grab->interface->frame(grab);
2223}
2224
Derek Foreman3cc004a2015-11-06 15:56:09 -06002225WL_EXPORT void
2226notify_touch_cancel(struct weston_seat *seat)
2227{
2228 struct weston_touch *touch = weston_seat_get_touch(seat);
2229 struct weston_touch_grab *grab = touch->grab;
2230
2231 grab->interface->cancel(grab);
2232}
2233
Pekka Paalanen8274d902014-08-06 19:36:51 +03002234static int
2235pointer_cursor_surface_get_label(struct weston_surface *surface,
2236 char *buf, size_t len)
2237{
2238 return snprintf(buf, len, "cursor");
2239}
2240
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002241static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002242pointer_cursor_surface_committed(struct weston_surface *es,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002243 int32_t dx, int32_t dy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002244{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002245 struct weston_pointer *pointer = es->committed_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002246 int x, y;
2247
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002248 if (es->width == 0)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002249 return;
2250
Jason Ekstranda7af7042013-10-12 22:38:11 -05002251 assert(es == pointer->sprite->surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002252
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002253 pointer->hotspot_x -= dx;
2254 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002255
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002256 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
2257 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002258
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002259 weston_view_set_position(pointer->sprite, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002260
2261 empty_region(&es->pending.input);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02002262 empty_region(&es->input);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002263
2264 if (!weston_surface_is_mapped(es)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002265 weston_layer_entry_insert(&es->compositor->cursor_layer.view_list,
2266 &pointer->sprite->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002267 weston_view_update_transform(pointer->sprite);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002268 es->is_mapped = true;
2269 pointer->sprite->is_mapped = true;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002270 }
2271}
2272
2273static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002274pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2275 uint32_t serial, struct wl_resource *surface_resource,
2276 int32_t x, int32_t y)
2277{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002278 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002279 struct weston_surface *surface = NULL;
2280
2281 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002282 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002283
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002284 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002285 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002286 /* pointer->focus->surface->resource can be NULL. Surfaces like the
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02002287 black_surface used in shell.c for fullscreen don't have
2288 a resource, but can still have focus */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002289 if (pointer->focus->surface->resource == NULL)
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02002290 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002291 if (wl_resource_get_client(pointer->focus->surface->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002292 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002293 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002294 return;
2295
Derek Foreman4e53c532015-03-23 10:55:32 -05002296 if (!surface) {
2297 if (pointer->sprite)
2298 pointer_unmap_sprite(pointer);
2299 return;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002300 }
2301
Jonas Ådahlb4070242015-03-18 15:08:03 +08002302 if (pointer->sprite && pointer->sprite->surface == surface &&
2303 pointer->hotspot_x == x && pointer->hotspot_y == y)
2304 return;
2305
Derek Foreman4e53c532015-03-23 10:55:32 -05002306 if (!pointer->sprite || pointer->sprite->surface != surface) {
2307 if (weston_surface_set_role(surface, "wl_pointer-cursor",
2308 resource,
2309 WL_POINTER_ERROR_ROLE) < 0)
2310 return;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002311
Derek Foreman4e53c532015-03-23 10:55:32 -05002312 if (pointer->sprite)
2313 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002314
Derek Foreman4e53c532015-03-23 10:55:32 -05002315 wl_signal_add(&surface->destroy_signal,
2316 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002317
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002318 surface->committed = pointer_cursor_surface_committed;
2319 surface->committed_private = pointer;
Derek Foreman4e53c532015-03-23 10:55:32 -05002320 weston_surface_set_label_func(surface,
2321 pointer_cursor_surface_get_label);
2322 pointer->sprite = weston_view_create(surface);
2323 }
2324
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002325 pointer->hotspot_x = x;
2326 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002327
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +02002328 if (surface->buffer_ref.buffer) {
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002329 pointer_cursor_surface_committed(surface, 0, 0);
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +02002330 weston_view_schedule_repaint(pointer->sprite);
2331 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002332}
2333
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002334static void
2335pointer_release(struct wl_client *client, struct wl_resource *resource)
2336{
2337 wl_resource_destroy(resource);
2338}
2339
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002340static const struct wl_pointer_interface pointer_interface = {
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002341 pointer_set_cursor,
2342 pointer_release
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002343};
2344
2345static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002346seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2347 uint32_t id)
2348{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002349 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002350 /* We use the pointer_state directly, which means we'll
2351 * give a wl_pointer if the seat has ever had one - even though
2352 * the spec explicitly states that this request only takes effect
2353 * if the seat has the pointer capability.
2354 *
2355 * This prevents a race between the compositor sending new
2356 * capabilities and the client trying to use the old ones.
2357 */
2358 struct weston_pointer *pointer = seat->pointer_state;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002359 struct wl_resource *cr;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002360 struct weston_pointer_client *pointer_client;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002361
Derek Foreman1281a362015-07-31 16:55:32 -05002362 if (!pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002363 return;
2364
Jason Ekstranda85118c2013-06-27 20:17:02 -05002365 cr = wl_resource_create(client, &wl_pointer_interface,
2366 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002367 if (cr == NULL) {
2368 wl_client_post_no_memory(client);
2369 return;
2370 }
2371
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002372 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
2373 if (!pointer_client) {
2374 wl_client_post_no_memory(client);
2375 return;
2376 }
2377
2378 wl_list_insert(&pointer_client->pointer_resources,
2379 wl_resource_get_link(cr));
Derek Foreman1281a362015-07-31 16:55:32 -05002380 wl_resource_set_implementation(cr, &pointer_interface, pointer,
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002381 unbind_pointer_client_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002382
Derek Foreman1281a362015-07-31 16:55:32 -05002383 if (pointer->focus && pointer->focus->surface->resource &&
2384 wl_resource_get_client(pointer->focus->surface->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002385 wl_fixed_t sx, sy;
2386
Derek Foreman1281a362015-07-31 16:55:32 -05002387 weston_view_from_global_fixed(pointer->focus,
2388 pointer->x,
2389 pointer->y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002390 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002391
Neil Roberts96d790e2013-09-19 17:32:00 +01002392 wl_pointer_send_enter(cr,
Derek Foreman1281a362015-07-31 16:55:32 -05002393 pointer->focus_serial,
2394 pointer->focus->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01002395 sx, sy);
Peter Hutterer87743e92016-01-18 16:38:22 +10002396 pointer_send_frame(cr);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002397 }
2398}
2399
2400static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002401keyboard_release(struct wl_client *client, struct wl_resource *resource)
2402{
2403 wl_resource_destroy(resource);
2404}
2405
2406static const struct wl_keyboard_interface keyboard_interface = {
2407 keyboard_release
2408};
2409
Derek Foreman280e7dd2014-10-03 13:13:42 -05002410static bool
Neil Roberts96d790e2013-09-19 17:32:00 +01002411should_send_modifiers_to_client(struct weston_seat *seat,
2412 struct wl_client *client)
2413{
Derek Foreman1281a362015-07-31 16:55:32 -05002414 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2415 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2416
2417 if (keyboard &&
2418 keyboard->focus &&
2419 keyboard->focus->resource &&
2420 wl_resource_get_client(keyboard->focus->resource) == client)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002421 return true;
Neil Roberts96d790e2013-09-19 17:32:00 +01002422
Derek Foreman1281a362015-07-31 16:55:32 -05002423 if (pointer &&
2424 pointer->focus &&
2425 pointer->focus->surface->resource &&
2426 wl_resource_get_client(pointer->focus->surface->resource) == client)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002427 return true;
Neil Roberts96d790e2013-09-19 17:32:00 +01002428
Derek Foreman280e7dd2014-10-03 13:13:42 -05002429 return false;
Neil Roberts96d790e2013-09-19 17:32:00 +01002430}
2431
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002432static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002433seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2434 uint32_t id)
2435{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002436 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002437 /* We use the keyboard_state directly, which means we'll
2438 * give a wl_keyboard if the seat has ever had one - even though
2439 * the spec explicitly states that this request only takes effect
2440 * if the seat has the keyboard capability.
2441 *
2442 * This prevents a race between the compositor sending new
2443 * capabilities and the client trying to use the old ones.
2444 */
2445 struct weston_keyboard *keyboard = seat->keyboard_state;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002446 struct wl_resource *cr;
2447
Derek Foreman345c9f32015-06-03 15:53:28 -05002448 if (!keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002449 return;
2450
Jason Ekstranda85118c2013-06-27 20:17:02 -05002451 cr = wl_resource_create(client, &wl_keyboard_interface,
2452 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002453 if (cr == NULL) {
2454 wl_client_post_no_memory(client);
2455 return;
2456 }
2457
Neil Roberts96d790e2013-09-19 17:32:00 +01002458 /* May be moved to focused list later by either
2459 * weston_keyboard_set_focus or directly if this client is already
2460 * focused */
Derek Foreman345c9f32015-06-03 15:53:28 -05002461 wl_list_insert(&keyboard->resource_list, wl_resource_get_link(cr));
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002462 wl_resource_set_implementation(cr, &keyboard_interface,
2463 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002464
Jonny Lamb66a41a02014-08-12 14:58:25 +02002465 if (wl_resource_get_version(cr) >= WL_KEYBOARD_REPEAT_INFO_SINCE_VERSION) {
2466 wl_keyboard_send_repeat_info(cr,
2467 seat->compositor->kb_repeat_rate,
2468 seat->compositor->kb_repeat_delay);
2469 }
Jasper St. Pierred8c6aeb2014-08-04 13:43:24 -04002470
Derek Foreman185d1582017-06-28 11:17:23 -05002471 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2472 keyboard->xkb_info->keymap_fd,
2473 keyboard->xkb_info->keymap_size);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002474
Neil Roberts96d790e2013-09-19 17:32:00 +01002475 if (should_send_modifiers_to_client(seat, client)) {
Derek Foreman345c9f32015-06-03 15:53:28 -05002476 send_modifiers_to_resource(keyboard,
Neil Roberts96d790e2013-09-19 17:32:00 +01002477 cr,
Derek Foreman345c9f32015-06-03 15:53:28 -05002478 keyboard->focus_serial);
Neil Roberts96d790e2013-09-19 17:32:00 +01002479 }
2480
Derek Foreman345c9f32015-06-03 15:53:28 -05002481 if (keyboard->focus && keyboard->focus->resource &&
2482 wl_resource_get_client(keyboard->focus->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01002483 struct weston_surface *surface =
Derek Foreman345c9f32015-06-03 15:53:28 -05002484 (struct weston_surface *)keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +01002485
2486 wl_list_remove(wl_resource_get_link(cr));
Derek Foreman345c9f32015-06-03 15:53:28 -05002487 wl_list_insert(&keyboard->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002488 wl_resource_get_link(cr));
2489 wl_keyboard_send_enter(cr,
Derek Foreman345c9f32015-06-03 15:53:28 -05002490 keyboard->focus_serial,
Neil Roberts96d790e2013-09-19 17:32:00 +01002491 surface->resource,
Derek Foreman345c9f32015-06-03 15:53:28 -05002492 &keyboard->keys);
Neil Roberts96d790e2013-09-19 17:32:00 +01002493
2494 /* If this is the first keyboard resource for this
2495 * client... */
Derek Foreman345c9f32015-06-03 15:53:28 -05002496 if (keyboard->focus_resource_list.prev ==
Neil Roberts96d790e2013-09-19 17:32:00 +01002497 wl_resource_get_link(cr))
2498 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002499 }
2500}
2501
2502static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002503touch_release(struct wl_client *client, struct wl_resource *resource)
2504{
2505 wl_resource_destroy(resource);
2506}
2507
2508static const struct wl_touch_interface touch_interface = {
2509 touch_release
2510};
2511
2512static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002513seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2514 uint32_t id)
2515{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002516 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002517 /* We use the touch_state directly, which means we'll
2518 * give a wl_touch if the seat has ever had one - even though
2519 * the spec explicitly states that this request only takes effect
2520 * if the seat has the touch capability.
2521 *
2522 * This prevents a race between the compositor sending new
2523 * capabilities and the client trying to use the old ones.
2524 */
2525 struct weston_touch *touch = seat->touch_state;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002526 struct wl_resource *cr;
2527
Derek Foreman1281a362015-07-31 16:55:32 -05002528 if (!touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002529 return;
2530
Jason Ekstranda85118c2013-06-27 20:17:02 -05002531 cr = wl_resource_create(client, &wl_touch_interface,
2532 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002533 if (cr == NULL) {
2534 wl_client_post_no_memory(client);
2535 return;
2536 }
2537
Derek Foreman1281a362015-07-31 16:55:32 -05002538 if (touch->focus &&
2539 wl_resource_get_client(touch->focus->surface->resource) == client) {
Chokshi, Mituld6697142015-10-09 08:28:47 +00002540 wl_list_insert(&touch->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002541 wl_resource_get_link(cr));
2542 } else {
Chokshi, Mituld6697142015-10-09 08:28:47 +00002543 wl_list_insert(&touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002544 wl_resource_get_link(cr));
2545 }
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002546 wl_resource_set_implementation(cr, &touch_interface,
2547 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002548}
2549
Quentin Glidicaab1d362016-03-13 17:49:08 +01002550static void
2551seat_release(struct wl_client *client, struct wl_resource *resource)
2552{
2553 wl_resource_destroy(resource);
2554}
2555
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002556static const struct wl_seat_interface seat_interface = {
2557 seat_get_pointer,
2558 seat_get_keyboard,
2559 seat_get_touch,
Quentin Glidicaab1d362016-03-13 17:49:08 +01002560 seat_release,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002561};
2562
2563static void
2564bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2565{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002566 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002567 struct wl_resource *resource;
2568 enum wl_seat_capability caps = 0;
2569
Jason Ekstranda85118c2013-06-27 20:17:02 -05002570 resource = wl_resource_create(client,
Derek Foreman1909c102015-11-26 14:17:47 -06002571 &wl_seat_interface, version, id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05002572 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05002573 wl_resource_set_implementation(resource, &seat_interface, data,
2574 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002575
Derek Foreman1281a362015-07-31 16:55:32 -05002576 if (weston_seat_get_pointer(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002577 caps |= WL_SEAT_CAPABILITY_POINTER;
Derek Foreman1281a362015-07-31 16:55:32 -05002578 if (weston_seat_get_keyboard(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002579 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Derek Foreman1281a362015-07-31 16:55:32 -05002580 if (weston_seat_get_touch(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002581 caps |= WL_SEAT_CAPABILITY_TOUCH;
2582
2583 wl_seat_send_capabilities(resource, caps);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04002584 if (version >= WL_SEAT_NAME_SINCE_VERSION)
Rob Bradforde445ae62013-05-31 18:09:51 +01002585 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002586}
2587
Jonas Ådahl30d61d82014-10-22 21:21:17 +02002588static void
2589relative_pointer_destroy(struct wl_client *client,
2590 struct wl_resource *resource)
2591{
2592 wl_resource_destroy(resource);
2593}
2594
2595static const struct zwp_relative_pointer_v1_interface relative_pointer_interface = {
2596 relative_pointer_destroy
2597};
2598
2599static void
2600relative_pointer_manager_destroy(struct wl_client *client,
2601 struct wl_resource *resource)
2602{
2603 wl_resource_destroy(resource);
2604}
2605
2606static void
2607relative_pointer_manager_get_relative_pointer(struct wl_client *client,
2608 struct wl_resource *resource,
2609 uint32_t id,
2610 struct wl_resource *pointer_resource)
2611{
2612 struct weston_pointer *pointer =
2613 wl_resource_get_user_data(pointer_resource);
2614 struct weston_pointer_client *pointer_client;
2615 struct wl_resource *cr;
2616
2617 cr = wl_resource_create(client, &zwp_relative_pointer_v1_interface,
2618 wl_resource_get_version(resource), id);
2619 if (cr == NULL) {
2620 wl_client_post_no_memory(client);
2621 return;
2622 }
2623
2624 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
2625 if (!pointer_client) {
2626 wl_client_post_no_memory(client);
2627 return;
2628 }
2629
2630 wl_list_insert(&pointer_client->relative_pointer_resources,
2631 wl_resource_get_link(cr));
2632 wl_resource_set_implementation(cr, &relative_pointer_interface,
2633 pointer,
2634 unbind_pointer_client_resource);
2635}
2636
2637static const struct zwp_relative_pointer_manager_v1_interface relative_pointer_manager = {
2638 relative_pointer_manager_destroy,
2639 relative_pointer_manager_get_relative_pointer,
2640};
2641
2642static void
2643bind_relative_pointer_manager(struct wl_client *client, void *data,
2644 uint32_t version, uint32_t id)
2645{
2646 struct weston_compositor *compositor = data;
2647 struct wl_resource *resource;
2648
2649 resource = wl_resource_create(client,
2650 &zwp_relative_pointer_manager_v1_interface,
2651 1, id);
2652
2653 wl_resource_set_implementation(resource, &relative_pointer_manager,
2654 compositor,
2655 NULL);
2656}
2657
Giulio Camuffo0358af42016-06-02 21:48:08 +03002658WL_EXPORT int
2659weston_compositor_set_xkb_rule_names(struct weston_compositor *ec,
2660 struct xkb_rule_names *names)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002661{
2662 if (ec->xkb_context == NULL) {
2663 ec->xkb_context = xkb_context_new(0);
2664 if (ec->xkb_context == NULL) {
2665 weston_log("failed to create XKB context\n");
2666 return -1;
2667 }
2668 }
2669
2670 if (names)
2671 ec->xkb_names = *names;
2672 if (!ec->xkb_names.rules)
2673 ec->xkb_names.rules = strdup("evdev");
2674 if (!ec->xkb_names.model)
2675 ec->xkb_names.model = strdup("pc105");
2676 if (!ec->xkb_names.layout)
2677 ec->xkb_names.layout = strdup("us");
2678
2679 return 0;
2680}
2681
Stefan Schmidtfda26522013-09-17 10:54:09 +01002682static void
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002683weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002684{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002685 if (--xkb_info->ref_count > 0)
2686 return;
2687
Ran Benitac9c74152014-08-19 23:59:52 +03002688 xkb_keymap_unref(xkb_info->keymap);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002689
2690 if (xkb_info->keymap_area)
2691 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2692 if (xkb_info->keymap_fd >= 0)
2693 close(xkb_info->keymap_fd);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002694 free(xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002695}
2696
2697void
2698weston_compositor_xkb_destroy(struct weston_compositor *ec)
2699{
2700 free((char *) ec->xkb_names.rules);
2701 free((char *) ec->xkb_names.model);
2702 free((char *) ec->xkb_names.layout);
2703 free((char *) ec->xkb_names.variant);
2704 free((char *) ec->xkb_names.options);
Stefan Schmidtfda26522013-09-17 10:54:09 +01002705
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002706 if (ec->xkb_info)
2707 weston_xkb_info_destroy(ec->xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002708 xkb_context_unref(ec->xkb_context);
2709}
2710
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002711static struct weston_xkb_info *
2712weston_xkb_info_create(struct xkb_keymap *keymap)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002713{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002714 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
2715 if (xkb_info == NULL)
2716 return NULL;
2717
Ran Benita2e1968f2014-08-19 23:59:51 +03002718 xkb_info->keymap = xkb_keymap_ref(keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002719 xkb_info->ref_count = 1;
2720
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002721 char *keymap_str;
2722
Ran Benita2e1968f2014-08-19 23:59:51 +03002723 xkb_info->shift_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2724 XKB_MOD_NAME_SHIFT);
2725 xkb_info->caps_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2726 XKB_MOD_NAME_CAPS);
2727 xkb_info->ctrl_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2728 XKB_MOD_NAME_CTRL);
2729 xkb_info->alt_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2730 XKB_MOD_NAME_ALT);
2731 xkb_info->mod2_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2732 "Mod2");
2733 xkb_info->mod3_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2734 "Mod3");
2735 xkb_info->super_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2736 XKB_MOD_NAME_LOGO);
2737 xkb_info->mod5_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2738 "Mod5");
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002739
Ran Benita2e1968f2014-08-19 23:59:51 +03002740 xkb_info->num_led = xkb_keymap_led_get_index(xkb_info->keymap,
2741 XKB_LED_NAME_NUM);
2742 xkb_info->caps_led = xkb_keymap_led_get_index(xkb_info->keymap,
2743 XKB_LED_NAME_CAPS);
2744 xkb_info->scroll_led = xkb_keymap_led_get_index(xkb_info->keymap,
2745 XKB_LED_NAME_SCROLL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002746
Ran Benita2e1968f2014-08-19 23:59:51 +03002747 keymap_str = xkb_keymap_get_as_string(xkb_info->keymap,
2748 XKB_KEYMAP_FORMAT_TEXT_V1);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002749 if (keymap_str == NULL) {
2750 weston_log("failed to get string version of keymap\n");
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002751 goto err_keymap;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002752 }
2753 xkb_info->keymap_size = strlen(keymap_str) + 1;
2754
2755 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
2756 if (xkb_info->keymap_fd < 0) {
2757 weston_log("creating a keymap file for %lu bytes failed: %m\n",
2758 (unsigned long) xkb_info->keymap_size);
2759 goto err_keymap_str;
2760 }
2761
2762 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2763 PROT_READ | PROT_WRITE,
2764 MAP_SHARED, xkb_info->keymap_fd, 0);
2765 if (xkb_info->keymap_area == MAP_FAILED) {
2766 weston_log("failed to mmap() %lu bytes\n",
2767 (unsigned long) xkb_info->keymap_size);
2768 goto err_dev_zero;
2769 }
2770 strcpy(xkb_info->keymap_area, keymap_str);
2771 free(keymap_str);
2772
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002773 return xkb_info;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002774
2775err_dev_zero:
2776 close(xkb_info->keymap_fd);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002777err_keymap_str:
2778 free(keymap_str);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002779err_keymap:
Ran Benita2e1968f2014-08-19 23:59:51 +03002780 xkb_keymap_unref(xkb_info->keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002781 free(xkb_info);
2782 return NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002783}
2784
2785static int
2786weston_compositor_build_global_keymap(struct weston_compositor *ec)
2787{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002788 struct xkb_keymap *keymap;
2789
2790 if (ec->xkb_info != NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002791 return 0;
2792
Ran Benita2e1968f2014-08-19 23:59:51 +03002793 keymap = xkb_keymap_new_from_names(ec->xkb_context,
2794 &ec->xkb_names,
2795 0);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002796 if (keymap == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002797 weston_log("failed to compile global XKB keymap\n");
2798 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
2799 "options %s\n",
2800 ec->xkb_names.rules, ec->xkb_names.model,
2801 ec->xkb_names.layout, ec->xkb_names.variant,
2802 ec->xkb_names.options);
2803 return -1;
2804 }
2805
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002806 ec->xkb_info = weston_xkb_info_create(keymap);
Rui Matos73d93952013-10-24 19:28:41 +02002807 xkb_keymap_unref(keymap);
Stefan Schmidtfda26522013-09-17 10:54:09 +01002808 if (ec->xkb_info == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002809 return -1;
2810
2811 return 0;
2812}
2813
Rui Matos65196bc2013-10-10 19:44:19 +02002814WL_EXPORT void
2815weston_seat_update_keymap(struct weston_seat *seat, struct xkb_keymap *keymap)
2816{
Derek Foreman1281a362015-07-31 16:55:32 -05002817 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2818
2819 if (!keyboard || !keymap)
Rui Matos65196bc2013-10-10 19:44:19 +02002820 return;
2821
Derek Foreman1281a362015-07-31 16:55:32 -05002822 xkb_keymap_unref(keyboard->pending_keymap);
2823 keyboard->pending_keymap = xkb_keymap_ref(keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02002824
Derek Foreman1281a362015-07-31 16:55:32 -05002825 if (keyboard->keys.size == 0)
Rui Matos65196bc2013-10-10 19:44:19 +02002826 update_keymap(seat);
Rui Matos65196bc2013-10-10 19:44:19 +02002827}
2828
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002829WL_EXPORT int
2830weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
2831{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002832 struct weston_keyboard *keyboard;
2833
Derek Foreman1281a362015-07-31 16:55:32 -05002834 if (seat->keyboard_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002835 seat->keyboard_device_count += 1;
2836 if (seat->keyboard_device_count == 1)
2837 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002838 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002839 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002840
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002841 keyboard = weston_keyboard_create();
2842 if (keyboard == NULL) {
2843 weston_log("failed to allocate weston keyboard struct\n");
2844 return -1;
2845 }
2846
Derek Foreman185d1582017-06-28 11:17:23 -05002847 if (keymap != NULL) {
2848 keyboard->xkb_info = weston_xkb_info_create(keymap);
2849 if (keyboard->xkb_info == NULL)
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02002850 goto err;
Derek Foreman185d1582017-06-28 11:17:23 -05002851 } else {
2852 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
2853 goto err;
2854 keyboard->xkb_info = seat->compositor->xkb_info;
2855 keyboard->xkb_info->ref_count++;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002856 }
Derek Foreman185d1582017-06-28 11:17:23 -05002857
2858 keyboard->xkb_state.state = xkb_state_new(keyboard->xkb_info->keymap);
2859 if (keyboard->xkb_state.state == NULL) {
2860 weston_log("failed to initialise XKB state\n");
2861 goto err;
2862 }
2863
2864 keyboard->xkb_state.leds = 0;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002865
Derek Foreman1281a362015-07-31 16:55:32 -05002866 seat->keyboard_state = keyboard;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02002867 seat->keyboard_device_count = 1;
2868 keyboard->seat = seat;
2869
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002870 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002871
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002872 return 0;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02002873
2874err:
2875 if (keyboard->xkb_info)
2876 weston_xkb_info_destroy(keyboard->xkb_info);
2877 free(keyboard);
2878
2879 return -1;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002880}
2881
Jonas Ådahl91fed542013-12-03 09:14:27 +01002882static void
2883weston_keyboard_reset_state(struct weston_keyboard *keyboard)
2884{
2885 struct weston_seat *seat = keyboard->seat;
2886 struct xkb_state *state;
2887
Derek Foreman185d1582017-06-28 11:17:23 -05002888 state = xkb_state_new(keyboard->xkb_info->keymap);
2889 if (!state) {
2890 weston_log("failed to reset XKB state\n");
2891 return;
Jonas Ådahl91fed542013-12-03 09:14:27 +01002892 }
Derek Foreman185d1582017-06-28 11:17:23 -05002893 xkb_state_unref(keyboard->xkb_state.state);
2894 keyboard->xkb_state.state = state;
2895
2896 keyboard->xkb_state.leds = 0;
Jonas Ådahl91fed542013-12-03 09:14:27 +01002897
2898 seat->modifier_state = 0;
2899}
2900
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002901WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002902weston_seat_release_keyboard(struct weston_seat *seat)
2903{
2904 seat->keyboard_device_count--;
Derek Foremand621df22014-11-19 11:04:12 -06002905 assert(seat->keyboard_device_count >= 0);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002906 if (seat->keyboard_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05002907 weston_keyboard_set_focus(seat->keyboard_state, NULL);
2908 weston_keyboard_cancel_grab(seat->keyboard_state);
2909 weston_keyboard_reset_state(seat->keyboard_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002910 seat_send_updated_caps(seat);
2911 }
2912}
2913
2914WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002915weston_seat_init_pointer(struct weston_seat *seat)
2916{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002917 struct weston_pointer *pointer;
2918
Derek Foreman1281a362015-07-31 16:55:32 -05002919 if (seat->pointer_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002920 seat->pointer_device_count += 1;
2921 if (seat->pointer_device_count == 1)
2922 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002923 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002924 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002925
Giulio Camuffocdb4d292013-11-14 23:42:53 +01002926 pointer = weston_pointer_create(seat);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002927 if (pointer == NULL)
2928 return;
2929
Derek Foreman1281a362015-07-31 16:55:32 -05002930 seat->pointer_state = pointer;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002931 seat->pointer_device_count = 1;
2932 pointer->seat = seat;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002933
2934 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002935}
2936
2937WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002938weston_seat_release_pointer(struct weston_seat *seat)
2939{
Derek Foreman1281a362015-07-31 16:55:32 -05002940 struct weston_pointer *pointer = seat->pointer_state;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002941
2942 seat->pointer_device_count--;
2943 if (seat->pointer_device_count == 0) {
Derek Foremanf9318d12015-05-11 15:40:11 -05002944 weston_pointer_clear_focus(pointer);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002945 weston_pointer_cancel_grab(pointer);
Jonas Ådahl630bae82013-10-17 23:04:06 +02002946
Jonas Ådahla4932742013-10-17 23:04:07 +02002947 if (pointer->sprite)
2948 pointer_unmap_sprite(pointer);
2949
Jonas Ådahl3e12e632013-12-02 22:05:05 +01002950 weston_pointer_reset_state(pointer);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002951 seat_send_updated_caps(seat);
Derek Foremanfd5ca512015-01-07 15:00:25 -06002952
2953 /* seat->pointer is intentionally not destroyed so that
2954 * a newly attached pointer on this seat will retain
2955 * the previous cursor co-ordinates.
2956 */
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002957 }
2958}
2959
2960WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002961weston_seat_init_touch(struct weston_seat *seat)
2962{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002963 struct weston_touch *touch;
2964
Derek Foreman1281a362015-07-31 16:55:32 -05002965 if (seat->touch_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002966 seat->touch_device_count += 1;
2967 if (seat->touch_device_count == 1)
2968 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002969 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002970 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002971
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002972 touch = weston_touch_create();
2973 if (touch == NULL)
2974 return;
2975
Derek Foreman1281a362015-07-31 16:55:32 -05002976 seat->touch_state = touch;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002977 seat->touch_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002978 touch->seat = seat;
2979
2980 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002981}
2982
2983WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002984weston_seat_release_touch(struct weston_seat *seat)
2985{
2986 seat->touch_device_count--;
2987 if (seat->touch_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05002988 weston_touch_set_focus(seat->touch_state, NULL);
2989 weston_touch_cancel_grab(seat->touch_state);
2990 weston_touch_reset_state(seat->touch_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002991 seat_send_updated_caps(seat);
2992 }
2993}
2994
2995WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01002996weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
2997 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002998{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04002999 memset(seat, 0, sizeof *seat);
3000
Kristian Høgsberge3148752013-05-06 23:19:49 -04003001 seat->selection_data_source = NULL;
3002 wl_list_init(&seat->base_resource_list);
3003 wl_signal_init(&seat->selection_signal);
3004 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04003005 wl_signal_init(&seat->destroy_signal);
Jason Ekstranda4ab5422014-04-02 19:53:45 -05003006 wl_signal_init(&seat->updated_caps_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003007
Peter Hutterer87743e92016-01-18 16:38:22 +10003008 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 5,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003009 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003010
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003011 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003012 seat->modifier_state = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003013 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003014
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003015 wl_list_insert(ec->seat_list.prev, &seat->link);
3016
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003017 clipboard_create(seat);
3018
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003019 wl_signal_emit(&ec->seat_created_signal, seat);
3020}
3021
3022WL_EXPORT void
3023weston_seat_release(struct weston_seat *seat)
3024{
3025 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003026
Jonas Ådahl1afb2382014-01-03 19:46:51 +01003027 if (seat->saved_kbd_focus)
3028 wl_list_remove(&seat->saved_kbd_focus_listener.link);
3029
Derek Foreman1281a362015-07-31 16:55:32 -05003030 if (seat->pointer_state)
3031 weston_pointer_destroy(seat->pointer_state);
3032 if (seat->keyboard_state)
3033 weston_keyboard_destroy(seat->keyboard_state);
3034 if (seat->touch_state)
3035 weston_touch_destroy(seat->touch_state);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003036
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003037 free (seat->seat_name);
3038
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003039 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04003040
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003041 wl_signal_emit(&seat->destroy_signal, seat);
3042}
Derek Foreman1281a362015-07-31 16:55:32 -05003043
3044/** Get a seat's keyboard pointer
3045 *
3046 * \param seat The seat to query
3047 * \return The seat's keyboard pointer, or NULL if no keyboard is present
3048 *
3049 * The keyboard pointer for a seat isn't freed when all keyboards are removed,
3050 * so it should only be used when the seat's keyboard_device_count is greater
3051 * than zero. This function does that test and only returns a pointer
3052 * when a keyboard is present.
3053 */
3054WL_EXPORT struct weston_keyboard *
3055weston_seat_get_keyboard(struct weston_seat *seat)
3056{
3057 if (!seat)
3058 return NULL;
3059
3060 if (seat->keyboard_device_count)
3061 return seat->keyboard_state;
3062
3063 return NULL;
3064}
3065
3066/** Get a seat's pointer pointer
3067 *
3068 * \param seat The seat to query
3069 * \return The seat's pointer pointer, or NULL if no pointer device is present
3070 *
3071 * The pointer pointer for a seat isn't freed when all mice are removed,
3072 * so it should only be used when the seat's pointer_device_count is greater
3073 * than zero. This function does that test and only returns a pointer
3074 * when a pointing device is present.
3075 */
3076WL_EXPORT struct weston_pointer *
3077weston_seat_get_pointer(struct weston_seat *seat)
3078{
3079 if (!seat)
3080 return NULL;
3081
3082 if (seat->pointer_device_count)
3083 return seat->pointer_state;
3084
3085 return NULL;
3086}
3087
Jonas Ådahld3414f22016-07-22 17:56:31 +08003088static const struct zwp_locked_pointer_v1_interface locked_pointer_interface;
3089static const struct zwp_confined_pointer_v1_interface confined_pointer_interface;
3090
3091static enum pointer_constraint_type
3092pointer_constraint_get_type(struct weston_pointer_constraint *constraint)
3093{
3094 if (wl_resource_instance_of(constraint->resource,
3095 &zwp_locked_pointer_v1_interface,
3096 &locked_pointer_interface)) {
3097 return POINTER_CONSTRAINT_TYPE_LOCK;
3098 } else if (wl_resource_instance_of(constraint->resource,
3099 &zwp_confined_pointer_v1_interface,
3100 &confined_pointer_interface)) {
3101 return POINTER_CONSTRAINT_TYPE_CONFINE;
3102 }
3103
3104 abort();
3105 return 0;
3106}
3107
3108static void
3109pointer_constraint_notify_activated(struct weston_pointer_constraint *constraint)
3110{
3111 struct wl_resource *resource = constraint->resource;
3112
3113 switch (pointer_constraint_get_type(constraint)) {
3114 case POINTER_CONSTRAINT_TYPE_LOCK:
3115 zwp_locked_pointer_v1_send_locked(resource);
3116 break;
3117 case POINTER_CONSTRAINT_TYPE_CONFINE:
3118 zwp_confined_pointer_v1_send_confined(resource);
3119 break;
3120 }
3121}
3122
3123static void
3124pointer_constraint_notify_deactivated(struct weston_pointer_constraint *constraint)
3125{
3126 struct wl_resource *resource = constraint->resource;
3127
3128 switch (pointer_constraint_get_type(constraint)) {
3129 case POINTER_CONSTRAINT_TYPE_LOCK:
3130 zwp_locked_pointer_v1_send_unlocked(resource);
3131 break;
3132 case POINTER_CONSTRAINT_TYPE_CONFINE:
3133 zwp_confined_pointer_v1_send_unconfined(resource);
3134 break;
3135 }
3136}
3137
3138static struct weston_pointer_constraint *
3139get_pointer_constraint_for_pointer(struct weston_surface *surface,
3140 struct weston_pointer *pointer)
3141{
3142 struct weston_pointer_constraint *constraint;
3143
3144 wl_list_for_each(constraint, &surface->pointer_constraints, link) {
3145 if (constraint->pointer == pointer)
3146 return constraint;
3147 }
3148
3149 return NULL;
3150}
3151
Derek Foreman1281a362015-07-31 16:55:32 -05003152/** Get a seat's touch pointer
3153 *
3154 * \param seat The seat to query
3155 * \return The seat's touch pointer, or NULL if no touch device is present
3156 *
3157 * The touch pointer for a seat isn't freed when all touch devices are removed,
3158 * so it should only be used when the seat's touch_device_count is greater
3159 * than zero. This function does that test and only returns a pointer
3160 * when a touch device is present.
3161 */
3162WL_EXPORT struct weston_touch *
3163weston_seat_get_touch(struct weston_seat *seat)
3164{
3165 if (!seat)
3166 return NULL;
3167
3168 if (seat->touch_device_count)
3169 return seat->touch_state;
3170
3171 return NULL;
3172}
Bryce Harrington24f917e2016-06-29 19:04:07 -07003173
3174/** Sets the keyboard focus to the given surface
3175 *
3176 * \param seat The seat to query
3177 */
3178WL_EXPORT void
3179weston_seat_set_keyboard_focus(struct weston_seat *seat,
3180 struct weston_surface *surface)
3181{
3182 struct weston_compositor *compositor = seat->compositor;
3183 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003184 struct weston_surface_activation_data activation_data;
Bryce Harrington24f917e2016-06-29 19:04:07 -07003185
Jonas Ådahlef8e1c32016-03-15 20:28:51 +08003186 if (keyboard && keyboard->focus != surface) {
Bryce Harrington24f917e2016-06-29 19:04:07 -07003187 weston_keyboard_set_focus(keyboard, surface);
3188 wl_data_device_set_keyboard_focus(seat);
3189 }
3190
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003191 inc_activate_serial(compositor);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003192
3193 activation_data = (struct weston_surface_activation_data) {
3194 .surface = surface,
3195 .seat = seat,
3196 };
3197 wl_signal_emit(&compositor->activate_signal, &activation_data);
Bryce Harrington24f917e2016-06-29 19:04:07 -07003198}
Jonas Ådahl30d61d82014-10-22 21:21:17 +02003199
Jonas Ådahld3414f22016-07-22 17:56:31 +08003200static void
3201enable_pointer_constraint(struct weston_pointer_constraint *constraint,
3202 struct weston_view *view)
3203{
3204 assert(constraint->view == NULL);
3205 constraint->view = view;
3206 pointer_constraint_notify_activated(constraint);
3207 weston_pointer_start_grab(constraint->pointer, &constraint->grab);
3208 wl_list_remove(&constraint->surface_destroy_listener.link);
3209 wl_list_init(&constraint->surface_destroy_listener.link);
3210}
3211
3212static bool
3213is_pointer_constraint_enabled(struct weston_pointer_constraint *constraint)
3214{
3215 return constraint->view != NULL;
3216}
3217
3218static void
3219weston_pointer_constraint_disable(struct weston_pointer_constraint *constraint)
3220{
3221 constraint->view = NULL;
3222 pointer_constraint_notify_deactivated(constraint);
3223 weston_pointer_end_grab(constraint->grab.pointer);
3224}
3225
3226void
3227weston_pointer_constraint_destroy(struct weston_pointer_constraint *constraint)
3228{
3229 if (is_pointer_constraint_enabled(constraint))
3230 weston_pointer_constraint_disable(constraint);
3231
3232 wl_list_remove(&constraint->pointer_destroy_listener.link);
3233 wl_list_remove(&constraint->surface_destroy_listener.link);
3234 wl_list_remove(&constraint->surface_commit_listener.link);
3235 wl_list_remove(&constraint->surface_activate_listener.link);
3236
3237 wl_resource_set_user_data(constraint->resource, NULL);
3238 pixman_region32_fini(&constraint->region);
3239 wl_list_remove(&constraint->link);
3240 free(constraint);
3241}
3242
3243static void
3244disable_pointer_constraint(struct weston_pointer_constraint *constraint)
3245{
3246 switch (constraint->lifetime) {
3247 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT:
3248 weston_pointer_constraint_destroy(constraint);
3249 break;
3250 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT:
3251 weston_pointer_constraint_disable(constraint);
3252 break;
3253 }
3254}
3255
3256static bool
3257is_within_constraint_region(struct weston_pointer_constraint *constraint,
3258 wl_fixed_t sx, wl_fixed_t sy)
3259{
3260 struct weston_surface *surface = constraint->surface;
3261 pixman_region32_t constraint_region;
3262 bool result;
3263
3264 pixman_region32_init(&constraint_region);
3265 pixman_region32_intersect(&constraint_region,
3266 &surface->input,
3267 &constraint->region);
3268 result = pixman_region32_contains_point(&constraint_region,
3269 wl_fixed_to_int(sx),
3270 wl_fixed_to_int(sy),
3271 NULL);
3272 pixman_region32_fini(&constraint_region);
3273
3274 return result;
3275}
3276
3277static void
3278maybe_enable_pointer_constraint(struct weston_pointer_constraint *constraint)
3279{
3280 struct weston_surface *surface = constraint->surface;
3281 struct weston_view *vit;
3282 struct weston_view *view = NULL;
3283 struct weston_pointer *pointer = constraint->pointer;
3284 struct weston_keyboard *keyboard;
3285 struct weston_seat *seat = pointer->seat;
3286 int32_t x, y;
3287
3288 /* Postpone if no view of the surface was most recently clicked. */
3289 wl_list_for_each(vit, &surface->views, surface_link) {
3290 if (vit->click_to_activate_serial ==
3291 surface->compositor->activate_serial) {
3292 view = vit;
3293 }
3294 }
3295 if (view == NULL)
3296 return;
3297
3298 /* Postpone if surface doesn't have keyboard focus. */
3299 keyboard = weston_seat_get_keyboard(seat);
3300 if (!keyboard || keyboard->focus != surface)
3301 return;
3302
3303 /* Postpone constraint if the pointer is not within the
3304 * constraint region.
3305 */
3306 weston_view_from_global(view,
3307 wl_fixed_to_int(pointer->x),
3308 wl_fixed_to_int(pointer->y),
3309 &x, &y);
3310 if (!is_within_constraint_region(constraint,
3311 wl_fixed_from_int(x),
3312 wl_fixed_from_int(y)))
3313 return;
3314
3315 enable_pointer_constraint(constraint, view);
3316}
3317
3318static void
3319locked_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
3320{
3321}
3322
3323static void
3324locked_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02003325 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003326 struct weston_pointer_motion_event *event)
3327{
Quentin Glidiccde13452016-08-12 10:41:32 +02003328 pointer_send_relative_motion(grab->pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08003329}
3330
3331static void
3332locked_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003333 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003334 uint32_t button,
3335 uint32_t state_w)
3336{
3337 weston_pointer_send_button(grab->pointer, time, button, state_w);
3338}
3339
3340static void
3341locked_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
3342 uint32_t time,
3343 struct weston_pointer_axis_event *event)
3344{
3345 weston_pointer_send_axis(grab->pointer, time, event);
3346}
3347
3348static void
3349locked_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
3350 uint32_t source)
3351{
3352 weston_pointer_send_axis_source(grab->pointer, source);
3353}
3354
3355static void
3356locked_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
3357{
3358 weston_pointer_send_frame(grab->pointer);
3359}
3360
3361static void
3362locked_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
3363{
3364 struct weston_pointer_constraint *constraint =
3365 container_of(grab, struct weston_pointer_constraint, grab);
3366
3367 disable_pointer_constraint(constraint);
3368}
3369
3370static const struct weston_pointer_grab_interface
3371 locked_pointer_grab_interface = {
3372 locked_pointer_grab_pointer_focus,
3373 locked_pointer_grab_pointer_motion,
3374 locked_pointer_grab_pointer_button,
3375 locked_pointer_grab_pointer_axis,
3376 locked_pointer_grab_pointer_axis_source,
3377 locked_pointer_grab_pointer_frame,
3378 locked_pointer_grab_pointer_cancel,
3379};
3380
3381static void
3382pointer_constraint_constrain_resource_destroyed(struct wl_resource *resource)
3383{
3384 struct weston_pointer_constraint *constraint =
3385 wl_resource_get_user_data(resource);
3386
3387 if (!constraint)
3388 return;
3389
3390 weston_pointer_constraint_destroy(constraint);
3391}
3392
3393static void
3394pointer_constraint_surface_activate(struct wl_listener *listener, void *data)
3395{
3396 struct weston_surface_activation_data *activation = data;
3397 struct weston_pointer *pointer;
3398 struct weston_surface *focus = activation->surface;
3399 struct weston_pointer_constraint *constraint =
3400 container_of(listener, struct weston_pointer_constraint,
3401 surface_activate_listener);
3402 bool is_constraint_surface;
3403
3404 pointer = weston_seat_get_pointer(activation->seat);
3405 if (!pointer)
3406 return;
3407
3408 is_constraint_surface =
3409 get_pointer_constraint_for_pointer(focus, pointer) == constraint;
3410
3411 if (is_constraint_surface &&
3412 !is_pointer_constraint_enabled(constraint))
3413 maybe_enable_pointer_constraint(constraint);
3414 else if (!is_constraint_surface &&
3415 is_pointer_constraint_enabled(constraint))
3416 disable_pointer_constraint(constraint);
3417}
3418
3419static void
3420pointer_constraint_pointer_destroyed(struct wl_listener *listener, void *data)
3421{
3422 struct weston_pointer_constraint *constraint =
3423 container_of(listener, struct weston_pointer_constraint,
3424 pointer_destroy_listener);
3425
3426 weston_pointer_constraint_destroy(constraint);
3427}
3428
3429static void
3430pointer_constraint_surface_destroyed(struct wl_listener *listener, void *data)
3431{
3432 struct weston_pointer_constraint *constraint =
3433 container_of(listener, struct weston_pointer_constraint,
3434 surface_destroy_listener);
3435
3436 weston_pointer_constraint_destroy(constraint);
3437}
3438
3439static void
3440pointer_constraint_surface_committed(struct wl_listener *listener, void *data)
3441{
3442 struct weston_pointer_constraint *constraint =
3443 container_of(listener, struct weston_pointer_constraint,
3444 surface_commit_listener);
3445
3446 if (constraint->region_is_pending) {
3447 constraint->region_is_pending = false;
3448 pixman_region32_copy(&constraint->region,
3449 &constraint->region_pending);
3450 pixman_region32_fini(&constraint->region_pending);
3451 pixman_region32_init(&constraint->region_pending);
3452 }
3453
3454 if (constraint->hint_is_pending) {
3455 constraint->hint_is_pending = false;
3456
3457 constraint->hint_is_pending = true;
3458 constraint->hint_x = constraint->hint_x_pending;
3459 constraint->hint_y = constraint->hint_y_pending;
3460 }
3461
3462 if (pointer_constraint_get_type(constraint) ==
3463 POINTER_CONSTRAINT_TYPE_CONFINE &&
3464 is_pointer_constraint_enabled(constraint))
3465 maybe_warp_confined_pointer(constraint);
3466}
3467
3468static struct weston_pointer_constraint *
3469weston_pointer_constraint_create(struct weston_surface *surface,
3470 struct weston_pointer *pointer,
3471 struct weston_region *region,
3472 enum zwp_pointer_constraints_v1_lifetime lifetime,
3473 struct wl_resource *cr,
3474 const struct weston_pointer_grab_interface *grab_interface)
3475{
3476 struct weston_pointer_constraint *constraint;
3477
3478 constraint = zalloc(sizeof *constraint);
3479 if (!constraint)
3480 return NULL;
3481
3482 constraint->lifetime = lifetime;
3483 pixman_region32_init(&constraint->region);
3484 pixman_region32_init(&constraint->region_pending);
3485 wl_list_insert(&surface->pointer_constraints, &constraint->link);
3486 constraint->surface = surface;
3487 constraint->pointer = pointer;
3488 constraint->resource = cr;
3489 constraint->grab.interface = grab_interface;
3490 if (region) {
3491 pixman_region32_copy(&constraint->region,
3492 &region->region);
3493 } else {
3494 pixman_region32_fini(&constraint->region);
3495 region_init_infinite(&constraint->region);
3496 }
3497
3498 constraint->surface_activate_listener.notify =
3499 pointer_constraint_surface_activate;
3500 constraint->surface_destroy_listener.notify =
3501 pointer_constraint_surface_destroyed;
3502 constraint->surface_commit_listener.notify =
3503 pointer_constraint_surface_committed;
3504 constraint->pointer_destroy_listener.notify =
3505 pointer_constraint_pointer_destroyed;
3506
3507 wl_signal_add(&surface->compositor->activate_signal,
3508 &constraint->surface_activate_listener);
3509 wl_signal_add(&pointer->destroy_signal,
3510 &constraint->pointer_destroy_listener);
3511 wl_signal_add(&surface->destroy_signal,
3512 &constraint->surface_destroy_listener);
3513 wl_signal_add(&surface->commit_signal,
3514 &constraint->surface_commit_listener);
3515
3516 return constraint;
3517}
3518
3519static void
3520init_pointer_constraint(struct wl_resource *pointer_constraints_resource,
3521 uint32_t id,
3522 struct weston_surface *surface,
3523 struct weston_pointer *pointer,
3524 struct weston_region *region,
3525 enum zwp_pointer_constraints_v1_lifetime lifetime,
3526 const struct wl_interface *interface,
3527 const void *implementation,
3528 const struct weston_pointer_grab_interface *grab_interface)
3529{
3530 struct wl_client *client =
3531 wl_resource_get_client(pointer_constraints_resource);
3532 struct wl_resource *cr;
3533 struct weston_pointer_constraint *constraint;
3534
3535 if (get_pointer_constraint_for_pointer(surface, pointer)) {
3536 wl_resource_post_error(pointer_constraints_resource,
3537 ZWP_POINTER_CONSTRAINTS_V1_ERROR_ALREADY_CONSTRAINED,
3538 "the pointer has a lock/confine request on this surface");
3539 return;
3540 }
3541
3542 cr = wl_resource_create(client, interface,
3543 wl_resource_get_version(pointer_constraints_resource),
3544 id);
3545 if (cr == NULL) {
3546 wl_client_post_no_memory(client);
3547 return;
3548 }
3549
3550 constraint = weston_pointer_constraint_create(surface, pointer,
3551 region, lifetime,
3552 cr, grab_interface);
3553 if (constraint == NULL) {
3554 wl_client_post_no_memory(client);
3555 return;
3556 }
3557
3558 wl_resource_set_implementation(cr, implementation, constraint,
3559 pointer_constraint_constrain_resource_destroyed);
3560
3561 maybe_enable_pointer_constraint(constraint);
3562}
3563
3564static void
3565pointer_constraints_destroy(struct wl_client *client,
3566 struct wl_resource *resource)
3567{
3568 wl_resource_destroy(resource);
3569}
3570
3571static void
3572locked_pointer_destroy(struct wl_client *client,
3573 struct wl_resource *resource)
3574{
3575 struct weston_pointer_constraint *constraint =
3576 wl_resource_get_user_data(resource);
3577 wl_fixed_t x, y;
3578
3579 if (constraint && constraint->view && constraint->hint_is_pending &&
3580 is_within_constraint_region(constraint,
3581 constraint->hint_x,
3582 constraint->hint_y)) {
3583 weston_view_to_global_fixed(constraint->view,
3584 constraint->hint_x,
3585 constraint->hint_y,
3586 &x, &y);
3587 weston_pointer_move_to(constraint->pointer, x, y);
3588 }
3589 wl_resource_destroy(resource);
3590}
3591
3592static void
3593locked_pointer_set_cursor_position_hint(struct wl_client *client,
3594 struct wl_resource *resource,
3595 wl_fixed_t surface_x,
3596 wl_fixed_t surface_y)
3597{
3598 struct weston_pointer_constraint *constraint =
3599 wl_resource_get_user_data(resource);
3600
3601 /* Ignore a set cursor hint that was sent after the lock was cancelled.
3602 */
3603 if (!constraint ||
3604 !constraint->resource ||
3605 constraint->resource != resource)
3606 return;
3607
3608 constraint->hint_is_pending = true;
3609 constraint->hint_x_pending = surface_x;
3610 constraint->hint_y_pending = surface_y;
3611}
3612
3613static void
3614locked_pointer_set_region(struct wl_client *client,
3615 struct wl_resource *resource,
3616 struct wl_resource *region_resource)
3617{
3618 struct weston_pointer_constraint *constraint =
3619 wl_resource_get_user_data(resource);
3620 struct weston_region *region = region_resource ?
3621 wl_resource_get_user_data(region_resource) : NULL;
3622
3623 if (!constraint)
3624 return;
3625
3626 if (region) {
3627 pixman_region32_copy(&constraint->region_pending,
3628 &region->region);
3629 } else {
3630 pixman_region32_fini(&constraint->region_pending);
3631 region_init_infinite(&constraint->region_pending);
3632 }
3633 constraint->region_is_pending = true;
3634}
3635
3636
3637static const struct zwp_locked_pointer_v1_interface locked_pointer_interface = {
3638 locked_pointer_destroy,
3639 locked_pointer_set_cursor_position_hint,
3640 locked_pointer_set_region,
3641};
3642
3643static void
3644pointer_constraints_lock_pointer(struct wl_client *client,
3645 struct wl_resource *resource,
3646 uint32_t id,
3647 struct wl_resource *surface_resource,
3648 struct wl_resource *pointer_resource,
3649 struct wl_resource *region_resource,
3650 uint32_t lifetime)
3651{
3652 struct weston_surface *surface =
3653 wl_resource_get_user_data(surface_resource);
3654 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
3655 struct weston_region *region = region_resource ?
3656 wl_resource_get_user_data(region_resource) : NULL;
3657
3658 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
3659 &zwp_locked_pointer_v1_interface,
3660 &locked_pointer_interface,
3661 &locked_pointer_grab_interface);
3662}
3663
3664static void
3665confined_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
3666{
3667}
3668
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08003669static double
3670vec2d_cross_product(struct vec2d a, struct vec2d b)
3671{
3672 return a.x * b.y - a.y * b.x;
3673}
3674
3675static struct vec2d
3676vec2d_add(struct vec2d a, struct vec2d b)
3677{
3678 return (struct vec2d) {
3679 .x = a.x + b.x,
3680 .y = a.y + b.y,
3681 };
3682}
3683
3684static struct vec2d
3685vec2d_subtract(struct vec2d a, struct vec2d b)
3686{
3687 return (struct vec2d) {
3688 .x = a.x - b.x,
3689 .y = a.y - b.y,
3690 };
3691}
3692
3693static struct vec2d
3694vec2d_multiply_constant(double c, struct vec2d a)
3695{
3696 return (struct vec2d) {
3697 .x = c * a.x,
3698 .y = c * a.y,
3699 };
3700}
3701
3702static bool
3703lines_intersect(struct line *line1, struct line *line2,
3704 struct vec2d *intersection)
3705{
3706 struct vec2d p = line1->a;
3707 struct vec2d r = vec2d_subtract(line1->b, line1->a);
3708 struct vec2d q = line2->a;
3709 struct vec2d s = vec2d_subtract(line2->b, line2->a);
3710 double rxs;
3711 double sxr;
3712 double t;
3713 double u;
3714
3715 /*
3716 * The line (p, r) and (q, s) intersects where
3717 *
3718 * p + t r = q + u s
3719 *
3720 * Calculate t:
3721 *
3722 * (p + t r) × s = (q + u s) × s
3723 * p × s + t (r × s) = q × s + u (s × s)
3724 * p × s + t (r × s) = q × s
3725 * t (r × s) = q × s - p × s
3726 * t (r × s) = (q - p) × s
3727 * t = ((q - p) × s) / (r × s)
3728 *
3729 * Using the same method, for u we get:
3730 *
3731 * u = ((p - q) × r) / (s × r)
3732 */
3733
3734 rxs = vec2d_cross_product(r, s);
3735 sxr = vec2d_cross_product(s, r);
3736
3737 /* If r × s = 0 then the lines are either parallel or collinear. */
3738 if (fabs(rxs) < DBL_MIN)
3739 return false;
3740
3741 t = vec2d_cross_product(vec2d_subtract(q, p), s) / rxs;
3742 u = vec2d_cross_product(vec2d_subtract(p, q), r) / sxr;
3743
3744 /* The lines only intersect if 0 ≤ t ≤ 1 and 0 ≤ u ≤ 1. */
3745 if (t < 0.0 || t > 1.0 || u < 0.0 || u > 1.0)
3746 return false;
3747
3748 *intersection = vec2d_add(p, vec2d_multiply_constant(t, r));
3749 return true;
3750}
3751
3752static struct border *
3753add_border(struct wl_array *array,
3754 double x1, double y1,
3755 double x2, double y2,
3756 enum motion_direction blocking_dir)
3757{
3758 struct border *border = wl_array_add(array, sizeof *border);
3759
3760 *border = (struct border) {
3761 .line = (struct line) {
3762 .a = (struct vec2d) {
3763 .x = x1,
3764 .y = y1,
3765 },
3766 .b = (struct vec2d) {
3767 .x = x2,
3768 .y = y2,
3769 },
3770 },
3771 .blocking_dir = blocking_dir,
3772 };
3773
3774 return border;
3775}
3776
3777static int
3778compare_lines_x(const void *a, const void *b)
3779{
3780 const struct border *border_a = a;
3781 const struct border *border_b = b;
3782
3783
3784 if (border_a->line.a.x == border_b->line.a.x)
3785 return border_a->line.b.x < border_b->line.b.x;
3786 else
3787 return border_a->line.a.x > border_b->line.a.x;
3788}
3789
3790static void
3791add_non_overlapping_edges(pixman_box32_t *boxes,
3792 int band_above_start,
3793 int band_below_start,
3794 int band_below_end,
3795 struct wl_array *borders)
3796{
3797 int i;
3798 struct wl_array band_merge;
3799 struct border *border;
3800 struct border *prev_border;
3801 struct border *new_border;
3802
3803 wl_array_init(&band_merge);
3804
3805 /* Add bottom band of previous row, and top band of current row, and
3806 * sort them so lower left x coordinate comes first. If there are two
3807 * borders with the same left x coordinate, the wider one comes first.
3808 */
3809 for (i = band_above_start; i < band_below_start; i++) {
3810 pixman_box32_t *box = &boxes[i];
3811 add_border(&band_merge, box->x1, box->y2, box->x2, box->y2,
3812 MOTION_DIRECTION_POSITIVE_Y);
3813 }
3814 for (i = band_below_start; i < band_below_end; i++) {
3815 pixman_box32_t *box= &boxes[i];
3816 add_border(&band_merge, box->x1, box->y1, box->x2, box->y1,
3817 MOTION_DIRECTION_NEGATIVE_Y);
3818 }
3819 qsort(band_merge.data,
3820 band_merge.size / sizeof *border,
3821 sizeof *border,
3822 compare_lines_x);
3823
3824 /* Combine the two combined bands so that any overlapping border is
3825 * eliminated. */
3826 prev_border = NULL;
3827 wl_array_for_each(border, &band_merge) {
3828 assert(border->line.a.y == border->line.b.y);
3829 assert(!prev_border ||
3830 prev_border->line.a.y == border->line.a.y);
3831 assert(!prev_border ||
3832 (prev_border->line.a.x != border->line.a.x ||
3833 prev_border->line.b.x != border->line.b.x));
3834 assert(!prev_border ||
3835 prev_border->line.a.x <= border->line.a.x);
3836
3837 if (prev_border &&
3838 prev_border->line.a.x == border->line.a.x) {
3839 /*
3840 * ------------ +
3841 * ------- =
3842 * [ ]-----
3843 */
3844 prev_border->line.a.x = border->line.b.x;
3845 } else if (prev_border &&
3846 prev_border->line.b.x == border->line.b.x) {
3847 /*
3848 * ------------ +
3849 * ------ =
3850 * ------[ ]
3851 */
3852 prev_border->line.b.x = border->line.a.x;
3853 } else if (prev_border &&
3854 prev_border->line.b.x == border->line.a.x) {
3855 /*
3856 * -------- +
3857 * ------ =
3858 * --------------
3859 */
3860 prev_border->line.b.x = border->line.b.x;
3861 } else if (prev_border &&
3862 prev_border->line.b.x >= border->line.a.x) {
3863 /*
3864 * --------------- +
3865 * ------ =
3866 * -----[ ]----
3867 */
3868 new_border = add_border(borders,
3869 border->line.b.x,
3870 border->line.b.y,
3871 prev_border->line.b.x,
3872 prev_border->line.b.y,
3873 prev_border->blocking_dir);
3874 prev_border->line.b.x = border->line.a.x;
3875 prev_border = new_border;
3876 } else {
3877 assert(!prev_border ||
3878 prev_border->line.b.x < border->line.a.x);
3879 /*
3880 * First border or non-overlapping.
3881 *
3882 * ----- +
3883 * ----- =
3884 * ----- -----
3885 */
3886 new_border = wl_array_add(borders, sizeof *border);
3887 *new_border = *border;
3888 prev_border = new_border;
3889 }
3890 }
3891
3892 wl_array_release(&band_merge);
3893}
3894
3895static void
3896add_band_bottom_edges(pixman_box32_t *boxes,
3897 int band_start,
3898 int band_end,
3899 struct wl_array *borders)
3900{
3901 int i;
3902
3903 for (i = band_start; i < band_end; i++) {
3904 add_border(borders,
3905 boxes[i].x1, boxes[i].y2,
3906 boxes[i].x2, boxes[i].y2,
3907 MOTION_DIRECTION_POSITIVE_Y);
3908 }
3909}
3910
3911static void
3912region_to_outline(pixman_region32_t *region, struct wl_array *borders)
3913{
3914 pixman_box32_t *boxes;
3915 int num_boxes;
3916 int i;
3917 int top_most, bottom_most;
3918 int current_roof;
3919 int prev_top;
3920 int band_start, prev_band_start;
3921
3922 /*
3923 * Remove any overlapping lines from the set of rectangles. Note that
3924 * pixman regions are grouped as rows of rectangles, where rectangles
3925 * in one row never touch or overlap and are all of the same height.
3926 *
3927 * -------- --- -------- ---
3928 * | | | | | | | |
3929 * ----------====---- --- ----------- ----- ---
3930 * | | => | |
3931 * ----==========--------- ----- ----------
3932 * | | | |
3933 * ------------------- -------------------
3934 *
3935 */
3936
3937 boxes = pixman_region32_rectangles(region, &num_boxes);
3938 prev_top = 0;
3939 top_most = boxes[0].y1;
3940 current_roof = top_most;
3941 bottom_most = boxes[num_boxes - 1].y2;
3942 band_start = 0;
3943 prev_band_start = 0;
3944 for (i = 0; i < num_boxes; i++) {
3945 /* Detect if there is a vertical empty space, and add the lower
3946 * level of the previous band if so was the case. */
3947 if (i > 0 &&
3948 boxes[i].y1 != prev_top &&
3949 boxes[i].y1 != boxes[i - 1].y2) {
3950 current_roof = boxes[i].y1;
3951 add_band_bottom_edges(boxes,
3952 band_start,
3953 i,
3954 borders);
3955 }
3956
3957 /* Special case adding the last band, since it won't be handled
3958 * by the band change detection below. */
3959 if (boxes[i].y1 != current_roof && i == num_boxes - 1) {
3960 if (boxes[i].y1 != prev_top) {
3961 /* The last band is a single box, so we don't
3962 * have a prev_band_start to tell us when the
3963 * previous band started. */
3964 add_non_overlapping_edges(boxes,
3965 band_start,
3966 i,
3967 i + 1,
3968 borders);
3969 } else {
3970 add_non_overlapping_edges(boxes,
3971 prev_band_start,
3972 band_start,
3973 i + 1,
3974 borders);
3975 }
3976 }
3977
3978 /* Detect when passing a band and combine the top border of the
3979 * just passed band with the bottom band of the previous band.
3980 */
3981 if (boxes[i].y1 != top_most && boxes[i].y1 != prev_top) {
3982 /* Combine the two passed bands. */
3983 if (prev_top != current_roof) {
3984 add_non_overlapping_edges(boxes,
3985 prev_band_start,
3986 band_start,
3987 i,
3988 borders);
3989 }
3990
3991 prev_band_start = band_start;
3992 band_start = i;
3993 }
3994
3995 /* Add the top border if the box is part of the current roof. */
3996 if (boxes[i].y1 == current_roof) {
3997 add_border(borders,
3998 boxes[i].x1, boxes[i].y1,
3999 boxes[i].x2, boxes[i].y1,
4000 MOTION_DIRECTION_NEGATIVE_Y);
4001 }
4002
4003 /* Add the bottom border of the last band. */
4004 if (boxes[i].y2 == bottom_most) {
4005 add_border(borders,
4006 boxes[i].x1, boxes[i].y2,
4007 boxes[i].x2, boxes[i].y2,
4008 MOTION_DIRECTION_POSITIVE_Y);
4009 }
4010
4011 /* Always add the left border. */
4012 add_border(borders,
4013 boxes[i].x1, boxes[i].y1,
4014 boxes[i].x1, boxes[i].y2,
4015 MOTION_DIRECTION_NEGATIVE_X);
4016
4017 /* Always add the right border. */
4018 add_border(borders,
4019 boxes[i].x2, boxes[i].y1,
4020 boxes[i].x2, boxes[i].y2,
4021 MOTION_DIRECTION_POSITIVE_X);
4022
4023 prev_top = boxes[i].y1;
4024 }
4025}
4026
4027static bool
4028is_border_horizontal (struct border *border)
4029{
4030 return border->line.a.y == border->line.b.y;
4031}
4032
4033static bool
4034is_border_blocking_directions(struct border *border,
4035 uint32_t directions)
4036{
4037 /* Don't block parallel motions. */
4038 if (is_border_horizontal(border)) {
4039 if ((directions & (MOTION_DIRECTION_POSITIVE_Y |
4040 MOTION_DIRECTION_NEGATIVE_Y)) == 0)
4041 return false;
4042 } else {
4043 if ((directions & (MOTION_DIRECTION_POSITIVE_X |
4044 MOTION_DIRECTION_NEGATIVE_X)) == 0)
4045 return false;
4046 }
4047
4048 return (~border->blocking_dir & directions) != directions;
4049}
4050
4051static struct border *
4052get_closest_border(struct wl_array *borders,
4053 struct line *motion,
4054 uint32_t directions)
4055{
4056 struct border *border;
4057 struct vec2d intersection;
4058 struct vec2d delta;
4059 double distance_2;
4060 struct border *closest_border = NULL;
4061 double closest_distance_2 = DBL_MAX;
4062
4063 wl_array_for_each(border, borders) {
4064 if (!is_border_blocking_directions(border, directions))
4065 continue;
4066
4067 if (!lines_intersect(&border->line, motion, &intersection))
4068 continue;
4069
4070 delta = vec2d_subtract(intersection, motion->a);
4071 distance_2 = delta.x*delta.x + delta.y*delta.y;
4072 if (distance_2 < closest_distance_2) {
4073 closest_border = border;
4074 closest_distance_2 = distance_2;
4075 }
4076 }
4077
4078 return closest_border;
4079}
4080
4081static void
4082clamp_to_border(struct border *border,
4083 struct line *motion,
4084 uint32_t *motion_dir)
4085{
4086 /*
4087 * When clamping either rightward or downward motions, the motion needs
4088 * to be clamped so that the destination coordinate does not end up on
4089 * the border (see weston_pointer_clamp_event_to_region). Do this by
4090 * clamping such motions to the border minus the smallest possible
4091 * wl_fixed_t value.
4092 */
4093 if (is_border_horizontal(border)) {
4094 if (*motion_dir & MOTION_DIRECTION_POSITIVE_Y)
4095 motion->b.y = border->line.a.y - wl_fixed_to_double(1);
4096 else
4097 motion->b.y = border->line.a.y;
4098 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_Y |
4099 MOTION_DIRECTION_NEGATIVE_Y);
4100 } else {
4101 if (*motion_dir & MOTION_DIRECTION_POSITIVE_X)
4102 motion->b.x = border->line.a.x - wl_fixed_to_double(1);
4103 else
4104 motion->b.x = border->line.a.x;
4105 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_X |
4106 MOTION_DIRECTION_NEGATIVE_X);
4107 }
4108}
4109
4110static uint32_t
4111get_motion_directions(struct line *motion)
4112{
4113 uint32_t directions = 0;
4114
4115 if (motion->a.x < motion->b.x)
4116 directions |= MOTION_DIRECTION_POSITIVE_X;
4117 else if (motion->a.x > motion->b.x)
4118 directions |= MOTION_DIRECTION_NEGATIVE_X;
4119 if (motion->a.y < motion->b.y)
4120 directions |= MOTION_DIRECTION_POSITIVE_Y;
4121 else if (motion->a.y > motion->b.y)
4122 directions |= MOTION_DIRECTION_NEGATIVE_Y;
4123
4124 return directions;
4125}
4126
Jonas Ådahld3414f22016-07-22 17:56:31 +08004127static void
4128weston_pointer_clamp_event_to_region(struct weston_pointer *pointer,
4129 struct weston_pointer_motion_event *event,
4130 pixman_region32_t *region,
4131 wl_fixed_t *clamped_x,
4132 wl_fixed_t *clamped_y)
4133{
4134 wl_fixed_t x, y;
4135 wl_fixed_t sx, sy;
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004136 wl_fixed_t old_sx = pointer->sx;
4137 wl_fixed_t old_sy = pointer->sy;
4138 struct wl_array borders;
4139 struct line motion;
4140 struct border *closest_border;
4141 float new_x_f, new_y_f;
4142 uint32_t directions;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004143
4144 weston_pointer_motion_to_abs(pointer, event, &x, &y);
4145 weston_view_from_global_fixed(pointer->focus, x, y, &sx, &sy);
4146
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004147 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004148
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004149 /*
4150 * Generate borders given the confine region we are to use. The borders
4151 * are defined to be the outer region of the allowed area. This means
4152 * top/left borders are "within" the allowed area, while bottom/right
4153 * borders are outside. This needs to be considered when clamping
4154 * confined motion vectors.
4155 */
4156 region_to_outline(region, &borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004157
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004158 motion = (struct line) {
4159 .a = (struct vec2d) {
4160 .x = wl_fixed_to_double(old_sx),
4161 .y = wl_fixed_to_double(old_sy),
4162 },
4163 .b = (struct vec2d) {
4164 .x = wl_fixed_to_double(sx),
4165 .y = wl_fixed_to_double(sy),
4166 },
4167 };
4168 directions = get_motion_directions(&motion);
4169
4170 while (directions) {
4171 closest_border = get_closest_border(&borders,
4172 &motion,
4173 directions);
4174 if (closest_border)
4175 clamp_to_border(closest_border, &motion, &directions);
4176 else
4177 break;
4178 }
4179
4180 weston_view_to_global_float(pointer->focus,
4181 (float) motion.b.x, (float) motion.b.y,
4182 &new_x_f, &new_y_f);
4183 *clamped_x = wl_fixed_from_double(new_x_f);
4184 *clamped_y = wl_fixed_from_double(new_y_f);
4185
4186 wl_array_release(&borders);
4187}
4188
4189static double
4190point_to_border_distance_2(struct border *border, double x, double y)
4191{
4192 double orig_x, orig_y;
4193 double dx, dy;
4194
4195 if (is_border_horizontal(border)) {
4196 if (x < border->line.a.x)
4197 orig_x = border->line.a.x;
4198 else if (x > border->line.b.x)
4199 orig_x = border->line.b.x;
4200 else
4201 orig_x = x;
4202 orig_y = border->line.a.y;
4203 } else {
4204 if (y < border->line.a.y)
4205 orig_y = border->line.a.y;
4206 else if (y > border->line.b.y)
4207 orig_y = border->line.b.y;
4208 else
4209 orig_y = y;
4210 orig_x = border->line.a.x;
4211 }
4212
4213
4214 dx = fabs(orig_x - x);
4215 dy = fabs(orig_y - y);
4216 return dx*dx + dy*dy;
4217}
4218
4219static void
4220warp_to_behind_border(struct border *border, wl_fixed_t *sx, wl_fixed_t *sy)
4221{
4222 switch (border->blocking_dir) {
4223 case MOTION_DIRECTION_POSITIVE_X:
4224 case MOTION_DIRECTION_NEGATIVE_X:
4225 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_X)
4226 *sx = wl_fixed_from_double(border->line.a.x) - 1;
4227 else
4228 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4229 if (*sy < wl_fixed_from_double(border->line.a.y))
4230 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4231 else if (*sy > wl_fixed_from_double(border->line.b.y))
4232 *sy = wl_fixed_from_double(border->line.b.y) - 1;
4233 break;
4234 case MOTION_DIRECTION_POSITIVE_Y:
4235 case MOTION_DIRECTION_NEGATIVE_Y:
4236 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_Y)
4237 *sy = wl_fixed_from_double(border->line.a.y) - 1;
4238 else
4239 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4240 if (*sx < wl_fixed_from_double(border->line.a.x))
4241 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4242 else if (*sx > wl_fixed_from_double(border->line.b.x))
4243 *sx = wl_fixed_from_double(border->line.b.x) - 1;
4244 break;
4245 }
Jonas Ådahld3414f22016-07-22 17:56:31 +08004246}
4247
4248static void
4249maybe_warp_confined_pointer(struct weston_pointer_constraint *constraint)
4250{
4251 wl_fixed_t x;
4252 wl_fixed_t y;
4253 wl_fixed_t sx;
4254 wl_fixed_t sy;
4255
4256 weston_view_from_global_fixed(constraint->view,
4257 constraint->pointer->x,
4258 constraint->pointer->y,
4259 &sx,
4260 &sy);
4261
4262 if (!is_within_constraint_region(constraint, sx, sy)) {
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004263 double xf = wl_fixed_to_double(sx);
4264 double yf = wl_fixed_to_double(sy);
4265 pixman_region32_t confine_region;
4266 struct wl_array borders;
4267 struct border *border;
4268 double closest_distance_2 = DBL_MAX;
4269 struct border *closest_border = NULL;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004270
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004271 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004272
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004273 pixman_region32_init(&confine_region);
4274 pixman_region32_intersect(&confine_region,
4275 &constraint->view->surface->input,
4276 &constraint->region);
4277 region_to_outline(&confine_region, &borders);
4278 pixman_region32_fini(&confine_region);
4279
4280 wl_array_for_each(border, &borders) {
4281 double distance_2;
4282
4283 distance_2 = point_to_border_distance_2(border, xf, yf);
4284 if (distance_2 < closest_distance_2) {
4285 closest_border = border;
4286 closest_distance_2 = distance_2;
4287 }
4288 }
4289 assert(closest_border);
4290
4291 warp_to_behind_border(closest_border, &sx, &sy);
4292
4293 wl_array_release(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004294
4295 weston_view_to_global_fixed(constraint->view, sx, sy, &x, &y);
4296 weston_pointer_move_to(constraint->pointer, x, y);
4297 }
4298}
4299
4300static void
4301confined_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02004302 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004303 struct weston_pointer_motion_event *event)
4304{
4305 struct weston_pointer_constraint *constraint =
4306 container_of(grab, struct weston_pointer_constraint, grab);
4307 struct weston_pointer *pointer = grab->pointer;
4308 struct weston_surface *surface;
4309 wl_fixed_t x, y;
4310 wl_fixed_t old_sx = pointer->sx;
4311 wl_fixed_t old_sy = pointer->sy;
4312 pixman_region32_t confine_region;
4313
4314 assert(pointer->focus);
4315 assert(pointer->focus->surface == constraint->surface);
4316
4317 surface = pointer->focus->surface;
4318
4319 pixman_region32_init(&confine_region);
4320 pixman_region32_intersect(&confine_region,
4321 &surface->input,
4322 &constraint->region);
4323 weston_pointer_clamp_event_to_region(pointer, event,
4324 &confine_region, &x, &y);
4325 weston_pointer_move_to(pointer, x, y);
4326 pixman_region32_fini(&confine_region);
4327
4328 weston_view_from_global_fixed(pointer->focus, x, y,
4329 &pointer->sx, &pointer->sy);
4330
4331 if (old_sx != pointer->sx || old_sy != pointer->sy) {
Quentin Glidiccde13452016-08-12 10:41:32 +02004332 pointer_send_motion(pointer, time,
4333 pointer->sx, pointer->sy);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004334 }
4335
Quentin Glidiccde13452016-08-12 10:41:32 +02004336 pointer_send_relative_motion(pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004337}
4338
4339static void
4340confined_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02004341 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004342 uint32_t button,
4343 uint32_t state_w)
4344{
4345 weston_pointer_send_button(grab->pointer, time, button, state_w);
4346}
4347
4348static void
4349confined_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
4350 uint32_t time,
4351 struct weston_pointer_axis_event *event)
4352{
4353 weston_pointer_send_axis(grab->pointer, time, event);
4354}
4355
4356static void
4357confined_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
4358 uint32_t source)
4359{
4360 weston_pointer_send_axis_source(grab->pointer, source);
4361}
4362
4363static void
4364confined_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
4365{
4366 weston_pointer_send_frame(grab->pointer);
4367}
4368
4369static void
4370confined_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
4371{
4372 struct weston_pointer_constraint *constraint =
4373 container_of(grab, struct weston_pointer_constraint, grab);
4374
4375 disable_pointer_constraint(constraint);
4376
4377 /* If this is a persistent constraint, re-add the surface destroy signal
4378 * listener only if we are currently not destroying the surface. */
4379 switch (constraint->lifetime) {
4380 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT:
4381 if (constraint->surface->resource)
4382 wl_signal_add(&constraint->surface->destroy_signal,
4383 &constraint->surface_destroy_listener);
4384 break;
4385 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT:
4386 break;
4387 }
4388}
4389
4390static const struct weston_pointer_grab_interface
4391 confined_pointer_grab_interface = {
4392 confined_pointer_grab_pointer_focus,
4393 confined_pointer_grab_pointer_motion,
4394 confined_pointer_grab_pointer_button,
4395 confined_pointer_grab_pointer_axis,
4396 confined_pointer_grab_pointer_axis_source,
4397 confined_pointer_grab_pointer_frame,
4398 confined_pointer_grab_pointer_cancel,
4399};
4400
4401static void
4402confined_pointer_destroy(struct wl_client *client,
4403 struct wl_resource *resource)
4404{
4405 wl_resource_destroy(resource);
4406}
4407
4408static void
4409confined_pointer_set_region(struct wl_client *client,
4410 struct wl_resource *resource,
4411 struct wl_resource *region_resource)
4412{
4413 struct weston_pointer_constraint *constraint =
4414 wl_resource_get_user_data(resource);
4415 struct weston_region *region = region_resource ?
4416 wl_resource_get_user_data(region_resource) : NULL;
4417
4418 if (!constraint)
4419 return;
4420
4421 if (region) {
4422 pixman_region32_copy(&constraint->region_pending,
4423 &region->region);
4424 } else {
4425 pixman_region32_fini(&constraint->region_pending);
4426 region_init_infinite(&constraint->region_pending);
4427 }
4428 constraint->region_is_pending = true;
4429}
4430
4431static const struct zwp_confined_pointer_v1_interface confined_pointer_interface = {
4432 confined_pointer_destroy,
4433 confined_pointer_set_region,
4434};
4435
4436static void
4437pointer_constraints_confine_pointer(struct wl_client *client,
4438 struct wl_resource *resource,
4439 uint32_t id,
4440 struct wl_resource *surface_resource,
4441 struct wl_resource *pointer_resource,
4442 struct wl_resource *region_resource,
4443 uint32_t lifetime)
4444{
4445 struct weston_surface *surface =
4446 wl_resource_get_user_data(surface_resource);
4447 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
4448 struct weston_region *region = region_resource ?
4449 wl_resource_get_user_data(region_resource) : NULL;
4450
4451 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
4452 &zwp_confined_pointer_v1_interface,
4453 &confined_pointer_interface,
4454 &confined_pointer_grab_interface);
4455}
4456
4457static const struct zwp_pointer_constraints_v1_interface pointer_constraints_interface = {
4458 pointer_constraints_destroy,
4459 pointer_constraints_lock_pointer,
4460 pointer_constraints_confine_pointer,
4461};
4462
4463static void
4464bind_pointer_constraints(struct wl_client *client, void *data,
4465 uint32_t version, uint32_t id)
4466{
4467 struct wl_resource *resource;
4468
4469 resource = wl_resource_create(client,
4470 &zwp_pointer_constraints_v1_interface,
4471 1, id);
4472
4473 wl_resource_set_implementation(resource, &pointer_constraints_interface,
4474 NULL, NULL);
4475}
4476
Jonas Ådahl30d61d82014-10-22 21:21:17 +02004477int
4478weston_input_init(struct weston_compositor *compositor)
4479{
4480 if (!wl_global_create(compositor->wl_display,
4481 &zwp_relative_pointer_manager_v1_interface, 1,
4482 compositor, bind_relative_pointer_manager))
4483 return -1;
4484
Jonas Ådahld3414f22016-07-22 17:56:31 +08004485 if (!wl_global_create(compositor->wl_display,
4486 &zwp_pointer_constraints_v1_interface, 1,
4487 NULL, bind_pointer_constraints))
4488 return -1;
4489
Jonas Ådahl30d61d82014-10-22 21:21:17 +02004490 return 0;
4491}