blob: 6db476f73e38e086a1eff1e189ad62b3fe8de77b [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{
Alexandros Frantzis7a314d62018-01-26 18:47:56 +02001147 struct wl_resource *resource;
1148
1149 wl_resource_for_each(resource, &keyboard->resource_list) {
1150 wl_resource_set_user_data(resource, NULL);
1151 }
1152
1153 wl_resource_for_each(resource, &keyboard->focus_resource_list) {
1154 wl_resource_set_user_data(resource, NULL);
1155 }
1156
1157 wl_list_remove(&keyboard->resource_list);
1158 wl_list_remove(&keyboard->focus_resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +01001159
Derek Foreman185d1582017-06-28 11:17:23 -05001160 xkb_state_unref(keyboard->xkb_state.state);
1161 if (keyboard->xkb_info)
1162 weston_xkb_info_destroy(keyboard->xkb_info);
1163 xkb_keymap_unref(keyboard->pending_keymap);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001164
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001165 wl_array_release(&keyboard->keys);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001166 wl_list_remove(&keyboard->focus_resource_listener.link);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001167 free(keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001168}
1169
Jonas Ådahlcbfa7f72013-12-02 22:05:04 +01001170static void
1171weston_touch_reset_state(struct weston_touch *touch)
1172{
1173 touch->num_tp = 0;
1174}
1175
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001176WL_EXPORT struct weston_touch *
1177weston_touch_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001178{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001179 struct weston_touch *touch;
1180
Peter Huttererf3d62272013-08-08 11:57:05 +10001181 touch = zalloc(sizeof *touch);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001182 if (touch == NULL)
1183 return NULL;
1184
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001185 wl_list_init(&touch->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +01001186 wl_list_init(&touch->focus_resource_list);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001187 wl_list_init(&touch->focus_view_listener.link);
1188 touch->focus_view_listener.notify = touch_focus_view_destroyed;
1189 wl_list_init(&touch->focus_resource_listener.link);
1190 touch->focus_resource_listener.notify = touch_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001191 touch->default_grab.interface = &default_touch_grab_interface;
1192 touch->default_grab.touch = touch;
1193 touch->grab = &touch->default_grab;
1194 wl_signal_init(&touch->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001195
1196 return touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001197}
1198
1199WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001200weston_touch_destroy(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001201{
1202 /* XXX: What about touch->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +01001203
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001204 wl_list_remove(&touch->focus_view_listener.link);
1205 wl_list_remove(&touch->focus_resource_listener.link);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001206 free(touch);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001207}
1208
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001209static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04001210seat_send_updated_caps(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001211{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001212 enum wl_seat_capability caps = 0;
Rob Bradford6e737f52013-09-06 17:48:19 +01001213 struct wl_resource *resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001214
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001215 if (seat->pointer_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001216 caps |= WL_SEAT_CAPABILITY_POINTER;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001217 if (seat->keyboard_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001218 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001219 if (seat->touch_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001220 caps |= WL_SEAT_CAPABILITY_TOUCH;
1221
Rob Bradford6e737f52013-09-06 17:48:19 +01001222 wl_resource_for_each(resource, &seat->base_resource_list) {
1223 wl_seat_send_capabilities(resource, caps);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001224 }
Jason Ekstranda4ab5422014-04-02 19:53:45 -05001225 wl_signal_emit(&seat->updated_caps_signal, seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001226}
1227
Derek Foremanf9318d12015-05-11 15:40:11 -05001228
1229/** Clear the pointer focus
1230 *
1231 * \param pointer the pointer to clear focus for.
1232 *
1233 * This can be used to unset pointer focus and set the co-ordinates to the
1234 * arbitrary values we use for the no focus case.
1235 *
1236 * There's no requirement to use this function. For example, passing the
1237 * results of a weston_compositor_pick_view() directly to
1238 * weston_pointer_set_focus() will do the right thing when no view is found.
1239 */
1240WL_EXPORT void
1241weston_pointer_clear_focus(struct weston_pointer *pointer)
1242{
1243 weston_pointer_set_focus(pointer, NULL,
1244 wl_fixed_from_int(-1000000),
1245 wl_fixed_from_int(-1000000));
1246}
1247
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001248WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001249weston_pointer_set_focus(struct weston_pointer *pointer,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001250 struct weston_view *view,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001251 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001252{
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001253 struct weston_pointer_client *pointer_client;
Derek Foreman1281a362015-07-31 16:55:32 -05001254 struct weston_keyboard *kbd = weston_seat_get_keyboard(pointer->seat);
Neil Roberts96d790e2013-09-19 17:32:00 +01001255 struct wl_resource *resource;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001256 struct wl_resource *surface_resource;
Rob Bradford880ebc72013-07-22 17:31:38 +01001257 struct wl_display *display = pointer->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001258 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01001259 struct wl_list *focus_resource_list;
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001260 int refocus = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001261
1262 if ((!pointer->focus && view) ||
1263 (pointer->focus && !view) ||
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001264 (pointer->focus && pointer->focus->surface != view->surface) ||
1265 pointer->sx != sx || pointer->sy != sy)
1266 refocus = 1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001267
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001268 if (pointer->focus_client && refocus) {
1269 focus_resource_list = &pointer->focus_client->pointer_resources;
1270 if (!wl_list_empty(focus_resource_list)) {
1271 serial = wl_display_next_serial(display);
1272 surface_resource = pointer->focus->surface->resource;
1273 wl_resource_for_each(resource, focus_resource_list) {
1274 wl_pointer_send_leave(resource, serial,
1275 surface_resource);
Peter Hutterer87743e92016-01-18 16:38:22 +10001276 pointer_send_frame(resource);
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001277 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001278 }
1279
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001280 pointer->focus_client = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001281 }
1282
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001283 pointer_client = find_pointer_client_for_view(pointer, view);
1284 if (pointer_client && refocus) {
1285 struct wl_client *surface_client = pointer_client->client;
Neil Roberts96d790e2013-09-19 17:32:00 +01001286
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001287 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001288
Jason Ekstranda7af7042013-10-12 22:38:11 -05001289 if (kbd && kbd->focus != view->surface)
Kristian Høgsbergcb406f12013-10-09 10:54:03 -07001290 send_modifiers_to_client_in_list(surface_client,
1291 &kbd->resource_list,
1292 serial,
1293 kbd);
1294
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001295 pointer->focus_client = pointer_client;
Neil Roberts96d790e2013-09-19 17:32:00 +01001296
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001297 focus_resource_list = &pointer->focus_client->pointer_resources;
Neil Roberts96d790e2013-09-19 17:32:00 +01001298 wl_resource_for_each(resource, focus_resource_list) {
1299 wl_pointer_send_enter(resource,
1300 serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001301 view->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01001302 sx, sy);
Peter Hutterer87743e92016-01-18 16:38:22 +10001303 pointer_send_frame(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001304 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001305
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001306 pointer->focus_serial = serial;
1307 }
1308
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001309 wl_list_remove(&pointer->focus_view_listener.link);
1310 wl_list_init(&pointer->focus_view_listener.link);
1311 wl_list_remove(&pointer->focus_resource_listener.link);
1312 wl_list_init(&pointer->focus_resource_listener.link);
Emilio Pozuelo Monfortaa7a4762013-11-19 11:37:15 +01001313 if (view)
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001314 wl_signal_add(&view->destroy_signal, &pointer->focus_view_listener);
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001315 if (view && view->surface->resource)
1316 wl_resource_add_destroy_listener(view->surface->resource,
1317 &pointer->focus_resource_listener);
1318
1319 pointer->focus = view;
1320 pointer->focus_view_listener.notify = pointer_focus_view_destroyed;
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001321 pointer->sx = sx;
1322 pointer->sy = sy;
1323
Derek Foremanf9318d12015-05-11 15:40:11 -05001324 assert(view || sx == wl_fixed_from_int(-1000000));
1325 assert(view || sy == wl_fixed_from_int(-1000000));
1326
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001327 wl_signal_emit(&pointer->focus_signal, pointer);
1328}
1329
Neil Roberts96d790e2013-09-19 17:32:00 +01001330static void
1331send_enter_to_resource_list(struct wl_list *list,
1332 struct weston_keyboard *keyboard,
1333 struct weston_surface *surface,
1334 uint32_t serial)
1335{
1336 struct wl_resource *resource;
1337
1338 wl_resource_for_each(resource, list) {
1339 send_modifiers_to_resource(keyboard, resource, serial);
1340 wl_keyboard_send_enter(resource, serial,
1341 surface->resource,
1342 &keyboard->keys);
1343 }
1344}
1345
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001346WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001347weston_keyboard_set_focus(struct weston_keyboard *keyboard,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001348 struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001349{
Quentin Glidic85d55542017-07-21 14:02:40 +02001350 struct weston_seat *seat = keyboard->seat;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001351 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +01001352 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001353 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01001354 struct wl_list *focus_resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001355
Neil Roberts96d790e2013-09-19 17:32:00 +01001356 focus_resource_list = &keyboard->focus_resource_list;
1357
1358 if (!wl_list_empty(focus_resource_list) && keyboard->focus != surface) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001359 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001360 wl_resource_for_each(resource, focus_resource_list) {
1361 wl_keyboard_send_leave(resource, serial,
1362 keyboard->focus->resource);
1363 }
1364 move_resources(&keyboard->resource_list, focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001365 }
1366
Neil Roberts96d790e2013-09-19 17:32:00 +01001367 if (find_resource_for_surface(&keyboard->resource_list, surface) &&
1368 keyboard->focus != surface) {
1369 struct wl_client *surface_client =
1370 wl_resource_get_client(surface->resource);
1371
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001372 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001373
1374 move_resources_for_client(focus_resource_list,
1375 &keyboard->resource_list,
1376 surface_client);
1377 send_enter_to_resource_list(focus_resource_list,
1378 keyboard,
1379 surface,
1380 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001381 keyboard->focus_serial = serial;
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001382 }
1383
Quentin Glidic85d55542017-07-21 14:02:40 +02001384 if (seat->saved_kbd_focus) {
1385 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1386 seat->saved_kbd_focus = NULL;
1387 }
1388
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001389 wl_list_remove(&keyboard->focus_resource_listener.link);
1390 wl_list_init(&keyboard->focus_resource_listener.link);
1391 if (surface && surface->resource)
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001392 wl_resource_add_destroy_listener(surface->resource,
1393 &keyboard->focus_resource_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001394
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001395 keyboard->focus = surface;
1396 wl_signal_emit(&keyboard->focus_signal, keyboard);
1397}
1398
Giulio Camuffoa20ca812014-11-22 11:16:56 +02001399/* Users of this function must manually manage the keyboard focus */
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001400WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001401weston_keyboard_start_grab(struct weston_keyboard *keyboard,
1402 struct weston_keyboard_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001403{
1404 keyboard->grab = grab;
1405 grab->keyboard = keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001406}
1407
1408WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001409weston_keyboard_end_grab(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001410{
1411 keyboard->grab = &keyboard->default_grab;
1412}
1413
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001414static void
1415weston_keyboard_cancel_grab(struct weston_keyboard *keyboard)
1416{
1417 keyboard->grab->interface->cancel(keyboard->grab);
1418}
1419
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001420WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001421weston_pointer_start_grab(struct weston_pointer *pointer,
1422 struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001423{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001424 pointer->grab = grab;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001425 grab->pointer = pointer;
Kristian Høgsbergda751b82013-07-04 00:58:07 -04001426 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001427}
1428
1429WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001430weston_pointer_end_grab(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001431{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001432 pointer->grab = &pointer->default_grab;
Kristian Høgsbergda751b82013-07-04 00:58:07 -04001433 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001434}
1435
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001436static void
1437weston_pointer_cancel_grab(struct weston_pointer *pointer)
1438{
1439 pointer->grab->interface->cancel(pointer->grab);
1440}
1441
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001442WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -04001443weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001444{
1445 touch->grab = grab;
1446 grab->touch = touch;
1447}
1448
1449WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -04001450weston_touch_end_grab(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001451{
1452 touch->grab = &touch->default_grab;
1453}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001454
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001455static void
1456weston_touch_cancel_grab(struct weston_touch *touch)
1457{
1458 touch->grab->interface->cancel(touch->grab);
1459}
1460
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001461static void
1462weston_pointer_clamp_for_output(struct weston_pointer *pointer,
1463 struct weston_output *output,
1464 wl_fixed_t *fx, wl_fixed_t *fy)
1465{
1466 int x, y;
1467
1468 x = wl_fixed_to_int(*fx);
1469 y = wl_fixed_to_int(*fy);
1470
1471 if (x < output->x)
1472 *fx = wl_fixed_from_int(output->x);
1473 else if (x >= output->x + output->width)
1474 *fx = wl_fixed_from_int(output->x +
1475 output->width - 1);
1476 if (y < output->y)
1477 *fy = wl_fixed_from_int(output->y);
1478 else if (y >= output->y + output->height)
1479 *fy = wl_fixed_from_int(output->y +
1480 output->height - 1);
1481}
1482
Rob Bradford806d8c02013-06-25 18:56:41 +01001483WL_EXPORT void
1484weston_pointer_clamp(struct weston_pointer *pointer, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001485{
Rob Bradford806d8c02013-06-25 18:56:41 +01001486 struct weston_compositor *ec = pointer->seat->compositor;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001487 struct weston_output *output, *prev = NULL;
1488 int x, y, old_x, old_y, valid = 0;
1489
1490 x = wl_fixed_to_int(*fx);
1491 y = wl_fixed_to_int(*fy);
Rob Bradford806d8c02013-06-25 18:56:41 +01001492 old_x = wl_fixed_to_int(pointer->x);
1493 old_y = wl_fixed_to_int(pointer->y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001494
1495 wl_list_for_each(output, &ec->output_list, link) {
Rob Bradford66bd9f52013-06-25 18:56:42 +01001496 if (pointer->seat->output && pointer->seat->output != output)
1497 continue;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001498 if (pixman_region32_contains_point(&output->region,
1499 x, y, NULL))
1500 valid = 1;
1501 if (pixman_region32_contains_point(&output->region,
1502 old_x, old_y, NULL))
1503 prev = output;
1504 }
1505
Rob Bradford66bd9f52013-06-25 18:56:42 +01001506 if (!prev)
1507 prev = pointer->seat->output;
1508
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001509 if (prev && !valid)
1510 weston_pointer_clamp_for_output(pointer, prev, fx, fy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001511}
1512
Jonas Ådahld2510102014-10-05 21:39:14 +02001513static void
1514weston_pointer_move_to(struct weston_pointer *pointer,
1515 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001516{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001517 int32_t ix, iy;
1518
Rob Bradford806d8c02013-06-25 18:56:41 +01001519 weston_pointer_clamp (pointer, &x, &y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001520
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001521 pointer->x = x;
1522 pointer->y = y;
1523
1524 ix = wl_fixed_to_int(x);
1525 iy = wl_fixed_to_int(y);
1526
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001527 if (pointer->sprite) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001528 weston_view_set_position(pointer->sprite,
1529 ix - pointer->hotspot_x,
1530 iy - pointer->hotspot_y);
1531 weston_view_schedule_repaint(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001532 }
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001533
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001534 pointer->grab->interface->focus(pointer->grab);
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001535 wl_signal_emit(&pointer->motion_signal, pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001536}
1537
Jonas Ådahld2510102014-10-05 21:39:14 +02001538WL_EXPORT void
Jonas Ådahld2510102014-10-05 21:39:14 +02001539weston_pointer_move(struct weston_pointer *pointer,
1540 struct weston_pointer_motion_event *event)
1541{
1542 wl_fixed_t x, y;
1543
1544 weston_pointer_motion_to_abs(pointer, event, &x, &y);
1545 weston_pointer_move_to(pointer, x, y);
1546}
1547
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001548/** Verify if the pointer is in a valid position and move it if it isn't.
1549 */
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001550static void
1551weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001552{
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001553 struct weston_pointer *pointer;
1554 struct weston_compositor *ec;
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001555 struct weston_output *output, *closest = NULL;
1556 int x, y, distance, min = INT_MAX;
1557 wl_fixed_t fx, fy;
1558
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001559 pointer = container_of(listener, struct weston_pointer,
1560 output_destroy_listener);
1561 ec = pointer->seat->compositor;
1562
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001563 x = wl_fixed_to_int(pointer->x);
1564 y = wl_fixed_to_int(pointer->y);
1565
1566 wl_list_for_each(output, &ec->output_list, link) {
1567 if (pixman_region32_contains_point(&output->region,
1568 x, y, NULL))
1569 return;
1570
1571 /* Aproximante the distance from the pointer to the center of
1572 * the output. */
1573 distance = abs(output->x + output->width / 2 - x) +
1574 abs(output->y + output->height / 2 - y);
1575 if (distance < min) {
1576 min = distance;
1577 closest = output;
1578 }
1579 }
1580
1581 /* Nothing to do if there's no output left. */
1582 if (!closest)
1583 return;
1584
1585 fx = pointer->x;
1586 fy = pointer->y;
1587
1588 weston_pointer_clamp_for_output(pointer, closest, &fx, &fy);
Jonas Ådahld2510102014-10-05 21:39:14 +02001589 weston_pointer_move_to(pointer, fx, fy);
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001590}
1591
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001592WL_EXPORT void
1593notify_motion(struct weston_seat *seat,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001594 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001595 struct weston_pointer_motion_event *event)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001596{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001597 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001598 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001599
1600 weston_compositor_wake(ec);
Jonas Ådahld2510102014-10-05 21:39:14 +02001601 pointer->grab->interface->motion(pointer->grab, time, event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001602}
1603
Daniel Stone96d47c02013-11-19 11:37:12 +01001604static void
1605run_modifier_bindings(struct weston_seat *seat, uint32_t old, uint32_t new)
1606{
1607 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001608 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Daniel Stone96d47c02013-11-19 11:37:12 +01001609 uint32_t diff;
1610 unsigned int i;
1611 struct {
1612 uint32_t xkb;
1613 enum weston_keyboard_modifier weston;
1614 } mods[] = {
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001615 { keyboard->xkb_info->ctrl_mod, MODIFIER_CTRL },
1616 { keyboard->xkb_info->alt_mod, MODIFIER_ALT },
1617 { keyboard->xkb_info->super_mod, MODIFIER_SUPER },
1618 { keyboard->xkb_info->shift_mod, MODIFIER_SHIFT },
Daniel Stone96d47c02013-11-19 11:37:12 +01001619 };
1620
1621 diff = new & ~old;
1622 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
1623 if (diff & (1 << mods[i].xkb))
1624 weston_compositor_run_modifier_binding(compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001625 keyboard,
Daniel Stone96d47c02013-11-19 11:37:12 +01001626 mods[i].weston,
1627 WL_KEYBOARD_KEY_STATE_PRESSED);
1628 }
1629
1630 diff = old & ~new;
1631 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
1632 if (diff & (1 << mods[i].xkb))
1633 weston_compositor_run_modifier_binding(compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001634 keyboard,
Daniel Stone96d47c02013-11-19 11:37:12 +01001635 mods[i].weston,
1636 WL_KEYBOARD_KEY_STATE_RELEASED);
1637 }
1638}
1639
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001640WL_EXPORT void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001641notify_motion_absolute(struct weston_seat *seat, const struct timespec *time,
1642 double x, double y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001643{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001644 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001645 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Jonas Ådahld2510102014-10-05 21:39:14 +02001646 struct weston_pointer_motion_event event = { 0 };
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001647
1648 weston_compositor_wake(ec);
Jonas Ådahld2510102014-10-05 21:39:14 +02001649
1650 event = (struct weston_pointer_motion_event) {
1651 .mask = WESTON_POINTER_MOTION_ABS,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001652 .x = x,
1653 .y = y,
Jonas Ådahld2510102014-10-05 21:39:14 +02001654 };
1655
1656 pointer->grab->interface->motion(pointer->grab, time, &event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001657}
1658
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02001659static unsigned int
1660peek_next_activate_serial(struct weston_compositor *c)
1661{
1662 unsigned serial = c->activate_serial + 1;
1663
1664 return serial == 0 ? 1 : serial;
1665}
1666
1667static void
1668inc_activate_serial(struct weston_compositor *c)
1669{
1670 c->activate_serial = peek_next_activate_serial (c);
1671}
1672
1673WL_EXPORT void
1674weston_view_activate(struct weston_view *view,
1675 struct weston_seat *seat,
1676 uint32_t flags)
1677{
1678 struct weston_compositor *compositor = seat->compositor;
1679
1680 if (flags & WESTON_ACTIVATE_FLAG_CLICKED) {
1681 view->click_to_activate_serial =
1682 peek_next_activate_serial(compositor);
1683 }
1684
1685 weston_seat_set_keyboard_focus(seat, view->surface);
1686}
1687
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001688WL_EXPORT void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001689notify_button(struct weston_seat *seat, const struct timespec *time,
1690 int32_t button, enum wl_pointer_button_state state)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001691{
1692 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001693 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001694
1695 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001696 weston_compositor_idle_inhibit(compositor);
1697 if (pointer->button_count == 0) {
1698 pointer->grab_button = button;
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001699 pointer->grab_time = *time;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001700 pointer->grab_x = pointer->x;
1701 pointer->grab_y = pointer->y;
1702 }
1703 pointer->button_count++;
1704 } else {
1705 weston_compositor_idle_release(compositor);
1706 pointer->button_count--;
1707 }
1708
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001709 weston_compositor_run_button_binding(compositor, pointer, time, button,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001710 state);
1711
1712 pointer->grab->interface->button(pointer->grab, time, button, state);
1713
1714 if (pointer->button_count == 1)
1715 pointer->grab_serial =
1716 wl_display_get_serial(compositor->wl_display);
1717}
1718
1719WL_EXPORT void
Alexandros Frantzis80321942017-11-16 18:20:56 +02001720notify_axis(struct weston_seat *seat, const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001721 struct weston_pointer_axis_event *event)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001722{
1723 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001724 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001725
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001726 weston_compositor_wake(compositor);
1727
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001728 if (weston_compositor_run_axis_binding(compositor, pointer,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001729 time, event))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001730 return;
1731
Peter Hutterer89b6a492016-01-18 15:58:17 +10001732 pointer->grab->interface->axis(pointer->grab, time, event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001733}
1734
Peter Hutterer87743e92016-01-18 16:38:22 +10001735WL_EXPORT void
1736notify_axis_source(struct weston_seat *seat, uint32_t source)
1737{
1738 struct weston_compositor *compositor = seat->compositor;
1739 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1740
1741 weston_compositor_wake(compositor);
1742
1743 pointer->grab->interface->axis_source(pointer->grab, source);
1744}
1745
1746WL_EXPORT void
1747notify_pointer_frame(struct weston_seat *seat)
1748{
1749 struct weston_compositor *compositor = seat->compositor;
1750 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1751
1752 weston_compositor_wake(compositor);
1753
1754 pointer->grab->interface->frame(pointer->grab);
1755}
1756
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001757WL_EXPORT int
1758weston_keyboard_set_locks(struct weston_keyboard *keyboard,
1759 uint32_t mask, uint32_t value)
1760{
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001761 uint32_t serial;
1762 xkb_mod_mask_t mods_depressed, mods_latched, mods_locked, group;
1763 xkb_mod_mask_t num, caps;
1764
1765 /* We don't want the leds to go out of sync with the actual state
1766 * so if the backend has no way to change the leds don't try to
1767 * change the state */
1768 if (!keyboard->seat->led_update)
1769 return -1;
1770
1771 mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
1772 XKB_STATE_DEPRESSED);
1773 mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
1774 XKB_STATE_LATCHED);
1775 mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
1776 XKB_STATE_LOCKED);
1777 group = xkb_state_serialize_group(keyboard->xkb_state.state,
1778 XKB_STATE_EFFECTIVE);
1779
1780 num = (1 << keyboard->xkb_info->mod2_mod);
1781 caps = (1 << keyboard->xkb_info->caps_mod);
1782 if (mask & WESTON_NUM_LOCK) {
1783 if (value & WESTON_NUM_LOCK)
1784 mods_locked |= num;
1785 else
1786 mods_locked &= ~num;
1787 }
1788 if (mask & WESTON_CAPS_LOCK) {
1789 if (value & WESTON_CAPS_LOCK)
1790 mods_locked |= caps;
1791 else
1792 mods_locked &= ~caps;
1793 }
1794
1795 xkb_state_update_mask(keyboard->xkb_state.state, mods_depressed,
1796 mods_latched, mods_locked, 0, 0, group);
1797
1798 serial = wl_display_next_serial(
1799 keyboard->seat->compositor->wl_display);
1800 notify_modifiers(keyboard->seat, serial);
1801
1802 return 0;
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001803}
1804
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001805WL_EXPORT void
1806notify_modifiers(struct weston_seat *seat, uint32_t serial)
1807{
Derek Foreman1281a362015-07-31 16:55:32 -05001808 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001809 struct weston_keyboard_grab *grab = keyboard->grab;
1810 uint32_t mods_depressed, mods_latched, mods_locked, group;
1811 uint32_t mods_lookup;
1812 enum weston_led leds = 0;
1813 int changed = 0;
1814
1815 /* Serialize and update our internal state, checking to see if it's
1816 * different to the previous state. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001817 mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03001818 XKB_STATE_MODS_DEPRESSED);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001819 mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03001820 XKB_STATE_MODS_LATCHED);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001821 mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03001822 XKB_STATE_MODS_LOCKED);
1823 group = xkb_state_serialize_layout(keyboard->xkb_state.state,
1824 XKB_STATE_LAYOUT_EFFECTIVE);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001825
Derek Foreman244e99e2015-06-03 15:53:26 -05001826 if (mods_depressed != keyboard->modifiers.mods_depressed ||
1827 mods_latched != keyboard->modifiers.mods_latched ||
1828 mods_locked != keyboard->modifiers.mods_locked ||
1829 group != keyboard->modifiers.group)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001830 changed = 1;
1831
Derek Foreman244e99e2015-06-03 15:53:26 -05001832 run_modifier_bindings(seat, keyboard->modifiers.mods_depressed,
Daniel Stone96d47c02013-11-19 11:37:12 +01001833 mods_depressed);
1834
Derek Foreman244e99e2015-06-03 15:53:26 -05001835 keyboard->modifiers.mods_depressed = mods_depressed;
1836 keyboard->modifiers.mods_latched = mods_latched;
1837 keyboard->modifiers.mods_locked = mods_locked;
1838 keyboard->modifiers.group = group;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001839
1840 /* And update the modifier_state for bindings. */
1841 mods_lookup = mods_depressed | mods_latched;
1842 seat->modifier_state = 0;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001843 if (mods_lookup & (1 << keyboard->xkb_info->ctrl_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001844 seat->modifier_state |= MODIFIER_CTRL;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001845 if (mods_lookup & (1 << keyboard->xkb_info->alt_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001846 seat->modifier_state |= MODIFIER_ALT;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001847 if (mods_lookup & (1 << keyboard->xkb_info->super_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001848 seat->modifier_state |= MODIFIER_SUPER;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001849 if (mods_lookup & (1 << keyboard->xkb_info->shift_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001850 seat->modifier_state |= MODIFIER_SHIFT;
1851
1852 /* Finally, notify the compositor that LEDs have changed. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001853 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
1854 keyboard->xkb_info->num_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001855 leds |= LED_NUM_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001856 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
1857 keyboard->xkb_info->caps_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001858 leds |= LED_CAPS_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001859 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
1860 keyboard->xkb_info->scroll_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001861 leds |= LED_SCROLL_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001862 if (leds != keyboard->xkb_state.leds && seat->led_update)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001863 seat->led_update(seat, leds);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001864 keyboard->xkb_state.leds = leds;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001865
1866 if (changed) {
1867 grab->interface->modifiers(grab,
1868 serial,
1869 keyboard->modifiers.mods_depressed,
1870 keyboard->modifiers.mods_latched,
1871 keyboard->modifiers.mods_locked,
1872 keyboard->modifiers.group);
1873 }
1874}
1875
1876static void
1877update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
1878 enum wl_keyboard_key_state state)
1879{
Derek Foreman1281a362015-07-31 16:55:32 -05001880 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001881 enum xkb_key_direction direction;
1882
1883 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1884 direction = XKB_KEY_DOWN;
1885 else
1886 direction = XKB_KEY_UP;
1887
1888 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
1889 * broken keycode system, which starts at 8. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001890 xkb_state_update_key(keyboard->xkb_state.state, key + 8, direction);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001891
1892 notify_modifiers(seat, serial);
1893}
Rui Matos65196bc2013-10-10 19:44:19 +02001894
1895static void
1896send_keymap(struct wl_resource *resource, struct weston_xkb_info *xkb_info)
1897{
1898 wl_keyboard_send_keymap(resource,
1899 WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
1900 xkb_info->keymap_fd,
1901 xkb_info->keymap_size);
1902}
1903
1904static void
1905send_modifiers(struct wl_resource *resource, uint32_t serial, struct weston_keyboard *keyboard)
1906{
1907 wl_keyboard_send_modifiers(resource, serial,
1908 keyboard->modifiers.mods_depressed,
1909 keyboard->modifiers.mods_latched,
1910 keyboard->modifiers.mods_locked,
1911 keyboard->modifiers.group);
1912}
1913
1914static struct weston_xkb_info *
1915weston_xkb_info_create(struct xkb_keymap *keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02001916
1917static void
1918update_keymap(struct weston_seat *seat)
1919{
Derek Foreman1281a362015-07-31 16:55:32 -05001920 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Rui Matos65196bc2013-10-10 19:44:19 +02001921 struct wl_resource *resource;
1922 struct weston_xkb_info *xkb_info;
1923 struct xkb_state *state;
1924 xkb_mod_mask_t latched_mods;
1925 xkb_mod_mask_t locked_mods;
1926
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001927 xkb_info = weston_xkb_info_create(keyboard->pending_keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02001928
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001929 xkb_keymap_unref(keyboard->pending_keymap);
1930 keyboard->pending_keymap = NULL;
Rui Matos65196bc2013-10-10 19:44:19 +02001931
1932 if (!xkb_info) {
1933 weston_log("failed to create XKB info\n");
1934 return;
1935 }
1936
1937 state = xkb_state_new(xkb_info->keymap);
1938 if (!state) {
1939 weston_log("failed to initialise XKB state\n");
1940 weston_xkb_info_destroy(xkb_info);
1941 return;
1942 }
1943
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001944 latched_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
1945 XKB_STATE_MODS_LATCHED);
1946 locked_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
1947 XKB_STATE_MODS_LOCKED);
Rui Matos65196bc2013-10-10 19:44:19 +02001948 xkb_state_update_mask(state,
1949 0, /* depressed */
1950 latched_mods,
1951 locked_mods,
1952 0, 0, 0);
1953
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001954 weston_xkb_info_destroy(keyboard->xkb_info);
1955 keyboard->xkb_info = xkb_info;
Rui Matos65196bc2013-10-10 19:44:19 +02001956
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001957 xkb_state_unref(keyboard->xkb_state.state);
1958 keyboard->xkb_state.state = state;
Rui Matos65196bc2013-10-10 19:44:19 +02001959
Derek Foremanbc91e542015-06-03 15:53:27 -05001960 wl_resource_for_each(resource, &keyboard->resource_list)
Rui Matos65196bc2013-10-10 19:44:19 +02001961 send_keymap(resource, xkb_info);
Derek Foremanbc91e542015-06-03 15:53:27 -05001962 wl_resource_for_each(resource, &keyboard->focus_resource_list)
Rui Matos65196bc2013-10-10 19:44:19 +02001963 send_keymap(resource, xkb_info);
1964
1965 notify_modifiers(seat, wl_display_next_serial(seat->compositor->wl_display));
1966
1967 if (!latched_mods && !locked_mods)
1968 return;
1969
Derek Foremanbc91e542015-06-03 15:53:27 -05001970 wl_resource_for_each(resource, &keyboard->resource_list)
1971 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), keyboard);
1972 wl_resource_for_each(resource, &keyboard->focus_resource_list)
1973 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), keyboard);
Rui Matos65196bc2013-10-10 19:44:19 +02001974}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001975
1976WL_EXPORT void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02001977notify_key(struct weston_seat *seat, const struct timespec *time, uint32_t key,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001978 enum wl_keyboard_key_state state,
1979 enum weston_key_state_update update_state)
1980{
1981 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001982 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001983 struct weston_keyboard_grab *grab = keyboard->grab;
Pekka Paalanen86b53962014-11-19 13:43:32 +02001984 uint32_t *k, *end;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001985
1986 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001987 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001988 } else {
1989 weston_compositor_idle_release(compositor);
1990 }
1991
Pekka Paalanen86b53962014-11-19 13:43:32 +02001992 end = keyboard->keys.data + keyboard->keys.size;
1993 for (k = keyboard->keys.data; k < end; k++) {
1994 if (*k == key) {
1995 /* Ignore server-generated repeats. */
1996 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1997 return;
1998 *k = *--end;
1999 }
2000 }
2001 keyboard->keys.size = (void *) end - keyboard->keys.data;
2002 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2003 k = wl_array_add(&keyboard->keys, sizeof *k);
2004 *k = key;
2005 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002006
2007 if (grab == &keyboard->default_grab ||
2008 grab == &keyboard->input_method_grab) {
Derek Foreman99a6a2d2015-07-15 13:00:43 -05002009 weston_compositor_run_key_binding(compositor, keyboard, time,
2010 key, state);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002011 grab = keyboard->grab;
2012 }
2013
2014 grab->interface->key(grab, time, key, state);
2015
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002016 if (keyboard->pending_keymap &&
Pekka Paalanen86b53962014-11-19 13:43:32 +02002017 keyboard->keys.size == 0)
Rui Matos65196bc2013-10-10 19:44:19 +02002018 update_keymap(seat);
2019
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002020 if (update_state == STATE_UPDATE_AUTOMATIC) {
2021 update_modifier_state(seat,
2022 wl_display_get_serial(compositor->wl_display),
2023 key,
2024 state);
2025 }
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002026
Olivier Fourdandf84dbe2016-06-30 16:01:56 +02002027 keyboard->grab_serial = wl_display_get_serial(compositor->wl_display);
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002028 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02002029 keyboard->grab_time = *time;
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002030 keyboard->grab_key = key;
2031 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002032}
2033
2034WL_EXPORT void
2035notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002036 double x, double y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002037{
Derek Foreman1281a362015-07-31 16:55:32 -05002038 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2039
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002040 if (output) {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002041 weston_pointer_move_to(pointer,
2042 wl_fixed_from_double(x),
2043 wl_fixed_from_double(y));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002044 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002045 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002046 * NULL) here, but somehow that breaks re-entry... */
2047 }
2048}
2049
2050static void
2051destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
2052{
2053 struct weston_seat *ws;
2054
2055 ws = container_of(listener, struct weston_seat,
2056 saved_kbd_focus_listener);
2057
2058 ws->saved_kbd_focus = NULL;
2059}
2060
2061WL_EXPORT void
2062notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
2063 enum weston_key_state_update update_state)
2064{
2065 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002066 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04002067 struct weston_surface *surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002068 uint32_t *k, serial;
2069
2070 serial = wl_display_next_serial(compositor->wl_display);
2071 wl_array_copy(&keyboard->keys, keys);
2072 wl_array_for_each(k, &keyboard->keys) {
2073 weston_compositor_idle_inhibit(compositor);
2074 if (update_state == STATE_UPDATE_AUTOMATIC)
2075 update_modifier_state(seat, serial, *k,
2076 WL_KEYBOARD_KEY_STATE_PRESSED);
2077 }
2078
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002079 surface = seat->saved_kbd_focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002080 if (surface) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002081 weston_keyboard_set_focus(keyboard, surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002082 }
2083}
2084
2085WL_EXPORT void
2086notify_keyboard_focus_out(struct weston_seat *seat)
2087{
2088 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002089 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2090 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Quentin Glidic85d55542017-07-21 14:02:40 +02002091 struct weston_surface *focus = keyboard->focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002092 uint32_t *k, serial;
2093
2094 serial = wl_display_next_serial(compositor->wl_display);
2095 wl_array_for_each(k, &keyboard->keys) {
2096 weston_compositor_idle_release(compositor);
2097 update_modifier_state(seat, serial, *k,
2098 WL_KEYBOARD_KEY_STATE_RELEASED);
2099 }
2100
2101 seat->modifier_state = 0;
2102
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002103 weston_keyboard_set_focus(keyboard, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002104 weston_keyboard_cancel_grab(keyboard);
Derek Foreman1281a362015-07-31 16:55:32 -05002105 if (pointer)
2106 weston_pointer_cancel_grab(pointer);
Quentin Glidic85d55542017-07-21 14:02:40 +02002107
2108 if (focus) {
2109 seat->saved_kbd_focus = focus;
2110 seat->saved_kbd_focus_listener.notify =
2111 destroy_device_saved_kbd_focus;
2112 wl_signal_add(&focus->destroy_signal,
2113 &seat->saved_kbd_focus_listener);
2114 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002115}
2116
Michael Fua2bb7912013-07-23 15:51:06 +08002117WL_EXPORT void
Derek Foreman4c93c082015-04-30 16:45:41 -05002118weston_touch_set_focus(struct weston_touch *touch, struct weston_view *view)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002119{
Neil Roberts96d790e2013-09-19 17:32:00 +01002120 struct wl_list *focus_resource_list;
2121
Derek Foreman4c93c082015-04-30 16:45:41 -05002122 focus_resource_list = &touch->focus_resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002123
Derek Foreman4c93c082015-04-30 16:45:41 -05002124 if (view && touch->focus &&
2125 touch->focus->surface == view->surface) {
2126 touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002127 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002128 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002129
Derek Foreman4c93c082015-04-30 16:45:41 -05002130 wl_list_remove(&touch->focus_resource_listener.link);
2131 wl_list_init(&touch->focus_resource_listener.link);
2132 wl_list_remove(&touch->focus_view_listener.link);
2133 wl_list_init(&touch->focus_view_listener.link);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01002134
Neil Roberts96d790e2013-09-19 17:32:00 +01002135 if (!wl_list_empty(focus_resource_list)) {
Derek Foreman4c93c082015-04-30 16:45:41 -05002136 move_resources(&touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002137 focus_resource_list);
2138 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002139
Jason Ekstranda7af7042013-10-12 22:38:11 -05002140 if (view) {
Derek Foreman362656b2014-09-04 10:23:05 -05002141 struct wl_client *surface_client;
2142
2143 if (!view->surface->resource) {
Derek Foreman4c93c082015-04-30 16:45:41 -05002144 touch->focus = NULL;
Derek Foreman362656b2014-09-04 10:23:05 -05002145 return;
2146 }
2147
2148 surface_client = wl_resource_get_client(view->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +01002149 move_resources_for_client(focus_resource_list,
Derek Foreman4c93c082015-04-30 16:45:41 -05002150 &touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002151 surface_client);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01002152 wl_resource_add_destroy_listener(view->surface->resource,
Derek Foreman4c93c082015-04-30 16:45:41 -05002153 &touch->focus_resource_listener);
2154 wl_signal_add(&view->destroy_signal, &touch->focus_view_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002155 }
Derek Foreman4c93c082015-04-30 16:45:41 -05002156 touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002157}
2158
2159/**
2160 * notify_touch - emulates button touches and notifies surfaces accordingly.
2161 *
2162 * It assumes always the correct cycle sequence until it gets here: touch_down
2163 * → touch_update → ... → touch_update → touch_end. The driver is responsible
2164 * for sending along such order.
2165 *
2166 */
2167WL_EXPORT void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02002168notify_touch(struct weston_seat *seat, const struct timespec *time,
2169 int touch_id, double double_x, double double_y, int touch_type)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002170{
2171 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002172 struct weston_touch *touch = weston_seat_get_touch(seat);
Kristian Høgsberge329f362013-05-06 22:19:57 -04002173 struct weston_touch_grab *grab = touch->grab;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002174 struct weston_view *ev;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002175 wl_fixed_t sx, sy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002176 wl_fixed_t x = wl_fixed_from_double(double_x);
2177 wl_fixed_t y = wl_fixed_from_double(double_y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002178
2179 /* Update grab's global coordinates. */
Neil Roberts306fe082013-10-03 16:43:06 +01002180 if (touch_id == touch->grab_touch_id && touch_type != WL_TOUCH_UP) {
2181 touch->grab_x = x;
2182 touch->grab_y = y;
2183 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002184
2185 switch (touch_type) {
2186 case WL_TOUCH_DOWN:
2187 weston_compositor_idle_inhibit(ec);
2188
Jonas Ådahl9484b692013-12-02 22:05:03 +01002189 touch->num_tp++;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002190
Jason Ekstranda7af7042013-10-12 22:38:11 -05002191 /* the first finger down picks the view, and all further go
2192 * to that view for the remainder of the touch session i.e.
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002193 * until all touch points are up again. */
Jonas Ådahl9484b692013-12-02 22:05:03 +01002194 if (touch->num_tp == 1) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002195 ev = weston_compositor_pick_view(ec, x, y, &sx, &sy);
Derek Foreman4c93c082015-04-30 16:45:41 -05002196 weston_touch_set_focus(touch, ev);
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002197 } else if (!touch->focus) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002198 /* Unexpected condition: We have non-initial touch but
2199 * there is no focused surface.
2200 */
Chris Michael3f607d32015-10-07 11:59:49 -04002201 weston_log("touch event received with %d points down "
Jonas Ådahl9484b692013-12-02 22:05:03 +01002202 "but no surface focused\n", touch->num_tp);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002203 return;
2204 }
2205
Derek Foreman99a6a2d2015-07-15 13:00:43 -05002206 weston_compositor_run_touch_binding(ec, touch,
Kristian Høgsbergc8964012014-02-05 14:25:18 -08002207 time, touch_type);
2208
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002209 grab->interface->down(grab, time, touch_id, x, y);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002210 if (touch->num_tp == 1) {
Rusty Lynchf1407ff2013-08-08 21:13:57 -07002211 touch->grab_serial =
2212 wl_display_get_serial(ec->wl_display);
Neil Roberts306fe082013-10-03 16:43:06 +01002213 touch->grab_touch_id = touch_id;
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02002214 touch->grab_time = *time;
Rusty Lynchf1407ff2013-08-08 21:13:57 -07002215 touch->grab_x = x;
2216 touch->grab_y = y;
2217 }
2218
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002219 break;
2220 case WL_TOUCH_MOTION:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002221 ev = touch->focus;
2222 if (!ev)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002223 break;
2224
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +02002225 grab->interface->motion(grab, time, touch_id, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002226 break;
2227 case WL_TOUCH_UP:
Kristian Høgsberga30e29a2014-01-08 22:29:20 -08002228 if (touch->num_tp == 0) {
2229 /* This can happen if we start out with one or
2230 * more fingers on the touch screen, in which
2231 * case we didn't get the corresponding down
2232 * event. */
2233 weston_log("unmatched touch up event\n");
2234 break;
2235 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002236 weston_compositor_idle_release(ec);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002237 touch->num_tp--;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002238
Alexandros Frantzis27a51b82017-11-16 18:20:59 +02002239 grab->interface->up(grab, time, touch_id);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002240 if (touch->num_tp == 0)
Derek Foreman4c93c082015-04-30 16:45:41 -05002241 weston_touch_set_focus(touch, NULL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002242 break;
2243 }
2244}
2245
Jonas Ådahl1679f232014-04-12 09:39:51 +02002246WL_EXPORT void
2247notify_touch_frame(struct weston_seat *seat)
2248{
Derek Foreman1281a362015-07-31 16:55:32 -05002249 struct weston_touch *touch = weston_seat_get_touch(seat);
Jonas Ådahl1679f232014-04-12 09:39:51 +02002250 struct weston_touch_grab *grab = touch->grab;
2251
2252 grab->interface->frame(grab);
2253}
2254
Derek Foreman3cc004a2015-11-06 15:56:09 -06002255WL_EXPORT void
2256notify_touch_cancel(struct weston_seat *seat)
2257{
2258 struct weston_touch *touch = weston_seat_get_touch(seat);
2259 struct weston_touch_grab *grab = touch->grab;
2260
2261 grab->interface->cancel(grab);
2262}
2263
Pekka Paalanen8274d902014-08-06 19:36:51 +03002264static int
2265pointer_cursor_surface_get_label(struct weston_surface *surface,
2266 char *buf, size_t len)
2267{
2268 return snprintf(buf, len, "cursor");
2269}
2270
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002271static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002272pointer_cursor_surface_committed(struct weston_surface *es,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002273 int32_t dx, int32_t dy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002274{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002275 struct weston_pointer *pointer = es->committed_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002276 int x, y;
2277
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002278 if (es->width == 0)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002279 return;
2280
Jason Ekstranda7af7042013-10-12 22:38:11 -05002281 assert(es == pointer->sprite->surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002282
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002283 pointer->hotspot_x -= dx;
2284 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002285
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002286 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
2287 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002288
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002289 weston_view_set_position(pointer->sprite, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002290
2291 empty_region(&es->pending.input);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02002292 empty_region(&es->input);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002293
2294 if (!weston_surface_is_mapped(es)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002295 weston_layer_entry_insert(&es->compositor->cursor_layer.view_list,
2296 &pointer->sprite->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002297 weston_view_update_transform(pointer->sprite);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002298 es->is_mapped = true;
2299 pointer->sprite->is_mapped = true;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002300 }
2301}
2302
2303static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002304pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2305 uint32_t serial, struct wl_resource *surface_resource,
2306 int32_t x, int32_t y)
2307{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002308 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002309 struct weston_surface *surface = NULL;
2310
2311 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002312 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002313
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002314 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002315 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002316 /* pointer->focus->surface->resource can be NULL. Surfaces like the
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02002317 black_surface used in shell.c for fullscreen don't have
2318 a resource, but can still have focus */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002319 if (pointer->focus->surface->resource == NULL)
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02002320 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002321 if (wl_resource_get_client(pointer->focus->surface->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002322 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002323 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002324 return;
2325
Derek Foreman4e53c532015-03-23 10:55:32 -05002326 if (!surface) {
2327 if (pointer->sprite)
2328 pointer_unmap_sprite(pointer);
2329 return;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002330 }
2331
Jonas Ådahlb4070242015-03-18 15:08:03 +08002332 if (pointer->sprite && pointer->sprite->surface == surface &&
2333 pointer->hotspot_x == x && pointer->hotspot_y == y)
2334 return;
2335
Derek Foreman4e53c532015-03-23 10:55:32 -05002336 if (!pointer->sprite || pointer->sprite->surface != surface) {
2337 if (weston_surface_set_role(surface, "wl_pointer-cursor",
2338 resource,
2339 WL_POINTER_ERROR_ROLE) < 0)
2340 return;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002341
Derek Foreman4e53c532015-03-23 10:55:32 -05002342 if (pointer->sprite)
2343 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002344
Derek Foreman4e53c532015-03-23 10:55:32 -05002345 wl_signal_add(&surface->destroy_signal,
2346 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002347
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002348 surface->committed = pointer_cursor_surface_committed;
2349 surface->committed_private = pointer;
Derek Foreman4e53c532015-03-23 10:55:32 -05002350 weston_surface_set_label_func(surface,
2351 pointer_cursor_surface_get_label);
2352 pointer->sprite = weston_view_create(surface);
2353 }
2354
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002355 pointer->hotspot_x = x;
2356 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002357
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +02002358 if (surface->buffer_ref.buffer) {
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002359 pointer_cursor_surface_committed(surface, 0, 0);
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +02002360 weston_view_schedule_repaint(pointer->sprite);
2361 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002362}
2363
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002364static void
2365pointer_release(struct wl_client *client, struct wl_resource *resource)
2366{
2367 wl_resource_destroy(resource);
2368}
2369
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002370static const struct wl_pointer_interface pointer_interface = {
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002371 pointer_set_cursor,
2372 pointer_release
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002373};
2374
2375static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002376seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2377 uint32_t id)
2378{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002379 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002380 /* We use the pointer_state directly, which means we'll
2381 * give a wl_pointer if the seat has ever had one - even though
2382 * the spec explicitly states that this request only takes effect
2383 * if the seat has the pointer capability.
2384 *
2385 * This prevents a race between the compositor sending new
2386 * capabilities and the client trying to use the old ones.
2387 */
2388 struct weston_pointer *pointer = seat->pointer_state;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002389 struct wl_resource *cr;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002390 struct weston_pointer_client *pointer_client;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002391
Derek Foreman1281a362015-07-31 16:55:32 -05002392 if (!pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002393 return;
2394
Jason Ekstranda85118c2013-06-27 20:17:02 -05002395 cr = wl_resource_create(client, &wl_pointer_interface,
2396 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002397 if (cr == NULL) {
2398 wl_client_post_no_memory(client);
2399 return;
2400 }
2401
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002402 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
2403 if (!pointer_client) {
2404 wl_client_post_no_memory(client);
2405 return;
2406 }
2407
2408 wl_list_insert(&pointer_client->pointer_resources,
2409 wl_resource_get_link(cr));
Derek Foreman1281a362015-07-31 16:55:32 -05002410 wl_resource_set_implementation(cr, &pointer_interface, pointer,
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002411 unbind_pointer_client_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002412
Derek Foreman1281a362015-07-31 16:55:32 -05002413 if (pointer->focus && pointer->focus->surface->resource &&
2414 wl_resource_get_client(pointer->focus->surface->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002415 wl_fixed_t sx, sy;
2416
Derek Foreman1281a362015-07-31 16:55:32 -05002417 weston_view_from_global_fixed(pointer->focus,
2418 pointer->x,
2419 pointer->y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002420 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002421
Neil Roberts96d790e2013-09-19 17:32:00 +01002422 wl_pointer_send_enter(cr,
Derek Foreman1281a362015-07-31 16:55:32 -05002423 pointer->focus_serial,
2424 pointer->focus->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01002425 sx, sy);
Peter Hutterer87743e92016-01-18 16:38:22 +10002426 pointer_send_frame(cr);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002427 }
2428}
2429
2430static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002431keyboard_release(struct wl_client *client, struct wl_resource *resource)
2432{
2433 wl_resource_destroy(resource);
2434}
2435
2436static const struct wl_keyboard_interface keyboard_interface = {
2437 keyboard_release
2438};
2439
Derek Foreman280e7dd2014-10-03 13:13:42 -05002440static bool
Neil Roberts96d790e2013-09-19 17:32:00 +01002441should_send_modifiers_to_client(struct weston_seat *seat,
2442 struct wl_client *client)
2443{
Derek Foreman1281a362015-07-31 16:55:32 -05002444 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2445 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2446
2447 if (keyboard &&
2448 keyboard->focus &&
2449 keyboard->focus->resource &&
2450 wl_resource_get_client(keyboard->focus->resource) == client)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002451 return true;
Neil Roberts96d790e2013-09-19 17:32:00 +01002452
Derek Foreman1281a362015-07-31 16:55:32 -05002453 if (pointer &&
2454 pointer->focus &&
2455 pointer->focus->surface->resource &&
2456 wl_resource_get_client(pointer->focus->surface->resource) == client)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002457 return true;
Neil Roberts96d790e2013-09-19 17:32:00 +01002458
Derek Foreman280e7dd2014-10-03 13:13:42 -05002459 return false;
Neil Roberts96d790e2013-09-19 17:32:00 +01002460}
2461
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002462static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002463seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2464 uint32_t id)
2465{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002466 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002467 /* We use the keyboard_state directly, which means we'll
2468 * give a wl_keyboard if the seat has ever had one - even though
2469 * the spec explicitly states that this request only takes effect
2470 * if the seat has the keyboard capability.
2471 *
2472 * This prevents a race between the compositor sending new
2473 * capabilities and the client trying to use the old ones.
2474 */
2475 struct weston_keyboard *keyboard = seat->keyboard_state;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002476 struct wl_resource *cr;
2477
Derek Foreman345c9f32015-06-03 15:53:28 -05002478 if (!keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002479 return;
2480
Jason Ekstranda85118c2013-06-27 20:17:02 -05002481 cr = wl_resource_create(client, &wl_keyboard_interface,
2482 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002483 if (cr == NULL) {
2484 wl_client_post_no_memory(client);
2485 return;
2486 }
2487
Neil Roberts96d790e2013-09-19 17:32:00 +01002488 /* May be moved to focused list later by either
2489 * weston_keyboard_set_focus or directly if this client is already
2490 * focused */
Derek Foreman345c9f32015-06-03 15:53:28 -05002491 wl_list_insert(&keyboard->resource_list, wl_resource_get_link(cr));
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002492 wl_resource_set_implementation(cr, &keyboard_interface,
Alexandros Frantzis7a314d62018-01-26 18:47:56 +02002493 keyboard, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002494
Jonny Lamb66a41a02014-08-12 14:58:25 +02002495 if (wl_resource_get_version(cr) >= WL_KEYBOARD_REPEAT_INFO_SINCE_VERSION) {
2496 wl_keyboard_send_repeat_info(cr,
2497 seat->compositor->kb_repeat_rate,
2498 seat->compositor->kb_repeat_delay);
2499 }
Jasper St. Pierred8c6aeb2014-08-04 13:43:24 -04002500
Derek Foreman185d1582017-06-28 11:17:23 -05002501 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2502 keyboard->xkb_info->keymap_fd,
2503 keyboard->xkb_info->keymap_size);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002504
Neil Roberts96d790e2013-09-19 17:32:00 +01002505 if (should_send_modifiers_to_client(seat, client)) {
Derek Foreman345c9f32015-06-03 15:53:28 -05002506 send_modifiers_to_resource(keyboard,
Neil Roberts96d790e2013-09-19 17:32:00 +01002507 cr,
Derek Foreman345c9f32015-06-03 15:53:28 -05002508 keyboard->focus_serial);
Neil Roberts96d790e2013-09-19 17:32:00 +01002509 }
2510
Derek Foreman345c9f32015-06-03 15:53:28 -05002511 if (keyboard->focus && keyboard->focus->resource &&
2512 wl_resource_get_client(keyboard->focus->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01002513 struct weston_surface *surface =
Derek Foreman345c9f32015-06-03 15:53:28 -05002514 (struct weston_surface *)keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +01002515
2516 wl_list_remove(wl_resource_get_link(cr));
Derek Foreman345c9f32015-06-03 15:53:28 -05002517 wl_list_insert(&keyboard->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002518 wl_resource_get_link(cr));
2519 wl_keyboard_send_enter(cr,
Derek Foreman345c9f32015-06-03 15:53:28 -05002520 keyboard->focus_serial,
Neil Roberts96d790e2013-09-19 17:32:00 +01002521 surface->resource,
Derek Foreman345c9f32015-06-03 15:53:28 -05002522 &keyboard->keys);
Neil Roberts96d790e2013-09-19 17:32:00 +01002523
2524 /* If this is the first keyboard resource for this
2525 * client... */
Derek Foreman345c9f32015-06-03 15:53:28 -05002526 if (keyboard->focus_resource_list.prev ==
Neil Roberts96d790e2013-09-19 17:32:00 +01002527 wl_resource_get_link(cr))
2528 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002529 }
2530}
2531
2532static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002533touch_release(struct wl_client *client, struct wl_resource *resource)
2534{
2535 wl_resource_destroy(resource);
2536}
2537
2538static const struct wl_touch_interface touch_interface = {
2539 touch_release
2540};
2541
2542static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002543seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2544 uint32_t id)
2545{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002546 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002547 /* We use the touch_state directly, which means we'll
2548 * give a wl_touch if the seat has ever had one - even though
2549 * the spec explicitly states that this request only takes effect
2550 * if the seat has the touch capability.
2551 *
2552 * This prevents a race between the compositor sending new
2553 * capabilities and the client trying to use the old ones.
2554 */
2555 struct weston_touch *touch = seat->touch_state;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002556 struct wl_resource *cr;
2557
Derek Foreman1281a362015-07-31 16:55:32 -05002558 if (!touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002559 return;
2560
Jason Ekstranda85118c2013-06-27 20:17:02 -05002561 cr = wl_resource_create(client, &wl_touch_interface,
2562 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002563 if (cr == NULL) {
2564 wl_client_post_no_memory(client);
2565 return;
2566 }
2567
Derek Foreman1281a362015-07-31 16:55:32 -05002568 if (touch->focus &&
2569 wl_resource_get_client(touch->focus->surface->resource) == client) {
Chokshi, Mituld6697142015-10-09 08:28:47 +00002570 wl_list_insert(&touch->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002571 wl_resource_get_link(cr));
2572 } else {
Chokshi, Mituld6697142015-10-09 08:28:47 +00002573 wl_list_insert(&touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002574 wl_resource_get_link(cr));
2575 }
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002576 wl_resource_set_implementation(cr, &touch_interface,
2577 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002578}
2579
Quentin Glidicaab1d362016-03-13 17:49:08 +01002580static void
2581seat_release(struct wl_client *client, struct wl_resource *resource)
2582{
2583 wl_resource_destroy(resource);
2584}
2585
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002586static const struct wl_seat_interface seat_interface = {
2587 seat_get_pointer,
2588 seat_get_keyboard,
2589 seat_get_touch,
Quentin Glidicaab1d362016-03-13 17:49:08 +01002590 seat_release,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002591};
2592
2593static void
2594bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2595{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002596 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002597 struct wl_resource *resource;
2598 enum wl_seat_capability caps = 0;
2599
Jason Ekstranda85118c2013-06-27 20:17:02 -05002600 resource = wl_resource_create(client,
Derek Foreman1909c102015-11-26 14:17:47 -06002601 &wl_seat_interface, version, id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05002602 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05002603 wl_resource_set_implementation(resource, &seat_interface, data,
2604 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002605
Derek Foreman1281a362015-07-31 16:55:32 -05002606 if (weston_seat_get_pointer(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002607 caps |= WL_SEAT_CAPABILITY_POINTER;
Derek Foreman1281a362015-07-31 16:55:32 -05002608 if (weston_seat_get_keyboard(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002609 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Derek Foreman1281a362015-07-31 16:55:32 -05002610 if (weston_seat_get_touch(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002611 caps |= WL_SEAT_CAPABILITY_TOUCH;
2612
2613 wl_seat_send_capabilities(resource, caps);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04002614 if (version >= WL_SEAT_NAME_SINCE_VERSION)
Rob Bradforde445ae62013-05-31 18:09:51 +01002615 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002616}
2617
Jonas Ådahl30d61d82014-10-22 21:21:17 +02002618static void
2619relative_pointer_destroy(struct wl_client *client,
2620 struct wl_resource *resource)
2621{
2622 wl_resource_destroy(resource);
2623}
2624
2625static const struct zwp_relative_pointer_v1_interface relative_pointer_interface = {
2626 relative_pointer_destroy
2627};
2628
2629static void
2630relative_pointer_manager_destroy(struct wl_client *client,
2631 struct wl_resource *resource)
2632{
2633 wl_resource_destroy(resource);
2634}
2635
2636static void
2637relative_pointer_manager_get_relative_pointer(struct wl_client *client,
2638 struct wl_resource *resource,
2639 uint32_t id,
2640 struct wl_resource *pointer_resource)
2641{
2642 struct weston_pointer *pointer =
2643 wl_resource_get_user_data(pointer_resource);
2644 struct weston_pointer_client *pointer_client;
2645 struct wl_resource *cr;
2646
2647 cr = wl_resource_create(client, &zwp_relative_pointer_v1_interface,
2648 wl_resource_get_version(resource), id);
2649 if (cr == NULL) {
2650 wl_client_post_no_memory(client);
2651 return;
2652 }
2653
2654 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
2655 if (!pointer_client) {
2656 wl_client_post_no_memory(client);
2657 return;
2658 }
2659
2660 wl_list_insert(&pointer_client->relative_pointer_resources,
2661 wl_resource_get_link(cr));
2662 wl_resource_set_implementation(cr, &relative_pointer_interface,
2663 pointer,
2664 unbind_pointer_client_resource);
2665}
2666
2667static const struct zwp_relative_pointer_manager_v1_interface relative_pointer_manager = {
2668 relative_pointer_manager_destroy,
2669 relative_pointer_manager_get_relative_pointer,
2670};
2671
2672static void
2673bind_relative_pointer_manager(struct wl_client *client, void *data,
2674 uint32_t version, uint32_t id)
2675{
2676 struct weston_compositor *compositor = data;
2677 struct wl_resource *resource;
2678
2679 resource = wl_resource_create(client,
2680 &zwp_relative_pointer_manager_v1_interface,
2681 1, id);
2682
2683 wl_resource_set_implementation(resource, &relative_pointer_manager,
2684 compositor,
2685 NULL);
2686}
2687
Giulio Camuffo0358af42016-06-02 21:48:08 +03002688WL_EXPORT int
2689weston_compositor_set_xkb_rule_names(struct weston_compositor *ec,
2690 struct xkb_rule_names *names)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002691{
2692 if (ec->xkb_context == NULL) {
2693 ec->xkb_context = xkb_context_new(0);
2694 if (ec->xkb_context == NULL) {
2695 weston_log("failed to create XKB context\n");
2696 return -1;
2697 }
2698 }
2699
2700 if (names)
2701 ec->xkb_names = *names;
2702 if (!ec->xkb_names.rules)
2703 ec->xkb_names.rules = strdup("evdev");
2704 if (!ec->xkb_names.model)
2705 ec->xkb_names.model = strdup("pc105");
2706 if (!ec->xkb_names.layout)
2707 ec->xkb_names.layout = strdup("us");
2708
2709 return 0;
2710}
2711
Stefan Schmidtfda26522013-09-17 10:54:09 +01002712static void
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002713weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002714{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002715 if (--xkb_info->ref_count > 0)
2716 return;
2717
Ran Benitac9c74152014-08-19 23:59:52 +03002718 xkb_keymap_unref(xkb_info->keymap);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002719
2720 if (xkb_info->keymap_area)
2721 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2722 if (xkb_info->keymap_fd >= 0)
2723 close(xkb_info->keymap_fd);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002724 free(xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002725}
2726
2727void
2728weston_compositor_xkb_destroy(struct weston_compositor *ec)
2729{
2730 free((char *) ec->xkb_names.rules);
2731 free((char *) ec->xkb_names.model);
2732 free((char *) ec->xkb_names.layout);
2733 free((char *) ec->xkb_names.variant);
2734 free((char *) ec->xkb_names.options);
Stefan Schmidtfda26522013-09-17 10:54:09 +01002735
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002736 if (ec->xkb_info)
2737 weston_xkb_info_destroy(ec->xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002738 xkb_context_unref(ec->xkb_context);
2739}
2740
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002741static struct weston_xkb_info *
2742weston_xkb_info_create(struct xkb_keymap *keymap)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002743{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002744 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
2745 if (xkb_info == NULL)
2746 return NULL;
2747
Ran Benita2e1968f2014-08-19 23:59:51 +03002748 xkb_info->keymap = xkb_keymap_ref(keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002749 xkb_info->ref_count = 1;
2750
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002751 char *keymap_str;
2752
Ran Benita2e1968f2014-08-19 23:59:51 +03002753 xkb_info->shift_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2754 XKB_MOD_NAME_SHIFT);
2755 xkb_info->caps_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2756 XKB_MOD_NAME_CAPS);
2757 xkb_info->ctrl_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2758 XKB_MOD_NAME_CTRL);
2759 xkb_info->alt_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2760 XKB_MOD_NAME_ALT);
2761 xkb_info->mod2_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2762 "Mod2");
2763 xkb_info->mod3_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2764 "Mod3");
2765 xkb_info->super_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2766 XKB_MOD_NAME_LOGO);
2767 xkb_info->mod5_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2768 "Mod5");
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002769
Ran Benita2e1968f2014-08-19 23:59:51 +03002770 xkb_info->num_led = xkb_keymap_led_get_index(xkb_info->keymap,
2771 XKB_LED_NAME_NUM);
2772 xkb_info->caps_led = xkb_keymap_led_get_index(xkb_info->keymap,
2773 XKB_LED_NAME_CAPS);
2774 xkb_info->scroll_led = xkb_keymap_led_get_index(xkb_info->keymap,
2775 XKB_LED_NAME_SCROLL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002776
Ran Benita2e1968f2014-08-19 23:59:51 +03002777 keymap_str = xkb_keymap_get_as_string(xkb_info->keymap,
2778 XKB_KEYMAP_FORMAT_TEXT_V1);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002779 if (keymap_str == NULL) {
2780 weston_log("failed to get string version of keymap\n");
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002781 goto err_keymap;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002782 }
2783 xkb_info->keymap_size = strlen(keymap_str) + 1;
2784
2785 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
2786 if (xkb_info->keymap_fd < 0) {
2787 weston_log("creating a keymap file for %lu bytes failed: %m\n",
2788 (unsigned long) xkb_info->keymap_size);
2789 goto err_keymap_str;
2790 }
2791
2792 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2793 PROT_READ | PROT_WRITE,
2794 MAP_SHARED, xkb_info->keymap_fd, 0);
2795 if (xkb_info->keymap_area == MAP_FAILED) {
2796 weston_log("failed to mmap() %lu bytes\n",
2797 (unsigned long) xkb_info->keymap_size);
2798 goto err_dev_zero;
2799 }
2800 strcpy(xkb_info->keymap_area, keymap_str);
2801 free(keymap_str);
2802
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002803 return xkb_info;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002804
2805err_dev_zero:
2806 close(xkb_info->keymap_fd);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002807err_keymap_str:
2808 free(keymap_str);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002809err_keymap:
Ran Benita2e1968f2014-08-19 23:59:51 +03002810 xkb_keymap_unref(xkb_info->keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002811 free(xkb_info);
2812 return NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002813}
2814
2815static int
2816weston_compositor_build_global_keymap(struct weston_compositor *ec)
2817{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002818 struct xkb_keymap *keymap;
2819
2820 if (ec->xkb_info != NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002821 return 0;
2822
Ran Benita2e1968f2014-08-19 23:59:51 +03002823 keymap = xkb_keymap_new_from_names(ec->xkb_context,
2824 &ec->xkb_names,
2825 0);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002826 if (keymap == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002827 weston_log("failed to compile global XKB keymap\n");
2828 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
2829 "options %s\n",
2830 ec->xkb_names.rules, ec->xkb_names.model,
2831 ec->xkb_names.layout, ec->xkb_names.variant,
2832 ec->xkb_names.options);
2833 return -1;
2834 }
2835
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002836 ec->xkb_info = weston_xkb_info_create(keymap);
Rui Matos73d93952013-10-24 19:28:41 +02002837 xkb_keymap_unref(keymap);
Stefan Schmidtfda26522013-09-17 10:54:09 +01002838 if (ec->xkb_info == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002839 return -1;
2840
2841 return 0;
2842}
2843
Rui Matos65196bc2013-10-10 19:44:19 +02002844WL_EXPORT void
2845weston_seat_update_keymap(struct weston_seat *seat, struct xkb_keymap *keymap)
2846{
Derek Foreman1281a362015-07-31 16:55:32 -05002847 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2848
2849 if (!keyboard || !keymap)
Rui Matos65196bc2013-10-10 19:44:19 +02002850 return;
2851
Derek Foreman1281a362015-07-31 16:55:32 -05002852 xkb_keymap_unref(keyboard->pending_keymap);
2853 keyboard->pending_keymap = xkb_keymap_ref(keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02002854
Derek Foreman1281a362015-07-31 16:55:32 -05002855 if (keyboard->keys.size == 0)
Rui Matos65196bc2013-10-10 19:44:19 +02002856 update_keymap(seat);
Rui Matos65196bc2013-10-10 19:44:19 +02002857}
2858
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002859WL_EXPORT int
2860weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
2861{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002862 struct weston_keyboard *keyboard;
2863
Derek Foreman1281a362015-07-31 16:55:32 -05002864 if (seat->keyboard_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002865 seat->keyboard_device_count += 1;
2866 if (seat->keyboard_device_count == 1)
2867 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002868 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002869 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002870
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002871 keyboard = weston_keyboard_create();
2872 if (keyboard == NULL) {
2873 weston_log("failed to allocate weston keyboard struct\n");
2874 return -1;
2875 }
2876
Derek Foreman185d1582017-06-28 11:17:23 -05002877 if (keymap != NULL) {
2878 keyboard->xkb_info = weston_xkb_info_create(keymap);
2879 if (keyboard->xkb_info == NULL)
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02002880 goto err;
Derek Foreman185d1582017-06-28 11:17:23 -05002881 } else {
2882 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
2883 goto err;
2884 keyboard->xkb_info = seat->compositor->xkb_info;
2885 keyboard->xkb_info->ref_count++;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002886 }
Derek Foreman185d1582017-06-28 11:17:23 -05002887
2888 keyboard->xkb_state.state = xkb_state_new(keyboard->xkb_info->keymap);
2889 if (keyboard->xkb_state.state == NULL) {
2890 weston_log("failed to initialise XKB state\n");
2891 goto err;
2892 }
2893
2894 keyboard->xkb_state.leds = 0;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002895
Derek Foreman1281a362015-07-31 16:55:32 -05002896 seat->keyboard_state = keyboard;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02002897 seat->keyboard_device_count = 1;
2898 keyboard->seat = seat;
2899
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002900 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002901
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002902 return 0;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02002903
2904err:
2905 if (keyboard->xkb_info)
2906 weston_xkb_info_destroy(keyboard->xkb_info);
2907 free(keyboard);
2908
2909 return -1;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002910}
2911
Jonas Ådahl91fed542013-12-03 09:14:27 +01002912static void
2913weston_keyboard_reset_state(struct weston_keyboard *keyboard)
2914{
2915 struct weston_seat *seat = keyboard->seat;
2916 struct xkb_state *state;
2917
Derek Foreman185d1582017-06-28 11:17:23 -05002918 state = xkb_state_new(keyboard->xkb_info->keymap);
2919 if (!state) {
2920 weston_log("failed to reset XKB state\n");
2921 return;
Jonas Ådahl91fed542013-12-03 09:14:27 +01002922 }
Derek Foreman185d1582017-06-28 11:17:23 -05002923 xkb_state_unref(keyboard->xkb_state.state);
2924 keyboard->xkb_state.state = state;
2925
2926 keyboard->xkb_state.leds = 0;
Jonas Ådahl91fed542013-12-03 09:14:27 +01002927
2928 seat->modifier_state = 0;
2929}
2930
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002931WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002932weston_seat_release_keyboard(struct weston_seat *seat)
2933{
2934 seat->keyboard_device_count--;
Derek Foremand621df22014-11-19 11:04:12 -06002935 assert(seat->keyboard_device_count >= 0);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002936 if (seat->keyboard_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05002937 weston_keyboard_set_focus(seat->keyboard_state, NULL);
2938 weston_keyboard_cancel_grab(seat->keyboard_state);
2939 weston_keyboard_reset_state(seat->keyboard_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002940 seat_send_updated_caps(seat);
2941 }
2942}
2943
2944WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002945weston_seat_init_pointer(struct weston_seat *seat)
2946{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002947 struct weston_pointer *pointer;
2948
Derek Foreman1281a362015-07-31 16:55:32 -05002949 if (seat->pointer_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002950 seat->pointer_device_count += 1;
2951 if (seat->pointer_device_count == 1)
2952 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002953 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002954 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002955
Giulio Camuffocdb4d292013-11-14 23:42:53 +01002956 pointer = weston_pointer_create(seat);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002957 if (pointer == NULL)
2958 return;
2959
Derek Foreman1281a362015-07-31 16:55:32 -05002960 seat->pointer_state = pointer;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002961 seat->pointer_device_count = 1;
2962 pointer->seat = seat;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002963
2964 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002965}
2966
2967WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002968weston_seat_release_pointer(struct weston_seat *seat)
2969{
Derek Foreman1281a362015-07-31 16:55:32 -05002970 struct weston_pointer *pointer = seat->pointer_state;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002971
2972 seat->pointer_device_count--;
2973 if (seat->pointer_device_count == 0) {
Derek Foremanf9318d12015-05-11 15:40:11 -05002974 weston_pointer_clear_focus(pointer);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002975 weston_pointer_cancel_grab(pointer);
Jonas Ådahl630bae82013-10-17 23:04:06 +02002976
Jonas Ådahla4932742013-10-17 23:04:07 +02002977 if (pointer->sprite)
2978 pointer_unmap_sprite(pointer);
2979
Jonas Ådahl3e12e632013-12-02 22:05:05 +01002980 weston_pointer_reset_state(pointer);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002981 seat_send_updated_caps(seat);
Derek Foremanfd5ca512015-01-07 15:00:25 -06002982
2983 /* seat->pointer is intentionally not destroyed so that
2984 * a newly attached pointer on this seat will retain
2985 * the previous cursor co-ordinates.
2986 */
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002987 }
2988}
2989
2990WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002991weston_seat_init_touch(struct weston_seat *seat)
2992{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002993 struct weston_touch *touch;
2994
Derek Foreman1281a362015-07-31 16:55:32 -05002995 if (seat->touch_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002996 seat->touch_device_count += 1;
2997 if (seat->touch_device_count == 1)
2998 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002999 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003000 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003001
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003002 touch = weston_touch_create();
3003 if (touch == NULL)
3004 return;
3005
Derek Foreman1281a362015-07-31 16:55:32 -05003006 seat->touch_state = touch;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003007 seat->touch_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003008 touch->seat = seat;
3009
3010 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003011}
3012
3013WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003014weston_seat_release_touch(struct weston_seat *seat)
3015{
3016 seat->touch_device_count--;
3017 if (seat->touch_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05003018 weston_touch_set_focus(seat->touch_state, NULL);
3019 weston_touch_cancel_grab(seat->touch_state);
3020 weston_touch_reset_state(seat->touch_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003021 seat_send_updated_caps(seat);
3022 }
3023}
3024
3025WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003026weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
3027 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003028{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003029 memset(seat, 0, sizeof *seat);
3030
Kristian Høgsberge3148752013-05-06 23:19:49 -04003031 seat->selection_data_source = NULL;
3032 wl_list_init(&seat->base_resource_list);
3033 wl_signal_init(&seat->selection_signal);
3034 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04003035 wl_signal_init(&seat->destroy_signal);
Jason Ekstranda4ab5422014-04-02 19:53:45 -05003036 wl_signal_init(&seat->updated_caps_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003037
Peter Hutterer87743e92016-01-18 16:38:22 +10003038 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 5,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003039 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003040
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003041 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003042 seat->modifier_state = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003043 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003044
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003045 wl_list_insert(ec->seat_list.prev, &seat->link);
3046
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003047 clipboard_create(seat);
3048
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003049 wl_signal_emit(&ec->seat_created_signal, seat);
3050}
3051
3052WL_EXPORT void
3053weston_seat_release(struct weston_seat *seat)
3054{
3055 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003056
Jonas Ådahl1afb2382014-01-03 19:46:51 +01003057 if (seat->saved_kbd_focus)
3058 wl_list_remove(&seat->saved_kbd_focus_listener.link);
3059
Derek Foreman1281a362015-07-31 16:55:32 -05003060 if (seat->pointer_state)
3061 weston_pointer_destroy(seat->pointer_state);
3062 if (seat->keyboard_state)
3063 weston_keyboard_destroy(seat->keyboard_state);
3064 if (seat->touch_state)
3065 weston_touch_destroy(seat->touch_state);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003066
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003067 free (seat->seat_name);
3068
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003069 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04003070
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003071 wl_signal_emit(&seat->destroy_signal, seat);
3072}
Derek Foreman1281a362015-07-31 16:55:32 -05003073
3074/** Get a seat's keyboard pointer
3075 *
3076 * \param seat The seat to query
3077 * \return The seat's keyboard pointer, or NULL if no keyboard is present
3078 *
3079 * The keyboard pointer for a seat isn't freed when all keyboards are removed,
3080 * so it should only be used when the seat's keyboard_device_count is greater
3081 * than zero. This function does that test and only returns a pointer
3082 * when a keyboard is present.
3083 */
3084WL_EXPORT struct weston_keyboard *
3085weston_seat_get_keyboard(struct weston_seat *seat)
3086{
3087 if (!seat)
3088 return NULL;
3089
3090 if (seat->keyboard_device_count)
3091 return seat->keyboard_state;
3092
3093 return NULL;
3094}
3095
3096/** Get a seat's pointer pointer
3097 *
3098 * \param seat The seat to query
3099 * \return The seat's pointer pointer, or NULL if no pointer device is present
3100 *
3101 * The pointer pointer for a seat isn't freed when all mice are removed,
3102 * so it should only be used when the seat's pointer_device_count is greater
3103 * than zero. This function does that test and only returns a pointer
3104 * when a pointing device is present.
3105 */
3106WL_EXPORT struct weston_pointer *
3107weston_seat_get_pointer(struct weston_seat *seat)
3108{
3109 if (!seat)
3110 return NULL;
3111
3112 if (seat->pointer_device_count)
3113 return seat->pointer_state;
3114
3115 return NULL;
3116}
3117
Jonas Ådahld3414f22016-07-22 17:56:31 +08003118static const struct zwp_locked_pointer_v1_interface locked_pointer_interface;
3119static const struct zwp_confined_pointer_v1_interface confined_pointer_interface;
3120
3121static enum pointer_constraint_type
3122pointer_constraint_get_type(struct weston_pointer_constraint *constraint)
3123{
3124 if (wl_resource_instance_of(constraint->resource,
3125 &zwp_locked_pointer_v1_interface,
3126 &locked_pointer_interface)) {
3127 return POINTER_CONSTRAINT_TYPE_LOCK;
3128 } else if (wl_resource_instance_of(constraint->resource,
3129 &zwp_confined_pointer_v1_interface,
3130 &confined_pointer_interface)) {
3131 return POINTER_CONSTRAINT_TYPE_CONFINE;
3132 }
3133
3134 abort();
3135 return 0;
3136}
3137
3138static void
3139pointer_constraint_notify_activated(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_locked(resource);
3146 break;
3147 case POINTER_CONSTRAINT_TYPE_CONFINE:
3148 zwp_confined_pointer_v1_send_confined(resource);
3149 break;
3150 }
3151}
3152
3153static void
3154pointer_constraint_notify_deactivated(struct weston_pointer_constraint *constraint)
3155{
3156 struct wl_resource *resource = constraint->resource;
3157
3158 switch (pointer_constraint_get_type(constraint)) {
3159 case POINTER_CONSTRAINT_TYPE_LOCK:
3160 zwp_locked_pointer_v1_send_unlocked(resource);
3161 break;
3162 case POINTER_CONSTRAINT_TYPE_CONFINE:
3163 zwp_confined_pointer_v1_send_unconfined(resource);
3164 break;
3165 }
3166}
3167
3168static struct weston_pointer_constraint *
3169get_pointer_constraint_for_pointer(struct weston_surface *surface,
3170 struct weston_pointer *pointer)
3171{
3172 struct weston_pointer_constraint *constraint;
3173
3174 wl_list_for_each(constraint, &surface->pointer_constraints, link) {
3175 if (constraint->pointer == pointer)
3176 return constraint;
3177 }
3178
3179 return NULL;
3180}
3181
Derek Foreman1281a362015-07-31 16:55:32 -05003182/** Get a seat's touch pointer
3183 *
3184 * \param seat The seat to query
3185 * \return The seat's touch pointer, or NULL if no touch device is present
3186 *
3187 * The touch pointer for a seat isn't freed when all touch devices are removed,
3188 * so it should only be used when the seat's touch_device_count is greater
3189 * than zero. This function does that test and only returns a pointer
3190 * when a touch device is present.
3191 */
3192WL_EXPORT struct weston_touch *
3193weston_seat_get_touch(struct weston_seat *seat)
3194{
3195 if (!seat)
3196 return NULL;
3197
3198 if (seat->touch_device_count)
3199 return seat->touch_state;
3200
3201 return NULL;
3202}
Bryce Harrington24f917e2016-06-29 19:04:07 -07003203
3204/** Sets the keyboard focus to the given surface
3205 *
3206 * \param seat The seat to query
3207 */
3208WL_EXPORT void
3209weston_seat_set_keyboard_focus(struct weston_seat *seat,
3210 struct weston_surface *surface)
3211{
3212 struct weston_compositor *compositor = seat->compositor;
3213 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003214 struct weston_surface_activation_data activation_data;
Bryce Harrington24f917e2016-06-29 19:04:07 -07003215
Jonas Ådahlef8e1c32016-03-15 20:28:51 +08003216 if (keyboard && keyboard->focus != surface) {
Bryce Harrington24f917e2016-06-29 19:04:07 -07003217 weston_keyboard_set_focus(keyboard, surface);
3218 wl_data_device_set_keyboard_focus(seat);
3219 }
3220
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003221 inc_activate_serial(compositor);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003222
3223 activation_data = (struct weston_surface_activation_data) {
3224 .surface = surface,
3225 .seat = seat,
3226 };
3227 wl_signal_emit(&compositor->activate_signal, &activation_data);
Bryce Harrington24f917e2016-06-29 19:04:07 -07003228}
Jonas Ådahl30d61d82014-10-22 21:21:17 +02003229
Jonas Ådahld3414f22016-07-22 17:56:31 +08003230static void
3231enable_pointer_constraint(struct weston_pointer_constraint *constraint,
3232 struct weston_view *view)
3233{
3234 assert(constraint->view == NULL);
3235 constraint->view = view;
3236 pointer_constraint_notify_activated(constraint);
3237 weston_pointer_start_grab(constraint->pointer, &constraint->grab);
3238 wl_list_remove(&constraint->surface_destroy_listener.link);
3239 wl_list_init(&constraint->surface_destroy_listener.link);
3240}
3241
3242static bool
3243is_pointer_constraint_enabled(struct weston_pointer_constraint *constraint)
3244{
3245 return constraint->view != NULL;
3246}
3247
3248static void
3249weston_pointer_constraint_disable(struct weston_pointer_constraint *constraint)
3250{
3251 constraint->view = NULL;
3252 pointer_constraint_notify_deactivated(constraint);
3253 weston_pointer_end_grab(constraint->grab.pointer);
3254}
3255
3256void
3257weston_pointer_constraint_destroy(struct weston_pointer_constraint *constraint)
3258{
3259 if (is_pointer_constraint_enabled(constraint))
3260 weston_pointer_constraint_disable(constraint);
3261
3262 wl_list_remove(&constraint->pointer_destroy_listener.link);
3263 wl_list_remove(&constraint->surface_destroy_listener.link);
3264 wl_list_remove(&constraint->surface_commit_listener.link);
3265 wl_list_remove(&constraint->surface_activate_listener.link);
3266
3267 wl_resource_set_user_data(constraint->resource, NULL);
3268 pixman_region32_fini(&constraint->region);
3269 wl_list_remove(&constraint->link);
3270 free(constraint);
3271}
3272
3273static void
3274disable_pointer_constraint(struct weston_pointer_constraint *constraint)
3275{
3276 switch (constraint->lifetime) {
3277 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT:
3278 weston_pointer_constraint_destroy(constraint);
3279 break;
3280 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT:
3281 weston_pointer_constraint_disable(constraint);
3282 break;
3283 }
3284}
3285
3286static bool
3287is_within_constraint_region(struct weston_pointer_constraint *constraint,
3288 wl_fixed_t sx, wl_fixed_t sy)
3289{
3290 struct weston_surface *surface = constraint->surface;
3291 pixman_region32_t constraint_region;
3292 bool result;
3293
3294 pixman_region32_init(&constraint_region);
3295 pixman_region32_intersect(&constraint_region,
3296 &surface->input,
3297 &constraint->region);
3298 result = pixman_region32_contains_point(&constraint_region,
3299 wl_fixed_to_int(sx),
3300 wl_fixed_to_int(sy),
3301 NULL);
3302 pixman_region32_fini(&constraint_region);
3303
3304 return result;
3305}
3306
3307static void
3308maybe_enable_pointer_constraint(struct weston_pointer_constraint *constraint)
3309{
3310 struct weston_surface *surface = constraint->surface;
3311 struct weston_view *vit;
3312 struct weston_view *view = NULL;
3313 struct weston_pointer *pointer = constraint->pointer;
3314 struct weston_keyboard *keyboard;
3315 struct weston_seat *seat = pointer->seat;
3316 int32_t x, y;
3317
3318 /* Postpone if no view of the surface was most recently clicked. */
3319 wl_list_for_each(vit, &surface->views, surface_link) {
3320 if (vit->click_to_activate_serial ==
3321 surface->compositor->activate_serial) {
3322 view = vit;
3323 }
3324 }
3325 if (view == NULL)
3326 return;
3327
3328 /* Postpone if surface doesn't have keyboard focus. */
3329 keyboard = weston_seat_get_keyboard(seat);
3330 if (!keyboard || keyboard->focus != surface)
3331 return;
3332
3333 /* Postpone constraint if the pointer is not within the
3334 * constraint region.
3335 */
3336 weston_view_from_global(view,
3337 wl_fixed_to_int(pointer->x),
3338 wl_fixed_to_int(pointer->y),
3339 &x, &y);
3340 if (!is_within_constraint_region(constraint,
3341 wl_fixed_from_int(x),
3342 wl_fixed_from_int(y)))
3343 return;
3344
3345 enable_pointer_constraint(constraint, view);
3346}
3347
3348static void
3349locked_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
3350{
3351}
3352
3353static void
3354locked_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02003355 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003356 struct weston_pointer_motion_event *event)
3357{
Quentin Glidiccde13452016-08-12 10:41:32 +02003358 pointer_send_relative_motion(grab->pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08003359}
3360
3361static void
3362locked_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003363 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003364 uint32_t button,
3365 uint32_t state_w)
3366{
3367 weston_pointer_send_button(grab->pointer, time, button, state_w);
3368}
3369
3370static void
3371locked_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02003372 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003373 struct weston_pointer_axis_event *event)
3374{
3375 weston_pointer_send_axis(grab->pointer, time, event);
3376}
3377
3378static void
3379locked_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
3380 uint32_t source)
3381{
3382 weston_pointer_send_axis_source(grab->pointer, source);
3383}
3384
3385static void
3386locked_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
3387{
3388 weston_pointer_send_frame(grab->pointer);
3389}
3390
3391static void
3392locked_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
3393{
3394 struct weston_pointer_constraint *constraint =
3395 container_of(grab, struct weston_pointer_constraint, grab);
3396
3397 disable_pointer_constraint(constraint);
3398}
3399
3400static const struct weston_pointer_grab_interface
3401 locked_pointer_grab_interface = {
3402 locked_pointer_grab_pointer_focus,
3403 locked_pointer_grab_pointer_motion,
3404 locked_pointer_grab_pointer_button,
3405 locked_pointer_grab_pointer_axis,
3406 locked_pointer_grab_pointer_axis_source,
3407 locked_pointer_grab_pointer_frame,
3408 locked_pointer_grab_pointer_cancel,
3409};
3410
3411static void
3412pointer_constraint_constrain_resource_destroyed(struct wl_resource *resource)
3413{
3414 struct weston_pointer_constraint *constraint =
3415 wl_resource_get_user_data(resource);
3416
3417 if (!constraint)
3418 return;
3419
3420 weston_pointer_constraint_destroy(constraint);
3421}
3422
3423static void
3424pointer_constraint_surface_activate(struct wl_listener *listener, void *data)
3425{
3426 struct weston_surface_activation_data *activation = data;
3427 struct weston_pointer *pointer;
3428 struct weston_surface *focus = activation->surface;
3429 struct weston_pointer_constraint *constraint =
3430 container_of(listener, struct weston_pointer_constraint,
3431 surface_activate_listener);
3432 bool is_constraint_surface;
3433
3434 pointer = weston_seat_get_pointer(activation->seat);
3435 if (!pointer)
3436 return;
3437
3438 is_constraint_surface =
3439 get_pointer_constraint_for_pointer(focus, pointer) == constraint;
3440
3441 if (is_constraint_surface &&
3442 !is_pointer_constraint_enabled(constraint))
3443 maybe_enable_pointer_constraint(constraint);
3444 else if (!is_constraint_surface &&
3445 is_pointer_constraint_enabled(constraint))
3446 disable_pointer_constraint(constraint);
3447}
3448
3449static void
3450pointer_constraint_pointer_destroyed(struct wl_listener *listener, void *data)
3451{
3452 struct weston_pointer_constraint *constraint =
3453 container_of(listener, struct weston_pointer_constraint,
3454 pointer_destroy_listener);
3455
3456 weston_pointer_constraint_destroy(constraint);
3457}
3458
3459static void
3460pointer_constraint_surface_destroyed(struct wl_listener *listener, void *data)
3461{
3462 struct weston_pointer_constraint *constraint =
3463 container_of(listener, struct weston_pointer_constraint,
3464 surface_destroy_listener);
3465
3466 weston_pointer_constraint_destroy(constraint);
3467}
3468
3469static void
3470pointer_constraint_surface_committed(struct wl_listener *listener, void *data)
3471{
3472 struct weston_pointer_constraint *constraint =
3473 container_of(listener, struct weston_pointer_constraint,
3474 surface_commit_listener);
3475
3476 if (constraint->region_is_pending) {
3477 constraint->region_is_pending = false;
3478 pixman_region32_copy(&constraint->region,
3479 &constraint->region_pending);
3480 pixman_region32_fini(&constraint->region_pending);
3481 pixman_region32_init(&constraint->region_pending);
3482 }
3483
3484 if (constraint->hint_is_pending) {
3485 constraint->hint_is_pending = false;
3486
3487 constraint->hint_is_pending = true;
3488 constraint->hint_x = constraint->hint_x_pending;
3489 constraint->hint_y = constraint->hint_y_pending;
3490 }
3491
3492 if (pointer_constraint_get_type(constraint) ==
3493 POINTER_CONSTRAINT_TYPE_CONFINE &&
3494 is_pointer_constraint_enabled(constraint))
3495 maybe_warp_confined_pointer(constraint);
3496}
3497
3498static struct weston_pointer_constraint *
3499weston_pointer_constraint_create(struct weston_surface *surface,
3500 struct weston_pointer *pointer,
3501 struct weston_region *region,
3502 enum zwp_pointer_constraints_v1_lifetime lifetime,
3503 struct wl_resource *cr,
3504 const struct weston_pointer_grab_interface *grab_interface)
3505{
3506 struct weston_pointer_constraint *constraint;
3507
3508 constraint = zalloc(sizeof *constraint);
3509 if (!constraint)
3510 return NULL;
3511
3512 constraint->lifetime = lifetime;
3513 pixman_region32_init(&constraint->region);
3514 pixman_region32_init(&constraint->region_pending);
3515 wl_list_insert(&surface->pointer_constraints, &constraint->link);
3516 constraint->surface = surface;
3517 constraint->pointer = pointer;
3518 constraint->resource = cr;
3519 constraint->grab.interface = grab_interface;
3520 if (region) {
3521 pixman_region32_copy(&constraint->region,
3522 &region->region);
3523 } else {
3524 pixman_region32_fini(&constraint->region);
3525 region_init_infinite(&constraint->region);
3526 }
3527
3528 constraint->surface_activate_listener.notify =
3529 pointer_constraint_surface_activate;
3530 constraint->surface_destroy_listener.notify =
3531 pointer_constraint_surface_destroyed;
3532 constraint->surface_commit_listener.notify =
3533 pointer_constraint_surface_committed;
3534 constraint->pointer_destroy_listener.notify =
3535 pointer_constraint_pointer_destroyed;
3536
3537 wl_signal_add(&surface->compositor->activate_signal,
3538 &constraint->surface_activate_listener);
3539 wl_signal_add(&pointer->destroy_signal,
3540 &constraint->pointer_destroy_listener);
3541 wl_signal_add(&surface->destroy_signal,
3542 &constraint->surface_destroy_listener);
3543 wl_signal_add(&surface->commit_signal,
3544 &constraint->surface_commit_listener);
3545
3546 return constraint;
3547}
3548
3549static void
3550init_pointer_constraint(struct wl_resource *pointer_constraints_resource,
3551 uint32_t id,
3552 struct weston_surface *surface,
3553 struct weston_pointer *pointer,
3554 struct weston_region *region,
3555 enum zwp_pointer_constraints_v1_lifetime lifetime,
3556 const struct wl_interface *interface,
3557 const void *implementation,
3558 const struct weston_pointer_grab_interface *grab_interface)
3559{
3560 struct wl_client *client =
3561 wl_resource_get_client(pointer_constraints_resource);
3562 struct wl_resource *cr;
3563 struct weston_pointer_constraint *constraint;
3564
3565 if (get_pointer_constraint_for_pointer(surface, pointer)) {
3566 wl_resource_post_error(pointer_constraints_resource,
3567 ZWP_POINTER_CONSTRAINTS_V1_ERROR_ALREADY_CONSTRAINED,
3568 "the pointer has a lock/confine request on this surface");
3569 return;
3570 }
3571
3572 cr = wl_resource_create(client, interface,
3573 wl_resource_get_version(pointer_constraints_resource),
3574 id);
3575 if (cr == NULL) {
3576 wl_client_post_no_memory(client);
3577 return;
3578 }
3579
3580 constraint = weston_pointer_constraint_create(surface, pointer,
3581 region, lifetime,
3582 cr, grab_interface);
3583 if (constraint == NULL) {
3584 wl_client_post_no_memory(client);
3585 return;
3586 }
3587
3588 wl_resource_set_implementation(cr, implementation, constraint,
3589 pointer_constraint_constrain_resource_destroyed);
3590
3591 maybe_enable_pointer_constraint(constraint);
3592}
3593
3594static void
3595pointer_constraints_destroy(struct wl_client *client,
3596 struct wl_resource *resource)
3597{
3598 wl_resource_destroy(resource);
3599}
3600
3601static void
3602locked_pointer_destroy(struct wl_client *client,
3603 struct wl_resource *resource)
3604{
3605 struct weston_pointer_constraint *constraint =
3606 wl_resource_get_user_data(resource);
3607 wl_fixed_t x, y;
3608
3609 if (constraint && constraint->view && constraint->hint_is_pending &&
3610 is_within_constraint_region(constraint,
3611 constraint->hint_x,
3612 constraint->hint_y)) {
3613 weston_view_to_global_fixed(constraint->view,
3614 constraint->hint_x,
3615 constraint->hint_y,
3616 &x, &y);
3617 weston_pointer_move_to(constraint->pointer, x, y);
3618 }
3619 wl_resource_destroy(resource);
3620}
3621
3622static void
3623locked_pointer_set_cursor_position_hint(struct wl_client *client,
3624 struct wl_resource *resource,
3625 wl_fixed_t surface_x,
3626 wl_fixed_t surface_y)
3627{
3628 struct weston_pointer_constraint *constraint =
3629 wl_resource_get_user_data(resource);
3630
3631 /* Ignore a set cursor hint that was sent after the lock was cancelled.
3632 */
3633 if (!constraint ||
3634 !constraint->resource ||
3635 constraint->resource != resource)
3636 return;
3637
3638 constraint->hint_is_pending = true;
3639 constraint->hint_x_pending = surface_x;
3640 constraint->hint_y_pending = surface_y;
3641}
3642
3643static void
3644locked_pointer_set_region(struct wl_client *client,
3645 struct wl_resource *resource,
3646 struct wl_resource *region_resource)
3647{
3648 struct weston_pointer_constraint *constraint =
3649 wl_resource_get_user_data(resource);
3650 struct weston_region *region = region_resource ?
3651 wl_resource_get_user_data(region_resource) : NULL;
3652
3653 if (!constraint)
3654 return;
3655
3656 if (region) {
3657 pixman_region32_copy(&constraint->region_pending,
3658 &region->region);
3659 } else {
3660 pixman_region32_fini(&constraint->region_pending);
3661 region_init_infinite(&constraint->region_pending);
3662 }
3663 constraint->region_is_pending = true;
3664}
3665
3666
3667static const struct zwp_locked_pointer_v1_interface locked_pointer_interface = {
3668 locked_pointer_destroy,
3669 locked_pointer_set_cursor_position_hint,
3670 locked_pointer_set_region,
3671};
3672
3673static void
3674pointer_constraints_lock_pointer(struct wl_client *client,
3675 struct wl_resource *resource,
3676 uint32_t id,
3677 struct wl_resource *surface_resource,
3678 struct wl_resource *pointer_resource,
3679 struct wl_resource *region_resource,
3680 uint32_t lifetime)
3681{
3682 struct weston_surface *surface =
3683 wl_resource_get_user_data(surface_resource);
3684 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
3685 struct weston_region *region = region_resource ?
3686 wl_resource_get_user_data(region_resource) : NULL;
3687
3688 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
3689 &zwp_locked_pointer_v1_interface,
3690 &locked_pointer_interface,
3691 &locked_pointer_grab_interface);
3692}
3693
3694static void
3695confined_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
3696{
3697}
3698
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08003699static double
3700vec2d_cross_product(struct vec2d a, struct vec2d b)
3701{
3702 return a.x * b.y - a.y * b.x;
3703}
3704
3705static struct vec2d
3706vec2d_add(struct vec2d a, struct vec2d b)
3707{
3708 return (struct vec2d) {
3709 .x = a.x + b.x,
3710 .y = a.y + b.y,
3711 };
3712}
3713
3714static struct vec2d
3715vec2d_subtract(struct vec2d a, struct vec2d b)
3716{
3717 return (struct vec2d) {
3718 .x = a.x - b.x,
3719 .y = a.y - b.y,
3720 };
3721}
3722
3723static struct vec2d
3724vec2d_multiply_constant(double c, struct vec2d a)
3725{
3726 return (struct vec2d) {
3727 .x = c * a.x,
3728 .y = c * a.y,
3729 };
3730}
3731
3732static bool
3733lines_intersect(struct line *line1, struct line *line2,
3734 struct vec2d *intersection)
3735{
3736 struct vec2d p = line1->a;
3737 struct vec2d r = vec2d_subtract(line1->b, line1->a);
3738 struct vec2d q = line2->a;
3739 struct vec2d s = vec2d_subtract(line2->b, line2->a);
3740 double rxs;
3741 double sxr;
3742 double t;
3743 double u;
3744
3745 /*
3746 * The line (p, r) and (q, s) intersects where
3747 *
3748 * p + t r = q + u s
3749 *
3750 * Calculate t:
3751 *
3752 * (p + t r) × s = (q + u s) × s
3753 * p × s + t (r × s) = q × s + u (s × s)
3754 * p × s + t (r × s) = q × s
3755 * t (r × s) = q × s - p × s
3756 * t (r × s) = (q - p) × s
3757 * t = ((q - p) × s) / (r × s)
3758 *
3759 * Using the same method, for u we get:
3760 *
3761 * u = ((p - q) × r) / (s × r)
3762 */
3763
3764 rxs = vec2d_cross_product(r, s);
3765 sxr = vec2d_cross_product(s, r);
3766
3767 /* If r × s = 0 then the lines are either parallel or collinear. */
3768 if (fabs(rxs) < DBL_MIN)
3769 return false;
3770
3771 t = vec2d_cross_product(vec2d_subtract(q, p), s) / rxs;
3772 u = vec2d_cross_product(vec2d_subtract(p, q), r) / sxr;
3773
3774 /* The lines only intersect if 0 ≤ t ≤ 1 and 0 ≤ u ≤ 1. */
3775 if (t < 0.0 || t > 1.0 || u < 0.0 || u > 1.0)
3776 return false;
3777
3778 *intersection = vec2d_add(p, vec2d_multiply_constant(t, r));
3779 return true;
3780}
3781
3782static struct border *
3783add_border(struct wl_array *array,
3784 double x1, double y1,
3785 double x2, double y2,
3786 enum motion_direction blocking_dir)
3787{
3788 struct border *border = wl_array_add(array, sizeof *border);
3789
3790 *border = (struct border) {
3791 .line = (struct line) {
3792 .a = (struct vec2d) {
3793 .x = x1,
3794 .y = y1,
3795 },
3796 .b = (struct vec2d) {
3797 .x = x2,
3798 .y = y2,
3799 },
3800 },
3801 .blocking_dir = blocking_dir,
3802 };
3803
3804 return border;
3805}
3806
3807static int
3808compare_lines_x(const void *a, const void *b)
3809{
3810 const struct border *border_a = a;
3811 const struct border *border_b = b;
3812
3813
3814 if (border_a->line.a.x == border_b->line.a.x)
3815 return border_a->line.b.x < border_b->line.b.x;
3816 else
3817 return border_a->line.a.x > border_b->line.a.x;
3818}
3819
3820static void
3821add_non_overlapping_edges(pixman_box32_t *boxes,
3822 int band_above_start,
3823 int band_below_start,
3824 int band_below_end,
3825 struct wl_array *borders)
3826{
3827 int i;
3828 struct wl_array band_merge;
3829 struct border *border;
3830 struct border *prev_border;
3831 struct border *new_border;
3832
3833 wl_array_init(&band_merge);
3834
3835 /* Add bottom band of previous row, and top band of current row, and
3836 * sort them so lower left x coordinate comes first. If there are two
3837 * borders with the same left x coordinate, the wider one comes first.
3838 */
3839 for (i = band_above_start; i < band_below_start; i++) {
3840 pixman_box32_t *box = &boxes[i];
3841 add_border(&band_merge, box->x1, box->y2, box->x2, box->y2,
3842 MOTION_DIRECTION_POSITIVE_Y);
3843 }
3844 for (i = band_below_start; i < band_below_end; i++) {
3845 pixman_box32_t *box= &boxes[i];
3846 add_border(&band_merge, box->x1, box->y1, box->x2, box->y1,
3847 MOTION_DIRECTION_NEGATIVE_Y);
3848 }
3849 qsort(band_merge.data,
3850 band_merge.size / sizeof *border,
3851 sizeof *border,
3852 compare_lines_x);
3853
3854 /* Combine the two combined bands so that any overlapping border is
3855 * eliminated. */
3856 prev_border = NULL;
3857 wl_array_for_each(border, &band_merge) {
3858 assert(border->line.a.y == border->line.b.y);
3859 assert(!prev_border ||
3860 prev_border->line.a.y == border->line.a.y);
3861 assert(!prev_border ||
3862 (prev_border->line.a.x != border->line.a.x ||
3863 prev_border->line.b.x != border->line.b.x));
3864 assert(!prev_border ||
3865 prev_border->line.a.x <= border->line.a.x);
3866
3867 if (prev_border &&
3868 prev_border->line.a.x == border->line.a.x) {
3869 /*
3870 * ------------ +
3871 * ------- =
3872 * [ ]-----
3873 */
3874 prev_border->line.a.x = border->line.b.x;
3875 } else if (prev_border &&
3876 prev_border->line.b.x == border->line.b.x) {
3877 /*
3878 * ------------ +
3879 * ------ =
3880 * ------[ ]
3881 */
3882 prev_border->line.b.x = border->line.a.x;
3883 } else if (prev_border &&
3884 prev_border->line.b.x == border->line.a.x) {
3885 /*
3886 * -------- +
3887 * ------ =
3888 * --------------
3889 */
3890 prev_border->line.b.x = border->line.b.x;
3891 } else if (prev_border &&
3892 prev_border->line.b.x >= border->line.a.x) {
3893 /*
3894 * --------------- +
3895 * ------ =
3896 * -----[ ]----
3897 */
3898 new_border = add_border(borders,
3899 border->line.b.x,
3900 border->line.b.y,
3901 prev_border->line.b.x,
3902 prev_border->line.b.y,
3903 prev_border->blocking_dir);
3904 prev_border->line.b.x = border->line.a.x;
3905 prev_border = new_border;
3906 } else {
3907 assert(!prev_border ||
3908 prev_border->line.b.x < border->line.a.x);
3909 /*
3910 * First border or non-overlapping.
3911 *
3912 * ----- +
3913 * ----- =
3914 * ----- -----
3915 */
3916 new_border = wl_array_add(borders, sizeof *border);
3917 *new_border = *border;
3918 prev_border = new_border;
3919 }
3920 }
3921
3922 wl_array_release(&band_merge);
3923}
3924
3925static void
3926add_band_bottom_edges(pixman_box32_t *boxes,
3927 int band_start,
3928 int band_end,
3929 struct wl_array *borders)
3930{
3931 int i;
3932
3933 for (i = band_start; i < band_end; i++) {
3934 add_border(borders,
3935 boxes[i].x1, boxes[i].y2,
3936 boxes[i].x2, boxes[i].y2,
3937 MOTION_DIRECTION_POSITIVE_Y);
3938 }
3939}
3940
3941static void
3942region_to_outline(pixman_region32_t *region, struct wl_array *borders)
3943{
3944 pixman_box32_t *boxes;
3945 int num_boxes;
3946 int i;
3947 int top_most, bottom_most;
3948 int current_roof;
3949 int prev_top;
3950 int band_start, prev_band_start;
3951
3952 /*
3953 * Remove any overlapping lines from the set of rectangles. Note that
3954 * pixman regions are grouped as rows of rectangles, where rectangles
3955 * in one row never touch or overlap and are all of the same height.
3956 *
3957 * -------- --- -------- ---
3958 * | | | | | | | |
3959 * ----------====---- --- ----------- ----- ---
3960 * | | => | |
3961 * ----==========--------- ----- ----------
3962 * | | | |
3963 * ------------------- -------------------
3964 *
3965 */
3966
3967 boxes = pixman_region32_rectangles(region, &num_boxes);
3968 prev_top = 0;
3969 top_most = boxes[0].y1;
3970 current_roof = top_most;
3971 bottom_most = boxes[num_boxes - 1].y2;
3972 band_start = 0;
3973 prev_band_start = 0;
3974 for (i = 0; i < num_boxes; i++) {
3975 /* Detect if there is a vertical empty space, and add the lower
3976 * level of the previous band if so was the case. */
3977 if (i > 0 &&
3978 boxes[i].y1 != prev_top &&
3979 boxes[i].y1 != boxes[i - 1].y2) {
3980 current_roof = boxes[i].y1;
3981 add_band_bottom_edges(boxes,
3982 band_start,
3983 i,
3984 borders);
3985 }
3986
3987 /* Special case adding the last band, since it won't be handled
3988 * by the band change detection below. */
3989 if (boxes[i].y1 != current_roof && i == num_boxes - 1) {
3990 if (boxes[i].y1 != prev_top) {
3991 /* The last band is a single box, so we don't
3992 * have a prev_band_start to tell us when the
3993 * previous band started. */
3994 add_non_overlapping_edges(boxes,
3995 band_start,
3996 i,
3997 i + 1,
3998 borders);
3999 } else {
4000 add_non_overlapping_edges(boxes,
4001 prev_band_start,
4002 band_start,
4003 i + 1,
4004 borders);
4005 }
4006 }
4007
4008 /* Detect when passing a band and combine the top border of the
4009 * just passed band with the bottom band of the previous band.
4010 */
4011 if (boxes[i].y1 != top_most && boxes[i].y1 != prev_top) {
4012 /* Combine the two passed bands. */
4013 if (prev_top != current_roof) {
4014 add_non_overlapping_edges(boxes,
4015 prev_band_start,
4016 band_start,
4017 i,
4018 borders);
4019 }
4020
4021 prev_band_start = band_start;
4022 band_start = i;
4023 }
4024
4025 /* Add the top border if the box is part of the current roof. */
4026 if (boxes[i].y1 == current_roof) {
4027 add_border(borders,
4028 boxes[i].x1, boxes[i].y1,
4029 boxes[i].x2, boxes[i].y1,
4030 MOTION_DIRECTION_NEGATIVE_Y);
4031 }
4032
4033 /* Add the bottom border of the last band. */
4034 if (boxes[i].y2 == bottom_most) {
4035 add_border(borders,
4036 boxes[i].x1, boxes[i].y2,
4037 boxes[i].x2, boxes[i].y2,
4038 MOTION_DIRECTION_POSITIVE_Y);
4039 }
4040
4041 /* Always add the left border. */
4042 add_border(borders,
4043 boxes[i].x1, boxes[i].y1,
4044 boxes[i].x1, boxes[i].y2,
4045 MOTION_DIRECTION_NEGATIVE_X);
4046
4047 /* Always add the right border. */
4048 add_border(borders,
4049 boxes[i].x2, boxes[i].y1,
4050 boxes[i].x2, boxes[i].y2,
4051 MOTION_DIRECTION_POSITIVE_X);
4052
4053 prev_top = boxes[i].y1;
4054 }
4055}
4056
4057static bool
4058is_border_horizontal (struct border *border)
4059{
4060 return border->line.a.y == border->line.b.y;
4061}
4062
4063static bool
4064is_border_blocking_directions(struct border *border,
4065 uint32_t directions)
4066{
4067 /* Don't block parallel motions. */
4068 if (is_border_horizontal(border)) {
4069 if ((directions & (MOTION_DIRECTION_POSITIVE_Y |
4070 MOTION_DIRECTION_NEGATIVE_Y)) == 0)
4071 return false;
4072 } else {
4073 if ((directions & (MOTION_DIRECTION_POSITIVE_X |
4074 MOTION_DIRECTION_NEGATIVE_X)) == 0)
4075 return false;
4076 }
4077
4078 return (~border->blocking_dir & directions) != directions;
4079}
4080
4081static struct border *
4082get_closest_border(struct wl_array *borders,
4083 struct line *motion,
4084 uint32_t directions)
4085{
4086 struct border *border;
4087 struct vec2d intersection;
4088 struct vec2d delta;
4089 double distance_2;
4090 struct border *closest_border = NULL;
4091 double closest_distance_2 = DBL_MAX;
4092
4093 wl_array_for_each(border, borders) {
4094 if (!is_border_blocking_directions(border, directions))
4095 continue;
4096
4097 if (!lines_intersect(&border->line, motion, &intersection))
4098 continue;
4099
4100 delta = vec2d_subtract(intersection, motion->a);
4101 distance_2 = delta.x*delta.x + delta.y*delta.y;
4102 if (distance_2 < closest_distance_2) {
4103 closest_border = border;
4104 closest_distance_2 = distance_2;
4105 }
4106 }
4107
4108 return closest_border;
4109}
4110
4111static void
4112clamp_to_border(struct border *border,
4113 struct line *motion,
4114 uint32_t *motion_dir)
4115{
4116 /*
4117 * When clamping either rightward or downward motions, the motion needs
4118 * to be clamped so that the destination coordinate does not end up on
4119 * the border (see weston_pointer_clamp_event_to_region). Do this by
4120 * clamping such motions to the border minus the smallest possible
4121 * wl_fixed_t value.
4122 */
4123 if (is_border_horizontal(border)) {
4124 if (*motion_dir & MOTION_DIRECTION_POSITIVE_Y)
4125 motion->b.y = border->line.a.y - wl_fixed_to_double(1);
4126 else
4127 motion->b.y = border->line.a.y;
4128 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_Y |
4129 MOTION_DIRECTION_NEGATIVE_Y);
4130 } else {
4131 if (*motion_dir & MOTION_DIRECTION_POSITIVE_X)
4132 motion->b.x = border->line.a.x - wl_fixed_to_double(1);
4133 else
4134 motion->b.x = border->line.a.x;
4135 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_X |
4136 MOTION_DIRECTION_NEGATIVE_X);
4137 }
4138}
4139
4140static uint32_t
4141get_motion_directions(struct line *motion)
4142{
4143 uint32_t directions = 0;
4144
4145 if (motion->a.x < motion->b.x)
4146 directions |= MOTION_DIRECTION_POSITIVE_X;
4147 else if (motion->a.x > motion->b.x)
4148 directions |= MOTION_DIRECTION_NEGATIVE_X;
4149 if (motion->a.y < motion->b.y)
4150 directions |= MOTION_DIRECTION_POSITIVE_Y;
4151 else if (motion->a.y > motion->b.y)
4152 directions |= MOTION_DIRECTION_NEGATIVE_Y;
4153
4154 return directions;
4155}
4156
Jonas Ådahld3414f22016-07-22 17:56:31 +08004157static void
4158weston_pointer_clamp_event_to_region(struct weston_pointer *pointer,
4159 struct weston_pointer_motion_event *event,
4160 pixman_region32_t *region,
4161 wl_fixed_t *clamped_x,
4162 wl_fixed_t *clamped_y)
4163{
4164 wl_fixed_t x, y;
4165 wl_fixed_t sx, sy;
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004166 wl_fixed_t old_sx = pointer->sx;
4167 wl_fixed_t old_sy = pointer->sy;
4168 struct wl_array borders;
4169 struct line motion;
4170 struct border *closest_border;
4171 float new_x_f, new_y_f;
4172 uint32_t directions;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004173
4174 weston_pointer_motion_to_abs(pointer, event, &x, &y);
4175 weston_view_from_global_fixed(pointer->focus, x, y, &sx, &sy);
4176
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004177 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004178
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004179 /*
4180 * Generate borders given the confine region we are to use. The borders
4181 * are defined to be the outer region of the allowed area. This means
4182 * top/left borders are "within" the allowed area, while bottom/right
4183 * borders are outside. This needs to be considered when clamping
4184 * confined motion vectors.
4185 */
4186 region_to_outline(region, &borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004187
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004188 motion = (struct line) {
4189 .a = (struct vec2d) {
4190 .x = wl_fixed_to_double(old_sx),
4191 .y = wl_fixed_to_double(old_sy),
4192 },
4193 .b = (struct vec2d) {
4194 .x = wl_fixed_to_double(sx),
4195 .y = wl_fixed_to_double(sy),
4196 },
4197 };
4198 directions = get_motion_directions(&motion);
4199
4200 while (directions) {
4201 closest_border = get_closest_border(&borders,
4202 &motion,
4203 directions);
4204 if (closest_border)
4205 clamp_to_border(closest_border, &motion, &directions);
4206 else
4207 break;
4208 }
4209
4210 weston_view_to_global_float(pointer->focus,
4211 (float) motion.b.x, (float) motion.b.y,
4212 &new_x_f, &new_y_f);
4213 *clamped_x = wl_fixed_from_double(new_x_f);
4214 *clamped_y = wl_fixed_from_double(new_y_f);
4215
4216 wl_array_release(&borders);
4217}
4218
4219static double
4220point_to_border_distance_2(struct border *border, double x, double y)
4221{
4222 double orig_x, orig_y;
4223 double dx, dy;
4224
4225 if (is_border_horizontal(border)) {
4226 if (x < border->line.a.x)
4227 orig_x = border->line.a.x;
4228 else if (x > border->line.b.x)
4229 orig_x = border->line.b.x;
4230 else
4231 orig_x = x;
4232 orig_y = border->line.a.y;
4233 } else {
4234 if (y < border->line.a.y)
4235 orig_y = border->line.a.y;
4236 else if (y > border->line.b.y)
4237 orig_y = border->line.b.y;
4238 else
4239 orig_y = y;
4240 orig_x = border->line.a.x;
4241 }
4242
4243
4244 dx = fabs(orig_x - x);
4245 dy = fabs(orig_y - y);
4246 return dx*dx + dy*dy;
4247}
4248
4249static void
4250warp_to_behind_border(struct border *border, wl_fixed_t *sx, wl_fixed_t *sy)
4251{
4252 switch (border->blocking_dir) {
4253 case MOTION_DIRECTION_POSITIVE_X:
4254 case MOTION_DIRECTION_NEGATIVE_X:
4255 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_X)
4256 *sx = wl_fixed_from_double(border->line.a.x) - 1;
4257 else
4258 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4259 if (*sy < wl_fixed_from_double(border->line.a.y))
4260 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4261 else if (*sy > wl_fixed_from_double(border->line.b.y))
4262 *sy = wl_fixed_from_double(border->line.b.y) - 1;
4263 break;
4264 case MOTION_DIRECTION_POSITIVE_Y:
4265 case MOTION_DIRECTION_NEGATIVE_Y:
4266 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_Y)
4267 *sy = wl_fixed_from_double(border->line.a.y) - 1;
4268 else
4269 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4270 if (*sx < wl_fixed_from_double(border->line.a.x))
4271 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4272 else if (*sx > wl_fixed_from_double(border->line.b.x))
4273 *sx = wl_fixed_from_double(border->line.b.x) - 1;
4274 break;
4275 }
Jonas Ådahld3414f22016-07-22 17:56:31 +08004276}
4277
4278static void
4279maybe_warp_confined_pointer(struct weston_pointer_constraint *constraint)
4280{
4281 wl_fixed_t x;
4282 wl_fixed_t y;
4283 wl_fixed_t sx;
4284 wl_fixed_t sy;
4285
4286 weston_view_from_global_fixed(constraint->view,
4287 constraint->pointer->x,
4288 constraint->pointer->y,
4289 &sx,
4290 &sy);
4291
4292 if (!is_within_constraint_region(constraint, sx, sy)) {
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004293 double xf = wl_fixed_to_double(sx);
4294 double yf = wl_fixed_to_double(sy);
4295 pixman_region32_t confine_region;
4296 struct wl_array borders;
4297 struct border *border;
4298 double closest_distance_2 = DBL_MAX;
4299 struct border *closest_border = NULL;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004300
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004301 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004302
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004303 pixman_region32_init(&confine_region);
4304 pixman_region32_intersect(&confine_region,
4305 &constraint->view->surface->input,
4306 &constraint->region);
4307 region_to_outline(&confine_region, &borders);
4308 pixman_region32_fini(&confine_region);
4309
4310 wl_array_for_each(border, &borders) {
4311 double distance_2;
4312
4313 distance_2 = point_to_border_distance_2(border, xf, yf);
4314 if (distance_2 < closest_distance_2) {
4315 closest_border = border;
4316 closest_distance_2 = distance_2;
4317 }
4318 }
4319 assert(closest_border);
4320
4321 warp_to_behind_border(closest_border, &sx, &sy);
4322
4323 wl_array_release(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004324
4325 weston_view_to_global_fixed(constraint->view, sx, sy, &x, &y);
4326 weston_pointer_move_to(constraint->pointer, x, y);
4327 }
4328}
4329
4330static void
4331confined_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02004332 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004333 struct weston_pointer_motion_event *event)
4334{
4335 struct weston_pointer_constraint *constraint =
4336 container_of(grab, struct weston_pointer_constraint, grab);
4337 struct weston_pointer *pointer = grab->pointer;
4338 struct weston_surface *surface;
4339 wl_fixed_t x, y;
4340 wl_fixed_t old_sx = pointer->sx;
4341 wl_fixed_t old_sy = pointer->sy;
4342 pixman_region32_t confine_region;
4343
4344 assert(pointer->focus);
4345 assert(pointer->focus->surface == constraint->surface);
4346
4347 surface = pointer->focus->surface;
4348
4349 pixman_region32_init(&confine_region);
4350 pixman_region32_intersect(&confine_region,
4351 &surface->input,
4352 &constraint->region);
4353 weston_pointer_clamp_event_to_region(pointer, event,
4354 &confine_region, &x, &y);
4355 weston_pointer_move_to(pointer, x, y);
4356 pixman_region32_fini(&confine_region);
4357
4358 weston_view_from_global_fixed(pointer->focus, x, y,
4359 &pointer->sx, &pointer->sy);
4360
4361 if (old_sx != pointer->sx || old_sy != pointer->sy) {
Quentin Glidiccde13452016-08-12 10:41:32 +02004362 pointer_send_motion(pointer, time,
4363 pointer->sx, pointer->sy);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004364 }
4365
Quentin Glidiccde13452016-08-12 10:41:32 +02004366 pointer_send_relative_motion(pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004367}
4368
4369static void
4370confined_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02004371 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004372 uint32_t button,
4373 uint32_t state_w)
4374{
4375 weston_pointer_send_button(grab->pointer, time, button, state_w);
4376}
4377
4378static void
4379confined_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02004380 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004381 struct weston_pointer_axis_event *event)
4382{
4383 weston_pointer_send_axis(grab->pointer, time, event);
4384}
4385
4386static void
4387confined_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
4388 uint32_t source)
4389{
4390 weston_pointer_send_axis_source(grab->pointer, source);
4391}
4392
4393static void
4394confined_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
4395{
4396 weston_pointer_send_frame(grab->pointer);
4397}
4398
4399static void
4400confined_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
4401{
4402 struct weston_pointer_constraint *constraint =
4403 container_of(grab, struct weston_pointer_constraint, grab);
4404
4405 disable_pointer_constraint(constraint);
4406
4407 /* If this is a persistent constraint, re-add the surface destroy signal
4408 * listener only if we are currently not destroying the surface. */
4409 switch (constraint->lifetime) {
4410 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT:
4411 if (constraint->surface->resource)
4412 wl_signal_add(&constraint->surface->destroy_signal,
4413 &constraint->surface_destroy_listener);
4414 break;
4415 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT:
4416 break;
4417 }
4418}
4419
4420static const struct weston_pointer_grab_interface
4421 confined_pointer_grab_interface = {
4422 confined_pointer_grab_pointer_focus,
4423 confined_pointer_grab_pointer_motion,
4424 confined_pointer_grab_pointer_button,
4425 confined_pointer_grab_pointer_axis,
4426 confined_pointer_grab_pointer_axis_source,
4427 confined_pointer_grab_pointer_frame,
4428 confined_pointer_grab_pointer_cancel,
4429};
4430
4431static void
4432confined_pointer_destroy(struct wl_client *client,
4433 struct wl_resource *resource)
4434{
4435 wl_resource_destroy(resource);
4436}
4437
4438static void
4439confined_pointer_set_region(struct wl_client *client,
4440 struct wl_resource *resource,
4441 struct wl_resource *region_resource)
4442{
4443 struct weston_pointer_constraint *constraint =
4444 wl_resource_get_user_data(resource);
4445 struct weston_region *region = region_resource ?
4446 wl_resource_get_user_data(region_resource) : NULL;
4447
4448 if (!constraint)
4449 return;
4450
4451 if (region) {
4452 pixman_region32_copy(&constraint->region_pending,
4453 &region->region);
4454 } else {
4455 pixman_region32_fini(&constraint->region_pending);
4456 region_init_infinite(&constraint->region_pending);
4457 }
4458 constraint->region_is_pending = true;
4459}
4460
4461static const struct zwp_confined_pointer_v1_interface confined_pointer_interface = {
4462 confined_pointer_destroy,
4463 confined_pointer_set_region,
4464};
4465
4466static void
4467pointer_constraints_confine_pointer(struct wl_client *client,
4468 struct wl_resource *resource,
4469 uint32_t id,
4470 struct wl_resource *surface_resource,
4471 struct wl_resource *pointer_resource,
4472 struct wl_resource *region_resource,
4473 uint32_t lifetime)
4474{
4475 struct weston_surface *surface =
4476 wl_resource_get_user_data(surface_resource);
4477 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
4478 struct weston_region *region = region_resource ?
4479 wl_resource_get_user_data(region_resource) : NULL;
4480
4481 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
4482 &zwp_confined_pointer_v1_interface,
4483 &confined_pointer_interface,
4484 &confined_pointer_grab_interface);
4485}
4486
4487static const struct zwp_pointer_constraints_v1_interface pointer_constraints_interface = {
4488 pointer_constraints_destroy,
4489 pointer_constraints_lock_pointer,
4490 pointer_constraints_confine_pointer,
4491};
4492
4493static void
4494bind_pointer_constraints(struct wl_client *client, void *data,
4495 uint32_t version, uint32_t id)
4496{
4497 struct wl_resource *resource;
4498
4499 resource = wl_resource_create(client,
4500 &zwp_pointer_constraints_v1_interface,
4501 1, id);
4502
4503 wl_resource_set_implementation(resource, &pointer_constraints_interface,
4504 NULL, NULL);
4505}
4506
Jonas Ådahl30d61d82014-10-22 21:21:17 +02004507int
4508weston_input_init(struct weston_compositor *compositor)
4509{
4510 if (!wl_global_create(compositor->wl_display,
4511 &zwp_relative_pointer_manager_v1_interface, 1,
4512 compositor, bind_relative_pointer_manager))
4513 return -1;
4514
Jonas Ådahld3414f22016-07-22 17:56:31 +08004515 if (!wl_global_create(compositor->wl_display,
4516 &zwp_pointer_constraints_v1_interface, 1,
4517 NULL, bind_pointer_constraints))
4518 return -1;
4519
Jonas Ådahl30d61d82014-10-22 21:21:17 +02004520 return 0;
4521}