blob: 877b0b83c47dd8d936f5f38733a9204df61b1116 [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
673weston_touch_send_down(struct weston_touch *touch, uint32_t time,
674 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;
681
Quentin Glidiccde13452016-08-12 10:41:32 +0200682 if (!weston_touch_has_focus_resource(touch))
Bryce Harrington2c40d1d2016-02-02 10:18:48 -0800683 return;
684
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300685 weston_view_from_global_fixed(touch->focus, x, y, &sx, &sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400686
Neil Roberts96d790e2013-09-19 17:32:00 +0100687 resource_list = &touch->focus_resource_list;
Quentin Glidiccde13452016-08-12 10:41:32 +0200688 serial = wl_display_next_serial(display);
689 wl_resource_for_each(resource, resource_list)
690 wl_touch_send_down(resource, serial, time,
691 touch->focus->surface->resource,
692 touch_id, sx, sy);
693}
Neil Roberts96d790e2013-09-19 17:32:00 +0100694
Quentin Glidiccde13452016-08-12 10:41:32 +0200695static void
696default_grab_touch_down(struct weston_touch_grab *grab, uint32_t time,
697 int touch_id, wl_fixed_t x, wl_fixed_t y)
698{
699 weston_touch_send_down(grab->touch, time, touch_id, x, y);
700}
701
702/** Send wl_touch.up events to focused resources.
703 *
704 * \param touch The touch where the up events originates from.
705 * \param time The timestamp of the event
706 * \param touch_id The touch_id value of the event
707 *
708 * For every resource that is currently in focus, send a wl_touch.up event
709 * with the passed parameters. The focused resources are the wl_touch
710 * resources of the client which currently has the surface with touch focus.
711 */
712WL_EXPORT void
713weston_touch_send_up(struct weston_touch *touch, uint32_t time, int touch_id)
714{
715 struct wl_display *display = touch->seat->compositor->wl_display;
716 uint32_t serial;
717 struct wl_resource *resource;
718 struct wl_list *resource_list;
719
720 if (!weston_touch_has_focus_resource(touch))
721 return;
722
723 resource_list = &touch->focus_resource_list;
724 serial = wl_display_next_serial(display);
725 wl_resource_for_each(resource, resource_list)
726 wl_touch_send_up(resource, serial, time, touch_id);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400727}
728
Kristian Høgsberge329f362013-05-06 22:19:57 -0400729static void
730default_grab_touch_up(struct weston_touch_grab *grab,
731 uint32_t time, int touch_id)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400732{
Quentin Glidiccde13452016-08-12 10:41:32 +0200733 weston_touch_send_up(grab->touch, time, touch_id);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400734}
735
Quentin Glidiccde13452016-08-12 10:41:32 +0200736/** Send wl_touch.motion events to focused resources.
737 *
738 * \param touch The touch where the motion events originates from.
739 * \param time The timestamp of the event
740 * \param touch_id The touch_id value of the event
741 * \param x The x value of the event
742 * \param y The y value of the event
743 *
744 * For every resource that is currently in focus, send a wl_touch.motion event
745 * with the passed parameters. The focused resources are the wl_touch
746 * resources of the client which currently has the surface with touch focus.
747 */
748WL_EXPORT void
749weston_touch_send_motion(struct weston_touch *touch, uint32_t time,
750 int touch_id, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400751{
Neil Roberts96d790e2013-09-19 17:32:00 +0100752 struct wl_resource *resource;
753 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300754 wl_fixed_t sx, sy;
755
Quentin Glidiccde13452016-08-12 10:41:32 +0200756 if (!weston_touch_has_focus_resource(touch))
757 return;
758
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300759 weston_view_from_global_fixed(touch->focus, x, y, &sx, &sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400760
Neil Roberts96d790e2013-09-19 17:32:00 +0100761 resource_list = &touch->focus_resource_list;
Neil Roberts96d790e2013-09-19 17:32:00 +0100762 wl_resource_for_each(resource, resource_list) {
763 wl_touch_send_motion(resource, time,
Kristian Høgsberge329f362013-05-06 22:19:57 -0400764 touch_id, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400765 }
766}
767
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200768static void
Quentin Glidiccde13452016-08-12 10:41:32 +0200769default_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time,
770 int touch_id, wl_fixed_t x, wl_fixed_t y)
771{
772 weston_touch_send_motion(grab->touch, time, touch_id, x, y);
773}
774
775
776/** Send wl_touch.frame events to focused resources.
777 *
778 * \param touch The touch where the frame events originates from.
779 *
780 * For every resource that is currently in focus, send a wl_touch.frame event.
781 * The focused resources are the wl_touch resources of the client which
782 * currently has the surface with touch focus.
783 */
784WL_EXPORT void
785weston_touch_send_frame(struct weston_touch *touch)
Jonas Ådahl1679f232014-04-12 09:39:51 +0200786{
787 struct wl_resource *resource;
788
Quentin Glidiccde13452016-08-12 10:41:32 +0200789 if (!weston_touch_has_focus_resource(touch))
790 return;
791
792 wl_resource_for_each(resource, &touch->focus_resource_list)
Jonas Ådahl1679f232014-04-12 09:39:51 +0200793 wl_touch_send_frame(resource);
794}
795
796static void
Quentin Glidiccde13452016-08-12 10:41:32 +0200797default_grab_touch_frame(struct weston_touch_grab *grab)
798{
799 weston_touch_send_frame(grab->touch);
800}
801
802static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200803default_grab_touch_cancel(struct weston_touch_grab *grab)
804{
805}
806
Kristian Høgsberge329f362013-05-06 22:19:57 -0400807static const struct weston_touch_grab_interface default_touch_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400808 default_grab_touch_down,
809 default_grab_touch_up,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200810 default_grab_touch_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +0200811 default_grab_touch_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200812 default_grab_touch_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400813};
814
Quentin Glidiccde13452016-08-12 10:41:32 +0200815/** Check if the keyboard has focused resources.
816 *
817 * \param keyboard The keyboard to check for focused resources.
818 * \return Whether or not this keyboard has focused resources
819 */
820WL_EXPORT bool
821weston_keyboard_has_focus_resource(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400822{
Quentin Glidiccde13452016-08-12 10:41:32 +0200823 if (!keyboard->focus)
824 return false;
825
826 if (wl_list_empty(&keyboard->focus_resource_list))
827 return false;
828
829 return true;
830}
831
832/** Send wl_keyboard.key events to focused resources.
833 *
834 * \param keyboard The keyboard where the key events originates from.
835 * \param time The timestamp of the event
836 * \param key The key value of the event
837 * \param state The state enum value of the event
838 *
839 * For every resource that is currently in focus, send a wl_keyboard.key event
840 * with the passed parameters. The focused resources are the wl_keyboard
841 * resources of the client which currently has the surface with keyboard focus.
842 */
843WL_EXPORT void
844weston_keyboard_send_key(struct weston_keyboard *keyboard,
845 uint32_t time, uint32_t key,
846 enum wl_keyboard_key_state state)
847{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400848 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +0100849 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400850 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100851 struct wl_list *resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400852
Quentin Glidiccde13452016-08-12 10:41:32 +0200853 if (!weston_keyboard_has_focus_resource(keyboard))
854 return;
855
Neil Roberts96d790e2013-09-19 17:32:00 +0100856 resource_list = &keyboard->focus_resource_list;
Quentin Glidiccde13452016-08-12 10:41:32 +0200857 serial = wl_display_next_serial(display);
858 wl_resource_for_each(resource, resource_list)
859 wl_keyboard_send_key(resource, serial, time, key, state);
860};
861
862static void
863default_grab_keyboard_key(struct weston_keyboard_grab *grab,
864 uint32_t time, uint32_t key, uint32_t state)
865{
866 weston_keyboard_send_key(grab->keyboard, time, key, state);
Neil Roberts96d790e2013-09-19 17:32:00 +0100867}
868
869static void
870send_modifiers_to_resource(struct weston_keyboard *keyboard,
871 struct wl_resource *resource,
872 uint32_t serial)
873{
874 wl_keyboard_send_modifiers(resource,
875 serial,
876 keyboard->modifiers.mods_depressed,
877 keyboard->modifiers.mods_latched,
878 keyboard->modifiers.mods_locked,
879 keyboard->modifiers.group);
880}
881
882static void
883send_modifiers_to_client_in_list(struct wl_client *client,
884 struct wl_list *list,
885 uint32_t serial,
886 struct weston_keyboard *keyboard)
887{
888 struct wl_resource *resource;
889
890 wl_resource_for_each(resource, list) {
891 if (wl_resource_get_client(resource) == client)
892 send_modifiers_to_resource(keyboard,
893 resource,
894 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400895 }
896}
897
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800898static struct weston_pointer_client *
899find_pointer_client_for_surface(struct weston_pointer *pointer,
900 struct weston_surface *surface)
901{
902 struct wl_client *client;
903
904 if (!surface)
905 return NULL;
906
907 if (!surface->resource)
908 return NULL;
909
910 client = wl_resource_get_client(surface->resource);
911 return weston_pointer_get_pointer_client(pointer, client);
912}
913
914static struct weston_pointer_client *
915find_pointer_client_for_view(struct weston_pointer *pointer, struct weston_view *view)
916{
917 if (!view)
918 return NULL;
919
920 return find_pointer_client_for_surface(pointer, view->surface);
921}
922
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400923static struct wl_resource *
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400924find_resource_for_surface(struct wl_list *list, struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400925{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400926 if (!surface)
927 return NULL;
928
Jason Ekstrand44a38632013-06-14 10:08:00 -0500929 if (!surface->resource)
930 return NULL;
Stefan Schmidtfda26522013-09-17 10:54:09 +0100931
Jason Ekstrand44a38632013-06-14 10:08:00 -0500932 return wl_resource_find_for_client(list, wl_resource_get_client(surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400933}
934
Quentin Glidiccde13452016-08-12 10:41:32 +0200935/** Send wl_keyboard.modifiers events to focused resources and pointer
936 * focused resources.
937 *
938 * \param keyboard The keyboard where the modifiers events originates from.
939 * \param serial The serial of the event
940 * \param mods_depressed The mods_depressed value of the event
941 * \param mods_latched The mods_latched value of the event
942 * \param mods_locked The mods_locked value of the event
943 * \param group The group value of the event
944 *
945 * For every resource that is currently in focus, send a wl_keyboard.modifiers
946 * event with the passed parameters. The focused resources are the wl_keyboard
947 * resources of the client which currently has the surface with keyboard focus.
948 * This also sends wl_keyboard.modifiers events to the wl_keyboard resources of
949 * the client having pointer focus (if different from the keyboard focus client).
950 */
951WL_EXPORT void
952weston_keyboard_send_modifiers(struct weston_keyboard *keyboard,
953 uint32_t serial, uint32_t mods_depressed,
954 uint32_t mods_latched,
955 uint32_t mods_locked, uint32_t group)
956{
957 struct weston_pointer *pointer =
958 weston_seat_get_pointer(keyboard->seat);
959
960 if (weston_keyboard_has_focus_resource(keyboard)) {
961 struct wl_list *resource_list;
962 struct wl_resource *resource;
963
964 resource_list = &keyboard->focus_resource_list;
965 wl_resource_for_each(resource, resource_list) {
966 wl_keyboard_send_modifiers(resource, serial,
967 mods_depressed, mods_latched,
968 mods_locked, group);
969 }
970 }
971
972 if (pointer && pointer->focus && pointer->focus->surface->resource &&
973 pointer->focus->surface != keyboard->focus) {
974 struct wl_client *pointer_client =
975 wl_resource_get_client(pointer->focus->surface->resource);
976
977 send_modifiers_to_client_in_list(pointer_client,
978 &keyboard->resource_list,
979 serial,
980 keyboard);
981 }
982}
983
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400984static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700985default_grab_keyboard_modifiers(struct weston_keyboard_grab *grab,
986 uint32_t serial, uint32_t mods_depressed,
987 uint32_t mods_latched,
988 uint32_t mods_locked, uint32_t group)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400989{
Quentin Glidiccde13452016-08-12 10:41:32 +0200990 weston_keyboard_send_modifiers(grab->keyboard, serial, mods_depressed,
991 mods_latched, mods_locked, group);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400992}
993
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200994static void
995default_grab_keyboard_cancel(struct weston_keyboard_grab *grab)
996{
997}
998
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400999static const struct weston_keyboard_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001000 default_keyboard_grab_interface = {
Kristian Høgsbergb27901c2013-10-28 15:32:02 -07001001 default_grab_keyboard_key,
1002 default_grab_keyboard_modifiers,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001003 default_grab_keyboard_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001004};
1005
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001006static void
1007pointer_unmap_sprite(struct weston_pointer *pointer)
1008{
Pekka Paalanenc557ff72014-11-12 16:42:52 +02001009 struct weston_surface *surface = pointer->sprite->surface;
1010
1011 if (weston_surface_is_mapped(surface))
1012 weston_surface_unmap(surface);
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001013
1014 wl_list_remove(&pointer->sprite_destroy_listener.link);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02001015 surface->committed = NULL;
1016 surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03001017 weston_surface_set_label_func(surface, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001018 weston_view_destroy(pointer->sprite);
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001019 pointer->sprite = NULL;
1020}
1021
1022static void
1023pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
1024{
1025 struct weston_pointer *pointer =
1026 container_of(listener, struct weston_pointer,
1027 sprite_destroy_listener);
1028
1029 pointer->sprite = NULL;
1030}
1031
Jonas Ådahl3e12e632013-12-02 22:05:05 +01001032static void
1033weston_pointer_reset_state(struct weston_pointer *pointer)
1034{
1035 pointer->button_count = 0;
1036}
1037
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001038static void
1039weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data);
1040
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001041WL_EXPORT struct weston_pointer *
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001042weston_pointer_create(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001043{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001044 struct weston_pointer *pointer;
1045
Peter Huttererf3d62272013-08-08 11:57:05 +10001046 pointer = zalloc(sizeof *pointer);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001047 if (pointer == NULL)
1048 return NULL;
1049
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001050 wl_list_init(&pointer->pointer_clients);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001051 weston_pointer_set_default_grab(pointer,
1052 seat->compositor->default_pointer_grab);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001053 wl_list_init(&pointer->focus_resource_listener.link);
1054 pointer->focus_resource_listener.notify = pointer_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001055 pointer->default_grab.pointer = pointer;
1056 pointer->grab = &pointer->default_grab;
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001057 wl_signal_init(&pointer->motion_signal);
Emilio Pozuelo Monfortaa7a4762013-11-19 11:37:15 +01001058 wl_signal_init(&pointer->focus_signal);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001059 wl_list_init(&pointer->focus_view_listener.link);
Jonas Ådahl3eb4ddd2016-07-22 17:52:58 +08001060 wl_signal_init(&pointer->destroy_signal);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001061
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001062 pointer->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
1063
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001064 /* FIXME: Pick better co-ords. */
1065 pointer->x = wl_fixed_from_int(100);
1066 pointer->y = wl_fixed_from_int(100);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001067
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001068 pointer->output_destroy_listener.notify =
1069 weston_pointer_handle_output_destroy;
1070 wl_signal_add(&seat->compositor->output_destroyed_signal,
1071 &pointer->output_destroy_listener);
1072
Derek Foremanf9318d12015-05-11 15:40:11 -05001073 pointer->sx = wl_fixed_from_int(-1000000);
1074 pointer->sy = wl_fixed_from_int(-1000000);
1075
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001076 return pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001077}
1078
1079WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001080weston_pointer_destroy(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001081{
Jonas Ådahl3eb4ddd2016-07-22 17:52:58 +08001082 wl_signal_emit(&pointer->destroy_signal, pointer);
1083
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001084 if (pointer->sprite)
1085 pointer_unmap_sprite(pointer);
1086
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001087 /* XXX: What about pointer->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +01001088
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001089 wl_list_remove(&pointer->focus_resource_listener.link);
1090 wl_list_remove(&pointer->focus_view_listener.link);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001091 wl_list_remove(&pointer->output_destroy_listener.link);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001092 free(pointer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001093}
1094
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001095void
1096weston_pointer_set_default_grab(struct weston_pointer *pointer,
1097 const struct weston_pointer_grab_interface *interface)
1098{
1099 if (interface)
1100 pointer->default_grab.interface = interface;
1101 else
1102 pointer->default_grab.interface =
1103 &default_pointer_grab_interface;
1104}
1105
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001106WL_EXPORT struct weston_keyboard *
1107weston_keyboard_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001108{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001109 struct weston_keyboard *keyboard;
1110
Peter Huttererf3d62272013-08-08 11:57:05 +10001111 keyboard = zalloc(sizeof *keyboard);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001112 if (keyboard == NULL)
Neil Roberts96d790e2013-09-19 17:32:00 +01001113 return NULL;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001114
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001115 wl_list_init(&keyboard->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +01001116 wl_list_init(&keyboard->focus_resource_list);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001117 wl_list_init(&keyboard->focus_resource_listener.link);
1118 keyboard->focus_resource_listener.notify = keyboard_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001119 wl_array_init(&keyboard->keys);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001120 keyboard->default_grab.interface = &default_keyboard_grab_interface;
1121 keyboard->default_grab.keyboard = keyboard;
1122 keyboard->grab = &keyboard->default_grab;
1123 wl_signal_init(&keyboard->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001124
1125 return keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001126}
1127
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001128static void
1129weston_xkb_info_destroy(struct weston_xkb_info *xkb_info);
1130
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001131WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001132weston_keyboard_destroy(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001133{
1134 /* XXX: What about keyboard->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +01001135
Derek Foreman185d1582017-06-28 11:17:23 -05001136 xkb_state_unref(keyboard->xkb_state.state);
1137 if (keyboard->xkb_info)
1138 weston_xkb_info_destroy(keyboard->xkb_info);
1139 xkb_keymap_unref(keyboard->pending_keymap);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001140
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001141 wl_array_release(&keyboard->keys);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001142 wl_list_remove(&keyboard->focus_resource_listener.link);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001143 free(keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001144}
1145
Jonas Ådahlcbfa7f72013-12-02 22:05:04 +01001146static void
1147weston_touch_reset_state(struct weston_touch *touch)
1148{
1149 touch->num_tp = 0;
1150}
1151
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001152WL_EXPORT struct weston_touch *
1153weston_touch_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001154{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001155 struct weston_touch *touch;
1156
Peter Huttererf3d62272013-08-08 11:57:05 +10001157 touch = zalloc(sizeof *touch);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001158 if (touch == NULL)
1159 return NULL;
1160
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001161 wl_list_init(&touch->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +01001162 wl_list_init(&touch->focus_resource_list);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001163 wl_list_init(&touch->focus_view_listener.link);
1164 touch->focus_view_listener.notify = touch_focus_view_destroyed;
1165 wl_list_init(&touch->focus_resource_listener.link);
1166 touch->focus_resource_listener.notify = touch_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001167 touch->default_grab.interface = &default_touch_grab_interface;
1168 touch->default_grab.touch = touch;
1169 touch->grab = &touch->default_grab;
1170 wl_signal_init(&touch->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001171
1172 return touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001173}
1174
1175WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001176weston_touch_destroy(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001177{
1178 /* XXX: What about touch->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +01001179
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001180 wl_list_remove(&touch->focus_view_listener.link);
1181 wl_list_remove(&touch->focus_resource_listener.link);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001182 free(touch);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001183}
1184
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001185static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04001186seat_send_updated_caps(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001187{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001188 enum wl_seat_capability caps = 0;
Rob Bradford6e737f52013-09-06 17:48:19 +01001189 struct wl_resource *resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001190
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001191 if (seat->pointer_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001192 caps |= WL_SEAT_CAPABILITY_POINTER;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001193 if (seat->keyboard_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001194 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001195 if (seat->touch_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001196 caps |= WL_SEAT_CAPABILITY_TOUCH;
1197
Rob Bradford6e737f52013-09-06 17:48:19 +01001198 wl_resource_for_each(resource, &seat->base_resource_list) {
1199 wl_seat_send_capabilities(resource, caps);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001200 }
Jason Ekstranda4ab5422014-04-02 19:53:45 -05001201 wl_signal_emit(&seat->updated_caps_signal, seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001202}
1203
Derek Foremanf9318d12015-05-11 15:40:11 -05001204
1205/** Clear the pointer focus
1206 *
1207 * \param pointer the pointer to clear focus for.
1208 *
1209 * This can be used to unset pointer focus and set the co-ordinates to the
1210 * arbitrary values we use for the no focus case.
1211 *
1212 * There's no requirement to use this function. For example, passing the
1213 * results of a weston_compositor_pick_view() directly to
1214 * weston_pointer_set_focus() will do the right thing when no view is found.
1215 */
1216WL_EXPORT void
1217weston_pointer_clear_focus(struct weston_pointer *pointer)
1218{
1219 weston_pointer_set_focus(pointer, NULL,
1220 wl_fixed_from_int(-1000000),
1221 wl_fixed_from_int(-1000000));
1222}
1223
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001224WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001225weston_pointer_set_focus(struct weston_pointer *pointer,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001226 struct weston_view *view,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001227 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001228{
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001229 struct weston_pointer_client *pointer_client;
Derek Foreman1281a362015-07-31 16:55:32 -05001230 struct weston_keyboard *kbd = weston_seat_get_keyboard(pointer->seat);
Neil Roberts96d790e2013-09-19 17:32:00 +01001231 struct wl_resource *resource;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001232 struct wl_resource *surface_resource;
Rob Bradford880ebc72013-07-22 17:31:38 +01001233 struct wl_display *display = pointer->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001234 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01001235 struct wl_list *focus_resource_list;
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001236 int refocus = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001237
1238 if ((!pointer->focus && view) ||
1239 (pointer->focus && !view) ||
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001240 (pointer->focus && pointer->focus->surface != view->surface) ||
1241 pointer->sx != sx || pointer->sy != sy)
1242 refocus = 1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001243
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001244 if (pointer->focus_client && refocus) {
1245 focus_resource_list = &pointer->focus_client->pointer_resources;
1246 if (!wl_list_empty(focus_resource_list)) {
1247 serial = wl_display_next_serial(display);
1248 surface_resource = pointer->focus->surface->resource;
1249 wl_resource_for_each(resource, focus_resource_list) {
1250 wl_pointer_send_leave(resource, serial,
1251 surface_resource);
Peter Hutterer87743e92016-01-18 16:38:22 +10001252 pointer_send_frame(resource);
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001253 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001254 }
1255
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001256 pointer->focus_client = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001257 }
1258
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001259 pointer_client = find_pointer_client_for_view(pointer, view);
1260 if (pointer_client && refocus) {
1261 struct wl_client *surface_client = pointer_client->client;
Neil Roberts96d790e2013-09-19 17:32:00 +01001262
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001263 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001264
Jason Ekstranda7af7042013-10-12 22:38:11 -05001265 if (kbd && kbd->focus != view->surface)
Kristian Høgsbergcb406f12013-10-09 10:54:03 -07001266 send_modifiers_to_client_in_list(surface_client,
1267 &kbd->resource_list,
1268 serial,
1269 kbd);
1270
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001271 pointer->focus_client = pointer_client;
Neil Roberts96d790e2013-09-19 17:32:00 +01001272
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001273 focus_resource_list = &pointer->focus_client->pointer_resources;
Neil Roberts96d790e2013-09-19 17:32:00 +01001274 wl_resource_for_each(resource, focus_resource_list) {
1275 wl_pointer_send_enter(resource,
1276 serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001277 view->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01001278 sx, sy);
Peter Hutterer87743e92016-01-18 16:38:22 +10001279 pointer_send_frame(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001280 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001281
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001282 pointer->focus_serial = serial;
1283 }
1284
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001285 wl_list_remove(&pointer->focus_view_listener.link);
1286 wl_list_init(&pointer->focus_view_listener.link);
1287 wl_list_remove(&pointer->focus_resource_listener.link);
1288 wl_list_init(&pointer->focus_resource_listener.link);
Emilio Pozuelo Monfortaa7a4762013-11-19 11:37:15 +01001289 if (view)
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001290 wl_signal_add(&view->destroy_signal, &pointer->focus_view_listener);
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001291 if (view && view->surface->resource)
1292 wl_resource_add_destroy_listener(view->surface->resource,
1293 &pointer->focus_resource_listener);
1294
1295 pointer->focus = view;
1296 pointer->focus_view_listener.notify = pointer_focus_view_destroyed;
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001297 pointer->sx = sx;
1298 pointer->sy = sy;
1299
Derek Foremanf9318d12015-05-11 15:40:11 -05001300 assert(view || sx == wl_fixed_from_int(-1000000));
1301 assert(view || sy == wl_fixed_from_int(-1000000));
1302
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001303 wl_signal_emit(&pointer->focus_signal, pointer);
1304}
1305
Neil Roberts96d790e2013-09-19 17:32:00 +01001306static void
1307send_enter_to_resource_list(struct wl_list *list,
1308 struct weston_keyboard *keyboard,
1309 struct weston_surface *surface,
1310 uint32_t serial)
1311{
1312 struct wl_resource *resource;
1313
1314 wl_resource_for_each(resource, list) {
1315 send_modifiers_to_resource(keyboard, resource, serial);
1316 wl_keyboard_send_enter(resource, serial,
1317 surface->resource,
1318 &keyboard->keys);
1319 }
1320}
1321
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001322WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001323weston_keyboard_set_focus(struct weston_keyboard *keyboard,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001324 struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001325{
1326 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +01001327 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001328 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01001329 struct wl_list *focus_resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001330
Neil Roberts96d790e2013-09-19 17:32:00 +01001331 focus_resource_list = &keyboard->focus_resource_list;
1332
1333 if (!wl_list_empty(focus_resource_list) && keyboard->focus != surface) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001334 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001335 wl_resource_for_each(resource, focus_resource_list) {
1336 wl_keyboard_send_leave(resource, serial,
1337 keyboard->focus->resource);
1338 }
1339 move_resources(&keyboard->resource_list, focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001340 }
1341
Neil Roberts96d790e2013-09-19 17:32:00 +01001342 if (find_resource_for_surface(&keyboard->resource_list, surface) &&
1343 keyboard->focus != surface) {
1344 struct wl_client *surface_client =
1345 wl_resource_get_client(surface->resource);
1346
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001347 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001348
1349 move_resources_for_client(focus_resource_list,
1350 &keyboard->resource_list,
1351 surface_client);
1352 send_enter_to_resource_list(focus_resource_list,
1353 keyboard,
1354 surface,
1355 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001356 keyboard->focus_serial = serial;
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001357 }
1358
1359 wl_list_remove(&keyboard->focus_resource_listener.link);
1360 wl_list_init(&keyboard->focus_resource_listener.link);
1361 if (surface && surface->resource)
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001362 wl_resource_add_destroy_listener(surface->resource,
1363 &keyboard->focus_resource_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001364
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001365 keyboard->focus = surface;
1366 wl_signal_emit(&keyboard->focus_signal, keyboard);
1367}
1368
Giulio Camuffoa20ca812014-11-22 11:16:56 +02001369/* Users of this function must manually manage the keyboard focus */
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001370WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001371weston_keyboard_start_grab(struct weston_keyboard *keyboard,
1372 struct weston_keyboard_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001373{
1374 keyboard->grab = grab;
1375 grab->keyboard = keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001376}
1377
1378WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001379weston_keyboard_end_grab(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001380{
1381 keyboard->grab = &keyboard->default_grab;
1382}
1383
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001384static void
1385weston_keyboard_cancel_grab(struct weston_keyboard *keyboard)
1386{
1387 keyboard->grab->interface->cancel(keyboard->grab);
1388}
1389
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001390WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001391weston_pointer_start_grab(struct weston_pointer *pointer,
1392 struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001393{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001394 pointer->grab = grab;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001395 grab->pointer = pointer;
Kristian Høgsbergda751b82013-07-04 00:58:07 -04001396 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001397}
1398
1399WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001400weston_pointer_end_grab(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001401{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001402 pointer->grab = &pointer->default_grab;
Kristian Høgsbergda751b82013-07-04 00:58:07 -04001403 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001404}
1405
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001406static void
1407weston_pointer_cancel_grab(struct weston_pointer *pointer)
1408{
1409 pointer->grab->interface->cancel(pointer->grab);
1410}
1411
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001412WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -04001413weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001414{
1415 touch->grab = grab;
1416 grab->touch = touch;
1417}
1418
1419WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -04001420weston_touch_end_grab(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001421{
1422 touch->grab = &touch->default_grab;
1423}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001424
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001425static void
1426weston_touch_cancel_grab(struct weston_touch *touch)
1427{
1428 touch->grab->interface->cancel(touch->grab);
1429}
1430
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001431static void
1432weston_pointer_clamp_for_output(struct weston_pointer *pointer,
1433 struct weston_output *output,
1434 wl_fixed_t *fx, wl_fixed_t *fy)
1435{
1436 int x, y;
1437
1438 x = wl_fixed_to_int(*fx);
1439 y = wl_fixed_to_int(*fy);
1440
1441 if (x < output->x)
1442 *fx = wl_fixed_from_int(output->x);
1443 else if (x >= output->x + output->width)
1444 *fx = wl_fixed_from_int(output->x +
1445 output->width - 1);
1446 if (y < output->y)
1447 *fy = wl_fixed_from_int(output->y);
1448 else if (y >= output->y + output->height)
1449 *fy = wl_fixed_from_int(output->y +
1450 output->height - 1);
1451}
1452
Rob Bradford806d8c02013-06-25 18:56:41 +01001453WL_EXPORT void
1454weston_pointer_clamp(struct weston_pointer *pointer, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001455{
Rob Bradford806d8c02013-06-25 18:56:41 +01001456 struct weston_compositor *ec = pointer->seat->compositor;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001457 struct weston_output *output, *prev = NULL;
1458 int x, y, old_x, old_y, valid = 0;
1459
1460 x = wl_fixed_to_int(*fx);
1461 y = wl_fixed_to_int(*fy);
Rob Bradford806d8c02013-06-25 18:56:41 +01001462 old_x = wl_fixed_to_int(pointer->x);
1463 old_y = wl_fixed_to_int(pointer->y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001464
1465 wl_list_for_each(output, &ec->output_list, link) {
Rob Bradford66bd9f52013-06-25 18:56:42 +01001466 if (pointer->seat->output && pointer->seat->output != output)
1467 continue;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001468 if (pixman_region32_contains_point(&output->region,
1469 x, y, NULL))
1470 valid = 1;
1471 if (pixman_region32_contains_point(&output->region,
1472 old_x, old_y, NULL))
1473 prev = output;
1474 }
1475
Rob Bradford66bd9f52013-06-25 18:56:42 +01001476 if (!prev)
1477 prev = pointer->seat->output;
1478
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001479 if (prev && !valid)
1480 weston_pointer_clamp_for_output(pointer, prev, fx, fy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001481}
1482
Jonas Ådahld2510102014-10-05 21:39:14 +02001483static void
1484weston_pointer_move_to(struct weston_pointer *pointer,
1485 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001486{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001487 int32_t ix, iy;
1488
Rob Bradford806d8c02013-06-25 18:56:41 +01001489 weston_pointer_clamp (pointer, &x, &y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001490
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001491 pointer->x = x;
1492 pointer->y = y;
1493
1494 ix = wl_fixed_to_int(x);
1495 iy = wl_fixed_to_int(y);
1496
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001497 if (pointer->sprite) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001498 weston_view_set_position(pointer->sprite,
1499 ix - pointer->hotspot_x,
1500 iy - pointer->hotspot_y);
1501 weston_view_schedule_repaint(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001502 }
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001503
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001504 pointer->grab->interface->focus(pointer->grab);
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001505 wl_signal_emit(&pointer->motion_signal, pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001506}
1507
Jonas Ådahld2510102014-10-05 21:39:14 +02001508WL_EXPORT void
Jonas Ådahld2510102014-10-05 21:39:14 +02001509weston_pointer_move(struct weston_pointer *pointer,
1510 struct weston_pointer_motion_event *event)
1511{
1512 wl_fixed_t x, y;
1513
1514 weston_pointer_motion_to_abs(pointer, event, &x, &y);
1515 weston_pointer_move_to(pointer, x, y);
1516}
1517
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001518/** Verify if the pointer is in a valid position and move it if it isn't.
1519 */
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001520static void
1521weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001522{
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001523 struct weston_pointer *pointer;
1524 struct weston_compositor *ec;
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001525 struct weston_output *output, *closest = NULL;
1526 int x, y, distance, min = INT_MAX;
1527 wl_fixed_t fx, fy;
1528
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001529 pointer = container_of(listener, struct weston_pointer,
1530 output_destroy_listener);
1531 ec = pointer->seat->compositor;
1532
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001533 x = wl_fixed_to_int(pointer->x);
1534 y = wl_fixed_to_int(pointer->y);
1535
1536 wl_list_for_each(output, &ec->output_list, link) {
1537 if (pixman_region32_contains_point(&output->region,
1538 x, y, NULL))
1539 return;
1540
1541 /* Aproximante the distance from the pointer to the center of
1542 * the output. */
1543 distance = abs(output->x + output->width / 2 - x) +
1544 abs(output->y + output->height / 2 - y);
1545 if (distance < min) {
1546 min = distance;
1547 closest = output;
1548 }
1549 }
1550
1551 /* Nothing to do if there's no output left. */
1552 if (!closest)
1553 return;
1554
1555 fx = pointer->x;
1556 fy = pointer->y;
1557
1558 weston_pointer_clamp_for_output(pointer, closest, &fx, &fy);
Jonas Ådahld2510102014-10-05 21:39:14 +02001559 weston_pointer_move_to(pointer, fx, fy);
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001560}
1561
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001562WL_EXPORT void
1563notify_motion(struct weston_seat *seat,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001564 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001565 struct weston_pointer_motion_event *event)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001566{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001567 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001568 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001569
1570 weston_compositor_wake(ec);
Jonas Ådahld2510102014-10-05 21:39:14 +02001571 pointer->grab->interface->motion(pointer->grab, time, event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001572}
1573
Daniel Stone96d47c02013-11-19 11:37:12 +01001574static void
1575run_modifier_bindings(struct weston_seat *seat, uint32_t old, uint32_t new)
1576{
1577 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001578 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Daniel Stone96d47c02013-11-19 11:37:12 +01001579 uint32_t diff;
1580 unsigned int i;
1581 struct {
1582 uint32_t xkb;
1583 enum weston_keyboard_modifier weston;
1584 } mods[] = {
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001585 { keyboard->xkb_info->ctrl_mod, MODIFIER_CTRL },
1586 { keyboard->xkb_info->alt_mod, MODIFIER_ALT },
1587 { keyboard->xkb_info->super_mod, MODIFIER_SUPER },
1588 { keyboard->xkb_info->shift_mod, MODIFIER_SHIFT },
Daniel Stone96d47c02013-11-19 11:37:12 +01001589 };
1590
1591 diff = new & ~old;
1592 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
1593 if (diff & (1 << mods[i].xkb))
1594 weston_compositor_run_modifier_binding(compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001595 keyboard,
Daniel Stone96d47c02013-11-19 11:37:12 +01001596 mods[i].weston,
1597 WL_KEYBOARD_KEY_STATE_PRESSED);
1598 }
1599
1600 diff = old & ~new;
1601 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
1602 if (diff & (1 << mods[i].xkb))
1603 weston_compositor_run_modifier_binding(compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001604 keyboard,
Daniel Stone96d47c02013-11-19 11:37:12 +01001605 mods[i].weston,
1606 WL_KEYBOARD_KEY_STATE_RELEASED);
1607 }
1608}
1609
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001610WL_EXPORT void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001611notify_motion_absolute(struct weston_seat *seat, const struct timespec *time,
1612 double x, double y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001613{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001614 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001615 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Jonas Ådahld2510102014-10-05 21:39:14 +02001616 struct weston_pointer_motion_event event = { 0 };
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001617
1618 weston_compositor_wake(ec);
Jonas Ådahld2510102014-10-05 21:39:14 +02001619
1620 event = (struct weston_pointer_motion_event) {
1621 .mask = WESTON_POINTER_MOTION_ABS,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001622 .x = x,
1623 .y = y,
Jonas Ådahld2510102014-10-05 21:39:14 +02001624 };
1625
1626 pointer->grab->interface->motion(pointer->grab, time, &event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001627}
1628
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02001629static unsigned int
1630peek_next_activate_serial(struct weston_compositor *c)
1631{
1632 unsigned serial = c->activate_serial + 1;
1633
1634 return serial == 0 ? 1 : serial;
1635}
1636
1637static void
1638inc_activate_serial(struct weston_compositor *c)
1639{
1640 c->activate_serial = peek_next_activate_serial (c);
1641}
1642
1643WL_EXPORT void
1644weston_view_activate(struct weston_view *view,
1645 struct weston_seat *seat,
1646 uint32_t flags)
1647{
1648 struct weston_compositor *compositor = seat->compositor;
1649
1650 if (flags & WESTON_ACTIVATE_FLAG_CLICKED) {
1651 view->click_to_activate_serial =
1652 peek_next_activate_serial(compositor);
1653 }
1654
1655 weston_seat_set_keyboard_focus(seat, view->surface);
1656}
1657
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001658WL_EXPORT void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001659notify_button(struct weston_seat *seat, const struct timespec *time,
1660 int32_t button, enum wl_pointer_button_state state)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001661{
1662 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001663 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001664
1665 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001666 weston_compositor_idle_inhibit(compositor);
1667 if (pointer->button_count == 0) {
1668 pointer->grab_button = button;
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001669 pointer->grab_time = *time;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001670 pointer->grab_x = pointer->x;
1671 pointer->grab_y = pointer->y;
1672 }
1673 pointer->button_count++;
1674 } else {
1675 weston_compositor_idle_release(compositor);
1676 pointer->button_count--;
1677 }
1678
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001679 weston_compositor_run_button_binding(compositor, pointer, time, button,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001680 state);
1681
1682 pointer->grab->interface->button(pointer->grab, time, button, state);
1683
1684 if (pointer->button_count == 1)
1685 pointer->grab_serial =
1686 wl_display_get_serial(compositor->wl_display);
1687}
1688
1689WL_EXPORT void
Alexandros Frantzis80321942017-11-16 18:20:56 +02001690notify_axis(struct weston_seat *seat, const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001691 struct weston_pointer_axis_event *event)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001692{
1693 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001694 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001695
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001696 weston_compositor_wake(compositor);
1697
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001698 if (weston_compositor_run_axis_binding(compositor, pointer,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001699 time, event))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001700 return;
1701
Peter Hutterer89b6a492016-01-18 15:58:17 +10001702 pointer->grab->interface->axis(pointer->grab, time, event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001703}
1704
Peter Hutterer87743e92016-01-18 16:38:22 +10001705WL_EXPORT void
1706notify_axis_source(struct weston_seat *seat, uint32_t source)
1707{
1708 struct weston_compositor *compositor = seat->compositor;
1709 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1710
1711 weston_compositor_wake(compositor);
1712
1713 pointer->grab->interface->axis_source(pointer->grab, source);
1714}
1715
1716WL_EXPORT void
1717notify_pointer_frame(struct weston_seat *seat)
1718{
1719 struct weston_compositor *compositor = seat->compositor;
1720 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1721
1722 weston_compositor_wake(compositor);
1723
1724 pointer->grab->interface->frame(pointer->grab);
1725}
1726
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001727WL_EXPORT int
1728weston_keyboard_set_locks(struct weston_keyboard *keyboard,
1729 uint32_t mask, uint32_t value)
1730{
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001731 uint32_t serial;
1732 xkb_mod_mask_t mods_depressed, mods_latched, mods_locked, group;
1733 xkb_mod_mask_t num, caps;
1734
1735 /* We don't want the leds to go out of sync with the actual state
1736 * so if the backend has no way to change the leds don't try to
1737 * change the state */
1738 if (!keyboard->seat->led_update)
1739 return -1;
1740
1741 mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
1742 XKB_STATE_DEPRESSED);
1743 mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
1744 XKB_STATE_LATCHED);
1745 mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
1746 XKB_STATE_LOCKED);
1747 group = xkb_state_serialize_group(keyboard->xkb_state.state,
1748 XKB_STATE_EFFECTIVE);
1749
1750 num = (1 << keyboard->xkb_info->mod2_mod);
1751 caps = (1 << keyboard->xkb_info->caps_mod);
1752 if (mask & WESTON_NUM_LOCK) {
1753 if (value & WESTON_NUM_LOCK)
1754 mods_locked |= num;
1755 else
1756 mods_locked &= ~num;
1757 }
1758 if (mask & WESTON_CAPS_LOCK) {
1759 if (value & WESTON_CAPS_LOCK)
1760 mods_locked |= caps;
1761 else
1762 mods_locked &= ~caps;
1763 }
1764
1765 xkb_state_update_mask(keyboard->xkb_state.state, mods_depressed,
1766 mods_latched, mods_locked, 0, 0, group);
1767
1768 serial = wl_display_next_serial(
1769 keyboard->seat->compositor->wl_display);
1770 notify_modifiers(keyboard->seat, serial);
1771
1772 return 0;
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001773}
1774
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001775WL_EXPORT void
1776notify_modifiers(struct weston_seat *seat, uint32_t serial)
1777{
Derek Foreman1281a362015-07-31 16:55:32 -05001778 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001779 struct weston_keyboard_grab *grab = keyboard->grab;
1780 uint32_t mods_depressed, mods_latched, mods_locked, group;
1781 uint32_t mods_lookup;
1782 enum weston_led leds = 0;
1783 int changed = 0;
1784
1785 /* Serialize and update our internal state, checking to see if it's
1786 * different to the previous state. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001787 mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03001788 XKB_STATE_MODS_DEPRESSED);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001789 mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03001790 XKB_STATE_MODS_LATCHED);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001791 mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03001792 XKB_STATE_MODS_LOCKED);
1793 group = xkb_state_serialize_layout(keyboard->xkb_state.state,
1794 XKB_STATE_LAYOUT_EFFECTIVE);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001795
Derek Foreman244e99e2015-06-03 15:53:26 -05001796 if (mods_depressed != keyboard->modifiers.mods_depressed ||
1797 mods_latched != keyboard->modifiers.mods_latched ||
1798 mods_locked != keyboard->modifiers.mods_locked ||
1799 group != keyboard->modifiers.group)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001800 changed = 1;
1801
Derek Foreman244e99e2015-06-03 15:53:26 -05001802 run_modifier_bindings(seat, keyboard->modifiers.mods_depressed,
Daniel Stone96d47c02013-11-19 11:37:12 +01001803 mods_depressed);
1804
Derek Foreman244e99e2015-06-03 15:53:26 -05001805 keyboard->modifiers.mods_depressed = mods_depressed;
1806 keyboard->modifiers.mods_latched = mods_latched;
1807 keyboard->modifiers.mods_locked = mods_locked;
1808 keyboard->modifiers.group = group;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001809
1810 /* And update the modifier_state for bindings. */
1811 mods_lookup = mods_depressed | mods_latched;
1812 seat->modifier_state = 0;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001813 if (mods_lookup & (1 << keyboard->xkb_info->ctrl_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001814 seat->modifier_state |= MODIFIER_CTRL;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001815 if (mods_lookup & (1 << keyboard->xkb_info->alt_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001816 seat->modifier_state |= MODIFIER_ALT;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001817 if (mods_lookup & (1 << keyboard->xkb_info->super_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001818 seat->modifier_state |= MODIFIER_SUPER;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001819 if (mods_lookup & (1 << keyboard->xkb_info->shift_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001820 seat->modifier_state |= MODIFIER_SHIFT;
1821
1822 /* Finally, notify the compositor that LEDs have changed. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001823 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
1824 keyboard->xkb_info->num_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001825 leds |= LED_NUM_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001826 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
1827 keyboard->xkb_info->caps_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001828 leds |= LED_CAPS_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001829 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
1830 keyboard->xkb_info->scroll_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001831 leds |= LED_SCROLL_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001832 if (leds != keyboard->xkb_state.leds && seat->led_update)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001833 seat->led_update(seat, leds);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001834 keyboard->xkb_state.leds = leds;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001835
1836 if (changed) {
1837 grab->interface->modifiers(grab,
1838 serial,
1839 keyboard->modifiers.mods_depressed,
1840 keyboard->modifiers.mods_latched,
1841 keyboard->modifiers.mods_locked,
1842 keyboard->modifiers.group);
1843 }
1844}
1845
1846static void
1847update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
1848 enum wl_keyboard_key_state state)
1849{
Derek Foreman1281a362015-07-31 16:55:32 -05001850 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001851 enum xkb_key_direction direction;
1852
1853 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1854 direction = XKB_KEY_DOWN;
1855 else
1856 direction = XKB_KEY_UP;
1857
1858 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
1859 * broken keycode system, which starts at 8. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001860 xkb_state_update_key(keyboard->xkb_state.state, key + 8, direction);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001861
1862 notify_modifiers(seat, serial);
1863}
Rui Matos65196bc2013-10-10 19:44:19 +02001864
1865static void
1866send_keymap(struct wl_resource *resource, struct weston_xkb_info *xkb_info)
1867{
1868 wl_keyboard_send_keymap(resource,
1869 WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
1870 xkb_info->keymap_fd,
1871 xkb_info->keymap_size);
1872}
1873
1874static void
1875send_modifiers(struct wl_resource *resource, uint32_t serial, struct weston_keyboard *keyboard)
1876{
1877 wl_keyboard_send_modifiers(resource, serial,
1878 keyboard->modifiers.mods_depressed,
1879 keyboard->modifiers.mods_latched,
1880 keyboard->modifiers.mods_locked,
1881 keyboard->modifiers.group);
1882}
1883
1884static struct weston_xkb_info *
1885weston_xkb_info_create(struct xkb_keymap *keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02001886
1887static void
1888update_keymap(struct weston_seat *seat)
1889{
Derek Foreman1281a362015-07-31 16:55:32 -05001890 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Rui Matos65196bc2013-10-10 19:44:19 +02001891 struct wl_resource *resource;
1892 struct weston_xkb_info *xkb_info;
1893 struct xkb_state *state;
1894 xkb_mod_mask_t latched_mods;
1895 xkb_mod_mask_t locked_mods;
1896
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001897 xkb_info = weston_xkb_info_create(keyboard->pending_keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02001898
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001899 xkb_keymap_unref(keyboard->pending_keymap);
1900 keyboard->pending_keymap = NULL;
Rui Matos65196bc2013-10-10 19:44:19 +02001901
1902 if (!xkb_info) {
1903 weston_log("failed to create XKB info\n");
1904 return;
1905 }
1906
1907 state = xkb_state_new(xkb_info->keymap);
1908 if (!state) {
1909 weston_log("failed to initialise XKB state\n");
1910 weston_xkb_info_destroy(xkb_info);
1911 return;
1912 }
1913
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001914 latched_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
1915 XKB_STATE_MODS_LATCHED);
1916 locked_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
1917 XKB_STATE_MODS_LOCKED);
Rui Matos65196bc2013-10-10 19:44:19 +02001918 xkb_state_update_mask(state,
1919 0, /* depressed */
1920 latched_mods,
1921 locked_mods,
1922 0, 0, 0);
1923
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001924 weston_xkb_info_destroy(keyboard->xkb_info);
1925 keyboard->xkb_info = xkb_info;
Rui Matos65196bc2013-10-10 19:44:19 +02001926
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001927 xkb_state_unref(keyboard->xkb_state.state);
1928 keyboard->xkb_state.state = state;
Rui Matos65196bc2013-10-10 19:44:19 +02001929
Derek Foremanbc91e542015-06-03 15:53:27 -05001930 wl_resource_for_each(resource, &keyboard->resource_list)
Rui Matos65196bc2013-10-10 19:44:19 +02001931 send_keymap(resource, xkb_info);
Derek Foremanbc91e542015-06-03 15:53:27 -05001932 wl_resource_for_each(resource, &keyboard->focus_resource_list)
Rui Matos65196bc2013-10-10 19:44:19 +02001933 send_keymap(resource, xkb_info);
1934
1935 notify_modifiers(seat, wl_display_next_serial(seat->compositor->wl_display));
1936
1937 if (!latched_mods && !locked_mods)
1938 return;
1939
Derek Foremanbc91e542015-06-03 15:53:27 -05001940 wl_resource_for_each(resource, &keyboard->resource_list)
1941 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), keyboard);
1942 wl_resource_for_each(resource, &keyboard->focus_resource_list)
1943 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), keyboard);
Rui Matos65196bc2013-10-10 19:44:19 +02001944}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001945
1946WL_EXPORT void
1947notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
1948 enum wl_keyboard_key_state state,
1949 enum weston_key_state_update update_state)
1950{
1951 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001952 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001953 struct weston_keyboard_grab *grab = keyboard->grab;
Pekka Paalanen86b53962014-11-19 13:43:32 +02001954 uint32_t *k, *end;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001955
1956 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001957 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001958 } else {
1959 weston_compositor_idle_release(compositor);
1960 }
1961
Pekka Paalanen86b53962014-11-19 13:43:32 +02001962 end = keyboard->keys.data + keyboard->keys.size;
1963 for (k = keyboard->keys.data; k < end; k++) {
1964 if (*k == key) {
1965 /* Ignore server-generated repeats. */
1966 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1967 return;
1968 *k = *--end;
1969 }
1970 }
1971 keyboard->keys.size = (void *) end - keyboard->keys.data;
1972 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1973 k = wl_array_add(&keyboard->keys, sizeof *k);
1974 *k = key;
1975 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001976
1977 if (grab == &keyboard->default_grab ||
1978 grab == &keyboard->input_method_grab) {
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001979 weston_compositor_run_key_binding(compositor, keyboard, time,
1980 key, state);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001981 grab = keyboard->grab;
1982 }
1983
1984 grab->interface->key(grab, time, key, state);
1985
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001986 if (keyboard->pending_keymap &&
Pekka Paalanen86b53962014-11-19 13:43:32 +02001987 keyboard->keys.size == 0)
Rui Matos65196bc2013-10-10 19:44:19 +02001988 update_keymap(seat);
1989
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001990 if (update_state == STATE_UPDATE_AUTOMATIC) {
1991 update_modifier_state(seat,
1992 wl_display_get_serial(compositor->wl_display),
1993 key,
1994 state);
1995 }
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02001996
Olivier Fourdandf84dbe2016-06-30 16:01:56 +02001997 keyboard->grab_serial = wl_display_get_serial(compositor->wl_display);
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02001998 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02001999 keyboard->grab_time = time;
2000 keyboard->grab_key = key;
2001 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002002}
2003
2004WL_EXPORT void
2005notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002006 double x, double y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002007{
Derek Foreman1281a362015-07-31 16:55:32 -05002008 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2009
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002010 if (output) {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002011 weston_pointer_move_to(pointer,
2012 wl_fixed_from_double(x),
2013 wl_fixed_from_double(y));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002014 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002015 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002016 * NULL) here, but somehow that breaks re-entry... */
2017 }
2018}
2019
2020static void
2021destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
2022{
2023 struct weston_seat *ws;
2024
2025 ws = container_of(listener, struct weston_seat,
2026 saved_kbd_focus_listener);
2027
2028 ws->saved_kbd_focus = NULL;
2029}
2030
2031WL_EXPORT void
2032notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
2033 enum weston_key_state_update update_state)
2034{
2035 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002036 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04002037 struct weston_surface *surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002038 uint32_t *k, serial;
2039
2040 serial = wl_display_next_serial(compositor->wl_display);
2041 wl_array_copy(&keyboard->keys, keys);
2042 wl_array_for_each(k, &keyboard->keys) {
2043 weston_compositor_idle_inhibit(compositor);
2044 if (update_state == STATE_UPDATE_AUTOMATIC)
2045 update_modifier_state(seat, serial, *k,
2046 WL_KEYBOARD_KEY_STATE_PRESSED);
2047 }
2048
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002049 surface = seat->saved_kbd_focus;
2050
2051 if (surface) {
2052 wl_list_remove(&seat->saved_kbd_focus_listener.link);
2053 weston_keyboard_set_focus(keyboard, surface);
2054 seat->saved_kbd_focus = NULL;
2055 }
2056}
2057
2058WL_EXPORT void
2059notify_keyboard_focus_out(struct weston_seat *seat)
2060{
2061 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002062 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2063 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002064 uint32_t *k, serial;
2065
2066 serial = wl_display_next_serial(compositor->wl_display);
2067 wl_array_for_each(k, &keyboard->keys) {
2068 weston_compositor_idle_release(compositor);
2069 update_modifier_state(seat, serial, *k,
2070 WL_KEYBOARD_KEY_STATE_RELEASED);
2071 }
2072
2073 seat->modifier_state = 0;
2074
2075 if (keyboard->focus) {
2076 seat->saved_kbd_focus = keyboard->focus;
2077 seat->saved_kbd_focus_listener.notify =
2078 destroy_device_saved_kbd_focus;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002079 wl_signal_add(&keyboard->focus->destroy_signal,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002080 &seat->saved_kbd_focus_listener);
2081 }
2082
2083 weston_keyboard_set_focus(keyboard, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002084 weston_keyboard_cancel_grab(keyboard);
Derek Foreman1281a362015-07-31 16:55:32 -05002085 if (pointer)
2086 weston_pointer_cancel_grab(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002087}
2088
Michael Fua2bb7912013-07-23 15:51:06 +08002089WL_EXPORT void
Derek Foreman4c93c082015-04-30 16:45:41 -05002090weston_touch_set_focus(struct weston_touch *touch, struct weston_view *view)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002091{
Neil Roberts96d790e2013-09-19 17:32:00 +01002092 struct wl_list *focus_resource_list;
2093
Derek Foreman4c93c082015-04-30 16:45:41 -05002094 focus_resource_list = &touch->focus_resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002095
Derek Foreman4c93c082015-04-30 16:45:41 -05002096 if (view && touch->focus &&
2097 touch->focus->surface == view->surface) {
2098 touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002099 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002100 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002101
Derek Foreman4c93c082015-04-30 16:45:41 -05002102 wl_list_remove(&touch->focus_resource_listener.link);
2103 wl_list_init(&touch->focus_resource_listener.link);
2104 wl_list_remove(&touch->focus_view_listener.link);
2105 wl_list_init(&touch->focus_view_listener.link);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01002106
Neil Roberts96d790e2013-09-19 17:32:00 +01002107 if (!wl_list_empty(focus_resource_list)) {
Derek Foreman4c93c082015-04-30 16:45:41 -05002108 move_resources(&touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002109 focus_resource_list);
2110 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002111
Jason Ekstranda7af7042013-10-12 22:38:11 -05002112 if (view) {
Derek Foreman362656b2014-09-04 10:23:05 -05002113 struct wl_client *surface_client;
2114
2115 if (!view->surface->resource) {
Derek Foreman4c93c082015-04-30 16:45:41 -05002116 touch->focus = NULL;
Derek Foreman362656b2014-09-04 10:23:05 -05002117 return;
2118 }
2119
2120 surface_client = wl_resource_get_client(view->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +01002121 move_resources_for_client(focus_resource_list,
Derek Foreman4c93c082015-04-30 16:45:41 -05002122 &touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002123 surface_client);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01002124 wl_resource_add_destroy_listener(view->surface->resource,
Derek Foreman4c93c082015-04-30 16:45:41 -05002125 &touch->focus_resource_listener);
2126 wl_signal_add(&view->destroy_signal, &touch->focus_view_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002127 }
Derek Foreman4c93c082015-04-30 16:45:41 -05002128 touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002129}
2130
2131/**
2132 * notify_touch - emulates button touches and notifies surfaces accordingly.
2133 *
2134 * It assumes always the correct cycle sequence until it gets here: touch_down
2135 * → touch_update → ... → touch_update → touch_end. The driver is responsible
2136 * for sending along such order.
2137 *
2138 */
2139WL_EXPORT void
2140notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002141 double double_x, double double_y, int touch_type)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002142{
2143 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002144 struct weston_touch *touch = weston_seat_get_touch(seat);
Kristian Høgsberge329f362013-05-06 22:19:57 -04002145 struct weston_touch_grab *grab = touch->grab;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002146 struct weston_view *ev;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002147 wl_fixed_t sx, sy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002148 wl_fixed_t x = wl_fixed_from_double(double_x);
2149 wl_fixed_t y = wl_fixed_from_double(double_y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002150
2151 /* Update grab's global coordinates. */
Neil Roberts306fe082013-10-03 16:43:06 +01002152 if (touch_id == touch->grab_touch_id && touch_type != WL_TOUCH_UP) {
2153 touch->grab_x = x;
2154 touch->grab_y = y;
2155 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002156
2157 switch (touch_type) {
2158 case WL_TOUCH_DOWN:
2159 weston_compositor_idle_inhibit(ec);
2160
Jonas Ådahl9484b692013-12-02 22:05:03 +01002161 touch->num_tp++;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002162
Jason Ekstranda7af7042013-10-12 22:38:11 -05002163 /* the first finger down picks the view, and all further go
2164 * to that view for the remainder of the touch session i.e.
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002165 * until all touch points are up again. */
Jonas Ådahl9484b692013-12-02 22:05:03 +01002166 if (touch->num_tp == 1) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002167 ev = weston_compositor_pick_view(ec, x, y, &sx, &sy);
Derek Foreman4c93c082015-04-30 16:45:41 -05002168 weston_touch_set_focus(touch, ev);
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002169 } else if (!touch->focus) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002170 /* Unexpected condition: We have non-initial touch but
2171 * there is no focused surface.
2172 */
Chris Michael3f607d32015-10-07 11:59:49 -04002173 weston_log("touch event received with %d points down "
Jonas Ådahl9484b692013-12-02 22:05:03 +01002174 "but no surface focused\n", touch->num_tp);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002175 return;
2176 }
2177
Derek Foreman99a6a2d2015-07-15 13:00:43 -05002178 weston_compositor_run_touch_binding(ec, touch,
Kristian Høgsbergc8964012014-02-05 14:25:18 -08002179 time, touch_type);
2180
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002181 grab->interface->down(grab, time, touch_id, x, y);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002182 if (touch->num_tp == 1) {
Rusty Lynchf1407ff2013-08-08 21:13:57 -07002183 touch->grab_serial =
2184 wl_display_get_serial(ec->wl_display);
Neil Roberts306fe082013-10-03 16:43:06 +01002185 touch->grab_touch_id = touch_id;
Rusty Lynchf1407ff2013-08-08 21:13:57 -07002186 touch->grab_time = time;
2187 touch->grab_x = x;
2188 touch->grab_y = y;
2189 }
2190
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002191 break;
2192 case WL_TOUCH_MOTION:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002193 ev = touch->focus;
2194 if (!ev)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002195 break;
2196
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002197 grab->interface->motion(grab, time, touch_id, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002198 break;
2199 case WL_TOUCH_UP:
Kristian Høgsberga30e29a2014-01-08 22:29:20 -08002200 if (touch->num_tp == 0) {
2201 /* This can happen if we start out with one or
2202 * more fingers on the touch screen, in which
2203 * case we didn't get the corresponding down
2204 * event. */
2205 weston_log("unmatched touch up event\n");
2206 break;
2207 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002208 weston_compositor_idle_release(ec);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002209 touch->num_tp--;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002210
2211 grab->interface->up(grab, time, touch_id);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002212 if (touch->num_tp == 0)
Derek Foreman4c93c082015-04-30 16:45:41 -05002213 weston_touch_set_focus(touch, NULL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002214 break;
2215 }
2216}
2217
Jonas Ådahl1679f232014-04-12 09:39:51 +02002218WL_EXPORT void
2219notify_touch_frame(struct weston_seat *seat)
2220{
Derek Foreman1281a362015-07-31 16:55:32 -05002221 struct weston_touch *touch = weston_seat_get_touch(seat);
Jonas Ådahl1679f232014-04-12 09:39:51 +02002222 struct weston_touch_grab *grab = touch->grab;
2223
2224 grab->interface->frame(grab);
2225}
2226
Derek Foreman3cc004a2015-11-06 15:56:09 -06002227WL_EXPORT void
2228notify_touch_cancel(struct weston_seat *seat)
2229{
2230 struct weston_touch *touch = weston_seat_get_touch(seat);
2231 struct weston_touch_grab *grab = touch->grab;
2232
2233 grab->interface->cancel(grab);
2234}
2235
Pekka Paalanen8274d902014-08-06 19:36:51 +03002236static int
2237pointer_cursor_surface_get_label(struct weston_surface *surface,
2238 char *buf, size_t len)
2239{
2240 return snprintf(buf, len, "cursor");
2241}
2242
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002243static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002244pointer_cursor_surface_committed(struct weston_surface *es,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002245 int32_t dx, int32_t dy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002246{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002247 struct weston_pointer *pointer = es->committed_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002248 int x, y;
2249
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002250 if (es->width == 0)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002251 return;
2252
Jason Ekstranda7af7042013-10-12 22:38:11 -05002253 assert(es == pointer->sprite->surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002254
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002255 pointer->hotspot_x -= dx;
2256 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002257
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002258 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
2259 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002260
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002261 weston_view_set_position(pointer->sprite, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002262
2263 empty_region(&es->pending.input);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02002264 empty_region(&es->input);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002265
2266 if (!weston_surface_is_mapped(es)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002267 weston_layer_entry_insert(&es->compositor->cursor_layer.view_list,
2268 &pointer->sprite->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002269 weston_view_update_transform(pointer->sprite);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002270 es->is_mapped = true;
2271 pointer->sprite->is_mapped = true;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002272 }
2273}
2274
2275static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002276pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2277 uint32_t serial, struct wl_resource *surface_resource,
2278 int32_t x, int32_t y)
2279{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002280 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002281 struct weston_surface *surface = NULL;
2282
2283 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002284 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002285
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002286 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002287 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002288 /* pointer->focus->surface->resource can be NULL. Surfaces like the
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02002289 black_surface used in shell.c for fullscreen don't have
2290 a resource, but can still have focus */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002291 if (pointer->focus->surface->resource == NULL)
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02002292 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002293 if (wl_resource_get_client(pointer->focus->surface->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002294 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002295 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002296 return;
2297
Derek Foreman4e53c532015-03-23 10:55:32 -05002298 if (!surface) {
2299 if (pointer->sprite)
2300 pointer_unmap_sprite(pointer);
2301 return;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002302 }
2303
Jonas Ådahlb4070242015-03-18 15:08:03 +08002304 if (pointer->sprite && pointer->sprite->surface == surface &&
2305 pointer->hotspot_x == x && pointer->hotspot_y == y)
2306 return;
2307
Derek Foreman4e53c532015-03-23 10:55:32 -05002308 if (!pointer->sprite || pointer->sprite->surface != surface) {
2309 if (weston_surface_set_role(surface, "wl_pointer-cursor",
2310 resource,
2311 WL_POINTER_ERROR_ROLE) < 0)
2312 return;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002313
Derek Foreman4e53c532015-03-23 10:55:32 -05002314 if (pointer->sprite)
2315 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002316
Derek Foreman4e53c532015-03-23 10:55:32 -05002317 wl_signal_add(&surface->destroy_signal,
2318 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002319
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002320 surface->committed = pointer_cursor_surface_committed;
2321 surface->committed_private = pointer;
Derek Foreman4e53c532015-03-23 10:55:32 -05002322 weston_surface_set_label_func(surface,
2323 pointer_cursor_surface_get_label);
2324 pointer->sprite = weston_view_create(surface);
2325 }
2326
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002327 pointer->hotspot_x = x;
2328 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002329
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +02002330 if (surface->buffer_ref.buffer) {
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002331 pointer_cursor_surface_committed(surface, 0, 0);
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +02002332 weston_view_schedule_repaint(pointer->sprite);
2333 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002334}
2335
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002336static void
2337pointer_release(struct wl_client *client, struct wl_resource *resource)
2338{
2339 wl_resource_destroy(resource);
2340}
2341
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002342static const struct wl_pointer_interface pointer_interface = {
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002343 pointer_set_cursor,
2344 pointer_release
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002345};
2346
2347static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002348seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2349 uint32_t id)
2350{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002351 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002352 /* We use the pointer_state directly, which means we'll
2353 * give a wl_pointer if the seat has ever had one - even though
2354 * the spec explicitly states that this request only takes effect
2355 * if the seat has the pointer capability.
2356 *
2357 * This prevents a race between the compositor sending new
2358 * capabilities and the client trying to use the old ones.
2359 */
2360 struct weston_pointer *pointer = seat->pointer_state;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002361 struct wl_resource *cr;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002362 struct weston_pointer_client *pointer_client;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002363
Derek Foreman1281a362015-07-31 16:55:32 -05002364 if (!pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002365 return;
2366
Jason Ekstranda85118c2013-06-27 20:17:02 -05002367 cr = wl_resource_create(client, &wl_pointer_interface,
2368 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002369 if (cr == NULL) {
2370 wl_client_post_no_memory(client);
2371 return;
2372 }
2373
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002374 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
2375 if (!pointer_client) {
2376 wl_client_post_no_memory(client);
2377 return;
2378 }
2379
2380 wl_list_insert(&pointer_client->pointer_resources,
2381 wl_resource_get_link(cr));
Derek Foreman1281a362015-07-31 16:55:32 -05002382 wl_resource_set_implementation(cr, &pointer_interface, pointer,
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002383 unbind_pointer_client_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002384
Derek Foreman1281a362015-07-31 16:55:32 -05002385 if (pointer->focus && pointer->focus->surface->resource &&
2386 wl_resource_get_client(pointer->focus->surface->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002387 wl_fixed_t sx, sy;
2388
Derek Foreman1281a362015-07-31 16:55:32 -05002389 weston_view_from_global_fixed(pointer->focus,
2390 pointer->x,
2391 pointer->y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002392 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002393
Neil Roberts96d790e2013-09-19 17:32:00 +01002394 wl_pointer_send_enter(cr,
Derek Foreman1281a362015-07-31 16:55:32 -05002395 pointer->focus_serial,
2396 pointer->focus->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01002397 sx, sy);
Peter Hutterer87743e92016-01-18 16:38:22 +10002398 pointer_send_frame(cr);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002399 }
2400}
2401
2402static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002403keyboard_release(struct wl_client *client, struct wl_resource *resource)
2404{
2405 wl_resource_destroy(resource);
2406}
2407
2408static const struct wl_keyboard_interface keyboard_interface = {
2409 keyboard_release
2410};
2411
Derek Foreman280e7dd2014-10-03 13:13:42 -05002412static bool
Neil Roberts96d790e2013-09-19 17:32:00 +01002413should_send_modifiers_to_client(struct weston_seat *seat,
2414 struct wl_client *client)
2415{
Derek Foreman1281a362015-07-31 16:55:32 -05002416 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2417 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2418
2419 if (keyboard &&
2420 keyboard->focus &&
2421 keyboard->focus->resource &&
2422 wl_resource_get_client(keyboard->focus->resource) == client)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002423 return true;
Neil Roberts96d790e2013-09-19 17:32:00 +01002424
Derek Foreman1281a362015-07-31 16:55:32 -05002425 if (pointer &&
2426 pointer->focus &&
2427 pointer->focus->surface->resource &&
2428 wl_resource_get_client(pointer->focus->surface->resource) == client)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002429 return true;
Neil Roberts96d790e2013-09-19 17:32:00 +01002430
Derek Foreman280e7dd2014-10-03 13:13:42 -05002431 return false;
Neil Roberts96d790e2013-09-19 17:32:00 +01002432}
2433
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002434static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002435seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2436 uint32_t id)
2437{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002438 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002439 /* We use the keyboard_state directly, which means we'll
2440 * give a wl_keyboard if the seat has ever had one - even though
2441 * the spec explicitly states that this request only takes effect
2442 * if the seat has the keyboard capability.
2443 *
2444 * This prevents a race between the compositor sending new
2445 * capabilities and the client trying to use the old ones.
2446 */
2447 struct weston_keyboard *keyboard = seat->keyboard_state;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002448 struct wl_resource *cr;
2449
Derek Foreman345c9f32015-06-03 15:53:28 -05002450 if (!keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002451 return;
2452
Jason Ekstranda85118c2013-06-27 20:17:02 -05002453 cr = wl_resource_create(client, &wl_keyboard_interface,
2454 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002455 if (cr == NULL) {
2456 wl_client_post_no_memory(client);
2457 return;
2458 }
2459
Neil Roberts96d790e2013-09-19 17:32:00 +01002460 /* May be moved to focused list later by either
2461 * weston_keyboard_set_focus or directly if this client is already
2462 * focused */
Derek Foreman345c9f32015-06-03 15:53:28 -05002463 wl_list_insert(&keyboard->resource_list, wl_resource_get_link(cr));
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002464 wl_resource_set_implementation(cr, &keyboard_interface,
2465 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002466
Jonny Lamb66a41a02014-08-12 14:58:25 +02002467 if (wl_resource_get_version(cr) >= WL_KEYBOARD_REPEAT_INFO_SINCE_VERSION) {
2468 wl_keyboard_send_repeat_info(cr,
2469 seat->compositor->kb_repeat_rate,
2470 seat->compositor->kb_repeat_delay);
2471 }
Jasper St. Pierred8c6aeb2014-08-04 13:43:24 -04002472
Derek Foreman185d1582017-06-28 11:17:23 -05002473 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2474 keyboard->xkb_info->keymap_fd,
2475 keyboard->xkb_info->keymap_size);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002476
Neil Roberts96d790e2013-09-19 17:32:00 +01002477 if (should_send_modifiers_to_client(seat, client)) {
Derek Foreman345c9f32015-06-03 15:53:28 -05002478 send_modifiers_to_resource(keyboard,
Neil Roberts96d790e2013-09-19 17:32:00 +01002479 cr,
Derek Foreman345c9f32015-06-03 15:53:28 -05002480 keyboard->focus_serial);
Neil Roberts96d790e2013-09-19 17:32:00 +01002481 }
2482
Derek Foreman345c9f32015-06-03 15:53:28 -05002483 if (keyboard->focus && keyboard->focus->resource &&
2484 wl_resource_get_client(keyboard->focus->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01002485 struct weston_surface *surface =
Derek Foreman345c9f32015-06-03 15:53:28 -05002486 (struct weston_surface *)keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +01002487
2488 wl_list_remove(wl_resource_get_link(cr));
Derek Foreman345c9f32015-06-03 15:53:28 -05002489 wl_list_insert(&keyboard->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002490 wl_resource_get_link(cr));
2491 wl_keyboard_send_enter(cr,
Derek Foreman345c9f32015-06-03 15:53:28 -05002492 keyboard->focus_serial,
Neil Roberts96d790e2013-09-19 17:32:00 +01002493 surface->resource,
Derek Foreman345c9f32015-06-03 15:53:28 -05002494 &keyboard->keys);
Neil Roberts96d790e2013-09-19 17:32:00 +01002495
2496 /* If this is the first keyboard resource for this
2497 * client... */
Derek Foreman345c9f32015-06-03 15:53:28 -05002498 if (keyboard->focus_resource_list.prev ==
Neil Roberts96d790e2013-09-19 17:32:00 +01002499 wl_resource_get_link(cr))
2500 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002501 }
2502}
2503
2504static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002505touch_release(struct wl_client *client, struct wl_resource *resource)
2506{
2507 wl_resource_destroy(resource);
2508}
2509
2510static const struct wl_touch_interface touch_interface = {
2511 touch_release
2512};
2513
2514static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002515seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2516 uint32_t id)
2517{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002518 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002519 /* We use the touch_state directly, which means we'll
2520 * give a wl_touch if the seat has ever had one - even though
2521 * the spec explicitly states that this request only takes effect
2522 * if the seat has the touch capability.
2523 *
2524 * This prevents a race between the compositor sending new
2525 * capabilities and the client trying to use the old ones.
2526 */
2527 struct weston_touch *touch = seat->touch_state;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002528 struct wl_resource *cr;
2529
Derek Foreman1281a362015-07-31 16:55:32 -05002530 if (!touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002531 return;
2532
Jason Ekstranda85118c2013-06-27 20:17:02 -05002533 cr = wl_resource_create(client, &wl_touch_interface,
2534 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002535 if (cr == NULL) {
2536 wl_client_post_no_memory(client);
2537 return;
2538 }
2539
Derek Foreman1281a362015-07-31 16:55:32 -05002540 if (touch->focus &&
2541 wl_resource_get_client(touch->focus->surface->resource) == client) {
Chokshi, Mituld6697142015-10-09 08:28:47 +00002542 wl_list_insert(&touch->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002543 wl_resource_get_link(cr));
2544 } else {
Chokshi, Mituld6697142015-10-09 08:28:47 +00002545 wl_list_insert(&touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002546 wl_resource_get_link(cr));
2547 }
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002548 wl_resource_set_implementation(cr, &touch_interface,
2549 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002550}
2551
Quentin Glidicaab1d362016-03-13 17:49:08 +01002552static void
2553seat_release(struct wl_client *client, struct wl_resource *resource)
2554{
2555 wl_resource_destroy(resource);
2556}
2557
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002558static const struct wl_seat_interface seat_interface = {
2559 seat_get_pointer,
2560 seat_get_keyboard,
2561 seat_get_touch,
Quentin Glidicaab1d362016-03-13 17:49:08 +01002562 seat_release,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002563};
2564
2565static void
2566bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2567{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002568 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002569 struct wl_resource *resource;
2570 enum wl_seat_capability caps = 0;
2571
Jason Ekstranda85118c2013-06-27 20:17:02 -05002572 resource = wl_resource_create(client,
Derek Foreman1909c102015-11-26 14:17:47 -06002573 &wl_seat_interface, version, id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05002574 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05002575 wl_resource_set_implementation(resource, &seat_interface, data,
2576 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002577
Derek Foreman1281a362015-07-31 16:55:32 -05002578 if (weston_seat_get_pointer(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002579 caps |= WL_SEAT_CAPABILITY_POINTER;
Derek Foreman1281a362015-07-31 16:55:32 -05002580 if (weston_seat_get_keyboard(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002581 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Derek Foreman1281a362015-07-31 16:55:32 -05002582 if (weston_seat_get_touch(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002583 caps |= WL_SEAT_CAPABILITY_TOUCH;
2584
2585 wl_seat_send_capabilities(resource, caps);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04002586 if (version >= WL_SEAT_NAME_SINCE_VERSION)
Rob Bradforde445ae62013-05-31 18:09:51 +01002587 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002588}
2589
Jonas Ådahl30d61d82014-10-22 21:21:17 +02002590static void
2591relative_pointer_destroy(struct wl_client *client,
2592 struct wl_resource *resource)
2593{
2594 wl_resource_destroy(resource);
2595}
2596
2597static const struct zwp_relative_pointer_v1_interface relative_pointer_interface = {
2598 relative_pointer_destroy
2599};
2600
2601static void
2602relative_pointer_manager_destroy(struct wl_client *client,
2603 struct wl_resource *resource)
2604{
2605 wl_resource_destroy(resource);
2606}
2607
2608static void
2609relative_pointer_manager_get_relative_pointer(struct wl_client *client,
2610 struct wl_resource *resource,
2611 uint32_t id,
2612 struct wl_resource *pointer_resource)
2613{
2614 struct weston_pointer *pointer =
2615 wl_resource_get_user_data(pointer_resource);
2616 struct weston_pointer_client *pointer_client;
2617 struct wl_resource *cr;
2618
2619 cr = wl_resource_create(client, &zwp_relative_pointer_v1_interface,
2620 wl_resource_get_version(resource), id);
2621 if (cr == NULL) {
2622 wl_client_post_no_memory(client);
2623 return;
2624 }
2625
2626 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
2627 if (!pointer_client) {
2628 wl_client_post_no_memory(client);
2629 return;
2630 }
2631
2632 wl_list_insert(&pointer_client->relative_pointer_resources,
2633 wl_resource_get_link(cr));
2634 wl_resource_set_implementation(cr, &relative_pointer_interface,
2635 pointer,
2636 unbind_pointer_client_resource);
2637}
2638
2639static const struct zwp_relative_pointer_manager_v1_interface relative_pointer_manager = {
2640 relative_pointer_manager_destroy,
2641 relative_pointer_manager_get_relative_pointer,
2642};
2643
2644static void
2645bind_relative_pointer_manager(struct wl_client *client, void *data,
2646 uint32_t version, uint32_t id)
2647{
2648 struct weston_compositor *compositor = data;
2649 struct wl_resource *resource;
2650
2651 resource = wl_resource_create(client,
2652 &zwp_relative_pointer_manager_v1_interface,
2653 1, id);
2654
2655 wl_resource_set_implementation(resource, &relative_pointer_manager,
2656 compositor,
2657 NULL);
2658}
2659
Giulio Camuffo0358af42016-06-02 21:48:08 +03002660WL_EXPORT int
2661weston_compositor_set_xkb_rule_names(struct weston_compositor *ec,
2662 struct xkb_rule_names *names)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002663{
2664 if (ec->xkb_context == NULL) {
2665 ec->xkb_context = xkb_context_new(0);
2666 if (ec->xkb_context == NULL) {
2667 weston_log("failed to create XKB context\n");
2668 return -1;
2669 }
2670 }
2671
2672 if (names)
2673 ec->xkb_names = *names;
2674 if (!ec->xkb_names.rules)
2675 ec->xkb_names.rules = strdup("evdev");
2676 if (!ec->xkb_names.model)
2677 ec->xkb_names.model = strdup("pc105");
2678 if (!ec->xkb_names.layout)
2679 ec->xkb_names.layout = strdup("us");
2680
2681 return 0;
2682}
2683
Stefan Schmidtfda26522013-09-17 10:54:09 +01002684static void
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002685weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002686{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002687 if (--xkb_info->ref_count > 0)
2688 return;
2689
Ran Benitac9c74152014-08-19 23:59:52 +03002690 xkb_keymap_unref(xkb_info->keymap);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002691
2692 if (xkb_info->keymap_area)
2693 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2694 if (xkb_info->keymap_fd >= 0)
2695 close(xkb_info->keymap_fd);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002696 free(xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002697}
2698
2699void
2700weston_compositor_xkb_destroy(struct weston_compositor *ec)
2701{
2702 free((char *) ec->xkb_names.rules);
2703 free((char *) ec->xkb_names.model);
2704 free((char *) ec->xkb_names.layout);
2705 free((char *) ec->xkb_names.variant);
2706 free((char *) ec->xkb_names.options);
Stefan Schmidtfda26522013-09-17 10:54:09 +01002707
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002708 if (ec->xkb_info)
2709 weston_xkb_info_destroy(ec->xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002710 xkb_context_unref(ec->xkb_context);
2711}
2712
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002713static struct weston_xkb_info *
2714weston_xkb_info_create(struct xkb_keymap *keymap)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002715{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002716 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
2717 if (xkb_info == NULL)
2718 return NULL;
2719
Ran Benita2e1968f2014-08-19 23:59:51 +03002720 xkb_info->keymap = xkb_keymap_ref(keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002721 xkb_info->ref_count = 1;
2722
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002723 char *keymap_str;
2724
Ran Benita2e1968f2014-08-19 23:59:51 +03002725 xkb_info->shift_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2726 XKB_MOD_NAME_SHIFT);
2727 xkb_info->caps_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2728 XKB_MOD_NAME_CAPS);
2729 xkb_info->ctrl_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2730 XKB_MOD_NAME_CTRL);
2731 xkb_info->alt_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2732 XKB_MOD_NAME_ALT);
2733 xkb_info->mod2_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2734 "Mod2");
2735 xkb_info->mod3_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2736 "Mod3");
2737 xkb_info->super_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2738 XKB_MOD_NAME_LOGO);
2739 xkb_info->mod5_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2740 "Mod5");
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002741
Ran Benita2e1968f2014-08-19 23:59:51 +03002742 xkb_info->num_led = xkb_keymap_led_get_index(xkb_info->keymap,
2743 XKB_LED_NAME_NUM);
2744 xkb_info->caps_led = xkb_keymap_led_get_index(xkb_info->keymap,
2745 XKB_LED_NAME_CAPS);
2746 xkb_info->scroll_led = xkb_keymap_led_get_index(xkb_info->keymap,
2747 XKB_LED_NAME_SCROLL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002748
Ran Benita2e1968f2014-08-19 23:59:51 +03002749 keymap_str = xkb_keymap_get_as_string(xkb_info->keymap,
2750 XKB_KEYMAP_FORMAT_TEXT_V1);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002751 if (keymap_str == NULL) {
2752 weston_log("failed to get string version of keymap\n");
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002753 goto err_keymap;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002754 }
2755 xkb_info->keymap_size = strlen(keymap_str) + 1;
2756
2757 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
2758 if (xkb_info->keymap_fd < 0) {
2759 weston_log("creating a keymap file for %lu bytes failed: %m\n",
2760 (unsigned long) xkb_info->keymap_size);
2761 goto err_keymap_str;
2762 }
2763
2764 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2765 PROT_READ | PROT_WRITE,
2766 MAP_SHARED, xkb_info->keymap_fd, 0);
2767 if (xkb_info->keymap_area == MAP_FAILED) {
2768 weston_log("failed to mmap() %lu bytes\n",
2769 (unsigned long) xkb_info->keymap_size);
2770 goto err_dev_zero;
2771 }
2772 strcpy(xkb_info->keymap_area, keymap_str);
2773 free(keymap_str);
2774
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002775 return xkb_info;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002776
2777err_dev_zero:
2778 close(xkb_info->keymap_fd);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002779err_keymap_str:
2780 free(keymap_str);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002781err_keymap:
Ran Benita2e1968f2014-08-19 23:59:51 +03002782 xkb_keymap_unref(xkb_info->keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002783 free(xkb_info);
2784 return NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002785}
2786
2787static int
2788weston_compositor_build_global_keymap(struct weston_compositor *ec)
2789{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002790 struct xkb_keymap *keymap;
2791
2792 if (ec->xkb_info != NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002793 return 0;
2794
Ran Benita2e1968f2014-08-19 23:59:51 +03002795 keymap = xkb_keymap_new_from_names(ec->xkb_context,
2796 &ec->xkb_names,
2797 0);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002798 if (keymap == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002799 weston_log("failed to compile global XKB keymap\n");
2800 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
2801 "options %s\n",
2802 ec->xkb_names.rules, ec->xkb_names.model,
2803 ec->xkb_names.layout, ec->xkb_names.variant,
2804 ec->xkb_names.options);
2805 return -1;
2806 }
2807
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002808 ec->xkb_info = weston_xkb_info_create(keymap);
Rui Matos73d93952013-10-24 19:28:41 +02002809 xkb_keymap_unref(keymap);
Stefan Schmidtfda26522013-09-17 10:54:09 +01002810 if (ec->xkb_info == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002811 return -1;
2812
2813 return 0;
2814}
2815
Rui Matos65196bc2013-10-10 19:44:19 +02002816WL_EXPORT void
2817weston_seat_update_keymap(struct weston_seat *seat, struct xkb_keymap *keymap)
2818{
Derek Foreman1281a362015-07-31 16:55:32 -05002819 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2820
2821 if (!keyboard || !keymap)
Rui Matos65196bc2013-10-10 19:44:19 +02002822 return;
2823
Derek Foreman1281a362015-07-31 16:55:32 -05002824 xkb_keymap_unref(keyboard->pending_keymap);
2825 keyboard->pending_keymap = xkb_keymap_ref(keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02002826
Derek Foreman1281a362015-07-31 16:55:32 -05002827 if (keyboard->keys.size == 0)
Rui Matos65196bc2013-10-10 19:44:19 +02002828 update_keymap(seat);
Rui Matos65196bc2013-10-10 19:44:19 +02002829}
2830
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002831WL_EXPORT int
2832weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
2833{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002834 struct weston_keyboard *keyboard;
2835
Derek Foreman1281a362015-07-31 16:55:32 -05002836 if (seat->keyboard_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002837 seat->keyboard_device_count += 1;
2838 if (seat->keyboard_device_count == 1)
2839 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002840 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002841 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002842
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002843 keyboard = weston_keyboard_create();
2844 if (keyboard == NULL) {
2845 weston_log("failed to allocate weston keyboard struct\n");
2846 return -1;
2847 }
2848
Derek Foreman185d1582017-06-28 11:17:23 -05002849 if (keymap != NULL) {
2850 keyboard->xkb_info = weston_xkb_info_create(keymap);
2851 if (keyboard->xkb_info == NULL)
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02002852 goto err;
Derek Foreman185d1582017-06-28 11:17:23 -05002853 } else {
2854 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
2855 goto err;
2856 keyboard->xkb_info = seat->compositor->xkb_info;
2857 keyboard->xkb_info->ref_count++;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002858 }
Derek Foreman185d1582017-06-28 11:17:23 -05002859
2860 keyboard->xkb_state.state = xkb_state_new(keyboard->xkb_info->keymap);
2861 if (keyboard->xkb_state.state == NULL) {
2862 weston_log("failed to initialise XKB state\n");
2863 goto err;
2864 }
2865
2866 keyboard->xkb_state.leds = 0;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002867
Derek Foreman1281a362015-07-31 16:55:32 -05002868 seat->keyboard_state = keyboard;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02002869 seat->keyboard_device_count = 1;
2870 keyboard->seat = seat;
2871
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002872 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002873
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002874 return 0;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02002875
2876err:
2877 if (keyboard->xkb_info)
2878 weston_xkb_info_destroy(keyboard->xkb_info);
2879 free(keyboard);
2880
2881 return -1;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002882}
2883
Jonas Ådahl91fed542013-12-03 09:14:27 +01002884static void
2885weston_keyboard_reset_state(struct weston_keyboard *keyboard)
2886{
2887 struct weston_seat *seat = keyboard->seat;
2888 struct xkb_state *state;
2889
Derek Foreman185d1582017-06-28 11:17:23 -05002890 state = xkb_state_new(keyboard->xkb_info->keymap);
2891 if (!state) {
2892 weston_log("failed to reset XKB state\n");
2893 return;
Jonas Ådahl91fed542013-12-03 09:14:27 +01002894 }
Derek Foreman185d1582017-06-28 11:17:23 -05002895 xkb_state_unref(keyboard->xkb_state.state);
2896 keyboard->xkb_state.state = state;
2897
2898 keyboard->xkb_state.leds = 0;
Jonas Ådahl91fed542013-12-03 09:14:27 +01002899
2900 seat->modifier_state = 0;
2901}
2902
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002903WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002904weston_seat_release_keyboard(struct weston_seat *seat)
2905{
2906 seat->keyboard_device_count--;
Derek Foremand621df22014-11-19 11:04:12 -06002907 assert(seat->keyboard_device_count >= 0);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002908 if (seat->keyboard_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05002909 weston_keyboard_set_focus(seat->keyboard_state, NULL);
2910 weston_keyboard_cancel_grab(seat->keyboard_state);
2911 weston_keyboard_reset_state(seat->keyboard_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002912 seat_send_updated_caps(seat);
2913 }
2914}
2915
2916WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002917weston_seat_init_pointer(struct weston_seat *seat)
2918{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002919 struct weston_pointer *pointer;
2920
Derek Foreman1281a362015-07-31 16:55:32 -05002921 if (seat->pointer_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002922 seat->pointer_device_count += 1;
2923 if (seat->pointer_device_count == 1)
2924 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002925 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002926 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002927
Giulio Camuffocdb4d292013-11-14 23:42:53 +01002928 pointer = weston_pointer_create(seat);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002929 if (pointer == NULL)
2930 return;
2931
Derek Foreman1281a362015-07-31 16:55:32 -05002932 seat->pointer_state = pointer;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002933 seat->pointer_device_count = 1;
2934 pointer->seat = seat;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002935
2936 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002937}
2938
2939WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002940weston_seat_release_pointer(struct weston_seat *seat)
2941{
Derek Foreman1281a362015-07-31 16:55:32 -05002942 struct weston_pointer *pointer = seat->pointer_state;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002943
2944 seat->pointer_device_count--;
2945 if (seat->pointer_device_count == 0) {
Derek Foremanf9318d12015-05-11 15:40:11 -05002946 weston_pointer_clear_focus(pointer);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002947 weston_pointer_cancel_grab(pointer);
Jonas Ådahl630bae82013-10-17 23:04:06 +02002948
Jonas Ådahla4932742013-10-17 23:04:07 +02002949 if (pointer->sprite)
2950 pointer_unmap_sprite(pointer);
2951
Jonas Ådahl3e12e632013-12-02 22:05:05 +01002952 weston_pointer_reset_state(pointer);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002953 seat_send_updated_caps(seat);
Derek Foremanfd5ca512015-01-07 15:00:25 -06002954
2955 /* seat->pointer is intentionally not destroyed so that
2956 * a newly attached pointer on this seat will retain
2957 * the previous cursor co-ordinates.
2958 */
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002959 }
2960}
2961
2962WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002963weston_seat_init_touch(struct weston_seat *seat)
2964{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002965 struct weston_touch *touch;
2966
Derek Foreman1281a362015-07-31 16:55:32 -05002967 if (seat->touch_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002968 seat->touch_device_count += 1;
2969 if (seat->touch_device_count == 1)
2970 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002971 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002972 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002973
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002974 touch = weston_touch_create();
2975 if (touch == NULL)
2976 return;
2977
Derek Foreman1281a362015-07-31 16:55:32 -05002978 seat->touch_state = touch;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002979 seat->touch_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002980 touch->seat = seat;
2981
2982 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002983}
2984
2985WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002986weston_seat_release_touch(struct weston_seat *seat)
2987{
2988 seat->touch_device_count--;
2989 if (seat->touch_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05002990 weston_touch_set_focus(seat->touch_state, NULL);
2991 weston_touch_cancel_grab(seat->touch_state);
2992 weston_touch_reset_state(seat->touch_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002993 seat_send_updated_caps(seat);
2994 }
2995}
2996
2997WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01002998weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
2999 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003000{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003001 memset(seat, 0, sizeof *seat);
3002
Kristian Høgsberge3148752013-05-06 23:19:49 -04003003 seat->selection_data_source = NULL;
3004 wl_list_init(&seat->base_resource_list);
3005 wl_signal_init(&seat->selection_signal);
3006 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04003007 wl_signal_init(&seat->destroy_signal);
Jason Ekstranda4ab5422014-04-02 19:53:45 -05003008 wl_signal_init(&seat->updated_caps_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003009
Peter Hutterer87743e92016-01-18 16:38:22 +10003010 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 5,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003011 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003012
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003013 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003014 seat->modifier_state = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003015 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003016
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003017 wl_list_insert(ec->seat_list.prev, &seat->link);
3018
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003019 clipboard_create(seat);
3020
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003021 wl_signal_emit(&ec->seat_created_signal, seat);
3022}
3023
3024WL_EXPORT void
3025weston_seat_release(struct weston_seat *seat)
3026{
3027 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003028
Jonas Ådahl1afb2382014-01-03 19:46:51 +01003029 if (seat->saved_kbd_focus)
3030 wl_list_remove(&seat->saved_kbd_focus_listener.link);
3031
Derek Foreman1281a362015-07-31 16:55:32 -05003032 if (seat->pointer_state)
3033 weston_pointer_destroy(seat->pointer_state);
3034 if (seat->keyboard_state)
3035 weston_keyboard_destroy(seat->keyboard_state);
3036 if (seat->touch_state)
3037 weston_touch_destroy(seat->touch_state);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003038
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003039 free (seat->seat_name);
3040
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003041 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04003042
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003043 wl_signal_emit(&seat->destroy_signal, seat);
3044}
Derek Foreman1281a362015-07-31 16:55:32 -05003045
3046/** Get a seat's keyboard pointer
3047 *
3048 * \param seat The seat to query
3049 * \return The seat's keyboard pointer, or NULL if no keyboard is present
3050 *
3051 * The keyboard pointer for a seat isn't freed when all keyboards are removed,
3052 * so it should only be used when the seat's keyboard_device_count is greater
3053 * than zero. This function does that test and only returns a pointer
3054 * when a keyboard is present.
3055 */
3056WL_EXPORT struct weston_keyboard *
3057weston_seat_get_keyboard(struct weston_seat *seat)
3058{
3059 if (!seat)
3060 return NULL;
3061
3062 if (seat->keyboard_device_count)
3063 return seat->keyboard_state;
3064
3065 return NULL;
3066}
3067
3068/** Get a seat's pointer pointer
3069 *
3070 * \param seat The seat to query
3071 * \return The seat's pointer pointer, or NULL if no pointer device is present
3072 *
3073 * The pointer pointer for a seat isn't freed when all mice are removed,
3074 * so it should only be used when the seat's pointer_device_count is greater
3075 * than zero. This function does that test and only returns a pointer
3076 * when a pointing device is present.
3077 */
3078WL_EXPORT struct weston_pointer *
3079weston_seat_get_pointer(struct weston_seat *seat)
3080{
3081 if (!seat)
3082 return NULL;
3083
3084 if (seat->pointer_device_count)
3085 return seat->pointer_state;
3086
3087 return NULL;
3088}
3089
Jonas Ådahld3414f22016-07-22 17:56:31 +08003090static const struct zwp_locked_pointer_v1_interface locked_pointer_interface;
3091static const struct zwp_confined_pointer_v1_interface confined_pointer_interface;
3092
3093static enum pointer_constraint_type
3094pointer_constraint_get_type(struct weston_pointer_constraint *constraint)
3095{
3096 if (wl_resource_instance_of(constraint->resource,
3097 &zwp_locked_pointer_v1_interface,
3098 &locked_pointer_interface)) {
3099 return POINTER_CONSTRAINT_TYPE_LOCK;
3100 } else if (wl_resource_instance_of(constraint->resource,
3101 &zwp_confined_pointer_v1_interface,
3102 &confined_pointer_interface)) {
3103 return POINTER_CONSTRAINT_TYPE_CONFINE;
3104 }
3105
3106 abort();
3107 return 0;
3108}
3109
3110static void
3111pointer_constraint_notify_activated(struct weston_pointer_constraint *constraint)
3112{
3113 struct wl_resource *resource = constraint->resource;
3114
3115 switch (pointer_constraint_get_type(constraint)) {
3116 case POINTER_CONSTRAINT_TYPE_LOCK:
3117 zwp_locked_pointer_v1_send_locked(resource);
3118 break;
3119 case POINTER_CONSTRAINT_TYPE_CONFINE:
3120 zwp_confined_pointer_v1_send_confined(resource);
3121 break;
3122 }
3123}
3124
3125static void
3126pointer_constraint_notify_deactivated(struct weston_pointer_constraint *constraint)
3127{
3128 struct wl_resource *resource = constraint->resource;
3129
3130 switch (pointer_constraint_get_type(constraint)) {
3131 case POINTER_CONSTRAINT_TYPE_LOCK:
3132 zwp_locked_pointer_v1_send_unlocked(resource);
3133 break;
3134 case POINTER_CONSTRAINT_TYPE_CONFINE:
3135 zwp_confined_pointer_v1_send_unconfined(resource);
3136 break;
3137 }
3138}
3139
3140static struct weston_pointer_constraint *
3141get_pointer_constraint_for_pointer(struct weston_surface *surface,
3142 struct weston_pointer *pointer)
3143{
3144 struct weston_pointer_constraint *constraint;
3145
3146 wl_list_for_each(constraint, &surface->pointer_constraints, link) {
3147 if (constraint->pointer == pointer)
3148 return constraint;
3149 }
3150
3151 return NULL;
3152}
3153
Derek Foreman1281a362015-07-31 16:55:32 -05003154/** Get a seat's touch pointer
3155 *
3156 * \param seat The seat to query
3157 * \return The seat's touch pointer, or NULL if no touch device is present
3158 *
3159 * The touch pointer for a seat isn't freed when all touch devices are removed,
3160 * so it should only be used when the seat's touch_device_count is greater
3161 * than zero. This function does that test and only returns a pointer
3162 * when a touch device is present.
3163 */
3164WL_EXPORT struct weston_touch *
3165weston_seat_get_touch(struct weston_seat *seat)
3166{
3167 if (!seat)
3168 return NULL;
3169
3170 if (seat->touch_device_count)
3171 return seat->touch_state;
3172
3173 return NULL;
3174}
Bryce Harrington24f917e2016-06-29 19:04:07 -07003175
3176/** Sets the keyboard focus to the given surface
3177 *
3178 * \param seat The seat to query
3179 */
3180WL_EXPORT void
3181weston_seat_set_keyboard_focus(struct weston_seat *seat,
3182 struct weston_surface *surface)
3183{
3184 struct weston_compositor *compositor = seat->compositor;
3185 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003186 struct weston_surface_activation_data activation_data;
Bryce Harrington24f917e2016-06-29 19:04:07 -07003187
Jonas Ådahlef8e1c32016-03-15 20:28:51 +08003188 if (keyboard && keyboard->focus != surface) {
Bryce Harrington24f917e2016-06-29 19:04:07 -07003189 weston_keyboard_set_focus(keyboard, surface);
3190 wl_data_device_set_keyboard_focus(seat);
3191 }
3192
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003193 inc_activate_serial(compositor);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003194
3195 activation_data = (struct weston_surface_activation_data) {
3196 .surface = surface,
3197 .seat = seat,
3198 };
3199 wl_signal_emit(&compositor->activate_signal, &activation_data);
Bryce Harrington24f917e2016-06-29 19:04:07 -07003200}
Jonas Ådahl30d61d82014-10-22 21:21:17 +02003201
Jonas Ådahld3414f22016-07-22 17:56:31 +08003202static void
3203enable_pointer_constraint(struct weston_pointer_constraint *constraint,
3204 struct weston_view *view)
3205{
3206 assert(constraint->view == NULL);
3207 constraint->view = view;
3208 pointer_constraint_notify_activated(constraint);
3209 weston_pointer_start_grab(constraint->pointer, &constraint->grab);
3210 wl_list_remove(&constraint->surface_destroy_listener.link);
3211 wl_list_init(&constraint->surface_destroy_listener.link);
3212}
3213
3214static bool
3215is_pointer_constraint_enabled(struct weston_pointer_constraint *constraint)
3216{
3217 return constraint->view != NULL;
3218}
3219
3220static void
3221weston_pointer_constraint_disable(struct weston_pointer_constraint *constraint)
3222{
3223 constraint->view = NULL;
3224 pointer_constraint_notify_deactivated(constraint);
3225 weston_pointer_end_grab(constraint->grab.pointer);
3226}
3227
3228void
3229weston_pointer_constraint_destroy(struct weston_pointer_constraint *constraint)
3230{
3231 if (is_pointer_constraint_enabled(constraint))
3232 weston_pointer_constraint_disable(constraint);
3233
3234 wl_list_remove(&constraint->pointer_destroy_listener.link);
3235 wl_list_remove(&constraint->surface_destroy_listener.link);
3236 wl_list_remove(&constraint->surface_commit_listener.link);
3237 wl_list_remove(&constraint->surface_activate_listener.link);
3238
3239 wl_resource_set_user_data(constraint->resource, NULL);
3240 pixman_region32_fini(&constraint->region);
3241 wl_list_remove(&constraint->link);
3242 free(constraint);
3243}
3244
3245static void
3246disable_pointer_constraint(struct weston_pointer_constraint *constraint)
3247{
3248 switch (constraint->lifetime) {
3249 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT:
3250 weston_pointer_constraint_destroy(constraint);
3251 break;
3252 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT:
3253 weston_pointer_constraint_disable(constraint);
3254 break;
3255 }
3256}
3257
3258static bool
3259is_within_constraint_region(struct weston_pointer_constraint *constraint,
3260 wl_fixed_t sx, wl_fixed_t sy)
3261{
3262 struct weston_surface *surface = constraint->surface;
3263 pixman_region32_t constraint_region;
3264 bool result;
3265
3266 pixman_region32_init(&constraint_region);
3267 pixman_region32_intersect(&constraint_region,
3268 &surface->input,
3269 &constraint->region);
3270 result = pixman_region32_contains_point(&constraint_region,
3271 wl_fixed_to_int(sx),
3272 wl_fixed_to_int(sy),
3273 NULL);
3274 pixman_region32_fini(&constraint_region);
3275
3276 return result;
3277}
3278
3279static void
3280maybe_enable_pointer_constraint(struct weston_pointer_constraint *constraint)
3281{
3282 struct weston_surface *surface = constraint->surface;
3283 struct weston_view *vit;
3284 struct weston_view *view = NULL;
3285 struct weston_pointer *pointer = constraint->pointer;
3286 struct weston_keyboard *keyboard;
3287 struct weston_seat *seat = pointer->seat;
3288 int32_t x, y;
3289
3290 /* Postpone if no view of the surface was most recently clicked. */
3291 wl_list_for_each(vit, &surface->views, surface_link) {
3292 if (vit->click_to_activate_serial ==
3293 surface->compositor->activate_serial) {
3294 view = vit;
3295 }
3296 }
3297 if (view == NULL)
3298 return;
3299
3300 /* Postpone if surface doesn't have keyboard focus. */
3301 keyboard = weston_seat_get_keyboard(seat);
3302 if (!keyboard || keyboard->focus != surface)
3303 return;
3304
3305 /* Postpone constraint if the pointer is not within the
3306 * constraint region.
3307 */
3308 weston_view_from_global(view,
3309 wl_fixed_to_int(pointer->x),
3310 wl_fixed_to_int(pointer->y),
3311 &x, &y);
3312 if (!is_within_constraint_region(constraint,
3313 wl_fixed_from_int(x),
3314 wl_fixed_from_int(y)))
3315 return;
3316
3317 enable_pointer_constraint(constraint, view);
3318}
3319
3320static void
3321locked_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
3322{
3323}
3324
3325static void
3326locked_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02003327 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003328 struct weston_pointer_motion_event *event)
3329{
Quentin Glidiccde13452016-08-12 10:41:32 +02003330 pointer_send_relative_motion(grab->pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08003331}
3332
3333static void
3334locked_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003335 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003336 uint32_t button,
3337 uint32_t state_w)
3338{
3339 weston_pointer_send_button(grab->pointer, time, button, state_w);
3340}
3341
3342static void
3343locked_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02003344 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08003345 struct weston_pointer_axis_event *event)
3346{
3347 weston_pointer_send_axis(grab->pointer, time, event);
3348}
3349
3350static void
3351locked_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
3352 uint32_t source)
3353{
3354 weston_pointer_send_axis_source(grab->pointer, source);
3355}
3356
3357static void
3358locked_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
3359{
3360 weston_pointer_send_frame(grab->pointer);
3361}
3362
3363static void
3364locked_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
3365{
3366 struct weston_pointer_constraint *constraint =
3367 container_of(grab, struct weston_pointer_constraint, grab);
3368
3369 disable_pointer_constraint(constraint);
3370}
3371
3372static const struct weston_pointer_grab_interface
3373 locked_pointer_grab_interface = {
3374 locked_pointer_grab_pointer_focus,
3375 locked_pointer_grab_pointer_motion,
3376 locked_pointer_grab_pointer_button,
3377 locked_pointer_grab_pointer_axis,
3378 locked_pointer_grab_pointer_axis_source,
3379 locked_pointer_grab_pointer_frame,
3380 locked_pointer_grab_pointer_cancel,
3381};
3382
3383static void
3384pointer_constraint_constrain_resource_destroyed(struct wl_resource *resource)
3385{
3386 struct weston_pointer_constraint *constraint =
3387 wl_resource_get_user_data(resource);
3388
3389 if (!constraint)
3390 return;
3391
3392 weston_pointer_constraint_destroy(constraint);
3393}
3394
3395static void
3396pointer_constraint_surface_activate(struct wl_listener *listener, void *data)
3397{
3398 struct weston_surface_activation_data *activation = data;
3399 struct weston_pointer *pointer;
3400 struct weston_surface *focus = activation->surface;
3401 struct weston_pointer_constraint *constraint =
3402 container_of(listener, struct weston_pointer_constraint,
3403 surface_activate_listener);
3404 bool is_constraint_surface;
3405
3406 pointer = weston_seat_get_pointer(activation->seat);
3407 if (!pointer)
3408 return;
3409
3410 is_constraint_surface =
3411 get_pointer_constraint_for_pointer(focus, pointer) == constraint;
3412
3413 if (is_constraint_surface &&
3414 !is_pointer_constraint_enabled(constraint))
3415 maybe_enable_pointer_constraint(constraint);
3416 else if (!is_constraint_surface &&
3417 is_pointer_constraint_enabled(constraint))
3418 disable_pointer_constraint(constraint);
3419}
3420
3421static void
3422pointer_constraint_pointer_destroyed(struct wl_listener *listener, void *data)
3423{
3424 struct weston_pointer_constraint *constraint =
3425 container_of(listener, struct weston_pointer_constraint,
3426 pointer_destroy_listener);
3427
3428 weston_pointer_constraint_destroy(constraint);
3429}
3430
3431static void
3432pointer_constraint_surface_destroyed(struct wl_listener *listener, void *data)
3433{
3434 struct weston_pointer_constraint *constraint =
3435 container_of(listener, struct weston_pointer_constraint,
3436 surface_destroy_listener);
3437
3438 weston_pointer_constraint_destroy(constraint);
3439}
3440
3441static void
3442pointer_constraint_surface_committed(struct wl_listener *listener, void *data)
3443{
3444 struct weston_pointer_constraint *constraint =
3445 container_of(listener, struct weston_pointer_constraint,
3446 surface_commit_listener);
3447
3448 if (constraint->region_is_pending) {
3449 constraint->region_is_pending = false;
3450 pixman_region32_copy(&constraint->region,
3451 &constraint->region_pending);
3452 pixman_region32_fini(&constraint->region_pending);
3453 pixman_region32_init(&constraint->region_pending);
3454 }
3455
3456 if (constraint->hint_is_pending) {
3457 constraint->hint_is_pending = false;
3458
3459 constraint->hint_is_pending = true;
3460 constraint->hint_x = constraint->hint_x_pending;
3461 constraint->hint_y = constraint->hint_y_pending;
3462 }
3463
3464 if (pointer_constraint_get_type(constraint) ==
3465 POINTER_CONSTRAINT_TYPE_CONFINE &&
3466 is_pointer_constraint_enabled(constraint))
3467 maybe_warp_confined_pointer(constraint);
3468}
3469
3470static struct weston_pointer_constraint *
3471weston_pointer_constraint_create(struct weston_surface *surface,
3472 struct weston_pointer *pointer,
3473 struct weston_region *region,
3474 enum zwp_pointer_constraints_v1_lifetime lifetime,
3475 struct wl_resource *cr,
3476 const struct weston_pointer_grab_interface *grab_interface)
3477{
3478 struct weston_pointer_constraint *constraint;
3479
3480 constraint = zalloc(sizeof *constraint);
3481 if (!constraint)
3482 return NULL;
3483
3484 constraint->lifetime = lifetime;
3485 pixman_region32_init(&constraint->region);
3486 pixman_region32_init(&constraint->region_pending);
3487 wl_list_insert(&surface->pointer_constraints, &constraint->link);
3488 constraint->surface = surface;
3489 constraint->pointer = pointer;
3490 constraint->resource = cr;
3491 constraint->grab.interface = grab_interface;
3492 if (region) {
3493 pixman_region32_copy(&constraint->region,
3494 &region->region);
3495 } else {
3496 pixman_region32_fini(&constraint->region);
3497 region_init_infinite(&constraint->region);
3498 }
3499
3500 constraint->surface_activate_listener.notify =
3501 pointer_constraint_surface_activate;
3502 constraint->surface_destroy_listener.notify =
3503 pointer_constraint_surface_destroyed;
3504 constraint->surface_commit_listener.notify =
3505 pointer_constraint_surface_committed;
3506 constraint->pointer_destroy_listener.notify =
3507 pointer_constraint_pointer_destroyed;
3508
3509 wl_signal_add(&surface->compositor->activate_signal,
3510 &constraint->surface_activate_listener);
3511 wl_signal_add(&pointer->destroy_signal,
3512 &constraint->pointer_destroy_listener);
3513 wl_signal_add(&surface->destroy_signal,
3514 &constraint->surface_destroy_listener);
3515 wl_signal_add(&surface->commit_signal,
3516 &constraint->surface_commit_listener);
3517
3518 return constraint;
3519}
3520
3521static void
3522init_pointer_constraint(struct wl_resource *pointer_constraints_resource,
3523 uint32_t id,
3524 struct weston_surface *surface,
3525 struct weston_pointer *pointer,
3526 struct weston_region *region,
3527 enum zwp_pointer_constraints_v1_lifetime lifetime,
3528 const struct wl_interface *interface,
3529 const void *implementation,
3530 const struct weston_pointer_grab_interface *grab_interface)
3531{
3532 struct wl_client *client =
3533 wl_resource_get_client(pointer_constraints_resource);
3534 struct wl_resource *cr;
3535 struct weston_pointer_constraint *constraint;
3536
3537 if (get_pointer_constraint_for_pointer(surface, pointer)) {
3538 wl_resource_post_error(pointer_constraints_resource,
3539 ZWP_POINTER_CONSTRAINTS_V1_ERROR_ALREADY_CONSTRAINED,
3540 "the pointer has a lock/confine request on this surface");
3541 return;
3542 }
3543
3544 cr = wl_resource_create(client, interface,
3545 wl_resource_get_version(pointer_constraints_resource),
3546 id);
3547 if (cr == NULL) {
3548 wl_client_post_no_memory(client);
3549 return;
3550 }
3551
3552 constraint = weston_pointer_constraint_create(surface, pointer,
3553 region, lifetime,
3554 cr, grab_interface);
3555 if (constraint == NULL) {
3556 wl_client_post_no_memory(client);
3557 return;
3558 }
3559
3560 wl_resource_set_implementation(cr, implementation, constraint,
3561 pointer_constraint_constrain_resource_destroyed);
3562
3563 maybe_enable_pointer_constraint(constraint);
3564}
3565
3566static void
3567pointer_constraints_destroy(struct wl_client *client,
3568 struct wl_resource *resource)
3569{
3570 wl_resource_destroy(resource);
3571}
3572
3573static void
3574locked_pointer_destroy(struct wl_client *client,
3575 struct wl_resource *resource)
3576{
3577 struct weston_pointer_constraint *constraint =
3578 wl_resource_get_user_data(resource);
3579 wl_fixed_t x, y;
3580
3581 if (constraint && constraint->view && constraint->hint_is_pending &&
3582 is_within_constraint_region(constraint,
3583 constraint->hint_x,
3584 constraint->hint_y)) {
3585 weston_view_to_global_fixed(constraint->view,
3586 constraint->hint_x,
3587 constraint->hint_y,
3588 &x, &y);
3589 weston_pointer_move_to(constraint->pointer, x, y);
3590 }
3591 wl_resource_destroy(resource);
3592}
3593
3594static void
3595locked_pointer_set_cursor_position_hint(struct wl_client *client,
3596 struct wl_resource *resource,
3597 wl_fixed_t surface_x,
3598 wl_fixed_t surface_y)
3599{
3600 struct weston_pointer_constraint *constraint =
3601 wl_resource_get_user_data(resource);
3602
3603 /* Ignore a set cursor hint that was sent after the lock was cancelled.
3604 */
3605 if (!constraint ||
3606 !constraint->resource ||
3607 constraint->resource != resource)
3608 return;
3609
3610 constraint->hint_is_pending = true;
3611 constraint->hint_x_pending = surface_x;
3612 constraint->hint_y_pending = surface_y;
3613}
3614
3615static void
3616locked_pointer_set_region(struct wl_client *client,
3617 struct wl_resource *resource,
3618 struct wl_resource *region_resource)
3619{
3620 struct weston_pointer_constraint *constraint =
3621 wl_resource_get_user_data(resource);
3622 struct weston_region *region = region_resource ?
3623 wl_resource_get_user_data(region_resource) : NULL;
3624
3625 if (!constraint)
3626 return;
3627
3628 if (region) {
3629 pixman_region32_copy(&constraint->region_pending,
3630 &region->region);
3631 } else {
3632 pixman_region32_fini(&constraint->region_pending);
3633 region_init_infinite(&constraint->region_pending);
3634 }
3635 constraint->region_is_pending = true;
3636}
3637
3638
3639static const struct zwp_locked_pointer_v1_interface locked_pointer_interface = {
3640 locked_pointer_destroy,
3641 locked_pointer_set_cursor_position_hint,
3642 locked_pointer_set_region,
3643};
3644
3645static void
3646pointer_constraints_lock_pointer(struct wl_client *client,
3647 struct wl_resource *resource,
3648 uint32_t id,
3649 struct wl_resource *surface_resource,
3650 struct wl_resource *pointer_resource,
3651 struct wl_resource *region_resource,
3652 uint32_t lifetime)
3653{
3654 struct weston_surface *surface =
3655 wl_resource_get_user_data(surface_resource);
3656 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
3657 struct weston_region *region = region_resource ?
3658 wl_resource_get_user_data(region_resource) : NULL;
3659
3660 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
3661 &zwp_locked_pointer_v1_interface,
3662 &locked_pointer_interface,
3663 &locked_pointer_grab_interface);
3664}
3665
3666static void
3667confined_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
3668{
3669}
3670
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08003671static double
3672vec2d_cross_product(struct vec2d a, struct vec2d b)
3673{
3674 return a.x * b.y - a.y * b.x;
3675}
3676
3677static struct vec2d
3678vec2d_add(struct vec2d a, struct vec2d b)
3679{
3680 return (struct vec2d) {
3681 .x = a.x + b.x,
3682 .y = a.y + b.y,
3683 };
3684}
3685
3686static struct vec2d
3687vec2d_subtract(struct vec2d a, struct vec2d b)
3688{
3689 return (struct vec2d) {
3690 .x = a.x - b.x,
3691 .y = a.y - b.y,
3692 };
3693}
3694
3695static struct vec2d
3696vec2d_multiply_constant(double c, struct vec2d a)
3697{
3698 return (struct vec2d) {
3699 .x = c * a.x,
3700 .y = c * a.y,
3701 };
3702}
3703
3704static bool
3705lines_intersect(struct line *line1, struct line *line2,
3706 struct vec2d *intersection)
3707{
3708 struct vec2d p = line1->a;
3709 struct vec2d r = vec2d_subtract(line1->b, line1->a);
3710 struct vec2d q = line2->a;
3711 struct vec2d s = vec2d_subtract(line2->b, line2->a);
3712 double rxs;
3713 double sxr;
3714 double t;
3715 double u;
3716
3717 /*
3718 * The line (p, r) and (q, s) intersects where
3719 *
3720 * p + t r = q + u s
3721 *
3722 * Calculate t:
3723 *
3724 * (p + t r) × s = (q + u s) × s
3725 * p × s + t (r × s) = q × s + u (s × s)
3726 * p × s + t (r × s) = q × s
3727 * t (r × s) = q × s - p × s
3728 * t (r × s) = (q - p) × s
3729 * t = ((q - p) × s) / (r × s)
3730 *
3731 * Using the same method, for u we get:
3732 *
3733 * u = ((p - q) × r) / (s × r)
3734 */
3735
3736 rxs = vec2d_cross_product(r, s);
3737 sxr = vec2d_cross_product(s, r);
3738
3739 /* If r × s = 0 then the lines are either parallel or collinear. */
3740 if (fabs(rxs) < DBL_MIN)
3741 return false;
3742
3743 t = vec2d_cross_product(vec2d_subtract(q, p), s) / rxs;
3744 u = vec2d_cross_product(vec2d_subtract(p, q), r) / sxr;
3745
3746 /* The lines only intersect if 0 ≤ t ≤ 1 and 0 ≤ u ≤ 1. */
3747 if (t < 0.0 || t > 1.0 || u < 0.0 || u > 1.0)
3748 return false;
3749
3750 *intersection = vec2d_add(p, vec2d_multiply_constant(t, r));
3751 return true;
3752}
3753
3754static struct border *
3755add_border(struct wl_array *array,
3756 double x1, double y1,
3757 double x2, double y2,
3758 enum motion_direction blocking_dir)
3759{
3760 struct border *border = wl_array_add(array, sizeof *border);
3761
3762 *border = (struct border) {
3763 .line = (struct line) {
3764 .a = (struct vec2d) {
3765 .x = x1,
3766 .y = y1,
3767 },
3768 .b = (struct vec2d) {
3769 .x = x2,
3770 .y = y2,
3771 },
3772 },
3773 .blocking_dir = blocking_dir,
3774 };
3775
3776 return border;
3777}
3778
3779static int
3780compare_lines_x(const void *a, const void *b)
3781{
3782 const struct border *border_a = a;
3783 const struct border *border_b = b;
3784
3785
3786 if (border_a->line.a.x == border_b->line.a.x)
3787 return border_a->line.b.x < border_b->line.b.x;
3788 else
3789 return border_a->line.a.x > border_b->line.a.x;
3790}
3791
3792static void
3793add_non_overlapping_edges(pixman_box32_t *boxes,
3794 int band_above_start,
3795 int band_below_start,
3796 int band_below_end,
3797 struct wl_array *borders)
3798{
3799 int i;
3800 struct wl_array band_merge;
3801 struct border *border;
3802 struct border *prev_border;
3803 struct border *new_border;
3804
3805 wl_array_init(&band_merge);
3806
3807 /* Add bottom band of previous row, and top band of current row, and
3808 * sort them so lower left x coordinate comes first. If there are two
3809 * borders with the same left x coordinate, the wider one comes first.
3810 */
3811 for (i = band_above_start; i < band_below_start; i++) {
3812 pixman_box32_t *box = &boxes[i];
3813 add_border(&band_merge, box->x1, box->y2, box->x2, box->y2,
3814 MOTION_DIRECTION_POSITIVE_Y);
3815 }
3816 for (i = band_below_start; i < band_below_end; i++) {
3817 pixman_box32_t *box= &boxes[i];
3818 add_border(&band_merge, box->x1, box->y1, box->x2, box->y1,
3819 MOTION_DIRECTION_NEGATIVE_Y);
3820 }
3821 qsort(band_merge.data,
3822 band_merge.size / sizeof *border,
3823 sizeof *border,
3824 compare_lines_x);
3825
3826 /* Combine the two combined bands so that any overlapping border is
3827 * eliminated. */
3828 prev_border = NULL;
3829 wl_array_for_each(border, &band_merge) {
3830 assert(border->line.a.y == border->line.b.y);
3831 assert(!prev_border ||
3832 prev_border->line.a.y == border->line.a.y);
3833 assert(!prev_border ||
3834 (prev_border->line.a.x != border->line.a.x ||
3835 prev_border->line.b.x != border->line.b.x));
3836 assert(!prev_border ||
3837 prev_border->line.a.x <= border->line.a.x);
3838
3839 if (prev_border &&
3840 prev_border->line.a.x == border->line.a.x) {
3841 /*
3842 * ------------ +
3843 * ------- =
3844 * [ ]-----
3845 */
3846 prev_border->line.a.x = border->line.b.x;
3847 } else if (prev_border &&
3848 prev_border->line.b.x == border->line.b.x) {
3849 /*
3850 * ------------ +
3851 * ------ =
3852 * ------[ ]
3853 */
3854 prev_border->line.b.x = border->line.a.x;
3855 } else if (prev_border &&
3856 prev_border->line.b.x == border->line.a.x) {
3857 /*
3858 * -------- +
3859 * ------ =
3860 * --------------
3861 */
3862 prev_border->line.b.x = border->line.b.x;
3863 } else if (prev_border &&
3864 prev_border->line.b.x >= border->line.a.x) {
3865 /*
3866 * --------------- +
3867 * ------ =
3868 * -----[ ]----
3869 */
3870 new_border = add_border(borders,
3871 border->line.b.x,
3872 border->line.b.y,
3873 prev_border->line.b.x,
3874 prev_border->line.b.y,
3875 prev_border->blocking_dir);
3876 prev_border->line.b.x = border->line.a.x;
3877 prev_border = new_border;
3878 } else {
3879 assert(!prev_border ||
3880 prev_border->line.b.x < border->line.a.x);
3881 /*
3882 * First border or non-overlapping.
3883 *
3884 * ----- +
3885 * ----- =
3886 * ----- -----
3887 */
3888 new_border = wl_array_add(borders, sizeof *border);
3889 *new_border = *border;
3890 prev_border = new_border;
3891 }
3892 }
3893
3894 wl_array_release(&band_merge);
3895}
3896
3897static void
3898add_band_bottom_edges(pixman_box32_t *boxes,
3899 int band_start,
3900 int band_end,
3901 struct wl_array *borders)
3902{
3903 int i;
3904
3905 for (i = band_start; i < band_end; i++) {
3906 add_border(borders,
3907 boxes[i].x1, boxes[i].y2,
3908 boxes[i].x2, boxes[i].y2,
3909 MOTION_DIRECTION_POSITIVE_Y);
3910 }
3911}
3912
3913static void
3914region_to_outline(pixman_region32_t *region, struct wl_array *borders)
3915{
3916 pixman_box32_t *boxes;
3917 int num_boxes;
3918 int i;
3919 int top_most, bottom_most;
3920 int current_roof;
3921 int prev_top;
3922 int band_start, prev_band_start;
3923
3924 /*
3925 * Remove any overlapping lines from the set of rectangles. Note that
3926 * pixman regions are grouped as rows of rectangles, where rectangles
3927 * in one row never touch or overlap and are all of the same height.
3928 *
3929 * -------- --- -------- ---
3930 * | | | | | | | |
3931 * ----------====---- --- ----------- ----- ---
3932 * | | => | |
3933 * ----==========--------- ----- ----------
3934 * | | | |
3935 * ------------------- -------------------
3936 *
3937 */
3938
3939 boxes = pixman_region32_rectangles(region, &num_boxes);
3940 prev_top = 0;
3941 top_most = boxes[0].y1;
3942 current_roof = top_most;
3943 bottom_most = boxes[num_boxes - 1].y2;
3944 band_start = 0;
3945 prev_band_start = 0;
3946 for (i = 0; i < num_boxes; i++) {
3947 /* Detect if there is a vertical empty space, and add the lower
3948 * level of the previous band if so was the case. */
3949 if (i > 0 &&
3950 boxes[i].y1 != prev_top &&
3951 boxes[i].y1 != boxes[i - 1].y2) {
3952 current_roof = boxes[i].y1;
3953 add_band_bottom_edges(boxes,
3954 band_start,
3955 i,
3956 borders);
3957 }
3958
3959 /* Special case adding the last band, since it won't be handled
3960 * by the band change detection below. */
3961 if (boxes[i].y1 != current_roof && i == num_boxes - 1) {
3962 if (boxes[i].y1 != prev_top) {
3963 /* The last band is a single box, so we don't
3964 * have a prev_band_start to tell us when the
3965 * previous band started. */
3966 add_non_overlapping_edges(boxes,
3967 band_start,
3968 i,
3969 i + 1,
3970 borders);
3971 } else {
3972 add_non_overlapping_edges(boxes,
3973 prev_band_start,
3974 band_start,
3975 i + 1,
3976 borders);
3977 }
3978 }
3979
3980 /* Detect when passing a band and combine the top border of the
3981 * just passed band with the bottom band of the previous band.
3982 */
3983 if (boxes[i].y1 != top_most && boxes[i].y1 != prev_top) {
3984 /* Combine the two passed bands. */
3985 if (prev_top != current_roof) {
3986 add_non_overlapping_edges(boxes,
3987 prev_band_start,
3988 band_start,
3989 i,
3990 borders);
3991 }
3992
3993 prev_band_start = band_start;
3994 band_start = i;
3995 }
3996
3997 /* Add the top border if the box is part of the current roof. */
3998 if (boxes[i].y1 == current_roof) {
3999 add_border(borders,
4000 boxes[i].x1, boxes[i].y1,
4001 boxes[i].x2, boxes[i].y1,
4002 MOTION_DIRECTION_NEGATIVE_Y);
4003 }
4004
4005 /* Add the bottom border of the last band. */
4006 if (boxes[i].y2 == bottom_most) {
4007 add_border(borders,
4008 boxes[i].x1, boxes[i].y2,
4009 boxes[i].x2, boxes[i].y2,
4010 MOTION_DIRECTION_POSITIVE_Y);
4011 }
4012
4013 /* Always add the left border. */
4014 add_border(borders,
4015 boxes[i].x1, boxes[i].y1,
4016 boxes[i].x1, boxes[i].y2,
4017 MOTION_DIRECTION_NEGATIVE_X);
4018
4019 /* Always add the right border. */
4020 add_border(borders,
4021 boxes[i].x2, boxes[i].y1,
4022 boxes[i].x2, boxes[i].y2,
4023 MOTION_DIRECTION_POSITIVE_X);
4024
4025 prev_top = boxes[i].y1;
4026 }
4027}
4028
4029static bool
4030is_border_horizontal (struct border *border)
4031{
4032 return border->line.a.y == border->line.b.y;
4033}
4034
4035static bool
4036is_border_blocking_directions(struct border *border,
4037 uint32_t directions)
4038{
4039 /* Don't block parallel motions. */
4040 if (is_border_horizontal(border)) {
4041 if ((directions & (MOTION_DIRECTION_POSITIVE_Y |
4042 MOTION_DIRECTION_NEGATIVE_Y)) == 0)
4043 return false;
4044 } else {
4045 if ((directions & (MOTION_DIRECTION_POSITIVE_X |
4046 MOTION_DIRECTION_NEGATIVE_X)) == 0)
4047 return false;
4048 }
4049
4050 return (~border->blocking_dir & directions) != directions;
4051}
4052
4053static struct border *
4054get_closest_border(struct wl_array *borders,
4055 struct line *motion,
4056 uint32_t directions)
4057{
4058 struct border *border;
4059 struct vec2d intersection;
4060 struct vec2d delta;
4061 double distance_2;
4062 struct border *closest_border = NULL;
4063 double closest_distance_2 = DBL_MAX;
4064
4065 wl_array_for_each(border, borders) {
4066 if (!is_border_blocking_directions(border, directions))
4067 continue;
4068
4069 if (!lines_intersect(&border->line, motion, &intersection))
4070 continue;
4071
4072 delta = vec2d_subtract(intersection, motion->a);
4073 distance_2 = delta.x*delta.x + delta.y*delta.y;
4074 if (distance_2 < closest_distance_2) {
4075 closest_border = border;
4076 closest_distance_2 = distance_2;
4077 }
4078 }
4079
4080 return closest_border;
4081}
4082
4083static void
4084clamp_to_border(struct border *border,
4085 struct line *motion,
4086 uint32_t *motion_dir)
4087{
4088 /*
4089 * When clamping either rightward or downward motions, the motion needs
4090 * to be clamped so that the destination coordinate does not end up on
4091 * the border (see weston_pointer_clamp_event_to_region). Do this by
4092 * clamping such motions to the border minus the smallest possible
4093 * wl_fixed_t value.
4094 */
4095 if (is_border_horizontal(border)) {
4096 if (*motion_dir & MOTION_DIRECTION_POSITIVE_Y)
4097 motion->b.y = border->line.a.y - wl_fixed_to_double(1);
4098 else
4099 motion->b.y = border->line.a.y;
4100 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_Y |
4101 MOTION_DIRECTION_NEGATIVE_Y);
4102 } else {
4103 if (*motion_dir & MOTION_DIRECTION_POSITIVE_X)
4104 motion->b.x = border->line.a.x - wl_fixed_to_double(1);
4105 else
4106 motion->b.x = border->line.a.x;
4107 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_X |
4108 MOTION_DIRECTION_NEGATIVE_X);
4109 }
4110}
4111
4112static uint32_t
4113get_motion_directions(struct line *motion)
4114{
4115 uint32_t directions = 0;
4116
4117 if (motion->a.x < motion->b.x)
4118 directions |= MOTION_DIRECTION_POSITIVE_X;
4119 else if (motion->a.x > motion->b.x)
4120 directions |= MOTION_DIRECTION_NEGATIVE_X;
4121 if (motion->a.y < motion->b.y)
4122 directions |= MOTION_DIRECTION_POSITIVE_Y;
4123 else if (motion->a.y > motion->b.y)
4124 directions |= MOTION_DIRECTION_NEGATIVE_Y;
4125
4126 return directions;
4127}
4128
Jonas Ådahld3414f22016-07-22 17:56:31 +08004129static void
4130weston_pointer_clamp_event_to_region(struct weston_pointer *pointer,
4131 struct weston_pointer_motion_event *event,
4132 pixman_region32_t *region,
4133 wl_fixed_t *clamped_x,
4134 wl_fixed_t *clamped_y)
4135{
4136 wl_fixed_t x, y;
4137 wl_fixed_t sx, sy;
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004138 wl_fixed_t old_sx = pointer->sx;
4139 wl_fixed_t old_sy = pointer->sy;
4140 struct wl_array borders;
4141 struct line motion;
4142 struct border *closest_border;
4143 float new_x_f, new_y_f;
4144 uint32_t directions;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004145
4146 weston_pointer_motion_to_abs(pointer, event, &x, &y);
4147 weston_view_from_global_fixed(pointer->focus, x, y, &sx, &sy);
4148
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004149 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004150
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004151 /*
4152 * Generate borders given the confine region we are to use. The borders
4153 * are defined to be the outer region of the allowed area. This means
4154 * top/left borders are "within" the allowed area, while bottom/right
4155 * borders are outside. This needs to be considered when clamping
4156 * confined motion vectors.
4157 */
4158 region_to_outline(region, &borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004159
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004160 motion = (struct line) {
4161 .a = (struct vec2d) {
4162 .x = wl_fixed_to_double(old_sx),
4163 .y = wl_fixed_to_double(old_sy),
4164 },
4165 .b = (struct vec2d) {
4166 .x = wl_fixed_to_double(sx),
4167 .y = wl_fixed_to_double(sy),
4168 },
4169 };
4170 directions = get_motion_directions(&motion);
4171
4172 while (directions) {
4173 closest_border = get_closest_border(&borders,
4174 &motion,
4175 directions);
4176 if (closest_border)
4177 clamp_to_border(closest_border, &motion, &directions);
4178 else
4179 break;
4180 }
4181
4182 weston_view_to_global_float(pointer->focus,
4183 (float) motion.b.x, (float) motion.b.y,
4184 &new_x_f, &new_y_f);
4185 *clamped_x = wl_fixed_from_double(new_x_f);
4186 *clamped_y = wl_fixed_from_double(new_y_f);
4187
4188 wl_array_release(&borders);
4189}
4190
4191static double
4192point_to_border_distance_2(struct border *border, double x, double y)
4193{
4194 double orig_x, orig_y;
4195 double dx, dy;
4196
4197 if (is_border_horizontal(border)) {
4198 if (x < border->line.a.x)
4199 orig_x = border->line.a.x;
4200 else if (x > border->line.b.x)
4201 orig_x = border->line.b.x;
4202 else
4203 orig_x = x;
4204 orig_y = border->line.a.y;
4205 } else {
4206 if (y < border->line.a.y)
4207 orig_y = border->line.a.y;
4208 else if (y > border->line.b.y)
4209 orig_y = border->line.b.y;
4210 else
4211 orig_y = y;
4212 orig_x = border->line.a.x;
4213 }
4214
4215
4216 dx = fabs(orig_x - x);
4217 dy = fabs(orig_y - y);
4218 return dx*dx + dy*dy;
4219}
4220
4221static void
4222warp_to_behind_border(struct border *border, wl_fixed_t *sx, wl_fixed_t *sy)
4223{
4224 switch (border->blocking_dir) {
4225 case MOTION_DIRECTION_POSITIVE_X:
4226 case MOTION_DIRECTION_NEGATIVE_X:
4227 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_X)
4228 *sx = wl_fixed_from_double(border->line.a.x) - 1;
4229 else
4230 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4231 if (*sy < wl_fixed_from_double(border->line.a.y))
4232 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4233 else if (*sy > wl_fixed_from_double(border->line.b.y))
4234 *sy = wl_fixed_from_double(border->line.b.y) - 1;
4235 break;
4236 case MOTION_DIRECTION_POSITIVE_Y:
4237 case MOTION_DIRECTION_NEGATIVE_Y:
4238 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_Y)
4239 *sy = wl_fixed_from_double(border->line.a.y) - 1;
4240 else
4241 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4242 if (*sx < wl_fixed_from_double(border->line.a.x))
4243 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4244 else if (*sx > wl_fixed_from_double(border->line.b.x))
4245 *sx = wl_fixed_from_double(border->line.b.x) - 1;
4246 break;
4247 }
Jonas Ådahld3414f22016-07-22 17:56:31 +08004248}
4249
4250static void
4251maybe_warp_confined_pointer(struct weston_pointer_constraint *constraint)
4252{
4253 wl_fixed_t x;
4254 wl_fixed_t y;
4255 wl_fixed_t sx;
4256 wl_fixed_t sy;
4257
4258 weston_view_from_global_fixed(constraint->view,
4259 constraint->pointer->x,
4260 constraint->pointer->y,
4261 &sx,
4262 &sy);
4263
4264 if (!is_within_constraint_region(constraint, sx, sy)) {
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004265 double xf = wl_fixed_to_double(sx);
4266 double yf = wl_fixed_to_double(sy);
4267 pixman_region32_t confine_region;
4268 struct wl_array borders;
4269 struct border *border;
4270 double closest_distance_2 = DBL_MAX;
4271 struct border *closest_border = NULL;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004272
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004273 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004274
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004275 pixman_region32_init(&confine_region);
4276 pixman_region32_intersect(&confine_region,
4277 &constraint->view->surface->input,
4278 &constraint->region);
4279 region_to_outline(&confine_region, &borders);
4280 pixman_region32_fini(&confine_region);
4281
4282 wl_array_for_each(border, &borders) {
4283 double distance_2;
4284
4285 distance_2 = point_to_border_distance_2(border, xf, yf);
4286 if (distance_2 < closest_distance_2) {
4287 closest_border = border;
4288 closest_distance_2 = distance_2;
4289 }
4290 }
4291 assert(closest_border);
4292
4293 warp_to_behind_border(closest_border, &sx, &sy);
4294
4295 wl_array_release(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004296
4297 weston_view_to_global_fixed(constraint->view, sx, sy, &x, &y);
4298 weston_pointer_move_to(constraint->pointer, x, y);
4299 }
4300}
4301
4302static void
4303confined_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02004304 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004305 struct weston_pointer_motion_event *event)
4306{
4307 struct weston_pointer_constraint *constraint =
4308 container_of(grab, struct weston_pointer_constraint, grab);
4309 struct weston_pointer *pointer = grab->pointer;
4310 struct weston_surface *surface;
4311 wl_fixed_t x, y;
4312 wl_fixed_t old_sx = pointer->sx;
4313 wl_fixed_t old_sy = pointer->sy;
4314 pixman_region32_t confine_region;
4315
4316 assert(pointer->focus);
4317 assert(pointer->focus->surface == constraint->surface);
4318
4319 surface = pointer->focus->surface;
4320
4321 pixman_region32_init(&confine_region);
4322 pixman_region32_intersect(&confine_region,
4323 &surface->input,
4324 &constraint->region);
4325 weston_pointer_clamp_event_to_region(pointer, event,
4326 &confine_region, &x, &y);
4327 weston_pointer_move_to(pointer, x, y);
4328 pixman_region32_fini(&confine_region);
4329
4330 weston_view_from_global_fixed(pointer->focus, x, y,
4331 &pointer->sx, &pointer->sy);
4332
4333 if (old_sx != pointer->sx || old_sy != pointer->sy) {
Quentin Glidiccde13452016-08-12 10:41:32 +02004334 pointer_send_motion(pointer, time,
4335 pointer->sx, pointer->sy);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004336 }
4337
Quentin Glidiccde13452016-08-12 10:41:32 +02004338 pointer_send_relative_motion(pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004339}
4340
4341static void
4342confined_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02004343 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004344 uint32_t button,
4345 uint32_t state_w)
4346{
4347 weston_pointer_send_button(grab->pointer, time, button, state_w);
4348}
4349
4350static void
4351confined_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02004352 const struct timespec *time,
Jonas Ådahld3414f22016-07-22 17:56:31 +08004353 struct weston_pointer_axis_event *event)
4354{
4355 weston_pointer_send_axis(grab->pointer, time, event);
4356}
4357
4358static void
4359confined_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
4360 uint32_t source)
4361{
4362 weston_pointer_send_axis_source(grab->pointer, source);
4363}
4364
4365static void
4366confined_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
4367{
4368 weston_pointer_send_frame(grab->pointer);
4369}
4370
4371static void
4372confined_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
4373{
4374 struct weston_pointer_constraint *constraint =
4375 container_of(grab, struct weston_pointer_constraint, grab);
4376
4377 disable_pointer_constraint(constraint);
4378
4379 /* If this is a persistent constraint, re-add the surface destroy signal
4380 * listener only if we are currently not destroying the surface. */
4381 switch (constraint->lifetime) {
4382 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT:
4383 if (constraint->surface->resource)
4384 wl_signal_add(&constraint->surface->destroy_signal,
4385 &constraint->surface_destroy_listener);
4386 break;
4387 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT:
4388 break;
4389 }
4390}
4391
4392static const struct weston_pointer_grab_interface
4393 confined_pointer_grab_interface = {
4394 confined_pointer_grab_pointer_focus,
4395 confined_pointer_grab_pointer_motion,
4396 confined_pointer_grab_pointer_button,
4397 confined_pointer_grab_pointer_axis,
4398 confined_pointer_grab_pointer_axis_source,
4399 confined_pointer_grab_pointer_frame,
4400 confined_pointer_grab_pointer_cancel,
4401};
4402
4403static void
4404confined_pointer_destroy(struct wl_client *client,
4405 struct wl_resource *resource)
4406{
4407 wl_resource_destroy(resource);
4408}
4409
4410static void
4411confined_pointer_set_region(struct wl_client *client,
4412 struct wl_resource *resource,
4413 struct wl_resource *region_resource)
4414{
4415 struct weston_pointer_constraint *constraint =
4416 wl_resource_get_user_data(resource);
4417 struct weston_region *region = region_resource ?
4418 wl_resource_get_user_data(region_resource) : NULL;
4419
4420 if (!constraint)
4421 return;
4422
4423 if (region) {
4424 pixman_region32_copy(&constraint->region_pending,
4425 &region->region);
4426 } else {
4427 pixman_region32_fini(&constraint->region_pending);
4428 region_init_infinite(&constraint->region_pending);
4429 }
4430 constraint->region_is_pending = true;
4431}
4432
4433static const struct zwp_confined_pointer_v1_interface confined_pointer_interface = {
4434 confined_pointer_destroy,
4435 confined_pointer_set_region,
4436};
4437
4438static void
4439pointer_constraints_confine_pointer(struct wl_client *client,
4440 struct wl_resource *resource,
4441 uint32_t id,
4442 struct wl_resource *surface_resource,
4443 struct wl_resource *pointer_resource,
4444 struct wl_resource *region_resource,
4445 uint32_t lifetime)
4446{
4447 struct weston_surface *surface =
4448 wl_resource_get_user_data(surface_resource);
4449 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
4450 struct weston_region *region = region_resource ?
4451 wl_resource_get_user_data(region_resource) : NULL;
4452
4453 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
4454 &zwp_confined_pointer_v1_interface,
4455 &confined_pointer_interface,
4456 &confined_pointer_grab_interface);
4457}
4458
4459static const struct zwp_pointer_constraints_v1_interface pointer_constraints_interface = {
4460 pointer_constraints_destroy,
4461 pointer_constraints_lock_pointer,
4462 pointer_constraints_confine_pointer,
4463};
4464
4465static void
4466bind_pointer_constraints(struct wl_client *client, void *data,
4467 uint32_t version, uint32_t id)
4468{
4469 struct wl_resource *resource;
4470
4471 resource = wl_resource_create(client,
4472 &zwp_pointer_constraints_v1_interface,
4473 1, id);
4474
4475 wl_resource_set_implementation(resource, &pointer_constraints_interface,
4476 NULL, NULL);
4477}
4478
Jonas Ådahl30d61d82014-10-22 21:21:17 +02004479int
4480weston_input_init(struct weston_compositor *compositor)
4481{
4482 if (!wl_global_create(compositor->wl_display,
4483 &zwp_relative_pointer_manager_v1_interface, 1,
4484 compositor, bind_relative_pointer_manager))
4485 return -1;
4486
Jonas Ådahld3414f22016-07-22 17:56:31 +08004487 if (!wl_global_create(compositor->wl_display,
4488 &zwp_pointer_constraints_v1_interface, 1,
4489 NULL, bind_pointer_constraints))
4490 return -1;
4491
Jonas Ådahl30d61d82014-10-22 21:21:17 +02004492 return 0;
4493}