833ce22e925c49feb444999445b398eab1b6b582
[platform/upstream/weston.git] / libweston / input.c
1 /*
2  * Copyright © 2013 Intel Corporation
3  *
4  * 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:
11  *
12  * 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.
24  */
25
26 #include "config.h"
27
28 #include <stdbool.h>
29 #include <stdlib.h>
30 #include <stdint.h>
31 #include <string.h>
32 #include <sys/mman.h>
33 #include <assert.h>
34 #include <unistd.h>
35 #include <values.h>
36 #include <fcntl.h>
37 #include <limits.h>
38
39 #include "shared/helpers.h"
40 #include "shared/os-compatibility.h"
41 #include "shared/timespec-util.h"
42 #include "compositor.h"
43 #include "relative-pointer-unstable-v1-server-protocol.h"
44 #include "pointer-constraints-unstable-v1-server-protocol.h"
45 #include "input-timestamps-unstable-v1-server-protocol.h"
46
47 enum pointer_constraint_type {
48         POINTER_CONSTRAINT_TYPE_LOCK,
49         POINTER_CONSTRAINT_TYPE_CONFINE,
50 };
51
52 enum motion_direction {
53         MOTION_DIRECTION_POSITIVE_X = 1 << 0,
54         MOTION_DIRECTION_NEGATIVE_X = 1 << 1,
55         MOTION_DIRECTION_POSITIVE_Y = 1 << 2,
56         MOTION_DIRECTION_NEGATIVE_Y = 1 << 3,
57 };
58
59 struct vec2d {
60         double x, y;
61 };
62
63 struct line {
64         struct vec2d a;
65         struct vec2d b;
66 };
67
68 struct border {
69         struct line line;
70         enum motion_direction blocking_dir;
71 };
72
73 static void
74 maybe_warp_confined_pointer(struct weston_pointer_constraint *constraint);
75
76 static void
77 empty_region(pixman_region32_t *region)
78 {
79         pixman_region32_fini(region);
80         pixman_region32_init(region);
81 }
82
83 static void
84 region_init_infinite(pixman_region32_t *region)
85 {
86         pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
87                                   UINT32_MAX, UINT32_MAX);
88 }
89
90 static void
91 send_timestamp(struct wl_resource *resource,
92                const struct timespec *time)
93 {
94         uint32_t tv_sec_hi, tv_sec_lo, tv_nsec;
95
96         timespec_to_proto(time, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
97         zwp_input_timestamps_v1_send_timestamp(resource, tv_sec_hi, tv_sec_lo,
98                                                tv_nsec);
99 }
100
101 static void
102 send_timestamps_for_input_resource(struct wl_resource *input_resource,
103                                    struct wl_list *list,
104                                    const struct timespec *time)
105 {
106         struct wl_resource *resource;
107
108         wl_resource_for_each(resource, list) {
109                 if (wl_resource_get_user_data(resource) == input_resource)
110                         send_timestamp(resource, time);
111         }
112 }
113
114 static void
115 remove_input_resource_from_timestamps(struct wl_resource *input_resource,
116                                       struct wl_list *list)
117 {
118         struct wl_resource *resource;
119
120         wl_resource_for_each(resource, list) {
121                 if (wl_resource_get_user_data(resource) == input_resource)
122                         wl_resource_set_user_data(resource, NULL);
123         }
124 }
125
126 static struct weston_pointer_client *
127 weston_pointer_client_create(struct wl_client *client)
128 {
129         struct weston_pointer_client *pointer_client;
130
131         pointer_client = zalloc(sizeof *pointer_client);
132         if (!pointer_client)
133                 return NULL;
134
135         pointer_client->client = client;
136         wl_list_init(&pointer_client->pointer_resources);
137         wl_list_init(&pointer_client->relative_pointer_resources);
138
139         return pointer_client;
140 }
141
142 static void
143 weston_pointer_client_destroy(struct weston_pointer_client *pointer_client)
144 {
145         struct wl_resource *resource;
146
147         wl_resource_for_each(resource, &pointer_client->pointer_resources) {
148                 wl_resource_set_user_data(resource, NULL);
149         }
150
151         wl_resource_for_each(resource,
152                              &pointer_client->relative_pointer_resources) {
153                 wl_resource_set_user_data(resource, NULL);
154         }
155
156         wl_list_remove(&pointer_client->pointer_resources);
157         wl_list_remove(&pointer_client->relative_pointer_resources);
158         free(pointer_client);
159 }
160
161 static bool
162 weston_pointer_client_is_empty(struct weston_pointer_client *pointer_client)
163 {
164         return (wl_list_empty(&pointer_client->pointer_resources) &&
165                 wl_list_empty(&pointer_client->relative_pointer_resources));
166 }
167
168 static struct weston_pointer_client *
169 weston_pointer_get_pointer_client(struct weston_pointer *pointer,
170                                   struct wl_client *client)
171 {
172         struct weston_pointer_client *pointer_client;
173
174         wl_list_for_each(pointer_client, &pointer->pointer_clients, link) {
175                 if (pointer_client->client == client)
176                         return pointer_client;
177         }
178
179         return NULL;
180 }
181
182 static struct weston_pointer_client *
183 weston_pointer_ensure_pointer_client(struct weston_pointer *pointer,
184                                      struct wl_client *client)
185 {
186         struct weston_pointer_client *pointer_client;
187
188         pointer_client = weston_pointer_get_pointer_client(pointer, client);
189         if (pointer_client)
190                 return pointer_client;
191
192         pointer_client = weston_pointer_client_create(client);
193         wl_list_insert(&pointer->pointer_clients, &pointer_client->link);
194
195         if (pointer->focus &&
196             pointer->focus->surface->resource &&
197             wl_resource_get_client(pointer->focus->surface->resource) == client) {
198                 pointer->focus_client = pointer_client;
199         }
200
201         return pointer_client;
202 }
203
204 static void
205 weston_pointer_cleanup_pointer_client(struct weston_pointer *pointer,
206                                       struct weston_pointer_client *pointer_client)
207 {
208         if (weston_pointer_client_is_empty(pointer_client)) {
209                 if (pointer->focus_client == pointer_client)
210                         pointer->focus_client = NULL;
211                 wl_list_remove(&pointer_client->link);
212                 weston_pointer_client_destroy(pointer_client);
213         }
214 }
215
216 static void
217 unbind_pointer_client_resource(struct wl_resource *resource)
218 {
219         struct weston_pointer *pointer = wl_resource_get_user_data(resource);
220         struct wl_client *client = wl_resource_get_client(resource);
221         struct weston_pointer_client *pointer_client;
222
223         wl_list_remove(wl_resource_get_link(resource));
224
225         if (pointer) {
226                 pointer_client = weston_pointer_get_pointer_client(pointer,
227                                                                    client);
228                 assert(pointer_client);
229                 remove_input_resource_from_timestamps(resource,
230                                                       &pointer->timestamps_list);
231                 weston_pointer_cleanup_pointer_client(pointer, pointer_client);
232         }
233 }
234
235 static void unbind_resource(struct wl_resource *resource)
236 {
237         wl_list_remove(wl_resource_get_link(resource));
238 }
239
240 WL_EXPORT void
241 weston_pointer_motion_to_abs(struct weston_pointer *pointer,
242                              struct weston_pointer_motion_event *event,
243                              wl_fixed_t *x, wl_fixed_t *y)
244 {
245         if (event->mask & WESTON_POINTER_MOTION_ABS) {
246                 *x = wl_fixed_from_double(event->x);
247                 *y = wl_fixed_from_double(event->y);
248         } else if (event->mask & WESTON_POINTER_MOTION_REL) {
249                 *x = pointer->x + wl_fixed_from_double(event->dx);
250                 *y = pointer->y + wl_fixed_from_double(event->dy);
251         } else {
252                 assert(!"invalid motion event");
253                 *x = *y = 0;
254         }
255 }
256
257 static bool
258 weston_pointer_motion_to_rel(struct weston_pointer *pointer,
259                              struct weston_pointer_motion_event *event,
260                              double *dx, double *dy,
261                              double *dx_unaccel, double *dy_unaccel)
262 {
263         if (event->mask & WESTON_POINTER_MOTION_REL &&
264             event->mask & WESTON_POINTER_MOTION_REL_UNACCEL) {
265                 *dx = event->dx;
266                 *dy = event->dy;
267                 *dx_unaccel = event->dx_unaccel;
268                 *dy_unaccel = event->dy_unaccel;
269                 return true;
270         } else if (event->mask & WESTON_POINTER_MOTION_REL) {
271                 *dx_unaccel = *dx = event->dx;
272                 *dy_unaccel = *dy = event->dy;
273                 return true;
274         } else if (event->mask & WESTON_POINTER_MOTION_REL_UNACCEL) {
275                 *dx_unaccel = *dx = event->dx_unaccel;
276                 *dy_unaccel = *dy = event->dy_unaccel;
277                 return true;
278         } else {
279                 return false;
280         }
281 }
282
283 WL_EXPORT void
284 weston_seat_repick(struct weston_seat *seat)
285 {
286         const struct weston_pointer *pointer = weston_seat_get_pointer(seat);
287
288         if (!pointer)
289                 return;
290
291         pointer->grab->interface->focus(pointer->grab);
292 }
293
294 static void
295 weston_compositor_idle_inhibit(struct weston_compositor *compositor)
296 {
297         weston_compositor_wake(compositor);
298         compositor->idle_inhibit++;
299 }
300
301 static void
302 weston_compositor_idle_release(struct weston_compositor *compositor)
303 {
304         compositor->idle_inhibit--;
305         weston_compositor_wake(compositor);
306 }
307
308 static void
309 pointer_focus_view_destroyed(struct wl_listener *listener, void *data)
310 {
311         struct weston_pointer *pointer =
312                 container_of(listener, struct weston_pointer,
313                              focus_view_listener);
314
315         weston_pointer_clear_focus(pointer);
316 }
317
318 static void
319 pointer_focus_resource_destroyed(struct wl_listener *listener, void *data)
320 {
321         struct weston_pointer *pointer =
322                 container_of(listener, struct weston_pointer,
323                              focus_resource_listener);
324
325         weston_pointer_clear_focus(pointer);
326 }
327
328 static void
329 keyboard_focus_resource_destroyed(struct wl_listener *listener, void *data)
330 {
331         struct weston_keyboard *keyboard =
332                 container_of(listener, struct weston_keyboard,
333                              focus_resource_listener);
334
335         weston_keyboard_set_focus(keyboard, NULL);
336 }
337
338 static void
339 touch_focus_view_destroyed(struct wl_listener *listener, void *data)
340 {
341         struct weston_touch *touch =
342                 container_of(listener, struct weston_touch,
343                              focus_view_listener);
344
345         weston_touch_set_focus(touch, NULL);
346 }
347
348 static void
349 touch_focus_resource_destroyed(struct wl_listener *listener, void *data)
350 {
351         struct weston_touch *touch =
352                 container_of(listener, struct weston_touch,
353                              focus_resource_listener);
354
355         weston_touch_set_focus(touch, NULL);
356 }
357
358 static void
359 move_resources(struct wl_list *destination, struct wl_list *source)
360 {
361         wl_list_insert_list(destination, source);
362         wl_list_init(source);
363 }
364
365 static void
366 move_resources_for_client(struct wl_list *destination,
367                           struct wl_list *source,
368                           struct wl_client *client)
369 {
370         struct wl_resource *resource, *tmp;
371         wl_resource_for_each_safe(resource, tmp, source) {
372                 if (wl_resource_get_client(resource) == client) {
373                         wl_list_remove(wl_resource_get_link(resource));
374                         wl_list_insert(destination,
375                                        wl_resource_get_link(resource));
376                 }
377         }
378 }
379
380 static void
381 default_grab_pointer_focus(struct weston_pointer_grab *grab)
382 {
383         struct weston_pointer *pointer = grab->pointer;
384         struct weston_view *view;
385         wl_fixed_t sx, sy;
386
387         if (pointer->button_count > 0)
388                 return;
389
390         view = weston_compositor_pick_view(pointer->seat->compositor,
391                                            pointer->x, pointer->y,
392                                            &sx, &sy);
393
394         if (pointer->focus != view || pointer->sx != sx || pointer->sy != sy)
395                 weston_pointer_set_focus(pointer, view, sx, sy);
396 }
397
398 static void
399 pointer_send_relative_motion(struct weston_pointer *pointer,
400                              const struct timespec *time,
401                              struct weston_pointer_motion_event *event)
402 {
403         uint64_t time_usec;
404         double dx, dy, dx_unaccel, dy_unaccel;
405         wl_fixed_t dxf, dyf, dxf_unaccel, dyf_unaccel;
406         struct wl_list *resource_list;
407         struct wl_resource *resource;
408
409         if (!pointer->focus_client)
410                 return;
411
412         if (!weston_pointer_motion_to_rel(pointer, event,
413                                           &dx, &dy,
414                                           &dx_unaccel, &dy_unaccel))
415                 return;
416
417         resource_list = &pointer->focus_client->relative_pointer_resources;
418         time_usec = timespec_to_usec(&event->time);
419         if (time_usec == 0)
420                 time_usec = timespec_to_usec(time);
421
422         dxf = wl_fixed_from_double(dx);
423         dyf = wl_fixed_from_double(dy);
424         dxf_unaccel = wl_fixed_from_double(dx_unaccel);
425         dyf_unaccel = wl_fixed_from_double(dy_unaccel);
426
427         wl_resource_for_each(resource, resource_list) {
428                 zwp_relative_pointer_v1_send_relative_motion(
429                         resource,
430                         (uint32_t) (time_usec >> 32),
431                         (uint32_t) time_usec,
432                         dxf, dyf,
433                         dxf_unaccel, dyf_unaccel);
434         }
435 }
436
437 static void
438 pointer_send_motion(struct weston_pointer *pointer,
439                     const struct timespec *time,
440                     wl_fixed_t sx, wl_fixed_t sy)
441 {
442         struct wl_list *resource_list;
443         struct wl_resource *resource;
444         uint32_t msecs;
445
446         if (!pointer->focus_client)
447                 return;
448
449         resource_list = &pointer->focus_client->pointer_resources;
450         msecs = timespec_to_msec(time);
451         wl_resource_for_each(resource, resource_list) {
452                 send_timestamps_for_input_resource(resource,
453                                                    &pointer->timestamps_list,
454                                                    time);
455                 wl_pointer_send_motion(resource, msecs, sx, sy);
456         }
457 }
458
459 WL_EXPORT void
460 weston_pointer_send_motion(struct weston_pointer *pointer,
461                            const struct timespec *time,
462                            struct weston_pointer_motion_event *event)
463 {
464         wl_fixed_t x, y;
465         wl_fixed_t old_sx = pointer->sx;
466         wl_fixed_t old_sy = pointer->sy;
467
468         if (pointer->focus) {
469                 weston_pointer_motion_to_abs(pointer, event, &x, &y);
470                 weston_view_from_global_fixed(pointer->focus, x, y,
471                                               &pointer->sx, &pointer->sy);
472         }
473
474         weston_pointer_move(pointer, event);
475
476         if (old_sx != pointer->sx || old_sy != pointer->sy) {
477                 pointer_send_motion(pointer, time,
478                                     pointer->sx, pointer->sy);
479         }
480
481         pointer_send_relative_motion(pointer, time, event);
482 }
483
484 static void
485 default_grab_pointer_motion(struct weston_pointer_grab *grab,
486                             const struct timespec *time,
487                             struct weston_pointer_motion_event *event)
488 {
489         weston_pointer_send_motion(grab->pointer, time, event);
490 }
491
492 /** Check if the pointer has focused resources.
493  *
494  * \param pointer The pointer to check for focused resources.
495  * \return Whether or not this pointer has focused resources
496  */
497 WL_EXPORT bool
498 weston_pointer_has_focus_resource(struct weston_pointer *pointer)
499 {
500         if (!pointer->focus_client)
501                 return false;
502
503         if (wl_list_empty(&pointer->focus_client->pointer_resources))
504                 return false;
505
506         return true;
507 }
508
509 /** Send wl_pointer.button events to focused resources.
510  *
511  * \param pointer The pointer where the button events originates from.
512  * \param time The timestamp of the event
513  * \param button The button value of the event
514  * \param value The state enum value of the event
515  *
516  * For every resource that is currently in focus, send a wl_pointer.button event
517  * with the passed parameters. The focused resources are the wl_pointer
518  * resources of the client which currently has the surface with pointer focus.
519  */
520 WL_EXPORT void
521 weston_pointer_send_button(struct weston_pointer *pointer,
522                            const struct timespec *time, uint32_t button,
523                            enum wl_pointer_button_state state)
524 {
525         struct wl_display *display = pointer->seat->compositor->wl_display;
526         struct wl_list *resource_list;
527         struct wl_resource *resource;
528         uint32_t serial;
529         uint32_t msecs;
530
531         if (!weston_pointer_has_focus_resource(pointer))
532                 return;
533
534         resource_list = &pointer->focus_client->pointer_resources;
535         serial = wl_display_next_serial(display);
536         msecs = timespec_to_msec(time);
537         wl_resource_for_each(resource, resource_list) {
538                 send_timestamps_for_input_resource(resource,
539                                                    &pointer->timestamps_list,
540                                                    time);
541                 wl_pointer_send_button(resource, serial, msecs, button, state);
542         }
543 }
544
545 static void
546 default_grab_pointer_button(struct weston_pointer_grab *grab,
547                             const struct timespec *time, uint32_t button,
548                             enum wl_pointer_button_state state)
549 {
550         struct weston_pointer *pointer = grab->pointer;
551         struct weston_compositor *compositor = pointer->seat->compositor;
552         struct weston_view *view;
553         wl_fixed_t sx, sy;
554
555         weston_pointer_send_button(pointer, time, button, state);
556
557         if (pointer->button_count == 0 &&
558             state == WL_POINTER_BUTTON_STATE_RELEASED) {
559                 view = weston_compositor_pick_view(compositor,
560                                                    pointer->x, pointer->y,
561                                                    &sx, &sy);
562
563                 weston_pointer_set_focus(pointer, view, sx, sy);
564         }
565 }
566
567 /** Send wl_pointer.axis events to focused resources.
568  *
569  * \param pointer The pointer where the axis events originates from.
570  * \param time The timestamp of the event
571  * \param axis The axis enum value of the event
572  * \param value The axis value of the event
573  *
574  * For every resource that is currently in focus, send a wl_pointer.axis event
575  * with the passed parameters. The focused resources are the wl_pointer
576  * resources of the client which currently has the surface with pointer focus.
577  */
578 WL_EXPORT void
579 weston_pointer_send_axis(struct weston_pointer *pointer,
580                          const struct timespec *time,
581                          struct weston_pointer_axis_event *event)
582 {
583         struct wl_resource *resource;
584         struct wl_list *resource_list;
585         uint32_t msecs;
586
587         if (!weston_pointer_has_focus_resource(pointer))
588                 return;
589
590         resource_list = &pointer->focus_client->pointer_resources;
591         msecs = timespec_to_msec(time);
592         wl_resource_for_each(resource, resource_list) {
593                 if (event->has_discrete &&
594                     wl_resource_get_version(resource) >=
595                     WL_POINTER_AXIS_DISCRETE_SINCE_VERSION)
596                         wl_pointer_send_axis_discrete(resource, event->axis,
597                                                       event->discrete);
598
599                 if (event->value) {
600                         send_timestamps_for_input_resource(resource,
601                                                            &pointer->timestamps_list,
602                                                            time);
603                         wl_pointer_send_axis(resource, msecs,
604                                              event->axis,
605                                              wl_fixed_from_double(event->value));
606                 } else if (wl_resource_get_version(resource) >=
607                          WL_POINTER_AXIS_STOP_SINCE_VERSION) {
608                         send_timestamps_for_input_resource(resource,
609                                                            &pointer->timestamps_list,
610                                                            time);
611                         wl_pointer_send_axis_stop(resource, msecs,
612                                                   event->axis);
613                 }
614         }
615 }
616
617 /** Send wl_pointer.axis_source events to focused resources.
618  *
619  * \param pointer The pointer where the axis_source events originates from.
620  * \param source The axis_source enum value of the event
621  *
622  * For every resource that is currently in focus, send a wl_pointer.axis_source
623  * event with the passed parameter. The focused resources are the wl_pointer
624  * resources of the client which currently has the surface with pointer focus.
625  */
626 WL_EXPORT void
627 weston_pointer_send_axis_source(struct weston_pointer *pointer,
628                                 enum wl_pointer_axis_source source)
629 {
630         struct wl_resource *resource;
631         struct wl_list *resource_list;
632
633         if (!weston_pointer_has_focus_resource(pointer))
634                 return;
635
636         resource_list = &pointer->focus_client->pointer_resources;
637         wl_resource_for_each(resource, resource_list) {
638                 if (wl_resource_get_version(resource) >=
639                     WL_POINTER_AXIS_SOURCE_SINCE_VERSION) {
640                         wl_pointer_send_axis_source(resource, source);
641                 }
642         }
643 }
644
645 static void
646 pointer_send_frame(struct wl_resource *resource)
647 {
648         if (wl_resource_get_version(resource) >=
649             WL_POINTER_FRAME_SINCE_VERSION) {
650                 wl_pointer_send_frame(resource);
651         }
652 }
653
654 /** Send wl_pointer.frame events to focused resources.
655  *
656  * \param pointer The pointer where the frame events originates from.
657  *
658  * For every resource that is currently in focus, send a wl_pointer.frame event.
659  * The focused resources are the wl_pointer resources of the client which
660  * currently has the surface with pointer focus.
661  */
662 WL_EXPORT void
663 weston_pointer_send_frame(struct weston_pointer *pointer)
664 {
665         struct wl_resource *resource;
666         struct wl_list *resource_list;
667
668         if (!weston_pointer_has_focus_resource(pointer))
669                 return;
670
671         resource_list = &pointer->focus_client->pointer_resources;
672         wl_resource_for_each(resource, resource_list)
673                 pointer_send_frame(resource);
674 }
675
676 static void
677 default_grab_pointer_axis(struct weston_pointer_grab *grab,
678                           const struct timespec *time,
679                           struct weston_pointer_axis_event *event)
680 {
681         weston_pointer_send_axis(grab->pointer, time, event);
682 }
683
684 static void
685 default_grab_pointer_axis_source(struct weston_pointer_grab *grab,
686                                  enum wl_pointer_axis_source source)
687 {
688         weston_pointer_send_axis_source(grab->pointer, source);
689 }
690
691 static void
692 default_grab_pointer_frame(struct weston_pointer_grab *grab)
693 {
694         weston_pointer_send_frame(grab->pointer);
695 }
696
697 static void
698 default_grab_pointer_cancel(struct weston_pointer_grab *grab)
699 {
700 }
701
702 static const struct weston_pointer_grab_interface
703                                 default_pointer_grab_interface = {
704         default_grab_pointer_focus,
705         default_grab_pointer_motion,
706         default_grab_pointer_button,
707         default_grab_pointer_axis,
708         default_grab_pointer_axis_source,
709         default_grab_pointer_frame,
710         default_grab_pointer_cancel,
711 };
712
713 /** Check if the touch has focused resources.
714  *
715  * \param touch The touch to check for focused resources.
716  * \return Whether or not this touch has focused resources
717  */
718 WL_EXPORT bool
719 weston_touch_has_focus_resource(struct weston_touch *touch)
720 {
721         if (!touch->focus)
722                 return false;
723
724         if (wl_list_empty(&touch->focus_resource_list))
725                 return false;
726
727         return true;
728 }
729
730 /** Send wl_touch.down events to focused resources.
731  *
732  * \param touch The touch where the down events originates from.
733  * \param time The timestamp of the event
734  * \param touch_id The touch_id value of the event
735  * \param x The x value of the event
736  * \param y The y value of the event
737  *
738  * For every resource that is currently in focus, send a wl_touch.down event
739  * with the passed parameters. The focused resources are the wl_touch
740  * resources of the client which currently has the surface with touch focus.
741  */
742 WL_EXPORT void
743 weston_touch_send_down(struct weston_touch *touch, const struct timespec *time,
744                        int touch_id, wl_fixed_t x, wl_fixed_t y)
745 {
746         struct wl_display *display = touch->seat->compositor->wl_display;
747         uint32_t serial;
748         struct wl_resource *resource;
749         struct wl_list *resource_list;
750         wl_fixed_t sx, sy;
751         uint32_t msecs;
752
753         if (!weston_touch_has_focus_resource(touch))
754                 return;
755
756         weston_view_from_global_fixed(touch->focus, x, y, &sx, &sy);
757
758         resource_list = &touch->focus_resource_list;
759         serial = wl_display_next_serial(display);
760         msecs = timespec_to_msec(time);
761         wl_resource_for_each(resource, resource_list) {
762                 send_timestamps_for_input_resource(resource,
763                                                    &touch->timestamps_list,
764                                                    time);
765                 wl_touch_send_down(resource, serial, msecs,
766                                    touch->focus->surface->resource,
767                                    touch_id, sx, sy);
768         }
769 }
770
771 static void
772 default_grab_touch_down(struct weston_touch_grab *grab,
773                         const struct timespec *time, int touch_id,
774                         wl_fixed_t x, wl_fixed_t y)
775 {
776         weston_touch_send_down(grab->touch, time, touch_id, x, y);
777 }
778
779 /** Send wl_touch.up events to focused resources.
780  *
781  * \param touch The touch where the up events originates from.
782  * \param time The timestamp of the event
783  * \param touch_id The touch_id value of the event
784  *
785  * For every resource that is currently in focus, send a wl_touch.up event
786  * with the passed parameters. The focused resources are the wl_touch
787  * resources of the client which currently has the surface with touch focus.
788  */
789 WL_EXPORT void
790 weston_touch_send_up(struct weston_touch *touch, const struct timespec *time,
791                      int touch_id)
792 {
793         struct wl_display *display = touch->seat->compositor->wl_display;
794         uint32_t serial;
795         struct wl_resource *resource;
796         struct wl_list *resource_list;
797         uint32_t msecs;
798
799         if (!weston_touch_has_focus_resource(touch))
800                 return;
801
802         resource_list = &touch->focus_resource_list;
803         serial = wl_display_next_serial(display);
804         msecs = timespec_to_msec(time);
805         wl_resource_for_each(resource, resource_list) {
806                 send_timestamps_for_input_resource(resource,
807                                                    &touch->timestamps_list,
808                                                    time);
809                 wl_touch_send_up(resource, serial, msecs, touch_id);
810         }
811 }
812
813 static void
814 default_grab_touch_up(struct weston_touch_grab *grab,
815                       const struct timespec *time, int touch_id)
816 {
817         weston_touch_send_up(grab->touch, time, touch_id);
818 }
819
820 /** Send wl_touch.motion events to focused resources.
821  *
822  * \param touch The touch where the motion events originates from.
823  * \param time The timestamp of the event
824  * \param touch_id The touch_id value of the event
825  * \param x The x value of the event
826  * \param y The y value of the event
827  *
828  * For every resource that is currently in focus, send a wl_touch.motion event
829  * with the passed parameters. The focused resources are the wl_touch
830  * resources of the client which currently has the surface with touch focus.
831  */
832 WL_EXPORT void
833 weston_touch_send_motion(struct weston_touch *touch,
834                          const struct timespec *time, int touch_id,
835                          wl_fixed_t x, wl_fixed_t y)
836 {
837         struct wl_resource *resource;
838         struct wl_list *resource_list;
839         wl_fixed_t sx, sy;
840         uint32_t msecs;
841
842         if (!weston_touch_has_focus_resource(touch))
843                 return;
844
845         weston_view_from_global_fixed(touch->focus, x, y, &sx, &sy);
846
847         resource_list = &touch->focus_resource_list;
848         msecs = timespec_to_msec(time);
849         wl_resource_for_each(resource, resource_list) {
850                 send_timestamps_for_input_resource(resource,
851                                                    &touch->timestamps_list,
852                                                    time);
853                 wl_touch_send_motion(resource, msecs,
854                                      touch_id, sx, sy);
855         }
856 }
857
858 static void
859 default_grab_touch_motion(struct weston_touch_grab *grab,
860                           const struct timespec *time, int touch_id,
861                           wl_fixed_t x, wl_fixed_t y)
862 {
863         weston_touch_send_motion(grab->touch, time, touch_id, x, y);
864 }
865
866
867 /** Send wl_touch.frame events to focused resources.
868  *
869  * \param touch The touch where the frame events originates from.
870  *
871  * For every resource that is currently in focus, send a wl_touch.frame event.
872  * The focused resources are the wl_touch resources of the client which
873  * currently has the surface with touch focus.
874  */
875 WL_EXPORT void
876 weston_touch_send_frame(struct weston_touch *touch)
877 {
878         struct wl_resource *resource;
879
880         if (!weston_touch_has_focus_resource(touch))
881                 return;
882
883         wl_resource_for_each(resource, &touch->focus_resource_list)
884                 wl_touch_send_frame(resource);
885 }
886
887 static void
888 default_grab_touch_frame(struct weston_touch_grab *grab)
889 {
890         weston_touch_send_frame(grab->touch);
891 }
892
893 static void
894 default_grab_touch_cancel(struct weston_touch_grab *grab)
895 {
896 }
897
898 static const struct weston_touch_grab_interface default_touch_grab_interface = {
899         default_grab_touch_down,
900         default_grab_touch_up,
901         default_grab_touch_motion,
902         default_grab_touch_frame,
903         default_grab_touch_cancel,
904 };
905
906 /** Check if the keyboard has focused resources.
907  *
908  * \param keyboard The keyboard to check for focused resources.
909  * \return Whether or not this keyboard has focused resources
910  */
911 WL_EXPORT bool
912 weston_keyboard_has_focus_resource(struct weston_keyboard *keyboard)
913 {
914         if (!keyboard->focus)
915                 return false;
916
917         if (wl_list_empty(&keyboard->focus_resource_list))
918                 return false;
919
920         return true;
921 }
922
923 /** Send wl_keyboard.key events to focused resources.
924  *
925  * \param keyboard The keyboard where the key events originates from.
926  * \param time The timestamp of the event
927  * \param key The key value of the event
928  * \param state The state enum value of the event
929  *
930  * For every resource that is currently in focus, send a wl_keyboard.key event
931  * with the passed parameters. The focused resources are the wl_keyboard
932  * resources of the client which currently has the surface with keyboard focus.
933  */
934 WL_EXPORT void
935 weston_keyboard_send_key(struct weston_keyboard *keyboard,
936                          const struct timespec *time, uint32_t key,
937                          enum wl_keyboard_key_state state)
938 {
939         struct wl_resource *resource;
940         struct wl_display *display = keyboard->seat->compositor->wl_display;
941         uint32_t serial;
942         struct wl_list *resource_list;
943         uint32_t msecs;
944
945         if (!weston_keyboard_has_focus_resource(keyboard))
946                 return;
947
948         resource_list = &keyboard->focus_resource_list;
949         serial = wl_display_next_serial(display);
950         msecs = timespec_to_msec(time);
951         wl_resource_for_each(resource, resource_list) {
952                 send_timestamps_for_input_resource(resource,
953                                                    &keyboard->timestamps_list,
954                                                    time);
955                 wl_keyboard_send_key(resource, serial, msecs, key, state);
956         }
957 };
958
959 static void
960 default_grab_keyboard_key(struct weston_keyboard_grab *grab,
961                           const struct timespec *time, uint32_t key,
962                           uint32_t state)
963 {
964         weston_keyboard_send_key(grab->keyboard, time, key, state);
965 }
966
967 static void
968 send_modifiers_to_resource(struct weston_keyboard *keyboard,
969                            struct wl_resource *resource,
970                            uint32_t serial)
971 {
972         wl_keyboard_send_modifiers(resource,
973                                    serial,
974                                    keyboard->modifiers.mods_depressed,
975                                    keyboard->modifiers.mods_latched,
976                                    keyboard->modifiers.mods_locked,
977                                    keyboard->modifiers.group);
978 }
979
980 static void
981 send_modifiers_to_client_in_list(struct wl_client *client,
982                                  struct wl_list *list,
983                                  uint32_t serial,
984                                  struct weston_keyboard *keyboard)
985 {
986         struct wl_resource *resource;
987
988         wl_resource_for_each(resource, list) {
989                 if (wl_resource_get_client(resource) == client)
990                         send_modifiers_to_resource(keyboard,
991                                                    resource,
992                                                    serial);
993         }
994 }
995
996 static struct weston_pointer_client *
997 find_pointer_client_for_surface(struct weston_pointer *pointer,
998                                 struct weston_surface *surface)
999 {
1000         struct wl_client *client;
1001
1002         if (!surface)
1003                 return NULL;
1004
1005         if (!surface->resource)
1006                 return NULL;
1007
1008         client = wl_resource_get_client(surface->resource);
1009         return weston_pointer_get_pointer_client(pointer, client);
1010 }
1011
1012 static struct weston_pointer_client *
1013 find_pointer_client_for_view(struct weston_pointer *pointer, struct weston_view *view)
1014 {
1015         if (!view)
1016                 return NULL;
1017
1018         return find_pointer_client_for_surface(pointer, view->surface);
1019 }
1020
1021 static struct wl_resource *
1022 find_resource_for_surface(struct wl_list *list, struct weston_surface *surface)
1023 {
1024         if (!surface)
1025                 return NULL;
1026
1027         if (!surface->resource)
1028                 return NULL;
1029
1030         return wl_resource_find_for_client(list, wl_resource_get_client(surface->resource));
1031 }
1032
1033 /** Send wl_keyboard.modifiers events to focused resources and pointer
1034  *  focused resources.
1035  *
1036  * \param keyboard The keyboard where the modifiers events originates from.
1037  * \param serial The serial of the event
1038  * \param mods_depressed The mods_depressed value of the event
1039  * \param mods_latched The mods_latched value of the event
1040  * \param mods_locked The mods_locked value of the event
1041  * \param group The group value of the event
1042  *
1043  * For every resource that is currently in focus, send a wl_keyboard.modifiers
1044  * event with the passed parameters. The focused resources are the wl_keyboard
1045  * resources of the client which currently has the surface with keyboard focus.
1046  * This also sends wl_keyboard.modifiers events to the wl_keyboard resources of
1047  * the client having pointer focus (if different from the keyboard focus client).
1048  */
1049 WL_EXPORT void
1050 weston_keyboard_send_modifiers(struct weston_keyboard *keyboard,
1051                                uint32_t serial, uint32_t mods_depressed,
1052                                uint32_t mods_latched,
1053                                uint32_t mods_locked, uint32_t group)
1054 {
1055         struct weston_pointer *pointer =
1056                 weston_seat_get_pointer(keyboard->seat);
1057
1058         if (weston_keyboard_has_focus_resource(keyboard)) {
1059                 struct wl_list *resource_list;
1060                 struct wl_resource *resource;
1061
1062                 resource_list = &keyboard->focus_resource_list;
1063                 wl_resource_for_each(resource, resource_list) {
1064                         wl_keyboard_send_modifiers(resource, serial,
1065                                                    mods_depressed, mods_latched,
1066                                                    mods_locked, group);
1067                 }
1068         }
1069
1070         if (pointer && pointer->focus && pointer->focus->surface->resource &&
1071             pointer->focus->surface != keyboard->focus) {
1072                 struct wl_client *pointer_client =
1073                         wl_resource_get_client(pointer->focus->surface->resource);
1074
1075                 send_modifiers_to_client_in_list(pointer_client,
1076                                                  &keyboard->resource_list,
1077                                                  serial,
1078                                                  keyboard);
1079         }
1080 }
1081
1082 static void
1083 default_grab_keyboard_modifiers(struct weston_keyboard_grab *grab,
1084                                 uint32_t serial, uint32_t mods_depressed,
1085                                 uint32_t mods_latched,
1086                                 uint32_t mods_locked, uint32_t group)
1087 {
1088         weston_keyboard_send_modifiers(grab->keyboard, serial, mods_depressed,
1089                                        mods_latched, mods_locked, group);
1090 }
1091
1092 static void
1093 default_grab_keyboard_cancel(struct weston_keyboard_grab *grab)
1094 {
1095 }
1096
1097 static const struct weston_keyboard_grab_interface
1098                                 default_keyboard_grab_interface = {
1099         default_grab_keyboard_key,
1100         default_grab_keyboard_modifiers,
1101         default_grab_keyboard_cancel,
1102 };
1103
1104 static void
1105 pointer_unmap_sprite(struct weston_pointer *pointer)
1106 {
1107         struct weston_surface *surface = pointer->sprite->surface;
1108
1109         if (weston_surface_is_mapped(surface))
1110                 weston_surface_unmap(surface);
1111
1112         wl_list_remove(&pointer->sprite_destroy_listener.link);
1113         surface->committed = NULL;
1114         surface->committed_private = NULL;
1115         weston_surface_set_label_func(surface, NULL);
1116         weston_view_destroy(pointer->sprite);
1117         pointer->sprite = NULL;
1118 }
1119
1120 static void
1121 pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
1122 {
1123         struct weston_pointer *pointer =
1124                 container_of(listener, struct weston_pointer,
1125                              sprite_destroy_listener);
1126
1127         pointer->sprite = NULL;
1128 }
1129
1130 static void
1131 weston_pointer_reset_state(struct weston_pointer *pointer)
1132 {
1133         pointer->button_count = 0;
1134 }
1135
1136 static void
1137 weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data);
1138
1139 static struct weston_pointer *
1140 weston_pointer_create(struct weston_seat *seat)
1141 {
1142         struct weston_pointer *pointer;
1143
1144         pointer = zalloc(sizeof *pointer);
1145         if (pointer == NULL)
1146                 return NULL;
1147
1148         wl_list_init(&pointer->pointer_clients);
1149         weston_pointer_set_default_grab(pointer,
1150                                         seat->compositor->default_pointer_grab);
1151         wl_list_init(&pointer->focus_resource_listener.link);
1152         pointer->focus_resource_listener.notify = pointer_focus_resource_destroyed;
1153         pointer->default_grab.pointer = pointer;
1154         pointer->grab = &pointer->default_grab;
1155         wl_signal_init(&pointer->motion_signal);
1156         wl_signal_init(&pointer->focus_signal);
1157         wl_list_init(&pointer->focus_view_listener.link);
1158         wl_signal_init(&pointer->destroy_signal);
1159         wl_list_init(&pointer->timestamps_list);
1160
1161         pointer->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
1162
1163         /* FIXME: Pick better co-ords. */
1164         pointer->x = wl_fixed_from_int(100);
1165         pointer->y = wl_fixed_from_int(100);
1166
1167         pointer->output_destroy_listener.notify =
1168                 weston_pointer_handle_output_destroy;
1169         wl_signal_add(&seat->compositor->output_destroyed_signal,
1170                       &pointer->output_destroy_listener);
1171
1172         pointer->sx = wl_fixed_from_int(-1000000);
1173         pointer->sy = wl_fixed_from_int(-1000000);
1174
1175         return pointer;
1176 }
1177
1178 static void
1179 weston_pointer_destroy(struct weston_pointer *pointer)
1180 {
1181         struct weston_pointer_client *pointer_client, *tmp;
1182
1183         wl_signal_emit(&pointer->destroy_signal, pointer);
1184
1185         if (pointer->sprite)
1186                 pointer_unmap_sprite(pointer);
1187
1188         wl_list_for_each_safe(pointer_client, tmp, &pointer->pointer_clients,
1189                               link) {
1190                 wl_list_remove(&pointer_client->link);
1191                 weston_pointer_client_destroy(pointer_client);
1192         }
1193
1194         wl_list_remove(&pointer->focus_resource_listener.link);
1195         wl_list_remove(&pointer->focus_view_listener.link);
1196         wl_list_remove(&pointer->output_destroy_listener.link);
1197         wl_list_remove(&pointer->timestamps_list);
1198         free(pointer);
1199 }
1200
1201 void
1202 weston_pointer_set_default_grab(struct weston_pointer *pointer,
1203                 const struct weston_pointer_grab_interface *interface)
1204 {
1205         if (interface)
1206                 pointer->default_grab.interface = interface;
1207         else
1208                 pointer->default_grab.interface =
1209                         &default_pointer_grab_interface;
1210 }
1211
1212 static struct weston_keyboard *
1213 weston_keyboard_create(void)
1214 {
1215         struct weston_keyboard *keyboard;
1216
1217         keyboard = zalloc(sizeof *keyboard);
1218         if (keyboard == NULL)
1219             return NULL;
1220
1221         wl_list_init(&keyboard->resource_list);
1222         wl_list_init(&keyboard->focus_resource_list);
1223         wl_list_init(&keyboard->focus_resource_listener.link);
1224         keyboard->focus_resource_listener.notify = keyboard_focus_resource_destroyed;
1225         wl_array_init(&keyboard->keys);
1226         keyboard->default_grab.interface = &default_keyboard_grab_interface;
1227         keyboard->default_grab.keyboard = keyboard;
1228         keyboard->grab = &keyboard->default_grab;
1229         wl_signal_init(&keyboard->focus_signal);
1230         wl_list_init(&keyboard->timestamps_list);
1231
1232         return keyboard;
1233 }
1234
1235 static void
1236 weston_xkb_info_destroy(struct weston_xkb_info *xkb_info);
1237
1238 static void
1239 weston_keyboard_destroy(struct weston_keyboard *keyboard)
1240 {
1241         struct wl_resource *resource;
1242
1243         wl_resource_for_each(resource, &keyboard->resource_list) {
1244                 wl_resource_set_user_data(resource, NULL);
1245         }
1246
1247         wl_resource_for_each(resource, &keyboard->focus_resource_list) {
1248                 wl_resource_set_user_data(resource, NULL);
1249         }
1250
1251         wl_list_remove(&keyboard->resource_list);
1252         wl_list_remove(&keyboard->focus_resource_list);
1253
1254         xkb_state_unref(keyboard->xkb_state.state);
1255         if (keyboard->xkb_info)
1256                 weston_xkb_info_destroy(keyboard->xkb_info);
1257         xkb_keymap_unref(keyboard->pending_keymap);
1258
1259         wl_array_release(&keyboard->keys);
1260         wl_list_remove(&keyboard->focus_resource_listener.link);
1261         wl_list_remove(&keyboard->timestamps_list);
1262         free(keyboard);
1263 }
1264
1265 static void
1266 weston_touch_reset_state(struct weston_touch *touch)
1267 {
1268         touch->num_tp = 0;
1269 }
1270
1271 static struct weston_touch *
1272 weston_touch_create(void)
1273 {
1274         struct weston_touch *touch;
1275
1276         touch = zalloc(sizeof *touch);
1277         if (touch == NULL)
1278                 return NULL;
1279
1280         wl_list_init(&touch->resource_list);
1281         wl_list_init(&touch->focus_resource_list);
1282         wl_list_init(&touch->focus_view_listener.link);
1283         touch->focus_view_listener.notify = touch_focus_view_destroyed;
1284         wl_list_init(&touch->focus_resource_listener.link);
1285         touch->focus_resource_listener.notify = touch_focus_resource_destroyed;
1286         touch->default_grab.interface = &default_touch_grab_interface;
1287         touch->default_grab.touch = touch;
1288         touch->grab = &touch->default_grab;
1289         wl_signal_init(&touch->focus_signal);
1290         wl_list_init(&touch->timestamps_list);
1291
1292         return touch;
1293 }
1294
1295 static void
1296 weston_touch_destroy(struct weston_touch *touch)
1297 {
1298         struct wl_resource *resource;
1299
1300         wl_resource_for_each(resource, &touch->resource_list) {
1301                 wl_resource_set_user_data(resource, NULL);
1302         }
1303
1304         wl_resource_for_each(resource, &touch->focus_resource_list) {
1305                 wl_resource_set_user_data(resource, NULL);
1306         }
1307
1308         wl_list_remove(&touch->resource_list);
1309         wl_list_remove(&touch->focus_resource_list);
1310         wl_list_remove(&touch->focus_view_listener.link);
1311         wl_list_remove(&touch->focus_resource_listener.link);
1312         wl_list_remove(&touch->timestamps_list);
1313         free(touch);
1314 }
1315
1316 static void
1317 seat_send_updated_caps(struct weston_seat *seat)
1318 {
1319         enum wl_seat_capability caps = 0;
1320         struct wl_resource *resource;
1321
1322         if (seat->pointer_device_count > 0)
1323                 caps |= WL_SEAT_CAPABILITY_POINTER;
1324         if (seat->keyboard_device_count > 0)
1325                 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
1326         if (seat->touch_device_count > 0)
1327                 caps |= WL_SEAT_CAPABILITY_TOUCH;
1328
1329         wl_resource_for_each(resource, &seat->base_resource_list) {
1330                 wl_seat_send_capabilities(resource, caps);
1331         }
1332         wl_signal_emit(&seat->updated_caps_signal, seat);
1333 }
1334
1335
1336 /** Clear the pointer focus
1337  *
1338  * \param pointer the pointer to clear focus for.
1339  *
1340  * This can be used to unset pointer focus and set the co-ordinates to the
1341  * arbitrary values we use for the no focus case.
1342  *
1343  * There's no requirement to use this function.  For example, passing the
1344  * results of a weston_compositor_pick_view() directly to
1345  * weston_pointer_set_focus() will do the right thing when no view is found.
1346  */
1347 WL_EXPORT void
1348 weston_pointer_clear_focus(struct weston_pointer *pointer)
1349 {
1350         weston_pointer_set_focus(pointer, NULL,
1351                                  wl_fixed_from_int(-1000000),
1352                                  wl_fixed_from_int(-1000000));
1353 }
1354
1355 WL_EXPORT void
1356 weston_pointer_set_focus(struct weston_pointer *pointer,
1357                          struct weston_view *view,
1358                          wl_fixed_t sx, wl_fixed_t sy)
1359 {
1360         struct weston_pointer_client *pointer_client;
1361         struct weston_keyboard *kbd = weston_seat_get_keyboard(pointer->seat);
1362         struct wl_resource *resource;
1363         struct wl_resource *surface_resource;
1364         struct wl_display *display = pointer->seat->compositor->wl_display;
1365         uint32_t serial;
1366         struct wl_list *focus_resource_list;
1367         int refocus = 0;
1368
1369         if ((!pointer->focus && view) ||
1370             (pointer->focus && !view) ||
1371             (pointer->focus && pointer->focus->surface != view->surface) ||
1372             pointer->sx != sx || pointer->sy != sy)
1373                 refocus = 1;
1374
1375         if (pointer->focus_client && refocus) {
1376                 focus_resource_list = &pointer->focus_client->pointer_resources;
1377                 if (!wl_list_empty(focus_resource_list)) {
1378                         serial = wl_display_next_serial(display);
1379                         surface_resource = pointer->focus->surface->resource;
1380                         wl_resource_for_each(resource, focus_resource_list) {
1381                                 wl_pointer_send_leave(resource, serial,
1382                                                       surface_resource);
1383                                 pointer_send_frame(resource);
1384                         }
1385                 }
1386
1387                 pointer->focus_client = NULL;
1388         }
1389
1390         pointer_client = find_pointer_client_for_view(pointer, view);
1391         if (pointer_client && refocus) {
1392                 struct wl_client *surface_client = pointer_client->client;
1393
1394                 serial = wl_display_next_serial(display);
1395
1396                 if (kbd && kbd->focus != view->surface)
1397                         send_modifiers_to_client_in_list(surface_client,
1398                                                          &kbd->resource_list,
1399                                                          serial,
1400                                                          kbd);
1401
1402                 pointer->focus_client = pointer_client;
1403
1404                 focus_resource_list = &pointer->focus_client->pointer_resources;
1405                 wl_resource_for_each(resource, focus_resource_list) {
1406                         wl_pointer_send_enter(resource,
1407                                               serial,
1408                                               view->surface->resource,
1409                                               sx, sy);
1410                         pointer_send_frame(resource);
1411                 }
1412
1413                 pointer->focus_serial = serial;
1414         }
1415
1416         wl_list_remove(&pointer->focus_view_listener.link);
1417         wl_list_init(&pointer->focus_view_listener.link);
1418         wl_list_remove(&pointer->focus_resource_listener.link);
1419         wl_list_init(&pointer->focus_resource_listener.link);
1420         if (view)
1421                 wl_signal_add(&view->destroy_signal, &pointer->focus_view_listener);
1422         if (view && view->surface->resource)
1423                 wl_resource_add_destroy_listener(view->surface->resource,
1424                                                  &pointer->focus_resource_listener);
1425
1426         pointer->focus = view;
1427         pointer->focus_view_listener.notify = pointer_focus_view_destroyed;
1428         pointer->sx = sx;
1429         pointer->sy = sy;
1430
1431         assert(view || sx == wl_fixed_from_int(-1000000));
1432         assert(view || sy == wl_fixed_from_int(-1000000));
1433
1434         wl_signal_emit(&pointer->focus_signal, pointer);
1435 }
1436
1437 static void
1438 send_enter_to_resource_list(struct wl_list *list,
1439                             struct weston_keyboard *keyboard,
1440                             struct weston_surface *surface,
1441                             uint32_t serial)
1442 {
1443         struct wl_resource *resource;
1444
1445         wl_resource_for_each(resource, list) {
1446                 send_modifiers_to_resource(keyboard, resource, serial);
1447                 wl_keyboard_send_enter(resource, serial,
1448                                        surface->resource,
1449                                        &keyboard->keys);
1450         }
1451 }
1452
1453 WL_EXPORT void
1454 weston_keyboard_set_focus(struct weston_keyboard *keyboard,
1455                           struct weston_surface *surface)
1456 {
1457         struct weston_seat *seat = keyboard->seat;
1458         struct wl_resource *resource;
1459         struct wl_display *display = keyboard->seat->compositor->wl_display;
1460         uint32_t serial;
1461         struct wl_list *focus_resource_list;
1462
1463         /* Keyboard focus on a surface without a client is equivalent to NULL
1464          * focus as nothing would react to the keyboard events anyway.
1465          * Just set focus to NULL instead - the destroy listener hangs on the
1466          * wl_resource anyway.
1467          */
1468         if (surface && !surface->resource)
1469                 surface = NULL;
1470
1471         focus_resource_list = &keyboard->focus_resource_list;
1472
1473         if (!wl_list_empty(focus_resource_list) && keyboard->focus != surface) {
1474                 serial = wl_display_next_serial(display);
1475                 wl_resource_for_each(resource, focus_resource_list) {
1476                         wl_keyboard_send_leave(resource, serial,
1477                                         keyboard->focus->resource);
1478                 }
1479                 move_resources(&keyboard->resource_list, focus_resource_list);
1480         }
1481
1482         if (find_resource_for_surface(&keyboard->resource_list, surface) &&
1483             keyboard->focus != surface) {
1484                 struct wl_client *surface_client =
1485                         wl_resource_get_client(surface->resource);
1486
1487                 serial = wl_display_next_serial(display);
1488
1489                 move_resources_for_client(focus_resource_list,
1490                                           &keyboard->resource_list,
1491                                           surface_client);
1492                 send_enter_to_resource_list(focus_resource_list,
1493                                             keyboard,
1494                                             surface,
1495                                             serial);
1496                 keyboard->focus_serial = serial;
1497         }
1498
1499         if (seat->saved_kbd_focus) {
1500                 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1501                 seat->saved_kbd_focus = NULL;
1502         }
1503
1504         wl_list_remove(&keyboard->focus_resource_listener.link);
1505         wl_list_init(&keyboard->focus_resource_listener.link);
1506         if (surface)
1507                 wl_resource_add_destroy_listener(surface->resource,
1508                                                  &keyboard->focus_resource_listener);
1509
1510         keyboard->focus = surface;
1511         wl_signal_emit(&keyboard->focus_signal, keyboard);
1512 }
1513
1514 /* Users of this function must manually manage the keyboard focus */
1515 WL_EXPORT void
1516 weston_keyboard_start_grab(struct weston_keyboard *keyboard,
1517                            struct weston_keyboard_grab *grab)
1518 {
1519         keyboard->grab = grab;
1520         grab->keyboard = keyboard;
1521 }
1522
1523 WL_EXPORT void
1524 weston_keyboard_end_grab(struct weston_keyboard *keyboard)
1525 {
1526         keyboard->grab = &keyboard->default_grab;
1527 }
1528
1529 static void
1530 weston_keyboard_cancel_grab(struct weston_keyboard *keyboard)
1531 {
1532         keyboard->grab->interface->cancel(keyboard->grab);
1533 }
1534
1535 WL_EXPORT void
1536 weston_pointer_start_grab(struct weston_pointer *pointer,
1537                           struct weston_pointer_grab *grab)
1538 {
1539         pointer->grab = grab;
1540         grab->pointer = pointer;
1541         pointer->grab->interface->focus(pointer->grab);
1542 }
1543
1544 WL_EXPORT void
1545 weston_pointer_end_grab(struct weston_pointer *pointer)
1546 {
1547         pointer->grab = &pointer->default_grab;
1548         pointer->grab->interface->focus(pointer->grab);
1549 }
1550
1551 static void
1552 weston_pointer_cancel_grab(struct weston_pointer *pointer)
1553 {
1554         pointer->grab->interface->cancel(pointer->grab);
1555 }
1556
1557 WL_EXPORT void
1558 weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
1559 {
1560         touch->grab = grab;
1561         grab->touch = touch;
1562 }
1563
1564 WL_EXPORT void
1565 weston_touch_end_grab(struct weston_touch *touch)
1566 {
1567         touch->grab = &touch->default_grab;
1568 }
1569
1570 static void
1571 weston_touch_cancel_grab(struct weston_touch *touch)
1572 {
1573         touch->grab->interface->cancel(touch->grab);
1574 }
1575
1576 static void
1577 weston_pointer_clamp_for_output(struct weston_pointer *pointer,
1578                                 struct weston_output *output,
1579                                 wl_fixed_t *fx, wl_fixed_t *fy)
1580 {
1581         int x, y;
1582
1583         x = wl_fixed_to_int(*fx);
1584         y = wl_fixed_to_int(*fy);
1585
1586         if (x < output->x)
1587                 *fx = wl_fixed_from_int(output->x);
1588         else if (x >= output->x + output->width)
1589                 *fx = wl_fixed_from_int(output->x +
1590                                         output->width - 1);
1591         if (y < output->y)
1592                 *fy = wl_fixed_from_int(output->y);
1593         else if (y >= output->y + output->height)
1594                 *fy = wl_fixed_from_int(output->y +
1595                                         output->height - 1);
1596 }
1597
1598 WL_EXPORT void
1599 weston_pointer_clamp(struct weston_pointer *pointer, wl_fixed_t *fx, wl_fixed_t *fy)
1600 {
1601         struct weston_compositor *ec = pointer->seat->compositor;
1602         struct weston_output *output, *prev = NULL;
1603         int x, y, old_x, old_y, valid = 0;
1604
1605         x = wl_fixed_to_int(*fx);
1606         y = wl_fixed_to_int(*fy);
1607         old_x = wl_fixed_to_int(pointer->x);
1608         old_y = wl_fixed_to_int(pointer->y);
1609
1610         wl_list_for_each(output, &ec->output_list, link) {
1611                 if (pointer->seat->output && pointer->seat->output != output)
1612                         continue;
1613                 if (pixman_region32_contains_point(&output->region,
1614                                                    x, y, NULL))
1615                         valid = 1;
1616                 if (pixman_region32_contains_point(&output->region,
1617                                                    old_x, old_y, NULL))
1618                         prev = output;
1619         }
1620
1621         if (!prev)
1622                 prev = pointer->seat->output;
1623
1624         if (prev && !valid)
1625                 weston_pointer_clamp_for_output(pointer, prev, fx, fy);
1626 }
1627
1628 static void
1629 weston_pointer_move_to(struct weston_pointer *pointer,
1630                        wl_fixed_t x, wl_fixed_t y)
1631 {
1632         int32_t ix, iy;
1633
1634         weston_pointer_clamp (pointer, &x, &y);
1635
1636         pointer->x = x;
1637         pointer->y = y;
1638
1639         ix = wl_fixed_to_int(x);
1640         iy = wl_fixed_to_int(y);
1641
1642         if (pointer->sprite) {
1643                 weston_view_set_position(pointer->sprite,
1644                                          ix - pointer->hotspot_x,
1645                                          iy - pointer->hotspot_y);
1646                 weston_view_schedule_repaint(pointer->sprite);
1647         }
1648
1649         pointer->grab->interface->focus(pointer->grab);
1650         wl_signal_emit(&pointer->motion_signal, pointer);
1651 }
1652
1653 WL_EXPORT void
1654 weston_pointer_move(struct weston_pointer *pointer,
1655                     struct weston_pointer_motion_event *event)
1656 {
1657         wl_fixed_t x, y;
1658
1659         weston_pointer_motion_to_abs(pointer, event, &x, &y);
1660         weston_pointer_move_to(pointer, x, y);
1661 }
1662
1663 /** Verify if the pointer is in a valid position and move it if it isn't.
1664  */
1665 static void
1666 weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data)
1667 {
1668         struct weston_pointer *pointer;
1669         struct weston_compositor *ec;
1670         struct weston_output *output, *closest = NULL;
1671         int x, y, distance, min = INT_MAX;
1672         wl_fixed_t fx, fy;
1673
1674         pointer = container_of(listener, struct weston_pointer,
1675                                output_destroy_listener);
1676         ec = pointer->seat->compositor;
1677
1678         x = wl_fixed_to_int(pointer->x);
1679         y = wl_fixed_to_int(pointer->y);
1680
1681         wl_list_for_each(output, &ec->output_list, link) {
1682                 if (pixman_region32_contains_point(&output->region,
1683                                                    x, y, NULL))
1684                         return;
1685
1686                 /* Aproximante the distance from the pointer to the center of
1687                  * the output. */
1688                 distance = abs(output->x + output->width / 2 - x) +
1689                            abs(output->y + output->height / 2 - y);
1690                 if (distance < min) {
1691                         min = distance;
1692                         closest = output;
1693                 }
1694         }
1695
1696         /* Nothing to do if there's no output left. */
1697         if (!closest)
1698                 return;
1699
1700         fx = pointer->x;
1701         fy = pointer->y;
1702
1703         weston_pointer_clamp_for_output(pointer, closest, &fx, &fy);
1704         weston_pointer_move_to(pointer, fx, fy);
1705 }
1706
1707 WL_EXPORT void
1708 notify_motion(struct weston_seat *seat,
1709               const struct timespec *time,
1710               struct weston_pointer_motion_event *event)
1711 {
1712         struct weston_compositor *ec = seat->compositor;
1713         struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1714
1715         weston_compositor_wake(ec);
1716         pointer->grab->interface->motion(pointer->grab, time, event);
1717 }
1718
1719 static void
1720 run_modifier_bindings(struct weston_seat *seat, uint32_t old, uint32_t new)
1721 {
1722         struct weston_compositor *compositor = seat->compositor;
1723         struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
1724         uint32_t diff;
1725         unsigned int i;
1726         struct {
1727                 uint32_t xkb;
1728                 enum weston_keyboard_modifier weston;
1729         } mods[] = {
1730                 { keyboard->xkb_info->ctrl_mod, MODIFIER_CTRL },
1731                 { keyboard->xkb_info->alt_mod, MODIFIER_ALT },
1732                 { keyboard->xkb_info->super_mod, MODIFIER_SUPER },
1733                 { keyboard->xkb_info->shift_mod, MODIFIER_SHIFT },
1734         };
1735
1736         diff = new & ~old;
1737         for (i = 0; i < ARRAY_LENGTH(mods); i++) {
1738                 if (diff & (1 << mods[i].xkb))
1739                         weston_compositor_run_modifier_binding(compositor,
1740                                                                keyboard,
1741                                                                mods[i].weston,
1742                                                                WL_KEYBOARD_KEY_STATE_PRESSED);
1743         }
1744
1745         diff = old & ~new;
1746         for (i = 0; i < ARRAY_LENGTH(mods); i++) {
1747                 if (diff & (1 << mods[i].xkb))
1748                         weston_compositor_run_modifier_binding(compositor,
1749                                                                keyboard,
1750                                                                mods[i].weston,
1751                                                                WL_KEYBOARD_KEY_STATE_RELEASED);
1752         }
1753 }
1754
1755 WL_EXPORT void
1756 notify_motion_absolute(struct weston_seat *seat, const struct timespec *time,
1757                        double x, double y)
1758 {
1759         struct weston_compositor *ec = seat->compositor;
1760         struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1761         struct weston_pointer_motion_event event = { 0 };
1762
1763         weston_compositor_wake(ec);
1764
1765         event = (struct weston_pointer_motion_event) {
1766                 .mask = WESTON_POINTER_MOTION_ABS,
1767                 .x = x,
1768                 .y = y,
1769         };
1770
1771         pointer->grab->interface->motion(pointer->grab, time, &event);
1772 }
1773
1774 static unsigned int
1775 peek_next_activate_serial(struct weston_compositor *c)
1776 {
1777         unsigned serial = c->activate_serial + 1;
1778
1779         return serial == 0 ? 1 : serial;
1780 }
1781
1782 static void
1783 inc_activate_serial(struct weston_compositor *c)
1784 {
1785         c->activate_serial = peek_next_activate_serial (c);
1786 }
1787
1788 WL_EXPORT void
1789 weston_view_activate(struct weston_view *view,
1790                      struct weston_seat *seat,
1791                      uint32_t flags)
1792 {
1793         struct weston_compositor *compositor = seat->compositor;
1794
1795         if (flags & WESTON_ACTIVATE_FLAG_CLICKED) {
1796                 view->click_to_activate_serial =
1797                         peek_next_activate_serial(compositor);
1798         }
1799
1800         weston_seat_set_keyboard_focus(seat, view->surface);
1801 }
1802
1803 WL_EXPORT void
1804 notify_button(struct weston_seat *seat, const struct timespec *time,
1805               int32_t button, enum wl_pointer_button_state state)
1806 {
1807         struct weston_compositor *compositor = seat->compositor;
1808         struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1809
1810         if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
1811                 weston_compositor_idle_inhibit(compositor);
1812                 if (pointer->button_count == 0) {
1813                         pointer->grab_button = button;
1814                         pointer->grab_time = *time;
1815                         pointer->grab_x = pointer->x;
1816                         pointer->grab_y = pointer->y;
1817                 }
1818                 pointer->button_count++;
1819         } else {
1820                 weston_compositor_idle_release(compositor);
1821                 pointer->button_count--;
1822         }
1823
1824         weston_compositor_run_button_binding(compositor, pointer, time, button,
1825                                              state);
1826
1827         pointer->grab->interface->button(pointer->grab, time, button, state);
1828
1829         if (pointer->button_count == 1)
1830                 pointer->grab_serial =
1831                         wl_display_get_serial(compositor->wl_display);
1832 }
1833
1834 WL_EXPORT void
1835 notify_axis(struct weston_seat *seat, const struct timespec *time,
1836             struct weston_pointer_axis_event *event)
1837 {
1838         struct weston_compositor *compositor = seat->compositor;
1839         struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1840
1841         weston_compositor_wake(compositor);
1842
1843         if (weston_compositor_run_axis_binding(compositor, pointer,
1844                                                time, event))
1845                 return;
1846
1847         pointer->grab->interface->axis(pointer->grab, time, event);
1848 }
1849
1850 WL_EXPORT void
1851 notify_axis_source(struct weston_seat *seat, uint32_t source)
1852 {
1853         struct weston_compositor *compositor = seat->compositor;
1854         struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1855
1856         weston_compositor_wake(compositor);
1857
1858         pointer->grab->interface->axis_source(pointer->grab, source);
1859 }
1860
1861 WL_EXPORT void
1862 notify_pointer_frame(struct weston_seat *seat)
1863 {
1864         struct weston_compositor *compositor = seat->compositor;
1865         struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1866
1867         weston_compositor_wake(compositor);
1868
1869         pointer->grab->interface->frame(pointer->grab);
1870 }
1871
1872 WL_EXPORT int
1873 weston_keyboard_set_locks(struct weston_keyboard *keyboard,
1874                           uint32_t mask, uint32_t value)
1875 {
1876         uint32_t serial;
1877         xkb_mod_mask_t mods_depressed, mods_latched, mods_locked, group;
1878         xkb_mod_mask_t num, caps;
1879
1880         /* We don't want the leds to go out of sync with the actual state
1881          * so if the backend has no way to change the leds don't try to
1882          * change the state */
1883         if (!keyboard->seat->led_update)
1884                 return -1;
1885
1886         mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
1887                                                 XKB_STATE_DEPRESSED);
1888         mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
1889                                                 XKB_STATE_LATCHED);
1890         mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
1891                                                 XKB_STATE_LOCKED);
1892         group = xkb_state_serialize_group(keyboard->xkb_state.state,
1893                                       XKB_STATE_EFFECTIVE);
1894
1895         num = (1 << keyboard->xkb_info->mod2_mod);
1896         caps = (1 << keyboard->xkb_info->caps_mod);
1897         if (mask & WESTON_NUM_LOCK) {
1898                 if (value & WESTON_NUM_LOCK)
1899                         mods_locked |= num;
1900                 else
1901                         mods_locked &= ~num;
1902         }
1903         if (mask & WESTON_CAPS_LOCK) {
1904                 if (value & WESTON_CAPS_LOCK)
1905                         mods_locked |= caps;
1906                 else
1907                         mods_locked &= ~caps;
1908         }
1909
1910         xkb_state_update_mask(keyboard->xkb_state.state, mods_depressed,
1911                               mods_latched, mods_locked, 0, 0, group);
1912
1913         serial = wl_display_next_serial(
1914                                 keyboard->seat->compositor->wl_display);
1915         notify_modifiers(keyboard->seat, serial);
1916
1917         return 0;
1918 }
1919
1920 WL_EXPORT void
1921 notify_modifiers(struct weston_seat *seat, uint32_t serial)
1922 {
1923         struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
1924         struct weston_keyboard_grab *grab = keyboard->grab;
1925         uint32_t mods_depressed, mods_latched, mods_locked, group;
1926         uint32_t mods_lookup;
1927         enum weston_led leds = 0;
1928         int changed = 0;
1929
1930         /* Serialize and update our internal state, checking to see if it's
1931          * different to the previous state. */
1932         mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
1933                                                   XKB_STATE_MODS_DEPRESSED);
1934         mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
1935                                                 XKB_STATE_MODS_LATCHED);
1936         mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
1937                                                XKB_STATE_MODS_LOCKED);
1938         group = xkb_state_serialize_layout(keyboard->xkb_state.state,
1939                                            XKB_STATE_LAYOUT_EFFECTIVE);
1940
1941         if (mods_depressed != keyboard->modifiers.mods_depressed ||
1942             mods_latched != keyboard->modifiers.mods_latched ||
1943             mods_locked != keyboard->modifiers.mods_locked ||
1944             group != keyboard->modifiers.group)
1945                 changed = 1;
1946
1947         run_modifier_bindings(seat, keyboard->modifiers.mods_depressed,
1948                               mods_depressed);
1949
1950         keyboard->modifiers.mods_depressed = mods_depressed;
1951         keyboard->modifiers.mods_latched = mods_latched;
1952         keyboard->modifiers.mods_locked = mods_locked;
1953         keyboard->modifiers.group = group;
1954
1955         /* And update the modifier_state for bindings. */
1956         mods_lookup = mods_depressed | mods_latched;
1957         seat->modifier_state = 0;
1958         if (mods_lookup & (1 << keyboard->xkb_info->ctrl_mod))
1959                 seat->modifier_state |= MODIFIER_CTRL;
1960         if (mods_lookup & (1 << keyboard->xkb_info->alt_mod))
1961                 seat->modifier_state |= MODIFIER_ALT;
1962         if (mods_lookup & (1 << keyboard->xkb_info->super_mod))
1963                 seat->modifier_state |= MODIFIER_SUPER;
1964         if (mods_lookup & (1 << keyboard->xkb_info->shift_mod))
1965                 seat->modifier_state |= MODIFIER_SHIFT;
1966
1967         /* Finally, notify the compositor that LEDs have changed. */
1968         if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
1969                                           keyboard->xkb_info->num_led))
1970                 leds |= LED_NUM_LOCK;
1971         if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
1972                                           keyboard->xkb_info->caps_led))
1973                 leds |= LED_CAPS_LOCK;
1974         if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
1975                                           keyboard->xkb_info->scroll_led))
1976                 leds |= LED_SCROLL_LOCK;
1977         if (leds != keyboard->xkb_state.leds && seat->led_update)
1978                 seat->led_update(seat, leds);
1979         keyboard->xkb_state.leds = leds;
1980
1981         if (changed) {
1982                 grab->interface->modifiers(grab,
1983                                            serial,
1984                                            keyboard->modifiers.mods_depressed,
1985                                            keyboard->modifiers.mods_latched,
1986                                            keyboard->modifiers.mods_locked,
1987                                            keyboard->modifiers.group);
1988         }
1989 }
1990
1991 static void
1992 update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
1993                       enum wl_keyboard_key_state state)
1994 {
1995         struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
1996         enum xkb_key_direction direction;
1997
1998         if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1999                 direction = XKB_KEY_DOWN;
2000         else
2001                 direction = XKB_KEY_UP;
2002
2003         /* Offset the keycode by 8, as the evdev XKB rules reflect X's
2004          * broken keycode system, which starts at 8. */
2005         xkb_state_update_key(keyboard->xkb_state.state, key + 8, direction);
2006
2007         notify_modifiers(seat, serial);
2008 }
2009
2010 static void
2011 send_keymap(struct wl_resource *resource, struct weston_xkb_info *xkb_info)
2012 {
2013         wl_keyboard_send_keymap(resource,
2014                                 WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2015                                 xkb_info->keymap_fd,
2016                                 xkb_info->keymap_size);
2017 }
2018
2019 static void
2020 send_modifiers(struct wl_resource *resource, uint32_t serial, struct weston_keyboard *keyboard)
2021 {
2022         wl_keyboard_send_modifiers(resource, serial,
2023                                    keyboard->modifiers.mods_depressed,
2024                                    keyboard->modifiers.mods_latched,
2025                                    keyboard->modifiers.mods_locked,
2026                                    keyboard->modifiers.group);
2027 }
2028
2029 static struct weston_xkb_info *
2030 weston_xkb_info_create(struct xkb_keymap *keymap);
2031
2032 static void
2033 update_keymap(struct weston_seat *seat)
2034 {
2035         struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2036         struct wl_resource *resource;
2037         struct weston_xkb_info *xkb_info;
2038         struct xkb_state *state;
2039         xkb_mod_mask_t latched_mods;
2040         xkb_mod_mask_t locked_mods;
2041
2042         xkb_info = weston_xkb_info_create(keyboard->pending_keymap);
2043
2044         xkb_keymap_unref(keyboard->pending_keymap);
2045         keyboard->pending_keymap = NULL;
2046
2047         if (!xkb_info) {
2048                 weston_log("failed to create XKB info\n");
2049                 return;
2050         }
2051
2052         state = xkb_state_new(xkb_info->keymap);
2053         if (!state) {
2054                 weston_log("failed to initialise XKB state\n");
2055                 weston_xkb_info_destroy(xkb_info);
2056                 return;
2057         }
2058
2059         latched_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
2060                                                 XKB_STATE_MODS_LATCHED);
2061         locked_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
2062                                                XKB_STATE_MODS_LOCKED);
2063         xkb_state_update_mask(state,
2064                               0, /* depressed */
2065                               latched_mods,
2066                               locked_mods,
2067                               0, 0, 0);
2068
2069         weston_xkb_info_destroy(keyboard->xkb_info);
2070         keyboard->xkb_info = xkb_info;
2071
2072         xkb_state_unref(keyboard->xkb_state.state);
2073         keyboard->xkb_state.state = state;
2074
2075         wl_resource_for_each(resource, &keyboard->resource_list)
2076                 send_keymap(resource, xkb_info);
2077         wl_resource_for_each(resource, &keyboard->focus_resource_list)
2078                 send_keymap(resource, xkb_info);
2079
2080         notify_modifiers(seat, wl_display_next_serial(seat->compositor->wl_display));
2081
2082         if (!latched_mods && !locked_mods)
2083                 return;
2084
2085         wl_resource_for_each(resource, &keyboard->resource_list)
2086                 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), keyboard);
2087         wl_resource_for_each(resource, &keyboard->focus_resource_list)
2088                 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), keyboard);
2089 }
2090
2091 WL_EXPORT void
2092 notify_key(struct weston_seat *seat, const struct timespec *time, uint32_t key,
2093            enum wl_keyboard_key_state state,
2094            enum weston_key_state_update update_state)
2095 {
2096         struct weston_compositor *compositor = seat->compositor;
2097         struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2098         struct weston_keyboard_grab *grab = keyboard->grab;
2099         uint32_t *k, *end;
2100
2101         if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2102                 weston_compositor_idle_inhibit(compositor);
2103         } else {
2104                 weston_compositor_idle_release(compositor);
2105         }
2106
2107         end = keyboard->keys.data + keyboard->keys.size;
2108         for (k = keyboard->keys.data; k < end; k++) {
2109                 if (*k == key) {
2110                         /* Ignore server-generated repeats. */
2111                         if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2112                                 return;
2113                         *k = *--end;
2114                 }
2115         }
2116         keyboard->keys.size = (void *) end - keyboard->keys.data;
2117         if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2118                 k = wl_array_add(&keyboard->keys, sizeof *k);
2119                 *k = key;
2120         }
2121
2122         if (grab == &keyboard->default_grab ||
2123             grab == &keyboard->input_method_grab) {
2124                 weston_compositor_run_key_binding(compositor, keyboard, time,
2125                                                   key, state);
2126                 grab = keyboard->grab;
2127         }
2128
2129         grab->interface->key(grab, time, key, state);
2130
2131         if (keyboard->pending_keymap &&
2132             keyboard->keys.size == 0)
2133                 update_keymap(seat);
2134
2135         if (update_state == STATE_UPDATE_AUTOMATIC) {
2136                 update_modifier_state(seat,
2137                                       wl_display_get_serial(compositor->wl_display),
2138                                       key,
2139                                       state);
2140         }
2141
2142         keyboard->grab_serial = wl_display_get_serial(compositor->wl_display);
2143         if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2144                 keyboard->grab_time = *time;
2145                 keyboard->grab_key = key;
2146         }
2147 }
2148
2149 WL_EXPORT void
2150 notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
2151                      double x, double y)
2152 {
2153         struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2154
2155         if (output) {
2156                 weston_pointer_move_to(pointer,
2157                                        wl_fixed_from_double(x),
2158                                        wl_fixed_from_double(y));
2159         } else {
2160                 /* FIXME: We should call weston_pointer_set_focus(seat,
2161                  * NULL) here, but somehow that breaks re-entry... */
2162         }
2163 }
2164
2165 static void
2166 destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
2167 {
2168         struct weston_seat *ws;
2169
2170         ws = container_of(listener, struct weston_seat,
2171                           saved_kbd_focus_listener);
2172
2173         ws->saved_kbd_focus = NULL;
2174 }
2175
2176 WL_EXPORT void
2177 notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
2178                          enum weston_key_state_update update_state)
2179 {
2180         struct weston_compositor *compositor = seat->compositor;
2181         struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2182         struct weston_surface *surface;
2183         uint32_t *k, serial;
2184
2185         serial = wl_display_next_serial(compositor->wl_display);
2186         wl_array_copy(&keyboard->keys, keys);
2187         wl_array_for_each(k, &keyboard->keys) {
2188                 weston_compositor_idle_inhibit(compositor);
2189                 if (update_state == STATE_UPDATE_AUTOMATIC)
2190                         update_modifier_state(seat, serial, *k,
2191                                               WL_KEYBOARD_KEY_STATE_PRESSED);
2192         }
2193
2194         surface = seat->saved_kbd_focus;
2195         if (surface) {
2196                 weston_keyboard_set_focus(keyboard, surface);
2197         }
2198 }
2199
2200 WL_EXPORT void
2201 notify_keyboard_focus_out(struct weston_seat *seat)
2202 {
2203         struct weston_compositor *compositor = seat->compositor;
2204         struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2205         struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2206         struct weston_surface *focus = keyboard->focus;
2207         uint32_t *k, serial;
2208
2209         serial = wl_display_next_serial(compositor->wl_display);
2210         wl_array_for_each(k, &keyboard->keys) {
2211                 weston_compositor_idle_release(compositor);
2212                 update_modifier_state(seat, serial, *k,
2213                                       WL_KEYBOARD_KEY_STATE_RELEASED);
2214         }
2215
2216         seat->modifier_state = 0;
2217
2218         weston_keyboard_set_focus(keyboard, NULL);
2219         weston_keyboard_cancel_grab(keyboard);
2220         if (pointer)
2221                 weston_pointer_cancel_grab(pointer);
2222
2223         if (focus) {
2224                 seat->saved_kbd_focus = focus;
2225                 seat->saved_kbd_focus_listener.notify =
2226                         destroy_device_saved_kbd_focus;
2227                 wl_signal_add(&focus->destroy_signal,
2228                               &seat->saved_kbd_focus_listener);
2229         }
2230 }
2231
2232 WL_EXPORT void
2233 weston_touch_set_focus(struct weston_touch *touch, struct weston_view *view)
2234 {
2235         struct wl_list *focus_resource_list;
2236
2237         focus_resource_list = &touch->focus_resource_list;
2238
2239         if (view && touch->focus &&
2240             touch->focus->surface == view->surface) {
2241                 touch->focus = view;
2242                 return;
2243         }
2244
2245         wl_list_remove(&touch->focus_resource_listener.link);
2246         wl_list_init(&touch->focus_resource_listener.link);
2247         wl_list_remove(&touch->focus_view_listener.link);
2248         wl_list_init(&touch->focus_view_listener.link);
2249
2250         if (!wl_list_empty(focus_resource_list)) {
2251                 move_resources(&touch->resource_list,
2252                                focus_resource_list);
2253         }
2254
2255         if (view) {
2256                 struct wl_client *surface_client;
2257
2258                 if (!view->surface->resource) {
2259                         touch->focus = NULL;
2260                         return;
2261                 }
2262
2263                 surface_client = wl_resource_get_client(view->surface->resource);
2264                 move_resources_for_client(focus_resource_list,
2265                                           &touch->resource_list,
2266                                           surface_client);
2267                 wl_resource_add_destroy_listener(view->surface->resource,
2268                                                  &touch->focus_resource_listener);
2269                 wl_signal_add(&view->destroy_signal, &touch->focus_view_listener);
2270         }
2271         touch->focus = view;
2272 }
2273
2274 /**
2275  * notify_touch - emulates button touches and notifies surfaces accordingly.
2276  *
2277  * It assumes always the correct cycle sequence until it gets here: touch_down
2278  * → touch_update → ... → touch_update → touch_end. The driver is responsible
2279  * for sending along such order.
2280  *
2281  */
2282 WL_EXPORT void
2283 notify_touch(struct weston_seat *seat, const struct timespec *time,
2284              int touch_id, double double_x, double double_y, int touch_type)
2285 {
2286         struct weston_compositor *ec = seat->compositor;
2287         struct weston_touch *touch = weston_seat_get_touch(seat);
2288         struct weston_touch_grab *grab = touch->grab;
2289         struct weston_view *ev;
2290         wl_fixed_t sx, sy;
2291         wl_fixed_t x = wl_fixed_from_double(double_x);
2292         wl_fixed_t y = wl_fixed_from_double(double_y);
2293
2294         /* Update grab's global coordinates. */
2295         if (touch_id == touch->grab_touch_id && touch_type != WL_TOUCH_UP) {
2296                 touch->grab_x = x;
2297                 touch->grab_y = y;
2298         }
2299
2300         switch (touch_type) {
2301         case WL_TOUCH_DOWN:
2302                 weston_compositor_idle_inhibit(ec);
2303
2304                 touch->num_tp++;
2305
2306                 /* the first finger down picks the view, and all further go
2307                  * to that view for the remainder of the touch session i.e.
2308                  * until all touch points are up again. */
2309                 if (touch->num_tp == 1) {
2310                         ev = weston_compositor_pick_view(ec, x, y, &sx, &sy);
2311                         weston_touch_set_focus(touch, ev);
2312                 } else if (!touch->focus) {
2313                         /* Unexpected condition: We have non-initial touch but
2314                          * there is no focused surface.
2315                          */
2316                         weston_log("touch event received with %d points down "
2317                                    "but no surface focused\n", touch->num_tp);
2318                         return;
2319                 }
2320
2321                 weston_compositor_run_touch_binding(ec, touch,
2322                                                     time, touch_type);
2323
2324                 grab->interface->down(grab, time, touch_id, x, y);
2325                 if (touch->num_tp == 1) {
2326                         touch->grab_serial =
2327                                 wl_display_get_serial(ec->wl_display);
2328                         touch->grab_touch_id = touch_id;
2329                         touch->grab_time = *time;
2330                         touch->grab_x = x;
2331                         touch->grab_y = y;
2332                 }
2333
2334                 break;
2335         case WL_TOUCH_MOTION:
2336                 ev = touch->focus;
2337                 if (!ev)
2338                         break;
2339
2340                 grab->interface->motion(grab, time, touch_id, x, y);
2341                 break;
2342         case WL_TOUCH_UP:
2343                 if (touch->num_tp == 0) {
2344                         /* This can happen if we start out with one or
2345                          * more fingers on the touch screen, in which
2346                          * case we didn't get the corresponding down
2347                          * event. */
2348                         weston_log("unmatched touch up event\n");
2349                         break;
2350                 }
2351                 weston_compositor_idle_release(ec);
2352                 touch->num_tp--;
2353
2354                 grab->interface->up(grab, time, touch_id);
2355                 if (touch->num_tp == 0)
2356                         weston_touch_set_focus(touch, NULL);
2357                 break;
2358         }
2359 }
2360
2361 WL_EXPORT void
2362 notify_touch_frame(struct weston_seat *seat)
2363 {
2364         struct weston_touch *touch = weston_seat_get_touch(seat);
2365         struct weston_touch_grab *grab = touch->grab;
2366
2367         grab->interface->frame(grab);
2368 }
2369
2370 WL_EXPORT void
2371 notify_touch_cancel(struct weston_seat *seat)
2372 {
2373         struct weston_touch *touch = weston_seat_get_touch(seat);
2374         struct weston_touch_grab *grab = touch->grab;
2375
2376         grab->interface->cancel(grab);
2377 }
2378
2379 static int
2380 pointer_cursor_surface_get_label(struct weston_surface *surface,
2381                                  char *buf, size_t len)
2382 {
2383         return snprintf(buf, len, "cursor");
2384 }
2385
2386 static void
2387 pointer_cursor_surface_committed(struct weston_surface *es,
2388                                  int32_t dx, int32_t dy)
2389 {
2390         struct weston_pointer *pointer = es->committed_private;
2391         int x, y;
2392
2393         if (es->width == 0)
2394                 return;
2395
2396         assert(es == pointer->sprite->surface);
2397
2398         pointer->hotspot_x -= dx;
2399         pointer->hotspot_y -= dy;
2400
2401         x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
2402         y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
2403
2404         weston_view_set_position(pointer->sprite, x, y);
2405
2406         empty_region(&es->pending.input);
2407         empty_region(&es->input);
2408
2409         if (!weston_surface_is_mapped(es)) {
2410                 weston_layer_entry_insert(&es->compositor->cursor_layer.view_list,
2411                                           &pointer->sprite->layer_link);
2412                 weston_view_update_transform(pointer->sprite);
2413                 es->is_mapped = true;
2414                 pointer->sprite->is_mapped = true;
2415         }
2416 }
2417
2418 static void
2419 pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2420                    uint32_t serial, struct wl_resource *surface_resource,
2421                    int32_t x, int32_t y)
2422 {
2423         struct weston_pointer *pointer = wl_resource_get_user_data(resource);
2424         struct weston_surface *surface = NULL;
2425
2426         if (!pointer)
2427                 return;
2428
2429         if (surface_resource)
2430                 surface = wl_resource_get_user_data(surface_resource);
2431
2432         if (pointer->focus == NULL)
2433                 return;
2434         /* pointer->focus->surface->resource can be NULL. Surfaces like the
2435         black_surface used in shell.c for fullscreen don't have
2436         a resource, but can still have focus */
2437         if (pointer->focus->surface->resource == NULL)
2438                 return;
2439         if (wl_resource_get_client(pointer->focus->surface->resource) != client)
2440                 return;
2441         if (pointer->focus_serial - serial > UINT32_MAX / 2)
2442                 return;
2443
2444         if (!surface) {
2445                 if (pointer->sprite)
2446                         pointer_unmap_sprite(pointer);
2447                 return;
2448         }
2449
2450         if (pointer->sprite && pointer->sprite->surface == surface &&
2451             pointer->hotspot_x == x && pointer->hotspot_y == y)
2452                 return;
2453
2454         if (!pointer->sprite || pointer->sprite->surface != surface) {
2455                 if (weston_surface_set_role(surface, "wl_pointer-cursor",
2456                                             resource,
2457                                             WL_POINTER_ERROR_ROLE) < 0)
2458                         return;
2459
2460                 if (pointer->sprite)
2461                         pointer_unmap_sprite(pointer);
2462
2463                 wl_signal_add(&surface->destroy_signal,
2464                               &pointer->sprite_destroy_listener);
2465
2466                 surface->committed = pointer_cursor_surface_committed;
2467                 surface->committed_private = pointer;
2468                 weston_surface_set_label_func(surface,
2469                                             pointer_cursor_surface_get_label);
2470                 pointer->sprite = weston_view_create(surface);
2471         }
2472
2473         pointer->hotspot_x = x;
2474         pointer->hotspot_y = y;
2475
2476         if (surface->buffer_ref.buffer) {
2477                 pointer_cursor_surface_committed(surface, 0, 0);
2478                 weston_view_schedule_repaint(pointer->sprite);
2479         }
2480 }
2481
2482 static void
2483 pointer_release(struct wl_client *client, struct wl_resource *resource)
2484 {
2485         wl_resource_destroy(resource);
2486 }
2487
2488 static const struct wl_pointer_interface pointer_interface = {
2489         pointer_set_cursor,
2490         pointer_release
2491 };
2492
2493 static void
2494 seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2495                  uint32_t id)
2496 {
2497         struct weston_seat *seat = wl_resource_get_user_data(resource);
2498         /* We use the pointer_state directly, which means we'll
2499          * give a wl_pointer if the seat has ever had one - even though
2500          * the spec explicitly states that this request only takes effect
2501          * if the seat has the pointer capability.
2502          *
2503          * This prevents a race between the compositor sending new
2504          * capabilities and the client trying to use the old ones.
2505          */
2506         struct weston_pointer *pointer = seat ? seat->pointer_state : NULL;
2507         struct wl_resource *cr;
2508         struct weston_pointer_client *pointer_client;
2509
2510         cr = wl_resource_create(client, &wl_pointer_interface,
2511                                 wl_resource_get_version(resource), id);
2512         if (cr == NULL) {
2513                 wl_client_post_no_memory(client);
2514                 return;
2515         }
2516
2517         wl_list_init(wl_resource_get_link(cr));
2518         wl_resource_set_implementation(cr, &pointer_interface, pointer,
2519                                        unbind_pointer_client_resource);
2520
2521         /* If we don't have a pointer_state, the resource is inert, so there
2522          * is nothing more to set up */
2523         if (!pointer)
2524                 return;
2525
2526         pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
2527         if (!pointer_client) {
2528                 wl_client_post_no_memory(client);
2529                 return;
2530         }
2531
2532         wl_list_insert(&pointer_client->pointer_resources,
2533                        wl_resource_get_link(cr));
2534
2535         if (pointer->focus && pointer->focus->surface->resource &&
2536             wl_resource_get_client(pointer->focus->surface->resource) == client) {
2537                 wl_fixed_t sx, sy;
2538
2539                 weston_view_from_global_fixed(pointer->focus,
2540                                               pointer->x,
2541                                               pointer->y,
2542                                               &sx, &sy);
2543
2544                 wl_pointer_send_enter(cr,
2545                                       pointer->focus_serial,
2546                                       pointer->focus->surface->resource,
2547                                       sx, sy);
2548                 pointer_send_frame(cr);
2549         }
2550 }
2551
2552 static void
2553 destroy_keyboard_resource(struct wl_resource *resource)
2554 {
2555         struct weston_keyboard *keyboard = wl_resource_get_user_data(resource);
2556
2557         wl_list_remove(wl_resource_get_link(resource));
2558
2559         if (keyboard) {
2560                 remove_input_resource_from_timestamps(resource,
2561                                                       &keyboard->timestamps_list);
2562         }
2563 }
2564
2565 static void
2566 keyboard_release(struct wl_client *client, struct wl_resource *resource)
2567 {
2568         wl_resource_destroy(resource);
2569 }
2570
2571 static const struct wl_keyboard_interface keyboard_interface = {
2572         keyboard_release
2573 };
2574
2575 static bool
2576 should_send_modifiers_to_client(struct weston_seat *seat,
2577                                 struct wl_client *client)
2578 {
2579         struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2580         struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2581
2582         if (keyboard &&
2583             keyboard->focus &&
2584             keyboard->focus->resource &&
2585             wl_resource_get_client(keyboard->focus->resource) == client)
2586                 return true;
2587
2588         if (pointer &&
2589             pointer->focus &&
2590             pointer->focus->surface->resource &&
2591             wl_resource_get_client(pointer->focus->surface->resource) == client)
2592                 return true;
2593
2594         return false;
2595 }
2596
2597 static void
2598 seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2599                   uint32_t id)
2600 {
2601         struct weston_seat *seat = wl_resource_get_user_data(resource);
2602         /* We use the keyboard_state directly, which means we'll
2603          * give a wl_keyboard if the seat has ever had one - even though
2604          * the spec explicitly states that this request only takes effect
2605          * if the seat has the keyboard capability.
2606          *
2607          * This prevents a race between the compositor sending new
2608          * capabilities and the client trying to use the old ones.
2609          */
2610         struct weston_keyboard *keyboard = seat ? seat->keyboard_state : NULL;
2611         struct wl_resource *cr;
2612
2613         cr = wl_resource_create(client, &wl_keyboard_interface,
2614                                 wl_resource_get_version(resource), id);
2615         if (cr == NULL) {
2616                 wl_client_post_no_memory(client);
2617                 return;
2618         }
2619
2620         wl_list_init(wl_resource_get_link(cr));
2621         wl_resource_set_implementation(cr, &keyboard_interface,
2622                                        keyboard, destroy_keyboard_resource);
2623
2624         /* If we don't have a keyboard_state, the resource is inert, so there
2625          * is nothing more to set up */
2626         if (!keyboard)
2627                 return;
2628
2629         /* May be moved to focused list later by either
2630          * weston_keyboard_set_focus or directly if this client is already
2631          * focused */
2632         wl_list_insert(&keyboard->resource_list, wl_resource_get_link(cr));
2633
2634         if (wl_resource_get_version(cr) >= WL_KEYBOARD_REPEAT_INFO_SINCE_VERSION) {
2635                 wl_keyboard_send_repeat_info(cr,
2636                                              seat->compositor->kb_repeat_rate,
2637                                              seat->compositor->kb_repeat_delay);
2638         }
2639
2640         wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2641                                 keyboard->xkb_info->keymap_fd,
2642                                 keyboard->xkb_info->keymap_size);
2643
2644         if (should_send_modifiers_to_client(seat, client)) {
2645                 send_modifiers_to_resource(keyboard,
2646                                            cr,
2647                                            keyboard->focus_serial);
2648         }
2649
2650         if (keyboard->focus && keyboard->focus->resource &&
2651             wl_resource_get_client(keyboard->focus->resource) == client) {
2652                 struct weston_surface *surface =
2653                         (struct weston_surface *)keyboard->focus;
2654
2655                 wl_list_remove(wl_resource_get_link(cr));
2656                 wl_list_insert(&keyboard->focus_resource_list,
2657                                wl_resource_get_link(cr));
2658                 wl_keyboard_send_enter(cr,
2659                                        keyboard->focus_serial,
2660                                        surface->resource,
2661                                        &keyboard->keys);
2662
2663                 /* If this is the first keyboard resource for this
2664                  * client... */
2665                 if (keyboard->focus_resource_list.prev ==
2666                     wl_resource_get_link(cr))
2667                         wl_data_device_set_keyboard_focus(seat);
2668         }
2669 }
2670
2671 static void
2672 destroy_touch_resource(struct wl_resource *resource)
2673 {
2674         struct weston_touch *touch = wl_resource_get_user_data(resource);
2675
2676         wl_list_remove(wl_resource_get_link(resource));
2677
2678         if (touch) {
2679                 remove_input_resource_from_timestamps(resource,
2680                                                       &touch->timestamps_list);
2681         }
2682 }
2683
2684 static void
2685 touch_release(struct wl_client *client, struct wl_resource *resource)
2686 {
2687         wl_resource_destroy(resource);
2688 }
2689
2690 static const struct wl_touch_interface touch_interface = {
2691         touch_release
2692 };
2693
2694 static void
2695 seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2696                uint32_t id)
2697 {
2698         struct weston_seat *seat = wl_resource_get_user_data(resource);
2699         /* We use the touch_state directly, which means we'll
2700          * give a wl_touch if the seat has ever had one - even though
2701          * the spec explicitly states that this request only takes effect
2702          * if the seat has the touch capability.
2703          *
2704          * This prevents a race between the compositor sending new
2705          * capabilities and the client trying to use the old ones.
2706          */
2707         struct weston_touch *touch = seat ? seat->touch_state : NULL;
2708         struct wl_resource *cr;
2709
2710         cr = wl_resource_create(client, &wl_touch_interface,
2711                                 wl_resource_get_version(resource), id);
2712         if (cr == NULL) {
2713                 wl_client_post_no_memory(client);
2714                 return;
2715         }
2716
2717         wl_list_init(wl_resource_get_link(cr));
2718         wl_resource_set_implementation(cr, &touch_interface,
2719                                        touch, destroy_touch_resource);
2720
2721         /* If we don't have a touch_state, the resource is inert, so there
2722          * is nothing more to set up */
2723         if (!touch)
2724                 return;
2725
2726         if (touch->focus &&
2727             wl_resource_get_client(touch->focus->surface->resource) == client) {
2728                 wl_list_insert(&touch->focus_resource_list,
2729                                wl_resource_get_link(cr));
2730         } else {
2731                 wl_list_insert(&touch->resource_list,
2732                                wl_resource_get_link(cr));
2733         }
2734 }
2735
2736 static void
2737 seat_release(struct wl_client *client, struct wl_resource *resource)
2738 {
2739         wl_resource_destroy(resource);
2740 }
2741
2742 static const struct wl_seat_interface seat_interface = {
2743         seat_get_pointer,
2744         seat_get_keyboard,
2745         seat_get_touch,
2746         seat_release,
2747 };
2748
2749 static void
2750 bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2751 {
2752         struct weston_seat *seat = data;
2753         struct wl_resource *resource;
2754         enum wl_seat_capability caps = 0;
2755
2756         resource = wl_resource_create(client,
2757                                       &wl_seat_interface, version, id);
2758         wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
2759         wl_resource_set_implementation(resource, &seat_interface, data,
2760                                        unbind_resource);
2761
2762         if (weston_seat_get_pointer(seat))
2763                 caps |= WL_SEAT_CAPABILITY_POINTER;
2764         if (weston_seat_get_keyboard(seat))
2765                 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
2766         if (weston_seat_get_touch(seat))
2767                 caps |= WL_SEAT_CAPABILITY_TOUCH;
2768
2769         wl_seat_send_capabilities(resource, caps);
2770         if (version >= WL_SEAT_NAME_SINCE_VERSION)
2771                 wl_seat_send_name(resource, seat->seat_name);
2772 }
2773
2774 static void
2775 relative_pointer_destroy(struct wl_client *client,
2776                          struct wl_resource *resource)
2777 {
2778         wl_resource_destroy(resource);
2779 }
2780
2781 static const struct zwp_relative_pointer_v1_interface relative_pointer_interface = {
2782         relative_pointer_destroy
2783 };
2784
2785 static void
2786 relative_pointer_manager_destroy(struct wl_client *client,
2787                                  struct wl_resource *resource)
2788 {
2789         wl_resource_destroy(resource);
2790 }
2791
2792 static void
2793 relative_pointer_manager_get_relative_pointer(struct wl_client *client,
2794                                               struct wl_resource *resource,
2795                                               uint32_t id,
2796                                               struct wl_resource *pointer_resource)
2797 {
2798         struct weston_pointer *pointer =
2799                 wl_resource_get_user_data(pointer_resource);
2800         struct weston_pointer_client *pointer_client;
2801         struct wl_resource *cr;
2802
2803         cr = wl_resource_create(client, &zwp_relative_pointer_v1_interface,
2804                                 wl_resource_get_version(resource), id);
2805         if (cr == NULL) {
2806                 wl_client_post_no_memory(client);
2807                 return;
2808         }
2809
2810         pointer_client = weston_pointer_ensure_pointer_client(pointer, client);
2811         if (!pointer_client) {
2812                 wl_client_post_no_memory(client);
2813                 return;
2814         }
2815
2816         wl_list_insert(&pointer_client->relative_pointer_resources,
2817                        wl_resource_get_link(cr));
2818         wl_resource_set_implementation(cr, &relative_pointer_interface,
2819                                        pointer,
2820                                        unbind_pointer_client_resource);
2821 }
2822
2823 static const struct zwp_relative_pointer_manager_v1_interface relative_pointer_manager = {
2824         relative_pointer_manager_destroy,
2825         relative_pointer_manager_get_relative_pointer,
2826 };
2827
2828 static void
2829 bind_relative_pointer_manager(struct wl_client *client, void *data,
2830                               uint32_t version, uint32_t id)
2831 {
2832         struct weston_compositor *compositor = data;
2833         struct wl_resource *resource;
2834
2835         resource = wl_resource_create(client,
2836                                       &zwp_relative_pointer_manager_v1_interface,
2837                                       1, id);
2838
2839         wl_resource_set_implementation(resource, &relative_pointer_manager,
2840                                        compositor,
2841                                        NULL);
2842 }
2843
2844 WL_EXPORT int
2845 weston_compositor_set_xkb_rule_names(struct weston_compositor *ec,
2846                                      struct xkb_rule_names *names)
2847 {
2848         if (ec->xkb_context == NULL) {
2849                 ec->xkb_context = xkb_context_new(0);
2850                 if (ec->xkb_context == NULL) {
2851                         weston_log("failed to create XKB context\n");
2852                         return -1;
2853                 }
2854         }
2855
2856         if (names)
2857                 ec->xkb_names = *names;
2858         if (!ec->xkb_names.rules)
2859                 ec->xkb_names.rules = strdup("evdev");
2860         if (!ec->xkb_names.model)
2861                 ec->xkb_names.model = strdup("pc105");
2862         if (!ec->xkb_names.layout)
2863                 ec->xkb_names.layout = strdup("us");
2864
2865         return 0;
2866 }
2867
2868 static void
2869 weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
2870 {
2871         if (--xkb_info->ref_count > 0)
2872                 return;
2873
2874         xkb_keymap_unref(xkb_info->keymap);
2875
2876         if (xkb_info->keymap_area)
2877                 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2878         if (xkb_info->keymap_fd >= 0)
2879                 close(xkb_info->keymap_fd);
2880         free(xkb_info);
2881 }
2882
2883 void
2884 weston_compositor_xkb_destroy(struct weston_compositor *ec)
2885 {
2886         free((char *) ec->xkb_names.rules);
2887         free((char *) ec->xkb_names.model);
2888         free((char *) ec->xkb_names.layout);
2889         free((char *) ec->xkb_names.variant);
2890         free((char *) ec->xkb_names.options);
2891
2892         if (ec->xkb_info)
2893                 weston_xkb_info_destroy(ec->xkb_info);
2894         xkb_context_unref(ec->xkb_context);
2895 }
2896
2897 static struct weston_xkb_info *
2898 weston_xkb_info_create(struct xkb_keymap *keymap)
2899 {
2900         struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
2901         if (xkb_info == NULL)
2902                 return NULL;
2903
2904         xkb_info->keymap = xkb_keymap_ref(keymap);
2905         xkb_info->ref_count = 1;
2906
2907         char *keymap_str;
2908
2909         xkb_info->shift_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2910                                                        XKB_MOD_NAME_SHIFT);
2911         xkb_info->caps_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2912                                                       XKB_MOD_NAME_CAPS);
2913         xkb_info->ctrl_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2914                                                       XKB_MOD_NAME_CTRL);
2915         xkb_info->alt_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2916                                                      XKB_MOD_NAME_ALT);
2917         xkb_info->mod2_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2918                                                       "Mod2");
2919         xkb_info->mod3_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2920                                                       "Mod3");
2921         xkb_info->super_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2922                                                        XKB_MOD_NAME_LOGO);
2923         xkb_info->mod5_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2924                                                       "Mod5");
2925
2926         xkb_info->num_led = xkb_keymap_led_get_index(xkb_info->keymap,
2927                                                      XKB_LED_NAME_NUM);
2928         xkb_info->caps_led = xkb_keymap_led_get_index(xkb_info->keymap,
2929                                                       XKB_LED_NAME_CAPS);
2930         xkb_info->scroll_led = xkb_keymap_led_get_index(xkb_info->keymap,
2931                                                         XKB_LED_NAME_SCROLL);
2932
2933         keymap_str = xkb_keymap_get_as_string(xkb_info->keymap,
2934                                               XKB_KEYMAP_FORMAT_TEXT_V1);
2935         if (keymap_str == NULL) {
2936                 weston_log("failed to get string version of keymap\n");
2937                 goto err_keymap;
2938         }
2939         xkb_info->keymap_size = strlen(keymap_str) + 1;
2940
2941         xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
2942         if (xkb_info->keymap_fd < 0) {
2943                 weston_log("creating a keymap file for %lu bytes failed: %m\n",
2944                         (unsigned long) xkb_info->keymap_size);
2945                 goto err_keymap_str;
2946         }
2947
2948         xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2949                                      PROT_READ | PROT_WRITE,
2950                                      MAP_SHARED, xkb_info->keymap_fd, 0);
2951         if (xkb_info->keymap_area == MAP_FAILED) {
2952                 weston_log("failed to mmap() %lu bytes\n",
2953                         (unsigned long) xkb_info->keymap_size);
2954                 goto err_dev_zero;
2955         }
2956         strcpy(xkb_info->keymap_area, keymap_str);
2957         free(keymap_str);
2958
2959         return xkb_info;
2960
2961 err_dev_zero:
2962         close(xkb_info->keymap_fd);
2963 err_keymap_str:
2964         free(keymap_str);
2965 err_keymap:
2966         xkb_keymap_unref(xkb_info->keymap);
2967         free(xkb_info);
2968         return NULL;
2969 }
2970
2971 static int
2972 weston_compositor_build_global_keymap(struct weston_compositor *ec)
2973 {
2974         struct xkb_keymap *keymap;
2975
2976         if (ec->xkb_info != NULL)
2977                 return 0;
2978
2979         keymap = xkb_keymap_new_from_names(ec->xkb_context,
2980                                            &ec->xkb_names,
2981                                            0);
2982         if (keymap == NULL) {
2983                 weston_log("failed to compile global XKB keymap\n");
2984                 weston_log("  tried rules %s, model %s, layout %s, variant %s, "
2985                         "options %s\n",
2986                         ec->xkb_names.rules, ec->xkb_names.model,
2987                         ec->xkb_names.layout, ec->xkb_names.variant,
2988                         ec->xkb_names.options);
2989                 return -1;
2990         }
2991
2992         ec->xkb_info = weston_xkb_info_create(keymap);
2993         xkb_keymap_unref(keymap);
2994         if (ec->xkb_info == NULL)
2995                 return -1;
2996
2997         return 0;
2998 }
2999
3000 WL_EXPORT void
3001 weston_seat_update_keymap(struct weston_seat *seat, struct xkb_keymap *keymap)
3002 {
3003         struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
3004
3005         if (!keyboard || !keymap)
3006                 return;
3007
3008         xkb_keymap_unref(keyboard->pending_keymap);
3009         keyboard->pending_keymap = xkb_keymap_ref(keymap);
3010
3011         if (keyboard->keys.size == 0)
3012                 update_keymap(seat);
3013 }
3014
3015 WL_EXPORT int
3016 weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
3017 {
3018         struct weston_keyboard *keyboard;
3019
3020         if (seat->keyboard_state) {
3021                 seat->keyboard_device_count += 1;
3022                 if (seat->keyboard_device_count == 1)
3023                         seat_send_updated_caps(seat);
3024                 return 0;
3025         }
3026
3027         keyboard = weston_keyboard_create();
3028         if (keyboard == NULL) {
3029                 weston_log("failed to allocate weston keyboard struct\n");
3030                 return -1;
3031         }
3032
3033         if (keymap != NULL) {
3034                 keyboard->xkb_info = weston_xkb_info_create(keymap);
3035                 if (keyboard->xkb_info == NULL)
3036                         goto err;
3037         } else {
3038                 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
3039                         goto err;
3040                 keyboard->xkb_info = seat->compositor->xkb_info;
3041                 keyboard->xkb_info->ref_count++;
3042         }
3043
3044         keyboard->xkb_state.state = xkb_state_new(keyboard->xkb_info->keymap);
3045         if (keyboard->xkb_state.state == NULL) {
3046                 weston_log("failed to initialise XKB state\n");
3047                 goto err;
3048         }
3049
3050         keyboard->xkb_state.leds = 0;
3051
3052         seat->keyboard_state = keyboard;
3053         seat->keyboard_device_count = 1;
3054         keyboard->seat = seat;
3055
3056         seat_send_updated_caps(seat);
3057
3058         return 0;
3059
3060 err:
3061         if (keyboard->xkb_info)
3062                 weston_xkb_info_destroy(keyboard->xkb_info);
3063         free(keyboard);
3064
3065         return -1;
3066 }
3067
3068 static void
3069 weston_keyboard_reset_state(struct weston_keyboard *keyboard)
3070 {
3071         struct weston_seat *seat = keyboard->seat;
3072         struct xkb_state *state;
3073
3074         state = xkb_state_new(keyboard->xkb_info->keymap);
3075         if (!state) {
3076                 weston_log("failed to reset XKB state\n");
3077                 return;
3078         }
3079         xkb_state_unref(keyboard->xkb_state.state);
3080         keyboard->xkb_state.state = state;
3081
3082         keyboard->xkb_state.leds = 0;
3083
3084         seat->modifier_state = 0;
3085 }
3086
3087 WL_EXPORT void
3088 weston_seat_release_keyboard(struct weston_seat *seat)
3089 {
3090         seat->keyboard_device_count--;
3091         assert(seat->keyboard_device_count >= 0);
3092         if (seat->keyboard_device_count == 0) {
3093                 weston_keyboard_set_focus(seat->keyboard_state, NULL);
3094                 weston_keyboard_cancel_grab(seat->keyboard_state);
3095                 weston_keyboard_reset_state(seat->keyboard_state);
3096                 seat_send_updated_caps(seat);
3097         }
3098 }
3099
3100 WL_EXPORT void
3101 weston_seat_init_pointer(struct weston_seat *seat)
3102 {
3103         struct weston_pointer *pointer;
3104
3105         if (seat->pointer_state) {
3106                 seat->pointer_device_count += 1;
3107                 if (seat->pointer_device_count == 1)
3108                         seat_send_updated_caps(seat);
3109                 return;
3110         }
3111
3112         pointer = weston_pointer_create(seat);
3113         if (pointer == NULL)
3114                 return;
3115
3116         seat->pointer_state = pointer;
3117         seat->pointer_device_count = 1;
3118         pointer->seat = seat;
3119
3120         seat_send_updated_caps(seat);
3121 }
3122
3123 WL_EXPORT void
3124 weston_seat_release_pointer(struct weston_seat *seat)
3125 {
3126         struct weston_pointer *pointer = seat->pointer_state;
3127
3128         seat->pointer_device_count--;
3129         if (seat->pointer_device_count == 0) {
3130                 weston_pointer_clear_focus(pointer);
3131                 weston_pointer_cancel_grab(pointer);
3132
3133                 if (pointer->sprite)
3134                         pointer_unmap_sprite(pointer);
3135
3136                 weston_pointer_reset_state(pointer);
3137                 seat_send_updated_caps(seat);
3138
3139                 /* seat->pointer is intentionally not destroyed so that
3140                  * a newly attached pointer on this seat will retain
3141                  * the previous cursor co-ordinates.
3142                  */
3143         }
3144 }
3145
3146 WL_EXPORT void
3147 weston_seat_init_touch(struct weston_seat *seat)
3148 {
3149         struct weston_touch *touch;
3150
3151         if (seat->touch_state) {
3152                 seat->touch_device_count += 1;
3153                 if (seat->touch_device_count == 1)
3154                         seat_send_updated_caps(seat);
3155                 return;
3156         }
3157
3158         touch = weston_touch_create();
3159         if (touch == NULL)
3160                 return;
3161
3162         seat->touch_state = touch;
3163         seat->touch_device_count = 1;
3164         touch->seat = seat;
3165
3166         seat_send_updated_caps(seat);
3167 }
3168
3169 WL_EXPORT void
3170 weston_seat_release_touch(struct weston_seat *seat)
3171 {
3172         seat->touch_device_count--;
3173         if (seat->touch_device_count == 0) {
3174                 weston_touch_set_focus(seat->touch_state, NULL);
3175                 weston_touch_cancel_grab(seat->touch_state);
3176                 weston_touch_reset_state(seat->touch_state);
3177                 seat_send_updated_caps(seat);
3178         }
3179 }
3180
3181 WL_EXPORT void
3182 weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
3183                  const char *seat_name)
3184 {
3185         memset(seat, 0, sizeof *seat);
3186
3187         seat->selection_data_source = NULL;
3188         wl_list_init(&seat->base_resource_list);
3189         wl_signal_init(&seat->selection_signal);
3190         wl_list_init(&seat->drag_resource_list);
3191         wl_signal_init(&seat->destroy_signal);
3192         wl_signal_init(&seat->updated_caps_signal);
3193
3194         seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 5,
3195                                         seat, bind_seat);
3196
3197         seat->compositor = ec;
3198         seat->modifier_state = 0;
3199         seat->seat_name = strdup(seat_name);
3200
3201         wl_list_insert(ec->seat_list.prev, &seat->link);
3202
3203         clipboard_create(seat);
3204
3205         wl_signal_emit(&ec->seat_created_signal, seat);
3206 }
3207
3208 WL_EXPORT void
3209 weston_seat_release(struct weston_seat *seat)
3210 {
3211         struct wl_resource *resource;
3212
3213         wl_resource_for_each(resource, &seat->base_resource_list) {
3214                 wl_resource_set_user_data(resource, NULL);
3215         }
3216
3217         wl_resource_for_each(resource, &seat->drag_resource_list) {
3218                 wl_resource_set_user_data(resource, NULL);
3219         }
3220
3221         wl_list_remove(&seat->base_resource_list);
3222         wl_list_remove(&seat->drag_resource_list);
3223
3224         wl_list_remove(&seat->link);
3225
3226         if (seat->saved_kbd_focus)
3227                 wl_list_remove(&seat->saved_kbd_focus_listener.link);
3228
3229         if (seat->pointer_state)
3230                 weston_pointer_destroy(seat->pointer_state);
3231         if (seat->keyboard_state)
3232                 weston_keyboard_destroy(seat->keyboard_state);
3233         if (seat->touch_state)
3234                 weston_touch_destroy(seat->touch_state);
3235
3236         free (seat->seat_name);
3237
3238         wl_global_destroy(seat->global);
3239
3240         wl_signal_emit(&seat->destroy_signal, seat);
3241 }
3242
3243 /** Get a seat's keyboard pointer
3244  *
3245  * \param seat The seat to query
3246  * \return The seat's keyboard pointer, or NULL if no keyboard is present
3247  *
3248  * The keyboard pointer for a seat isn't freed when all keyboards are removed,
3249  * so it should only be used when the seat's keyboard_device_count is greater
3250  * than zero.  This function does that test and only returns a pointer
3251  * when a keyboard is present.
3252  */
3253 WL_EXPORT struct weston_keyboard *
3254 weston_seat_get_keyboard(struct weston_seat *seat)
3255 {
3256         if (!seat)
3257                 return NULL;
3258
3259         if (seat->keyboard_device_count)
3260                 return seat->keyboard_state;
3261
3262         return NULL;
3263 }
3264
3265 /** Get a seat's pointer pointer
3266  *
3267  * \param seat The seat to query
3268  * \return The seat's pointer pointer, or NULL if no pointer device is present
3269  *
3270  * The pointer pointer for a seat isn't freed when all mice are removed,
3271  * so it should only be used when the seat's pointer_device_count is greater
3272  * than zero.  This function does that test and only returns a pointer
3273  * when a pointing device is present.
3274  */
3275 WL_EXPORT struct weston_pointer *
3276 weston_seat_get_pointer(struct weston_seat *seat)
3277 {
3278         if (!seat)
3279                 return NULL;
3280
3281         if (seat->pointer_device_count)
3282                 return seat->pointer_state;
3283
3284         return NULL;
3285 }
3286
3287 static const struct zwp_locked_pointer_v1_interface locked_pointer_interface;
3288 static const struct zwp_confined_pointer_v1_interface confined_pointer_interface;
3289
3290 static enum pointer_constraint_type
3291 pointer_constraint_get_type(struct weston_pointer_constraint *constraint)
3292 {
3293         if (wl_resource_instance_of(constraint->resource,
3294                                     &zwp_locked_pointer_v1_interface,
3295                                     &locked_pointer_interface)) {
3296                 return POINTER_CONSTRAINT_TYPE_LOCK;
3297         } else if (wl_resource_instance_of(constraint->resource,
3298                                            &zwp_confined_pointer_v1_interface,
3299                                            &confined_pointer_interface)) {
3300                 return POINTER_CONSTRAINT_TYPE_CONFINE;
3301         }
3302
3303         abort();
3304         return 0;
3305 }
3306
3307 static void
3308 pointer_constraint_notify_activated(struct weston_pointer_constraint *constraint)
3309 {
3310         struct wl_resource *resource = constraint->resource;
3311
3312         switch (pointer_constraint_get_type(constraint)) {
3313         case POINTER_CONSTRAINT_TYPE_LOCK:
3314                 zwp_locked_pointer_v1_send_locked(resource);
3315                 break;
3316         case POINTER_CONSTRAINT_TYPE_CONFINE:
3317                 zwp_confined_pointer_v1_send_confined(resource);
3318                 break;
3319         }
3320 }
3321
3322 static void
3323 pointer_constraint_notify_deactivated(struct weston_pointer_constraint *constraint)
3324 {
3325         struct wl_resource *resource = constraint->resource;
3326
3327         switch (pointer_constraint_get_type(constraint)) {
3328         case POINTER_CONSTRAINT_TYPE_LOCK:
3329                 zwp_locked_pointer_v1_send_unlocked(resource);
3330                 break;
3331         case POINTER_CONSTRAINT_TYPE_CONFINE:
3332                 zwp_confined_pointer_v1_send_unconfined(resource);
3333                 break;
3334         }
3335 }
3336
3337 static struct weston_pointer_constraint *
3338 get_pointer_constraint_for_pointer(struct weston_surface *surface,
3339                                    struct weston_pointer *pointer)
3340 {
3341         struct weston_pointer_constraint *constraint;
3342
3343         wl_list_for_each(constraint, &surface->pointer_constraints, link) {
3344                 if (constraint->pointer == pointer)
3345                         return constraint;
3346         }
3347
3348         return NULL;
3349 }
3350
3351 /** Get a seat's touch pointer
3352  *
3353  * \param seat The seat to query
3354  * \return The seat's touch pointer, or NULL if no touch device is present
3355  *
3356  * The touch pointer for a seat isn't freed when all touch devices are removed,
3357  * so it should only be used when the seat's touch_device_count is greater
3358  * than zero.  This function does that test and only returns a pointer
3359  * when a touch device is present.
3360  */
3361 WL_EXPORT struct weston_touch *
3362 weston_seat_get_touch(struct weston_seat *seat)
3363 {
3364         if (!seat)
3365                 return NULL;
3366
3367         if (seat->touch_device_count)
3368                 return seat->touch_state;
3369
3370         return NULL;
3371 }
3372
3373 /** Sets the keyboard focus to the given surface
3374  *
3375  * \param seat The seat to query
3376  */
3377 WL_EXPORT void
3378 weston_seat_set_keyboard_focus(struct weston_seat *seat,
3379                                struct weston_surface *surface)
3380 {
3381         struct weston_compositor *compositor = seat->compositor;
3382         struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
3383         struct weston_surface_activation_data activation_data;
3384
3385         if (keyboard && keyboard->focus != surface) {
3386                 weston_keyboard_set_focus(keyboard, surface);
3387                 wl_data_device_set_keyboard_focus(seat);
3388         }
3389
3390         inc_activate_serial(compositor);
3391
3392         activation_data = (struct weston_surface_activation_data) {
3393                 .surface = surface,
3394                 .seat = seat,
3395         };
3396         wl_signal_emit(&compositor->activate_signal, &activation_data);
3397 }
3398
3399 static void
3400 enable_pointer_constraint(struct weston_pointer_constraint *constraint,
3401                           struct weston_view *view)
3402 {
3403         assert(constraint->view == NULL);
3404         constraint->view = view;
3405         pointer_constraint_notify_activated(constraint);
3406         weston_pointer_start_grab(constraint->pointer, &constraint->grab);
3407         wl_list_remove(&constraint->surface_destroy_listener.link);
3408         wl_list_init(&constraint->surface_destroy_listener.link);
3409 }
3410
3411 static bool
3412 is_pointer_constraint_enabled(struct weston_pointer_constraint *constraint)
3413 {
3414         return constraint->view != NULL;
3415 }
3416
3417 static void
3418 weston_pointer_constraint_disable(struct weston_pointer_constraint *constraint)
3419 {
3420         constraint->view = NULL;
3421         pointer_constraint_notify_deactivated(constraint);
3422         weston_pointer_end_grab(constraint->grab.pointer);
3423 }
3424
3425 void
3426 weston_pointer_constraint_destroy(struct weston_pointer_constraint *constraint)
3427 {
3428         if (is_pointer_constraint_enabled(constraint))
3429                 weston_pointer_constraint_disable(constraint);
3430
3431         wl_list_remove(&constraint->pointer_destroy_listener.link);
3432         wl_list_remove(&constraint->surface_destroy_listener.link);
3433         wl_list_remove(&constraint->surface_commit_listener.link);
3434         wl_list_remove(&constraint->surface_activate_listener.link);
3435
3436         wl_resource_set_user_data(constraint->resource, NULL);
3437         pixman_region32_fini(&constraint->region);
3438         wl_list_remove(&constraint->link);
3439         free(constraint);
3440 }
3441
3442 static void
3443 disable_pointer_constraint(struct weston_pointer_constraint *constraint)
3444 {
3445         switch (constraint->lifetime) {
3446         case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT:
3447                 weston_pointer_constraint_destroy(constraint);
3448                 break;
3449         case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT:
3450                 weston_pointer_constraint_disable(constraint);
3451                 break;
3452         }
3453 }
3454
3455 static bool
3456 is_within_constraint_region(struct weston_pointer_constraint *constraint,
3457                             wl_fixed_t sx, wl_fixed_t sy)
3458 {
3459         struct weston_surface *surface = constraint->surface;
3460         pixman_region32_t constraint_region;
3461         bool result;
3462
3463         pixman_region32_init(&constraint_region);
3464         pixman_region32_intersect(&constraint_region,
3465                                   &surface->input,
3466                                   &constraint->region);
3467         result = pixman_region32_contains_point(&constraint_region,
3468                                                 wl_fixed_to_int(sx),
3469                                                 wl_fixed_to_int(sy),
3470                                                 NULL);
3471         pixman_region32_fini(&constraint_region);
3472
3473         return result;
3474 }
3475
3476 static void
3477 maybe_enable_pointer_constraint(struct weston_pointer_constraint *constraint)
3478 {
3479         struct weston_surface *surface = constraint->surface;
3480         struct weston_view *vit;
3481         struct weston_view *view = NULL;
3482         struct weston_pointer *pointer = constraint->pointer;
3483         struct weston_keyboard *keyboard;
3484         struct weston_seat *seat = pointer->seat;
3485         int32_t x, y;
3486
3487         /* Postpone if no view of the surface was most recently clicked. */
3488         wl_list_for_each(vit, &surface->views, surface_link) {
3489                 if (vit->click_to_activate_serial ==
3490                     surface->compositor->activate_serial) {
3491                         view = vit;
3492                 }
3493         }
3494         if (view == NULL)
3495                 return;
3496
3497         /* Postpone if surface doesn't have keyboard focus. */
3498         keyboard = weston_seat_get_keyboard(seat);
3499         if (!keyboard || keyboard->focus != surface)
3500                 return;
3501
3502         /* Postpone constraint if the pointer is not within the
3503          * constraint region.
3504          */
3505         weston_view_from_global(view,
3506                                 wl_fixed_to_int(pointer->x),
3507                                 wl_fixed_to_int(pointer->y),
3508                                 &x, &y);
3509         if (!is_within_constraint_region(constraint,
3510                                          wl_fixed_from_int(x),
3511                                          wl_fixed_from_int(y)))
3512                 return;
3513
3514         enable_pointer_constraint(constraint, view);
3515 }
3516
3517 static void
3518 locked_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
3519 {
3520 }
3521
3522 static void
3523 locked_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
3524                                    const struct timespec *time,
3525                                    struct weston_pointer_motion_event *event)
3526 {
3527         pointer_send_relative_motion(grab->pointer, time, event);
3528 }
3529
3530 static void
3531 locked_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
3532                                    const struct timespec *time,
3533                                    uint32_t button,
3534                                    uint32_t state_w)
3535 {
3536         weston_pointer_send_button(grab->pointer, time, button, state_w);
3537 }
3538
3539 static void
3540 locked_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
3541                                  const struct timespec *time,
3542                                  struct weston_pointer_axis_event *event)
3543 {
3544         weston_pointer_send_axis(grab->pointer, time, event);
3545 }
3546
3547 static void
3548 locked_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
3549                                         uint32_t source)
3550 {
3551         weston_pointer_send_axis_source(grab->pointer, source);
3552 }
3553
3554 static void
3555 locked_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
3556 {
3557         weston_pointer_send_frame(grab->pointer);
3558 }
3559
3560 static void
3561 locked_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
3562 {
3563         struct weston_pointer_constraint *constraint =
3564                 container_of(grab, struct weston_pointer_constraint, grab);
3565
3566         disable_pointer_constraint(constraint);
3567 }
3568
3569 static const struct weston_pointer_grab_interface
3570                                 locked_pointer_grab_interface = {
3571         locked_pointer_grab_pointer_focus,
3572         locked_pointer_grab_pointer_motion,
3573         locked_pointer_grab_pointer_button,
3574         locked_pointer_grab_pointer_axis,
3575         locked_pointer_grab_pointer_axis_source,
3576         locked_pointer_grab_pointer_frame,
3577         locked_pointer_grab_pointer_cancel,
3578 };
3579
3580 static void
3581 pointer_constraint_constrain_resource_destroyed(struct wl_resource *resource)
3582 {
3583         struct weston_pointer_constraint *constraint =
3584                 wl_resource_get_user_data(resource);
3585
3586         if (!constraint)
3587                 return;
3588
3589         weston_pointer_constraint_destroy(constraint);
3590 }
3591
3592 static void
3593 pointer_constraint_surface_activate(struct wl_listener *listener, void *data)
3594 {
3595         struct weston_surface_activation_data *activation = data;
3596         struct weston_pointer *pointer;
3597         struct weston_surface *focus = activation->surface;
3598         struct weston_pointer_constraint *constraint =
3599                 container_of(listener, struct weston_pointer_constraint,
3600                              surface_activate_listener);
3601         bool is_constraint_surface;
3602
3603         pointer = weston_seat_get_pointer(activation->seat);
3604         if (!pointer)
3605                 return;
3606
3607         is_constraint_surface =
3608                 get_pointer_constraint_for_pointer(focus, pointer) == constraint;
3609
3610         if (is_constraint_surface &&
3611             !is_pointer_constraint_enabled(constraint))
3612                 maybe_enable_pointer_constraint(constraint);
3613         else if (!is_constraint_surface &&
3614                  is_pointer_constraint_enabled(constraint))
3615                 disable_pointer_constraint(constraint);
3616 }
3617
3618 static void
3619 pointer_constraint_pointer_destroyed(struct wl_listener *listener, void *data)
3620 {
3621         struct weston_pointer_constraint *constraint =
3622                 container_of(listener, struct weston_pointer_constraint,
3623                              pointer_destroy_listener);
3624
3625         weston_pointer_constraint_destroy(constraint);
3626 }
3627
3628 static void
3629 pointer_constraint_surface_destroyed(struct wl_listener *listener, void *data)
3630 {
3631         struct weston_pointer_constraint *constraint =
3632                 container_of(listener, struct weston_pointer_constraint,
3633                              surface_destroy_listener);
3634
3635         weston_pointer_constraint_destroy(constraint);
3636 }
3637
3638 static void
3639 pointer_constraint_surface_committed(struct wl_listener *listener, void *data)
3640 {
3641         struct weston_pointer_constraint *constraint =
3642                 container_of(listener, struct weston_pointer_constraint,
3643                              surface_commit_listener);
3644
3645         if (constraint->region_is_pending) {
3646                 constraint->region_is_pending = false;
3647                 pixman_region32_copy(&constraint->region,
3648                                      &constraint->region_pending);
3649                 pixman_region32_fini(&constraint->region_pending);
3650                 pixman_region32_init(&constraint->region_pending);
3651         }
3652
3653         if (constraint->hint_is_pending) {
3654                 constraint->hint_is_pending = false;
3655
3656                 constraint->hint_is_pending = true;
3657                 constraint->hint_x = constraint->hint_x_pending;
3658                 constraint->hint_y = constraint->hint_y_pending;
3659         }
3660
3661         if (pointer_constraint_get_type(constraint) ==
3662             POINTER_CONSTRAINT_TYPE_CONFINE &&
3663             is_pointer_constraint_enabled(constraint))
3664                 maybe_warp_confined_pointer(constraint);
3665 }
3666
3667 static struct weston_pointer_constraint *
3668 weston_pointer_constraint_create(struct weston_surface *surface,
3669                                  struct weston_pointer *pointer,
3670                                  struct weston_region *region,
3671                                  enum zwp_pointer_constraints_v1_lifetime lifetime,
3672                                  struct wl_resource *cr,
3673                                  const struct weston_pointer_grab_interface *grab_interface)
3674 {
3675         struct weston_pointer_constraint *constraint;
3676
3677         constraint = zalloc(sizeof *constraint);
3678         if (!constraint)
3679                 return NULL;
3680
3681         constraint->lifetime = lifetime;
3682         pixman_region32_init(&constraint->region);
3683         pixman_region32_init(&constraint->region_pending);
3684         wl_list_insert(&surface->pointer_constraints, &constraint->link);
3685         constraint->surface = surface;
3686         constraint->pointer = pointer;
3687         constraint->resource = cr;
3688         constraint->grab.interface = grab_interface;
3689         if (region) {
3690                 pixman_region32_copy(&constraint->region,
3691                                      &region->region);
3692         } else {
3693                 pixman_region32_fini(&constraint->region);
3694                 region_init_infinite(&constraint->region);
3695         }
3696
3697         constraint->surface_activate_listener.notify =
3698                 pointer_constraint_surface_activate;
3699         constraint->surface_destroy_listener.notify =
3700                 pointer_constraint_surface_destroyed;
3701         constraint->surface_commit_listener.notify =
3702                 pointer_constraint_surface_committed;
3703         constraint->pointer_destroy_listener.notify =
3704                 pointer_constraint_pointer_destroyed;
3705
3706         wl_signal_add(&surface->compositor->activate_signal,
3707                       &constraint->surface_activate_listener);
3708         wl_signal_add(&pointer->destroy_signal,
3709                       &constraint->pointer_destroy_listener);
3710         wl_signal_add(&surface->destroy_signal,
3711                       &constraint->surface_destroy_listener);
3712         wl_signal_add(&surface->commit_signal,
3713                       &constraint->surface_commit_listener);
3714
3715         return constraint;
3716 }
3717
3718 static void
3719 init_pointer_constraint(struct wl_resource *pointer_constraints_resource,
3720                         uint32_t id,
3721                         struct weston_surface *surface,
3722                         struct weston_pointer *pointer,
3723                         struct weston_region *region,
3724                         enum zwp_pointer_constraints_v1_lifetime lifetime,
3725                         const struct wl_interface *interface,
3726                         const void *implementation,
3727                         const struct weston_pointer_grab_interface *grab_interface)
3728 {
3729         struct wl_client *client =
3730                 wl_resource_get_client(pointer_constraints_resource);
3731         struct wl_resource *cr;
3732         struct weston_pointer_constraint *constraint;
3733
3734         if (pointer && get_pointer_constraint_for_pointer(surface, pointer)) {
3735                 wl_resource_post_error(pointer_constraints_resource,
3736                                        ZWP_POINTER_CONSTRAINTS_V1_ERROR_ALREADY_CONSTRAINED,
3737                                        "the pointer has a lock/confine request on this surface");
3738                 return;
3739         }
3740
3741         cr = wl_resource_create(client, interface,
3742                                 wl_resource_get_version(pointer_constraints_resource),
3743                                 id);
3744         if (cr == NULL) {
3745                 wl_client_post_no_memory(client);
3746                 return;
3747         }
3748
3749         if (pointer) {
3750                 constraint = weston_pointer_constraint_create(surface, pointer,
3751                                                               region, lifetime,
3752                                                               cr, grab_interface);
3753                 if (constraint == NULL) {
3754                         wl_client_post_no_memory(client);
3755                         return;
3756                 }
3757         } else {
3758                 constraint = NULL;
3759         }
3760
3761         wl_resource_set_implementation(cr, implementation, constraint,
3762                                        pointer_constraint_constrain_resource_destroyed);
3763
3764         if (constraint)
3765                 maybe_enable_pointer_constraint(constraint);
3766 }
3767
3768 static void
3769 pointer_constraints_destroy(struct wl_client *client,
3770                             struct wl_resource *resource)
3771 {
3772         wl_resource_destroy(resource);
3773 }
3774
3775 static void
3776 locked_pointer_destroy(struct wl_client *client,
3777                        struct wl_resource *resource)
3778 {
3779         struct weston_pointer_constraint *constraint =
3780                 wl_resource_get_user_data(resource);
3781         wl_fixed_t x, y;
3782
3783         if (constraint && constraint->view && constraint->hint_is_pending &&
3784             is_within_constraint_region(constraint,
3785                                         constraint->hint_x,
3786                                         constraint->hint_y)) {
3787                 weston_view_to_global_fixed(constraint->view,
3788                                             constraint->hint_x,
3789                                             constraint->hint_y,
3790                                             &x, &y);
3791                 weston_pointer_move_to(constraint->pointer, x, y);
3792         }
3793         wl_resource_destroy(resource);
3794 }
3795
3796 static void
3797 locked_pointer_set_cursor_position_hint(struct wl_client *client,
3798                                         struct wl_resource *resource,
3799                                         wl_fixed_t surface_x,
3800                                         wl_fixed_t surface_y)
3801 {
3802         struct weston_pointer_constraint *constraint =
3803                 wl_resource_get_user_data(resource);
3804
3805         /* Ignore a set cursor hint that was sent after the lock was cancelled.
3806          */
3807         if (!constraint ||
3808             !constraint->resource ||
3809             constraint->resource != resource)
3810                 return;
3811
3812         constraint->hint_is_pending = true;
3813         constraint->hint_x_pending = surface_x;
3814         constraint->hint_y_pending = surface_y;
3815 }
3816
3817 static void
3818 locked_pointer_set_region(struct wl_client *client,
3819                           struct wl_resource *resource,
3820                           struct wl_resource *region_resource)
3821 {
3822         struct weston_pointer_constraint *constraint =
3823                 wl_resource_get_user_data(resource);
3824         struct weston_region *region = region_resource ?
3825                 wl_resource_get_user_data(region_resource) : NULL;
3826
3827         if (!constraint)
3828                 return;
3829
3830         if (region) {
3831                 pixman_region32_copy(&constraint->region_pending,
3832                                      &region->region);
3833         } else {
3834                 pixman_region32_fini(&constraint->region_pending);
3835                 region_init_infinite(&constraint->region_pending);
3836         }
3837         constraint->region_is_pending = true;
3838 }
3839
3840
3841 static const struct zwp_locked_pointer_v1_interface locked_pointer_interface = {
3842         locked_pointer_destroy,
3843         locked_pointer_set_cursor_position_hint,
3844         locked_pointer_set_region,
3845 };
3846
3847 static void
3848 pointer_constraints_lock_pointer(struct wl_client *client,
3849                                  struct wl_resource *resource,
3850                                  uint32_t id,
3851                                  struct wl_resource *surface_resource,
3852                                  struct wl_resource *pointer_resource,
3853                                  struct wl_resource *region_resource,
3854                                  uint32_t lifetime)
3855 {
3856         struct weston_surface *surface =
3857                 wl_resource_get_user_data(surface_resource);
3858         struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
3859         struct weston_region *region = region_resource ?
3860                 wl_resource_get_user_data(region_resource) : NULL;
3861
3862         init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
3863                                 &zwp_locked_pointer_v1_interface,
3864                                 &locked_pointer_interface,
3865                                 &locked_pointer_grab_interface);
3866 }
3867
3868 static void
3869 confined_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
3870 {
3871 }
3872
3873 static double
3874 vec2d_cross_product(struct vec2d a, struct vec2d b)
3875 {
3876         return a.x * b.y - a.y * b.x;
3877 }
3878
3879 static struct vec2d
3880 vec2d_add(struct vec2d a, struct vec2d b)
3881 {
3882         return (struct vec2d) {
3883                 .x = a.x + b.x,
3884                 .y = a.y + b.y,
3885         };
3886 }
3887
3888 static struct vec2d
3889 vec2d_subtract(struct vec2d a, struct vec2d b)
3890 {
3891         return (struct vec2d) {
3892                 .x = a.x - b.x,
3893                 .y = a.y - b.y,
3894         };
3895 }
3896
3897 static struct vec2d
3898 vec2d_multiply_constant(double c, struct vec2d a)
3899 {
3900         return (struct vec2d) {
3901                 .x = c * a.x,
3902                 .y = c * a.y,
3903         };
3904 }
3905
3906 static bool
3907 lines_intersect(struct line *line1, struct line *line2,
3908                 struct vec2d *intersection)
3909 {
3910         struct vec2d p = line1->a;
3911         struct vec2d r = vec2d_subtract(line1->b, line1->a);
3912         struct vec2d q = line2->a;
3913         struct vec2d s = vec2d_subtract(line2->b, line2->a);
3914         double rxs;
3915         double sxr;
3916         double t;
3917         double u;
3918
3919         /*
3920          * The line (p, r) and (q, s) intersects where
3921          *
3922          *   p + t r = q + u s
3923          *
3924          * Calculate t:
3925          *
3926          *   (p + t r) × s = (q + u s) × s
3927          *   p × s + t (r × s) = q × s + u (s × s)
3928          *   p × s + t (r × s) = q × s
3929          *   t (r × s) = q × s - p × s
3930          *   t (r × s) = (q - p) × s
3931          *   t = ((q - p) × s) / (r × s)
3932          *
3933          * Using the same method, for u we get:
3934          *
3935          *   u = ((p - q) × r) / (s × r)
3936          */
3937
3938         rxs = vec2d_cross_product(r, s);
3939         sxr = vec2d_cross_product(s, r);
3940
3941         /* If r × s = 0 then the lines are either parallel or collinear. */
3942         if (fabs(rxs) < DBL_MIN)
3943                 return false;
3944
3945         t = vec2d_cross_product(vec2d_subtract(q, p), s) / rxs;
3946         u = vec2d_cross_product(vec2d_subtract(p, q), r) / sxr;
3947
3948         /* The lines only intersect if 0 ≤ t ≤ 1 and 0 ≤ u ≤ 1. */
3949         if (t < 0.0 || t > 1.0 || u < 0.0 || u > 1.0)
3950                 return false;
3951
3952         *intersection = vec2d_add(p, vec2d_multiply_constant(t, r));
3953         return true;
3954 }
3955
3956 static struct border *
3957 add_border(struct wl_array *array,
3958            double x1, double y1,
3959            double x2, double y2,
3960            enum motion_direction blocking_dir)
3961 {
3962         struct border *border = wl_array_add(array, sizeof *border);
3963
3964         *border = (struct border) {
3965                 .line = (struct line) {
3966                         .a = (struct vec2d) {
3967                                 .x = x1,
3968                                 .y = y1,
3969                         },
3970                         .b = (struct vec2d) {
3971                                 .x = x2,
3972                                 .y = y2,
3973                         },
3974                 },
3975                 .blocking_dir = blocking_dir,
3976         };
3977
3978         return border;
3979 }
3980
3981 static int
3982 compare_lines_x(const void *a, const void *b)
3983 {
3984         const struct border *border_a = a;
3985         const struct border *border_b = b;
3986
3987
3988         if (border_a->line.a.x == border_b->line.a.x)
3989                 return border_a->line.b.x < border_b->line.b.x;
3990         else
3991                 return border_a->line.a.x > border_b->line.a.x;
3992 }
3993
3994 static void
3995 add_non_overlapping_edges(pixman_box32_t *boxes,
3996                           int band_above_start,
3997                           int band_below_start,
3998                           int band_below_end,
3999                           struct wl_array *borders)
4000 {
4001         int i;
4002         struct wl_array band_merge;
4003         struct border *border;
4004         struct border *prev_border;
4005         struct border *new_border;
4006
4007         wl_array_init(&band_merge);
4008
4009         /* Add bottom band of previous row, and top band of current row, and
4010          * sort them so lower left x coordinate comes first. If there are two
4011          * borders with the same left x coordinate, the wider one comes first.
4012          */
4013         for (i = band_above_start; i < band_below_start; i++) {
4014                 pixman_box32_t *box = &boxes[i];
4015                 add_border(&band_merge, box->x1, box->y2, box->x2, box->y2,
4016                            MOTION_DIRECTION_POSITIVE_Y);
4017         }
4018         for (i = band_below_start; i < band_below_end; i++) {
4019                 pixman_box32_t *box= &boxes[i];
4020                 add_border(&band_merge, box->x1, box->y1, box->x2, box->y1,
4021                            MOTION_DIRECTION_NEGATIVE_Y);
4022         }
4023         qsort(band_merge.data,
4024               band_merge.size / sizeof *border,
4025               sizeof *border,
4026               compare_lines_x);
4027
4028         /* Combine the two combined bands so that any overlapping border is
4029          * eliminated. */
4030         prev_border = NULL;
4031         wl_array_for_each(border, &band_merge) {
4032                 assert(border->line.a.y == border->line.b.y);
4033                 assert(!prev_border ||
4034                        prev_border->line.a.y == border->line.a.y);
4035                 assert(!prev_border ||
4036                        (prev_border->line.a.x != border->line.a.x ||
4037                         prev_border->line.b.x != border->line.b.x));
4038                 assert(!prev_border ||
4039                        prev_border->line.a.x <= border->line.a.x);
4040
4041                 if (prev_border &&
4042                     prev_border->line.a.x == border->line.a.x) {
4043                         /*
4044                          * ------------ +
4045                          * -------      =
4046                          * [     ]-----
4047                          */
4048                         prev_border->line.a.x = border->line.b.x;
4049                 } else if (prev_border &&
4050                            prev_border->line.b.x == border->line.b.x) {
4051                         /*
4052                          * ------------ +
4053                          *       ------ =
4054                          * ------[    ]
4055                          */
4056                         prev_border->line.b.x = border->line.a.x;
4057                 } else if (prev_border &&
4058                            prev_border->line.b.x == border->line.a.x) {
4059                         /*
4060                          * --------        +
4061                          *         ------  =
4062                          * --------------
4063                          */
4064                         prev_border->line.b.x = border->line.b.x;
4065                 } else if (prev_border &&
4066                            prev_border->line.b.x >= border->line.a.x) {
4067                         /*
4068                          * --------------- +
4069                          *      ------     =
4070                          * -----[    ]----
4071                          */
4072                         new_border = add_border(borders,
4073                                                 border->line.b.x,
4074                                                 border->line.b.y,
4075                                                 prev_border->line.b.x,
4076                                                 prev_border->line.b.y,
4077                                                 prev_border->blocking_dir);
4078                         prev_border->line.b.x = border->line.a.x;
4079                         prev_border = new_border;
4080                 } else {
4081                         assert(!prev_border ||
4082                                prev_border->line.b.x < border->line.a.x);
4083                         /*
4084                          * First border or non-overlapping.
4085                          *
4086                          * -----           +
4087                          *        -----    =
4088                          * -----  -----
4089                          */
4090                         new_border = wl_array_add(borders, sizeof *border);
4091                         *new_border = *border;
4092                         prev_border = new_border;
4093                 }
4094         }
4095
4096         wl_array_release(&band_merge);
4097 }
4098
4099 static void
4100 add_band_bottom_edges(pixman_box32_t *boxes,
4101                       int band_start,
4102                       int band_end,
4103                       struct wl_array *borders)
4104 {
4105         int i;
4106
4107         for (i = band_start; i < band_end; i++) {
4108                 add_border(borders,
4109                            boxes[i].x1, boxes[i].y2,
4110                            boxes[i].x2, boxes[i].y2,
4111                            MOTION_DIRECTION_POSITIVE_Y);
4112         }
4113 }
4114
4115 static void
4116 region_to_outline(pixman_region32_t *region, struct wl_array *borders)
4117 {
4118         pixman_box32_t *boxes;
4119         int num_boxes;
4120         int i;
4121         int top_most, bottom_most;
4122         int current_roof;
4123         int prev_top;
4124         int band_start, prev_band_start;
4125
4126         /*
4127          * Remove any overlapping lines from the set of rectangles. Note that
4128          * pixman regions are grouped as rows of rectangles, where rectangles
4129          * in one row never touch or overlap and are all of the same height.
4130          *
4131          *             -------- ---                   -------- ---
4132          *             |      | | |                   |      | | |
4133          *   ----------====---- ---         -----------  ----- ---
4134          *   |            |            =>   |            |
4135          *   ----==========---------        -----        ----------
4136          *       |                 |            |                 |
4137          *       -------------------            -------------------
4138          *
4139          */
4140
4141         boxes = pixman_region32_rectangles(region, &num_boxes);
4142         prev_top = 0;
4143         top_most = boxes[0].y1;
4144         current_roof = top_most;
4145         bottom_most = boxes[num_boxes - 1].y2;
4146         band_start = 0;
4147         prev_band_start = 0;
4148         for (i = 0; i < num_boxes; i++) {
4149                 /* Detect if there is a vertical empty space, and add the lower
4150                  * level of the previous band if so was the case. */
4151                 if (i > 0 &&
4152                     boxes[i].y1 != prev_top &&
4153                     boxes[i].y1 != boxes[i - 1].y2) {
4154                         current_roof = boxes[i].y1;
4155                         add_band_bottom_edges(boxes,
4156                                               band_start,
4157                                               i,
4158                                               borders);
4159                 }
4160
4161                 /* Special case adding the last band, since it won't be handled
4162                  * by the band change detection below. */
4163                 if (boxes[i].y1 != current_roof && i == num_boxes - 1) {
4164                         if (boxes[i].y1 != prev_top) {
4165                                 /* The last band is a single box, so we don't
4166                                  * have a prev_band_start to tell us when the
4167                                  * previous band started. */
4168                                 add_non_overlapping_edges(boxes,
4169                                                           band_start,
4170                                                           i,
4171                                                           i + 1,
4172                                                           borders);
4173                         } else {
4174                                 add_non_overlapping_edges(boxes,
4175                                                           prev_band_start,
4176                                                           band_start,
4177                                                           i + 1,
4178                                                           borders);
4179                         }
4180                 }
4181
4182                 /* Detect when passing a band and combine the top border of the
4183                  * just passed band with the bottom band of the previous band.
4184                  */
4185                 if (boxes[i].y1 != top_most && boxes[i].y1 != prev_top) {
4186                         /* Combine the two passed bands. */
4187                         if (prev_top != current_roof) {
4188                                 add_non_overlapping_edges(boxes,
4189                                                           prev_band_start,
4190                                                           band_start,
4191                                                           i,
4192                                                           borders);
4193                         }
4194
4195                         prev_band_start = band_start;
4196                         band_start = i;
4197                 }
4198
4199                 /* Add the top border if the box is part of the current roof. */
4200                 if (boxes[i].y1 == current_roof) {
4201                         add_border(borders,
4202                                    boxes[i].x1, boxes[i].y1,
4203                                    boxes[i].x2, boxes[i].y1,
4204                                    MOTION_DIRECTION_NEGATIVE_Y);
4205                 }
4206
4207                 /* Add the bottom border of the last band. */
4208                 if (boxes[i].y2 == bottom_most) {
4209                         add_border(borders,
4210                                    boxes[i].x1, boxes[i].y2,
4211                                    boxes[i].x2, boxes[i].y2,
4212                                    MOTION_DIRECTION_POSITIVE_Y);
4213                 }
4214
4215                 /* Always add the left border. */
4216                 add_border(borders,
4217                            boxes[i].x1, boxes[i].y1,
4218                            boxes[i].x1, boxes[i].y2,
4219                            MOTION_DIRECTION_NEGATIVE_X);
4220
4221                 /* Always add the right border. */
4222                 add_border(borders,
4223                            boxes[i].x2, boxes[i].y1,
4224                            boxes[i].x2, boxes[i].y2,
4225                            MOTION_DIRECTION_POSITIVE_X);
4226
4227                 prev_top = boxes[i].y1;
4228         }
4229 }
4230
4231 static bool
4232 is_border_horizontal (struct border *border)
4233 {
4234         return border->line.a.y == border->line.b.y;
4235 }
4236
4237 static bool
4238 is_border_blocking_directions(struct border *border,
4239                               uint32_t directions)
4240 {
4241         /* Don't block parallel motions. */
4242         if (is_border_horizontal(border)) {
4243                 if ((directions & (MOTION_DIRECTION_POSITIVE_Y |
4244                                    MOTION_DIRECTION_NEGATIVE_Y)) == 0)
4245                         return false;
4246         } else {
4247                 if ((directions & (MOTION_DIRECTION_POSITIVE_X |
4248                                    MOTION_DIRECTION_NEGATIVE_X)) == 0)
4249                         return false;
4250         }
4251
4252         return (~border->blocking_dir & directions) != directions;
4253 }
4254
4255 static struct border *
4256 get_closest_border(struct wl_array *borders,
4257                    struct line *motion,
4258                    uint32_t directions)
4259 {
4260         struct border *border;
4261         struct vec2d intersection;
4262         struct vec2d delta;
4263         double distance_2;
4264         struct border *closest_border = NULL;
4265         double closest_distance_2 = DBL_MAX;
4266
4267         wl_array_for_each(border, borders) {
4268                 if (!is_border_blocking_directions(border, directions))
4269                         continue;
4270
4271                 if (!lines_intersect(&border->line, motion, &intersection))
4272                         continue;
4273
4274                 delta = vec2d_subtract(intersection, motion->a);
4275                 distance_2 = delta.x*delta.x + delta.y*delta.y;
4276                 if (distance_2 < closest_distance_2) {
4277                         closest_border = border;
4278                         closest_distance_2 = distance_2;
4279                 }
4280         }
4281
4282         return closest_border;
4283 }
4284
4285 static void
4286 clamp_to_border(struct border *border,
4287                 struct line *motion,
4288                 uint32_t *motion_dir)
4289 {
4290         /*
4291          * When clamping either rightward or downward motions, the motion needs
4292          * to be clamped so that the destination coordinate does not end up on
4293          * the border (see weston_pointer_clamp_event_to_region). Do this by
4294          * clamping such motions to the border minus the smallest possible
4295          * wl_fixed_t value.
4296          */
4297         if (is_border_horizontal(border)) {
4298                 if (*motion_dir & MOTION_DIRECTION_POSITIVE_Y)
4299                         motion->b.y = border->line.a.y - wl_fixed_to_double(1);
4300                 else
4301                         motion->b.y = border->line.a.y;
4302                 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_Y |
4303                                  MOTION_DIRECTION_NEGATIVE_Y);
4304         } else {
4305                 if (*motion_dir & MOTION_DIRECTION_POSITIVE_X)
4306                         motion->b.x = border->line.a.x - wl_fixed_to_double(1);
4307                 else
4308                         motion->b.x = border->line.a.x;
4309                 *motion_dir &= ~(MOTION_DIRECTION_POSITIVE_X |
4310                                  MOTION_DIRECTION_NEGATIVE_X);
4311         }
4312 }
4313
4314 static uint32_t
4315 get_motion_directions(struct line *motion)
4316 {
4317         uint32_t directions = 0;
4318
4319         if (motion->a.x < motion->b.x)
4320                 directions |= MOTION_DIRECTION_POSITIVE_X;
4321         else if (motion->a.x > motion->b.x)
4322                 directions |= MOTION_DIRECTION_NEGATIVE_X;
4323         if (motion->a.y < motion->b.y)
4324                 directions |= MOTION_DIRECTION_POSITIVE_Y;
4325         else if (motion->a.y > motion->b.y)
4326                 directions |= MOTION_DIRECTION_NEGATIVE_Y;
4327
4328         return directions;
4329 }
4330
4331 static void
4332 weston_pointer_clamp_event_to_region(struct weston_pointer *pointer,
4333                                      struct weston_pointer_motion_event *event,
4334                                      pixman_region32_t *region,
4335                                      wl_fixed_t *clamped_x,
4336                                      wl_fixed_t *clamped_y)
4337 {
4338         wl_fixed_t x, y;
4339         wl_fixed_t sx, sy;
4340         wl_fixed_t old_sx = pointer->sx;
4341         wl_fixed_t old_sy = pointer->sy;
4342         struct wl_array borders;
4343         struct line motion;
4344         struct border *closest_border;
4345         float new_x_f, new_y_f;
4346         uint32_t directions;
4347
4348         weston_pointer_motion_to_abs(pointer, event, &x, &y);
4349         weston_view_from_global_fixed(pointer->focus, x, y, &sx, &sy);
4350
4351         wl_array_init(&borders);
4352
4353         /*
4354          * Generate borders given the confine region we are to use. The borders
4355          * are defined to be the outer region of the allowed area. This means
4356          * top/left borders are "within" the allowed area, while bottom/right
4357          * borders are outside. This needs to be considered when clamping
4358          * confined motion vectors.
4359          */
4360         region_to_outline(region, &borders);
4361
4362         motion = (struct line) {
4363                 .a = (struct vec2d) {
4364                         .x = wl_fixed_to_double(old_sx),
4365                         .y = wl_fixed_to_double(old_sy),
4366                 },
4367                 .b = (struct vec2d) {
4368                         .x = wl_fixed_to_double(sx),
4369                         .y = wl_fixed_to_double(sy),
4370                 },
4371         };
4372         directions = get_motion_directions(&motion);
4373
4374         while (directions) {
4375                 closest_border = get_closest_border(&borders,
4376                                                     &motion,
4377                                                     directions);
4378                 if (closest_border)
4379                         clamp_to_border(closest_border, &motion, &directions);
4380                 else
4381                         break;
4382         }
4383
4384         weston_view_to_global_float(pointer->focus,
4385                                     (float) motion.b.x, (float) motion.b.y,
4386                                     &new_x_f, &new_y_f);
4387         *clamped_x = wl_fixed_from_double(new_x_f);
4388         *clamped_y = wl_fixed_from_double(new_y_f);
4389
4390         wl_array_release(&borders);
4391 }
4392
4393 static double
4394 point_to_border_distance_2(struct border *border, double x, double y)
4395 {
4396         double orig_x, orig_y;
4397         double dx, dy;
4398
4399         if (is_border_horizontal(border)) {
4400                 if (x < border->line.a.x)
4401                         orig_x = border->line.a.x;
4402                 else if (x > border->line.b.x)
4403                         orig_x = border->line.b.x;
4404                 else
4405                         orig_x = x;
4406                 orig_y = border->line.a.y;
4407         } else {
4408                 if (y < border->line.a.y)
4409                         orig_y = border->line.a.y;
4410                 else if (y > border->line.b.y)
4411                         orig_y = border->line.b.y;
4412                 else
4413                         orig_y = y;
4414                 orig_x = border->line.a.x;
4415         }
4416
4417
4418         dx = fabs(orig_x - x);
4419         dy = fabs(orig_y - y);
4420         return dx*dx + dy*dy;
4421 }
4422
4423 static void
4424 warp_to_behind_border(struct border *border, wl_fixed_t *sx, wl_fixed_t *sy)
4425 {
4426         switch (border->blocking_dir) {
4427         case MOTION_DIRECTION_POSITIVE_X:
4428         case MOTION_DIRECTION_NEGATIVE_X:
4429                 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_X)
4430                         *sx = wl_fixed_from_double(border->line.a.x) - 1;
4431                 else
4432                         *sx = wl_fixed_from_double(border->line.a.x) + 1;
4433                 if (*sy < wl_fixed_from_double(border->line.a.y))
4434                         *sy = wl_fixed_from_double(border->line.a.y) + 1;
4435                 else if (*sy > wl_fixed_from_double(border->line.b.y))
4436                         *sy = wl_fixed_from_double(border->line.b.y) - 1;
4437                 break;
4438         case MOTION_DIRECTION_POSITIVE_Y:
4439         case MOTION_DIRECTION_NEGATIVE_Y:
4440                 if (border->blocking_dir == MOTION_DIRECTION_POSITIVE_Y)
4441                         *sy = wl_fixed_from_double(border->line.a.y) - 1;
4442                 else
4443                         *sy = wl_fixed_from_double(border->line.a.y) + 1;
4444                 if (*sx < wl_fixed_from_double(border->line.a.x))
4445                         *sx = wl_fixed_from_double(border->line.a.x) + 1;
4446                 else if (*sx > wl_fixed_from_double(border->line.b.x))
4447                         *sx = wl_fixed_from_double(border->line.b.x) - 1;
4448                 break;
4449         }
4450 }
4451
4452 static void
4453 maybe_warp_confined_pointer(struct weston_pointer_constraint *constraint)
4454 {
4455         wl_fixed_t x;
4456         wl_fixed_t y;
4457         wl_fixed_t sx;
4458         wl_fixed_t sy;
4459
4460         weston_view_from_global_fixed(constraint->view,
4461                                       constraint->pointer->x,
4462                                       constraint->pointer->y,
4463                                       &sx,
4464                                       &sy);
4465
4466         if (!is_within_constraint_region(constraint, sx, sy)) {
4467                 double xf = wl_fixed_to_double(sx);
4468                 double yf = wl_fixed_to_double(sy);
4469                 pixman_region32_t confine_region;
4470                 struct wl_array borders;
4471                 struct border *border;
4472                 double closest_distance_2 = DBL_MAX;
4473                 struct border *closest_border = NULL;
4474
4475                 wl_array_init(&borders);
4476
4477                 pixman_region32_init(&confine_region);
4478                 pixman_region32_intersect(&confine_region,
4479                                           &constraint->view->surface->input,
4480                                           &constraint->region);
4481                 region_to_outline(&confine_region, &borders);
4482                 pixman_region32_fini(&confine_region);
4483
4484                 wl_array_for_each(border, &borders) {
4485                         double distance_2;
4486
4487                         distance_2 = point_to_border_distance_2(border, xf, yf);
4488                         if (distance_2 < closest_distance_2) {
4489                                 closest_border = border;
4490                                 closest_distance_2 = distance_2;
4491                         }
4492                 }
4493                 assert(closest_border);
4494
4495                 warp_to_behind_border(closest_border, &sx, &sy);
4496
4497                 wl_array_release(&borders);
4498
4499                 weston_view_to_global_fixed(constraint->view, sx, sy, &x, &y);
4500                 weston_pointer_move_to(constraint->pointer, x, y);
4501         }
4502 }
4503
4504 static void
4505 confined_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
4506                                      const struct timespec *time,
4507                                      struct weston_pointer_motion_event *event)
4508 {
4509         struct weston_pointer_constraint *constraint =
4510                 container_of(grab, struct weston_pointer_constraint, grab);
4511         struct weston_pointer *pointer = grab->pointer;
4512         struct weston_surface *surface;
4513         wl_fixed_t x, y;
4514         wl_fixed_t old_sx = pointer->sx;
4515         wl_fixed_t old_sy = pointer->sy;
4516         pixman_region32_t confine_region;
4517
4518         assert(pointer->focus);
4519         assert(pointer->focus->surface == constraint->surface);
4520
4521         surface = pointer->focus->surface;
4522
4523         pixman_region32_init(&confine_region);
4524         pixman_region32_intersect(&confine_region,
4525                                   &surface->input,
4526                                   &constraint->region);
4527         weston_pointer_clamp_event_to_region(pointer, event,
4528                                              &confine_region, &x, &y);
4529         weston_pointer_move_to(pointer, x, y);
4530         pixman_region32_fini(&confine_region);
4531
4532         weston_view_from_global_fixed(pointer->focus, x, y,
4533                                       &pointer->sx, &pointer->sy);
4534
4535         if (old_sx != pointer->sx || old_sy != pointer->sy) {
4536                 pointer_send_motion(pointer, time,
4537                                     pointer->sx, pointer->sy);
4538         }
4539
4540         pointer_send_relative_motion(pointer, time, event);
4541 }
4542
4543 static void
4544 confined_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
4545                                      const struct timespec *time,
4546                                      uint32_t button,
4547                                      uint32_t state_w)
4548 {
4549         weston_pointer_send_button(grab->pointer, time, button, state_w);
4550 }
4551
4552 static void
4553 confined_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
4554                                    const struct timespec *time,
4555                                    struct weston_pointer_axis_event *event)
4556 {
4557         weston_pointer_send_axis(grab->pointer, time, event);
4558 }
4559
4560 static void
4561 confined_pointer_grab_pointer_axis_source(struct weston_pointer_grab *grab,
4562                                           uint32_t source)
4563 {
4564         weston_pointer_send_axis_source(grab->pointer, source);
4565 }
4566
4567 static void
4568 confined_pointer_grab_pointer_frame(struct weston_pointer_grab *grab)
4569 {
4570         weston_pointer_send_frame(grab->pointer);
4571 }
4572
4573 static void
4574 confined_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
4575 {
4576         struct weston_pointer_constraint *constraint =
4577                 container_of(grab, struct weston_pointer_constraint, grab);
4578
4579         disable_pointer_constraint(constraint);
4580 }
4581
4582 static const struct weston_pointer_grab_interface
4583                                 confined_pointer_grab_interface = {
4584         confined_pointer_grab_pointer_focus,
4585         confined_pointer_grab_pointer_motion,
4586         confined_pointer_grab_pointer_button,
4587         confined_pointer_grab_pointer_axis,
4588         confined_pointer_grab_pointer_axis_source,
4589         confined_pointer_grab_pointer_frame,
4590         confined_pointer_grab_pointer_cancel,
4591 };
4592
4593 static void
4594 confined_pointer_destroy(struct wl_client *client,
4595                          struct wl_resource *resource)
4596 {
4597         wl_resource_destroy(resource);
4598 }
4599
4600 static void
4601 confined_pointer_set_region(struct wl_client *client,
4602                             struct wl_resource *resource,
4603                             struct wl_resource *region_resource)
4604 {
4605         struct weston_pointer_constraint *constraint =
4606                 wl_resource_get_user_data(resource);
4607         struct weston_region *region = region_resource ?
4608                 wl_resource_get_user_data(region_resource) : NULL;
4609
4610         if (!constraint)
4611                 return;
4612
4613         if (region) {
4614                 pixman_region32_copy(&constraint->region_pending,
4615                                      &region->region);
4616         } else {
4617                 pixman_region32_fini(&constraint->region_pending);
4618                 region_init_infinite(&constraint->region_pending);
4619         }
4620         constraint->region_is_pending = true;
4621 }
4622
4623 static const struct zwp_confined_pointer_v1_interface confined_pointer_interface = {
4624         confined_pointer_destroy,
4625         confined_pointer_set_region,
4626 };
4627
4628 static void
4629 pointer_constraints_confine_pointer(struct wl_client *client,
4630                                     struct wl_resource *resource,
4631                                     uint32_t id,
4632                                     struct wl_resource *surface_resource,
4633                                     struct wl_resource *pointer_resource,
4634                                     struct wl_resource *region_resource,
4635                                     uint32_t lifetime)
4636 {
4637         struct weston_surface *surface =
4638                 wl_resource_get_user_data(surface_resource);
4639         struct weston_pointer *pointer = wl_resource_get_user_data(pointer_resource);
4640         struct weston_region *region = region_resource ?
4641                 wl_resource_get_user_data(region_resource) : NULL;
4642
4643         init_pointer_constraint(resource, id, surface, pointer, region, lifetime,
4644                                 &zwp_confined_pointer_v1_interface,
4645                                 &confined_pointer_interface,
4646                                 &confined_pointer_grab_interface);
4647 }
4648
4649 static const struct zwp_pointer_constraints_v1_interface pointer_constraints_interface = {
4650         pointer_constraints_destroy,
4651         pointer_constraints_lock_pointer,
4652         pointer_constraints_confine_pointer,
4653 };
4654
4655 static void
4656 bind_pointer_constraints(struct wl_client *client, void *data,
4657                          uint32_t version, uint32_t id)
4658 {
4659         struct wl_resource *resource;
4660
4661         resource = wl_resource_create(client,
4662                                       &zwp_pointer_constraints_v1_interface,
4663                                       1, id);
4664
4665         wl_resource_set_implementation(resource, &pointer_constraints_interface,
4666                                        NULL, NULL);
4667 }
4668
4669 static void
4670 input_timestamps_destroy(struct wl_client *client,
4671                          struct wl_resource *resource)
4672 {
4673         wl_resource_destroy(resource);
4674 }
4675
4676 static const struct zwp_input_timestamps_v1_interface
4677                                 input_timestamps_interface = {
4678         input_timestamps_destroy,
4679 };
4680
4681 static void
4682 input_timestamps_manager_destroy(struct wl_client *client,
4683                                  struct wl_resource *resource)
4684 {
4685         wl_resource_destroy(resource);
4686 }
4687
4688 static void
4689 input_timestamps_manager_get_keyboard_timestamps(struct wl_client *client,
4690                                                  struct wl_resource *resource,
4691                                                  uint32_t id,
4692                                                  struct wl_resource *keyboard_resource)
4693 {
4694         struct weston_keyboard *keyboard =
4695                 wl_resource_get_user_data(keyboard_resource);
4696         struct wl_resource *input_ts;
4697
4698         input_ts = wl_resource_create(client,
4699                                       &zwp_input_timestamps_v1_interface,
4700                                       1, id);
4701         if (!input_ts) {
4702                 wl_client_post_no_memory(client);
4703                 return;
4704         }
4705
4706         if (keyboard) {
4707                 wl_list_insert(&keyboard->timestamps_list,
4708                                wl_resource_get_link(input_ts));
4709         } else {
4710                 wl_list_init(wl_resource_get_link(input_ts));
4711         }
4712
4713         wl_resource_set_implementation(input_ts,
4714                                        &input_timestamps_interface,
4715                                        keyboard_resource,
4716                                        unbind_resource);
4717 }
4718
4719 static void
4720 input_timestamps_manager_get_pointer_timestamps(struct wl_client *client,
4721                                                 struct wl_resource *resource,
4722                                                 uint32_t id,
4723                                                 struct wl_resource *pointer_resource)
4724 {
4725         struct weston_pointer *pointer =
4726                 wl_resource_get_user_data(pointer_resource);
4727         struct wl_resource *input_ts;
4728
4729         input_ts = wl_resource_create(client,
4730                                       &zwp_input_timestamps_v1_interface,
4731                                       1, id);
4732         if (!input_ts) {
4733                 wl_client_post_no_memory(client);
4734                 return;
4735         }
4736
4737         if (pointer) {
4738                 wl_list_insert(&pointer->timestamps_list,
4739                                wl_resource_get_link(input_ts));
4740         } else {
4741                 wl_list_init(wl_resource_get_link(input_ts));
4742         }
4743
4744         wl_resource_set_implementation(input_ts,
4745                                        &input_timestamps_interface,
4746                                        pointer_resource,
4747                                        unbind_resource);
4748 }
4749
4750 static void
4751 input_timestamps_manager_get_touch_timestamps(struct wl_client *client,
4752                                               struct wl_resource *resource,
4753                                               uint32_t id,
4754                                               struct wl_resource *touch_resource)
4755 {
4756         struct weston_touch *touch = wl_resource_get_user_data(touch_resource);
4757         struct wl_resource *input_ts;
4758
4759         input_ts = wl_resource_create(client,
4760                                       &zwp_input_timestamps_v1_interface,
4761                                       1, id);
4762         if (!input_ts) {
4763                 wl_client_post_no_memory(client);
4764                 return;
4765         }
4766
4767         if (touch) {
4768                 wl_list_insert(&touch->timestamps_list,
4769                                wl_resource_get_link(input_ts));
4770         } else {
4771                 wl_list_init(wl_resource_get_link(input_ts));
4772         }
4773
4774         wl_resource_set_implementation(input_ts,
4775                                        &input_timestamps_interface,
4776                                        touch_resource,
4777                                        unbind_resource);
4778 }
4779
4780 static const struct zwp_input_timestamps_manager_v1_interface
4781                                 input_timestamps_manager_interface = {
4782         input_timestamps_manager_destroy,
4783         input_timestamps_manager_get_keyboard_timestamps,
4784         input_timestamps_manager_get_pointer_timestamps,
4785         input_timestamps_manager_get_touch_timestamps,
4786 };
4787
4788 static void
4789 bind_input_timestamps_manager(struct wl_client *client, void *data,
4790                               uint32_t version, uint32_t id)
4791 {
4792         struct wl_resource *resource =
4793                 wl_resource_create(client,
4794                                    &zwp_input_timestamps_manager_v1_interface,
4795                                    1, id);
4796
4797         if (resource == NULL) {
4798                 wl_client_post_no_memory(client);
4799                 return;
4800         }
4801
4802         wl_resource_set_implementation(resource,
4803                                        &input_timestamps_manager_interface,
4804                                        NULL, NULL);
4805 }
4806
4807 int
4808 weston_input_init(struct weston_compositor *compositor)
4809 {
4810         if (!wl_global_create(compositor->wl_display,
4811                               &zwp_relative_pointer_manager_v1_interface, 1,
4812                               compositor, bind_relative_pointer_manager))
4813                 return -1;
4814
4815         if (!wl_global_create(compositor->wl_display,
4816                               &zwp_pointer_constraints_v1_interface, 1,
4817                               NULL, bind_pointer_constraints))
4818                 return -1;
4819
4820         if (!wl_global_create(compositor->wl_display,
4821                               &zwp_input_timestamps_manager_v1_interface, 1,
4822                               NULL, bind_input_timestamps_manager))
4823                 return -1;
4824
4825         return 0;
4826 }