2 * Copyright © 2012 Collabora, Ltd.
3 * Copyright © 2013 Ran Benita <ran234@gmail.com>
4 * Copyright © 2016 Daniel Stone <daniel@fooishbar.org>
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
38 #include "xkbcommon/xkbcommon.h"
39 #include "tools-common.h"
41 #include <wayland-client.h>
42 #include "xdg-shell-client-protocol.h"
43 #include <wayland-util.h>
45 #define MIN(a, b) ((a) < (b) ? (a) : (b))
47 struct interactive_dpy {
48 struct wl_display *dpy;
49 struct wl_compositor *compositor;
50 struct xdg_wm_base *shell;
54 struct xkb_context *ctx;
56 struct wl_surface *wl_surf;
57 struct xdg_surface *xdg_surf;
58 struct xdg_toplevel *xdg_top;
63 struct interactive_seat {
64 struct interactive_dpy *inter;
66 struct wl_seat *wl_seat;
67 struct wl_keyboard *wl_kbd;
68 struct wl_pointer *wl_pointer;
69 uint32_t version; /* ... of wl_seat */
70 uint32_t global_name; /* an ID of sorts */
71 char *name_str; /* a descriptor */
73 struct xkb_keymap *keymap;
74 struct xkb_state *state;
79 static bool terminate;
83 create_tmpfile_cloexec(char *tmpname)
85 int fd = mkostemp(tmpname, O_CLOEXEC);
91 /* The following utility functions are taken from Weston's
92 * shared/os-compatibility.c. */
94 os_fd_set_cloexec(int fd)
101 flags = fcntl(fd, F_GETFD);
105 if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
112 set_cloexec_or_close(int fd)
114 if (os_fd_set_cloexec(fd) != 0) {
122 create_tmpfile_cloexec(char *tmpname)
124 int fd = mkstemp(tmpname);
126 fd = set_cloexec_or_close(fd);
134 os_resize_anonymous_file(int fd, off_t size)
137 #ifdef HAVE_POSIX_FALLOCATE
138 ret = posix_fallocate(fd, 0, size);
142 * Filesystems that do support fallocate will return EINVAL
143 * or EOPNOTSUPP, fallback to ftruncate() then.
145 if (ret != EINVAL && ret != EOPNOTSUPP)
148 ret = ftruncate(fd, size);
155 * Create a new, unique, anonymous file of the given size, and
156 * return the file descriptor for it. The file descriptor is set
157 * CLOEXEC. The file is immediately suitable for mmap()'ing
158 * the given size at offset zero.
160 * The file should not have a permanent backing store like a disk,
161 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
163 * The file name is deleted from the file system.
165 * The file is suitable for buffer sharing between processes by
166 * transmitting the file descriptor over Unix sockets using the
167 * SCM_RIGHTS methods.
169 * If the C library implements posix_fallocate(), it is used to
170 * guarantee that disk space is available for the file at the
171 * given size. If disk space is insufficent, errno is set to ENOSPC.
172 * If posix_fallocate() is not supported, program will fallback
173 * to ftruncate() instead.
176 os_create_anonymous_file(off_t size)
178 static const char template[] = "/weston-shared-XXXXXX";
184 path = getenv("XDG_RUNTIME_DIR");
190 name = malloc(strlen(path) + sizeof(template));
195 strcat(name, template);
197 fd = create_tmpfile_cloexec(name);
204 ret = os_resize_anonymous_file(fd, size);
215 buffer_release(void *data, struct wl_buffer *buffer)
217 wl_buffer_destroy(buffer);
220 static const struct wl_buffer_listener buffer_listener = {
225 buffer_create(struct interactive_dpy *inter, uint32_t width, uint32_t height)
227 struct wl_shm_pool *pool;
228 struct wl_buffer *buf;
229 struct wl_region *opaque;
235 switch (inter->shm_format) {
236 case WL_SHM_FORMAT_ARGB8888:
237 case WL_SHM_FORMAT_XRGB8888:
238 case WL_SHM_FORMAT_ABGR8888:
239 case WL_SHM_FORMAT_XBGR8888:
242 case WL_SHM_FORMAT_RGB565:
243 case WL_SHM_FORMAT_BGR565:
247 fprintf(stderr, "Unsupported SHM format %d\n", inter->shm_format);
251 size = stride * height;
252 fd = os_create_anonymous_file(size);
254 fprintf(stderr, "Couldn't create surface buffer\n");
258 map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
259 if (map == MAP_FAILED) {
260 fprintf(stderr, "Couldn't mmap surface buffer\n");
263 memset(map, 0xff, size);
266 pool = wl_shm_create_pool(inter->shm, fd, size);
267 buf = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
269 wl_buffer_add_listener(buf, &buffer_listener, inter);
271 wl_surface_attach(inter->wl_surf, buf, 0, 0);
272 wl_surface_damage(inter->wl_surf, 0, 0, width, height);
274 opaque = wl_compositor_create_region(inter->compositor);
275 wl_region_add(opaque, 0, 0, width, height);
276 wl_surface_set_opaque_region(inter->wl_surf, opaque);
277 wl_region_destroy(opaque);
279 wl_shm_pool_destroy(pool);
284 surface_configure(void *data, struct xdg_surface *surface,
287 struct interactive_dpy *inter = data;
289 xdg_surface_ack_configure(inter->xdg_surf, serial);
290 wl_surface_commit(inter->wl_surf);
293 static const struct xdg_surface_listener surface_listener = {
298 toplevel_configure(void *data, struct xdg_toplevel *toplevel,
299 int32_t width, int32_t height, struct wl_array *states)
301 struct interactive_dpy *inter = data;
308 buffer_create(inter, width, height);
312 toplevel_close(void *data, struct xdg_toplevel *toplevel)
317 static const struct xdg_toplevel_listener toplevel_listener = {
322 static void surface_create(struct interactive_dpy *inter)
324 inter->wl_surf = wl_compositor_create_surface(inter->compositor);
325 inter->xdg_surf = xdg_wm_base_get_xdg_surface(inter->shell, inter->wl_surf);
326 xdg_surface_add_listener(inter->xdg_surf, &surface_listener, inter);
327 inter->xdg_top = xdg_surface_get_toplevel(inter->xdg_surf);
328 xdg_toplevel_add_listener(inter->xdg_top, &toplevel_listener, inter);
329 xdg_toplevel_set_title(inter->xdg_top, "xkbcommon event tester");
330 xdg_toplevel_set_app_id(inter->xdg_top,
331 "org.xkbcommon.test.interactive-wayland");
332 wl_surface_commit(inter->wl_surf);
336 shell_ping(void *data, struct xdg_wm_base *shell, uint32_t serial)
338 xdg_wm_base_pong(shell, serial);
341 static const struct xdg_wm_base_listener shell_listener = {
346 kbd_keymap(void *data, struct wl_keyboard *wl_kbd, uint32_t format,
347 int fd, uint32_t size)
349 struct interactive_seat *seat = data;
352 buf = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
353 if (buf == MAP_FAILED) {
354 fprintf(stderr, "Failed to mmap keymap: %d\n", errno);
359 seat->keymap = xkb_keymap_new_from_buffer(seat->inter->ctx, buf, size - 1,
360 XKB_KEYMAP_FORMAT_TEXT_V1,
361 XKB_KEYMAP_COMPILE_NO_FLAGS);
365 fprintf(stderr, "Failed to compile keymap!\n");
369 seat->state = xkb_state_new(seat->keymap);
371 fprintf(stderr, "Failed to create XKB state!\n");
377 kbd_enter(void *data, struct wl_keyboard *wl_kbd, uint32_t serial,
378 struct wl_surface *surf, struct wl_array *keys)
383 kbd_leave(void *data, struct wl_keyboard *wl_kbd, uint32_t serial,
384 struct wl_surface *surf)
389 kbd_key(void *data, struct wl_keyboard *wl_kbd, uint32_t serial, uint32_t time,
390 uint32_t key, uint32_t state)
392 struct interactive_seat *seat = data;
394 if (state != WL_KEYBOARD_KEY_STATE_PRESSED)
397 printf("%s: ", seat->name_str);
398 tools_print_keycode_state(seat->state, NULL, key + 8,
399 XKB_CONSUMED_MODE_XKB);
402 if (xkb_state_key_get_one_sym(seat->state, key + 8) == XKB_KEY_Escape)
407 kbd_modifiers(void *data, struct wl_keyboard *wl_kbd, uint32_t serial,
408 uint32_t mods_depressed, uint32_t mods_latched,
409 uint32_t mods_locked, uint32_t group)
411 struct interactive_seat *seat = data;
413 xkb_state_update_mask(seat->state, mods_depressed, mods_latched,
414 mods_locked, 0, 0, group);
418 kbd_repeat_info(void *data, struct wl_keyboard *wl_kbd, int32_t rate,
423 static const struct wl_keyboard_listener kbd_listener = {
433 pointer_enter(void *data, struct wl_pointer *wl_pointer, uint32_t serial,
434 struct wl_surface *surf, wl_fixed_t fsx, wl_fixed_t fsy)
439 pointer_leave(void *data, struct wl_pointer *wl_pointer, uint32_t serial,
440 struct wl_surface *surf)
445 pointer_motion(void *data, struct wl_pointer *wl_pointer, uint32_t time,
446 wl_fixed_t fsx, wl_fixed_t fsy)
451 pointer_button(void *data, struct wl_pointer *wl_pointer, uint32_t serial,
452 uint32_t time, uint32_t button, uint32_t state)
454 struct interactive_seat *seat = data;
456 xdg_toplevel_move(seat->inter->xdg_top, seat->wl_seat, serial);
460 pointer_axis(void *data, struct wl_pointer *wl_pointer, uint32_t time,
461 uint32_t axis, wl_fixed_t value)
466 pointer_frame(void *data, struct wl_pointer *wl_pointer)
471 pointer_axis_source(void *data, struct wl_pointer *wl_pointer, uint32_t source)
476 pointer_axis_stop(void *data, struct wl_pointer *wl_pointer, uint32_t time,
482 pointer_axis_discrete(void *data, struct wl_pointer *wl_pointer, uint32_t time,
487 static const struct wl_pointer_listener pointer_listener = {
496 pointer_axis_discrete
500 seat_capabilities(void *data, struct wl_seat *wl_seat, uint32_t caps)
502 struct interactive_seat *seat = data;
504 if (!seat->wl_kbd && (caps & WL_SEAT_CAPABILITY_KEYBOARD)) {
505 seat->wl_kbd = wl_seat_get_keyboard(seat->wl_seat);
506 wl_keyboard_add_listener(seat->wl_kbd, &kbd_listener, seat);
508 else if (seat->wl_kbd && !(caps & WL_SEAT_CAPABILITY_KEYBOARD)) {
509 if (seat->version >= WL_SEAT_RELEASE_SINCE_VERSION)
510 wl_keyboard_release(seat->wl_kbd);
512 wl_keyboard_destroy(seat->wl_kbd);
514 xkb_state_unref(seat->state);
515 xkb_keymap_unref(seat->keymap);
522 if (!seat->wl_pointer && (caps & WL_SEAT_CAPABILITY_POINTER)) {
523 seat->wl_pointer = wl_seat_get_pointer(seat->wl_seat);
524 wl_pointer_add_listener(seat->wl_pointer, &pointer_listener,
527 else if (seat->wl_pointer && !(caps & WL_SEAT_CAPABILITY_POINTER)) {
528 if (seat->version >= WL_SEAT_RELEASE_SINCE_VERSION)
529 wl_pointer_release(seat->wl_pointer);
531 wl_pointer_destroy(seat->wl_pointer);
532 seat->wl_pointer = NULL;
537 seat_name(void *data, struct wl_seat *wl_seat, const char *name)
539 struct interactive_seat *seat = data;
541 free(seat->name_str);
542 seat->name_str = strdup(name);
545 static const struct wl_seat_listener seat_listener = {
551 seat_create(struct interactive_dpy *inter, struct wl_registry *registry,
552 uint32_t name, uint32_t version)
555 struct interactive_seat *seat = calloc(1, sizeof(*seat));
557 seat->global_name = name;
559 seat->wl_seat = wl_registry_bind(registry, name, &wl_seat_interface,
561 wl_seat_add_listener(seat->wl_seat, &seat_listener, seat);
562 ret = asprintf(&seat->name_str, "seat:%d",
563 wl_proxy_get_id((struct wl_proxy *) seat->wl_seat));
565 wl_list_insert(&inter->seats, &seat->link);
569 seat_destroy(struct interactive_seat *seat)
572 if (seat->version >= WL_SEAT_RELEASE_SINCE_VERSION)
573 wl_keyboard_release(seat->wl_kbd);
575 wl_keyboard_destroy(seat->wl_kbd);
577 xkb_state_unref(seat->state);
578 xkb_keymap_unref(seat->keymap);
581 if (seat->wl_pointer) {
582 if (seat->version >= WL_SEAT_RELEASE_SINCE_VERSION)
583 wl_pointer_release(seat->wl_pointer);
585 wl_pointer_destroy(seat->wl_pointer);
588 if (seat->version >= WL_SEAT_RELEASE_SINCE_VERSION)
589 wl_seat_release(seat->wl_seat);
591 wl_seat_destroy(seat->wl_seat);
593 free(seat->name_str);
594 wl_list_remove(&seat->link);
599 registry_global(void *data, struct wl_registry *registry, uint32_t name,
600 const char *interface, uint32_t version)
602 struct interactive_dpy *inter = data;
604 if (strcmp(interface, "wl_seat") == 0) {
605 seat_create(inter, registry, name, version);
607 else if (strcmp(interface, "xdg_wm_base") == 0) {
608 inter->shell = wl_registry_bind(registry, name,
609 &xdg_wm_base_interface,
611 xdg_wm_base_add_listener(inter->shell, &shell_listener, inter);
613 else if (strcmp(interface, "wl_compositor") == 0) {
614 inter->compositor = wl_registry_bind(registry, name,
615 &wl_compositor_interface,
618 else if (strcmp(interface, "wl_shm") == 0) {
619 inter->shm = wl_registry_bind(registry, name, &wl_shm_interface,
625 registry_delete(void *data, struct wl_registry *registry, uint32_t name)
627 struct interactive_dpy *inter = data;
628 struct interactive_seat *seat, *tmp;
630 wl_list_for_each_safe(seat, tmp, &inter->seats, link) {
631 if (seat->global_name != name)
638 static const struct wl_registry_listener registry_listener = {
644 dpy_disconnect(struct interactive_dpy *inter)
646 struct interactive_seat *seat, *tmp;
648 wl_list_for_each_safe(seat, tmp, &inter->seats, link)
652 xdg_surface_destroy(inter->xdg_surf);
654 xdg_toplevel_destroy(inter->xdg_top);
656 wl_surface_destroy(inter->wl_surf);
658 xdg_wm_base_destroy(inter->shell);
659 if (inter->compositor)
660 wl_compositor_destroy(inter->compositor);
662 wl_shm_destroy(inter->shm);
664 /* Do one last roundtrip to try to destroy our wl_buffer. */
665 wl_display_roundtrip(inter->dpy);
667 xkb_context_unref(inter->ctx);
668 wl_display_disconnect(inter->dpy);
672 main(int argc, char *argv[])
675 struct interactive_dpy inter;
676 struct wl_registry *registry;
679 ret = strcmp(argv[1], "--help");
680 fprintf(ret ? stderr : stdout, "Usage: %s [--help]\n", argv[0]);
682 fprintf(stderr, "unrecognized option: %s\n", argv[1]);
683 return ret ? EXIT_INVALID_USAGE : EXIT_SUCCESS;
686 setlocale(LC_ALL, "");
688 memset(&inter, 0, sizeof(inter));
689 wl_list_init(&inter.seats);
691 inter.dpy = wl_display_connect(NULL);
693 fprintf(stderr, "Couldn't connect to Wayland server\n");
698 inter.ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
701 fprintf(stderr, "Couldn't create xkb context\n");
705 registry = wl_display_get_registry(inter.dpy);
706 wl_registry_add_listener(registry, ®istry_listener, &inter);
708 /* The first roundtrip gets the list of advertised globals. */
709 wl_display_roundtrip(inter.dpy);
711 /* The second roundtrip dispatches the events sent after binding, e.g.
712 * after binding to wl_seat globals in the first roundtrip, we will get
713 * the wl_seat::capabilities event in this roundtrip. */
714 wl_display_roundtrip(inter.dpy);
716 if (!inter.shell || !inter.shm || !inter.compositor) {
717 fprintf(stderr, "Required Wayland interfaces %s%s%s unsupported\n",
718 (inter.shell) ? "" : "xdg_shell ",
719 (inter.shm) ? "" : "wl_shm",
720 (inter.compositor) ? "" : "wl_compositor");
725 surface_create(&inter);
727 tools_disable_stdin_echo();
729 ret = wl_display_dispatch(inter.dpy);
730 } while (ret >= 0 && !terminate);
731 tools_enable_stdin_echo();
733 wl_registry_destroy(registry);
735 dpy_disconnect(&inter);
737 exit(ret >= 0 ? EXIT_SUCCESS : EXIT_FAILURE);