blob: 0a694d138b61a72d7f0a7c87d216f5c496ebeb74 [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
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200755weston_touch_send_motion(struct weston_touch *touch,
756 const struct timespec *time, int touch_id,
757 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400758{
Neil Roberts96d790e2013-09-19 17:32:00 +0100759 struct wl_resource *resource;
760 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300761 wl_fixed_t sx, sy;
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200762 uint32_t msecs;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300763
Quentin Glidiccde13452016-08-12 10:41:32 +0200764 if (!weston_touch_has_focus_resource(touch))
765 return;
766
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300767 weston_view_from_global_fixed(touch->focus, x, y, &sx, &sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400768
Neil Roberts96d790e2013-09-19 17:32:00 +0100769 resource_list = &touch->focus_resource_list;
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200770 msecs = timespec_to_msec(time);
Neil Roberts96d790e2013-09-19 17:32:00 +0100771 wl_resource_for_each(resource, resource_list) {
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200772 wl_touch_send_motion(resource, msecs,
Kristian Høgsberge329f362013-05-06 22:19:57 -0400773 touch_id, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400774 }
775}
776
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200777static void
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200778default_grab_touch_motion(struct weston_touch_grab *grab,
779 const struct timespec *time, int touch_id,
780 wl_fixed_t x, wl_fixed_t y)
Quentin Glidiccde13452016-08-12 10:41:32 +0200781{
782 weston_touch_send_motion(grab->touch, time, touch_id, x, y);
783}
784
785
786/** Send wl_touch.frame events to focused resources.
787 *
788 * \param touch The touch where the frame events originates from.
789 *
790 * For every resource that is currently in focus, send a wl_touch.frame event.
791 * The focused resources are the wl_touch resources of the client which
792 * currently has the surface with touch focus.
793 */
794WL_EXPORT void
795weston_touch_send_frame(struct weston_touch *touch)
Jonas Ådahl1679f232014-04-12 09:39:51 +0200796{
797 struct wl_resource *resource;
798
Quentin Glidiccde13452016-08-12 10:41:32 +0200799 if (!weston_touch_has_focus_resource(touch))
800 return;
801
802 wl_resource_for_each(resource, &touch->focus_resource_list)
Jonas Ådahl1679f232014-04-12 09:39:51 +0200803 wl_touch_send_frame(resource);
804}
805
806static void
Quentin Glidiccde13452016-08-12 10:41:32 +0200807default_grab_touch_frame(struct weston_touch_grab *grab)
808{
809 weston_touch_send_frame(grab->touch);
810}
811
812static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200813default_grab_touch_cancel(struct weston_touch_grab *grab)
814{
815}
816
Kristian Høgsberge329f362013-05-06 22:19:57 -0400817static const struct weston_touch_grab_interface default_touch_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400818 default_grab_touch_down,
819 default_grab_touch_up,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200820 default_grab_touch_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +0200821 default_grab_touch_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200822 default_grab_touch_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400823};
824
Quentin Glidiccde13452016-08-12 10:41:32 +0200825/** Check if the keyboard has focused resources.
826 *
827 * \param keyboard The keyboard to check for focused resources.
828 * \return Whether or not this keyboard has focused resources
829 */
830WL_EXPORT bool
831weston_keyboard_has_focus_resource(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400832{
Quentin Glidiccde13452016-08-12 10:41:32 +0200833 if (!keyboard->focus)
834 return false;
835
836 if (wl_list_empty(&keyboard->focus_resource_list))
837 return false;
838
839 return true;
840}
841
842/** Send wl_keyboard.key events to focused resources.
843 *
844 * \param keyboard The keyboard where the key events originates from.
845 * \param time The timestamp of the event
846 * \param key The key value of the event
847 * \param state The state enum value of the event
848 *
849 * For every resource that is currently in focus, send a wl_keyboard.key event
850 * with the passed parameters. The focused resources are the wl_keyboard
851 * resources of the client which currently has the surface with keyboard focus.
852 */
853WL_EXPORT void
854weston_keyboard_send_key(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200855 const struct timespec *time, uint32_t key,
Quentin Glidiccde13452016-08-12 10:41:32 +0200856 enum wl_keyboard_key_state state)
857{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400858 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +0100859 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400860 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100861 struct wl_list *resource_list;
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200862 uint32_t msecs;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400863
Quentin Glidiccde13452016-08-12 10:41:32 +0200864 if (!weston_keyboard_has_focus_resource(keyboard))
865 return;
866
Neil Roberts96d790e2013-09-19 17:32:00 +0100867 resource_list = &keyboard->focus_resource_list;
Quentin Glidiccde13452016-08-12 10:41:32 +0200868 serial = wl_display_next_serial(display);
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200869 msecs = timespec_to_msec(time);
Quentin Glidiccde13452016-08-12 10:41:32 +0200870 wl_resource_for_each(resource, resource_list)
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200871 wl_keyboard_send_key(resource, serial, msecs, key, state);
Quentin Glidiccde13452016-08-12 10:41:32 +0200872};
873
874static void
875default_grab_keyboard_key(struct weston_keyboard_grab *grab,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200876 const struct timespec *time, uint32_t key,
877 uint32_t state)
Quentin Glidiccde13452016-08-12 10:41:32 +0200878{
879 weston_keyboard_send_key(grab->keyboard, time, key, state);
Neil Roberts96d790e2013-09-19 17:32:00 +0100880}
881
882static void
883send_modifiers_to_resource(struct weston_keyboard *keyboard,
884 struct wl_resource *resource,
885 uint32_t serial)
886{
887 wl_keyboard_send_modifiers(resource,
888 serial,
889 keyboard->modifiers.mods_depressed,
890 keyboard->modifiers.mods_latched,
891 keyboard->modifiers.mods_locked,
892 keyboard->modifiers.group);
893}
894
895static void
896send_modifiers_to_client_in_list(struct wl_client *client,
897 struct wl_list *list,
898 uint32_t serial,
899 struct weston_keyboard *keyboard)
900{
901 struct wl_resource *resource;
902
903 wl_resource_for_each(resource, list) {
904 if (wl_resource_get_client(resource) == client)
905 send_modifiers_to_resource(keyboard,
906 resource,
907 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400908 }
909}
910
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800911static struct weston_pointer_client *
912find_pointer_client_for_surface(struct weston_pointer *pointer,
913 struct weston_surface *surface)
914{
915 struct wl_client *client;
916
917 if (!surface)
918 return NULL;
919
920 if (!surface->resource)
921 return NULL;
922
923 client = wl_resource_get_client(surface->resource);
924 return weston_pointer_get_pointer_client(pointer, client);
925}
926
927static struct weston_pointer_client *
928find_pointer_client_for_view(struct weston_pointer *pointer, struct weston_view *view)
929{
930 if (!view)
931 return NULL;
932
933 return find_pointer_client_for_surface(pointer, view->surface);
934}
935
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400936static struct wl_resource *
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400937find_resource_for_surface(struct wl_list *list, struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400938{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400939 if (!surface)
940 return NULL;
941
Jason Ekstrand44a38632013-06-14 10:08:00 -0500942 if (!surface->resource)
943 return NULL;
Stefan Schmidtfda26522013-09-17 10:54:09 +0100944
Jason Ekstrand44a38632013-06-14 10:08:00 -0500945 return wl_resource_find_for_client(list, wl_resource_get_client(surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400946}
947
Quentin Glidiccde13452016-08-12 10:41:32 +0200948/** Send wl_keyboard.modifiers events to focused resources and pointer
949 * focused resources.
950 *
951 * \param keyboard The keyboard where the modifiers events originates from.
952 * \param serial The serial of the event
953 * \param mods_depressed The mods_depressed value of the event
954 * \param mods_latched The mods_latched value of the event
955 * \param mods_locked The mods_locked value of the event
956 * \param group The group value of the event
957 *
958 * For every resource that is currently in focus, send a wl_keyboard.modifiers
959 * event with the passed parameters. The focused resources are the wl_keyboard
960 * resources of the client which currently has the surface with keyboard focus.
961 * This also sends wl_keyboard.modifiers events to the wl_keyboard resources of
962 * the client having pointer focus (if different from the keyboard focus client).
963 */
964WL_EXPORT void
965weston_keyboard_send_modifiers(struct weston_keyboard *keyboard,
966 uint32_t serial, uint32_t mods_depressed,
967 uint32_t mods_latched,
968 uint32_t mods_locked, uint32_t group)
969{
970 struct weston_pointer *pointer =
971 weston_seat_get_pointer(keyboard->seat);
972
973 if (weston_keyboard_has_focus_resource(keyboard)) {
974 struct wl_list *resource_list;
975 struct wl_resource *resource;
976
977 resource_list = &keyboard->focus_resource_list;
978 wl_resource_for_each(resource, resource_list) {
979 wl_keyboard_send_modifiers(resource, serial,
980 mods_depressed, mods_latched,
981 mods_locked, group);
982 }
983 }
984
985 if (pointer && pointer->focus && pointer->focus->surface->resource &&
986 pointer->focus->surface != keyboard->focus) {
987 struct wl_client *pointer_client =
988 wl_resource_get_client(pointer->focus->surface->resource);
989
990 send_modifiers_to_client_in_list(pointer_client,
991 &keyboard->resource_list,
992 serial,
993 keyboard);
994 }
995}
996
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400997static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700998default_grab_keyboard_modifiers(struct weston_keyboard_grab *grab,
999 uint32_t serial, uint32_t mods_depressed,
1000 uint32_t mods_latched,
1001 uint32_t mods_locked, uint32_t group)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001002{
Quentin Glidiccde13452016-08-12 10:41:32 +02001003 weston_keyboard_send_modifiers(grab->keyboard, serial, mods_depressed,
1004 mods_latched, mods_locked, group);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001005}
1006
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001007static void
1008default_grab_keyboard_cancel(struct weston_keyboard_grab *grab)
1009{
1010}
1011
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001012static const struct weston_keyboard_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001013 default_keyboard_grab_interface = {
Kristian Høgsbergb27901c2013-10-28 15:32:02 -07001014 default_grab_keyboard_key,
1015 default_grab_keyboard_modifiers,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001016 default_grab_keyboard_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001017};
1018
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001019static void
1020pointer_unmap_sprite(struct weston_pointer *pointer)
1021{
Pekka Paalanenc557ff72014-11-12 16:42:52 +02001022 struct weston_surface *surface = pointer->sprite->surface;
1023
1024 if (weston_surface_is_mapped(surface))
1025 weston_surface_unmap(surface);
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001026
1027 wl_list_remove(&pointer->sprite_destroy_listener.link);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02001028 surface->committed = NULL;
1029 surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03001030 weston_surface_set_label_func(surface, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001031 weston_view_destroy(pointer->sprite);
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001032 pointer->sprite = NULL;
1033}
1034
1035static void
1036pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
1037{
1038 struct weston_pointer *pointer =
1039 container_of(listener, struct weston_pointer,
1040 sprite_destroy_listener);
1041
1042 pointer->sprite = NULL;
1043}
1044
Jonas Ådahl3e12e632013-12-02 22:05:05 +01001045static void
1046weston_pointer_reset_state(struct weston_pointer *pointer)
1047{
1048 pointer->button_count = 0;
1049}
1050
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001051static void
1052weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data);
1053
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001054WL_EXPORT struct weston_pointer *
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001055weston_pointer_create(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001056{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001057 struct weston_pointer *pointer;
1058
Peter Huttererf3d62272013-08-08 11:57:05 +10001059 pointer = zalloc(sizeof *pointer);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001060 if (pointer == NULL)
1061 return NULL;
1062
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001063 wl_list_init(&pointer->pointer_clients);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001064 weston_pointer_set_default_grab(pointer,
1065 seat->compositor->default_pointer_grab);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001066 wl_list_init(&pointer->focus_resource_listener.link);
1067 pointer->focus_resource_listener.notify = pointer_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001068 pointer->default_grab.pointer = pointer;
1069 pointer->grab = &pointer->default_grab;
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001070 wl_signal_init(&pointer->motion_signal);
Emilio Pozuelo Monfortaa7a4762013-11-19 11:37:15 +01001071 wl_signal_init(&pointer->focus_signal);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001072 wl_list_init(&pointer->focus_view_listener.link);
Jonas Ådahl3eb4ddd2016-07-22 17:52:58 +08001073 wl_signal_init(&pointer->destroy_signal);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001074
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001075 pointer->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
1076
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001077 /* FIXME: Pick better co-ords. */
1078 pointer->x = wl_fixed_from_int(100);
1079 pointer->y = wl_fixed_from_int(100);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001080
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001081 pointer->output_destroy_listener.notify =
1082 weston_pointer_handle_output_destroy;
1083 wl_signal_add(&seat->compositor->output_destroyed_signal,
1084 &pointer->output_destroy_listener);
1085
Derek Foremanf9318d12015-05-11 15:40:11 -05001086 pointer->sx = wl_fixed_from_int(-1000000);
1087 pointer->sy = wl_fixed_from_int(-1000000);
1088
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001089 return pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001090}
1091
1092WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001093weston_pointer_destroy(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001094{
Jonas Ådahl3eb4ddd2016-07-22 17:52:58 +08001095 wl_signal_emit(&pointer->destroy_signal, pointer);
1096
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001097 if (pointer->sprite)
1098 pointer_unmap_sprite(pointer);
1099
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001100 /* XXX: What about pointer->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +01001101
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001102 wl_list_remove(&pointer->focus_resource_listener.link);
1103 wl_list_remove(&pointer->focus_view_listener.link);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001104 wl_list_remove(&pointer->output_destroy_listener.link);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001105 free(pointer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001106}
1107
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001108void
1109weston_pointer_set_default_grab(struct weston_pointer *pointer,
1110 const struct weston_pointer_grab_interface *interface)
1111{
1112 if (interface)
1113 pointer->default_grab.interface = interface;
1114 else
1115 pointer->default_grab.interface =
1116 &default_pointer_grab_interface;
1117}
1118
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001119WL_EXPORT struct weston_keyboard *
1120weston_keyboard_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001121{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001122 struct weston_keyboard *keyboard;
1123
Peter Huttererf3d62272013-08-08 11:57:05 +10001124 keyboard = zalloc(sizeof *keyboard);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001125 if (keyboard == NULL)
Neil Roberts96d790e2013-09-19 17:32:00 +01001126 return NULL;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001127
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001128 wl_list_init(&keyboard->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +01001129 wl_list_init(&keyboard->focus_resource_list);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001130 wl_list_init(&keyboard->focus_resource_listener.link);
1131 keyboard->focus_resource_listener.notify = keyboard_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001132 wl_array_init(&keyboard->keys);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001133 keyboard->default_grab.interface = &default_keyboard_grab_interface;
1134 keyboard->default_grab.keyboard = keyboard;
1135 keyboard->grab = &keyboard->default_grab;
1136 wl_signal_init(&keyboard->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001137
1138 return keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001139}
1140
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001141static void
1142weston_xkb_info_destroy(struct weston_xkb_info *xkb_info);
1143
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001144WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001145weston_keyboard_destroy(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001146{
1147 /* XXX: What about keyboard->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +01001148
Derek Foreman185d1582017-06-28 11:17:23 -05001149 xkb_state_unref(keyboard->xkb_state.state);
1150 if (keyboard->xkb_info)
1151 weston_xkb_info_destroy(keyboard->xkb_info);
1152 xkb_keymap_unref(keyboard->pending_keymap);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001153
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001154 wl_array_release(&keyboard->keys);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001155 wl_list_remove(&keyboard->focus_resource_listener.link);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001156 free(keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001157}
1158
Jonas Ådahlcbfa7f72013-12-02 22:05:04 +01001159static void
1160weston_touch_reset_state(struct weston_touch *touch)
1161{
1162 touch->num_tp = 0;
1163}
1164
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001165WL_EXPORT struct weston_touch *
1166weston_touch_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001167{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001168 struct weston_touch *touch;
1169
Peter Huttererf3d62272013-08-08 11:57:05 +10001170 touch = zalloc(sizeof *touch);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001171 if (touch == NULL)
1172 return NULL;
1173
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001174 wl_list_init(&touch->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +01001175 wl_list_init(&touch->focus_resource_list);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001176 wl_list_init(&touch->focus_view_listener.link);
1177 touch->focus_view_listener.notify = touch_focus_view_destroyed;
1178 wl_list_init(&touch->focus_resource_listener.link);
1179 touch->focus_resource_listener.notify = touch_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001180 touch->default_grab.interface = &default_touch_grab_interface;
1181 touch->default_grab.touch = touch;
1182 touch->grab = &touch->default_grab;
1183 wl_signal_init(&touch->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001184
1185 return touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001186}
1187
1188WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001189weston_touch_destroy(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001190{
1191 /* XXX: What about touch->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +01001192
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001193 wl_list_remove(&touch->focus_view_listener.link);
1194 wl_list_remove(&touch->focus_resource_listener.link);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001195 free(touch);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001196}
1197
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001198static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04001199seat_send_updated_caps(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001200{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001201 enum wl_seat_capability caps = 0;
Rob Bradford6e737f52013-09-06 17:48:19 +01001202 struct wl_resource *resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001203
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001204 if (seat->pointer_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001205 caps |= WL_SEAT_CAPABILITY_POINTER;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001206 if (seat->keyboard_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001207 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001208 if (seat->touch_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001209 caps |= WL_SEAT_CAPABILITY_TOUCH;
1210
Rob Bradford6e737f52013-09-06 17:48:19 +01001211 wl_resource_for_each(resource, &seat->base_resource_list) {
1212 wl_seat_send_capabilities(resource, caps);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001213 }
Jason Ekstranda4ab5422014-04-02 19:53:45 -05001214 wl_signal_emit(&seat->updated_caps_signal, seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001215}
1216
Derek Foremanf9318d12015-05-11 15:40:11 -05001217
1218/** Clear the pointer focus
1219 *
1220 * \param pointer the pointer to clear focus for.
1221 *
1222 * This can be used to unset pointer focus and set the co-ordinates to the
1223 * arbitrary values we use for the no focus case.
1224 *
1225 * There's no requirement to use this function. For example, passing the
1226 * results of a weston_compositor_pick_view() directly to
1227 * weston_pointer_set_focus() will do the right thing when no view is found.
1228 */
1229WL_EXPORT void
1230weston_pointer_clear_focus(struct weston_pointer *pointer)
1231{
1232 weston_pointer_set_focus(pointer, NULL,
1233 wl_fixed_from_int(-1000000),
1234 wl_fixed_from_int(-1000000));
1235}
1236
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001237WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001238weston_pointer_set_focus(struct weston_pointer *pointer,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001239 struct weston_view *view,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001240 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001241{
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001242 struct weston_pointer_client *pointer_client;
Derek Foreman1281a362015-07-31 16:55:32 -05001243 struct weston_keyboard *kbd = weston_seat_get_keyboard(pointer->seat);
Neil Roberts96d790e2013-09-19 17:32:00 +01001244 struct wl_resource *resource;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001245 struct wl_resource *surface_resource;
Rob Bradford880ebc72013-07-22 17:31:38 +01001246 struct wl_display *display = pointer->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001247 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01001248 struct wl_list *focus_resource_list;
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001249 int refocus = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001250
1251 if ((!pointer->focus && view) ||
1252 (pointer->focus && !view) ||
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001253 (pointer->focus && pointer->focus->surface != view->surface) ||
1254 pointer->sx != sx || pointer->sy != sy)
1255 refocus = 1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001256
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001257 if (pointer->focus_client && refocus) {
1258 focus_resource_list = &pointer->focus_client->pointer_resources;
1259 if (!wl_list_empty(focus_resource_list)) {
1260 serial = wl_display_next_serial(display);
1261 surface_resource = pointer->focus->surface->resource;
1262 wl_resource_for_each(resource, focus_resource_list) {
1263 wl_pointer_send_leave(resource, serial,
1264 surface_resource);
Peter Hutterer87743e92016-01-18 16:38:22 +10001265 pointer_send_frame(resource);
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001266 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001267 }
1268
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001269 pointer->focus_client = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001270 }
1271
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001272 pointer_client = find_pointer_client_for_view(pointer, view);
1273 if (pointer_client && refocus) {
1274 struct wl_client *surface_client = pointer_client->client;
Neil Roberts96d790e2013-09-19 17:32:00 +01001275
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001276 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001277
Jason Ekstranda7af7042013-10-12 22:38:11 -05001278 if (kbd && kbd->focus != view->surface)
Kristian Høgsbergcb406f12013-10-09 10:54:03 -07001279 send_modifiers_to_client_in_list(surface_client,
1280 &kbd->resource_list,
1281 serial,
1282 kbd);
1283
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001284 pointer->focus_client = pointer_client;
Neil Roberts96d790e2013-09-19 17:32:00 +01001285
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001286 focus_resource_list = &pointer->focus_client->pointer_resources;
Neil Roberts96d790e2013-09-19 17:32:00 +01001287 wl_resource_for_each(resource, focus_resource_list) {
1288 wl_pointer_send_enter(resource,
1289 serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001290 view->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01001291 sx, sy);
Peter Hutterer87743e92016-01-18 16:38:22 +10001292 pointer_send_frame(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001293 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001294
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001295 pointer->focus_serial = serial;
1296 }
1297
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001298 wl_list_remove(&pointer->focus_view_listener.link);
1299 wl_list_init(&pointer->focus_view_listener.link);
1300 wl_list_remove(&pointer->focus_resource_listener.link);
1301 wl_list_init(&pointer->focus_resource_listener.link);
Emilio Pozuelo Monfortaa7a4762013-11-19 11:37:15 +01001302 if (view)
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001303 wl_signal_add(&view->destroy_signal, &pointer->focus_view_listener);
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001304 if (view && view->surface->resource)
1305 wl_resource_add_destroy_listener(view->surface->resource,
1306 &pointer->focus_resource_listener);
1307
1308 pointer->focus = view;
1309 pointer->focus_view_listener.notify = pointer_focus_view_destroyed;
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001310 pointer->sx = sx;
1311 pointer->sy = sy;
1312
Derek Foremanf9318d12015-05-11 15:40:11 -05001313 assert(view || sx == wl_fixed_from_int(-1000000));
1314 assert(view || sy == wl_fixed_from_int(-1000000));
1315
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001316 wl_signal_emit(&pointer->focus_signal, pointer);
1317}
1318
Neil Roberts96d790e2013-09-19 17:32:00 +01001319static void
1320send_enter_to_resource_list(struct wl_list *list,
1321 struct weston_keyboard *keyboard,
1322 struct weston_surface *surface,
1323 uint32_t serial)
1324{
1325 struct wl_resource *resource;
1326
1327 wl_resource_for_each(resource, list) {
1328 send_modifiers_to_resource(keyboard, resource, serial);
1329 wl_keyboard_send_enter(resource, serial,
1330 surface->resource,
1331 &keyboard->keys);
1332 }
1333}
1334
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001335WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001336weston_keyboard_set_focus(struct weston_keyboard *keyboard,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001337 struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001338{
1339 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +01001340 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001341 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01001342 struct wl_list *focus_resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001343
Neil Roberts96d790e2013-09-19 17:32:00 +01001344 focus_resource_list = &keyboard->focus_resource_list;
1345
1346 if (!wl_list_empty(focus_resource_list) && keyboard->focus != surface) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001347 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001348 wl_resource_for_each(resource, focus_resource_list) {
1349 wl_keyboard_send_leave(resource, serial,
1350 keyboard->focus->resource);
1351 }
1352 move_resources(&keyboard->resource_list, focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001353 }
1354
Neil Roberts96d790e2013-09-19 17:32:00 +01001355 if (find_resource_for_surface(&keyboard->resource_list, surface) &&
1356 keyboard->focus != surface) {
1357 struct wl_client *surface_client =
1358 wl_resource_get_client(surface->resource);
1359
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001360 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001361
1362 move_resources_for_client(focus_resource_list,
1363 &keyboard->resource_list,
1364 surface_client);
1365 send_enter_to_resource_list(focus_resource_list,
1366 keyboard,
1367 surface,
1368 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001369 keyboard->focus_serial = serial;
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001370 }
1371
1372 wl_list_remove(&keyboard->focus_resource_listener.link);
1373 wl_list_init(&keyboard->focus_resource_listener.link);
1374 if (surface && surface->resource)
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001375 wl_resource_add_destroy_listener(surface->resource,
1376 &keyboard->focus_resource_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001377
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001378 keyboard->focus = surface;
1379 wl_signal_emit(&keyboard->focus_signal, keyboard);
1380}
1381
Giulio Camuffoa20ca812014-11-22 11:16:56 +02001382/* Users of this function must manually manage the keyboard focus */
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001383WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001384weston_keyboard_start_grab(struct weston_keyboard *keyboard,
1385 struct weston_keyboard_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001386{
1387 keyboard->grab = grab;
1388 grab->keyboard = keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001389}
1390
1391WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001392weston_keyboard_end_grab(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001393{
1394 keyboard->grab = &keyboard->default_grab;
1395}
1396
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001397static void
1398weston_keyboard_cancel_grab(struct weston_keyboard *keyboard)
1399{
1400 keyboard->grab->interface->cancel(keyboard->grab);
1401}
1402
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001403WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001404weston_pointer_start_grab(struct weston_pointer *pointer,
1405 struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001406{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001407 pointer->grab = grab;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001408 grab->pointer = pointer;
Kristian Høgsbergda751b82013-07-04 00:58:07 -04001409 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001410}
1411
1412WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001413weston_pointer_end_grab(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001414{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001415 pointer->grab = &pointer->default_grab;
Kristian Høgsbergda751b82013-07-04 00:58:07 -04001416 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001417}
1418
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001419static void
1420weston_pointer_cancel_grab(struct weston_pointer *pointer)
1421{
1422 pointer->grab->interface->cancel(pointer->grab);
1423}
1424
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001425WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -04001426weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001427{
1428 touch->grab = grab;
1429 grab->touch = touch;
1430}
1431
1432WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -04001433weston_touch_end_grab(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001434{
1435 touch->grab = &touch->default_grab;
1436}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001437
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001438static void
1439weston_touch_cancel_grab(struct weston_touch *touch)
1440{
1441 touch->grab->interface->cancel(touch->grab);
1442}
1443
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001444static void
1445weston_pointer_clamp_for_output(struct weston_pointer *pointer,
1446 struct weston_output *output,
1447 wl_fixed_t *fx, wl_fixed_t *fy)
1448{
1449 int x, y;
1450
1451 x = wl_fixed_to_int(*fx);
1452 y = wl_fixed_to_int(*fy);
1453
1454 if (x < output->x)
1455 *fx = wl_fixed_from_int(output->x);
1456 else if (x >= output->x + output->width)
1457 *fx = wl_fixed_from_int(output->x +
1458 output->width - 1);
1459 if (y < output->y)
1460 *fy = wl_fixed_from_int(output->y);
1461 else if (y >= output->y + output->height)
1462 *fy = wl_fixed_from_int(output->y +
1463 output->height - 1);
1464}
1465
Rob Bradford806d8c02013-06-25 18:56:41 +01001466WL_EXPORT void
1467weston_pointer_clamp(struct weston_pointer *pointer, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001468{
Rob Bradford806d8c02013-06-25 18:56:41 +01001469 struct weston_compositor *ec = pointer->seat->compositor;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001470 struct weston_output *output, *prev = NULL;
1471 int x, y, old_x, old_y, valid = 0;
1472
1473 x = wl_fixed_to_int(*fx);
1474 y = wl_fixed_to_int(*fy);
Rob Bradford806d8c02013-06-25 18:56:41 +01001475 old_x = wl_fixed_to_int(pointer->x);
1476 old_y = wl_fixed_to_int(pointer->y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001477
1478 wl_list_for_each(output, &ec->output_list, link) {
Rob Bradford66bd9f52013-06-25 18:56:42 +01001479 if (pointer->seat->output && pointer->seat->output != output)
1480 continue;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001481 if (pixman_region32_contains_point(&output->region,
1482 x, y, NULL))
1483 valid = 1;
1484 if (pixman_region32_contains_point(&output->region,
1485 old_x, old_y, NULL))
1486 prev = output;
1487 }
1488
Rob Bradford66bd9f52013-06-25 18:56:42 +01001489 if (!prev)
1490 prev = pointer->seat->output;
1491
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001492 if (prev && !valid)
1493 weston_pointer_clamp_for_output(pointer, prev, fx, fy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001494}
1495
Jonas Ådahld2510102014-10-05 21:39:14 +02001496static void
1497weston_pointer_move_to(struct weston_pointer *pointer,
1498 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001499{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001500 int32_t ix, iy;
1501
Rob Bradford806d8c02013-06-25 18:56:41 +01001502 weston_pointer_clamp (pointer, &x, &y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001503
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001504 pointer->x = x;
1505 pointer->y = y;
1506
1507 ix = wl_fixed_to_int(x);
1508 iy = wl_fixed_to_int(y);
1509
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001510 if (pointer->sprite) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001511 weston_view_set_position(pointer->sprite,
1512 ix - pointer->hotspot_x,
1513 iy - pointer->hotspot_y);
1514 weston_view_schedule_repaint(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001515 }
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001516
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001517 pointer->grab->interface->focus(pointer->grab);
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001518 wl_signal_emit(&pointer->motion_signal, pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001519}
1520
Jonas Ådahld2510102014-10-05 21:39:14 +02001521WL_EXPORT void
Jonas Ådahld2510102014-10-05 21:39:14 +02001522weston_pointer_move(struct weston_pointer *pointer,
1523 struct weston_pointer_motion_event *event)
1524{
1525 wl_fixed_t x, y;
1526
1527 weston_pointer_motion_to_abs(pointer, event, &x, &y);
1528 weston_pointer_move_to(pointer, x, y);
1529}
1530
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001531/** Verify if the pointer is in a valid position and move it if it isn't.
1532 */
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001533static void
1534weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001535{
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001536 struct weston_pointer *pointer;
1537 struct weston_compositor *ec;
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001538 struct weston_output *output, *closest = NULL;
1539 int x, y, distance, min = INT_MAX;
1540 wl_fixed_t fx, fy;
1541
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001542 pointer = container_of(listener, struct weston_pointer,
1543 output_destroy_listener);
1544 ec = pointer->seat->compositor;
1545
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001546 x = wl_fixed_to_int(pointer->x);
1547 y = wl_fixed_to_int(pointer->y);
1548
1549 wl_list_for_each(output, &ec->output_list, link) {
1550 if (pixman_region32_contains_point(&output->region,
1551 x, y, NULL))
1552 return;
1553
1554 /* Aproximante the distance from the pointer to the center of
1555 * the output. */
1556 distance = abs(output->x + output->width / 2 - x) +
1557 abs(output->y + output->height / 2 - y);
1558 if (distance < min) {
1559 min = distance;
1560 closest = output;
1561 }
1562 }
1563
1564 /* Nothing to do if there's no output left. */
1565 if (!closest)
1566 return;
1567
1568 fx = pointer->x;
1569 fy = pointer->y;
1570
1571 weston_pointer_clamp_for_output(pointer, closest, &fx, &fy);
Jonas Ådahld2510102014-10-05 21:39:14 +02001572 weston_pointer_move_to(pointer, fx, fy);
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001573}
1574
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001575WL_EXPORT void
1576notify_motion(struct weston_seat *seat,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001577 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001578 struct weston_pointer_motion_event *event)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001579{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001580 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001581 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001582
1583 weston_compositor_wake(ec);
Jonas Ådahld2510102014-10-05 21:39:14 +02001584 pointer->grab->interface->motion(pointer->grab, time, event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001585}
1586
Daniel Stone96d47c02013-11-19 11:37:12 +01001587static void
1588run_modifier_bindings(struct weston_seat *seat, uint32_t old, uint32_t new)
1589{
1590 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001591 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Daniel Stone96d47c02013-11-19 11:37:12 +01001592 uint32_t diff;
1593 unsigned int i;
1594 struct {
1595 uint32_t xkb;
1596 enum weston_keyboard_modifier weston;
1597 } mods[] = {
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001598 { keyboard->xkb_info->ctrl_mod, MODIFIER_CTRL },
1599 { keyboard->xkb_info->alt_mod, MODIFIER_ALT },
1600 { keyboard->xkb_info->super_mod, MODIFIER_SUPER },
1601 { keyboard->xkb_info->shift_mod, MODIFIER_SHIFT },
Daniel Stone96d47c02013-11-19 11:37:12 +01001602 };
1603
1604 diff = new & ~old;
1605 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
1606 if (diff & (1 << mods[i].xkb))
1607 weston_compositor_run_modifier_binding(compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001608 keyboard,
Daniel Stone96d47c02013-11-19 11:37:12 +01001609 mods[i].weston,
1610 WL_KEYBOARD_KEY_STATE_PRESSED);
1611 }
1612
1613 diff = old & ~new;
1614 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
1615 if (diff & (1 << mods[i].xkb))
1616 weston_compositor_run_modifier_binding(compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001617 keyboard,
Daniel Stone96d47c02013-11-19 11:37:12 +01001618 mods[i].weston,
1619 WL_KEYBOARD_KEY_STATE_RELEASED);
1620 }
1621}
1622
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001623WL_EXPORT void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001624notify_motion_absolute(struct weston_seat *seat, const struct timespec *time,
1625 double x, double y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001626{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001627 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001628 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Jonas Ådahld2510102014-10-05 21:39:14 +02001629 struct weston_pointer_motion_event event = { 0 };
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001630
1631 weston_compositor_wake(ec);
Jonas Ådahld2510102014-10-05 21:39:14 +02001632
1633 event = (struct weston_pointer_motion_event) {
1634 .mask = WESTON_POINTER_MOTION_ABS,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001635 .x = x,
1636 .y = y,
Jonas Ådahld2510102014-10-05 21:39:14 +02001637 };
1638
1639 pointer->grab->interface->motion(pointer->grab, time, &event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001640}
1641
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02001642static unsigned int
1643peek_next_activate_serial(struct weston_compositor *c)
1644{
1645 unsigned serial = c->activate_serial + 1;
1646
1647 return serial == 0 ? 1 : serial;
1648}
1649
1650static void
1651inc_activate_serial(struct weston_compositor *c)
1652{
1653 c->activate_serial = peek_next_activate_serial (c);
1654}
1655
1656WL_EXPORT void
1657weston_view_activate(struct weston_view *view,
1658 struct weston_seat *seat,
1659 uint32_t flags)
1660{
1661 struct weston_compositor *compositor = seat->compositor;
1662
1663 if (flags & WESTON_ACTIVATE_FLAG_CLICKED) {
1664 view->click_to_activate_serial =
1665 peek_next_activate_serial(compositor);
1666 }
1667
1668 weston_seat_set_keyboard_focus(seat, view->surface);
1669}
1670
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001671WL_EXPORT void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001672notify_button(struct weston_seat *seat, const struct timespec *time,
1673 int32_t button, enum wl_pointer_button_state state)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001674{
1675 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001676 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001677
1678 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001679 weston_compositor_idle_inhibit(compositor);
1680 if (pointer->button_count == 0) {
1681 pointer->grab_button = button;
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001682 pointer->grab_time = *time;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001683 pointer->grab_x = pointer->x;
1684 pointer->grab_y = pointer->y;
1685 }
1686 pointer->button_count++;
1687 } else {
1688 weston_compositor_idle_release(compositor);
1689 pointer->button_count--;
1690 }
1691
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001692 weston_compositor_run_button_binding(compositor, pointer, time, button,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001693 state);
1694
1695 pointer->grab->interface->button(pointer->grab, time, button, state);
1696
1697 if (pointer->button_count == 1)
1698 pointer->grab_serial =
1699 wl_display_get_serial(compositor->wl_display);
1700}
1701
1702WL_EXPORT void
Alexandros Frantzis80321942017-11-16 18:20:56 +02001703notify_axis(struct weston_seat *seat, const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001704 struct weston_pointer_axis_event *event)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001705{
1706 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001707 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001708
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001709 weston_compositor_wake(compositor);
1710
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001711 if (weston_compositor_run_axis_binding(compositor, pointer,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001712 time, event))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001713 return;
1714
Peter Hutterer89b6a492016-01-18 15:58:17 +10001715 pointer->grab->interface->axis(pointer->grab, time, event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001716}
1717
Peter Hutterer87743e92016-01-18 16:38:22 +10001718WL_EXPORT void
1719notify_axis_source(struct weston_seat *seat, uint32_t source)
1720{
1721 struct weston_compositor *compositor = seat->compositor;
1722 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1723
1724 weston_compositor_wake(compositor);
1725
1726 pointer->grab->interface->axis_source(pointer->grab, source);
1727}
1728
1729WL_EXPORT void
1730notify_pointer_frame(struct weston_seat *seat)
1731{
1732 struct weston_compositor *compositor = seat->compositor;
1733 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1734
1735 weston_compositor_wake(compositor);
1736
1737 pointer->grab->interface->frame(pointer->grab);
1738}
1739
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001740WL_EXPORT int
1741weston_keyboard_set_locks(struct weston_keyboard *keyboard,
1742 uint32_t mask, uint32_t value)
1743{
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001744 uint32_t serial;
1745 xkb_mod_mask_t mods_depressed, mods_latched, mods_locked, group;
1746 xkb_mod_mask_t num, caps;
1747
1748 /* We don't want the leds to go out of sync with the actual state
1749 * so if the backend has no way to change the leds don't try to
1750 * change the state */
1751 if (!keyboard->seat->led_update)
1752 return -1;
1753
1754 mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
1755 XKB_STATE_DEPRESSED);
1756 mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
1757 XKB_STATE_LATCHED);
1758 mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
1759 XKB_STATE_LOCKED);
1760 group = xkb_state_serialize_group(keyboard->xkb_state.state,
1761 XKB_STATE_EFFECTIVE);
1762
1763 num = (1 << keyboard->xkb_info->mod2_mod);
1764 caps = (1 << keyboard->xkb_info->caps_mod);
1765 if (mask & WESTON_NUM_LOCK) {
1766 if (value & WESTON_NUM_LOCK)
1767 mods_locked |= num;
1768 else
1769 mods_locked &= ~num;
1770 }
1771 if (mask & WESTON_CAPS_LOCK) {
1772 if (value & WESTON_CAPS_LOCK)
1773 mods_locked |= caps;
1774 else
1775 mods_locked &= ~caps;
1776 }
1777
1778 xkb_state_update_mask(keyboard->xkb_state.state, mods_depressed,
1779 mods_latched, mods_locked, 0, 0, group);
1780
1781 serial = wl_display_next_serial(
1782 keyboard->seat->compositor->wl_display);
1783 notify_modifiers(keyboard->seat, serial);
1784
1785 return 0;
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001786}
1787
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001788WL_EXPORT void
1789notify_modifiers(struct weston_seat *seat, uint32_t serial)
1790{
Derek Foreman1281a362015-07-31 16:55:32 -05001791 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001792 struct weston_keyboard_grab *grab = keyboard->grab;
1793 uint32_t mods_depressed, mods_latched, mods_locked, group;
1794 uint32_t mods_lookup;
1795 enum weston_led leds = 0;
1796 int changed = 0;
1797
1798 /* Serialize and update our internal state, checking to see if it's
1799 * different to the previous state. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001800 mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03001801 XKB_STATE_MODS_DEPRESSED);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001802 mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03001803 XKB_STATE_MODS_LATCHED);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001804 mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03001805 XKB_STATE_MODS_LOCKED);
1806 group = xkb_state_serialize_layout(keyboard->xkb_state.state,
1807 XKB_STATE_LAYOUT_EFFECTIVE);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001808
Derek Foreman244e99e2015-06-03 15:53:26 -05001809 if (mods_depressed != keyboard->modifiers.mods_depressed ||
1810 mods_latched != keyboard->modifiers.mods_latched ||
1811 mods_locked != keyboard->modifiers.mods_locked ||
1812 group != keyboard->modifiers.group)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001813 changed = 1;
1814
Derek Foreman244e99e2015-06-03 15:53:26 -05001815 run_modifier_bindings(seat, keyboard->modifiers.mods_depressed,
Daniel Stone96d47c02013-11-19 11:37:12 +01001816 mods_depressed);
1817
Derek Foreman244e99e2015-06-03 15:53:26 -05001818 keyboard->modifiers.mods_depressed = mods_depressed;
1819 keyboard->modifiers.mods_latched = mods_latched;
1820 keyboard->modifiers.mods_locked = mods_locked;
1821 keyboard->modifiers.group = group;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001822
1823 /* And update the modifier_state for bindings. */
1824 mods_lookup = mods_depressed | mods_latched;
1825 seat->modifier_state = 0;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001826 if (mods_lookup & (1 << keyboard->xkb_info->ctrl_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001827 seat->modifier_state |= MODIFIER_CTRL;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001828 if (mods_lookup & (1 << keyboard->xkb_info->alt_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001829 seat->modifier_state |= MODIFIER_ALT;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001830 if (mods_lookup & (1 << keyboard->xkb_info->super_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001831 seat->modifier_state |= MODIFIER_SUPER;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001832 if (mods_lookup & (1 << keyboard->xkb_info->shift_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001833 seat->modifier_state |= MODIFIER_SHIFT;
1834
1835 /* Finally, notify the compositor that LEDs have changed. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001836 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
1837 keyboard->xkb_info->num_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001838 leds |= LED_NUM_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001839 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
1840 keyboard->xkb_info->caps_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001841 leds |= LED_CAPS_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001842 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
1843 keyboard->xkb_info->scroll_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001844 leds |= LED_SCROLL_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001845 if (leds != keyboard->xkb_state.leds && seat->led_update)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001846 seat->led_update(seat, leds);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001847 keyboard->xkb_state.leds = leds;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001848
1849 if (changed) {
1850 grab->interface->modifiers(grab,
1851 serial,
1852 keyboard->modifiers.mods_depressed,
1853 keyboard->modifiers.mods_latched,
1854 keyboard->modifiers.mods_locked,
1855 keyboard->modifiers.group);
1856 }
1857}
1858
1859static void
1860update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
1861 enum wl_keyboard_key_state state)
1862{
Derek Foreman1281a362015-07-31 16:55:32 -05001863 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001864 enum xkb_key_direction direction;
1865
1866 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1867 direction = XKB_KEY_DOWN;
1868 else
1869 direction = XKB_KEY_UP;
1870
1871 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
1872 * broken keycode system, which starts at 8. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001873 xkb_state_update_key(keyboard->xkb_state.state, key + 8, direction);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001874
1875 notify_modifiers(seat, serial);
1876}
Rui Matos65196bc2013-10-10 19:44:19 +02001877
1878static void
1879send_keymap(struct wl_resource *resource, struct weston_xkb_info *xkb_info)
1880{
1881 wl_keyboard_send_keymap(resource,
1882 WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
1883 xkb_info->keymap_fd,
1884 xkb_info->keymap_size);
1885}
1886
1887static void
1888send_modifiers(struct wl_resource *resource, uint32_t serial, struct weston_keyboard *keyboard)
1889{
1890 wl_keyboard_send_modifiers(resource, serial,
1891 keyboard->modifiers.mods_depressed,
1892 keyboard->modifiers.mods_latched,
1893 keyboard->modifiers.mods_locked,
1894 keyboard->modifiers.group);
1895}
1896
1897static struct weston_xkb_info *
1898weston_xkb_info_create(struct xkb_keymap *keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02001899
1900static void
1901update_keymap(struct weston_seat *seat)
1902{
Derek Foreman1281a362015-07-31 16:55:32 -05001903 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Rui Matos65196bc2013-10-10 19:44:19 +02001904 struct wl_resource *resource;
1905 struct weston_xkb_info *xkb_info;
1906 struct xkb_state *state;
1907 xkb_mod_mask_t latched_mods;
1908 xkb_mod_mask_t locked_mods;
1909
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001910 xkb_info = weston_xkb_info_create(keyboard->pending_keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02001911
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001912 xkb_keymap_unref(keyboard->pending_keymap);
1913 keyboard->pending_keymap = NULL;
Rui Matos65196bc2013-10-10 19:44:19 +02001914
1915 if (!xkb_info) {
1916 weston_log("failed to create XKB info\n");
1917 return;
1918 }
1919
1920 state = xkb_state_new(xkb_info->keymap);
1921 if (!state) {
1922 weston_log("failed to initialise XKB state\n");
1923 weston_xkb_info_destroy(xkb_info);
1924 return;
1925 }
1926
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001927 latched_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
1928 XKB_STATE_MODS_LATCHED);
1929 locked_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
1930 XKB_STATE_MODS_LOCKED);
Rui Matos65196bc2013-10-10 19:44:19 +02001931 xkb_state_update_mask(state,
1932 0, /* depressed */
1933 latched_mods,
1934 locked_mods,
1935 0, 0, 0);
1936
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001937 weston_xkb_info_destroy(keyboard->xkb_info);
1938 keyboard->xkb_info = xkb_info;
Rui Matos65196bc2013-10-10 19:44:19 +02001939
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001940 xkb_state_unref(keyboard->xkb_state.state);
1941 keyboard->xkb_state.state = state;
Rui Matos65196bc2013-10-10 19:44:19 +02001942
Derek Foremanbc91e542015-06-03 15:53:27 -05001943 wl_resource_for_each(resource, &keyboard->resource_list)
Rui Matos65196bc2013-10-10 19:44:19 +02001944 send_keymap(resource, xkb_info);
Derek Foremanbc91e542015-06-03 15:53:27 -05001945 wl_resource_for_each(resource, &keyboard->focus_resource_list)
Rui Matos65196bc2013-10-10 19:44:19 +02001946 send_keymap(resource, xkb_info);
1947
1948 notify_modifiers(seat, wl_display_next_serial(seat->compositor->wl_display));
1949
1950 if (!latched_mods && !locked_mods)
1951 return;
1952
Derek Foremanbc91e542015-06-03 15:53:27 -05001953 wl_resource_for_each(resource, &keyboard->resource_list)
1954 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), keyboard);
1955 wl_resource_for_each(resource, &keyboard->focus_resource_list)
1956 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), keyboard);
Rui Matos65196bc2013-10-10 19:44:19 +02001957}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001958
1959WL_EXPORT void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02001960notify_key(struct weston_seat *seat, const struct timespec *time, uint32_t key,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001961 enum wl_keyboard_key_state state,
1962 enum weston_key_state_update update_state)
1963{
1964 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001965 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001966 struct weston_keyboard_grab *grab = keyboard->grab;
Pekka Paalanen86b53962014-11-19 13:43:32 +02001967 uint32_t *k, *end;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001968
1969 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001970 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001971 } else {
1972 weston_compositor_idle_release(compositor);
1973 }
1974
Pekka Paalanen86b53962014-11-19 13:43:32 +02001975 end = keyboard->keys.data + keyboard->keys.size;
1976 for (k = keyboard->keys.data; k < end; k++) {
1977 if (*k == key) {
1978 /* Ignore server-generated repeats. */
1979 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1980 return;
1981 *k = *--end;
1982 }
1983 }
1984 keyboard->keys.size = (void *) end - keyboard->keys.data;
1985 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1986 k = wl_array_add(&keyboard->keys, sizeof *k);
1987 *k = key;
1988 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001989
1990 if (grab == &keyboard->default_grab ||
1991 grab == &keyboard->input_method_grab) {
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001992 weston_compositor_run_key_binding(compositor, keyboard, time,
1993 key, state);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001994 grab = keyboard->grab;
1995 }
1996
1997 grab->interface->key(grab, time, key, state);
1998
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001999 if (keyboard->pending_keymap &&
Pekka Paalanen86b53962014-11-19 13:43:32 +02002000 keyboard->keys.size == 0)
Rui Matos65196bc2013-10-10 19:44:19 +02002001 update_keymap(seat);
2002
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002003 if (update_state == STATE_UPDATE_AUTOMATIC) {
2004 update_modifier_state(seat,
2005 wl_display_get_serial(compositor->wl_display),
2006 key,
2007 state);
2008 }
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002009
Olivier Fourdandf84dbe2016-06-30 16:01:56 +02002010 keyboard->grab_serial = wl_display_get_serial(compositor->wl_display);
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002011 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02002012 keyboard->grab_time = *time;
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002013 keyboard->grab_key = key;
2014 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002015}
2016
2017WL_EXPORT void
2018notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002019 double x, double y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002020{
Derek Foreman1281a362015-07-31 16:55:32 -05002021 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2022
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002023 if (output) {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002024 weston_pointer_move_to(pointer,
2025 wl_fixed_from_double(x),
2026 wl_fixed_from_double(y));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002027 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002028 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002029 * NULL) here, but somehow that breaks re-entry... */
2030 }
2031}
2032
2033static void
2034destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
2035{
2036 struct weston_seat *ws;
2037
2038 ws = container_of(listener, struct weston_seat,
2039 saved_kbd_focus_listener);
2040
2041 ws->saved_kbd_focus = NULL;
2042}
2043
2044WL_EXPORT void
2045notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
2046 enum weston_key_state_update update_state)
2047{
2048 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002049 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04002050 struct weston_surface *surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002051 uint32_t *k, serial;
2052
2053 serial = wl_display_next_serial(compositor->wl_display);
2054 wl_array_copy(&keyboard->keys, keys);
2055 wl_array_for_each(k, &keyboard->keys) {
2056 weston_compositor_idle_inhibit(compositor);
2057 if (update_state == STATE_UPDATE_AUTOMATIC)
2058 update_modifier_state(seat, serial, *k,
2059 WL_KEYBOARD_KEY_STATE_PRESSED);
2060 }
2061
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002062 surface = seat->saved_kbd_focus;
2063
2064 if (surface) {
2065 wl_list_remove(&seat->saved_kbd_focus_listener.link);
2066 weston_keyboard_set_focus(keyboard, surface);
2067 seat->saved_kbd_focus = NULL;
2068 }
2069}
2070
2071WL_EXPORT void
2072notify_keyboard_focus_out(struct weston_seat *seat)
2073{
2074 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002075 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2076 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002077 uint32_t *k, serial;
2078
2079 serial = wl_display_next_serial(compositor->wl_display);
2080 wl_array_for_each(k, &keyboard->keys) {
2081 weston_compositor_idle_release(compositor);
2082 update_modifier_state(seat, serial, *k,
2083 WL_KEYBOARD_KEY_STATE_RELEASED);
2084 }
2085
2086 seat->modifier_state = 0;
2087
2088 if (keyboard->focus) {
2089 seat->saved_kbd_focus = keyboard->focus;
2090 seat->saved_kbd_focus_listener.notify =
2091 destroy_device_saved_kbd_focus;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002092 wl_signal_add(&keyboard->focus->destroy_signal,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002093 &seat->saved_kbd_focus_listener);
2094 }
2095
2096 weston_keyboard_set_focus(keyboard, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002097 weston_keyboard_cancel_grab(keyboard);
Derek Foreman1281a362015-07-31 16:55:32 -05002098 if (pointer)
2099 weston_pointer_cancel_grab(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002100}
2101
Michael Fua2bb7912013-07-23 15:51:06 +08002102WL_EXPORT void
Derek Foreman4c93c082015-04-30 16:45:41 -05002103weston_touch_set_focus(struct weston_touch *touch, struct weston_view *view)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002104{
Neil Roberts96d790e2013-09-19 17:32:00 +01002105 struct wl_list *focus_resource_list;
2106
Derek Foreman4c93c082015-04-30 16:45:41 -05002107 focus_resource_list = &touch->focus_resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002108
Derek Foreman4c93c082015-04-30 16:45:41 -05002109 if (view && touch->focus &&
2110 touch->focus->surface == view->surface) {
2111 touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002112 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002113 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002114
Derek Foreman4c93c082015-04-30 16:45:41 -05002115 wl_list_remove(&touch->focus_resource_listener.link);
2116 wl_list_init(&touch->focus_resource_listener.link);
2117 wl_list_remove(&touch->focus_view_listener.link);
2118 wl_list_init(&touch->focus_view_listener.link);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01002119
Neil Roberts96d790e2013-09-19 17:32:00 +01002120 if (!wl_list_empty(focus_resource_list)) {
Derek Foreman4c93c082015-04-30 16:45:41 -05002121 move_resources(&touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002122 focus_resource_list);
2123 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002124
Jason Ekstranda7af7042013-10-12 22:38:11 -05002125 if (view) {
Derek Foreman362656b2014-09-04 10:23:05 -05002126 struct wl_client *surface_client;
2127
2128 if (!view->surface->resource) {
Derek Foreman4c93c082015-04-30 16:45:41 -05002129 touch->focus = NULL;
Derek Foreman362656b2014-09-04 10:23:05 -05002130 return;
2131 }
2132
2133 surface_client = wl_resource_get_client(view->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +01002134 move_resources_for_client(focus_resource_list,
Derek Foreman4c93c082015-04-30 16:45:41 -05002135 &touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002136 surface_client);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01002137 wl_resource_add_destroy_listener(view->surface->resource,
Derek Foreman4c93c082015-04-30 16:45:41 -05002138 &touch->focus_resource_listener);
2139 wl_signal_add(&view->destroy_signal, &touch->focus_view_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002140 }
Derek Foreman4c93c082015-04-30 16:45:41 -05002141 touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002142}
2143
2144/**
2145 * notify_touch - emulates button touches and notifies surfaces accordingly.
2146 *
2147 * It assumes always the correct cycle sequence until it gets here: touch_down
2148 * → touch_update → ... → touch_update → touch_end. The driver is responsible
2149 * for sending along such order.
2150 *
2151 */
2152WL_EXPORT void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02002153notify_touch(struct weston_seat *seat, const struct timespec *time,
2154 int touch_id, double double_x, double double_y, int touch_type)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002155{
2156 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002157 struct weston_touch *touch = weston_seat_get_touch(seat);
Kristian Høgsberge329f362013-05-06 22:19:57 -04002158 struct weston_touch_grab *grab = touch->grab;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002159 struct weston_view *ev;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002160 wl_fixed_t sx, sy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002161 wl_fixed_t x = wl_fixed_from_double(double_x);
2162 wl_fixed_t y = wl_fixed_from_double(double_y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002163
2164 /* Update grab's global coordinates. */
Neil Roberts306fe082013-10-03 16:43:06 +01002165 if (touch_id == touch->grab_touch_id && touch_type != WL_TOUCH_UP) {
2166 touch->grab_x = x;
2167 touch->grab_y = y;
2168 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002169
2170 switch (touch_type) {
2171 case WL_TOUCH_DOWN:
2172 weston_compositor_idle_inhibit(ec);
2173
Jonas Ådahl9484b692013-12-02 22:05:03 +01002174 touch->num_tp++;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002175
Jason Ekstranda7af7042013-10-12 22:38:11 -05002176 /* the first finger down picks the view, and all further go
2177 * to that view for the remainder of the touch session i.e.
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002178 * until all touch points are up again. */
Jonas Ådahl9484b692013-12-02 22:05:03 +01002179 if (touch->num_tp == 1) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002180 ev = weston_compositor_pick_view(ec, x, y, &sx, &sy);
Derek Foreman4c93c082015-04-30 16:45:41 -05002181 weston_touch_set_focus(touch, ev);
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002182 } else if (!touch->focus) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002183 /* Unexpected condition: We have non-initial touch but
2184 * there is no focused surface.
2185 */
Chris Michael3f607d32015-10-07 11:59:49 -04002186 weston_log("touch event received with %d points down "
Jonas Ådahl9484b692013-12-02 22:05:03 +01002187 "but no surface focused\n", touch->num_tp);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002188 return;
2189 }
2190
Derek Foreman99a6a2d2015-07-15 13:00:43 -05002191 weston_compositor_run_touch_binding(ec, touch,
Kristian Høgsbergc8964012014-02-05 14:25:18 -08002192 time, touch_type);
2193
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002194 grab->interface->down(grab, time, touch_id, x, y);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002195 if (touch->num_tp == 1) {
Rusty Lynchf1407ff2013-08-08 21:13:57 -07002196 touch->grab_serial =
2197 wl_display_get_serial(ec->wl_display);
Neil Roberts306fe082013-10-03 16:43:06 +01002198 touch->grab_touch_id = touch_id;
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02002199 touch->grab_time = *time;
Rusty Lynchf1407ff2013-08-08 21:13:57 -07002200 touch->grab_x = x;
2201 touch->grab_y = y;
2202 }
2203
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002204 break;
2205 case WL_TOUCH_MOTION:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002206 ev = touch->focus;
2207 if (!ev)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002208 break;
2209
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +02002210 grab->interface->motion(grab, time, touch_id, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002211 break;
2212 case WL_TOUCH_UP:
Kristian Høgsberga30e29a2014-01-08 22:29:20 -08002213 if (touch->num_tp == 0) {
2214 /* This can happen if we start out with one or
2215 * more fingers on the touch screen, in which
2216 * case we didn't get the corresponding down
2217 * event. */
2218 weston_log("unmatched touch up event\n");
2219 break;
2220 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002221 weston_compositor_idle_release(ec);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002222 touch->num_tp--;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002223
Alexandros Frantzis27a51b82017-11-16 18:20:59 +02002224 grab->interface->up(grab, time, touch_id);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002225 if (touch->num_tp == 0)
Derek Foreman4c93c082015-04-30 16:45:41 -05002226 weston_touch_set_focus(touch, NULL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002227 break;
2228 }
2229}
2230
Jonas Ådahl1679f232014-04-12 09:39:51 +02002231WL_EXPORT void
2232notify_touch_frame(struct weston_seat *seat)
2233{
Derek Foreman1281a362015-07-31 16:55:32 -05002234 struct weston_touch *touch = weston_seat_get_touch(seat);
Jonas Ådahl1679f232014-04-12 09:39:51 +02002235 struct weston_touch_grab *grab = touch->grab;
2236
2237 grab->interface->frame(grab);
2238}
2239
Derek Foreman3cc004a2015-11-06 15:56:09 -06002240WL_EXPORT void
2241notify_touch_cancel(struct weston_seat *seat)
2242{
2243 struct weston_touch *touch = weston_seat_get_touch(seat);
2244 struct weston_touch_grab *grab = touch->grab;
2245
2246 grab->interface->cancel(grab);
2247}
2248
Pekka Paalanen8274d902014-08-06 19:36:51 +03002249static int
2250pointer_cursor_surface_get_label(struct weston_surface *surface,
2251 char *buf, size_t len)
2252{
2253 return snprintf(buf, len, "cursor");
2254}
2255
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002256static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002257pointer_cursor_surface_committed(struct weston_surface *es,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002258 int32_t dx, int32_t dy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002259{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002260 struct weston_pointer *pointer = es->committed_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002261 int x, y;
2262
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002263 if (es->width == 0)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002264 return;
2265
Jason Ekstranda7af7042013-10-12 22:38:11 -05002266 assert(es == pointer->sprite->surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002267
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002268 pointer->hotspot_x -= dx;
2269 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002270
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002271 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
2272 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002273
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002274 weston_view_set_position(pointer->sprite, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002275
2276 empty_region(&es->pending.input);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02002277 empty_region(&es->input);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002278
2279 if (!weston_surface_is_mapped(es)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002280 weston_layer_entry_insert(&es->compositor->cursor_layer.view_list,
2281 &pointer->sprite->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002282 weston_view_update_transform(pointer->sprite);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002283 es->is_mapped = true;
2284 pointer->sprite->is_mapped = true;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002285 }
2286}
2287
2288static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002289pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2290 uint32_t serial, struct wl_resource *surface_resource,
2291 int32_t x, int32_t y)
2292{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002293 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002294 struct weston_surface *surface = NULL;
2295
2296 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002297 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002298
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002299 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002300 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002301 /* pointer->focus->surface->resource can be NULL. Surfaces like the
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02002302 black_surface used in shell.c for fullscreen don't have
2303 a resource, but can still have focus */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002304 if (pointer->focus->surface->resource == NULL)
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02002305 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002306 if (wl_resource_get_client(pointer->focus->surface->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002307 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002308 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002309 return;
2310
Derek Foreman4e53c532015-03-23 10:55:32 -05002311 if (!surface) {
2312 if (pointer->sprite)
2313 pointer_unmap_sprite(pointer);
2314 return;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002315 }
2316
Jonas Ådahlb4070242015-03-18 15:08:03 +08002317 if (pointer->sprite && pointer->sprite->surface == surface &&
2318 pointer->hotspot_x == x && pointer->hotspot_y == y)
2319 return;
2320
Derek Foreman4e53c532015-03-23 10:55:32 -05002321 if (!pointer->sprite || pointer->sprite->surface != surface) {
2322 if (weston_surface_set_role(surface, "wl_pointer-cursor",
2323 resource,
2324 WL_POINTER_ERROR_ROLE) < 0)
2325 return;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002326
Derek Foreman4e53c532015-03-23 10:55:32 -05002327 if (pointer->sprite)
2328 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002329
Derek Foreman4e53c532015-03-23 10:55:32 -05002330 wl_signal_add(&surface->destroy_signal,
2331 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002332
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002333 surface->committed = pointer_cursor_surface_committed;
2334 surface->committed_private = pointer;
Derek Foreman4e53c532015-03-23 10:55:32 -05002335 weston_surface_set_label_func(surface,
2336 pointer_cursor_surface_get_label);
2337 pointer->sprite = weston_view_create(surface);
2338 }
2339
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002340 pointer->hotspot_x = x;
2341 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002342
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +02002343 if (surface->buffer_ref.buffer) {
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002344 pointer_cursor_surface_committed(surface, 0, 0);
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +02002345 weston_view_schedule_repaint(pointer->sprite);
2346 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002347}
2348
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002349static void
2350pointer_release(struct wl_client *client, struct wl_resource *resource)
2351{
2352 wl_resource_destroy(resource);
2353}
2354
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002355static const struct wl_pointer_interface pointer_interface = {
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002356 pointer_set_cursor,
2357 pointer_release
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002358};
2359
2360static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002361seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2362 uint32_t id)
2363{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002364 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002365 /* We use the pointer_state directly, which means we'll
2366 * give a wl_pointer if the seat has ever had one - even though
2367 * the spec explicitly states that this request only takes effect
2368 * if the seat has the pointer capability.
2369 *
2370 * This prevents a race between the compositor sending new
2371 * capabilities and the client trying to use the old ones.
2372 */
2373 struct weston_pointer *pointer = seat->pointer_state;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002374 struct wl_resource *cr;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002375 struct weston_pointer_client *pointer_client;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002376
Derek Foreman1281a362015-07-31 16:55:32 -05002377 if (!pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002378 return;
2379
Jason Ekstranda85118c2013-06-27 20:17:02 -05002380 cr = wl_resource_create(client, &wl_pointer_interface,
2381 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002382 if (cr == NULL) {
2383 wl_client_post_no_memory(client);
2384 return;
2385 }
2386
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002387 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
2388 if (!pointer_client) {
2389 wl_client_post_no_memory(client);
2390 return;
2391 }
2392
2393 wl_list_insert(&pointer_client->pointer_resources,
2394 wl_resource_get_link(cr));
Derek Foreman1281a362015-07-31 16:55:32 -05002395 wl_resource_set_implementation(cr, &pointer_interface, pointer,
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002396 unbind_pointer_client_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002397
Derek Foreman1281a362015-07-31 16:55:32 -05002398 if (pointer->focus && pointer->focus->surface->resource &&
2399 wl_resource_get_client(pointer->focus->surface->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002400 wl_fixed_t sx, sy;
2401
Derek Foreman1281a362015-07-31 16:55:32 -05002402 weston_view_from_global_fixed(pointer->focus,
2403 pointer->x,
2404 pointer->y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002405 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002406
Neil Roberts96d790e2013-09-19 17:32:00 +01002407 wl_pointer_send_enter(cr,
Derek Foreman1281a362015-07-31 16:55:32 -05002408 pointer->focus_serial,
2409 pointer->focus->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01002410 sx, sy);
Peter Hutterer87743e92016-01-18 16:38:22 +10002411 pointer_send_frame(cr);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002412 }
2413}
2414
2415static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002416keyboard_release(struct wl_client *client, struct wl_resource *resource)
2417{
2418 wl_resource_destroy(resource);
2419}
2420
2421static const struct wl_keyboard_interface keyboard_interface = {
2422 keyboard_release
2423};
2424
Derek Foreman280e7dd2014-10-03 13:13:42 -05002425static bool
Neil Roberts96d790e2013-09-19 17:32:00 +01002426should_send_modifiers_to_client(struct weston_seat *seat,
2427 struct wl_client *client)
2428{
Derek Foreman1281a362015-07-31 16:55:32 -05002429 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2430 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2431
2432 if (keyboard &&
2433 keyboard->focus &&
2434 keyboard->focus->resource &&
2435 wl_resource_get_client(keyboard->focus->resource) == client)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002436 return true;
Neil Roberts96d790e2013-09-19 17:32:00 +01002437
Derek Foreman1281a362015-07-31 16:55:32 -05002438 if (pointer &&
2439 pointer->focus &&
2440 pointer->focus->surface->resource &&
2441 wl_resource_get_client(pointer->focus->surface->resource) == client)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002442 return true;
Neil Roberts96d790e2013-09-19 17:32:00 +01002443
Derek Foreman280e7dd2014-10-03 13:13:42 -05002444 return false;
Neil Roberts96d790e2013-09-19 17:32:00 +01002445}
2446
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002447static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002448seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2449 uint32_t id)
2450{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002451 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002452 /* We use the keyboard_state directly, which means we'll
2453 * give a wl_keyboard if the seat has ever had one - even though
2454 * the spec explicitly states that this request only takes effect
2455 * if the seat has the keyboard capability.
2456 *
2457 * This prevents a race between the compositor sending new
2458 * capabilities and the client trying to use the old ones.
2459 */
2460 struct weston_keyboard *keyboard = seat->keyboard_state;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002461 struct wl_resource *cr;
2462
Derek Foreman345c9f32015-06-03 15:53:28 -05002463 if (!keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002464 return;
2465
Jason Ekstranda85118c2013-06-27 20:17:02 -05002466 cr = wl_resource_create(client, &wl_keyboard_interface,
2467 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002468 if (cr == NULL) {
2469 wl_client_post_no_memory(client);
2470 return;
2471 }
2472
Neil Roberts96d790e2013-09-19 17:32:00 +01002473 /* May be moved to focused list later by either
2474 * weston_keyboard_set_focus or directly if this client is already
2475 * focused */
Derek Foreman345c9f32015-06-03 15:53:28 -05002476 wl_list_insert(&keyboard->resource_list, wl_resource_get_link(cr));
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002477 wl_resource_set_implementation(cr, &keyboard_interface,
2478 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002479
Jonny Lamb66a41a02014-08-12 14:58:25 +02002480 if (wl_resource_get_version(cr) >= WL_KEYBOARD_REPEAT_INFO_SINCE_VERSION) {
2481 wl_keyboard_send_repeat_info(cr,
2482 seat->compositor->kb_repeat_rate,
2483 seat->compositor->kb_repeat_delay);
2484 }
Jasper St. Pierred8c6aeb2014-08-04 13:43:24 -04002485
Derek Foreman185d1582017-06-28 11:17:23 -05002486 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2487 keyboard->xkb_info->keymap_fd,
2488 keyboard->xkb_info->keymap_size);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002489
Neil Roberts96d790e2013-09-19 17:32:00 +01002490 if (should_send_modifiers_to_client(seat, client)) {
Derek Foreman345c9f32015-06-03 15:53:28 -05002491 send_modifiers_to_resource(keyboard,
Neil Roberts96d790e2013-09-19 17:32:00 +01002492 cr,
Derek Foreman345c9f32015-06-03 15:53:28 -05002493 keyboard->focus_serial);
Neil Roberts96d790e2013-09-19 17:32:00 +01002494 }
2495
Derek Foreman345c9f32015-06-03 15:53:28 -05002496 if (keyboard->focus && keyboard->focus->resource &&
2497 wl_resource_get_client(keyboard->focus->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01002498 struct weston_surface *surface =
Derek Foreman345c9f32015-06-03 15:53:28 -05002499 (struct weston_surface *)keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +01002500
2501 wl_list_remove(wl_resource_get_link(cr));
Derek Foreman345c9f32015-06-03 15:53:28 -05002502 wl_list_insert(&keyboard->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002503 wl_resource_get_link(cr));
2504 wl_keyboard_send_enter(cr,
Derek Foreman345c9f32015-06-03 15:53:28 -05002505 keyboard->focus_serial,
Neil Roberts96d790e2013-09-19 17:32:00 +01002506 surface->resource,
Derek Foreman345c9f32015-06-03 15:53:28 -05002507 &keyboard->keys);
Neil Roberts96d790e2013-09-19 17:32:00 +01002508
2509 /* If this is the first keyboard resource for this
2510 * client... */
Derek Foreman345c9f32015-06-03 15:53:28 -05002511 if (keyboard->focus_resource_list.prev ==
Neil Roberts96d790e2013-09-19 17:32:00 +01002512 wl_resource_get_link(cr))
2513 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002514 }
2515}
2516
2517static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002518touch_release(struct wl_client *client, struct wl_resource *resource)
2519{
2520 wl_resource_destroy(resource);
2521}
2522
2523static const struct wl_touch_interface touch_interface = {
2524 touch_release
2525};
2526
2527static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002528seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2529 uint32_t id)
2530{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002531 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002532 /* We use the touch_state directly, which means we'll
2533 * give a wl_touch if the seat has ever had one - even though
2534 * the spec explicitly states that this request only takes effect
2535 * if the seat has the touch capability.
2536 *
2537 * This prevents a race between the compositor sending new
2538 * capabilities and the client trying to use the old ones.
2539 */
2540 struct weston_touch *touch = seat->touch_state;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002541 struct wl_resource *cr;
2542
Derek Foreman1281a362015-07-31 16:55:32 -05002543 if (!touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002544 return;
2545
Jason Ekstranda85118c2013-06-27 20:17:02 -05002546 cr = wl_resource_create(client, &wl_touch_interface,
2547 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002548 if (cr == NULL) {
2549 wl_client_post_no_memory(client);
2550 return;
2551 }
2552
Derek Foreman1281a362015-07-31 16:55:32 -05002553 if (touch->focus &&
2554 wl_resource_get_client(touch->focus->surface->resource) == client) {
Chokshi, Mituld6697142015-10-09 08:28:47 +00002555 wl_list_insert(&touch->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002556 wl_resource_get_link(cr));
2557 } else {
Chokshi, Mituld6697142015-10-09 08:28:47 +00002558 wl_list_insert(&touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002559 wl_resource_get_link(cr));
2560 }
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002561 wl_resource_set_implementation(cr, &touch_interface,
2562 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002563}
2564
Quentin Glidicaab1d362016-03-13 17:49:08 +01002565static void
2566seat_release(struct wl_client *client, struct wl_resource *resource)
2567{
2568 wl_resource_destroy(resource);
2569}
2570
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002571static const struct wl_seat_interface seat_interface = {
2572 seat_get_pointer,
2573 seat_get_keyboard,
2574 seat_get_touch,
Quentin Glidicaab1d362016-03-13 17:49:08 +01002575 seat_release,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002576};
2577
2578static void
2579bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2580{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002581 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002582 struct wl_resource *resource;
2583 enum wl_seat_capability caps = 0;
2584
Jason Ekstranda85118c2013-06-27 20:17:02 -05002585 resource = wl_resource_create(client,
Derek Foreman1909c102015-11-26 14:17:47 -06002586 &wl_seat_interface, version, id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05002587 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05002588 wl_resource_set_implementation(resource, &seat_interface, data,
2589 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002590
Derek Foreman1281a362015-07-31 16:55:32 -05002591 if (weston_seat_get_pointer(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002592 caps |= WL_SEAT_CAPABILITY_POINTER;
Derek Foreman1281a362015-07-31 16:55:32 -05002593 if (weston_seat_get_keyboard(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002594 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Derek Foreman1281a362015-07-31 16:55:32 -05002595 if (weston_seat_get_touch(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002596 caps |= WL_SEAT_CAPABILITY_TOUCH;
2597
2598 wl_seat_send_capabilities(resource, caps);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04002599 if (version >= WL_SEAT_NAME_SINCE_VERSION)
Rob Bradforde445ae62013-05-31 18:09:51 +01002600 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002601}
2602
Jonas Ådahl30d61d82014-10-22 21:21:17 +02002603static void
2604relative_pointer_destroy(struct wl_client *client,
2605 struct wl_resource *resource)
2606{
2607 wl_resource_destroy(resource);
2608}
2609
2610static const struct zwp_relative_pointer_v1_interface relative_pointer_interface = {
2611 relative_pointer_destroy
2612};
2613
2614static void
2615relative_pointer_manager_destroy(struct wl_client *client,
2616 struct wl_resource *resource)
2617{
2618 wl_resource_destroy(resource);
2619}
2620
2621static void
2622relative_pointer_manager_get_relative_pointer(struct wl_client *client,
2623 struct wl_resource *resource,
2624 uint32_t id,
2625 struct wl_resource *pointer_resource)
2626{
2627 struct weston_pointer *pointer =
2628 wl_resource_get_user_data(pointer_resource);
2629 struct weston_pointer_client *pointer_client;
2630 struct wl_resource *cr;
2631
2632 cr = wl_resource_create(client, &zwp_relative_pointer_v1_interface,
2633 wl_resource_get_version(resource), id);
2634 if (cr == NULL) {
2635 wl_client_post_no_memory(client);
2636 return;
2637 }
2638
2639 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
2640 if (!pointer_client) {
2641 wl_client_post_no_memory(client);
2642 return;
2643 }
2644
2645 wl_list_insert(&pointer_client->relative_pointer_resources,
2646 wl_resource_get_link(cr));
2647 wl_resource_set_implementation(cr, &relative_pointer_interface,
2648 pointer,
2649 unbind_pointer_client_resource);
2650}
2651
2652static const struct zwp_relative_pointer_manager_v1_interface relative_pointer_manager = {
2653 relative_pointer_manager_destroy,
2654 relative_pointer_manager_get_relative_pointer,
2655};
2656
2657static void
2658bind_relative_pointer_manager(struct wl_client *client, void *data,
2659 uint32_t version, uint32_t id)
2660{
2661 struct weston_compositor *compositor = data;
2662 struct wl_resource *resource;
2663
2664 resource = wl_resource_create(client,
2665 &zwp_relative_pointer_manager_v1_interface,
2666 1, id);
2667
2668 wl_resource_set_implementation(resource, &relative_pointer_manager,
2669 compositor,
2670 NULL);
2671}
2672
Giulio Camuffo0358af42016-06-02 21:48:08 +03002673WL_EXPORT int
2674weston_compositor_set_xkb_rule_names(struct weston_compositor *ec,
2675 struct xkb_rule_names *names)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002676{
2677 if (ec->xkb_context == NULL) {
2678 ec->xkb_context = xkb_context_new(0);
2679 if (ec->xkb_context == NULL) {
2680 weston_log("failed to create XKB context\n");
2681 return -1;
2682 }
2683 }
2684
2685 if (names)
2686 ec->xkb_names = *names;
2687 if (!ec->xkb_names.rules)
2688 ec->xkb_names.rules = strdup("evdev");
2689 if (!ec->xkb_names.model)
2690 ec->xkb_names.model = strdup("pc105");
2691 if (!ec->xkb_names.layout)
2692 ec->xkb_names.layout = strdup("us");
2693
2694 return 0;
2695}
2696
Stefan Schmidtfda26522013-09-17 10:54:09 +01002697static void
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002698weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002699{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002700 if (--xkb_info->ref_count > 0)
2701 return;
2702
Ran Benitac9c74152014-08-19 23:59:52 +03002703 xkb_keymap_unref(xkb_info->keymap);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002704
2705 if (xkb_info->keymap_area)
2706 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2707 if (xkb_info->keymap_fd >= 0)
2708 close(xkb_info->keymap_fd);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002709 free(xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002710}
2711
2712void
2713weston_compositor_xkb_destroy(struct weston_compositor *ec)
2714{
2715 free((char *) ec->xkb_names.rules);
2716 free((char *) ec->xkb_names.model);
2717 free((char *) ec->xkb_names.layout);
2718 free((char *) ec->xkb_names.variant);
2719 free((char *) ec->xkb_names.options);
Stefan Schmidtfda26522013-09-17 10:54:09 +01002720
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002721 if (ec->xkb_info)
2722 weston_xkb_info_destroy(ec->xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002723 xkb_context_unref(ec->xkb_context);
2724}
2725
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002726static struct weston_xkb_info *
2727weston_xkb_info_create(struct xkb_keymap *keymap)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002728{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002729 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
2730 if (xkb_info == NULL)
2731 return NULL;
2732
Ran Benita2e1968f2014-08-19 23:59:51 +03002733 xkb_info->keymap = xkb_keymap_ref(keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002734 xkb_info->ref_count = 1;
2735
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002736 char *keymap_str;
2737
Ran Benita2e1968f2014-08-19 23:59:51 +03002738 xkb_info->shift_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2739 XKB_MOD_NAME_SHIFT);
2740 xkb_info->caps_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2741 XKB_MOD_NAME_CAPS);
2742 xkb_info->ctrl_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2743 XKB_MOD_NAME_CTRL);
2744 xkb_info->alt_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2745 XKB_MOD_NAME_ALT);
2746 xkb_info->mod2_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2747 "Mod2");
2748 xkb_info->mod3_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2749 "Mod3");
2750 xkb_info->super_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2751 XKB_MOD_NAME_LOGO);
2752 xkb_info->mod5_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2753 "Mod5");
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002754
Ran Benita2e1968f2014-08-19 23:59:51 +03002755 xkb_info->num_led = xkb_keymap_led_get_index(xkb_info->keymap,
2756 XKB_LED_NAME_NUM);
2757 xkb_info->caps_led = xkb_keymap_led_get_index(xkb_info->keymap,
2758 XKB_LED_NAME_CAPS);
2759 xkb_info->scroll_led = xkb_keymap_led_get_index(xkb_info->keymap,
2760 XKB_LED_NAME_SCROLL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002761
Ran Benita2e1968f2014-08-19 23:59:51 +03002762 keymap_str = xkb_keymap_get_as_string(xkb_info->keymap,
2763 XKB_KEYMAP_FORMAT_TEXT_V1);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002764 if (keymap_str == NULL) {
2765 weston_log("failed to get string version of keymap\n");
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002766 goto err_keymap;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002767 }
2768 xkb_info->keymap_size = strlen(keymap_str) + 1;
2769
2770 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
2771 if (xkb_info->keymap_fd < 0) {
2772 weston_log("creating a keymap file for %lu bytes failed: %m\n",
2773 (unsigned long) xkb_info->keymap_size);
2774 goto err_keymap_str;
2775 }
2776
2777 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2778 PROT_READ | PROT_WRITE,
2779 MAP_SHARED, xkb_info->keymap_fd, 0);
2780 if (xkb_info->keymap_area == MAP_FAILED) {
2781 weston_log("failed to mmap() %lu bytes\n",
2782 (unsigned long) xkb_info->keymap_size);
2783 goto err_dev_zero;
2784 }
2785 strcpy(xkb_info->keymap_area, keymap_str);
2786 free(keymap_str);
2787
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002788 return xkb_info;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002789
2790err_dev_zero:
2791 close(xkb_info->keymap_fd);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002792err_keymap_str:
2793 free(keymap_str);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002794err_keymap:
Ran Benita2e1968f2014-08-19 23:59:51 +03002795 xkb_keymap_unref(xkb_info->keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002796 free(xkb_info);
2797 return NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002798}
2799
2800static int
2801weston_compositor_build_global_keymap(struct weston_compositor *ec)
2802{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002803 struct xkb_keymap *keymap;
2804
2805 if (ec->xkb_info != NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002806 return 0;
2807
Ran Benita2e1968f2014-08-19 23:59:51 +03002808 keymap = xkb_keymap_new_from_names(ec->xkb_context,
2809 &ec->xkb_names,
2810 0);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002811 if (keymap == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002812 weston_log("failed to compile global XKB keymap\n");
2813 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
2814 "options %s\n",
2815 ec->xkb_names.rules, ec->xkb_names.model,
2816 ec->xkb_names.layout, ec->xkb_names.variant,
2817 ec->xkb_names.options);
2818 return -1;
2819 }
2820
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002821 ec->xkb_info = weston_xkb_info_create(keymap);
Rui Matos73d93952013-10-24 19:28:41 +02002822 xkb_keymap_unref(keymap);
Stefan Schmidtfda26522013-09-17 10:54:09 +01002823 if (ec->xkb_info == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002824 return -1;
2825
2826 return 0;
2827}
2828
Rui Matos65196bc2013-10-10 19:44:19 +02002829WL_EXPORT void
2830weston_seat_update_keymap(struct weston_seat *seat, struct xkb_keymap *keymap)
2831{
Derek Foreman1281a362015-07-31 16:55:32 -05002832 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2833
2834 if (!keyboard || !keymap)
Rui Matos65196bc2013-10-10 19:44:19 +02002835 return;
2836
Derek Foreman1281a362015-07-31 16:55:32 -05002837 xkb_keymap_unref(keyboard->pending_keymap);
2838 keyboard->pending_keymap = xkb_keymap_ref(keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02002839
Derek Foreman1281a362015-07-31 16:55:32 -05002840 if (keyboard->keys.size == 0)
Rui Matos65196bc2013-10-10 19:44:19 +02002841 update_keymap(seat);
Rui Matos65196bc2013-10-10 19:44:19 +02002842}
2843
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002844WL_EXPORT int
2845weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
2846{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002847 struct weston_keyboard *keyboard;
2848
Derek Foreman1281a362015-07-31 16:55:32 -05002849 if (seat->keyboard_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002850 seat->keyboard_device_count += 1;
2851 if (seat->keyboard_device_count == 1)
2852 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002853 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002854 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002855
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002856 keyboard = weston_keyboard_create();
2857 if (keyboard == NULL) {
2858 weston_log("failed to allocate weston keyboard struct\n");
2859 return -1;
2860 }
2861
Derek Foreman185d1582017-06-28 11:17:23 -05002862 if (keymap != NULL) {
2863 keyboard->xkb_info = weston_xkb_info_create(keymap);
2864 if (keyboard->xkb_info == NULL)
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02002865 goto err;
Derek Foreman185d1582017-06-28 11:17:23 -05002866 } else {
2867 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
2868 goto err;
2869 keyboard->xkb_info = seat->compositor->xkb_info;
2870 keyboard->xkb_info->ref_count++;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002871 }
Derek Foreman185d1582017-06-28 11:17:23 -05002872
2873 keyboard->xkb_state.state = xkb_state_new(keyboard->xkb_info->keymap);
2874 if (keyboard->xkb_state.state == NULL) {
2875 weston_log("failed to initialise XKB state\n");
2876 goto err;
2877 }
2878
2879 keyboard->xkb_state.leds = 0;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002880
Derek Foreman1281a362015-07-31 16:55:32 -05002881 seat->keyboard_state = keyboard;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02002882 seat->keyboard_device_count = 1;
2883 keyboard->seat = seat;
2884
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002885 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002886
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002887 return 0;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02002888
2889err:
2890 if (keyboard->xkb_info)
2891 weston_xkb_info_destroy(keyboard->xkb_info);
2892 free(keyboard);
2893
2894 return -1;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002895}
2896
Jonas Ådahl91fed542013-12-03 09:14:27 +01002897static void
2898weston_keyboard_reset_state(struct weston_keyboard *keyboard)
2899{
2900 struct weston_seat *seat = keyboard->seat;
2901 struct xkb_state *state;
2902
Derek Foreman185d1582017-06-28 11:17:23 -05002903 state = xkb_state_new(keyboard->xkb_info->keymap);
2904 if (!state) {
2905 weston_log("failed to reset XKB state\n");
2906 return;
Jonas Ådahl91fed542013-12-03 09:14:27 +01002907 }
Derek Foreman185d1582017-06-28 11:17:23 -05002908 xkb_state_unref(keyboard->xkb_state.state);
2909 keyboard->xkb_state.state = state;
2910
2911 keyboard->xkb_state.leds = 0;
Jonas Ådahl91fed542013-12-03 09:14:27 +01002912
2913 seat->modifier_state = 0;
2914}
2915
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002916WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002917weston_seat_release_keyboard(struct weston_seat *seat)
2918{
2919 seat->keyboard_device_count--;
Derek Foremand621df22014-11-19 11:04:12 -06002920 assert(seat->keyboard_device_count >= 0);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002921 if (seat->keyboard_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05002922 weston_keyboard_set_focus(seat->keyboard_state, NULL);
2923 weston_keyboard_cancel_grab(seat->keyboard_state);
2924 weston_keyboard_reset_state(seat->keyboard_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002925 seat_send_updated_caps(seat);
2926 }
2927}
2928
2929WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002930weston_seat_init_pointer(struct weston_seat *seat)
2931{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002932 struct weston_pointer *pointer;
2933
Derek Foreman1281a362015-07-31 16:55:32 -05002934 if (seat->pointer_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002935 seat->pointer_device_count += 1;
2936 if (seat->pointer_device_count == 1)
2937 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002938 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002939 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002940
Giulio Camuffocdb4d292013-11-14 23:42:53 +01002941 pointer = weston_pointer_create(seat);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002942 if (pointer == NULL)
2943 return;
2944
Derek Foreman1281a362015-07-31 16:55:32 -05002945 seat->pointer_state = pointer;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002946 seat->pointer_device_count = 1;
2947 pointer->seat = seat;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002948
2949 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002950}
2951
2952WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002953weston_seat_release_pointer(struct weston_seat *seat)
2954{
Derek Foreman1281a362015-07-31 16:55:32 -05002955 struct weston_pointer *pointer = seat->pointer_state;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002956
2957 seat->pointer_device_count--;
2958 if (seat->pointer_device_count == 0) {
Derek Foremanf9318d12015-05-11 15:40:11 -05002959 weston_pointer_clear_focus(pointer);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002960 weston_pointer_cancel_grab(pointer);
Jonas Ådahl630bae82013-10-17 23:04:06 +02002961
Jonas Ådahla4932742013-10-17 23:04:07 +02002962 if (pointer->sprite)
2963 pointer_unmap_sprite(pointer);
2964
Jonas Ådahl3e12e632013-12-02 22:05:05 +01002965 weston_pointer_reset_state(pointer);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002966 seat_send_updated_caps(seat);
Derek Foremanfd5ca512015-01-07 15:00:25 -06002967
2968 /* seat->pointer is intentionally not destroyed so that
2969 * a newly attached pointer on this seat will retain
2970 * the previous cursor co-ordinates.
2971 */
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002972 }
2973}
2974
2975WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002976weston_seat_init_touch(struct weston_seat *seat)
2977{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002978 struct weston_touch *touch;
2979
Derek Foreman1281a362015-07-31 16:55:32 -05002980 if (seat->touch_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002981 seat->touch_device_count += 1;
2982 if (seat->touch_device_count == 1)
2983 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002984 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002985 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002986
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002987 touch = weston_touch_create();
2988 if (touch == NULL)
2989 return;
2990
Derek Foreman1281a362015-07-31 16:55:32 -05002991 seat->touch_state = touch;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002992 seat->touch_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002993 touch->seat = seat;
2994
2995 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002996}
2997
2998WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002999weston_seat_release_touch(struct weston_seat *seat)
3000{
3001 seat->touch_device_count--;
3002 if (seat->touch_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05003003 weston_touch_set_focus(seat->touch_state, NULL);
3004 weston_touch_cancel_grab(seat->touch_state);
3005 weston_touch_reset_state(seat->touch_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003006 seat_send_updated_caps(seat);
3007 }
3008}
3009
3010WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003011weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
3012 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003013{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003014 memset(seat, 0, sizeof *seat);
3015
Kristian Høgsberge3148752013-05-06 23:19:49 -04003016 seat->selection_data_source = NULL;
3017 wl_list_init(&seat->base_resource_list);
3018 wl_signal_init(&seat->selection_signal);
3019 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04003020 wl_signal_init(&seat->destroy_signal);
Jason Ekstranda4ab5422014-04-02 19:53:45 -05003021 wl_signal_init(&seat->updated_caps_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003022
Peter Hutterer87743e92016-01-18 16:38:22 +10003023 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 5,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003024 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003025
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003026 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003027 seat->modifier_state = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003028 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003029
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003030 wl_list_insert(ec->seat_list.prev, &seat->link);
3031
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003032 clipboard_create(seat);
3033
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003034 wl_signal_emit(&ec->seat_created_signal, seat);
3035}
3036
3037WL_EXPORT void
3038weston_seat_release(struct weston_seat *seat)
3039{
3040 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003041
Jonas Ådahl1afb2382014-01-03 19:46:51 +01003042 if (seat->saved_kbd_focus)
3043 wl_list_remove(&seat->saved_kbd_focus_listener.link);
3044
Derek Foreman1281a362015-07-31 16:55:32 -05003045 if (seat->pointer_state)
3046 weston_pointer_destroy(seat->pointer_state);
3047 if (seat->keyboard_state)
3048 weston_keyboard_destroy(seat->keyboard_state);
3049 if (seat->touch_state)
3050 weston_touch_destroy(seat->touch_state);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003051
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003052 free (seat->seat_name);
3053
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003054 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04003055
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003056 wl_signal_emit(&seat->destroy_signal, seat);
3057}
Derek Foreman1281a362015-07-31 16:55:32 -05003058
3059/** Get a seat's keyboard pointer
3060 *
3061 * \param seat The seat to query
3062 * \return The seat's keyboard pointer, or NULL if no keyboard is present
3063 *
3064 * The keyboard pointer for a seat isn't freed when all keyboards are removed,
3065 * so it should only be used when the seat's keyboard_device_count is greater
3066 * than zero. This function does that test and only returns a pointer
3067 * when a keyboard is present.
3068 */
3069WL_EXPORT struct weston_keyboard *
3070weston_seat_get_keyboard(struct weston_seat *seat)
3071{
3072 if (!seat)
3073 return NULL;
3074
3075 if (seat->keyboard_device_count)
3076 return seat->keyboard_state;
3077
3078 return NULL;
3079}
3080
3081/** Get a seat's pointer pointer
3082 *
3083 * \param seat The seat to query
3084 * \return The seat's pointer pointer, or NULL if no pointer device is present
3085 *
3086 * The pointer pointer for a seat isn't freed when all mice are removed,
3087 * so it should only be used when the seat's pointer_device_count is greater
3088 * than zero. This function does that test and only returns a pointer
3089 * when a pointing device is present.
3090 */
3091WL_EXPORT struct weston_pointer *
3092weston_seat_get_pointer(struct weston_seat *seat)
3093{
3094 if (!seat)
3095 return NULL;
3096
3097 if (seat->pointer_device_count)
3098 return seat->pointer_state;
3099
3100 return NULL;
3101}
3102
Jonas Ådahld3414f22016-07-22 17:56:31 +08003103static const struct zwp_locked_pointer_v1_interface locked_pointer_interface;
3104static const struct zwp_confined_pointer_v1_interface confined_pointer_interface;
3105
3106static enum pointer_constraint_type
3107pointer_constraint_get_type(struct weston_pointer_constraint *constraint)
3108{
3109 if (wl_resource_instance_of(constraint->resource,
3110 &zwp_locked_pointer_v1_interface,
3111 &locked_pointer_interface)) {
3112 return POINTER_CONSTRAINT_TYPE_LOCK;
3113 } else if (wl_resource_instance_of(constraint->resource,
3114 &zwp_confined_pointer_v1_interface,
3115 &confined_pointer_interface)) {
3116 return POINTER_CONSTRAINT_TYPE_CONFINE;
3117 }
3118
3119 abort();
3120 return 0;
3121}
3122
3123static void
3124pointer_constraint_notify_activated(struct weston_pointer_constraint *constraint)
3125{
3126 struct wl_resource *resource = constraint->resource;
3127
3128 switch (pointer_constraint_get_type(constraint)) {
3129 case POINTER_CONSTRAINT_TYPE_LOCK:
3130 zwp_locked_pointer_v1_send_locked(resource);
3131 break;
3132 case POINTER_CONSTRAINT_TYPE_CONFINE:
3133 zwp_confined_pointer_v1_send_confined(resource);
3134 break;
3135 }
3136}
3137
3138static void
3139pointer_constraint_notify_deactivated(struct weston_pointer_constraint *constraint)
3140{
3141 struct wl_resource *resource = constraint->resource;
3142
3143 switch (pointer_constraint_get_type(constraint)) {
3144 case POINTER_CONSTRAINT_TYPE_LOCK:
3145 zwp_locked_pointer_v1_send_unlocked(resource);
3146 break;
3147 case POINTER_CONSTRAINT_TYPE_CONFINE:
3148 zwp_confined_pointer_v1_send_unconfined(resource);
3149 break;
3150 }
3151}
3152
3153static struct weston_pointer_constraint *
3154get_pointer_constraint_for_pointer(struct weston_surface *surface,
3155 struct weston_pointer *pointer)
3156{
3157 struct weston_pointer_constraint *constraint;
3158
3159 wl_list_for_each(constraint, &surface->pointer_constraints, link) {
3160 if (constraint->pointer == pointer)
3161 return constraint;
3162 }
3163
3164 return NULL;
3165}
3166
Derek Foreman1281a362015-07-31 16:55:32 -05003167/** Get a seat's touch pointer
3168 *
3169 * \param seat The seat to query
3170 * \return The seat's touch pointer, or NULL if no touch device is present
3171 *
3172 * The touch pointer for a seat isn't freed when all touch devices are removed,
3173 * so it should only be used when the seat's touch_device_count is greater
3174 * than zero. This function does that test and only returns a pointer
3175 * when a touch device is present.
3176 */
3177WL_EXPORT struct weston_touch *
3178weston_seat_get_touch(struct weston_seat *seat)
3179{
3180 if (!seat)
3181 return NULL;
3182
3183 if (seat->touch_device_count)
3184 return seat->touch_state;
3185
3186 return NULL;
3187}
Bryce Harrington24f917e2016-06-29 19:04:07 -07003188
3189/** Sets the keyboard focus to the given surface
3190 *
3191 * \param seat The seat to query
3192 */
3193WL_EXPORT void
3194weston_seat_set_keyboard_focus(struct weston_seat *seat,
3195 struct weston_surface *surface)
3196{
3197 struct weston_compositor *compositor = seat->compositor;
3198 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003199 struct weston_surface_activation_data activation_data;
Bryce Harrington24f917e2016-06-29 19:04:07 -07003200
Jonas Ådahlef8e1c32016-03-15 20:28:51 +08003201 if (keyboard && keyboard->focus != surface) {
Bryce Harrington24f917e2016-06-29 19:04:07 -07003202 weston_keyboard_set_focus(keyboard, surface);
3203 wl_data_device_set_keyboard_focus(seat);
3204 }
3205
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003206 inc_activate_serial(compositor);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003207
3208 activation_data = (struct weston_surface_activation_data) {
3209 .surface = surface,
3210 .seat = seat,
3211 };
3212 wl_signal_emit(&compositor->activate_signal, &activation_data);
Bryce Harrington24f917e2016-06-29 19:04:07 -07003213}
Jonas Ådahl30d61d82014-10-22 21:21:17 +02003214
Jonas Ådahld3414f22016-07-22 17:56:31 +08003215static void
3216enable_pointer_constraint(struct weston_pointer_constraint *constraint,
3217 struct weston_view *view)
3218{
3219 assert(constraint->view == NULL);
3220 constraint->view = view;
3221 pointer_constraint_notify_activated(constraint);
3222 weston_pointer_start_grab(constraint->pointer, &constraint->grab);
3223 wl_list_remove(&constraint->surface_destroy_listener.link);
3224 wl_list_init(&constraint->surface_destroy_listener.link);
3225}
3226
3227static bool
3228is_pointer_constraint_enabled(struct weston_pointer_constraint *constraint)
3229{
3230 return constraint->view != NULL;
3231}
3232
3233static void
3234weston_pointer_constraint_disable(struct weston_pointer_constraint *constraint)
3235{
3236 constraint->view = NULL;
3237 pointer_constraint_notify_deactivated(constraint);
3238 weston_pointer_end_grab(constraint->grab.pointer);
3239}
3240
3241void
3242weston_pointer_constraint_destroy(struct weston_pointer_constraint *constraint)
3243{
3244 if (is_pointer_constraint_enabled(constraint))
3245 weston_pointer_constraint_disable(constraint);
3246
3247 wl_list_remove(&constraint->pointer_destroy_listener.link);
3248 wl_list_remove(&constraint->surface_destroy_listener.link);
3249 wl_list_remove(&constraint->surface_commit_listener.link);
3250 wl_list_remove(&constraint->surface_activate_listener.link);
3251
3252 wl_resource_set_user_data(constraint->resource, NULL);
3253 pixman_region32_fini(&constraint->region);
3254 wl_list_remove(&constraint->link);
3255 free(constraint);
3256}
3257
3258static void
3259disable_pointer_constraint(struct weston_pointer_constraint *constraint)
3260{
3261 switch (constraint->lifetime) {
3262 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT:
3263 weston_pointer_constraint_destroy(constraint);
3264 break;
3265 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT:
3266 weston_pointer_constraint_disable(constraint);
3267 break;
3268 }
3269}
3270
3271static bool
3272is_within_constraint_region(struct weston_pointer_constraint *constraint,
3273 wl_fixed_t sx, wl_fixed_t sy)
3274{
3275 struct weston_surface *surface = constraint->surface;
3276 pixman_region32_t constraint_region;
3277 bool result;
3278
3279 pixman_region32_init(&constraint_region);
3280 pixman_region32_intersect(&constraint_region,
3281 &surface->input,
3282 &constraint->region);
3283 result = pixman_region32_contains_point(&constraint_region,
3284 wl_fixed_to_int(sx),
3285 wl_fixed_to_int(sy),
3286 NULL);
3287 pixman_region32_fini(&constraint_region);
3288
3289 return result;
3290}
3291
3292static void
3293maybe_enable_pointer_constraint(struct weston_pointer_constraint *constraint)
3294{
3295 struct weston_surface *surface = constraint->surface;
3296 struct weston_view *vit;
3297 struct weston_view *view = NULL;
3298 struct weston_pointer *pointer = constraint->pointer;
3299 struct weston_keyboard *keyboard;
3300 struct weston_seat *seat = pointer->seat;
3301 int32_t x, y;
3302
3303 /* Postpone if no view of the surface was most recently clicked. */
3304 wl_list_for_each(vit, &surface->views, surface_link) {
3305 if (vit->click_to_activate_serial ==
3306 surface->compositor->activate_serial) {
3307 view = vit;
3308 }
3309 }
3310 if (view == NULL)
3311 return;
3312
3313 /* Postpone if surface doesn't have keyboard focus. */
3314 keyboard = weston_seat_get_keyboard(seat);
3315 if (!keyboard || keyboard->focus != surface)
3316 return;
3317
3318 /* Postpone constraint if the pointer is not within the
3319 * constraint region.
3320 */
3321 weston_view_from_global(view,
3322 wl_fixed_to_int(pointer->x),
3323 wl_fixed_to_int(pointer->y),
3324 &x, &y);
3325 if (!is_within_constraint_region(constraint,
3326 wl_fixed_from_int(x),
3327 wl_fixed_from_int(y)))
3328 return;
3329
3330 enable_pointer_constraint(constraint, view);
3331}
3332
3333static void
3334locked_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
3335{
3336}
3337
3338static void
3339locked_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02003340 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003341 struct weston_pointer_motion_event *event)
3342{
Quentin Glidiccde13452016-08-12 10:41:32 +02003343 pointer_send_relative_motion(grab->pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08003344}
3345
3346static void
3347locked_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003348 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003349 uint32_t button,
3350 uint32_t state_w)
3351{
3352 weston_pointer_send_button(grab->pointer, time, button, state_w);
3353}
3354
3355static void
3356locked_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02003357 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003358 struct weston_pointer_axis_event *event)
3359{
3360 weston_pointer_send_axis(grab->pointer, time, event);
3361}
3362
3363static void
3364locked_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
3365 uint32_t source)
3366{
3367 weston_pointer_send_axis_source(grab->pointer, source);
3368}
3369
3370static void
3371locked_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
3372{
3373 weston_pointer_send_frame(grab->pointer);
3374}
3375
3376static void
3377locked_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
3378{
3379 struct weston_pointer_constraint *constraint =
3380 container_of(grab, struct weston_pointer_constraint, grab);
3381
3382 disable_pointer_constraint(constraint);
3383}
3384
3385static const struct weston_pointer_grab_interface
3386 locked_pointer_grab_interface = {
3387 locked_pointer_grab_pointer_focus,
3388 locked_pointer_grab_pointer_motion,
3389 locked_pointer_grab_pointer_button,
3390 locked_pointer_grab_pointer_axis,
3391 locked_pointer_grab_pointer_axis_source,
3392 locked_pointer_grab_pointer_frame,
3393 locked_pointer_grab_pointer_cancel,
3394};
3395
3396static void
3397pointer_constraint_constrain_resource_destroyed(struct wl_resource *resource)
3398{
3399 struct weston_pointer_constraint *constraint =
3400 wl_resource_get_user_data(resource);
3401
3402 if (!constraint)
3403 return;
3404
3405 weston_pointer_constraint_destroy(constraint);
3406}
3407
3408static void
3409pointer_constraint_surface_activate(struct wl_listener *listener, void *data)
3410{
3411 struct weston_surface_activation_data *activation = data;
3412 struct weston_pointer *pointer;
3413 struct weston_surface *focus = activation->surface;
3414 struct weston_pointer_constraint *constraint =
3415 container_of(listener, struct weston_pointer_constraint,
3416 surface_activate_listener);
3417 bool is_constraint_surface;
3418
3419 pointer = weston_seat_get_pointer(activation->seat);
3420 if (!pointer)
3421 return;
3422
3423 is_constraint_surface =
3424 get_pointer_constraint_for_pointer(focus, pointer) == constraint;
3425
3426 if (is_constraint_surface &&
3427 !is_pointer_constraint_enabled(constraint))
3428 maybe_enable_pointer_constraint(constraint);
3429 else if (!is_constraint_surface &&
3430 is_pointer_constraint_enabled(constraint))
3431 disable_pointer_constraint(constraint);
3432}
3433
3434static void
3435pointer_constraint_pointer_destroyed(struct wl_listener *listener, void *data)
3436{
3437 struct weston_pointer_constraint *constraint =
3438 container_of(listener, struct weston_pointer_constraint,
3439 pointer_destroy_listener);
3440
3441 weston_pointer_constraint_destroy(constraint);
3442}
3443
3444static void
3445pointer_constraint_surface_destroyed(struct wl_listener *listener, void *data)
3446{
3447 struct weston_pointer_constraint *constraint =
3448 container_of(listener, struct weston_pointer_constraint,
3449 surface_destroy_listener);
3450
3451 weston_pointer_constraint_destroy(constraint);
3452}
3453
3454static void
3455pointer_constraint_surface_committed(struct wl_listener *listener, void *data)
3456{
3457 struct weston_pointer_constraint *constraint =
3458 container_of(listener, struct weston_pointer_constraint,
3459 surface_commit_listener);
3460
3461 if (constraint->region_is_pending) {
3462 constraint->region_is_pending = false;
3463 pixman_region32_copy(&constraint->region,
3464 &constraint->region_pending);
3465 pixman_region32_fini(&constraint->region_pending);
3466 pixman_region32_init(&constraint->region_pending);
3467 }
3468
3469 if (constraint->hint_is_pending) {
3470 constraint->hint_is_pending = false;
3471
3472 constraint->hint_is_pending = true;
3473 constraint->hint_x = constraint->hint_x_pending;
3474 constraint->hint_y = constraint->hint_y_pending;
3475 }
3476
3477 if (pointer_constraint_get_type(constraint) ==
3478 POINTER_CONSTRAINT_TYPE_CONFINE &&
3479 is_pointer_constraint_enabled(constraint))
3480 maybe_warp_confined_pointer(constraint);
3481}
3482
3483static struct weston_pointer_constraint *
3484weston_pointer_constraint_create(struct weston_surface *surface,
3485 struct weston_pointer *pointer,
3486 struct weston_region *region,
3487 enum zwp_pointer_constraints_v1_lifetime lifetime,
3488 struct wl_resource *cr,
3489 const struct weston_pointer_grab_interface *grab_interface)
3490{
3491 struct weston_pointer_constraint *constraint;
3492
3493 constraint = zalloc(sizeof *constraint);
3494 if (!constraint)
3495 return NULL;
3496
3497 constraint->lifetime = lifetime;
3498 pixman_region32_init(&constraint->region);
3499 pixman_region32_init(&constraint->region_pending);
3500 wl_list_insert(&surface->pointer_constraints, &constraint->link);
3501 constraint->surface = surface;
3502 constraint->pointer = pointer;
3503 constraint->resource = cr;
3504 constraint->grab.interface = grab_interface;
3505 if (region) {
3506 pixman_region32_copy(&constraint->region,
3507 &region->region);
3508 } else {
3509 pixman_region32_fini(&constraint->region);
3510 region_init_infinite(&constraint->region);
3511 }
3512
3513 constraint->surface_activate_listener.notify =
3514 pointer_constraint_surface_activate;
3515 constraint->surface_destroy_listener.notify =
3516 pointer_constraint_surface_destroyed;
3517 constraint->surface_commit_listener.notify =
3518 pointer_constraint_surface_committed;
3519 constraint->pointer_destroy_listener.notify =
3520 pointer_constraint_pointer_destroyed;
3521
3522 wl_signal_add(&surface->compositor->activate_signal,
3523 &constraint->surface_activate_listener);
3524 wl_signal_add(&pointer->destroy_signal,
3525 &constraint->pointer_destroy_listener);
3526 wl_signal_add(&surface->destroy_signal,
3527 &constraint->surface_destroy_listener);
3528 wl_signal_add(&surface->commit_signal,
3529 &constraint->surface_commit_listener);
3530
3531 return constraint;
3532}
3533
3534static void
3535init_pointer_constraint(struct wl_resource *pointer_constraints_resource,
3536 uint32_t id,
3537 struct weston_surface *surface,
3538 struct weston_pointer *pointer,
3539 struct weston_region *region,
3540 enum zwp_pointer_constraints_v1_lifetime lifetime,
3541 const struct wl_interface *interface,
3542 const void *implementation,
3543 const struct weston_pointer_grab_interface *grab_interface)
3544{
3545 struct wl_client *client =
3546 wl_resource_get_client(pointer_constraints_resource);
3547 struct wl_resource *cr;
3548 struct weston_pointer_constraint *constraint;
3549
3550 if (get_pointer_constraint_for_pointer(surface, pointer)) {
3551 wl_resource_post_error(pointer_constraints_resource,
3552 ZWP_POINTER_CONSTRAINTS_V1_ERROR_ALREADY_CONSTRAINED,
3553 "the pointer has a lock/confine request on this surface");
3554 return;
3555 }
3556
3557 cr = wl_resource_create(client, interface,
3558 wl_resource_get_version(pointer_constraints_resource),
3559 id);
3560 if (cr == NULL) {
3561 wl_client_post_no_memory(client);
3562 return;
3563 }
3564
3565 constraint = weston_pointer_constraint_create(surface, pointer,
3566 region, lifetime,
3567 cr, grab_interface);
3568 if (constraint == NULL) {
3569 wl_client_post_no_memory(client);
3570 return;
3571 }
3572
3573 wl_resource_set_implementation(cr, implementation, constraint,
3574 pointer_constraint_constrain_resource_destroyed);
3575
3576 maybe_enable_pointer_constraint(constraint);
3577}
3578
3579static void
3580pointer_constraints_destroy(struct wl_client *client,
3581 struct wl_resource *resource)
3582{
3583 wl_resource_destroy(resource);
3584}
3585
3586static void
3587locked_pointer_destroy(struct wl_client *client,
3588 struct wl_resource *resource)
3589{
3590 struct weston_pointer_constraint *constraint =
3591 wl_resource_get_user_data(resource);
3592 wl_fixed_t x, y;
3593
3594 if (constraint && constraint->view && constraint->hint_is_pending &&
3595 is_within_constraint_region(constraint,
3596 constraint->hint_x,
3597 constraint->hint_y)) {
3598 weston_view_to_global_fixed(constraint->view,
3599 constraint->hint_x,
3600 constraint->hint_y,
3601 &x, &y);
3602 weston_pointer_move_to(constraint->pointer, x, y);
3603 }
3604 wl_resource_destroy(resource);
3605}
3606
3607static void
3608locked_pointer_set_cursor_position_hint(struct wl_client *client,
3609 struct wl_resource *resource,
3610 wl_fixed_t surface_x,
3611 wl_fixed_t surface_y)
3612{
3613 struct weston_pointer_constraint *constraint =
3614 wl_resource_get_user_data(resource);
3615
3616 /* Ignore a set cursor hint that was sent after the lock was cancelled.
3617 */
3618 if (!constraint ||
3619 !constraint->resource ||
3620 constraint->resource != resource)
3621 return;
3622
3623 constraint->hint_is_pending = true;
3624 constraint->hint_x_pending = surface_x;
3625 constraint->hint_y_pending = surface_y;
3626}
3627
3628static void
3629locked_pointer_set_region(struct wl_client *client,
3630 struct wl_resource *resource,
3631 struct wl_resource *region_resource)
3632{
3633 struct weston_pointer_constraint *constraint =
3634 wl_resource_get_user_data(resource);
3635 struct weston_region *region = region_resource ?
3636 wl_resource_get_user_data(region_resource) : NULL;
3637
3638 if (!constraint)
3639 return;
3640
3641 if (region) {
3642 pixman_region32_copy(&constraint->region_pending,
3643 &region->region);
3644 } else {
3645 pixman_region32_fini(&constraint->region_pending);
3646 region_init_infinite(&constraint->region_pending);
3647 }
3648 constraint->region_is_pending = true;
3649}
3650
3651
3652static const struct zwp_locked_pointer_v1_interface locked_pointer_interface = {
3653 locked_pointer_destroy,
3654 locked_pointer_set_cursor_position_hint,
3655 locked_pointer_set_region,
3656};
3657
3658static void
3659pointer_constraints_lock_pointer(struct wl_client *client,
3660 struct wl_resource *resource,
3661 uint32_t id,
3662 struct wl_resource *surface_resource,
3663 struct wl_resource *pointer_resource,
3664 struct wl_resource *region_resource,
3665 uint32_t lifetime)
3666{
3667 struct weston_surface *surface =
3668 wl_resource_get_user_data(surface_resource);
3669 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
3670 struct weston_region *region = region_resource ?
3671 wl_resource_get_user_data(region_resource) : NULL;
3672
3673 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
3674 &zwp_locked_pointer_v1_interface,
3675 &locked_pointer_interface,
3676 &locked_pointer_grab_interface);
3677}
3678
3679static void
3680confined_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
3681{
3682}
3683
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08003684static double
3685vec2d_cross_product(struct vec2d a, struct vec2d b)
3686{
3687 return a.x * b.y - a.y * b.x;
3688}
3689
3690static struct vec2d
3691vec2d_add(struct vec2d a, struct vec2d b)
3692{
3693 return (struct vec2d) {
3694 .x = a.x + b.x,
3695 .y = a.y + b.y,
3696 };
3697}
3698
3699static struct vec2d
3700vec2d_subtract(struct vec2d a, struct vec2d b)
3701{
3702 return (struct vec2d) {
3703 .x = a.x - b.x,
3704 .y = a.y - b.y,
3705 };
3706}
3707
3708static struct vec2d
3709vec2d_multiply_constant(double c, struct vec2d a)
3710{
3711 return (struct vec2d) {
3712 .x = c * a.x,
3713 .y = c * a.y,
3714 };
3715}
3716
3717static bool
3718lines_intersect(struct line *line1, struct line *line2,
3719 struct vec2d *intersection)
3720{
3721 struct vec2d p = line1->a;
3722 struct vec2d r = vec2d_subtract(line1->b, line1->a);
3723 struct vec2d q = line2->a;
3724 struct vec2d s = vec2d_subtract(line2->b, line2->a);
3725 double rxs;
3726 double sxr;
3727 double t;
3728 double u;
3729
3730 /*
3731 * The line (p, r) and (q, s) intersects where
3732 *
3733 * p + t r = q + u s
3734 *
3735 * Calculate t:
3736 *
3737 * (p + t r) × s = (q + u s) × s
3738 * p × s + t (r × s) = q × s + u (s × s)
3739 * p × s + t (r × s) = q × s
3740 * t (r × s) = q × s - p × s
3741 * t (r × s) = (q - p) × s
3742 * t = ((q - p) × s) / (r × s)
3743 *
3744 * Using the same method, for u we get:
3745 *
3746 * u = ((p - q) × r) / (s × r)
3747 */
3748
3749 rxs = vec2d_cross_product(r, s);
3750 sxr = vec2d_cross_product(s, r);
3751
3752 /* If r × s = 0 then the lines are either parallel or collinear. */
3753 if (fabs(rxs) < DBL_MIN)
3754 return false;
3755
3756 t = vec2d_cross_product(vec2d_subtract(q, p), s) / rxs;
3757 u = vec2d_cross_product(vec2d_subtract(p, q), r) / sxr;
3758
3759 /* The lines only intersect if 0 ≤ t ≤ 1 and 0 ≤ u ≤ 1. */
3760 if (t < 0.0 || t > 1.0 || u < 0.0 || u > 1.0)
3761 return false;
3762
3763 *intersection = vec2d_add(p, vec2d_multiply_constant(t, r));
3764 return true;
3765}
3766
3767static struct border *
3768add_border(struct wl_array *array,
3769 double x1, double y1,
3770 double x2, double y2,
3771 enum motion_direction blocking_dir)
3772{
3773 struct border *border = wl_array_add(array, sizeof *border);
3774
3775 *border = (struct border) {
3776 .line = (struct line) {
3777 .a = (struct vec2d) {
3778 .x = x1,
3779 .y = y1,
3780 },
3781 .b = (struct vec2d) {
3782 .x = x2,
3783 .y = y2,
3784 },
3785 },
3786 .blocking_dir = blocking_dir,
3787 };
3788
3789 return border;
3790}
3791
3792static int
3793compare_lines_x(const void *a, const void *b)
3794{
3795 const struct border *border_a = a;
3796 const struct border *border_b = b;
3797
3798
3799 if (border_a->line.a.x == border_b->line.a.x)
3800 return border_a->line.b.x < border_b->line.b.x;
3801 else
3802 return border_a->line.a.x > border_b->line.a.x;
3803}
3804
3805static void
3806add_non_overlapping_edges(pixman_box32_t *boxes,
3807 int band_above_start,
3808 int band_below_start,
3809 int band_below_end,
3810 struct wl_array *borders)
3811{
3812 int i;
3813 struct wl_array band_merge;
3814 struct border *border;
3815 struct border *prev_border;
3816 struct border *new_border;
3817
3818 wl_array_init(&band_merge);
3819
3820 /* Add bottom band of previous row, and top band of current row, and
3821 * sort them so lower left x coordinate comes first. If there are two
3822 * borders with the same left x coordinate, the wider one comes first.
3823 */
3824 for (i = band_above_start; i < band_below_start; i++) {
3825 pixman_box32_t *box = &boxes[i];
3826 add_border(&band_merge, box->x1, box->y2, box->x2, box->y2,
3827 MOTION_DIRECTION_POSITIVE_Y);
3828 }
3829 for (i = band_below_start; i < band_below_end; i++) {
3830 pixman_box32_t *box= &boxes[i];
3831 add_border(&band_merge, box->x1, box->y1, box->x2, box->y1,
3832 MOTION_DIRECTION_NEGATIVE_Y);
3833 }
3834 qsort(band_merge.data,
3835 band_merge.size / sizeof *border,
3836 sizeof *border,
3837 compare_lines_x);
3838
3839 /* Combine the two combined bands so that any overlapping border is
3840 * eliminated. */
3841 prev_border = NULL;
3842 wl_array_for_each(border, &band_merge) {
3843 assert(border->line.a.y == border->line.b.y);
3844 assert(!prev_border ||
3845 prev_border->line.a.y == border->line.a.y);
3846 assert(!prev_border ||
3847 (prev_border->line.a.x != border->line.a.x ||
3848 prev_border->line.b.x != border->line.b.x));
3849 assert(!prev_border ||
3850 prev_border->line.a.x <= border->line.a.x);
3851
3852 if (prev_border &&
3853 prev_border->line.a.x == border->line.a.x) {
3854 /*
3855 * ------------ +
3856 * ------- =
3857 * [ ]-----
3858 */
3859 prev_border->line.a.x = border->line.b.x;
3860 } else if (prev_border &&
3861 prev_border->line.b.x == border->line.b.x) {
3862 /*
3863 * ------------ +
3864 * ------ =
3865 * ------[ ]
3866 */
3867 prev_border->line.b.x = border->line.a.x;
3868 } else if (prev_border &&
3869 prev_border->line.b.x == border->line.a.x) {
3870 /*
3871 * -------- +
3872 * ------ =
3873 * --------------
3874 */
3875 prev_border->line.b.x = border->line.b.x;
3876 } else if (prev_border &&
3877 prev_border->line.b.x >= border->line.a.x) {
3878 /*
3879 * --------------- +
3880 * ------ =
3881 * -----[ ]----
3882 */
3883 new_border = add_border(borders,
3884 border->line.b.x,
3885 border->line.b.y,
3886 prev_border->line.b.x,
3887 prev_border->line.b.y,
3888 prev_border->blocking_dir);
3889 prev_border->line.b.x = border->line.a.x;
3890 prev_border = new_border;
3891 } else {
3892 assert(!prev_border ||
3893 prev_border->line.b.x < border->line.a.x);
3894 /*
3895 * First border or non-overlapping.
3896 *
3897 * ----- +
3898 * ----- =
3899 * ----- -----
3900 */
3901 new_border = wl_array_add(borders, sizeof *border);
3902 *new_border = *border;
3903 prev_border = new_border;
3904 }
3905 }
3906
3907 wl_array_release(&band_merge);
3908}
3909
3910static void
3911add_band_bottom_edges(pixman_box32_t *boxes,
3912 int band_start,
3913 int band_end,
3914 struct wl_array *borders)
3915{
3916 int i;
3917
3918 for (i = band_start; i < band_end; i++) {
3919 add_border(borders,
3920 boxes[i].x1, boxes[i].y2,
3921 boxes[i].x2, boxes[i].y2,
3922 MOTION_DIRECTION_POSITIVE_Y);
3923 }
3924}
3925
3926static void
3927region_to_outline(pixman_region32_t *region, struct wl_array *borders)
3928{
3929 pixman_box32_t *boxes;
3930 int num_boxes;
3931 int i;
3932 int top_most, bottom_most;
3933 int current_roof;
3934 int prev_top;
3935 int band_start, prev_band_start;
3936
3937 /*
3938 * Remove any overlapping lines from the set of rectangles. Note that
3939 * pixman regions are grouped as rows of rectangles, where rectangles
3940 * in one row never touch or overlap and are all of the same height.
3941 *
3942 * -------- --- -------- ---
3943 * | | | | | | | |
3944 * ----------====---- --- ----------- ----- ---
3945 * | | => | |
3946 * ----==========--------- ----- ----------
3947 * | | | |
3948 * ------------------- -------------------
3949 *
3950 */
3951
3952 boxes = pixman_region32_rectangles(region, &num_boxes);
3953 prev_top = 0;
3954 top_most = boxes[0].y1;
3955 current_roof = top_most;
3956 bottom_most = boxes[num_boxes - 1].y2;
3957 band_start = 0;
3958 prev_band_start = 0;
3959 for (i = 0; i < num_boxes; i++) {
3960 /* Detect if there is a vertical empty space, and add the lower
3961 * level of the previous band if so was the case. */
3962 if (i > 0 &&
3963 boxes[i].y1 != prev_top &&
3964 boxes[i].y1 != boxes[i - 1].y2) {
3965 current_roof = boxes[i].y1;
3966 add_band_bottom_edges(boxes,
3967 band_start,
3968 i,
3969 borders);
3970 }
3971
3972 /* Special case adding the last band, since it won't be handled
3973 * by the band change detection below. */
3974 if (boxes[i].y1 != current_roof && i == num_boxes - 1) {
3975 if (boxes[i].y1 != prev_top) {
3976 /* The last band is a single box, so we don't
3977 * have a prev_band_start to tell us when the
3978 * previous band started. */
3979 add_non_overlapping_edges(boxes,
3980 band_start,
3981 i,
3982 i + 1,
3983 borders);
3984 } else {
3985 add_non_overlapping_edges(boxes,
3986 prev_band_start,
3987 band_start,
3988 i + 1,
3989 borders);
3990 }
3991 }
3992
3993 /* Detect when passing a band and combine the top border of the
3994 * just passed band with the bottom band of the previous band.
3995 */
3996 if (boxes[i].y1 != top_most && boxes[i].y1 != prev_top) {
3997 /* Combine the two passed bands. */
3998 if (prev_top != current_roof) {
3999 add_non_overlapping_edges(boxes,
4000 prev_band_start,
4001 band_start,
4002 i,
4003 borders);
4004 }
4005
4006 prev_band_start = band_start;
4007 band_start = i;
4008 }
4009
4010 /* Add the top border if the box is part of the current roof. */
4011 if (boxes[i].y1 == current_roof) {
4012 add_border(borders,
4013 boxes[i].x1, boxes[i].y1,
4014 boxes[i].x2, boxes[i].y1,
4015 MOTION_DIRECTION_NEGATIVE_Y);
4016 }
4017
4018 /* Add the bottom border of the last band. */
4019 if (boxes[i].y2 == bottom_most) {
4020 add_border(borders,
4021 boxes[i].x1, boxes[i].y2,
4022 boxes[i].x2, boxes[i].y2,
4023 MOTION_DIRECTION_POSITIVE_Y);
4024 }
4025
4026 /* Always add the left border. */
4027 add_border(borders,
4028 boxes[i].x1, boxes[i].y1,
4029 boxes[i].x1, boxes[i].y2,
4030 MOTION_DIRECTION_NEGATIVE_X);
4031
4032 /* Always add the right border. */
4033 add_border(borders,
4034 boxes[i].x2, boxes[i].y1,
4035 boxes[i].x2, boxes[i].y2,
4036 MOTION_DIRECTION_POSITIVE_X);
4037
4038 prev_top = boxes[i].y1;
4039 }
4040}
4041
4042static bool
4043is_border_horizontal (struct border *border)
4044{
4045 return border->line.a.y == border->line.b.y;
4046}
4047
4048static bool
4049is_border_blocking_directions(struct border *border,
4050 uint32_t directions)
4051{
4052 /* Don't block parallel motions. */
4053 if (is_border_horizontal(border)) {
4054 if ((directions & (MOTION_DIRECTION_POSITIVE_Y |
4055 MOTION_DIRECTION_NEGATIVE_Y)) == 0)
4056 return false;
4057 } else {
4058 if ((directions & (MOTION_DIRECTION_POSITIVE_X |
4059 MOTION_DIRECTION_NEGATIVE_X)) == 0)
4060 return false;
4061 }
4062
4063 return (~border->blocking_dir & directions) != directions;
4064}
4065
4066static struct border *
4067get_closest_border(struct wl_array *borders,
4068 struct line *motion,
4069 uint32_t directions)
4070{
4071 struct border *border;
4072 struct vec2d intersection;
4073 struct vec2d delta;
4074 double distance_2;
4075 struct border *closest_border = NULL;
4076 double closest_distance_2 = DBL_MAX;
4077
4078 wl_array_for_each(border, borders) {
4079 if (!is_border_blocking_directions(border, directions))
4080 continue;
4081
4082 if (!lines_intersect(&border->line, motion, &intersection))
4083 continue;
4084
4085 delta = vec2d_subtract(intersection, motion->a);
4086 distance_2 = delta.x*delta.x + delta.y*delta.y;
4087 if (distance_2 < closest_distance_2) {
4088 closest_border = border;
4089 closest_distance_2 = distance_2;
4090 }
4091 }
4092
4093 return closest_border;
4094}
4095
4096static void
4097clamp_to_border(struct border *border,
4098 struct line *motion,
4099 uint32_t *motion_dir)
4100{
4101 /*
4102 * When clamping either rightward or downward motions, the motion needs
4103 * to be clamped so that the destination coordinate does not end up on
4104 * the border (see weston_pointer_clamp_event_to_region). Do this by
4105 * clamping such motions to the border minus the smallest possible
4106 * wl_fixed_t value.
4107 */
4108 if (is_border_horizontal(border)) {
4109 if (*motion_dir & MOTION_DIRECTION_POSITIVE_Y)
4110 motion->b.y = border->line.a.y - wl_fixed_to_double(1);
4111 else
4112 motion->b.y = border->line.a.y;
4113 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_Y |
4114 MOTION_DIRECTION_NEGATIVE_Y);
4115 } else {
4116 if (*motion_dir & MOTION_DIRECTION_POSITIVE_X)
4117 motion->b.x = border->line.a.x - wl_fixed_to_double(1);
4118 else
4119 motion->b.x = border->line.a.x;
4120 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_X |
4121 MOTION_DIRECTION_NEGATIVE_X);
4122 }
4123}
4124
4125static uint32_t
4126get_motion_directions(struct line *motion)
4127{
4128 uint32_t directions = 0;
4129
4130 if (motion->a.x < motion->b.x)
4131 directions |= MOTION_DIRECTION_POSITIVE_X;
4132 else if (motion->a.x > motion->b.x)
4133 directions |= MOTION_DIRECTION_NEGATIVE_X;
4134 if (motion->a.y < motion->b.y)
4135 directions |= MOTION_DIRECTION_POSITIVE_Y;
4136 else if (motion->a.y > motion->b.y)
4137 directions |= MOTION_DIRECTION_NEGATIVE_Y;
4138
4139 return directions;
4140}
4141
Jonas Ådahld3414f22016-07-22 17:56:31 +08004142static void
4143weston_pointer_clamp_event_to_region(struct weston_pointer *pointer,
4144 struct weston_pointer_motion_event *event,
4145 pixman_region32_t *region,
4146 wl_fixed_t *clamped_x,
4147 wl_fixed_t *clamped_y)
4148{
4149 wl_fixed_t x, y;
4150 wl_fixed_t sx, sy;
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004151 wl_fixed_t old_sx = pointer->sx;
4152 wl_fixed_t old_sy = pointer->sy;
4153 struct wl_array borders;
4154 struct line motion;
4155 struct border *closest_border;
4156 float new_x_f, new_y_f;
4157 uint32_t directions;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004158
4159 weston_pointer_motion_to_abs(pointer, event, &x, &y);
4160 weston_view_from_global_fixed(pointer->focus, x, y, &sx, &sy);
4161
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004162 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004163
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004164 /*
4165 * Generate borders given the confine region we are to use. The borders
4166 * are defined to be the outer region of the allowed area. This means
4167 * top/left borders are "within" the allowed area, while bottom/right
4168 * borders are outside. This needs to be considered when clamping
4169 * confined motion vectors.
4170 */
4171 region_to_outline(region, &borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004172
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004173 motion = (struct line) {
4174 .a = (struct vec2d) {
4175 .x = wl_fixed_to_double(old_sx),
4176 .y = wl_fixed_to_double(old_sy),
4177 },
4178 .b = (struct vec2d) {
4179 .x = wl_fixed_to_double(sx),
4180 .y = wl_fixed_to_double(sy),
4181 },
4182 };
4183 directions = get_motion_directions(&motion);
4184
4185 while (directions) {
4186 closest_border = get_closest_border(&borders,
4187 &motion,
4188 directions);
4189 if (closest_border)
4190 clamp_to_border(closest_border, &motion, &directions);
4191 else
4192 break;
4193 }
4194
4195 weston_view_to_global_float(pointer->focus,
4196 (float) motion.b.x, (float) motion.b.y,
4197 &new_x_f, &new_y_f);
4198 *clamped_x = wl_fixed_from_double(new_x_f);
4199 *clamped_y = wl_fixed_from_double(new_y_f);
4200
4201 wl_array_release(&borders);
4202}
4203
4204static double
4205point_to_border_distance_2(struct border *border, double x, double y)
4206{
4207 double orig_x, orig_y;
4208 double dx, dy;
4209
4210 if (is_border_horizontal(border)) {
4211 if (x < border->line.a.x)
4212 orig_x = border->line.a.x;
4213 else if (x > border->line.b.x)
4214 orig_x = border->line.b.x;
4215 else
4216 orig_x = x;
4217 orig_y = border->line.a.y;
4218 } else {
4219 if (y < border->line.a.y)
4220 orig_y = border->line.a.y;
4221 else if (y > border->line.b.y)
4222 orig_y = border->line.b.y;
4223 else
4224 orig_y = y;
4225 orig_x = border->line.a.x;
4226 }
4227
4228
4229 dx = fabs(orig_x - x);
4230 dy = fabs(orig_y - y);
4231 return dx*dx + dy*dy;
4232}
4233
4234static void
4235warp_to_behind_border(struct border *border, wl_fixed_t *sx, wl_fixed_t *sy)
4236{
4237 switch (border->blocking_dir) {
4238 case MOTION_DIRECTION_POSITIVE_X:
4239 case MOTION_DIRECTION_NEGATIVE_X:
4240 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_X)
4241 *sx = wl_fixed_from_double(border->line.a.x) - 1;
4242 else
4243 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4244 if (*sy < wl_fixed_from_double(border->line.a.y))
4245 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4246 else if (*sy > wl_fixed_from_double(border->line.b.y))
4247 *sy = wl_fixed_from_double(border->line.b.y) - 1;
4248 break;
4249 case MOTION_DIRECTION_POSITIVE_Y:
4250 case MOTION_DIRECTION_NEGATIVE_Y:
4251 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_Y)
4252 *sy = wl_fixed_from_double(border->line.a.y) - 1;
4253 else
4254 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4255 if (*sx < wl_fixed_from_double(border->line.a.x))
4256 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4257 else if (*sx > wl_fixed_from_double(border->line.b.x))
4258 *sx = wl_fixed_from_double(border->line.b.x) - 1;
4259 break;
4260 }
Jonas Ådahld3414f22016-07-22 17:56:31 +08004261}
4262
4263static void
4264maybe_warp_confined_pointer(struct weston_pointer_constraint *constraint)
4265{
4266 wl_fixed_t x;
4267 wl_fixed_t y;
4268 wl_fixed_t sx;
4269 wl_fixed_t sy;
4270
4271 weston_view_from_global_fixed(constraint->view,
4272 constraint->pointer->x,
4273 constraint->pointer->y,
4274 &sx,
4275 &sy);
4276
4277 if (!is_within_constraint_region(constraint, sx, sy)) {
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004278 double xf = wl_fixed_to_double(sx);
4279 double yf = wl_fixed_to_double(sy);
4280 pixman_region32_t confine_region;
4281 struct wl_array borders;
4282 struct border *border;
4283 double closest_distance_2 = DBL_MAX;
4284 struct border *closest_border = NULL;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004285
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004286 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004287
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004288 pixman_region32_init(&confine_region);
4289 pixman_region32_intersect(&confine_region,
4290 &constraint->view->surface->input,
4291 &constraint->region);
4292 region_to_outline(&confine_region, &borders);
4293 pixman_region32_fini(&confine_region);
4294
4295 wl_array_for_each(border, &borders) {
4296 double distance_2;
4297
4298 distance_2 = point_to_border_distance_2(border, xf, yf);
4299 if (distance_2 < closest_distance_2) {
4300 closest_border = border;
4301 closest_distance_2 = distance_2;
4302 }
4303 }
4304 assert(closest_border);
4305
4306 warp_to_behind_border(closest_border, &sx, &sy);
4307
4308 wl_array_release(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004309
4310 weston_view_to_global_fixed(constraint->view, sx, sy, &x, &y);
4311 weston_pointer_move_to(constraint->pointer, x, y);
4312 }
4313}
4314
4315static void
4316confined_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02004317 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004318 struct weston_pointer_motion_event *event)
4319{
4320 struct weston_pointer_constraint *constraint =
4321 container_of(grab, struct weston_pointer_constraint, grab);
4322 struct weston_pointer *pointer = grab->pointer;
4323 struct weston_surface *surface;
4324 wl_fixed_t x, y;
4325 wl_fixed_t old_sx = pointer->sx;
4326 wl_fixed_t old_sy = pointer->sy;
4327 pixman_region32_t confine_region;
4328
4329 assert(pointer->focus);
4330 assert(pointer->focus->surface == constraint->surface);
4331
4332 surface = pointer->focus->surface;
4333
4334 pixman_region32_init(&confine_region);
4335 pixman_region32_intersect(&confine_region,
4336 &surface->input,
4337 &constraint->region);
4338 weston_pointer_clamp_event_to_region(pointer, event,
4339 &confine_region, &x, &y);
4340 weston_pointer_move_to(pointer, x, y);
4341 pixman_region32_fini(&confine_region);
4342
4343 weston_view_from_global_fixed(pointer->focus, x, y,
4344 &pointer->sx, &pointer->sy);
4345
4346 if (old_sx != pointer->sx || old_sy != pointer->sy) {
Quentin Glidiccde13452016-08-12 10:41:32 +02004347 pointer_send_motion(pointer, time,
4348 pointer->sx, pointer->sy);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004349 }
4350
Quentin Glidiccde13452016-08-12 10:41:32 +02004351 pointer_send_relative_motion(pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004352}
4353
4354static void
4355confined_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02004356 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004357 uint32_t button,
4358 uint32_t state_w)
4359{
4360 weston_pointer_send_button(grab->pointer, time, button, state_w);
4361}
4362
4363static void
4364confined_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02004365 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004366 struct weston_pointer_axis_event *event)
4367{
4368 weston_pointer_send_axis(grab->pointer, time, event);
4369}
4370
4371static void
4372confined_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
4373 uint32_t source)
4374{
4375 weston_pointer_send_axis_source(grab->pointer, source);
4376}
4377
4378static void
4379confined_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
4380{
4381 weston_pointer_send_frame(grab->pointer);
4382}
4383
4384static void
4385confined_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
4386{
4387 struct weston_pointer_constraint *constraint =
4388 container_of(grab, struct weston_pointer_constraint, grab);
4389
4390 disable_pointer_constraint(constraint);
4391
4392 /* If this is a persistent constraint, re-add the surface destroy signal
4393 * listener only if we are currently not destroying the surface. */
4394 switch (constraint->lifetime) {
4395 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT:
4396 if (constraint->surface->resource)
4397 wl_signal_add(&constraint->surface->destroy_signal,
4398 &constraint->surface_destroy_listener);
4399 break;
4400 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT:
4401 break;
4402 }
4403}
4404
4405static const struct weston_pointer_grab_interface
4406 confined_pointer_grab_interface = {
4407 confined_pointer_grab_pointer_focus,
4408 confined_pointer_grab_pointer_motion,
4409 confined_pointer_grab_pointer_button,
4410 confined_pointer_grab_pointer_axis,
4411 confined_pointer_grab_pointer_axis_source,
4412 confined_pointer_grab_pointer_frame,
4413 confined_pointer_grab_pointer_cancel,
4414};
4415
4416static void
4417confined_pointer_destroy(struct wl_client *client,
4418 struct wl_resource *resource)
4419{
4420 wl_resource_destroy(resource);
4421}
4422
4423static void
4424confined_pointer_set_region(struct wl_client *client,
4425 struct wl_resource *resource,
4426 struct wl_resource *region_resource)
4427{
4428 struct weston_pointer_constraint *constraint =
4429 wl_resource_get_user_data(resource);
4430 struct weston_region *region = region_resource ?
4431 wl_resource_get_user_data(region_resource) : NULL;
4432
4433 if (!constraint)
4434 return;
4435
4436 if (region) {
4437 pixman_region32_copy(&constraint->region_pending,
4438 &region->region);
4439 } else {
4440 pixman_region32_fini(&constraint->region_pending);
4441 region_init_infinite(&constraint->region_pending);
4442 }
4443 constraint->region_is_pending = true;
4444}
4445
4446static const struct zwp_confined_pointer_v1_interface confined_pointer_interface = {
4447 confined_pointer_destroy,
4448 confined_pointer_set_region,
4449};
4450
4451static void
4452pointer_constraints_confine_pointer(struct wl_client *client,
4453 struct wl_resource *resource,
4454 uint32_t id,
4455 struct wl_resource *surface_resource,
4456 struct wl_resource *pointer_resource,
4457 struct wl_resource *region_resource,
4458 uint32_t lifetime)
4459{
4460 struct weston_surface *surface =
4461 wl_resource_get_user_data(surface_resource);
4462 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
4463 struct weston_region *region = region_resource ?
4464 wl_resource_get_user_data(region_resource) : NULL;
4465
4466 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
4467 &zwp_confined_pointer_v1_interface,
4468 &confined_pointer_interface,
4469 &confined_pointer_grab_interface);
4470}
4471
4472static const struct zwp_pointer_constraints_v1_interface pointer_constraints_interface = {
4473 pointer_constraints_destroy,
4474 pointer_constraints_lock_pointer,
4475 pointer_constraints_confine_pointer,
4476};
4477
4478static void
4479bind_pointer_constraints(struct wl_client *client, void *data,
4480 uint32_t version, uint32_t id)
4481{
4482 struct wl_resource *resource;
4483
4484 resource = wl_resource_create(client,
4485 &zwp_pointer_constraints_v1_interface,
4486 1, id);
4487
4488 wl_resource_set_implementation(resource, &pointer_constraints_interface,
4489 NULL, NULL);
4490}
4491
Jonas Ådahl30d61d82014-10-22 21:21:17 +02004492int
4493weston_input_init(struct weston_compositor *compositor)
4494{
4495 if (!wl_global_create(compositor->wl_display,
4496 &zwp_relative_pointer_manager_v1_interface, 1,
4497 compositor, bind_relative_pointer_manager))
4498 return -1;
4499
Jonas Ådahld3414f22016-07-22 17:56:31 +08004500 if (!wl_global_create(compositor->wl_display,
4501 &zwp_pointer_constraints_v1_interface, 1,
4502 NULL, bind_pointer_constraints))
4503 return -1;
4504
Jonas Ådahl30d61d82014-10-22 21:21:17 +02004505 return 0;
4506}