Don't protect xkb_*_unref from NULL
[platform/upstream/weston.git] / src / input.c
1 /*
2  * Copyright © 2013 Intel Corporation
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and
5  * its documentation for any purpose is hereby granted without fee, provided
6  * that the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation, and that the name of the copyright holders not be used in
9  * advertising or publicity pertaining to distribution of the software
10  * without specific, written prior permission.  The copyright holders make
11  * no representations about the suitability of this software for any
12  * purpose.  It is provided "as is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22
23 #include "config.h"
24
25 #include <stdlib.h>
26 #include <stdint.h>
27 #include <string.h>
28 #include <sys/mman.h>
29 #include <assert.h>
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <limits.h>
33
34 #include "../shared/os-compatibility.h"
35 #include "compositor.h"
36
37 static void
38 empty_region(pixman_region32_t *region)
39 {
40         pixman_region32_fini(region);
41         pixman_region32_init(region);
42 }
43
44 static void unbind_resource(struct wl_resource *resource)
45 {
46         wl_list_remove(wl_resource_get_link(resource));
47 }
48
49 WL_EXPORT void
50 weston_seat_repick(struct weston_seat *seat)
51 {
52         const struct weston_pointer *pointer = seat->pointer;
53
54         if (pointer == NULL)
55                 return;
56
57         pointer->grab->interface->focus(seat->pointer->grab);
58 }
59
60 static void
61 weston_compositor_idle_inhibit(struct weston_compositor *compositor)
62 {
63         weston_compositor_wake(compositor);
64         compositor->idle_inhibit++;
65 }
66
67 static void
68 weston_compositor_idle_release(struct weston_compositor *compositor)
69 {
70         compositor->idle_inhibit--;
71         weston_compositor_wake(compositor);
72 }
73
74 static void
75 pointer_focus_view_destroyed(struct wl_listener *listener, void *data)
76 {
77         struct weston_pointer *pointer =
78                 container_of(listener, struct weston_pointer,
79                              focus_view_listener);
80
81         weston_pointer_set_focus(pointer, NULL, 0, 0);
82 }
83
84 static void
85 pointer_focus_resource_destroyed(struct wl_listener *listener, void *data)
86 {
87         struct weston_pointer *pointer =
88                 container_of(listener, struct weston_pointer,
89                              focus_resource_listener);
90
91         weston_pointer_set_focus(pointer, NULL, 0, 0);
92 }
93
94 static void
95 keyboard_focus_resource_destroyed(struct wl_listener *listener, void *data)
96 {
97         struct weston_keyboard *keyboard =
98                 container_of(listener, struct weston_keyboard,
99                              focus_resource_listener);
100
101         weston_keyboard_set_focus(keyboard, NULL);
102 }
103
104 static void
105 touch_focus_view_destroyed(struct wl_listener *listener, void *data)
106 {
107         struct weston_touch *touch =
108                 container_of(listener, struct weston_touch,
109                              focus_view_listener);
110
111         weston_touch_set_focus(touch->seat, NULL);
112 }
113
114 static void
115 touch_focus_resource_destroyed(struct wl_listener *listener, void *data)
116 {
117         struct weston_touch *touch =
118                 container_of(listener, struct weston_touch,
119                              focus_resource_listener);
120
121         weston_touch_set_focus(touch->seat, NULL);
122 }
123
124 static void
125 move_resources(struct wl_list *destination, struct wl_list *source)
126 {
127         wl_list_insert_list(destination, source);
128         wl_list_init(source);
129 }
130
131 static void
132 move_resources_for_client(struct wl_list *destination,
133                           struct wl_list *source,
134                           struct wl_client *client)
135 {
136         struct wl_resource *resource, *tmp;
137         wl_resource_for_each_safe(resource, tmp, source) {
138                 if (wl_resource_get_client(resource) == client) {
139                         wl_list_remove(wl_resource_get_link(resource));
140                         wl_list_insert(destination,
141                                        wl_resource_get_link(resource));
142                 }
143         }
144 }
145
146 static void
147 default_grab_pointer_focus(struct weston_pointer_grab *grab)
148 {
149         struct weston_pointer *pointer = grab->pointer;
150         struct weston_view *view;
151         wl_fixed_t sx, sy;
152
153         if (pointer->button_count > 0)
154                 return;
155
156         view = weston_compositor_pick_view(pointer->seat->compositor,
157                                            pointer->x, pointer->y,
158                                            &sx, &sy);
159
160         if (pointer->focus != view || pointer->sx != sx || pointer->sy != sy)
161                 weston_pointer_set_focus(pointer, view, sx, sy);
162 }
163
164 static void
165 default_grab_pointer_motion(struct weston_pointer_grab *grab, uint32_t time,
166                             wl_fixed_t x, wl_fixed_t y)
167 {
168         struct weston_pointer *pointer = grab->pointer;
169         struct wl_list *resource_list;
170         struct wl_resource *resource;
171
172         if (pointer->focus)
173                 weston_view_from_global_fixed(pointer->focus, x, y,
174                                               &pointer->sx, &pointer->sy);
175
176         weston_pointer_move(pointer, x, y);
177
178         resource_list = &pointer->focus_resource_list;
179         wl_resource_for_each(resource, resource_list) {
180                 wl_pointer_send_motion(resource, time,
181                                        pointer->sx, pointer->sy);
182         }
183 }
184
185 static void
186 default_grab_pointer_button(struct weston_pointer_grab *grab,
187                             uint32_t time, uint32_t button, uint32_t state_w)
188 {
189         struct weston_pointer *pointer = grab->pointer;
190         struct weston_compositor *compositor = pointer->seat->compositor;
191         struct weston_view *view;
192         struct wl_resource *resource;
193         uint32_t serial;
194         enum wl_pointer_button_state state = state_w;
195         struct wl_display *display = compositor->wl_display;
196         wl_fixed_t sx, sy;
197         struct wl_list *resource_list;
198
199         resource_list = &pointer->focus_resource_list;
200         if (!wl_list_empty(resource_list)) {
201                 serial = wl_display_next_serial(display);
202                 wl_resource_for_each(resource, resource_list)
203                         wl_pointer_send_button(resource,
204                                                serial,
205                                                time,
206                                                button,
207                                                state_w);
208         }
209
210         if (pointer->button_count == 0 &&
211             state == WL_POINTER_BUTTON_STATE_RELEASED) {
212                 view = weston_compositor_pick_view(compositor,
213                                                    pointer->x, pointer->y,
214                                                    &sx, &sy);
215
216                 weston_pointer_set_focus(pointer, view, sx, sy);
217         }
218 }
219
220 static void
221 default_grab_pointer_cancel(struct weston_pointer_grab *grab)
222 {
223 }
224
225 static const struct weston_pointer_grab_interface
226                                 default_pointer_grab_interface = {
227         default_grab_pointer_focus,
228         default_grab_pointer_motion,
229         default_grab_pointer_button,
230         default_grab_pointer_cancel,
231 };
232
233 static void
234 default_grab_touch_down(struct weston_touch_grab *grab, uint32_t time,
235                         int touch_id, wl_fixed_t sx, wl_fixed_t sy)
236 {
237         struct weston_touch *touch = grab->touch;
238         struct wl_display *display = touch->seat->compositor->wl_display;
239         uint32_t serial;
240         struct wl_resource *resource;
241         struct wl_list *resource_list;
242
243         resource_list = &touch->focus_resource_list;
244
245         if (!wl_list_empty(resource_list) && touch->focus) {
246                 serial = wl_display_next_serial(display);
247                 wl_resource_for_each(resource, resource_list)
248                                 wl_touch_send_down(resource, serial, time,
249                                                    touch->focus->surface->resource,
250                                                    touch_id, sx, sy);
251         }
252 }
253
254 static void
255 default_grab_touch_up(struct weston_touch_grab *grab,
256                       uint32_t time, int touch_id)
257 {
258         struct weston_touch *touch = grab->touch;
259         struct wl_display *display = touch->seat->compositor->wl_display;
260         uint32_t serial;
261         struct wl_resource *resource;
262         struct wl_list *resource_list;
263
264         resource_list = &touch->focus_resource_list;
265
266         if (!wl_list_empty(resource_list)) {
267                 serial = wl_display_next_serial(display);
268                 wl_resource_for_each(resource, resource_list)
269                         wl_touch_send_up(resource, serial, time, touch_id);
270         }
271 }
272
273 static void
274 default_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time,
275                           int touch_id, wl_fixed_t sx, wl_fixed_t sy)
276 {
277         struct weston_touch *touch = grab->touch;
278         struct wl_resource *resource;
279         struct wl_list *resource_list;
280
281         resource_list = &touch->focus_resource_list;
282
283         wl_resource_for_each(resource, resource_list) {
284                 wl_touch_send_motion(resource, time,
285                                      touch_id, sx, sy);
286         }
287 }
288
289 static void
290 default_grab_touch_frame(struct weston_touch_grab *grab)
291 {
292         struct wl_resource *resource;
293
294         wl_resource_for_each(resource, &grab->touch->focus_resource_list)
295                 wl_touch_send_frame(resource);
296 }
297
298 static void
299 default_grab_touch_cancel(struct weston_touch_grab *grab)
300 {
301 }
302
303 static const struct weston_touch_grab_interface default_touch_grab_interface = {
304         default_grab_touch_down,
305         default_grab_touch_up,
306         default_grab_touch_motion,
307         default_grab_touch_frame,
308         default_grab_touch_cancel,
309 };
310
311 static void
312 default_grab_keyboard_key(struct weston_keyboard_grab *grab,
313                           uint32_t time, uint32_t key, uint32_t state)
314 {
315         struct weston_keyboard *keyboard = grab->keyboard;
316         struct wl_resource *resource;
317         struct wl_display *display = keyboard->seat->compositor->wl_display;
318         uint32_t serial;
319         struct wl_list *resource_list;
320
321         resource_list = &keyboard->focus_resource_list;
322         if (!wl_list_empty(resource_list)) {
323                 serial = wl_display_next_serial(display);
324                 wl_resource_for_each(resource, resource_list)
325                         wl_keyboard_send_key(resource,
326                                              serial,
327                                              time,
328                                              key,
329                                              state);
330         }
331 }
332
333 static void
334 send_modifiers_to_resource(struct weston_keyboard *keyboard,
335                            struct wl_resource *resource,
336                            uint32_t serial)
337 {
338         wl_keyboard_send_modifiers(resource,
339                                    serial,
340                                    keyboard->modifiers.mods_depressed,
341                                    keyboard->modifiers.mods_latched,
342                                    keyboard->modifiers.mods_locked,
343                                    keyboard->modifiers.group);
344 }
345
346 static void
347 send_modifiers_to_client_in_list(struct wl_client *client,
348                                  struct wl_list *list,
349                                  uint32_t serial,
350                                  struct weston_keyboard *keyboard)
351 {
352         struct wl_resource *resource;
353
354         wl_resource_for_each(resource, list) {
355                 if (wl_resource_get_client(resource) == client)
356                         send_modifiers_to_resource(keyboard,
357                                                    resource,
358                                                    serial);
359         }
360 }
361
362 static struct wl_resource *
363 find_resource_for_surface(struct wl_list *list, struct weston_surface *surface)
364 {
365         if (!surface)
366                 return NULL;
367
368         if (!surface->resource)
369                 return NULL;
370
371         return wl_resource_find_for_client(list, wl_resource_get_client(surface->resource));
372 }
373
374 static struct wl_resource *
375 find_resource_for_view(struct wl_list *list, struct weston_view *view)
376 {
377         if (!view)
378                 return NULL;
379
380         return find_resource_for_surface(list, view->surface);
381 }
382
383 static void
384 default_grab_keyboard_modifiers(struct weston_keyboard_grab *grab,
385                                 uint32_t serial, uint32_t mods_depressed,
386                                 uint32_t mods_latched,
387                                 uint32_t mods_locked, uint32_t group)
388 {
389         struct weston_keyboard *keyboard = grab->keyboard;
390         struct weston_pointer *pointer = grab->keyboard->seat->pointer;
391         struct wl_resource *resource;
392         struct wl_list *resource_list;
393
394         resource_list = &keyboard->focus_resource_list;
395
396         wl_resource_for_each(resource, resource_list) {
397                 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
398                                            mods_latched, mods_locked, group);
399         }
400         if (pointer && pointer->focus && pointer->focus->surface->resource &&
401             pointer->focus->surface != keyboard->focus) {
402                 struct wl_client *pointer_client =
403                         wl_resource_get_client(pointer->focus->surface->resource);
404                 send_modifiers_to_client_in_list(pointer_client,
405                                                  &keyboard->resource_list,
406                                                  serial,
407                                                  keyboard);
408         }
409 }
410
411 static void
412 default_grab_keyboard_cancel(struct weston_keyboard_grab *grab)
413 {
414 }
415
416 static const struct weston_keyboard_grab_interface
417                                 default_keyboard_grab_interface = {
418         default_grab_keyboard_key,
419         default_grab_keyboard_modifiers,
420         default_grab_keyboard_cancel,
421 };
422
423 static void
424 pointer_unmap_sprite(struct weston_pointer *pointer)
425 {
426         if (weston_surface_is_mapped(pointer->sprite->surface))
427                 weston_surface_unmap(pointer->sprite->surface);
428
429         wl_list_remove(&pointer->sprite_destroy_listener.link);
430         pointer->sprite->surface->configure = NULL;
431         pointer->sprite->surface->configure_private = NULL;
432         weston_view_destroy(pointer->sprite);
433         pointer->sprite = NULL;
434 }
435
436 static void
437 pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
438 {
439         struct weston_pointer *pointer =
440                 container_of(listener, struct weston_pointer,
441                              sprite_destroy_listener);
442
443         pointer->sprite = NULL;
444 }
445
446 static void
447 weston_pointer_reset_state(struct weston_pointer *pointer)
448 {
449         pointer->button_count = 0;
450 }
451
452 static void
453 weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data);
454
455 WL_EXPORT struct weston_pointer *
456 weston_pointer_create(struct weston_seat *seat)
457 {
458         struct weston_pointer *pointer;
459
460         pointer = zalloc(sizeof *pointer);
461         if (pointer == NULL)
462                 return NULL;
463
464         wl_list_init(&pointer->resource_list);
465         wl_list_init(&pointer->focus_resource_list);
466         weston_pointer_set_default_grab(pointer,
467                                         seat->compositor->default_pointer_grab);
468         wl_list_init(&pointer->focus_resource_listener.link);
469         pointer->focus_resource_listener.notify = pointer_focus_resource_destroyed;
470         pointer->default_grab.pointer = pointer;
471         pointer->grab = &pointer->default_grab;
472         wl_signal_init(&pointer->motion_signal);
473         wl_signal_init(&pointer->focus_signal);
474         wl_list_init(&pointer->focus_view_listener.link);
475
476         pointer->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
477
478         /* FIXME: Pick better co-ords. */
479         pointer->x = wl_fixed_from_int(100);
480         pointer->y = wl_fixed_from_int(100);
481
482         pointer->output_destroy_listener.notify =
483                 weston_pointer_handle_output_destroy;
484         wl_signal_add(&seat->compositor->output_destroyed_signal,
485                       &pointer->output_destroy_listener);
486
487         return pointer;
488 }
489
490 WL_EXPORT void
491 weston_pointer_destroy(struct weston_pointer *pointer)
492 {
493         if (pointer->sprite)
494                 pointer_unmap_sprite(pointer);
495
496         /* XXX: What about pointer->resource_list? */
497
498         wl_list_remove(&pointer->focus_resource_listener.link);
499         wl_list_remove(&pointer->focus_view_listener.link);
500         wl_list_remove(&pointer->output_destroy_listener.link);
501         free(pointer);
502 }
503
504 void
505 weston_pointer_set_default_grab(struct weston_pointer *pointer,
506                 const struct weston_pointer_grab_interface *interface)
507 {
508         if (interface)
509                 pointer->default_grab.interface = interface;
510         else
511                 pointer->default_grab.interface =
512                         &default_pointer_grab_interface;
513 }
514
515 WL_EXPORT struct weston_keyboard *
516 weston_keyboard_create(void)
517 {
518         struct weston_keyboard *keyboard;
519
520         keyboard = zalloc(sizeof *keyboard);
521         if (keyboard == NULL)
522             return NULL;
523
524         wl_list_init(&keyboard->resource_list);
525         wl_list_init(&keyboard->focus_resource_list);
526         wl_list_init(&keyboard->focus_resource_listener.link);
527         keyboard->focus_resource_listener.notify = keyboard_focus_resource_destroyed;
528         wl_array_init(&keyboard->keys);
529         keyboard->default_grab.interface = &default_keyboard_grab_interface;
530         keyboard->default_grab.keyboard = keyboard;
531         keyboard->grab = &keyboard->default_grab;
532         wl_signal_init(&keyboard->focus_signal);
533
534         return keyboard;
535 }
536
537 static void
538 weston_xkb_info_destroy(struct weston_xkb_info *xkb_info);
539
540 WL_EXPORT void
541 weston_keyboard_destroy(struct weston_keyboard *keyboard)
542 {
543         /* XXX: What about keyboard->resource_list? */
544
545 #ifdef ENABLE_XKBCOMMON
546         if (keyboard->seat->compositor->use_xkbcommon) {
547                 xkb_state_unref(keyboard->xkb_state.state);
548                 if (keyboard->xkb_info)
549                         weston_xkb_info_destroy(keyboard->xkb_info);
550                 xkb_keymap_unref(keyboard->pending_keymap);
551         }
552 #endif
553
554         wl_array_release(&keyboard->keys);
555         wl_list_remove(&keyboard->focus_resource_listener.link);
556         free(keyboard);
557 }
558
559 static void
560 weston_touch_reset_state(struct weston_touch *touch)
561 {
562         touch->num_tp = 0;
563 }
564
565 WL_EXPORT struct weston_touch *
566 weston_touch_create(void)
567 {
568         struct weston_touch *touch;
569
570         touch = zalloc(sizeof *touch);
571         if (touch == NULL)
572                 return NULL;
573
574         wl_list_init(&touch->resource_list);
575         wl_list_init(&touch->focus_resource_list);
576         wl_list_init(&touch->focus_view_listener.link);
577         touch->focus_view_listener.notify = touch_focus_view_destroyed;
578         wl_list_init(&touch->focus_resource_listener.link);
579         touch->focus_resource_listener.notify = touch_focus_resource_destroyed;
580         touch->default_grab.interface = &default_touch_grab_interface;
581         touch->default_grab.touch = touch;
582         touch->grab = &touch->default_grab;
583         wl_signal_init(&touch->focus_signal);
584
585         return touch;
586 }
587
588 WL_EXPORT void
589 weston_touch_destroy(struct weston_touch *touch)
590 {
591         /* XXX: What about touch->resource_list? */
592
593         wl_list_remove(&touch->focus_view_listener.link);
594         wl_list_remove(&touch->focus_resource_listener.link);
595         free(touch);
596 }
597
598 static void
599 seat_send_updated_caps(struct weston_seat *seat)
600 {
601         enum wl_seat_capability caps = 0;
602         struct wl_resource *resource;
603
604         if (seat->pointer_device_count > 0)
605                 caps |= WL_SEAT_CAPABILITY_POINTER;
606         if (seat->keyboard_device_count > 0)
607                 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
608         if (seat->touch_device_count > 0)
609                 caps |= WL_SEAT_CAPABILITY_TOUCH;
610
611         wl_resource_for_each(resource, &seat->base_resource_list) {
612                 wl_seat_send_capabilities(resource, caps);
613         }
614         wl_signal_emit(&seat->updated_caps_signal, seat);
615 }
616
617 WL_EXPORT void
618 weston_pointer_set_focus(struct weston_pointer *pointer,
619                          struct weston_view *view,
620                          wl_fixed_t sx, wl_fixed_t sy)
621 {
622         struct weston_keyboard *kbd = pointer->seat->keyboard;
623         struct wl_resource *resource;
624         struct wl_display *display = pointer->seat->compositor->wl_display;
625         uint32_t serial;
626         struct wl_list *focus_resource_list;
627         int refocus = 0;
628
629         if ((!pointer->focus && view) ||
630             (pointer->focus && !view) ||
631             (pointer->focus && pointer->focus->surface != view->surface) ||
632             pointer->sx != sx || pointer->sy != sy)
633                 refocus = 1;
634
635         focus_resource_list = &pointer->focus_resource_list;
636
637         if (!wl_list_empty(focus_resource_list) && refocus) {
638                 serial = wl_display_next_serial(display);
639                 wl_resource_for_each(resource, focus_resource_list) {
640                         wl_pointer_send_leave(resource, serial,
641                                               pointer->focus->surface->resource);
642                 }
643
644                 move_resources(&pointer->resource_list, focus_resource_list);
645         }
646
647         if (find_resource_for_view(&pointer->resource_list, view) && refocus) {
648                 struct wl_client *surface_client =
649                         wl_resource_get_client(view->surface->resource);
650
651                 serial = wl_display_next_serial(display);
652
653                 if (kbd && kbd->focus != view->surface)
654                         send_modifiers_to_client_in_list(surface_client,
655                                                          &kbd->resource_list,
656                                                          serial,
657                                                          kbd);
658
659                 move_resources_for_client(focus_resource_list,
660                                           &pointer->resource_list,
661                                           surface_client);
662
663                 wl_resource_for_each(resource, focus_resource_list) {
664                         wl_pointer_send_enter(resource,
665                                               serial,
666                                               view->surface->resource,
667                                               sx, sy);
668                 }
669
670                 pointer->focus_serial = serial;
671         }
672
673         wl_list_remove(&pointer->focus_view_listener.link);
674         wl_list_init(&pointer->focus_view_listener.link);
675         wl_list_remove(&pointer->focus_resource_listener.link);
676         wl_list_init(&pointer->focus_resource_listener.link);
677         if (view)
678                 wl_signal_add(&view->destroy_signal, &pointer->focus_view_listener);
679         if (view && view->surface->resource)
680                 wl_resource_add_destroy_listener(view->surface->resource,
681                                                  &pointer->focus_resource_listener);
682
683         pointer->focus = view;
684         pointer->focus_view_listener.notify = pointer_focus_view_destroyed;
685         pointer->sx = sx;
686         pointer->sy = sy;
687
688         wl_signal_emit(&pointer->focus_signal, pointer);
689 }
690
691 static void
692 send_enter_to_resource_list(struct wl_list *list,
693                             struct weston_keyboard *keyboard,
694                             struct weston_surface *surface,
695                             uint32_t serial)
696 {
697         struct wl_resource *resource;
698
699         wl_resource_for_each(resource, list) {
700                 send_modifiers_to_resource(keyboard, resource, serial);
701                 wl_keyboard_send_enter(resource, serial,
702                                        surface->resource,
703                                        &keyboard->keys);
704         }
705 }
706
707 WL_EXPORT void
708 weston_keyboard_set_focus(struct weston_keyboard *keyboard,
709                           struct weston_surface *surface)
710 {
711         struct wl_resource *resource;
712         struct wl_display *display = keyboard->seat->compositor->wl_display;
713         uint32_t serial;
714         struct wl_list *focus_resource_list;
715
716         focus_resource_list = &keyboard->focus_resource_list;
717
718         if (!wl_list_empty(focus_resource_list) && keyboard->focus != surface) {
719                 serial = wl_display_next_serial(display);
720                 wl_resource_for_each(resource, focus_resource_list) {
721                         wl_keyboard_send_leave(resource, serial,
722                                         keyboard->focus->resource);
723                 }
724                 move_resources(&keyboard->resource_list, focus_resource_list);
725         }
726
727         if (find_resource_for_surface(&keyboard->resource_list, surface) &&
728             keyboard->focus != surface) {
729                 struct wl_client *surface_client =
730                         wl_resource_get_client(surface->resource);
731
732                 serial = wl_display_next_serial(display);
733
734                 move_resources_for_client(focus_resource_list,
735                                           &keyboard->resource_list,
736                                           surface_client);
737                 send_enter_to_resource_list(focus_resource_list,
738                                             keyboard,
739                                             surface,
740                                             serial);
741                 keyboard->focus_serial = serial;
742         }
743
744         wl_list_remove(&keyboard->focus_resource_listener.link);
745         wl_list_init(&keyboard->focus_resource_listener.link);
746         if (surface && surface->resource)
747                 wl_resource_add_destroy_listener(surface->resource,
748                                                  &keyboard->focus_resource_listener);
749
750         keyboard->focus = surface;
751         wl_signal_emit(&keyboard->focus_signal, keyboard);
752 }
753
754 WL_EXPORT void
755 weston_keyboard_start_grab(struct weston_keyboard *keyboard,
756                            struct weston_keyboard_grab *grab)
757 {
758         keyboard->grab = grab;
759         grab->keyboard = keyboard;
760
761         /* XXX focus? */
762 }
763
764 WL_EXPORT void
765 weston_keyboard_end_grab(struct weston_keyboard *keyboard)
766 {
767         keyboard->grab = &keyboard->default_grab;
768 }
769
770 static void
771 weston_keyboard_cancel_grab(struct weston_keyboard *keyboard)
772 {
773         keyboard->grab->interface->cancel(keyboard->grab);
774 }
775
776 WL_EXPORT void
777 weston_pointer_start_grab(struct weston_pointer *pointer,
778                           struct weston_pointer_grab *grab)
779 {
780         pointer->grab = grab;
781         grab->pointer = pointer;
782         pointer->grab->interface->focus(pointer->grab);
783 }
784
785 WL_EXPORT void
786 weston_pointer_end_grab(struct weston_pointer *pointer)
787 {
788         pointer->grab = &pointer->default_grab;
789         pointer->grab->interface->focus(pointer->grab);
790 }
791
792 static void
793 weston_pointer_cancel_grab(struct weston_pointer *pointer)
794 {
795         pointer->grab->interface->cancel(pointer->grab);
796 }
797
798 WL_EXPORT void
799 weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
800 {
801         touch->grab = grab;
802         grab->touch = touch;
803 }
804
805 WL_EXPORT void
806 weston_touch_end_grab(struct weston_touch *touch)
807 {
808         touch->grab = &touch->default_grab;
809 }
810
811 static void
812 weston_touch_cancel_grab(struct weston_touch *touch)
813 {
814         touch->grab->interface->cancel(touch->grab);
815 }
816
817 static void
818 weston_pointer_clamp_for_output(struct weston_pointer *pointer,
819                                 struct weston_output *output,
820                                 wl_fixed_t *fx, wl_fixed_t *fy)
821 {
822         int x, y;
823
824         x = wl_fixed_to_int(*fx);
825         y = wl_fixed_to_int(*fy);
826
827         if (x < output->x)
828                 *fx = wl_fixed_from_int(output->x);
829         else if (x >= output->x + output->width)
830                 *fx = wl_fixed_from_int(output->x +
831                                         output->width - 1);
832         if (y < output->y)
833                 *fy = wl_fixed_from_int(output->y);
834         else if (y >= output->y + output->height)
835                 *fy = wl_fixed_from_int(output->y +
836                                         output->height - 1);
837 }
838
839 WL_EXPORT void
840 weston_pointer_clamp(struct weston_pointer *pointer, wl_fixed_t *fx, wl_fixed_t *fy)
841 {
842         struct weston_compositor *ec = pointer->seat->compositor;
843         struct weston_output *output, *prev = NULL;
844         int x, y, old_x, old_y, valid = 0;
845
846         x = wl_fixed_to_int(*fx);
847         y = wl_fixed_to_int(*fy);
848         old_x = wl_fixed_to_int(pointer->x);
849         old_y = wl_fixed_to_int(pointer->y);
850
851         wl_list_for_each(output, &ec->output_list, link) {
852                 if (pointer->seat->output && pointer->seat->output != output)
853                         continue;
854                 if (pixman_region32_contains_point(&output->region,
855                                                    x, y, NULL))
856                         valid = 1;
857                 if (pixman_region32_contains_point(&output->region,
858                                                    old_x, old_y, NULL))
859                         prev = output;
860         }
861
862         if (!prev)
863                 prev = pointer->seat->output;
864
865         if (prev && !valid)
866                 weston_pointer_clamp_for_output(pointer, prev, fx, fy);
867 }
868
869 /* Takes absolute values */
870 WL_EXPORT void
871 weston_pointer_move(struct weston_pointer *pointer, wl_fixed_t x, wl_fixed_t y)
872 {
873         int32_t ix, iy;
874
875         weston_pointer_clamp (pointer, &x, &y);
876
877         pointer->x = x;
878         pointer->y = y;
879
880         ix = wl_fixed_to_int(x);
881         iy = wl_fixed_to_int(y);
882
883         if (pointer->sprite) {
884                 weston_view_set_position(pointer->sprite,
885                                          ix - pointer->hotspot_x,
886                                          iy - pointer->hotspot_y);
887                 weston_view_schedule_repaint(pointer->sprite);
888         }
889
890         pointer->grab->interface->focus(pointer->grab);
891         wl_signal_emit(&pointer->motion_signal, pointer);
892 }
893
894 /** Verify if the pointer is in a valid position and move it if it isn't.
895  */
896 static void
897 weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data)
898 {
899         struct weston_pointer *pointer;
900         struct weston_compositor *ec;
901         struct weston_output *output, *closest = NULL;
902         int x, y, distance, min = INT_MAX;
903         wl_fixed_t fx, fy;
904
905         pointer = container_of(listener, struct weston_pointer,
906                                output_destroy_listener);
907         ec = pointer->seat->compositor;
908
909         x = wl_fixed_to_int(pointer->x);
910         y = wl_fixed_to_int(pointer->y);
911
912         wl_list_for_each(output, &ec->output_list, link) {
913                 if (pixman_region32_contains_point(&output->region,
914                                                    x, y, NULL))
915                         return;
916
917                 /* Aproximante the distance from the pointer to the center of
918                  * the output. */
919                 distance = abs(output->x + output->width / 2 - x) +
920                            abs(output->y + output->height / 2 - y);
921                 if (distance < min) {
922                         min = distance;
923                         closest = output;
924                 }
925         }
926
927         /* Nothing to do if there's no output left. */
928         if (!closest)
929                 return;
930
931         fx = pointer->x;
932         fy = pointer->y;
933
934         weston_pointer_clamp_for_output(pointer, closest, &fx, &fy);
935         weston_pointer_move(pointer, fx, fy);
936 }
937
938 WL_EXPORT void
939 notify_motion(struct weston_seat *seat,
940               uint32_t time, wl_fixed_t dx, wl_fixed_t dy)
941 {
942         struct weston_compositor *ec = seat->compositor;
943         struct weston_pointer *pointer = seat->pointer;
944
945         weston_compositor_wake(ec);
946         pointer->grab->interface->motion(pointer->grab, time, pointer->x + dx, pointer->y + dy);
947 }
948
949 static void
950 run_modifier_bindings(struct weston_seat *seat, uint32_t old, uint32_t new)
951 {
952         struct weston_compositor *compositor = seat->compositor;
953         struct weston_keyboard *keyboard = seat->keyboard;
954         uint32_t diff;
955         unsigned int i;
956         struct {
957                 uint32_t xkb;
958                 enum weston_keyboard_modifier weston;
959         } mods[] = {
960                 { keyboard->xkb_info->ctrl_mod, MODIFIER_CTRL },
961                 { keyboard->xkb_info->alt_mod, MODIFIER_ALT },
962                 { keyboard->xkb_info->super_mod, MODIFIER_SUPER },
963                 { keyboard->xkb_info->shift_mod, MODIFIER_SHIFT },
964         };
965
966         diff = new & ~old;
967         for (i = 0; i < ARRAY_LENGTH(mods); i++) {
968                 if (diff & (1 << mods[i].xkb))
969                         weston_compositor_run_modifier_binding(compositor,
970                                                                seat,
971                                                                mods[i].weston,
972                                                                WL_KEYBOARD_KEY_STATE_PRESSED);
973         }
974
975         diff = old & ~new;
976         for (i = 0; i < ARRAY_LENGTH(mods); i++) {
977                 if (diff & (1 << mods[i].xkb))
978                         weston_compositor_run_modifier_binding(compositor,
979                                                                seat,
980                                                                mods[i].weston,
981                                                                WL_KEYBOARD_KEY_STATE_RELEASED);
982         }
983 }
984
985 WL_EXPORT void
986 notify_motion_absolute(struct weston_seat *seat,
987                        uint32_t time, wl_fixed_t x, wl_fixed_t y)
988 {
989         struct weston_compositor *ec = seat->compositor;
990         struct weston_pointer *pointer = seat->pointer;
991
992         weston_compositor_wake(ec);
993         pointer->grab->interface->motion(pointer->grab, time, x, y);
994 }
995
996 WL_EXPORT void
997 weston_surface_activate(struct weston_surface *surface,
998                         struct weston_seat *seat)
999 {
1000         struct weston_compositor *compositor = seat->compositor;
1001
1002         if (seat->keyboard) {
1003                 weston_keyboard_set_focus(seat->keyboard, surface);
1004                 wl_data_device_set_keyboard_focus(seat);
1005         }
1006
1007         wl_signal_emit(&compositor->activate_signal, surface);
1008 }
1009
1010 WL_EXPORT void
1011 notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
1012               enum wl_pointer_button_state state)
1013 {
1014         struct weston_compositor *compositor = seat->compositor;
1015         struct weston_pointer *pointer = seat->pointer;
1016
1017         if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
1018                 weston_compositor_idle_inhibit(compositor);
1019                 if (pointer->button_count == 0) {
1020                         pointer->grab_button = button;
1021                         pointer->grab_time = time;
1022                         pointer->grab_x = pointer->x;
1023                         pointer->grab_y = pointer->y;
1024                 }
1025                 pointer->button_count++;
1026         } else {
1027                 weston_compositor_idle_release(compositor);
1028                 pointer->button_count--;
1029         }
1030
1031         weston_compositor_run_button_binding(compositor, seat, time, button,
1032                                              state);
1033
1034         pointer->grab->interface->button(pointer->grab, time, button, state);
1035
1036         if (pointer->button_count == 1)
1037                 pointer->grab_serial =
1038                         wl_display_get_serial(compositor->wl_display);
1039 }
1040
1041 WL_EXPORT void
1042 notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
1043             wl_fixed_t value)
1044 {
1045         struct weston_compositor *compositor = seat->compositor;
1046         struct weston_pointer *pointer = seat->pointer;
1047         struct wl_resource *resource;
1048         struct wl_list *resource_list;
1049
1050         weston_compositor_wake(compositor);
1051
1052         if (!value)
1053                 return;
1054
1055         if (weston_compositor_run_axis_binding(compositor, seat,
1056                                                    time, axis, value))
1057                 return;
1058
1059         resource_list = &pointer->focus_resource_list;
1060         wl_resource_for_each(resource, resource_list)
1061                 wl_pointer_send_axis(resource, time, axis,
1062                                      value);
1063 }
1064
1065 #ifdef ENABLE_XKBCOMMON
1066 WL_EXPORT void
1067 notify_modifiers(struct weston_seat *seat, uint32_t serial)
1068 {
1069         struct weston_keyboard *keyboard = seat->keyboard;
1070         struct weston_keyboard_grab *grab = keyboard->grab;
1071         uint32_t mods_depressed, mods_latched, mods_locked, group;
1072         uint32_t mods_lookup;
1073         enum weston_led leds = 0;
1074         int changed = 0;
1075
1076         /* Serialize and update our internal state, checking to see if it's
1077          * different to the previous state. */
1078         mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
1079                                                   XKB_STATE_MODS_DEPRESSED);
1080         mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
1081                                                 XKB_STATE_MODS_LATCHED);
1082         mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
1083                                                XKB_STATE_MODS_LOCKED);
1084         group = xkb_state_serialize_layout(keyboard->xkb_state.state,
1085                                            XKB_STATE_LAYOUT_EFFECTIVE);
1086
1087         if (mods_depressed != seat->keyboard->modifiers.mods_depressed ||
1088             mods_latched != seat->keyboard->modifiers.mods_latched ||
1089             mods_locked != seat->keyboard->modifiers.mods_locked ||
1090             group != seat->keyboard->modifiers.group)
1091                 changed = 1;
1092
1093         run_modifier_bindings(seat, seat->keyboard->modifiers.mods_depressed,
1094                               mods_depressed);
1095
1096         seat->keyboard->modifiers.mods_depressed = mods_depressed;
1097         seat->keyboard->modifiers.mods_latched = mods_latched;
1098         seat->keyboard->modifiers.mods_locked = mods_locked;
1099         seat->keyboard->modifiers.group = group;
1100
1101         /* And update the modifier_state for bindings. */
1102         mods_lookup = mods_depressed | mods_latched;
1103         seat->modifier_state = 0;
1104         if (mods_lookup & (1 << keyboard->xkb_info->ctrl_mod))
1105                 seat->modifier_state |= MODIFIER_CTRL;
1106         if (mods_lookup & (1 << keyboard->xkb_info->alt_mod))
1107                 seat->modifier_state |= MODIFIER_ALT;
1108         if (mods_lookup & (1 << keyboard->xkb_info->super_mod))
1109                 seat->modifier_state |= MODIFIER_SUPER;
1110         if (mods_lookup & (1 << keyboard->xkb_info->shift_mod))
1111                 seat->modifier_state |= MODIFIER_SHIFT;
1112
1113         /* Finally, notify the compositor that LEDs have changed. */
1114         if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
1115                                           keyboard->xkb_info->num_led))
1116                 leds |= LED_NUM_LOCK;
1117         if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
1118                                           keyboard->xkb_info->caps_led))
1119                 leds |= LED_CAPS_LOCK;
1120         if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
1121                                           keyboard->xkb_info->scroll_led))
1122                 leds |= LED_SCROLL_LOCK;
1123         if (leds != keyboard->xkb_state.leds && seat->led_update)
1124                 seat->led_update(seat, leds);
1125         keyboard->xkb_state.leds = leds;
1126
1127         if (changed) {
1128                 grab->interface->modifiers(grab,
1129                                            serial,
1130                                            keyboard->modifiers.mods_depressed,
1131                                            keyboard->modifiers.mods_latched,
1132                                            keyboard->modifiers.mods_locked,
1133                                            keyboard->modifiers.group);
1134         }
1135 }
1136
1137 static void
1138 update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
1139                       enum wl_keyboard_key_state state)
1140 {
1141         struct weston_keyboard *keyboard = seat->keyboard;
1142         enum xkb_key_direction direction;
1143
1144         /* Keyboard modifiers don't exist in raw keyboard mode */
1145         if (!seat->compositor->use_xkbcommon)
1146                 return;
1147
1148         if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1149                 direction = XKB_KEY_DOWN;
1150         else
1151                 direction = XKB_KEY_UP;
1152
1153         /* Offset the keycode by 8, as the evdev XKB rules reflect X's
1154          * broken keycode system, which starts at 8. */
1155         xkb_state_update_key(keyboard->xkb_state.state, key + 8, direction);
1156
1157         notify_modifiers(seat, serial);
1158 }
1159
1160 static void
1161 send_keymap(struct wl_resource *resource, struct weston_xkb_info *xkb_info)
1162 {
1163         wl_keyboard_send_keymap(resource,
1164                                 WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
1165                                 xkb_info->keymap_fd,
1166                                 xkb_info->keymap_size);
1167 }
1168
1169 static void
1170 send_modifiers(struct wl_resource *resource, uint32_t serial, struct weston_keyboard *keyboard)
1171 {
1172         wl_keyboard_send_modifiers(resource, serial,
1173                                    keyboard->modifiers.mods_depressed,
1174                                    keyboard->modifiers.mods_latched,
1175                                    keyboard->modifiers.mods_locked,
1176                                    keyboard->modifiers.group);
1177 }
1178
1179 static struct weston_xkb_info *
1180 weston_xkb_info_create(struct xkb_keymap *keymap);
1181
1182 static void
1183 update_keymap(struct weston_seat *seat)
1184 {
1185         struct weston_keyboard *keyboard = seat->keyboard;
1186         struct wl_resource *resource;
1187         struct weston_xkb_info *xkb_info;
1188         struct xkb_state *state;
1189         xkb_mod_mask_t latched_mods;
1190         xkb_mod_mask_t locked_mods;
1191
1192         xkb_info = weston_xkb_info_create(keyboard->pending_keymap);
1193
1194         xkb_keymap_unref(keyboard->pending_keymap);
1195         keyboard->pending_keymap = NULL;
1196
1197         if (!xkb_info) {
1198                 weston_log("failed to create XKB info\n");
1199                 return;
1200         }
1201
1202         state = xkb_state_new(xkb_info->keymap);
1203         if (!state) {
1204                 weston_log("failed to initialise XKB state\n");
1205                 weston_xkb_info_destroy(xkb_info);
1206                 return;
1207         }
1208
1209         latched_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
1210                                                 XKB_STATE_MODS_LATCHED);
1211         locked_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
1212                                                XKB_STATE_MODS_LOCKED);
1213         xkb_state_update_mask(state,
1214                               0, /* depressed */
1215                               latched_mods,
1216                               locked_mods,
1217                               0, 0, 0);
1218
1219         weston_xkb_info_destroy(keyboard->xkb_info);
1220         keyboard->xkb_info = xkb_info;
1221
1222         xkb_state_unref(keyboard->xkb_state.state);
1223         keyboard->xkb_state.state = state;
1224
1225         wl_resource_for_each(resource, &seat->keyboard->resource_list)
1226                 send_keymap(resource, xkb_info);
1227         wl_resource_for_each(resource, &seat->keyboard->focus_resource_list)
1228                 send_keymap(resource, xkb_info);
1229
1230         notify_modifiers(seat, wl_display_next_serial(seat->compositor->wl_display));
1231
1232         if (!latched_mods && !locked_mods)
1233                 return;
1234
1235         wl_resource_for_each(resource, &seat->keyboard->resource_list)
1236                 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), seat->keyboard);
1237         wl_resource_for_each(resource, &seat->keyboard->focus_resource_list)
1238                 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), seat->keyboard);
1239 }
1240 #else
1241 WL_EXPORT void
1242 notify_modifiers(struct weston_seat *seat, uint32_t serial)
1243 {
1244 }
1245
1246 static void
1247 update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
1248                       enum wl_keyboard_key_state state)
1249 {
1250 }
1251
1252 static void
1253 update_keymap(struct weston_seat *seat)
1254 {
1255 }
1256 #endif
1257
1258 WL_EXPORT void
1259 notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
1260            enum wl_keyboard_key_state state,
1261            enum weston_key_state_update update_state)
1262 {
1263         struct weston_compositor *compositor = seat->compositor;
1264         struct weston_keyboard *keyboard = seat->keyboard;
1265         struct weston_keyboard_grab *grab = keyboard->grab;
1266         uint32_t *k, *end;
1267
1268         if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1269                 weston_compositor_idle_inhibit(compositor);
1270                 keyboard->grab_key = key;
1271                 keyboard->grab_time = time;
1272         } else {
1273                 weston_compositor_idle_release(compositor);
1274         }
1275
1276         end = keyboard->keys.data + keyboard->keys.size;
1277         for (k = keyboard->keys.data; k < end; k++) {
1278                 if (*k == key) {
1279                         /* Ignore server-generated repeats. */
1280                         if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1281                                 return;
1282                         *k = *--end;
1283                 }
1284         }
1285         keyboard->keys.size = (void *) end - keyboard->keys.data;
1286         if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1287                 k = wl_array_add(&keyboard->keys, sizeof *k);
1288                 *k = key;
1289         }
1290
1291         if (grab == &keyboard->default_grab ||
1292             grab == &keyboard->input_method_grab) {
1293                 weston_compositor_run_key_binding(compositor, seat, time, key,
1294                                                   state);
1295                 grab = keyboard->grab;
1296         }
1297
1298         grab->interface->key(grab, time, key, state);
1299
1300         if (keyboard->pending_keymap &&
1301             keyboard->keys.size == 0)
1302                 update_keymap(seat);
1303
1304         if (update_state == STATE_UPDATE_AUTOMATIC) {
1305                 update_modifier_state(seat,
1306                                       wl_display_get_serial(compositor->wl_display),
1307                                       key,
1308                                       state);
1309         }
1310 }
1311
1312 WL_EXPORT void
1313 notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
1314                      wl_fixed_t x, wl_fixed_t y)
1315 {
1316         if (output) {
1317                 weston_pointer_move(seat->pointer, x, y);
1318         } else {
1319                 /* FIXME: We should call weston_pointer_set_focus(seat,
1320                  * NULL) here, but somehow that breaks re-entry... */
1321         }
1322 }
1323
1324 static void
1325 destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
1326 {
1327         struct weston_seat *ws;
1328
1329         ws = container_of(listener, struct weston_seat,
1330                           saved_kbd_focus_listener);
1331
1332         ws->saved_kbd_focus = NULL;
1333 }
1334
1335 WL_EXPORT void
1336 notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
1337                          enum weston_key_state_update update_state)
1338 {
1339         struct weston_compositor *compositor = seat->compositor;
1340         struct weston_keyboard *keyboard = seat->keyboard;
1341         struct weston_surface *surface;
1342         uint32_t *k, serial;
1343
1344         serial = wl_display_next_serial(compositor->wl_display);
1345         wl_array_copy(&keyboard->keys, keys);
1346         wl_array_for_each(k, &keyboard->keys) {
1347                 weston_compositor_idle_inhibit(compositor);
1348                 if (update_state == STATE_UPDATE_AUTOMATIC)
1349                         update_modifier_state(seat, serial, *k,
1350                                               WL_KEYBOARD_KEY_STATE_PRESSED);
1351         }
1352
1353         /* Run key bindings after we've updated the state. */
1354         wl_array_for_each(k, &keyboard->keys) {
1355                 weston_compositor_run_key_binding(compositor, seat, 0, *k,
1356                                                   WL_KEYBOARD_KEY_STATE_PRESSED);
1357         }
1358
1359         surface = seat->saved_kbd_focus;
1360
1361         if (surface) {
1362                 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1363                 weston_keyboard_set_focus(keyboard, surface);
1364                 seat->saved_kbd_focus = NULL;
1365         }
1366 }
1367
1368 WL_EXPORT void
1369 notify_keyboard_focus_out(struct weston_seat *seat)
1370 {
1371         struct weston_compositor *compositor = seat->compositor;
1372         struct weston_keyboard *keyboard = seat->keyboard;
1373         uint32_t *k, serial;
1374
1375         serial = wl_display_next_serial(compositor->wl_display);
1376         wl_array_for_each(k, &keyboard->keys) {
1377                 weston_compositor_idle_release(compositor);
1378                 update_modifier_state(seat, serial, *k,
1379                                       WL_KEYBOARD_KEY_STATE_RELEASED);
1380         }
1381
1382         seat->modifier_state = 0;
1383
1384         if (keyboard->focus) {
1385                 seat->saved_kbd_focus = keyboard->focus;
1386                 seat->saved_kbd_focus_listener.notify =
1387                         destroy_device_saved_kbd_focus;
1388                 wl_signal_add(&keyboard->focus->destroy_signal,
1389                               &seat->saved_kbd_focus_listener);
1390         }
1391
1392         weston_keyboard_set_focus(keyboard, NULL);
1393         weston_keyboard_cancel_grab(keyboard);
1394         if (seat->pointer)
1395                 weston_pointer_cancel_grab(seat->pointer);
1396 }
1397
1398 WL_EXPORT void
1399 weston_touch_set_focus(struct weston_seat *seat, struct weston_view *view)
1400 {
1401         struct wl_list *focus_resource_list;
1402
1403         focus_resource_list = &seat->touch->focus_resource_list;
1404
1405         if (view && seat->touch->focus &&
1406             seat->touch->focus->surface == view->surface) {
1407                 seat->touch->focus = view;
1408                 return;
1409         }
1410
1411         wl_list_remove(&seat->touch->focus_resource_listener.link);
1412         wl_list_init(&seat->touch->focus_resource_listener.link);
1413         wl_list_remove(&seat->touch->focus_view_listener.link);
1414         wl_list_init(&seat->touch->focus_view_listener.link);
1415
1416         if (!wl_list_empty(focus_resource_list)) {
1417                 move_resources(&seat->touch->resource_list,
1418                                focus_resource_list);
1419         }
1420
1421         if (view) {
1422                 struct wl_client *surface_client =
1423                         wl_resource_get_client(view->surface->resource);
1424                 move_resources_for_client(focus_resource_list,
1425                                           &seat->touch->resource_list,
1426                                           surface_client);
1427                 wl_resource_add_destroy_listener(view->surface->resource,
1428                                                  &seat->touch->focus_resource_listener);
1429                 wl_signal_add(&view->destroy_signal, &seat->touch->focus_view_listener);
1430         }
1431         seat->touch->focus = view;
1432 }
1433
1434 /**
1435  * notify_touch - emulates button touches and notifies surfaces accordingly.
1436  *
1437  * It assumes always the correct cycle sequence until it gets here: touch_down
1438  * → touch_update → ... → touch_update → touch_end. The driver is responsible
1439  * for sending along such order.
1440  *
1441  */
1442 WL_EXPORT void
1443 notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
1444              wl_fixed_t x, wl_fixed_t y, int touch_type)
1445 {
1446         struct weston_compositor *ec = seat->compositor;
1447         struct weston_touch *touch = seat->touch;
1448         struct weston_touch_grab *grab = touch->grab;
1449         struct weston_view *ev;
1450         wl_fixed_t sx, sy;
1451
1452         /* Update grab's global coordinates. */
1453         if (touch_id == touch->grab_touch_id && touch_type != WL_TOUCH_UP) {
1454                 touch->grab_x = x;
1455                 touch->grab_y = y;
1456         }
1457
1458         switch (touch_type) {
1459         case WL_TOUCH_DOWN:
1460                 weston_compositor_idle_inhibit(ec);
1461
1462                 touch->num_tp++;
1463
1464                 /* the first finger down picks the view, and all further go
1465                  * to that view for the remainder of the touch session i.e.
1466                  * until all touch points are up again. */
1467                 if (touch->num_tp == 1) {
1468                         ev = weston_compositor_pick_view(ec, x, y, &sx, &sy);
1469                         weston_touch_set_focus(seat, ev);
1470                 } else if (touch->focus) {
1471                         ev = touch->focus;
1472                         weston_view_from_global_fixed(ev, x, y, &sx, &sy);
1473                 } else {
1474                         /* Unexpected condition: We have non-initial touch but
1475                          * there is no focused surface.
1476                          */
1477                         weston_log("touch event received with %d points down"
1478                                    "but no surface focused\n", touch->num_tp);
1479                         return;
1480                 }
1481
1482                 weston_compositor_run_touch_binding(ec, seat,
1483                                                     time, touch_type);
1484
1485                 grab->interface->down(grab, time, touch_id, sx, sy);
1486                 if (touch->num_tp == 1) {
1487                         touch->grab_serial =
1488                                 wl_display_get_serial(ec->wl_display);
1489                         touch->grab_touch_id = touch_id;
1490                         touch->grab_time = time;
1491                         touch->grab_x = x;
1492                         touch->grab_y = y;
1493                 }
1494
1495                 break;
1496         case WL_TOUCH_MOTION:
1497                 ev = touch->focus;
1498                 if (!ev)
1499                         break;
1500
1501                 weston_view_from_global_fixed(ev, x, y, &sx, &sy);
1502                 grab->interface->motion(grab, time, touch_id, sx, sy);
1503                 break;
1504         case WL_TOUCH_UP:
1505                 if (touch->num_tp == 0) {
1506                         /* This can happen if we start out with one or
1507                          * more fingers on the touch screen, in which
1508                          * case we didn't get the corresponding down
1509                          * event. */
1510                         weston_log("unmatched touch up event\n");
1511                         break;
1512                 }
1513                 weston_compositor_idle_release(ec);
1514                 touch->num_tp--;
1515
1516                 grab->interface->up(grab, time, touch_id);
1517                 if (touch->num_tp == 0)
1518                         weston_touch_set_focus(seat, NULL);
1519                 break;
1520         }
1521 }
1522
1523 WL_EXPORT void
1524 notify_touch_frame(struct weston_seat *seat)
1525 {
1526         struct weston_touch *touch = seat->touch;
1527         struct weston_touch_grab *grab = touch->grab;
1528
1529         grab->interface->frame(grab);
1530 }
1531
1532 static void
1533 pointer_cursor_surface_configure(struct weston_surface *es,
1534                                  int32_t dx, int32_t dy)
1535 {
1536         struct weston_pointer *pointer = es->configure_private;
1537         int x, y;
1538
1539         if (es->width == 0)
1540                 return;
1541
1542         assert(es == pointer->sprite->surface);
1543
1544         pointer->hotspot_x -= dx;
1545         pointer->hotspot_y -= dy;
1546
1547         x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
1548         y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
1549
1550         weston_view_set_position(pointer->sprite, x, y);
1551
1552         empty_region(&es->pending.input);
1553         empty_region(&es->input);
1554
1555         if (!weston_surface_is_mapped(es)) {
1556                 weston_layer_entry_insert(&es->compositor->cursor_layer.view_list,
1557                                           &pointer->sprite->layer_link);
1558                 weston_view_update_transform(pointer->sprite);
1559         }
1560 }
1561
1562 static void
1563 pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
1564                    uint32_t serial, struct wl_resource *surface_resource,
1565                    int32_t x, int32_t y)
1566 {
1567         struct weston_pointer *pointer = wl_resource_get_user_data(resource);
1568         struct weston_surface *surface = NULL;
1569
1570         if (surface_resource)
1571                 surface = wl_resource_get_user_data(surface_resource);
1572
1573         if (pointer->focus == NULL)
1574                 return;
1575         /* pointer->focus->surface->resource can be NULL. Surfaces like the
1576         black_surface used in shell.c for fullscreen don't have
1577         a resource, but can still have focus */
1578         if (pointer->focus->surface->resource == NULL)
1579                 return;
1580         if (wl_resource_get_client(pointer->focus->surface->resource) != client)
1581                 return;
1582         if (pointer->focus_serial - serial > UINT32_MAX / 2)
1583                 return;
1584
1585         if (surface && pointer->sprite && surface != pointer->sprite->surface) {
1586                 if (surface->configure) {
1587                         wl_resource_post_error(surface->resource,
1588                                                WL_DISPLAY_ERROR_INVALID_OBJECT,
1589                                                "surface->configure already "
1590                                                "set");
1591                         return;
1592                 }
1593         }
1594
1595         if (pointer->sprite)
1596                 pointer_unmap_sprite(pointer);
1597
1598         if (!surface)
1599                 return;
1600
1601         wl_signal_add(&surface->destroy_signal,
1602                       &pointer->sprite_destroy_listener);
1603
1604         surface->configure = pointer_cursor_surface_configure;
1605         surface->configure_private = pointer;
1606         pointer->sprite = weston_view_create(surface);
1607         pointer->hotspot_x = x;
1608         pointer->hotspot_y = y;
1609
1610         if (surface->buffer_ref.buffer)
1611                 pointer_cursor_surface_configure(surface, 0, 0);
1612 }
1613
1614 static void
1615 pointer_release(struct wl_client *client, struct wl_resource *resource)
1616 {
1617         wl_resource_destroy(resource);
1618 }
1619
1620 static const struct wl_pointer_interface pointer_interface = {
1621         pointer_set_cursor,
1622         pointer_release
1623 };
1624
1625 static void
1626 seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
1627                  uint32_t id)
1628 {
1629         struct weston_seat *seat = wl_resource_get_user_data(resource);
1630         struct wl_resource *cr;
1631
1632         if (!seat->pointer)
1633                 return;
1634
1635         cr = wl_resource_create(client, &wl_pointer_interface,
1636                                 wl_resource_get_version(resource), id);
1637         if (cr == NULL) {
1638                 wl_client_post_no_memory(client);
1639                 return;
1640         }
1641
1642         /* May be moved to focused list later by either
1643          * weston_pointer_set_focus or directly if this client is already
1644          * focused */
1645         wl_list_insert(&seat->pointer->resource_list, wl_resource_get_link(cr));
1646         wl_resource_set_implementation(cr, &pointer_interface, seat->pointer,
1647                                        unbind_resource);
1648
1649         if (seat->pointer->focus && seat->pointer->focus->surface->resource &&
1650             wl_resource_get_client(seat->pointer->focus->surface->resource) == client) {
1651                 wl_fixed_t sx, sy;
1652
1653                 weston_view_from_global_fixed(seat->pointer->focus,
1654                                               seat->pointer->x,
1655                                               seat->pointer->y,
1656                                               &sx, &sy);
1657
1658                 wl_list_remove(wl_resource_get_link(cr));
1659                 wl_list_insert(&seat->pointer->focus_resource_list,
1660                                wl_resource_get_link(cr));
1661                 wl_pointer_send_enter(cr,
1662                                       seat->pointer->focus_serial,
1663                                       seat->pointer->focus->surface->resource,
1664                                       sx, sy);
1665         }
1666 }
1667
1668 static void
1669 keyboard_release(struct wl_client *client, struct wl_resource *resource)
1670 {
1671         wl_resource_destroy(resource);
1672 }
1673
1674 static const struct wl_keyboard_interface keyboard_interface = {
1675         keyboard_release
1676 };
1677
1678 static int
1679 should_send_modifiers_to_client(struct weston_seat *seat,
1680                                 struct wl_client *client)
1681 {
1682         if (seat->keyboard &&
1683             seat->keyboard->focus &&
1684             seat->keyboard->focus->resource &&
1685             wl_resource_get_client(seat->keyboard->focus->resource) == client)
1686                 return 1;
1687
1688         if (seat->pointer &&
1689             seat->pointer->focus &&
1690             seat->pointer->focus->surface->resource &&
1691             wl_resource_get_client(seat->pointer->focus->surface->resource) == client)
1692                 return 1;
1693
1694         return 0;
1695 }
1696
1697 static void
1698 seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
1699                   uint32_t id)
1700 {
1701         struct weston_seat *seat = wl_resource_get_user_data(resource);
1702         struct weston_keyboard *keyboard = seat->keyboard;
1703         struct wl_resource *cr;
1704
1705         if (!seat->keyboard)
1706                 return;
1707
1708         cr = wl_resource_create(client, &wl_keyboard_interface,
1709                                 wl_resource_get_version(resource), id);
1710         if (cr == NULL) {
1711                 wl_client_post_no_memory(client);
1712                 return;
1713         }
1714
1715         /* May be moved to focused list later by either
1716          * weston_keyboard_set_focus or directly if this client is already
1717          * focused */
1718         wl_list_insert(&seat->keyboard->resource_list, wl_resource_get_link(cr));
1719         wl_resource_set_implementation(cr, &keyboard_interface,
1720                                        seat, unbind_resource);
1721
1722         if (wl_resource_get_version(cr) >= WL_KEYBOARD_REPEAT_INFO_SINCE_VERSION) {
1723                 wl_keyboard_send_repeat_info(cr,
1724                                              seat->compositor->kb_repeat_rate,
1725                                              seat->compositor->kb_repeat_delay);
1726         }
1727
1728         if (seat->compositor->use_xkbcommon) {
1729                 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
1730                                         keyboard->xkb_info->keymap_fd,
1731                                         keyboard->xkb_info->keymap_size);
1732         } else {
1733                 int null_fd = open("/dev/null", O_RDONLY);
1734                 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP,
1735                                         null_fd,
1736                                         0);
1737                 close(null_fd);
1738         }
1739
1740         if (should_send_modifiers_to_client(seat, client)) {
1741                 send_modifiers_to_resource(seat->keyboard,
1742                                            cr,
1743                                            seat->keyboard->focus_serial);
1744         }
1745
1746         if (seat->keyboard->focus &&
1747             wl_resource_get_client(seat->keyboard->focus->resource) == client) {
1748                 struct weston_surface *surface =
1749                         (struct weston_surface *) seat->keyboard->focus;
1750
1751                 wl_list_remove(wl_resource_get_link(cr));
1752                 wl_list_insert(&seat->keyboard->focus_resource_list,
1753                                wl_resource_get_link(cr));
1754                 wl_keyboard_send_enter(cr,
1755                                        seat->keyboard->focus_serial,
1756                                        surface->resource,
1757                                        &seat->keyboard->keys);
1758
1759                 /* If this is the first keyboard resource for this
1760                  * client... */
1761                 if (seat->keyboard->focus_resource_list.prev ==
1762                     wl_resource_get_link(cr))
1763                         wl_data_device_set_keyboard_focus(seat);
1764         }
1765 }
1766
1767 static void
1768 touch_release(struct wl_client *client, struct wl_resource *resource)
1769 {
1770         wl_resource_destroy(resource);
1771 }
1772
1773 static const struct wl_touch_interface touch_interface = {
1774         touch_release
1775 };
1776
1777 static void
1778 seat_get_touch(struct wl_client *client, struct wl_resource *resource,
1779                uint32_t id)
1780 {
1781         struct weston_seat *seat = wl_resource_get_user_data(resource);
1782         struct wl_resource *cr;
1783
1784         if (!seat->touch)
1785                 return;
1786
1787         cr = wl_resource_create(client, &wl_touch_interface,
1788                                 wl_resource_get_version(resource), id);
1789         if (cr == NULL) {
1790                 wl_client_post_no_memory(client);
1791                 return;
1792         }
1793
1794         if (seat->touch->focus &&
1795             wl_resource_get_client(seat->touch->focus->surface->resource) == client) {
1796                 wl_list_insert(&seat->touch->resource_list,
1797                                wl_resource_get_link(cr));
1798         } else {
1799                 wl_list_insert(&seat->touch->focus_resource_list,
1800                                wl_resource_get_link(cr));
1801         }
1802         wl_resource_set_implementation(cr, &touch_interface,
1803                                        seat, unbind_resource);
1804 }
1805
1806 static const struct wl_seat_interface seat_interface = {
1807         seat_get_pointer,
1808         seat_get_keyboard,
1809         seat_get_touch,
1810 };
1811
1812 static void
1813 bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1814 {
1815         struct weston_seat *seat = data;
1816         struct wl_resource *resource;
1817         enum wl_seat_capability caps = 0;
1818
1819         resource = wl_resource_create(client,
1820                                       &wl_seat_interface, MIN(version, 4), id);
1821         wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
1822         wl_resource_set_implementation(resource, &seat_interface, data,
1823                                        unbind_resource);
1824
1825         if (seat->pointer)
1826                 caps |= WL_SEAT_CAPABILITY_POINTER;
1827         if (seat->keyboard)
1828                 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
1829         if (seat->touch)
1830                 caps |= WL_SEAT_CAPABILITY_TOUCH;
1831
1832         wl_seat_send_capabilities(resource, caps);
1833         if (version >= WL_SEAT_NAME_SINCE_VERSION)
1834                 wl_seat_send_name(resource, seat->seat_name);
1835 }
1836
1837 #ifdef ENABLE_XKBCOMMON
1838 int
1839 weston_compositor_xkb_init(struct weston_compositor *ec,
1840                            struct xkb_rule_names *names)
1841 {
1842         ec->use_xkbcommon = 1;
1843
1844         if (ec->xkb_context == NULL) {
1845                 ec->xkb_context = xkb_context_new(0);
1846                 if (ec->xkb_context == NULL) {
1847                         weston_log("failed to create XKB context\n");
1848                         return -1;
1849                 }
1850         }
1851
1852         if (names)
1853                 ec->xkb_names = *names;
1854         if (!ec->xkb_names.rules)
1855                 ec->xkb_names.rules = strdup("evdev");
1856         if (!ec->xkb_names.model)
1857                 ec->xkb_names.model = strdup("pc105");
1858         if (!ec->xkb_names.layout)
1859                 ec->xkb_names.layout = strdup("us");
1860
1861         return 0;
1862 }
1863
1864 static void
1865 weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
1866 {
1867         if (--xkb_info->ref_count > 0)
1868                 return;
1869
1870         xkb_keymap_unref(xkb_info->keymap);
1871
1872         if (xkb_info->keymap_area)
1873                 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
1874         if (xkb_info->keymap_fd >= 0)
1875                 close(xkb_info->keymap_fd);
1876         free(xkb_info);
1877 }
1878
1879 void
1880 weston_compositor_xkb_destroy(struct weston_compositor *ec)
1881 {
1882         /*
1883          * If we're operating in raw keyboard mode, we never initialized
1884          * libxkbcommon so there's no cleanup to do either.
1885          */
1886         if (!ec->use_xkbcommon)
1887                 return;
1888
1889         free((char *) ec->xkb_names.rules);
1890         free((char *) ec->xkb_names.model);
1891         free((char *) ec->xkb_names.layout);
1892         free((char *) ec->xkb_names.variant);
1893         free((char *) ec->xkb_names.options);
1894
1895         if (ec->xkb_info)
1896                 weston_xkb_info_destroy(ec->xkb_info);
1897         xkb_context_unref(ec->xkb_context);
1898 }
1899
1900 static struct weston_xkb_info *
1901 weston_xkb_info_create(struct xkb_keymap *keymap)
1902 {
1903         struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
1904         if (xkb_info == NULL)
1905                 return NULL;
1906
1907         xkb_info->keymap = xkb_keymap_ref(keymap);
1908         xkb_info->ref_count = 1;
1909
1910         char *keymap_str;
1911
1912         xkb_info->shift_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
1913                                                        XKB_MOD_NAME_SHIFT);
1914         xkb_info->caps_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
1915                                                       XKB_MOD_NAME_CAPS);
1916         xkb_info->ctrl_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
1917                                                       XKB_MOD_NAME_CTRL);
1918         xkb_info->alt_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
1919                                                      XKB_MOD_NAME_ALT);
1920         xkb_info->mod2_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
1921                                                       "Mod2");
1922         xkb_info->mod3_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
1923                                                       "Mod3");
1924         xkb_info->super_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
1925                                                        XKB_MOD_NAME_LOGO);
1926         xkb_info->mod5_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
1927                                                       "Mod5");
1928
1929         xkb_info->num_led = xkb_keymap_led_get_index(xkb_info->keymap,
1930                                                      XKB_LED_NAME_NUM);
1931         xkb_info->caps_led = xkb_keymap_led_get_index(xkb_info->keymap,
1932                                                       XKB_LED_NAME_CAPS);
1933         xkb_info->scroll_led = xkb_keymap_led_get_index(xkb_info->keymap,
1934                                                         XKB_LED_NAME_SCROLL);
1935
1936         keymap_str = xkb_keymap_get_as_string(xkb_info->keymap,
1937                                               XKB_KEYMAP_FORMAT_TEXT_V1);
1938         if (keymap_str == NULL) {
1939                 weston_log("failed to get string version of keymap\n");
1940                 goto err_keymap;
1941         }
1942         xkb_info->keymap_size = strlen(keymap_str) + 1;
1943
1944         xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
1945         if (xkb_info->keymap_fd < 0) {
1946                 weston_log("creating a keymap file for %lu bytes failed: %m\n",
1947                         (unsigned long) xkb_info->keymap_size);
1948                 goto err_keymap_str;
1949         }
1950
1951         xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
1952                                      PROT_READ | PROT_WRITE,
1953                                      MAP_SHARED, xkb_info->keymap_fd, 0);
1954         if (xkb_info->keymap_area == MAP_FAILED) {
1955                 weston_log("failed to mmap() %lu bytes\n",
1956                         (unsigned long) xkb_info->keymap_size);
1957                 goto err_dev_zero;
1958         }
1959         strcpy(xkb_info->keymap_area, keymap_str);
1960         free(keymap_str);
1961
1962         return xkb_info;
1963
1964 err_dev_zero:
1965         close(xkb_info->keymap_fd);
1966 err_keymap_str:
1967         free(keymap_str);
1968 err_keymap:
1969         xkb_keymap_unref(xkb_info->keymap);
1970         free(xkb_info);
1971         return NULL;
1972 }
1973
1974 static int
1975 weston_compositor_build_global_keymap(struct weston_compositor *ec)
1976 {
1977         struct xkb_keymap *keymap;
1978
1979         if (ec->xkb_info != NULL)
1980                 return 0;
1981
1982         keymap = xkb_keymap_new_from_names(ec->xkb_context,
1983                                            &ec->xkb_names,
1984                                            0);
1985         if (keymap == NULL) {
1986                 weston_log("failed to compile global XKB keymap\n");
1987                 weston_log("  tried rules %s, model %s, layout %s, variant %s, "
1988                         "options %s\n",
1989                         ec->xkb_names.rules, ec->xkb_names.model,
1990                         ec->xkb_names.layout, ec->xkb_names.variant,
1991                         ec->xkb_names.options);
1992                 return -1;
1993         }
1994
1995         ec->xkb_info = weston_xkb_info_create(keymap);
1996         xkb_keymap_unref(keymap);
1997         if (ec->xkb_info == NULL)
1998                 return -1;
1999
2000         return 0;
2001 }
2002 #else
2003 int
2004 weston_compositor_xkb_init(struct weston_compositor *ec,
2005                            struct xkb_rule_names *names)
2006 {
2007         return 0;
2008 }
2009
2010 void
2011 weston_compositor_xkb_destroy(struct weston_compositor *ec)
2012 {
2013 }
2014 #endif
2015
2016 WL_EXPORT void
2017 weston_seat_update_keymap(struct weston_seat *seat, struct xkb_keymap *keymap)
2018 {
2019         if (!seat->keyboard || !keymap)
2020                 return;
2021
2022 #ifdef ENABLE_XKBCOMMON
2023         if (!seat->compositor->use_xkbcommon)
2024                 return;
2025
2026         xkb_keymap_unref(seat->keyboard->pending_keymap);
2027         seat->keyboard->pending_keymap = xkb_keymap_ref(keymap);
2028
2029         if (seat->keyboard->keys.size == 0)
2030                 update_keymap(seat);
2031 #endif
2032 }
2033
2034 WL_EXPORT int
2035 weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
2036 {
2037         struct weston_keyboard *keyboard;
2038
2039         if (seat->keyboard) {
2040                 seat->keyboard_device_count += 1;
2041                 if (seat->keyboard_device_count == 1)
2042                         seat_send_updated_caps(seat);
2043                 return 0;
2044         }
2045
2046         keyboard = weston_keyboard_create();
2047         if (keyboard == NULL) {
2048                 weston_log("failed to allocate weston keyboard struct\n");
2049                 return -1;
2050         }
2051
2052 #ifdef ENABLE_XKBCOMMON
2053         if (seat->compositor->use_xkbcommon) {
2054                 if (keymap != NULL) {
2055                         keyboard->xkb_info = weston_xkb_info_create(keymap);
2056                         if (keyboard->xkb_info == NULL)
2057                                 goto err;
2058                 } else {
2059                         if (weston_compositor_build_global_keymap(seat->compositor) < 0)
2060                                 goto err;
2061                         keyboard->xkb_info = seat->compositor->xkb_info;
2062                         keyboard->xkb_info->ref_count++;
2063                 }
2064
2065                 keyboard->xkb_state.state = xkb_state_new(keyboard->xkb_info->keymap);
2066                 if (keyboard->xkb_state.state == NULL) {
2067                         weston_log("failed to initialise XKB state\n");
2068                         goto err;
2069                 }
2070
2071                 keyboard->xkb_state.leds = 0;
2072         }
2073 #endif
2074
2075         seat->keyboard = keyboard;
2076         seat->keyboard_device_count = 1;
2077         keyboard->seat = seat;
2078
2079         seat_send_updated_caps(seat);
2080
2081         return 0;
2082
2083 err:
2084         if (keyboard->xkb_info)
2085                 weston_xkb_info_destroy(keyboard->xkb_info);
2086         free(keyboard);
2087
2088         return -1;
2089 }
2090
2091 static void
2092 weston_keyboard_reset_state(struct weston_keyboard *keyboard)
2093 {
2094         struct weston_seat *seat = keyboard->seat;
2095         struct xkb_state *state;
2096
2097 #ifdef ENABLE_XKBCOMMON
2098         if (seat->compositor->use_xkbcommon) {
2099                 state = xkb_state_new(keyboard->xkb_info->keymap);
2100                 if (!state) {
2101                         weston_log("failed to reset XKB state\n");
2102                         return;
2103                 }
2104                 xkb_state_unref(keyboard->xkb_state.state);
2105                 keyboard->xkb_state.state = state;
2106
2107                 keyboard->xkb_state.leds = 0;
2108         }
2109 #endif
2110
2111         seat->modifier_state = 0;
2112 }
2113
2114 WL_EXPORT void
2115 weston_seat_release_keyboard(struct weston_seat *seat)
2116 {
2117         seat->keyboard_device_count--;
2118         if (seat->keyboard_device_count == 0) {
2119                 weston_keyboard_set_focus(seat->keyboard, NULL);
2120                 weston_keyboard_cancel_grab(seat->keyboard);
2121                 weston_keyboard_reset_state(seat->keyboard);
2122                 seat_send_updated_caps(seat);
2123         }
2124 }
2125
2126 WL_EXPORT void
2127 weston_seat_init_pointer(struct weston_seat *seat)
2128 {
2129         struct weston_pointer *pointer;
2130
2131         if (seat->pointer) {
2132                 seat->pointer_device_count += 1;
2133                 if (seat->pointer_device_count == 1)
2134                         seat_send_updated_caps(seat);
2135                 return;
2136         }
2137
2138         pointer = weston_pointer_create(seat);
2139         if (pointer == NULL)
2140                 return;
2141
2142         seat->pointer = pointer;
2143         seat->pointer_device_count = 1;
2144         pointer->seat = seat;
2145
2146         seat_send_updated_caps(seat);
2147 }
2148
2149 WL_EXPORT void
2150 weston_seat_release_pointer(struct weston_seat *seat)
2151 {
2152         struct weston_pointer *pointer = seat->pointer;
2153
2154         seat->pointer_device_count--;
2155         if (seat->pointer_device_count == 0) {
2156                 weston_pointer_set_focus(pointer, NULL,
2157                                          wl_fixed_from_int(0),
2158                                          wl_fixed_from_int(0));
2159                 weston_pointer_cancel_grab(pointer);
2160
2161                 if (pointer->sprite)
2162                         pointer_unmap_sprite(pointer);
2163
2164                 weston_pointer_reset_state(pointer);
2165                 seat_send_updated_caps(seat);
2166         }
2167 }
2168
2169 WL_EXPORT void
2170 weston_seat_init_touch(struct weston_seat *seat)
2171 {
2172         struct weston_touch *touch;
2173
2174         if (seat->touch) {
2175                 seat->touch_device_count += 1;
2176                 if (seat->touch_device_count == 1)
2177                         seat_send_updated_caps(seat);
2178                 return;
2179         }
2180
2181         touch = weston_touch_create();
2182         if (touch == NULL)
2183                 return;
2184
2185         seat->touch = touch;
2186         seat->touch_device_count = 1;
2187         touch->seat = seat;
2188
2189         seat_send_updated_caps(seat);
2190 }
2191
2192 WL_EXPORT void
2193 weston_seat_release_touch(struct weston_seat *seat)
2194 {
2195         seat->touch_device_count--;
2196         if (seat->touch_device_count == 0) {
2197                 weston_touch_set_focus(seat, NULL);
2198                 weston_touch_cancel_grab(seat->touch);
2199                 weston_touch_reset_state(seat->touch);
2200                 seat_send_updated_caps(seat);
2201         }
2202 }
2203
2204 WL_EXPORT void
2205 weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
2206                  const char *seat_name)
2207 {
2208         memset(seat, 0, sizeof *seat);
2209
2210         seat->selection_data_source = NULL;
2211         wl_list_init(&seat->base_resource_list);
2212         wl_signal_init(&seat->selection_signal);
2213         wl_list_init(&seat->drag_resource_list);
2214         wl_signal_init(&seat->destroy_signal);
2215         wl_signal_init(&seat->updated_caps_signal);
2216
2217         seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 4,
2218                                         seat, bind_seat);
2219
2220         seat->compositor = ec;
2221         seat->modifier_state = 0;
2222         seat->seat_name = strdup(seat_name);
2223
2224         wl_list_insert(ec->seat_list.prev, &seat->link);
2225
2226         clipboard_create(seat);
2227
2228         wl_signal_emit(&ec->seat_created_signal, seat);
2229 }
2230
2231 WL_EXPORT void
2232 weston_seat_release(struct weston_seat *seat)
2233 {
2234         wl_list_remove(&seat->link);
2235
2236         if (seat->saved_kbd_focus)
2237                 wl_list_remove(&seat->saved_kbd_focus_listener.link);
2238
2239         if (seat->pointer)
2240                 weston_pointer_destroy(seat->pointer);
2241         if (seat->keyboard)
2242                 weston_keyboard_destroy(seat->keyboard);
2243         if (seat->touch)
2244                 weston_touch_destroy(seat->touch);
2245
2246         free (seat->seat_name);
2247
2248         wl_global_destroy(seat->global);
2249
2250         wl_signal_emit(&seat->destroy_signal, seat);
2251 }