downstream: terminal: destroy display at the end.
[profile/ivi/weston-ivi-shell.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, *closest = NULL;
844         int x, y, old_x, old_y;
845         int distance, min = INT_MAX;
846         int has_output = 0;
847
848         /* check if any output is attached to the pointer's seat or not */
849         wl_list_for_each(output, &ec->output_list, link) {
850                 if (pointer->seat == output->seat_data.seat) {
851                         has_output = 1;
852                         break;
853                 }
854         }
855
856         x = wl_fixed_to_int(*fx);
857         y = wl_fixed_to_int(*fy);
858         old_x = wl_fixed_to_int(pointer->x);
859         old_y = wl_fixed_to_int(pointer->y);
860
861         wl_list_for_each(output, &ec->output_list, link) {
862                 if (has_output && pointer->seat != output->seat_data.seat)
863                         continue;
864                 if (pixman_region32_contains_point(&output->region,
865                                                    x, y, NULL))
866                         return;
867                 if (pixman_region32_contains_point(&output->region,
868                                                    old_x, old_y, NULL))
869                         prev = output;
870                 distance = abs(output->x + output->width / 2 - x) +
871                            abs(output->y + output->height / 2 - y);
872                 if (distance < min) {
873                         min = distance;
874                         closest = output;
875                 }
876         }
877
878         if (!prev)
879                 prev = closest;
880
881         if (prev)
882                 weston_pointer_clamp_for_output(pointer, prev, fx, fy);
883 }
884
885 /* Takes absolute values */
886 WL_EXPORT void
887 weston_pointer_move(struct weston_pointer *pointer, wl_fixed_t x, wl_fixed_t y)
888 {
889         int32_t ix, iy;
890
891         weston_pointer_clamp (pointer, &x, &y);
892
893         pointer->x = x;
894         pointer->y = y;
895
896         ix = wl_fixed_to_int(x);
897         iy = wl_fixed_to_int(y);
898
899         if (pointer->sprite) {
900                 weston_view_set_position(pointer->sprite,
901                                          ix - pointer->hotspot_x,
902                                          iy - pointer->hotspot_y);
903                 weston_view_schedule_repaint(pointer->sprite);
904         }
905
906         pointer->grab->interface->focus(pointer->grab);
907         wl_signal_emit(&pointer->motion_signal, pointer);
908 }
909
910 /** Verify if the pointer is in a valid position and move it if it isn't.
911  */
912 static void
913 weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data)
914 {
915         struct weston_pointer *pointer;
916         struct weston_compositor *ec;
917         struct weston_output *output, *closest = NULL;
918         int x, y, distance, min = INT_MAX;
919         wl_fixed_t fx, fy;
920         pointer = container_of(listener, struct weston_pointer,
921                                output_destroy_listener);
922         ec = pointer->seat->compositor;
923
924         x = wl_fixed_to_int(pointer->x);
925         y = wl_fixed_to_int(pointer->y);
926
927         wl_list_for_each(output, &ec->output_list, link) {
928                 if (pixman_region32_contains_point(&output->region,
929                                                    x, y, NULL))
930                         return;
931                 /* Approximate the distance from the pointer to the centre of
932                  * the output. */
933                 distance = abs(output->x + output->width / 2 - x) +
934                            abs(output->y + output->height / 2 - y);
935                 if (distance < min) {
936                         min = distance;
937                         closest = output;
938                 }
939         }
940
941         /* Nothing to do if there's no output left. */
942         if (!closest)
943                 return;
944
945         fx = pointer->x;
946         fy = pointer->y;
947
948         weston_pointer_clamp_for_output(pointer, closest, &fx, &fy);
949         weston_pointer_move(pointer, fx, fy);
950
951 }
952
953 WL_EXPORT void
954 notify_motion(struct weston_seat *seat,
955               uint32_t time, wl_fixed_t dx, wl_fixed_t dy)
956 {
957         struct weston_compositor *ec = seat->compositor;
958         struct weston_pointer *pointer = seat->pointer;
959
960         weston_compositor_wake(ec);
961         pointer->grab->interface->motion(pointer->grab, time, pointer->x + dx, pointer->y + dy);
962 }
963
964 static void
965 run_modifier_bindings(struct weston_seat *seat, uint32_t old, uint32_t new)
966 {
967         struct weston_compositor *compositor = seat->compositor;
968         struct weston_keyboard *keyboard = seat->keyboard;
969         uint32_t diff;
970         unsigned int i;
971         struct {
972                 uint32_t xkb;
973                 enum weston_keyboard_modifier weston;
974         } mods[] = {
975                 { keyboard->xkb_info->ctrl_mod, MODIFIER_CTRL },
976                 { keyboard->xkb_info->alt_mod, MODIFIER_ALT },
977                 { keyboard->xkb_info->super_mod, MODIFIER_SUPER },
978                 { keyboard->xkb_info->shift_mod, MODIFIER_SHIFT },
979         };
980
981         diff = new & ~old;
982         for (i = 0; i < ARRAY_LENGTH(mods); i++) {
983                 if (diff & (1 << mods[i].xkb))
984                         weston_compositor_run_modifier_binding(compositor,
985                                                                seat,
986                                                                mods[i].weston,
987                                                                WL_KEYBOARD_KEY_STATE_PRESSED);
988         }
989
990         diff = old & ~new;
991         for (i = 0; i < ARRAY_LENGTH(mods); i++) {
992                 if (diff & (1 << mods[i].xkb))
993                         weston_compositor_run_modifier_binding(compositor,
994                                                                seat,
995                                                                mods[i].weston,
996                                                                WL_KEYBOARD_KEY_STATE_RELEASED);
997         }
998 }
999
1000 WL_EXPORT void
1001 notify_motion_absolute(struct weston_seat *seat,
1002                        uint32_t time, wl_fixed_t x, wl_fixed_t y)
1003 {
1004         struct weston_compositor *ec = seat->compositor;
1005         struct weston_pointer *pointer = seat->pointer;
1006
1007         weston_compositor_wake(ec);
1008         pointer->grab->interface->motion(pointer->grab, time, x, y);
1009 }
1010
1011 WL_EXPORT void
1012 weston_surface_activate(struct weston_surface *surface,
1013                         struct weston_seat *seat)
1014 {
1015         struct weston_compositor *compositor = seat->compositor;
1016
1017         if (seat->keyboard) {
1018                 weston_keyboard_set_focus(seat->keyboard, surface);
1019                 wl_data_device_set_keyboard_focus(seat);
1020         }
1021
1022         wl_signal_emit(&compositor->activate_signal, surface);
1023 }
1024
1025 WL_EXPORT void
1026 notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
1027               enum wl_pointer_button_state state)
1028 {
1029         struct weston_compositor *compositor = seat->compositor;
1030         struct weston_pointer *pointer = seat->pointer;
1031
1032         if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
1033                 weston_compositor_idle_inhibit(compositor);
1034                 if (pointer->button_count == 0) {
1035                         pointer->grab_button = button;
1036                         pointer->grab_time = time;
1037                         pointer->grab_x = pointer->x;
1038                         pointer->grab_y = pointer->y;
1039                 }
1040                 pointer->button_count++;
1041         } else {
1042                 weston_compositor_idle_release(compositor);
1043                 pointer->button_count--;
1044         }
1045
1046         weston_compositor_run_button_binding(compositor, seat, time, button,
1047                                              state);
1048
1049         pointer->grab->interface->button(pointer->grab, time, button, state);
1050
1051         if (pointer->button_count == 1)
1052                 pointer->grab_serial =
1053                         wl_display_get_serial(compositor->wl_display);
1054 }
1055
1056 WL_EXPORT void
1057 notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
1058             wl_fixed_t value)
1059 {
1060         struct weston_compositor *compositor = seat->compositor;
1061         struct weston_pointer *pointer = seat->pointer;
1062         struct wl_resource *resource;
1063         struct wl_list *resource_list;
1064
1065         weston_compositor_wake(compositor);
1066
1067         if (!value)
1068                 return;
1069
1070         if (weston_compositor_run_axis_binding(compositor, seat,
1071                                                    time, axis, value))
1072                 return;
1073
1074         resource_list = &pointer->focus_resource_list;
1075         wl_resource_for_each(resource, resource_list)
1076                 wl_pointer_send_axis(resource, time, axis,
1077                                      value);
1078 }
1079
1080 WL_EXPORT int
1081 weston_keyboard_set_locks(struct weston_keyboard *keyboard,
1082                           uint32_t mask, uint32_t value)
1083 {
1084 #ifdef ENABLE_XKBCOMMON
1085         uint32_t serial;
1086         xkb_mod_mask_t mods_depressed, mods_latched, mods_locked, group;
1087         xkb_mod_mask_t num, caps;
1088
1089         /* We don't want the leds to go out of sync with the actual state
1090          * so if the backend has no way to change the leds don't try to
1091          * change the state */
1092         if (!keyboard->seat->led_update)
1093                 return -1;
1094
1095         mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
1096                                                 XKB_STATE_DEPRESSED);
1097         mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
1098                                                 XKB_STATE_LATCHED);
1099         mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
1100                                                 XKB_STATE_LOCKED);
1101         group = xkb_state_serialize_group(keyboard->xkb_state.state,
1102                                       XKB_STATE_EFFECTIVE);
1103
1104         num = (1 << keyboard->xkb_info->mod2_mod);
1105         caps = (1 << keyboard->xkb_info->caps_mod);
1106         if (mask & WESTON_NUM_LOCK) {
1107                 if (value & WESTON_NUM_LOCK)
1108                         mods_locked |= num;
1109                 else
1110                         mods_locked &= ~num;
1111         }
1112         if (mask & WESTON_CAPS_LOCK) {
1113                 if (value & WESTON_CAPS_LOCK)
1114                         mods_locked |= caps;
1115                 else
1116                         mods_locked &= ~caps;
1117         }
1118
1119         xkb_state_update_mask(keyboard->xkb_state.state, mods_depressed,
1120                               mods_latched, mods_locked, 0, 0, group);
1121
1122         serial = wl_display_next_serial(
1123                                 keyboard->seat->compositor->wl_display);
1124         notify_modifiers(keyboard->seat, serial);
1125
1126         return 0;
1127 #else
1128         return -1;
1129 #endif
1130 }
1131
1132 #ifdef ENABLE_XKBCOMMON
1133 WL_EXPORT void
1134 notify_modifiers(struct weston_seat *seat, uint32_t serial)
1135 {
1136         struct weston_keyboard *keyboard = seat->keyboard;
1137         struct weston_keyboard_grab *grab = keyboard->grab;
1138         uint32_t mods_depressed, mods_latched, mods_locked, group;
1139         uint32_t mods_lookup;
1140         enum weston_led leds = 0;
1141         int changed = 0;
1142
1143         /* Serialize and update our internal state, checking to see if it's
1144          * different to the previous state. */
1145         mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
1146                                                   XKB_STATE_MODS_DEPRESSED);
1147         mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
1148                                                 XKB_STATE_MODS_LATCHED);
1149         mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
1150                                                XKB_STATE_MODS_LOCKED);
1151         group = xkb_state_serialize_layout(keyboard->xkb_state.state,
1152                                            XKB_STATE_LAYOUT_EFFECTIVE);
1153
1154         if (mods_depressed != seat->keyboard->modifiers.mods_depressed ||
1155             mods_latched != seat->keyboard->modifiers.mods_latched ||
1156             mods_locked != seat->keyboard->modifiers.mods_locked ||
1157             group != seat->keyboard->modifiers.group)
1158                 changed = 1;
1159
1160         run_modifier_bindings(seat, seat->keyboard->modifiers.mods_depressed,
1161                               mods_depressed);
1162
1163         seat->keyboard->modifiers.mods_depressed = mods_depressed;
1164         seat->keyboard->modifiers.mods_latched = mods_latched;
1165         seat->keyboard->modifiers.mods_locked = mods_locked;
1166         seat->keyboard->modifiers.group = group;
1167
1168         /* And update the modifier_state for bindings. */
1169         mods_lookup = mods_depressed | mods_latched;
1170         seat->modifier_state = 0;
1171         if (mods_lookup & (1 << keyboard->xkb_info->ctrl_mod))
1172                 seat->modifier_state |= MODIFIER_CTRL;
1173         if (mods_lookup & (1 << keyboard->xkb_info->alt_mod))
1174                 seat->modifier_state |= MODIFIER_ALT;
1175         if (mods_lookup & (1 << keyboard->xkb_info->super_mod))
1176                 seat->modifier_state |= MODIFIER_SUPER;
1177         if (mods_lookup & (1 << keyboard->xkb_info->shift_mod))
1178                 seat->modifier_state |= MODIFIER_SHIFT;
1179
1180         /* Finally, notify the compositor that LEDs have changed. */
1181         if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
1182                                           keyboard->xkb_info->num_led))
1183                 leds |= LED_NUM_LOCK;
1184         if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
1185                                           keyboard->xkb_info->caps_led))
1186                 leds |= LED_CAPS_LOCK;
1187         if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
1188                                           keyboard->xkb_info->scroll_led))
1189                 leds |= LED_SCROLL_LOCK;
1190         if (leds != keyboard->xkb_state.leds && seat->led_update)
1191                 seat->led_update(seat, leds);
1192         keyboard->xkb_state.leds = leds;
1193
1194         if (changed) {
1195                 grab->interface->modifiers(grab,
1196                                            serial,
1197                                            keyboard->modifiers.mods_depressed,
1198                                            keyboard->modifiers.mods_latched,
1199                                            keyboard->modifiers.mods_locked,
1200                                            keyboard->modifiers.group);
1201         }
1202 }
1203
1204 static void
1205 update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
1206                       enum wl_keyboard_key_state state)
1207 {
1208         struct weston_keyboard *keyboard = seat->keyboard;
1209         enum xkb_key_direction direction;
1210
1211         /* Keyboard modifiers don't exist in raw keyboard mode */
1212         if (!seat->compositor->use_xkbcommon)
1213                 return;
1214
1215         if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1216                 direction = XKB_KEY_DOWN;
1217         else
1218                 direction = XKB_KEY_UP;
1219
1220         /* Offset the keycode by 8, as the evdev XKB rules reflect X's
1221          * broken keycode system, which starts at 8. */
1222         xkb_state_update_key(keyboard->xkb_state.state, key + 8, direction);
1223
1224         notify_modifiers(seat, serial);
1225 }
1226
1227 static void
1228 send_keymap(struct wl_resource *resource, struct weston_xkb_info *xkb_info)
1229 {
1230         wl_keyboard_send_keymap(resource,
1231                                 WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
1232                                 xkb_info->keymap_fd,
1233                                 xkb_info->keymap_size);
1234 }
1235
1236 static void
1237 send_modifiers(struct wl_resource *resource, uint32_t serial, struct weston_keyboard *keyboard)
1238 {
1239         wl_keyboard_send_modifiers(resource, serial,
1240                                    keyboard->modifiers.mods_depressed,
1241                                    keyboard->modifiers.mods_latched,
1242                                    keyboard->modifiers.mods_locked,
1243                                    keyboard->modifiers.group);
1244 }
1245
1246 static struct weston_xkb_info *
1247 weston_xkb_info_create(struct xkb_keymap *keymap);
1248
1249 static void
1250 update_keymap(struct weston_seat *seat)
1251 {
1252         struct weston_keyboard *keyboard = seat->keyboard;
1253         struct wl_resource *resource;
1254         struct weston_xkb_info *xkb_info;
1255         struct xkb_state *state;
1256         xkb_mod_mask_t latched_mods;
1257         xkb_mod_mask_t locked_mods;
1258
1259         xkb_info = weston_xkb_info_create(keyboard->pending_keymap);
1260
1261         xkb_keymap_unref(keyboard->pending_keymap);
1262         keyboard->pending_keymap = NULL;
1263
1264         if (!xkb_info) {
1265                 weston_log("failed to create XKB info\n");
1266                 return;
1267         }
1268
1269         state = xkb_state_new(xkb_info->keymap);
1270         if (!state) {
1271                 weston_log("failed to initialise XKB state\n");
1272                 weston_xkb_info_destroy(xkb_info);
1273                 return;
1274         }
1275
1276         latched_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
1277                                                 XKB_STATE_MODS_LATCHED);
1278         locked_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
1279                                                XKB_STATE_MODS_LOCKED);
1280         xkb_state_update_mask(state,
1281                               0, /* depressed */
1282                               latched_mods,
1283                               locked_mods,
1284                               0, 0, 0);
1285
1286         weston_xkb_info_destroy(keyboard->xkb_info);
1287         keyboard->xkb_info = xkb_info;
1288
1289         xkb_state_unref(keyboard->xkb_state.state);
1290         keyboard->xkb_state.state = state;
1291
1292         wl_resource_for_each(resource, &seat->keyboard->resource_list)
1293                 send_keymap(resource, xkb_info);
1294         wl_resource_for_each(resource, &seat->keyboard->focus_resource_list)
1295                 send_keymap(resource, xkb_info);
1296
1297         notify_modifiers(seat, wl_display_next_serial(seat->compositor->wl_display));
1298
1299         if (!latched_mods && !locked_mods)
1300                 return;
1301
1302         wl_resource_for_each(resource, &seat->keyboard->resource_list)
1303                 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), seat->keyboard);
1304         wl_resource_for_each(resource, &seat->keyboard->focus_resource_list)
1305                 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), seat->keyboard);
1306 }
1307 #else
1308 WL_EXPORT void
1309 notify_modifiers(struct weston_seat *seat, uint32_t serial)
1310 {
1311 }
1312
1313 static void
1314 update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
1315                       enum wl_keyboard_key_state state)
1316 {
1317 }
1318
1319 static void
1320 update_keymap(struct weston_seat *seat)
1321 {
1322 }
1323 #endif
1324
1325 WL_EXPORT void
1326 notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
1327            enum wl_keyboard_key_state state,
1328            enum weston_key_state_update update_state)
1329 {
1330         struct weston_compositor *compositor = seat->compositor;
1331         struct weston_keyboard *keyboard = seat->keyboard;
1332         struct weston_keyboard_grab *grab = keyboard->grab;
1333         uint32_t *k, *end;
1334
1335         if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1336                 weston_compositor_idle_inhibit(compositor);
1337                 keyboard->grab_key = key;
1338                 keyboard->grab_time = time;
1339         } else {
1340                 weston_compositor_idle_release(compositor);
1341         }
1342
1343         end = keyboard->keys.data + keyboard->keys.size;
1344         for (k = keyboard->keys.data; k < end; k++) {
1345                 if (*k == key) {
1346                         /* Ignore server-generated repeats. */
1347                         if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1348                                 return;
1349                         *k = *--end;
1350                 }
1351         }
1352         keyboard->keys.size = (void *) end - keyboard->keys.data;
1353         if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1354                 k = wl_array_add(&keyboard->keys, sizeof *k);
1355                 *k = key;
1356         }
1357
1358         if (grab == &keyboard->default_grab ||
1359             grab == &keyboard->input_method_grab) {
1360                 weston_compositor_run_key_binding(compositor, seat, time, key,
1361                                                   state);
1362                 grab = keyboard->grab;
1363         }
1364
1365         grab->interface->key(grab, time, key, state);
1366
1367         if (keyboard->pending_keymap &&
1368             keyboard->keys.size == 0)
1369                 update_keymap(seat);
1370
1371         if (update_state == STATE_UPDATE_AUTOMATIC) {
1372                 update_modifier_state(seat,
1373                                       wl_display_get_serial(compositor->wl_display),
1374                                       key,
1375                                       state);
1376         }
1377 }
1378
1379 WL_EXPORT void
1380 notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
1381                      wl_fixed_t x, wl_fixed_t y)
1382 {
1383         if (output) {
1384                 weston_pointer_move(seat->pointer, x, y);
1385         } else {
1386                 /* FIXME: We should call weston_pointer_set_focus(seat,
1387                  * NULL) here, but somehow that breaks re-entry... */
1388         }
1389 }
1390
1391 static void
1392 destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
1393 {
1394         struct weston_seat *ws;
1395
1396         ws = container_of(listener, struct weston_seat,
1397                           saved_kbd_focus_listener);
1398
1399         ws->saved_kbd_focus = NULL;
1400 }
1401
1402 WL_EXPORT void
1403 notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
1404                          enum weston_key_state_update update_state)
1405 {
1406         struct weston_compositor *compositor = seat->compositor;
1407         struct weston_keyboard *keyboard = seat->keyboard;
1408         struct weston_surface *surface;
1409         uint32_t *k, serial;
1410
1411         serial = wl_display_next_serial(compositor->wl_display);
1412         wl_array_copy(&keyboard->keys, keys);
1413         wl_array_for_each(k, &keyboard->keys) {
1414                 weston_compositor_idle_inhibit(compositor);
1415                 if (update_state == STATE_UPDATE_AUTOMATIC)
1416                         update_modifier_state(seat, serial, *k,
1417                                               WL_KEYBOARD_KEY_STATE_PRESSED);
1418         }
1419
1420         /* Run key bindings after we've updated the state. */
1421         wl_array_for_each(k, &keyboard->keys) {
1422                 weston_compositor_run_key_binding(compositor, seat, 0, *k,
1423                                                   WL_KEYBOARD_KEY_STATE_PRESSED);
1424         }
1425
1426         surface = seat->saved_kbd_focus;
1427
1428         if (surface) {
1429                 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1430                 weston_keyboard_set_focus(keyboard, surface);
1431                 seat->saved_kbd_focus = NULL;
1432         }
1433 }
1434
1435 WL_EXPORT void
1436 notify_keyboard_focus_out(struct weston_seat *seat)
1437 {
1438         struct weston_compositor *compositor = seat->compositor;
1439         struct weston_keyboard *keyboard = seat->keyboard;
1440         uint32_t *k, serial;
1441
1442         serial = wl_display_next_serial(compositor->wl_display);
1443         wl_array_for_each(k, &keyboard->keys) {
1444                 weston_compositor_idle_release(compositor);
1445                 update_modifier_state(seat, serial, *k,
1446                                       WL_KEYBOARD_KEY_STATE_RELEASED);
1447         }
1448
1449         seat->modifier_state = 0;
1450
1451         if (keyboard->focus) {
1452                 seat->saved_kbd_focus = keyboard->focus;
1453                 seat->saved_kbd_focus_listener.notify =
1454                         destroy_device_saved_kbd_focus;
1455                 wl_signal_add(&keyboard->focus->destroy_signal,
1456                               &seat->saved_kbd_focus_listener);
1457         }
1458
1459         weston_keyboard_set_focus(keyboard, NULL);
1460         weston_keyboard_cancel_grab(keyboard);
1461         if (seat->pointer)
1462                 weston_pointer_cancel_grab(seat->pointer);
1463 }
1464
1465 WL_EXPORT void
1466 weston_touch_set_focus(struct weston_seat *seat, struct weston_view *view)
1467 {
1468         struct wl_list *focus_resource_list;
1469
1470         focus_resource_list = &seat->touch->focus_resource_list;
1471
1472         if (view && seat->touch->focus &&
1473             seat->touch->focus->surface == view->surface) {
1474                 seat->touch->focus = view;
1475                 return;
1476         }
1477
1478         wl_list_remove(&seat->touch->focus_resource_listener.link);
1479         wl_list_init(&seat->touch->focus_resource_listener.link);
1480         wl_list_remove(&seat->touch->focus_view_listener.link);
1481         wl_list_init(&seat->touch->focus_view_listener.link);
1482
1483         if (!wl_list_empty(focus_resource_list)) {
1484                 move_resources(&seat->touch->resource_list,
1485                                focus_resource_list);
1486         }
1487
1488         if (view) {
1489                 struct wl_client *surface_client;
1490
1491                 if (!view->surface->resource) {
1492                         seat->touch->focus = NULL;
1493                         return;
1494                 }
1495
1496                 surface_client = wl_resource_get_client(view->surface->resource);
1497                 move_resources_for_client(focus_resource_list,
1498                                           &seat->touch->resource_list,
1499                                           surface_client);
1500                 wl_resource_add_destroy_listener(view->surface->resource,
1501                                                  &seat->touch->focus_resource_listener);
1502                 wl_signal_add(&view->destroy_signal, &seat->touch->focus_view_listener);
1503         }
1504         seat->touch->focus = view;
1505 }
1506
1507 /**
1508  * notify_touch - emulates button touches and notifies surfaces accordingly.
1509  *
1510  * It assumes always the correct cycle sequence until it gets here: touch_down
1511  * → touch_update → ... → touch_update → touch_end. The driver is responsible
1512  * for sending along such order.
1513  *
1514  */
1515 WL_EXPORT void
1516 notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
1517              wl_fixed_t x, wl_fixed_t y, int touch_type)
1518 {
1519         struct weston_compositor *ec = seat->compositor;
1520         struct weston_touch *touch = seat->touch;
1521         struct weston_touch_grab *grab = touch->grab;
1522         struct weston_view *ev;
1523         wl_fixed_t sx, sy;
1524
1525         /* Update grab's global coordinates. */
1526         if (touch_id == touch->grab_touch_id && touch_type != WL_TOUCH_UP) {
1527                 touch->grab_x = x;
1528                 touch->grab_y = y;
1529         }
1530
1531         switch (touch_type) {
1532         case WL_TOUCH_DOWN:
1533                 weston_compositor_idle_inhibit(ec);
1534
1535                 touch->num_tp++;
1536
1537                 /* the first finger down picks the view, and all further go
1538                  * to that view for the remainder of the touch session i.e.
1539                  * until all touch points are up again. */
1540                 if (touch->num_tp == 1) {
1541                         ev = weston_compositor_pick_view(ec, x, y, &sx, &sy);
1542                         weston_touch_set_focus(seat, ev);
1543                 } else if (touch->focus) {
1544                         ev = touch->focus;
1545                         weston_view_from_global_fixed(ev, x, y, &sx, &sy);
1546                 } else {
1547                         /* Unexpected condition: We have non-initial touch but
1548                          * there is no focused surface.
1549                          */
1550                         weston_log("touch event received with %d points down"
1551                                    "but no surface focused\n", touch->num_tp);
1552                         return;
1553                 }
1554
1555                 weston_compositor_run_touch_binding(ec, seat,
1556                                                     time, touch_type);
1557
1558                 grab->interface->down(grab, time, touch_id, sx, sy);
1559                 if (touch->num_tp == 1) {
1560                         touch->grab_serial =
1561                                 wl_display_get_serial(ec->wl_display);
1562                         touch->grab_touch_id = touch_id;
1563                         touch->grab_time = time;
1564                         touch->grab_x = x;
1565                         touch->grab_y = y;
1566                 }
1567
1568                 break;
1569         case WL_TOUCH_MOTION:
1570                 ev = touch->focus;
1571                 if (!ev)
1572                         break;
1573
1574                 weston_view_from_global_fixed(ev, x, y, &sx, &sy);
1575                 grab->interface->motion(grab, time, touch_id, sx, sy);
1576                 break;
1577         case WL_TOUCH_UP:
1578                 if (touch->num_tp == 0) {
1579                         /* This can happen if we start out with one or
1580                          * more fingers on the touch screen, in which
1581                          * case we didn't get the corresponding down
1582                          * event. */
1583                         weston_log("unmatched touch up event\n");
1584                         break;
1585                 }
1586                 weston_compositor_idle_release(ec);
1587                 touch->num_tp--;
1588
1589                 grab->interface->up(grab, time, touch_id);
1590                 if (touch->num_tp == 0)
1591                         weston_touch_set_focus(seat, NULL);
1592                 break;
1593         }
1594 }
1595
1596 WL_EXPORT void
1597 notify_touch_frame(struct weston_seat *seat)
1598 {
1599         struct weston_touch *touch = seat->touch;
1600         struct weston_touch_grab *grab = touch->grab;
1601
1602         grab->interface->frame(grab);
1603 }
1604
1605 static void
1606 pointer_cursor_surface_configure(struct weston_surface *es,
1607                                  int32_t dx, int32_t dy)
1608 {
1609         struct weston_pointer *pointer = es->configure_private;
1610         int x, y;
1611
1612         if (es->width == 0)
1613                 return;
1614
1615         assert(es == pointer->sprite->surface);
1616
1617         pointer->hotspot_x -= dx;
1618         pointer->hotspot_y -= dy;
1619
1620         x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
1621         y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
1622
1623         weston_view_set_position(pointer->sprite, x, y);
1624
1625         empty_region(&es->pending.input);
1626         empty_region(&es->input);
1627
1628         if (!weston_surface_is_mapped(es)) {
1629                 weston_layer_entry_insert(&es->compositor->cursor_layer.view_list,
1630                                           &pointer->sprite->layer_link);
1631                 weston_view_update_transform(pointer->sprite);
1632         }
1633 }
1634
1635 static void
1636 pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
1637                    uint32_t serial, struct wl_resource *surface_resource,
1638                    int32_t x, int32_t y)
1639 {
1640         struct weston_pointer *pointer = wl_resource_get_user_data(resource);
1641         struct weston_surface *surface = NULL;
1642
1643         if (surface_resource)
1644                 surface = wl_resource_get_user_data(surface_resource);
1645
1646         if (pointer->focus == NULL)
1647                 return;
1648         /* pointer->focus->surface->resource can be NULL. Surfaces like the
1649         black_surface used in shell.c for fullscreen don't have
1650         a resource, but can still have focus */
1651         if (pointer->focus->surface->resource == NULL)
1652                 return;
1653         if (wl_resource_get_client(pointer->focus->surface->resource) != client)
1654                 return;
1655         if (pointer->focus_serial - serial > UINT32_MAX / 2)
1656                 return;
1657
1658         if (surface && pointer->sprite && surface != pointer->sprite->surface) {
1659                 if (surface->configure) {
1660                         wl_resource_post_error(surface->resource,
1661                                                WL_DISPLAY_ERROR_INVALID_OBJECT,
1662                                                "surface->configure already "
1663                                                "set");
1664                         return;
1665                 }
1666         }
1667
1668         if (pointer->sprite)
1669                 pointer_unmap_sprite(pointer);
1670
1671         if (!surface)
1672                 return;
1673
1674         wl_signal_add(&surface->destroy_signal,
1675                       &pointer->sprite_destroy_listener);
1676
1677         surface->configure = pointer_cursor_surface_configure;
1678         surface->configure_private = pointer;
1679         pointer->sprite = weston_view_create(surface);
1680         pointer->hotspot_x = x;
1681         pointer->hotspot_y = y;
1682
1683         if (surface->buffer_ref.buffer) {
1684                 pointer_cursor_surface_configure(surface, 0, 0);
1685                 weston_view_schedule_repaint(pointer->sprite);
1686         }
1687 }
1688
1689 static void
1690 pointer_release(struct wl_client *client, struct wl_resource *resource)
1691 {
1692         wl_resource_destroy(resource);
1693 }
1694
1695 static const struct wl_pointer_interface pointer_interface = {
1696         pointer_set_cursor,
1697         pointer_release
1698 };
1699
1700 static void
1701 seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
1702                  uint32_t id)
1703 {
1704         struct weston_seat *seat = wl_resource_get_user_data(resource);
1705         struct wl_resource *cr;
1706
1707         if (!seat->pointer)
1708                 return;
1709
1710         cr = wl_resource_create(client, &wl_pointer_interface,
1711                                 wl_resource_get_version(resource), id);
1712         if (cr == NULL) {
1713                 wl_client_post_no_memory(client);
1714                 return;
1715         }
1716
1717         /* May be moved to focused list later by either
1718          * weston_pointer_set_focus or directly if this client is already
1719          * focused */
1720         wl_list_insert(&seat->pointer->resource_list, wl_resource_get_link(cr));
1721         wl_resource_set_implementation(cr, &pointer_interface, seat->pointer,
1722                                        unbind_resource);
1723
1724         if (seat->pointer->focus && seat->pointer->focus->surface->resource &&
1725             wl_resource_get_client(seat->pointer->focus->surface->resource) == client) {
1726                 wl_fixed_t sx, sy;
1727
1728                 weston_view_from_global_fixed(seat->pointer->focus,
1729                                               seat->pointer->x,
1730                                               seat->pointer->y,
1731                                               &sx, &sy);
1732
1733                 wl_list_remove(wl_resource_get_link(cr));
1734                 wl_list_insert(&seat->pointer->focus_resource_list,
1735                                wl_resource_get_link(cr));
1736                 wl_pointer_send_enter(cr,
1737                                       seat->pointer->focus_serial,
1738                                       seat->pointer->focus->surface->resource,
1739                                       sx, sy);
1740         }
1741 }
1742
1743 static void
1744 keyboard_release(struct wl_client *client, struct wl_resource *resource)
1745 {
1746         wl_resource_destroy(resource);
1747 }
1748
1749 static const struct wl_keyboard_interface keyboard_interface = {
1750         keyboard_release
1751 };
1752
1753 static int
1754 should_send_modifiers_to_client(struct weston_seat *seat,
1755                                 struct wl_client *client)
1756 {
1757         if (seat->keyboard &&
1758             seat->keyboard->focus &&
1759             seat->keyboard->focus->resource &&
1760             wl_resource_get_client(seat->keyboard->focus->resource) == client)
1761                 return 1;
1762
1763         if (seat->pointer &&
1764             seat->pointer->focus &&
1765             seat->pointer->focus->surface->resource &&
1766             wl_resource_get_client(seat->pointer->focus->surface->resource) == client)
1767                 return 1;
1768
1769         return 0;
1770 }
1771
1772 static void
1773 seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
1774                   uint32_t id)
1775 {
1776         struct weston_seat *seat = wl_resource_get_user_data(resource);
1777         struct weston_keyboard *keyboard = seat->keyboard;
1778         struct wl_resource *cr;
1779
1780         if (!seat->keyboard)
1781                 return;
1782
1783         cr = wl_resource_create(client, &wl_keyboard_interface,
1784                                 wl_resource_get_version(resource), id);
1785         if (cr == NULL) {
1786                 wl_client_post_no_memory(client);
1787                 return;
1788         }
1789
1790         /* May be moved to focused list later by either
1791          * weston_keyboard_set_focus or directly if this client is already
1792          * focused */
1793         wl_list_insert(&seat->keyboard->resource_list, wl_resource_get_link(cr));
1794         wl_resource_set_implementation(cr, &keyboard_interface,
1795                                        seat, unbind_resource);
1796
1797         if (wl_resource_get_version(cr) >= WL_KEYBOARD_REPEAT_INFO_SINCE_VERSION) {
1798                 wl_keyboard_send_repeat_info(cr,
1799                                              seat->compositor->kb_repeat_rate,
1800                                              seat->compositor->kb_repeat_delay);
1801         }
1802
1803         if (seat->compositor->use_xkbcommon) {
1804                 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
1805                                         keyboard->xkb_info->keymap_fd,
1806                                         keyboard->xkb_info->keymap_size);
1807         } else {
1808                 int null_fd = open("/dev/null", O_RDONLY);
1809                 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP,
1810                                         null_fd,
1811                                         0);
1812                 close(null_fd);
1813         }
1814
1815         if (should_send_modifiers_to_client(seat, client)) {
1816                 send_modifiers_to_resource(seat->keyboard,
1817                                            cr,
1818                                            seat->keyboard->focus_serial);
1819         }
1820
1821         if (seat->keyboard->focus &&
1822             wl_resource_get_client(seat->keyboard->focus->resource) == client) {
1823                 struct weston_surface *surface =
1824                         (struct weston_surface *) seat->keyboard->focus;
1825
1826                 wl_list_remove(wl_resource_get_link(cr));
1827                 wl_list_insert(&seat->keyboard->focus_resource_list,
1828                                wl_resource_get_link(cr));
1829                 wl_keyboard_send_enter(cr,
1830                                        seat->keyboard->focus_serial,
1831                                        surface->resource,
1832                                        &seat->keyboard->keys);
1833
1834                 /* If this is the first keyboard resource for this
1835                  * client... */
1836                 if (seat->keyboard->focus_resource_list.prev ==
1837                     wl_resource_get_link(cr))
1838                         wl_data_device_set_keyboard_focus(seat);
1839         }
1840 }
1841
1842 static void
1843 touch_release(struct wl_client *client, struct wl_resource *resource)
1844 {
1845         wl_resource_destroy(resource);
1846 }
1847
1848 static const struct wl_touch_interface touch_interface = {
1849         touch_release
1850 };
1851
1852 static void
1853 seat_get_touch(struct wl_client *client, struct wl_resource *resource,
1854                uint32_t id)
1855 {
1856         struct weston_seat *seat = wl_resource_get_user_data(resource);
1857         struct wl_resource *cr;
1858
1859         if (!seat->touch)
1860                 return;
1861
1862         cr = wl_resource_create(client, &wl_touch_interface,
1863                                 wl_resource_get_version(resource), id);
1864         if (cr == NULL) {
1865                 wl_client_post_no_memory(client);
1866                 return;
1867         }
1868
1869         if (seat->touch->focus &&
1870             wl_resource_get_client(seat->touch->focus->surface->resource) == client) {
1871                 wl_list_insert(&seat->touch->resource_list,
1872                                wl_resource_get_link(cr));
1873         } else {
1874                 wl_list_insert(&seat->touch->focus_resource_list,
1875                                wl_resource_get_link(cr));
1876         }
1877         wl_resource_set_implementation(cr, &touch_interface,
1878                                        seat, unbind_resource);
1879 }
1880
1881 static const struct wl_seat_interface seat_interface = {
1882         seat_get_pointer,
1883         seat_get_keyboard,
1884         seat_get_touch,
1885 };
1886
1887 static void
1888 bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1889 {
1890         struct weston_seat *seat = data;
1891         struct wl_resource *resource;
1892         enum wl_seat_capability caps = 0;
1893
1894         resource = wl_resource_create(client,
1895                                       &wl_seat_interface, MIN(version, 4), id);
1896         wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
1897         wl_resource_set_implementation(resource, &seat_interface, data,
1898                                        unbind_resource);
1899
1900         if (seat->pointer)
1901                 caps |= WL_SEAT_CAPABILITY_POINTER;
1902         if (seat->keyboard)
1903                 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
1904         if (seat->touch)
1905                 caps |= WL_SEAT_CAPABILITY_TOUCH;
1906
1907         wl_seat_send_capabilities(resource, caps);
1908         if (version >= WL_SEAT_NAME_SINCE_VERSION)
1909                 wl_seat_send_name(resource, seat->seat_name);
1910 }
1911
1912 #ifdef ENABLE_XKBCOMMON
1913 int
1914 weston_compositor_xkb_init(struct weston_compositor *ec,
1915                            struct xkb_rule_names *names)
1916 {
1917         ec->use_xkbcommon = 1;
1918
1919         if (ec->xkb_context == NULL) {
1920                 ec->xkb_context = xkb_context_new(0);
1921                 if (ec->xkb_context == NULL) {
1922                         weston_log("failed to create XKB context\n");
1923                         return -1;
1924                 }
1925         }
1926
1927         if (names)
1928                 ec->xkb_names = *names;
1929         if (!ec->xkb_names.rules)
1930                 ec->xkb_names.rules = strdup("evdev");
1931         if (!ec->xkb_names.model)
1932                 ec->xkb_names.model = strdup("pc105");
1933         if (!ec->xkb_names.layout)
1934                 ec->xkb_names.layout = strdup("us");
1935
1936         return 0;
1937 }
1938
1939 static void
1940 weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
1941 {
1942         if (--xkb_info->ref_count > 0)
1943                 return;
1944
1945         xkb_keymap_unref(xkb_info->keymap);
1946
1947         if (xkb_info->keymap_area)
1948                 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
1949         if (xkb_info->keymap_fd >= 0)
1950                 close(xkb_info->keymap_fd);
1951         free(xkb_info);
1952 }
1953
1954 void
1955 weston_compositor_xkb_destroy(struct weston_compositor *ec)
1956 {
1957         /*
1958          * If we're operating in raw keyboard mode, we never initialized
1959          * libxkbcommon so there's no cleanup to do either.
1960          */
1961         if (!ec->use_xkbcommon)
1962                 return;
1963
1964         free((char *) ec->xkb_names.rules);
1965         free((char *) ec->xkb_names.model);
1966         free((char *) ec->xkb_names.layout);
1967         free((char *) ec->xkb_names.variant);
1968         free((char *) ec->xkb_names.options);
1969
1970         if (ec->xkb_info)
1971                 weston_xkb_info_destroy(ec->xkb_info);
1972         xkb_context_unref(ec->xkb_context);
1973 }
1974
1975 static struct weston_xkb_info *
1976 weston_xkb_info_create(struct xkb_keymap *keymap)
1977 {
1978         struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
1979         if (xkb_info == NULL)
1980                 return NULL;
1981
1982         xkb_info->keymap = xkb_keymap_ref(keymap);
1983         xkb_info->ref_count = 1;
1984
1985         char *keymap_str;
1986
1987         xkb_info->shift_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
1988                                                        XKB_MOD_NAME_SHIFT);
1989         xkb_info->caps_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
1990                                                       XKB_MOD_NAME_CAPS);
1991         xkb_info->ctrl_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
1992                                                       XKB_MOD_NAME_CTRL);
1993         xkb_info->alt_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
1994                                                      XKB_MOD_NAME_ALT);
1995         xkb_info->mod2_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
1996                                                       "Mod2");
1997         xkb_info->mod3_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
1998                                                       "Mod3");
1999         xkb_info->super_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2000                                                        XKB_MOD_NAME_LOGO);
2001         xkb_info->mod5_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2002                                                       "Mod5");
2003
2004         xkb_info->num_led = xkb_keymap_led_get_index(xkb_info->keymap,
2005                                                      XKB_LED_NAME_NUM);
2006         xkb_info->caps_led = xkb_keymap_led_get_index(xkb_info->keymap,
2007                                                       XKB_LED_NAME_CAPS);
2008         xkb_info->scroll_led = xkb_keymap_led_get_index(xkb_info->keymap,
2009                                                         XKB_LED_NAME_SCROLL);
2010
2011         keymap_str = xkb_keymap_get_as_string(xkb_info->keymap,
2012                                               XKB_KEYMAP_FORMAT_TEXT_V1);
2013         if (keymap_str == NULL) {
2014                 weston_log("failed to get string version of keymap\n");
2015                 goto err_keymap;
2016         }
2017         xkb_info->keymap_size = strlen(keymap_str) + 1;
2018
2019         xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
2020         if (xkb_info->keymap_fd < 0) {
2021                 weston_log("creating a keymap file for %lu bytes failed: %m\n",
2022                         (unsigned long) xkb_info->keymap_size);
2023                 goto err_keymap_str;
2024         }
2025
2026         xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2027                                      PROT_READ | PROT_WRITE,
2028                                      MAP_SHARED, xkb_info->keymap_fd, 0);
2029         if (xkb_info->keymap_area == MAP_FAILED) {
2030                 weston_log("failed to mmap() %lu bytes\n",
2031                         (unsigned long) xkb_info->keymap_size);
2032                 goto err_dev_zero;
2033         }
2034         strcpy(xkb_info->keymap_area, keymap_str);
2035         free(keymap_str);
2036
2037         return xkb_info;
2038
2039 err_dev_zero:
2040         close(xkb_info->keymap_fd);
2041 err_keymap_str:
2042         free(keymap_str);
2043 err_keymap:
2044         xkb_keymap_unref(xkb_info->keymap);
2045         free(xkb_info);
2046         return NULL;
2047 }
2048
2049 static int
2050 weston_compositor_build_global_keymap(struct weston_compositor *ec)
2051 {
2052         struct xkb_keymap *keymap;
2053
2054         if (ec->xkb_info != NULL)
2055                 return 0;
2056
2057         keymap = xkb_keymap_new_from_names(ec->xkb_context,
2058                                            &ec->xkb_names,
2059                                            0);
2060         if (keymap == NULL) {
2061                 weston_log("failed to compile global XKB keymap\n");
2062                 weston_log("  tried rules %s, model %s, layout %s, variant %s, "
2063                         "options %s\n",
2064                         ec->xkb_names.rules, ec->xkb_names.model,
2065                         ec->xkb_names.layout, ec->xkb_names.variant,
2066                         ec->xkb_names.options);
2067                 return -1;
2068         }
2069
2070         ec->xkb_info = weston_xkb_info_create(keymap);
2071         xkb_keymap_unref(keymap);
2072         if (ec->xkb_info == NULL)
2073                 return -1;
2074
2075         return 0;
2076 }
2077 #else
2078 int
2079 weston_compositor_xkb_init(struct weston_compositor *ec,
2080                            struct xkb_rule_names *names)
2081 {
2082         return 0;
2083 }
2084
2085 void
2086 weston_compositor_xkb_destroy(struct weston_compositor *ec)
2087 {
2088 }
2089 #endif
2090
2091 WL_EXPORT void
2092 weston_seat_update_keymap(struct weston_seat *seat, struct xkb_keymap *keymap)
2093 {
2094         if (!seat->keyboard || !keymap)
2095                 return;
2096
2097 #ifdef ENABLE_XKBCOMMON
2098         if (!seat->compositor->use_xkbcommon)
2099                 return;
2100
2101         xkb_keymap_unref(seat->keyboard->pending_keymap);
2102         seat->keyboard->pending_keymap = xkb_keymap_ref(keymap);
2103
2104         if (seat->keyboard->keys.size == 0)
2105                 update_keymap(seat);
2106 #endif
2107 }
2108
2109 WL_EXPORT int
2110 weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
2111 {
2112         struct weston_keyboard *keyboard;
2113
2114         if (seat->keyboard) {
2115                 seat->keyboard_device_count += 1;
2116                 if (seat->keyboard_device_count == 1)
2117                         seat_send_updated_caps(seat);
2118                 return 0;
2119         }
2120
2121         keyboard = weston_keyboard_create();
2122         if (keyboard == NULL) {
2123                 weston_log("failed to allocate weston keyboard struct\n");
2124                 return -1;
2125         }
2126
2127 #ifdef ENABLE_XKBCOMMON
2128         if (seat->compositor->use_xkbcommon) {
2129                 if (keymap != NULL) {
2130                         keyboard->xkb_info = weston_xkb_info_create(keymap);
2131                         if (keyboard->xkb_info == NULL)
2132                                 goto err;
2133                 } else {
2134                         if (weston_compositor_build_global_keymap(seat->compositor) < 0)
2135                                 goto err;
2136                         keyboard->xkb_info = seat->compositor->xkb_info;
2137                         keyboard->xkb_info->ref_count++;
2138                 }
2139
2140                 keyboard->xkb_state.state = xkb_state_new(keyboard->xkb_info->keymap);
2141                 if (keyboard->xkb_state.state == NULL) {
2142                         weston_log("failed to initialise XKB state\n");
2143                         goto err;
2144                 }
2145
2146                 keyboard->xkb_state.leds = 0;
2147         }
2148 #endif
2149
2150         seat->keyboard = keyboard;
2151         seat->keyboard_device_count = 1;
2152         keyboard->seat = seat;
2153
2154         seat_send_updated_caps(seat);
2155
2156         return 0;
2157
2158 err:
2159         if (keyboard->xkb_info)
2160                 weston_xkb_info_destroy(keyboard->xkb_info);
2161         free(keyboard);
2162
2163         return -1;
2164 }
2165
2166 static void
2167 weston_keyboard_reset_state(struct weston_keyboard *keyboard)
2168 {
2169         struct weston_seat *seat = keyboard->seat;
2170         struct xkb_state *state;
2171
2172 #ifdef ENABLE_XKBCOMMON
2173         if (seat->compositor->use_xkbcommon) {
2174                 state = xkb_state_new(keyboard->xkb_info->keymap);
2175                 if (!state) {
2176                         weston_log("failed to reset XKB state\n");
2177                         return;
2178                 }
2179                 xkb_state_unref(keyboard->xkb_state.state);
2180                 keyboard->xkb_state.state = state;
2181
2182                 keyboard->xkb_state.leds = 0;
2183         }
2184 #endif
2185
2186         seat->modifier_state = 0;
2187 }
2188
2189 WL_EXPORT void
2190 weston_seat_release_keyboard(struct weston_seat *seat)
2191 {
2192         seat->keyboard_device_count--;
2193         if (seat->keyboard_device_count == 0) {
2194                 weston_keyboard_set_focus(seat->keyboard, NULL);
2195                 weston_keyboard_cancel_grab(seat->keyboard);
2196                 weston_keyboard_reset_state(seat->keyboard);
2197                 seat_send_updated_caps(seat);
2198         }
2199 }
2200
2201 WL_EXPORT void
2202 weston_seat_init_pointer(struct weston_seat *seat)
2203 {
2204         struct weston_pointer *pointer;
2205
2206         if (seat->pointer) {
2207                 seat->pointer_device_count += 1;
2208                 if (seat->pointer_device_count == 1)
2209                         seat_send_updated_caps(seat);
2210                 return;
2211         }
2212
2213         pointer = weston_pointer_create(seat);
2214         if (pointer == NULL)
2215                 return;
2216
2217         seat->pointer = pointer;
2218         seat->pointer_device_count = 1;
2219         pointer->seat = seat;
2220
2221         seat_send_updated_caps(seat);
2222 }
2223
2224 WL_EXPORT void
2225 weston_seat_release_pointer(struct weston_seat *seat)
2226 {
2227         struct weston_pointer *pointer = seat->pointer;
2228
2229         seat->pointer_device_count--;
2230         if (seat->pointer_device_count == 0) {
2231                 weston_pointer_set_focus(pointer, NULL,
2232                                          wl_fixed_from_int(0),
2233                                          wl_fixed_from_int(0));
2234                 weston_pointer_cancel_grab(pointer);
2235
2236                 if (pointer->sprite)
2237                         pointer_unmap_sprite(pointer);
2238
2239                 weston_pointer_reset_state(pointer);
2240                 seat_send_updated_caps(seat);
2241         }
2242 }
2243
2244 WL_EXPORT void
2245 weston_seat_init_touch(struct weston_seat *seat)
2246 {
2247         struct weston_touch *touch;
2248
2249         if (seat->touch) {
2250                 seat->touch_device_count += 1;
2251                 if (seat->touch_device_count == 1)
2252                         seat_send_updated_caps(seat);
2253                 return;
2254         }
2255
2256         touch = weston_touch_create();
2257         if (touch == NULL)
2258                 return;
2259
2260         seat->touch = touch;
2261         seat->touch_device_count = 1;
2262         touch->seat = seat;
2263
2264         seat_send_updated_caps(seat);
2265 }
2266
2267 WL_EXPORT void
2268 weston_seat_release_touch(struct weston_seat *seat)
2269 {
2270         seat->touch_device_count--;
2271         if (seat->touch_device_count == 0) {
2272                 weston_touch_set_focus(seat, NULL);
2273                 weston_touch_cancel_grab(seat->touch);
2274                 weston_touch_reset_state(seat->touch);
2275                 seat_send_updated_caps(seat);
2276         }
2277 }
2278
2279 WL_EXPORT void
2280 weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
2281                  const char *seat_name)
2282 {
2283         memset(seat, 0, sizeof *seat);
2284
2285         seat->selection_data_source = NULL;
2286         wl_list_init(&seat->base_resource_list);
2287         wl_signal_init(&seat->selection_signal);
2288         wl_list_init(&seat->drag_resource_list);
2289         wl_signal_init(&seat->destroy_signal);
2290         wl_signal_init(&seat->updated_caps_signal);
2291
2292         seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 4,
2293                                         seat, bind_seat);
2294
2295         seat->compositor = ec;
2296         seat->modifier_state = 0;
2297         seat->seat_name = strdup(seat_name);
2298
2299         wl_list_insert(ec->seat_list.prev, &seat->link);
2300
2301         clipboard_create(seat);
2302
2303         wl_signal_emit(&ec->seat_created_signal, seat);
2304 }
2305
2306 WL_EXPORT void
2307 weston_seat_release(struct weston_seat *seat)
2308 {
2309         wl_list_remove(&seat->link);
2310
2311         if (seat->saved_kbd_focus)
2312                 wl_list_remove(&seat->saved_kbd_focus_listener.link);
2313
2314         if (seat->pointer)
2315                 weston_pointer_destroy(seat->pointer);
2316         if (seat->keyboard)
2317                 weston_keyboard_destroy(seat->keyboard);
2318         if (seat->touch)
2319                 weston_touch_destroy(seat->touch);
2320
2321         free (seat->seat_name);
2322
2323         wl_global_destroy(seat->global);
2324
2325         wl_signal_emit(&seat->destroy_signal, seat);
2326 }