2 * Copyright © 2013 Intel Corporation
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.
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.
34 #include "../shared/os-compatibility.h"
35 #include "compositor.h"
38 empty_region(pixman_region32_t *region)
40 pixman_region32_fini(region);
41 pixman_region32_init(region);
44 static void unbind_resource(struct wl_resource *resource)
46 wl_list_remove(wl_resource_get_link(resource));
50 weston_seat_repick(struct weston_seat *seat)
52 const struct weston_pointer *pointer = seat->pointer;
57 pointer->grab->interface->focus(seat->pointer->grab);
61 weston_compositor_idle_inhibit(struct weston_compositor *compositor)
63 weston_compositor_wake(compositor);
64 compositor->idle_inhibit++;
68 weston_compositor_idle_release(struct weston_compositor *compositor)
70 compositor->idle_inhibit--;
71 weston_compositor_wake(compositor);
75 pointer_focus_view_destroyed(struct wl_listener *listener, void *data)
77 struct weston_pointer *pointer =
78 container_of(listener, struct weston_pointer,
81 weston_pointer_set_focus(pointer, NULL, 0, 0);
85 pointer_focus_resource_destroyed(struct wl_listener *listener, void *data)
87 struct weston_pointer *pointer =
88 container_of(listener, struct weston_pointer,
89 focus_resource_listener);
91 weston_pointer_set_focus(pointer, NULL, 0, 0);
95 keyboard_focus_resource_destroyed(struct wl_listener *listener, void *data)
97 struct weston_keyboard *keyboard =
98 container_of(listener, struct weston_keyboard,
99 focus_resource_listener);
101 weston_keyboard_set_focus(keyboard, NULL);
105 touch_focus_view_destroyed(struct wl_listener *listener, void *data)
107 struct weston_touch *touch =
108 container_of(listener, struct weston_touch,
109 focus_view_listener);
111 weston_touch_set_focus(touch->seat, NULL);
115 touch_focus_resource_destroyed(struct wl_listener *listener, void *data)
117 struct weston_touch *touch =
118 container_of(listener, struct weston_touch,
119 focus_resource_listener);
121 weston_touch_set_focus(touch->seat, NULL);
125 move_resources(struct wl_list *destination, struct wl_list *source)
127 wl_list_insert_list(destination, source);
128 wl_list_init(source);
132 move_resources_for_client(struct wl_list *destination,
133 struct wl_list *source,
134 struct wl_client *client)
136 struct wl_resource *resource, *tmp;
137 wl_resource_for_each_safe(resource, tmp, source) {
138 if (wl_resource_get_client(resource) == client) {
139 wl_list_remove(wl_resource_get_link(resource));
140 wl_list_insert(destination,
141 wl_resource_get_link(resource));
147 default_grab_pointer_focus(struct weston_pointer_grab *grab)
149 struct weston_pointer *pointer = grab->pointer;
150 struct weston_view *view;
153 if (pointer->button_count > 0)
156 view = weston_compositor_pick_view(pointer->seat->compositor,
157 pointer->x, pointer->y,
160 if (pointer->focus != view || pointer->sx != sx || pointer->sy != sy)
161 weston_pointer_set_focus(pointer, view, sx, sy);
165 default_grab_pointer_motion(struct weston_pointer_grab *grab, uint32_t time,
166 wl_fixed_t x, wl_fixed_t y)
168 struct weston_pointer *pointer = grab->pointer;
169 struct wl_list *resource_list;
170 struct wl_resource *resource;
173 weston_view_from_global_fixed(pointer->focus, x, y,
174 &pointer->sx, &pointer->sy);
176 weston_pointer_move(pointer, x, y);
178 resource_list = &pointer->focus_resource_list;
179 wl_resource_for_each(resource, resource_list) {
180 wl_pointer_send_motion(resource, time,
181 pointer->sx, pointer->sy);
186 default_grab_pointer_button(struct weston_pointer_grab *grab,
187 uint32_t time, uint32_t button, uint32_t state_w)
189 struct weston_pointer *pointer = grab->pointer;
190 struct weston_compositor *compositor = pointer->seat->compositor;
191 struct weston_view *view;
192 struct wl_resource *resource;
194 enum wl_pointer_button_state state = state_w;
195 struct wl_display *display = compositor->wl_display;
197 struct wl_list *resource_list;
199 resource_list = &pointer->focus_resource_list;
200 if (!wl_list_empty(resource_list)) {
201 serial = wl_display_next_serial(display);
202 wl_resource_for_each(resource, resource_list)
203 wl_pointer_send_button(resource,
210 if (pointer->button_count == 0 &&
211 state == WL_POINTER_BUTTON_STATE_RELEASED) {
212 view = weston_compositor_pick_view(compositor,
213 pointer->x, pointer->y,
216 weston_pointer_set_focus(pointer, view, sx, sy);
221 default_grab_pointer_cancel(struct weston_pointer_grab *grab)
225 static const struct weston_pointer_grab_interface
226 default_pointer_grab_interface = {
227 default_grab_pointer_focus,
228 default_grab_pointer_motion,
229 default_grab_pointer_button,
230 default_grab_pointer_cancel,
234 default_grab_touch_down(struct weston_touch_grab *grab, uint32_t time,
235 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
237 struct weston_touch *touch = grab->touch;
238 struct wl_display *display = touch->seat->compositor->wl_display;
240 struct wl_resource *resource;
241 struct wl_list *resource_list;
243 resource_list = &touch->focus_resource_list;
245 if (!wl_list_empty(resource_list) && touch->focus) {
246 serial = wl_display_next_serial(display);
247 wl_resource_for_each(resource, resource_list)
248 wl_touch_send_down(resource, serial, time,
249 touch->focus->surface->resource,
255 default_grab_touch_up(struct weston_touch_grab *grab,
256 uint32_t time, int touch_id)
258 struct weston_touch *touch = grab->touch;
259 struct wl_display *display = touch->seat->compositor->wl_display;
261 struct wl_resource *resource;
262 struct wl_list *resource_list;
264 resource_list = &touch->focus_resource_list;
266 if (!wl_list_empty(resource_list)) {
267 serial = wl_display_next_serial(display);
268 wl_resource_for_each(resource, resource_list)
269 wl_touch_send_up(resource, serial, time, touch_id);
274 default_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time,
275 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
277 struct weston_touch *touch = grab->touch;
278 struct wl_resource *resource;
279 struct wl_list *resource_list;
281 resource_list = &touch->focus_resource_list;
283 wl_resource_for_each(resource, resource_list) {
284 wl_touch_send_motion(resource, time,
290 default_grab_touch_frame(struct weston_touch_grab *grab)
292 struct wl_resource *resource;
294 wl_resource_for_each(resource, &grab->touch->focus_resource_list)
295 wl_touch_send_frame(resource);
299 default_grab_touch_cancel(struct weston_touch_grab *grab)
303 static const struct weston_touch_grab_interface default_touch_grab_interface = {
304 default_grab_touch_down,
305 default_grab_touch_up,
306 default_grab_touch_motion,
307 default_grab_touch_frame,
308 default_grab_touch_cancel,
312 default_grab_keyboard_key(struct weston_keyboard_grab *grab,
313 uint32_t time, uint32_t key, uint32_t state)
315 struct weston_keyboard *keyboard = grab->keyboard;
316 struct wl_resource *resource;
317 struct wl_display *display = keyboard->seat->compositor->wl_display;
319 struct wl_list *resource_list;
321 resource_list = &keyboard->focus_resource_list;
322 if (!wl_list_empty(resource_list)) {
323 serial = wl_display_next_serial(display);
324 wl_resource_for_each(resource, resource_list)
325 wl_keyboard_send_key(resource,
334 send_modifiers_to_resource(struct weston_keyboard *keyboard,
335 struct wl_resource *resource,
338 wl_keyboard_send_modifiers(resource,
340 keyboard->modifiers.mods_depressed,
341 keyboard->modifiers.mods_latched,
342 keyboard->modifiers.mods_locked,
343 keyboard->modifiers.group);
347 send_modifiers_to_client_in_list(struct wl_client *client,
348 struct wl_list *list,
350 struct weston_keyboard *keyboard)
352 struct wl_resource *resource;
354 wl_resource_for_each(resource, list) {
355 if (wl_resource_get_client(resource) == client)
356 send_modifiers_to_resource(keyboard,
362 static struct wl_resource *
363 find_resource_for_surface(struct wl_list *list, struct weston_surface *surface)
368 if (!surface->resource)
371 return wl_resource_find_for_client(list, wl_resource_get_client(surface->resource));
374 static struct wl_resource *
375 find_resource_for_view(struct wl_list *list, struct weston_view *view)
380 return find_resource_for_surface(list, view->surface);
384 default_grab_keyboard_modifiers(struct weston_keyboard_grab *grab,
385 uint32_t serial, uint32_t mods_depressed,
386 uint32_t mods_latched,
387 uint32_t mods_locked, uint32_t group)
389 struct weston_keyboard *keyboard = grab->keyboard;
390 struct weston_pointer *pointer = grab->keyboard->seat->pointer;
391 struct wl_resource *resource;
392 struct wl_list *resource_list;
394 resource_list = &keyboard->focus_resource_list;
396 wl_resource_for_each(resource, resource_list) {
397 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
398 mods_latched, mods_locked, group);
400 if (pointer && pointer->focus && pointer->focus->surface->resource &&
401 pointer->focus->surface != keyboard->focus) {
402 struct wl_client *pointer_client =
403 wl_resource_get_client(pointer->focus->surface->resource);
404 send_modifiers_to_client_in_list(pointer_client,
405 &keyboard->resource_list,
412 default_grab_keyboard_cancel(struct weston_keyboard_grab *grab)
416 static const struct weston_keyboard_grab_interface
417 default_keyboard_grab_interface = {
418 default_grab_keyboard_key,
419 default_grab_keyboard_modifiers,
420 default_grab_keyboard_cancel,
424 pointer_unmap_sprite(struct weston_pointer *pointer)
426 if (weston_surface_is_mapped(pointer->sprite->surface))
427 weston_surface_unmap(pointer->sprite->surface);
429 wl_list_remove(&pointer->sprite_destroy_listener.link);
430 pointer->sprite->surface->configure = NULL;
431 pointer->sprite->surface->configure_private = NULL;
432 weston_view_destroy(pointer->sprite);
433 pointer->sprite = NULL;
437 pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
439 struct weston_pointer *pointer =
440 container_of(listener, struct weston_pointer,
441 sprite_destroy_listener);
443 pointer->sprite = NULL;
447 weston_pointer_reset_state(struct weston_pointer *pointer)
449 pointer->button_count = 0;
453 weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data);
455 WL_EXPORT struct weston_pointer *
456 weston_pointer_create(struct weston_seat *seat)
458 struct weston_pointer *pointer;
460 pointer = zalloc(sizeof *pointer);
464 wl_list_init(&pointer->resource_list);
465 wl_list_init(&pointer->focus_resource_list);
466 weston_pointer_set_default_grab(pointer,
467 seat->compositor->default_pointer_grab);
468 wl_list_init(&pointer->focus_resource_listener.link);
469 pointer->focus_resource_listener.notify = pointer_focus_resource_destroyed;
470 pointer->default_grab.pointer = pointer;
471 pointer->grab = &pointer->default_grab;
472 wl_signal_init(&pointer->motion_signal);
473 wl_signal_init(&pointer->focus_signal);
474 wl_list_init(&pointer->focus_view_listener.link);
476 pointer->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
478 /* FIXME: Pick better co-ords. */
479 pointer->x = wl_fixed_from_int(100);
480 pointer->y = wl_fixed_from_int(100);
482 pointer->output_destroy_listener.notify =
483 weston_pointer_handle_output_destroy;
484 wl_signal_add(&seat->compositor->output_destroyed_signal,
485 &pointer->output_destroy_listener);
491 weston_pointer_destroy(struct weston_pointer *pointer)
494 pointer_unmap_sprite(pointer);
496 /* XXX: What about pointer->resource_list? */
498 wl_list_remove(&pointer->focus_resource_listener.link);
499 wl_list_remove(&pointer->focus_view_listener.link);
500 wl_list_remove(&pointer->output_destroy_listener.link);
505 weston_pointer_set_default_grab(struct weston_pointer *pointer,
506 const struct weston_pointer_grab_interface *interface)
509 pointer->default_grab.interface = interface;
511 pointer->default_grab.interface =
512 &default_pointer_grab_interface;
515 WL_EXPORT struct weston_keyboard *
516 weston_keyboard_create(void)
518 struct weston_keyboard *keyboard;
520 keyboard = zalloc(sizeof *keyboard);
521 if (keyboard == NULL)
524 wl_list_init(&keyboard->resource_list);
525 wl_list_init(&keyboard->focus_resource_list);
526 wl_list_init(&keyboard->focus_resource_listener.link);
527 keyboard->focus_resource_listener.notify = keyboard_focus_resource_destroyed;
528 wl_array_init(&keyboard->keys);
529 keyboard->default_grab.interface = &default_keyboard_grab_interface;
530 keyboard->default_grab.keyboard = keyboard;
531 keyboard->grab = &keyboard->default_grab;
532 wl_signal_init(&keyboard->focus_signal);
538 weston_xkb_info_destroy(struct weston_xkb_info *xkb_info);
541 weston_keyboard_destroy(struct weston_keyboard *keyboard)
543 /* XXX: What about keyboard->resource_list? */
545 #ifdef ENABLE_XKBCOMMON
546 if (keyboard->seat->compositor->use_xkbcommon) {
547 xkb_state_unref(keyboard->xkb_state.state);
548 if (keyboard->xkb_info)
549 weston_xkb_info_destroy(keyboard->xkb_info);
550 xkb_keymap_unref(keyboard->pending_keymap);
554 wl_array_release(&keyboard->keys);
555 wl_list_remove(&keyboard->focus_resource_listener.link);
560 weston_touch_reset_state(struct weston_touch *touch)
565 WL_EXPORT struct weston_touch *
566 weston_touch_create(void)
568 struct weston_touch *touch;
570 touch = zalloc(sizeof *touch);
574 wl_list_init(&touch->resource_list);
575 wl_list_init(&touch->focus_resource_list);
576 wl_list_init(&touch->focus_view_listener.link);
577 touch->focus_view_listener.notify = touch_focus_view_destroyed;
578 wl_list_init(&touch->focus_resource_listener.link);
579 touch->focus_resource_listener.notify = touch_focus_resource_destroyed;
580 touch->default_grab.interface = &default_touch_grab_interface;
581 touch->default_grab.touch = touch;
582 touch->grab = &touch->default_grab;
583 wl_signal_init(&touch->focus_signal);
589 weston_touch_destroy(struct weston_touch *touch)
591 /* XXX: What about touch->resource_list? */
593 wl_list_remove(&touch->focus_view_listener.link);
594 wl_list_remove(&touch->focus_resource_listener.link);
599 seat_send_updated_caps(struct weston_seat *seat)
601 enum wl_seat_capability caps = 0;
602 struct wl_resource *resource;
604 if (seat->pointer_device_count > 0)
605 caps |= WL_SEAT_CAPABILITY_POINTER;
606 if (seat->keyboard_device_count > 0)
607 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
608 if (seat->touch_device_count > 0)
609 caps |= WL_SEAT_CAPABILITY_TOUCH;
611 wl_resource_for_each(resource, &seat->base_resource_list) {
612 wl_seat_send_capabilities(resource, caps);
614 wl_signal_emit(&seat->updated_caps_signal, seat);
618 weston_pointer_set_focus(struct weston_pointer *pointer,
619 struct weston_view *view,
620 wl_fixed_t sx, wl_fixed_t sy)
622 struct weston_keyboard *kbd = pointer->seat->keyboard;
623 struct wl_resource *resource;
624 struct wl_display *display = pointer->seat->compositor->wl_display;
626 struct wl_list *focus_resource_list;
629 if ((!pointer->focus && view) ||
630 (pointer->focus && !view) ||
631 (pointer->focus && pointer->focus->surface != view->surface) ||
632 pointer->sx != sx || pointer->sy != sy)
635 focus_resource_list = &pointer->focus_resource_list;
637 if (!wl_list_empty(focus_resource_list) && refocus) {
638 serial = wl_display_next_serial(display);
639 wl_resource_for_each(resource, focus_resource_list) {
640 wl_pointer_send_leave(resource, serial,
641 pointer->focus->surface->resource);
644 move_resources(&pointer->resource_list, focus_resource_list);
647 if (find_resource_for_view(&pointer->resource_list, view) && refocus) {
648 struct wl_client *surface_client =
649 wl_resource_get_client(view->surface->resource);
651 serial = wl_display_next_serial(display);
653 if (kbd && kbd->focus != view->surface)
654 send_modifiers_to_client_in_list(surface_client,
659 move_resources_for_client(focus_resource_list,
660 &pointer->resource_list,
663 wl_resource_for_each(resource, focus_resource_list) {
664 wl_pointer_send_enter(resource,
666 view->surface->resource,
670 pointer->focus_serial = serial;
673 wl_list_remove(&pointer->focus_view_listener.link);
674 wl_list_init(&pointer->focus_view_listener.link);
675 wl_list_remove(&pointer->focus_resource_listener.link);
676 wl_list_init(&pointer->focus_resource_listener.link);
678 wl_signal_add(&view->destroy_signal, &pointer->focus_view_listener);
679 if (view && view->surface->resource)
680 wl_resource_add_destroy_listener(view->surface->resource,
681 &pointer->focus_resource_listener);
683 pointer->focus = view;
684 pointer->focus_view_listener.notify = pointer_focus_view_destroyed;
688 wl_signal_emit(&pointer->focus_signal, pointer);
692 send_enter_to_resource_list(struct wl_list *list,
693 struct weston_keyboard *keyboard,
694 struct weston_surface *surface,
697 struct wl_resource *resource;
699 wl_resource_for_each(resource, list) {
700 send_modifiers_to_resource(keyboard, resource, serial);
701 wl_keyboard_send_enter(resource, serial,
708 weston_keyboard_set_focus(struct weston_keyboard *keyboard,
709 struct weston_surface *surface)
711 struct wl_resource *resource;
712 struct wl_display *display = keyboard->seat->compositor->wl_display;
714 struct wl_list *focus_resource_list;
716 focus_resource_list = &keyboard->focus_resource_list;
718 if (!wl_list_empty(focus_resource_list) && keyboard->focus != surface) {
719 serial = wl_display_next_serial(display);
720 wl_resource_for_each(resource, focus_resource_list) {
721 wl_keyboard_send_leave(resource, serial,
722 keyboard->focus->resource);
724 move_resources(&keyboard->resource_list, focus_resource_list);
727 if (find_resource_for_surface(&keyboard->resource_list, surface) &&
728 keyboard->focus != surface) {
729 struct wl_client *surface_client =
730 wl_resource_get_client(surface->resource);
732 serial = wl_display_next_serial(display);
734 move_resources_for_client(focus_resource_list,
735 &keyboard->resource_list,
737 send_enter_to_resource_list(focus_resource_list,
741 keyboard->focus_serial = serial;
744 wl_list_remove(&keyboard->focus_resource_listener.link);
745 wl_list_init(&keyboard->focus_resource_listener.link);
746 if (surface && surface->resource)
747 wl_resource_add_destroy_listener(surface->resource,
748 &keyboard->focus_resource_listener);
750 keyboard->focus = surface;
751 wl_signal_emit(&keyboard->focus_signal, keyboard);
755 weston_keyboard_start_grab(struct weston_keyboard *keyboard,
756 struct weston_keyboard_grab *grab)
758 keyboard->grab = grab;
759 grab->keyboard = keyboard;
765 weston_keyboard_end_grab(struct weston_keyboard *keyboard)
767 keyboard->grab = &keyboard->default_grab;
771 weston_keyboard_cancel_grab(struct weston_keyboard *keyboard)
773 keyboard->grab->interface->cancel(keyboard->grab);
777 weston_pointer_start_grab(struct weston_pointer *pointer,
778 struct weston_pointer_grab *grab)
780 pointer->grab = grab;
781 grab->pointer = pointer;
782 pointer->grab->interface->focus(pointer->grab);
786 weston_pointer_end_grab(struct weston_pointer *pointer)
788 pointer->grab = &pointer->default_grab;
789 pointer->grab->interface->focus(pointer->grab);
793 weston_pointer_cancel_grab(struct weston_pointer *pointer)
795 pointer->grab->interface->cancel(pointer->grab);
799 weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab)
806 weston_touch_end_grab(struct weston_touch *touch)
808 touch->grab = &touch->default_grab;
812 weston_touch_cancel_grab(struct weston_touch *touch)
814 touch->grab->interface->cancel(touch->grab);
818 weston_pointer_clamp_for_output(struct weston_pointer *pointer,
819 struct weston_output *output,
820 wl_fixed_t *fx, wl_fixed_t *fy)
824 x = wl_fixed_to_int(*fx);
825 y = wl_fixed_to_int(*fy);
828 *fx = wl_fixed_from_int(output->x);
829 else if (x >= output->x + output->width)
830 *fx = wl_fixed_from_int(output->x +
833 *fy = wl_fixed_from_int(output->y);
834 else if (y >= output->y + output->height)
835 *fy = wl_fixed_from_int(output->y +
840 weston_pointer_clamp(struct weston_pointer *pointer, wl_fixed_t *fx, wl_fixed_t *fy)
842 struct weston_compositor *ec = pointer->seat->compositor;
843 struct weston_output *output, *prev = NULL, *closest = NULL;
844 int x, y, old_x, old_y;
845 int distance, min = INT_MAX;
848 /* check if any output is attached to the pointer's seat or not */
849 wl_list_for_each(output, &ec->output_list, link) {
850 if (pointer->seat == output->seat_data.seat) {
856 x = wl_fixed_to_int(*fx);
857 y = wl_fixed_to_int(*fy);
858 old_x = wl_fixed_to_int(pointer->x);
859 old_y = wl_fixed_to_int(pointer->y);
861 wl_list_for_each(output, &ec->output_list, link) {
862 if (has_output && pointer->seat != output->seat_data.seat)
864 if (pixman_region32_contains_point(&output->region,
867 if (pixman_region32_contains_point(&output->region,
870 distance = abs(output->x + output->width / 2 - x) +
871 abs(output->y + output->height / 2 - y);
872 if (distance < min) {
882 weston_pointer_clamp_for_output(pointer, prev, fx, fy);
885 /* Takes absolute values */
887 weston_pointer_move(struct weston_pointer *pointer, wl_fixed_t x, wl_fixed_t y)
891 weston_pointer_clamp (pointer, &x, &y);
896 ix = wl_fixed_to_int(x);
897 iy = wl_fixed_to_int(y);
899 if (pointer->sprite) {
900 weston_view_set_position(pointer->sprite,
901 ix - pointer->hotspot_x,
902 iy - pointer->hotspot_y);
903 weston_view_schedule_repaint(pointer->sprite);
906 pointer->grab->interface->focus(pointer->grab);
907 wl_signal_emit(&pointer->motion_signal, pointer);
910 /** Verify if the pointer is in a valid position and move it if it isn't.
913 weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data)
915 struct weston_pointer *pointer;
916 struct weston_compositor *ec;
917 struct weston_output *output, *closest = NULL;
918 int x, y, distance, min = INT_MAX;
920 pointer = container_of(listener, struct weston_pointer,
921 output_destroy_listener);
922 ec = pointer->seat->compositor;
924 x = wl_fixed_to_int(pointer->x);
925 y = wl_fixed_to_int(pointer->y);
927 wl_list_for_each(output, &ec->output_list, link) {
928 if (pixman_region32_contains_point(&output->region,
931 /* Approximate the distance from the pointer to the centre of
933 distance = abs(output->x + output->width / 2 - x) +
934 abs(output->y + output->height / 2 - y);
935 if (distance < min) {
941 /* Nothing to do if there's no output left. */
948 weston_pointer_clamp_for_output(pointer, closest, &fx, &fy);
949 weston_pointer_move(pointer, fx, fy);
954 notify_motion(struct weston_seat *seat,
955 uint32_t time, wl_fixed_t dx, wl_fixed_t dy)
957 struct weston_compositor *ec = seat->compositor;
958 struct weston_pointer *pointer = seat->pointer;
960 weston_compositor_wake(ec);
961 pointer->grab->interface->motion(pointer->grab, time, pointer->x + dx, pointer->y + dy);
965 run_modifier_bindings(struct weston_seat *seat, uint32_t old, uint32_t new)
967 struct weston_compositor *compositor = seat->compositor;
968 struct weston_keyboard *keyboard = seat->keyboard;
973 enum weston_keyboard_modifier weston;
975 { keyboard->xkb_info->ctrl_mod, MODIFIER_CTRL },
976 { keyboard->xkb_info->alt_mod, MODIFIER_ALT },
977 { keyboard->xkb_info->super_mod, MODIFIER_SUPER },
978 { keyboard->xkb_info->shift_mod, MODIFIER_SHIFT },
982 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
983 if (diff & (1 << mods[i].xkb))
984 weston_compositor_run_modifier_binding(compositor,
987 WL_KEYBOARD_KEY_STATE_PRESSED);
991 for (i = 0; i < ARRAY_LENGTH(mods); i++) {
992 if (diff & (1 << mods[i].xkb))
993 weston_compositor_run_modifier_binding(compositor,
996 WL_KEYBOARD_KEY_STATE_RELEASED);
1001 notify_motion_absolute(struct weston_seat *seat,
1002 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1004 struct weston_compositor *ec = seat->compositor;
1005 struct weston_pointer *pointer = seat->pointer;
1007 weston_compositor_wake(ec);
1008 pointer->grab->interface->motion(pointer->grab, time, x, y);
1012 weston_surface_activate(struct weston_surface *surface,
1013 struct weston_seat *seat)
1015 struct weston_compositor *compositor = seat->compositor;
1017 if (seat->keyboard) {
1018 weston_keyboard_set_focus(seat->keyboard, surface);
1019 wl_data_device_set_keyboard_focus(seat);
1022 wl_signal_emit(&compositor->activate_signal, surface);
1026 notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
1027 enum wl_pointer_button_state state)
1029 struct weston_compositor *compositor = seat->compositor;
1030 struct weston_pointer *pointer = seat->pointer;
1032 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
1033 weston_compositor_idle_inhibit(compositor);
1034 if (pointer->button_count == 0) {
1035 pointer->grab_button = button;
1036 pointer->grab_time = time;
1037 pointer->grab_x = pointer->x;
1038 pointer->grab_y = pointer->y;
1040 pointer->button_count++;
1042 weston_compositor_idle_release(compositor);
1043 pointer->button_count--;
1046 weston_compositor_run_button_binding(compositor, seat, time, button,
1049 pointer->grab->interface->button(pointer->grab, time, button, state);
1051 if (pointer->button_count == 1)
1052 pointer->grab_serial =
1053 wl_display_get_serial(compositor->wl_display);
1057 notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
1060 struct weston_compositor *compositor = seat->compositor;
1061 struct weston_pointer *pointer = seat->pointer;
1062 struct wl_resource *resource;
1063 struct wl_list *resource_list;
1065 weston_compositor_wake(compositor);
1070 if (weston_compositor_run_axis_binding(compositor, seat,
1074 resource_list = &pointer->focus_resource_list;
1075 wl_resource_for_each(resource, resource_list)
1076 wl_pointer_send_axis(resource, time, axis,
1081 weston_keyboard_set_locks(struct weston_keyboard *keyboard,
1082 uint32_t mask, uint32_t value)
1084 #ifdef ENABLE_XKBCOMMON
1086 xkb_mod_mask_t mods_depressed, mods_latched, mods_locked, group;
1087 xkb_mod_mask_t num, caps;
1089 /* We don't want the leds to go out of sync with the actual state
1090 * so if the backend has no way to change the leds don't try to
1091 * change the state */
1092 if (!keyboard->seat->led_update)
1095 mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
1096 XKB_STATE_DEPRESSED);
1097 mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
1099 mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
1101 group = xkb_state_serialize_group(keyboard->xkb_state.state,
1102 XKB_STATE_EFFECTIVE);
1104 num = (1 << keyboard->xkb_info->mod2_mod);
1105 caps = (1 << keyboard->xkb_info->caps_mod);
1106 if (mask & WESTON_NUM_LOCK) {
1107 if (value & WESTON_NUM_LOCK)
1110 mods_locked &= ~num;
1112 if (mask & WESTON_CAPS_LOCK) {
1113 if (value & WESTON_CAPS_LOCK)
1114 mods_locked |= caps;
1116 mods_locked &= ~caps;
1119 xkb_state_update_mask(keyboard->xkb_state.state, mods_depressed,
1120 mods_latched, mods_locked, 0, 0, group);
1122 serial = wl_display_next_serial(
1123 keyboard->seat->compositor->wl_display);
1124 notify_modifiers(keyboard->seat, serial);
1132 #ifdef ENABLE_XKBCOMMON
1134 notify_modifiers(struct weston_seat *seat, uint32_t serial)
1136 struct weston_keyboard *keyboard = seat->keyboard;
1137 struct weston_keyboard_grab *grab = keyboard->grab;
1138 uint32_t mods_depressed, mods_latched, mods_locked, group;
1139 uint32_t mods_lookup;
1140 enum weston_led leds = 0;
1143 /* Serialize and update our internal state, checking to see if it's
1144 * different to the previous state. */
1145 mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state,
1146 XKB_STATE_MODS_DEPRESSED);
1147 mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state,
1148 XKB_STATE_MODS_LATCHED);
1149 mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state,
1150 XKB_STATE_MODS_LOCKED);
1151 group = xkb_state_serialize_layout(keyboard->xkb_state.state,
1152 XKB_STATE_LAYOUT_EFFECTIVE);
1154 if (mods_depressed != seat->keyboard->modifiers.mods_depressed ||
1155 mods_latched != seat->keyboard->modifiers.mods_latched ||
1156 mods_locked != seat->keyboard->modifiers.mods_locked ||
1157 group != seat->keyboard->modifiers.group)
1160 run_modifier_bindings(seat, seat->keyboard->modifiers.mods_depressed,
1163 seat->keyboard->modifiers.mods_depressed = mods_depressed;
1164 seat->keyboard->modifiers.mods_latched = mods_latched;
1165 seat->keyboard->modifiers.mods_locked = mods_locked;
1166 seat->keyboard->modifiers.group = group;
1168 /* And update the modifier_state for bindings. */
1169 mods_lookup = mods_depressed | mods_latched;
1170 seat->modifier_state = 0;
1171 if (mods_lookup & (1 << keyboard->xkb_info->ctrl_mod))
1172 seat->modifier_state |= MODIFIER_CTRL;
1173 if (mods_lookup & (1 << keyboard->xkb_info->alt_mod))
1174 seat->modifier_state |= MODIFIER_ALT;
1175 if (mods_lookup & (1 << keyboard->xkb_info->super_mod))
1176 seat->modifier_state |= MODIFIER_SUPER;
1177 if (mods_lookup & (1 << keyboard->xkb_info->shift_mod))
1178 seat->modifier_state |= MODIFIER_SHIFT;
1180 /* Finally, notify the compositor that LEDs have changed. */
1181 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
1182 keyboard->xkb_info->num_led))
1183 leds |= LED_NUM_LOCK;
1184 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
1185 keyboard->xkb_info->caps_led))
1186 leds |= LED_CAPS_LOCK;
1187 if (xkb_state_led_index_is_active(keyboard->xkb_state.state,
1188 keyboard->xkb_info->scroll_led))
1189 leds |= LED_SCROLL_LOCK;
1190 if (leds != keyboard->xkb_state.leds && seat->led_update)
1191 seat->led_update(seat, leds);
1192 keyboard->xkb_state.leds = leds;
1195 grab->interface->modifiers(grab,
1197 keyboard->modifiers.mods_depressed,
1198 keyboard->modifiers.mods_latched,
1199 keyboard->modifiers.mods_locked,
1200 keyboard->modifiers.group);
1205 update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
1206 enum wl_keyboard_key_state state)
1208 struct weston_keyboard *keyboard = seat->keyboard;
1209 enum xkb_key_direction direction;
1211 /* Keyboard modifiers don't exist in raw keyboard mode */
1212 if (!seat->compositor->use_xkbcommon)
1215 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1216 direction = XKB_KEY_DOWN;
1218 direction = XKB_KEY_UP;
1220 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
1221 * broken keycode system, which starts at 8. */
1222 xkb_state_update_key(keyboard->xkb_state.state, key + 8, direction);
1224 notify_modifiers(seat, serial);
1228 send_keymap(struct wl_resource *resource, struct weston_xkb_info *xkb_info)
1230 wl_keyboard_send_keymap(resource,
1231 WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
1232 xkb_info->keymap_fd,
1233 xkb_info->keymap_size);
1237 send_modifiers(struct wl_resource *resource, uint32_t serial, struct weston_keyboard *keyboard)
1239 wl_keyboard_send_modifiers(resource, serial,
1240 keyboard->modifiers.mods_depressed,
1241 keyboard->modifiers.mods_latched,
1242 keyboard->modifiers.mods_locked,
1243 keyboard->modifiers.group);
1246 static struct weston_xkb_info *
1247 weston_xkb_info_create(struct xkb_keymap *keymap);
1250 update_keymap(struct weston_seat *seat)
1252 struct weston_keyboard *keyboard = seat->keyboard;
1253 struct wl_resource *resource;
1254 struct weston_xkb_info *xkb_info;
1255 struct xkb_state *state;
1256 xkb_mod_mask_t latched_mods;
1257 xkb_mod_mask_t locked_mods;
1259 xkb_info = weston_xkb_info_create(keyboard->pending_keymap);
1261 xkb_keymap_unref(keyboard->pending_keymap);
1262 keyboard->pending_keymap = NULL;
1265 weston_log("failed to create XKB info\n");
1269 state = xkb_state_new(xkb_info->keymap);
1271 weston_log("failed to initialise XKB state\n");
1272 weston_xkb_info_destroy(xkb_info);
1276 latched_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
1277 XKB_STATE_MODS_LATCHED);
1278 locked_mods = xkb_state_serialize_mods(keyboard->xkb_state.state,
1279 XKB_STATE_MODS_LOCKED);
1280 xkb_state_update_mask(state,
1286 weston_xkb_info_destroy(keyboard->xkb_info);
1287 keyboard->xkb_info = xkb_info;
1289 xkb_state_unref(keyboard->xkb_state.state);
1290 keyboard->xkb_state.state = state;
1292 wl_resource_for_each(resource, &seat->keyboard->resource_list)
1293 send_keymap(resource, xkb_info);
1294 wl_resource_for_each(resource, &seat->keyboard->focus_resource_list)
1295 send_keymap(resource, xkb_info);
1297 notify_modifiers(seat, wl_display_next_serial(seat->compositor->wl_display));
1299 if (!latched_mods && !locked_mods)
1302 wl_resource_for_each(resource, &seat->keyboard->resource_list)
1303 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), seat->keyboard);
1304 wl_resource_for_each(resource, &seat->keyboard->focus_resource_list)
1305 send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), seat->keyboard);
1309 notify_modifiers(struct weston_seat *seat, uint32_t serial)
1314 update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
1315 enum wl_keyboard_key_state state)
1320 update_keymap(struct weston_seat *seat)
1326 notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
1327 enum wl_keyboard_key_state state,
1328 enum weston_key_state_update update_state)
1330 struct weston_compositor *compositor = seat->compositor;
1331 struct weston_keyboard *keyboard = seat->keyboard;
1332 struct weston_keyboard_grab *grab = keyboard->grab;
1335 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1336 weston_compositor_idle_inhibit(compositor);
1337 keyboard->grab_key = key;
1338 keyboard->grab_time = time;
1340 weston_compositor_idle_release(compositor);
1343 end = keyboard->keys.data + keyboard->keys.size;
1344 for (k = keyboard->keys.data; k < end; k++) {
1346 /* Ignore server-generated repeats. */
1347 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1352 keyboard->keys.size = (void *) end - keyboard->keys.data;
1353 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1354 k = wl_array_add(&keyboard->keys, sizeof *k);
1358 if (grab == &keyboard->default_grab ||
1359 grab == &keyboard->input_method_grab) {
1360 weston_compositor_run_key_binding(compositor, seat, time, key,
1362 grab = keyboard->grab;
1365 grab->interface->key(grab, time, key, state);
1367 if (keyboard->pending_keymap &&
1368 keyboard->keys.size == 0)
1369 update_keymap(seat);
1371 if (update_state == STATE_UPDATE_AUTOMATIC) {
1372 update_modifier_state(seat,
1373 wl_display_get_serial(compositor->wl_display),
1380 notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
1381 wl_fixed_t x, wl_fixed_t y)
1384 weston_pointer_move(seat->pointer, x, y);
1386 /* FIXME: We should call weston_pointer_set_focus(seat,
1387 * NULL) here, but somehow that breaks re-entry... */
1392 destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
1394 struct weston_seat *ws;
1396 ws = container_of(listener, struct weston_seat,
1397 saved_kbd_focus_listener);
1399 ws->saved_kbd_focus = NULL;
1403 notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
1404 enum weston_key_state_update update_state)
1406 struct weston_compositor *compositor = seat->compositor;
1407 struct weston_keyboard *keyboard = seat->keyboard;
1408 struct weston_surface *surface;
1409 uint32_t *k, serial;
1411 serial = wl_display_next_serial(compositor->wl_display);
1412 wl_array_copy(&keyboard->keys, keys);
1413 wl_array_for_each(k, &keyboard->keys) {
1414 weston_compositor_idle_inhibit(compositor);
1415 if (update_state == STATE_UPDATE_AUTOMATIC)
1416 update_modifier_state(seat, serial, *k,
1417 WL_KEYBOARD_KEY_STATE_PRESSED);
1420 /* Run key bindings after we've updated the state. */
1421 wl_array_for_each(k, &keyboard->keys) {
1422 weston_compositor_run_key_binding(compositor, seat, 0, *k,
1423 WL_KEYBOARD_KEY_STATE_PRESSED);
1426 surface = seat->saved_kbd_focus;
1429 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1430 weston_keyboard_set_focus(keyboard, surface);
1431 seat->saved_kbd_focus = NULL;
1436 notify_keyboard_focus_out(struct weston_seat *seat)
1438 struct weston_compositor *compositor = seat->compositor;
1439 struct weston_keyboard *keyboard = seat->keyboard;
1440 uint32_t *k, serial;
1442 serial = wl_display_next_serial(compositor->wl_display);
1443 wl_array_for_each(k, &keyboard->keys) {
1444 weston_compositor_idle_release(compositor);
1445 update_modifier_state(seat, serial, *k,
1446 WL_KEYBOARD_KEY_STATE_RELEASED);
1449 seat->modifier_state = 0;
1451 if (keyboard->focus) {
1452 seat->saved_kbd_focus = keyboard->focus;
1453 seat->saved_kbd_focus_listener.notify =
1454 destroy_device_saved_kbd_focus;
1455 wl_signal_add(&keyboard->focus->destroy_signal,
1456 &seat->saved_kbd_focus_listener);
1459 weston_keyboard_set_focus(keyboard, NULL);
1460 weston_keyboard_cancel_grab(keyboard);
1462 weston_pointer_cancel_grab(seat->pointer);
1466 weston_touch_set_focus(struct weston_seat *seat, struct weston_view *view)
1468 struct wl_list *focus_resource_list;
1470 focus_resource_list = &seat->touch->focus_resource_list;
1472 if (view && seat->touch->focus &&
1473 seat->touch->focus->surface == view->surface) {
1474 seat->touch->focus = view;
1478 wl_list_remove(&seat->touch->focus_resource_listener.link);
1479 wl_list_init(&seat->touch->focus_resource_listener.link);
1480 wl_list_remove(&seat->touch->focus_view_listener.link);
1481 wl_list_init(&seat->touch->focus_view_listener.link);
1483 if (!wl_list_empty(focus_resource_list)) {
1484 move_resources(&seat->touch->resource_list,
1485 focus_resource_list);
1489 struct wl_client *surface_client;
1491 if (!view->surface->resource) {
1492 seat->touch->focus = NULL;
1496 surface_client = wl_resource_get_client(view->surface->resource);
1497 move_resources_for_client(focus_resource_list,
1498 &seat->touch->resource_list,
1500 wl_resource_add_destroy_listener(view->surface->resource,
1501 &seat->touch->focus_resource_listener);
1502 wl_signal_add(&view->destroy_signal, &seat->touch->focus_view_listener);
1504 seat->touch->focus = view;
1508 * notify_touch - emulates button touches and notifies surfaces accordingly.
1510 * It assumes always the correct cycle sequence until it gets here: touch_down
1511 * → touch_update → ... → touch_update → touch_end. The driver is responsible
1512 * for sending along such order.
1516 notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
1517 wl_fixed_t x, wl_fixed_t y, int touch_type)
1519 struct weston_compositor *ec = seat->compositor;
1520 struct weston_touch *touch = seat->touch;
1521 struct weston_touch_grab *grab = touch->grab;
1522 struct weston_view *ev;
1525 /* Update grab's global coordinates. */
1526 if (touch_id == touch->grab_touch_id && touch_type != WL_TOUCH_UP) {
1531 switch (touch_type) {
1533 weston_compositor_idle_inhibit(ec);
1537 /* the first finger down picks the view, and all further go
1538 * to that view for the remainder of the touch session i.e.
1539 * until all touch points are up again. */
1540 if (touch->num_tp == 1) {
1541 ev = weston_compositor_pick_view(ec, x, y, &sx, &sy);
1542 weston_touch_set_focus(seat, ev);
1543 } else if (touch->focus) {
1545 weston_view_from_global_fixed(ev, x, y, &sx, &sy);
1547 /* Unexpected condition: We have non-initial touch but
1548 * there is no focused surface.
1550 weston_log("touch event received with %d points down"
1551 "but no surface focused\n", touch->num_tp);
1555 weston_compositor_run_touch_binding(ec, seat,
1558 grab->interface->down(grab, time, touch_id, sx, sy);
1559 if (touch->num_tp == 1) {
1560 touch->grab_serial =
1561 wl_display_get_serial(ec->wl_display);
1562 touch->grab_touch_id = touch_id;
1563 touch->grab_time = time;
1569 case WL_TOUCH_MOTION:
1574 weston_view_from_global_fixed(ev, x, y, &sx, &sy);
1575 grab->interface->motion(grab, time, touch_id, sx, sy);
1578 if (touch->num_tp == 0) {
1579 /* This can happen if we start out with one or
1580 * more fingers on the touch screen, in which
1581 * case we didn't get the corresponding down
1583 weston_log("unmatched touch up event\n");
1586 weston_compositor_idle_release(ec);
1589 grab->interface->up(grab, time, touch_id);
1590 if (touch->num_tp == 0)
1591 weston_touch_set_focus(seat, NULL);
1597 notify_touch_frame(struct weston_seat *seat)
1599 struct weston_touch *touch = seat->touch;
1600 struct weston_touch_grab *grab = touch->grab;
1602 grab->interface->frame(grab);
1606 pointer_cursor_surface_configure(struct weston_surface *es,
1607 int32_t dx, int32_t dy)
1609 struct weston_pointer *pointer = es->configure_private;
1615 assert(es == pointer->sprite->surface);
1617 pointer->hotspot_x -= dx;
1618 pointer->hotspot_y -= dy;
1620 x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x;
1621 y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y;
1623 weston_view_set_position(pointer->sprite, x, y);
1625 empty_region(&es->pending.input);
1626 empty_region(&es->input);
1628 if (!weston_surface_is_mapped(es)) {
1629 weston_layer_entry_insert(&es->compositor->cursor_layer.view_list,
1630 &pointer->sprite->layer_link);
1631 weston_view_update_transform(pointer->sprite);
1636 pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
1637 uint32_t serial, struct wl_resource *surface_resource,
1638 int32_t x, int32_t y)
1640 struct weston_pointer *pointer = wl_resource_get_user_data(resource);
1641 struct weston_surface *surface = NULL;
1643 if (surface_resource)
1644 surface = wl_resource_get_user_data(surface_resource);
1646 if (pointer->focus == NULL)
1648 /* pointer->focus->surface->resource can be NULL. Surfaces like the
1649 black_surface used in shell.c for fullscreen don't have
1650 a resource, but can still have focus */
1651 if (pointer->focus->surface->resource == NULL)
1653 if (wl_resource_get_client(pointer->focus->surface->resource) != client)
1655 if (pointer->focus_serial - serial > UINT32_MAX / 2)
1658 if (surface && pointer->sprite && surface != pointer->sprite->surface) {
1659 if (surface->configure) {
1660 wl_resource_post_error(surface->resource,
1661 WL_DISPLAY_ERROR_INVALID_OBJECT,
1662 "surface->configure already "
1668 if (pointer->sprite)
1669 pointer_unmap_sprite(pointer);
1674 wl_signal_add(&surface->destroy_signal,
1675 &pointer->sprite_destroy_listener);
1677 surface->configure = pointer_cursor_surface_configure;
1678 surface->configure_private = pointer;
1679 pointer->sprite = weston_view_create(surface);
1680 pointer->hotspot_x = x;
1681 pointer->hotspot_y = y;
1683 if (surface->buffer_ref.buffer) {
1684 pointer_cursor_surface_configure(surface, 0, 0);
1685 weston_view_schedule_repaint(pointer->sprite);
1690 pointer_release(struct wl_client *client, struct wl_resource *resource)
1692 wl_resource_destroy(resource);
1695 static const struct wl_pointer_interface pointer_interface = {
1701 seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
1704 struct weston_seat *seat = wl_resource_get_user_data(resource);
1705 struct wl_resource *cr;
1710 cr = wl_resource_create(client, &wl_pointer_interface,
1711 wl_resource_get_version(resource), id);
1713 wl_client_post_no_memory(client);
1717 /* May be moved to focused list later by either
1718 * weston_pointer_set_focus or directly if this client is already
1720 wl_list_insert(&seat->pointer->resource_list, wl_resource_get_link(cr));
1721 wl_resource_set_implementation(cr, &pointer_interface, seat->pointer,
1724 if (seat->pointer->focus && seat->pointer->focus->surface->resource &&
1725 wl_resource_get_client(seat->pointer->focus->surface->resource) == client) {
1728 weston_view_from_global_fixed(seat->pointer->focus,
1733 wl_list_remove(wl_resource_get_link(cr));
1734 wl_list_insert(&seat->pointer->focus_resource_list,
1735 wl_resource_get_link(cr));
1736 wl_pointer_send_enter(cr,
1737 seat->pointer->focus_serial,
1738 seat->pointer->focus->surface->resource,
1744 keyboard_release(struct wl_client *client, struct wl_resource *resource)
1746 wl_resource_destroy(resource);
1749 static const struct wl_keyboard_interface keyboard_interface = {
1754 should_send_modifiers_to_client(struct weston_seat *seat,
1755 struct wl_client *client)
1757 if (seat->keyboard &&
1758 seat->keyboard->focus &&
1759 seat->keyboard->focus->resource &&
1760 wl_resource_get_client(seat->keyboard->focus->resource) == client)
1763 if (seat->pointer &&
1764 seat->pointer->focus &&
1765 seat->pointer->focus->surface->resource &&
1766 wl_resource_get_client(seat->pointer->focus->surface->resource) == client)
1773 seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
1776 struct weston_seat *seat = wl_resource_get_user_data(resource);
1777 struct weston_keyboard *keyboard = seat->keyboard;
1778 struct wl_resource *cr;
1780 if (!seat->keyboard)
1783 cr = wl_resource_create(client, &wl_keyboard_interface,
1784 wl_resource_get_version(resource), id);
1786 wl_client_post_no_memory(client);
1790 /* May be moved to focused list later by either
1791 * weston_keyboard_set_focus or directly if this client is already
1793 wl_list_insert(&seat->keyboard->resource_list, wl_resource_get_link(cr));
1794 wl_resource_set_implementation(cr, &keyboard_interface,
1795 seat, unbind_resource);
1797 if (wl_resource_get_version(cr) >= WL_KEYBOARD_REPEAT_INFO_SINCE_VERSION) {
1798 wl_keyboard_send_repeat_info(cr,
1799 seat->compositor->kb_repeat_rate,
1800 seat->compositor->kb_repeat_delay);
1803 if (seat->compositor->use_xkbcommon) {
1804 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
1805 keyboard->xkb_info->keymap_fd,
1806 keyboard->xkb_info->keymap_size);
1808 int null_fd = open("/dev/null", O_RDONLY);
1809 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP,
1815 if (should_send_modifiers_to_client(seat, client)) {
1816 send_modifiers_to_resource(seat->keyboard,
1818 seat->keyboard->focus_serial);
1821 if (seat->keyboard->focus &&
1822 wl_resource_get_client(seat->keyboard->focus->resource) == client) {
1823 struct weston_surface *surface =
1824 (struct weston_surface *) seat->keyboard->focus;
1826 wl_list_remove(wl_resource_get_link(cr));
1827 wl_list_insert(&seat->keyboard->focus_resource_list,
1828 wl_resource_get_link(cr));
1829 wl_keyboard_send_enter(cr,
1830 seat->keyboard->focus_serial,
1832 &seat->keyboard->keys);
1834 /* If this is the first keyboard resource for this
1836 if (seat->keyboard->focus_resource_list.prev ==
1837 wl_resource_get_link(cr))
1838 wl_data_device_set_keyboard_focus(seat);
1843 touch_release(struct wl_client *client, struct wl_resource *resource)
1845 wl_resource_destroy(resource);
1848 static const struct wl_touch_interface touch_interface = {
1853 seat_get_touch(struct wl_client *client, struct wl_resource *resource,
1856 struct weston_seat *seat = wl_resource_get_user_data(resource);
1857 struct wl_resource *cr;
1862 cr = wl_resource_create(client, &wl_touch_interface,
1863 wl_resource_get_version(resource), id);
1865 wl_client_post_no_memory(client);
1869 if (seat->touch->focus &&
1870 wl_resource_get_client(seat->touch->focus->surface->resource) == client) {
1871 wl_list_insert(&seat->touch->resource_list,
1872 wl_resource_get_link(cr));
1874 wl_list_insert(&seat->touch->focus_resource_list,
1875 wl_resource_get_link(cr));
1877 wl_resource_set_implementation(cr, &touch_interface,
1878 seat, unbind_resource);
1881 static const struct wl_seat_interface seat_interface = {
1888 bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1890 struct weston_seat *seat = data;
1891 struct wl_resource *resource;
1892 enum wl_seat_capability caps = 0;
1894 resource = wl_resource_create(client,
1895 &wl_seat_interface, MIN(version, 4), id);
1896 wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource));
1897 wl_resource_set_implementation(resource, &seat_interface, data,
1901 caps |= WL_SEAT_CAPABILITY_POINTER;
1903 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
1905 caps |= WL_SEAT_CAPABILITY_TOUCH;
1907 wl_seat_send_capabilities(resource, caps);
1908 if (version >= WL_SEAT_NAME_SINCE_VERSION)
1909 wl_seat_send_name(resource, seat->seat_name);
1912 #ifdef ENABLE_XKBCOMMON
1914 weston_compositor_xkb_init(struct weston_compositor *ec,
1915 struct xkb_rule_names *names)
1917 ec->use_xkbcommon = 1;
1919 if (ec->xkb_context == NULL) {
1920 ec->xkb_context = xkb_context_new(0);
1921 if (ec->xkb_context == NULL) {
1922 weston_log("failed to create XKB context\n");
1928 ec->xkb_names = *names;
1929 if (!ec->xkb_names.rules)
1930 ec->xkb_names.rules = strdup("evdev");
1931 if (!ec->xkb_names.model)
1932 ec->xkb_names.model = strdup("pc105");
1933 if (!ec->xkb_names.layout)
1934 ec->xkb_names.layout = strdup("us");
1940 weston_xkb_info_destroy(struct weston_xkb_info *xkb_info)
1942 if (--xkb_info->ref_count > 0)
1945 xkb_keymap_unref(xkb_info->keymap);
1947 if (xkb_info->keymap_area)
1948 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
1949 if (xkb_info->keymap_fd >= 0)
1950 close(xkb_info->keymap_fd);
1955 weston_compositor_xkb_destroy(struct weston_compositor *ec)
1958 * If we're operating in raw keyboard mode, we never initialized
1959 * libxkbcommon so there's no cleanup to do either.
1961 if (!ec->use_xkbcommon)
1964 free((char *) ec->xkb_names.rules);
1965 free((char *) ec->xkb_names.model);
1966 free((char *) ec->xkb_names.layout);
1967 free((char *) ec->xkb_names.variant);
1968 free((char *) ec->xkb_names.options);
1971 weston_xkb_info_destroy(ec->xkb_info);
1972 xkb_context_unref(ec->xkb_context);
1975 static struct weston_xkb_info *
1976 weston_xkb_info_create(struct xkb_keymap *keymap)
1978 struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info);
1979 if (xkb_info == NULL)
1982 xkb_info->keymap = xkb_keymap_ref(keymap);
1983 xkb_info->ref_count = 1;
1987 xkb_info->shift_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
1988 XKB_MOD_NAME_SHIFT);
1989 xkb_info->caps_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
1991 xkb_info->ctrl_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
1993 xkb_info->alt_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
1995 xkb_info->mod2_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
1997 xkb_info->mod3_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
1999 xkb_info->super_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2001 xkb_info->mod5_mod = xkb_keymap_mod_get_index(xkb_info->keymap,
2004 xkb_info->num_led = xkb_keymap_led_get_index(xkb_info->keymap,
2006 xkb_info->caps_led = xkb_keymap_led_get_index(xkb_info->keymap,
2008 xkb_info->scroll_led = xkb_keymap_led_get_index(xkb_info->keymap,
2009 XKB_LED_NAME_SCROLL);
2011 keymap_str = xkb_keymap_get_as_string(xkb_info->keymap,
2012 XKB_KEYMAP_FORMAT_TEXT_V1);
2013 if (keymap_str == NULL) {
2014 weston_log("failed to get string version of keymap\n");
2017 xkb_info->keymap_size = strlen(keymap_str) + 1;
2019 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
2020 if (xkb_info->keymap_fd < 0) {
2021 weston_log("creating a keymap file for %lu bytes failed: %m\n",
2022 (unsigned long) xkb_info->keymap_size);
2023 goto err_keymap_str;
2026 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2027 PROT_READ | PROT_WRITE,
2028 MAP_SHARED, xkb_info->keymap_fd, 0);
2029 if (xkb_info->keymap_area == MAP_FAILED) {
2030 weston_log("failed to mmap() %lu bytes\n",
2031 (unsigned long) xkb_info->keymap_size);
2034 strcpy(xkb_info->keymap_area, keymap_str);
2040 close(xkb_info->keymap_fd);
2044 xkb_keymap_unref(xkb_info->keymap);
2050 weston_compositor_build_global_keymap(struct weston_compositor *ec)
2052 struct xkb_keymap *keymap;
2054 if (ec->xkb_info != NULL)
2057 keymap = xkb_keymap_new_from_names(ec->xkb_context,
2060 if (keymap == NULL) {
2061 weston_log("failed to compile global XKB keymap\n");
2062 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
2064 ec->xkb_names.rules, ec->xkb_names.model,
2065 ec->xkb_names.layout, ec->xkb_names.variant,
2066 ec->xkb_names.options);
2070 ec->xkb_info = weston_xkb_info_create(keymap);
2071 xkb_keymap_unref(keymap);
2072 if (ec->xkb_info == NULL)
2079 weston_compositor_xkb_init(struct weston_compositor *ec,
2080 struct xkb_rule_names *names)
2086 weston_compositor_xkb_destroy(struct weston_compositor *ec)
2092 weston_seat_update_keymap(struct weston_seat *seat, struct xkb_keymap *keymap)
2094 if (!seat->keyboard || !keymap)
2097 #ifdef ENABLE_XKBCOMMON
2098 if (!seat->compositor->use_xkbcommon)
2101 xkb_keymap_unref(seat->keyboard->pending_keymap);
2102 seat->keyboard->pending_keymap = xkb_keymap_ref(keymap);
2104 if (seat->keyboard->keys.size == 0)
2105 update_keymap(seat);
2110 weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
2112 struct weston_keyboard *keyboard;
2114 if (seat->keyboard) {
2115 seat->keyboard_device_count += 1;
2116 if (seat->keyboard_device_count == 1)
2117 seat_send_updated_caps(seat);
2121 keyboard = weston_keyboard_create();
2122 if (keyboard == NULL) {
2123 weston_log("failed to allocate weston keyboard struct\n");
2127 #ifdef ENABLE_XKBCOMMON
2128 if (seat->compositor->use_xkbcommon) {
2129 if (keymap != NULL) {
2130 keyboard->xkb_info = weston_xkb_info_create(keymap);
2131 if (keyboard->xkb_info == NULL)
2134 if (weston_compositor_build_global_keymap(seat->compositor) < 0)
2136 keyboard->xkb_info = seat->compositor->xkb_info;
2137 keyboard->xkb_info->ref_count++;
2140 keyboard->xkb_state.state = xkb_state_new(keyboard->xkb_info->keymap);
2141 if (keyboard->xkb_state.state == NULL) {
2142 weston_log("failed to initialise XKB state\n");
2146 keyboard->xkb_state.leds = 0;
2150 seat->keyboard = keyboard;
2151 seat->keyboard_device_count = 1;
2152 keyboard->seat = seat;
2154 seat_send_updated_caps(seat);
2159 if (keyboard->xkb_info)
2160 weston_xkb_info_destroy(keyboard->xkb_info);
2167 weston_keyboard_reset_state(struct weston_keyboard *keyboard)
2169 struct weston_seat *seat = keyboard->seat;
2170 struct xkb_state *state;
2172 #ifdef ENABLE_XKBCOMMON
2173 if (seat->compositor->use_xkbcommon) {
2174 state = xkb_state_new(keyboard->xkb_info->keymap);
2176 weston_log("failed to reset XKB state\n");
2179 xkb_state_unref(keyboard->xkb_state.state);
2180 keyboard->xkb_state.state = state;
2182 keyboard->xkb_state.leds = 0;
2186 seat->modifier_state = 0;
2190 weston_seat_release_keyboard(struct weston_seat *seat)
2192 seat->keyboard_device_count--;
2193 if (seat->keyboard_device_count == 0) {
2194 weston_keyboard_set_focus(seat->keyboard, NULL);
2195 weston_keyboard_cancel_grab(seat->keyboard);
2196 weston_keyboard_reset_state(seat->keyboard);
2197 seat_send_updated_caps(seat);
2202 weston_seat_init_pointer(struct weston_seat *seat)
2204 struct weston_pointer *pointer;
2206 if (seat->pointer) {
2207 seat->pointer_device_count += 1;
2208 if (seat->pointer_device_count == 1)
2209 seat_send_updated_caps(seat);
2213 pointer = weston_pointer_create(seat);
2214 if (pointer == NULL)
2217 seat->pointer = pointer;
2218 seat->pointer_device_count = 1;
2219 pointer->seat = seat;
2221 seat_send_updated_caps(seat);
2225 weston_seat_release_pointer(struct weston_seat *seat)
2227 struct weston_pointer *pointer = seat->pointer;
2229 seat->pointer_device_count--;
2230 if (seat->pointer_device_count == 0) {
2231 weston_pointer_set_focus(pointer, NULL,
2232 wl_fixed_from_int(0),
2233 wl_fixed_from_int(0));
2234 weston_pointer_cancel_grab(pointer);
2236 if (pointer->sprite)
2237 pointer_unmap_sprite(pointer);
2239 weston_pointer_reset_state(pointer);
2240 seat_send_updated_caps(seat);
2245 weston_seat_init_touch(struct weston_seat *seat)
2247 struct weston_touch *touch;
2250 seat->touch_device_count += 1;
2251 if (seat->touch_device_count == 1)
2252 seat_send_updated_caps(seat);
2256 touch = weston_touch_create();
2260 seat->touch = touch;
2261 seat->touch_device_count = 1;
2264 seat_send_updated_caps(seat);
2268 weston_seat_release_touch(struct weston_seat *seat)
2270 seat->touch_device_count--;
2271 if (seat->touch_device_count == 0) {
2272 weston_touch_set_focus(seat, NULL);
2273 weston_touch_cancel_grab(seat->touch);
2274 weston_touch_reset_state(seat->touch);
2275 seat_send_updated_caps(seat);
2280 weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec,
2281 const char *seat_name)
2283 memset(seat, 0, sizeof *seat);
2285 seat->selection_data_source = NULL;
2286 wl_list_init(&seat->base_resource_list);
2287 wl_signal_init(&seat->selection_signal);
2288 wl_list_init(&seat->drag_resource_list);
2289 wl_signal_init(&seat->destroy_signal);
2290 wl_signal_init(&seat->updated_caps_signal);
2292 seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 4,
2295 seat->compositor = ec;
2296 seat->modifier_state = 0;
2297 seat->seat_name = strdup(seat_name);
2299 wl_list_insert(ec->seat_list.prev, &seat->link);
2301 clipboard_create(seat);
2303 wl_signal_emit(&ec->seat_created_signal, seat);
2307 weston_seat_release(struct weston_seat *seat)
2309 wl_list_remove(&seat->link);
2311 if (seat->saved_kbd_focus)
2312 wl_list_remove(&seat->saved_kbd_focus_listener.link);
2315 weston_pointer_destroy(seat->pointer);
2317 weston_keyboard_destroy(seat->keyboard);
2319 weston_touch_destroy(seat->touch);
2321 free (seat->seat_name);
2323 wl_global_destroy(seat->global);
2325 wl_signal_emit(&seat->destroy_signal, seat);