blob: 94a3423a9a008dcee5de7e5c7719ed79a16a478c [file] [log] [blame]
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001/*
2 * Copyright © 2013 Intel Corporation
3 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07004 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
Kristian Høgsberg2158a882013-04-18 15:07:39 -040011 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070012 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
Kristian Høgsberg2158a882013-04-18 15:07:39 -040024 */
25
Daniel Stone8e7a8bd2013-08-15 01:10:24 +010026#include "config.h"
27
Jonas Ådahld3414f22016-07-22 17:56:31 +080028#include <stdbool.h>
Kristian Høgsberg2158a882013-04-18 15:07:39 -040029#include <stdlib.h>
30#include <stdint.h>
31#include <string.h>
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040032#include <sys/mman.h>
33#include <assert.h>
34#include <unistd.h>
Jonas Ådahld0be2bb2015-04-30 17:56:37 +080035#include <values.h>
Matt Roper01a92732013-06-24 16:52:44 +010036#include <fcntl.h>
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +020037#include <limits.h>
Kristian Høgsberg2158a882013-04-18 15:07:39 -040038
Jon Cruz35b2eaa2015-06-15 15:37:08 -070039#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070040#include "shared/os-compatibility.h"
Alexandros Frantzis84b31f82017-11-24 18:01:46 +020041#include "shared/timespec-util.h"
Kristian Høgsberg2158a882013-04-18 15:07:39 -040042#include "compositor.h"
Daniel Stone7dbb0e12016-11-24 15:30:41 +000043#include "relative-pointer-unstable-v1-server-protocol.h"
44#include "pointer-constraints-unstable-v1-server-protocol.h"
Jonas Ådahld3414f22016-07-22 17:56:31 +080045
46enum pointer_constraint_type {
47 POINTER_CONSTRAINT_TYPE_LOCK,
48 POINTER_CONSTRAINT_TYPE_CONFINE,
49};
50
Jonas Ådahld0be2bb2015-04-30 17:56:37 +080051enum motion_direction {
52 MOTION_DIRECTION_POSITIVE_X = 1 << 0,
53 MOTION_DIRECTION_NEGATIVE_X = 1 << 1,
54 MOTION_DIRECTION_POSITIVE_Y = 1 << 2,
55 MOTION_DIRECTION_NEGATIVE_Y = 1 << 3,
56};
57
58struct vec2d {
59 double x, y;
60};
61
62struct line {
63 struct vec2d a;
64 struct vec2d b;
65};
66
67struct border {
68 struct line line;
69 enum motion_direction blocking_dir;
70};
71
Jonas Ådahld3414f22016-07-22 17:56:31 +080072static void
73maybe_warp_confined_pointer(struct weston_pointer_constraint *constraint);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040074
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040075static void
76empty_region(pixman_region32_t *region)
77{
78 pixman_region32_fini(region);
79 pixman_region32_init(region);
80}
81
Jonas Ådahld3414f22016-07-22 17:56:31 +080082static void
83region_init_infinite(pixman_region32_t *region)
84{
85 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
86 UINT32_MAX, UINT32_MAX);
87}
88
Jonas Ådahl2cbf2932015-07-22 12:05:38 +080089static struct weston_pointer_client *
90weston_pointer_client_create(struct wl_client *client)
91{
92 struct weston_pointer_client *pointer_client;
93
94 pointer_client = zalloc(sizeof *pointer_client);
95 if (!pointer_client)
96 return NULL;
97
98 pointer_client->client = client;
99 wl_list_init(&pointer_client->pointer_resources);
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200100 wl_list_init(&pointer_client->relative_pointer_resources);
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800101
102 return pointer_client;
103}
104
105static void
106weston_pointer_client_destroy(struct weston_pointer_client *pointer_client)
107{
108 free(pointer_client);
109}
110
111static bool
112weston_pointer_client_is_empty(struct weston_pointer_client *pointer_client)
113{
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200114 return (wl_list_empty(&pointer_client->pointer_resources) &&
115 wl_list_empty(&pointer_client->relative_pointer_resources));
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800116}
117
118static struct weston_pointer_client *
119weston_pointer_get_pointer_client(struct weston_pointer *pointer,
120 struct wl_client *client)
121{
122 struct weston_pointer_client *pointer_client;
123
124 wl_list_for_each(pointer_client, &pointer->pointer_clients, link) {
125 if (pointer_client->client == client)
126 return pointer_client;
127 }
128
129 return NULL;
130}
131
132static struct weston_pointer_client *
133weston_pointer_ensure_pointer_client(struct weston_pointer *pointer,
134 struct wl_client *client)
135{
136 struct weston_pointer_client *pointer_client;
137
138 pointer_client = weston_pointer_get_pointer_client(pointer, client);
139 if (pointer_client)
140 return pointer_client;
141
142 pointer_client = weston_pointer_client_create(client);
143 wl_list_insert(&pointer->pointer_clients, &pointer_client->link);
144
145 if (pointer->focus &&
146 pointer->focus->surface->resource &&
147 wl_resource_get_client(pointer->focus->surface->resource) == client) {
148 pointer->focus_client = pointer_client;
149 }
150
151 return pointer_client;
152}
153
154static void
155weston_pointer_cleanup_pointer_client(struct weston_pointer *pointer,
156 struct weston_pointer_client *pointer_client)
157{
158 if (weston_pointer_client_is_empty(pointer_client)) {
159 if (pointer->focus_client == pointer_client)
160 pointer->focus_client = NULL;
161 wl_list_remove(&pointer_client->link);
162 weston_pointer_client_destroy(pointer_client);
163 }
164}
165
166static void
167unbind_pointer_client_resource(struct wl_resource *resource)
168{
169 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
170 struct wl_client *client = wl_resource_get_client(resource);
171 struct weston_pointer_client *pointer_client;
172
173 pointer_client = weston_pointer_get_pointer_client(pointer, client);
174 assert(pointer_client);
175
176 wl_list_remove(wl_resource_get_link(resource));
177 weston_pointer_cleanup_pointer_client(pointer, pointer_client);
178}
179
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400180static void unbind_resource(struct wl_resource *resource)
181{
Jason Ekstrand44a38632013-06-14 10:08:00 -0500182 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400183}
184
Jonas Ådahl3042ffe2013-10-17 23:04:08 +0200185WL_EXPORT void
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200186weston_pointer_motion_to_abs(struct weston_pointer *pointer,
187 struct weston_pointer_motion_event *event,
188 wl_fixed_t *x, wl_fixed_t *y)
189{
190 if (event->mask & WESTON_POINTER_MOTION_ABS) {
191 *x = wl_fixed_from_double(event->x);
192 *y = wl_fixed_from_double(event->y);
193 } else if (event->mask & WESTON_POINTER_MOTION_REL) {
194 *x = pointer->x + wl_fixed_from_double(event->dx);
195 *y = pointer->y + wl_fixed_from_double(event->dy);
196 } else {
197 assert(!"invalid motion event");
198 *x = *y = 0;
199 }
200}
201
202static bool
203weston_pointer_motion_to_rel(struct weston_pointer *pointer,
204 struct weston_pointer_motion_event *event,
205 double *dx, double *dy,
206 double *dx_unaccel, double *dy_unaccel)
207{
208 if (event->mask & WESTON_POINTER_MOTION_REL &&
209 event->mask & WESTON_POINTER_MOTION_REL_UNACCEL) {
210 *dx = event->dx;
211 *dy = event->dy;
212 *dx_unaccel = event->dx_unaccel;
213 *dy_unaccel = event->dy_unaccel;
214 return true;
215 } else if (event->mask & WESTON_POINTER_MOTION_REL) {
216 *dx_unaccel = *dx = event->dx;
217 *dy_unaccel = *dy = event->dy;
218 return true;
219 } else if (event->mask & WESTON_POINTER_MOTION_REL_UNACCEL) {
220 *dx_unaccel = *dx = event->dx_unaccel;
221 *dy_unaccel = *dy = event->dy_unaccel;
222 return true;
223 } else {
224 return false;
225 }
226}
227
228WL_EXPORT void
Kristian Høgsberga71e8b22013-05-06 21:51:21 -0400229weston_seat_repick(struct weston_seat *seat)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400230{
Derek Foreman1281a362015-07-31 16:55:32 -0500231 const struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400232
Derek Foreman1b786ee2015-06-03 15:53:23 -0500233 if (!pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400234 return;
235
Derek Foreman1b786ee2015-06-03 15:53:23 -0500236 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400237}
238
239static void
240weston_compositor_idle_inhibit(struct weston_compositor *compositor)
241{
242 weston_compositor_wake(compositor);
243 compositor->idle_inhibit++;
244}
245
246static void
247weston_compositor_idle_release(struct weston_compositor *compositor)
248{
249 compositor->idle_inhibit--;
250 weston_compositor_wake(compositor);
251}
252
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400253static void
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100254pointer_focus_view_destroyed(struct wl_listener *listener, void *data)
255{
256 struct weston_pointer *pointer =
257 container_of(listener, struct weston_pointer,
258 focus_view_listener);
259
Derek Foremanf9318d12015-05-11 15:40:11 -0500260 weston_pointer_clear_focus(pointer);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100261}
262
263static void
264pointer_focus_resource_destroyed(struct wl_listener *listener, void *data)
265{
266 struct weston_pointer *pointer =
267 container_of(listener, struct weston_pointer,
268 focus_resource_listener);
269
Derek Foremanf9318d12015-05-11 15:40:11 -0500270 weston_pointer_clear_focus(pointer);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100271}
272
273static void
274keyboard_focus_resource_destroyed(struct wl_listener *listener, void *data)
275{
276 struct weston_keyboard *keyboard =
277 container_of(listener, struct weston_keyboard,
278 focus_resource_listener);
279
280 weston_keyboard_set_focus(keyboard, NULL);
281}
282
283static void
284touch_focus_view_destroyed(struct wl_listener *listener, void *data)
285{
286 struct weston_touch *touch =
287 container_of(listener, struct weston_touch,
288 focus_view_listener);
289
Derek Foreman4c93c082015-04-30 16:45:41 -0500290 weston_touch_set_focus(touch, NULL);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100291}
292
293static void
294touch_focus_resource_destroyed(struct wl_listener *listener, void *data)
295{
296 struct weston_touch *touch =
297 container_of(listener, struct weston_touch,
298 focus_resource_listener);
299
Derek Foreman4c93c082015-04-30 16:45:41 -0500300 weston_touch_set_focus(touch, NULL);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100301}
302
303static void
Neil Roberts96d790e2013-09-19 17:32:00 +0100304move_resources(struct wl_list *destination, struct wl_list *source)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400305{
Neil Roberts96d790e2013-09-19 17:32:00 +0100306 wl_list_insert_list(destination, source);
307 wl_list_init(source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400308}
309
310static void
Neil Roberts96d790e2013-09-19 17:32:00 +0100311move_resources_for_client(struct wl_list *destination,
312 struct wl_list *source,
313 struct wl_client *client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400314{
Neil Roberts96d790e2013-09-19 17:32:00 +0100315 struct wl_resource *resource, *tmp;
316 wl_resource_for_each_safe(resource, tmp, source) {
317 if (wl_resource_get_client(resource) == client) {
318 wl_list_remove(wl_resource_get_link(resource));
319 wl_list_insert(destination,
320 wl_resource_get_link(resource));
321 }
322 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400323}
324
325static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700326default_grab_pointer_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400327{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400328 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500329 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400330 wl_fixed_t sx, sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400331
332 if (pointer->button_count > 0)
333 return;
334
Jason Ekstranda7af7042013-10-12 22:38:11 -0500335 view = weston_compositor_pick_view(pointer->seat->compositor,
336 pointer->x, pointer->y,
337 &sx, &sy);
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400338
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -0800339 if (pointer->focus != view || pointer->sx != sx || pointer->sy != sy)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500340 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400341}
342
343static void
Quentin Glidiccde13452016-08-12 10:41:32 +0200344pointer_send_relative_motion(struct weston_pointer *pointer,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200345 const struct timespec *time,
Quentin Glidiccde13452016-08-12 10:41:32 +0200346 struct weston_pointer_motion_event *event)
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200347{
348 uint64_t time_usec;
349 double dx, dy, dx_unaccel, dy_unaccel;
350 wl_fixed_t dxf, dyf, dxf_unaccel, dyf_unaccel;
351 struct wl_list *resource_list;
352 struct wl_resource *resource;
353
354 if (!pointer->focus_client)
355 return;
356
357 if (!weston_pointer_motion_to_rel(pointer, event,
358 &dx, &dy,
359 &dx_unaccel, &dy_unaccel))
360 return;
361
362 resource_list = &pointer->focus_client->relative_pointer_resources;
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200363 time_usec = timespec_to_usec(&event->time);
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200364 if (time_usec == 0)
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200365 time_usec = timespec_to_usec(time);
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200366
367 dxf = wl_fixed_from_double(dx);
368 dyf = wl_fixed_from_double(dy);
369 dxf_unaccel = wl_fixed_from_double(dx_unaccel);
370 dyf_unaccel = wl_fixed_from_double(dy_unaccel);
371
372 wl_resource_for_each(resource, resource_list) {
373 zwp_relative_pointer_v1_send_relative_motion(
374 resource,
375 (uint32_t) (time_usec >> 32),
376 (uint32_t) time_usec,
377 dxf, dyf,
378 dxf_unaccel, dyf_unaccel);
379 }
380}
381
382static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200383pointer_send_motion(struct weston_pointer *pointer,
384 const struct timespec *time,
Quentin Glidiccde13452016-08-12 10:41:32 +0200385 wl_fixed_t sx, wl_fixed_t sy)
Jonas Ådahlf44942e2016-07-22 17:54:55 +0800386{
387 struct wl_list *resource_list;
388 struct wl_resource *resource;
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200389 uint32_t msecs;
Jonas Ådahlf44942e2016-07-22 17:54:55 +0800390
391 if (!pointer->focus_client)
392 return;
393
394 resource_list = &pointer->focus_client->pointer_resources;
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200395 msecs = timespec_to_msec(time);
Jonas Ådahlf44942e2016-07-22 17:54:55 +0800396 wl_resource_for_each(resource, resource_list)
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200397 wl_pointer_send_motion(resource, msecs, sx, sy);
Jonas Ådahlf44942e2016-07-22 17:54:55 +0800398}
399
Quentin Glidiccde13452016-08-12 10:41:32 +0200400WL_EXPORT void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200401weston_pointer_send_motion(struct weston_pointer *pointer,
402 const struct timespec *time,
Quentin Glidiccde13452016-08-12 10:41:32 +0200403 struct weston_pointer_motion_event *event)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400404{
Jonas Ådahld2510102014-10-05 21:39:14 +0200405 wl_fixed_t x, y;
Jonas Ådahl8283c342015-04-24 15:26:17 +0800406 wl_fixed_t old_sx = pointer->sx;
407 wl_fixed_t old_sy = pointer->sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400408
Jonas Ådahld2510102014-10-05 21:39:14 +0200409 if (pointer->focus) {
410 weston_pointer_motion_to_abs(pointer, event, &x, &y);
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -0800411 weston_view_from_global_fixed(pointer->focus, x, y,
412 &pointer->sx, &pointer->sy);
Jonas Ådahld2510102014-10-05 21:39:14 +0200413 }
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -0800414
Jonas Ådahld2510102014-10-05 21:39:14 +0200415 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100416
Jonas Ådahlf44942e2016-07-22 17:54:55 +0800417 if (old_sx != pointer->sx || old_sy != pointer->sy) {
Quentin Glidiccde13452016-08-12 10:41:32 +0200418 pointer_send_motion(pointer, time,
419 pointer->sx, pointer->sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400420 }
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200421
Quentin Glidiccde13452016-08-12 10:41:32 +0200422 pointer_send_relative_motion(pointer, time, event);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400423}
424
425static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200426default_grab_pointer_motion(struct weston_pointer_grab *grab,
427 const struct timespec *time,
Quentin Glidiccde13452016-08-12 10:41:32 +0200428 struct weston_pointer_motion_event *event)
429{
430 weston_pointer_send_motion(grab->pointer, time, event);
431}
432
433/** Check if the pointer has focused resources.
434 *
435 * \param pointer The pointer to check for focused resources.
436 * \return Whether or not this pointer has focused resources
437 */
438WL_EXPORT bool
439weston_pointer_has_focus_resource(struct weston_pointer *pointer)
440{
441 if (!pointer->focus_client)
442 return false;
443
444 if (wl_list_empty(&pointer->focus_client->pointer_resources))
445 return false;
446
447 return true;
448}
449
450/** Send wl_pointer.button events to focused resources.
451 *
452 * \param pointer The pointer where the button events originates from.
453 * \param time The timestamp of the event
454 * \param button The button value of the event
455 * \param value The state enum value of the event
456 *
457 * For every resource that is currently in focus, send a wl_pointer.button event
458 * with the passed parameters. The focused resources are the wl_pointer
459 * resources of the client which currently has the surface with pointer focus.
460 */
461WL_EXPORT void
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800462weston_pointer_send_button(struct weston_pointer *pointer,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200463 const struct timespec *time, uint32_t button,
Quentin Glidiccde13452016-08-12 10:41:32 +0200464 enum wl_pointer_button_state state)
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800465{
466 struct wl_display *display = pointer->seat->compositor->wl_display;
467 struct wl_list *resource_list;
468 struct wl_resource *resource;
469 uint32_t serial;
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200470 uint32_t msecs;
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800471
Quentin Glidiccde13452016-08-12 10:41:32 +0200472 if (!weston_pointer_has_focus_resource(pointer))
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800473 return;
474
475 resource_list = &pointer->focus_client->pointer_resources;
Quentin Glidiccde13452016-08-12 10:41:32 +0200476 serial = wl_display_next_serial(display);
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200477 msecs = timespec_to_msec(time);
Quentin Glidiccde13452016-08-12 10:41:32 +0200478 wl_resource_for_each(resource, resource_list)
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200479 wl_pointer_send_button(resource, serial, msecs, button, state);
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800480}
481
482static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700483default_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200484 const struct timespec *time, uint32_t button,
Quentin Glidiccde13452016-08-12 10:41:32 +0200485 enum wl_pointer_button_state state)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400486{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400487 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400488 struct weston_compositor *compositor = pointer->seat->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500489 struct weston_view *view;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400490 wl_fixed_t sx, sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400491
Quentin Glidiccde13452016-08-12 10:41:32 +0200492 weston_pointer_send_button(pointer, time, button, state);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400493
494 if (pointer->button_count == 0 &&
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400495 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500496 view = weston_compositor_pick_view(compositor,
497 pointer->x, pointer->y,
498 &sx, &sy);
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400499
Jason Ekstranda7af7042013-10-12 22:38:11 -0500500 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400501 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400502}
503
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200504/** Send wl_pointer.axis events to focused resources.
505 *
506 * \param pointer The pointer where the axis events originates from.
507 * \param time The timestamp of the event
508 * \param axis The axis enum value of the event
509 * \param value The axis value of the event
510 *
511 * For every resource that is currently in focus, send a wl_pointer.axis event
512 * with the passed parameters. The focused resources are the wl_pointer
513 * resources of the client which currently has the surface with pointer focus.
514 */
515WL_EXPORT void
516weston_pointer_send_axis(struct weston_pointer *pointer,
Alexandros Frantzis80321942017-11-16 18:20:56 +0200517 const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +1000518 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200519{
520 struct wl_resource *resource;
521 struct wl_list *resource_list;
Alexandros Frantzis80321942017-11-16 18:20:56 +0200522 uint32_t msecs;
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200523
Quentin Glidiccde13452016-08-12 10:41:32 +0200524 if (!weston_pointer_has_focus_resource(pointer))
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800525 return;
526
527 resource_list = &pointer->focus_client->pointer_resources;
Alexandros Frantzis80321942017-11-16 18:20:56 +0200528 msecs = timespec_to_msec(time);
Peter Hutterer87743e92016-01-18 16:38:22 +1000529 wl_resource_for_each(resource, resource_list) {
530 if (event->has_discrete &&
531 wl_resource_get_version(resource) >=
532 WL_POINTER_AXIS_DISCRETE_SINCE_VERSION)
533 wl_pointer_send_axis_discrete(resource, event->axis,
534 event->discrete);
535
536 if (event->value)
Alexandros Frantzis80321942017-11-16 18:20:56 +0200537 wl_pointer_send_axis(resource, msecs,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200538 event->axis,
539 wl_fixed_from_double(event->value));
Peter Hutterer87743e92016-01-18 16:38:22 +1000540 else if (wl_resource_get_version(resource) >=
541 WL_POINTER_AXIS_STOP_SINCE_VERSION)
Alexandros Frantzis80321942017-11-16 18:20:56 +0200542 wl_pointer_send_axis_stop(resource, msecs,
Peter Hutterer87743e92016-01-18 16:38:22 +1000543 event->axis);
544 }
545}
546
Quentin Glidiccde13452016-08-12 10:41:32 +0200547/** Send wl_pointer.axis_source events to focused resources.
548 *
549 * \param pointer The pointer where the axis_source events originates from.
550 * \param source The axis_source enum value of the event
551 *
552 * For every resource that is currently in focus, send a wl_pointer.axis_source
553 * event with the passed parameter. The focused resources are the wl_pointer
554 * resources of the client which currently has the surface with pointer focus.
555 */
Peter Hutterer87743e92016-01-18 16:38:22 +1000556WL_EXPORT void
Quentin Glidiccde13452016-08-12 10:41:32 +0200557weston_pointer_send_axis_source(struct weston_pointer *pointer,
558 enum wl_pointer_axis_source source)
Peter Hutterer87743e92016-01-18 16:38:22 +1000559{
560 struct wl_resource *resource;
561 struct wl_list *resource_list;
562
Quentin Glidiccde13452016-08-12 10:41:32 +0200563 if (!weston_pointer_has_focus_resource(pointer))
Jonas Ådahled6014a2016-04-21 10:21:48 +0800564 return;
565
Peter Hutterer87743e92016-01-18 16:38:22 +1000566 resource_list = &pointer->focus_client->pointer_resources;
567 wl_resource_for_each(resource, resource_list) {
568 if (wl_resource_get_version(resource) >=
569 WL_POINTER_AXIS_SOURCE_SINCE_VERSION) {
570 wl_pointer_send_axis_source(resource, source);
571 }
572 }
573}
574
575static void
576pointer_send_frame(struct wl_resource *resource)
577{
578 if (wl_resource_get_version(resource) >=
579 WL_POINTER_FRAME_SINCE_VERSION) {
580 wl_pointer_send_frame(resource);
581 }
582}
583
Quentin Glidiccde13452016-08-12 10:41:32 +0200584/** Send wl_pointer.frame events to focused resources.
585 *
586 * \param pointer The pointer where the frame events originates from.
587 *
588 * For every resource that is currently in focus, send a wl_pointer.frame event.
589 * The focused resources are the wl_pointer resources of the client which
590 * currently has the surface with pointer focus.
591 */
Peter Hutterer87743e92016-01-18 16:38:22 +1000592WL_EXPORT void
593weston_pointer_send_frame(struct weston_pointer *pointer)
594{
595 struct wl_resource *resource;
596 struct wl_list *resource_list;
597
Quentin Glidiccde13452016-08-12 10:41:32 +0200598 if (!weston_pointer_has_focus_resource(pointer))
Derek Foreman8efa31b2016-01-29 10:29:46 -0600599 return;
600
Peter Hutterer87743e92016-01-18 16:38:22 +1000601 resource_list = &pointer->focus_client->pointer_resources;
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200602 wl_resource_for_each(resource, resource_list)
Peter Hutterer87743e92016-01-18 16:38:22 +1000603 pointer_send_frame(resource);
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200604}
605
606static void
607default_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +0200608 const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +1000609 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200610{
Peter Hutterer89b6a492016-01-18 15:58:17 +1000611 weston_pointer_send_axis(grab->pointer, time, event);
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200612}
613
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200614static void
Peter Hutterer87743e92016-01-18 16:38:22 +1000615default_grab_pointer_axis_source(struct weston_pointer_grab *grab,
Quentin Glidiccde13452016-08-12 10:41:32 +0200616 enum wl_pointer_axis_source source)
Peter Hutterer87743e92016-01-18 16:38:22 +1000617{
618 weston_pointer_send_axis_source(grab->pointer, source);
619}
620
621static void
622default_grab_pointer_frame(struct weston_pointer_grab *grab)
623{
624 weston_pointer_send_frame(grab->pointer);
625}
626
627static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200628default_grab_pointer_cancel(struct weston_pointer_grab *grab)
629{
630}
631
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400632static const struct weston_pointer_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400633 default_pointer_grab_interface = {
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700634 default_grab_pointer_focus,
635 default_grab_pointer_motion,
636 default_grab_pointer_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200637 default_grab_pointer_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +1000638 default_grab_pointer_axis_source,
639 default_grab_pointer_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200640 default_grab_pointer_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400641};
642
Quentin Glidiccde13452016-08-12 10:41:32 +0200643/** Check if the touch has focused resources.
644 *
645 * \param touch The touch to check for focused resources.
646 * \return Whether or not this touch has focused resources
647 */
648WL_EXPORT bool
649weston_touch_has_focus_resource(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400650{
Quentin Glidiccde13452016-08-12 10:41:32 +0200651 if (!touch->focus)
652 return false;
653
654 if (wl_list_empty(&touch->focus_resource_list))
655 return false;
656
657 return true;
658}
659
660/** Send wl_touch.down events to focused resources.
661 *
662 * \param touch The touch where the down events originates from.
663 * \param time The timestamp of the event
664 * \param touch_id The touch_id value of the event
665 * \param x The x value of the event
666 * \param y The y value of the event
667 *
668 * For every resource that is currently in focus, send a wl_touch.down event
669 * with the passed parameters. The focused resources are the wl_touch
670 * resources of the client which currently has the surface with touch focus.
671 */
672WL_EXPORT void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200673weston_touch_send_down(struct weston_touch *touch, const struct timespec *time,
Quentin Glidiccde13452016-08-12 10:41:32 +0200674 int touch_id, wl_fixed_t x, wl_fixed_t y)
675{
Rob Bradford880ebc72013-07-22 17:31:38 +0100676 struct wl_display *display = touch->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400677 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100678 struct wl_resource *resource;
679 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300680 wl_fixed_t sx, sy;
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200681 uint32_t msecs;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300682
Quentin Glidiccde13452016-08-12 10:41:32 +0200683 if (!weston_touch_has_focus_resource(touch))
Bryce Harrington2c40d1d2016-02-02 10:18:48 -0800684 return;
685
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300686 weston_view_from_global_fixed(touch->focus, x, y, &sx, &sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400687
Neil Roberts96d790e2013-09-19 17:32:00 +0100688 resource_list = &touch->focus_resource_list;
Quentin Glidiccde13452016-08-12 10:41:32 +0200689 serial = wl_display_next_serial(display);
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200690 msecs = timespec_to_msec(time);
Quentin Glidiccde13452016-08-12 10:41:32 +0200691 wl_resource_for_each(resource, resource_list)
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200692 wl_touch_send_down(resource, serial, msecs,
Quentin Glidiccde13452016-08-12 10:41:32 +0200693 touch->focus->surface->resource,
694 touch_id, sx, sy);
695}
Neil Roberts96d790e2013-09-19 17:32:00 +0100696
Quentin Glidiccde13452016-08-12 10:41:32 +0200697static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200698default_grab_touch_down(struct weston_touch_grab *grab,
699 const struct timespec *time, int touch_id,
700 wl_fixed_t x, wl_fixed_t y)
Quentin Glidiccde13452016-08-12 10:41:32 +0200701{
702 weston_touch_send_down(grab->touch, time, touch_id, x, y);
703}
704
705/** Send wl_touch.up events to focused resources.
706 *
707 * \param touch The touch where the up events originates from.
708 * \param time The timestamp of the event
709 * \param touch_id The touch_id value of the event
710 *
711 * For every resource that is currently in focus, send a wl_touch.up event
712 * with the passed parameters. The focused resources are the wl_touch
713 * resources of the client which currently has the surface with touch focus.
714 */
715WL_EXPORT void
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200716weston_touch_send_up(struct weston_touch *touch, const struct timespec *time,
717 int touch_id)
Quentin Glidiccde13452016-08-12 10:41:32 +0200718{
719 struct wl_display *display = touch->seat->compositor->wl_display;
720 uint32_t serial;
721 struct wl_resource *resource;
722 struct wl_list *resource_list;
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200723 uint32_t msecs;
Quentin Glidiccde13452016-08-12 10:41:32 +0200724
725 if (!weston_touch_has_focus_resource(touch))
726 return;
727
728 resource_list = &touch->focus_resource_list;
729 serial = wl_display_next_serial(display);
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200730 msecs = timespec_to_msec(time);
Quentin Glidiccde13452016-08-12 10:41:32 +0200731 wl_resource_for_each(resource, resource_list)
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200732 wl_touch_send_up(resource, serial, msecs, touch_id);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400733}
734
Kristian Høgsberge329f362013-05-06 22:19:57 -0400735static void
736default_grab_touch_up(struct weston_touch_grab *grab,
Alexandros Frantzis27a51b82017-11-16 18:20:59 +0200737 const struct timespec *time, int touch_id)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400738{
Quentin Glidiccde13452016-08-12 10:41:32 +0200739 weston_touch_send_up(grab->touch, time, touch_id);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400740}
741
Quentin Glidiccde13452016-08-12 10:41:32 +0200742/** Send wl_touch.motion events to focused resources.
743 *
744 * \param touch The touch where the motion events originates from.
745 * \param time The timestamp of the event
746 * \param touch_id The touch_id value of the event
747 * \param x The x value of the event
748 * \param y The y value of the event
749 *
750 * For every resource that is currently in focus, send a wl_touch.motion event
751 * with the passed parameters. The focused resources are the wl_touch
752 * resources of the client which currently has the surface with touch focus.
753 */
754WL_EXPORT void
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200755weston_touch_send_motion(struct weston_touch *touch,
756 const struct timespec *time, int touch_id,
757 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400758{
Neil Roberts96d790e2013-09-19 17:32:00 +0100759 struct wl_resource *resource;
760 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300761 wl_fixed_t sx, sy;
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200762 uint32_t msecs;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300763
Quentin Glidiccde13452016-08-12 10:41:32 +0200764 if (!weston_touch_has_focus_resource(touch))
765 return;
766
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300767 weston_view_from_global_fixed(touch->focus, x, y, &sx, &sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400768
Neil Roberts96d790e2013-09-19 17:32:00 +0100769 resource_list = &touch->focus_resource_list;
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200770 msecs = timespec_to_msec(time);
Neil Roberts96d790e2013-09-19 17:32:00 +0100771 wl_resource_for_each(resource, resource_list) {
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200772 wl_touch_send_motion(resource, msecs,
Kristian Høgsberge329f362013-05-06 22:19:57 -0400773 touch_id, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400774 }
775}
776
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200777static void
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +0200778default_grab_touch_motion(struct weston_touch_grab *grab,
779 const struct timespec *time, int touch_id,
780 wl_fixed_t x, wl_fixed_t y)
Quentin Glidiccde13452016-08-12 10:41:32 +0200781{
782 weston_touch_send_motion(grab->touch, time, touch_id, x, y);
783}
784
785
786/** Send wl_touch.frame events to focused resources.
787 *
788 * \param touch The touch where the frame events originates from.
789 *
790 * For every resource that is currently in focus, send a wl_touch.frame event.
791 * The focused resources are the wl_touch resources of the client which
792 * currently has the surface with touch focus.
793 */
794WL_EXPORT void
795weston_touch_send_frame(struct weston_touch *touch)
Jonas Ådahl1679f232014-04-12 09:39:51 +0200796{
797 struct wl_resource *resource;
798
Quentin Glidiccde13452016-08-12 10:41:32 +0200799 if (!weston_touch_has_focus_resource(touch))
800 return;
801
802 wl_resource_for_each(resource, &touch->focus_resource_list)
Jonas Ådahl1679f232014-04-12 09:39:51 +0200803 wl_touch_send_frame(resource);
804}
805
806static void
Quentin Glidiccde13452016-08-12 10:41:32 +0200807default_grab_touch_frame(struct weston_touch_grab *grab)
808{
809 weston_touch_send_frame(grab->touch);
810}
811
812static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200813default_grab_touch_cancel(struct weston_touch_grab *grab)
814{
815}
816
Kristian Høgsberge329f362013-05-06 22:19:57 -0400817static const struct weston_touch_grab_interface default_touch_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400818 default_grab_touch_down,
819 default_grab_touch_up,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200820 default_grab_touch_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +0200821 default_grab_touch_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200822 default_grab_touch_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400823};
824
Quentin Glidiccde13452016-08-12 10:41:32 +0200825/** Check if the keyboard has focused resources.
826 *
827 * \param keyboard The keyboard to check for focused resources.
828 * \return Whether or not this keyboard has focused resources
829 */
830WL_EXPORT bool
831weston_keyboard_has_focus_resource(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400832{
Quentin Glidiccde13452016-08-12 10:41:32 +0200833 if (!keyboard->focus)
834 return false;
835
836 if (wl_list_empty(&keyboard->focus_resource_list))
837 return false;
838
839 return true;
840}
841
842/** Send wl_keyboard.key events to focused resources.
843 *
844 * \param keyboard The keyboard where the key events originates from.
845 * \param time The timestamp of the event
846 * \param key The key value of the event
847 * \param state The state enum value of the event
848 *
849 * For every resource that is currently in focus, send a wl_keyboard.key event
850 * with the passed parameters. The focused resources are the wl_keyboard
851 * resources of the client which currently has the surface with keyboard focus.
852 */
853WL_EXPORT void
854weston_keyboard_send_key(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200855 const struct timespec *time, uint32_t key,
Quentin Glidiccde13452016-08-12 10:41:32 +0200856 enum wl_keyboard_key_state state)
857{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400858 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +0100859 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400860 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100861 struct wl_list *resource_list;
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200862 uint32_t msecs;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400863
Quentin Glidiccde13452016-08-12 10:41:32 +0200864 if (!weston_keyboard_has_focus_resource(keyboard))
865 return;
866
Neil Roberts96d790e2013-09-19 17:32:00 +0100867 resource_list = &keyboard->focus_resource_list;
Quentin Glidiccde13452016-08-12 10:41:32 +0200868 serial = wl_display_next_serial(display);
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200869 msecs = timespec_to_msec(time);
Quentin Glidiccde13452016-08-12 10:41:32 +0200870 wl_resource_for_each(resource, resource_list)
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200871 wl_keyboard_send_key(resource, serial, msecs, key, state);
Quentin Glidiccde13452016-08-12 10:41:32 +0200872};
873
874static void
875default_grab_keyboard_key(struct weston_keyboard_grab *grab,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200876 const struct timespec *time, uint32_t key,
877 uint32_t state)
Quentin Glidiccde13452016-08-12 10:41:32 +0200878{
879 weston_keyboard_send_key(grab->keyboard, time, key, state);
Neil Roberts96d790e2013-09-19 17:32:00 +0100880}
881
882static void
883send_modifiers_to_resource(struct weston_keyboard *keyboard,
884 struct wl_resource *resource,
885 uint32_t serial)
886{
887 wl_keyboard_send_modifiers(resource,
888 serial,
889 keyboard->modifiers.mods_depressed,
890 keyboard->modifiers.mods_latched,
891 keyboard->modifiers.mods_locked,
892 keyboard->modifiers.group);
893}
894
895static void
896send_modifiers_to_client_in_list(struct wl_client *client,
897 struct wl_list *list,
898 uint32_t serial,
899 struct weston_keyboard *keyboard)
900{
901 struct wl_resource *resource;
902
903 wl_resource_for_each(resource, list) {
904 if (wl_resource_get_client(resource) == client)
905 send_modifiers_to_resource(keyboard,
906 resource,
907 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400908 }
909}
910
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800911static struct weston_pointer_client *
912find_pointer_client_for_surface(struct weston_pointer *pointer,
913 struct weston_surface *surface)
914{
915 struct wl_client *client;
916
917 if (!surface)
918 return NULL;
919
920 if (!surface->resource)
921 return NULL;
922
923 client = wl_resource_get_client(surface->resource);
924 return weston_pointer_get_pointer_client(pointer, client);
925}
926
927static struct weston_pointer_client *
928find_pointer_client_for_view(struct weston_pointer *pointer, struct weston_view *view)
929{
930 if (!view)
931 return NULL;
932
933 return find_pointer_client_for_surface(pointer, view->surface);
934}
935
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400936static struct wl_resource *
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400937find_resource_for_surface(struct wl_list *list, struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400938{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400939 if (!surface)
940 return NULL;
941
Jason Ekstrand44a38632013-06-14 10:08:00 -0500942 if (!surface->resource)
943 return NULL;
Stefan Schmidtfda26522013-09-17 10:54:09 +0100944
Jason Ekstrand44a38632013-06-14 10:08:00 -0500945 return wl_resource_find_for_client(list, wl_resource_get_client(surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400946}
947
Quentin Glidiccde13452016-08-12 10:41:32 +0200948/** Send wl_keyboard.modifiers events to focused resources and pointer
949 * focused resources.
950 *
951 * \param keyboard The keyboard where the modifiers events originates from.
952 * \param serial The serial of the event
953 * \param mods_depressed The mods_depressed value of the event
954 * \param mods_latched The mods_latched value of the event
955 * \param mods_locked The mods_locked value of the event
956 * \param group The group value of the event
957 *
958 * For every resource that is currently in focus, send a wl_keyboard.modifiers
959 * event with the passed parameters. The focused resources are the wl_keyboard
960 * resources of the client which currently has the surface with keyboard focus.
961 * This also sends wl_keyboard.modifiers events to the wl_keyboard resources of
962 * the client having pointer focus (if different from the keyboard focus client).
963 */
964WL_EXPORT void
965weston_keyboard_send_modifiers(struct weston_keyboard *keyboard,
966 uint32_t serial, uint32_t mods_depressed,
967 uint32_t mods_latched,
968 uint32_t mods_locked, uint32_t group)
969{
970 struct weston_pointer *pointer =
971 weston_seat_get_pointer(keyboard->seat);
972
973 if (weston_keyboard_has_focus_resource(keyboard)) {
974 struct wl_list *resource_list;
975 struct wl_resource *resource;
976
977 resource_list = &keyboard->focus_resource_list;
978 wl_resource_for_each(resource, resource_list) {
979 wl_keyboard_send_modifiers(resource, serial,
980 mods_depressed, mods_latched,
981 mods_locked, group);
982 }
983 }
984
985 if (pointer && pointer->focus && pointer->focus->surface->resource &&
986 pointer->focus->surface != keyboard->focus) {
987 struct wl_client *pointer_client =
988 wl_resource_get_client(pointer->focus->surface->resource);
989
990 send_modifiers_to_client_in_list(pointer_client,
991 &keyboard->resource_list,
992 serial,
993 keyboard);
994 }
995}
996
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400997static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700998default_grab_keyboard_modifiers(struct weston_keyboard_grab *grab,
999 uint32_t serial, uint32_t mods_depressed,
1000 uint32_t mods_latched,
1001 uint32_t mods_locked, uint32_t group)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001002{
Quentin Glidiccde13452016-08-12 10:41:32 +02001003 weston_keyboard_send_modifiers(grab->keyboard, serial, mods_depressed,
1004 mods_latched, mods_locked, group);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001005}
1006
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001007static void
1008default_grab_keyboard_cancel(struct weston_keyboard_grab *grab)
1009{
1010}
1011
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001012static const struct weston_keyboard_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001013 default_keyboard_grab_interface = {
Kristian Høgsbergb27901c2013-10-28 15:32:02 -07001014 default_grab_keyboard_key,
1015 default_grab_keyboard_modifiers,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001016 default_grab_keyboard_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001017};
1018
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001019static void
1020pointer_unmap_sprite(struct weston_pointer *pointer)
1021{
Pekka Paalanenc557ff72014-11-12 16:42:52 +02001022 struct weston_surface *surface = pointer->sprite->surface;
1023
1024 if (weston_surface_is_mapped(surface))
1025 weston_surface_unmap(surface);
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001026
1027 wl_list_remove(&pointer->sprite_destroy_listener.link);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02001028 surface->committed = NULL;
1029 surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03001030 weston_surface_set_label_func(surface, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001031 weston_view_destroy(pointer->sprite);
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001032 pointer->sprite = NULL;
1033}
1034
1035static void
1036pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
1037{
1038 struct weston_pointer *pointer =
1039 container_of(listener, struct weston_pointer,
1040 sprite_destroy_listener);
1041
1042 pointer->sprite = NULL;
1043}
1044
Jonas Ådahl3e12e632013-12-02 22:05:05 +01001045static void
1046weston_pointer_reset_state(struct weston_pointer *pointer)
1047{
1048 pointer->button_count = 0;
1049}
1050
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001051static void
1052weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data);
1053
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001054WL_EXPORT struct weston_pointer *
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001055weston_pointer_create(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001056{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001057 struct weston_pointer *pointer;
1058
Peter Huttererf3d62272013-08-08 11:57:05 +10001059 pointer = zalloc(sizeof *pointer);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001060 if (pointer == NULL)
1061 return NULL;
1062
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001063 wl_list_init(&pointer->pointer_clients);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001064 weston_pointer_set_default_grab(pointer,
1065 seat->compositor->default_pointer_grab);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001066 wl_list_init(&pointer->focus_resource_listener.link);
1067 pointer->focus_resource_listener.notify = pointer_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001068 pointer->default_grab.pointer = pointer;
1069 pointer->grab = &pointer->default_grab;
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001070 wl_signal_init(&pointer->motion_signal);
Emilio Pozuelo Monfortaa7a4762013-11-19 11:37:15 +01001071 wl_signal_init(&pointer->focus_signal);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001072 wl_list_init(&pointer->focus_view_listener.link);
Jonas Ådahl3eb4ddd2016-07-22 17:52:58 +08001073 wl_signal_init(&pointer->destroy_signal);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001074
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001075 pointer->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
1076
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001077 /* FIXME: Pick better co-ords. */
1078 pointer->x = wl_fixed_from_int(100);
1079 pointer->y = wl_fixed_from_int(100);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001080
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001081 pointer->output_destroy_listener.notify =
1082 weston_pointer_handle_output_destroy;
1083 wl_signal_add(&seat->compositor->output_destroyed_signal,
1084 &pointer->output_destroy_listener);
1085
Derek Foremanf9318d12015-05-11 15:40:11 -05001086 pointer->sx = wl_fixed_from_int(-1000000);
1087 pointer->sy = wl_fixed_from_int(-1000000);
1088
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001089 return pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001090}
1091
1092WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001093weston_pointer_destroy(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001094{
Jonas Ådahl3eb4ddd2016-07-22 17:52:58 +08001095 wl_signal_emit(&pointer->destroy_signal, pointer);
1096
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001097 if (pointer->sprite)
1098 pointer_unmap_sprite(pointer);
1099
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001100 /* XXX: What about pointer->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +01001101
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001102 wl_list_remove(&pointer->focus_resource_listener.link);
1103 wl_list_remove(&pointer->focus_view_listener.link);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001104 wl_list_remove(&pointer->output_destroy_listener.link);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001105 free(pointer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001106}
1107
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001108void
1109weston_pointer_set_default_grab(struct weston_pointer *pointer,
1110 const struct weston_pointer_grab_interface *interface)
1111{
1112 if (interface)
1113 pointer->default_grab.interface = interface;
1114 else
1115 pointer->default_grab.interface =
1116 &default_pointer_grab_interface;
1117}
1118
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001119WL_EXPORT struct weston_keyboard *
1120weston_keyboard_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001121{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001122 struct weston_keyboard *keyboard;
1123
Peter Huttererf3d62272013-08-08 11:57:05 +10001124 keyboard = zalloc(sizeof *keyboard);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001125 if (keyboard == NULL)
Neil Roberts96d790e2013-09-19 17:32:00 +01001126 return NULL;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001127
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001128 wl_list_init(&keyboard->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +01001129 wl_list_init(&keyboard->focus_resource_list);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001130 wl_list_init(&keyboard->focus_resource_listener.link);
1131 keyboard->focus_resource_listener.notify = keyboard_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001132 wl_array_init(&keyboard->keys);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001133 keyboard->default_grab.interface = &default_keyboard_grab_interface;
1134 keyboard->default_grab.keyboard = keyboard;
1135 keyboard->grab = &keyboard->default_grab;
1136 wl_signal_init(&keyboard->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001137
1138 return keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001139}
1140
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001141static void
1142weston_xkb_info_destroy(struct weston_xkb_info *xkb_info);
1143
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001144WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001145weston_keyboard_destroy(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001146{
1147 /* XXX: What about keyboard->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +01001148
Derek Foreman185d1582017-06-28 11:17:23 -05001149 xkb_state_unref(keyboard->xkb_state.state);
1150 if (keyboard->xkb_info)
1151 weston_xkb_info_destroy(keyboard->xkb_info);
1152 xkb_keymap_unref(keyboard->pending_keymap);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001153
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001154 wl_array_release(&keyboard->keys);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001155 wl_list_remove(&keyboard->focus_resource_listener.link);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001156 free(keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001157}
1158
Jonas Ådahlcbfa7f72013-12-02 22:05:04 +01001159static void
1160weston_touch_reset_state(struct weston_touch *touch)
1161{
1162 touch->num_tp = 0;
1163}
1164
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001165WL_EXPORT struct weston_touch *
1166weston_touch_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001167{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001168 struct weston_touch *touch;
1169
Peter Huttererf3d62272013-08-08 11:57:05 +10001170 touch = zalloc(sizeof *touch);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001171 if (touch == NULL)
1172 return NULL;
1173
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001174 wl_list_init(&touch->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +01001175 wl_list_init(&touch->focus_resource_list);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001176 wl_list_init(&touch->focus_view_listener.link);
1177 touch->focus_view_listener.notify = touch_focus_view_destroyed;
1178 wl_list_init(&touch->focus_resource_listener.link);
1179 touch->focus_resource_listener.notify = touch_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001180 touch->default_grab.interface = &default_touch_grab_interface;
1181 touch->default_grab.touch = touch;
1182 touch->grab = &touch->default_grab;
1183 wl_signal_init(&touch->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001184
1185 return touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001186}
1187
1188WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001189weston_touch_destroy(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001190{
1191 /* XXX: What about touch->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +01001192
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001193 wl_list_remove(&touch->focus_view_listener.link);
1194 wl_list_remove(&touch->focus_resource_listener.link);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001195 free(touch);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001196}
1197
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001198static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04001199seat_send_updated_caps(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001200{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001201 enum wl_seat_capability caps = 0;
Rob Bradford6e737f52013-09-06 17:48:19 +01001202 struct wl_resource *resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001203
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001204 if (seat->pointer_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001205 caps |= WL_SEAT_CAPABILITY_POINTER;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001206 if (seat->keyboard_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001207 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001208 if (seat->touch_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001209 caps |= WL_SEAT_CAPABILITY_TOUCH;
1210
Rob Bradford6e737f52013-09-06 17:48:19 +01001211 wl_resource_for_each(resource, &seat->base_resource_list) {
1212 wl_seat_send_capabilities(resource, caps);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001213 }
Jason Ekstranda4ab5422014-04-02 19:53:45 -05001214 wl_signal_emit(&seat->updated_caps_signal, seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001215}
1216
Derek Foremanf9318d12015-05-11 15:40:11 -05001217
1218/** Clear the pointer focus
1219 *
1220 * \param pointer the pointer to clear focus for.
1221 *
1222 * This can be used to unset pointer focus and set the co-ordinates to the
1223 * arbitrary values we use for the no focus case.
1224 *
1225 * There's no requirement to use this function. For example, passing the
1226 * results of a weston_compositor_pick_view() directly to
1227 * weston_pointer_set_focus() will do the right thing when no view is found.
1228 */
1229WL_EXPORT void
1230weston_pointer_clear_focus(struct weston_pointer *pointer)
1231{
1232 weston_pointer_set_focus(pointer, NULL,
1233 wl_fixed_from_int(-1000000),
1234 wl_fixed_from_int(-1000000));
1235}
1236
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001237WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001238weston_pointer_set_focus(struct weston_pointer *pointer,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001239 struct weston_view *view,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001240 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001241{
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001242 struct weston_pointer_client *pointer_client;
Derek Foreman1281a362015-07-31 16:55:32 -05001243 struct weston_keyboard *kbd = weston_seat_get_keyboard(pointer->seat);
Neil Roberts96d790e2013-09-19 17:32:00 +01001244 struct wl_resource *resource;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001245 struct wl_resource *surface_resource;
Rob Bradford880ebc72013-07-22 17:31:38 +01001246 struct wl_display *display = pointer->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001247 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01001248 struct wl_list *focus_resource_list;
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001249 int refocus = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001250
1251 if ((!pointer->focus && view) ||
1252 (pointer->focus && !view) ||
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001253 (pointer->focus && pointer->focus->surface != view->surface) ||
1254 pointer->sx != sx || pointer->sy != sy)
1255 refocus = 1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001256
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001257 if (pointer->focus_client && refocus) {
1258 focus_resource_list = &pointer->focus_client->pointer_resources;
1259 if (!wl_list_empty(focus_resource_list)) {
1260 serial = wl_display_next_serial(display);
1261 surface_resource = pointer->focus->surface->resource;
1262 wl_resource_for_each(resource, focus_resource_list) {
1263 wl_pointer_send_leave(resource, serial,
1264 surface_resource);
Peter Hutterer87743e92016-01-18 16:38:22 +10001265 pointer_send_frame(resource);
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001266 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001267 }
1268
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001269 pointer->focus_client = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001270 }
1271
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001272 pointer_client = find_pointer_client_for_view(pointer, view);
1273 if (pointer_client && refocus) {
1274 struct wl_client *surface_client = pointer_client->client;
Neil Roberts96d790e2013-09-19 17:32:00 +01001275
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001276 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001277
Jason Ekstranda7af7042013-10-12 22:38:11 -05001278 if (kbd && kbd->focus != view->surface)
Kristian Høgsbergcb406f12013-10-09 10:54:03 -07001279 send_modifiers_to_client_in_list(surface_client,
1280 &kbd->resource_list,
1281 serial,
1282 kbd);
1283
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001284 pointer->focus_client = pointer_client;
Neil Roberts96d790e2013-09-19 17:32:00 +01001285
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001286 focus_resource_list = &pointer->focus_client->pointer_resources;
Neil Roberts96d790e2013-09-19 17:32:00 +01001287 wl_resource_for_each(resource, focus_resource_list) {
1288 wl_pointer_send_enter(resource,
1289 serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001290 view->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01001291 sx, sy);
Peter Hutterer87743e92016-01-18 16:38:22 +10001292 pointer_send_frame(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001293 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001294
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001295 pointer->focus_serial = serial;
1296 }
1297
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001298 wl_list_remove(&pointer->focus_view_listener.link);
1299 wl_list_init(&pointer->focus_view_listener.link);
1300 wl_list_remove(&pointer->focus_resource_listener.link);
1301 wl_list_init(&pointer->focus_resource_listener.link);
Emilio Pozuelo Monfortaa7a4762013-11-19 11:37:15 +01001302 if (view)
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001303 wl_signal_add(&view->destroy_signal, &pointer->focus_view_listener);
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001304 if (view && view->surface->resource)
1305 wl_resource_add_destroy_listener(view->surface->resource,
1306 &pointer->focus_resource_listener);
1307
1308 pointer->focus = view;
1309 pointer->focus_view_listener.notify = pointer_focus_view_destroyed;
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001310 pointer->sx = sx;
1311 pointer->sy = sy;
1312
Derek Foremanf9318d12015-05-11 15:40:11 -05001313 assert(view || sx == wl_fixed_from_int(-1000000));
1314 assert(view || sy == wl_fixed_from_int(-1000000));
1315
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001316 wl_signal_emit(&pointer->focus_signal, pointer);
1317}
1318
Neil Roberts96d790e2013-09-19 17:32:00 +01001319static void
1320send_enter_to_resource_list(struct wl_list *list,
1321 struct weston_keyboard *keyboard,
1322 struct weston_surface *surface,
1323 uint32_t serial)
1324{
1325 struct wl_resource *resource;
1326
1327 wl_resource_for_each(resource, list) {
1328 send_modifiers_to_resource(keyboard, resource, serial);
1329 wl_keyboard_send_enter(resource, serial,
1330 surface->resource,
1331 &keyboard->keys);
1332 }
1333}
1334
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001335WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001336weston_keyboard_set_focus(struct weston_keyboard *keyboard,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001337 struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001338{
Quentin Glidic85d55542017-07-21 14:02:40 +02001339 struct weston_seat *seat = keyboard->seat;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001340 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +01001341 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001342 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01001343 struct wl_list *focus_resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001344
Neil Roberts96d790e2013-09-19 17:32:00 +01001345 focus_resource_list = &keyboard->focus_resource_list;
1346
1347 if (!wl_list_empty(focus_resource_list) && keyboard->focus != surface) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001348 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001349 wl_resource_for_each(resource, focus_resource_list) {
1350 wl_keyboard_send_leave(resource, serial,
1351 keyboard->focus->resource);
1352 }
1353 move_resources(&keyboard->resource_list, focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001354 }
1355
Neil Roberts96d790e2013-09-19 17:32:00 +01001356 if (find_resource_for_surface(&keyboard->resource_list, surface) &&
1357 keyboard->focus != surface) {
1358 struct wl_client *surface_client =
1359 wl_resource_get_client(surface->resource);
1360
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001361 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001362
1363 move_resources_for_client(focus_resource_list,
1364 &keyboard->resource_list,
1365 surface_client);
1366 send_enter_to_resource_list(focus_resource_list,
1367 keyboard,
1368 surface,
1369 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001370 keyboard->focus_serial = serial;
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001371 }
1372
Quentin Glidic85d55542017-07-21 14:02:40 +02001373 if (seat->saved_kbd_focus) {
1374 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1375 seat->saved_kbd_focus = NULL;
1376 }
1377
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001378 wl_list_remove(&keyboard->focus_resource_listener.link);
1379 wl_list_init(&keyboard->focus_resource_listener.link);
1380 if (surface && surface->resource)
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001381 wl_resource_add_destroy_listener(surface->resource,
1382 &keyboard->focus_resource_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001383
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001384 keyboard->focus = surface;
1385 wl_signal_emit(&keyboard->focus_signal, keyboard);
1386}
1387
Giulio Camuffoa20ca812014-11-22 11:16:56 +02001388/* Users of this function must manually manage the keyboard focus */
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001389WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001390weston_keyboard_start_grab(struct weston_keyboard *keyboard,
1391 struct weston_keyboard_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001392{
1393 keyboard->grab = grab;
1394 grab->keyboard = keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001395}
1396
1397WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001398weston_keyboard_end_grab(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001399{
1400 keyboard->grab = &keyboard->default_grab;
1401}
1402
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001403static void
1404weston_keyboard_cancel_grab(struct weston_keyboard *keyboard)
1405{
1406 keyboard->grab->interface->cancel(keyboard->grab);
1407}
1408
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001409WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001410weston_pointer_start_grab(struct weston_pointer *pointer,
1411 struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001412{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001413 pointer->grab = grab;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001414 grab->pointer = pointer;
Kristian Høgsbergda751b82013-07-04 00:58:07 -04001415 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001416}
1417
1418WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001419weston_pointer_end_grab(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001420{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001421 pointer->grab = &pointer->default_grab;
Kristian Høgsbergda751b82013-07-04 00:58:07 -04001422 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001423}
1424
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001425static void
1426weston_pointer_cancel_grab(struct weston_pointer *pointer)
1427{
1428 pointer->grab->interface->cancel(pointer->grab);
1429}
1430
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001431WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -04001432weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001433{
1434 touch->grab = grab;
1435 grab->touch = touch;
1436}
1437
1438WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -04001439weston_touch_end_grab(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001440{
1441 touch->grab = &touch->default_grab;
1442}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001443
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001444static void
1445weston_touch_cancel_grab(struct weston_touch *touch)
1446{
1447 touch->grab->interface->cancel(touch->grab);
1448}
1449
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001450static void
1451weston_pointer_clamp_for_output(struct weston_pointer *pointer,
1452 struct weston_output *output,
1453 wl_fixed_t *fx, wl_fixed_t *fy)
1454{
1455 int x, y;
1456
1457 x = wl_fixed_to_int(*fx);
1458 y = wl_fixed_to_int(*fy);
1459
1460 if (x < output->x)
1461 *fx = wl_fixed_from_int(output->x);
1462 else if (x >= output->x + output->width)
1463 *fx = wl_fixed_from_int(output->x +
1464 output->width - 1);
1465 if (y < output->y)
1466 *fy = wl_fixed_from_int(output->y);
1467 else if (y >= output->y + output->height)
1468 *fy = wl_fixed_from_int(output->y +
1469 output->height - 1);
1470}
1471
Rob Bradford806d8c02013-06-25 18:56:41 +01001472WL_EXPORT void
1473weston_pointer_clamp(struct weston_pointer *pointer, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001474{
Rob Bradford806d8c02013-06-25 18:56:41 +01001475 struct weston_compositor *ec = pointer->seat->compositor;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001476 struct weston_output *output, *prev = NULL;
1477 int x, y, old_x, old_y, valid = 0;
1478
1479 x = wl_fixed_to_int(*fx);
1480 y = wl_fixed_to_int(*fy);
Rob Bradford806d8c02013-06-25 18:56:41 +01001481 old_x = wl_fixed_to_int(pointer->x);
1482 old_y = wl_fixed_to_int(pointer->y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001483
1484 wl_list_for_each(output, &ec->output_list, link) {
Rob Bradford66bd9f52013-06-25 18:56:42 +01001485 if (pointer->seat->output && pointer->seat->output != output)
1486 continue;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001487 if (pixman_region32_contains_point(&output->region,
1488 x, y, NULL))
1489 valid = 1;
1490 if (pixman_region32_contains_point(&output->region,
1491 old_x, old_y, NULL))
1492 prev = output;
1493 }
1494
Rob Bradford66bd9f52013-06-25 18:56:42 +01001495 if (!prev)
1496 prev = pointer->seat->output;
1497
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001498 if (prev && !valid)
1499 weston_pointer_clamp_for_output(pointer, prev, fx, fy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001500}
1501
Jonas Ådahld2510102014-10-05 21:39:14 +02001502static void
1503weston_pointer_move_to(struct weston_pointer *pointer,
1504 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001505{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001506 int32_t ix, iy;
1507
Rob Bradford806d8c02013-06-25 18:56:41 +01001508 weston_pointer_clamp (pointer, &x, &y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001509
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001510 pointer->x = x;
1511 pointer->y = y;
1512
1513 ix = wl_fixed_to_int(x);
1514 iy = wl_fixed_to_int(y);
1515
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001516 if (pointer->sprite) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001517 weston_view_set_position(pointer->sprite,
1518 ix - pointer->hotspot_x,
1519 iy - pointer->hotspot_y);
1520 weston_view_schedule_repaint(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001521 }
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001522
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001523 pointer->grab->interface->focus(pointer->grab);
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001524 wl_signal_emit(&pointer->motion_signal, pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001525}
1526
Jonas Ådahld2510102014-10-05 21:39:14 +02001527WL_EXPORT void
Jonas Ådahld2510102014-10-05 21:39:14 +02001528weston_pointer_move(struct weston_pointer *pointer,
1529 struct weston_pointer_motion_event *event)
1530{
1531 wl_fixed_t x, y;
1532
1533 weston_pointer_motion_to_abs(pointer, event, &x, &y);
1534 weston_pointer_move_to(pointer, x, y);
1535}
1536
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001537/** Verify if the pointer is in a valid position and move it if it isn't.
1538 */
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001539static void
1540weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001541{
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001542 struct weston_pointer *pointer;
1543 struct weston_compositor *ec;
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001544 struct weston_output *output, *closest = NULL;
1545 int x, y, distance, min = INT_MAX;
1546 wl_fixed_t fx, fy;
1547
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001548 pointer = container_of(listener, struct weston_pointer,
1549 output_destroy_listener);
1550 ec = pointer->seat->compositor;
1551
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001552 x = wl_fixed_to_int(pointer->x);
1553 y = wl_fixed_to_int(pointer->y);
1554
1555 wl_list_for_each(output, &ec->output_list, link) {
1556 if (pixman_region32_contains_point(&output->region,
1557 x, y, NULL))
1558 return;
1559
1560 /* Aproximante the distance from the pointer to the center of
1561 * the output. */
1562 distance = abs(output->x + output->width / 2 - x) +
1563 abs(output->y + output->height / 2 - y);
1564 if (distance < min) {
1565 min = distance;
1566 closest = output;
1567 }
1568 }
1569
1570 /* Nothing to do if there's no output left. */
1571 if (!closest)
1572 return;
1573
1574 fx = pointer->x;
1575 fy = pointer->y;
1576
1577 weston_pointer_clamp_for_output(pointer, closest, &fx, &fy);
Jonas Ådahld2510102014-10-05 21:39:14 +02001578 weston_pointer_move_to(pointer, fx, fy);
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001579}
1580
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001581WL_EXPORT void
1582notify_motion(struct weston_seat *seat,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001583 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001584 struct weston_pointer_motion_event *event)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001585{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001586 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001587 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001588
1589 weston_compositor_wake(ec);
Jonas Ådahld2510102014-10-05 21:39:14 +02001590 pointer->grab->interface->motion(pointer->grab, time, event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001591}
1592
Daniel Stone96d47c02013-11-19 11:37:12 +01001593static void
1594run_modifier_bindings(struct weston_seat *seat, uint32_t old, uint32_t new)
1595{
1596 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001597 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Daniel Stone96d47c02013-11-19 11:37:12 +01001598 uint32_t diff;
1599 unsigned int i;
1600 struct {
1601 uint32_t xkb;
1602 enum weston_keyboard_modifier weston;
1603 } mods[] = {
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001604 { keyboard->xkb_info->ctrl_mod, MODIFIER_CTRL },
1605 { keyboard->xkb_info->alt_mod, MODIFIER_ALT },
1606 { keyboard->xkb_info->super_mod, MODIFIER_SUPER },
1607 { keyboard->xkb_info->shift_mod, MODIFIER_SHIFT },
Daniel Stone96d47c02013-11-19 11:37:12 +01001608 };
1609
1610 diff = new & ~old;
1611 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
1612 if (diff & (1 << mods[i].xkb))
1613 weston_compositor_run_modifier_binding(compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001614 keyboard,
Daniel Stone96d47c02013-11-19 11:37:12 +01001615 mods[i].weston,
1616 WL_KEYBOARD_KEY_STATE_PRESSED);
1617 }
1618
1619 diff = old & ~new;
1620 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
1621 if (diff & (1 << mods[i].xkb))
1622 weston_compositor_run_modifier_binding(compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001623 keyboard,
Daniel Stone96d47c02013-11-19 11:37:12 +01001624 mods[i].weston,
1625 WL_KEYBOARD_KEY_STATE_RELEASED);
1626 }
1627}
1628
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001629WL_EXPORT void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001630notify_motion_absolute(struct weston_seat *seat, const struct timespec *time,
1631 double x, double y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001632{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001633 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001634 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Jonas Ådahld2510102014-10-05 21:39:14 +02001635 struct weston_pointer_motion_event event = { 0 };
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001636
1637 weston_compositor_wake(ec);
Jonas Ådahld2510102014-10-05 21:39:14 +02001638
1639 event = (struct weston_pointer_motion_event) {
1640 .mask = WESTON_POINTER_MOTION_ABS,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001641 .x = x,
1642 .y = y,
Jonas Ådahld2510102014-10-05 21:39:14 +02001643 };
1644
1645 pointer->grab->interface->motion(pointer->grab, time, &event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001646}
1647
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02001648static unsigned int
1649peek_next_activate_serial(struct weston_compositor *c)
1650{
1651 unsigned serial = c->activate_serial + 1;
1652
1653 return serial == 0 ? 1 : serial;
1654}
1655
1656static void
1657inc_activate_serial(struct weston_compositor *c)
1658{
1659 c->activate_serial = peek_next_activate_serial (c);
1660}
1661
1662WL_EXPORT void
1663weston_view_activate(struct weston_view *view,
1664 struct weston_seat *seat,
1665 uint32_t flags)
1666{
1667 struct weston_compositor *compositor = seat->compositor;
1668
1669 if (flags & WESTON_ACTIVATE_FLAG_CLICKED) {
1670 view->click_to_activate_serial =
1671 peek_next_activate_serial(compositor);
1672 }
1673
1674 weston_seat_set_keyboard_focus(seat, view->surface);
1675}
1676
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001677WL_EXPORT void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001678notify_button(struct weston_seat *seat, const struct timespec *time,
1679 int32_t button, enum wl_pointer_button_state state)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001680{
1681 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001682 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001683
1684 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001685 weston_compositor_idle_inhibit(compositor);
1686 if (pointer->button_count == 0) {
1687 pointer->grab_button = button;
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001688 pointer->grab_time = *time;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001689 pointer->grab_x = pointer->x;
1690 pointer->grab_y = pointer->y;
1691 }
1692 pointer->button_count++;
1693 } else {
1694 weston_compositor_idle_release(compositor);
1695 pointer->button_count--;
1696 }
1697
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001698 weston_compositor_run_button_binding(compositor, pointer, time, button,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001699 state);
1700
1701 pointer->grab->interface->button(pointer->grab, time, button, state);
1702
1703 if (pointer->button_count == 1)
1704 pointer->grab_serial =
1705 wl_display_get_serial(compositor->wl_display);
1706}
1707
1708WL_EXPORT void
Alexandros Frantzis80321942017-11-16 18:20:56 +02001709notify_axis(struct weston_seat *seat, const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001710 struct weston_pointer_axis_event *event)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001711{
1712 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001713 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001714
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001715 weston_compositor_wake(compositor);
1716
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001717 if (weston_compositor_run_axis_binding(compositor, pointer,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001718 time, event))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001719 return;
1720
Peter Hutterer89b6a492016-01-18 15:58:17 +10001721 pointer->grab->interface->axis(pointer->grab, time, event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001722}
1723
Peter Hutterer87743e92016-01-18 16:38:22 +10001724WL_EXPORT void
1725notify_axis_source(struct weston_seat *seat, uint32_t source)
1726{
1727 struct weston_compositor *compositor = seat->compositor;
1728 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1729
1730 weston_compositor_wake(compositor);
1731
1732 pointer->grab->interface->axis_source(pointer->grab, source);
1733}
1734
1735WL_EXPORT void
1736notify_pointer_frame(struct weston_seat *seat)
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->frame(pointer->grab);
1744}
1745
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001746WL_EXPORT int
1747weston_keyboard_set_locks(struct weston_keyboard *keyboard,
1748 uint32_t mask, uint32_t value)
1749{
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001750 uint32_t serial;
1751 xkb_mod_mask_t mods_depressed, mods_latched, mods_locked, group;
1752 xkb_mod_mask_t num, caps;
1753
1754 /* We don't want the leds to go out of sync with the actual state
1755 * so if the backend has no way to change the leds don't try to
1756 * change the state */
1757 if (!keyboard->seat->led_update)
1758 return -1;
1759
1760 mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
1761 XKB_STATE_DEPRESSED);
1762 mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
1763 XKB_STATE_LATCHED);
1764 mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
1765 XKB_STATE_LOCKED);
1766 group = xkb_state_serialize_group(keyboard->xkb_state.state,
1767 XKB_STATE_EFFECTIVE);
1768
1769 num = (1 << keyboard->xkb_info->mod2_mod);
1770 caps = (1 << keyboard->xkb_info->caps_mod);
1771 if (mask & WESTON_NUM_LOCK) {
1772 if (value & WESTON_NUM_LOCK)
1773 mods_locked |= num;
1774 else
1775 mods_locked &= ~num;
1776 }
1777 if (mask & WESTON_CAPS_LOCK) {
1778 if (value & WESTON_CAPS_LOCK)
1779 mods_locked |= caps;
1780 else
1781 mods_locked &= ~caps;
1782 }
1783
1784 xkb_state_update_mask(keyboard->xkb_state.state, mods_depressed,
1785 mods_latched, mods_locked, 0, 0, group);
1786
1787 serial = wl_display_next_serial(
1788 keyboard->seat->compositor->wl_display);
1789 notify_modifiers(keyboard->seat, serial);
1790
1791 return 0;
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001792}
1793
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001794WL_EXPORT void
1795notify_modifiers(struct weston_seat *seat, uint32_t serial)
1796{
Derek Foreman1281a362015-07-31 16:55:32 -05001797 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001798 struct weston_keyboard_grab *grab = keyboard->grab;
1799 uint32_t mods_depressed, mods_latched, mods_locked, group;
1800 uint32_t mods_lookup;
1801 enum weston_led leds = 0;
1802 int changed = 0;
1803
1804 /* Serialize and update our internal state, checking to see if it's
1805 * different to the previous state. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001806 mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03001807 XKB_STATE_MODS_DEPRESSED);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001808 mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03001809 XKB_STATE_MODS_LATCHED);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001810 mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03001811 XKB_STATE_MODS_LOCKED);
1812 group = xkb_state_serialize_layout(keyboard->xkb_state.state,
1813 XKB_STATE_LAYOUT_EFFECTIVE);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001814
Derek Foreman244e99e2015-06-03 15:53:26 -05001815 if (mods_depressed != keyboard->modifiers.mods_depressed ||
1816 mods_latched != keyboard->modifiers.mods_latched ||
1817 mods_locked != keyboard->modifiers.mods_locked ||
1818 group != keyboard->modifiers.group)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001819 changed = 1;
1820
Derek Foreman244e99e2015-06-03 15:53:26 -05001821 run_modifier_bindings(seat, keyboard->modifiers.mods_depressed,
Daniel Stone96d47c02013-11-19 11:37:12 +01001822 mods_depressed);
1823
Derek Foreman244e99e2015-06-03 15:53:26 -05001824 keyboard->modifiers.mods_depressed = mods_depressed;
1825 keyboard->modifiers.mods_latched = mods_latched;
1826 keyboard->modifiers.mods_locked = mods_locked;
1827 keyboard->modifiers.group = group;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001828
1829 /* And update the modifier_state for bindings. */
1830 mods_lookup = mods_depressed | mods_latched;
1831 seat->modifier_state = 0;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001832 if (mods_lookup & (1 << keyboard->xkb_info->ctrl_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001833 seat->modifier_state |= MODIFIER_CTRL;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001834 if (mods_lookup & (1 << keyboard->xkb_info->alt_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001835 seat->modifier_state |= MODIFIER_ALT;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001836 if (mods_lookup & (1 << keyboard->xkb_info->super_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001837 seat->modifier_state |= MODIFIER_SUPER;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001838 if (mods_lookup & (1 << keyboard->xkb_info->shift_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001839 seat->modifier_state |= MODIFIER_SHIFT;
1840
1841 /* Finally, notify the compositor that LEDs have changed. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001842 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
1843 keyboard->xkb_info->num_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001844 leds |= LED_NUM_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001845 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
1846 keyboard->xkb_info->caps_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001847 leds |= LED_CAPS_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001848 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
1849 keyboard->xkb_info->scroll_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001850 leds |= LED_SCROLL_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001851 if (leds != keyboard->xkb_state.leds && seat->led_update)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001852 seat->led_update(seat, leds);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001853 keyboard->xkb_state.leds = leds;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001854
1855 if (changed) {
1856 grab->interface->modifiers(grab,
1857 serial,
1858 keyboard->modifiers.mods_depressed,
1859 keyboard->modifiers.mods_latched,
1860 keyboard->modifiers.mods_locked,
1861 keyboard->modifiers.group);
1862 }
1863}
1864
1865static void
1866update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
1867 enum wl_keyboard_key_state state)
1868{
Derek Foreman1281a362015-07-31 16:55:32 -05001869 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001870 enum xkb_key_direction direction;
1871
1872 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1873 direction = XKB_KEY_DOWN;
1874 else
1875 direction = XKB_KEY_UP;
1876
1877 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
1878 * broken keycode system, which starts at 8. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001879 xkb_state_update_key(keyboard->xkb_state.state, key + 8, direction);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001880
1881 notify_modifiers(seat, serial);
1882}
Rui Matos65196bc2013-10-10 19:44:19 +02001883
1884static void
1885send_keymap(struct wl_resource *resource, struct weston_xkb_info *xkb_info)
1886{
1887 wl_keyboard_send_keymap(resource,
1888 WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
1889 xkb_info->keymap_fd,
1890 xkb_info->keymap_size);
1891}
1892
1893static void
1894send_modifiers(struct wl_resource *resource, uint32_t serial, struct weston_keyboard *keyboard)
1895{
1896 wl_keyboard_send_modifiers(resource, serial,
1897 keyboard->modifiers.mods_depressed,
1898 keyboard->modifiers.mods_latched,
1899 keyboard->modifiers.mods_locked,
1900 keyboard->modifiers.group);
1901}
1902
1903static struct weston_xkb_info *
1904weston_xkb_info_create(struct xkb_keymap *keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02001905
1906static void
1907update_keymap(struct weston_seat *seat)
1908{
Derek Foreman1281a362015-07-31 16:55:32 -05001909 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Rui Matos65196bc2013-10-10 19:44:19 +02001910 struct wl_resource *resource;
1911 struct weston_xkb_info *xkb_info;
1912 struct xkb_state *state;
1913 xkb_mod_mask_t latched_mods;
1914 xkb_mod_mask_t locked_mods;
1915
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001916 xkb_info = weston_xkb_info_create(keyboard->pending_keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02001917
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001918 xkb_keymap_unref(keyboard->pending_keymap);
1919 keyboard->pending_keymap = NULL;
Rui Matos65196bc2013-10-10 19:44:19 +02001920
1921 if (!xkb_info) {
1922 weston_log("failed to create XKB info\n");
1923 return;
1924 }
1925
1926 state = xkb_state_new(xkb_info->keymap);
1927 if (!state) {
1928 weston_log("failed to initialise XKB state\n");
1929 weston_xkb_info_destroy(xkb_info);
1930 return;
1931 }
1932
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001933 latched_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
1934 XKB_STATE_MODS_LATCHED);
1935 locked_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
1936 XKB_STATE_MODS_LOCKED);
Rui Matos65196bc2013-10-10 19:44:19 +02001937 xkb_state_update_mask(state,
1938 0, /* depressed */
1939 latched_mods,
1940 locked_mods,
1941 0, 0, 0);
1942
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001943 weston_xkb_info_destroy(keyboard->xkb_info);
1944 keyboard->xkb_info = xkb_info;
Rui Matos65196bc2013-10-10 19:44:19 +02001945
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001946 xkb_state_unref(keyboard->xkb_state.state);
1947 keyboard->xkb_state.state = state;
Rui Matos65196bc2013-10-10 19:44:19 +02001948
Derek Foremanbc91e542015-06-03 15:53:27 -05001949 wl_resource_for_each(resource, &keyboard->resource_list)
Rui Matos65196bc2013-10-10 19:44:19 +02001950 send_keymap(resource, xkb_info);
Derek Foremanbc91e542015-06-03 15:53:27 -05001951 wl_resource_for_each(resource, &keyboard->focus_resource_list)
Rui Matos65196bc2013-10-10 19:44:19 +02001952 send_keymap(resource, xkb_info);
1953
1954 notify_modifiers(seat, wl_display_next_serial(seat->compositor->wl_display));
1955
1956 if (!latched_mods && !locked_mods)
1957 return;
1958
Derek Foremanbc91e542015-06-03 15:53:27 -05001959 wl_resource_for_each(resource, &keyboard->resource_list)
1960 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), keyboard);
1961 wl_resource_for_each(resource, &keyboard->focus_resource_list)
1962 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), keyboard);
Rui Matos65196bc2013-10-10 19:44:19 +02001963}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001964
1965WL_EXPORT void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02001966notify_key(struct weston_seat *seat, const struct timespec *time, uint32_t key,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001967 enum wl_keyboard_key_state state,
1968 enum weston_key_state_update update_state)
1969{
1970 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001971 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001972 struct weston_keyboard_grab *grab = keyboard->grab;
Pekka Paalanen86b53962014-11-19 13:43:32 +02001973 uint32_t *k, *end;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001974
1975 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001976 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001977 } else {
1978 weston_compositor_idle_release(compositor);
1979 }
1980
Pekka Paalanen86b53962014-11-19 13:43:32 +02001981 end = keyboard->keys.data + keyboard->keys.size;
1982 for (k = keyboard->keys.data; k < end; k++) {
1983 if (*k == key) {
1984 /* Ignore server-generated repeats. */
1985 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1986 return;
1987 *k = *--end;
1988 }
1989 }
1990 keyboard->keys.size = (void *) end - keyboard->keys.data;
1991 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1992 k = wl_array_add(&keyboard->keys, sizeof *k);
1993 *k = key;
1994 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001995
1996 if (grab == &keyboard->default_grab ||
1997 grab == &keyboard->input_method_grab) {
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001998 weston_compositor_run_key_binding(compositor, keyboard, time,
1999 key, state);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002000 grab = keyboard->grab;
2001 }
2002
2003 grab->interface->key(grab, time, key, state);
2004
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002005 if (keyboard->pending_keymap &&
Pekka Paalanen86b53962014-11-19 13:43:32 +02002006 keyboard->keys.size == 0)
Rui Matos65196bc2013-10-10 19:44:19 +02002007 update_keymap(seat);
2008
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002009 if (update_state == STATE_UPDATE_AUTOMATIC) {
2010 update_modifier_state(seat,
2011 wl_display_get_serial(compositor->wl_display),
2012 key,
2013 state);
2014 }
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002015
Olivier Fourdandf84dbe2016-06-30 16:01:56 +02002016 keyboard->grab_serial = wl_display_get_serial(compositor->wl_display);
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002017 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02002018 keyboard->grab_time = *time;
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002019 keyboard->grab_key = key;
2020 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002021}
2022
2023WL_EXPORT void
2024notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002025 double x, double y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002026{
Derek Foreman1281a362015-07-31 16:55:32 -05002027 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2028
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002029 if (output) {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002030 weston_pointer_move_to(pointer,
2031 wl_fixed_from_double(x),
2032 wl_fixed_from_double(y));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002033 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002034 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002035 * NULL) here, but somehow that breaks re-entry... */
2036 }
2037}
2038
2039static void
2040destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
2041{
2042 struct weston_seat *ws;
2043
2044 ws = container_of(listener, struct weston_seat,
2045 saved_kbd_focus_listener);
2046
2047 ws->saved_kbd_focus = NULL;
2048}
2049
2050WL_EXPORT void
2051notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
2052 enum weston_key_state_update update_state)
2053{
2054 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002055 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04002056 struct weston_surface *surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002057 uint32_t *k, serial;
2058
2059 serial = wl_display_next_serial(compositor->wl_display);
2060 wl_array_copy(&keyboard->keys, keys);
2061 wl_array_for_each(k, &keyboard->keys) {
2062 weston_compositor_idle_inhibit(compositor);
2063 if (update_state == STATE_UPDATE_AUTOMATIC)
2064 update_modifier_state(seat, serial, *k,
2065 WL_KEYBOARD_KEY_STATE_PRESSED);
2066 }
2067
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002068 surface = seat->saved_kbd_focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002069 if (surface) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002070 weston_keyboard_set_focus(keyboard, surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002071 }
2072}
2073
2074WL_EXPORT void
2075notify_keyboard_focus_out(struct weston_seat *seat)
2076{
2077 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002078 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2079 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Quentin Glidic85d55542017-07-21 14:02:40 +02002080 struct weston_surface *focus = keyboard->focus;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002081 uint32_t *k, serial;
2082
2083 serial = wl_display_next_serial(compositor->wl_display);
2084 wl_array_for_each(k, &keyboard->keys) {
2085 weston_compositor_idle_release(compositor);
2086 update_modifier_state(seat, serial, *k,
2087 WL_KEYBOARD_KEY_STATE_RELEASED);
2088 }
2089
2090 seat->modifier_state = 0;
2091
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002092 weston_keyboard_set_focus(keyboard, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002093 weston_keyboard_cancel_grab(keyboard);
Derek Foreman1281a362015-07-31 16:55:32 -05002094 if (pointer)
2095 weston_pointer_cancel_grab(pointer);
Quentin Glidic85d55542017-07-21 14:02:40 +02002096
2097 if (focus) {
2098 seat->saved_kbd_focus = focus;
2099 seat->saved_kbd_focus_listener.notify =
2100 destroy_device_saved_kbd_focus;
2101 wl_signal_add(&focus->destroy_signal,
2102 &seat->saved_kbd_focus_listener);
2103 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002104}
2105
Michael Fua2bb7912013-07-23 15:51:06 +08002106WL_EXPORT void
Derek Foreman4c93c082015-04-30 16:45:41 -05002107weston_touch_set_focus(struct weston_touch *touch, struct weston_view *view)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002108{
Neil Roberts96d790e2013-09-19 17:32:00 +01002109 struct wl_list *focus_resource_list;
2110
Derek Foreman4c93c082015-04-30 16:45:41 -05002111 focus_resource_list = &touch->focus_resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002112
Derek Foreman4c93c082015-04-30 16:45:41 -05002113 if (view && touch->focus &&
2114 touch->focus->surface == view->surface) {
2115 touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002116 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002117 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002118
Derek Foreman4c93c082015-04-30 16:45:41 -05002119 wl_list_remove(&touch->focus_resource_listener.link);
2120 wl_list_init(&touch->focus_resource_listener.link);
2121 wl_list_remove(&touch->focus_view_listener.link);
2122 wl_list_init(&touch->focus_view_listener.link);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01002123
Neil Roberts96d790e2013-09-19 17:32:00 +01002124 if (!wl_list_empty(focus_resource_list)) {
Derek Foreman4c93c082015-04-30 16:45:41 -05002125 move_resources(&touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002126 focus_resource_list);
2127 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002128
Jason Ekstranda7af7042013-10-12 22:38:11 -05002129 if (view) {
Derek Foreman362656b2014-09-04 10:23:05 -05002130 struct wl_client *surface_client;
2131
2132 if (!view->surface->resource) {
Derek Foreman4c93c082015-04-30 16:45:41 -05002133 touch->focus = NULL;
Derek Foreman362656b2014-09-04 10:23:05 -05002134 return;
2135 }
2136
2137 surface_client = wl_resource_get_client(view->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +01002138 move_resources_for_client(focus_resource_list,
Derek Foreman4c93c082015-04-30 16:45:41 -05002139 &touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002140 surface_client);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01002141 wl_resource_add_destroy_listener(view->surface->resource,
Derek Foreman4c93c082015-04-30 16:45:41 -05002142 &touch->focus_resource_listener);
2143 wl_signal_add(&view->destroy_signal, &touch->focus_view_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002144 }
Derek Foreman4c93c082015-04-30 16:45:41 -05002145 touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002146}
2147
2148/**
2149 * notify_touch - emulates button touches and notifies surfaces accordingly.
2150 *
2151 * It assumes always the correct cycle sequence until it gets here: touch_down
2152 * → touch_update → ... → touch_update → touch_end. The driver is responsible
2153 * for sending along such order.
2154 *
2155 */
2156WL_EXPORT void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02002157notify_touch(struct weston_seat *seat, const struct timespec *time,
2158 int touch_id, double double_x, double double_y, int touch_type)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002159{
2160 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002161 struct weston_touch *touch = weston_seat_get_touch(seat);
Kristian Høgsberge329f362013-05-06 22:19:57 -04002162 struct weston_touch_grab *grab = touch->grab;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002163 struct weston_view *ev;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002164 wl_fixed_t sx, sy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002165 wl_fixed_t x = wl_fixed_from_double(double_x);
2166 wl_fixed_t y = wl_fixed_from_double(double_y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002167
2168 /* Update grab's global coordinates. */
Neil Roberts306fe082013-10-03 16:43:06 +01002169 if (touch_id == touch->grab_touch_id && touch_type != WL_TOUCH_UP) {
2170 touch->grab_x = x;
2171 touch->grab_y = y;
2172 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002173
2174 switch (touch_type) {
2175 case WL_TOUCH_DOWN:
2176 weston_compositor_idle_inhibit(ec);
2177
Jonas Ådahl9484b692013-12-02 22:05:03 +01002178 touch->num_tp++;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002179
Jason Ekstranda7af7042013-10-12 22:38:11 -05002180 /* the first finger down picks the view, and all further go
2181 * to that view for the remainder of the touch session i.e.
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002182 * until all touch points are up again. */
Jonas Ådahl9484b692013-12-02 22:05:03 +01002183 if (touch->num_tp == 1) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002184 ev = weston_compositor_pick_view(ec, x, y, &sx, &sy);
Derek Foreman4c93c082015-04-30 16:45:41 -05002185 weston_touch_set_focus(touch, ev);
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002186 } else if (!touch->focus) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002187 /* Unexpected condition: We have non-initial touch but
2188 * there is no focused surface.
2189 */
Chris Michael3f607d32015-10-07 11:59:49 -04002190 weston_log("touch event received with %d points down "
Jonas Ådahl9484b692013-12-02 22:05:03 +01002191 "but no surface focused\n", touch->num_tp);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002192 return;
2193 }
2194
Derek Foreman99a6a2d2015-07-15 13:00:43 -05002195 weston_compositor_run_touch_binding(ec, touch,
Kristian Høgsbergc8964012014-02-05 14:25:18 -08002196 time, touch_type);
2197
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002198 grab->interface->down(grab, time, touch_id, x, y);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002199 if (touch->num_tp == 1) {
Rusty Lynchf1407ff2013-08-08 21:13:57 -07002200 touch->grab_serial =
2201 wl_display_get_serial(ec->wl_display);
Neil Roberts306fe082013-10-03 16:43:06 +01002202 touch->grab_touch_id = touch_id;
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02002203 touch->grab_time = *time;
Rusty Lynchf1407ff2013-08-08 21:13:57 -07002204 touch->grab_x = x;
2205 touch->grab_y = y;
2206 }
2207
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002208 break;
2209 case WL_TOUCH_MOTION:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002210 ev = touch->focus;
2211 if (!ev)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002212 break;
2213
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +02002214 grab->interface->motion(grab, time, touch_id, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002215 break;
2216 case WL_TOUCH_UP:
Kristian Høgsberga30e29a2014-01-08 22:29:20 -08002217 if (touch->num_tp == 0) {
2218 /* This can happen if we start out with one or
2219 * more fingers on the touch screen, in which
2220 * case we didn't get the corresponding down
2221 * event. */
2222 weston_log("unmatched touch up event\n");
2223 break;
2224 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002225 weston_compositor_idle_release(ec);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002226 touch->num_tp--;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002227
Alexandros Frantzis27a51b82017-11-16 18:20:59 +02002228 grab->interface->up(grab, time, touch_id);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002229 if (touch->num_tp == 0)
Derek Foreman4c93c082015-04-30 16:45:41 -05002230 weston_touch_set_focus(touch, NULL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002231 break;
2232 }
2233}
2234
Jonas Ådahl1679f232014-04-12 09:39:51 +02002235WL_EXPORT void
2236notify_touch_frame(struct weston_seat *seat)
2237{
Derek Foreman1281a362015-07-31 16:55:32 -05002238 struct weston_touch *touch = weston_seat_get_touch(seat);
Jonas Ådahl1679f232014-04-12 09:39:51 +02002239 struct weston_touch_grab *grab = touch->grab;
2240
2241 grab->interface->frame(grab);
2242}
2243
Derek Foreman3cc004a2015-11-06 15:56:09 -06002244WL_EXPORT void
2245notify_touch_cancel(struct weston_seat *seat)
2246{
2247 struct weston_touch *touch = weston_seat_get_touch(seat);
2248 struct weston_touch_grab *grab = touch->grab;
2249
2250 grab->interface->cancel(grab);
2251}
2252
Pekka Paalanen8274d902014-08-06 19:36:51 +03002253static int
2254pointer_cursor_surface_get_label(struct weston_surface *surface,
2255 char *buf, size_t len)
2256{
2257 return snprintf(buf, len, "cursor");
2258}
2259
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002260static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002261pointer_cursor_surface_committed(struct weston_surface *es,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002262 int32_t dx, int32_t dy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002263{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002264 struct weston_pointer *pointer = es->committed_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002265 int x, y;
2266
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002267 if (es->width == 0)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002268 return;
2269
Jason Ekstranda7af7042013-10-12 22:38:11 -05002270 assert(es == pointer->sprite->surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002271
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002272 pointer->hotspot_x -= dx;
2273 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002274
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002275 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
2276 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002277
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002278 weston_view_set_position(pointer->sprite, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002279
2280 empty_region(&es->pending.input);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02002281 empty_region(&es->input);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002282
2283 if (!weston_surface_is_mapped(es)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002284 weston_layer_entry_insert(&es->compositor->cursor_layer.view_list,
2285 &pointer->sprite->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002286 weston_view_update_transform(pointer->sprite);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002287 es->is_mapped = true;
2288 pointer->sprite->is_mapped = true;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002289 }
2290}
2291
2292static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002293pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2294 uint32_t serial, struct wl_resource *surface_resource,
2295 int32_t x, int32_t y)
2296{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002297 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002298 struct weston_surface *surface = NULL;
2299
2300 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002301 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002302
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002303 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002304 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002305 /* pointer->focus->surface->resource can be NULL. Surfaces like the
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02002306 black_surface used in shell.c for fullscreen don't have
2307 a resource, but can still have focus */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002308 if (pointer->focus->surface->resource == NULL)
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02002309 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002310 if (wl_resource_get_client(pointer->focus->surface->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002311 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002312 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002313 return;
2314
Derek Foreman4e53c532015-03-23 10:55:32 -05002315 if (!surface) {
2316 if (pointer->sprite)
2317 pointer_unmap_sprite(pointer);
2318 return;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002319 }
2320
Jonas Ådahlb4070242015-03-18 15:08:03 +08002321 if (pointer->sprite && pointer->sprite->surface == surface &&
2322 pointer->hotspot_x == x && pointer->hotspot_y == y)
2323 return;
2324
Derek Foreman4e53c532015-03-23 10:55:32 -05002325 if (!pointer->sprite || pointer->sprite->surface != surface) {
2326 if (weston_surface_set_role(surface, "wl_pointer-cursor",
2327 resource,
2328 WL_POINTER_ERROR_ROLE) < 0)
2329 return;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002330
Derek Foreman4e53c532015-03-23 10:55:32 -05002331 if (pointer->sprite)
2332 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002333
Derek Foreman4e53c532015-03-23 10:55:32 -05002334 wl_signal_add(&surface->destroy_signal,
2335 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002336
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002337 surface->committed = pointer_cursor_surface_committed;
2338 surface->committed_private = pointer;
Derek Foreman4e53c532015-03-23 10:55:32 -05002339 weston_surface_set_label_func(surface,
2340 pointer_cursor_surface_get_label);
2341 pointer->sprite = weston_view_create(surface);
2342 }
2343
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002344 pointer->hotspot_x = x;
2345 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002346
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +02002347 if (surface->buffer_ref.buffer) {
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002348 pointer_cursor_surface_committed(surface, 0, 0);
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +02002349 weston_view_schedule_repaint(pointer->sprite);
2350 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002351}
2352
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002353static void
2354pointer_release(struct wl_client *client, struct wl_resource *resource)
2355{
2356 wl_resource_destroy(resource);
2357}
2358
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002359static const struct wl_pointer_interface pointer_interface = {
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002360 pointer_set_cursor,
2361 pointer_release
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002362};
2363
2364static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002365seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2366 uint32_t id)
2367{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002368 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002369 /* We use the pointer_state directly, which means we'll
2370 * give a wl_pointer if the seat has ever had one - even though
2371 * the spec explicitly states that this request only takes effect
2372 * if the seat has the pointer capability.
2373 *
2374 * This prevents a race between the compositor sending new
2375 * capabilities and the client trying to use the old ones.
2376 */
2377 struct weston_pointer *pointer = seat->pointer_state;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002378 struct wl_resource *cr;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002379 struct weston_pointer_client *pointer_client;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002380
Derek Foreman1281a362015-07-31 16:55:32 -05002381 if (!pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002382 return;
2383
Jason Ekstranda85118c2013-06-27 20:17:02 -05002384 cr = wl_resource_create(client, &wl_pointer_interface,
2385 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002386 if (cr == NULL) {
2387 wl_client_post_no_memory(client);
2388 return;
2389 }
2390
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002391 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
2392 if (!pointer_client) {
2393 wl_client_post_no_memory(client);
2394 return;
2395 }
2396
2397 wl_list_insert(&pointer_client->pointer_resources,
2398 wl_resource_get_link(cr));
Derek Foreman1281a362015-07-31 16:55:32 -05002399 wl_resource_set_implementation(cr, &pointer_interface, pointer,
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002400 unbind_pointer_client_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002401
Derek Foreman1281a362015-07-31 16:55:32 -05002402 if (pointer->focus && pointer->focus->surface->resource &&
2403 wl_resource_get_client(pointer->focus->surface->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002404 wl_fixed_t sx, sy;
2405
Derek Foreman1281a362015-07-31 16:55:32 -05002406 weston_view_from_global_fixed(pointer->focus,
2407 pointer->x,
2408 pointer->y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002409 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002410
Neil Roberts96d790e2013-09-19 17:32:00 +01002411 wl_pointer_send_enter(cr,
Derek Foreman1281a362015-07-31 16:55:32 -05002412 pointer->focus_serial,
2413 pointer->focus->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01002414 sx, sy);
Peter Hutterer87743e92016-01-18 16:38:22 +10002415 pointer_send_frame(cr);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002416 }
2417}
2418
2419static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002420keyboard_release(struct wl_client *client, struct wl_resource *resource)
2421{
2422 wl_resource_destroy(resource);
2423}
2424
2425static const struct wl_keyboard_interface keyboard_interface = {
2426 keyboard_release
2427};
2428
Derek Foreman280e7dd2014-10-03 13:13:42 -05002429static bool
Neil Roberts96d790e2013-09-19 17:32:00 +01002430should_send_modifiers_to_client(struct weston_seat *seat,
2431 struct wl_client *client)
2432{
Derek Foreman1281a362015-07-31 16:55:32 -05002433 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2434 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2435
2436 if (keyboard &&
2437 keyboard->focus &&
2438 keyboard->focus->resource &&
2439 wl_resource_get_client(keyboard->focus->resource) == client)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002440 return true;
Neil Roberts96d790e2013-09-19 17:32:00 +01002441
Derek Foreman1281a362015-07-31 16:55:32 -05002442 if (pointer &&
2443 pointer->focus &&
2444 pointer->focus->surface->resource &&
2445 wl_resource_get_client(pointer->focus->surface->resource) == client)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002446 return true;
Neil Roberts96d790e2013-09-19 17:32:00 +01002447
Derek Foreman280e7dd2014-10-03 13:13:42 -05002448 return false;
Neil Roberts96d790e2013-09-19 17:32:00 +01002449}
2450
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002451static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002452seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2453 uint32_t id)
2454{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002455 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002456 /* We use the keyboard_state directly, which means we'll
2457 * give a wl_keyboard if the seat has ever had one - even though
2458 * the spec explicitly states that this request only takes effect
2459 * if the seat has the keyboard capability.
2460 *
2461 * This prevents a race between the compositor sending new
2462 * capabilities and the client trying to use the old ones.
2463 */
2464 struct weston_keyboard *keyboard = seat->keyboard_state;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002465 struct wl_resource *cr;
2466
Derek Foreman345c9f32015-06-03 15:53:28 -05002467 if (!keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002468 return;
2469
Jason Ekstranda85118c2013-06-27 20:17:02 -05002470 cr = wl_resource_create(client, &wl_keyboard_interface,
2471 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002472 if (cr == NULL) {
2473 wl_client_post_no_memory(client);
2474 return;
2475 }
2476
Neil Roberts96d790e2013-09-19 17:32:00 +01002477 /* May be moved to focused list later by either
2478 * weston_keyboard_set_focus or directly if this client is already
2479 * focused */
Derek Foreman345c9f32015-06-03 15:53:28 -05002480 wl_list_insert(&keyboard->resource_list, wl_resource_get_link(cr));
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002481 wl_resource_set_implementation(cr, &keyboard_interface,
2482 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002483
Jonny Lamb66a41a02014-08-12 14:58:25 +02002484 if (wl_resource_get_version(cr) >= WL_KEYBOARD_REPEAT_INFO_SINCE_VERSION) {
2485 wl_keyboard_send_repeat_info(cr,
2486 seat->compositor->kb_repeat_rate,
2487 seat->compositor->kb_repeat_delay);
2488 }
Jasper St. Pierred8c6aeb2014-08-04 13:43:24 -04002489
Derek Foreman185d1582017-06-28 11:17:23 -05002490 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2491 keyboard->xkb_info->keymap_fd,
2492 keyboard->xkb_info->keymap_size);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002493
Neil Roberts96d790e2013-09-19 17:32:00 +01002494 if (should_send_modifiers_to_client(seat, client)) {
Derek Foreman345c9f32015-06-03 15:53:28 -05002495 send_modifiers_to_resource(keyboard,
Neil Roberts96d790e2013-09-19 17:32:00 +01002496 cr,
Derek Foreman345c9f32015-06-03 15:53:28 -05002497 keyboard->focus_serial);
Neil Roberts96d790e2013-09-19 17:32:00 +01002498 }
2499
Derek Foreman345c9f32015-06-03 15:53:28 -05002500 if (keyboard->focus && keyboard->focus->resource &&
2501 wl_resource_get_client(keyboard->focus->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01002502 struct weston_surface *surface =
Derek Foreman345c9f32015-06-03 15:53:28 -05002503 (struct weston_surface *)keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +01002504
2505 wl_list_remove(wl_resource_get_link(cr));
Derek Foreman345c9f32015-06-03 15:53:28 -05002506 wl_list_insert(&keyboard->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002507 wl_resource_get_link(cr));
2508 wl_keyboard_send_enter(cr,
Derek Foreman345c9f32015-06-03 15:53:28 -05002509 keyboard->focus_serial,
Neil Roberts96d790e2013-09-19 17:32:00 +01002510 surface->resource,
Derek Foreman345c9f32015-06-03 15:53:28 -05002511 &keyboard->keys);
Neil Roberts96d790e2013-09-19 17:32:00 +01002512
2513 /* If this is the first keyboard resource for this
2514 * client... */
Derek Foreman345c9f32015-06-03 15:53:28 -05002515 if (keyboard->focus_resource_list.prev ==
Neil Roberts96d790e2013-09-19 17:32:00 +01002516 wl_resource_get_link(cr))
2517 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002518 }
2519}
2520
2521static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002522touch_release(struct wl_client *client, struct wl_resource *resource)
2523{
2524 wl_resource_destroy(resource);
2525}
2526
2527static const struct wl_touch_interface touch_interface = {
2528 touch_release
2529};
2530
2531static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002532seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2533 uint32_t id)
2534{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002535 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002536 /* We use the touch_state directly, which means we'll
2537 * give a wl_touch if the seat has ever had one - even though
2538 * the spec explicitly states that this request only takes effect
2539 * if the seat has the touch capability.
2540 *
2541 * This prevents a race between the compositor sending new
2542 * capabilities and the client trying to use the old ones.
2543 */
2544 struct weston_touch *touch = seat->touch_state;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002545 struct wl_resource *cr;
2546
Derek Foreman1281a362015-07-31 16:55:32 -05002547 if (!touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002548 return;
2549
Jason Ekstranda85118c2013-06-27 20:17:02 -05002550 cr = wl_resource_create(client, &wl_touch_interface,
2551 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002552 if (cr == NULL) {
2553 wl_client_post_no_memory(client);
2554 return;
2555 }
2556
Derek Foreman1281a362015-07-31 16:55:32 -05002557 if (touch->focus &&
2558 wl_resource_get_client(touch->focus->surface->resource) == client) {
Chokshi, Mituld6697142015-10-09 08:28:47 +00002559 wl_list_insert(&touch->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002560 wl_resource_get_link(cr));
2561 } else {
Chokshi, Mituld6697142015-10-09 08:28:47 +00002562 wl_list_insert(&touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002563 wl_resource_get_link(cr));
2564 }
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002565 wl_resource_set_implementation(cr, &touch_interface,
2566 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002567}
2568
Quentin Glidicaab1d362016-03-13 17:49:08 +01002569static void
2570seat_release(struct wl_client *client, struct wl_resource *resource)
2571{
2572 wl_resource_destroy(resource);
2573}
2574
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002575static const struct wl_seat_interface seat_interface = {
2576 seat_get_pointer,
2577 seat_get_keyboard,
2578 seat_get_touch,
Quentin Glidicaab1d362016-03-13 17:49:08 +01002579 seat_release,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002580};
2581
2582static void
2583bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2584{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002585 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002586 struct wl_resource *resource;
2587 enum wl_seat_capability caps = 0;
2588
Jason Ekstranda85118c2013-06-27 20:17:02 -05002589 resource = wl_resource_create(client,
Derek Foreman1909c102015-11-26 14:17:47 -06002590 &wl_seat_interface, version, id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05002591 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05002592 wl_resource_set_implementation(resource, &seat_interface, data,
2593 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002594
Derek Foreman1281a362015-07-31 16:55:32 -05002595 if (weston_seat_get_pointer(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002596 caps |= WL_SEAT_CAPABILITY_POINTER;
Derek Foreman1281a362015-07-31 16:55:32 -05002597 if (weston_seat_get_keyboard(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002598 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Derek Foreman1281a362015-07-31 16:55:32 -05002599 if (weston_seat_get_touch(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002600 caps |= WL_SEAT_CAPABILITY_TOUCH;
2601
2602 wl_seat_send_capabilities(resource, caps);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04002603 if (version >= WL_SEAT_NAME_SINCE_VERSION)
Rob Bradforde445ae62013-05-31 18:09:51 +01002604 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002605}
2606
Jonas Ådahl30d61d82014-10-22 21:21:17 +02002607static void
2608relative_pointer_destroy(struct wl_client *client,
2609 struct wl_resource *resource)
2610{
2611 wl_resource_destroy(resource);
2612}
2613
2614static const struct zwp_relative_pointer_v1_interface relative_pointer_interface = {
2615 relative_pointer_destroy
2616};
2617
2618static void
2619relative_pointer_manager_destroy(struct wl_client *client,
2620 struct wl_resource *resource)
2621{
2622 wl_resource_destroy(resource);
2623}
2624
2625static void
2626relative_pointer_manager_get_relative_pointer(struct wl_client *client,
2627 struct wl_resource *resource,
2628 uint32_t id,
2629 struct wl_resource *pointer_resource)
2630{
2631 struct weston_pointer *pointer =
2632 wl_resource_get_user_data(pointer_resource);
2633 struct weston_pointer_client *pointer_client;
2634 struct wl_resource *cr;
2635
2636 cr = wl_resource_create(client, &zwp_relative_pointer_v1_interface,
2637 wl_resource_get_version(resource), id);
2638 if (cr == NULL) {
2639 wl_client_post_no_memory(client);
2640 return;
2641 }
2642
2643 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
2644 if (!pointer_client) {
2645 wl_client_post_no_memory(client);
2646 return;
2647 }
2648
2649 wl_list_insert(&pointer_client->relative_pointer_resources,
2650 wl_resource_get_link(cr));
2651 wl_resource_set_implementation(cr, &relative_pointer_interface,
2652 pointer,
2653 unbind_pointer_client_resource);
2654}
2655
2656static const struct zwp_relative_pointer_manager_v1_interface relative_pointer_manager = {
2657 relative_pointer_manager_destroy,
2658 relative_pointer_manager_get_relative_pointer,
2659};
2660
2661static void
2662bind_relative_pointer_manager(struct wl_client *client, void *data,
2663 uint32_t version, uint32_t id)
2664{
2665 struct weston_compositor *compositor = data;
2666 struct wl_resource *resource;
2667
2668 resource = wl_resource_create(client,
2669 &zwp_relative_pointer_manager_v1_interface,
2670 1, id);
2671
2672 wl_resource_set_implementation(resource, &relative_pointer_manager,
2673 compositor,
2674 NULL);
2675}
2676
Giulio Camuffo0358af42016-06-02 21:48:08 +03002677WL_EXPORT int
2678weston_compositor_set_xkb_rule_names(struct weston_compositor *ec,
2679 struct xkb_rule_names *names)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002680{
2681 if (ec->xkb_context == NULL) {
2682 ec->xkb_context = xkb_context_new(0);
2683 if (ec->xkb_context == NULL) {
2684 weston_log("failed to create XKB context\n");
2685 return -1;
2686 }
2687 }
2688
2689 if (names)
2690 ec->xkb_names = *names;
2691 if (!ec->xkb_names.rules)
2692 ec->xkb_names.rules = strdup("evdev");
2693 if (!ec->xkb_names.model)
2694 ec->xkb_names.model = strdup("pc105");
2695 if (!ec->xkb_names.layout)
2696 ec->xkb_names.layout = strdup("us");
2697
2698 return 0;
2699}
2700
Stefan Schmidtfda26522013-09-17 10:54:09 +01002701static void
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002702weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002703{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002704 if (--xkb_info->ref_count > 0)
2705 return;
2706
Ran Benitac9c74152014-08-19 23:59:52 +03002707 xkb_keymap_unref(xkb_info->keymap);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002708
2709 if (xkb_info->keymap_area)
2710 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2711 if (xkb_info->keymap_fd >= 0)
2712 close(xkb_info->keymap_fd);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002713 free(xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002714}
2715
2716void
2717weston_compositor_xkb_destroy(struct weston_compositor *ec)
2718{
2719 free((char *) ec->xkb_names.rules);
2720 free((char *) ec->xkb_names.model);
2721 free((char *) ec->xkb_names.layout);
2722 free((char *) ec->xkb_names.variant);
2723 free((char *) ec->xkb_names.options);
Stefan Schmidtfda26522013-09-17 10:54:09 +01002724
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002725 if (ec->xkb_info)
2726 weston_xkb_info_destroy(ec->xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002727 xkb_context_unref(ec->xkb_context);
2728}
2729
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002730static struct weston_xkb_info *
2731weston_xkb_info_create(struct xkb_keymap *keymap)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002732{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002733 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
2734 if (xkb_info == NULL)
2735 return NULL;
2736
Ran Benita2e1968f2014-08-19 23:59:51 +03002737 xkb_info->keymap = xkb_keymap_ref(keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002738 xkb_info->ref_count = 1;
2739
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002740 char *keymap_str;
2741
Ran Benita2e1968f2014-08-19 23:59:51 +03002742 xkb_info->shift_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2743 XKB_MOD_NAME_SHIFT);
2744 xkb_info->caps_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2745 XKB_MOD_NAME_CAPS);
2746 xkb_info->ctrl_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2747 XKB_MOD_NAME_CTRL);
2748 xkb_info->alt_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2749 XKB_MOD_NAME_ALT);
2750 xkb_info->mod2_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2751 "Mod2");
2752 xkb_info->mod3_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2753 "Mod3");
2754 xkb_info->super_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2755 XKB_MOD_NAME_LOGO);
2756 xkb_info->mod5_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2757 "Mod5");
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002758
Ran Benita2e1968f2014-08-19 23:59:51 +03002759 xkb_info->num_led = xkb_keymap_led_get_index(xkb_info->keymap,
2760 XKB_LED_NAME_NUM);
2761 xkb_info->caps_led = xkb_keymap_led_get_index(xkb_info->keymap,
2762 XKB_LED_NAME_CAPS);
2763 xkb_info->scroll_led = xkb_keymap_led_get_index(xkb_info->keymap,
2764 XKB_LED_NAME_SCROLL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002765
Ran Benita2e1968f2014-08-19 23:59:51 +03002766 keymap_str = xkb_keymap_get_as_string(xkb_info->keymap,
2767 XKB_KEYMAP_FORMAT_TEXT_V1);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002768 if (keymap_str == NULL) {
2769 weston_log("failed to get string version of keymap\n");
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002770 goto err_keymap;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002771 }
2772 xkb_info->keymap_size = strlen(keymap_str) + 1;
2773
2774 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
2775 if (xkb_info->keymap_fd < 0) {
2776 weston_log("creating a keymap file for %lu bytes failed: %m\n",
2777 (unsigned long) xkb_info->keymap_size);
2778 goto err_keymap_str;
2779 }
2780
2781 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2782 PROT_READ | PROT_WRITE,
2783 MAP_SHARED, xkb_info->keymap_fd, 0);
2784 if (xkb_info->keymap_area == MAP_FAILED) {
2785 weston_log("failed to mmap() %lu bytes\n",
2786 (unsigned long) xkb_info->keymap_size);
2787 goto err_dev_zero;
2788 }
2789 strcpy(xkb_info->keymap_area, keymap_str);
2790 free(keymap_str);
2791
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002792 return xkb_info;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002793
2794err_dev_zero:
2795 close(xkb_info->keymap_fd);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002796err_keymap_str:
2797 free(keymap_str);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002798err_keymap:
Ran Benita2e1968f2014-08-19 23:59:51 +03002799 xkb_keymap_unref(xkb_info->keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002800 free(xkb_info);
2801 return NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002802}
2803
2804static int
2805weston_compositor_build_global_keymap(struct weston_compositor *ec)
2806{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002807 struct xkb_keymap *keymap;
2808
2809 if (ec->xkb_info != NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002810 return 0;
2811
Ran Benita2e1968f2014-08-19 23:59:51 +03002812 keymap = xkb_keymap_new_from_names(ec->xkb_context,
2813 &ec->xkb_names,
2814 0);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002815 if (keymap == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002816 weston_log("failed to compile global XKB keymap\n");
2817 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
2818 "options %s\n",
2819 ec->xkb_names.rules, ec->xkb_names.model,
2820 ec->xkb_names.layout, ec->xkb_names.variant,
2821 ec->xkb_names.options);
2822 return -1;
2823 }
2824
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002825 ec->xkb_info = weston_xkb_info_create(keymap);
Rui Matos73d93952013-10-24 19:28:41 +02002826 xkb_keymap_unref(keymap);
Stefan Schmidtfda26522013-09-17 10:54:09 +01002827 if (ec->xkb_info == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002828 return -1;
2829
2830 return 0;
2831}
2832
Rui Matos65196bc2013-10-10 19:44:19 +02002833WL_EXPORT void
2834weston_seat_update_keymap(struct weston_seat *seat, struct xkb_keymap *keymap)
2835{
Derek Foreman1281a362015-07-31 16:55:32 -05002836 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2837
2838 if (!keyboard || !keymap)
Rui Matos65196bc2013-10-10 19:44:19 +02002839 return;
2840
Derek Foreman1281a362015-07-31 16:55:32 -05002841 xkb_keymap_unref(keyboard->pending_keymap);
2842 keyboard->pending_keymap = xkb_keymap_ref(keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02002843
Derek Foreman1281a362015-07-31 16:55:32 -05002844 if (keyboard->keys.size == 0)
Rui Matos65196bc2013-10-10 19:44:19 +02002845 update_keymap(seat);
Rui Matos65196bc2013-10-10 19:44:19 +02002846}
2847
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002848WL_EXPORT int
2849weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
2850{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002851 struct weston_keyboard *keyboard;
2852
Derek Foreman1281a362015-07-31 16:55:32 -05002853 if (seat->keyboard_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002854 seat->keyboard_device_count += 1;
2855 if (seat->keyboard_device_count == 1)
2856 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002857 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002858 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002859
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002860 keyboard = weston_keyboard_create();
2861 if (keyboard == NULL) {
2862 weston_log("failed to allocate weston keyboard struct\n");
2863 return -1;
2864 }
2865
Derek Foreman185d1582017-06-28 11:17:23 -05002866 if (keymap != NULL) {
2867 keyboard->xkb_info = weston_xkb_info_create(keymap);
2868 if (keyboard->xkb_info == NULL)
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02002869 goto err;
Derek Foreman185d1582017-06-28 11:17:23 -05002870 } else {
2871 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
2872 goto err;
2873 keyboard->xkb_info = seat->compositor->xkb_info;
2874 keyboard->xkb_info->ref_count++;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002875 }
Derek Foreman185d1582017-06-28 11:17:23 -05002876
2877 keyboard->xkb_state.state = xkb_state_new(keyboard->xkb_info->keymap);
2878 if (keyboard->xkb_state.state == NULL) {
2879 weston_log("failed to initialise XKB state\n");
2880 goto err;
2881 }
2882
2883 keyboard->xkb_state.leds = 0;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002884
Derek Foreman1281a362015-07-31 16:55:32 -05002885 seat->keyboard_state = keyboard;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02002886 seat->keyboard_device_count = 1;
2887 keyboard->seat = seat;
2888
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002889 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002890
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002891 return 0;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02002892
2893err:
2894 if (keyboard->xkb_info)
2895 weston_xkb_info_destroy(keyboard->xkb_info);
2896 free(keyboard);
2897
2898 return -1;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002899}
2900
Jonas Ådahl91fed542013-12-03 09:14:27 +01002901static void
2902weston_keyboard_reset_state(struct weston_keyboard *keyboard)
2903{
2904 struct weston_seat *seat = keyboard->seat;
2905 struct xkb_state *state;
2906
Derek Foreman185d1582017-06-28 11:17:23 -05002907 state = xkb_state_new(keyboard->xkb_info->keymap);
2908 if (!state) {
2909 weston_log("failed to reset XKB state\n");
2910 return;
Jonas Ådahl91fed542013-12-03 09:14:27 +01002911 }
Derek Foreman185d1582017-06-28 11:17:23 -05002912 xkb_state_unref(keyboard->xkb_state.state);
2913 keyboard->xkb_state.state = state;
2914
2915 keyboard->xkb_state.leds = 0;
Jonas Ådahl91fed542013-12-03 09:14:27 +01002916
2917 seat->modifier_state = 0;
2918}
2919
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002920WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002921weston_seat_release_keyboard(struct weston_seat *seat)
2922{
2923 seat->keyboard_device_count--;
Derek Foremand621df22014-11-19 11:04:12 -06002924 assert(seat->keyboard_device_count >= 0);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002925 if (seat->keyboard_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05002926 weston_keyboard_set_focus(seat->keyboard_state, NULL);
2927 weston_keyboard_cancel_grab(seat->keyboard_state);
2928 weston_keyboard_reset_state(seat->keyboard_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002929 seat_send_updated_caps(seat);
2930 }
2931}
2932
2933WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002934weston_seat_init_pointer(struct weston_seat *seat)
2935{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002936 struct weston_pointer *pointer;
2937
Derek Foreman1281a362015-07-31 16:55:32 -05002938 if (seat->pointer_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002939 seat->pointer_device_count += 1;
2940 if (seat->pointer_device_count == 1)
2941 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002942 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002943 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002944
Giulio Camuffocdb4d292013-11-14 23:42:53 +01002945 pointer = weston_pointer_create(seat);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002946 if (pointer == NULL)
2947 return;
2948
Derek Foreman1281a362015-07-31 16:55:32 -05002949 seat->pointer_state = pointer;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002950 seat->pointer_device_count = 1;
2951 pointer->seat = seat;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002952
2953 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002954}
2955
2956WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002957weston_seat_release_pointer(struct weston_seat *seat)
2958{
Derek Foreman1281a362015-07-31 16:55:32 -05002959 struct weston_pointer *pointer = seat->pointer_state;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002960
2961 seat->pointer_device_count--;
2962 if (seat->pointer_device_count == 0) {
Derek Foremanf9318d12015-05-11 15:40:11 -05002963 weston_pointer_clear_focus(pointer);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002964 weston_pointer_cancel_grab(pointer);
Jonas Ådahl630bae82013-10-17 23:04:06 +02002965
Jonas Ådahla4932742013-10-17 23:04:07 +02002966 if (pointer->sprite)
2967 pointer_unmap_sprite(pointer);
2968
Jonas Ådahl3e12e632013-12-02 22:05:05 +01002969 weston_pointer_reset_state(pointer);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002970 seat_send_updated_caps(seat);
Derek Foremanfd5ca512015-01-07 15:00:25 -06002971
2972 /* seat->pointer is intentionally not destroyed so that
2973 * a newly attached pointer on this seat will retain
2974 * the previous cursor co-ordinates.
2975 */
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002976 }
2977}
2978
2979WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002980weston_seat_init_touch(struct weston_seat *seat)
2981{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002982 struct weston_touch *touch;
2983
Derek Foreman1281a362015-07-31 16:55:32 -05002984 if (seat->touch_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002985 seat->touch_device_count += 1;
2986 if (seat->touch_device_count == 1)
2987 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002988 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002989 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002990
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002991 touch = weston_touch_create();
2992 if (touch == NULL)
2993 return;
2994
Derek Foreman1281a362015-07-31 16:55:32 -05002995 seat->touch_state = touch;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002996 seat->touch_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002997 touch->seat = seat;
2998
2999 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003000}
3001
3002WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003003weston_seat_release_touch(struct weston_seat *seat)
3004{
3005 seat->touch_device_count--;
3006 if (seat->touch_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05003007 weston_touch_set_focus(seat->touch_state, NULL);
3008 weston_touch_cancel_grab(seat->touch_state);
3009 weston_touch_reset_state(seat->touch_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003010 seat_send_updated_caps(seat);
3011 }
3012}
3013
3014WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003015weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
3016 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003017{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003018 memset(seat, 0, sizeof *seat);
3019
Kristian Høgsberge3148752013-05-06 23:19:49 -04003020 seat->selection_data_source = NULL;
3021 wl_list_init(&seat->base_resource_list);
3022 wl_signal_init(&seat->selection_signal);
3023 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04003024 wl_signal_init(&seat->destroy_signal);
Jason Ekstranda4ab5422014-04-02 19:53:45 -05003025 wl_signal_init(&seat->updated_caps_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003026
Peter Hutterer87743e92016-01-18 16:38:22 +10003027 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 5,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003028 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003029
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003030 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003031 seat->modifier_state = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003032 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003033
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003034 wl_list_insert(ec->seat_list.prev, &seat->link);
3035
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003036 clipboard_create(seat);
3037
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003038 wl_signal_emit(&ec->seat_created_signal, seat);
3039}
3040
3041WL_EXPORT void
3042weston_seat_release(struct weston_seat *seat)
3043{
3044 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003045
Jonas Ådahl1afb2382014-01-03 19:46:51 +01003046 if (seat->saved_kbd_focus)
3047 wl_list_remove(&seat->saved_kbd_focus_listener.link);
3048
Derek Foreman1281a362015-07-31 16:55:32 -05003049 if (seat->pointer_state)
3050 weston_pointer_destroy(seat->pointer_state);
3051 if (seat->keyboard_state)
3052 weston_keyboard_destroy(seat->keyboard_state);
3053 if (seat->touch_state)
3054 weston_touch_destroy(seat->touch_state);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003055
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003056 free (seat->seat_name);
3057
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003058 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04003059
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003060 wl_signal_emit(&seat->destroy_signal, seat);
3061}
Derek Foreman1281a362015-07-31 16:55:32 -05003062
3063/** Get a seat's keyboard pointer
3064 *
3065 * \param seat The seat to query
3066 * \return The seat's keyboard pointer, or NULL if no keyboard is present
3067 *
3068 * The keyboard pointer for a seat isn't freed when all keyboards are removed,
3069 * so it should only be used when the seat's keyboard_device_count is greater
3070 * than zero. This function does that test and only returns a pointer
3071 * when a keyboard is present.
3072 */
3073WL_EXPORT struct weston_keyboard *
3074weston_seat_get_keyboard(struct weston_seat *seat)
3075{
3076 if (!seat)
3077 return NULL;
3078
3079 if (seat->keyboard_device_count)
3080 return seat->keyboard_state;
3081
3082 return NULL;
3083}
3084
3085/** Get a seat's pointer pointer
3086 *
3087 * \param seat The seat to query
3088 * \return The seat's pointer pointer, or NULL if no pointer device is present
3089 *
3090 * The pointer pointer for a seat isn't freed when all mice are removed,
3091 * so it should only be used when the seat's pointer_device_count is greater
3092 * than zero. This function does that test and only returns a pointer
3093 * when a pointing device is present.
3094 */
3095WL_EXPORT struct weston_pointer *
3096weston_seat_get_pointer(struct weston_seat *seat)
3097{
3098 if (!seat)
3099 return NULL;
3100
3101 if (seat->pointer_device_count)
3102 return seat->pointer_state;
3103
3104 return NULL;
3105}
3106
Jonas Ådahld3414f22016-07-22 17:56:31 +08003107static const struct zwp_locked_pointer_v1_interface locked_pointer_interface;
3108static const struct zwp_confined_pointer_v1_interface confined_pointer_interface;
3109
3110static enum pointer_constraint_type
3111pointer_constraint_get_type(struct weston_pointer_constraint *constraint)
3112{
3113 if (wl_resource_instance_of(constraint->resource,
3114 &zwp_locked_pointer_v1_interface,
3115 &locked_pointer_interface)) {
3116 return POINTER_CONSTRAINT_TYPE_LOCK;
3117 } else if (wl_resource_instance_of(constraint->resource,
3118 &zwp_confined_pointer_v1_interface,
3119 &confined_pointer_interface)) {
3120 return POINTER_CONSTRAINT_TYPE_CONFINE;
3121 }
3122
3123 abort();
3124 return 0;
3125}
3126
3127static void
3128pointer_constraint_notify_activated(struct weston_pointer_constraint *constraint)
3129{
3130 struct wl_resource *resource = constraint->resource;
3131
3132 switch (pointer_constraint_get_type(constraint)) {
3133 case POINTER_CONSTRAINT_TYPE_LOCK:
3134 zwp_locked_pointer_v1_send_locked(resource);
3135 break;
3136 case POINTER_CONSTRAINT_TYPE_CONFINE:
3137 zwp_confined_pointer_v1_send_confined(resource);
3138 break;
3139 }
3140}
3141
3142static void
3143pointer_constraint_notify_deactivated(struct weston_pointer_constraint *constraint)
3144{
3145 struct wl_resource *resource = constraint->resource;
3146
3147 switch (pointer_constraint_get_type(constraint)) {
3148 case POINTER_CONSTRAINT_TYPE_LOCK:
3149 zwp_locked_pointer_v1_send_unlocked(resource);
3150 break;
3151 case POINTER_CONSTRAINT_TYPE_CONFINE:
3152 zwp_confined_pointer_v1_send_unconfined(resource);
3153 break;
3154 }
3155}
3156
3157static struct weston_pointer_constraint *
3158get_pointer_constraint_for_pointer(struct weston_surface *surface,
3159 struct weston_pointer *pointer)
3160{
3161 struct weston_pointer_constraint *constraint;
3162
3163 wl_list_for_each(constraint, &surface->pointer_constraints, link) {
3164 if (constraint->pointer == pointer)
3165 return constraint;
3166 }
3167
3168 return NULL;
3169}
3170
Derek Foreman1281a362015-07-31 16:55:32 -05003171/** Get a seat's touch pointer
3172 *
3173 * \param seat The seat to query
3174 * \return The seat's touch pointer, or NULL if no touch device is present
3175 *
3176 * The touch pointer for a seat isn't freed when all touch devices are removed,
3177 * so it should only be used when the seat's touch_device_count is greater
3178 * than zero. This function does that test and only returns a pointer
3179 * when a touch device is present.
3180 */
3181WL_EXPORT struct weston_touch *
3182weston_seat_get_touch(struct weston_seat *seat)
3183{
3184 if (!seat)
3185 return NULL;
3186
3187 if (seat->touch_device_count)
3188 return seat->touch_state;
3189
3190 return NULL;
3191}
Bryce Harrington24f917e2016-06-29 19:04:07 -07003192
3193/** Sets the keyboard focus to the given surface
3194 *
3195 * \param seat The seat to query
3196 */
3197WL_EXPORT void
3198weston_seat_set_keyboard_focus(struct weston_seat *seat,
3199 struct weston_surface *surface)
3200{
3201 struct weston_compositor *compositor = seat->compositor;
3202 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003203 struct weston_surface_activation_data activation_data;
Bryce Harrington24f917e2016-06-29 19:04:07 -07003204
Jonas Ådahlef8e1c32016-03-15 20:28:51 +08003205 if (keyboard && keyboard->focus != surface) {
Bryce Harrington24f917e2016-06-29 19:04:07 -07003206 weston_keyboard_set_focus(keyboard, surface);
3207 wl_data_device_set_keyboard_focus(seat);
3208 }
3209
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003210 inc_activate_serial(compositor);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003211
3212 activation_data = (struct weston_surface_activation_data) {
3213 .surface = surface,
3214 .seat = seat,
3215 };
3216 wl_signal_emit(&compositor->activate_signal, &activation_data);
Bryce Harrington24f917e2016-06-29 19:04:07 -07003217}
Jonas Ådahl30d61d82014-10-22 21:21:17 +02003218
Jonas Ådahld3414f22016-07-22 17:56:31 +08003219static void
3220enable_pointer_constraint(struct weston_pointer_constraint *constraint,
3221 struct weston_view *view)
3222{
3223 assert(constraint->view == NULL);
3224 constraint->view = view;
3225 pointer_constraint_notify_activated(constraint);
3226 weston_pointer_start_grab(constraint->pointer, &constraint->grab);
3227 wl_list_remove(&constraint->surface_destroy_listener.link);
3228 wl_list_init(&constraint->surface_destroy_listener.link);
3229}
3230
3231static bool
3232is_pointer_constraint_enabled(struct weston_pointer_constraint *constraint)
3233{
3234 return constraint->view != NULL;
3235}
3236
3237static void
3238weston_pointer_constraint_disable(struct weston_pointer_constraint *constraint)
3239{
3240 constraint->view = NULL;
3241 pointer_constraint_notify_deactivated(constraint);
3242 weston_pointer_end_grab(constraint->grab.pointer);
3243}
3244
3245void
3246weston_pointer_constraint_destroy(struct weston_pointer_constraint *constraint)
3247{
3248 if (is_pointer_constraint_enabled(constraint))
3249 weston_pointer_constraint_disable(constraint);
3250
3251 wl_list_remove(&constraint->pointer_destroy_listener.link);
3252 wl_list_remove(&constraint->surface_destroy_listener.link);
3253 wl_list_remove(&constraint->surface_commit_listener.link);
3254 wl_list_remove(&constraint->surface_activate_listener.link);
3255
3256 wl_resource_set_user_data(constraint->resource, NULL);
3257 pixman_region32_fini(&constraint->region);
3258 wl_list_remove(&constraint->link);
3259 free(constraint);
3260}
3261
3262static void
3263disable_pointer_constraint(struct weston_pointer_constraint *constraint)
3264{
3265 switch (constraint->lifetime) {
3266 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT:
3267 weston_pointer_constraint_destroy(constraint);
3268 break;
3269 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT:
3270 weston_pointer_constraint_disable(constraint);
3271 break;
3272 }
3273}
3274
3275static bool
3276is_within_constraint_region(struct weston_pointer_constraint *constraint,
3277 wl_fixed_t sx, wl_fixed_t sy)
3278{
3279 struct weston_surface *surface = constraint->surface;
3280 pixman_region32_t constraint_region;
3281 bool result;
3282
3283 pixman_region32_init(&constraint_region);
3284 pixman_region32_intersect(&constraint_region,
3285 &surface->input,
3286 &constraint->region);
3287 result = pixman_region32_contains_point(&constraint_region,
3288 wl_fixed_to_int(sx),
3289 wl_fixed_to_int(sy),
3290 NULL);
3291 pixman_region32_fini(&constraint_region);
3292
3293 return result;
3294}
3295
3296static void
3297maybe_enable_pointer_constraint(struct weston_pointer_constraint *constraint)
3298{
3299 struct weston_surface *surface = constraint->surface;
3300 struct weston_view *vit;
3301 struct weston_view *view = NULL;
3302 struct weston_pointer *pointer = constraint->pointer;
3303 struct weston_keyboard *keyboard;
3304 struct weston_seat *seat = pointer->seat;
3305 int32_t x, y;
3306
3307 /* Postpone if no view of the surface was most recently clicked. */
3308 wl_list_for_each(vit, &surface->views, surface_link) {
3309 if (vit->click_to_activate_serial ==
3310 surface->compositor->activate_serial) {
3311 view = vit;
3312 }
3313 }
3314 if (view == NULL)
3315 return;
3316
3317 /* Postpone if surface doesn't have keyboard focus. */
3318 keyboard = weston_seat_get_keyboard(seat);
3319 if (!keyboard || keyboard->focus != surface)
3320 return;
3321
3322 /* Postpone constraint if the pointer is not within the
3323 * constraint region.
3324 */
3325 weston_view_from_global(view,
3326 wl_fixed_to_int(pointer->x),
3327 wl_fixed_to_int(pointer->y),
3328 &x, &y);
3329 if (!is_within_constraint_region(constraint,
3330 wl_fixed_from_int(x),
3331 wl_fixed_from_int(y)))
3332 return;
3333
3334 enable_pointer_constraint(constraint, view);
3335}
3336
3337static void
3338locked_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
3339{
3340}
3341
3342static void
3343locked_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02003344 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003345 struct weston_pointer_motion_event *event)
3346{
Quentin Glidiccde13452016-08-12 10:41:32 +02003347 pointer_send_relative_motion(grab->pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08003348}
3349
3350static void
3351locked_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003352 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003353 uint32_t button,
3354 uint32_t state_w)
3355{
3356 weston_pointer_send_button(grab->pointer, time, button, state_w);
3357}
3358
3359static void
3360locked_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02003361 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003362 struct weston_pointer_axis_event *event)
3363{
3364 weston_pointer_send_axis(grab->pointer, time, event);
3365}
3366
3367static void
3368locked_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
3369 uint32_t source)
3370{
3371 weston_pointer_send_axis_source(grab->pointer, source);
3372}
3373
3374static void
3375locked_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
3376{
3377 weston_pointer_send_frame(grab->pointer);
3378}
3379
3380static void
3381locked_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
3382{
3383 struct weston_pointer_constraint *constraint =
3384 container_of(grab, struct weston_pointer_constraint, grab);
3385
3386 disable_pointer_constraint(constraint);
3387}
3388
3389static const struct weston_pointer_grab_interface
3390 locked_pointer_grab_interface = {
3391 locked_pointer_grab_pointer_focus,
3392 locked_pointer_grab_pointer_motion,
3393 locked_pointer_grab_pointer_button,
3394 locked_pointer_grab_pointer_axis,
3395 locked_pointer_grab_pointer_axis_source,
3396 locked_pointer_grab_pointer_frame,
3397 locked_pointer_grab_pointer_cancel,
3398};
3399
3400static void
3401pointer_constraint_constrain_resource_destroyed(struct wl_resource *resource)
3402{
3403 struct weston_pointer_constraint *constraint =
3404 wl_resource_get_user_data(resource);
3405
3406 if (!constraint)
3407 return;
3408
3409 weston_pointer_constraint_destroy(constraint);
3410}
3411
3412static void
3413pointer_constraint_surface_activate(struct wl_listener *listener, void *data)
3414{
3415 struct weston_surface_activation_data *activation = data;
3416 struct weston_pointer *pointer;
3417 struct weston_surface *focus = activation->surface;
3418 struct weston_pointer_constraint *constraint =
3419 container_of(listener, struct weston_pointer_constraint,
3420 surface_activate_listener);
3421 bool is_constraint_surface;
3422
3423 pointer = weston_seat_get_pointer(activation->seat);
3424 if (!pointer)
3425 return;
3426
3427 is_constraint_surface =
3428 get_pointer_constraint_for_pointer(focus, pointer) == constraint;
3429
3430 if (is_constraint_surface &&
3431 !is_pointer_constraint_enabled(constraint))
3432 maybe_enable_pointer_constraint(constraint);
3433 else if (!is_constraint_surface &&
3434 is_pointer_constraint_enabled(constraint))
3435 disable_pointer_constraint(constraint);
3436}
3437
3438static void
3439pointer_constraint_pointer_destroyed(struct wl_listener *listener, void *data)
3440{
3441 struct weston_pointer_constraint *constraint =
3442 container_of(listener, struct weston_pointer_constraint,
3443 pointer_destroy_listener);
3444
3445 weston_pointer_constraint_destroy(constraint);
3446}
3447
3448static void
3449pointer_constraint_surface_destroyed(struct wl_listener *listener, void *data)
3450{
3451 struct weston_pointer_constraint *constraint =
3452 container_of(listener, struct weston_pointer_constraint,
3453 surface_destroy_listener);
3454
3455 weston_pointer_constraint_destroy(constraint);
3456}
3457
3458static void
3459pointer_constraint_surface_committed(struct wl_listener *listener, void *data)
3460{
3461 struct weston_pointer_constraint *constraint =
3462 container_of(listener, struct weston_pointer_constraint,
3463 surface_commit_listener);
3464
3465 if (constraint->region_is_pending) {
3466 constraint->region_is_pending = false;
3467 pixman_region32_copy(&constraint->region,
3468 &constraint->region_pending);
3469 pixman_region32_fini(&constraint->region_pending);
3470 pixman_region32_init(&constraint->region_pending);
3471 }
3472
3473 if (constraint->hint_is_pending) {
3474 constraint->hint_is_pending = false;
3475
3476 constraint->hint_is_pending = true;
3477 constraint->hint_x = constraint->hint_x_pending;
3478 constraint->hint_y = constraint->hint_y_pending;
3479 }
3480
3481 if (pointer_constraint_get_type(constraint) ==
3482 POINTER_CONSTRAINT_TYPE_CONFINE &&
3483 is_pointer_constraint_enabled(constraint))
3484 maybe_warp_confined_pointer(constraint);
3485}
3486
3487static struct weston_pointer_constraint *
3488weston_pointer_constraint_create(struct weston_surface *surface,
3489 struct weston_pointer *pointer,
3490 struct weston_region *region,
3491 enum zwp_pointer_constraints_v1_lifetime lifetime,
3492 struct wl_resource *cr,
3493 const struct weston_pointer_grab_interface *grab_interface)
3494{
3495 struct weston_pointer_constraint *constraint;
3496
3497 constraint = zalloc(sizeof *constraint);
3498 if (!constraint)
3499 return NULL;
3500
3501 constraint->lifetime = lifetime;
3502 pixman_region32_init(&constraint->region);
3503 pixman_region32_init(&constraint->region_pending);
3504 wl_list_insert(&surface->pointer_constraints, &constraint->link);
3505 constraint->surface = surface;
3506 constraint->pointer = pointer;
3507 constraint->resource = cr;
3508 constraint->grab.interface = grab_interface;
3509 if (region) {
3510 pixman_region32_copy(&constraint->region,
3511 &region->region);
3512 } else {
3513 pixman_region32_fini(&constraint->region);
3514 region_init_infinite(&constraint->region);
3515 }
3516
3517 constraint->surface_activate_listener.notify =
3518 pointer_constraint_surface_activate;
3519 constraint->surface_destroy_listener.notify =
3520 pointer_constraint_surface_destroyed;
3521 constraint->surface_commit_listener.notify =
3522 pointer_constraint_surface_committed;
3523 constraint->pointer_destroy_listener.notify =
3524 pointer_constraint_pointer_destroyed;
3525
3526 wl_signal_add(&surface->compositor->activate_signal,
3527 &constraint->surface_activate_listener);
3528 wl_signal_add(&pointer->destroy_signal,
3529 &constraint->pointer_destroy_listener);
3530 wl_signal_add(&surface->destroy_signal,
3531 &constraint->surface_destroy_listener);
3532 wl_signal_add(&surface->commit_signal,
3533 &constraint->surface_commit_listener);
3534
3535 return constraint;
3536}
3537
3538static void
3539init_pointer_constraint(struct wl_resource *pointer_constraints_resource,
3540 uint32_t id,
3541 struct weston_surface *surface,
3542 struct weston_pointer *pointer,
3543 struct weston_region *region,
3544 enum zwp_pointer_constraints_v1_lifetime lifetime,
3545 const struct wl_interface *interface,
3546 const void *implementation,
3547 const struct weston_pointer_grab_interface *grab_interface)
3548{
3549 struct wl_client *client =
3550 wl_resource_get_client(pointer_constraints_resource);
3551 struct wl_resource *cr;
3552 struct weston_pointer_constraint *constraint;
3553
3554 if (get_pointer_constraint_for_pointer(surface, pointer)) {
3555 wl_resource_post_error(pointer_constraints_resource,
3556 ZWP_POINTER_CONSTRAINTS_V1_ERROR_ALREADY_CONSTRAINED,
3557 "the pointer has a lock/confine request on this surface");
3558 return;
3559 }
3560
3561 cr = wl_resource_create(client, interface,
3562 wl_resource_get_version(pointer_constraints_resource),
3563 id);
3564 if (cr == NULL) {
3565 wl_client_post_no_memory(client);
3566 return;
3567 }
3568
3569 constraint = weston_pointer_constraint_create(surface, pointer,
3570 region, lifetime,
3571 cr, grab_interface);
3572 if (constraint == NULL) {
3573 wl_client_post_no_memory(client);
3574 return;
3575 }
3576
3577 wl_resource_set_implementation(cr, implementation, constraint,
3578 pointer_constraint_constrain_resource_destroyed);
3579
3580 maybe_enable_pointer_constraint(constraint);
3581}
3582
3583static void
3584pointer_constraints_destroy(struct wl_client *client,
3585 struct wl_resource *resource)
3586{
3587 wl_resource_destroy(resource);
3588}
3589
3590static void
3591locked_pointer_destroy(struct wl_client *client,
3592 struct wl_resource *resource)
3593{
3594 struct weston_pointer_constraint *constraint =
3595 wl_resource_get_user_data(resource);
3596 wl_fixed_t x, y;
3597
3598 if (constraint && constraint->view && constraint->hint_is_pending &&
3599 is_within_constraint_region(constraint,
3600 constraint->hint_x,
3601 constraint->hint_y)) {
3602 weston_view_to_global_fixed(constraint->view,
3603 constraint->hint_x,
3604 constraint->hint_y,
3605 &x, &y);
3606 weston_pointer_move_to(constraint->pointer, x, y);
3607 }
3608 wl_resource_destroy(resource);
3609}
3610
3611static void
3612locked_pointer_set_cursor_position_hint(struct wl_client *client,
3613 struct wl_resource *resource,
3614 wl_fixed_t surface_x,
3615 wl_fixed_t surface_y)
3616{
3617 struct weston_pointer_constraint *constraint =
3618 wl_resource_get_user_data(resource);
3619
3620 /* Ignore a set cursor hint that was sent after the lock was cancelled.
3621 */
3622 if (!constraint ||
3623 !constraint->resource ||
3624 constraint->resource != resource)
3625 return;
3626
3627 constraint->hint_is_pending = true;
3628 constraint->hint_x_pending = surface_x;
3629 constraint->hint_y_pending = surface_y;
3630}
3631
3632static void
3633locked_pointer_set_region(struct wl_client *client,
3634 struct wl_resource *resource,
3635 struct wl_resource *region_resource)
3636{
3637 struct weston_pointer_constraint *constraint =
3638 wl_resource_get_user_data(resource);
3639 struct weston_region *region = region_resource ?
3640 wl_resource_get_user_data(region_resource) : NULL;
3641
3642 if (!constraint)
3643 return;
3644
3645 if (region) {
3646 pixman_region32_copy(&constraint->region_pending,
3647 &region->region);
3648 } else {
3649 pixman_region32_fini(&constraint->region_pending);
3650 region_init_infinite(&constraint->region_pending);
3651 }
3652 constraint->region_is_pending = true;
3653}
3654
3655
3656static const struct zwp_locked_pointer_v1_interface locked_pointer_interface = {
3657 locked_pointer_destroy,
3658 locked_pointer_set_cursor_position_hint,
3659 locked_pointer_set_region,
3660};
3661
3662static void
3663pointer_constraints_lock_pointer(struct wl_client *client,
3664 struct wl_resource *resource,
3665 uint32_t id,
3666 struct wl_resource *surface_resource,
3667 struct wl_resource *pointer_resource,
3668 struct wl_resource *region_resource,
3669 uint32_t lifetime)
3670{
3671 struct weston_surface *surface =
3672 wl_resource_get_user_data(surface_resource);
3673 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
3674 struct weston_region *region = region_resource ?
3675 wl_resource_get_user_data(region_resource) : NULL;
3676
3677 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
3678 &zwp_locked_pointer_v1_interface,
3679 &locked_pointer_interface,
3680 &locked_pointer_grab_interface);
3681}
3682
3683static void
3684confined_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
3685{
3686}
3687
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08003688static double
3689vec2d_cross_product(struct vec2d a, struct vec2d b)
3690{
3691 return a.x * b.y - a.y * b.x;
3692}
3693
3694static struct vec2d
3695vec2d_add(struct vec2d a, struct vec2d b)
3696{
3697 return (struct vec2d) {
3698 .x = a.x + b.x,
3699 .y = a.y + b.y,
3700 };
3701}
3702
3703static struct vec2d
3704vec2d_subtract(struct vec2d a, struct vec2d b)
3705{
3706 return (struct vec2d) {
3707 .x = a.x - b.x,
3708 .y = a.y - b.y,
3709 };
3710}
3711
3712static struct vec2d
3713vec2d_multiply_constant(double c, struct vec2d a)
3714{
3715 return (struct vec2d) {
3716 .x = c * a.x,
3717 .y = c * a.y,
3718 };
3719}
3720
3721static bool
3722lines_intersect(struct line *line1, struct line *line2,
3723 struct vec2d *intersection)
3724{
3725 struct vec2d p = line1->a;
3726 struct vec2d r = vec2d_subtract(line1->b, line1->a);
3727 struct vec2d q = line2->a;
3728 struct vec2d s = vec2d_subtract(line2->b, line2->a);
3729 double rxs;
3730 double sxr;
3731 double t;
3732 double u;
3733
3734 /*
3735 * The line (p, r) and (q, s) intersects where
3736 *
3737 * p + t r = q + u s
3738 *
3739 * Calculate t:
3740 *
3741 * (p + t r) × s = (q + u s) × s
3742 * p × s + t (r × s) = q × s + u (s × s)
3743 * p × s + t (r × s) = q × s
3744 * t (r × s) = q × s - p × s
3745 * t (r × s) = (q - p) × s
3746 * t = ((q - p) × s) / (r × s)
3747 *
3748 * Using the same method, for u we get:
3749 *
3750 * u = ((p - q) × r) / (s × r)
3751 */
3752
3753 rxs = vec2d_cross_product(r, s);
3754 sxr = vec2d_cross_product(s, r);
3755
3756 /* If r × s = 0 then the lines are either parallel or collinear. */
3757 if (fabs(rxs) < DBL_MIN)
3758 return false;
3759
3760 t = vec2d_cross_product(vec2d_subtract(q, p), s) / rxs;
3761 u = vec2d_cross_product(vec2d_subtract(p, q), r) / sxr;
3762
3763 /* The lines only intersect if 0 ≤ t ≤ 1 and 0 ≤ u ≤ 1. */
3764 if (t < 0.0 || t > 1.0 || u < 0.0 || u > 1.0)
3765 return false;
3766
3767 *intersection = vec2d_add(p, vec2d_multiply_constant(t, r));
3768 return true;
3769}
3770
3771static struct border *
3772add_border(struct wl_array *array,
3773 double x1, double y1,
3774 double x2, double y2,
3775 enum motion_direction blocking_dir)
3776{
3777 struct border *border = wl_array_add(array, sizeof *border);
3778
3779 *border = (struct border) {
3780 .line = (struct line) {
3781 .a = (struct vec2d) {
3782 .x = x1,
3783 .y = y1,
3784 },
3785 .b = (struct vec2d) {
3786 .x = x2,
3787 .y = y2,
3788 },
3789 },
3790 .blocking_dir = blocking_dir,
3791 };
3792
3793 return border;
3794}
3795
3796static int
3797compare_lines_x(const void *a, const void *b)
3798{
3799 const struct border *border_a = a;
3800 const struct border *border_b = b;
3801
3802
3803 if (border_a->line.a.x == border_b->line.a.x)
3804 return border_a->line.b.x < border_b->line.b.x;
3805 else
3806 return border_a->line.a.x > border_b->line.a.x;
3807}
3808
3809static void
3810add_non_overlapping_edges(pixman_box32_t *boxes,
3811 int band_above_start,
3812 int band_below_start,
3813 int band_below_end,
3814 struct wl_array *borders)
3815{
3816 int i;
3817 struct wl_array band_merge;
3818 struct border *border;
3819 struct border *prev_border;
3820 struct border *new_border;
3821
3822 wl_array_init(&band_merge);
3823
3824 /* Add bottom band of previous row, and top band of current row, and
3825 * sort them so lower left x coordinate comes first. If there are two
3826 * borders with the same left x coordinate, the wider one comes first.
3827 */
3828 for (i = band_above_start; i < band_below_start; i++) {
3829 pixman_box32_t *box = &boxes[i];
3830 add_border(&band_merge, box->x1, box->y2, box->x2, box->y2,
3831 MOTION_DIRECTION_POSITIVE_Y);
3832 }
3833 for (i = band_below_start; i < band_below_end; i++) {
3834 pixman_box32_t *box= &boxes[i];
3835 add_border(&band_merge, box->x1, box->y1, box->x2, box->y1,
3836 MOTION_DIRECTION_NEGATIVE_Y);
3837 }
3838 qsort(band_merge.data,
3839 band_merge.size / sizeof *border,
3840 sizeof *border,
3841 compare_lines_x);
3842
3843 /* Combine the two combined bands so that any overlapping border is
3844 * eliminated. */
3845 prev_border = NULL;
3846 wl_array_for_each(border, &band_merge) {
3847 assert(border->line.a.y == border->line.b.y);
3848 assert(!prev_border ||
3849 prev_border->line.a.y == border->line.a.y);
3850 assert(!prev_border ||
3851 (prev_border->line.a.x != border->line.a.x ||
3852 prev_border->line.b.x != border->line.b.x));
3853 assert(!prev_border ||
3854 prev_border->line.a.x <= border->line.a.x);
3855
3856 if (prev_border &&
3857 prev_border->line.a.x == border->line.a.x) {
3858 /*
3859 * ------------ +
3860 * ------- =
3861 * [ ]-----
3862 */
3863 prev_border->line.a.x = border->line.b.x;
3864 } else if (prev_border &&
3865 prev_border->line.b.x == border->line.b.x) {
3866 /*
3867 * ------------ +
3868 * ------ =
3869 * ------[ ]
3870 */
3871 prev_border->line.b.x = border->line.a.x;
3872 } else if (prev_border &&
3873 prev_border->line.b.x == border->line.a.x) {
3874 /*
3875 * -------- +
3876 * ------ =
3877 * --------------
3878 */
3879 prev_border->line.b.x = border->line.b.x;
3880 } else if (prev_border &&
3881 prev_border->line.b.x >= border->line.a.x) {
3882 /*
3883 * --------------- +
3884 * ------ =
3885 * -----[ ]----
3886 */
3887 new_border = add_border(borders,
3888 border->line.b.x,
3889 border->line.b.y,
3890 prev_border->line.b.x,
3891 prev_border->line.b.y,
3892 prev_border->blocking_dir);
3893 prev_border->line.b.x = border->line.a.x;
3894 prev_border = new_border;
3895 } else {
3896 assert(!prev_border ||
3897 prev_border->line.b.x < border->line.a.x);
3898 /*
3899 * First border or non-overlapping.
3900 *
3901 * ----- +
3902 * ----- =
3903 * ----- -----
3904 */
3905 new_border = wl_array_add(borders, sizeof *border);
3906 *new_border = *border;
3907 prev_border = new_border;
3908 }
3909 }
3910
3911 wl_array_release(&band_merge);
3912}
3913
3914static void
3915add_band_bottom_edges(pixman_box32_t *boxes,
3916 int band_start,
3917 int band_end,
3918 struct wl_array *borders)
3919{
3920 int i;
3921
3922 for (i = band_start; i < band_end; i++) {
3923 add_border(borders,
3924 boxes[i].x1, boxes[i].y2,
3925 boxes[i].x2, boxes[i].y2,
3926 MOTION_DIRECTION_POSITIVE_Y);
3927 }
3928}
3929
3930static void
3931region_to_outline(pixman_region32_t *region, struct wl_array *borders)
3932{
3933 pixman_box32_t *boxes;
3934 int num_boxes;
3935 int i;
3936 int top_most, bottom_most;
3937 int current_roof;
3938 int prev_top;
3939 int band_start, prev_band_start;
3940
3941 /*
3942 * Remove any overlapping lines from the set of rectangles. Note that
3943 * pixman regions are grouped as rows of rectangles, where rectangles
3944 * in one row never touch or overlap and are all of the same height.
3945 *
3946 * -------- --- -------- ---
3947 * | | | | | | | |
3948 * ----------====---- --- ----------- ----- ---
3949 * | | => | |
3950 * ----==========--------- ----- ----------
3951 * | | | |
3952 * ------------------- -------------------
3953 *
3954 */
3955
3956 boxes = pixman_region32_rectangles(region, &num_boxes);
3957 prev_top = 0;
3958 top_most = boxes[0].y1;
3959 current_roof = top_most;
3960 bottom_most = boxes[num_boxes - 1].y2;
3961 band_start = 0;
3962 prev_band_start = 0;
3963 for (i = 0; i < num_boxes; i++) {
3964 /* Detect if there is a vertical empty space, and add the lower
3965 * level of the previous band if so was the case. */
3966 if (i > 0 &&
3967 boxes[i].y1 != prev_top &&
3968 boxes[i].y1 != boxes[i - 1].y2) {
3969 current_roof = boxes[i].y1;
3970 add_band_bottom_edges(boxes,
3971 band_start,
3972 i,
3973 borders);
3974 }
3975
3976 /* Special case adding the last band, since it won't be handled
3977 * by the band change detection below. */
3978 if (boxes[i].y1 != current_roof && i == num_boxes - 1) {
3979 if (boxes[i].y1 != prev_top) {
3980 /* The last band is a single box, so we don't
3981 * have a prev_band_start to tell us when the
3982 * previous band started. */
3983 add_non_overlapping_edges(boxes,
3984 band_start,
3985 i,
3986 i + 1,
3987 borders);
3988 } else {
3989 add_non_overlapping_edges(boxes,
3990 prev_band_start,
3991 band_start,
3992 i + 1,
3993 borders);
3994 }
3995 }
3996
3997 /* Detect when passing a band and combine the top border of the
3998 * just passed band with the bottom band of the previous band.
3999 */
4000 if (boxes[i].y1 != top_most && boxes[i].y1 != prev_top) {
4001 /* Combine the two passed bands. */
4002 if (prev_top != current_roof) {
4003 add_non_overlapping_edges(boxes,
4004 prev_band_start,
4005 band_start,
4006 i,
4007 borders);
4008 }
4009
4010 prev_band_start = band_start;
4011 band_start = i;
4012 }
4013
4014 /* Add the top border if the box is part of the current roof. */
4015 if (boxes[i].y1 == current_roof) {
4016 add_border(borders,
4017 boxes[i].x1, boxes[i].y1,
4018 boxes[i].x2, boxes[i].y1,
4019 MOTION_DIRECTION_NEGATIVE_Y);
4020 }
4021
4022 /* Add the bottom border of the last band. */
4023 if (boxes[i].y2 == bottom_most) {
4024 add_border(borders,
4025 boxes[i].x1, boxes[i].y2,
4026 boxes[i].x2, boxes[i].y2,
4027 MOTION_DIRECTION_POSITIVE_Y);
4028 }
4029
4030 /* Always add the left border. */
4031 add_border(borders,
4032 boxes[i].x1, boxes[i].y1,
4033 boxes[i].x1, boxes[i].y2,
4034 MOTION_DIRECTION_NEGATIVE_X);
4035
4036 /* Always add the right border. */
4037 add_border(borders,
4038 boxes[i].x2, boxes[i].y1,
4039 boxes[i].x2, boxes[i].y2,
4040 MOTION_DIRECTION_POSITIVE_X);
4041
4042 prev_top = boxes[i].y1;
4043 }
4044}
4045
4046static bool
4047is_border_horizontal (struct border *border)
4048{
4049 return border->line.a.y == border->line.b.y;
4050}
4051
4052static bool
4053is_border_blocking_directions(struct border *border,
4054 uint32_t directions)
4055{
4056 /* Don't block parallel motions. */
4057 if (is_border_horizontal(border)) {
4058 if ((directions & (MOTION_DIRECTION_POSITIVE_Y |
4059 MOTION_DIRECTION_NEGATIVE_Y)) == 0)
4060 return false;
4061 } else {
4062 if ((directions & (MOTION_DIRECTION_POSITIVE_X |
4063 MOTION_DIRECTION_NEGATIVE_X)) == 0)
4064 return false;
4065 }
4066
4067 return (~border->blocking_dir & directions) != directions;
4068}
4069
4070static struct border *
4071get_closest_border(struct wl_array *borders,
4072 struct line *motion,
4073 uint32_t directions)
4074{
4075 struct border *border;
4076 struct vec2d intersection;
4077 struct vec2d delta;
4078 double distance_2;
4079 struct border *closest_border = NULL;
4080 double closest_distance_2 = DBL_MAX;
4081
4082 wl_array_for_each(border, borders) {
4083 if (!is_border_blocking_directions(border, directions))
4084 continue;
4085
4086 if (!lines_intersect(&border->line, motion, &intersection))
4087 continue;
4088
4089 delta = vec2d_subtract(intersection, motion->a);
4090 distance_2 = delta.x*delta.x + delta.y*delta.y;
4091 if (distance_2 < closest_distance_2) {
4092 closest_border = border;
4093 closest_distance_2 = distance_2;
4094 }
4095 }
4096
4097 return closest_border;
4098}
4099
4100static void
4101clamp_to_border(struct border *border,
4102 struct line *motion,
4103 uint32_t *motion_dir)
4104{
4105 /*
4106 * When clamping either rightward or downward motions, the motion needs
4107 * to be clamped so that the destination coordinate does not end up on
4108 * the border (see weston_pointer_clamp_event_to_region). Do this by
4109 * clamping such motions to the border minus the smallest possible
4110 * wl_fixed_t value.
4111 */
4112 if (is_border_horizontal(border)) {
4113 if (*motion_dir & MOTION_DIRECTION_POSITIVE_Y)
4114 motion->b.y = border->line.a.y - wl_fixed_to_double(1);
4115 else
4116 motion->b.y = border->line.a.y;
4117 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_Y |
4118 MOTION_DIRECTION_NEGATIVE_Y);
4119 } else {
4120 if (*motion_dir & MOTION_DIRECTION_POSITIVE_X)
4121 motion->b.x = border->line.a.x - wl_fixed_to_double(1);
4122 else
4123 motion->b.x = border->line.a.x;
4124 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_X |
4125 MOTION_DIRECTION_NEGATIVE_X);
4126 }
4127}
4128
4129static uint32_t
4130get_motion_directions(struct line *motion)
4131{
4132 uint32_t directions = 0;
4133
4134 if (motion->a.x < motion->b.x)
4135 directions |= MOTION_DIRECTION_POSITIVE_X;
4136 else if (motion->a.x > motion->b.x)
4137 directions |= MOTION_DIRECTION_NEGATIVE_X;
4138 if (motion->a.y < motion->b.y)
4139 directions |= MOTION_DIRECTION_POSITIVE_Y;
4140 else if (motion->a.y > motion->b.y)
4141 directions |= MOTION_DIRECTION_NEGATIVE_Y;
4142
4143 return directions;
4144}
4145
Jonas Ådahld3414f22016-07-22 17:56:31 +08004146static void
4147weston_pointer_clamp_event_to_region(struct weston_pointer *pointer,
4148 struct weston_pointer_motion_event *event,
4149 pixman_region32_t *region,
4150 wl_fixed_t *clamped_x,
4151 wl_fixed_t *clamped_y)
4152{
4153 wl_fixed_t x, y;
4154 wl_fixed_t sx, sy;
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004155 wl_fixed_t old_sx = pointer->sx;
4156 wl_fixed_t old_sy = pointer->sy;
4157 struct wl_array borders;
4158 struct line motion;
4159 struct border *closest_border;
4160 float new_x_f, new_y_f;
4161 uint32_t directions;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004162
4163 weston_pointer_motion_to_abs(pointer, event, &x, &y);
4164 weston_view_from_global_fixed(pointer->focus, x, y, &sx, &sy);
4165
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004166 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004167
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004168 /*
4169 * Generate borders given the confine region we are to use. The borders
4170 * are defined to be the outer region of the allowed area. This means
4171 * top/left borders are "within" the allowed area, while bottom/right
4172 * borders are outside. This needs to be considered when clamping
4173 * confined motion vectors.
4174 */
4175 region_to_outline(region, &borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004176
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004177 motion = (struct line) {
4178 .a = (struct vec2d) {
4179 .x = wl_fixed_to_double(old_sx),
4180 .y = wl_fixed_to_double(old_sy),
4181 },
4182 .b = (struct vec2d) {
4183 .x = wl_fixed_to_double(sx),
4184 .y = wl_fixed_to_double(sy),
4185 },
4186 };
4187 directions = get_motion_directions(&motion);
4188
4189 while (directions) {
4190 closest_border = get_closest_border(&borders,
4191 &motion,
4192 directions);
4193 if (closest_border)
4194 clamp_to_border(closest_border, &motion, &directions);
4195 else
4196 break;
4197 }
4198
4199 weston_view_to_global_float(pointer->focus,
4200 (float) motion.b.x, (float) motion.b.y,
4201 &new_x_f, &new_y_f);
4202 *clamped_x = wl_fixed_from_double(new_x_f);
4203 *clamped_y = wl_fixed_from_double(new_y_f);
4204
4205 wl_array_release(&borders);
4206}
4207
4208static double
4209point_to_border_distance_2(struct border *border, double x, double y)
4210{
4211 double orig_x, orig_y;
4212 double dx, dy;
4213
4214 if (is_border_horizontal(border)) {
4215 if (x < border->line.a.x)
4216 orig_x = border->line.a.x;
4217 else if (x > border->line.b.x)
4218 orig_x = border->line.b.x;
4219 else
4220 orig_x = x;
4221 orig_y = border->line.a.y;
4222 } else {
4223 if (y < border->line.a.y)
4224 orig_y = border->line.a.y;
4225 else if (y > border->line.b.y)
4226 orig_y = border->line.b.y;
4227 else
4228 orig_y = y;
4229 orig_x = border->line.a.x;
4230 }
4231
4232
4233 dx = fabs(orig_x - x);
4234 dy = fabs(orig_y - y);
4235 return dx*dx + dy*dy;
4236}
4237
4238static void
4239warp_to_behind_border(struct border *border, wl_fixed_t *sx, wl_fixed_t *sy)
4240{
4241 switch (border->blocking_dir) {
4242 case MOTION_DIRECTION_POSITIVE_X:
4243 case MOTION_DIRECTION_NEGATIVE_X:
4244 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_X)
4245 *sx = wl_fixed_from_double(border->line.a.x) - 1;
4246 else
4247 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4248 if (*sy < wl_fixed_from_double(border->line.a.y))
4249 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4250 else if (*sy > wl_fixed_from_double(border->line.b.y))
4251 *sy = wl_fixed_from_double(border->line.b.y) - 1;
4252 break;
4253 case MOTION_DIRECTION_POSITIVE_Y:
4254 case MOTION_DIRECTION_NEGATIVE_Y:
4255 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_Y)
4256 *sy = wl_fixed_from_double(border->line.a.y) - 1;
4257 else
4258 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4259 if (*sx < wl_fixed_from_double(border->line.a.x))
4260 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4261 else if (*sx > wl_fixed_from_double(border->line.b.x))
4262 *sx = wl_fixed_from_double(border->line.b.x) - 1;
4263 break;
4264 }
Jonas Ådahld3414f22016-07-22 17:56:31 +08004265}
4266
4267static void
4268maybe_warp_confined_pointer(struct weston_pointer_constraint *constraint)
4269{
4270 wl_fixed_t x;
4271 wl_fixed_t y;
4272 wl_fixed_t sx;
4273 wl_fixed_t sy;
4274
4275 weston_view_from_global_fixed(constraint->view,
4276 constraint->pointer->x,
4277 constraint->pointer->y,
4278 &sx,
4279 &sy);
4280
4281 if (!is_within_constraint_region(constraint, sx, sy)) {
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004282 double xf = wl_fixed_to_double(sx);
4283 double yf = wl_fixed_to_double(sy);
4284 pixman_region32_t confine_region;
4285 struct wl_array borders;
4286 struct border *border;
4287 double closest_distance_2 = DBL_MAX;
4288 struct border *closest_border = NULL;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004289
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004290 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004291
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004292 pixman_region32_init(&confine_region);
4293 pixman_region32_intersect(&confine_region,
4294 &constraint->view->surface->input,
4295 &constraint->region);
4296 region_to_outline(&confine_region, &borders);
4297 pixman_region32_fini(&confine_region);
4298
4299 wl_array_for_each(border, &borders) {
4300 double distance_2;
4301
4302 distance_2 = point_to_border_distance_2(border, xf, yf);
4303 if (distance_2 < closest_distance_2) {
4304 closest_border = border;
4305 closest_distance_2 = distance_2;
4306 }
4307 }
4308 assert(closest_border);
4309
4310 warp_to_behind_border(closest_border, &sx, &sy);
4311
4312 wl_array_release(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004313
4314 weston_view_to_global_fixed(constraint->view, sx, sy, &x, &y);
4315 weston_pointer_move_to(constraint->pointer, x, y);
4316 }
4317}
4318
4319static void
4320confined_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02004321 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004322 struct weston_pointer_motion_event *event)
4323{
4324 struct weston_pointer_constraint *constraint =
4325 container_of(grab, struct weston_pointer_constraint, grab);
4326 struct weston_pointer *pointer = grab->pointer;
4327 struct weston_surface *surface;
4328 wl_fixed_t x, y;
4329 wl_fixed_t old_sx = pointer->sx;
4330 wl_fixed_t old_sy = pointer->sy;
4331 pixman_region32_t confine_region;
4332
4333 assert(pointer->focus);
4334 assert(pointer->focus->surface == constraint->surface);
4335
4336 surface = pointer->focus->surface;
4337
4338 pixman_region32_init(&confine_region);
4339 pixman_region32_intersect(&confine_region,
4340 &surface->input,
4341 &constraint->region);
4342 weston_pointer_clamp_event_to_region(pointer, event,
4343 &confine_region, &x, &y);
4344 weston_pointer_move_to(pointer, x, y);
4345 pixman_region32_fini(&confine_region);
4346
4347 weston_view_from_global_fixed(pointer->focus, x, y,
4348 &pointer->sx, &pointer->sy);
4349
4350 if (old_sx != pointer->sx || old_sy != pointer->sy) {
Quentin Glidiccde13452016-08-12 10:41:32 +02004351 pointer_send_motion(pointer, time,
4352 pointer->sx, pointer->sy);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004353 }
4354
Quentin Glidiccde13452016-08-12 10:41:32 +02004355 pointer_send_relative_motion(pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004356}
4357
4358static void
4359confined_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02004360 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004361 uint32_t button,
4362 uint32_t state_w)
4363{
4364 weston_pointer_send_button(grab->pointer, time, button, state_w);
4365}
4366
4367static void
4368confined_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02004369 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004370 struct weston_pointer_axis_event *event)
4371{
4372 weston_pointer_send_axis(grab->pointer, time, event);
4373}
4374
4375static void
4376confined_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
4377 uint32_t source)
4378{
4379 weston_pointer_send_axis_source(grab->pointer, source);
4380}
4381
4382static void
4383confined_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
4384{
4385 weston_pointer_send_frame(grab->pointer);
4386}
4387
4388static void
4389confined_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
4390{
4391 struct weston_pointer_constraint *constraint =
4392 container_of(grab, struct weston_pointer_constraint, grab);
4393
4394 disable_pointer_constraint(constraint);
4395
4396 /* If this is a persistent constraint, re-add the surface destroy signal
4397 * listener only if we are currently not destroying the surface. */
4398 switch (constraint->lifetime) {
4399 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT:
4400 if (constraint->surface->resource)
4401 wl_signal_add(&constraint->surface->destroy_signal,
4402 &constraint->surface_destroy_listener);
4403 break;
4404 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT:
4405 break;
4406 }
4407}
4408
4409static const struct weston_pointer_grab_interface
4410 confined_pointer_grab_interface = {
4411 confined_pointer_grab_pointer_focus,
4412 confined_pointer_grab_pointer_motion,
4413 confined_pointer_grab_pointer_button,
4414 confined_pointer_grab_pointer_axis,
4415 confined_pointer_grab_pointer_axis_source,
4416 confined_pointer_grab_pointer_frame,
4417 confined_pointer_grab_pointer_cancel,
4418};
4419
4420static void
4421confined_pointer_destroy(struct wl_client *client,
4422 struct wl_resource *resource)
4423{
4424 wl_resource_destroy(resource);
4425}
4426
4427static void
4428confined_pointer_set_region(struct wl_client *client,
4429 struct wl_resource *resource,
4430 struct wl_resource *region_resource)
4431{
4432 struct weston_pointer_constraint *constraint =
4433 wl_resource_get_user_data(resource);
4434 struct weston_region *region = region_resource ?
4435 wl_resource_get_user_data(region_resource) : NULL;
4436
4437 if (!constraint)
4438 return;
4439
4440 if (region) {
4441 pixman_region32_copy(&constraint->region_pending,
4442 &region->region);
4443 } else {
4444 pixman_region32_fini(&constraint->region_pending);
4445 region_init_infinite(&constraint->region_pending);
4446 }
4447 constraint->region_is_pending = true;
4448}
4449
4450static const struct zwp_confined_pointer_v1_interface confined_pointer_interface = {
4451 confined_pointer_destroy,
4452 confined_pointer_set_region,
4453};
4454
4455static void
4456pointer_constraints_confine_pointer(struct wl_client *client,
4457 struct wl_resource *resource,
4458 uint32_t id,
4459 struct wl_resource *surface_resource,
4460 struct wl_resource *pointer_resource,
4461 struct wl_resource *region_resource,
4462 uint32_t lifetime)
4463{
4464 struct weston_surface *surface =
4465 wl_resource_get_user_data(surface_resource);
4466 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
4467 struct weston_region *region = region_resource ?
4468 wl_resource_get_user_data(region_resource) : NULL;
4469
4470 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
4471 &zwp_confined_pointer_v1_interface,
4472 &confined_pointer_interface,
4473 &confined_pointer_grab_interface);
4474}
4475
4476static const struct zwp_pointer_constraints_v1_interface pointer_constraints_interface = {
4477 pointer_constraints_destroy,
4478 pointer_constraints_lock_pointer,
4479 pointer_constraints_confine_pointer,
4480};
4481
4482static void
4483bind_pointer_constraints(struct wl_client *client, void *data,
4484 uint32_t version, uint32_t id)
4485{
4486 struct wl_resource *resource;
4487
4488 resource = wl_resource_create(client,
4489 &zwp_pointer_constraints_v1_interface,
4490 1, id);
4491
4492 wl_resource_set_implementation(resource, &pointer_constraints_interface,
4493 NULL, NULL);
4494}
4495
Jonas Ådahl30d61d82014-10-22 21:21:17 +02004496int
4497weston_input_init(struct weston_compositor *compositor)
4498{
4499 if (!wl_global_create(compositor->wl_display,
4500 &zwp_relative_pointer_manager_v1_interface, 1,
4501 compositor, bind_relative_pointer_manager))
4502 return -1;
4503
Jonas Ådahld3414f22016-07-22 17:56:31 +08004504 if (!wl_global_create(compositor->wl_display,
4505 &zwp_pointer_constraints_v1_interface, 1,
4506 NULL, bind_pointer_constraints))
4507 return -1;
4508
Jonas Ådahl30d61d82014-10-22 21:21:17 +02004509 return 0;
4510}