blob: 996c00f759ab3e1f2a412ea0a79807e0e5678ad5 [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,
Alexandros Frantzis80321942017-11-16 18:20:56 +0200517 const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +1000518 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;
Alexandros Frantzis80321942017-11-16 18:20:56 +0200522 uint32_t msecs;
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200523
Quentin Glidiccde13452016-08-12 10:41:32 +0200524 if (!weston_pointer_has_focus_resource(pointer))
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800525 return;
526
527 resource_list = &pointer->focus_client->pointer_resources;
Alexandros Frantzis80321942017-11-16 18:20:56 +0200528 msecs = timespec_to_msec(time);
Peter Hutterer87743e92016-01-18 16:38:22 +1000529 wl_resource_for_each(resource, resource_list) {
530 if (event->has_discrete &&
531 wl_resource_get_version(resource) >=
532 WL_POINTER_AXIS_DISCRETE_SINCE_VERSION)
533 wl_pointer_send_axis_discrete(resource, event->axis,
534 event->discrete);
535
536 if (event->value)
Alexandros Frantzis80321942017-11-16 18:20:56 +0200537 wl_pointer_send_axis(resource, msecs,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200538 event->axis,
539 wl_fixed_from_double(event->value));
Peter Hutterer87743e92016-01-18 16:38:22 +1000540 else if (wl_resource_get_version(resource) >=
541 WL_POINTER_AXIS_STOP_SINCE_VERSION)
Alexandros Frantzis80321942017-11-16 18:20:56 +0200542 wl_pointer_send_axis_stop(resource, msecs,
Peter Hutterer87743e92016-01-18 16:38:22 +1000543 event->axis);
544 }
545}
546
Quentin Glidiccde13452016-08-12 10:41:32 +0200547/** Send wl_pointer.axis_source events to focused resources.
548 *
549 * \param pointer The pointer where the axis_source events originates from.
550 * \param source The axis_source enum value of the event
551 *
552 * For every resource that is currently in focus, send a wl_pointer.axis_source
553 * event with the passed parameter. The focused resources are the wl_pointer
554 * resources of the client which currently has the surface with pointer focus.
555 */
Peter Hutterer87743e92016-01-18 16:38:22 +1000556WL_EXPORT void
Quentin Glidiccde13452016-08-12 10:41:32 +0200557weston_pointer_send_axis_source(struct weston_pointer *pointer,
558 enum wl_pointer_axis_source source)
Peter Hutterer87743e92016-01-18 16:38:22 +1000559{
560 struct wl_resource *resource;
561 struct wl_list *resource_list;
562
Quentin Glidiccde13452016-08-12 10:41:32 +0200563 if (!weston_pointer_has_focus_resource(pointer))
Jonas Ådahled6014a2016-04-21 10:21:48 +0800564 return;
565
Peter Hutterer87743e92016-01-18 16:38:22 +1000566 resource_list = &pointer->focus_client->pointer_resources;
567 wl_resource_for_each(resource, resource_list) {
568 if (wl_resource_get_version(resource) >=
569 WL_POINTER_AXIS_SOURCE_SINCE_VERSION) {
570 wl_pointer_send_axis_source(resource, source);
571 }
572 }
573}
574
575static void
576pointer_send_frame(struct wl_resource *resource)
577{
578 if (wl_resource_get_version(resource) >=
579 WL_POINTER_FRAME_SINCE_VERSION) {
580 wl_pointer_send_frame(resource);
581 }
582}
583
Quentin Glidiccde13452016-08-12 10:41:32 +0200584/** Send wl_pointer.frame events to focused resources.
585 *
586 * \param pointer The pointer where the frame events originates from.
587 *
588 * For every resource that is currently in focus, send a wl_pointer.frame event.
589 * The focused resources are the wl_pointer resources of the client which
590 * currently has the surface with pointer focus.
591 */
Peter Hutterer87743e92016-01-18 16:38:22 +1000592WL_EXPORT void
593weston_pointer_send_frame(struct weston_pointer *pointer)
594{
595 struct wl_resource *resource;
596 struct wl_list *resource_list;
597
Quentin Glidiccde13452016-08-12 10:41:32 +0200598 if (!weston_pointer_has_focus_resource(pointer))
Derek Foreman8efa31b2016-01-29 10:29:46 -0600599 return;
600
Peter Hutterer87743e92016-01-18 16:38:22 +1000601 resource_list = &pointer->focus_client->pointer_resources;
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200602 wl_resource_for_each(resource, resource_list)
Peter Hutterer87743e92016-01-18 16:38:22 +1000603 pointer_send_frame(resource);
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200604}
605
606static void
607default_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +0200608 const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +1000609 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200610{
Peter Hutterer89b6a492016-01-18 15:58:17 +1000611 weston_pointer_send_axis(grab->pointer, time, event);
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200612}
613
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200614static void
Peter Hutterer87743e92016-01-18 16:38:22 +1000615default_grab_pointer_axis_source(struct weston_pointer_grab *grab,
Quentin Glidiccde13452016-08-12 10:41:32 +0200616 enum wl_pointer_axis_source source)
Peter Hutterer87743e92016-01-18 16:38:22 +1000617{
618 weston_pointer_send_axis_source(grab->pointer, source);
619}
620
621static void
622default_grab_pointer_frame(struct weston_pointer_grab *grab)
623{
624 weston_pointer_send_frame(grab->pointer);
625}
626
627static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200628default_grab_pointer_cancel(struct weston_pointer_grab *grab)
629{
630}
631
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400632static const struct weston_pointer_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400633 default_pointer_grab_interface = {
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700634 default_grab_pointer_focus,
635 default_grab_pointer_motion,
636 default_grab_pointer_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200637 default_grab_pointer_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +1000638 default_grab_pointer_axis_source,
639 default_grab_pointer_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200640 default_grab_pointer_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400641};
642
Quentin Glidiccde13452016-08-12 10:41:32 +0200643/** Check if the touch has focused resources.
644 *
645 * \param touch The touch to check for focused resources.
646 * \return Whether or not this touch has focused resources
647 */
648WL_EXPORT bool
649weston_touch_has_focus_resource(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400650{
Quentin Glidiccde13452016-08-12 10:41:32 +0200651 if (!touch->focus)
652 return false;
653
654 if (wl_list_empty(&touch->focus_resource_list))
655 return false;
656
657 return true;
658}
659
660/** Send wl_touch.down events to focused resources.
661 *
662 * \param touch The touch where the down events originates from.
663 * \param time The timestamp of the event
664 * \param touch_id The touch_id value of the event
665 * \param x The x value of the event
666 * \param y The y value of the event
667 *
668 * For every resource that is currently in focus, send a wl_touch.down event
669 * with the passed parameters. The focused resources are the wl_touch
670 * resources of the client which currently has the surface with touch focus.
671 */
672WL_EXPORT void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200673weston_touch_send_down(struct weston_touch *touch, const struct timespec *time,
Quentin Glidiccde13452016-08-12 10:41:32 +0200674 int touch_id, wl_fixed_t x, wl_fixed_t y)
675{
Rob Bradford880ebc72013-07-22 17:31:38 +0100676 struct wl_display *display = touch->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400677 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100678 struct wl_resource *resource;
679 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300680 wl_fixed_t sx, sy;
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200681 uint32_t msecs;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300682
Quentin Glidiccde13452016-08-12 10:41:32 +0200683 if (!weston_touch_has_focus_resource(touch))
Bryce Harrington2c40d1d2016-02-02 10:18:48 -0800684 return;
685
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300686 weston_view_from_global_fixed(touch->focus, x, y, &sx, &sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400687
Neil Roberts96d790e2013-09-19 17:32:00 +0100688 resource_list = &touch->focus_resource_list;
Quentin Glidiccde13452016-08-12 10:41:32 +0200689 serial = wl_display_next_serial(display);
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200690 msecs = timespec_to_msec(time);
Quentin Glidiccde13452016-08-12 10:41:32 +0200691 wl_resource_for_each(resource, resource_list)
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200692 wl_touch_send_down(resource, serial, msecs,
Quentin Glidiccde13452016-08-12 10:41:32 +0200693 touch->focus->surface->resource,
694 touch_id, sx, sy);
695}
Neil Roberts96d790e2013-09-19 17:32:00 +0100696
Quentin Glidiccde13452016-08-12 10:41:32 +0200697static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200698default_grab_touch_down(struct weston_touch_grab *grab,
699 const struct timespec *time, int touch_id,
700 wl_fixed_t x, wl_fixed_t y)
Quentin Glidiccde13452016-08-12 10:41:32 +0200701{
702 weston_touch_send_down(grab->touch, time, touch_id, x, y);
703}
704
705/** Send wl_touch.up events to focused resources.
706 *
707 * \param touch The touch where the up events originates from.
708 * \param time The timestamp of the event
709 * \param touch_id The touch_id value of the event
710 *
711 * For every resource that is currently in focus, send a wl_touch.up event
712 * with the passed parameters. The focused resources are the wl_touch
713 * resources of the client which currently has the surface with touch focus.
714 */
715WL_EXPORT void
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200716weston_touch_send_up(struct weston_touch *touch, const struct timespec *time,
717 int touch_id)
Quentin Glidiccde13452016-08-12 10:41:32 +0200718{
719 struct wl_display *display = touch->seat->compositor->wl_display;
720 uint32_t serial;
721 struct wl_resource *resource;
722 struct wl_list *resource_list;
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200723 uint32_t msecs;
Quentin Glidiccde13452016-08-12 10:41:32 +0200724
725 if (!weston_touch_has_focus_resource(touch))
726 return;
727
728 resource_list = &touch->focus_resource_list;
729 serial = wl_display_next_serial(display);
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200730 msecs = timespec_to_msec(time);
Quentin Glidiccde13452016-08-12 10:41:32 +0200731 wl_resource_for_each(resource, resource_list)
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200732 wl_touch_send_up(resource, serial, msecs, touch_id);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400733}
734
Kristian Høgsberge329f362013-05-06 22:19:57 -0400735static void
736default_grab_touch_up(struct weston_touch_grab *grab,
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200737 const struct timespec *time, int touch_id)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400738{
Quentin Glidiccde13452016-08-12 10:41:32 +0200739 weston_touch_send_up(grab->touch, time, touch_id);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400740}
741
Quentin Glidiccde13452016-08-12 10:41:32 +0200742/** Send wl_touch.motion events to focused resources.
743 *
744 * \param touch The touch where the motion events originates from.
745 * \param time The timestamp of the event
746 * \param touch_id The touch_id value of the event
747 * \param x The x value of the event
748 * \param y The y value of the event
749 *
750 * For every resource that is currently in focus, send a wl_touch.motion event
751 * with the passed parameters. The focused resources are the wl_touch
752 * resources of the client which currently has the surface with touch focus.
753 */
754WL_EXPORT void
755weston_touch_send_motion(struct weston_touch *touch, uint32_t time,
756 int touch_id, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400757{
Neil Roberts96d790e2013-09-19 17:32:00 +0100758 struct wl_resource *resource;
759 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300760 wl_fixed_t sx, sy;
761
Quentin Glidiccde13452016-08-12 10:41:32 +0200762 if (!weston_touch_has_focus_resource(touch))
763 return;
764
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300765 weston_view_from_global_fixed(touch->focus, x, y, &sx, &sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400766
Neil Roberts96d790e2013-09-19 17:32:00 +0100767 resource_list = &touch->focus_resource_list;
Neil Roberts96d790e2013-09-19 17:32:00 +0100768 wl_resource_for_each(resource, resource_list) {
769 wl_touch_send_motion(resource, time,
Kristian Høgsberge329f362013-05-06 22:19:57 -0400770 touch_id, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400771 }
772}
773
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200774static void
Quentin Glidiccde13452016-08-12 10:41:32 +0200775default_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time,
776 int touch_id, wl_fixed_t x, wl_fixed_t y)
777{
778 weston_touch_send_motion(grab->touch, time, touch_id, x, y);
779}
780
781
782/** Send wl_touch.frame events to focused resources.
783 *
784 * \param touch The touch where the frame events originates from.
785 *
786 * For every resource that is currently in focus, send a wl_touch.frame event.
787 * The focused resources are the wl_touch resources of the client which
788 * currently has the surface with touch focus.
789 */
790WL_EXPORT void
791weston_touch_send_frame(struct weston_touch *touch)
Jonas Ådahl1679f232014-04-12 09:39:51 +0200792{
793 struct wl_resource *resource;
794
Quentin Glidiccde13452016-08-12 10:41:32 +0200795 if (!weston_touch_has_focus_resource(touch))
796 return;
797
798 wl_resource_for_each(resource, &touch->focus_resource_list)
Jonas Ådahl1679f232014-04-12 09:39:51 +0200799 wl_touch_send_frame(resource);
800}
801
802static void
Quentin Glidiccde13452016-08-12 10:41:32 +0200803default_grab_touch_frame(struct weston_touch_grab *grab)
804{
805 weston_touch_send_frame(grab->touch);
806}
807
808static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200809default_grab_touch_cancel(struct weston_touch_grab *grab)
810{
811}
812
Kristian Høgsberge329f362013-05-06 22:19:57 -0400813static const struct weston_touch_grab_interface default_touch_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400814 default_grab_touch_down,
815 default_grab_touch_up,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200816 default_grab_touch_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +0200817 default_grab_touch_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200818 default_grab_touch_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400819};
820
Quentin Glidiccde13452016-08-12 10:41:32 +0200821/** Check if the keyboard has focused resources.
822 *
823 * \param keyboard The keyboard to check for focused resources.
824 * \return Whether or not this keyboard has focused resources
825 */
826WL_EXPORT bool
827weston_keyboard_has_focus_resource(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400828{
Quentin Glidiccde13452016-08-12 10:41:32 +0200829 if (!keyboard->focus)
830 return false;
831
832 if (wl_list_empty(&keyboard->focus_resource_list))
833 return false;
834
835 return true;
836}
837
838/** Send wl_keyboard.key events to focused resources.
839 *
840 * \param keyboard The keyboard where the key events originates from.
841 * \param time The timestamp of the event
842 * \param key The key value of the event
843 * \param state The state enum value of the event
844 *
845 * For every resource that is currently in focus, send a wl_keyboard.key event
846 * with the passed parameters. The focused resources are the wl_keyboard
847 * resources of the client which currently has the surface with keyboard focus.
848 */
849WL_EXPORT void
850weston_keyboard_send_key(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200851 const struct timespec *time, uint32_t key,
Quentin Glidiccde13452016-08-12 10:41:32 +0200852 enum wl_keyboard_key_state state)
853{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400854 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +0100855 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400856 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100857 struct wl_list *resource_list;
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200858 uint32_t msecs;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400859
Quentin Glidiccde13452016-08-12 10:41:32 +0200860 if (!weston_keyboard_has_focus_resource(keyboard))
861 return;
862
Neil Roberts96d790e2013-09-19 17:32:00 +0100863 resource_list = &keyboard->focus_resource_list;
Quentin Glidiccde13452016-08-12 10:41:32 +0200864 serial = wl_display_next_serial(display);
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200865 msecs = timespec_to_msec(time);
Quentin Glidiccde13452016-08-12 10:41:32 +0200866 wl_resource_for_each(resource, resource_list)
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200867 wl_keyboard_send_key(resource, serial, msecs, key, state);
Quentin Glidiccde13452016-08-12 10:41:32 +0200868};
869
870static void
871default_grab_keyboard_key(struct weston_keyboard_grab *grab,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200872 const struct timespec *time, uint32_t key,
873 uint32_t state)
Quentin Glidiccde13452016-08-12 10:41:32 +0200874{
875 weston_keyboard_send_key(grab->keyboard, time, key, state);
Neil Roberts96d790e2013-09-19 17:32:00 +0100876}
877
878static void
879send_modifiers_to_resource(struct weston_keyboard *keyboard,
880 struct wl_resource *resource,
881 uint32_t serial)
882{
883 wl_keyboard_send_modifiers(resource,
884 serial,
885 keyboard->modifiers.mods_depressed,
886 keyboard->modifiers.mods_latched,
887 keyboard->modifiers.mods_locked,
888 keyboard->modifiers.group);
889}
890
891static void
892send_modifiers_to_client_in_list(struct wl_client *client,
893 struct wl_list *list,
894 uint32_t serial,
895 struct weston_keyboard *keyboard)
896{
897 struct wl_resource *resource;
898
899 wl_resource_for_each(resource, list) {
900 if (wl_resource_get_client(resource) == client)
901 send_modifiers_to_resource(keyboard,
902 resource,
903 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400904 }
905}
906
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800907static struct weston_pointer_client *
908find_pointer_client_for_surface(struct weston_pointer *pointer,
909 struct weston_surface *surface)
910{
911 struct wl_client *client;
912
913 if (!surface)
914 return NULL;
915
916 if (!surface->resource)
917 return NULL;
918
919 client = wl_resource_get_client(surface->resource);
920 return weston_pointer_get_pointer_client(pointer, client);
921}
922
923static struct weston_pointer_client *
924find_pointer_client_for_view(struct weston_pointer *pointer, struct weston_view *view)
925{
926 if (!view)
927 return NULL;
928
929 return find_pointer_client_for_surface(pointer, view->surface);
930}
931
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400932static struct wl_resource *
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400933find_resource_for_surface(struct wl_list *list, struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400934{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400935 if (!surface)
936 return NULL;
937
Jason Ekstrand44a38632013-06-14 10:08:00 -0500938 if (!surface->resource)
939 return NULL;
Stefan Schmidtfda26522013-09-17 10:54:09 +0100940
Jason Ekstrand44a38632013-06-14 10:08:00 -0500941 return wl_resource_find_for_client(list, wl_resource_get_client(surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400942}
943
Quentin Glidiccde13452016-08-12 10:41:32 +0200944/** Send wl_keyboard.modifiers events to focused resources and pointer
945 * focused resources.
946 *
947 * \param keyboard The keyboard where the modifiers events originates from.
948 * \param serial The serial of the event
949 * \param mods_depressed The mods_depressed value of the event
950 * \param mods_latched The mods_latched value of the event
951 * \param mods_locked The mods_locked value of the event
952 * \param group The group value of the event
953 *
954 * For every resource that is currently in focus, send a wl_keyboard.modifiers
955 * event with the passed parameters. The focused resources are the wl_keyboard
956 * resources of the client which currently has the surface with keyboard focus.
957 * This also sends wl_keyboard.modifiers events to the wl_keyboard resources of
958 * the client having pointer focus (if different from the keyboard focus client).
959 */
960WL_EXPORT void
961weston_keyboard_send_modifiers(struct weston_keyboard *keyboard,
962 uint32_t serial, uint32_t mods_depressed,
963 uint32_t mods_latched,
964 uint32_t mods_locked, uint32_t group)
965{
966 struct weston_pointer *pointer =
967 weston_seat_get_pointer(keyboard->seat);
968
969 if (weston_keyboard_has_focus_resource(keyboard)) {
970 struct wl_list *resource_list;
971 struct wl_resource *resource;
972
973 resource_list = &keyboard->focus_resource_list;
974 wl_resource_for_each(resource, resource_list) {
975 wl_keyboard_send_modifiers(resource, serial,
976 mods_depressed, mods_latched,
977 mods_locked, group);
978 }
979 }
980
981 if (pointer && pointer->focus && pointer->focus->surface->resource &&
982 pointer->focus->surface != keyboard->focus) {
983 struct wl_client *pointer_client =
984 wl_resource_get_client(pointer->focus->surface->resource);
985
986 send_modifiers_to_client_in_list(pointer_client,
987 &keyboard->resource_list,
988 serial,
989 keyboard);
990 }
991}
992
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400993static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700994default_grab_keyboard_modifiers(struct weston_keyboard_grab *grab,
995 uint32_t serial, uint32_t mods_depressed,
996 uint32_t mods_latched,
997 uint32_t mods_locked, uint32_t group)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400998{
Quentin Glidiccde13452016-08-12 10:41:32 +0200999 weston_keyboard_send_modifiers(grab->keyboard, serial, mods_depressed,
1000 mods_latched, mods_locked, group);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001001}
1002
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001003static void
1004default_grab_keyboard_cancel(struct weston_keyboard_grab *grab)
1005{
1006}
1007
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001008static const struct weston_keyboard_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001009 default_keyboard_grab_interface = {
Kristian Høgsbergb27901c2013-10-28 15:32:02 -07001010 default_grab_keyboard_key,
1011 default_grab_keyboard_modifiers,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001012 default_grab_keyboard_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001013};
1014
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001015static void
1016pointer_unmap_sprite(struct weston_pointer *pointer)
1017{
Pekka Paalanenc557ff72014-11-12 16:42:52 +02001018 struct weston_surface *surface = pointer->sprite->surface;
1019
1020 if (weston_surface_is_mapped(surface))
1021 weston_surface_unmap(surface);
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001022
1023 wl_list_remove(&pointer->sprite_destroy_listener.link);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02001024 surface->committed = NULL;
1025 surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03001026 weston_surface_set_label_func(surface, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001027 weston_view_destroy(pointer->sprite);
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001028 pointer->sprite = NULL;
1029}
1030
1031static void
1032pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
1033{
1034 struct weston_pointer *pointer =
1035 container_of(listener, struct weston_pointer,
1036 sprite_destroy_listener);
1037
1038 pointer->sprite = NULL;
1039}
1040
Jonas Ådahl3e12e632013-12-02 22:05:05 +01001041static void
1042weston_pointer_reset_state(struct weston_pointer *pointer)
1043{
1044 pointer->button_count = 0;
1045}
1046
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001047static void
1048weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data);
1049
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001050WL_EXPORT struct weston_pointer *
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001051weston_pointer_create(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001052{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001053 struct weston_pointer *pointer;
1054
Peter Huttererf3d62272013-08-08 11:57:05 +10001055 pointer = zalloc(sizeof *pointer);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001056 if (pointer == NULL)
1057 return NULL;
1058
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001059 wl_list_init(&pointer->pointer_clients);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001060 weston_pointer_set_default_grab(pointer,
1061 seat->compositor->default_pointer_grab);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001062 wl_list_init(&pointer->focus_resource_listener.link);
1063 pointer->focus_resource_listener.notify = pointer_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001064 pointer->default_grab.pointer = pointer;
1065 pointer->grab = &pointer->default_grab;
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001066 wl_signal_init(&pointer->motion_signal);
Emilio Pozuelo Monfortaa7a4762013-11-19 11:37:15 +01001067 wl_signal_init(&pointer->focus_signal);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001068 wl_list_init(&pointer->focus_view_listener.link);
Jonas Ådahl3eb4ddd2016-07-22 17:52:58 +08001069 wl_signal_init(&pointer->destroy_signal);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001070
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001071 pointer->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
1072
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001073 /* FIXME: Pick better co-ords. */
1074 pointer->x = wl_fixed_from_int(100);
1075 pointer->y = wl_fixed_from_int(100);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001076
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001077 pointer->output_destroy_listener.notify =
1078 weston_pointer_handle_output_destroy;
1079 wl_signal_add(&seat->compositor->output_destroyed_signal,
1080 &pointer->output_destroy_listener);
1081
Derek Foremanf9318d12015-05-11 15:40:11 -05001082 pointer->sx = wl_fixed_from_int(-1000000);
1083 pointer->sy = wl_fixed_from_int(-1000000);
1084
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001085 return pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001086}
1087
1088WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001089weston_pointer_destroy(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001090{
Jonas Ådahl3eb4ddd2016-07-22 17:52:58 +08001091 wl_signal_emit(&pointer->destroy_signal, pointer);
1092
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001093 if (pointer->sprite)
1094 pointer_unmap_sprite(pointer);
1095
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001096 /* XXX: What about pointer->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +01001097
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001098 wl_list_remove(&pointer->focus_resource_listener.link);
1099 wl_list_remove(&pointer->focus_view_listener.link);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001100 wl_list_remove(&pointer->output_destroy_listener.link);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001101 free(pointer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001102}
1103
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001104void
1105weston_pointer_set_default_grab(struct weston_pointer *pointer,
1106 const struct weston_pointer_grab_interface *interface)
1107{
1108 if (interface)
1109 pointer->default_grab.interface = interface;
1110 else
1111 pointer->default_grab.interface =
1112 &default_pointer_grab_interface;
1113}
1114
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001115WL_EXPORT struct weston_keyboard *
1116weston_keyboard_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001117{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001118 struct weston_keyboard *keyboard;
1119
Peter Huttererf3d62272013-08-08 11:57:05 +10001120 keyboard = zalloc(sizeof *keyboard);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001121 if (keyboard == NULL)
Neil Roberts96d790e2013-09-19 17:32:00 +01001122 return NULL;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001123
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001124 wl_list_init(&keyboard->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +01001125 wl_list_init(&keyboard->focus_resource_list);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001126 wl_list_init(&keyboard->focus_resource_listener.link);
1127 keyboard->focus_resource_listener.notify = keyboard_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001128 wl_array_init(&keyboard->keys);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001129 keyboard->default_grab.interface = &default_keyboard_grab_interface;
1130 keyboard->default_grab.keyboard = keyboard;
1131 keyboard->grab = &keyboard->default_grab;
1132 wl_signal_init(&keyboard->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001133
1134 return keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001135}
1136
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001137static void
1138weston_xkb_info_destroy(struct weston_xkb_info *xkb_info);
1139
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001140WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001141weston_keyboard_destroy(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001142{
1143 /* XXX: What about keyboard->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +01001144
Derek Foreman185d1582017-06-28 11:17:23 -05001145 xkb_state_unref(keyboard->xkb_state.state);
1146 if (keyboard->xkb_info)
1147 weston_xkb_info_destroy(keyboard->xkb_info);
1148 xkb_keymap_unref(keyboard->pending_keymap);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001149
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001150 wl_array_release(&keyboard->keys);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001151 wl_list_remove(&keyboard->focus_resource_listener.link);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001152 free(keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001153}
1154
Jonas Ådahlcbfa7f72013-12-02 22:05:04 +01001155static void
1156weston_touch_reset_state(struct weston_touch *touch)
1157{
1158 touch->num_tp = 0;
1159}
1160
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001161WL_EXPORT struct weston_touch *
1162weston_touch_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001163{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001164 struct weston_touch *touch;
1165
Peter Huttererf3d62272013-08-08 11:57:05 +10001166 touch = zalloc(sizeof *touch);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001167 if (touch == NULL)
1168 return NULL;
1169
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001170 wl_list_init(&touch->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +01001171 wl_list_init(&touch->focus_resource_list);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001172 wl_list_init(&touch->focus_view_listener.link);
1173 touch->focus_view_listener.notify = touch_focus_view_destroyed;
1174 wl_list_init(&touch->focus_resource_listener.link);
1175 touch->focus_resource_listener.notify = touch_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001176 touch->default_grab.interface = &default_touch_grab_interface;
1177 touch->default_grab.touch = touch;
1178 touch->grab = &touch->default_grab;
1179 wl_signal_init(&touch->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001180
1181 return touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001182}
1183
1184WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001185weston_touch_destroy(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001186{
1187 /* XXX: What about touch->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +01001188
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001189 wl_list_remove(&touch->focus_view_listener.link);
1190 wl_list_remove(&touch->focus_resource_listener.link);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001191 free(touch);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001192}
1193
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001194static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04001195seat_send_updated_caps(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001196{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001197 enum wl_seat_capability caps = 0;
Rob Bradford6e737f52013-09-06 17:48:19 +01001198 struct wl_resource *resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001199
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001200 if (seat->pointer_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001201 caps |= WL_SEAT_CAPABILITY_POINTER;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001202 if (seat->keyboard_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001203 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001204 if (seat->touch_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001205 caps |= WL_SEAT_CAPABILITY_TOUCH;
1206
Rob Bradford6e737f52013-09-06 17:48:19 +01001207 wl_resource_for_each(resource, &seat->base_resource_list) {
1208 wl_seat_send_capabilities(resource, caps);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001209 }
Jason Ekstranda4ab5422014-04-02 19:53:45 -05001210 wl_signal_emit(&seat->updated_caps_signal, seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001211}
1212
Derek Foremanf9318d12015-05-11 15:40:11 -05001213
1214/** Clear the pointer focus
1215 *
1216 * \param pointer the pointer to clear focus for.
1217 *
1218 * This can be used to unset pointer focus and set the co-ordinates to the
1219 * arbitrary values we use for the no focus case.
1220 *
1221 * There's no requirement to use this function. For example, passing the
1222 * results of a weston_compositor_pick_view() directly to
1223 * weston_pointer_set_focus() will do the right thing when no view is found.
1224 */
1225WL_EXPORT void
1226weston_pointer_clear_focus(struct weston_pointer *pointer)
1227{
1228 weston_pointer_set_focus(pointer, NULL,
1229 wl_fixed_from_int(-1000000),
1230 wl_fixed_from_int(-1000000));
1231}
1232
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001233WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001234weston_pointer_set_focus(struct weston_pointer *pointer,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001235 struct weston_view *view,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001236 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001237{
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001238 struct weston_pointer_client *pointer_client;
Derek Foreman1281a362015-07-31 16:55:32 -05001239 struct weston_keyboard *kbd = weston_seat_get_keyboard(pointer->seat);
Neil Roberts96d790e2013-09-19 17:32:00 +01001240 struct wl_resource *resource;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001241 struct wl_resource *surface_resource;
Rob Bradford880ebc72013-07-22 17:31:38 +01001242 struct wl_display *display = pointer->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001243 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01001244 struct wl_list *focus_resource_list;
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001245 int refocus = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001246
1247 if ((!pointer->focus && view) ||
1248 (pointer->focus && !view) ||
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001249 (pointer->focus && pointer->focus->surface != view->surface) ||
1250 pointer->sx != sx || pointer->sy != sy)
1251 refocus = 1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001252
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001253 if (pointer->focus_client && refocus) {
1254 focus_resource_list = &pointer->focus_client->pointer_resources;
1255 if (!wl_list_empty(focus_resource_list)) {
1256 serial = wl_display_next_serial(display);
1257 surface_resource = pointer->focus->surface->resource;
1258 wl_resource_for_each(resource, focus_resource_list) {
1259 wl_pointer_send_leave(resource, serial,
1260 surface_resource);
Peter Hutterer87743e92016-01-18 16:38:22 +10001261 pointer_send_frame(resource);
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001262 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001263 }
1264
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001265 pointer->focus_client = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001266 }
1267
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001268 pointer_client = find_pointer_client_for_view(pointer, view);
1269 if (pointer_client && refocus) {
1270 struct wl_client *surface_client = pointer_client->client;
Neil Roberts96d790e2013-09-19 17:32:00 +01001271
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001272 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001273
Jason Ekstranda7af7042013-10-12 22:38:11 -05001274 if (kbd && kbd->focus != view->surface)
Kristian Høgsbergcb406f12013-10-09 10:54:03 -07001275 send_modifiers_to_client_in_list(surface_client,
1276 &kbd->resource_list,
1277 serial,
1278 kbd);
1279
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001280 pointer->focus_client = pointer_client;
Neil Roberts96d790e2013-09-19 17:32:00 +01001281
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001282 focus_resource_list = &pointer->focus_client->pointer_resources;
Neil Roberts96d790e2013-09-19 17:32:00 +01001283 wl_resource_for_each(resource, focus_resource_list) {
1284 wl_pointer_send_enter(resource,
1285 serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001286 view->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01001287 sx, sy);
Peter Hutterer87743e92016-01-18 16:38:22 +10001288 pointer_send_frame(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001289 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001290
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001291 pointer->focus_serial = serial;
1292 }
1293
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001294 wl_list_remove(&pointer->focus_view_listener.link);
1295 wl_list_init(&pointer->focus_view_listener.link);
1296 wl_list_remove(&pointer->focus_resource_listener.link);
1297 wl_list_init(&pointer->focus_resource_listener.link);
Emilio Pozuelo Monfortaa7a4762013-11-19 11:37:15 +01001298 if (view)
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001299 wl_signal_add(&view->destroy_signal, &pointer->focus_view_listener);
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001300 if (view && view->surface->resource)
1301 wl_resource_add_destroy_listener(view->surface->resource,
1302 &pointer->focus_resource_listener);
1303
1304 pointer->focus = view;
1305 pointer->focus_view_listener.notify = pointer_focus_view_destroyed;
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001306 pointer->sx = sx;
1307 pointer->sy = sy;
1308
Derek Foremanf9318d12015-05-11 15:40:11 -05001309 assert(view || sx == wl_fixed_from_int(-1000000));
1310 assert(view || sy == wl_fixed_from_int(-1000000));
1311
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001312 wl_signal_emit(&pointer->focus_signal, pointer);
1313}
1314
Neil Roberts96d790e2013-09-19 17:32:00 +01001315static void
1316send_enter_to_resource_list(struct wl_list *list,
1317 struct weston_keyboard *keyboard,
1318 struct weston_surface *surface,
1319 uint32_t serial)
1320{
1321 struct wl_resource *resource;
1322
1323 wl_resource_for_each(resource, list) {
1324 send_modifiers_to_resource(keyboard, resource, serial);
1325 wl_keyboard_send_enter(resource, serial,
1326 surface->resource,
1327 &keyboard->keys);
1328 }
1329}
1330
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001331WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001332weston_keyboard_set_focus(struct weston_keyboard *keyboard,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001333 struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001334{
1335 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +01001336 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001337 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01001338 struct wl_list *focus_resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001339
Neil Roberts96d790e2013-09-19 17:32:00 +01001340 focus_resource_list = &keyboard->focus_resource_list;
1341
1342 if (!wl_list_empty(focus_resource_list) && keyboard->focus != surface) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001343 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001344 wl_resource_for_each(resource, focus_resource_list) {
1345 wl_keyboard_send_leave(resource, serial,
1346 keyboard->focus->resource);
1347 }
1348 move_resources(&keyboard->resource_list, focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001349 }
1350
Neil Roberts96d790e2013-09-19 17:32:00 +01001351 if (find_resource_for_surface(&keyboard->resource_list, surface) &&
1352 keyboard->focus != surface) {
1353 struct wl_client *surface_client =
1354 wl_resource_get_client(surface->resource);
1355
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001356 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001357
1358 move_resources_for_client(focus_resource_list,
1359 &keyboard->resource_list,
1360 surface_client);
1361 send_enter_to_resource_list(focus_resource_list,
1362 keyboard,
1363 surface,
1364 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001365 keyboard->focus_serial = serial;
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001366 }
1367
1368 wl_list_remove(&keyboard->focus_resource_listener.link);
1369 wl_list_init(&keyboard->focus_resource_listener.link);
1370 if (surface && surface->resource)
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001371 wl_resource_add_destroy_listener(surface->resource,
1372 &keyboard->focus_resource_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001373
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001374 keyboard->focus = surface;
1375 wl_signal_emit(&keyboard->focus_signal, keyboard);
1376}
1377
Giulio Camuffoa20ca812014-11-22 11:16:56 +02001378/* Users of this function must manually manage the keyboard focus */
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001379WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001380weston_keyboard_start_grab(struct weston_keyboard *keyboard,
1381 struct weston_keyboard_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001382{
1383 keyboard->grab = grab;
1384 grab->keyboard = keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001385}
1386
1387WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001388weston_keyboard_end_grab(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001389{
1390 keyboard->grab = &keyboard->default_grab;
1391}
1392
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001393static void
1394weston_keyboard_cancel_grab(struct weston_keyboard *keyboard)
1395{
1396 keyboard->grab->interface->cancel(keyboard->grab);
1397}
1398
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001399WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001400weston_pointer_start_grab(struct weston_pointer *pointer,
1401 struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001402{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001403 pointer->grab = grab;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001404 grab->pointer = pointer;
Kristian Høgsbergda751b82013-07-04 00:58:07 -04001405 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001406}
1407
1408WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001409weston_pointer_end_grab(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001410{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001411 pointer->grab = &pointer->default_grab;
Kristian Høgsbergda751b82013-07-04 00:58:07 -04001412 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001413}
1414
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001415static void
1416weston_pointer_cancel_grab(struct weston_pointer *pointer)
1417{
1418 pointer->grab->interface->cancel(pointer->grab);
1419}
1420
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001421WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -04001422weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001423{
1424 touch->grab = grab;
1425 grab->touch = touch;
1426}
1427
1428WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -04001429weston_touch_end_grab(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001430{
1431 touch->grab = &touch->default_grab;
1432}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001433
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001434static void
1435weston_touch_cancel_grab(struct weston_touch *touch)
1436{
1437 touch->grab->interface->cancel(touch->grab);
1438}
1439
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001440static void
1441weston_pointer_clamp_for_output(struct weston_pointer *pointer,
1442 struct weston_output *output,
1443 wl_fixed_t *fx, wl_fixed_t *fy)
1444{
1445 int x, y;
1446
1447 x = wl_fixed_to_int(*fx);
1448 y = wl_fixed_to_int(*fy);
1449
1450 if (x < output->x)
1451 *fx = wl_fixed_from_int(output->x);
1452 else if (x >= output->x + output->width)
1453 *fx = wl_fixed_from_int(output->x +
1454 output->width - 1);
1455 if (y < output->y)
1456 *fy = wl_fixed_from_int(output->y);
1457 else if (y >= output->y + output->height)
1458 *fy = wl_fixed_from_int(output->y +
1459 output->height - 1);
1460}
1461
Rob Bradford806d8c02013-06-25 18:56:41 +01001462WL_EXPORT void
1463weston_pointer_clamp(struct weston_pointer *pointer, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001464{
Rob Bradford806d8c02013-06-25 18:56:41 +01001465 struct weston_compositor *ec = pointer->seat->compositor;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001466 struct weston_output *output, *prev = NULL;
1467 int x, y, old_x, old_y, valid = 0;
1468
1469 x = wl_fixed_to_int(*fx);
1470 y = wl_fixed_to_int(*fy);
Rob Bradford806d8c02013-06-25 18:56:41 +01001471 old_x = wl_fixed_to_int(pointer->x);
1472 old_y = wl_fixed_to_int(pointer->y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001473
1474 wl_list_for_each(output, &ec->output_list, link) {
Rob Bradford66bd9f52013-06-25 18:56:42 +01001475 if (pointer->seat->output && pointer->seat->output != output)
1476 continue;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001477 if (pixman_region32_contains_point(&output->region,
1478 x, y, NULL))
1479 valid = 1;
1480 if (pixman_region32_contains_point(&output->region,
1481 old_x, old_y, NULL))
1482 prev = output;
1483 }
1484
Rob Bradford66bd9f52013-06-25 18:56:42 +01001485 if (!prev)
1486 prev = pointer->seat->output;
1487
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001488 if (prev && !valid)
1489 weston_pointer_clamp_for_output(pointer, prev, fx, fy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001490}
1491
Jonas Ådahld2510102014-10-05 21:39:14 +02001492static void
1493weston_pointer_move_to(struct weston_pointer *pointer,
1494 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001495{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001496 int32_t ix, iy;
1497
Rob Bradford806d8c02013-06-25 18:56:41 +01001498 weston_pointer_clamp (pointer, &x, &y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001499
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001500 pointer->x = x;
1501 pointer->y = y;
1502
1503 ix = wl_fixed_to_int(x);
1504 iy = wl_fixed_to_int(y);
1505
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001506 if (pointer->sprite) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001507 weston_view_set_position(pointer->sprite,
1508 ix - pointer->hotspot_x,
1509 iy - pointer->hotspot_y);
1510 weston_view_schedule_repaint(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001511 }
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001512
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001513 pointer->grab->interface->focus(pointer->grab);
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001514 wl_signal_emit(&pointer->motion_signal, pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001515}
1516
Jonas Ådahld2510102014-10-05 21:39:14 +02001517WL_EXPORT void
Jonas Ådahld2510102014-10-05 21:39:14 +02001518weston_pointer_move(struct weston_pointer *pointer,
1519 struct weston_pointer_motion_event *event)
1520{
1521 wl_fixed_t x, y;
1522
1523 weston_pointer_motion_to_abs(pointer, event, &x, &y);
1524 weston_pointer_move_to(pointer, x, y);
1525}
1526
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001527/** Verify if the pointer is in a valid position and move it if it isn't.
1528 */
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001529static void
1530weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001531{
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001532 struct weston_pointer *pointer;
1533 struct weston_compositor *ec;
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001534 struct weston_output *output, *closest = NULL;
1535 int x, y, distance, min = INT_MAX;
1536 wl_fixed_t fx, fy;
1537
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001538 pointer = container_of(listener, struct weston_pointer,
1539 output_destroy_listener);
1540 ec = pointer->seat->compositor;
1541
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001542 x = wl_fixed_to_int(pointer->x);
1543 y = wl_fixed_to_int(pointer->y);
1544
1545 wl_list_for_each(output, &ec->output_list, link) {
1546 if (pixman_region32_contains_point(&output->region,
1547 x, y, NULL))
1548 return;
1549
1550 /* Aproximante the distance from the pointer to the center of
1551 * the output. */
1552 distance = abs(output->x + output->width / 2 - x) +
1553 abs(output->y + output->height / 2 - y);
1554 if (distance < min) {
1555 min = distance;
1556 closest = output;
1557 }
1558 }
1559
1560 /* Nothing to do if there's no output left. */
1561 if (!closest)
1562 return;
1563
1564 fx = pointer->x;
1565 fy = pointer->y;
1566
1567 weston_pointer_clamp_for_output(pointer, closest, &fx, &fy);
Jonas Ådahld2510102014-10-05 21:39:14 +02001568 weston_pointer_move_to(pointer, fx, fy);
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001569}
1570
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001571WL_EXPORT void
1572notify_motion(struct weston_seat *seat,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001573 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001574 struct weston_pointer_motion_event *event)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001575{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001576 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001577 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001578
1579 weston_compositor_wake(ec);
Jonas Ådahld2510102014-10-05 21:39:14 +02001580 pointer->grab->interface->motion(pointer->grab, time, event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001581}
1582
Daniel Stone96d47c02013-11-19 11:37:12 +01001583static void
1584run_modifier_bindings(struct weston_seat *seat, uint32_t old, uint32_t new)
1585{
1586 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001587 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Daniel Stone96d47c02013-11-19 11:37:12 +01001588 uint32_t diff;
1589 unsigned int i;
1590 struct {
1591 uint32_t xkb;
1592 enum weston_keyboard_modifier weston;
1593 } mods[] = {
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001594 { keyboard->xkb_info->ctrl_mod, MODIFIER_CTRL },
1595 { keyboard->xkb_info->alt_mod, MODIFIER_ALT },
1596 { keyboard->xkb_info->super_mod, MODIFIER_SUPER },
1597 { keyboard->xkb_info->shift_mod, MODIFIER_SHIFT },
Daniel Stone96d47c02013-11-19 11:37:12 +01001598 };
1599
1600 diff = new & ~old;
1601 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
1602 if (diff & (1 << mods[i].xkb))
1603 weston_compositor_run_modifier_binding(compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001604 keyboard,
Daniel Stone96d47c02013-11-19 11:37:12 +01001605 mods[i].weston,
1606 WL_KEYBOARD_KEY_STATE_PRESSED);
1607 }
1608
1609 diff = old & ~new;
1610 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
1611 if (diff & (1 << mods[i].xkb))
1612 weston_compositor_run_modifier_binding(compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001613 keyboard,
Daniel Stone96d47c02013-11-19 11:37:12 +01001614 mods[i].weston,
1615 WL_KEYBOARD_KEY_STATE_RELEASED);
1616 }
1617}
1618
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001619WL_EXPORT void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001620notify_motion_absolute(struct weston_seat *seat, const struct timespec *time,
1621 double x, double y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001622{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001623 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001624 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Jonas Ådahld2510102014-10-05 21:39:14 +02001625 struct weston_pointer_motion_event event = { 0 };
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001626
1627 weston_compositor_wake(ec);
Jonas Ådahld2510102014-10-05 21:39:14 +02001628
1629 event = (struct weston_pointer_motion_event) {
1630 .mask = WESTON_POINTER_MOTION_ABS,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001631 .x = x,
1632 .y = y,
Jonas Ådahld2510102014-10-05 21:39:14 +02001633 };
1634
1635 pointer->grab->interface->motion(pointer->grab, time, &event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001636}
1637
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02001638static unsigned int
1639peek_next_activate_serial(struct weston_compositor *c)
1640{
1641 unsigned serial = c->activate_serial + 1;
1642
1643 return serial == 0 ? 1 : serial;
1644}
1645
1646static void
1647inc_activate_serial(struct weston_compositor *c)
1648{
1649 c->activate_serial = peek_next_activate_serial (c);
1650}
1651
1652WL_EXPORT void
1653weston_view_activate(struct weston_view *view,
1654 struct weston_seat *seat,
1655 uint32_t flags)
1656{
1657 struct weston_compositor *compositor = seat->compositor;
1658
1659 if (flags & WESTON_ACTIVATE_FLAG_CLICKED) {
1660 view->click_to_activate_serial =
1661 peek_next_activate_serial(compositor);
1662 }
1663
1664 weston_seat_set_keyboard_focus(seat, view->surface);
1665}
1666
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001667WL_EXPORT void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001668notify_button(struct weston_seat *seat, const struct timespec *time,
1669 int32_t button, enum wl_pointer_button_state state)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001670{
1671 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001672 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001673
1674 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001675 weston_compositor_idle_inhibit(compositor);
1676 if (pointer->button_count == 0) {
1677 pointer->grab_button = button;
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001678 pointer->grab_time = *time;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001679 pointer->grab_x = pointer->x;
1680 pointer->grab_y = pointer->y;
1681 }
1682 pointer->button_count++;
1683 } else {
1684 weston_compositor_idle_release(compositor);
1685 pointer->button_count--;
1686 }
1687
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001688 weston_compositor_run_button_binding(compositor, pointer, time, button,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001689 state);
1690
1691 pointer->grab->interface->button(pointer->grab, time, button, state);
1692
1693 if (pointer->button_count == 1)
1694 pointer->grab_serial =
1695 wl_display_get_serial(compositor->wl_display);
1696}
1697
1698WL_EXPORT void
Alexandros Frantzis80321942017-11-16 18:20:56 +02001699notify_axis(struct weston_seat *seat, const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001700 struct weston_pointer_axis_event *event)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001701{
1702 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001703 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001704
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001705 weston_compositor_wake(compositor);
1706
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001707 if (weston_compositor_run_axis_binding(compositor, pointer,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001708 time, event))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001709 return;
1710
Peter Hutterer89b6a492016-01-18 15:58:17 +10001711 pointer->grab->interface->axis(pointer->grab, time, event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001712}
1713
Peter Hutterer87743e92016-01-18 16:38:22 +10001714WL_EXPORT void
1715notify_axis_source(struct weston_seat *seat, uint32_t source)
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->axis_source(pointer->grab, source);
1723}
1724
1725WL_EXPORT void
1726notify_pointer_frame(struct weston_seat *seat)
1727{
1728 struct weston_compositor *compositor = seat->compositor;
1729 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1730
1731 weston_compositor_wake(compositor);
1732
1733 pointer->grab->interface->frame(pointer->grab);
1734}
1735
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001736WL_EXPORT int
1737weston_keyboard_set_locks(struct weston_keyboard *keyboard,
1738 uint32_t mask, uint32_t value)
1739{
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001740 uint32_t serial;
1741 xkb_mod_mask_t mods_depressed, mods_latched, mods_locked, group;
1742 xkb_mod_mask_t num, caps;
1743
1744 /* We don't want the leds to go out of sync with the actual state
1745 * so if the backend has no way to change the leds don't try to
1746 * change the state */
1747 if (!keyboard->seat->led_update)
1748 return -1;
1749
1750 mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
1751 XKB_STATE_DEPRESSED);
1752 mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
1753 XKB_STATE_LATCHED);
1754 mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
1755 XKB_STATE_LOCKED);
1756 group = xkb_state_serialize_group(keyboard->xkb_state.state,
1757 XKB_STATE_EFFECTIVE);
1758
1759 num = (1 << keyboard->xkb_info->mod2_mod);
1760 caps = (1 << keyboard->xkb_info->caps_mod);
1761 if (mask & WESTON_NUM_LOCK) {
1762 if (value & WESTON_NUM_LOCK)
1763 mods_locked |= num;
1764 else
1765 mods_locked &= ~num;
1766 }
1767 if (mask & WESTON_CAPS_LOCK) {
1768 if (value & WESTON_CAPS_LOCK)
1769 mods_locked |= caps;
1770 else
1771 mods_locked &= ~caps;
1772 }
1773
1774 xkb_state_update_mask(keyboard->xkb_state.state, mods_depressed,
1775 mods_latched, mods_locked, 0, 0, group);
1776
1777 serial = wl_display_next_serial(
1778 keyboard->seat->compositor->wl_display);
1779 notify_modifiers(keyboard->seat, serial);
1780
1781 return 0;
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001782}
1783
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001784WL_EXPORT void
1785notify_modifiers(struct weston_seat *seat, uint32_t serial)
1786{
Derek Foreman1281a362015-07-31 16:55:32 -05001787 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001788 struct weston_keyboard_grab *grab = keyboard->grab;
1789 uint32_t mods_depressed, mods_latched, mods_locked, group;
1790 uint32_t mods_lookup;
1791 enum weston_led leds = 0;
1792 int changed = 0;
1793
1794 /* Serialize and update our internal state, checking to see if it's
1795 * different to the previous state. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001796 mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03001797 XKB_STATE_MODS_DEPRESSED);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001798 mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03001799 XKB_STATE_MODS_LATCHED);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001800 mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03001801 XKB_STATE_MODS_LOCKED);
1802 group = xkb_state_serialize_layout(keyboard->xkb_state.state,
1803 XKB_STATE_LAYOUT_EFFECTIVE);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001804
Derek Foreman244e99e2015-06-03 15:53:26 -05001805 if (mods_depressed != keyboard->modifiers.mods_depressed ||
1806 mods_latched != keyboard->modifiers.mods_latched ||
1807 mods_locked != keyboard->modifiers.mods_locked ||
1808 group != keyboard->modifiers.group)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001809 changed = 1;
1810
Derek Foreman244e99e2015-06-03 15:53:26 -05001811 run_modifier_bindings(seat, keyboard->modifiers.mods_depressed,
Daniel Stone96d47c02013-11-19 11:37:12 +01001812 mods_depressed);
1813
Derek Foreman244e99e2015-06-03 15:53:26 -05001814 keyboard->modifiers.mods_depressed = mods_depressed;
1815 keyboard->modifiers.mods_latched = mods_latched;
1816 keyboard->modifiers.mods_locked = mods_locked;
1817 keyboard->modifiers.group = group;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001818
1819 /* And update the modifier_state for bindings. */
1820 mods_lookup = mods_depressed | mods_latched;
1821 seat->modifier_state = 0;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001822 if (mods_lookup & (1 << keyboard->xkb_info->ctrl_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001823 seat->modifier_state |= MODIFIER_CTRL;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001824 if (mods_lookup & (1 << keyboard->xkb_info->alt_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001825 seat->modifier_state |= MODIFIER_ALT;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001826 if (mods_lookup & (1 << keyboard->xkb_info->super_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001827 seat->modifier_state |= MODIFIER_SUPER;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001828 if (mods_lookup & (1 << keyboard->xkb_info->shift_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001829 seat->modifier_state |= MODIFIER_SHIFT;
1830
1831 /* Finally, notify the compositor that LEDs have changed. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001832 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
1833 keyboard->xkb_info->num_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001834 leds |= LED_NUM_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001835 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
1836 keyboard->xkb_info->caps_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001837 leds |= LED_CAPS_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001838 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
1839 keyboard->xkb_info->scroll_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001840 leds |= LED_SCROLL_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001841 if (leds != keyboard->xkb_state.leds && seat->led_update)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001842 seat->led_update(seat, leds);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001843 keyboard->xkb_state.leds = leds;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001844
1845 if (changed) {
1846 grab->interface->modifiers(grab,
1847 serial,
1848 keyboard->modifiers.mods_depressed,
1849 keyboard->modifiers.mods_latched,
1850 keyboard->modifiers.mods_locked,
1851 keyboard->modifiers.group);
1852 }
1853}
1854
1855static void
1856update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
1857 enum wl_keyboard_key_state state)
1858{
Derek Foreman1281a362015-07-31 16:55:32 -05001859 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001860 enum xkb_key_direction direction;
1861
1862 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1863 direction = XKB_KEY_DOWN;
1864 else
1865 direction = XKB_KEY_UP;
1866
1867 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
1868 * broken keycode system, which starts at 8. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001869 xkb_state_update_key(keyboard->xkb_state.state, key + 8, direction);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001870
1871 notify_modifiers(seat, serial);
1872}
Rui Matos65196bc2013-10-10 19:44:19 +02001873
1874static void
1875send_keymap(struct wl_resource *resource, struct weston_xkb_info *xkb_info)
1876{
1877 wl_keyboard_send_keymap(resource,
1878 WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
1879 xkb_info->keymap_fd,
1880 xkb_info->keymap_size);
1881}
1882
1883static void
1884send_modifiers(struct wl_resource *resource, uint32_t serial, struct weston_keyboard *keyboard)
1885{
1886 wl_keyboard_send_modifiers(resource, serial,
1887 keyboard->modifiers.mods_depressed,
1888 keyboard->modifiers.mods_latched,
1889 keyboard->modifiers.mods_locked,
1890 keyboard->modifiers.group);
1891}
1892
1893static struct weston_xkb_info *
1894weston_xkb_info_create(struct xkb_keymap *keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02001895
1896static void
1897update_keymap(struct weston_seat *seat)
1898{
Derek Foreman1281a362015-07-31 16:55:32 -05001899 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Rui Matos65196bc2013-10-10 19:44:19 +02001900 struct wl_resource *resource;
1901 struct weston_xkb_info *xkb_info;
1902 struct xkb_state *state;
1903 xkb_mod_mask_t latched_mods;
1904 xkb_mod_mask_t locked_mods;
1905
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001906 xkb_info = weston_xkb_info_create(keyboard->pending_keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02001907
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001908 xkb_keymap_unref(keyboard->pending_keymap);
1909 keyboard->pending_keymap = NULL;
Rui Matos65196bc2013-10-10 19:44:19 +02001910
1911 if (!xkb_info) {
1912 weston_log("failed to create XKB info\n");
1913 return;
1914 }
1915
1916 state = xkb_state_new(xkb_info->keymap);
1917 if (!state) {
1918 weston_log("failed to initialise XKB state\n");
1919 weston_xkb_info_destroy(xkb_info);
1920 return;
1921 }
1922
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001923 latched_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
1924 XKB_STATE_MODS_LATCHED);
1925 locked_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
1926 XKB_STATE_MODS_LOCKED);
Rui Matos65196bc2013-10-10 19:44:19 +02001927 xkb_state_update_mask(state,
1928 0, /* depressed */
1929 latched_mods,
1930 locked_mods,
1931 0, 0, 0);
1932
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001933 weston_xkb_info_destroy(keyboard->xkb_info);
1934 keyboard->xkb_info = xkb_info;
Rui Matos65196bc2013-10-10 19:44:19 +02001935
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001936 xkb_state_unref(keyboard->xkb_state.state);
1937 keyboard->xkb_state.state = state;
Rui Matos65196bc2013-10-10 19:44:19 +02001938
Derek Foremanbc91e542015-06-03 15:53:27 -05001939 wl_resource_for_each(resource, &keyboard->resource_list)
Rui Matos65196bc2013-10-10 19:44:19 +02001940 send_keymap(resource, xkb_info);
Derek Foremanbc91e542015-06-03 15:53:27 -05001941 wl_resource_for_each(resource, &keyboard->focus_resource_list)
Rui Matos65196bc2013-10-10 19:44:19 +02001942 send_keymap(resource, xkb_info);
1943
1944 notify_modifiers(seat, wl_display_next_serial(seat->compositor->wl_display));
1945
1946 if (!latched_mods && !locked_mods)
1947 return;
1948
Derek Foremanbc91e542015-06-03 15:53:27 -05001949 wl_resource_for_each(resource, &keyboard->resource_list)
1950 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), keyboard);
1951 wl_resource_for_each(resource, &keyboard->focus_resource_list)
1952 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), keyboard);
Rui Matos65196bc2013-10-10 19:44:19 +02001953}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001954
1955WL_EXPORT void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02001956notify_key(struct weston_seat *seat, const struct timespec *time, uint32_t key,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001957 enum wl_keyboard_key_state state,
1958 enum weston_key_state_update update_state)
1959{
1960 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001961 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001962 struct weston_keyboard_grab *grab = keyboard->grab;
Pekka Paalanen86b53962014-11-19 13:43:32 +02001963 uint32_t *k, *end;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001964
1965 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001966 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001967 } else {
1968 weston_compositor_idle_release(compositor);
1969 }
1970
Pekka Paalanen86b53962014-11-19 13:43:32 +02001971 end = keyboard->keys.data + keyboard->keys.size;
1972 for (k = keyboard->keys.data; k < end; k++) {
1973 if (*k == key) {
1974 /* Ignore server-generated repeats. */
1975 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1976 return;
1977 *k = *--end;
1978 }
1979 }
1980 keyboard->keys.size = (void *) end - keyboard->keys.data;
1981 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1982 k = wl_array_add(&keyboard->keys, sizeof *k);
1983 *k = key;
1984 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001985
1986 if (grab == &keyboard->default_grab ||
1987 grab == &keyboard->input_method_grab) {
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001988 weston_compositor_run_key_binding(compositor, keyboard, time,
1989 key, state);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001990 grab = keyboard->grab;
1991 }
1992
1993 grab->interface->key(grab, time, key, state);
1994
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001995 if (keyboard->pending_keymap &&
Pekka Paalanen86b53962014-11-19 13:43:32 +02001996 keyboard->keys.size == 0)
Rui Matos65196bc2013-10-10 19:44:19 +02001997 update_keymap(seat);
1998
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001999 if (update_state == STATE_UPDATE_AUTOMATIC) {
2000 update_modifier_state(seat,
2001 wl_display_get_serial(compositor->wl_display),
2002 key,
2003 state);
2004 }
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002005
Olivier Fourdandf84dbe2016-06-30 16:01:56 +02002006 keyboard->grab_serial = wl_display_get_serial(compositor->wl_display);
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002007 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02002008 keyboard->grab_time = *time;
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002009 keyboard->grab_key = key;
2010 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002011}
2012
2013WL_EXPORT void
2014notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002015 double x, double y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002016{
Derek Foreman1281a362015-07-31 16:55:32 -05002017 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2018
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002019 if (output) {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002020 weston_pointer_move_to(pointer,
2021 wl_fixed_from_double(x),
2022 wl_fixed_from_double(y));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002023 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002024 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002025 * NULL) here, but somehow that breaks re-entry... */
2026 }
2027}
2028
2029static void
2030destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
2031{
2032 struct weston_seat *ws;
2033
2034 ws = container_of(listener, struct weston_seat,
2035 saved_kbd_focus_listener);
2036
2037 ws->saved_kbd_focus = NULL;
2038}
2039
2040WL_EXPORT void
2041notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
2042 enum weston_key_state_update update_state)
2043{
2044 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002045 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04002046 struct weston_surface *surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002047 uint32_t *k, serial;
2048
2049 serial = wl_display_next_serial(compositor->wl_display);
2050 wl_array_copy(&keyboard->keys, keys);
2051 wl_array_for_each(k, &keyboard->keys) {
2052 weston_compositor_idle_inhibit(compositor);
2053 if (update_state == STATE_UPDATE_AUTOMATIC)
2054 update_modifier_state(seat, serial, *k,
2055 WL_KEYBOARD_KEY_STATE_PRESSED);
2056 }
2057
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002058 surface = seat->saved_kbd_focus;
2059
2060 if (surface) {
2061 wl_list_remove(&seat->saved_kbd_focus_listener.link);
2062 weston_keyboard_set_focus(keyboard, surface);
2063 seat->saved_kbd_focus = NULL;
2064 }
2065}
2066
2067WL_EXPORT void
2068notify_keyboard_focus_out(struct weston_seat *seat)
2069{
2070 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002071 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2072 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002073 uint32_t *k, serial;
2074
2075 serial = wl_display_next_serial(compositor->wl_display);
2076 wl_array_for_each(k, &keyboard->keys) {
2077 weston_compositor_idle_release(compositor);
2078 update_modifier_state(seat, serial, *k,
2079 WL_KEYBOARD_KEY_STATE_RELEASED);
2080 }
2081
2082 seat->modifier_state = 0;
2083
2084 if (keyboard->focus) {
2085 seat->saved_kbd_focus = keyboard->focus;
2086 seat->saved_kbd_focus_listener.notify =
2087 destroy_device_saved_kbd_focus;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002088 wl_signal_add(&keyboard->focus->destroy_signal,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002089 &seat->saved_kbd_focus_listener);
2090 }
2091
2092 weston_keyboard_set_focus(keyboard, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002093 weston_keyboard_cancel_grab(keyboard);
Derek Foreman1281a362015-07-31 16:55:32 -05002094 if (pointer)
2095 weston_pointer_cancel_grab(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002096}
2097
Michael Fua2bb7912013-07-23 15:51:06 +08002098WL_EXPORT void
Derek Foreman4c93c082015-04-30 16:45:41 -05002099weston_touch_set_focus(struct weston_touch *touch, struct weston_view *view)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002100{
Neil Roberts96d790e2013-09-19 17:32:00 +01002101 struct wl_list *focus_resource_list;
2102
Derek Foreman4c93c082015-04-30 16:45:41 -05002103 focus_resource_list = &touch->focus_resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002104
Derek Foreman4c93c082015-04-30 16:45:41 -05002105 if (view && touch->focus &&
2106 touch->focus->surface == view->surface) {
2107 touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002108 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002109 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002110
Derek Foreman4c93c082015-04-30 16:45:41 -05002111 wl_list_remove(&touch->focus_resource_listener.link);
2112 wl_list_init(&touch->focus_resource_listener.link);
2113 wl_list_remove(&touch->focus_view_listener.link);
2114 wl_list_init(&touch->focus_view_listener.link);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01002115
Neil Roberts96d790e2013-09-19 17:32:00 +01002116 if (!wl_list_empty(focus_resource_list)) {
Derek Foreman4c93c082015-04-30 16:45:41 -05002117 move_resources(&touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002118 focus_resource_list);
2119 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002120
Jason Ekstranda7af7042013-10-12 22:38:11 -05002121 if (view) {
Derek Foreman362656b2014-09-04 10:23:05 -05002122 struct wl_client *surface_client;
2123
2124 if (!view->surface->resource) {
Derek Foreman4c93c082015-04-30 16:45:41 -05002125 touch->focus = NULL;
Derek Foreman362656b2014-09-04 10:23:05 -05002126 return;
2127 }
2128
2129 surface_client = wl_resource_get_client(view->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +01002130 move_resources_for_client(focus_resource_list,
Derek Foreman4c93c082015-04-30 16:45:41 -05002131 &touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002132 surface_client);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01002133 wl_resource_add_destroy_listener(view->surface->resource,
Derek Foreman4c93c082015-04-30 16:45:41 -05002134 &touch->focus_resource_listener);
2135 wl_signal_add(&view->destroy_signal, &touch->focus_view_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002136 }
Derek Foreman4c93c082015-04-30 16:45:41 -05002137 touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002138}
2139
2140/**
2141 * notify_touch - emulates button touches and notifies surfaces accordingly.
2142 *
2143 * It assumes always the correct cycle sequence until it gets here: touch_down
2144 * → touch_update → ... → touch_update → touch_end. The driver is responsible
2145 * for sending along such order.
2146 *
2147 */
2148WL_EXPORT void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02002149notify_touch(struct weston_seat *seat, const struct timespec *time,
2150 int touch_id, double double_x, double double_y, int touch_type)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002151{
2152 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002153 struct weston_touch *touch = weston_seat_get_touch(seat);
Kristian Høgsberge329f362013-05-06 22:19:57 -04002154 struct weston_touch_grab *grab = touch->grab;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002155 struct weston_view *ev;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002156 wl_fixed_t sx, sy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002157 wl_fixed_t x = wl_fixed_from_double(double_x);
2158 wl_fixed_t y = wl_fixed_from_double(double_y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002159
2160 /* Update grab's global coordinates. */
Neil Roberts306fe082013-10-03 16:43:06 +01002161 if (touch_id == touch->grab_touch_id && touch_type != WL_TOUCH_UP) {
2162 touch->grab_x = x;
2163 touch->grab_y = y;
2164 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002165
2166 switch (touch_type) {
2167 case WL_TOUCH_DOWN:
2168 weston_compositor_idle_inhibit(ec);
2169
Jonas Ådahl9484b692013-12-02 22:05:03 +01002170 touch->num_tp++;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002171
Jason Ekstranda7af7042013-10-12 22:38:11 -05002172 /* the first finger down picks the view, and all further go
2173 * to that view for the remainder of the touch session i.e.
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002174 * until all touch points are up again. */
Jonas Ådahl9484b692013-12-02 22:05:03 +01002175 if (touch->num_tp == 1) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002176 ev = weston_compositor_pick_view(ec, x, y, &sx, &sy);
Derek Foreman4c93c082015-04-30 16:45:41 -05002177 weston_touch_set_focus(touch, ev);
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002178 } else if (!touch->focus) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002179 /* Unexpected condition: We have non-initial touch but
2180 * there is no focused surface.
2181 */
Chris Michael3f607d32015-10-07 11:59:49 -04002182 weston_log("touch event received with %d points down "
Jonas Ådahl9484b692013-12-02 22:05:03 +01002183 "but no surface focused\n", touch->num_tp);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002184 return;
2185 }
2186
Derek Foreman99a6a2d2015-07-15 13:00:43 -05002187 weston_compositor_run_touch_binding(ec, touch,
Kristian Høgsbergc8964012014-02-05 14:25:18 -08002188 time, touch_type);
2189
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002190 grab->interface->down(grab, time, touch_id, x, y);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002191 if (touch->num_tp == 1) {
Rusty Lynchf1407ff2013-08-08 21:13:57 -07002192 touch->grab_serial =
2193 wl_display_get_serial(ec->wl_display);
Neil Roberts306fe082013-10-03 16:43:06 +01002194 touch->grab_touch_id = touch_id;
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02002195 touch->grab_time = *time;
Rusty Lynchf1407ff2013-08-08 21:13:57 -07002196 touch->grab_x = x;
2197 touch->grab_y = y;
2198 }
2199
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002200 break;
2201 case WL_TOUCH_MOTION:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002202 ev = touch->focus;
2203 if (!ev)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002204 break;
2205
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02002206 grab->interface->motion(grab, timespec_to_msec(time),
2207 touch_id, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002208 break;
2209 case WL_TOUCH_UP:
Kristian Høgsberga30e29a2014-01-08 22:29:20 -08002210 if (touch->num_tp == 0) {
2211 /* This can happen if we start out with one or
2212 * more fingers on the touch screen, in which
2213 * case we didn't get the corresponding down
2214 * event. */
2215 weston_log("unmatched touch up event\n");
2216 break;
2217 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002218 weston_compositor_idle_release(ec);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002219 touch->num_tp--;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002220
Alexandros Frantzis27a51b82017-11-16 18:20:59 +02002221 grab->interface->up(grab, time, touch_id);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002222 if (touch->num_tp == 0)
Derek Foreman4c93c082015-04-30 16:45:41 -05002223 weston_touch_set_focus(touch, NULL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002224 break;
2225 }
2226}
2227
Jonas Ådahl1679f232014-04-12 09:39:51 +02002228WL_EXPORT void
2229notify_touch_frame(struct weston_seat *seat)
2230{
Derek Foreman1281a362015-07-31 16:55:32 -05002231 struct weston_touch *touch = weston_seat_get_touch(seat);
Jonas Ådahl1679f232014-04-12 09:39:51 +02002232 struct weston_touch_grab *grab = touch->grab;
2233
2234 grab->interface->frame(grab);
2235}
2236
Derek Foreman3cc004a2015-11-06 15:56:09 -06002237WL_EXPORT void
2238notify_touch_cancel(struct weston_seat *seat)
2239{
2240 struct weston_touch *touch = weston_seat_get_touch(seat);
2241 struct weston_touch_grab *grab = touch->grab;
2242
2243 grab->interface->cancel(grab);
2244}
2245
Pekka Paalanen8274d902014-08-06 19:36:51 +03002246static int
2247pointer_cursor_surface_get_label(struct weston_surface *surface,
2248 char *buf, size_t len)
2249{
2250 return snprintf(buf, len, "cursor");
2251}
2252
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002253static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002254pointer_cursor_surface_committed(struct weston_surface *es,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002255 int32_t dx, int32_t dy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002256{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002257 struct weston_pointer *pointer = es->committed_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002258 int x, y;
2259
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002260 if (es->width == 0)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002261 return;
2262
Jason Ekstranda7af7042013-10-12 22:38:11 -05002263 assert(es == pointer->sprite->surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002264
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002265 pointer->hotspot_x -= dx;
2266 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002267
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002268 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
2269 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002270
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002271 weston_view_set_position(pointer->sprite, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002272
2273 empty_region(&es->pending.input);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02002274 empty_region(&es->input);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002275
2276 if (!weston_surface_is_mapped(es)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002277 weston_layer_entry_insert(&es->compositor->cursor_layer.view_list,
2278 &pointer->sprite->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002279 weston_view_update_transform(pointer->sprite);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002280 es->is_mapped = true;
2281 pointer->sprite->is_mapped = true;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002282 }
2283}
2284
2285static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002286pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2287 uint32_t serial, struct wl_resource *surface_resource,
2288 int32_t x, int32_t y)
2289{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002290 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002291 struct weston_surface *surface = NULL;
2292
2293 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002294 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002295
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002296 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002297 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002298 /* pointer->focus->surface->resource can be NULL. Surfaces like the
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02002299 black_surface used in shell.c for fullscreen don't have
2300 a resource, but can still have focus */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002301 if (pointer->focus->surface->resource == NULL)
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02002302 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002303 if (wl_resource_get_client(pointer->focus->surface->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002304 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002305 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002306 return;
2307
Derek Foreman4e53c532015-03-23 10:55:32 -05002308 if (!surface) {
2309 if (pointer->sprite)
2310 pointer_unmap_sprite(pointer);
2311 return;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002312 }
2313
Jonas Ådahlb4070242015-03-18 15:08:03 +08002314 if (pointer->sprite && pointer->sprite->surface == surface &&
2315 pointer->hotspot_x == x && pointer->hotspot_y == y)
2316 return;
2317
Derek Foreman4e53c532015-03-23 10:55:32 -05002318 if (!pointer->sprite || pointer->sprite->surface != surface) {
2319 if (weston_surface_set_role(surface, "wl_pointer-cursor",
2320 resource,
2321 WL_POINTER_ERROR_ROLE) < 0)
2322 return;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002323
Derek Foreman4e53c532015-03-23 10:55:32 -05002324 if (pointer->sprite)
2325 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002326
Derek Foreman4e53c532015-03-23 10:55:32 -05002327 wl_signal_add(&surface->destroy_signal,
2328 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002329
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002330 surface->committed = pointer_cursor_surface_committed;
2331 surface->committed_private = pointer;
Derek Foreman4e53c532015-03-23 10:55:32 -05002332 weston_surface_set_label_func(surface,
2333 pointer_cursor_surface_get_label);
2334 pointer->sprite = weston_view_create(surface);
2335 }
2336
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002337 pointer->hotspot_x = x;
2338 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002339
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +02002340 if (surface->buffer_ref.buffer) {
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002341 pointer_cursor_surface_committed(surface, 0, 0);
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +02002342 weston_view_schedule_repaint(pointer->sprite);
2343 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002344}
2345
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002346static void
2347pointer_release(struct wl_client *client, struct wl_resource *resource)
2348{
2349 wl_resource_destroy(resource);
2350}
2351
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002352static const struct wl_pointer_interface pointer_interface = {
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002353 pointer_set_cursor,
2354 pointer_release
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002355};
2356
2357static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002358seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2359 uint32_t id)
2360{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002361 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002362 /* We use the pointer_state directly, which means we'll
2363 * give a wl_pointer if the seat has ever had one - even though
2364 * the spec explicitly states that this request only takes effect
2365 * if the seat has the pointer capability.
2366 *
2367 * This prevents a race between the compositor sending new
2368 * capabilities and the client trying to use the old ones.
2369 */
2370 struct weston_pointer *pointer = seat->pointer_state;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002371 struct wl_resource *cr;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002372 struct weston_pointer_client *pointer_client;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002373
Derek Foreman1281a362015-07-31 16:55:32 -05002374 if (!pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002375 return;
2376
Jason Ekstranda85118c2013-06-27 20:17:02 -05002377 cr = wl_resource_create(client, &wl_pointer_interface,
2378 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002379 if (cr == NULL) {
2380 wl_client_post_no_memory(client);
2381 return;
2382 }
2383
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002384 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
2385 if (!pointer_client) {
2386 wl_client_post_no_memory(client);
2387 return;
2388 }
2389
2390 wl_list_insert(&pointer_client->pointer_resources,
2391 wl_resource_get_link(cr));
Derek Foreman1281a362015-07-31 16:55:32 -05002392 wl_resource_set_implementation(cr, &pointer_interface, pointer,
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002393 unbind_pointer_client_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002394
Derek Foreman1281a362015-07-31 16:55:32 -05002395 if (pointer->focus && pointer->focus->surface->resource &&
2396 wl_resource_get_client(pointer->focus->surface->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002397 wl_fixed_t sx, sy;
2398
Derek Foreman1281a362015-07-31 16:55:32 -05002399 weston_view_from_global_fixed(pointer->focus,
2400 pointer->x,
2401 pointer->y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002402 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002403
Neil Roberts96d790e2013-09-19 17:32:00 +01002404 wl_pointer_send_enter(cr,
Derek Foreman1281a362015-07-31 16:55:32 -05002405 pointer->focus_serial,
2406 pointer->focus->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01002407 sx, sy);
Peter Hutterer87743e92016-01-18 16:38:22 +10002408 pointer_send_frame(cr);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002409 }
2410}
2411
2412static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002413keyboard_release(struct wl_client *client, struct wl_resource *resource)
2414{
2415 wl_resource_destroy(resource);
2416}
2417
2418static const struct wl_keyboard_interface keyboard_interface = {
2419 keyboard_release
2420};
2421
Derek Foreman280e7dd2014-10-03 13:13:42 -05002422static bool
Neil Roberts96d790e2013-09-19 17:32:00 +01002423should_send_modifiers_to_client(struct weston_seat *seat,
2424 struct wl_client *client)
2425{
Derek Foreman1281a362015-07-31 16:55:32 -05002426 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2427 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2428
2429 if (keyboard &&
2430 keyboard->focus &&
2431 keyboard->focus->resource &&
2432 wl_resource_get_client(keyboard->focus->resource) == client)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002433 return true;
Neil Roberts96d790e2013-09-19 17:32:00 +01002434
Derek Foreman1281a362015-07-31 16:55:32 -05002435 if (pointer &&
2436 pointer->focus &&
2437 pointer->focus->surface->resource &&
2438 wl_resource_get_client(pointer->focus->surface->resource) == client)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002439 return true;
Neil Roberts96d790e2013-09-19 17:32:00 +01002440
Derek Foreman280e7dd2014-10-03 13:13:42 -05002441 return false;
Neil Roberts96d790e2013-09-19 17:32:00 +01002442}
2443
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002444static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002445seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2446 uint32_t id)
2447{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002448 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002449 /* We use the keyboard_state directly, which means we'll
2450 * give a wl_keyboard if the seat has ever had one - even though
2451 * the spec explicitly states that this request only takes effect
2452 * if the seat has the keyboard capability.
2453 *
2454 * This prevents a race between the compositor sending new
2455 * capabilities and the client trying to use the old ones.
2456 */
2457 struct weston_keyboard *keyboard = seat->keyboard_state;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002458 struct wl_resource *cr;
2459
Derek Foreman345c9f32015-06-03 15:53:28 -05002460 if (!keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002461 return;
2462
Jason Ekstranda85118c2013-06-27 20:17:02 -05002463 cr = wl_resource_create(client, &wl_keyboard_interface,
2464 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002465 if (cr == NULL) {
2466 wl_client_post_no_memory(client);
2467 return;
2468 }
2469
Neil Roberts96d790e2013-09-19 17:32:00 +01002470 /* May be moved to focused list later by either
2471 * weston_keyboard_set_focus or directly if this client is already
2472 * focused */
Derek Foreman345c9f32015-06-03 15:53:28 -05002473 wl_list_insert(&keyboard->resource_list, wl_resource_get_link(cr));
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002474 wl_resource_set_implementation(cr, &keyboard_interface,
2475 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002476
Jonny Lamb66a41a02014-08-12 14:58:25 +02002477 if (wl_resource_get_version(cr) >= WL_KEYBOARD_REPEAT_INFO_SINCE_VERSION) {
2478 wl_keyboard_send_repeat_info(cr,
2479 seat->compositor->kb_repeat_rate,
2480 seat->compositor->kb_repeat_delay);
2481 }
Jasper St. Pierred8c6aeb2014-08-04 13:43:24 -04002482
Derek Foreman185d1582017-06-28 11:17:23 -05002483 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2484 keyboard->xkb_info->keymap_fd,
2485 keyboard->xkb_info->keymap_size);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002486
Neil Roberts96d790e2013-09-19 17:32:00 +01002487 if (should_send_modifiers_to_client(seat, client)) {
Derek Foreman345c9f32015-06-03 15:53:28 -05002488 send_modifiers_to_resource(keyboard,
Neil Roberts96d790e2013-09-19 17:32:00 +01002489 cr,
Derek Foreman345c9f32015-06-03 15:53:28 -05002490 keyboard->focus_serial);
Neil Roberts96d790e2013-09-19 17:32:00 +01002491 }
2492
Derek Foreman345c9f32015-06-03 15:53:28 -05002493 if (keyboard->focus && keyboard->focus->resource &&
2494 wl_resource_get_client(keyboard->focus->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01002495 struct weston_surface *surface =
Derek Foreman345c9f32015-06-03 15:53:28 -05002496 (struct weston_surface *)keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +01002497
2498 wl_list_remove(wl_resource_get_link(cr));
Derek Foreman345c9f32015-06-03 15:53:28 -05002499 wl_list_insert(&keyboard->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002500 wl_resource_get_link(cr));
2501 wl_keyboard_send_enter(cr,
Derek Foreman345c9f32015-06-03 15:53:28 -05002502 keyboard->focus_serial,
Neil Roberts96d790e2013-09-19 17:32:00 +01002503 surface->resource,
Derek Foreman345c9f32015-06-03 15:53:28 -05002504 &keyboard->keys);
Neil Roberts96d790e2013-09-19 17:32:00 +01002505
2506 /* If this is the first keyboard resource for this
2507 * client... */
Derek Foreman345c9f32015-06-03 15:53:28 -05002508 if (keyboard->focus_resource_list.prev ==
Neil Roberts96d790e2013-09-19 17:32:00 +01002509 wl_resource_get_link(cr))
2510 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002511 }
2512}
2513
2514static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002515touch_release(struct wl_client *client, struct wl_resource *resource)
2516{
2517 wl_resource_destroy(resource);
2518}
2519
2520static const struct wl_touch_interface touch_interface = {
2521 touch_release
2522};
2523
2524static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002525seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2526 uint32_t id)
2527{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002528 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002529 /* We use the touch_state directly, which means we'll
2530 * give a wl_touch if the seat has ever had one - even though
2531 * the spec explicitly states that this request only takes effect
2532 * if the seat has the touch capability.
2533 *
2534 * This prevents a race between the compositor sending new
2535 * capabilities and the client trying to use the old ones.
2536 */
2537 struct weston_touch *touch = seat->touch_state;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002538 struct wl_resource *cr;
2539
Derek Foreman1281a362015-07-31 16:55:32 -05002540 if (!touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002541 return;
2542
Jason Ekstranda85118c2013-06-27 20:17:02 -05002543 cr = wl_resource_create(client, &wl_touch_interface,
2544 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002545 if (cr == NULL) {
2546 wl_client_post_no_memory(client);
2547 return;
2548 }
2549
Derek Foreman1281a362015-07-31 16:55:32 -05002550 if (touch->focus &&
2551 wl_resource_get_client(touch->focus->surface->resource) == client) {
Chokshi, Mituld6697142015-10-09 08:28:47 +00002552 wl_list_insert(&touch->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002553 wl_resource_get_link(cr));
2554 } else {
Chokshi, Mituld6697142015-10-09 08:28:47 +00002555 wl_list_insert(&touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002556 wl_resource_get_link(cr));
2557 }
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002558 wl_resource_set_implementation(cr, &touch_interface,
2559 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002560}
2561
Quentin Glidicaab1d362016-03-13 17:49:08 +01002562static void
2563seat_release(struct wl_client *client, struct wl_resource *resource)
2564{
2565 wl_resource_destroy(resource);
2566}
2567
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002568static const struct wl_seat_interface seat_interface = {
2569 seat_get_pointer,
2570 seat_get_keyboard,
2571 seat_get_touch,
Quentin Glidicaab1d362016-03-13 17:49:08 +01002572 seat_release,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002573};
2574
2575static void
2576bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2577{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002578 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002579 struct wl_resource *resource;
2580 enum wl_seat_capability caps = 0;
2581
Jason Ekstranda85118c2013-06-27 20:17:02 -05002582 resource = wl_resource_create(client,
Derek Foreman1909c102015-11-26 14:17:47 -06002583 &wl_seat_interface, version, id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05002584 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05002585 wl_resource_set_implementation(resource, &seat_interface, data,
2586 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002587
Derek Foreman1281a362015-07-31 16:55:32 -05002588 if (weston_seat_get_pointer(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002589 caps |= WL_SEAT_CAPABILITY_POINTER;
Derek Foreman1281a362015-07-31 16:55:32 -05002590 if (weston_seat_get_keyboard(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002591 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Derek Foreman1281a362015-07-31 16:55:32 -05002592 if (weston_seat_get_touch(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002593 caps |= WL_SEAT_CAPABILITY_TOUCH;
2594
2595 wl_seat_send_capabilities(resource, caps);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04002596 if (version >= WL_SEAT_NAME_SINCE_VERSION)
Rob Bradforde445ae62013-05-31 18:09:51 +01002597 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002598}
2599
Jonas Ådahl30d61d82014-10-22 21:21:17 +02002600static void
2601relative_pointer_destroy(struct wl_client *client,
2602 struct wl_resource *resource)
2603{
2604 wl_resource_destroy(resource);
2605}
2606
2607static const struct zwp_relative_pointer_v1_interface relative_pointer_interface = {
2608 relative_pointer_destroy
2609};
2610
2611static void
2612relative_pointer_manager_destroy(struct wl_client *client,
2613 struct wl_resource *resource)
2614{
2615 wl_resource_destroy(resource);
2616}
2617
2618static void
2619relative_pointer_manager_get_relative_pointer(struct wl_client *client,
2620 struct wl_resource *resource,
2621 uint32_t id,
2622 struct wl_resource *pointer_resource)
2623{
2624 struct weston_pointer *pointer =
2625 wl_resource_get_user_data(pointer_resource);
2626 struct weston_pointer_client *pointer_client;
2627 struct wl_resource *cr;
2628
2629 cr = wl_resource_create(client, &zwp_relative_pointer_v1_interface,
2630 wl_resource_get_version(resource), id);
2631 if (cr == NULL) {
2632 wl_client_post_no_memory(client);
2633 return;
2634 }
2635
2636 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
2637 if (!pointer_client) {
2638 wl_client_post_no_memory(client);
2639 return;
2640 }
2641
2642 wl_list_insert(&pointer_client->relative_pointer_resources,
2643 wl_resource_get_link(cr));
2644 wl_resource_set_implementation(cr, &relative_pointer_interface,
2645 pointer,
2646 unbind_pointer_client_resource);
2647}
2648
2649static const struct zwp_relative_pointer_manager_v1_interface relative_pointer_manager = {
2650 relative_pointer_manager_destroy,
2651 relative_pointer_manager_get_relative_pointer,
2652};
2653
2654static void
2655bind_relative_pointer_manager(struct wl_client *client, void *data,
2656 uint32_t version, uint32_t id)
2657{
2658 struct weston_compositor *compositor = data;
2659 struct wl_resource *resource;
2660
2661 resource = wl_resource_create(client,
2662 &zwp_relative_pointer_manager_v1_interface,
2663 1, id);
2664
2665 wl_resource_set_implementation(resource, &relative_pointer_manager,
2666 compositor,
2667 NULL);
2668}
2669
Giulio Camuffo0358af42016-06-02 21:48:08 +03002670WL_EXPORT int
2671weston_compositor_set_xkb_rule_names(struct weston_compositor *ec,
2672 struct xkb_rule_names *names)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002673{
2674 if (ec->xkb_context == NULL) {
2675 ec->xkb_context = xkb_context_new(0);
2676 if (ec->xkb_context == NULL) {
2677 weston_log("failed to create XKB context\n");
2678 return -1;
2679 }
2680 }
2681
2682 if (names)
2683 ec->xkb_names = *names;
2684 if (!ec->xkb_names.rules)
2685 ec->xkb_names.rules = strdup("evdev");
2686 if (!ec->xkb_names.model)
2687 ec->xkb_names.model = strdup("pc105");
2688 if (!ec->xkb_names.layout)
2689 ec->xkb_names.layout = strdup("us");
2690
2691 return 0;
2692}
2693
Stefan Schmidtfda26522013-09-17 10:54:09 +01002694static void
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002695weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002696{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002697 if (--xkb_info->ref_count > 0)
2698 return;
2699
Ran Benitac9c74152014-08-19 23:59:52 +03002700 xkb_keymap_unref(xkb_info->keymap);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002701
2702 if (xkb_info->keymap_area)
2703 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2704 if (xkb_info->keymap_fd >= 0)
2705 close(xkb_info->keymap_fd);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002706 free(xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002707}
2708
2709void
2710weston_compositor_xkb_destroy(struct weston_compositor *ec)
2711{
2712 free((char *) ec->xkb_names.rules);
2713 free((char *) ec->xkb_names.model);
2714 free((char *) ec->xkb_names.layout);
2715 free((char *) ec->xkb_names.variant);
2716 free((char *) ec->xkb_names.options);
Stefan Schmidtfda26522013-09-17 10:54:09 +01002717
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002718 if (ec->xkb_info)
2719 weston_xkb_info_destroy(ec->xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002720 xkb_context_unref(ec->xkb_context);
2721}
2722
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002723static struct weston_xkb_info *
2724weston_xkb_info_create(struct xkb_keymap *keymap)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002725{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002726 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
2727 if (xkb_info == NULL)
2728 return NULL;
2729
Ran Benita2e1968f2014-08-19 23:59:51 +03002730 xkb_info->keymap = xkb_keymap_ref(keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002731 xkb_info->ref_count = 1;
2732
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002733 char *keymap_str;
2734
Ran Benita2e1968f2014-08-19 23:59:51 +03002735 xkb_info->shift_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2736 XKB_MOD_NAME_SHIFT);
2737 xkb_info->caps_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2738 XKB_MOD_NAME_CAPS);
2739 xkb_info->ctrl_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2740 XKB_MOD_NAME_CTRL);
2741 xkb_info->alt_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2742 XKB_MOD_NAME_ALT);
2743 xkb_info->mod2_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2744 "Mod2");
2745 xkb_info->mod3_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2746 "Mod3");
2747 xkb_info->super_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2748 XKB_MOD_NAME_LOGO);
2749 xkb_info->mod5_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2750 "Mod5");
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002751
Ran Benita2e1968f2014-08-19 23:59:51 +03002752 xkb_info->num_led = xkb_keymap_led_get_index(xkb_info->keymap,
2753 XKB_LED_NAME_NUM);
2754 xkb_info->caps_led = xkb_keymap_led_get_index(xkb_info->keymap,
2755 XKB_LED_NAME_CAPS);
2756 xkb_info->scroll_led = xkb_keymap_led_get_index(xkb_info->keymap,
2757 XKB_LED_NAME_SCROLL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002758
Ran Benita2e1968f2014-08-19 23:59:51 +03002759 keymap_str = xkb_keymap_get_as_string(xkb_info->keymap,
2760 XKB_KEYMAP_FORMAT_TEXT_V1);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002761 if (keymap_str == NULL) {
2762 weston_log("failed to get string version of keymap\n");
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002763 goto err_keymap;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002764 }
2765 xkb_info->keymap_size = strlen(keymap_str) + 1;
2766
2767 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
2768 if (xkb_info->keymap_fd < 0) {
2769 weston_log("creating a keymap file for %lu bytes failed: %m\n",
2770 (unsigned long) xkb_info->keymap_size);
2771 goto err_keymap_str;
2772 }
2773
2774 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2775 PROT_READ | PROT_WRITE,
2776 MAP_SHARED, xkb_info->keymap_fd, 0);
2777 if (xkb_info->keymap_area == MAP_FAILED) {
2778 weston_log("failed to mmap() %lu bytes\n",
2779 (unsigned long) xkb_info->keymap_size);
2780 goto err_dev_zero;
2781 }
2782 strcpy(xkb_info->keymap_area, keymap_str);
2783 free(keymap_str);
2784
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002785 return xkb_info;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002786
2787err_dev_zero:
2788 close(xkb_info->keymap_fd);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002789err_keymap_str:
2790 free(keymap_str);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002791err_keymap:
Ran Benita2e1968f2014-08-19 23:59:51 +03002792 xkb_keymap_unref(xkb_info->keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002793 free(xkb_info);
2794 return NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002795}
2796
2797static int
2798weston_compositor_build_global_keymap(struct weston_compositor *ec)
2799{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002800 struct xkb_keymap *keymap;
2801
2802 if (ec->xkb_info != NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002803 return 0;
2804
Ran Benita2e1968f2014-08-19 23:59:51 +03002805 keymap = xkb_keymap_new_from_names(ec->xkb_context,
2806 &ec->xkb_names,
2807 0);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002808 if (keymap == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002809 weston_log("failed to compile global XKB keymap\n");
2810 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
2811 "options %s\n",
2812 ec->xkb_names.rules, ec->xkb_names.model,
2813 ec->xkb_names.layout, ec->xkb_names.variant,
2814 ec->xkb_names.options);
2815 return -1;
2816 }
2817
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002818 ec->xkb_info = weston_xkb_info_create(keymap);
Rui Matos73d93952013-10-24 19:28:41 +02002819 xkb_keymap_unref(keymap);
Stefan Schmidtfda26522013-09-17 10:54:09 +01002820 if (ec->xkb_info == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002821 return -1;
2822
2823 return 0;
2824}
2825
Rui Matos65196bc2013-10-10 19:44:19 +02002826WL_EXPORT void
2827weston_seat_update_keymap(struct weston_seat *seat, struct xkb_keymap *keymap)
2828{
Derek Foreman1281a362015-07-31 16:55:32 -05002829 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2830
2831 if (!keyboard || !keymap)
Rui Matos65196bc2013-10-10 19:44:19 +02002832 return;
2833
Derek Foreman1281a362015-07-31 16:55:32 -05002834 xkb_keymap_unref(keyboard->pending_keymap);
2835 keyboard->pending_keymap = xkb_keymap_ref(keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02002836
Derek Foreman1281a362015-07-31 16:55:32 -05002837 if (keyboard->keys.size == 0)
Rui Matos65196bc2013-10-10 19:44:19 +02002838 update_keymap(seat);
Rui Matos65196bc2013-10-10 19:44:19 +02002839}
2840
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002841WL_EXPORT int
2842weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
2843{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002844 struct weston_keyboard *keyboard;
2845
Derek Foreman1281a362015-07-31 16:55:32 -05002846 if (seat->keyboard_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002847 seat->keyboard_device_count += 1;
2848 if (seat->keyboard_device_count == 1)
2849 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002850 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002851 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002852
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002853 keyboard = weston_keyboard_create();
2854 if (keyboard == NULL) {
2855 weston_log("failed to allocate weston keyboard struct\n");
2856 return -1;
2857 }
2858
Derek Foreman185d1582017-06-28 11:17:23 -05002859 if (keymap != NULL) {
2860 keyboard->xkb_info = weston_xkb_info_create(keymap);
2861 if (keyboard->xkb_info == NULL)
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02002862 goto err;
Derek Foreman185d1582017-06-28 11:17:23 -05002863 } else {
2864 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
2865 goto err;
2866 keyboard->xkb_info = seat->compositor->xkb_info;
2867 keyboard->xkb_info->ref_count++;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002868 }
Derek Foreman185d1582017-06-28 11:17:23 -05002869
2870 keyboard->xkb_state.state = xkb_state_new(keyboard->xkb_info->keymap);
2871 if (keyboard->xkb_state.state == NULL) {
2872 weston_log("failed to initialise XKB state\n");
2873 goto err;
2874 }
2875
2876 keyboard->xkb_state.leds = 0;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002877
Derek Foreman1281a362015-07-31 16:55:32 -05002878 seat->keyboard_state = keyboard;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02002879 seat->keyboard_device_count = 1;
2880 keyboard->seat = seat;
2881
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002882 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002883
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002884 return 0;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02002885
2886err:
2887 if (keyboard->xkb_info)
2888 weston_xkb_info_destroy(keyboard->xkb_info);
2889 free(keyboard);
2890
2891 return -1;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002892}
2893
Jonas Ådahl91fed542013-12-03 09:14:27 +01002894static void
2895weston_keyboard_reset_state(struct weston_keyboard *keyboard)
2896{
2897 struct weston_seat *seat = keyboard->seat;
2898 struct xkb_state *state;
2899
Derek Foreman185d1582017-06-28 11:17:23 -05002900 state = xkb_state_new(keyboard->xkb_info->keymap);
2901 if (!state) {
2902 weston_log("failed to reset XKB state\n");
2903 return;
Jonas Ådahl91fed542013-12-03 09:14:27 +01002904 }
Derek Foreman185d1582017-06-28 11:17:23 -05002905 xkb_state_unref(keyboard->xkb_state.state);
2906 keyboard->xkb_state.state = state;
2907
2908 keyboard->xkb_state.leds = 0;
Jonas Ådahl91fed542013-12-03 09:14:27 +01002909
2910 seat->modifier_state = 0;
2911}
2912
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002913WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002914weston_seat_release_keyboard(struct weston_seat *seat)
2915{
2916 seat->keyboard_device_count--;
Derek Foremand621df22014-11-19 11:04:12 -06002917 assert(seat->keyboard_device_count >= 0);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002918 if (seat->keyboard_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05002919 weston_keyboard_set_focus(seat->keyboard_state, NULL);
2920 weston_keyboard_cancel_grab(seat->keyboard_state);
2921 weston_keyboard_reset_state(seat->keyboard_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002922 seat_send_updated_caps(seat);
2923 }
2924}
2925
2926WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002927weston_seat_init_pointer(struct weston_seat *seat)
2928{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002929 struct weston_pointer *pointer;
2930
Derek Foreman1281a362015-07-31 16:55:32 -05002931 if (seat->pointer_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002932 seat->pointer_device_count += 1;
2933 if (seat->pointer_device_count == 1)
2934 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002935 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002936 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002937
Giulio Camuffocdb4d292013-11-14 23:42:53 +01002938 pointer = weston_pointer_create(seat);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002939 if (pointer == NULL)
2940 return;
2941
Derek Foreman1281a362015-07-31 16:55:32 -05002942 seat->pointer_state = pointer;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002943 seat->pointer_device_count = 1;
2944 pointer->seat = seat;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002945
2946 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002947}
2948
2949WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002950weston_seat_release_pointer(struct weston_seat *seat)
2951{
Derek Foreman1281a362015-07-31 16:55:32 -05002952 struct weston_pointer *pointer = seat->pointer_state;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002953
2954 seat->pointer_device_count--;
2955 if (seat->pointer_device_count == 0) {
Derek Foremanf9318d12015-05-11 15:40:11 -05002956 weston_pointer_clear_focus(pointer);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002957 weston_pointer_cancel_grab(pointer);
Jonas Ådahl630bae82013-10-17 23:04:06 +02002958
Jonas Ådahla4932742013-10-17 23:04:07 +02002959 if (pointer->sprite)
2960 pointer_unmap_sprite(pointer);
2961
Jonas Ådahl3e12e632013-12-02 22:05:05 +01002962 weston_pointer_reset_state(pointer);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002963 seat_send_updated_caps(seat);
Derek Foremanfd5ca512015-01-07 15:00:25 -06002964
2965 /* seat->pointer is intentionally not destroyed so that
2966 * a newly attached pointer on this seat will retain
2967 * the previous cursor co-ordinates.
2968 */
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002969 }
2970}
2971
2972WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002973weston_seat_init_touch(struct weston_seat *seat)
2974{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002975 struct weston_touch *touch;
2976
Derek Foreman1281a362015-07-31 16:55:32 -05002977 if (seat->touch_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002978 seat->touch_device_count += 1;
2979 if (seat->touch_device_count == 1)
2980 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002981 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002982 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002983
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002984 touch = weston_touch_create();
2985 if (touch == NULL)
2986 return;
2987
Derek Foreman1281a362015-07-31 16:55:32 -05002988 seat->touch_state = touch;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002989 seat->touch_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002990 touch->seat = seat;
2991
2992 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002993}
2994
2995WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002996weston_seat_release_touch(struct weston_seat *seat)
2997{
2998 seat->touch_device_count--;
2999 if (seat->touch_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05003000 weston_touch_set_focus(seat->touch_state, NULL);
3001 weston_touch_cancel_grab(seat->touch_state);
3002 weston_touch_reset_state(seat->touch_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003003 seat_send_updated_caps(seat);
3004 }
3005}
3006
3007WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003008weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
3009 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003010{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003011 memset(seat, 0, sizeof *seat);
3012
Kristian Høgsberge3148752013-05-06 23:19:49 -04003013 seat->selection_data_source = NULL;
3014 wl_list_init(&seat->base_resource_list);
3015 wl_signal_init(&seat->selection_signal);
3016 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04003017 wl_signal_init(&seat->destroy_signal);
Jason Ekstranda4ab5422014-04-02 19:53:45 -05003018 wl_signal_init(&seat->updated_caps_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003019
Peter Hutterer87743e92016-01-18 16:38:22 +10003020 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 5,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003021 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003022
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003023 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003024 seat->modifier_state = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003025 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003026
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003027 wl_list_insert(ec->seat_list.prev, &seat->link);
3028
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003029 clipboard_create(seat);
3030
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003031 wl_signal_emit(&ec->seat_created_signal, seat);
3032}
3033
3034WL_EXPORT void
3035weston_seat_release(struct weston_seat *seat)
3036{
3037 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003038
Jonas Ådahl1afb2382014-01-03 19:46:51 +01003039 if (seat->saved_kbd_focus)
3040 wl_list_remove(&seat->saved_kbd_focus_listener.link);
3041
Derek Foreman1281a362015-07-31 16:55:32 -05003042 if (seat->pointer_state)
3043 weston_pointer_destroy(seat->pointer_state);
3044 if (seat->keyboard_state)
3045 weston_keyboard_destroy(seat->keyboard_state);
3046 if (seat->touch_state)
3047 weston_touch_destroy(seat->touch_state);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003048
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003049 free (seat->seat_name);
3050
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003051 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04003052
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003053 wl_signal_emit(&seat->destroy_signal, seat);
3054}
Derek Foreman1281a362015-07-31 16:55:32 -05003055
3056/** Get a seat's keyboard pointer
3057 *
3058 * \param seat The seat to query
3059 * \return The seat's keyboard pointer, or NULL if no keyboard is present
3060 *
3061 * The keyboard pointer for a seat isn't freed when all keyboards are removed,
3062 * so it should only be used when the seat's keyboard_device_count is greater
3063 * than zero. This function does that test and only returns a pointer
3064 * when a keyboard is present.
3065 */
3066WL_EXPORT struct weston_keyboard *
3067weston_seat_get_keyboard(struct weston_seat *seat)
3068{
3069 if (!seat)
3070 return NULL;
3071
3072 if (seat->keyboard_device_count)
3073 return seat->keyboard_state;
3074
3075 return NULL;
3076}
3077
3078/** Get a seat's pointer pointer
3079 *
3080 * \param seat The seat to query
3081 * \return The seat's pointer pointer, or NULL if no pointer device is present
3082 *
3083 * The pointer pointer for a seat isn't freed when all mice are removed,
3084 * so it should only be used when the seat's pointer_device_count is greater
3085 * than zero. This function does that test and only returns a pointer
3086 * when a pointing device is present.
3087 */
3088WL_EXPORT struct weston_pointer *
3089weston_seat_get_pointer(struct weston_seat *seat)
3090{
3091 if (!seat)
3092 return NULL;
3093
3094 if (seat->pointer_device_count)
3095 return seat->pointer_state;
3096
3097 return NULL;
3098}
3099
Jonas Ådahld3414f22016-07-22 17:56:31 +08003100static const struct zwp_locked_pointer_v1_interface locked_pointer_interface;
3101static const struct zwp_confined_pointer_v1_interface confined_pointer_interface;
3102
3103static enum pointer_constraint_type
3104pointer_constraint_get_type(struct weston_pointer_constraint *constraint)
3105{
3106 if (wl_resource_instance_of(constraint->resource,
3107 &zwp_locked_pointer_v1_interface,
3108 &locked_pointer_interface)) {
3109 return POINTER_CONSTRAINT_TYPE_LOCK;
3110 } else if (wl_resource_instance_of(constraint->resource,
3111 &zwp_confined_pointer_v1_interface,
3112 &confined_pointer_interface)) {
3113 return POINTER_CONSTRAINT_TYPE_CONFINE;
3114 }
3115
3116 abort();
3117 return 0;
3118}
3119
3120static void
3121pointer_constraint_notify_activated(struct weston_pointer_constraint *constraint)
3122{
3123 struct wl_resource *resource = constraint->resource;
3124
3125 switch (pointer_constraint_get_type(constraint)) {
3126 case POINTER_CONSTRAINT_TYPE_LOCK:
3127 zwp_locked_pointer_v1_send_locked(resource);
3128 break;
3129 case POINTER_CONSTRAINT_TYPE_CONFINE:
3130 zwp_confined_pointer_v1_send_confined(resource);
3131 break;
3132 }
3133}
3134
3135static void
3136pointer_constraint_notify_deactivated(struct weston_pointer_constraint *constraint)
3137{
3138 struct wl_resource *resource = constraint->resource;
3139
3140 switch (pointer_constraint_get_type(constraint)) {
3141 case POINTER_CONSTRAINT_TYPE_LOCK:
3142 zwp_locked_pointer_v1_send_unlocked(resource);
3143 break;
3144 case POINTER_CONSTRAINT_TYPE_CONFINE:
3145 zwp_confined_pointer_v1_send_unconfined(resource);
3146 break;
3147 }
3148}
3149
3150static struct weston_pointer_constraint *
3151get_pointer_constraint_for_pointer(struct weston_surface *surface,
3152 struct weston_pointer *pointer)
3153{
3154 struct weston_pointer_constraint *constraint;
3155
3156 wl_list_for_each(constraint, &surface->pointer_constraints, link) {
3157 if (constraint->pointer == pointer)
3158 return constraint;
3159 }
3160
3161 return NULL;
3162}
3163
Derek Foreman1281a362015-07-31 16:55:32 -05003164/** Get a seat's touch pointer
3165 *
3166 * \param seat The seat to query
3167 * \return The seat's touch pointer, or NULL if no touch device is present
3168 *
3169 * The touch pointer for a seat isn't freed when all touch devices are removed,
3170 * so it should only be used when the seat's touch_device_count is greater
3171 * than zero. This function does that test and only returns a pointer
3172 * when a touch device is present.
3173 */
3174WL_EXPORT struct weston_touch *
3175weston_seat_get_touch(struct weston_seat *seat)
3176{
3177 if (!seat)
3178 return NULL;
3179
3180 if (seat->touch_device_count)
3181 return seat->touch_state;
3182
3183 return NULL;
3184}
Bryce Harrington24f917e2016-06-29 19:04:07 -07003185
3186/** Sets the keyboard focus to the given surface
3187 *
3188 * \param seat The seat to query
3189 */
3190WL_EXPORT void
3191weston_seat_set_keyboard_focus(struct weston_seat *seat,
3192 struct weston_surface *surface)
3193{
3194 struct weston_compositor *compositor = seat->compositor;
3195 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003196 struct weston_surface_activation_data activation_data;
Bryce Harrington24f917e2016-06-29 19:04:07 -07003197
Jonas Ådahlef8e1c32016-03-15 20:28:51 +08003198 if (keyboard && keyboard->focus != surface) {
Bryce Harrington24f917e2016-06-29 19:04:07 -07003199 weston_keyboard_set_focus(keyboard, surface);
3200 wl_data_device_set_keyboard_focus(seat);
3201 }
3202
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003203 inc_activate_serial(compositor);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003204
3205 activation_data = (struct weston_surface_activation_data) {
3206 .surface = surface,
3207 .seat = seat,
3208 };
3209 wl_signal_emit(&compositor->activate_signal, &activation_data);
Bryce Harrington24f917e2016-06-29 19:04:07 -07003210}
Jonas Ådahl30d61d82014-10-22 21:21:17 +02003211
Jonas Ådahld3414f22016-07-22 17:56:31 +08003212static void
3213enable_pointer_constraint(struct weston_pointer_constraint *constraint,
3214 struct weston_view *view)
3215{
3216 assert(constraint->view == NULL);
3217 constraint->view = view;
3218 pointer_constraint_notify_activated(constraint);
3219 weston_pointer_start_grab(constraint->pointer, &constraint->grab);
3220 wl_list_remove(&constraint->surface_destroy_listener.link);
3221 wl_list_init(&constraint->surface_destroy_listener.link);
3222}
3223
3224static bool
3225is_pointer_constraint_enabled(struct weston_pointer_constraint *constraint)
3226{
3227 return constraint->view != NULL;
3228}
3229
3230static void
3231weston_pointer_constraint_disable(struct weston_pointer_constraint *constraint)
3232{
3233 constraint->view = NULL;
3234 pointer_constraint_notify_deactivated(constraint);
3235 weston_pointer_end_grab(constraint->grab.pointer);
3236}
3237
3238void
3239weston_pointer_constraint_destroy(struct weston_pointer_constraint *constraint)
3240{
3241 if (is_pointer_constraint_enabled(constraint))
3242 weston_pointer_constraint_disable(constraint);
3243
3244 wl_list_remove(&constraint->pointer_destroy_listener.link);
3245 wl_list_remove(&constraint->surface_destroy_listener.link);
3246 wl_list_remove(&constraint->surface_commit_listener.link);
3247 wl_list_remove(&constraint->surface_activate_listener.link);
3248
3249 wl_resource_set_user_data(constraint->resource, NULL);
3250 pixman_region32_fini(&constraint->region);
3251 wl_list_remove(&constraint->link);
3252 free(constraint);
3253}
3254
3255static void
3256disable_pointer_constraint(struct weston_pointer_constraint *constraint)
3257{
3258 switch (constraint->lifetime) {
3259 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT:
3260 weston_pointer_constraint_destroy(constraint);
3261 break;
3262 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT:
3263 weston_pointer_constraint_disable(constraint);
3264 break;
3265 }
3266}
3267
3268static bool
3269is_within_constraint_region(struct weston_pointer_constraint *constraint,
3270 wl_fixed_t sx, wl_fixed_t sy)
3271{
3272 struct weston_surface *surface = constraint->surface;
3273 pixman_region32_t constraint_region;
3274 bool result;
3275
3276 pixman_region32_init(&constraint_region);
3277 pixman_region32_intersect(&constraint_region,
3278 &surface->input,
3279 &constraint->region);
3280 result = pixman_region32_contains_point(&constraint_region,
3281 wl_fixed_to_int(sx),
3282 wl_fixed_to_int(sy),
3283 NULL);
3284 pixman_region32_fini(&constraint_region);
3285
3286 return result;
3287}
3288
3289static void
3290maybe_enable_pointer_constraint(struct weston_pointer_constraint *constraint)
3291{
3292 struct weston_surface *surface = constraint->surface;
3293 struct weston_view *vit;
3294 struct weston_view *view = NULL;
3295 struct weston_pointer *pointer = constraint->pointer;
3296 struct weston_keyboard *keyboard;
3297 struct weston_seat *seat = pointer->seat;
3298 int32_t x, y;
3299
3300 /* Postpone if no view of the surface was most recently clicked. */
3301 wl_list_for_each(vit, &surface->views, surface_link) {
3302 if (vit->click_to_activate_serial ==
3303 surface->compositor->activate_serial) {
3304 view = vit;
3305 }
3306 }
3307 if (view == NULL)
3308 return;
3309
3310 /* Postpone if surface doesn't have keyboard focus. */
3311 keyboard = weston_seat_get_keyboard(seat);
3312 if (!keyboard || keyboard->focus != surface)
3313 return;
3314
3315 /* Postpone constraint if the pointer is not within the
3316 * constraint region.
3317 */
3318 weston_view_from_global(view,
3319 wl_fixed_to_int(pointer->x),
3320 wl_fixed_to_int(pointer->y),
3321 &x, &y);
3322 if (!is_within_constraint_region(constraint,
3323 wl_fixed_from_int(x),
3324 wl_fixed_from_int(y)))
3325 return;
3326
3327 enable_pointer_constraint(constraint, view);
3328}
3329
3330static void
3331locked_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
3332{
3333}
3334
3335static void
3336locked_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02003337 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003338 struct weston_pointer_motion_event *event)
3339{
Quentin Glidiccde13452016-08-12 10:41:32 +02003340 pointer_send_relative_motion(grab->pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08003341}
3342
3343static void
3344locked_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003345 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003346 uint32_t button,
3347 uint32_t state_w)
3348{
3349 weston_pointer_send_button(grab->pointer, time, button, state_w);
3350}
3351
3352static void
3353locked_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02003354 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003355 struct weston_pointer_axis_event *event)
3356{
3357 weston_pointer_send_axis(grab->pointer, time, event);
3358}
3359
3360static void
3361locked_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
3362 uint32_t source)
3363{
3364 weston_pointer_send_axis_source(grab->pointer, source);
3365}
3366
3367static void
3368locked_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
3369{
3370 weston_pointer_send_frame(grab->pointer);
3371}
3372
3373static void
3374locked_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
3375{
3376 struct weston_pointer_constraint *constraint =
3377 container_of(grab, struct weston_pointer_constraint, grab);
3378
3379 disable_pointer_constraint(constraint);
3380}
3381
3382static const struct weston_pointer_grab_interface
3383 locked_pointer_grab_interface = {
3384 locked_pointer_grab_pointer_focus,
3385 locked_pointer_grab_pointer_motion,
3386 locked_pointer_grab_pointer_button,
3387 locked_pointer_grab_pointer_axis,
3388 locked_pointer_grab_pointer_axis_source,
3389 locked_pointer_grab_pointer_frame,
3390 locked_pointer_grab_pointer_cancel,
3391};
3392
3393static void
3394pointer_constraint_constrain_resource_destroyed(struct wl_resource *resource)
3395{
3396 struct weston_pointer_constraint *constraint =
3397 wl_resource_get_user_data(resource);
3398
3399 if (!constraint)
3400 return;
3401
3402 weston_pointer_constraint_destroy(constraint);
3403}
3404
3405static void
3406pointer_constraint_surface_activate(struct wl_listener *listener, void *data)
3407{
3408 struct weston_surface_activation_data *activation = data;
3409 struct weston_pointer *pointer;
3410 struct weston_surface *focus = activation->surface;
3411 struct weston_pointer_constraint *constraint =
3412 container_of(listener, struct weston_pointer_constraint,
3413 surface_activate_listener);
3414 bool is_constraint_surface;
3415
3416 pointer = weston_seat_get_pointer(activation->seat);
3417 if (!pointer)
3418 return;
3419
3420 is_constraint_surface =
3421 get_pointer_constraint_for_pointer(focus, pointer) == constraint;
3422
3423 if (is_constraint_surface &&
3424 !is_pointer_constraint_enabled(constraint))
3425 maybe_enable_pointer_constraint(constraint);
3426 else if (!is_constraint_surface &&
3427 is_pointer_constraint_enabled(constraint))
3428 disable_pointer_constraint(constraint);
3429}
3430
3431static void
3432pointer_constraint_pointer_destroyed(struct wl_listener *listener, void *data)
3433{
3434 struct weston_pointer_constraint *constraint =
3435 container_of(listener, struct weston_pointer_constraint,
3436 pointer_destroy_listener);
3437
3438 weston_pointer_constraint_destroy(constraint);
3439}
3440
3441static void
3442pointer_constraint_surface_destroyed(struct wl_listener *listener, void *data)
3443{
3444 struct weston_pointer_constraint *constraint =
3445 container_of(listener, struct weston_pointer_constraint,
3446 surface_destroy_listener);
3447
3448 weston_pointer_constraint_destroy(constraint);
3449}
3450
3451static void
3452pointer_constraint_surface_committed(struct wl_listener *listener, void *data)
3453{
3454 struct weston_pointer_constraint *constraint =
3455 container_of(listener, struct weston_pointer_constraint,
3456 surface_commit_listener);
3457
3458 if (constraint->region_is_pending) {
3459 constraint->region_is_pending = false;
3460 pixman_region32_copy(&constraint->region,
3461 &constraint->region_pending);
3462 pixman_region32_fini(&constraint->region_pending);
3463 pixman_region32_init(&constraint->region_pending);
3464 }
3465
3466 if (constraint->hint_is_pending) {
3467 constraint->hint_is_pending = false;
3468
3469 constraint->hint_is_pending = true;
3470 constraint->hint_x = constraint->hint_x_pending;
3471 constraint->hint_y = constraint->hint_y_pending;
3472 }
3473
3474 if (pointer_constraint_get_type(constraint) ==
3475 POINTER_CONSTRAINT_TYPE_CONFINE &&
3476 is_pointer_constraint_enabled(constraint))
3477 maybe_warp_confined_pointer(constraint);
3478}
3479
3480static struct weston_pointer_constraint *
3481weston_pointer_constraint_create(struct weston_surface *surface,
3482 struct weston_pointer *pointer,
3483 struct weston_region *region,
3484 enum zwp_pointer_constraints_v1_lifetime lifetime,
3485 struct wl_resource *cr,
3486 const struct weston_pointer_grab_interface *grab_interface)
3487{
3488 struct weston_pointer_constraint *constraint;
3489
3490 constraint = zalloc(sizeof *constraint);
3491 if (!constraint)
3492 return NULL;
3493
3494 constraint->lifetime = lifetime;
3495 pixman_region32_init(&constraint->region);
3496 pixman_region32_init(&constraint->region_pending);
3497 wl_list_insert(&surface->pointer_constraints, &constraint->link);
3498 constraint->surface = surface;
3499 constraint->pointer = pointer;
3500 constraint->resource = cr;
3501 constraint->grab.interface = grab_interface;
3502 if (region) {
3503 pixman_region32_copy(&constraint->region,
3504 &region->region);
3505 } else {
3506 pixman_region32_fini(&constraint->region);
3507 region_init_infinite(&constraint->region);
3508 }
3509
3510 constraint->surface_activate_listener.notify =
3511 pointer_constraint_surface_activate;
3512 constraint->surface_destroy_listener.notify =
3513 pointer_constraint_surface_destroyed;
3514 constraint->surface_commit_listener.notify =
3515 pointer_constraint_surface_committed;
3516 constraint->pointer_destroy_listener.notify =
3517 pointer_constraint_pointer_destroyed;
3518
3519 wl_signal_add(&surface->compositor->activate_signal,
3520 &constraint->surface_activate_listener);
3521 wl_signal_add(&pointer->destroy_signal,
3522 &constraint->pointer_destroy_listener);
3523 wl_signal_add(&surface->destroy_signal,
3524 &constraint->surface_destroy_listener);
3525 wl_signal_add(&surface->commit_signal,
3526 &constraint->surface_commit_listener);
3527
3528 return constraint;
3529}
3530
3531static void
3532init_pointer_constraint(struct wl_resource *pointer_constraints_resource,
3533 uint32_t id,
3534 struct weston_surface *surface,
3535 struct weston_pointer *pointer,
3536 struct weston_region *region,
3537 enum zwp_pointer_constraints_v1_lifetime lifetime,
3538 const struct wl_interface *interface,
3539 const void *implementation,
3540 const struct weston_pointer_grab_interface *grab_interface)
3541{
3542 struct wl_client *client =
3543 wl_resource_get_client(pointer_constraints_resource);
3544 struct wl_resource *cr;
3545 struct weston_pointer_constraint *constraint;
3546
3547 if (get_pointer_constraint_for_pointer(surface, pointer)) {
3548 wl_resource_post_error(pointer_constraints_resource,
3549 ZWP_POINTER_CONSTRAINTS_V1_ERROR_ALREADY_CONSTRAINED,
3550 "the pointer has a lock/confine request on this surface");
3551 return;
3552 }
3553
3554 cr = wl_resource_create(client, interface,
3555 wl_resource_get_version(pointer_constraints_resource),
3556 id);
3557 if (cr == NULL) {
3558 wl_client_post_no_memory(client);
3559 return;
3560 }
3561
3562 constraint = weston_pointer_constraint_create(surface, pointer,
3563 region, lifetime,
3564 cr, grab_interface);
3565 if (constraint == NULL) {
3566 wl_client_post_no_memory(client);
3567 return;
3568 }
3569
3570 wl_resource_set_implementation(cr, implementation, constraint,
3571 pointer_constraint_constrain_resource_destroyed);
3572
3573 maybe_enable_pointer_constraint(constraint);
3574}
3575
3576static void
3577pointer_constraints_destroy(struct wl_client *client,
3578 struct wl_resource *resource)
3579{
3580 wl_resource_destroy(resource);
3581}
3582
3583static void
3584locked_pointer_destroy(struct wl_client *client,
3585 struct wl_resource *resource)
3586{
3587 struct weston_pointer_constraint *constraint =
3588 wl_resource_get_user_data(resource);
3589 wl_fixed_t x, y;
3590
3591 if (constraint && constraint->view && constraint->hint_is_pending &&
3592 is_within_constraint_region(constraint,
3593 constraint->hint_x,
3594 constraint->hint_y)) {
3595 weston_view_to_global_fixed(constraint->view,
3596 constraint->hint_x,
3597 constraint->hint_y,
3598 &x, &y);
3599 weston_pointer_move_to(constraint->pointer, x, y);
3600 }
3601 wl_resource_destroy(resource);
3602}
3603
3604static void
3605locked_pointer_set_cursor_position_hint(struct wl_client *client,
3606 struct wl_resource *resource,
3607 wl_fixed_t surface_x,
3608 wl_fixed_t surface_y)
3609{
3610 struct weston_pointer_constraint *constraint =
3611 wl_resource_get_user_data(resource);
3612
3613 /* Ignore a set cursor hint that was sent after the lock was cancelled.
3614 */
3615 if (!constraint ||
3616 !constraint->resource ||
3617 constraint->resource != resource)
3618 return;
3619
3620 constraint->hint_is_pending = true;
3621 constraint->hint_x_pending = surface_x;
3622 constraint->hint_y_pending = surface_y;
3623}
3624
3625static void
3626locked_pointer_set_region(struct wl_client *client,
3627 struct wl_resource *resource,
3628 struct wl_resource *region_resource)
3629{
3630 struct weston_pointer_constraint *constraint =
3631 wl_resource_get_user_data(resource);
3632 struct weston_region *region = region_resource ?
3633 wl_resource_get_user_data(region_resource) : NULL;
3634
3635 if (!constraint)
3636 return;
3637
3638 if (region) {
3639 pixman_region32_copy(&constraint->region_pending,
3640 &region->region);
3641 } else {
3642 pixman_region32_fini(&constraint->region_pending);
3643 region_init_infinite(&constraint->region_pending);
3644 }
3645 constraint->region_is_pending = true;
3646}
3647
3648
3649static const struct zwp_locked_pointer_v1_interface locked_pointer_interface = {
3650 locked_pointer_destroy,
3651 locked_pointer_set_cursor_position_hint,
3652 locked_pointer_set_region,
3653};
3654
3655static void
3656pointer_constraints_lock_pointer(struct wl_client *client,
3657 struct wl_resource *resource,
3658 uint32_t id,
3659 struct wl_resource *surface_resource,
3660 struct wl_resource *pointer_resource,
3661 struct wl_resource *region_resource,
3662 uint32_t lifetime)
3663{
3664 struct weston_surface *surface =
3665 wl_resource_get_user_data(surface_resource);
3666 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
3667 struct weston_region *region = region_resource ?
3668 wl_resource_get_user_data(region_resource) : NULL;
3669
3670 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
3671 &zwp_locked_pointer_v1_interface,
3672 &locked_pointer_interface,
3673 &locked_pointer_grab_interface);
3674}
3675
3676static void
3677confined_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
3678{
3679}
3680
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08003681static double
3682vec2d_cross_product(struct vec2d a, struct vec2d b)
3683{
3684 return a.x * b.y - a.y * b.x;
3685}
3686
3687static struct vec2d
3688vec2d_add(struct vec2d a, struct vec2d b)
3689{
3690 return (struct vec2d) {
3691 .x = a.x + b.x,
3692 .y = a.y + b.y,
3693 };
3694}
3695
3696static struct vec2d
3697vec2d_subtract(struct vec2d a, struct vec2d b)
3698{
3699 return (struct vec2d) {
3700 .x = a.x - b.x,
3701 .y = a.y - b.y,
3702 };
3703}
3704
3705static struct vec2d
3706vec2d_multiply_constant(double c, struct vec2d a)
3707{
3708 return (struct vec2d) {
3709 .x = c * a.x,
3710 .y = c * a.y,
3711 };
3712}
3713
3714static bool
3715lines_intersect(struct line *line1, struct line *line2,
3716 struct vec2d *intersection)
3717{
3718 struct vec2d p = line1->a;
3719 struct vec2d r = vec2d_subtract(line1->b, line1->a);
3720 struct vec2d q = line2->a;
3721 struct vec2d s = vec2d_subtract(line2->b, line2->a);
3722 double rxs;
3723 double sxr;
3724 double t;
3725 double u;
3726
3727 /*
3728 * The line (p, r) and (q, s) intersects where
3729 *
3730 * p + t r = q + u s
3731 *
3732 * Calculate t:
3733 *
3734 * (p + t r) × s = (q + u s) × s
3735 * p × s + t (r × s) = q × s + u (s × s)
3736 * p × s + t (r × s) = q × s
3737 * t (r × s) = q × s - p × s
3738 * t (r × s) = (q - p) × s
3739 * t = ((q - p) × s) / (r × s)
3740 *
3741 * Using the same method, for u we get:
3742 *
3743 * u = ((p - q) × r) / (s × r)
3744 */
3745
3746 rxs = vec2d_cross_product(r, s);
3747 sxr = vec2d_cross_product(s, r);
3748
3749 /* If r × s = 0 then the lines are either parallel or collinear. */
3750 if (fabs(rxs) < DBL_MIN)
3751 return false;
3752
3753 t = vec2d_cross_product(vec2d_subtract(q, p), s) / rxs;
3754 u = vec2d_cross_product(vec2d_subtract(p, q), r) / sxr;
3755
3756 /* The lines only intersect if 0 ≤ t ≤ 1 and 0 ≤ u ≤ 1. */
3757 if (t < 0.0 || t > 1.0 || u < 0.0 || u > 1.0)
3758 return false;
3759
3760 *intersection = vec2d_add(p, vec2d_multiply_constant(t, r));
3761 return true;
3762}
3763
3764static struct border *
3765add_border(struct wl_array *array,
3766 double x1, double y1,
3767 double x2, double y2,
3768 enum motion_direction blocking_dir)
3769{
3770 struct border *border = wl_array_add(array, sizeof *border);
3771
3772 *border = (struct border) {
3773 .line = (struct line) {
3774 .a = (struct vec2d) {
3775 .x = x1,
3776 .y = y1,
3777 },
3778 .b = (struct vec2d) {
3779 .x = x2,
3780 .y = y2,
3781 },
3782 },
3783 .blocking_dir = blocking_dir,
3784 };
3785
3786 return border;
3787}
3788
3789static int
3790compare_lines_x(const void *a, const void *b)
3791{
3792 const struct border *border_a = a;
3793 const struct border *border_b = b;
3794
3795
3796 if (border_a->line.a.x == border_b->line.a.x)
3797 return border_a->line.b.x < border_b->line.b.x;
3798 else
3799 return border_a->line.a.x > border_b->line.a.x;
3800}
3801
3802static void
3803add_non_overlapping_edges(pixman_box32_t *boxes,
3804 int band_above_start,
3805 int band_below_start,
3806 int band_below_end,
3807 struct wl_array *borders)
3808{
3809 int i;
3810 struct wl_array band_merge;
3811 struct border *border;
3812 struct border *prev_border;
3813 struct border *new_border;
3814
3815 wl_array_init(&band_merge);
3816
3817 /* Add bottom band of previous row, and top band of current row, and
3818 * sort them so lower left x coordinate comes first. If there are two
3819 * borders with the same left x coordinate, the wider one comes first.
3820 */
3821 for (i = band_above_start; i < band_below_start; i++) {
3822 pixman_box32_t *box = &boxes[i];
3823 add_border(&band_merge, box->x1, box->y2, box->x2, box->y2,
3824 MOTION_DIRECTION_POSITIVE_Y);
3825 }
3826 for (i = band_below_start; i < band_below_end; i++) {
3827 pixman_box32_t *box= &boxes[i];
3828 add_border(&band_merge, box->x1, box->y1, box->x2, box->y1,
3829 MOTION_DIRECTION_NEGATIVE_Y);
3830 }
3831 qsort(band_merge.data,
3832 band_merge.size / sizeof *border,
3833 sizeof *border,
3834 compare_lines_x);
3835
3836 /* Combine the two combined bands so that any overlapping border is
3837 * eliminated. */
3838 prev_border = NULL;
3839 wl_array_for_each(border, &band_merge) {
3840 assert(border->line.a.y == border->line.b.y);
3841 assert(!prev_border ||
3842 prev_border->line.a.y == border->line.a.y);
3843 assert(!prev_border ||
3844 (prev_border->line.a.x != border->line.a.x ||
3845 prev_border->line.b.x != border->line.b.x));
3846 assert(!prev_border ||
3847 prev_border->line.a.x <= border->line.a.x);
3848
3849 if (prev_border &&
3850 prev_border->line.a.x == border->line.a.x) {
3851 /*
3852 * ------------ +
3853 * ------- =
3854 * [ ]-----
3855 */
3856 prev_border->line.a.x = border->line.b.x;
3857 } else if (prev_border &&
3858 prev_border->line.b.x == border->line.b.x) {
3859 /*
3860 * ------------ +
3861 * ------ =
3862 * ------[ ]
3863 */
3864 prev_border->line.b.x = border->line.a.x;
3865 } else if (prev_border &&
3866 prev_border->line.b.x == border->line.a.x) {
3867 /*
3868 * -------- +
3869 * ------ =
3870 * --------------
3871 */
3872 prev_border->line.b.x = border->line.b.x;
3873 } else if (prev_border &&
3874 prev_border->line.b.x >= border->line.a.x) {
3875 /*
3876 * --------------- +
3877 * ------ =
3878 * -----[ ]----
3879 */
3880 new_border = add_border(borders,
3881 border->line.b.x,
3882 border->line.b.y,
3883 prev_border->line.b.x,
3884 prev_border->line.b.y,
3885 prev_border->blocking_dir);
3886 prev_border->line.b.x = border->line.a.x;
3887 prev_border = new_border;
3888 } else {
3889 assert(!prev_border ||
3890 prev_border->line.b.x < border->line.a.x);
3891 /*
3892 * First border or non-overlapping.
3893 *
3894 * ----- +
3895 * ----- =
3896 * ----- -----
3897 */
3898 new_border = wl_array_add(borders, sizeof *border);
3899 *new_border = *border;
3900 prev_border = new_border;
3901 }
3902 }
3903
3904 wl_array_release(&band_merge);
3905}
3906
3907static void
3908add_band_bottom_edges(pixman_box32_t *boxes,
3909 int band_start,
3910 int band_end,
3911 struct wl_array *borders)
3912{
3913 int i;
3914
3915 for (i = band_start; i < band_end; i++) {
3916 add_border(borders,
3917 boxes[i].x1, boxes[i].y2,
3918 boxes[i].x2, boxes[i].y2,
3919 MOTION_DIRECTION_POSITIVE_Y);
3920 }
3921}
3922
3923static void
3924region_to_outline(pixman_region32_t *region, struct wl_array *borders)
3925{
3926 pixman_box32_t *boxes;
3927 int num_boxes;
3928 int i;
3929 int top_most, bottom_most;
3930 int current_roof;
3931 int prev_top;
3932 int band_start, prev_band_start;
3933
3934 /*
3935 * Remove any overlapping lines from the set of rectangles. Note that
3936 * pixman regions are grouped as rows of rectangles, where rectangles
3937 * in one row never touch or overlap and are all of the same height.
3938 *
3939 * -------- --- -------- ---
3940 * | | | | | | | |
3941 * ----------====---- --- ----------- ----- ---
3942 * | | => | |
3943 * ----==========--------- ----- ----------
3944 * | | | |
3945 * ------------------- -------------------
3946 *
3947 */
3948
3949 boxes = pixman_region32_rectangles(region, &num_boxes);
3950 prev_top = 0;
3951 top_most = boxes[0].y1;
3952 current_roof = top_most;
3953 bottom_most = boxes[num_boxes - 1].y2;
3954 band_start = 0;
3955 prev_band_start = 0;
3956 for (i = 0; i < num_boxes; i++) {
3957 /* Detect if there is a vertical empty space, and add the lower
3958 * level of the previous band if so was the case. */
3959 if (i > 0 &&
3960 boxes[i].y1 != prev_top &&
3961 boxes[i].y1 != boxes[i - 1].y2) {
3962 current_roof = boxes[i].y1;
3963 add_band_bottom_edges(boxes,
3964 band_start,
3965 i,
3966 borders);
3967 }
3968
3969 /* Special case adding the last band, since it won't be handled
3970 * by the band change detection below. */
3971 if (boxes[i].y1 != current_roof && i == num_boxes - 1) {
3972 if (boxes[i].y1 != prev_top) {
3973 /* The last band is a single box, so we don't
3974 * have a prev_band_start to tell us when the
3975 * previous band started. */
3976 add_non_overlapping_edges(boxes,
3977 band_start,
3978 i,
3979 i + 1,
3980 borders);
3981 } else {
3982 add_non_overlapping_edges(boxes,
3983 prev_band_start,
3984 band_start,
3985 i + 1,
3986 borders);
3987 }
3988 }
3989
3990 /* Detect when passing a band and combine the top border of the
3991 * just passed band with the bottom band of the previous band.
3992 */
3993 if (boxes[i].y1 != top_most && boxes[i].y1 != prev_top) {
3994 /* Combine the two passed bands. */
3995 if (prev_top != current_roof) {
3996 add_non_overlapping_edges(boxes,
3997 prev_band_start,
3998 band_start,
3999 i,
4000 borders);
4001 }
4002
4003 prev_band_start = band_start;
4004 band_start = i;
4005 }
4006
4007 /* Add the top border if the box is part of the current roof. */
4008 if (boxes[i].y1 == current_roof) {
4009 add_border(borders,
4010 boxes[i].x1, boxes[i].y1,
4011 boxes[i].x2, boxes[i].y1,
4012 MOTION_DIRECTION_NEGATIVE_Y);
4013 }
4014
4015 /* Add the bottom border of the last band. */
4016 if (boxes[i].y2 == bottom_most) {
4017 add_border(borders,
4018 boxes[i].x1, boxes[i].y2,
4019 boxes[i].x2, boxes[i].y2,
4020 MOTION_DIRECTION_POSITIVE_Y);
4021 }
4022
4023 /* Always add the left border. */
4024 add_border(borders,
4025 boxes[i].x1, boxes[i].y1,
4026 boxes[i].x1, boxes[i].y2,
4027 MOTION_DIRECTION_NEGATIVE_X);
4028
4029 /* Always add the right border. */
4030 add_border(borders,
4031 boxes[i].x2, boxes[i].y1,
4032 boxes[i].x2, boxes[i].y2,
4033 MOTION_DIRECTION_POSITIVE_X);
4034
4035 prev_top = boxes[i].y1;
4036 }
4037}
4038
4039static bool
4040is_border_horizontal (struct border *border)
4041{
4042 return border->line.a.y == border->line.b.y;
4043}
4044
4045static bool
4046is_border_blocking_directions(struct border *border,
4047 uint32_t directions)
4048{
4049 /* Don't block parallel motions. */
4050 if (is_border_horizontal(border)) {
4051 if ((directions & (MOTION_DIRECTION_POSITIVE_Y |
4052 MOTION_DIRECTION_NEGATIVE_Y)) == 0)
4053 return false;
4054 } else {
4055 if ((directions & (MOTION_DIRECTION_POSITIVE_X |
4056 MOTION_DIRECTION_NEGATIVE_X)) == 0)
4057 return false;
4058 }
4059
4060 return (~border->blocking_dir & directions) != directions;
4061}
4062
4063static struct border *
4064get_closest_border(struct wl_array *borders,
4065 struct line *motion,
4066 uint32_t directions)
4067{
4068 struct border *border;
4069 struct vec2d intersection;
4070 struct vec2d delta;
4071 double distance_2;
4072 struct border *closest_border = NULL;
4073 double closest_distance_2 = DBL_MAX;
4074
4075 wl_array_for_each(border, borders) {
4076 if (!is_border_blocking_directions(border, directions))
4077 continue;
4078
4079 if (!lines_intersect(&border->line, motion, &intersection))
4080 continue;
4081
4082 delta = vec2d_subtract(intersection, motion->a);
4083 distance_2 = delta.x*delta.x + delta.y*delta.y;
4084 if (distance_2 < closest_distance_2) {
4085 closest_border = border;
4086 closest_distance_2 = distance_2;
4087 }
4088 }
4089
4090 return closest_border;
4091}
4092
4093static void
4094clamp_to_border(struct border *border,
4095 struct line *motion,
4096 uint32_t *motion_dir)
4097{
4098 /*
4099 * When clamping either rightward or downward motions, the motion needs
4100 * to be clamped so that the destination coordinate does not end up on
4101 * the border (see weston_pointer_clamp_event_to_region). Do this by
4102 * clamping such motions to the border minus the smallest possible
4103 * wl_fixed_t value.
4104 */
4105 if (is_border_horizontal(border)) {
4106 if (*motion_dir & MOTION_DIRECTION_POSITIVE_Y)
4107 motion->b.y = border->line.a.y - wl_fixed_to_double(1);
4108 else
4109 motion->b.y = border->line.a.y;
4110 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_Y |
4111 MOTION_DIRECTION_NEGATIVE_Y);
4112 } else {
4113 if (*motion_dir & MOTION_DIRECTION_POSITIVE_X)
4114 motion->b.x = border->line.a.x - wl_fixed_to_double(1);
4115 else
4116 motion->b.x = border->line.a.x;
4117 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_X |
4118 MOTION_DIRECTION_NEGATIVE_X);
4119 }
4120}
4121
4122static uint32_t
4123get_motion_directions(struct line *motion)
4124{
4125 uint32_t directions = 0;
4126
4127 if (motion->a.x < motion->b.x)
4128 directions |= MOTION_DIRECTION_POSITIVE_X;
4129 else if (motion->a.x > motion->b.x)
4130 directions |= MOTION_DIRECTION_NEGATIVE_X;
4131 if (motion->a.y < motion->b.y)
4132 directions |= MOTION_DIRECTION_POSITIVE_Y;
4133 else if (motion->a.y > motion->b.y)
4134 directions |= MOTION_DIRECTION_NEGATIVE_Y;
4135
4136 return directions;
4137}
4138
Jonas Ådahld3414f22016-07-22 17:56:31 +08004139static void
4140weston_pointer_clamp_event_to_region(struct weston_pointer *pointer,
4141 struct weston_pointer_motion_event *event,
4142 pixman_region32_t *region,
4143 wl_fixed_t *clamped_x,
4144 wl_fixed_t *clamped_y)
4145{
4146 wl_fixed_t x, y;
4147 wl_fixed_t sx, sy;
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004148 wl_fixed_t old_sx = pointer->sx;
4149 wl_fixed_t old_sy = pointer->sy;
4150 struct wl_array borders;
4151 struct line motion;
4152 struct border *closest_border;
4153 float new_x_f, new_y_f;
4154 uint32_t directions;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004155
4156 weston_pointer_motion_to_abs(pointer, event, &x, &y);
4157 weston_view_from_global_fixed(pointer->focus, x, y, &sx, &sy);
4158
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004159 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004160
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004161 /*
4162 * Generate borders given the confine region we are to use. The borders
4163 * are defined to be the outer region of the allowed area. This means
4164 * top/left borders are "within" the allowed area, while bottom/right
4165 * borders are outside. This needs to be considered when clamping
4166 * confined motion vectors.
4167 */
4168 region_to_outline(region, &borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004169
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004170 motion = (struct line) {
4171 .a = (struct vec2d) {
4172 .x = wl_fixed_to_double(old_sx),
4173 .y = wl_fixed_to_double(old_sy),
4174 },
4175 .b = (struct vec2d) {
4176 .x = wl_fixed_to_double(sx),
4177 .y = wl_fixed_to_double(sy),
4178 },
4179 };
4180 directions = get_motion_directions(&motion);
4181
4182 while (directions) {
4183 closest_border = get_closest_border(&borders,
4184 &motion,
4185 directions);
4186 if (closest_border)
4187 clamp_to_border(closest_border, &motion, &directions);
4188 else
4189 break;
4190 }
4191
4192 weston_view_to_global_float(pointer->focus,
4193 (float) motion.b.x, (float) motion.b.y,
4194 &new_x_f, &new_y_f);
4195 *clamped_x = wl_fixed_from_double(new_x_f);
4196 *clamped_y = wl_fixed_from_double(new_y_f);
4197
4198 wl_array_release(&borders);
4199}
4200
4201static double
4202point_to_border_distance_2(struct border *border, double x, double y)
4203{
4204 double orig_x, orig_y;
4205 double dx, dy;
4206
4207 if (is_border_horizontal(border)) {
4208 if (x < border->line.a.x)
4209 orig_x = border->line.a.x;
4210 else if (x > border->line.b.x)
4211 orig_x = border->line.b.x;
4212 else
4213 orig_x = x;
4214 orig_y = border->line.a.y;
4215 } else {
4216 if (y < border->line.a.y)
4217 orig_y = border->line.a.y;
4218 else if (y > border->line.b.y)
4219 orig_y = border->line.b.y;
4220 else
4221 orig_y = y;
4222 orig_x = border->line.a.x;
4223 }
4224
4225
4226 dx = fabs(orig_x - x);
4227 dy = fabs(orig_y - y);
4228 return dx*dx + dy*dy;
4229}
4230
4231static void
4232warp_to_behind_border(struct border *border, wl_fixed_t *sx, wl_fixed_t *sy)
4233{
4234 switch (border->blocking_dir) {
4235 case MOTION_DIRECTION_POSITIVE_X:
4236 case MOTION_DIRECTION_NEGATIVE_X:
4237 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_X)
4238 *sx = wl_fixed_from_double(border->line.a.x) - 1;
4239 else
4240 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4241 if (*sy < wl_fixed_from_double(border->line.a.y))
4242 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4243 else if (*sy > wl_fixed_from_double(border->line.b.y))
4244 *sy = wl_fixed_from_double(border->line.b.y) - 1;
4245 break;
4246 case MOTION_DIRECTION_POSITIVE_Y:
4247 case MOTION_DIRECTION_NEGATIVE_Y:
4248 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_Y)
4249 *sy = wl_fixed_from_double(border->line.a.y) - 1;
4250 else
4251 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4252 if (*sx < wl_fixed_from_double(border->line.a.x))
4253 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4254 else if (*sx > wl_fixed_from_double(border->line.b.x))
4255 *sx = wl_fixed_from_double(border->line.b.x) - 1;
4256 break;
4257 }
Jonas Ådahld3414f22016-07-22 17:56:31 +08004258}
4259
4260static void
4261maybe_warp_confined_pointer(struct weston_pointer_constraint *constraint)
4262{
4263 wl_fixed_t x;
4264 wl_fixed_t y;
4265 wl_fixed_t sx;
4266 wl_fixed_t sy;
4267
4268 weston_view_from_global_fixed(constraint->view,
4269 constraint->pointer->x,
4270 constraint->pointer->y,
4271 &sx,
4272 &sy);
4273
4274 if (!is_within_constraint_region(constraint, sx, sy)) {
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004275 double xf = wl_fixed_to_double(sx);
4276 double yf = wl_fixed_to_double(sy);
4277 pixman_region32_t confine_region;
4278 struct wl_array borders;
4279 struct border *border;
4280 double closest_distance_2 = DBL_MAX;
4281 struct border *closest_border = NULL;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004282
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004283 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004284
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004285 pixman_region32_init(&confine_region);
4286 pixman_region32_intersect(&confine_region,
4287 &constraint->view->surface->input,
4288 &constraint->region);
4289 region_to_outline(&confine_region, &borders);
4290 pixman_region32_fini(&confine_region);
4291
4292 wl_array_for_each(border, &borders) {
4293 double distance_2;
4294
4295 distance_2 = point_to_border_distance_2(border, xf, yf);
4296 if (distance_2 < closest_distance_2) {
4297 closest_border = border;
4298 closest_distance_2 = distance_2;
4299 }
4300 }
4301 assert(closest_border);
4302
4303 warp_to_behind_border(closest_border, &sx, &sy);
4304
4305 wl_array_release(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004306
4307 weston_view_to_global_fixed(constraint->view, sx, sy, &x, &y);
4308 weston_pointer_move_to(constraint->pointer, x, y);
4309 }
4310}
4311
4312static void
4313confined_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02004314 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004315 struct weston_pointer_motion_event *event)
4316{
4317 struct weston_pointer_constraint *constraint =
4318 container_of(grab, struct weston_pointer_constraint, grab);
4319 struct weston_pointer *pointer = grab->pointer;
4320 struct weston_surface *surface;
4321 wl_fixed_t x, y;
4322 wl_fixed_t old_sx = pointer->sx;
4323 wl_fixed_t old_sy = pointer->sy;
4324 pixman_region32_t confine_region;
4325
4326 assert(pointer->focus);
4327 assert(pointer->focus->surface == constraint->surface);
4328
4329 surface = pointer->focus->surface;
4330
4331 pixman_region32_init(&confine_region);
4332 pixman_region32_intersect(&confine_region,
4333 &surface->input,
4334 &constraint->region);
4335 weston_pointer_clamp_event_to_region(pointer, event,
4336 &confine_region, &x, &y);
4337 weston_pointer_move_to(pointer, x, y);
4338 pixman_region32_fini(&confine_region);
4339
4340 weston_view_from_global_fixed(pointer->focus, x, y,
4341 &pointer->sx, &pointer->sy);
4342
4343 if (old_sx != pointer->sx || old_sy != pointer->sy) {
Quentin Glidiccde13452016-08-12 10:41:32 +02004344 pointer_send_motion(pointer, time,
4345 pointer->sx, pointer->sy);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004346 }
4347
Quentin Glidiccde13452016-08-12 10:41:32 +02004348 pointer_send_relative_motion(pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004349}
4350
4351static void
4352confined_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02004353 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004354 uint32_t button,
4355 uint32_t state_w)
4356{
4357 weston_pointer_send_button(grab->pointer, time, button, state_w);
4358}
4359
4360static void
4361confined_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02004362 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004363 struct weston_pointer_axis_event *event)
4364{
4365 weston_pointer_send_axis(grab->pointer, time, event);
4366}
4367
4368static void
4369confined_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
4370 uint32_t source)
4371{
4372 weston_pointer_send_axis_source(grab->pointer, source);
4373}
4374
4375static void
4376confined_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
4377{
4378 weston_pointer_send_frame(grab->pointer);
4379}
4380
4381static void
4382confined_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
4383{
4384 struct weston_pointer_constraint *constraint =
4385 container_of(grab, struct weston_pointer_constraint, grab);
4386
4387 disable_pointer_constraint(constraint);
4388
4389 /* If this is a persistent constraint, re-add the surface destroy signal
4390 * listener only if we are currently not destroying the surface. */
4391 switch (constraint->lifetime) {
4392 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT:
4393 if (constraint->surface->resource)
4394 wl_signal_add(&constraint->surface->destroy_signal,
4395 &constraint->surface_destroy_listener);
4396 break;
4397 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT:
4398 break;
4399 }
4400}
4401
4402static const struct weston_pointer_grab_interface
4403 confined_pointer_grab_interface = {
4404 confined_pointer_grab_pointer_focus,
4405 confined_pointer_grab_pointer_motion,
4406 confined_pointer_grab_pointer_button,
4407 confined_pointer_grab_pointer_axis,
4408 confined_pointer_grab_pointer_axis_source,
4409 confined_pointer_grab_pointer_frame,
4410 confined_pointer_grab_pointer_cancel,
4411};
4412
4413static void
4414confined_pointer_destroy(struct wl_client *client,
4415 struct wl_resource *resource)
4416{
4417 wl_resource_destroy(resource);
4418}
4419
4420static void
4421confined_pointer_set_region(struct wl_client *client,
4422 struct wl_resource *resource,
4423 struct wl_resource *region_resource)
4424{
4425 struct weston_pointer_constraint *constraint =
4426 wl_resource_get_user_data(resource);
4427 struct weston_region *region = region_resource ?
4428 wl_resource_get_user_data(region_resource) : NULL;
4429
4430 if (!constraint)
4431 return;
4432
4433 if (region) {
4434 pixman_region32_copy(&constraint->region_pending,
4435 &region->region);
4436 } else {
4437 pixman_region32_fini(&constraint->region_pending);
4438 region_init_infinite(&constraint->region_pending);
4439 }
4440 constraint->region_is_pending = true;
4441}
4442
4443static const struct zwp_confined_pointer_v1_interface confined_pointer_interface = {
4444 confined_pointer_destroy,
4445 confined_pointer_set_region,
4446};
4447
4448static void
4449pointer_constraints_confine_pointer(struct wl_client *client,
4450 struct wl_resource *resource,
4451 uint32_t id,
4452 struct wl_resource *surface_resource,
4453 struct wl_resource *pointer_resource,
4454 struct wl_resource *region_resource,
4455 uint32_t lifetime)
4456{
4457 struct weston_surface *surface =
4458 wl_resource_get_user_data(surface_resource);
4459 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
4460 struct weston_region *region = region_resource ?
4461 wl_resource_get_user_data(region_resource) : NULL;
4462
4463 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
4464 &zwp_confined_pointer_v1_interface,
4465 &confined_pointer_interface,
4466 &confined_pointer_grab_interface);
4467}
4468
4469static const struct zwp_pointer_constraints_v1_interface pointer_constraints_interface = {
4470 pointer_constraints_destroy,
4471 pointer_constraints_lock_pointer,
4472 pointer_constraints_confine_pointer,
4473};
4474
4475static void
4476bind_pointer_constraints(struct wl_client *client, void *data,
4477 uint32_t version, uint32_t id)
4478{
4479 struct wl_resource *resource;
4480
4481 resource = wl_resource_create(client,
4482 &zwp_pointer_constraints_v1_interface,
4483 1, id);
4484
4485 wl_resource_set_implementation(resource, &pointer_constraints_interface,
4486 NULL, NULL);
4487}
4488
Jonas Ådahl30d61d82014-10-22 21:21:17 +02004489int
4490weston_input_init(struct weston_compositor *compositor)
4491{
4492 if (!wl_global_create(compositor->wl_display,
4493 &zwp_relative_pointer_manager_v1_interface, 1,
4494 compositor, bind_relative_pointer_manager))
4495 return -1;
4496
Jonas Ådahld3414f22016-07-22 17:56:31 +08004497 if (!wl_global_create(compositor->wl_display,
4498 &zwp_pointer_constraints_v1_interface, 1,
4499 NULL, bind_pointer_constraints))
4500 return -1;
4501
Jonas Ådahl30d61d82014-10-22 21:21:17 +02004502 return 0;
4503}