blob: 4fedc558ce7aca8a844ab89fe17ca6ad9aec6154 [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"
Kristian Høgsberg2158a882013-04-18 15:07:39 -040041#include "compositor.h"
Daniel Stone7dbb0e12016-11-24 15:30:41 +000042#include "relative-pointer-unstable-v1-server-protocol.h"
43#include "pointer-constraints-unstable-v1-server-protocol.h"
Jonas Ådahld3414f22016-07-22 17:56:31 +080044
45enum pointer_constraint_type {
46 POINTER_CONSTRAINT_TYPE_LOCK,
47 POINTER_CONSTRAINT_TYPE_CONFINE,
48};
49
Jonas Ådahld0be2bb2015-04-30 17:56:37 +080050enum motion_direction {
51 MOTION_DIRECTION_POSITIVE_X = 1 << 0,
52 MOTION_DIRECTION_NEGATIVE_X = 1 << 1,
53 MOTION_DIRECTION_POSITIVE_Y = 1 << 2,
54 MOTION_DIRECTION_NEGATIVE_Y = 1 << 3,
55};
56
57struct vec2d {
58 double x, y;
59};
60
61struct line {
62 struct vec2d a;
63 struct vec2d b;
64};
65
66struct border {
67 struct line line;
68 enum motion_direction blocking_dir;
69};
70
Jonas Ådahld3414f22016-07-22 17:56:31 +080071static void
72maybe_warp_confined_pointer(struct weston_pointer_constraint *constraint);
Kristian Høgsberg2158a882013-04-18 15:07:39 -040073
Kristian Høgsbergb5e26102013-04-18 15:40:10 -040074static void
75empty_region(pixman_region32_t *region)
76{
77 pixman_region32_fini(region);
78 pixman_region32_init(region);
79}
80
Jonas Ådahld3414f22016-07-22 17:56:31 +080081static void
82region_init_infinite(pixman_region32_t *region)
83{
84 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
85 UINT32_MAX, UINT32_MAX);
86}
87
Jonas Ådahl2cbf2932015-07-22 12:05:38 +080088static struct weston_pointer_client *
89weston_pointer_client_create(struct wl_client *client)
90{
91 struct weston_pointer_client *pointer_client;
92
93 pointer_client = zalloc(sizeof *pointer_client);
94 if (!pointer_client)
95 return NULL;
96
97 pointer_client->client = client;
98 wl_list_init(&pointer_client->pointer_resources);
Jonas Ådahl30d61d82014-10-22 21:21:17 +020099 wl_list_init(&pointer_client->relative_pointer_resources);
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800100
101 return pointer_client;
102}
103
104static void
105weston_pointer_client_destroy(struct weston_pointer_client *pointer_client)
106{
107 free(pointer_client);
108}
109
110static bool
111weston_pointer_client_is_empty(struct weston_pointer_client *pointer_client)
112{
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200113 return (wl_list_empty(&pointer_client->pointer_resources) &&
114 wl_list_empty(&pointer_client->relative_pointer_resources));
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800115}
116
117static struct weston_pointer_client *
118weston_pointer_get_pointer_client(struct weston_pointer *pointer,
119 struct wl_client *client)
120{
121 struct weston_pointer_client *pointer_client;
122
123 wl_list_for_each(pointer_client, &pointer->pointer_clients, link) {
124 if (pointer_client->client == client)
125 return pointer_client;
126 }
127
128 return NULL;
129}
130
131static struct weston_pointer_client *
132weston_pointer_ensure_pointer_client(struct weston_pointer *pointer,
133 struct wl_client *client)
134{
135 struct weston_pointer_client *pointer_client;
136
137 pointer_client = weston_pointer_get_pointer_client(pointer, client);
138 if (pointer_client)
139 return pointer_client;
140
141 pointer_client = weston_pointer_client_create(client);
142 wl_list_insert(&pointer->pointer_clients, &pointer_client->link);
143
144 if (pointer->focus &&
145 pointer->focus->surface->resource &&
146 wl_resource_get_client(pointer->focus->surface->resource) == client) {
147 pointer->focus_client = pointer_client;
148 }
149
150 return pointer_client;
151}
152
153static void
154weston_pointer_cleanup_pointer_client(struct weston_pointer *pointer,
155 struct weston_pointer_client *pointer_client)
156{
157 if (weston_pointer_client_is_empty(pointer_client)) {
158 if (pointer->focus_client == pointer_client)
159 pointer->focus_client = NULL;
160 wl_list_remove(&pointer_client->link);
161 weston_pointer_client_destroy(pointer_client);
162 }
163}
164
165static void
166unbind_pointer_client_resource(struct wl_resource *resource)
167{
168 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
169 struct wl_client *client = wl_resource_get_client(resource);
170 struct weston_pointer_client *pointer_client;
171
172 pointer_client = weston_pointer_get_pointer_client(pointer, client);
173 assert(pointer_client);
174
175 wl_list_remove(wl_resource_get_link(resource));
176 weston_pointer_cleanup_pointer_client(pointer, pointer_client);
177}
178
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400179static void unbind_resource(struct wl_resource *resource)
180{
Jason Ekstrand44a38632013-06-14 10:08:00 -0500181 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400182}
183
Jonas Ådahl3042ffe2013-10-17 23:04:08 +0200184WL_EXPORT void
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200185weston_pointer_motion_to_abs(struct weston_pointer *pointer,
186 struct weston_pointer_motion_event *event,
187 wl_fixed_t *x, wl_fixed_t *y)
188{
189 if (event->mask & WESTON_POINTER_MOTION_ABS) {
190 *x = wl_fixed_from_double(event->x);
191 *y = wl_fixed_from_double(event->y);
192 } else if (event->mask & WESTON_POINTER_MOTION_REL) {
193 *x = pointer->x + wl_fixed_from_double(event->dx);
194 *y = pointer->y + wl_fixed_from_double(event->dy);
195 } else {
196 assert(!"invalid motion event");
197 *x = *y = 0;
198 }
199}
200
201static bool
202weston_pointer_motion_to_rel(struct weston_pointer *pointer,
203 struct weston_pointer_motion_event *event,
204 double *dx, double *dy,
205 double *dx_unaccel, double *dy_unaccel)
206{
207 if (event->mask & WESTON_POINTER_MOTION_REL &&
208 event->mask & WESTON_POINTER_MOTION_REL_UNACCEL) {
209 *dx = event->dx;
210 *dy = event->dy;
211 *dx_unaccel = event->dx_unaccel;
212 *dy_unaccel = event->dy_unaccel;
213 return true;
214 } else if (event->mask & WESTON_POINTER_MOTION_REL) {
215 *dx_unaccel = *dx = event->dx;
216 *dy_unaccel = *dy = event->dy;
217 return true;
218 } else if (event->mask & WESTON_POINTER_MOTION_REL_UNACCEL) {
219 *dx_unaccel = *dx = event->dx_unaccel;
220 *dy_unaccel = *dy = event->dy_unaccel;
221 return true;
222 } else {
223 return false;
224 }
225}
226
227WL_EXPORT void
Kristian Høgsberga71e8b22013-05-06 21:51:21 -0400228weston_seat_repick(struct weston_seat *seat)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400229{
Derek Foreman1281a362015-07-31 16:55:32 -0500230 const struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400231
Derek Foreman1b786ee2015-06-03 15:53:23 -0500232 if (!pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400233 return;
234
Derek Foreman1b786ee2015-06-03 15:53:23 -0500235 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -0400236}
237
238static void
239weston_compositor_idle_inhibit(struct weston_compositor *compositor)
240{
241 weston_compositor_wake(compositor);
242 compositor->idle_inhibit++;
243}
244
245static void
246weston_compositor_idle_release(struct weston_compositor *compositor)
247{
248 compositor->idle_inhibit--;
249 weston_compositor_wake(compositor);
250}
251
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400252static void
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100253pointer_focus_view_destroyed(struct wl_listener *listener, void *data)
254{
255 struct weston_pointer *pointer =
256 container_of(listener, struct weston_pointer,
257 focus_view_listener);
258
Derek Foremanf9318d12015-05-11 15:40:11 -0500259 weston_pointer_clear_focus(pointer);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100260}
261
262static void
263pointer_focus_resource_destroyed(struct wl_listener *listener, void *data)
264{
265 struct weston_pointer *pointer =
266 container_of(listener, struct weston_pointer,
267 focus_resource_listener);
268
Derek Foremanf9318d12015-05-11 15:40:11 -0500269 weston_pointer_clear_focus(pointer);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100270}
271
272static void
273keyboard_focus_resource_destroyed(struct wl_listener *listener, void *data)
274{
275 struct weston_keyboard *keyboard =
276 container_of(listener, struct weston_keyboard,
277 focus_resource_listener);
278
279 weston_keyboard_set_focus(keyboard, NULL);
280}
281
282static void
283touch_focus_view_destroyed(struct wl_listener *listener, void *data)
284{
285 struct weston_touch *touch =
286 container_of(listener, struct weston_touch,
287 focus_view_listener);
288
Derek Foreman4c93c082015-04-30 16:45:41 -0500289 weston_touch_set_focus(touch, NULL);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100290}
291
292static void
293touch_focus_resource_destroyed(struct wl_listener *listener, void *data)
294{
295 struct weston_touch *touch =
296 container_of(listener, struct weston_touch,
297 focus_resource_listener);
298
Derek Foreman4c93c082015-04-30 16:45:41 -0500299 weston_touch_set_focus(touch, NULL);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +0100300}
301
302static void
Neil Roberts96d790e2013-09-19 17:32:00 +0100303move_resources(struct wl_list *destination, struct wl_list *source)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400304{
Neil Roberts96d790e2013-09-19 17:32:00 +0100305 wl_list_insert_list(destination, source);
306 wl_list_init(source);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400307}
308
309static void
Neil Roberts96d790e2013-09-19 17:32:00 +0100310move_resources_for_client(struct wl_list *destination,
311 struct wl_list *source,
312 struct wl_client *client)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400313{
Neil Roberts96d790e2013-09-19 17:32:00 +0100314 struct wl_resource *resource, *tmp;
315 wl_resource_for_each_safe(resource, tmp, source) {
316 if (wl_resource_get_client(resource) == client) {
317 wl_list_remove(wl_resource_get_link(resource));
318 wl_list_insert(destination,
319 wl_resource_get_link(resource));
320 }
321 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400322}
323
324static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700325default_grab_pointer_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400326{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400327 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500328 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400329 wl_fixed_t sx, sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400330
331 if (pointer->button_count > 0)
332 return;
333
Jason Ekstranda7af7042013-10-12 22:38:11 -0500334 view = weston_compositor_pick_view(pointer->seat->compositor,
335 pointer->x, pointer->y,
336 &sx, &sy);
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400337
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -0800338 if (pointer->focus != view || pointer->sx != sx || pointer->sy != sy)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500339 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400340}
341
342static void
Quentin Glidiccde13452016-08-12 10:41:32 +0200343pointer_send_relative_motion(struct weston_pointer *pointer,
344 uint32_t time,
345 struct weston_pointer_motion_event *event)
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200346{
347 uint64_t time_usec;
348 double dx, dy, dx_unaccel, dy_unaccel;
349 wl_fixed_t dxf, dyf, dxf_unaccel, dyf_unaccel;
350 struct wl_list *resource_list;
351 struct wl_resource *resource;
352
353 if (!pointer->focus_client)
354 return;
355
356 if (!weston_pointer_motion_to_rel(pointer, event,
357 &dx, &dy,
358 &dx_unaccel, &dy_unaccel))
359 return;
360
361 resource_list = &pointer->focus_client->relative_pointer_resources;
362 time_usec = event->time_usec;
363 if (time_usec == 0)
364 time_usec = time * 1000ULL;
365
366 dxf = wl_fixed_from_double(dx);
367 dyf = wl_fixed_from_double(dy);
368 dxf_unaccel = wl_fixed_from_double(dx_unaccel);
369 dyf_unaccel = wl_fixed_from_double(dy_unaccel);
370
371 wl_resource_for_each(resource, resource_list) {
372 zwp_relative_pointer_v1_send_relative_motion(
373 resource,
374 (uint32_t) (time_usec >> 32),
375 (uint32_t) time_usec,
376 dxf, dyf,
377 dxf_unaccel, dyf_unaccel);
378 }
379}
380
381static void
Quentin Glidiccde13452016-08-12 10:41:32 +0200382pointer_send_motion(struct weston_pointer *pointer, uint32_t time,
383 wl_fixed_t sx, wl_fixed_t sy)
Jonas Ådahlf44942e2016-07-22 17:54:55 +0800384{
385 struct wl_list *resource_list;
386 struct wl_resource *resource;
387
388 if (!pointer->focus_client)
389 return;
390
391 resource_list = &pointer->focus_client->pointer_resources;
392 wl_resource_for_each(resource, resource_list)
393 wl_pointer_send_motion(resource, time, sx, sy);
394}
395
Quentin Glidiccde13452016-08-12 10:41:32 +0200396WL_EXPORT void
397weston_pointer_send_motion(struct weston_pointer *pointer, uint32_t time,
398 struct weston_pointer_motion_event *event)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400399{
Jonas Ådahld2510102014-10-05 21:39:14 +0200400 wl_fixed_t x, y;
Jonas Ådahl8283c342015-04-24 15:26:17 +0800401 wl_fixed_t old_sx = pointer->sx;
402 wl_fixed_t old_sy = pointer->sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400403
Jonas Ådahld2510102014-10-05 21:39:14 +0200404 if (pointer->focus) {
405 weston_pointer_motion_to_abs(pointer, event, &x, &y);
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -0800406 weston_view_from_global_fixed(pointer->focus, x, y,
407 &pointer->sx, &pointer->sy);
Jonas Ådahld2510102014-10-05 21:39:14 +0200408 }
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -0800409
Jonas Ådahld2510102014-10-05 21:39:14 +0200410 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +0100411
Jonas Ådahlf44942e2016-07-22 17:54:55 +0800412 if (old_sx != pointer->sx || old_sy != pointer->sy) {
Quentin Glidiccde13452016-08-12 10:41:32 +0200413 pointer_send_motion(pointer, time,
414 pointer->sx, pointer->sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -0400415 }
Jonas Ådahl30d61d82014-10-22 21:21:17 +0200416
Quentin Glidiccde13452016-08-12 10:41:32 +0200417 pointer_send_relative_motion(pointer, time, event);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400418}
419
420static void
Quentin Glidiccde13452016-08-12 10:41:32 +0200421default_grab_pointer_motion(struct weston_pointer_grab *grab, uint32_t time,
422 struct weston_pointer_motion_event *event)
423{
424 weston_pointer_send_motion(grab->pointer, time, event);
425}
426
427/** Check if the pointer has focused resources.
428 *
429 * \param pointer The pointer to check for focused resources.
430 * \return Whether or not this pointer has focused resources
431 */
432WL_EXPORT bool
433weston_pointer_has_focus_resource(struct weston_pointer *pointer)
434{
435 if (!pointer->focus_client)
436 return false;
437
438 if (wl_list_empty(&pointer->focus_client->pointer_resources))
439 return false;
440
441 return true;
442}
443
444/** Send wl_pointer.button events to focused resources.
445 *
446 * \param pointer The pointer where the button events originates from.
447 * \param time The timestamp of the event
448 * \param button The button value of the event
449 * \param value The state enum value of the event
450 *
451 * For every resource that is currently in focus, send a wl_pointer.button event
452 * with the passed parameters. The focused resources are the wl_pointer
453 * resources of the client which currently has the surface with pointer focus.
454 */
455WL_EXPORT void
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800456weston_pointer_send_button(struct weston_pointer *pointer,
Quentin Glidiccde13452016-08-12 10:41:32 +0200457 uint32_t time, uint32_t button,
458 enum wl_pointer_button_state state)
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800459{
460 struct wl_display *display = pointer->seat->compositor->wl_display;
461 struct wl_list *resource_list;
462 struct wl_resource *resource;
463 uint32_t serial;
464
Quentin Glidiccde13452016-08-12 10:41:32 +0200465 if (!weston_pointer_has_focus_resource(pointer))
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800466 return;
467
468 resource_list = &pointer->focus_client->pointer_resources;
Quentin Glidiccde13452016-08-12 10:41:32 +0200469 serial = wl_display_next_serial(display);
470 wl_resource_for_each(resource, resource_list)
471 wl_pointer_send_button(resource, serial, time, button, state);
Jonas Ådahlc02ac112016-07-22 17:55:43 +0800472}
473
474static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700475default_grab_pointer_button(struct weston_pointer_grab *grab,
Quentin Glidiccde13452016-08-12 10:41:32 +0200476 uint32_t time, uint32_t button,
477 enum wl_pointer_button_state state)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400478{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400479 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400480 struct weston_compositor *compositor = pointer->seat->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500481 struct weston_view *view;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400482 wl_fixed_t sx, sy;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400483
Quentin Glidiccde13452016-08-12 10:41:32 +0200484 weston_pointer_send_button(pointer, time, button, state);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400485
486 if (pointer->button_count == 0 &&
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400487 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500488 view = weston_compositor_pick_view(compositor,
489 pointer->x, pointer->y,
490 &sx, &sy);
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400491
Jason Ekstranda7af7042013-10-12 22:38:11 -0500492 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsberge122b7b2013-05-08 16:47:00 -0400493 }
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400494}
495
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200496/** Send wl_pointer.axis events to focused resources.
497 *
498 * \param pointer The pointer where the axis events originates from.
499 * \param time The timestamp of the event
500 * \param axis The axis enum value of the event
501 * \param value The axis value of the event
502 *
503 * For every resource that is currently in focus, send a wl_pointer.axis event
504 * with the passed parameters. The focused resources are the wl_pointer
505 * resources of the client which currently has the surface with pointer focus.
506 */
507WL_EXPORT void
508weston_pointer_send_axis(struct weston_pointer *pointer,
Peter Hutterer89b6a492016-01-18 15:58:17 +1000509 uint32_t time,
510 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200511{
512 struct wl_resource *resource;
513 struct wl_list *resource_list;
514
Quentin Glidiccde13452016-08-12 10:41:32 +0200515 if (!weston_pointer_has_focus_resource(pointer))
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800516 return;
517
518 resource_list = &pointer->focus_client->pointer_resources;
Peter Hutterer87743e92016-01-18 16:38:22 +1000519 wl_resource_for_each(resource, resource_list) {
520 if (event->has_discrete &&
521 wl_resource_get_version(resource) >=
522 WL_POINTER_AXIS_DISCRETE_SINCE_VERSION)
523 wl_pointer_send_axis_discrete(resource, event->axis,
524 event->discrete);
525
526 if (event->value)
527 wl_pointer_send_axis(resource, time,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200528 event->axis,
529 wl_fixed_from_double(event->value));
Peter Hutterer87743e92016-01-18 16:38:22 +1000530 else if (wl_resource_get_version(resource) >=
531 WL_POINTER_AXIS_STOP_SINCE_VERSION)
532 wl_pointer_send_axis_stop(resource, time,
533 event->axis);
534 }
535}
536
Quentin Glidiccde13452016-08-12 10:41:32 +0200537/** Send wl_pointer.axis_source events to focused resources.
538 *
539 * \param pointer The pointer where the axis_source events originates from.
540 * \param source The axis_source enum value of the event
541 *
542 * For every resource that is currently in focus, send a wl_pointer.axis_source
543 * event with the passed parameter. The focused resources are the wl_pointer
544 * resources of the client which currently has the surface with pointer focus.
545 */
Peter Hutterer87743e92016-01-18 16:38:22 +1000546WL_EXPORT void
Quentin Glidiccde13452016-08-12 10:41:32 +0200547weston_pointer_send_axis_source(struct weston_pointer *pointer,
548 enum wl_pointer_axis_source source)
Peter Hutterer87743e92016-01-18 16:38:22 +1000549{
550 struct wl_resource *resource;
551 struct wl_list *resource_list;
552
Quentin Glidiccde13452016-08-12 10:41:32 +0200553 if (!weston_pointer_has_focus_resource(pointer))
Jonas Ådahled6014a2016-04-21 10:21:48 +0800554 return;
555
Peter Hutterer87743e92016-01-18 16:38:22 +1000556 resource_list = &pointer->focus_client->pointer_resources;
557 wl_resource_for_each(resource, resource_list) {
558 if (wl_resource_get_version(resource) >=
559 WL_POINTER_AXIS_SOURCE_SINCE_VERSION) {
560 wl_pointer_send_axis_source(resource, source);
561 }
562 }
563}
564
565static void
566pointer_send_frame(struct wl_resource *resource)
567{
568 if (wl_resource_get_version(resource) >=
569 WL_POINTER_FRAME_SINCE_VERSION) {
570 wl_pointer_send_frame(resource);
571 }
572}
573
Quentin Glidiccde13452016-08-12 10:41:32 +0200574/** Send wl_pointer.frame events to focused resources.
575 *
576 * \param pointer The pointer where the frame events originates from.
577 *
578 * For every resource that is currently in focus, send a wl_pointer.frame event.
579 * The focused resources are the wl_pointer resources of the client which
580 * currently has the surface with pointer focus.
581 */
Peter Hutterer87743e92016-01-18 16:38:22 +1000582WL_EXPORT void
583weston_pointer_send_frame(struct weston_pointer *pointer)
584{
585 struct wl_resource *resource;
586 struct wl_list *resource_list;
587
Quentin Glidiccde13452016-08-12 10:41:32 +0200588 if (!weston_pointer_has_focus_resource(pointer))
Derek Foreman8efa31b2016-01-29 10:29:46 -0600589 return;
590
Peter Hutterer87743e92016-01-18 16:38:22 +1000591 resource_list = &pointer->focus_client->pointer_resources;
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200592 wl_resource_for_each(resource, resource_list)
Peter Hutterer87743e92016-01-18 16:38:22 +1000593 pointer_send_frame(resource);
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200594}
595
596static void
597default_grab_pointer_axis(struct weston_pointer_grab *grab,
Peter Hutterer89b6a492016-01-18 15:58:17 +1000598 uint32_t time,
599 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200600{
Peter Hutterer89b6a492016-01-18 15:58:17 +1000601 weston_pointer_send_axis(grab->pointer, time, event);
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200602}
603
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200604static void
Peter Hutterer87743e92016-01-18 16:38:22 +1000605default_grab_pointer_axis_source(struct weston_pointer_grab *grab,
Quentin Glidiccde13452016-08-12 10:41:32 +0200606 enum wl_pointer_axis_source source)
Peter Hutterer87743e92016-01-18 16:38:22 +1000607{
608 weston_pointer_send_axis_source(grab->pointer, source);
609}
610
611static void
612default_grab_pointer_frame(struct weston_pointer_grab *grab)
613{
614 weston_pointer_send_frame(grab->pointer);
615}
616
617static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200618default_grab_pointer_cancel(struct weston_pointer_grab *grab)
619{
620}
621
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400622static const struct weston_pointer_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400623 default_pointer_grab_interface = {
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700624 default_grab_pointer_focus,
625 default_grab_pointer_motion,
626 default_grab_pointer_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200627 default_grab_pointer_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +1000628 default_grab_pointer_axis_source,
629 default_grab_pointer_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200630 default_grab_pointer_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400631};
632
Quentin Glidiccde13452016-08-12 10:41:32 +0200633/** Check if the touch has focused resources.
634 *
635 * \param touch The touch to check for focused resources.
636 * \return Whether or not this touch has focused resources
637 */
638WL_EXPORT bool
639weston_touch_has_focus_resource(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400640{
Quentin Glidiccde13452016-08-12 10:41:32 +0200641 if (!touch->focus)
642 return false;
643
644 if (wl_list_empty(&touch->focus_resource_list))
645 return false;
646
647 return true;
648}
649
650/** Send wl_touch.down events to focused resources.
651 *
652 * \param touch The touch where the down events originates from.
653 * \param time The timestamp of the event
654 * \param touch_id The touch_id value of the event
655 * \param x The x value of the event
656 * \param y The y value of the event
657 *
658 * For every resource that is currently in focus, send a wl_touch.down event
659 * with the passed parameters. The focused resources are the wl_touch
660 * resources of the client which currently has the surface with touch focus.
661 */
662WL_EXPORT void
663weston_touch_send_down(struct weston_touch *touch, uint32_t time,
664 int touch_id, wl_fixed_t x, wl_fixed_t y)
665{
Rob Bradford880ebc72013-07-22 17:31:38 +0100666 struct wl_display *display = touch->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400667 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100668 struct wl_resource *resource;
669 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300670 wl_fixed_t sx, sy;
671
Quentin Glidiccde13452016-08-12 10:41:32 +0200672 if (!weston_touch_has_focus_resource(touch))
Bryce Harrington2c40d1d2016-02-02 10:18:48 -0800673 return;
674
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300675 weston_view_from_global_fixed(touch->focus, x, y, &sx, &sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400676
Neil Roberts96d790e2013-09-19 17:32:00 +0100677 resource_list = &touch->focus_resource_list;
Quentin Glidiccde13452016-08-12 10:41:32 +0200678 serial = wl_display_next_serial(display);
679 wl_resource_for_each(resource, resource_list)
680 wl_touch_send_down(resource, serial, time,
681 touch->focus->surface->resource,
682 touch_id, sx, sy);
683}
Neil Roberts96d790e2013-09-19 17:32:00 +0100684
Quentin Glidiccde13452016-08-12 10:41:32 +0200685static void
686default_grab_touch_down(struct weston_touch_grab *grab, uint32_t time,
687 int touch_id, wl_fixed_t x, wl_fixed_t y)
688{
689 weston_touch_send_down(grab->touch, time, touch_id, x, y);
690}
691
692/** Send wl_touch.up events to focused resources.
693 *
694 * \param touch The touch where the up events originates from.
695 * \param time The timestamp of the event
696 * \param touch_id The touch_id value of the event
697 *
698 * For every resource that is currently in focus, send a wl_touch.up event
699 * with the passed parameters. The focused resources are the wl_touch
700 * resources of the client which currently has the surface with touch focus.
701 */
702WL_EXPORT void
703weston_touch_send_up(struct weston_touch *touch, uint32_t time, int touch_id)
704{
705 struct wl_display *display = touch->seat->compositor->wl_display;
706 uint32_t serial;
707 struct wl_resource *resource;
708 struct wl_list *resource_list;
709
710 if (!weston_touch_has_focus_resource(touch))
711 return;
712
713 resource_list = &touch->focus_resource_list;
714 serial = wl_display_next_serial(display);
715 wl_resource_for_each(resource, resource_list)
716 wl_touch_send_up(resource, serial, time, touch_id);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400717}
718
Kristian Høgsberge329f362013-05-06 22:19:57 -0400719static void
720default_grab_touch_up(struct weston_touch_grab *grab,
721 uint32_t time, int touch_id)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400722{
Quentin Glidiccde13452016-08-12 10:41:32 +0200723 weston_touch_send_up(grab->touch, time, touch_id);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400724}
725
Quentin Glidiccde13452016-08-12 10:41:32 +0200726/** Send wl_touch.motion events to focused resources.
727 *
728 * \param touch The touch where the motion events originates from.
729 * \param time The timestamp of the event
730 * \param touch_id The touch_id value of the event
731 * \param x The x value of the event
732 * \param y The y value of the event
733 *
734 * For every resource that is currently in focus, send a wl_touch.motion event
735 * with the passed parameters. The focused resources are the wl_touch
736 * resources of the client which currently has the surface with touch focus.
737 */
738WL_EXPORT void
739weston_touch_send_motion(struct weston_touch *touch, uint32_t time,
740 int touch_id, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400741{
Neil Roberts96d790e2013-09-19 17:32:00 +0100742 struct wl_resource *resource;
743 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300744 wl_fixed_t sx, sy;
745
Quentin Glidiccde13452016-08-12 10:41:32 +0200746 if (!weston_touch_has_focus_resource(touch))
747 return;
748
Giulio Camuffo61ed7b62015-07-08 11:55:28 +0300749 weston_view_from_global_fixed(touch->focus, x, y, &sx, &sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400750
Neil Roberts96d790e2013-09-19 17:32:00 +0100751 resource_list = &touch->focus_resource_list;
Neil Roberts96d790e2013-09-19 17:32:00 +0100752 wl_resource_for_each(resource, resource_list) {
753 wl_touch_send_motion(resource, time,
Kristian Høgsberge329f362013-05-06 22:19:57 -0400754 touch_id, sx, sy);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400755 }
756}
757
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200758static void
Quentin Glidiccde13452016-08-12 10:41:32 +0200759default_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time,
760 int touch_id, wl_fixed_t x, wl_fixed_t y)
761{
762 weston_touch_send_motion(grab->touch, time, touch_id, x, y);
763}
764
765
766/** Send wl_touch.frame events to focused resources.
767 *
768 * \param touch The touch where the frame events originates from.
769 *
770 * For every resource that is currently in focus, send a wl_touch.frame event.
771 * The focused resources are the wl_touch resources of the client which
772 * currently has the surface with touch focus.
773 */
774WL_EXPORT void
775weston_touch_send_frame(struct weston_touch *touch)
Jonas Ådahl1679f232014-04-12 09:39:51 +0200776{
777 struct wl_resource *resource;
778
Quentin Glidiccde13452016-08-12 10:41:32 +0200779 if (!weston_touch_has_focus_resource(touch))
780 return;
781
782 wl_resource_for_each(resource, &touch->focus_resource_list)
Jonas Ådahl1679f232014-04-12 09:39:51 +0200783 wl_touch_send_frame(resource);
784}
785
786static void
Quentin Glidiccde13452016-08-12 10:41:32 +0200787default_grab_touch_frame(struct weston_touch_grab *grab)
788{
789 weston_touch_send_frame(grab->touch);
790}
791
792static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200793default_grab_touch_cancel(struct weston_touch_grab *grab)
794{
795}
796
Kristian Høgsberge329f362013-05-06 22:19:57 -0400797static const struct weston_touch_grab_interface default_touch_grab_interface = {
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400798 default_grab_touch_down,
799 default_grab_touch_up,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200800 default_grab_touch_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +0200801 default_grab_touch_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200802 default_grab_touch_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400803};
804
Quentin Glidiccde13452016-08-12 10:41:32 +0200805/** Check if the keyboard has focused resources.
806 *
807 * \param keyboard The keyboard to check for focused resources.
808 * \return Whether or not this keyboard has focused resources
809 */
810WL_EXPORT bool
811weston_keyboard_has_focus_resource(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400812{
Quentin Glidiccde13452016-08-12 10:41:32 +0200813 if (!keyboard->focus)
814 return false;
815
816 if (wl_list_empty(&keyboard->focus_resource_list))
817 return false;
818
819 return true;
820}
821
822/** Send wl_keyboard.key events to focused resources.
823 *
824 * \param keyboard The keyboard where the key events originates from.
825 * \param time The timestamp of the event
826 * \param key The key value of the event
827 * \param state The state enum value of the event
828 *
829 * For every resource that is currently in focus, send a wl_keyboard.key event
830 * with the passed parameters. The focused resources are the wl_keyboard
831 * resources of the client which currently has the surface with keyboard focus.
832 */
833WL_EXPORT void
834weston_keyboard_send_key(struct weston_keyboard *keyboard,
835 uint32_t time, uint32_t key,
836 enum wl_keyboard_key_state state)
837{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400838 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +0100839 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400840 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +0100841 struct wl_list *resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400842
Quentin Glidiccde13452016-08-12 10:41:32 +0200843 if (!weston_keyboard_has_focus_resource(keyboard))
844 return;
845
Neil Roberts96d790e2013-09-19 17:32:00 +0100846 resource_list = &keyboard->focus_resource_list;
Quentin Glidiccde13452016-08-12 10:41:32 +0200847 serial = wl_display_next_serial(display);
848 wl_resource_for_each(resource, resource_list)
849 wl_keyboard_send_key(resource, serial, time, key, state);
850};
851
852static void
853default_grab_keyboard_key(struct weston_keyboard_grab *grab,
854 uint32_t time, uint32_t key, uint32_t state)
855{
856 weston_keyboard_send_key(grab->keyboard, time, key, state);
Neil Roberts96d790e2013-09-19 17:32:00 +0100857}
858
859static void
860send_modifiers_to_resource(struct weston_keyboard *keyboard,
861 struct wl_resource *resource,
862 uint32_t serial)
863{
864 wl_keyboard_send_modifiers(resource,
865 serial,
866 keyboard->modifiers.mods_depressed,
867 keyboard->modifiers.mods_latched,
868 keyboard->modifiers.mods_locked,
869 keyboard->modifiers.group);
870}
871
872static void
873send_modifiers_to_client_in_list(struct wl_client *client,
874 struct wl_list *list,
875 uint32_t serial,
876 struct weston_keyboard *keyboard)
877{
878 struct wl_resource *resource;
879
880 wl_resource_for_each(resource, list) {
881 if (wl_resource_get_client(resource) == client)
882 send_modifiers_to_resource(keyboard,
883 resource,
884 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400885 }
886}
887
Jonas Ådahl2cbf2932015-07-22 12:05:38 +0800888static struct weston_pointer_client *
889find_pointer_client_for_surface(struct weston_pointer *pointer,
890 struct weston_surface *surface)
891{
892 struct wl_client *client;
893
894 if (!surface)
895 return NULL;
896
897 if (!surface->resource)
898 return NULL;
899
900 client = wl_resource_get_client(surface->resource);
901 return weston_pointer_get_pointer_client(pointer, client);
902}
903
904static struct weston_pointer_client *
905find_pointer_client_for_view(struct weston_pointer *pointer, struct weston_view *view)
906{
907 if (!view)
908 return NULL;
909
910 return find_pointer_client_for_surface(pointer, view->surface);
911}
912
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400913static struct wl_resource *
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400914find_resource_for_surface(struct wl_list *list, struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400915{
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400916 if (!surface)
917 return NULL;
918
Jason Ekstrand44a38632013-06-14 10:08:00 -0500919 if (!surface->resource)
920 return NULL;
Stefan Schmidtfda26522013-09-17 10:54:09 +0100921
Jason Ekstrand44a38632013-06-14 10:08:00 -0500922 return wl_resource_find_for_client(list, wl_resource_get_client(surface->resource));
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400923}
924
Quentin Glidiccde13452016-08-12 10:41:32 +0200925/** Send wl_keyboard.modifiers events to focused resources and pointer
926 * focused resources.
927 *
928 * \param keyboard The keyboard where the modifiers events originates from.
929 * \param serial The serial of the event
930 * \param mods_depressed The mods_depressed value of the event
931 * \param mods_latched The mods_latched value of the event
932 * \param mods_locked The mods_locked value of the event
933 * \param group The group value of the event
934 *
935 * For every resource that is currently in focus, send a wl_keyboard.modifiers
936 * event with the passed parameters. The focused resources are the wl_keyboard
937 * resources of the client which currently has the surface with keyboard focus.
938 * This also sends wl_keyboard.modifiers events to the wl_keyboard resources of
939 * the client having pointer focus (if different from the keyboard focus client).
940 */
941WL_EXPORT void
942weston_keyboard_send_modifiers(struct weston_keyboard *keyboard,
943 uint32_t serial, uint32_t mods_depressed,
944 uint32_t mods_latched,
945 uint32_t mods_locked, uint32_t group)
946{
947 struct weston_pointer *pointer =
948 weston_seat_get_pointer(keyboard->seat);
949
950 if (weston_keyboard_has_focus_resource(keyboard)) {
951 struct wl_list *resource_list;
952 struct wl_resource *resource;
953
954 resource_list = &keyboard->focus_resource_list;
955 wl_resource_for_each(resource, resource_list) {
956 wl_keyboard_send_modifiers(resource, serial,
957 mods_depressed, mods_latched,
958 mods_locked, group);
959 }
960 }
961
962 if (pointer && pointer->focus && pointer->focus->surface->resource &&
963 pointer->focus->surface != keyboard->focus) {
964 struct wl_client *pointer_client =
965 wl_resource_get_client(pointer->focus->surface->resource);
966
967 send_modifiers_to_client_in_list(pointer_client,
968 &keyboard->resource_list,
969 serial,
970 keyboard);
971 }
972}
973
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400974static void
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700975default_grab_keyboard_modifiers(struct weston_keyboard_grab *grab,
976 uint32_t serial, uint32_t mods_depressed,
977 uint32_t mods_latched,
978 uint32_t mods_locked, uint32_t group)
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400979{
Quentin Glidiccde13452016-08-12 10:41:32 +0200980 weston_keyboard_send_modifiers(grab->keyboard, serial, mods_depressed,
981 mods_latched, mods_locked, group);
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400982}
983
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200984static void
985default_grab_keyboard_cancel(struct weston_keyboard_grab *grab)
986{
987}
988
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400989static const struct weston_keyboard_grab_interface
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400990 default_keyboard_grab_interface = {
Kristian Høgsbergb27901c2013-10-28 15:32:02 -0700991 default_grab_keyboard_key,
992 default_grab_keyboard_modifiers,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200993 default_grab_keyboard_cancel,
Kristian Høgsberg2158a882013-04-18 15:07:39 -0400994};
995
Kristian Høgsberg195b8692013-05-08 15:02:05 -0400996static void
997pointer_unmap_sprite(struct weston_pointer *pointer)
998{
Pekka Paalanenc557ff72014-11-12 16:42:52 +0200999 struct weston_surface *surface = pointer->sprite->surface;
1000
1001 if (weston_surface_is_mapped(surface))
1002 weston_surface_unmap(surface);
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001003
1004 wl_list_remove(&pointer->sprite_destroy_listener.link);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02001005 surface->committed = NULL;
1006 surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03001007 weston_surface_set_label_func(surface, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001008 weston_view_destroy(pointer->sprite);
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001009 pointer->sprite = NULL;
1010}
1011
1012static void
1013pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
1014{
1015 struct weston_pointer *pointer =
1016 container_of(listener, struct weston_pointer,
1017 sprite_destroy_listener);
1018
1019 pointer->sprite = NULL;
1020}
1021
Jonas Ådahl3e12e632013-12-02 22:05:05 +01001022static void
1023weston_pointer_reset_state(struct weston_pointer *pointer)
1024{
1025 pointer->button_count = 0;
1026}
1027
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001028static void
1029weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data);
1030
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001031WL_EXPORT struct weston_pointer *
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001032weston_pointer_create(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001033{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001034 struct weston_pointer *pointer;
1035
Peter Huttererf3d62272013-08-08 11:57:05 +10001036 pointer = zalloc(sizeof *pointer);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001037 if (pointer == NULL)
1038 return NULL;
1039
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001040 wl_list_init(&pointer->pointer_clients);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001041 weston_pointer_set_default_grab(pointer,
1042 seat->compositor->default_pointer_grab);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001043 wl_list_init(&pointer->focus_resource_listener.link);
1044 pointer->focus_resource_listener.notify = pointer_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001045 pointer->default_grab.pointer = pointer;
1046 pointer->grab = &pointer->default_grab;
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001047 wl_signal_init(&pointer->motion_signal);
Emilio Pozuelo Monfortaa7a4762013-11-19 11:37:15 +01001048 wl_signal_init(&pointer->focus_signal);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001049 wl_list_init(&pointer->focus_view_listener.link);
Jonas Ådahl3eb4ddd2016-07-22 17:52:58 +08001050 wl_signal_init(&pointer->destroy_signal);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001051
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001052 pointer->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
1053
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001054 /* FIXME: Pick better co-ords. */
1055 pointer->x = wl_fixed_from_int(100);
1056 pointer->y = wl_fixed_from_int(100);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001057
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001058 pointer->output_destroy_listener.notify =
1059 weston_pointer_handle_output_destroy;
1060 wl_signal_add(&seat->compositor->output_destroyed_signal,
1061 &pointer->output_destroy_listener);
1062
Derek Foremanf9318d12015-05-11 15:40:11 -05001063 pointer->sx = wl_fixed_from_int(-1000000);
1064 pointer->sy = wl_fixed_from_int(-1000000);
1065
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001066 return pointer;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001067}
1068
1069WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001070weston_pointer_destroy(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001071{
Jonas Ådahl3eb4ddd2016-07-22 17:52:58 +08001072 wl_signal_emit(&pointer->destroy_signal, pointer);
1073
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001074 if (pointer->sprite)
1075 pointer_unmap_sprite(pointer);
1076
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001077 /* XXX: What about pointer->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +01001078
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001079 wl_list_remove(&pointer->focus_resource_listener.link);
1080 wl_list_remove(&pointer->focus_view_listener.link);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001081 wl_list_remove(&pointer->output_destroy_listener.link);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001082 free(pointer);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001083}
1084
Giulio Camuffocdb4d292013-11-14 23:42:53 +01001085void
1086weston_pointer_set_default_grab(struct weston_pointer *pointer,
1087 const struct weston_pointer_grab_interface *interface)
1088{
1089 if (interface)
1090 pointer->default_grab.interface = interface;
1091 else
1092 pointer->default_grab.interface =
1093 &default_pointer_grab_interface;
1094}
1095
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001096WL_EXPORT struct weston_keyboard *
1097weston_keyboard_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001098{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001099 struct weston_keyboard *keyboard;
1100
Peter Huttererf3d62272013-08-08 11:57:05 +10001101 keyboard = zalloc(sizeof *keyboard);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001102 if (keyboard == NULL)
Neil Roberts96d790e2013-09-19 17:32:00 +01001103 return NULL;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001104
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001105 wl_list_init(&keyboard->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +01001106 wl_list_init(&keyboard->focus_resource_list);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001107 wl_list_init(&keyboard->focus_resource_listener.link);
1108 keyboard->focus_resource_listener.notify = keyboard_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001109 wl_array_init(&keyboard->keys);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001110 keyboard->default_grab.interface = &default_keyboard_grab_interface;
1111 keyboard->default_grab.keyboard = keyboard;
1112 keyboard->grab = &keyboard->default_grab;
1113 wl_signal_init(&keyboard->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001114
1115 return keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001116}
1117
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001118static void
1119weston_xkb_info_destroy(struct weston_xkb_info *xkb_info);
1120
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001121WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001122weston_keyboard_destroy(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001123{
1124 /* XXX: What about keyboard->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +01001125
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001126#ifdef ENABLE_XKBCOMMON
1127 if (keyboard->seat->compositor->use_xkbcommon) {
Ran Benitac9c74152014-08-19 23:59:52 +03001128 xkb_state_unref(keyboard->xkb_state.state);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001129 if (keyboard->xkb_info)
1130 weston_xkb_info_destroy(keyboard->xkb_info);
Ran Benitac9c74152014-08-19 23:59:52 +03001131 xkb_keymap_unref(keyboard->pending_keymap);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001132 }
1133#endif
1134
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001135 wl_array_release(&keyboard->keys);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001136 wl_list_remove(&keyboard->focus_resource_listener.link);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001137 free(keyboard);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001138}
1139
Jonas Ådahlcbfa7f72013-12-02 22:05:04 +01001140static void
1141weston_touch_reset_state(struct weston_touch *touch)
1142{
1143 touch->num_tp = 0;
1144}
1145
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001146WL_EXPORT struct weston_touch *
1147weston_touch_create(void)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001148{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001149 struct weston_touch *touch;
1150
Peter Huttererf3d62272013-08-08 11:57:05 +10001151 touch = zalloc(sizeof *touch);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001152 if (touch == NULL)
1153 return NULL;
1154
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001155 wl_list_init(&touch->resource_list);
Neil Roberts96d790e2013-09-19 17:32:00 +01001156 wl_list_init(&touch->focus_resource_list);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001157 wl_list_init(&touch->focus_view_listener.link);
1158 touch->focus_view_listener.notify = touch_focus_view_destroyed;
1159 wl_list_init(&touch->focus_resource_listener.link);
1160 touch->focus_resource_listener.notify = touch_focus_resource_destroyed;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001161 touch->default_grab.interface = &default_touch_grab_interface;
1162 touch->default_grab.touch = touch;
1163 touch->grab = &touch->default_grab;
1164 wl_signal_init(&touch->focus_signal);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001165
1166 return touch;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001167}
1168
1169WL_EXPORT void
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001170weston_touch_destroy(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001171{
1172 /* XXX: What about touch->resource_list? */
Neil Roberts96d790e2013-09-19 17:32:00 +01001173
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001174 wl_list_remove(&touch->focus_view_listener.link);
1175 wl_list_remove(&touch->focus_resource_listener.link);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04001176 free(touch);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001177}
1178
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001179static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04001180seat_send_updated_caps(struct weston_seat *seat)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001181{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001182 enum wl_seat_capability caps = 0;
Rob Bradford6e737f52013-09-06 17:48:19 +01001183 struct wl_resource *resource;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001184
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001185 if (seat->pointer_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001186 caps |= WL_SEAT_CAPABILITY_POINTER;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001187 if (seat->keyboard_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001188 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02001189 if (seat->touch_device_count > 0)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001190 caps |= WL_SEAT_CAPABILITY_TOUCH;
1191
Rob Bradford6e737f52013-09-06 17:48:19 +01001192 wl_resource_for_each(resource, &seat->base_resource_list) {
1193 wl_seat_send_capabilities(resource, caps);
Jason Ekstrand44a38632013-06-14 10:08:00 -05001194 }
Jason Ekstranda4ab5422014-04-02 19:53:45 -05001195 wl_signal_emit(&seat->updated_caps_signal, seat);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001196}
1197
Derek Foremanf9318d12015-05-11 15:40:11 -05001198
1199/** Clear the pointer focus
1200 *
1201 * \param pointer the pointer to clear focus for.
1202 *
1203 * This can be used to unset pointer focus and set the co-ordinates to the
1204 * arbitrary values we use for the no focus case.
1205 *
1206 * There's no requirement to use this function. For example, passing the
1207 * results of a weston_compositor_pick_view() directly to
1208 * weston_pointer_set_focus() will do the right thing when no view is found.
1209 */
1210WL_EXPORT void
1211weston_pointer_clear_focus(struct weston_pointer *pointer)
1212{
1213 weston_pointer_set_focus(pointer, NULL,
1214 wl_fixed_from_int(-1000000),
1215 wl_fixed_from_int(-1000000));
1216}
1217
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001218WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001219weston_pointer_set_focus(struct weston_pointer *pointer,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001220 struct weston_view *view,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001221 wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001222{
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001223 struct weston_pointer_client *pointer_client;
Derek Foreman1281a362015-07-31 16:55:32 -05001224 struct weston_keyboard *kbd = weston_seat_get_keyboard(pointer->seat);
Neil Roberts96d790e2013-09-19 17:32:00 +01001225 struct wl_resource *resource;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001226 struct wl_resource *surface_resource;
Rob Bradford880ebc72013-07-22 17:31:38 +01001227 struct wl_display *display = pointer->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001228 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01001229 struct wl_list *focus_resource_list;
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001230 int refocus = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001231
1232 if ((!pointer->focus && view) ||
1233 (pointer->focus && !view) ||
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001234 (pointer->focus && pointer->focus->surface != view->surface) ||
1235 pointer->sx != sx || pointer->sy != sy)
1236 refocus = 1;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001237
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001238 if (pointer->focus_client && refocus) {
1239 focus_resource_list = &pointer->focus_client->pointer_resources;
1240 if (!wl_list_empty(focus_resource_list)) {
1241 serial = wl_display_next_serial(display);
1242 surface_resource = pointer->focus->surface->resource;
1243 wl_resource_for_each(resource, focus_resource_list) {
1244 wl_pointer_send_leave(resource, serial,
1245 surface_resource);
Peter Hutterer87743e92016-01-18 16:38:22 +10001246 pointer_send_frame(resource);
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001247 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001248 }
1249
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001250 pointer->focus_client = NULL;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001251 }
1252
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001253 pointer_client = find_pointer_client_for_view(pointer, view);
1254 if (pointer_client && refocus) {
1255 struct wl_client *surface_client = pointer_client->client;
Neil Roberts96d790e2013-09-19 17:32:00 +01001256
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001257 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001258
Jason Ekstranda7af7042013-10-12 22:38:11 -05001259 if (kbd && kbd->focus != view->surface)
Kristian Høgsbergcb406f12013-10-09 10:54:03 -07001260 send_modifiers_to_client_in_list(surface_client,
1261 &kbd->resource_list,
1262 serial,
1263 kbd);
1264
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001265 pointer->focus_client = pointer_client;
Neil Roberts96d790e2013-09-19 17:32:00 +01001266
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08001267 focus_resource_list = &pointer->focus_client->pointer_resources;
Neil Roberts96d790e2013-09-19 17:32:00 +01001268 wl_resource_for_each(resource, focus_resource_list) {
1269 wl_pointer_send_enter(resource,
1270 serial,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001271 view->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01001272 sx, sy);
Peter Hutterer87743e92016-01-18 16:38:22 +10001273 pointer_send_frame(resource);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001274 }
Neil Roberts96d790e2013-09-19 17:32:00 +01001275
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001276 pointer->focus_serial = serial;
1277 }
1278
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001279 wl_list_remove(&pointer->focus_view_listener.link);
1280 wl_list_init(&pointer->focus_view_listener.link);
1281 wl_list_remove(&pointer->focus_resource_listener.link);
1282 wl_list_init(&pointer->focus_resource_listener.link);
Emilio Pozuelo Monfortaa7a4762013-11-19 11:37:15 +01001283 if (view)
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001284 wl_signal_add(&view->destroy_signal, &pointer->focus_view_listener);
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001285 if (view && view->surface->resource)
1286 wl_resource_add_destroy_listener(view->surface->resource,
1287 &pointer->focus_resource_listener);
1288
1289 pointer->focus = view;
1290 pointer->focus_view_listener.notify = pointer_focus_view_destroyed;
Kristian Høgsbergdb1fccb2014-02-05 17:14:42 -08001291 pointer->sx = sx;
1292 pointer->sy = sy;
1293
Derek Foremanf9318d12015-05-11 15:40:11 -05001294 assert(view || sx == wl_fixed_from_int(-1000000));
1295 assert(view || sy == wl_fixed_from_int(-1000000));
1296
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001297 wl_signal_emit(&pointer->focus_signal, pointer);
1298}
1299
Neil Roberts96d790e2013-09-19 17:32:00 +01001300static void
1301send_enter_to_resource_list(struct wl_list *list,
1302 struct weston_keyboard *keyboard,
1303 struct weston_surface *surface,
1304 uint32_t serial)
1305{
1306 struct wl_resource *resource;
1307
1308 wl_resource_for_each(resource, list) {
1309 send_modifiers_to_resource(keyboard, resource, serial);
1310 wl_keyboard_send_enter(resource, serial,
1311 surface->resource,
1312 &keyboard->keys);
1313 }
1314}
1315
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001316WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001317weston_keyboard_set_focus(struct weston_keyboard *keyboard,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001318 struct weston_surface *surface)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001319{
1320 struct wl_resource *resource;
Rob Bradford880ebc72013-07-22 17:31:38 +01001321 struct wl_display *display = keyboard->seat->compositor->wl_display;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001322 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01001323 struct wl_list *focus_resource_list;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001324
Neil Roberts96d790e2013-09-19 17:32:00 +01001325 focus_resource_list = &keyboard->focus_resource_list;
1326
1327 if (!wl_list_empty(focus_resource_list) && keyboard->focus != surface) {
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001328 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001329 wl_resource_for_each(resource, focus_resource_list) {
1330 wl_keyboard_send_leave(resource, serial,
1331 keyboard->focus->resource);
1332 }
1333 move_resources(&keyboard->resource_list, focus_resource_list);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001334 }
1335
Neil Roberts96d790e2013-09-19 17:32:00 +01001336 if (find_resource_for_surface(&keyboard->resource_list, surface) &&
1337 keyboard->focus != surface) {
1338 struct wl_client *surface_client =
1339 wl_resource_get_client(surface->resource);
1340
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001341 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01001342
1343 move_resources_for_client(focus_resource_list,
1344 &keyboard->resource_list,
1345 surface_client);
1346 send_enter_to_resource_list(focus_resource_list,
1347 keyboard,
1348 surface,
1349 serial);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001350 keyboard->focus_serial = serial;
Giulio Camuffo65a07f82013-12-06 12:46:27 +01001351 }
1352
1353 wl_list_remove(&keyboard->focus_resource_listener.link);
1354 wl_list_init(&keyboard->focus_resource_listener.link);
1355 if (surface && surface->resource)
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01001356 wl_resource_add_destroy_listener(surface->resource,
1357 &keyboard->focus_resource_listener);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001358
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001359 keyboard->focus = surface;
1360 wl_signal_emit(&keyboard->focus_signal, keyboard);
1361}
1362
Giulio Camuffoa20ca812014-11-22 11:16:56 +02001363/* Users of this function must manually manage the keyboard focus */
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001364WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001365weston_keyboard_start_grab(struct weston_keyboard *keyboard,
1366 struct weston_keyboard_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001367{
1368 keyboard->grab = grab;
1369 grab->keyboard = keyboard;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001370}
1371
1372WL_EXPORT void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04001373weston_keyboard_end_grab(struct weston_keyboard *keyboard)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001374{
1375 keyboard->grab = &keyboard->default_grab;
1376}
1377
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001378static void
1379weston_keyboard_cancel_grab(struct weston_keyboard *keyboard)
1380{
1381 keyboard->grab->interface->cancel(keyboard->grab);
1382}
1383
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001384WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001385weston_pointer_start_grab(struct weston_pointer *pointer,
1386 struct weston_pointer_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001387{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001388 pointer->grab = grab;
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001389 grab->pointer = pointer;
Kristian Høgsbergda751b82013-07-04 00:58:07 -04001390 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001391}
1392
1393WL_EXPORT void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001394weston_pointer_end_grab(struct weston_pointer *pointer)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001395{
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001396 pointer->grab = &pointer->default_grab;
Kristian Høgsbergda751b82013-07-04 00:58:07 -04001397 pointer->grab->interface->focus(pointer->grab);
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001398}
1399
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001400static void
1401weston_pointer_cancel_grab(struct weston_pointer *pointer)
1402{
1403 pointer->grab->interface->cancel(pointer->grab);
1404}
1405
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001406WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -04001407weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001408{
1409 touch->grab = grab;
1410 grab->touch = touch;
1411}
1412
1413WL_EXPORT void
Kristian Høgsberge329f362013-05-06 22:19:57 -04001414weston_touch_end_grab(struct weston_touch *touch)
Kristian Høgsberg2158a882013-04-18 15:07:39 -04001415{
1416 touch->grab = &touch->default_grab;
1417}
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001418
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001419static void
1420weston_touch_cancel_grab(struct weston_touch *touch)
1421{
1422 touch->grab->interface->cancel(touch->grab);
1423}
1424
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001425static void
1426weston_pointer_clamp_for_output(struct weston_pointer *pointer,
1427 struct weston_output *output,
1428 wl_fixed_t *fx, wl_fixed_t *fy)
1429{
1430 int x, y;
1431
1432 x = wl_fixed_to_int(*fx);
1433 y = wl_fixed_to_int(*fy);
1434
1435 if (x < output->x)
1436 *fx = wl_fixed_from_int(output->x);
1437 else if (x >= output->x + output->width)
1438 *fx = wl_fixed_from_int(output->x +
1439 output->width - 1);
1440 if (y < output->y)
1441 *fy = wl_fixed_from_int(output->y);
1442 else if (y >= output->y + output->height)
1443 *fy = wl_fixed_from_int(output->y +
1444 output->height - 1);
1445}
1446
Rob Bradford806d8c02013-06-25 18:56:41 +01001447WL_EXPORT void
1448weston_pointer_clamp(struct weston_pointer *pointer, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001449{
Rob Bradford806d8c02013-06-25 18:56:41 +01001450 struct weston_compositor *ec = pointer->seat->compositor;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001451 struct weston_output *output, *prev = NULL;
1452 int x, y, old_x, old_y, valid = 0;
1453
1454 x = wl_fixed_to_int(*fx);
1455 y = wl_fixed_to_int(*fy);
Rob Bradford806d8c02013-06-25 18:56:41 +01001456 old_x = wl_fixed_to_int(pointer->x);
1457 old_y = wl_fixed_to_int(pointer->y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001458
1459 wl_list_for_each(output, &ec->output_list, link) {
Rob Bradford66bd9f52013-06-25 18:56:42 +01001460 if (pointer->seat->output && pointer->seat->output != output)
1461 continue;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001462 if (pixman_region32_contains_point(&output->region,
1463 x, y, NULL))
1464 valid = 1;
1465 if (pixman_region32_contains_point(&output->region,
1466 old_x, old_y, NULL))
1467 prev = output;
1468 }
1469
Rob Bradford66bd9f52013-06-25 18:56:42 +01001470 if (!prev)
1471 prev = pointer->seat->output;
1472
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001473 if (prev && !valid)
1474 weston_pointer_clamp_for_output(pointer, prev, fx, fy);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001475}
1476
Jonas Ådahld2510102014-10-05 21:39:14 +02001477static void
1478weston_pointer_move_to(struct weston_pointer *pointer,
1479 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001480{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001481 int32_t ix, iy;
1482
Rob Bradford806d8c02013-06-25 18:56:41 +01001483 weston_pointer_clamp (pointer, &x, &y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001484
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001485 pointer->x = x;
1486 pointer->y = y;
1487
1488 ix = wl_fixed_to_int(x);
1489 iy = wl_fixed_to_int(y);
1490
Kristian Høgsberg195b8692013-05-08 15:02:05 -04001491 if (pointer->sprite) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001492 weston_view_set_position(pointer->sprite,
1493 ix - pointer->hotspot_x,
1494 iy - pointer->hotspot_y);
1495 weston_view_schedule_repaint(pointer->sprite);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001496 }
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001497
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001498 pointer->grab->interface->focus(pointer->grab);
Giulio Camuffo6fcb3782013-11-14 23:42:50 +01001499 wl_signal_emit(&pointer->motion_signal, pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001500}
1501
Jonas Ådahld2510102014-10-05 21:39:14 +02001502WL_EXPORT void
Jonas Ådahld2510102014-10-05 21:39:14 +02001503weston_pointer_move(struct weston_pointer *pointer,
1504 struct weston_pointer_motion_event *event)
1505{
1506 wl_fixed_t x, y;
1507
1508 weston_pointer_motion_to_abs(pointer, event, &x, &y);
1509 weston_pointer_move_to(pointer, x, y);
1510}
1511
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001512/** Verify if the pointer is in a valid position and move it if it isn't.
1513 */
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001514static void
1515weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001516{
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001517 struct weston_pointer *pointer;
1518 struct weston_compositor *ec;
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001519 struct weston_output *output, *closest = NULL;
1520 int x, y, distance, min = INT_MAX;
1521 wl_fixed_t fx, fy;
1522
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02001523 pointer = container_of(listener, struct weston_pointer,
1524 output_destroy_listener);
1525 ec = pointer->seat->compositor;
1526
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001527 x = wl_fixed_to_int(pointer->x);
1528 y = wl_fixed_to_int(pointer->y);
1529
1530 wl_list_for_each(output, &ec->output_list, link) {
1531 if (pixman_region32_contains_point(&output->region,
1532 x, y, NULL))
1533 return;
1534
1535 /* Aproximante the distance from the pointer to the center of
1536 * the output. */
1537 distance = abs(output->x + output->width / 2 - x) +
1538 abs(output->y + output->height / 2 - y);
1539 if (distance < min) {
1540 min = distance;
1541 closest = output;
1542 }
1543 }
1544
1545 /* Nothing to do if there's no output left. */
1546 if (!closest)
1547 return;
1548
1549 fx = pointer->x;
1550 fy = pointer->y;
1551
1552 weston_pointer_clamp_for_output(pointer, closest, &fx, &fy);
Jonas Ådahld2510102014-10-05 21:39:14 +02001553 weston_pointer_move_to(pointer, fx, fy);
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02001554}
1555
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001556WL_EXPORT void
1557notify_motion(struct weston_seat *seat,
Jonas Ådahld2510102014-10-05 21:39:14 +02001558 uint32_t time,
1559 struct weston_pointer_motion_event *event)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001560{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001561 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001562 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001563
1564 weston_compositor_wake(ec);
Jonas Ådahld2510102014-10-05 21:39:14 +02001565 pointer->grab->interface->motion(pointer->grab, time, event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001566}
1567
Daniel Stone96d47c02013-11-19 11:37:12 +01001568static void
1569run_modifier_bindings(struct weston_seat *seat, uint32_t old, uint32_t new)
1570{
1571 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001572 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Daniel Stone96d47c02013-11-19 11:37:12 +01001573 uint32_t diff;
1574 unsigned int i;
1575 struct {
1576 uint32_t xkb;
1577 enum weston_keyboard_modifier weston;
1578 } mods[] = {
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001579 { keyboard->xkb_info->ctrl_mod, MODIFIER_CTRL },
1580 { keyboard->xkb_info->alt_mod, MODIFIER_ALT },
1581 { keyboard->xkb_info->super_mod, MODIFIER_SUPER },
1582 { keyboard->xkb_info->shift_mod, MODIFIER_SHIFT },
Daniel Stone96d47c02013-11-19 11:37:12 +01001583 };
1584
1585 diff = new & ~old;
1586 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
1587 if (diff & (1 << mods[i].xkb))
1588 weston_compositor_run_modifier_binding(compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001589 keyboard,
Daniel Stone96d47c02013-11-19 11:37:12 +01001590 mods[i].weston,
1591 WL_KEYBOARD_KEY_STATE_PRESSED);
1592 }
1593
1594 diff = old & ~new;
1595 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
1596 if (diff & (1 << mods[i].xkb))
1597 weston_compositor_run_modifier_binding(compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001598 keyboard,
Daniel Stone96d47c02013-11-19 11:37:12 +01001599 mods[i].weston,
1600 WL_KEYBOARD_KEY_STATE_RELEASED);
1601 }
1602}
1603
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001604WL_EXPORT void
1605notify_motion_absolute(struct weston_seat *seat,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001606 uint32_t time, double x, double y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001607{
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001608 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001609 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Jonas Ådahld2510102014-10-05 21:39:14 +02001610 struct weston_pointer_motion_event event = { 0 };
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001611
1612 weston_compositor_wake(ec);
Jonas Ådahld2510102014-10-05 21:39:14 +02001613
1614 event = (struct weston_pointer_motion_event) {
1615 .mask = WESTON_POINTER_MOTION_ABS,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001616 .x = x,
1617 .y = y,
Jonas Ådahld2510102014-10-05 21:39:14 +02001618 };
1619
1620 pointer->grab->interface->motion(pointer->grab, time, &event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001621}
1622
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02001623static unsigned int
1624peek_next_activate_serial(struct weston_compositor *c)
1625{
1626 unsigned serial = c->activate_serial + 1;
1627
1628 return serial == 0 ? 1 : serial;
1629}
1630
1631static void
1632inc_activate_serial(struct weston_compositor *c)
1633{
1634 c->activate_serial = peek_next_activate_serial (c);
1635}
1636
1637WL_EXPORT void
1638weston_view_activate(struct weston_view *view,
1639 struct weston_seat *seat,
1640 uint32_t flags)
1641{
1642 struct weston_compositor *compositor = seat->compositor;
1643
1644 if (flags & WESTON_ACTIVATE_FLAG_CLICKED) {
1645 view->click_to_activate_serial =
1646 peek_next_activate_serial(compositor);
1647 }
1648
1649 weston_seat_set_keyboard_focus(seat, view->surface);
1650}
1651
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001652WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001653notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
1654 enum wl_pointer_button_state state)
1655{
1656 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001657 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001658
1659 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001660 weston_compositor_idle_inhibit(compositor);
1661 if (pointer->button_count == 0) {
1662 pointer->grab_button = button;
1663 pointer->grab_time = time;
1664 pointer->grab_x = pointer->x;
1665 pointer->grab_y = pointer->y;
1666 }
1667 pointer->button_count++;
1668 } else {
1669 weston_compositor_idle_release(compositor);
1670 pointer->button_count--;
1671 }
1672
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001673 weston_compositor_run_button_binding(compositor, pointer, time, button,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001674 state);
1675
1676 pointer->grab->interface->button(pointer->grab, time, button, state);
1677
1678 if (pointer->button_count == 1)
1679 pointer->grab_serial =
1680 wl_display_get_serial(compositor->wl_display);
1681}
1682
1683WL_EXPORT void
Peter Hutterer89b6a492016-01-18 15:58:17 +10001684notify_axis(struct weston_seat *seat, uint32_t time,
1685 struct weston_pointer_axis_event *event)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001686{
1687 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001688 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001689
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001690 weston_compositor_wake(compositor);
1691
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001692 if (weston_compositor_run_axis_binding(compositor, pointer,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001693 time, event))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001694 return;
1695
Peter Hutterer89b6a492016-01-18 15:58:17 +10001696 pointer->grab->interface->axis(pointer->grab, time, event);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001697}
1698
Peter Hutterer87743e92016-01-18 16:38:22 +10001699WL_EXPORT void
1700notify_axis_source(struct weston_seat *seat, uint32_t source)
1701{
1702 struct weston_compositor *compositor = seat->compositor;
1703 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1704
1705 weston_compositor_wake(compositor);
1706
1707 pointer->grab->interface->axis_source(pointer->grab, source);
1708}
1709
1710WL_EXPORT void
1711notify_pointer_frame(struct weston_seat *seat)
1712{
1713 struct weston_compositor *compositor = seat->compositor;
1714 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1715
1716 weston_compositor_wake(compositor);
1717
1718 pointer->grab->interface->frame(pointer->grab);
1719}
1720
Giulio Camuffo6ef444d2014-08-28 19:44:09 +03001721WL_EXPORT int
1722weston_keyboard_set_locks(struct weston_keyboard *keyboard,
1723 uint32_t mask, uint32_t value)
1724{
1725#ifdef ENABLE_XKBCOMMON
1726 uint32_t serial;
1727 xkb_mod_mask_t mods_depressed, mods_latched, mods_locked, group;
1728 xkb_mod_mask_t num, caps;
1729
1730 /* We don't want the leds to go out of sync with the actual state
1731 * so if the backend has no way to change the leds don't try to
1732 * change the state */
1733 if (!keyboard->seat->led_update)
1734 return -1;
1735
1736 mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
1737 XKB_STATE_DEPRESSED);
1738 mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
1739 XKB_STATE_LATCHED);
1740 mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
1741 XKB_STATE_LOCKED);
1742 group = xkb_state_serialize_group(keyboard->xkb_state.state,
1743 XKB_STATE_EFFECTIVE);
1744
1745 num = (1 << keyboard->xkb_info->mod2_mod);
1746 caps = (1 << keyboard->xkb_info->caps_mod);
1747 if (mask & WESTON_NUM_LOCK) {
1748 if (value & WESTON_NUM_LOCK)
1749 mods_locked |= num;
1750 else
1751 mods_locked &= ~num;
1752 }
1753 if (mask & WESTON_CAPS_LOCK) {
1754 if (value & WESTON_CAPS_LOCK)
1755 mods_locked |= caps;
1756 else
1757 mods_locked &= ~caps;
1758 }
1759
1760 xkb_state_update_mask(keyboard->xkb_state.state, mods_depressed,
1761 mods_latched, mods_locked, 0, 0, group);
1762
1763 serial = wl_display_next_serial(
1764 keyboard->seat->compositor->wl_display);
1765 notify_modifiers(keyboard->seat, serial);
1766
1767 return 0;
1768#else
1769 return -1;
1770#endif
1771}
1772
Rob Bradford382ff462013-06-24 16:52:45 +01001773#ifdef ENABLE_XKBCOMMON
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001774WL_EXPORT void
1775notify_modifiers(struct weston_seat *seat, uint32_t serial)
1776{
Derek Foreman1281a362015-07-31 16:55:32 -05001777 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001778 struct weston_keyboard_grab *grab = keyboard->grab;
1779 uint32_t mods_depressed, mods_latched, mods_locked, group;
1780 uint32_t mods_lookup;
1781 enum weston_led leds = 0;
1782 int changed = 0;
1783
1784 /* Serialize and update our internal state, checking to see if it's
1785 * different to the previous state. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001786 mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03001787 XKB_STATE_MODS_DEPRESSED);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001788 mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03001789 XKB_STATE_MODS_LATCHED);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001790 mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03001791 XKB_STATE_MODS_LOCKED);
1792 group = xkb_state_serialize_layout(keyboard->xkb_state.state,
1793 XKB_STATE_LAYOUT_EFFECTIVE);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001794
Derek Foreman244e99e2015-06-03 15:53:26 -05001795 if (mods_depressed != keyboard->modifiers.mods_depressed ||
1796 mods_latched != keyboard->modifiers.mods_latched ||
1797 mods_locked != keyboard->modifiers.mods_locked ||
1798 group != keyboard->modifiers.group)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001799 changed = 1;
1800
Derek Foreman244e99e2015-06-03 15:53:26 -05001801 run_modifier_bindings(seat, keyboard->modifiers.mods_depressed,
Daniel Stone96d47c02013-11-19 11:37:12 +01001802 mods_depressed);
1803
Derek Foreman244e99e2015-06-03 15:53:26 -05001804 keyboard->modifiers.mods_depressed = mods_depressed;
1805 keyboard->modifiers.mods_latched = mods_latched;
1806 keyboard->modifiers.mods_locked = mods_locked;
1807 keyboard->modifiers.group = group;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001808
1809 /* And update the modifier_state for bindings. */
1810 mods_lookup = mods_depressed | mods_latched;
1811 seat->modifier_state = 0;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001812 if (mods_lookup & (1 << keyboard->xkb_info->ctrl_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001813 seat->modifier_state |= MODIFIER_CTRL;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001814 if (mods_lookup & (1 << keyboard->xkb_info->alt_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001815 seat->modifier_state |= MODIFIER_ALT;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001816 if (mods_lookup & (1 << keyboard->xkb_info->super_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001817 seat->modifier_state |= MODIFIER_SUPER;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001818 if (mods_lookup & (1 << keyboard->xkb_info->shift_mod))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001819 seat->modifier_state |= MODIFIER_SHIFT;
1820
1821 /* Finally, notify the compositor that LEDs have changed. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001822 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
1823 keyboard->xkb_info->num_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001824 leds |= LED_NUM_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001825 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
1826 keyboard->xkb_info->caps_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001827 leds |= LED_CAPS_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001828 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
1829 keyboard->xkb_info->scroll_led))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001830 leds |= LED_SCROLL_LOCK;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001831 if (leds != keyboard->xkb_state.leds && seat->led_update)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001832 seat->led_update(seat, leds);
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001833 keyboard->xkb_state.leds = leds;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001834
1835 if (changed) {
1836 grab->interface->modifiers(grab,
1837 serial,
1838 keyboard->modifiers.mods_depressed,
1839 keyboard->modifiers.mods_latched,
1840 keyboard->modifiers.mods_locked,
1841 keyboard->modifiers.group);
1842 }
1843}
1844
1845static void
1846update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
1847 enum wl_keyboard_key_state state)
1848{
Derek Foreman1281a362015-07-31 16:55:32 -05001849 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001850 enum xkb_key_direction direction;
1851
Matt Roper01a92732013-06-24 16:52:44 +01001852 /* Keyboard modifiers don't exist in raw keyboard mode */
1853 if (!seat->compositor->use_xkbcommon)
1854 return;
1855
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001856 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1857 direction = XKB_KEY_DOWN;
1858 else
1859 direction = XKB_KEY_UP;
1860
1861 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
1862 * broken keycode system, which starts at 8. */
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001863 xkb_state_update_key(keyboard->xkb_state.state, key + 8, direction);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001864
1865 notify_modifiers(seat, serial);
1866}
Rui Matos65196bc2013-10-10 19:44:19 +02001867
1868static void
1869send_keymap(struct wl_resource *resource, struct weston_xkb_info *xkb_info)
1870{
1871 wl_keyboard_send_keymap(resource,
1872 WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
1873 xkb_info->keymap_fd,
1874 xkb_info->keymap_size);
1875}
1876
1877static void
1878send_modifiers(struct wl_resource *resource, uint32_t serial, struct weston_keyboard *keyboard)
1879{
1880 wl_keyboard_send_modifiers(resource, serial,
1881 keyboard->modifiers.mods_depressed,
1882 keyboard->modifiers.mods_latched,
1883 keyboard->modifiers.mods_locked,
1884 keyboard->modifiers.group);
1885}
1886
1887static struct weston_xkb_info *
1888weston_xkb_info_create(struct xkb_keymap *keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02001889
1890static void
1891update_keymap(struct weston_seat *seat)
1892{
Derek Foreman1281a362015-07-31 16:55:32 -05001893 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Rui Matos65196bc2013-10-10 19:44:19 +02001894 struct wl_resource *resource;
1895 struct weston_xkb_info *xkb_info;
1896 struct xkb_state *state;
1897 xkb_mod_mask_t latched_mods;
1898 xkb_mod_mask_t locked_mods;
1899
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001900 xkb_info = weston_xkb_info_create(keyboard->pending_keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02001901
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001902 xkb_keymap_unref(keyboard->pending_keymap);
1903 keyboard->pending_keymap = NULL;
Rui Matos65196bc2013-10-10 19:44:19 +02001904
1905 if (!xkb_info) {
1906 weston_log("failed to create XKB info\n");
1907 return;
1908 }
1909
1910 state = xkb_state_new(xkb_info->keymap);
1911 if (!state) {
1912 weston_log("failed to initialise XKB state\n");
1913 weston_xkb_info_destroy(xkb_info);
1914 return;
1915 }
1916
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001917 latched_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
1918 XKB_STATE_MODS_LATCHED);
1919 locked_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
1920 XKB_STATE_MODS_LOCKED);
Rui Matos65196bc2013-10-10 19:44:19 +02001921 xkb_state_update_mask(state,
1922 0, /* depressed */
1923 latched_mods,
1924 locked_mods,
1925 0, 0, 0);
1926
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001927 weston_xkb_info_destroy(keyboard->xkb_info);
1928 keyboard->xkb_info = xkb_info;
Rui Matos65196bc2013-10-10 19:44:19 +02001929
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001930 xkb_state_unref(keyboard->xkb_state.state);
1931 keyboard->xkb_state.state = state;
Rui Matos65196bc2013-10-10 19:44:19 +02001932
Derek Foremanbc91e542015-06-03 15:53:27 -05001933 wl_resource_for_each(resource, &keyboard->resource_list)
Rui Matos65196bc2013-10-10 19:44:19 +02001934 send_keymap(resource, xkb_info);
Derek Foremanbc91e542015-06-03 15:53:27 -05001935 wl_resource_for_each(resource, &keyboard->focus_resource_list)
Rui Matos65196bc2013-10-10 19:44:19 +02001936 send_keymap(resource, xkb_info);
1937
1938 notify_modifiers(seat, wl_display_next_serial(seat->compositor->wl_display));
1939
1940 if (!latched_mods && !locked_mods)
1941 return;
1942
Derek Foremanbc91e542015-06-03 15:53:27 -05001943 wl_resource_for_each(resource, &keyboard->resource_list)
1944 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), keyboard);
1945 wl_resource_for_each(resource, &keyboard->focus_resource_list)
1946 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), keyboard);
Rui Matos65196bc2013-10-10 19:44:19 +02001947}
Rob Bradford382ff462013-06-24 16:52:45 +01001948#else
1949WL_EXPORT void
1950notify_modifiers(struct weston_seat *seat, uint32_t serial)
1951{
1952}
1953
1954static void
1955update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
1956 enum wl_keyboard_key_state state)
1957{
1958}
Rui Matos65196bc2013-10-10 19:44:19 +02001959
1960static void
1961update_keymap(struct weston_seat *seat)
1962{
1963}
Rob Bradford382ff462013-06-24 16:52:45 +01001964#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001965
1966WL_EXPORT void
1967notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
1968 enum wl_keyboard_key_state state,
1969 enum weston_key_state_update update_state)
1970{
1971 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05001972 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001973 struct weston_keyboard_grab *grab = keyboard->grab;
Pekka Paalanen86b53962014-11-19 13:43:32 +02001974 uint32_t *k, *end;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001975
1976 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001977 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001978 } else {
1979 weston_compositor_idle_release(compositor);
1980 }
1981
Pekka Paalanen86b53962014-11-19 13:43:32 +02001982 end = keyboard->keys.data + keyboard->keys.size;
1983 for (k = keyboard->keys.data; k < end; k++) {
1984 if (*k == key) {
1985 /* Ignore server-generated repeats. */
1986 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1987 return;
1988 *k = *--end;
1989 }
1990 }
1991 keyboard->keys.size = (void *) end - keyboard->keys.data;
1992 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1993 k = wl_array_add(&keyboard->keys, sizeof *k);
1994 *k = key;
1995 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04001996
1997 if (grab == &keyboard->default_grab ||
1998 grab == &keyboard->input_method_grab) {
Derek Foreman99a6a2d2015-07-15 13:00:43 -05001999 weston_compositor_run_key_binding(compositor, keyboard, time,
2000 key, state);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002001 grab = keyboard->grab;
2002 }
2003
2004 grab->interface->key(grab, time, key, state);
2005
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002006 if (keyboard->pending_keymap &&
Pekka Paalanen86b53962014-11-19 13:43:32 +02002007 keyboard->keys.size == 0)
Rui Matos65196bc2013-10-10 19:44:19 +02002008 update_keymap(seat);
2009
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002010 if (update_state == STATE_UPDATE_AUTOMATIC) {
2011 update_modifier_state(seat,
2012 wl_display_get_serial(compositor->wl_display),
2013 key,
2014 state);
2015 }
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002016
Olivier Fourdandf84dbe2016-06-30 16:01:56 +02002017 keyboard->grab_serial = wl_display_get_serial(compositor->wl_display);
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002018 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Giulio Camuffob6ddf6c2015-02-06 19:06:54 +02002019 keyboard->grab_time = time;
2020 keyboard->grab_key = key;
2021 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002022}
2023
2024WL_EXPORT void
2025notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002026 double x, double y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002027{
Derek Foreman1281a362015-07-31 16:55:32 -05002028 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2029
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002030 if (output) {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002031 weston_pointer_move_to(pointer,
2032 wl_fixed_from_double(x),
2033 wl_fixed_from_double(y));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002034 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002035 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002036 * NULL) here, but somehow that breaks re-entry... */
2037 }
2038}
2039
2040static void
2041destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
2042{
2043 struct weston_seat *ws;
2044
2045 ws = container_of(listener, struct weston_seat,
2046 saved_kbd_focus_listener);
2047
2048 ws->saved_kbd_focus = NULL;
2049}
2050
2051WL_EXPORT void
2052notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
2053 enum weston_key_state_update update_state)
2054{
2055 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002056 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04002057 struct weston_surface *surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002058 uint32_t *k, serial;
2059
2060 serial = wl_display_next_serial(compositor->wl_display);
2061 wl_array_copy(&keyboard->keys, keys);
2062 wl_array_for_each(k, &keyboard->keys) {
2063 weston_compositor_idle_inhibit(compositor);
2064 if (update_state == STATE_UPDATE_AUTOMATIC)
2065 update_modifier_state(seat, serial, *k,
2066 WL_KEYBOARD_KEY_STATE_PRESSED);
2067 }
2068
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002069 surface = seat->saved_kbd_focus;
2070
2071 if (surface) {
2072 wl_list_remove(&seat->saved_kbd_focus_listener.link);
2073 weston_keyboard_set_focus(keyboard, surface);
2074 seat->saved_kbd_focus = NULL;
2075 }
2076}
2077
2078WL_EXPORT void
2079notify_keyboard_focus_out(struct weston_seat *seat)
2080{
2081 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002082 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2083 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002084 uint32_t *k, serial;
2085
2086 serial = wl_display_next_serial(compositor->wl_display);
2087 wl_array_for_each(k, &keyboard->keys) {
2088 weston_compositor_idle_release(compositor);
2089 update_modifier_state(seat, serial, *k,
2090 WL_KEYBOARD_KEY_STATE_RELEASED);
2091 }
2092
2093 seat->modifier_state = 0;
2094
2095 if (keyboard->focus) {
2096 seat->saved_kbd_focus = keyboard->focus;
2097 seat->saved_kbd_focus_listener.notify =
2098 destroy_device_saved_kbd_focus;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002099 wl_signal_add(&keyboard->focus->destroy_signal,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002100 &seat->saved_kbd_focus_listener);
2101 }
2102
2103 weston_keyboard_set_focus(keyboard, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002104 weston_keyboard_cancel_grab(keyboard);
Derek Foreman1281a362015-07-31 16:55:32 -05002105 if (pointer)
2106 weston_pointer_cancel_grab(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002107}
2108
Michael Fua2bb7912013-07-23 15:51:06 +08002109WL_EXPORT void
Derek Foreman4c93c082015-04-30 16:45:41 -05002110weston_touch_set_focus(struct weston_touch *touch, struct weston_view *view)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002111{
Neil Roberts96d790e2013-09-19 17:32:00 +01002112 struct wl_list *focus_resource_list;
2113
Derek Foreman4c93c082015-04-30 16:45:41 -05002114 focus_resource_list = &touch->focus_resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002115
Derek Foreman4c93c082015-04-30 16:45:41 -05002116 if (view && touch->focus &&
2117 touch->focus->surface == view->surface) {
2118 touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002119 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002120 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002121
Derek Foreman4c93c082015-04-30 16:45:41 -05002122 wl_list_remove(&touch->focus_resource_listener.link);
2123 wl_list_init(&touch->focus_resource_listener.link);
2124 wl_list_remove(&touch->focus_view_listener.link);
2125 wl_list_init(&touch->focus_view_listener.link);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01002126
Neil Roberts96d790e2013-09-19 17:32:00 +01002127 if (!wl_list_empty(focus_resource_list)) {
Derek Foreman4c93c082015-04-30 16:45:41 -05002128 move_resources(&touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002129 focus_resource_list);
2130 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002131
Jason Ekstranda7af7042013-10-12 22:38:11 -05002132 if (view) {
Derek Foreman362656b2014-09-04 10:23:05 -05002133 struct wl_client *surface_client;
2134
2135 if (!view->surface->resource) {
Derek Foreman4c93c082015-04-30 16:45:41 -05002136 touch->focus = NULL;
Derek Foreman362656b2014-09-04 10:23:05 -05002137 return;
2138 }
2139
2140 surface_client = wl_resource_get_client(view->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +01002141 move_resources_for_client(focus_resource_list,
Derek Foreman4c93c082015-04-30 16:45:41 -05002142 &touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002143 surface_client);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01002144 wl_resource_add_destroy_listener(view->surface->resource,
Derek Foreman4c93c082015-04-30 16:45:41 -05002145 &touch->focus_resource_listener);
2146 wl_signal_add(&view->destroy_signal, &touch->focus_view_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002147 }
Derek Foreman4c93c082015-04-30 16:45:41 -05002148 touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002149}
2150
2151/**
2152 * notify_touch - emulates button touches and notifies surfaces accordingly.
2153 *
2154 * It assumes always the correct cycle sequence until it gets here: touch_down
2155 * → touch_update → ... → touch_update → touch_end. The driver is responsible
2156 * for sending along such order.
2157 *
2158 */
2159WL_EXPORT void
2160notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002161 double double_x, double double_y, int touch_type)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002162{
2163 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002164 struct weston_touch *touch = weston_seat_get_touch(seat);
Kristian Høgsberge329f362013-05-06 22:19:57 -04002165 struct weston_touch_grab *grab = touch->grab;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002166 struct weston_view *ev;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002167 wl_fixed_t sx, sy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002168 wl_fixed_t x = wl_fixed_from_double(double_x);
2169 wl_fixed_t y = wl_fixed_from_double(double_y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002170
2171 /* Update grab's global coordinates. */
Neil Roberts306fe082013-10-03 16:43:06 +01002172 if (touch_id == touch->grab_touch_id && touch_type != WL_TOUCH_UP) {
2173 touch->grab_x = x;
2174 touch->grab_y = y;
2175 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002176
2177 switch (touch_type) {
2178 case WL_TOUCH_DOWN:
2179 weston_compositor_idle_inhibit(ec);
2180
Jonas Ådahl9484b692013-12-02 22:05:03 +01002181 touch->num_tp++;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002182
Jason Ekstranda7af7042013-10-12 22:38:11 -05002183 /* the first finger down picks the view, and all further go
2184 * to that view for the remainder of the touch session i.e.
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002185 * until all touch points are up again. */
Jonas Ådahl9484b692013-12-02 22:05:03 +01002186 if (touch->num_tp == 1) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002187 ev = weston_compositor_pick_view(ec, x, y, &sx, &sy);
Derek Foreman4c93c082015-04-30 16:45:41 -05002188 weston_touch_set_focus(touch, ev);
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002189 } else if (!touch->focus) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002190 /* Unexpected condition: We have non-initial touch but
2191 * there is no focused surface.
2192 */
Chris Michael3f607d32015-10-07 11:59:49 -04002193 weston_log("touch event received with %d points down "
Jonas Ådahl9484b692013-12-02 22:05:03 +01002194 "but no surface focused\n", touch->num_tp);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002195 return;
2196 }
2197
Derek Foreman99a6a2d2015-07-15 13:00:43 -05002198 weston_compositor_run_touch_binding(ec, touch,
Kristian Høgsbergc8964012014-02-05 14:25:18 -08002199 time, touch_type);
2200
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002201 grab->interface->down(grab, time, touch_id, x, y);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002202 if (touch->num_tp == 1) {
Rusty Lynchf1407ff2013-08-08 21:13:57 -07002203 touch->grab_serial =
2204 wl_display_get_serial(ec->wl_display);
Neil Roberts306fe082013-10-03 16:43:06 +01002205 touch->grab_touch_id = touch_id;
Rusty Lynchf1407ff2013-08-08 21:13:57 -07002206 touch->grab_time = time;
2207 touch->grab_x = x;
2208 touch->grab_y = y;
2209 }
2210
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002211 break;
2212 case WL_TOUCH_MOTION:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002213 ev = touch->focus;
2214 if (!ev)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002215 break;
2216
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002217 grab->interface->motion(grab, time, touch_id, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002218 break;
2219 case WL_TOUCH_UP:
Kristian Høgsberga30e29a2014-01-08 22:29:20 -08002220 if (touch->num_tp == 0) {
2221 /* This can happen if we start out with one or
2222 * more fingers on the touch screen, in which
2223 * case we didn't get the corresponding down
2224 * event. */
2225 weston_log("unmatched touch up event\n");
2226 break;
2227 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002228 weston_compositor_idle_release(ec);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002229 touch->num_tp--;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002230
2231 grab->interface->up(grab, time, touch_id);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002232 if (touch->num_tp == 0)
Derek Foreman4c93c082015-04-30 16:45:41 -05002233 weston_touch_set_focus(touch, NULL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002234 break;
2235 }
2236}
2237
Jonas Ådahl1679f232014-04-12 09:39:51 +02002238WL_EXPORT void
2239notify_touch_frame(struct weston_seat *seat)
2240{
Derek Foreman1281a362015-07-31 16:55:32 -05002241 struct weston_touch *touch = weston_seat_get_touch(seat);
Jonas Ådahl1679f232014-04-12 09:39:51 +02002242 struct weston_touch_grab *grab = touch->grab;
2243
2244 grab->interface->frame(grab);
2245}
2246
Derek Foreman3cc004a2015-11-06 15:56:09 -06002247WL_EXPORT void
2248notify_touch_cancel(struct weston_seat *seat)
2249{
2250 struct weston_touch *touch = weston_seat_get_touch(seat);
2251 struct weston_touch_grab *grab = touch->grab;
2252
2253 grab->interface->cancel(grab);
2254}
2255
Pekka Paalanen8274d902014-08-06 19:36:51 +03002256static int
2257pointer_cursor_surface_get_label(struct weston_surface *surface,
2258 char *buf, size_t len)
2259{
2260 return snprintf(buf, len, "cursor");
2261}
2262
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002263static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002264pointer_cursor_surface_committed(struct weston_surface *es,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002265 int32_t dx, int32_t dy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002266{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002267 struct weston_pointer *pointer = es->committed_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002268 int x, y;
2269
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002270 if (es->width == 0)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002271 return;
2272
Jason Ekstranda7af7042013-10-12 22:38:11 -05002273 assert(es == pointer->sprite->surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002274
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002275 pointer->hotspot_x -= dx;
2276 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002277
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002278 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
2279 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002280
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002281 weston_view_set_position(pointer->sprite, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002282
2283 empty_region(&es->pending.input);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02002284 empty_region(&es->input);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002285
2286 if (!weston_surface_is_mapped(es)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002287 weston_layer_entry_insert(&es->compositor->cursor_layer.view_list,
2288 &pointer->sprite->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002289 weston_view_update_transform(pointer->sprite);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002290 es->is_mapped = true;
2291 pointer->sprite->is_mapped = true;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002292 }
2293}
2294
2295static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002296pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2297 uint32_t serial, struct wl_resource *surface_resource,
2298 int32_t x, int32_t y)
2299{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002300 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002301 struct weston_surface *surface = NULL;
2302
2303 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002304 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002305
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002306 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002307 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002308 /* pointer->focus->surface->resource can be NULL. Surfaces like the
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02002309 black_surface used in shell.c for fullscreen don't have
2310 a resource, but can still have focus */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002311 if (pointer->focus->surface->resource == NULL)
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02002312 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002313 if (wl_resource_get_client(pointer->focus->surface->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002314 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002315 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002316 return;
2317
Derek Foreman4e53c532015-03-23 10:55:32 -05002318 if (!surface) {
2319 if (pointer->sprite)
2320 pointer_unmap_sprite(pointer);
2321 return;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002322 }
2323
Jonas Ådahlb4070242015-03-18 15:08:03 +08002324 if (pointer->sprite && pointer->sprite->surface == surface &&
2325 pointer->hotspot_x == x && pointer->hotspot_y == y)
2326 return;
2327
Derek Foreman4e53c532015-03-23 10:55:32 -05002328 if (!pointer->sprite || pointer->sprite->surface != surface) {
2329 if (weston_surface_set_role(surface, "wl_pointer-cursor",
2330 resource,
2331 WL_POINTER_ERROR_ROLE) < 0)
2332 return;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002333
Derek Foreman4e53c532015-03-23 10:55:32 -05002334 if (pointer->sprite)
2335 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002336
Derek Foreman4e53c532015-03-23 10:55:32 -05002337 wl_signal_add(&surface->destroy_signal,
2338 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002339
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002340 surface->committed = pointer_cursor_surface_committed;
2341 surface->committed_private = pointer;
Derek Foreman4e53c532015-03-23 10:55:32 -05002342 weston_surface_set_label_func(surface,
2343 pointer_cursor_surface_get_label);
2344 pointer->sprite = weston_view_create(surface);
2345 }
2346
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002347 pointer->hotspot_x = x;
2348 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002349
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +02002350 if (surface->buffer_ref.buffer) {
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002351 pointer_cursor_surface_committed(surface, 0, 0);
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +02002352 weston_view_schedule_repaint(pointer->sprite);
2353 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002354}
2355
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002356static void
2357pointer_release(struct wl_client *client, struct wl_resource *resource)
2358{
2359 wl_resource_destroy(resource);
2360}
2361
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002362static const struct wl_pointer_interface pointer_interface = {
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002363 pointer_set_cursor,
2364 pointer_release
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002365};
2366
2367static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002368seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2369 uint32_t id)
2370{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002371 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002372 /* We use the pointer_state directly, which means we'll
2373 * give a wl_pointer if the seat has ever had one - even though
2374 * the spec explicitly states that this request only takes effect
2375 * if the seat has the pointer capability.
2376 *
2377 * This prevents a race between the compositor sending new
2378 * capabilities and the client trying to use the old ones.
2379 */
2380 struct weston_pointer *pointer = seat->pointer_state;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002381 struct wl_resource *cr;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002382 struct weston_pointer_client *pointer_client;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002383
Derek Foreman1281a362015-07-31 16:55:32 -05002384 if (!pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002385 return;
2386
Jason Ekstranda85118c2013-06-27 20:17:02 -05002387 cr = wl_resource_create(client, &wl_pointer_interface,
2388 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002389 if (cr == NULL) {
2390 wl_client_post_no_memory(client);
2391 return;
2392 }
2393
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002394 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
2395 if (!pointer_client) {
2396 wl_client_post_no_memory(client);
2397 return;
2398 }
2399
2400 wl_list_insert(&pointer_client->pointer_resources,
2401 wl_resource_get_link(cr));
Derek Foreman1281a362015-07-31 16:55:32 -05002402 wl_resource_set_implementation(cr, &pointer_interface, pointer,
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002403 unbind_pointer_client_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002404
Derek Foreman1281a362015-07-31 16:55:32 -05002405 if (pointer->focus && pointer->focus->surface->resource &&
2406 wl_resource_get_client(pointer->focus->surface->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002407 wl_fixed_t sx, sy;
2408
Derek Foreman1281a362015-07-31 16:55:32 -05002409 weston_view_from_global_fixed(pointer->focus,
2410 pointer->x,
2411 pointer->y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002412 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002413
Neil Roberts96d790e2013-09-19 17:32:00 +01002414 wl_pointer_send_enter(cr,
Derek Foreman1281a362015-07-31 16:55:32 -05002415 pointer->focus_serial,
2416 pointer->focus->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01002417 sx, sy);
Peter Hutterer87743e92016-01-18 16:38:22 +10002418 pointer_send_frame(cr);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002419 }
2420}
2421
2422static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002423keyboard_release(struct wl_client *client, struct wl_resource *resource)
2424{
2425 wl_resource_destroy(resource);
2426}
2427
2428static const struct wl_keyboard_interface keyboard_interface = {
2429 keyboard_release
2430};
2431
Derek Foreman280e7dd2014-10-03 13:13:42 -05002432static bool
Neil Roberts96d790e2013-09-19 17:32:00 +01002433should_send_modifiers_to_client(struct weston_seat *seat,
2434 struct wl_client *client)
2435{
Derek Foreman1281a362015-07-31 16:55:32 -05002436 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2437 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2438
2439 if (keyboard &&
2440 keyboard->focus &&
2441 keyboard->focus->resource &&
2442 wl_resource_get_client(keyboard->focus->resource) == client)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002443 return true;
Neil Roberts96d790e2013-09-19 17:32:00 +01002444
Derek Foreman1281a362015-07-31 16:55:32 -05002445 if (pointer &&
2446 pointer->focus &&
2447 pointer->focus->surface->resource &&
2448 wl_resource_get_client(pointer->focus->surface->resource) == client)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002449 return true;
Neil Roberts96d790e2013-09-19 17:32:00 +01002450
Derek Foreman280e7dd2014-10-03 13:13:42 -05002451 return false;
Neil Roberts96d790e2013-09-19 17:32:00 +01002452}
2453
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002454static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002455seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2456 uint32_t id)
2457{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002458 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002459 /* We use the keyboard_state directly, which means we'll
2460 * give a wl_keyboard if the seat has ever had one - even though
2461 * the spec explicitly states that this request only takes effect
2462 * if the seat has the keyboard capability.
2463 *
2464 * This prevents a race between the compositor sending new
2465 * capabilities and the client trying to use the old ones.
2466 */
2467 struct weston_keyboard *keyboard = seat->keyboard_state;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002468 struct wl_resource *cr;
2469
Derek Foreman345c9f32015-06-03 15:53:28 -05002470 if (!keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002471 return;
2472
Jason Ekstranda85118c2013-06-27 20:17:02 -05002473 cr = wl_resource_create(client, &wl_keyboard_interface,
2474 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002475 if (cr == NULL) {
2476 wl_client_post_no_memory(client);
2477 return;
2478 }
2479
Neil Roberts96d790e2013-09-19 17:32:00 +01002480 /* May be moved to focused list later by either
2481 * weston_keyboard_set_focus or directly if this client is already
2482 * focused */
Derek Foreman345c9f32015-06-03 15:53:28 -05002483 wl_list_insert(&keyboard->resource_list, wl_resource_get_link(cr));
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002484 wl_resource_set_implementation(cr, &keyboard_interface,
2485 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002486
Jonny Lamb66a41a02014-08-12 14:58:25 +02002487 if (wl_resource_get_version(cr) >= WL_KEYBOARD_REPEAT_INFO_SINCE_VERSION) {
2488 wl_keyboard_send_repeat_info(cr,
2489 seat->compositor->kb_repeat_rate,
2490 seat->compositor->kb_repeat_delay);
2491 }
Jasper St. Pierred8c6aeb2014-08-04 13:43:24 -04002492
Matt Roper01a92732013-06-24 16:52:44 +01002493 if (seat->compositor->use_xkbcommon) {
2494 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002495 keyboard->xkb_info->keymap_fd,
2496 keyboard->xkb_info->keymap_size);
Matt Roper01a92732013-06-24 16:52:44 +01002497 } else {
2498 int null_fd = open("/dev/null", O_RDONLY);
2499 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP,
2500 null_fd,
2501 0);
2502 close(null_fd);
2503 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002504
Neil Roberts96d790e2013-09-19 17:32:00 +01002505 if (should_send_modifiers_to_client(seat, client)) {
Derek Foreman345c9f32015-06-03 15:53:28 -05002506 send_modifiers_to_resource(keyboard,
Neil Roberts96d790e2013-09-19 17:32:00 +01002507 cr,
Derek Foreman345c9f32015-06-03 15:53:28 -05002508 keyboard->focus_serial);
Neil Roberts96d790e2013-09-19 17:32:00 +01002509 }
2510
Derek Foreman345c9f32015-06-03 15:53:28 -05002511 if (keyboard->focus && keyboard->focus->resource &&
2512 wl_resource_get_client(keyboard->focus->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01002513 struct weston_surface *surface =
Derek Foreman345c9f32015-06-03 15:53:28 -05002514 (struct weston_surface *)keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +01002515
2516 wl_list_remove(wl_resource_get_link(cr));
Derek Foreman345c9f32015-06-03 15:53:28 -05002517 wl_list_insert(&keyboard->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002518 wl_resource_get_link(cr));
2519 wl_keyboard_send_enter(cr,
Derek Foreman345c9f32015-06-03 15:53:28 -05002520 keyboard->focus_serial,
Neil Roberts96d790e2013-09-19 17:32:00 +01002521 surface->resource,
Derek Foreman345c9f32015-06-03 15:53:28 -05002522 &keyboard->keys);
Neil Roberts96d790e2013-09-19 17:32:00 +01002523
2524 /* If this is the first keyboard resource for this
2525 * client... */
Derek Foreman345c9f32015-06-03 15:53:28 -05002526 if (keyboard->focus_resource_list.prev ==
Neil Roberts96d790e2013-09-19 17:32:00 +01002527 wl_resource_get_link(cr))
2528 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002529 }
2530}
2531
2532static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002533touch_release(struct wl_client *client, struct wl_resource *resource)
2534{
2535 wl_resource_destroy(resource);
2536}
2537
2538static const struct wl_touch_interface touch_interface = {
2539 touch_release
2540};
2541
2542static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002543seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2544 uint32_t id)
2545{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002546 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002547 /* We use the touch_state directly, which means we'll
2548 * give a wl_touch if the seat has ever had one - even though
2549 * the spec explicitly states that this request only takes effect
2550 * if the seat has the touch capability.
2551 *
2552 * This prevents a race between the compositor sending new
2553 * capabilities and the client trying to use the old ones.
2554 */
2555 struct weston_touch *touch = seat->touch_state;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002556 struct wl_resource *cr;
2557
Derek Foreman1281a362015-07-31 16:55:32 -05002558 if (!touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002559 return;
2560
Jason Ekstranda85118c2013-06-27 20:17:02 -05002561 cr = wl_resource_create(client, &wl_touch_interface,
2562 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002563 if (cr == NULL) {
2564 wl_client_post_no_memory(client);
2565 return;
2566 }
2567
Derek Foreman1281a362015-07-31 16:55:32 -05002568 if (touch->focus &&
2569 wl_resource_get_client(touch->focus->surface->resource) == client) {
Chokshi, Mituld6697142015-10-09 08:28:47 +00002570 wl_list_insert(&touch->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002571 wl_resource_get_link(cr));
2572 } else {
Chokshi, Mituld6697142015-10-09 08:28:47 +00002573 wl_list_insert(&touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002574 wl_resource_get_link(cr));
2575 }
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002576 wl_resource_set_implementation(cr, &touch_interface,
2577 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002578}
2579
Quentin Glidicaab1d362016-03-13 17:49:08 +01002580static void
2581seat_release(struct wl_client *client, struct wl_resource *resource)
2582{
2583 wl_resource_destroy(resource);
2584}
2585
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002586static const struct wl_seat_interface seat_interface = {
2587 seat_get_pointer,
2588 seat_get_keyboard,
2589 seat_get_touch,
Quentin Glidicaab1d362016-03-13 17:49:08 +01002590 seat_release,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002591};
2592
2593static void
2594bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2595{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002596 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002597 struct wl_resource *resource;
2598 enum wl_seat_capability caps = 0;
2599
Jason Ekstranda85118c2013-06-27 20:17:02 -05002600 resource = wl_resource_create(client,
Derek Foreman1909c102015-11-26 14:17:47 -06002601 &wl_seat_interface, version, id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05002602 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05002603 wl_resource_set_implementation(resource, &seat_interface, data,
2604 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002605
Derek Foreman1281a362015-07-31 16:55:32 -05002606 if (weston_seat_get_pointer(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002607 caps |= WL_SEAT_CAPABILITY_POINTER;
Derek Foreman1281a362015-07-31 16:55:32 -05002608 if (weston_seat_get_keyboard(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002609 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Derek Foreman1281a362015-07-31 16:55:32 -05002610 if (weston_seat_get_touch(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002611 caps |= WL_SEAT_CAPABILITY_TOUCH;
2612
2613 wl_seat_send_capabilities(resource, caps);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04002614 if (version >= WL_SEAT_NAME_SINCE_VERSION)
Rob Bradforde445ae62013-05-31 18:09:51 +01002615 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002616}
2617
Jonas Ådahl30d61d82014-10-22 21:21:17 +02002618static void
2619relative_pointer_destroy(struct wl_client *client,
2620 struct wl_resource *resource)
2621{
2622 wl_resource_destroy(resource);
2623}
2624
2625static const struct zwp_relative_pointer_v1_interface relative_pointer_interface = {
2626 relative_pointer_destroy
2627};
2628
2629static void
2630relative_pointer_manager_destroy(struct wl_client *client,
2631 struct wl_resource *resource)
2632{
2633 wl_resource_destroy(resource);
2634}
2635
2636static void
2637relative_pointer_manager_get_relative_pointer(struct wl_client *client,
2638 struct wl_resource *resource,
2639 uint32_t id,
2640 struct wl_resource *pointer_resource)
2641{
2642 struct weston_pointer *pointer =
2643 wl_resource_get_user_data(pointer_resource);
2644 struct weston_pointer_client *pointer_client;
2645 struct wl_resource *cr;
2646
2647 cr = wl_resource_create(client, &zwp_relative_pointer_v1_interface,
2648 wl_resource_get_version(resource), id);
2649 if (cr == NULL) {
2650 wl_client_post_no_memory(client);
2651 return;
2652 }
2653
2654 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
2655 if (!pointer_client) {
2656 wl_client_post_no_memory(client);
2657 return;
2658 }
2659
2660 wl_list_insert(&pointer_client->relative_pointer_resources,
2661 wl_resource_get_link(cr));
2662 wl_resource_set_implementation(cr, &relative_pointer_interface,
2663 pointer,
2664 unbind_pointer_client_resource);
2665}
2666
2667static const struct zwp_relative_pointer_manager_v1_interface relative_pointer_manager = {
2668 relative_pointer_manager_destroy,
2669 relative_pointer_manager_get_relative_pointer,
2670};
2671
2672static void
2673bind_relative_pointer_manager(struct wl_client *client, void *data,
2674 uint32_t version, uint32_t id)
2675{
2676 struct weston_compositor *compositor = data;
2677 struct wl_resource *resource;
2678
2679 resource = wl_resource_create(client,
2680 &zwp_relative_pointer_manager_v1_interface,
2681 1, id);
2682
2683 wl_resource_set_implementation(resource, &relative_pointer_manager,
2684 compositor,
2685 NULL);
2686}
2687
Rob Bradford382ff462013-06-24 16:52:45 +01002688#ifdef ENABLE_XKBCOMMON
Giulio Camuffo0358af42016-06-02 21:48:08 +03002689WL_EXPORT int
2690weston_compositor_set_xkb_rule_names(struct weston_compositor *ec,
2691 struct xkb_rule_names *names)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002692{
Rob Bradford382ff462013-06-24 16:52:45 +01002693 ec->use_xkbcommon = 1;
Matt Roper01a92732013-06-24 16:52:44 +01002694
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002695 if (ec->xkb_context == NULL) {
2696 ec->xkb_context = xkb_context_new(0);
2697 if (ec->xkb_context == NULL) {
2698 weston_log("failed to create XKB context\n");
2699 return -1;
2700 }
2701 }
2702
2703 if (names)
2704 ec->xkb_names = *names;
2705 if (!ec->xkb_names.rules)
2706 ec->xkb_names.rules = strdup("evdev");
2707 if (!ec->xkb_names.model)
2708 ec->xkb_names.model = strdup("pc105");
2709 if (!ec->xkb_names.layout)
2710 ec->xkb_names.layout = strdup("us");
2711
2712 return 0;
2713}
2714
Stefan Schmidtfda26522013-09-17 10:54:09 +01002715static void
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002716weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002717{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002718 if (--xkb_info->ref_count > 0)
2719 return;
2720
Ran Benitac9c74152014-08-19 23:59:52 +03002721 xkb_keymap_unref(xkb_info->keymap);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002722
2723 if (xkb_info->keymap_area)
2724 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2725 if (xkb_info->keymap_fd >= 0)
2726 close(xkb_info->keymap_fd);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002727 free(xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002728}
2729
2730void
2731weston_compositor_xkb_destroy(struct weston_compositor *ec)
2732{
Matt Roper01a92732013-06-24 16:52:44 +01002733 /*
2734 * If we're operating in raw keyboard mode, we never initialized
2735 * libxkbcommon so there's no cleanup to do either.
2736 */
2737 if (!ec->use_xkbcommon)
2738 return;
2739
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002740 free((char *) ec->xkb_names.rules);
2741 free((char *) ec->xkb_names.model);
2742 free((char *) ec->xkb_names.layout);
2743 free((char *) ec->xkb_names.variant);
2744 free((char *) ec->xkb_names.options);
Stefan Schmidtfda26522013-09-17 10:54:09 +01002745
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002746 if (ec->xkb_info)
2747 weston_xkb_info_destroy(ec->xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002748 xkb_context_unref(ec->xkb_context);
2749}
2750
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002751static struct weston_xkb_info *
2752weston_xkb_info_create(struct xkb_keymap *keymap)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002753{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002754 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
2755 if (xkb_info == NULL)
2756 return NULL;
2757
Ran Benita2e1968f2014-08-19 23:59:51 +03002758 xkb_info->keymap = xkb_keymap_ref(keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002759 xkb_info->ref_count = 1;
2760
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002761 char *keymap_str;
2762
Ran Benita2e1968f2014-08-19 23:59:51 +03002763 xkb_info->shift_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2764 XKB_MOD_NAME_SHIFT);
2765 xkb_info->caps_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2766 XKB_MOD_NAME_CAPS);
2767 xkb_info->ctrl_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2768 XKB_MOD_NAME_CTRL);
2769 xkb_info->alt_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2770 XKB_MOD_NAME_ALT);
2771 xkb_info->mod2_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2772 "Mod2");
2773 xkb_info->mod3_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2774 "Mod3");
2775 xkb_info->super_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2776 XKB_MOD_NAME_LOGO);
2777 xkb_info->mod5_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2778 "Mod5");
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002779
Ran Benita2e1968f2014-08-19 23:59:51 +03002780 xkb_info->num_led = xkb_keymap_led_get_index(xkb_info->keymap,
2781 XKB_LED_NAME_NUM);
2782 xkb_info->caps_led = xkb_keymap_led_get_index(xkb_info->keymap,
2783 XKB_LED_NAME_CAPS);
2784 xkb_info->scroll_led = xkb_keymap_led_get_index(xkb_info->keymap,
2785 XKB_LED_NAME_SCROLL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002786
Ran Benita2e1968f2014-08-19 23:59:51 +03002787 keymap_str = xkb_keymap_get_as_string(xkb_info->keymap,
2788 XKB_KEYMAP_FORMAT_TEXT_V1);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002789 if (keymap_str == NULL) {
2790 weston_log("failed to get string version of keymap\n");
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002791 goto err_keymap;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002792 }
2793 xkb_info->keymap_size = strlen(keymap_str) + 1;
2794
2795 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
2796 if (xkb_info->keymap_fd < 0) {
2797 weston_log("creating a keymap file for %lu bytes failed: %m\n",
2798 (unsigned long) xkb_info->keymap_size);
2799 goto err_keymap_str;
2800 }
2801
2802 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2803 PROT_READ | PROT_WRITE,
2804 MAP_SHARED, xkb_info->keymap_fd, 0);
2805 if (xkb_info->keymap_area == MAP_FAILED) {
2806 weston_log("failed to mmap() %lu bytes\n",
2807 (unsigned long) xkb_info->keymap_size);
2808 goto err_dev_zero;
2809 }
2810 strcpy(xkb_info->keymap_area, keymap_str);
2811 free(keymap_str);
2812
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002813 return xkb_info;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002814
2815err_dev_zero:
2816 close(xkb_info->keymap_fd);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002817err_keymap_str:
2818 free(keymap_str);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002819err_keymap:
Ran Benita2e1968f2014-08-19 23:59:51 +03002820 xkb_keymap_unref(xkb_info->keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002821 free(xkb_info);
2822 return NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002823}
2824
2825static int
2826weston_compositor_build_global_keymap(struct weston_compositor *ec)
2827{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002828 struct xkb_keymap *keymap;
2829
2830 if (ec->xkb_info != NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002831 return 0;
2832
Ran Benita2e1968f2014-08-19 23:59:51 +03002833 keymap = xkb_keymap_new_from_names(ec->xkb_context,
2834 &ec->xkb_names,
2835 0);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002836 if (keymap == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002837 weston_log("failed to compile global XKB keymap\n");
2838 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
2839 "options %s\n",
2840 ec->xkb_names.rules, ec->xkb_names.model,
2841 ec->xkb_names.layout, ec->xkb_names.variant,
2842 ec->xkb_names.options);
2843 return -1;
2844 }
2845
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002846 ec->xkb_info = weston_xkb_info_create(keymap);
Rui Matos73d93952013-10-24 19:28:41 +02002847 xkb_keymap_unref(keymap);
Stefan Schmidtfda26522013-09-17 10:54:09 +01002848 if (ec->xkb_info == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002849 return -1;
2850
2851 return 0;
2852}
Rob Bradford382ff462013-06-24 16:52:45 +01002853#else
Giulio Camuffo0358af42016-06-02 21:48:08 +03002854WL_EXPORT int
2855weston_compositor_set_xkb_rule_names(struct weston_compositor *ec,
2856 struct xkb_rule_names *names)
Rob Bradford382ff462013-06-24 16:52:45 +01002857{
2858 return 0;
2859}
2860
2861void
2862weston_compositor_xkb_destroy(struct weston_compositor *ec)
2863{
2864}
2865#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002866
Rui Matos65196bc2013-10-10 19:44:19 +02002867WL_EXPORT void
2868weston_seat_update_keymap(struct weston_seat *seat, struct xkb_keymap *keymap)
2869{
Derek Foreman1281a362015-07-31 16:55:32 -05002870 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2871
2872 if (!keyboard || !keymap)
Rui Matos65196bc2013-10-10 19:44:19 +02002873 return;
2874
2875#ifdef ENABLE_XKBCOMMON
2876 if (!seat->compositor->use_xkbcommon)
2877 return;
2878
Derek Foreman1281a362015-07-31 16:55:32 -05002879 xkb_keymap_unref(keyboard->pending_keymap);
2880 keyboard->pending_keymap = xkb_keymap_ref(keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02002881
Derek Foreman1281a362015-07-31 16:55:32 -05002882 if (keyboard->keys.size == 0)
Rui Matos65196bc2013-10-10 19:44:19 +02002883 update_keymap(seat);
2884#endif
2885}
2886
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002887WL_EXPORT int
2888weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
2889{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002890 struct weston_keyboard *keyboard;
2891
Derek Foreman1281a362015-07-31 16:55:32 -05002892 if (seat->keyboard_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002893 seat->keyboard_device_count += 1;
2894 if (seat->keyboard_device_count == 1)
2895 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002896 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002897 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002898
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002899 keyboard = weston_keyboard_create();
2900 if (keyboard == NULL) {
2901 weston_log("failed to allocate weston keyboard struct\n");
2902 return -1;
2903 }
2904
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002905#ifdef ENABLE_XKBCOMMON
2906 if (seat->compositor->use_xkbcommon) {
2907 if (keymap != NULL) {
2908 keyboard->xkb_info = weston_xkb_info_create(keymap);
2909 if (keyboard->xkb_info == NULL)
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02002910 goto err;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002911 } else {
2912 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02002913 goto err;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002914 keyboard->xkb_info = seat->compositor->xkb_info;
2915 keyboard->xkb_info->ref_count++;
2916 }
2917
2918 keyboard->xkb_state.state = xkb_state_new(keyboard->xkb_info->keymap);
2919 if (keyboard->xkb_state.state == NULL) {
2920 weston_log("failed to initialise XKB state\n");
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02002921 goto err;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002922 }
2923
2924 keyboard->xkb_state.leds = 0;
2925 }
2926#endif
2927
Derek Foreman1281a362015-07-31 16:55:32 -05002928 seat->keyboard_state = keyboard;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02002929 seat->keyboard_device_count = 1;
2930 keyboard->seat = seat;
2931
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002932 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002933
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002934 return 0;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02002935
2936err:
2937 if (keyboard->xkb_info)
2938 weston_xkb_info_destroy(keyboard->xkb_info);
2939 free(keyboard);
2940
2941 return -1;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002942}
2943
Jonas Ådahl91fed542013-12-03 09:14:27 +01002944static void
2945weston_keyboard_reset_state(struct weston_keyboard *keyboard)
2946{
2947 struct weston_seat *seat = keyboard->seat;
2948 struct xkb_state *state;
2949
2950#ifdef ENABLE_XKBCOMMON
2951 if (seat->compositor->use_xkbcommon) {
2952 state = xkb_state_new(keyboard->xkb_info->keymap);
2953 if (!state) {
2954 weston_log("failed to reset XKB state\n");
2955 return;
2956 }
2957 xkb_state_unref(keyboard->xkb_state.state);
2958 keyboard->xkb_state.state = state;
2959
2960 keyboard->xkb_state.leds = 0;
2961 }
2962#endif
2963
2964 seat->modifier_state = 0;
2965}
2966
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002967WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002968weston_seat_release_keyboard(struct weston_seat *seat)
2969{
2970 seat->keyboard_device_count--;
Derek Foremand621df22014-11-19 11:04:12 -06002971 assert(seat->keyboard_device_count >= 0);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002972 if (seat->keyboard_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05002973 weston_keyboard_set_focus(seat->keyboard_state, NULL);
2974 weston_keyboard_cancel_grab(seat->keyboard_state);
2975 weston_keyboard_reset_state(seat->keyboard_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002976 seat_send_updated_caps(seat);
2977 }
2978}
2979
2980WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002981weston_seat_init_pointer(struct weston_seat *seat)
2982{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002983 struct weston_pointer *pointer;
2984
Derek Foreman1281a362015-07-31 16:55:32 -05002985 if (seat->pointer_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002986 seat->pointer_device_count += 1;
2987 if (seat->pointer_device_count == 1)
2988 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002989 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002990 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002991
Giulio Camuffocdb4d292013-11-14 23:42:53 +01002992 pointer = weston_pointer_create(seat);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002993 if (pointer == NULL)
2994 return;
2995
Derek Foreman1281a362015-07-31 16:55:32 -05002996 seat->pointer_state = pointer;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002997 seat->pointer_device_count = 1;
2998 pointer->seat = seat;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002999
3000 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003001}
3002
3003WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003004weston_seat_release_pointer(struct weston_seat *seat)
3005{
Derek Foreman1281a362015-07-31 16:55:32 -05003006 struct weston_pointer *pointer = seat->pointer_state;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003007
3008 seat->pointer_device_count--;
3009 if (seat->pointer_device_count == 0) {
Derek Foremanf9318d12015-05-11 15:40:11 -05003010 weston_pointer_clear_focus(pointer);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003011 weston_pointer_cancel_grab(pointer);
Jonas Ådahl630bae82013-10-17 23:04:06 +02003012
Jonas Ådahla4932742013-10-17 23:04:07 +02003013 if (pointer->sprite)
3014 pointer_unmap_sprite(pointer);
3015
Jonas Ådahl3e12e632013-12-02 22:05:05 +01003016 weston_pointer_reset_state(pointer);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003017 seat_send_updated_caps(seat);
Derek Foremanfd5ca512015-01-07 15:00:25 -06003018
3019 /* seat->pointer is intentionally not destroyed so that
3020 * a newly attached pointer on this seat will retain
3021 * the previous cursor co-ordinates.
3022 */
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003023 }
3024}
3025
3026WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003027weston_seat_init_touch(struct weston_seat *seat)
3028{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003029 struct weston_touch *touch;
3030
Derek Foreman1281a362015-07-31 16:55:32 -05003031 if (seat->touch_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003032 seat->touch_device_count += 1;
3033 if (seat->touch_device_count == 1)
3034 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003035 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003036 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003037
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003038 touch = weston_touch_create();
3039 if (touch == NULL)
3040 return;
3041
Derek Foreman1281a362015-07-31 16:55:32 -05003042 seat->touch_state = touch;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003043 seat->touch_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003044 touch->seat = seat;
3045
3046 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003047}
3048
3049WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003050weston_seat_release_touch(struct weston_seat *seat)
3051{
3052 seat->touch_device_count--;
3053 if (seat->touch_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05003054 weston_touch_set_focus(seat->touch_state, NULL);
3055 weston_touch_cancel_grab(seat->touch_state);
3056 weston_touch_reset_state(seat->touch_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003057 seat_send_updated_caps(seat);
3058 }
3059}
3060
3061WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003062weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
3063 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003064{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003065 memset(seat, 0, sizeof *seat);
3066
Kristian Høgsberge3148752013-05-06 23:19:49 -04003067 seat->selection_data_source = NULL;
3068 wl_list_init(&seat->base_resource_list);
3069 wl_signal_init(&seat->selection_signal);
3070 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04003071 wl_signal_init(&seat->destroy_signal);
Jason Ekstranda4ab5422014-04-02 19:53:45 -05003072 wl_signal_init(&seat->updated_caps_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003073
Peter Hutterer87743e92016-01-18 16:38:22 +10003074 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 5,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003075 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003076
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003077 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003078 seat->modifier_state = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003079 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003080
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003081 wl_list_insert(ec->seat_list.prev, &seat->link);
3082
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003083 clipboard_create(seat);
3084
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003085 wl_signal_emit(&ec->seat_created_signal, seat);
3086}
3087
3088WL_EXPORT void
3089weston_seat_release(struct weston_seat *seat)
3090{
3091 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003092
Jonas Ådahl1afb2382014-01-03 19:46:51 +01003093 if (seat->saved_kbd_focus)
3094 wl_list_remove(&seat->saved_kbd_focus_listener.link);
3095
Derek Foreman1281a362015-07-31 16:55:32 -05003096 if (seat->pointer_state)
3097 weston_pointer_destroy(seat->pointer_state);
3098 if (seat->keyboard_state)
3099 weston_keyboard_destroy(seat->keyboard_state);
3100 if (seat->touch_state)
3101 weston_touch_destroy(seat->touch_state);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003102
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003103 free (seat->seat_name);
3104
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003105 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04003106
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003107 wl_signal_emit(&seat->destroy_signal, seat);
3108}
Derek Foreman1281a362015-07-31 16:55:32 -05003109
3110/** Get a seat's keyboard pointer
3111 *
3112 * \param seat The seat to query
3113 * \return The seat's keyboard pointer, or NULL if no keyboard is present
3114 *
3115 * The keyboard pointer for a seat isn't freed when all keyboards are removed,
3116 * so it should only be used when the seat's keyboard_device_count is greater
3117 * than zero. This function does that test and only returns a pointer
3118 * when a keyboard is present.
3119 */
3120WL_EXPORT struct weston_keyboard *
3121weston_seat_get_keyboard(struct weston_seat *seat)
3122{
3123 if (!seat)
3124 return NULL;
3125
3126 if (seat->keyboard_device_count)
3127 return seat->keyboard_state;
3128
3129 return NULL;
3130}
3131
3132/** Get a seat's pointer pointer
3133 *
3134 * \param seat The seat to query
3135 * \return The seat's pointer pointer, or NULL if no pointer device is present
3136 *
3137 * The pointer pointer for a seat isn't freed when all mice are removed,
3138 * so it should only be used when the seat's pointer_device_count is greater
3139 * than zero. This function does that test and only returns a pointer
3140 * when a pointing device is present.
3141 */
3142WL_EXPORT struct weston_pointer *
3143weston_seat_get_pointer(struct weston_seat *seat)
3144{
3145 if (!seat)
3146 return NULL;
3147
3148 if (seat->pointer_device_count)
3149 return seat->pointer_state;
3150
3151 return NULL;
3152}
3153
Jonas Ådahld3414f22016-07-22 17:56:31 +08003154static const struct zwp_locked_pointer_v1_interface locked_pointer_interface;
3155static const struct zwp_confined_pointer_v1_interface confined_pointer_interface;
3156
3157static enum pointer_constraint_type
3158pointer_constraint_get_type(struct weston_pointer_constraint *constraint)
3159{
3160 if (wl_resource_instance_of(constraint->resource,
3161 &zwp_locked_pointer_v1_interface,
3162 &locked_pointer_interface)) {
3163 return POINTER_CONSTRAINT_TYPE_LOCK;
3164 } else if (wl_resource_instance_of(constraint->resource,
3165 &zwp_confined_pointer_v1_interface,
3166 &confined_pointer_interface)) {
3167 return POINTER_CONSTRAINT_TYPE_CONFINE;
3168 }
3169
3170 abort();
3171 return 0;
3172}
3173
3174static void
3175pointer_constraint_notify_activated(struct weston_pointer_constraint *constraint)
3176{
3177 struct wl_resource *resource = constraint->resource;
3178
3179 switch (pointer_constraint_get_type(constraint)) {
3180 case POINTER_CONSTRAINT_TYPE_LOCK:
3181 zwp_locked_pointer_v1_send_locked(resource);
3182 break;
3183 case POINTER_CONSTRAINT_TYPE_CONFINE:
3184 zwp_confined_pointer_v1_send_confined(resource);
3185 break;
3186 }
3187}
3188
3189static void
3190pointer_constraint_notify_deactivated(struct weston_pointer_constraint *constraint)
3191{
3192 struct wl_resource *resource = constraint->resource;
3193
3194 switch (pointer_constraint_get_type(constraint)) {
3195 case POINTER_CONSTRAINT_TYPE_LOCK:
3196 zwp_locked_pointer_v1_send_unlocked(resource);
3197 break;
3198 case POINTER_CONSTRAINT_TYPE_CONFINE:
3199 zwp_confined_pointer_v1_send_unconfined(resource);
3200 break;
3201 }
3202}
3203
3204static struct weston_pointer_constraint *
3205get_pointer_constraint_for_pointer(struct weston_surface *surface,
3206 struct weston_pointer *pointer)
3207{
3208 struct weston_pointer_constraint *constraint;
3209
3210 wl_list_for_each(constraint, &surface->pointer_constraints, link) {
3211 if (constraint->pointer == pointer)
3212 return constraint;
3213 }
3214
3215 return NULL;
3216}
3217
Derek Foreman1281a362015-07-31 16:55:32 -05003218/** Get a seat's touch pointer
3219 *
3220 * \param seat The seat to query
3221 * \return The seat's touch pointer, or NULL if no touch device is present
3222 *
3223 * The touch pointer for a seat isn't freed when all touch devices are removed,
3224 * so it should only be used when the seat's touch_device_count is greater
3225 * than zero. This function does that test and only returns a pointer
3226 * when a touch device is present.
3227 */
3228WL_EXPORT struct weston_touch *
3229weston_seat_get_touch(struct weston_seat *seat)
3230{
3231 if (!seat)
3232 return NULL;
3233
3234 if (seat->touch_device_count)
3235 return seat->touch_state;
3236
3237 return NULL;
3238}
Bryce Harrington24f917e2016-06-29 19:04:07 -07003239
3240/** Sets the keyboard focus to the given surface
3241 *
3242 * \param seat The seat to query
3243 */
3244WL_EXPORT void
3245weston_seat_set_keyboard_focus(struct weston_seat *seat,
3246 struct weston_surface *surface)
3247{
3248 struct weston_compositor *compositor = seat->compositor;
3249 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003250 struct weston_surface_activation_data activation_data;
Bryce Harrington24f917e2016-06-29 19:04:07 -07003251
Jonas Ådahlef8e1c32016-03-15 20:28:51 +08003252 if (keyboard && keyboard->focus != surface) {
Bryce Harrington24f917e2016-06-29 19:04:07 -07003253 weston_keyboard_set_focus(keyboard, surface);
3254 wl_data_device_set_keyboard_focus(seat);
3255 }
3256
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003257 inc_activate_serial(compositor);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003258
3259 activation_data = (struct weston_surface_activation_data) {
3260 .surface = surface,
3261 .seat = seat,
3262 };
3263 wl_signal_emit(&compositor->activate_signal, &activation_data);
Bryce Harrington24f917e2016-06-29 19:04:07 -07003264}
Jonas Ådahl30d61d82014-10-22 21:21:17 +02003265
Jonas Ådahld3414f22016-07-22 17:56:31 +08003266static void
3267enable_pointer_constraint(struct weston_pointer_constraint *constraint,
3268 struct weston_view *view)
3269{
3270 assert(constraint->view == NULL);
3271 constraint->view = view;
3272 pointer_constraint_notify_activated(constraint);
3273 weston_pointer_start_grab(constraint->pointer, &constraint->grab);
3274 wl_list_remove(&constraint->surface_destroy_listener.link);
3275 wl_list_init(&constraint->surface_destroy_listener.link);
3276}
3277
3278static bool
3279is_pointer_constraint_enabled(struct weston_pointer_constraint *constraint)
3280{
3281 return constraint->view != NULL;
3282}
3283
3284static void
3285weston_pointer_constraint_disable(struct weston_pointer_constraint *constraint)
3286{
3287 constraint->view = NULL;
3288 pointer_constraint_notify_deactivated(constraint);
3289 weston_pointer_end_grab(constraint->grab.pointer);
3290}
3291
3292void
3293weston_pointer_constraint_destroy(struct weston_pointer_constraint *constraint)
3294{
3295 if (is_pointer_constraint_enabled(constraint))
3296 weston_pointer_constraint_disable(constraint);
3297
3298 wl_list_remove(&constraint->pointer_destroy_listener.link);
3299 wl_list_remove(&constraint->surface_destroy_listener.link);
3300 wl_list_remove(&constraint->surface_commit_listener.link);
3301 wl_list_remove(&constraint->surface_activate_listener.link);
3302
3303 wl_resource_set_user_data(constraint->resource, NULL);
3304 pixman_region32_fini(&constraint->region);
3305 wl_list_remove(&constraint->link);
3306 free(constraint);
3307}
3308
3309static void
3310disable_pointer_constraint(struct weston_pointer_constraint *constraint)
3311{
3312 switch (constraint->lifetime) {
3313 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT:
3314 weston_pointer_constraint_destroy(constraint);
3315 break;
3316 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT:
3317 weston_pointer_constraint_disable(constraint);
3318 break;
3319 }
3320}
3321
3322static bool
3323is_within_constraint_region(struct weston_pointer_constraint *constraint,
3324 wl_fixed_t sx, wl_fixed_t sy)
3325{
3326 struct weston_surface *surface = constraint->surface;
3327 pixman_region32_t constraint_region;
3328 bool result;
3329
3330 pixman_region32_init(&constraint_region);
3331 pixman_region32_intersect(&constraint_region,
3332 &surface->input,
3333 &constraint->region);
3334 result = pixman_region32_contains_point(&constraint_region,
3335 wl_fixed_to_int(sx),
3336 wl_fixed_to_int(sy),
3337 NULL);
3338 pixman_region32_fini(&constraint_region);
3339
3340 return result;
3341}
3342
3343static void
3344maybe_enable_pointer_constraint(struct weston_pointer_constraint *constraint)
3345{
3346 struct weston_surface *surface = constraint->surface;
3347 struct weston_view *vit;
3348 struct weston_view *view = NULL;
3349 struct weston_pointer *pointer = constraint->pointer;
3350 struct weston_keyboard *keyboard;
3351 struct weston_seat *seat = pointer->seat;
3352 int32_t x, y;
3353
3354 /* Postpone if no view of the surface was most recently clicked. */
3355 wl_list_for_each(vit, &surface->views, surface_link) {
3356 if (vit->click_to_activate_serial ==
3357 surface->compositor->activate_serial) {
3358 view = vit;
3359 }
3360 }
3361 if (view == NULL)
3362 return;
3363
3364 /* Postpone if surface doesn't have keyboard focus. */
3365 keyboard = weston_seat_get_keyboard(seat);
3366 if (!keyboard || keyboard->focus != surface)
3367 return;
3368
3369 /* Postpone constraint if the pointer is not within the
3370 * constraint region.
3371 */
3372 weston_view_from_global(view,
3373 wl_fixed_to_int(pointer->x),
3374 wl_fixed_to_int(pointer->y),
3375 &x, &y);
3376 if (!is_within_constraint_region(constraint,
3377 wl_fixed_from_int(x),
3378 wl_fixed_from_int(y)))
3379 return;
3380
3381 enable_pointer_constraint(constraint, view);
3382}
3383
3384static void
3385locked_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
3386{
3387}
3388
3389static void
3390locked_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
3391 uint32_t time,
3392 struct weston_pointer_motion_event *event)
3393{
Quentin Glidiccde13452016-08-12 10:41:32 +02003394 pointer_send_relative_motion(grab->pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08003395}
3396
3397static void
3398locked_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
3399 uint32_t time,
3400 uint32_t button,
3401 uint32_t state_w)
3402{
3403 weston_pointer_send_button(grab->pointer, time, button, state_w);
3404}
3405
3406static void
3407locked_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
3408 uint32_t time,
3409 struct weston_pointer_axis_event *event)
3410{
3411 weston_pointer_send_axis(grab->pointer, time, event);
3412}
3413
3414static void
3415locked_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
3416 uint32_t source)
3417{
3418 weston_pointer_send_axis_source(grab->pointer, source);
3419}
3420
3421static void
3422locked_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
3423{
3424 weston_pointer_send_frame(grab->pointer);
3425}
3426
3427static void
3428locked_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
3429{
3430 struct weston_pointer_constraint *constraint =
3431 container_of(grab, struct weston_pointer_constraint, grab);
3432
3433 disable_pointer_constraint(constraint);
3434}
3435
3436static const struct weston_pointer_grab_interface
3437 locked_pointer_grab_interface = {
3438 locked_pointer_grab_pointer_focus,
3439 locked_pointer_grab_pointer_motion,
3440 locked_pointer_grab_pointer_button,
3441 locked_pointer_grab_pointer_axis,
3442 locked_pointer_grab_pointer_axis_source,
3443 locked_pointer_grab_pointer_frame,
3444 locked_pointer_grab_pointer_cancel,
3445};
3446
3447static void
3448pointer_constraint_constrain_resource_destroyed(struct wl_resource *resource)
3449{
3450 struct weston_pointer_constraint *constraint =
3451 wl_resource_get_user_data(resource);
3452
3453 if (!constraint)
3454 return;
3455
3456 weston_pointer_constraint_destroy(constraint);
3457}
3458
3459static void
3460pointer_constraint_surface_activate(struct wl_listener *listener, void *data)
3461{
3462 struct weston_surface_activation_data *activation = data;
3463 struct weston_pointer *pointer;
3464 struct weston_surface *focus = activation->surface;
3465 struct weston_pointer_constraint *constraint =
3466 container_of(listener, struct weston_pointer_constraint,
3467 surface_activate_listener);
3468 bool is_constraint_surface;
3469
3470 pointer = weston_seat_get_pointer(activation->seat);
3471 if (!pointer)
3472 return;
3473
3474 is_constraint_surface =
3475 get_pointer_constraint_for_pointer(focus, pointer) == constraint;
3476
3477 if (is_constraint_surface &&
3478 !is_pointer_constraint_enabled(constraint))
3479 maybe_enable_pointer_constraint(constraint);
3480 else if (!is_constraint_surface &&
3481 is_pointer_constraint_enabled(constraint))
3482 disable_pointer_constraint(constraint);
3483}
3484
3485static void
3486pointer_constraint_pointer_destroyed(struct wl_listener *listener, void *data)
3487{
3488 struct weston_pointer_constraint *constraint =
3489 container_of(listener, struct weston_pointer_constraint,
3490 pointer_destroy_listener);
3491
3492 weston_pointer_constraint_destroy(constraint);
3493}
3494
3495static void
3496pointer_constraint_surface_destroyed(struct wl_listener *listener, void *data)
3497{
3498 struct weston_pointer_constraint *constraint =
3499 container_of(listener, struct weston_pointer_constraint,
3500 surface_destroy_listener);
3501
3502 weston_pointer_constraint_destroy(constraint);
3503}
3504
3505static void
3506pointer_constraint_surface_committed(struct wl_listener *listener, void *data)
3507{
3508 struct weston_pointer_constraint *constraint =
3509 container_of(listener, struct weston_pointer_constraint,
3510 surface_commit_listener);
3511
3512 if (constraint->region_is_pending) {
3513 constraint->region_is_pending = false;
3514 pixman_region32_copy(&constraint->region,
3515 &constraint->region_pending);
3516 pixman_region32_fini(&constraint->region_pending);
3517 pixman_region32_init(&constraint->region_pending);
3518 }
3519
3520 if (constraint->hint_is_pending) {
3521 constraint->hint_is_pending = false;
3522
3523 constraint->hint_is_pending = true;
3524 constraint->hint_x = constraint->hint_x_pending;
3525 constraint->hint_y = constraint->hint_y_pending;
3526 }
3527
3528 if (pointer_constraint_get_type(constraint) ==
3529 POINTER_CONSTRAINT_TYPE_CONFINE &&
3530 is_pointer_constraint_enabled(constraint))
3531 maybe_warp_confined_pointer(constraint);
3532}
3533
3534static struct weston_pointer_constraint *
3535weston_pointer_constraint_create(struct weston_surface *surface,
3536 struct weston_pointer *pointer,
3537 struct weston_region *region,
3538 enum zwp_pointer_constraints_v1_lifetime lifetime,
3539 struct wl_resource *cr,
3540 const struct weston_pointer_grab_interface *grab_interface)
3541{
3542 struct weston_pointer_constraint *constraint;
3543
3544 constraint = zalloc(sizeof *constraint);
3545 if (!constraint)
3546 return NULL;
3547
3548 constraint->lifetime = lifetime;
3549 pixman_region32_init(&constraint->region);
3550 pixman_region32_init(&constraint->region_pending);
3551 wl_list_insert(&surface->pointer_constraints, &constraint->link);
3552 constraint->surface = surface;
3553 constraint->pointer = pointer;
3554 constraint->resource = cr;
3555 constraint->grab.interface = grab_interface;
3556 if (region) {
3557 pixman_region32_copy(&constraint->region,
3558 &region->region);
3559 } else {
3560 pixman_region32_fini(&constraint->region);
3561 region_init_infinite(&constraint->region);
3562 }
3563
3564 constraint->surface_activate_listener.notify =
3565 pointer_constraint_surface_activate;
3566 constraint->surface_destroy_listener.notify =
3567 pointer_constraint_surface_destroyed;
3568 constraint->surface_commit_listener.notify =
3569 pointer_constraint_surface_committed;
3570 constraint->pointer_destroy_listener.notify =
3571 pointer_constraint_pointer_destroyed;
3572
3573 wl_signal_add(&surface->compositor->activate_signal,
3574 &constraint->surface_activate_listener);
3575 wl_signal_add(&pointer->destroy_signal,
3576 &constraint->pointer_destroy_listener);
3577 wl_signal_add(&surface->destroy_signal,
3578 &constraint->surface_destroy_listener);
3579 wl_signal_add(&surface->commit_signal,
3580 &constraint->surface_commit_listener);
3581
3582 return constraint;
3583}
3584
3585static void
3586init_pointer_constraint(struct wl_resource *pointer_constraints_resource,
3587 uint32_t id,
3588 struct weston_surface *surface,
3589 struct weston_pointer *pointer,
3590 struct weston_region *region,
3591 enum zwp_pointer_constraints_v1_lifetime lifetime,
3592 const struct wl_interface *interface,
3593 const void *implementation,
3594 const struct weston_pointer_grab_interface *grab_interface)
3595{
3596 struct wl_client *client =
3597 wl_resource_get_client(pointer_constraints_resource);
3598 struct wl_resource *cr;
3599 struct weston_pointer_constraint *constraint;
3600
3601 if (get_pointer_constraint_for_pointer(surface, pointer)) {
3602 wl_resource_post_error(pointer_constraints_resource,
3603 ZWP_POINTER_CONSTRAINTS_V1_ERROR_ALREADY_CONSTRAINED,
3604 "the pointer has a lock/confine request on this surface");
3605 return;
3606 }
3607
3608 cr = wl_resource_create(client, interface,
3609 wl_resource_get_version(pointer_constraints_resource),
3610 id);
3611 if (cr == NULL) {
3612 wl_client_post_no_memory(client);
3613 return;
3614 }
3615
3616 constraint = weston_pointer_constraint_create(surface, pointer,
3617 region, lifetime,
3618 cr, grab_interface);
3619 if (constraint == NULL) {
3620 wl_client_post_no_memory(client);
3621 return;
3622 }
3623
3624 wl_resource_set_implementation(cr, implementation, constraint,
3625 pointer_constraint_constrain_resource_destroyed);
3626
3627 maybe_enable_pointer_constraint(constraint);
3628}
3629
3630static void
3631pointer_constraints_destroy(struct wl_client *client,
3632 struct wl_resource *resource)
3633{
3634 wl_resource_destroy(resource);
3635}
3636
3637static void
3638locked_pointer_destroy(struct wl_client *client,
3639 struct wl_resource *resource)
3640{
3641 struct weston_pointer_constraint *constraint =
3642 wl_resource_get_user_data(resource);
3643 wl_fixed_t x, y;
3644
3645 if (constraint && constraint->view && constraint->hint_is_pending &&
3646 is_within_constraint_region(constraint,
3647 constraint->hint_x,
3648 constraint->hint_y)) {
3649 weston_view_to_global_fixed(constraint->view,
3650 constraint->hint_x,
3651 constraint->hint_y,
3652 &x, &y);
3653 weston_pointer_move_to(constraint->pointer, x, y);
3654 }
3655 wl_resource_destroy(resource);
3656}
3657
3658static void
3659locked_pointer_set_cursor_position_hint(struct wl_client *client,
3660 struct wl_resource *resource,
3661 wl_fixed_t surface_x,
3662 wl_fixed_t surface_y)
3663{
3664 struct weston_pointer_constraint *constraint =
3665 wl_resource_get_user_data(resource);
3666
3667 /* Ignore a set cursor hint that was sent after the lock was cancelled.
3668 */
3669 if (!constraint ||
3670 !constraint->resource ||
3671 constraint->resource != resource)
3672 return;
3673
3674 constraint->hint_is_pending = true;
3675 constraint->hint_x_pending = surface_x;
3676 constraint->hint_y_pending = surface_y;
3677}
3678
3679static void
3680locked_pointer_set_region(struct wl_client *client,
3681 struct wl_resource *resource,
3682 struct wl_resource *region_resource)
3683{
3684 struct weston_pointer_constraint *constraint =
3685 wl_resource_get_user_data(resource);
3686 struct weston_region *region = region_resource ?
3687 wl_resource_get_user_data(region_resource) : NULL;
3688
3689 if (!constraint)
3690 return;
3691
3692 if (region) {
3693 pixman_region32_copy(&constraint->region_pending,
3694 &region->region);
3695 } else {
3696 pixman_region32_fini(&constraint->region_pending);
3697 region_init_infinite(&constraint->region_pending);
3698 }
3699 constraint->region_is_pending = true;
3700}
3701
3702
3703static const struct zwp_locked_pointer_v1_interface locked_pointer_interface = {
3704 locked_pointer_destroy,
3705 locked_pointer_set_cursor_position_hint,
3706 locked_pointer_set_region,
3707};
3708
3709static void
3710pointer_constraints_lock_pointer(struct wl_client *client,
3711 struct wl_resource *resource,
3712 uint32_t id,
3713 struct wl_resource *surface_resource,
3714 struct wl_resource *pointer_resource,
3715 struct wl_resource *region_resource,
3716 uint32_t lifetime)
3717{
3718 struct weston_surface *surface =
3719 wl_resource_get_user_data(surface_resource);
3720 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
3721 struct weston_region *region = region_resource ?
3722 wl_resource_get_user_data(region_resource) : NULL;
3723
3724 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
3725 &zwp_locked_pointer_v1_interface,
3726 &locked_pointer_interface,
3727 &locked_pointer_grab_interface);
3728}
3729
3730static void
3731confined_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
3732{
3733}
3734
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08003735static double
3736vec2d_cross_product(struct vec2d a, struct vec2d b)
3737{
3738 return a.x * b.y - a.y * b.x;
3739}
3740
3741static struct vec2d
3742vec2d_add(struct vec2d a, struct vec2d b)
3743{
3744 return (struct vec2d) {
3745 .x = a.x + b.x,
3746 .y = a.y + b.y,
3747 };
3748}
3749
3750static struct vec2d
3751vec2d_subtract(struct vec2d a, struct vec2d b)
3752{
3753 return (struct vec2d) {
3754 .x = a.x - b.x,
3755 .y = a.y - b.y,
3756 };
3757}
3758
3759static struct vec2d
3760vec2d_multiply_constant(double c, struct vec2d a)
3761{
3762 return (struct vec2d) {
3763 .x = c * a.x,
3764 .y = c * a.y,
3765 };
3766}
3767
3768static bool
3769lines_intersect(struct line *line1, struct line *line2,
3770 struct vec2d *intersection)
3771{
3772 struct vec2d p = line1->a;
3773 struct vec2d r = vec2d_subtract(line1->b, line1->a);
3774 struct vec2d q = line2->a;
3775 struct vec2d s = vec2d_subtract(line2->b, line2->a);
3776 double rxs;
3777 double sxr;
3778 double t;
3779 double u;
3780
3781 /*
3782 * The line (p, r) and (q, s) intersects where
3783 *
3784 * p + t r = q + u s
3785 *
3786 * Calculate t:
3787 *
3788 * (p + t r) × s = (q + u s) × s
3789 * p × s + t (r × s) = q × s + u (s × s)
3790 * p × s + t (r × s) = q × s
3791 * t (r × s) = q × s - p × s
3792 * t (r × s) = (q - p) × s
3793 * t = ((q - p) × s) / (r × s)
3794 *
3795 * Using the same method, for u we get:
3796 *
3797 * u = ((p - q) × r) / (s × r)
3798 */
3799
3800 rxs = vec2d_cross_product(r, s);
3801 sxr = vec2d_cross_product(s, r);
3802
3803 /* If r × s = 0 then the lines are either parallel or collinear. */
3804 if (fabs(rxs) < DBL_MIN)
3805 return false;
3806
3807 t = vec2d_cross_product(vec2d_subtract(q, p), s) / rxs;
3808 u = vec2d_cross_product(vec2d_subtract(p, q), r) / sxr;
3809
3810 /* The lines only intersect if 0 ≤ t ≤ 1 and 0 ≤ u ≤ 1. */
3811 if (t < 0.0 || t > 1.0 || u < 0.0 || u > 1.0)
3812 return false;
3813
3814 *intersection = vec2d_add(p, vec2d_multiply_constant(t, r));
3815 return true;
3816}
3817
3818static struct border *
3819add_border(struct wl_array *array,
3820 double x1, double y1,
3821 double x2, double y2,
3822 enum motion_direction blocking_dir)
3823{
3824 struct border *border = wl_array_add(array, sizeof *border);
3825
3826 *border = (struct border) {
3827 .line = (struct line) {
3828 .a = (struct vec2d) {
3829 .x = x1,
3830 .y = y1,
3831 },
3832 .b = (struct vec2d) {
3833 .x = x2,
3834 .y = y2,
3835 },
3836 },
3837 .blocking_dir = blocking_dir,
3838 };
3839
3840 return border;
3841}
3842
3843static int
3844compare_lines_x(const void *a, const void *b)
3845{
3846 const struct border *border_a = a;
3847 const struct border *border_b = b;
3848
3849
3850 if (border_a->line.a.x == border_b->line.a.x)
3851 return border_a->line.b.x < border_b->line.b.x;
3852 else
3853 return border_a->line.a.x > border_b->line.a.x;
3854}
3855
3856static void
3857add_non_overlapping_edges(pixman_box32_t *boxes,
3858 int band_above_start,
3859 int band_below_start,
3860 int band_below_end,
3861 struct wl_array *borders)
3862{
3863 int i;
3864 struct wl_array band_merge;
3865 struct border *border;
3866 struct border *prev_border;
3867 struct border *new_border;
3868
3869 wl_array_init(&band_merge);
3870
3871 /* Add bottom band of previous row, and top band of current row, and
3872 * sort them so lower left x coordinate comes first. If there are two
3873 * borders with the same left x coordinate, the wider one comes first.
3874 */
3875 for (i = band_above_start; i < band_below_start; i++) {
3876 pixman_box32_t *box = &boxes[i];
3877 add_border(&band_merge, box->x1, box->y2, box->x2, box->y2,
3878 MOTION_DIRECTION_POSITIVE_Y);
3879 }
3880 for (i = band_below_start; i < band_below_end; i++) {
3881 pixman_box32_t *box= &boxes[i];
3882 add_border(&band_merge, box->x1, box->y1, box->x2, box->y1,
3883 MOTION_DIRECTION_NEGATIVE_Y);
3884 }
3885 qsort(band_merge.data,
3886 band_merge.size / sizeof *border,
3887 sizeof *border,
3888 compare_lines_x);
3889
3890 /* Combine the two combined bands so that any overlapping border is
3891 * eliminated. */
3892 prev_border = NULL;
3893 wl_array_for_each(border, &band_merge) {
3894 assert(border->line.a.y == border->line.b.y);
3895 assert(!prev_border ||
3896 prev_border->line.a.y == border->line.a.y);
3897 assert(!prev_border ||
3898 (prev_border->line.a.x != border->line.a.x ||
3899 prev_border->line.b.x != border->line.b.x));
3900 assert(!prev_border ||
3901 prev_border->line.a.x <= border->line.a.x);
3902
3903 if (prev_border &&
3904 prev_border->line.a.x == border->line.a.x) {
3905 /*
3906 * ------------ +
3907 * ------- =
3908 * [ ]-----
3909 */
3910 prev_border->line.a.x = border->line.b.x;
3911 } else if (prev_border &&
3912 prev_border->line.b.x == border->line.b.x) {
3913 /*
3914 * ------------ +
3915 * ------ =
3916 * ------[ ]
3917 */
3918 prev_border->line.b.x = border->line.a.x;
3919 } else if (prev_border &&
3920 prev_border->line.b.x == border->line.a.x) {
3921 /*
3922 * -------- +
3923 * ------ =
3924 * --------------
3925 */
3926 prev_border->line.b.x = border->line.b.x;
3927 } else if (prev_border &&
3928 prev_border->line.b.x >= border->line.a.x) {
3929 /*
3930 * --------------- +
3931 * ------ =
3932 * -----[ ]----
3933 */
3934 new_border = add_border(borders,
3935 border->line.b.x,
3936 border->line.b.y,
3937 prev_border->line.b.x,
3938 prev_border->line.b.y,
3939 prev_border->blocking_dir);
3940 prev_border->line.b.x = border->line.a.x;
3941 prev_border = new_border;
3942 } else {
3943 assert(!prev_border ||
3944 prev_border->line.b.x < border->line.a.x);
3945 /*
3946 * First border or non-overlapping.
3947 *
3948 * ----- +
3949 * ----- =
3950 * ----- -----
3951 */
3952 new_border = wl_array_add(borders, sizeof *border);
3953 *new_border = *border;
3954 prev_border = new_border;
3955 }
3956 }
3957
3958 wl_array_release(&band_merge);
3959}
3960
3961static void
3962add_band_bottom_edges(pixman_box32_t *boxes,
3963 int band_start,
3964 int band_end,
3965 struct wl_array *borders)
3966{
3967 int i;
3968
3969 for (i = band_start; i < band_end; i++) {
3970 add_border(borders,
3971 boxes[i].x1, boxes[i].y2,
3972 boxes[i].x2, boxes[i].y2,
3973 MOTION_DIRECTION_POSITIVE_Y);
3974 }
3975}
3976
3977static void
3978region_to_outline(pixman_region32_t *region, struct wl_array *borders)
3979{
3980 pixman_box32_t *boxes;
3981 int num_boxes;
3982 int i;
3983 int top_most, bottom_most;
3984 int current_roof;
3985 int prev_top;
3986 int band_start, prev_band_start;
3987
3988 /*
3989 * Remove any overlapping lines from the set of rectangles. Note that
3990 * pixman regions are grouped as rows of rectangles, where rectangles
3991 * in one row never touch or overlap and are all of the same height.
3992 *
3993 * -------- --- -------- ---
3994 * | | | | | | | |
3995 * ----------====---- --- ----------- ----- ---
3996 * | | => | |
3997 * ----==========--------- ----- ----------
3998 * | | | |
3999 * ------------------- -------------------
4000 *
4001 */
4002
4003 boxes = pixman_region32_rectangles(region, &num_boxes);
4004 prev_top = 0;
4005 top_most = boxes[0].y1;
4006 current_roof = top_most;
4007 bottom_most = boxes[num_boxes - 1].y2;
4008 band_start = 0;
4009 prev_band_start = 0;
4010 for (i = 0; i < num_boxes; i++) {
4011 /* Detect if there is a vertical empty space, and add the lower
4012 * level of the previous band if so was the case. */
4013 if (i > 0 &&
4014 boxes[i].y1 != prev_top &&
4015 boxes[i].y1 != boxes[i - 1].y2) {
4016 current_roof = boxes[i].y1;
4017 add_band_bottom_edges(boxes,
4018 band_start,
4019 i,
4020 borders);
4021 }
4022
4023 /* Special case adding the last band, since it won't be handled
4024 * by the band change detection below. */
4025 if (boxes[i].y1 != current_roof && i == num_boxes - 1) {
4026 if (boxes[i].y1 != prev_top) {
4027 /* The last band is a single box, so we don't
4028 * have a prev_band_start to tell us when the
4029 * previous band started. */
4030 add_non_overlapping_edges(boxes,
4031 band_start,
4032 i,
4033 i + 1,
4034 borders);
4035 } else {
4036 add_non_overlapping_edges(boxes,
4037 prev_band_start,
4038 band_start,
4039 i + 1,
4040 borders);
4041 }
4042 }
4043
4044 /* Detect when passing a band and combine the top border of the
4045 * just passed band with the bottom band of the previous band.
4046 */
4047 if (boxes[i].y1 != top_most && boxes[i].y1 != prev_top) {
4048 /* Combine the two passed bands. */
4049 if (prev_top != current_roof) {
4050 add_non_overlapping_edges(boxes,
4051 prev_band_start,
4052 band_start,
4053 i,
4054 borders);
4055 }
4056
4057 prev_band_start = band_start;
4058 band_start = i;
4059 }
4060
4061 /* Add the top border if the box is part of the current roof. */
4062 if (boxes[i].y1 == current_roof) {
4063 add_border(borders,
4064 boxes[i].x1, boxes[i].y1,
4065 boxes[i].x2, boxes[i].y1,
4066 MOTION_DIRECTION_NEGATIVE_Y);
4067 }
4068
4069 /* Add the bottom border of the last band. */
4070 if (boxes[i].y2 == bottom_most) {
4071 add_border(borders,
4072 boxes[i].x1, boxes[i].y2,
4073 boxes[i].x2, boxes[i].y2,
4074 MOTION_DIRECTION_POSITIVE_Y);
4075 }
4076
4077 /* Always add the left border. */
4078 add_border(borders,
4079 boxes[i].x1, boxes[i].y1,
4080 boxes[i].x1, boxes[i].y2,
4081 MOTION_DIRECTION_NEGATIVE_X);
4082
4083 /* Always add the right border. */
4084 add_border(borders,
4085 boxes[i].x2, boxes[i].y1,
4086 boxes[i].x2, boxes[i].y2,
4087 MOTION_DIRECTION_POSITIVE_X);
4088
4089 prev_top = boxes[i].y1;
4090 }
4091}
4092
4093static bool
4094is_border_horizontal (struct border *border)
4095{
4096 return border->line.a.y == border->line.b.y;
4097}
4098
4099static bool
4100is_border_blocking_directions(struct border *border,
4101 uint32_t directions)
4102{
4103 /* Don't block parallel motions. */
4104 if (is_border_horizontal(border)) {
4105 if ((directions & (MOTION_DIRECTION_POSITIVE_Y |
4106 MOTION_DIRECTION_NEGATIVE_Y)) == 0)
4107 return false;
4108 } else {
4109 if ((directions & (MOTION_DIRECTION_POSITIVE_X |
4110 MOTION_DIRECTION_NEGATIVE_X)) == 0)
4111 return false;
4112 }
4113
4114 return (~border->blocking_dir & directions) != directions;
4115}
4116
4117static struct border *
4118get_closest_border(struct wl_array *borders,
4119 struct line *motion,
4120 uint32_t directions)
4121{
4122 struct border *border;
4123 struct vec2d intersection;
4124 struct vec2d delta;
4125 double distance_2;
4126 struct border *closest_border = NULL;
4127 double closest_distance_2 = DBL_MAX;
4128
4129 wl_array_for_each(border, borders) {
4130 if (!is_border_blocking_directions(border, directions))
4131 continue;
4132
4133 if (!lines_intersect(&border->line, motion, &intersection))
4134 continue;
4135
4136 delta = vec2d_subtract(intersection, motion->a);
4137 distance_2 = delta.x*delta.x + delta.y*delta.y;
4138 if (distance_2 < closest_distance_2) {
4139 closest_border = border;
4140 closest_distance_2 = distance_2;
4141 }
4142 }
4143
4144 return closest_border;
4145}
4146
4147static void
4148clamp_to_border(struct border *border,
4149 struct line *motion,
4150 uint32_t *motion_dir)
4151{
4152 /*
4153 * When clamping either rightward or downward motions, the motion needs
4154 * to be clamped so that the destination coordinate does not end up on
4155 * the border (see weston_pointer_clamp_event_to_region). Do this by
4156 * clamping such motions to the border minus the smallest possible
4157 * wl_fixed_t value.
4158 */
4159 if (is_border_horizontal(border)) {
4160 if (*motion_dir & MOTION_DIRECTION_POSITIVE_Y)
4161 motion->b.y = border->line.a.y - wl_fixed_to_double(1);
4162 else
4163 motion->b.y = border->line.a.y;
4164 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_Y |
4165 MOTION_DIRECTION_NEGATIVE_Y);
4166 } else {
4167 if (*motion_dir & MOTION_DIRECTION_POSITIVE_X)
4168 motion->b.x = border->line.a.x - wl_fixed_to_double(1);
4169 else
4170 motion->b.x = border->line.a.x;
4171 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_X |
4172 MOTION_DIRECTION_NEGATIVE_X);
4173 }
4174}
4175
4176static uint32_t
4177get_motion_directions(struct line *motion)
4178{
4179 uint32_t directions = 0;
4180
4181 if (motion->a.x < motion->b.x)
4182 directions |= MOTION_DIRECTION_POSITIVE_X;
4183 else if (motion->a.x > motion->b.x)
4184 directions |= MOTION_DIRECTION_NEGATIVE_X;
4185 if (motion->a.y < motion->b.y)
4186 directions |= MOTION_DIRECTION_POSITIVE_Y;
4187 else if (motion->a.y > motion->b.y)
4188 directions |= MOTION_DIRECTION_NEGATIVE_Y;
4189
4190 return directions;
4191}
4192
Jonas Ådahld3414f22016-07-22 17:56:31 +08004193static void
4194weston_pointer_clamp_event_to_region(struct weston_pointer *pointer,
4195 struct weston_pointer_motion_event *event,
4196 pixman_region32_t *region,
4197 wl_fixed_t *clamped_x,
4198 wl_fixed_t *clamped_y)
4199{
4200 wl_fixed_t x, y;
4201 wl_fixed_t sx, sy;
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004202 wl_fixed_t old_sx = pointer->sx;
4203 wl_fixed_t old_sy = pointer->sy;
4204 struct wl_array borders;
4205 struct line motion;
4206 struct border *closest_border;
4207 float new_x_f, new_y_f;
4208 uint32_t directions;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004209
4210 weston_pointer_motion_to_abs(pointer, event, &x, &y);
4211 weston_view_from_global_fixed(pointer->focus, x, y, &sx, &sy);
4212
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004213 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004214
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004215 /*
4216 * Generate borders given the confine region we are to use. The borders
4217 * are defined to be the outer region of the allowed area. This means
4218 * top/left borders are "within" the allowed area, while bottom/right
4219 * borders are outside. This needs to be considered when clamping
4220 * confined motion vectors.
4221 */
4222 region_to_outline(region, &borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004223
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004224 motion = (struct line) {
4225 .a = (struct vec2d) {
4226 .x = wl_fixed_to_double(old_sx),
4227 .y = wl_fixed_to_double(old_sy),
4228 },
4229 .b = (struct vec2d) {
4230 .x = wl_fixed_to_double(sx),
4231 .y = wl_fixed_to_double(sy),
4232 },
4233 };
4234 directions = get_motion_directions(&motion);
4235
4236 while (directions) {
4237 closest_border = get_closest_border(&borders,
4238 &motion,
4239 directions);
4240 if (closest_border)
4241 clamp_to_border(closest_border, &motion, &directions);
4242 else
4243 break;
4244 }
4245
4246 weston_view_to_global_float(pointer->focus,
4247 (float) motion.b.x, (float) motion.b.y,
4248 &new_x_f, &new_y_f);
4249 *clamped_x = wl_fixed_from_double(new_x_f);
4250 *clamped_y = wl_fixed_from_double(new_y_f);
4251
4252 wl_array_release(&borders);
4253}
4254
4255static double
4256point_to_border_distance_2(struct border *border, double x, double y)
4257{
4258 double orig_x, orig_y;
4259 double dx, dy;
4260
4261 if (is_border_horizontal(border)) {
4262 if (x < border->line.a.x)
4263 orig_x = border->line.a.x;
4264 else if (x > border->line.b.x)
4265 orig_x = border->line.b.x;
4266 else
4267 orig_x = x;
4268 orig_y = border->line.a.y;
4269 } else {
4270 if (y < border->line.a.y)
4271 orig_y = border->line.a.y;
4272 else if (y > border->line.b.y)
4273 orig_y = border->line.b.y;
4274 else
4275 orig_y = y;
4276 orig_x = border->line.a.x;
4277 }
4278
4279
4280 dx = fabs(orig_x - x);
4281 dy = fabs(orig_y - y);
4282 return dx*dx + dy*dy;
4283}
4284
4285static void
4286warp_to_behind_border(struct border *border, wl_fixed_t *sx, wl_fixed_t *sy)
4287{
4288 switch (border->blocking_dir) {
4289 case MOTION_DIRECTION_POSITIVE_X:
4290 case MOTION_DIRECTION_NEGATIVE_X:
4291 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_X)
4292 *sx = wl_fixed_from_double(border->line.a.x) - 1;
4293 else
4294 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4295 if (*sy < wl_fixed_from_double(border->line.a.y))
4296 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4297 else if (*sy > wl_fixed_from_double(border->line.b.y))
4298 *sy = wl_fixed_from_double(border->line.b.y) - 1;
4299 break;
4300 case MOTION_DIRECTION_POSITIVE_Y:
4301 case MOTION_DIRECTION_NEGATIVE_Y:
4302 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_Y)
4303 *sy = wl_fixed_from_double(border->line.a.y) - 1;
4304 else
4305 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4306 if (*sx < wl_fixed_from_double(border->line.a.x))
4307 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4308 else if (*sx > wl_fixed_from_double(border->line.b.x))
4309 *sx = wl_fixed_from_double(border->line.b.x) - 1;
4310 break;
4311 }
Jonas Ådahld3414f22016-07-22 17:56:31 +08004312}
4313
4314static void
4315maybe_warp_confined_pointer(struct weston_pointer_constraint *constraint)
4316{
4317 wl_fixed_t x;
4318 wl_fixed_t y;
4319 wl_fixed_t sx;
4320 wl_fixed_t sy;
4321
4322 weston_view_from_global_fixed(constraint->view,
4323 constraint->pointer->x,
4324 constraint->pointer->y,
4325 &sx,
4326 &sy);
4327
4328 if (!is_within_constraint_region(constraint, sx, sy)) {
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004329 double xf = wl_fixed_to_double(sx);
4330 double yf = wl_fixed_to_double(sy);
4331 pixman_region32_t confine_region;
4332 struct wl_array borders;
4333 struct border *border;
4334 double closest_distance_2 = DBL_MAX;
4335 struct border *closest_border = NULL;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004336
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004337 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004338
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004339 pixman_region32_init(&confine_region);
4340 pixman_region32_intersect(&confine_region,
4341 &constraint->view->surface->input,
4342 &constraint->region);
4343 region_to_outline(&confine_region, &borders);
4344 pixman_region32_fini(&confine_region);
4345
4346 wl_array_for_each(border, &borders) {
4347 double distance_2;
4348
4349 distance_2 = point_to_border_distance_2(border, xf, yf);
4350 if (distance_2 < closest_distance_2) {
4351 closest_border = border;
4352 closest_distance_2 = distance_2;
4353 }
4354 }
4355 assert(closest_border);
4356
4357 warp_to_behind_border(closest_border, &sx, &sy);
4358
4359 wl_array_release(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004360
4361 weston_view_to_global_fixed(constraint->view, sx, sy, &x, &y);
4362 weston_pointer_move_to(constraint->pointer, x, y);
4363 }
4364}
4365
4366static void
4367confined_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
4368 uint32_t time,
4369 struct weston_pointer_motion_event *event)
4370{
4371 struct weston_pointer_constraint *constraint =
4372 container_of(grab, struct weston_pointer_constraint, grab);
4373 struct weston_pointer *pointer = grab->pointer;
4374 struct weston_surface *surface;
4375 wl_fixed_t x, y;
4376 wl_fixed_t old_sx = pointer->sx;
4377 wl_fixed_t old_sy = pointer->sy;
4378 pixman_region32_t confine_region;
4379
4380 assert(pointer->focus);
4381 assert(pointer->focus->surface == constraint->surface);
4382
4383 surface = pointer->focus->surface;
4384
4385 pixman_region32_init(&confine_region);
4386 pixman_region32_intersect(&confine_region,
4387 &surface->input,
4388 &constraint->region);
4389 weston_pointer_clamp_event_to_region(pointer, event,
4390 &confine_region, &x, &y);
4391 weston_pointer_move_to(pointer, x, y);
4392 pixman_region32_fini(&confine_region);
4393
4394 weston_view_from_global_fixed(pointer->focus, x, y,
4395 &pointer->sx, &pointer->sy);
4396
4397 if (old_sx != pointer->sx || old_sy != pointer->sy) {
Quentin Glidiccde13452016-08-12 10:41:32 +02004398 pointer_send_motion(pointer, time,
4399 pointer->sx, pointer->sy);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004400 }
4401
Quentin Glidiccde13452016-08-12 10:41:32 +02004402 pointer_send_relative_motion(pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004403}
4404
4405static void
4406confined_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
4407 uint32_t time,
4408 uint32_t button,
4409 uint32_t state_w)
4410{
4411 weston_pointer_send_button(grab->pointer, time, button, state_w);
4412}
4413
4414static void
4415confined_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
4416 uint32_t time,
4417 struct weston_pointer_axis_event *event)
4418{
4419 weston_pointer_send_axis(grab->pointer, time, event);
4420}
4421
4422static void
4423confined_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
4424 uint32_t source)
4425{
4426 weston_pointer_send_axis_source(grab->pointer, source);
4427}
4428
4429static void
4430confined_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
4431{
4432 weston_pointer_send_frame(grab->pointer);
4433}
4434
4435static void
4436confined_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
4437{
4438 struct weston_pointer_constraint *constraint =
4439 container_of(grab, struct weston_pointer_constraint, grab);
4440
4441 disable_pointer_constraint(constraint);
4442
4443 /* If this is a persistent constraint, re-add the surface destroy signal
4444 * listener only if we are currently not destroying the surface. */
4445 switch (constraint->lifetime) {
4446 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT:
4447 if (constraint->surface->resource)
4448 wl_signal_add(&constraint->surface->destroy_signal,
4449 &constraint->surface_destroy_listener);
4450 break;
4451 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT:
4452 break;
4453 }
4454}
4455
4456static const struct weston_pointer_grab_interface
4457 confined_pointer_grab_interface = {
4458 confined_pointer_grab_pointer_focus,
4459 confined_pointer_grab_pointer_motion,
4460 confined_pointer_grab_pointer_button,
4461 confined_pointer_grab_pointer_axis,
4462 confined_pointer_grab_pointer_axis_source,
4463 confined_pointer_grab_pointer_frame,
4464 confined_pointer_grab_pointer_cancel,
4465};
4466
4467static void
4468confined_pointer_destroy(struct wl_client *client,
4469 struct wl_resource *resource)
4470{
4471 wl_resource_destroy(resource);
4472}
4473
4474static void
4475confined_pointer_set_region(struct wl_client *client,
4476 struct wl_resource *resource,
4477 struct wl_resource *region_resource)
4478{
4479 struct weston_pointer_constraint *constraint =
4480 wl_resource_get_user_data(resource);
4481 struct weston_region *region = region_resource ?
4482 wl_resource_get_user_data(region_resource) : NULL;
4483
4484 if (!constraint)
4485 return;
4486
4487 if (region) {
4488 pixman_region32_copy(&constraint->region_pending,
4489 &region->region);
4490 } else {
4491 pixman_region32_fini(&constraint->region_pending);
4492 region_init_infinite(&constraint->region_pending);
4493 }
4494 constraint->region_is_pending = true;
4495}
4496
4497static const struct zwp_confined_pointer_v1_interface confined_pointer_interface = {
4498 confined_pointer_destroy,
4499 confined_pointer_set_region,
4500};
4501
4502static void
4503pointer_constraints_confine_pointer(struct wl_client *client,
4504 struct wl_resource *resource,
4505 uint32_t id,
4506 struct wl_resource *surface_resource,
4507 struct wl_resource *pointer_resource,
4508 struct wl_resource *region_resource,
4509 uint32_t lifetime)
4510{
4511 struct weston_surface *surface =
4512 wl_resource_get_user_data(surface_resource);
4513 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
4514 struct weston_region *region = region_resource ?
4515 wl_resource_get_user_data(region_resource) : NULL;
4516
4517 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
4518 &zwp_confined_pointer_v1_interface,
4519 &confined_pointer_interface,
4520 &confined_pointer_grab_interface);
4521}
4522
4523static const struct zwp_pointer_constraints_v1_interface pointer_constraints_interface = {
4524 pointer_constraints_destroy,
4525 pointer_constraints_lock_pointer,
4526 pointer_constraints_confine_pointer,
4527};
4528
4529static void
4530bind_pointer_constraints(struct wl_client *client, void *data,
4531 uint32_t version, uint32_t id)
4532{
4533 struct wl_resource *resource;
4534
4535 resource = wl_resource_create(client,
4536 &zwp_pointer_constraints_v1_interface,
4537 1, id);
4538
4539 wl_resource_set_implementation(resource, &pointer_constraints_interface,
4540 NULL, NULL);
4541}
4542
Jonas Ådahl30d61d82014-10-22 21:21:17 +02004543int
4544weston_input_init(struct weston_compositor *compositor)
4545{
4546 if (!wl_global_create(compositor->wl_display,
4547 &zwp_relative_pointer_manager_v1_interface, 1,
4548 compositor, bind_relative_pointer_manager))
4549 return -1;
4550
Jonas Ådahld3414f22016-07-22 17:56:31 +08004551 if (!wl_global_create(compositor->wl_display,
4552 &zwp_pointer_constraints_v1_interface, 1,
4553 NULL, bind_pointer_constraints))
4554 return -1;
4555
Jonas Ådahl30d61d82014-10-22 21:21:17 +02004556 return 0;
4557}