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