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