blob: 9697b4a6a192f9fd2957084749e78bcf08c6280d [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"
Jonas Ådahl30d61d82014-10-22 21:21:17 +020042#include "protocol/relative-pointer-unstable-v1-server-protocol.h"
Jonas Ådahld3414f22016-07-22 17:56:31 +080043#include "protocol/pointer-constraints-unstable-v1-server-protocol.h"
44
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
2017 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2018 keyboard->grab_serial =
2019 wl_display_get_serial(compositor->wl_display);
2020 keyboard->grab_time = time;
2021 keyboard->grab_key = key;
2022 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002023}
2024
2025WL_EXPORT void
2026notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002027 double x, double y)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002028{
Derek Foreman1281a362015-07-31 16:55:32 -05002029 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2030
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002031 if (output) {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002032 weston_pointer_move_to(pointer,
2033 wl_fixed_from_double(x),
2034 wl_fixed_from_double(y));
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002035 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002036 /* FIXME: We should call weston_pointer_set_focus(seat,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002037 * NULL) here, but somehow that breaks re-entry... */
2038 }
2039}
2040
2041static void
2042destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
2043{
2044 struct weston_seat *ws;
2045
2046 ws = container_of(listener, struct weston_seat,
2047 saved_kbd_focus_listener);
2048
2049 ws->saved_kbd_focus = NULL;
2050}
2051
2052WL_EXPORT void
2053notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
2054 enum weston_key_state_update update_state)
2055{
2056 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002057 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04002058 struct weston_surface *surface;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002059 uint32_t *k, serial;
2060
2061 serial = wl_display_next_serial(compositor->wl_display);
2062 wl_array_copy(&keyboard->keys, keys);
2063 wl_array_for_each(k, &keyboard->keys) {
2064 weston_compositor_idle_inhibit(compositor);
2065 if (update_state == STATE_UPDATE_AUTOMATIC)
2066 update_modifier_state(seat, serial, *k,
2067 WL_KEYBOARD_KEY_STATE_PRESSED);
2068 }
2069
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002070 surface = seat->saved_kbd_focus;
2071
2072 if (surface) {
2073 wl_list_remove(&seat->saved_kbd_focus_listener.link);
2074 weston_keyboard_set_focus(keyboard, surface);
2075 seat->saved_kbd_focus = NULL;
2076 }
2077}
2078
2079WL_EXPORT void
2080notify_keyboard_focus_out(struct weston_seat *seat)
2081{
2082 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002083 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2084 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002085 uint32_t *k, serial;
2086
2087 serial = wl_display_next_serial(compositor->wl_display);
2088 wl_array_for_each(k, &keyboard->keys) {
2089 weston_compositor_idle_release(compositor);
2090 update_modifier_state(seat, serial, *k,
2091 WL_KEYBOARD_KEY_STATE_RELEASED);
2092 }
2093
2094 seat->modifier_state = 0;
2095
2096 if (keyboard->focus) {
2097 seat->saved_kbd_focus = keyboard->focus;
2098 seat->saved_kbd_focus_listener.notify =
2099 destroy_device_saved_kbd_focus;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002100 wl_signal_add(&keyboard->focus->destroy_signal,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002101 &seat->saved_kbd_focus_listener);
2102 }
2103
2104 weston_keyboard_set_focus(keyboard, NULL);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002105 weston_keyboard_cancel_grab(keyboard);
Derek Foreman1281a362015-07-31 16:55:32 -05002106 if (pointer)
2107 weston_pointer_cancel_grab(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002108}
2109
Michael Fua2bb7912013-07-23 15:51:06 +08002110WL_EXPORT void
Derek Foreman4c93c082015-04-30 16:45:41 -05002111weston_touch_set_focus(struct weston_touch *touch, struct weston_view *view)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002112{
Neil Roberts96d790e2013-09-19 17:32:00 +01002113 struct wl_list *focus_resource_list;
2114
Derek Foreman4c93c082015-04-30 16:45:41 -05002115 focus_resource_list = &touch->focus_resource_list;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002116
Derek Foreman4c93c082015-04-30 16:45:41 -05002117 if (view && touch->focus &&
2118 touch->focus->surface == view->surface) {
2119 touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002120 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002121 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002122
Derek Foreman4c93c082015-04-30 16:45:41 -05002123 wl_list_remove(&touch->focus_resource_listener.link);
2124 wl_list_init(&touch->focus_resource_listener.link);
2125 wl_list_remove(&touch->focus_view_listener.link);
2126 wl_list_init(&touch->focus_view_listener.link);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01002127
Neil Roberts96d790e2013-09-19 17:32:00 +01002128 if (!wl_list_empty(focus_resource_list)) {
Derek Foreman4c93c082015-04-30 16:45:41 -05002129 move_resources(&touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002130 focus_resource_list);
2131 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002132
Jason Ekstranda7af7042013-10-12 22:38:11 -05002133 if (view) {
Derek Foreman362656b2014-09-04 10:23:05 -05002134 struct wl_client *surface_client;
2135
2136 if (!view->surface->resource) {
Derek Foreman4c93c082015-04-30 16:45:41 -05002137 touch->focus = NULL;
Derek Foreman362656b2014-09-04 10:23:05 -05002138 return;
2139 }
2140
2141 surface_client = wl_resource_get_client(view->surface->resource);
Neil Roberts96d790e2013-09-19 17:32:00 +01002142 move_resources_for_client(focus_resource_list,
Derek Foreman4c93c082015-04-30 16:45:41 -05002143 &touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002144 surface_client);
Giulio Camuffo576fe2a2013-11-20 18:00:24 +01002145 wl_resource_add_destroy_listener(view->surface->resource,
Derek Foreman4c93c082015-04-30 16:45:41 -05002146 &touch->focus_resource_listener);
2147 wl_signal_add(&view->destroy_signal, &touch->focus_view_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002148 }
Derek Foreman4c93c082015-04-30 16:45:41 -05002149 touch->focus = view;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002150}
2151
2152/**
2153 * notify_touch - emulates button touches and notifies surfaces accordingly.
2154 *
2155 * It assumes always the correct cycle sequence until it gets here: touch_down
2156 * → touch_update → ... → touch_update → touch_end. The driver is responsible
2157 * for sending along such order.
2158 *
2159 */
2160WL_EXPORT void
2161notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002162 double double_x, double double_y, int touch_type)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002163{
2164 struct weston_compositor *ec = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05002165 struct weston_touch *touch = weston_seat_get_touch(seat);
Kristian Høgsberge329f362013-05-06 22:19:57 -04002166 struct weston_touch_grab *grab = touch->grab;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002167 struct weston_view *ev;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002168 wl_fixed_t sx, sy;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02002169 wl_fixed_t x = wl_fixed_from_double(double_x);
2170 wl_fixed_t y = wl_fixed_from_double(double_y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002171
2172 /* Update grab's global coordinates. */
Neil Roberts306fe082013-10-03 16:43:06 +01002173 if (touch_id == touch->grab_touch_id && touch_type != WL_TOUCH_UP) {
2174 touch->grab_x = x;
2175 touch->grab_y = y;
2176 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002177
2178 switch (touch_type) {
2179 case WL_TOUCH_DOWN:
2180 weston_compositor_idle_inhibit(ec);
2181
Jonas Ådahl9484b692013-12-02 22:05:03 +01002182 touch->num_tp++;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002183
Jason Ekstranda7af7042013-10-12 22:38:11 -05002184 /* the first finger down picks the view, and all further go
2185 * to that view for the remainder of the touch session i.e.
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002186 * until all touch points are up again. */
Jonas Ådahl9484b692013-12-02 22:05:03 +01002187 if (touch->num_tp == 1) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002188 ev = weston_compositor_pick_view(ec, x, y, &sx, &sy);
Derek Foreman4c93c082015-04-30 16:45:41 -05002189 weston_touch_set_focus(touch, ev);
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002190 } else if (!touch->focus) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002191 /* Unexpected condition: We have non-initial touch but
2192 * there is no focused surface.
2193 */
Chris Michael3f607d32015-10-07 11:59:49 -04002194 weston_log("touch event received with %d points down "
Jonas Ådahl9484b692013-12-02 22:05:03 +01002195 "but no surface focused\n", touch->num_tp);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002196 return;
2197 }
2198
Derek Foreman99a6a2d2015-07-15 13:00:43 -05002199 weston_compositor_run_touch_binding(ec, touch,
Kristian Høgsbergc8964012014-02-05 14:25:18 -08002200 time, touch_type);
2201
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002202 grab->interface->down(grab, time, touch_id, x, y);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002203 if (touch->num_tp == 1) {
Rusty Lynchf1407ff2013-08-08 21:13:57 -07002204 touch->grab_serial =
2205 wl_display_get_serial(ec->wl_display);
Neil Roberts306fe082013-10-03 16:43:06 +01002206 touch->grab_touch_id = touch_id;
Rusty Lynchf1407ff2013-08-08 21:13:57 -07002207 touch->grab_time = time;
2208 touch->grab_x = x;
2209 touch->grab_y = y;
2210 }
2211
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002212 break;
2213 case WL_TOUCH_MOTION:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002214 ev = touch->focus;
2215 if (!ev)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002216 break;
2217
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03002218 grab->interface->motion(grab, time, touch_id, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002219 break;
2220 case WL_TOUCH_UP:
Kristian Høgsberga30e29a2014-01-08 22:29:20 -08002221 if (touch->num_tp == 0) {
2222 /* This can happen if we start out with one or
2223 * more fingers on the touch screen, in which
2224 * case we didn't get the corresponding down
2225 * event. */
2226 weston_log("unmatched touch up event\n");
2227 break;
2228 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002229 weston_compositor_idle_release(ec);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002230 touch->num_tp--;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002231
2232 grab->interface->up(grab, time, touch_id);
Jonas Ådahl9484b692013-12-02 22:05:03 +01002233 if (touch->num_tp == 0)
Derek Foreman4c93c082015-04-30 16:45:41 -05002234 weston_touch_set_focus(touch, NULL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002235 break;
2236 }
2237}
2238
Jonas Ådahl1679f232014-04-12 09:39:51 +02002239WL_EXPORT void
2240notify_touch_frame(struct weston_seat *seat)
2241{
Derek Foreman1281a362015-07-31 16:55:32 -05002242 struct weston_touch *touch = weston_seat_get_touch(seat);
Jonas Ådahl1679f232014-04-12 09:39:51 +02002243 struct weston_touch_grab *grab = touch->grab;
2244
2245 grab->interface->frame(grab);
2246}
2247
Derek Foreman3cc004a2015-11-06 15:56:09 -06002248WL_EXPORT void
2249notify_touch_cancel(struct weston_seat *seat)
2250{
2251 struct weston_touch *touch = weston_seat_get_touch(seat);
2252 struct weston_touch_grab *grab = touch->grab;
2253
2254 grab->interface->cancel(grab);
2255}
2256
Pekka Paalanen8274d902014-08-06 19:36:51 +03002257static int
2258pointer_cursor_surface_get_label(struct weston_surface *surface,
2259 char *buf, size_t len)
2260{
2261 return snprintf(buf, len, "cursor");
2262}
2263
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002264static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002265pointer_cursor_surface_committed(struct weston_surface *es,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002266 int32_t dx, int32_t dy)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002267{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002268 struct weston_pointer *pointer = es->committed_private;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002269 int x, y;
2270
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002271 if (es->width == 0)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002272 return;
2273
Jason Ekstranda7af7042013-10-12 22:38:11 -05002274 assert(es == pointer->sprite->surface);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002275
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002276 pointer->hotspot_x -= dx;
2277 pointer->hotspot_y -= dy;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002278
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002279 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
2280 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002281
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002282 weston_view_set_position(pointer->sprite, x, y);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002283
2284 empty_region(&es->pending.input);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02002285 empty_region(&es->input);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002286
2287 if (!weston_surface_is_mapped(es)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002288 weston_layer_entry_insert(&es->compositor->cursor_layer.view_list,
2289 &pointer->sprite->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002290 weston_view_update_transform(pointer->sprite);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002291 es->is_mapped = true;
2292 pointer->sprite->is_mapped = true;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002293 }
2294}
2295
2296static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002297pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2298 uint32_t serial, struct wl_resource *surface_resource,
2299 int32_t x, int32_t y)
2300{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002301 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002302 struct weston_surface *surface = NULL;
2303
2304 if (surface_resource)
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002305 surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002306
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002307 if (pointer->focus == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002308 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002309 /* pointer->focus->surface->resource can be NULL. Surfaces like the
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02002310 black_surface used in shell.c for fullscreen don't have
2311 a resource, but can still have focus */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002312 if (pointer->focus->surface->resource == NULL)
Giulio Camuffo1fd4b012013-06-20 18:13:07 +02002313 return;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002314 if (wl_resource_get_client(pointer->focus->surface->resource) != client)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002315 return;
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002316 if (pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002317 return;
2318
Derek Foreman4e53c532015-03-23 10:55:32 -05002319 if (!surface) {
2320 if (pointer->sprite)
2321 pointer_unmap_sprite(pointer);
2322 return;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002323 }
2324
Jonas Ådahlb4070242015-03-18 15:08:03 +08002325 if (pointer->sprite && pointer->sprite->surface == surface &&
2326 pointer->hotspot_x == x && pointer->hotspot_y == y)
2327 return;
2328
Derek Foreman4e53c532015-03-23 10:55:32 -05002329 if (!pointer->sprite || pointer->sprite->surface != surface) {
2330 if (weston_surface_set_role(surface, "wl_pointer-cursor",
2331 resource,
2332 WL_POINTER_ERROR_ROLE) < 0)
2333 return;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002334
Derek Foreman4e53c532015-03-23 10:55:32 -05002335 if (pointer->sprite)
2336 pointer_unmap_sprite(pointer);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002337
Derek Foreman4e53c532015-03-23 10:55:32 -05002338 wl_signal_add(&surface->destroy_signal,
2339 &pointer->sprite_destroy_listener);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002340
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002341 surface->committed = pointer_cursor_surface_committed;
2342 surface->committed_private = pointer;
Derek Foreman4e53c532015-03-23 10:55:32 -05002343 weston_surface_set_label_func(surface,
2344 pointer_cursor_surface_get_label);
2345 pointer->sprite = weston_view_create(surface);
2346 }
2347
Kristian Høgsberg195b8692013-05-08 15:02:05 -04002348 pointer->hotspot_x = x;
2349 pointer->hotspot_y = y;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002350
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +02002351 if (surface->buffer_ref.buffer) {
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002352 pointer_cursor_surface_committed(surface, 0, 0);
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +02002353 weston_view_schedule_repaint(pointer->sprite);
2354 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002355}
2356
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002357static void
2358pointer_release(struct wl_client *client, struct wl_resource *resource)
2359{
2360 wl_resource_destroy(resource);
2361}
2362
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002363static const struct wl_pointer_interface pointer_interface = {
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002364 pointer_set_cursor,
2365 pointer_release
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002366};
2367
2368static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002369seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2370 uint32_t id)
2371{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002372 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002373 /* We use the pointer_state directly, which means we'll
2374 * give a wl_pointer if the seat has ever had one - even though
2375 * the spec explicitly states that this request only takes effect
2376 * if the seat has the pointer capability.
2377 *
2378 * This prevents a race between the compositor sending new
2379 * capabilities and the client trying to use the old ones.
2380 */
2381 struct weston_pointer *pointer = seat->pointer_state;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002382 struct wl_resource *cr;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002383 struct weston_pointer_client *pointer_client;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002384
Derek Foreman1281a362015-07-31 16:55:32 -05002385 if (!pointer)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002386 return;
2387
Jason Ekstranda85118c2013-06-27 20:17:02 -05002388 cr = wl_resource_create(client, &wl_pointer_interface,
2389 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002390 if (cr == NULL) {
2391 wl_client_post_no_memory(client);
2392 return;
2393 }
2394
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002395 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
2396 if (!pointer_client) {
2397 wl_client_post_no_memory(client);
2398 return;
2399 }
2400
2401 wl_list_insert(&pointer_client->pointer_resources,
2402 wl_resource_get_link(cr));
Derek Foreman1281a362015-07-31 16:55:32 -05002403 wl_resource_set_implementation(cr, &pointer_interface, pointer,
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08002404 unbind_pointer_client_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002405
Derek Foreman1281a362015-07-31 16:55:32 -05002406 if (pointer->focus && pointer->focus->surface->resource &&
2407 wl_resource_get_client(pointer->focus->surface->resource) == client) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002408 wl_fixed_t sx, sy;
2409
Derek Foreman1281a362015-07-31 16:55:32 -05002410 weston_view_from_global_fixed(pointer->focus,
2411 pointer->x,
2412 pointer->y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002413 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002414
Neil Roberts96d790e2013-09-19 17:32:00 +01002415 wl_pointer_send_enter(cr,
Derek Foreman1281a362015-07-31 16:55:32 -05002416 pointer->focus_serial,
2417 pointer->focus->surface->resource,
Neil Roberts96d790e2013-09-19 17:32:00 +01002418 sx, sy);
Peter Hutterer87743e92016-01-18 16:38:22 +10002419 pointer_send_frame(cr);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002420 }
2421}
2422
2423static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002424keyboard_release(struct wl_client *client, struct wl_resource *resource)
2425{
2426 wl_resource_destroy(resource);
2427}
2428
2429static const struct wl_keyboard_interface keyboard_interface = {
2430 keyboard_release
2431};
2432
Derek Foreman280e7dd2014-10-03 13:13:42 -05002433static bool
Neil Roberts96d790e2013-09-19 17:32:00 +01002434should_send_modifiers_to_client(struct weston_seat *seat,
2435 struct wl_client *client)
2436{
Derek Foreman1281a362015-07-31 16:55:32 -05002437 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2438 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2439
2440 if (keyboard &&
2441 keyboard->focus &&
2442 keyboard->focus->resource &&
2443 wl_resource_get_client(keyboard->focus->resource) == client)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002444 return true;
Neil Roberts96d790e2013-09-19 17:32:00 +01002445
Derek Foreman1281a362015-07-31 16:55:32 -05002446 if (pointer &&
2447 pointer->focus &&
2448 pointer->focus->surface->resource &&
2449 wl_resource_get_client(pointer->focus->surface->resource) == client)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002450 return true;
Neil Roberts96d790e2013-09-19 17:32:00 +01002451
Derek Foreman280e7dd2014-10-03 13:13:42 -05002452 return false;
Neil Roberts96d790e2013-09-19 17:32:00 +01002453}
2454
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002455static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002456seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2457 uint32_t id)
2458{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002459 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002460 /* We use the keyboard_state directly, which means we'll
2461 * give a wl_keyboard if the seat has ever had one - even though
2462 * the spec explicitly states that this request only takes effect
2463 * if the seat has the keyboard capability.
2464 *
2465 * This prevents a race between the compositor sending new
2466 * capabilities and the client trying to use the old ones.
2467 */
2468 struct weston_keyboard *keyboard = seat->keyboard_state;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002469 struct wl_resource *cr;
2470
Derek Foreman345c9f32015-06-03 15:53:28 -05002471 if (!keyboard)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002472 return;
2473
Jason Ekstranda85118c2013-06-27 20:17:02 -05002474 cr = wl_resource_create(client, &wl_keyboard_interface,
2475 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002476 if (cr == NULL) {
2477 wl_client_post_no_memory(client);
2478 return;
2479 }
2480
Neil Roberts96d790e2013-09-19 17:32:00 +01002481 /* May be moved to focused list later by either
2482 * weston_keyboard_set_focus or directly if this client is already
2483 * focused */
Derek Foreman345c9f32015-06-03 15:53:28 -05002484 wl_list_insert(&keyboard->resource_list, wl_resource_get_link(cr));
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002485 wl_resource_set_implementation(cr, &keyboard_interface,
2486 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002487
Jonny Lamb66a41a02014-08-12 14:58:25 +02002488 if (wl_resource_get_version(cr) >= WL_KEYBOARD_REPEAT_INFO_SINCE_VERSION) {
2489 wl_keyboard_send_repeat_info(cr,
2490 seat->compositor->kb_repeat_rate,
2491 seat->compositor->kb_repeat_delay);
2492 }
Jasper St. Pierred8c6aeb2014-08-04 13:43:24 -04002493
Matt Roper01a92732013-06-24 16:52:44 +01002494 if (seat->compositor->use_xkbcommon) {
2495 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002496 keyboard->xkb_info->keymap_fd,
2497 keyboard->xkb_info->keymap_size);
Matt Roper01a92732013-06-24 16:52:44 +01002498 } else {
2499 int null_fd = open("/dev/null", O_RDONLY);
2500 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP,
2501 null_fd,
2502 0);
2503 close(null_fd);
2504 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002505
Neil Roberts96d790e2013-09-19 17:32:00 +01002506 if (should_send_modifiers_to_client(seat, client)) {
Derek Foreman345c9f32015-06-03 15:53:28 -05002507 send_modifiers_to_resource(keyboard,
Neil Roberts96d790e2013-09-19 17:32:00 +01002508 cr,
Derek Foreman345c9f32015-06-03 15:53:28 -05002509 keyboard->focus_serial);
Neil Roberts96d790e2013-09-19 17:32:00 +01002510 }
2511
Derek Foreman345c9f32015-06-03 15:53:28 -05002512 if (keyboard->focus && keyboard->focus->resource &&
2513 wl_resource_get_client(keyboard->focus->resource) == client) {
Neil Roberts96d790e2013-09-19 17:32:00 +01002514 struct weston_surface *surface =
Derek Foreman345c9f32015-06-03 15:53:28 -05002515 (struct weston_surface *)keyboard->focus;
Neil Roberts96d790e2013-09-19 17:32:00 +01002516
2517 wl_list_remove(wl_resource_get_link(cr));
Derek Foreman345c9f32015-06-03 15:53:28 -05002518 wl_list_insert(&keyboard->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002519 wl_resource_get_link(cr));
2520 wl_keyboard_send_enter(cr,
Derek Foreman345c9f32015-06-03 15:53:28 -05002521 keyboard->focus_serial,
Neil Roberts96d790e2013-09-19 17:32:00 +01002522 surface->resource,
Derek Foreman345c9f32015-06-03 15:53:28 -05002523 &keyboard->keys);
Neil Roberts96d790e2013-09-19 17:32:00 +01002524
2525 /* If this is the first keyboard resource for this
2526 * client... */
Derek Foreman345c9f32015-06-03 15:53:28 -05002527 if (keyboard->focus_resource_list.prev ==
Neil Roberts96d790e2013-09-19 17:32:00 +01002528 wl_resource_get_link(cr))
2529 wl_data_device_set_keyboard_focus(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002530 }
2531}
2532
2533static void
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002534touch_release(struct wl_client *client, struct wl_resource *resource)
2535{
2536 wl_resource_destroy(resource);
2537}
2538
2539static const struct wl_touch_interface touch_interface = {
2540 touch_release
2541};
2542
2543static void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002544seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2545 uint32_t id)
2546{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002547 struct weston_seat *seat = wl_resource_get_user_data(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002548 /* We use the touch_state directly, which means we'll
2549 * give a wl_touch if the seat has ever had one - even though
2550 * the spec explicitly states that this request only takes effect
2551 * if the seat has the touch capability.
2552 *
2553 * This prevents a race between the compositor sending new
2554 * capabilities and the client trying to use the old ones.
2555 */
2556 struct weston_touch *touch = seat->touch_state;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002557 struct wl_resource *cr;
2558
Derek Foreman1281a362015-07-31 16:55:32 -05002559 if (!touch)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002560 return;
2561
Jason Ekstranda85118c2013-06-27 20:17:02 -05002562 cr = wl_resource_create(client, &wl_touch_interface,
2563 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002564 if (cr == NULL) {
2565 wl_client_post_no_memory(client);
2566 return;
2567 }
2568
Derek Foreman1281a362015-07-31 16:55:32 -05002569 if (touch->focus &&
2570 wl_resource_get_client(touch->focus->surface->resource) == client) {
Chokshi, Mituld6697142015-10-09 08:28:47 +00002571 wl_list_insert(&touch->focus_resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002572 wl_resource_get_link(cr));
2573 } else {
Chokshi, Mituld6697142015-10-09 08:28:47 +00002574 wl_list_insert(&touch->resource_list,
Neil Roberts96d790e2013-09-19 17:32:00 +01002575 wl_resource_get_link(cr));
2576 }
Kristian Høgsberg69e25fc2013-08-13 20:11:02 +01002577 wl_resource_set_implementation(cr, &touch_interface,
2578 seat, unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002579}
2580
Quentin Glidicaab1d362016-03-13 17:49:08 +01002581static void
2582seat_release(struct wl_client *client, struct wl_resource *resource)
2583{
2584 wl_resource_destroy(resource);
2585}
2586
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002587static const struct wl_seat_interface seat_interface = {
2588 seat_get_pointer,
2589 seat_get_keyboard,
2590 seat_get_touch,
Quentin Glidicaab1d362016-03-13 17:49:08 +01002591 seat_release,
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002592};
2593
2594static void
2595bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2596{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002597 struct weston_seat *seat = data;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002598 struct wl_resource *resource;
2599 enum wl_seat_capability caps = 0;
2600
Jason Ekstranda85118c2013-06-27 20:17:02 -05002601 resource = wl_resource_create(client,
Derek Foreman1909c102015-11-26 14:17:47 -06002602 &wl_seat_interface, version, id);
Jason Ekstrand44a38632013-06-14 10:08:00 -05002603 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05002604 wl_resource_set_implementation(resource, &seat_interface, data,
2605 unbind_resource);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002606
Derek Foreman1281a362015-07-31 16:55:32 -05002607 if (weston_seat_get_pointer(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002608 caps |= WL_SEAT_CAPABILITY_POINTER;
Derek Foreman1281a362015-07-31 16:55:32 -05002609 if (weston_seat_get_keyboard(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002610 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
Derek Foreman1281a362015-07-31 16:55:32 -05002611 if (weston_seat_get_touch(seat))
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002612 caps |= WL_SEAT_CAPABILITY_TOUCH;
2613
2614 wl_seat_send_capabilities(resource, caps);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04002615 if (version >= WL_SEAT_NAME_SINCE_VERSION)
Rob Bradforde445ae62013-05-31 18:09:51 +01002616 wl_seat_send_name(resource, seat->seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002617}
2618
Jonas Ådahl30d61d82014-10-22 21:21:17 +02002619static void
2620relative_pointer_destroy(struct wl_client *client,
2621 struct wl_resource *resource)
2622{
2623 wl_resource_destroy(resource);
2624}
2625
2626static const struct zwp_relative_pointer_v1_interface relative_pointer_interface = {
2627 relative_pointer_destroy
2628};
2629
2630static void
2631relative_pointer_manager_destroy(struct wl_client *client,
2632 struct wl_resource *resource)
2633{
2634 wl_resource_destroy(resource);
2635}
2636
2637static void
2638relative_pointer_manager_get_relative_pointer(struct wl_client *client,
2639 struct wl_resource *resource,
2640 uint32_t id,
2641 struct wl_resource *pointer_resource)
2642{
2643 struct weston_pointer *pointer =
2644 wl_resource_get_user_data(pointer_resource);
2645 struct weston_pointer_client *pointer_client;
2646 struct wl_resource *cr;
2647
2648 cr = wl_resource_create(client, &zwp_relative_pointer_v1_interface,
2649 wl_resource_get_version(resource), id);
2650 if (cr == NULL) {
2651 wl_client_post_no_memory(client);
2652 return;
2653 }
2654
2655 pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
2656 if (!pointer_client) {
2657 wl_client_post_no_memory(client);
2658 return;
2659 }
2660
2661 wl_list_insert(&pointer_client->relative_pointer_resources,
2662 wl_resource_get_link(cr));
2663 wl_resource_set_implementation(cr, &relative_pointer_interface,
2664 pointer,
2665 unbind_pointer_client_resource);
2666}
2667
2668static const struct zwp_relative_pointer_manager_v1_interface relative_pointer_manager = {
2669 relative_pointer_manager_destroy,
2670 relative_pointer_manager_get_relative_pointer,
2671};
2672
2673static void
2674bind_relative_pointer_manager(struct wl_client *client, void *data,
2675 uint32_t version, uint32_t id)
2676{
2677 struct weston_compositor *compositor = data;
2678 struct wl_resource *resource;
2679
2680 resource = wl_resource_create(client,
2681 &zwp_relative_pointer_manager_v1_interface,
2682 1, id);
2683
2684 wl_resource_set_implementation(resource, &relative_pointer_manager,
2685 compositor,
2686 NULL);
2687}
2688
Rob Bradford382ff462013-06-24 16:52:45 +01002689#ifdef ENABLE_XKBCOMMON
Giulio Camuffo0358af42016-06-02 21:48:08 +03002690WL_EXPORT int
2691weston_compositor_set_xkb_rule_names(struct weston_compositor *ec,
2692 struct xkb_rule_names *names)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002693{
Rob Bradford382ff462013-06-24 16:52:45 +01002694 ec->use_xkbcommon = 1;
Matt Roper01a92732013-06-24 16:52:44 +01002695
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002696 if (ec->xkb_context == NULL) {
2697 ec->xkb_context = xkb_context_new(0);
2698 if (ec->xkb_context == NULL) {
2699 weston_log("failed to create XKB context\n");
2700 return -1;
2701 }
2702 }
2703
2704 if (names)
2705 ec->xkb_names = *names;
2706 if (!ec->xkb_names.rules)
2707 ec->xkb_names.rules = strdup("evdev");
2708 if (!ec->xkb_names.model)
2709 ec->xkb_names.model = strdup("pc105");
2710 if (!ec->xkb_names.layout)
2711 ec->xkb_names.layout = strdup("us");
2712
2713 return 0;
2714}
2715
Stefan Schmidtfda26522013-09-17 10:54:09 +01002716static void
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002717weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002718{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002719 if (--xkb_info->ref_count > 0)
2720 return;
2721
Ran Benitac9c74152014-08-19 23:59:52 +03002722 xkb_keymap_unref(xkb_info->keymap);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002723
2724 if (xkb_info->keymap_area)
2725 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2726 if (xkb_info->keymap_fd >= 0)
2727 close(xkb_info->keymap_fd);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002728 free(xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002729}
2730
2731void
2732weston_compositor_xkb_destroy(struct weston_compositor *ec)
2733{
Matt Roper01a92732013-06-24 16:52:44 +01002734 /*
2735 * If we're operating in raw keyboard mode, we never initialized
2736 * libxkbcommon so there's no cleanup to do either.
2737 */
2738 if (!ec->use_xkbcommon)
2739 return;
2740
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002741 free((char *) ec->xkb_names.rules);
2742 free((char *) ec->xkb_names.model);
2743 free((char *) ec->xkb_names.layout);
2744 free((char *) ec->xkb_names.variant);
2745 free((char *) ec->xkb_names.options);
Stefan Schmidtfda26522013-09-17 10:54:09 +01002746
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002747 if (ec->xkb_info)
2748 weston_xkb_info_destroy(ec->xkb_info);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002749 xkb_context_unref(ec->xkb_context);
2750}
2751
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002752static struct weston_xkb_info *
2753weston_xkb_info_create(struct xkb_keymap *keymap)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002754{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002755 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
2756 if (xkb_info == NULL)
2757 return NULL;
2758
Ran Benita2e1968f2014-08-19 23:59:51 +03002759 xkb_info->keymap = xkb_keymap_ref(keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002760 xkb_info->ref_count = 1;
2761
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002762 char *keymap_str;
2763
Ran Benita2e1968f2014-08-19 23:59:51 +03002764 xkb_info->shift_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2765 XKB_MOD_NAME_SHIFT);
2766 xkb_info->caps_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2767 XKB_MOD_NAME_CAPS);
2768 xkb_info->ctrl_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2769 XKB_MOD_NAME_CTRL);
2770 xkb_info->alt_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2771 XKB_MOD_NAME_ALT);
2772 xkb_info->mod2_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2773 "Mod2");
2774 xkb_info->mod3_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2775 "Mod3");
2776 xkb_info->super_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2777 XKB_MOD_NAME_LOGO);
2778 xkb_info->mod5_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2779 "Mod5");
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002780
Ran Benita2e1968f2014-08-19 23:59:51 +03002781 xkb_info->num_led = xkb_keymap_led_get_index(xkb_info->keymap,
2782 XKB_LED_NAME_NUM);
2783 xkb_info->caps_led = xkb_keymap_led_get_index(xkb_info->keymap,
2784 XKB_LED_NAME_CAPS);
2785 xkb_info->scroll_led = xkb_keymap_led_get_index(xkb_info->keymap,
2786 XKB_LED_NAME_SCROLL);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002787
Ran Benita2e1968f2014-08-19 23:59:51 +03002788 keymap_str = xkb_keymap_get_as_string(xkb_info->keymap,
2789 XKB_KEYMAP_FORMAT_TEXT_V1);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002790 if (keymap_str == NULL) {
2791 weston_log("failed to get string version of keymap\n");
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002792 goto err_keymap;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002793 }
2794 xkb_info->keymap_size = strlen(keymap_str) + 1;
2795
2796 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
2797 if (xkb_info->keymap_fd < 0) {
2798 weston_log("creating a keymap file for %lu bytes failed: %m\n",
2799 (unsigned long) xkb_info->keymap_size);
2800 goto err_keymap_str;
2801 }
2802
2803 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2804 PROT_READ | PROT_WRITE,
2805 MAP_SHARED, xkb_info->keymap_fd, 0);
2806 if (xkb_info->keymap_area == MAP_FAILED) {
2807 weston_log("failed to mmap() %lu bytes\n",
2808 (unsigned long) xkb_info->keymap_size);
2809 goto err_dev_zero;
2810 }
2811 strcpy(xkb_info->keymap_area, keymap_str);
2812 free(keymap_str);
2813
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002814 return xkb_info;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002815
2816err_dev_zero:
2817 close(xkb_info->keymap_fd);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002818err_keymap_str:
2819 free(keymap_str);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002820err_keymap:
Ran Benita2e1968f2014-08-19 23:59:51 +03002821 xkb_keymap_unref(xkb_info->keymap);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002822 free(xkb_info);
2823 return NULL;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002824}
2825
2826static int
2827weston_compositor_build_global_keymap(struct weston_compositor *ec)
2828{
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002829 struct xkb_keymap *keymap;
2830
2831 if (ec->xkb_info != NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002832 return 0;
2833
Ran Benita2e1968f2014-08-19 23:59:51 +03002834 keymap = xkb_keymap_new_from_names(ec->xkb_context,
2835 &ec->xkb_names,
2836 0);
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002837 if (keymap == NULL) {
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002838 weston_log("failed to compile global XKB keymap\n");
2839 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
2840 "options %s\n",
2841 ec->xkb_names.rules, ec->xkb_names.model,
2842 ec->xkb_names.layout, ec->xkb_names.variant,
2843 ec->xkb_names.options);
2844 return -1;
2845 }
2846
Andrew Wedgbury9a6f02a2013-09-05 13:31:40 +00002847 ec->xkb_info = weston_xkb_info_create(keymap);
Rui Matos73d93952013-10-24 19:28:41 +02002848 xkb_keymap_unref(keymap);
Stefan Schmidtfda26522013-09-17 10:54:09 +01002849 if (ec->xkb_info == NULL)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002850 return -1;
2851
2852 return 0;
2853}
Rob Bradford382ff462013-06-24 16:52:45 +01002854#else
Giulio Camuffo0358af42016-06-02 21:48:08 +03002855WL_EXPORT int
2856weston_compositor_set_xkb_rule_names(struct weston_compositor *ec,
2857 struct xkb_rule_names *names)
Rob Bradford382ff462013-06-24 16:52:45 +01002858{
2859 return 0;
2860}
2861
2862void
2863weston_compositor_xkb_destroy(struct weston_compositor *ec)
2864{
2865}
2866#endif
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002867
Rui Matos65196bc2013-10-10 19:44:19 +02002868WL_EXPORT void
2869weston_seat_update_keymap(struct weston_seat *seat, struct xkb_keymap *keymap)
2870{
Derek Foreman1281a362015-07-31 16:55:32 -05002871 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2872
2873 if (!keyboard || !keymap)
Rui Matos65196bc2013-10-10 19:44:19 +02002874 return;
2875
2876#ifdef ENABLE_XKBCOMMON
2877 if (!seat->compositor->use_xkbcommon)
2878 return;
2879
Derek Foreman1281a362015-07-31 16:55:32 -05002880 xkb_keymap_unref(keyboard->pending_keymap);
2881 keyboard->pending_keymap = xkb_keymap_ref(keymap);
Rui Matos65196bc2013-10-10 19:44:19 +02002882
Derek Foreman1281a362015-07-31 16:55:32 -05002883 if (keyboard->keys.size == 0)
Rui Matos65196bc2013-10-10 19:44:19 +02002884 update_keymap(seat);
2885#endif
2886}
2887
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002888WL_EXPORT int
2889weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
2890{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002891 struct weston_keyboard *keyboard;
2892
Derek Foreman1281a362015-07-31 16:55:32 -05002893 if (seat->keyboard_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002894 seat->keyboard_device_count += 1;
2895 if (seat->keyboard_device_count == 1)
2896 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002897 return 0;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002898 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002899
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002900 keyboard = weston_keyboard_create();
2901 if (keyboard == NULL) {
2902 weston_log("failed to allocate weston keyboard struct\n");
2903 return -1;
2904 }
2905
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002906#ifdef ENABLE_XKBCOMMON
2907 if (seat->compositor->use_xkbcommon) {
2908 if (keymap != NULL) {
2909 keyboard->xkb_info = weston_xkb_info_create(keymap);
2910 if (keyboard->xkb_info == NULL)
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02002911 goto err;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002912 } else {
2913 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02002914 goto err;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002915 keyboard->xkb_info = seat->compositor->xkb_info;
2916 keyboard->xkb_info->ref_count++;
2917 }
2918
2919 keyboard->xkb_state.state = xkb_state_new(keyboard->xkb_info->keymap);
2920 if (keyboard->xkb_state.state == NULL) {
2921 weston_log("failed to initialise XKB state\n");
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02002922 goto err;
Jonas Ådahl7395ea02013-12-03 09:14:26 +01002923 }
2924
2925 keyboard->xkb_state.leds = 0;
2926 }
2927#endif
2928
Derek Foreman1281a362015-07-31 16:55:32 -05002929 seat->keyboard_state = keyboard;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02002930 seat->keyboard_device_count = 1;
2931 keyboard->seat = seat;
2932
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002933 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002934
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002935 return 0;
Ander Conselvan de Oliveira4d363cf2014-01-31 17:35:45 +02002936
2937err:
2938 if (keyboard->xkb_info)
2939 weston_xkb_info_destroy(keyboard->xkb_info);
2940 free(keyboard);
2941
2942 return -1;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002943}
2944
Jonas Ådahl91fed542013-12-03 09:14:27 +01002945static void
2946weston_keyboard_reset_state(struct weston_keyboard *keyboard)
2947{
2948 struct weston_seat *seat = keyboard->seat;
2949 struct xkb_state *state;
2950
2951#ifdef ENABLE_XKBCOMMON
2952 if (seat->compositor->use_xkbcommon) {
2953 state = xkb_state_new(keyboard->xkb_info->keymap);
2954 if (!state) {
2955 weston_log("failed to reset XKB state\n");
2956 return;
2957 }
2958 xkb_state_unref(keyboard->xkb_state.state);
2959 keyboard->xkb_state.state = state;
2960
2961 keyboard->xkb_state.leds = 0;
2962 }
2963#endif
2964
2965 seat->modifier_state = 0;
2966}
2967
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002968WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002969weston_seat_release_keyboard(struct weston_seat *seat)
2970{
2971 seat->keyboard_device_count--;
Derek Foremand621df22014-11-19 11:04:12 -06002972 assert(seat->keyboard_device_count >= 0);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002973 if (seat->keyboard_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05002974 weston_keyboard_set_focus(seat->keyboard_state, NULL);
2975 weston_keyboard_cancel_grab(seat->keyboard_state);
2976 weston_keyboard_reset_state(seat->keyboard_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002977 seat_send_updated_caps(seat);
2978 }
2979}
2980
2981WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002982weston_seat_init_pointer(struct weston_seat *seat)
2983{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002984 struct weston_pointer *pointer;
2985
Derek Foreman1281a362015-07-31 16:55:32 -05002986 if (seat->pointer_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002987 seat->pointer_device_count += 1;
2988 if (seat->pointer_device_count == 1)
2989 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002990 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002991 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04002992
Giulio Camuffocdb4d292013-11-14 23:42:53 +01002993 pointer = weston_pointer_create(seat);
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04002994 if (pointer == NULL)
2995 return;
2996
Derek Foreman1281a362015-07-31 16:55:32 -05002997 seat->pointer_state = pointer;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02002998 seat->pointer_device_count = 1;
2999 pointer->seat = seat;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003000
3001 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003002}
3003
3004WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003005weston_seat_release_pointer(struct weston_seat *seat)
3006{
Derek Foreman1281a362015-07-31 16:55:32 -05003007 struct weston_pointer *pointer = seat->pointer_state;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003008
3009 seat->pointer_device_count--;
3010 if (seat->pointer_device_count == 0) {
Derek Foremanf9318d12015-05-11 15:40:11 -05003011 weston_pointer_clear_focus(pointer);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003012 weston_pointer_cancel_grab(pointer);
Jonas Ådahl630bae82013-10-17 23:04:06 +02003013
Jonas Ådahla4932742013-10-17 23:04:07 +02003014 if (pointer->sprite)
3015 pointer_unmap_sprite(pointer);
3016
Jonas Ådahl3e12e632013-12-02 22:05:05 +01003017 weston_pointer_reset_state(pointer);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003018 seat_send_updated_caps(seat);
Derek Foremanfd5ca512015-01-07 15:00:25 -06003019
3020 /* seat->pointer is intentionally not destroyed so that
3021 * a newly attached pointer on this seat will retain
3022 * the previous cursor co-ordinates.
3023 */
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003024 }
3025}
3026
3027WL_EXPORT void
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003028weston_seat_init_touch(struct weston_seat *seat)
3029{
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003030 struct weston_touch *touch;
3031
Derek Foreman1281a362015-07-31 16:55:32 -05003032 if (seat->touch_state) {
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003033 seat->touch_device_count += 1;
3034 if (seat->touch_device_count == 1)
3035 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003036 return;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003037 }
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003038
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003039 touch = weston_touch_create();
3040 if (touch == NULL)
3041 return;
3042
Derek Foreman1281a362015-07-31 16:55:32 -05003043 seat->touch_state = touch;
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003044 seat->touch_device_count = 1;
Kristian Høgsberga4036bb2013-05-07 23:52:07 -04003045 touch->seat = seat;
3046
3047 seat_send_updated_caps(seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003048}
3049
3050WL_EXPORT void
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003051weston_seat_release_touch(struct weston_seat *seat)
3052{
3053 seat->touch_device_count--;
3054 if (seat->touch_device_count == 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05003055 weston_touch_set_focus(seat->touch_state, NULL);
3056 weston_touch_cancel_grab(seat->touch_state);
3057 weston_touch_reset_state(seat->touch_state);
Jonas Ådahld6e1c342013-10-17 23:04:05 +02003058 seat_send_updated_caps(seat);
3059 }
3060}
3061
3062WL_EXPORT void
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003063weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
3064 const char *seat_name)
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003065{
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003066 memset(seat, 0, sizeof *seat);
3067
Kristian Høgsberge3148752013-05-06 23:19:49 -04003068 seat->selection_data_source = NULL;
3069 wl_list_init(&seat->base_resource_list);
3070 wl_signal_init(&seat->selection_signal);
3071 wl_list_init(&seat->drag_resource_list);
Kristian Høgsberge3148752013-05-06 23:19:49 -04003072 wl_signal_init(&seat->destroy_signal);
Jason Ekstranda4ab5422014-04-02 19:53:45 -05003073 wl_signal_init(&seat->updated_caps_signal);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003074
Peter Hutterer87743e92016-01-18 16:38:22 +10003075 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 5,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003076 seat, bind_seat);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003077
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003078 seat->compositor = ec;
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003079 seat->modifier_state = 0;
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003080 seat->seat_name = strdup(seat_name);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003081
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003082 wl_list_insert(ec->seat_list.prev, &seat->link);
3083
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003084 clipboard_create(seat);
3085
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003086 wl_signal_emit(&ec->seat_created_signal, seat);
3087}
3088
3089WL_EXPORT void
3090weston_seat_release(struct weston_seat *seat)
3091{
3092 wl_list_remove(&seat->link);
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003093
Jonas Ådahl1afb2382014-01-03 19:46:51 +01003094 if (seat->saved_kbd_focus)
3095 wl_list_remove(&seat->saved_kbd_focus_listener.link);
3096
Derek Foreman1281a362015-07-31 16:55:32 -05003097 if (seat->pointer_state)
3098 weston_pointer_destroy(seat->pointer_state);
3099 if (seat->keyboard_state)
3100 weston_keyboard_destroy(seat->keyboard_state);
3101 if (seat->touch_state)
3102 weston_touch_destroy(seat->touch_state);
Kristian Høgsberg4a2a2742013-05-06 22:24:50 -04003103
Rob Bradford9af5f9e2013-05-31 18:09:50 +01003104 free (seat->seat_name);
3105
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003106 wl_global_destroy(seat->global);
Kristian Høgsbergaaadc772013-07-08 16:20:31 -04003107
Kristian Høgsbergb5e26102013-04-18 15:40:10 -04003108 wl_signal_emit(&seat->destroy_signal, seat);
3109}
Derek Foreman1281a362015-07-31 16:55:32 -05003110
3111/** Get a seat's keyboard pointer
3112 *
3113 * \param seat The seat to query
3114 * \return The seat's keyboard pointer, or NULL if no keyboard is present
3115 *
3116 * The keyboard pointer for a seat isn't freed when all keyboards are removed,
3117 * so it should only be used when the seat's keyboard_device_count is greater
3118 * than zero. This function does that test and only returns a pointer
3119 * when a keyboard is present.
3120 */
3121WL_EXPORT struct weston_keyboard *
3122weston_seat_get_keyboard(struct weston_seat *seat)
3123{
3124 if (!seat)
3125 return NULL;
3126
3127 if (seat->keyboard_device_count)
3128 return seat->keyboard_state;
3129
3130 return NULL;
3131}
3132
3133/** Get a seat's pointer pointer
3134 *
3135 * \param seat The seat to query
3136 * \return The seat's pointer pointer, or NULL if no pointer device is present
3137 *
3138 * The pointer pointer for a seat isn't freed when all mice are removed,
3139 * so it should only be used when the seat's pointer_device_count is greater
3140 * than zero. This function does that test and only returns a pointer
3141 * when a pointing device is present.
3142 */
3143WL_EXPORT struct weston_pointer *
3144weston_seat_get_pointer(struct weston_seat *seat)
3145{
3146 if (!seat)
3147 return NULL;
3148
3149 if (seat->pointer_device_count)
3150 return seat->pointer_state;
3151
3152 return NULL;
3153}
3154
Jonas Ådahld3414f22016-07-22 17:56:31 +08003155static const struct zwp_locked_pointer_v1_interface locked_pointer_interface;
3156static const struct zwp_confined_pointer_v1_interface confined_pointer_interface;
3157
3158static enum pointer_constraint_type
3159pointer_constraint_get_type(struct weston_pointer_constraint *constraint)
3160{
3161 if (wl_resource_instance_of(constraint->resource,
3162 &zwp_locked_pointer_v1_interface,
3163 &locked_pointer_interface)) {
3164 return POINTER_CONSTRAINT_TYPE_LOCK;
3165 } else if (wl_resource_instance_of(constraint->resource,
3166 &zwp_confined_pointer_v1_interface,
3167 &confined_pointer_interface)) {
3168 return POINTER_CONSTRAINT_TYPE_CONFINE;
3169 }
3170
3171 abort();
3172 return 0;
3173}
3174
3175static void
3176pointer_constraint_notify_activated(struct weston_pointer_constraint *constraint)
3177{
3178 struct wl_resource *resource = constraint->resource;
3179
3180 switch (pointer_constraint_get_type(constraint)) {
3181 case POINTER_CONSTRAINT_TYPE_LOCK:
3182 zwp_locked_pointer_v1_send_locked(resource);
3183 break;
3184 case POINTER_CONSTRAINT_TYPE_CONFINE:
3185 zwp_confined_pointer_v1_send_confined(resource);
3186 break;
3187 }
3188}
3189
3190static void
3191pointer_constraint_notify_deactivated(struct weston_pointer_constraint *constraint)
3192{
3193 struct wl_resource *resource = constraint->resource;
3194
3195 switch (pointer_constraint_get_type(constraint)) {
3196 case POINTER_CONSTRAINT_TYPE_LOCK:
3197 zwp_locked_pointer_v1_send_unlocked(resource);
3198 break;
3199 case POINTER_CONSTRAINT_TYPE_CONFINE:
3200 zwp_confined_pointer_v1_send_unconfined(resource);
3201 break;
3202 }
3203}
3204
3205static struct weston_pointer_constraint *
3206get_pointer_constraint_for_pointer(struct weston_surface *surface,
3207 struct weston_pointer *pointer)
3208{
3209 struct weston_pointer_constraint *constraint;
3210
3211 wl_list_for_each(constraint, &surface->pointer_constraints, link) {
3212 if (constraint->pointer == pointer)
3213 return constraint;
3214 }
3215
3216 return NULL;
3217}
3218
Derek Foreman1281a362015-07-31 16:55:32 -05003219/** Get a seat's touch pointer
3220 *
3221 * \param seat The seat to query
3222 * \return The seat's touch pointer, or NULL if no touch device is present
3223 *
3224 * The touch pointer for a seat isn't freed when all touch devices are removed,
3225 * so it should only be used when the seat's touch_device_count is greater
3226 * than zero. This function does that test and only returns a pointer
3227 * when a touch device is present.
3228 */
3229WL_EXPORT struct weston_touch *
3230weston_seat_get_touch(struct weston_seat *seat)
3231{
3232 if (!seat)
3233 return NULL;
3234
3235 if (seat->touch_device_count)
3236 return seat->touch_state;
3237
3238 return NULL;
3239}
Bryce Harrington24f917e2016-06-29 19:04:07 -07003240
3241/** Sets the keyboard focus to the given surface
3242 *
3243 * \param seat The seat to query
3244 */
3245WL_EXPORT void
3246weston_seat_set_keyboard_focus(struct weston_seat *seat,
3247 struct weston_surface *surface)
3248{
3249 struct weston_compositor *compositor = seat->compositor;
3250 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003251 struct weston_surface_activation_data activation_data;
Bryce Harrington24f917e2016-06-29 19:04:07 -07003252
Jonas Ådahlef8e1c32016-03-15 20:28:51 +08003253 if (keyboard && keyboard->focus != surface) {
Bryce Harrington24f917e2016-06-29 19:04:07 -07003254 weston_keyboard_set_focus(keyboard, surface);
3255 wl_data_device_set_keyboard_focus(seat);
3256 }
3257
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003258 inc_activate_serial(compositor);
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +08003259
3260 activation_data = (struct weston_surface_activation_data) {
3261 .surface = surface,
3262 .seat = seat,
3263 };
3264 wl_signal_emit(&compositor->activate_signal, &activation_data);
Bryce Harrington24f917e2016-06-29 19:04:07 -07003265}
Jonas Ådahl30d61d82014-10-22 21:21:17 +02003266
Jonas Ådahld3414f22016-07-22 17:56:31 +08003267static void
3268enable_pointer_constraint(struct weston_pointer_constraint *constraint,
3269 struct weston_view *view)
3270{
3271 assert(constraint->view == NULL);
3272 constraint->view = view;
3273 pointer_constraint_notify_activated(constraint);
3274 weston_pointer_start_grab(constraint->pointer, &constraint->grab);
3275 wl_list_remove(&constraint->surface_destroy_listener.link);
3276 wl_list_init(&constraint->surface_destroy_listener.link);
3277}
3278
3279static bool
3280is_pointer_constraint_enabled(struct weston_pointer_constraint *constraint)
3281{
3282 return constraint->view != NULL;
3283}
3284
3285static void
3286weston_pointer_constraint_disable(struct weston_pointer_constraint *constraint)
3287{
3288 constraint->view = NULL;
3289 pointer_constraint_notify_deactivated(constraint);
3290 weston_pointer_end_grab(constraint->grab.pointer);
3291}
3292
3293void
3294weston_pointer_constraint_destroy(struct weston_pointer_constraint *constraint)
3295{
3296 if (is_pointer_constraint_enabled(constraint))
3297 weston_pointer_constraint_disable(constraint);
3298
3299 wl_list_remove(&constraint->pointer_destroy_listener.link);
3300 wl_list_remove(&constraint->surface_destroy_listener.link);
3301 wl_list_remove(&constraint->surface_commit_listener.link);
3302 wl_list_remove(&constraint->surface_activate_listener.link);
3303
3304 wl_resource_set_user_data(constraint->resource, NULL);
3305 pixman_region32_fini(&constraint->region);
3306 wl_list_remove(&constraint->link);
3307 free(constraint);
3308}
3309
3310static void
3311disable_pointer_constraint(struct weston_pointer_constraint *constraint)
3312{
3313 switch (constraint->lifetime) {
3314 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT:
3315 weston_pointer_constraint_destroy(constraint);
3316 break;
3317 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT:
3318 weston_pointer_constraint_disable(constraint);
3319 break;
3320 }
3321}
3322
3323static bool
3324is_within_constraint_region(struct weston_pointer_constraint *constraint,
3325 wl_fixed_t sx, wl_fixed_t sy)
3326{
3327 struct weston_surface *surface = constraint->surface;
3328 pixman_region32_t constraint_region;
3329 bool result;
3330
3331 pixman_region32_init(&constraint_region);
3332 pixman_region32_intersect(&constraint_region,
3333 &surface->input,
3334 &constraint->region);
3335 result = pixman_region32_contains_point(&constraint_region,
3336 wl_fixed_to_int(sx),
3337 wl_fixed_to_int(sy),
3338 NULL);
3339 pixman_region32_fini(&constraint_region);
3340
3341 return result;
3342}
3343
3344static void
3345maybe_enable_pointer_constraint(struct weston_pointer_constraint *constraint)
3346{
3347 struct weston_surface *surface = constraint->surface;
3348 struct weston_view *vit;
3349 struct weston_view *view = NULL;
3350 struct weston_pointer *pointer = constraint->pointer;
3351 struct weston_keyboard *keyboard;
3352 struct weston_seat *seat = pointer->seat;
3353 int32_t x, y;
3354
3355 /* Postpone if no view of the surface was most recently clicked. */
3356 wl_list_for_each(vit, &surface->views, surface_link) {
3357 if (vit->click_to_activate_serial ==
3358 surface->compositor->activate_serial) {
3359 view = vit;
3360 }
3361 }
3362 if (view == NULL)
3363 return;
3364
3365 /* Postpone if surface doesn't have keyboard focus. */
3366 keyboard = weston_seat_get_keyboard(seat);
3367 if (!keyboard || keyboard->focus != surface)
3368 return;
3369
3370 /* Postpone constraint if the pointer is not within the
3371 * constraint region.
3372 */
3373 weston_view_from_global(view,
3374 wl_fixed_to_int(pointer->x),
3375 wl_fixed_to_int(pointer->y),
3376 &x, &y);
3377 if (!is_within_constraint_region(constraint,
3378 wl_fixed_from_int(x),
3379 wl_fixed_from_int(y)))
3380 return;
3381
3382 enable_pointer_constraint(constraint, view);
3383}
3384
3385static void
3386locked_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
3387{
3388}
3389
3390static void
3391locked_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
3392 uint32_t time,
3393 struct weston_pointer_motion_event *event)
3394{
Quentin Glidiccde13452016-08-12 10:41:32 +02003395 pointer_send_relative_motion(grab->pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08003396}
3397
3398static void
3399locked_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
3400 uint32_t time,
3401 uint32_t button,
3402 uint32_t state_w)
3403{
3404 weston_pointer_send_button(grab->pointer, time, button, state_w);
3405}
3406
3407static void
3408locked_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
3409 uint32_t time,
3410 struct weston_pointer_axis_event *event)
3411{
3412 weston_pointer_send_axis(grab->pointer, time, event);
3413}
3414
3415static void
3416locked_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
3417 uint32_t source)
3418{
3419 weston_pointer_send_axis_source(grab->pointer, source);
3420}
3421
3422static void
3423locked_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
3424{
3425 weston_pointer_send_frame(grab->pointer);
3426}
3427
3428static void
3429locked_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
3430{
3431 struct weston_pointer_constraint *constraint =
3432 container_of(grab, struct weston_pointer_constraint, grab);
3433
3434 disable_pointer_constraint(constraint);
3435}
3436
3437static const struct weston_pointer_grab_interface
3438 locked_pointer_grab_interface = {
3439 locked_pointer_grab_pointer_focus,
3440 locked_pointer_grab_pointer_motion,
3441 locked_pointer_grab_pointer_button,
3442 locked_pointer_grab_pointer_axis,
3443 locked_pointer_grab_pointer_axis_source,
3444 locked_pointer_grab_pointer_frame,
3445 locked_pointer_grab_pointer_cancel,
3446};
3447
3448static void
3449pointer_constraint_constrain_resource_destroyed(struct wl_resource *resource)
3450{
3451 struct weston_pointer_constraint *constraint =
3452 wl_resource_get_user_data(resource);
3453
3454 if (!constraint)
3455 return;
3456
3457 weston_pointer_constraint_destroy(constraint);
3458}
3459
3460static void
3461pointer_constraint_surface_activate(struct wl_listener *listener, void *data)
3462{
3463 struct weston_surface_activation_data *activation = data;
3464 struct weston_pointer *pointer;
3465 struct weston_surface *focus = activation->surface;
3466 struct weston_pointer_constraint *constraint =
3467 container_of(listener, struct weston_pointer_constraint,
3468 surface_activate_listener);
3469 bool is_constraint_surface;
3470
3471 pointer = weston_seat_get_pointer(activation->seat);
3472 if (!pointer)
3473 return;
3474
3475 is_constraint_surface =
3476 get_pointer_constraint_for_pointer(focus, pointer) == constraint;
3477
3478 if (is_constraint_surface &&
3479 !is_pointer_constraint_enabled(constraint))
3480 maybe_enable_pointer_constraint(constraint);
3481 else if (!is_constraint_surface &&
3482 is_pointer_constraint_enabled(constraint))
3483 disable_pointer_constraint(constraint);
3484}
3485
3486static void
3487pointer_constraint_pointer_destroyed(struct wl_listener *listener, void *data)
3488{
3489 struct weston_pointer_constraint *constraint =
3490 container_of(listener, struct weston_pointer_constraint,
3491 pointer_destroy_listener);
3492
3493 weston_pointer_constraint_destroy(constraint);
3494}
3495
3496static void
3497pointer_constraint_surface_destroyed(struct wl_listener *listener, void *data)
3498{
3499 struct weston_pointer_constraint *constraint =
3500 container_of(listener, struct weston_pointer_constraint,
3501 surface_destroy_listener);
3502
3503 weston_pointer_constraint_destroy(constraint);
3504}
3505
3506static void
3507pointer_constraint_surface_committed(struct wl_listener *listener, void *data)
3508{
3509 struct weston_pointer_constraint *constraint =
3510 container_of(listener, struct weston_pointer_constraint,
3511 surface_commit_listener);
3512
3513 if (constraint->region_is_pending) {
3514 constraint->region_is_pending = false;
3515 pixman_region32_copy(&constraint->region,
3516 &constraint->region_pending);
3517 pixman_region32_fini(&constraint->region_pending);
3518 pixman_region32_init(&constraint->region_pending);
3519 }
3520
3521 if (constraint->hint_is_pending) {
3522 constraint->hint_is_pending = false;
3523
3524 constraint->hint_is_pending = true;
3525 constraint->hint_x = constraint->hint_x_pending;
3526 constraint->hint_y = constraint->hint_y_pending;
3527 }
3528
3529 if (pointer_constraint_get_type(constraint) ==
3530 POINTER_CONSTRAINT_TYPE_CONFINE &&
3531 is_pointer_constraint_enabled(constraint))
3532 maybe_warp_confined_pointer(constraint);
3533}
3534
3535static struct weston_pointer_constraint *
3536weston_pointer_constraint_create(struct weston_surface *surface,
3537 struct weston_pointer *pointer,
3538 struct weston_region *region,
3539 enum zwp_pointer_constraints_v1_lifetime lifetime,
3540 struct wl_resource *cr,
3541 const struct weston_pointer_grab_interface *grab_interface)
3542{
3543 struct weston_pointer_constraint *constraint;
3544
3545 constraint = zalloc(sizeof *constraint);
3546 if (!constraint)
3547 return NULL;
3548
3549 constraint->lifetime = lifetime;
3550 pixman_region32_init(&constraint->region);
3551 pixman_region32_init(&constraint->region_pending);
3552 wl_list_insert(&surface->pointer_constraints, &constraint->link);
3553 constraint->surface = surface;
3554 constraint->pointer = pointer;
3555 constraint->resource = cr;
3556 constraint->grab.interface = grab_interface;
3557 if (region) {
3558 pixman_region32_copy(&constraint->region,
3559 &region->region);
3560 } else {
3561 pixman_region32_fini(&constraint->region);
3562 region_init_infinite(&constraint->region);
3563 }
3564
3565 constraint->surface_activate_listener.notify =
3566 pointer_constraint_surface_activate;
3567 constraint->surface_destroy_listener.notify =
3568 pointer_constraint_surface_destroyed;
3569 constraint->surface_commit_listener.notify =
3570 pointer_constraint_surface_committed;
3571 constraint->pointer_destroy_listener.notify =
3572 pointer_constraint_pointer_destroyed;
3573
3574 wl_signal_add(&surface->compositor->activate_signal,
3575 &constraint->surface_activate_listener);
3576 wl_signal_add(&pointer->destroy_signal,
3577 &constraint->pointer_destroy_listener);
3578 wl_signal_add(&surface->destroy_signal,
3579 &constraint->surface_destroy_listener);
3580 wl_signal_add(&surface->commit_signal,
3581 &constraint->surface_commit_listener);
3582
3583 return constraint;
3584}
3585
3586static void
3587init_pointer_constraint(struct wl_resource *pointer_constraints_resource,
3588 uint32_t id,
3589 struct weston_surface *surface,
3590 struct weston_pointer *pointer,
3591 struct weston_region *region,
3592 enum zwp_pointer_constraints_v1_lifetime lifetime,
3593 const struct wl_interface *interface,
3594 const void *implementation,
3595 const struct weston_pointer_grab_interface *grab_interface)
3596{
3597 struct wl_client *client =
3598 wl_resource_get_client(pointer_constraints_resource);
3599 struct wl_resource *cr;
3600 struct weston_pointer_constraint *constraint;
3601
3602 if (get_pointer_constraint_for_pointer(surface, pointer)) {
3603 wl_resource_post_error(pointer_constraints_resource,
3604 ZWP_POINTER_CONSTRAINTS_V1_ERROR_ALREADY_CONSTRAINED,
3605 "the pointer has a lock/confine request on this surface");
3606 return;
3607 }
3608
3609 cr = wl_resource_create(client, interface,
3610 wl_resource_get_version(pointer_constraints_resource),
3611 id);
3612 if (cr == NULL) {
3613 wl_client_post_no_memory(client);
3614 return;
3615 }
3616
3617 constraint = weston_pointer_constraint_create(surface, pointer,
3618 region, lifetime,
3619 cr, grab_interface);
3620 if (constraint == NULL) {
3621 wl_client_post_no_memory(client);
3622 return;
3623 }
3624
3625 wl_resource_set_implementation(cr, implementation, constraint,
3626 pointer_constraint_constrain_resource_destroyed);
3627
3628 maybe_enable_pointer_constraint(constraint);
3629}
3630
3631static void
3632pointer_constraints_destroy(struct wl_client *client,
3633 struct wl_resource *resource)
3634{
3635 wl_resource_destroy(resource);
3636}
3637
3638static void
3639locked_pointer_destroy(struct wl_client *client,
3640 struct wl_resource *resource)
3641{
3642 struct weston_pointer_constraint *constraint =
3643 wl_resource_get_user_data(resource);
3644 wl_fixed_t x, y;
3645
3646 if (constraint && constraint->view && constraint->hint_is_pending &&
3647 is_within_constraint_region(constraint,
3648 constraint->hint_x,
3649 constraint->hint_y)) {
3650 weston_view_to_global_fixed(constraint->view,
3651 constraint->hint_x,
3652 constraint->hint_y,
3653 &x, &y);
3654 weston_pointer_move_to(constraint->pointer, x, y);
3655 }
3656 wl_resource_destroy(resource);
3657}
3658
3659static void
3660locked_pointer_set_cursor_position_hint(struct wl_client *client,
3661 struct wl_resource *resource,
3662 wl_fixed_t surface_x,
3663 wl_fixed_t surface_y)
3664{
3665 struct weston_pointer_constraint *constraint =
3666 wl_resource_get_user_data(resource);
3667
3668 /* Ignore a set cursor hint that was sent after the lock was cancelled.
3669 */
3670 if (!constraint ||
3671 !constraint->resource ||
3672 constraint->resource != resource)
3673 return;
3674
3675 constraint->hint_is_pending = true;
3676 constraint->hint_x_pending = surface_x;
3677 constraint->hint_y_pending = surface_y;
3678}
3679
3680static void
3681locked_pointer_set_region(struct wl_client *client,
3682 struct wl_resource *resource,
3683 struct wl_resource *region_resource)
3684{
3685 struct weston_pointer_constraint *constraint =
3686 wl_resource_get_user_data(resource);
3687 struct weston_region *region = region_resource ?
3688 wl_resource_get_user_data(region_resource) : NULL;
3689
3690 if (!constraint)
3691 return;
3692
3693 if (region) {
3694 pixman_region32_copy(&constraint->region_pending,
3695 &region->region);
3696 } else {
3697 pixman_region32_fini(&constraint->region_pending);
3698 region_init_infinite(&constraint->region_pending);
3699 }
3700 constraint->region_is_pending = true;
3701}
3702
3703
3704static const struct zwp_locked_pointer_v1_interface locked_pointer_interface = {
3705 locked_pointer_destroy,
3706 locked_pointer_set_cursor_position_hint,
3707 locked_pointer_set_region,
3708};
3709
3710static void
3711pointer_constraints_lock_pointer(struct wl_client *client,
3712 struct wl_resource *resource,
3713 uint32_t id,
3714 struct wl_resource *surface_resource,
3715 struct wl_resource *pointer_resource,
3716 struct wl_resource *region_resource,
3717 uint32_t lifetime)
3718{
3719 struct weston_surface *surface =
3720 wl_resource_get_user_data(surface_resource);
3721 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
3722 struct weston_region *region = region_resource ?
3723 wl_resource_get_user_data(region_resource) : NULL;
3724
3725 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
3726 &zwp_locked_pointer_v1_interface,
3727 &locked_pointer_interface,
3728 &locked_pointer_grab_interface);
3729}
3730
3731static void
3732confined_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
3733{
3734}
3735
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08003736static double
3737vec2d_cross_product(struct vec2d a, struct vec2d b)
3738{
3739 return a.x * b.y - a.y * b.x;
3740}
3741
3742static struct vec2d
3743vec2d_add(struct vec2d a, struct vec2d b)
3744{
3745 return (struct vec2d) {
3746 .x = a.x + b.x,
3747 .y = a.y + b.y,
3748 };
3749}
3750
3751static struct vec2d
3752vec2d_subtract(struct vec2d a, struct vec2d b)
3753{
3754 return (struct vec2d) {
3755 .x = a.x - b.x,
3756 .y = a.y - b.y,
3757 };
3758}
3759
3760static struct vec2d
3761vec2d_multiply_constant(double c, struct vec2d a)
3762{
3763 return (struct vec2d) {
3764 .x = c * a.x,
3765 .y = c * a.y,
3766 };
3767}
3768
3769static bool
3770lines_intersect(struct line *line1, struct line *line2,
3771 struct vec2d *intersection)
3772{
3773 struct vec2d p = line1->a;
3774 struct vec2d r = vec2d_subtract(line1->b, line1->a);
3775 struct vec2d q = line2->a;
3776 struct vec2d s = vec2d_subtract(line2->b, line2->a);
3777 double rxs;
3778 double sxr;
3779 double t;
3780 double u;
3781
3782 /*
3783 * The line (p, r) and (q, s) intersects where
3784 *
3785 * p + t r = q + u s
3786 *
3787 * Calculate t:
3788 *
3789 * (p + t r) × s = (q + u s) × s
3790 * p × s + t (r × s) = q × s + u (s × s)
3791 * p × s + t (r × s) = q × s
3792 * t (r × s) = q × s - p × s
3793 * t (r × s) = (q - p) × s
3794 * t = ((q - p) × s) / (r × s)
3795 *
3796 * Using the same method, for u we get:
3797 *
3798 * u = ((p - q) × r) / (s × r)
3799 */
3800
3801 rxs = vec2d_cross_product(r, s);
3802 sxr = vec2d_cross_product(s, r);
3803
3804 /* If r × s = 0 then the lines are either parallel or collinear. */
3805 if (fabs(rxs) < DBL_MIN)
3806 return false;
3807
3808 t = vec2d_cross_product(vec2d_subtract(q, p), s) / rxs;
3809 u = vec2d_cross_product(vec2d_subtract(p, q), r) / sxr;
3810
3811 /* The lines only intersect if 0 ≤ t ≤ 1 and 0 ≤ u ≤ 1. */
3812 if (t < 0.0 || t > 1.0 || u < 0.0 || u > 1.0)
3813 return false;
3814
3815 *intersection = vec2d_add(p, vec2d_multiply_constant(t, r));
3816 return true;
3817}
3818
3819static struct border *
3820add_border(struct wl_array *array,
3821 double x1, double y1,
3822 double x2, double y2,
3823 enum motion_direction blocking_dir)
3824{
3825 struct border *border = wl_array_add(array, sizeof *border);
3826
3827 *border = (struct border) {
3828 .line = (struct line) {
3829 .a = (struct vec2d) {
3830 .x = x1,
3831 .y = y1,
3832 },
3833 .b = (struct vec2d) {
3834 .x = x2,
3835 .y = y2,
3836 },
3837 },
3838 .blocking_dir = blocking_dir,
3839 };
3840
3841 return border;
3842}
3843
3844static int
3845compare_lines_x(const void *a, const void *b)
3846{
3847 const struct border *border_a = a;
3848 const struct border *border_b = b;
3849
3850
3851 if (border_a->line.a.x == border_b->line.a.x)
3852 return border_a->line.b.x < border_b->line.b.x;
3853 else
3854 return border_a->line.a.x > border_b->line.a.x;
3855}
3856
3857static void
3858add_non_overlapping_edges(pixman_box32_t *boxes,
3859 int band_above_start,
3860 int band_below_start,
3861 int band_below_end,
3862 struct wl_array *borders)
3863{
3864 int i;
3865 struct wl_array band_merge;
3866 struct border *border;
3867 struct border *prev_border;
3868 struct border *new_border;
3869
3870 wl_array_init(&band_merge);
3871
3872 /* Add bottom band of previous row, and top band of current row, and
3873 * sort them so lower left x coordinate comes first. If there are two
3874 * borders with the same left x coordinate, the wider one comes first.
3875 */
3876 for (i = band_above_start; i < band_below_start; i++) {
3877 pixman_box32_t *box = &boxes[i];
3878 add_border(&band_merge, box->x1, box->y2, box->x2, box->y2,
3879 MOTION_DIRECTION_POSITIVE_Y);
3880 }
3881 for (i = band_below_start; i < band_below_end; i++) {
3882 pixman_box32_t *box= &boxes[i];
3883 add_border(&band_merge, box->x1, box->y1, box->x2, box->y1,
3884 MOTION_DIRECTION_NEGATIVE_Y);
3885 }
3886 qsort(band_merge.data,
3887 band_merge.size / sizeof *border,
3888 sizeof *border,
3889 compare_lines_x);
3890
3891 /* Combine the two combined bands so that any overlapping border is
3892 * eliminated. */
3893 prev_border = NULL;
3894 wl_array_for_each(border, &band_merge) {
3895 assert(border->line.a.y == border->line.b.y);
3896 assert(!prev_border ||
3897 prev_border->line.a.y == border->line.a.y);
3898 assert(!prev_border ||
3899 (prev_border->line.a.x != border->line.a.x ||
3900 prev_border->line.b.x != border->line.b.x));
3901 assert(!prev_border ||
3902 prev_border->line.a.x <= border->line.a.x);
3903
3904 if (prev_border &&
3905 prev_border->line.a.x == border->line.a.x) {
3906 /*
3907 * ------------ +
3908 * ------- =
3909 * [ ]-----
3910 */
3911 prev_border->line.a.x = border->line.b.x;
3912 } else if (prev_border &&
3913 prev_border->line.b.x == border->line.b.x) {
3914 /*
3915 * ------------ +
3916 * ------ =
3917 * ------[ ]
3918 */
3919 prev_border->line.b.x = border->line.a.x;
3920 } else if (prev_border &&
3921 prev_border->line.b.x == border->line.a.x) {
3922 /*
3923 * -------- +
3924 * ------ =
3925 * --------------
3926 */
3927 prev_border->line.b.x = border->line.b.x;
3928 } else if (prev_border &&
3929 prev_border->line.b.x >= border->line.a.x) {
3930 /*
3931 * --------------- +
3932 * ------ =
3933 * -----[ ]----
3934 */
3935 new_border = add_border(borders,
3936 border->line.b.x,
3937 border->line.b.y,
3938 prev_border->line.b.x,
3939 prev_border->line.b.y,
3940 prev_border->blocking_dir);
3941 prev_border->line.b.x = border->line.a.x;
3942 prev_border = new_border;
3943 } else {
3944 assert(!prev_border ||
3945 prev_border->line.b.x < border->line.a.x);
3946 /*
3947 * First border or non-overlapping.
3948 *
3949 * ----- +
3950 * ----- =
3951 * ----- -----
3952 */
3953 new_border = wl_array_add(borders, sizeof *border);
3954 *new_border = *border;
3955 prev_border = new_border;
3956 }
3957 }
3958
3959 wl_array_release(&band_merge);
3960}
3961
3962static void
3963add_band_bottom_edges(pixman_box32_t *boxes,
3964 int band_start,
3965 int band_end,
3966 struct wl_array *borders)
3967{
3968 int i;
3969
3970 for (i = band_start; i < band_end; i++) {
3971 add_border(borders,
3972 boxes[i].x1, boxes[i].y2,
3973 boxes[i].x2, boxes[i].y2,
3974 MOTION_DIRECTION_POSITIVE_Y);
3975 }
3976}
3977
3978static void
3979region_to_outline(pixman_region32_t *region, struct wl_array *borders)
3980{
3981 pixman_box32_t *boxes;
3982 int num_boxes;
3983 int i;
3984 int top_most, bottom_most;
3985 int current_roof;
3986 int prev_top;
3987 int band_start, prev_band_start;
3988
3989 /*
3990 * Remove any overlapping lines from the set of rectangles. Note that
3991 * pixman regions are grouped as rows of rectangles, where rectangles
3992 * in one row never touch or overlap and are all of the same height.
3993 *
3994 * -------- --- -------- ---
3995 * | | | | | | | |
3996 * ----------====---- --- ----------- ----- ---
3997 * | | => | |
3998 * ----==========--------- ----- ----------
3999 * | | | |
4000 * ------------------- -------------------
4001 *
4002 */
4003
4004 boxes = pixman_region32_rectangles(region, &num_boxes);
4005 prev_top = 0;
4006 top_most = boxes[0].y1;
4007 current_roof = top_most;
4008 bottom_most = boxes[num_boxes - 1].y2;
4009 band_start = 0;
4010 prev_band_start = 0;
4011 for (i = 0; i < num_boxes; i++) {
4012 /* Detect if there is a vertical empty space, and add the lower
4013 * level of the previous band if so was the case. */
4014 if (i > 0 &&
4015 boxes[i].y1 != prev_top &&
4016 boxes[i].y1 != boxes[i - 1].y2) {
4017 current_roof = boxes[i].y1;
4018 add_band_bottom_edges(boxes,
4019 band_start,
4020 i,
4021 borders);
4022 }
4023
4024 /* Special case adding the last band, since it won't be handled
4025 * by the band change detection below. */
4026 if (boxes[i].y1 != current_roof && i == num_boxes - 1) {
4027 if (boxes[i].y1 != prev_top) {
4028 /* The last band is a single box, so we don't
4029 * have a prev_band_start to tell us when the
4030 * previous band started. */
4031 add_non_overlapping_edges(boxes,
4032 band_start,
4033 i,
4034 i + 1,
4035 borders);
4036 } else {
4037 add_non_overlapping_edges(boxes,
4038 prev_band_start,
4039 band_start,
4040 i + 1,
4041 borders);
4042 }
4043 }
4044
4045 /* Detect when passing a band and combine the top border of the
4046 * just passed band with the bottom band of the previous band.
4047 */
4048 if (boxes[i].y1 != top_most && boxes[i].y1 != prev_top) {
4049 /* Combine the two passed bands. */
4050 if (prev_top != current_roof) {
4051 add_non_overlapping_edges(boxes,
4052 prev_band_start,
4053 band_start,
4054 i,
4055 borders);
4056 }
4057
4058 prev_band_start = band_start;
4059 band_start = i;
4060 }
4061
4062 /* Add the top border if the box is part of the current roof. */
4063 if (boxes[i].y1 == current_roof) {
4064 add_border(borders,
4065 boxes[i].x1, boxes[i].y1,
4066 boxes[i].x2, boxes[i].y1,
4067 MOTION_DIRECTION_NEGATIVE_Y);
4068 }
4069
4070 /* Add the bottom border of the last band. */
4071 if (boxes[i].y2 == bottom_most) {
4072 add_border(borders,
4073 boxes[i].x1, boxes[i].y2,
4074 boxes[i].x2, boxes[i].y2,
4075 MOTION_DIRECTION_POSITIVE_Y);
4076 }
4077
4078 /* Always add the left border. */
4079 add_border(borders,
4080 boxes[i].x1, boxes[i].y1,
4081 boxes[i].x1, boxes[i].y2,
4082 MOTION_DIRECTION_NEGATIVE_X);
4083
4084 /* Always add the right border. */
4085 add_border(borders,
4086 boxes[i].x2, boxes[i].y1,
4087 boxes[i].x2, boxes[i].y2,
4088 MOTION_DIRECTION_POSITIVE_X);
4089
4090 prev_top = boxes[i].y1;
4091 }
4092}
4093
4094static bool
4095is_border_horizontal (struct border *border)
4096{
4097 return border->line.a.y == border->line.b.y;
4098}
4099
4100static bool
4101is_border_blocking_directions(struct border *border,
4102 uint32_t directions)
4103{
4104 /* Don't block parallel motions. */
4105 if (is_border_horizontal(border)) {
4106 if ((directions & (MOTION_DIRECTION_POSITIVE_Y |
4107 MOTION_DIRECTION_NEGATIVE_Y)) == 0)
4108 return false;
4109 } else {
4110 if ((directions & (MOTION_DIRECTION_POSITIVE_X |
4111 MOTION_DIRECTION_NEGATIVE_X)) == 0)
4112 return false;
4113 }
4114
4115 return (~border->blocking_dir & directions) != directions;
4116}
4117
4118static struct border *
4119get_closest_border(struct wl_array *borders,
4120 struct line *motion,
4121 uint32_t directions)
4122{
4123 struct border *border;
4124 struct vec2d intersection;
4125 struct vec2d delta;
4126 double distance_2;
4127 struct border *closest_border = NULL;
4128 double closest_distance_2 = DBL_MAX;
4129
4130 wl_array_for_each(border, borders) {
4131 if (!is_border_blocking_directions(border, directions))
4132 continue;
4133
4134 if (!lines_intersect(&border->line, motion, &intersection))
4135 continue;
4136
4137 delta = vec2d_subtract(intersection, motion->a);
4138 distance_2 = delta.x*delta.x + delta.y*delta.y;
4139 if (distance_2 < closest_distance_2) {
4140 closest_border = border;
4141 closest_distance_2 = distance_2;
4142 }
4143 }
4144
4145 return closest_border;
4146}
4147
4148static void
4149clamp_to_border(struct border *border,
4150 struct line *motion,
4151 uint32_t *motion_dir)
4152{
4153 /*
4154 * When clamping either rightward or downward motions, the motion needs
4155 * to be clamped so that the destination coordinate does not end up on
4156 * the border (see weston_pointer_clamp_event_to_region). Do this by
4157 * clamping such motions to the border minus the smallest possible
4158 * wl_fixed_t value.
4159 */
4160 if (is_border_horizontal(border)) {
4161 if (*motion_dir & MOTION_DIRECTION_POSITIVE_Y)
4162 motion->b.y = border->line.a.y - wl_fixed_to_double(1);
4163 else
4164 motion->b.y = border->line.a.y;
4165 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_Y |
4166 MOTION_DIRECTION_NEGATIVE_Y);
4167 } else {
4168 if (*motion_dir & MOTION_DIRECTION_POSITIVE_X)
4169 motion->b.x = border->line.a.x - wl_fixed_to_double(1);
4170 else
4171 motion->b.x = border->line.a.x;
4172 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_X |
4173 MOTION_DIRECTION_NEGATIVE_X);
4174 }
4175}
4176
4177static uint32_t
4178get_motion_directions(struct line *motion)
4179{
4180 uint32_t directions = 0;
4181
4182 if (motion->a.x < motion->b.x)
4183 directions |= MOTION_DIRECTION_POSITIVE_X;
4184 else if (motion->a.x > motion->b.x)
4185 directions |= MOTION_DIRECTION_NEGATIVE_X;
4186 if (motion->a.y < motion->b.y)
4187 directions |= MOTION_DIRECTION_POSITIVE_Y;
4188 else if (motion->a.y > motion->b.y)
4189 directions |= MOTION_DIRECTION_NEGATIVE_Y;
4190
4191 return directions;
4192}
4193
Jonas Ådahld3414f22016-07-22 17:56:31 +08004194static void
4195weston_pointer_clamp_event_to_region(struct weston_pointer *pointer,
4196 struct weston_pointer_motion_event *event,
4197 pixman_region32_t *region,
4198 wl_fixed_t *clamped_x,
4199 wl_fixed_t *clamped_y)
4200{
4201 wl_fixed_t x, y;
4202 wl_fixed_t sx, sy;
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004203 wl_fixed_t old_sx = pointer->sx;
4204 wl_fixed_t old_sy = pointer->sy;
4205 struct wl_array borders;
4206 struct line motion;
4207 struct border *closest_border;
4208 float new_x_f, new_y_f;
4209 uint32_t directions;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004210
4211 weston_pointer_motion_to_abs(pointer, event, &x, &y);
4212 weston_view_from_global_fixed(pointer->focus, x, y, &sx, &sy);
4213
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004214 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004215
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004216 /*
4217 * Generate borders given the confine region we are to use. The borders
4218 * are defined to be the outer region of the allowed area. This means
4219 * top/left borders are "within" the allowed area, while bottom/right
4220 * borders are outside. This needs to be considered when clamping
4221 * confined motion vectors.
4222 */
4223 region_to_outline(region, &borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004224
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004225 motion = (struct line) {
4226 .a = (struct vec2d) {
4227 .x = wl_fixed_to_double(old_sx),
4228 .y = wl_fixed_to_double(old_sy),
4229 },
4230 .b = (struct vec2d) {
4231 .x = wl_fixed_to_double(sx),
4232 .y = wl_fixed_to_double(sy),
4233 },
4234 };
4235 directions = get_motion_directions(&motion);
4236
4237 while (directions) {
4238 closest_border = get_closest_border(&borders,
4239 &motion,
4240 directions);
4241 if (closest_border)
4242 clamp_to_border(closest_border, &motion, &directions);
4243 else
4244 break;
4245 }
4246
4247 weston_view_to_global_float(pointer->focus,
4248 (float) motion.b.x, (float) motion.b.y,
4249 &new_x_f, &new_y_f);
4250 *clamped_x = wl_fixed_from_double(new_x_f);
4251 *clamped_y = wl_fixed_from_double(new_y_f);
4252
4253 wl_array_release(&borders);
4254}
4255
4256static double
4257point_to_border_distance_2(struct border *border, double x, double y)
4258{
4259 double orig_x, orig_y;
4260 double dx, dy;
4261
4262 if (is_border_horizontal(border)) {
4263 if (x < border->line.a.x)
4264 orig_x = border->line.a.x;
4265 else if (x > border->line.b.x)
4266 orig_x = border->line.b.x;
4267 else
4268 orig_x = x;
4269 orig_y = border->line.a.y;
4270 } else {
4271 if (y < border->line.a.y)
4272 orig_y = border->line.a.y;
4273 else if (y > border->line.b.y)
4274 orig_y = border->line.b.y;
4275 else
4276 orig_y = y;
4277 orig_x = border->line.a.x;
4278 }
4279
4280
4281 dx = fabs(orig_x - x);
4282 dy = fabs(orig_y - y);
4283 return dx*dx + dy*dy;
4284}
4285
4286static void
4287warp_to_behind_border(struct border *border, wl_fixed_t *sx, wl_fixed_t *sy)
4288{
4289 switch (border->blocking_dir) {
4290 case MOTION_DIRECTION_POSITIVE_X:
4291 case MOTION_DIRECTION_NEGATIVE_X:
4292 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_X)
4293 *sx = wl_fixed_from_double(border->line.a.x) - 1;
4294 else
4295 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4296 if (*sy < wl_fixed_from_double(border->line.a.y))
4297 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4298 else if (*sy > wl_fixed_from_double(border->line.b.y))
4299 *sy = wl_fixed_from_double(border->line.b.y) - 1;
4300 break;
4301 case MOTION_DIRECTION_POSITIVE_Y:
4302 case MOTION_DIRECTION_NEGATIVE_Y:
4303 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_Y)
4304 *sy = wl_fixed_from_double(border->line.a.y) - 1;
4305 else
4306 *sy = wl_fixed_from_double(border->line.a.y) + 1;
4307 if (*sx < wl_fixed_from_double(border->line.a.x))
4308 *sx = wl_fixed_from_double(border->line.a.x) + 1;
4309 else if (*sx > wl_fixed_from_double(border->line.b.x))
4310 *sx = wl_fixed_from_double(border->line.b.x) - 1;
4311 break;
4312 }
Jonas Ådahld3414f22016-07-22 17:56:31 +08004313}
4314
4315static void
4316maybe_warp_confined_pointer(struct weston_pointer_constraint *constraint)
4317{
4318 wl_fixed_t x;
4319 wl_fixed_t y;
4320 wl_fixed_t sx;
4321 wl_fixed_t sy;
4322
4323 weston_view_from_global_fixed(constraint->view,
4324 constraint->pointer->x,
4325 constraint->pointer->y,
4326 &sx,
4327 &sy);
4328
4329 if (!is_within_constraint_region(constraint, sx, sy)) {
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004330 double xf = wl_fixed_to_double(sx);
4331 double yf = wl_fixed_to_double(sy);
4332 pixman_region32_t confine_region;
4333 struct wl_array borders;
4334 struct border *border;
4335 double closest_distance_2 = DBL_MAX;
4336 struct border *closest_border = NULL;
Jonas Ådahld3414f22016-07-22 17:56:31 +08004337
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004338 wl_array_init(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004339
Jonas Ådahld0be2bb2015-04-30 17:56:37 +08004340 pixman_region32_init(&confine_region);
4341 pixman_region32_intersect(&confine_region,
4342 &constraint->view->surface->input,
4343 &constraint->region);
4344 region_to_outline(&confine_region, &borders);
4345 pixman_region32_fini(&confine_region);
4346
4347 wl_array_for_each(border, &borders) {
4348 double distance_2;
4349
4350 distance_2 = point_to_border_distance_2(border, xf, yf);
4351 if (distance_2 < closest_distance_2) {
4352 closest_border = border;
4353 closest_distance_2 = distance_2;
4354 }
4355 }
4356 assert(closest_border);
4357
4358 warp_to_behind_border(closest_border, &sx, &sy);
4359
4360 wl_array_release(&borders);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004361
4362 weston_view_to_global_fixed(constraint->view, sx, sy, &x, &y);
4363 weston_pointer_move_to(constraint->pointer, x, y);
4364 }
4365}
4366
4367static void
4368confined_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
4369 uint32_t time,
4370 struct weston_pointer_motion_event *event)
4371{
4372 struct weston_pointer_constraint *constraint =
4373 container_of(grab, struct weston_pointer_constraint, grab);
4374 struct weston_pointer *pointer = grab->pointer;
4375 struct weston_surface *surface;
4376 wl_fixed_t x, y;
4377 wl_fixed_t old_sx = pointer->sx;
4378 wl_fixed_t old_sy = pointer->sy;
4379 pixman_region32_t confine_region;
4380
4381 assert(pointer->focus);
4382 assert(pointer->focus->surface == constraint->surface);
4383
4384 surface = pointer->focus->surface;
4385
4386 pixman_region32_init(&confine_region);
4387 pixman_region32_intersect(&confine_region,
4388 &surface->input,
4389 &constraint->region);
4390 weston_pointer_clamp_event_to_region(pointer, event,
4391 &confine_region, &x, &y);
4392 weston_pointer_move_to(pointer, x, y);
4393 pixman_region32_fini(&confine_region);
4394
4395 weston_view_from_global_fixed(pointer->focus, x, y,
4396 &pointer->sx, &pointer->sy);
4397
4398 if (old_sx != pointer->sx || old_sy != pointer->sy) {
Quentin Glidiccde13452016-08-12 10:41:32 +02004399 pointer_send_motion(pointer, time,
4400 pointer->sx, pointer->sy);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004401 }
4402
Quentin Glidiccde13452016-08-12 10:41:32 +02004403 pointer_send_relative_motion(pointer, time, event);
Jonas Ådahld3414f22016-07-22 17:56:31 +08004404}
4405
4406static void
4407confined_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
4408 uint32_t time,
4409 uint32_t button,
4410 uint32_t state_w)
4411{
4412 weston_pointer_send_button(grab->pointer, time, button, state_w);
4413}
4414
4415static void
4416confined_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
4417 uint32_t time,
4418 struct weston_pointer_axis_event *event)
4419{
4420 weston_pointer_send_axis(grab->pointer, time, event);
4421}
4422
4423static void
4424confined_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
4425 uint32_t source)
4426{
4427 weston_pointer_send_axis_source(grab->pointer, source);
4428}
4429
4430static void
4431confined_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
4432{
4433 weston_pointer_send_frame(grab->pointer);
4434}
4435
4436static void
4437confined_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
4438{
4439 struct weston_pointer_constraint *constraint =
4440 container_of(grab, struct weston_pointer_constraint, grab);
4441
4442 disable_pointer_constraint(constraint);
4443
4444 /* If this is a persistent constraint, re-add the surface destroy signal
4445 * listener only if we are currently not destroying the surface. */
4446 switch (constraint->lifetime) {
4447 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT:
4448 if (constraint->surface->resource)
4449 wl_signal_add(&constraint->surface->destroy_signal,
4450 &constraint->surface_destroy_listener);
4451 break;
4452 case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT:
4453 break;
4454 }
4455}
4456
4457static const struct weston_pointer_grab_interface
4458 confined_pointer_grab_interface = {
4459 confined_pointer_grab_pointer_focus,
4460 confined_pointer_grab_pointer_motion,
4461 confined_pointer_grab_pointer_button,
4462 confined_pointer_grab_pointer_axis,
4463 confined_pointer_grab_pointer_axis_source,
4464 confined_pointer_grab_pointer_frame,
4465 confined_pointer_grab_pointer_cancel,
4466};
4467
4468static void
4469confined_pointer_destroy(struct wl_client *client,
4470 struct wl_resource *resource)
4471{
4472 wl_resource_destroy(resource);
4473}
4474
4475static void
4476confined_pointer_set_region(struct wl_client *client,
4477 struct wl_resource *resource,
4478 struct wl_resource *region_resource)
4479{
4480 struct weston_pointer_constraint *constraint =
4481 wl_resource_get_user_data(resource);
4482 struct weston_region *region = region_resource ?
4483 wl_resource_get_user_data(region_resource) : NULL;
4484
4485 if (!constraint)
4486 return;
4487
4488 if (region) {
4489 pixman_region32_copy(&constraint->region_pending,
4490 &region->region);
4491 } else {
4492 pixman_region32_fini(&constraint->region_pending);
4493 region_init_infinite(&constraint->region_pending);
4494 }
4495 constraint->region_is_pending = true;
4496}
4497
4498static const struct zwp_confined_pointer_v1_interface confined_pointer_interface = {
4499 confined_pointer_destroy,
4500 confined_pointer_set_region,
4501};
4502
4503static void
4504pointer_constraints_confine_pointer(struct wl_client *client,
4505 struct wl_resource *resource,
4506 uint32_t id,
4507 struct wl_resource *surface_resource,
4508 struct wl_resource *pointer_resource,
4509 struct wl_resource *region_resource,
4510 uint32_t lifetime)
4511{
4512 struct weston_surface *surface =
4513 wl_resource_get_user_data(surface_resource);
4514 struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
4515 struct weston_region *region = region_resource ?
4516 wl_resource_get_user_data(region_resource) : NULL;
4517
4518 init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
4519 &zwp_confined_pointer_v1_interface,
4520 &confined_pointer_interface,
4521 &confined_pointer_grab_interface);
4522}
4523
4524static const struct zwp_pointer_constraints_v1_interface pointer_constraints_interface = {
4525 pointer_constraints_destroy,
4526 pointer_constraints_lock_pointer,
4527 pointer_constraints_confine_pointer,
4528};
4529
4530static void
4531bind_pointer_constraints(struct wl_client *client, void *data,
4532 uint32_t version, uint32_t id)
4533{
4534 struct wl_resource *resource;
4535
4536 resource = wl_resource_create(client,
4537 &zwp_pointer_constraints_v1_interface,
4538 1, id);
4539
4540 wl_resource_set_implementation(resource, &pointer_constraints_interface,
4541 NULL, NULL);
4542}
4543
Jonas Ådahl30d61d82014-10-22 21:21:17 +02004544int
4545weston_input_init(struct weston_compositor *compositor)
4546{
4547 if (!wl_global_create(compositor->wl_display,
4548 &zwp_relative_pointer_manager_v1_interface, 1,
4549 compositor, bind_relative_pointer_manager))
4550 return -1;
4551
Jonas Ådahld3414f22016-07-22 17:56:31 +08004552 if (!wl_global_create(compositor->wl_display,
4553 &zwp_pointer_constraints_v1_interface, 1,
4554 NULL, bind_pointer_constraints))
4555 return -1;
4556
Jonas Ådahl30d61d82014-10-22 21:21:17 +02004557 return 0;
4558}