4f6ba7b3c29fc0b89e46d5514d5f07679a08cda1
[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
33 #include "../shared/os-compatibility.h"
34 #include "compositor.h"
35
36 static void
37 empty_region(pixman_region32_t *region)
38 {
39         pixman_region32_fini(region);
40         pixman_region32_init(region);
41 }
42
43 static void unbind_resource(struct wl_resource *resource)
44 {
45         wl_list_remove(wl_resource_get_link(resource));
46 }
47
48 void
49 weston_seat_repick(struct weston_seat *seat)
50 {
51         const struct weston_pointer *pointer = seat->pointer;
52
53         if (pointer == NULL)
54                 return;
55
56         pointer->grab->interface->focus(seat->pointer->grab);
57 }
58
59 static void
60 weston_compositor_idle_inhibit(struct weston_compositor *compositor)
61 {
62         weston_compositor_wake(compositor);
63         compositor->idle_inhibit++;
64 }
65
66 static void
67 weston_compositor_idle_release(struct weston_compositor *compositor)
68 {
69         compositor->idle_inhibit--;
70         weston_compositor_wake(compositor);
71 }
72
73 static void
74 move_resources(struct wl_list *destination, struct wl_list *source)
75 {
76         wl_list_insert_list(destination, source);
77         wl_list_init(source);
78 }
79
80 static void
81 move_resources_for_client(struct wl_list *destination,
82                           struct wl_list *source,
83                           struct wl_client *client)
84 {
85         struct wl_resource *resource, *tmp;
86         wl_resource_for_each_safe(resource, tmp, source) {
87                 if (wl_resource_get_client(resource) == client) {
88                         wl_list_remove(wl_resource_get_link(resource));
89                         wl_list_insert(destination,
90                                        wl_resource_get_link(resource));
91                 }
92         }
93 }
94
95 static void
96 default_grab_focus(struct weston_pointer_grab *grab)
97 {
98         struct weston_pointer *pointer = grab->pointer;
99         struct weston_surface *surface;
100         wl_fixed_t sx, sy;
101
102         if (pointer->button_count > 0)
103                 return;
104
105         surface = weston_compositor_pick_surface(pointer->seat->compositor,
106                                                  pointer->x, pointer->y,
107                                                  &sx, &sy);
108
109         if (pointer->focus != surface)
110                 weston_pointer_set_focus(pointer, surface, sx, sy);
111 }
112
113 static void
114 default_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
115 {
116         struct weston_pointer *pointer = grab->pointer;
117         wl_fixed_t sx, sy;
118         struct wl_list *resource_list;
119         struct wl_resource *resource;
120
121         resource_list = &pointer->focus_resource_list;
122         wl_resource_for_each(resource, resource_list) {
123                 weston_surface_from_global_fixed(pointer->focus,
124                                                  pointer->x, pointer->y,
125                                                  &sx, &sy);
126                 wl_pointer_send_motion(resource, time, sx, sy);
127         }
128 }
129
130 static void
131 default_grab_button(struct weston_pointer_grab *grab,
132                     uint32_t time, uint32_t button, uint32_t state_w)
133 {
134         struct weston_pointer *pointer = grab->pointer;
135         struct weston_compositor *compositor = pointer->seat->compositor;
136         struct weston_surface *surface;
137         struct wl_resource *resource;
138         uint32_t serial;
139         enum wl_pointer_button_state state = state_w;
140         struct wl_display *display = compositor->wl_display;
141         wl_fixed_t sx, sy;
142         struct wl_list *resource_list;
143
144         resource_list = &pointer->focus_resource_list;
145         if (!wl_list_empty(resource_list)) {
146                 serial = wl_display_next_serial(display);
147                 wl_resource_for_each(resource, resource_list)
148                         wl_pointer_send_button(resource,
149                                                serial,
150                                                time,
151                                                button,
152                                                state_w);
153         }
154
155         if (pointer->button_count == 0 &&
156             state == WL_POINTER_BUTTON_STATE_RELEASED) {
157                 surface = weston_compositor_pick_surface(compositor,
158                                                          pointer->x,
159                                                          pointer->y,
160                                                          &sx, &sy);
161
162                 weston_pointer_set_focus(pointer, surface, sx, sy);
163         }
164 }
165
166 static const struct weston_pointer_grab_interface
167                                 default_pointer_grab_interface = {
168         default_grab_focus,
169         default_grab_motion,
170         default_grab_button
171 };
172
173 static void
174 default_grab_touch_down(struct weston_touch_grab *grab, uint32_t time,
175                         int touch_id, wl_fixed_t sx, wl_fixed_t sy)
176 {
177         struct weston_touch *touch = grab->touch;
178         struct wl_display *display = touch->seat->compositor->wl_display;
179         uint32_t serial;
180         struct wl_resource *resource;
181         struct wl_list *resource_list;
182
183         resource_list = &touch->focus_resource_list;
184
185         if (!wl_list_empty(resource_list) && touch->focus) {
186                 serial = wl_display_next_serial(display);
187                 wl_resource_for_each(resource, resource_list)
188                                 wl_touch_send_down(resource, serial, time,
189                                                    touch->focus->resource,
190                                                    touch_id, sx, sy);
191         }
192 }
193
194 static void
195 default_grab_touch_up(struct weston_touch_grab *grab,
196                       uint32_t time, int touch_id)
197 {
198         struct weston_touch *touch = grab->touch;
199         struct wl_display *display = touch->seat->compositor->wl_display;
200         uint32_t serial;
201         struct wl_resource *resource;
202         struct wl_list *resource_list;
203
204         resource_list = &touch->focus_resource_list;
205
206         if (!wl_list_empty(resource_list)) {
207                 serial = wl_display_next_serial(display);
208                 wl_resource_for_each(resource, resource_list)
209                         wl_touch_send_up(resource, serial, time, touch_id);
210         }
211 }
212
213 static void
214 default_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time,
215                           int touch_id, wl_fixed_t sx, wl_fixed_t sy)
216 {
217         struct weston_touch *touch = grab->touch;
218         struct wl_resource *resource;
219         struct wl_list *resource_list;
220
221         resource_list = &touch->focus_resource_list;
222
223         wl_resource_for_each(resource, resource_list) {
224                 wl_touch_send_motion(resource, time,
225                                      touch_id, sx, sy);
226         }
227 }
228
229 static const struct weston_touch_grab_interface default_touch_grab_interface = {
230         default_grab_touch_down,
231         default_grab_touch_up,
232         default_grab_touch_motion
233 };
234
235 static void
236 default_grab_key(struct weston_keyboard_grab *grab,
237                  uint32_t time, uint32_t key, uint32_t state)
238 {
239         struct weston_keyboard *keyboard = grab->keyboard;
240         struct wl_resource *resource;
241         struct wl_display *display = keyboard->seat->compositor->wl_display;
242         uint32_t serial;
243         struct wl_list *resource_list;
244
245         resource_list = &keyboard->focus_resource_list;
246         if (!wl_list_empty(resource_list)) {
247                 serial = wl_display_next_serial(display);
248                 wl_resource_for_each(resource, resource_list)
249                         wl_keyboard_send_key(resource,
250                                              serial,
251                                              time,
252                                              key,
253                                              state);
254         }
255 }
256
257 static void
258 send_modifiers_to_resource(struct weston_keyboard *keyboard,
259                            struct wl_resource *resource,
260                            uint32_t serial)
261 {
262         wl_keyboard_send_modifiers(resource,
263                                    serial,
264                                    keyboard->modifiers.mods_depressed,
265                                    keyboard->modifiers.mods_latched,
266                                    keyboard->modifiers.mods_locked,
267                                    keyboard->modifiers.group);
268 }
269
270 static void
271 send_modifiers_to_client_in_list(struct wl_client *client,
272                                  struct wl_list *list,
273                                  uint32_t serial,
274                                  struct weston_keyboard *keyboard)
275 {
276         struct wl_resource *resource;
277
278         wl_resource_for_each(resource, list) {
279                 if (wl_resource_get_client(resource) == client)
280                         send_modifiers_to_resource(keyboard,
281                                                    resource,
282                                                    serial);
283         }
284 }
285
286 static struct wl_resource *
287 find_resource_for_surface(struct wl_list *list, struct weston_surface *surface)
288 {
289         if (!surface)
290                 return NULL;
291
292         if (!surface->resource)
293                 return NULL;
294
295         return wl_resource_find_for_client(list, wl_resource_get_client(surface->resource));
296 }
297
298 static void
299 default_grab_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
300                        uint32_t mods_depressed, uint32_t mods_latched,
301                        uint32_t mods_locked, uint32_t group)
302 {
303         struct weston_keyboard *keyboard = grab->keyboard;
304         struct weston_pointer *pointer = grab->keyboard->seat->pointer;
305         struct wl_resource *resource;
306         struct wl_list *resource_list;
307
308         resource_list = &keyboard->focus_resource_list;
309
310         wl_resource_for_each(resource, resource_list) {
311                 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
312                                            mods_latched, mods_locked, group);
313         }
314         if (pointer && pointer->focus && pointer->focus != keyboard->focus) {
315                 struct wl_client *pointer_client =
316                         wl_resource_get_client(pointer->focus->resource);
317                 send_modifiers_to_client_in_list(pointer_client,
318                                                  &keyboard->resource_list,
319                                                  serial,
320                                                  keyboard);
321         }
322 }
323
324 static const struct weston_keyboard_grab_interface
325                                 default_keyboard_grab_interface = {
326         default_grab_key,
327         default_grab_modifiers,
328 };
329
330 static void
331 pointer_unmap_sprite(struct weston_pointer *pointer)
332 {
333         if (weston_surface_is_mapped(pointer->sprite))
334                 weston_surface_unmap(pointer->sprite);
335
336         wl_list_remove(&pointer->sprite_destroy_listener.link);
337         pointer->sprite->configure = NULL;
338         pointer->sprite->configure_private = NULL;
339         pointer->sprite = NULL;
340 }
341
342 static void
343 pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
344 {
345         struct weston_pointer *pointer =
346                 container_of(listener, struct weston_pointer,
347                              sprite_destroy_listener);
348
349         pointer->sprite = NULL;
350 }
351
352 WL_EXPORT struct weston_pointer *
353 weston_pointer_create(void)
354 {
355         struct weston_pointer *pointer;
356
357         pointer = zalloc(sizeof *pointer);
358         if (pointer == NULL)
359                 return NULL;
360
361         wl_list_init(&pointer->resource_list);
362         wl_list_init(&pointer->focus_resource_list);
363         pointer->default_grab.interface = &default_pointer_grab_interface;
364         pointer->default_grab.pointer = pointer;
365         pointer->grab = &pointer->default_grab;
366         wl_signal_init(&pointer->focus_signal);
367
368         pointer->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
369
370         /* FIXME: Pick better co-ords. */
371         pointer->x = wl_fixed_from_int(100);
372         pointer->y = wl_fixed_from_int(100);
373
374         return pointer;
375 }
376
377 WL_EXPORT void
378 weston_pointer_destroy(struct weston_pointer *pointer)
379 {
380         if (pointer->sprite)
381                 pointer_unmap_sprite(pointer);
382
383         /* XXX: What about pointer->resource_list? */
384
385         free(pointer);
386 }
387
388 WL_EXPORT struct weston_keyboard *
389 weston_keyboard_create(void)
390 {
391         struct weston_keyboard *keyboard;
392
393         keyboard = zalloc(sizeof *keyboard);
394         if (keyboard == NULL)
395             return NULL;
396
397         wl_list_init(&keyboard->resource_list);
398         wl_list_init(&keyboard->focus_resource_list);
399         wl_array_init(&keyboard->keys);
400         keyboard->default_grab.interface = &default_keyboard_grab_interface;
401         keyboard->default_grab.keyboard = keyboard;
402         keyboard->grab = &keyboard->default_grab;
403         wl_signal_init(&keyboard->focus_signal);
404
405         return keyboard;
406 }
407
408 WL_EXPORT void
409 weston_keyboard_destroy(struct weston_keyboard *keyboard)
410 {
411         /* XXX: What about keyboard->resource_list? */
412
413         wl_array_release(&keyboard->keys);
414         free(keyboard);
415 }
416
417 WL_EXPORT struct weston_touch *
418 weston_touch_create(void)
419 {
420         struct weston_touch *touch;
421
422         touch = zalloc(sizeof *touch);
423         if (touch == NULL)
424                 return NULL;
425
426         wl_list_init(&touch->resource_list);
427         wl_list_init(&touch->focus_resource_list);
428         touch->default_grab.interface = &default_touch_grab_interface;
429         touch->default_grab.touch = touch;
430         touch->grab = &touch->default_grab;
431         wl_signal_init(&touch->focus_signal);
432
433         return touch;
434 }
435
436 WL_EXPORT void
437 weston_touch_destroy(struct weston_touch *touch)
438 {
439         /* XXX: What about touch->resource_list? */
440
441         free(touch);
442 }
443
444 static void
445 seat_send_updated_caps(struct weston_seat *seat)
446 {
447         enum wl_seat_capability caps = 0;
448         struct wl_resource *resource;
449
450         if (seat->pointer_device_count > 0)
451                 caps |= WL_SEAT_CAPABILITY_POINTER;
452         if (seat->keyboard_device_count > 0)
453                 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
454         if (seat->touch_device_count > 0)
455                 caps |= WL_SEAT_CAPABILITY_TOUCH;
456
457         wl_resource_for_each(resource, &seat->base_resource_list) {
458                 wl_seat_send_capabilities(resource, caps);
459         }
460 }
461
462 WL_EXPORT void
463 weston_pointer_set_focus(struct weston_pointer *pointer,
464                          struct weston_surface *surface,
465                          wl_fixed_t sx, wl_fixed_t sy)
466 {
467         struct weston_keyboard *kbd = pointer->seat->keyboard;
468         struct wl_resource *resource;
469         struct wl_display *display = pointer->seat->compositor->wl_display;
470         uint32_t serial;
471         struct wl_list *focus_resource_list;
472
473         focus_resource_list = &pointer->focus_resource_list;
474
475         if (!wl_list_empty(focus_resource_list) && pointer->focus != surface) {
476                 serial = wl_display_next_serial(display);
477                 wl_resource_for_each(resource, focus_resource_list) {
478                         wl_pointer_send_leave(resource, serial,
479                                               pointer->focus->resource);
480                 }
481
482                 move_resources(&pointer->resource_list, focus_resource_list);
483         }
484
485         if (find_resource_for_surface(&pointer->resource_list, surface) &&
486             pointer->focus != surface) {
487                 struct wl_client *surface_client =
488                         wl_resource_get_client(surface->resource);
489
490                 serial = wl_display_next_serial(display);
491
492                 if (kbd && kbd->focus != pointer->focus)
493                         send_modifiers_to_client_in_list(surface_client,
494                                                          &kbd->resource_list,
495                                                          serial,
496                                                          kbd);
497
498                 move_resources_for_client(focus_resource_list,
499                                           &pointer->resource_list,
500                                           surface_client);
501
502                 wl_resource_for_each(resource, focus_resource_list) {
503                         wl_pointer_send_enter(resource,
504                                               serial,
505                                               surface->resource,
506                                               sx, sy);
507                 }
508
509                 pointer->focus_serial = serial;
510         }
511
512         pointer->focus = surface;
513         wl_signal_emit(&pointer->focus_signal, pointer);
514 }
515
516 static void
517 send_enter_to_resource_list(struct wl_list *list,
518                             struct weston_keyboard *keyboard,
519                             struct weston_surface *surface,
520                             uint32_t serial)
521 {
522         struct wl_resource *resource;
523
524         wl_resource_for_each(resource, list) {
525                 send_modifiers_to_resource(keyboard, resource, serial);
526                 wl_keyboard_send_enter(resource, serial,
527                                        surface->resource,
528                                        &keyboard->keys);
529         }
530 }
531
532 WL_EXPORT void
533 weston_keyboard_set_focus(struct weston_keyboard *keyboard,
534                           struct weston_surface *surface)
535 {
536         struct wl_resource *resource;
537         struct wl_display *display = keyboard->seat->compositor->wl_display;
538         uint32_t serial;
539         struct wl_list *focus_resource_list;
540
541         focus_resource_list = &keyboard->focus_resource_list;
542
543         if (!wl_list_empty(focus_resource_list) && keyboard->focus != surface) {
544                 serial = wl_display_next_serial(display);
545                 wl_resource_for_each(resource, focus_resource_list) {
546                         wl_keyboard_send_leave(resource, serial,
547                                         keyboard->focus->resource);
548                 }
549                 move_resources(&keyboard->resource_list, focus_resource_list);
550         }
551
552         if (find_resource_for_surface(&keyboard->resource_list, surface) &&
553             keyboard->focus != surface) {
554                 struct wl_client *surface_client =
555                         wl_resource_get_client(surface->resource);
556
557                 serial = wl_display_next_serial(display);
558
559                 move_resources_for_client(focus_resource_list,
560                                           &keyboard->resource_list,
561                                           surface_client);
562                 send_enter_to_resource_list(focus_resource_list,
563                                             keyboard,
564                                             surface,
565                                             serial);
566                 keyboard->focus_serial = serial;
567         }
568
569         keyboard->focus = surface;
570         wl_signal_emit(&keyboard->focus_signal, keyboard);
571 }
572
573 WL_EXPORT void
574 weston_keyboard_start_grab(struct weston_keyboard *keyboard,
575                            struct weston_keyboard_grab *grab)
576 {
577         keyboard->grab = grab;
578         grab->keyboard = keyboard;
579
580         /* XXX focus? */
581 }
582
583 WL_EXPORT void
584 weston_keyboard_end_grab(struct weston_keyboard *keyboard)
585 {
586         keyboard->grab = &keyboard->default_grab;
587 }
588
589 WL_EXPORT void
590 weston_pointer_start_grab(struct weston_pointer *pointer,
591                           struct weston_pointer_grab *grab)
592 {
593         pointer->grab = grab;
594         grab->pointer = pointer;
595         pointer->grab->interface->focus(pointer->grab);
596 }
597
598 WL_EXPORT void
599 weston_pointer_end_grab(struct weston_pointer *pointer)
600 {
601         pointer->grab = &pointer->default_grab;
602         pointer->grab->interface->focus(pointer->grab);
603 }
604
605 WL_EXPORT void
606 weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
607 {
608         touch->grab = grab;
609         grab->touch = touch;
610 }
611
612 WL_EXPORT void
613 weston_touch_end_grab(struct weston_touch *touch)
614 {
615         touch->grab = &touch->default_grab;
616 }
617
618 WL_EXPORT void
619 weston_pointer_clamp(struct weston_pointer *pointer, wl_fixed_t *fx, wl_fixed_t *fy)
620 {
621         struct weston_compositor *ec = pointer->seat->compositor;
622         struct weston_output *output, *prev = NULL;
623         int x, y, old_x, old_y, valid = 0;
624
625         x = wl_fixed_to_int(*fx);
626         y = wl_fixed_to_int(*fy);
627         old_x = wl_fixed_to_int(pointer->x);
628         old_y = wl_fixed_to_int(pointer->y);
629
630         wl_list_for_each(output, &ec->output_list, link) {
631                 if (pointer->seat->output && pointer->seat->output != output)
632                         continue;
633                 if (pixman_region32_contains_point(&output->region,
634                                                    x, y, NULL))
635                         valid = 1;
636                 if (pixman_region32_contains_point(&output->region,
637                                                    old_x, old_y, NULL))
638                         prev = output;
639         }
640
641         if (!prev)
642                 prev = pointer->seat->output;
643
644         if (prev && !valid) {
645                 if (x < prev->x)
646                         *fx = wl_fixed_from_int(prev->x);
647                 else if (x >= prev->x + prev->width)
648                         *fx = wl_fixed_from_int(prev->x +
649                                                 prev->width - 1);
650                 if (y < prev->y)
651                         *fy = wl_fixed_from_int(prev->y);
652                 else if (y >= prev->y + prev->height)
653                         *fy = wl_fixed_from_int(prev->y +
654                                                 prev->height - 1);
655         }
656 }
657
658 /* Takes absolute values */
659 static void
660 move_pointer(struct weston_seat *seat, wl_fixed_t x, wl_fixed_t y)
661 {
662         struct weston_compositor *ec = seat->compositor;
663         struct weston_pointer *pointer = seat->pointer;
664         struct weston_output *output;
665         int32_t ix, iy;
666
667         weston_pointer_clamp (pointer, &x, &y);
668
669         pointer->x = x;
670         pointer->y = y;
671
672         ix = wl_fixed_to_int(x);
673         iy = wl_fixed_to_int(y);
674
675         wl_list_for_each(output, &ec->output_list, link)
676                 if (output->zoom.active &&
677                     pixman_region32_contains_point(&output->region,
678                                                    ix, iy, NULL))
679                         weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
680
681         if (pointer->sprite) {
682                 weston_surface_set_position(pointer->sprite,
683                                             ix - pointer->hotspot_x,
684                                             iy - pointer->hotspot_y);
685                 weston_surface_schedule_repaint(pointer->sprite);
686         }
687 }
688
689 WL_EXPORT void
690 notify_motion(struct weston_seat *seat,
691               uint32_t time, wl_fixed_t dx, wl_fixed_t dy)
692 {
693         struct weston_compositor *ec = seat->compositor;
694         struct weston_pointer *pointer = seat->pointer;
695
696         weston_compositor_wake(ec);
697
698         move_pointer(seat, pointer->x + dx, pointer->y + dy);
699
700         pointer->grab->interface->focus(pointer->grab);
701         pointer->grab->interface->motion(pointer->grab, time);
702 }
703
704 WL_EXPORT void
705 notify_motion_absolute(struct weston_seat *seat,
706                        uint32_t time, wl_fixed_t x, wl_fixed_t y)
707 {
708         struct weston_compositor *ec = seat->compositor;
709         struct weston_pointer *pointer = seat->pointer;
710
711         weston_compositor_wake(ec);
712
713         move_pointer(seat, x, y);
714
715         pointer->grab->interface->focus(pointer->grab);
716         pointer->grab->interface->motion(pointer->grab, time);
717 }
718
719 WL_EXPORT void
720 weston_surface_activate(struct weston_surface *surface,
721                         struct weston_seat *seat)
722 {
723         struct weston_compositor *compositor = seat->compositor;
724
725         if (seat->keyboard) {
726                 weston_keyboard_set_focus(seat->keyboard, surface);
727                 wl_data_device_set_keyboard_focus(seat);
728         }
729
730         wl_signal_emit(&compositor->activate_signal, surface);
731 }
732
733 WL_EXPORT void
734 notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
735               enum wl_pointer_button_state state)
736 {
737         struct weston_compositor *compositor = seat->compositor;
738         struct weston_pointer *pointer = seat->pointer;
739         struct weston_surface *focus =
740                 (struct weston_surface *) pointer->focus;
741         uint32_t serial = wl_display_next_serial(compositor->wl_display);
742
743         if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
744                 if (compositor->ping_handler && focus)
745                         compositor->ping_handler(focus, serial);
746                 weston_compositor_idle_inhibit(compositor);
747                 if (pointer->button_count == 0) {
748                         pointer->grab_button = button;
749                         pointer->grab_time = time;
750                         pointer->grab_x = pointer->x;
751                         pointer->grab_y = pointer->y;
752                 }
753                 pointer->button_count++;
754         } else {
755                 weston_compositor_idle_release(compositor);
756                 pointer->button_count--;
757         }
758
759         weston_compositor_run_button_binding(compositor, seat, time, button,
760                                              state);
761
762         pointer->grab->interface->button(pointer->grab, time, button, state);
763
764         if (pointer->button_count == 1)
765                 pointer->grab_serial =
766                         wl_display_get_serial(compositor->wl_display);
767 }
768
769 WL_EXPORT void
770 notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
771             wl_fixed_t value)
772 {
773         struct weston_compositor *compositor = seat->compositor;
774         struct weston_pointer *pointer = seat->pointer;
775         struct weston_surface *focus =
776                 (struct weston_surface *) pointer->focus;
777         uint32_t serial = wl_display_next_serial(compositor->wl_display);
778         struct wl_resource *resource;
779         struct wl_list *resource_list;
780
781         if (compositor->ping_handler && focus)
782                 compositor->ping_handler(focus, serial);
783
784         weston_compositor_wake(compositor);
785
786         if (!value)
787                 return;
788
789         if (weston_compositor_run_axis_binding(compositor, seat,
790                                                    time, axis, value))
791                 return;
792
793         resource_list = &pointer->focus_resource_list;
794         wl_resource_for_each(resource, resource_list)
795                 wl_pointer_send_axis(resource, time, axis,
796                                      value);
797 }
798
799 #ifdef ENABLE_XKBCOMMON
800 WL_EXPORT void
801 notify_modifiers(struct weston_seat *seat, uint32_t serial)
802 {
803         struct weston_keyboard *keyboard = seat->keyboard;
804         struct weston_keyboard_grab *grab = keyboard->grab;
805         uint32_t mods_depressed, mods_latched, mods_locked, group;
806         uint32_t mods_lookup;
807         enum weston_led leds = 0;
808         int changed = 0;
809
810         /* Serialize and update our internal state, checking to see if it's
811          * different to the previous state. */
812         mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
813                                                   XKB_STATE_DEPRESSED);
814         mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
815                                                 XKB_STATE_LATCHED);
816         mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
817                                                XKB_STATE_LOCKED);
818         group = xkb_state_serialize_group(seat->xkb_state.state,
819                                           XKB_STATE_EFFECTIVE);
820
821         if (mods_depressed != seat->keyboard->modifiers.mods_depressed ||
822             mods_latched != seat->keyboard->modifiers.mods_latched ||
823             mods_locked != seat->keyboard->modifiers.mods_locked ||
824             group != seat->keyboard->modifiers.group)
825                 changed = 1;
826
827         seat->keyboard->modifiers.mods_depressed = mods_depressed;
828         seat->keyboard->modifiers.mods_latched = mods_latched;
829         seat->keyboard->modifiers.mods_locked = mods_locked;
830         seat->keyboard->modifiers.group = group;
831
832         /* And update the modifier_state for bindings. */
833         mods_lookup = mods_depressed | mods_latched;
834         seat->modifier_state = 0;
835         if (mods_lookup & (1 << seat->xkb_info->ctrl_mod))
836                 seat->modifier_state |= MODIFIER_CTRL;
837         if (mods_lookup & (1 << seat->xkb_info->alt_mod))
838                 seat->modifier_state |= MODIFIER_ALT;
839         if (mods_lookup & (1 << seat->xkb_info->super_mod))
840                 seat->modifier_state |= MODIFIER_SUPER;
841         if (mods_lookup & (1 << seat->xkb_info->shift_mod))
842                 seat->modifier_state |= MODIFIER_SHIFT;
843
844         /* Finally, notify the compositor that LEDs have changed. */
845         if (xkb_state_led_index_is_active(seat->xkb_state.state,
846                                           seat->xkb_info->num_led))
847                 leds |= LED_NUM_LOCK;
848         if (xkb_state_led_index_is_active(seat->xkb_state.state,
849                                           seat->xkb_info->caps_led))
850                 leds |= LED_CAPS_LOCK;
851         if (xkb_state_led_index_is_active(seat->xkb_state.state,
852                                           seat->xkb_info->scroll_led))
853                 leds |= LED_SCROLL_LOCK;
854         if (leds != seat->xkb_state.leds && seat->led_update)
855                 seat->led_update(seat, leds);
856         seat->xkb_state.leds = leds;
857
858         if (changed) {
859                 grab->interface->modifiers(grab,
860                                            serial,
861                                            keyboard->modifiers.mods_depressed,
862                                            keyboard->modifiers.mods_latched,
863                                            keyboard->modifiers.mods_locked,
864                                            keyboard->modifiers.group);
865         }
866 }
867
868 static void
869 update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
870                       enum wl_keyboard_key_state state)
871 {
872         enum xkb_key_direction direction;
873
874         /* Keyboard modifiers don't exist in raw keyboard mode */
875         if (!seat->compositor->use_xkbcommon)
876                 return;
877
878         if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
879                 direction = XKB_KEY_DOWN;
880         else
881                 direction = XKB_KEY_UP;
882
883         /* Offset the keycode by 8, as the evdev XKB rules reflect X's
884          * broken keycode system, which starts at 8. */
885         xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
886
887         notify_modifiers(seat, serial);
888 }
889 #else
890 WL_EXPORT void
891 notify_modifiers(struct weston_seat *seat, uint32_t serial)
892 {
893 }
894
895 static void
896 update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
897                       enum wl_keyboard_key_state state)
898 {
899 }
900 #endif
901
902 WL_EXPORT void
903 notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
904            enum wl_keyboard_key_state state,
905            enum weston_key_state_update update_state)
906 {
907         struct weston_compositor *compositor = seat->compositor;
908         struct weston_keyboard *keyboard = seat->keyboard;
909         struct weston_surface *focus =
910                 (struct weston_surface *) keyboard->focus;
911         struct weston_keyboard_grab *grab = keyboard->grab;
912         uint32_t serial = wl_display_next_serial(compositor->wl_display);
913         uint32_t *k, *end;
914
915         if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
916                 if (compositor->ping_handler && focus)
917                         compositor->ping_handler(focus, serial);
918
919                 weston_compositor_idle_inhibit(compositor);
920                 keyboard->grab_key = key;
921                 keyboard->grab_time = time;
922         } else {
923                 weston_compositor_idle_release(compositor);
924         }
925
926         end = keyboard->keys.data + keyboard->keys.size;
927         for (k = keyboard->keys.data; k < end; k++) {
928                 if (*k == key) {
929                         /* Ignore server-generated repeats. */
930                         if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
931                                 return;
932                         *k = *--end;
933                 }
934         }
935         keyboard->keys.size = (void *) end - keyboard->keys.data;
936         if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
937                 k = wl_array_add(&keyboard->keys, sizeof *k);
938                 *k = key;
939         }
940
941         if (grab == &keyboard->default_grab ||
942             grab == &keyboard->input_method_grab) {
943                 weston_compositor_run_key_binding(compositor, seat, time, key,
944                                                   state);
945                 grab = keyboard->grab;
946         }
947
948         grab->interface->key(grab, time, key, state);
949
950         if (update_state == STATE_UPDATE_AUTOMATIC) {
951                 update_modifier_state(seat,
952                                       wl_display_get_serial(compositor->wl_display),
953                                       key,
954                                       state);
955         }
956 }
957
958 WL_EXPORT void
959 notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
960                      wl_fixed_t x, wl_fixed_t y)
961 {
962         struct weston_compositor *compositor = seat->compositor;
963
964         if (output) {
965                 move_pointer(seat, x, y);
966                 compositor->focus = 1;
967         } else {
968                 compositor->focus = 0;
969                 /* FIXME: We should call weston_pointer_set_focus(seat,
970                  * NULL) here, but somehow that breaks re-entry... */
971         }
972 }
973
974 static void
975 destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
976 {
977         struct weston_seat *ws;
978
979         ws = container_of(listener, struct weston_seat,
980                           saved_kbd_focus_listener);
981
982         ws->saved_kbd_focus = NULL;
983 }
984
985 WL_EXPORT void
986 notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
987                          enum weston_key_state_update update_state)
988 {
989         struct weston_compositor *compositor = seat->compositor;
990         struct weston_keyboard *keyboard = seat->keyboard;
991         struct weston_surface *surface;
992         uint32_t *k, serial;
993
994         serial = wl_display_next_serial(compositor->wl_display);
995         wl_array_copy(&keyboard->keys, keys);
996         wl_array_for_each(k, &keyboard->keys) {
997                 weston_compositor_idle_inhibit(compositor);
998                 if (update_state == STATE_UPDATE_AUTOMATIC)
999                         update_modifier_state(seat, serial, *k,
1000                                               WL_KEYBOARD_KEY_STATE_PRESSED);
1001         }
1002
1003         /* Run key bindings after we've updated the state. */
1004         wl_array_for_each(k, &keyboard->keys) {
1005                 weston_compositor_run_key_binding(compositor, seat, 0, *k,
1006                                                   WL_KEYBOARD_KEY_STATE_PRESSED);
1007         }
1008
1009         surface = seat->saved_kbd_focus;
1010
1011         if (surface) {
1012                 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1013                 weston_keyboard_set_focus(keyboard, surface);
1014                 seat->saved_kbd_focus = NULL;
1015         }
1016 }
1017
1018 WL_EXPORT void
1019 notify_keyboard_focus_out(struct weston_seat *seat)
1020 {
1021         struct weston_compositor *compositor = seat->compositor;
1022         struct weston_keyboard *keyboard = seat->keyboard;
1023         uint32_t *k, serial;
1024
1025         serial = wl_display_next_serial(compositor->wl_display);
1026         wl_array_for_each(k, &keyboard->keys) {
1027                 weston_compositor_idle_release(compositor);
1028                 update_modifier_state(seat, serial, *k,
1029                                       WL_KEYBOARD_KEY_STATE_RELEASED);
1030         }
1031
1032         seat->modifier_state = 0;
1033
1034         if (keyboard->focus) {
1035                 seat->saved_kbd_focus = keyboard->focus;
1036                 seat->saved_kbd_focus_listener.notify =
1037                         destroy_device_saved_kbd_focus;
1038                 wl_signal_add(&keyboard->focus->destroy_signal,
1039                               &seat->saved_kbd_focus_listener);
1040         }
1041
1042         weston_keyboard_set_focus(keyboard, NULL);
1043         /* FIXME: We really need keyboard grab cancel here to
1044          * let the grab shut down properly.  As it is we leak
1045          * the grab data. */
1046         weston_keyboard_end_grab(keyboard);
1047 }
1048
1049 WL_EXPORT void
1050 weston_touch_set_focus(struct weston_seat *seat, struct weston_surface *surface)
1051 {
1052         struct wl_list *focus_resource_list;
1053
1054         focus_resource_list = &seat->touch->focus_resource_list;
1055
1056         if (seat->touch->focus == surface)
1057                 return;
1058
1059         if (!wl_list_empty(focus_resource_list)) {
1060                 move_resources(&seat->touch->resource_list,
1061                                focus_resource_list);
1062         }
1063
1064         if (surface) {
1065                 struct wl_client *surface_client =
1066                         wl_resource_get_client(surface->resource);
1067                 move_resources_for_client(focus_resource_list,
1068                                           &seat->touch->resource_list,
1069                                           surface_client);
1070         }
1071         seat->touch->focus = surface;
1072 }
1073
1074 /**
1075  * notify_touch - emulates button touches and notifies surfaces accordingly.
1076  *
1077  * It assumes always the correct cycle sequence until it gets here: touch_down
1078  * → touch_update → ... → touch_update → touch_end. The driver is responsible
1079  * for sending along such order.
1080  *
1081  */
1082 WL_EXPORT void
1083 notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
1084              wl_fixed_t x, wl_fixed_t y, int touch_type)
1085 {
1086         struct weston_compositor *ec = seat->compositor;
1087         struct weston_touch *touch = seat->touch;
1088         struct weston_touch_grab *grab = touch->grab;
1089         struct weston_surface *es;
1090         wl_fixed_t sx, sy;
1091
1092         /* Update grab's global coordinates. */
1093         if (touch_id == touch->grab_touch_id && touch_type != WL_TOUCH_UP) {
1094                 touch->grab_x = x;
1095                 touch->grab_y = y;
1096         }
1097
1098         switch (touch_type) {
1099         case WL_TOUCH_DOWN:
1100                 weston_compositor_idle_inhibit(ec);
1101
1102                 seat->num_tp++;
1103
1104                 /* the first finger down picks the surface, and all further go
1105                  * to that surface for the remainder of the touch session i.e.
1106                  * until all touch points are up again. */
1107                 if (seat->num_tp == 1) {
1108                         es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
1109                         weston_touch_set_focus(seat, es);
1110                 } else if (touch->focus) {
1111                         es = (struct weston_surface *) touch->focus;
1112                         weston_surface_from_global_fixed(es, x, y, &sx, &sy);
1113                 } else {
1114                         /* Unexpected condition: We have non-initial touch but
1115                          * there is no focused surface.
1116                          */
1117                         weston_log("touch event received with %d points down"
1118                                    "but no surface focused\n", seat->num_tp);
1119                         return;
1120                 }
1121
1122                 grab->interface->down(grab, time, touch_id, sx, sy);
1123                 if (seat->num_tp == 1) {
1124                         touch->grab_serial =
1125                                 wl_display_get_serial(ec->wl_display);
1126                         touch->grab_touch_id = touch_id;
1127                         touch->grab_time = time;
1128                         touch->grab_x = x;
1129                         touch->grab_y = y;
1130                 }
1131
1132                 break;
1133         case WL_TOUCH_MOTION:
1134                 es = (struct weston_surface *) touch->focus;
1135                 if (!es)
1136                         break;
1137
1138                 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
1139                 grab->interface->motion(grab, time, touch_id, sx, sy);
1140                 break;
1141         case WL_TOUCH_UP:
1142                 weston_compositor_idle_release(ec);
1143                 seat->num_tp--;
1144
1145                 grab->interface->up(grab, time, touch_id);
1146                 if (seat->num_tp == 0)
1147                         weston_touch_set_focus(seat, NULL);
1148                 break;
1149         }
1150
1151         weston_compositor_run_touch_binding(ec, seat, time, touch_type);
1152 }
1153
1154 static void
1155 pointer_cursor_surface_configure(struct weston_surface *es,
1156                                  int32_t dx, int32_t dy, int32_t width, int32_t height)
1157 {
1158         struct weston_pointer *pointer = es->configure_private;
1159         int x, y;
1160
1161         if (width == 0)
1162                 return;
1163
1164         assert(es == pointer->sprite);
1165
1166         pointer->hotspot_x -= dx;
1167         pointer->hotspot_y -= dy;
1168
1169         x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
1170         y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
1171
1172         weston_surface_configure(pointer->sprite, x, y, width, height);
1173
1174         empty_region(&es->pending.input);
1175
1176         if (!weston_surface_is_mapped(es)) {
1177                 wl_list_insert(&es->compositor->cursor_layer.surface_list,
1178                                &es->layer_link);
1179                 weston_surface_update_transform(es);
1180         }
1181 }
1182
1183 static void
1184 pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
1185                    uint32_t serial, struct wl_resource *surface_resource,
1186                    int32_t x, int32_t y)
1187 {
1188         struct weston_pointer *pointer = wl_resource_get_user_data(resource);
1189         struct weston_surface *surface = NULL;
1190
1191         if (surface_resource)
1192                 surface = wl_resource_get_user_data(surface_resource);
1193
1194         if (pointer->focus == NULL)
1195                 return;
1196         /* pointer->focus->resource can be NULL. Surfaces like the
1197         black_surface used in shell.c for fullscreen don't have
1198         a resource, but can still have focus */
1199         if (pointer->focus->resource == NULL)
1200                 return;
1201         if (wl_resource_get_client(pointer->focus->resource) != client)
1202                 return;
1203         if (pointer->focus_serial - serial > UINT32_MAX / 2)
1204                 return;
1205
1206         if (surface && surface != pointer->sprite) {
1207                 if (surface->configure) {
1208                         wl_resource_post_error(surface->resource,
1209                                                WL_DISPLAY_ERROR_INVALID_OBJECT,
1210                                                "surface->configure already "
1211                                                "set");
1212                         return;
1213                 }
1214         }
1215
1216         if (pointer->sprite)
1217                 pointer_unmap_sprite(pointer);
1218
1219         if (!surface)
1220                 return;
1221
1222         wl_signal_add(&surface->destroy_signal,
1223                       &pointer->sprite_destroy_listener);
1224
1225         surface->configure = pointer_cursor_surface_configure;
1226         surface->configure_private = pointer;
1227         pointer->sprite = surface;
1228         pointer->hotspot_x = x;
1229         pointer->hotspot_y = y;
1230
1231         if (surface->buffer_ref.buffer)
1232                 pointer_cursor_surface_configure(surface, 0, 0, weston_surface_buffer_width(surface),
1233                                                                 weston_surface_buffer_height(surface));
1234 }
1235
1236 static void
1237 pointer_release(struct wl_client *client, struct wl_resource *resource)
1238 {
1239         wl_resource_destroy(resource);
1240 }
1241
1242 static const struct wl_pointer_interface pointer_interface = {
1243         pointer_set_cursor,
1244         pointer_release
1245 };
1246
1247 static void
1248 seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
1249                  uint32_t id)
1250 {
1251         struct weston_seat *seat = wl_resource_get_user_data(resource);
1252         struct wl_resource *cr;
1253
1254         if (!seat->pointer)
1255                 return;
1256
1257         cr = wl_resource_create(client, &wl_pointer_interface,
1258                                 wl_resource_get_version(resource), id);
1259         if (cr == NULL) {
1260                 wl_client_post_no_memory(client);
1261                 return;
1262         }
1263
1264         /* May be moved to focused list later by either
1265          * weston_pointer_set_focus or directly if this client is already
1266          * focused */
1267         wl_list_insert(&seat->pointer->resource_list, wl_resource_get_link(cr));
1268         wl_resource_set_implementation(cr, &pointer_interface, seat->pointer,
1269                                        unbind_resource);
1270
1271         if (seat->pointer->focus && seat->pointer->focus->resource &&
1272             wl_resource_get_client(seat->pointer->focus->resource) == client) {
1273                 struct weston_surface *surface;
1274                 wl_fixed_t sx, sy;
1275
1276                 surface = (struct weston_surface *) seat->pointer->focus;
1277                 weston_surface_from_global_fixed(surface,
1278                                                  seat->pointer->x,
1279                                                  seat->pointer->y,
1280                                                  &sx,
1281                                                  &sy);
1282
1283                 wl_list_remove(wl_resource_get_link(cr));
1284                 wl_list_insert(&seat->pointer->focus_resource_list,
1285                                wl_resource_get_link(cr));
1286                 wl_pointer_send_enter(cr,
1287                                       seat->pointer->focus_serial,
1288                                       surface->resource,
1289                                       sx, sy);
1290         }
1291 }
1292
1293 static void
1294 keyboard_release(struct wl_client *client, struct wl_resource *resource)
1295 {
1296         wl_resource_destroy(resource);
1297 }
1298
1299 static const struct wl_keyboard_interface keyboard_interface = {
1300         keyboard_release
1301 };
1302
1303 static int
1304 should_send_modifiers_to_client(struct weston_seat *seat,
1305                                 struct wl_client *client)
1306 {
1307         if (seat->keyboard &&
1308             seat->keyboard->focus &&
1309             wl_resource_get_client(seat->keyboard->focus->resource) == client)
1310                 return 1;
1311
1312         if (seat->pointer &&
1313             seat->pointer->focus &&
1314             wl_resource_get_client(seat->pointer->focus->resource) == client)
1315                 return 1;
1316
1317         return 0;
1318 }
1319
1320 static void
1321 seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
1322                   uint32_t id)
1323 {
1324         struct weston_seat *seat = wl_resource_get_user_data(resource);
1325         struct wl_resource *cr;
1326
1327         if (!seat->keyboard)
1328                 return;
1329
1330         cr = wl_resource_create(client, &wl_keyboard_interface,
1331                                 wl_resource_get_version(resource), id);
1332         if (cr == NULL) {
1333                 wl_client_post_no_memory(client);
1334                 return;
1335         }
1336
1337         /* May be moved to focused list later by either
1338          * weston_keyboard_set_focus or directly if this client is already
1339          * focused */
1340         wl_list_insert(&seat->keyboard->resource_list, wl_resource_get_link(cr));
1341         wl_resource_set_implementation(cr, &keyboard_interface,
1342                                        seat, unbind_resource);
1343
1344         if (seat->compositor->use_xkbcommon) {
1345                 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
1346                                         seat->xkb_info->keymap_fd,
1347                                         seat->xkb_info->keymap_size);
1348         } else {
1349                 int null_fd = open("/dev/null", O_RDONLY);
1350                 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP,
1351                                         null_fd,
1352                                         0);
1353                 close(null_fd);
1354         }
1355
1356         if (should_send_modifiers_to_client(seat, client)) {
1357                 send_modifiers_to_resource(seat->keyboard,
1358                                            cr,
1359                                            seat->keyboard->focus_serial);
1360         }
1361
1362         if (seat->keyboard->focus &&
1363             wl_resource_get_client(seat->keyboard->focus->resource) == client) {
1364                 struct weston_surface *surface =
1365                         (struct weston_surface *) seat->keyboard->focus;
1366
1367                 wl_list_remove(wl_resource_get_link(cr));
1368                 wl_list_insert(&seat->keyboard->focus_resource_list,
1369                                wl_resource_get_link(cr));
1370                 wl_keyboard_send_enter(cr,
1371                                        seat->keyboard->focus_serial,
1372                                        surface->resource,
1373                                        &seat->keyboard->keys);
1374
1375                 /* If this is the first keyboard resource for this
1376                  * client... */
1377                 if (seat->keyboard->focus_resource_list.prev ==
1378                     wl_resource_get_link(cr))
1379                         wl_data_device_set_keyboard_focus(seat);
1380         }
1381 }
1382
1383 static void
1384 touch_release(struct wl_client *client, struct wl_resource *resource)
1385 {
1386         wl_resource_destroy(resource);
1387 }
1388
1389 static const struct wl_touch_interface touch_interface = {
1390         touch_release
1391 };
1392
1393 static void
1394 seat_get_touch(struct wl_client *client, struct wl_resource *resource,
1395                uint32_t id)
1396 {
1397         struct weston_seat *seat = wl_resource_get_user_data(resource);
1398         struct wl_resource *cr;
1399
1400         if (!seat->touch)
1401                 return;
1402
1403         cr = wl_resource_create(client, &wl_touch_interface,
1404                                 wl_resource_get_version(resource), id);
1405         if (cr == NULL) {
1406                 wl_client_post_no_memory(client);
1407                 return;
1408         }
1409
1410         if (seat->touch->focus &&
1411             wl_resource_get_client(seat->touch->focus->resource) == client) {
1412                 wl_list_insert(&seat->touch->resource_list,
1413                                wl_resource_get_link(cr));
1414         } else {
1415                 wl_list_insert(&seat->touch->focus_resource_list,
1416                                wl_resource_get_link(cr));
1417         }
1418         wl_resource_set_implementation(cr, &touch_interface,
1419                                        seat, unbind_resource);
1420 }
1421
1422 static const struct wl_seat_interface seat_interface = {
1423         seat_get_pointer,
1424         seat_get_keyboard,
1425         seat_get_touch,
1426 };
1427
1428 static void
1429 bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1430 {
1431         struct weston_seat *seat = data;
1432         struct wl_resource *resource;
1433         enum wl_seat_capability caps = 0;
1434
1435         resource = wl_resource_create(client,
1436                                       &wl_seat_interface, MIN(version, 3), id);
1437         wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
1438         wl_resource_set_implementation(resource, &seat_interface, data,
1439                                        unbind_resource);
1440
1441         if (seat->pointer)
1442                 caps |= WL_SEAT_CAPABILITY_POINTER;
1443         if (seat->keyboard)
1444                 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
1445         if (seat->touch)
1446                 caps |= WL_SEAT_CAPABILITY_TOUCH;
1447
1448         wl_seat_send_capabilities(resource, caps);
1449         if (version >= 2)
1450                 wl_seat_send_name(resource, seat->seat_name);
1451 }
1452
1453 #ifdef ENABLE_XKBCOMMON
1454 int
1455 weston_compositor_xkb_init(struct weston_compositor *ec,
1456                            struct xkb_rule_names *names)
1457 {
1458         ec->use_xkbcommon = 1;
1459
1460         if (ec->xkb_context == NULL) {
1461                 ec->xkb_context = xkb_context_new(0);
1462                 if (ec->xkb_context == NULL) {
1463                         weston_log("failed to create XKB context\n");
1464                         return -1;
1465                 }
1466         }
1467
1468         if (names)
1469                 ec->xkb_names = *names;
1470         if (!ec->xkb_names.rules)
1471                 ec->xkb_names.rules = strdup("evdev");
1472         if (!ec->xkb_names.model)
1473                 ec->xkb_names.model = strdup("pc105");
1474         if (!ec->xkb_names.layout)
1475                 ec->xkb_names.layout = strdup("us");
1476
1477         return 0;
1478 }
1479
1480 static void
1481 weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
1482 {
1483         if (--xkb_info->ref_count > 0)
1484                 return;
1485
1486         if (xkb_info->keymap)
1487                 xkb_map_unref(xkb_info->keymap);
1488
1489         if (xkb_info->keymap_area)
1490                 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
1491         if (xkb_info->keymap_fd >= 0)
1492                 close(xkb_info->keymap_fd);
1493         free(xkb_info);
1494 }
1495
1496 void
1497 weston_compositor_xkb_destroy(struct weston_compositor *ec)
1498 {
1499         /*
1500          * If we're operating in raw keyboard mode, we never initialized
1501          * libxkbcommon so there's no cleanup to do either.
1502          */
1503         if (!ec->use_xkbcommon)
1504                 return;
1505
1506         free((char *) ec->xkb_names.rules);
1507         free((char *) ec->xkb_names.model);
1508         free((char *) ec->xkb_names.layout);
1509         free((char *) ec->xkb_names.variant);
1510         free((char *) ec->xkb_names.options);
1511
1512         if (ec->xkb_info)
1513                 weston_xkb_info_destroy(ec->xkb_info);
1514         xkb_context_unref(ec->xkb_context);
1515 }
1516
1517 static struct weston_xkb_info *
1518 weston_xkb_info_create(struct xkb_keymap *keymap)
1519 {
1520         struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
1521         if (xkb_info == NULL)
1522                 return NULL;
1523
1524         xkb_info->keymap = xkb_map_ref(keymap);
1525         xkb_info->ref_count = 1;
1526
1527         char *keymap_str;
1528
1529         xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
1530                                                     XKB_MOD_NAME_SHIFT);
1531         xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
1532                                                    XKB_MOD_NAME_CAPS);
1533         xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
1534                                                    XKB_MOD_NAME_CTRL);
1535         xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
1536                                                   XKB_MOD_NAME_ALT);
1537         xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
1538         xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
1539         xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
1540                                                     XKB_MOD_NAME_LOGO);
1541         xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
1542
1543         xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
1544                                                   XKB_LED_NAME_NUM);
1545         xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
1546                                                    XKB_LED_NAME_CAPS);
1547         xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
1548                                                      XKB_LED_NAME_SCROLL);
1549
1550         keymap_str = xkb_map_get_as_string(xkb_info->keymap);
1551         if (keymap_str == NULL) {
1552                 weston_log("failed to get string version of keymap\n");
1553                 goto err_keymap;
1554         }
1555         xkb_info->keymap_size = strlen(keymap_str) + 1;
1556
1557         xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
1558         if (xkb_info->keymap_fd < 0) {
1559                 weston_log("creating a keymap file for %lu bytes failed: %m\n",
1560                         (unsigned long) xkb_info->keymap_size);
1561                 goto err_keymap_str;
1562         }
1563
1564         xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
1565                                      PROT_READ | PROT_WRITE,
1566                                      MAP_SHARED, xkb_info->keymap_fd, 0);
1567         if (xkb_info->keymap_area == MAP_FAILED) {
1568                 weston_log("failed to mmap() %lu bytes\n",
1569                         (unsigned long) xkb_info->keymap_size);
1570                 goto err_dev_zero;
1571         }
1572         strcpy(xkb_info->keymap_area, keymap_str);
1573         free(keymap_str);
1574
1575         return xkb_info;
1576
1577 err_dev_zero:
1578         close(xkb_info->keymap_fd);
1579 err_keymap_str:
1580         free(keymap_str);
1581 err_keymap:
1582         xkb_map_unref(xkb_info->keymap);
1583         free(xkb_info);
1584         return NULL;
1585 }
1586
1587 static int
1588 weston_compositor_build_global_keymap(struct weston_compositor *ec)
1589 {
1590         struct xkb_keymap *keymap;
1591
1592         if (ec->xkb_info != NULL)
1593                 return 0;
1594
1595         keymap = xkb_map_new_from_names(ec->xkb_context,
1596                                         &ec->xkb_names,
1597                                         0);
1598         if (keymap == NULL) {
1599                 weston_log("failed to compile global XKB keymap\n");
1600                 weston_log("  tried rules %s, model %s, layout %s, variant %s, "
1601                         "options %s\n",
1602                         ec->xkb_names.rules, ec->xkb_names.model,
1603                         ec->xkb_names.layout, ec->xkb_names.variant,
1604                         ec->xkb_names.options);
1605                 return -1;
1606         }
1607
1608         ec->xkb_info = weston_xkb_info_create(keymap);
1609         if (ec->xkb_info == NULL)
1610                 return -1;
1611
1612         return 0;
1613 }
1614 #else
1615 int
1616 weston_compositor_xkb_init(struct weston_compositor *ec,
1617                            struct xkb_rule_names *names)
1618 {
1619         return 0;
1620 }
1621
1622 void
1623 weston_compositor_xkb_destroy(struct weston_compositor *ec)
1624 {
1625 }
1626 #endif
1627
1628 WL_EXPORT int
1629 weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
1630 {
1631         struct weston_keyboard *keyboard;
1632
1633         if (seat->keyboard) {
1634                 seat->keyboard_device_count += 1;
1635                 if (seat->keyboard_device_count == 1)
1636                         seat_send_updated_caps(seat);
1637                 return 0;
1638         }
1639
1640 #ifdef ENABLE_XKBCOMMON
1641         if (seat->compositor->use_xkbcommon) {
1642                 if (keymap != NULL) {
1643                         seat->xkb_info = weston_xkb_info_create(keymap);
1644                         if (seat->xkb_info == NULL)
1645                                 return -1;
1646                 } else {
1647                         if (weston_compositor_build_global_keymap(seat->compositor) < 0)
1648                                 return -1;
1649                         seat->xkb_info = seat->compositor->xkb_info;
1650                         seat->xkb_info->ref_count++;
1651                 }
1652
1653                 seat->xkb_state.state = xkb_state_new(seat->xkb_info->keymap);
1654                 if (seat->xkb_state.state == NULL) {
1655                         weston_log("failed to initialise XKB state\n");
1656                         return -1;
1657                 }
1658
1659                 seat->xkb_state.leds = 0;
1660         }
1661 #endif
1662
1663         keyboard = weston_keyboard_create();
1664         if (keyboard == NULL) {
1665                 weston_log("failed to allocate weston keyboard struct\n");
1666                 return -1;
1667         }
1668
1669         seat->keyboard = keyboard;
1670         seat->keyboard_device_count = 1;
1671         keyboard->seat = seat;
1672
1673         seat_send_updated_caps(seat);
1674
1675         return 0;
1676 }
1677
1678 WL_EXPORT void
1679 weston_seat_release_keyboard(struct weston_seat *seat)
1680 {
1681         seat->keyboard_device_count--;
1682         if (seat->keyboard_device_count == 0) {
1683                 weston_keyboard_set_focus(seat->keyboard, NULL);
1684                 seat_send_updated_caps(seat);
1685         }
1686 }
1687
1688 WL_EXPORT void
1689 weston_seat_init_pointer(struct weston_seat *seat)
1690 {
1691         struct weston_pointer *pointer;
1692
1693         if (seat->pointer) {
1694                 seat->pointer_device_count += 1;
1695                 if (seat->pointer_device_count == 1)
1696                         seat_send_updated_caps(seat);
1697                 return;
1698         }
1699
1700         pointer = weston_pointer_create();
1701         if (pointer == NULL)
1702                 return;
1703
1704         seat->pointer = pointer;
1705         seat->pointer_device_count = 1;
1706         pointer->seat = seat;
1707
1708         seat_send_updated_caps(seat);
1709 }
1710
1711 WL_EXPORT void
1712 weston_seat_release_pointer(struct weston_seat *seat)
1713 {
1714         struct weston_pointer *pointer = seat->pointer;
1715
1716         seat->pointer_device_count--;
1717         if (seat->pointer_device_count == 0) {
1718                 weston_pointer_set_focus(pointer, NULL,
1719                                          wl_fixed_from_int(0),
1720                                          wl_fixed_from_int(0));
1721
1722                 if (pointer->sprite)
1723                         pointer_unmap_sprite(pointer);
1724
1725                 seat_send_updated_caps(seat);
1726         }
1727 }
1728
1729 WL_EXPORT void
1730 weston_seat_init_touch(struct weston_seat *seat)
1731 {
1732         struct weston_touch *touch;
1733
1734         if (seat->touch) {
1735                 seat->touch_device_count += 1;
1736                 if (seat->touch_device_count == 1)
1737                         seat_send_updated_caps(seat);
1738                 return;
1739         }
1740
1741         touch = weston_touch_create();
1742         if (touch == NULL)
1743                 return;
1744
1745         seat->touch = touch;
1746         seat->touch_device_count = 1;
1747         touch->seat = seat;
1748
1749         seat_send_updated_caps(seat);
1750 }
1751
1752 WL_EXPORT void
1753 weston_seat_release_touch(struct weston_seat *seat)
1754 {
1755         seat->touch_device_count--;
1756         if (seat->touch_device_count == 0) {
1757                 weston_touch_set_focus(seat, NULL);
1758                 seat_send_updated_caps(seat);
1759         }
1760 }
1761
1762 WL_EXPORT void
1763 weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
1764                  const char *seat_name)
1765 {
1766         memset(seat, 0, sizeof *seat);
1767
1768         seat->selection_data_source = NULL;
1769         wl_list_init(&seat->base_resource_list);
1770         wl_signal_init(&seat->selection_signal);
1771         wl_list_init(&seat->drag_resource_list);
1772         wl_signal_init(&seat->destroy_signal);
1773
1774         seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 3,
1775                                         seat, bind_seat);
1776
1777         seat->compositor = ec;
1778         seat->modifier_state = 0;
1779         seat->num_tp = 0;
1780         seat->seat_name = strdup(seat_name);
1781
1782         wl_list_insert(ec->seat_list.prev, &seat->link);
1783
1784         clipboard_create(seat);
1785
1786         wl_signal_emit(&ec->seat_created_signal, seat);
1787 }
1788
1789 WL_EXPORT void
1790 weston_seat_release(struct weston_seat *seat)
1791 {
1792         wl_list_remove(&seat->link);
1793
1794 #ifdef ENABLE_XKBCOMMON
1795         if (seat->compositor->use_xkbcommon) {
1796                 if (seat->xkb_state.state != NULL)
1797                         xkb_state_unref(seat->xkb_state.state);
1798                 if (seat->xkb_info)
1799                         weston_xkb_info_destroy(seat->xkb_info);
1800         }
1801 #endif
1802
1803         if (seat->pointer)
1804                 weston_pointer_destroy(seat->pointer);
1805         if (seat->keyboard)
1806                 weston_keyboard_destroy(seat->keyboard);
1807         if (seat->touch)
1808                 weston_touch_destroy(seat->touch);
1809
1810         free (seat->seat_name);
1811
1812         wl_global_destroy(seat->global);
1813
1814         wl_signal_emit(&seat->destroy_signal, seat);
1815 }