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.
33 #include "xkbcommon/xkbcommon.h"
36 #include <wayland-client.h>
37 #include "xdg-shell-unstable-v6-client-protocol.h"
38 #include <wayland-util.h>
40 struct interactive_dpy {
41 struct wl_display *dpy;
42 struct wl_compositor *compositor;
43 struct zxdg_shell_v6 *shell;
47 struct xkb_context *ctx;
49 struct wl_surface *wl_surf;
50 struct zxdg_surface_v6 *xdg_surf;
51 struct zxdg_toplevel_v6 *xdg_top;
56 struct interactive_seat {
57 struct interactive_dpy *inter;
59 struct wl_seat *wl_seat;
60 struct wl_keyboard *wl_kbd;
61 struct wl_pointer *wl_pointer;
62 uint32_t version; /* ... of wl_seat */
63 uint32_t global_name; /* an ID of sorts */
64 char *name_str; /* a descriptor */
66 struct xkb_keymap *keymap;
67 struct xkb_state *state;
72 static bool terminate;
76 create_tmpfile_cloexec(char *tmpname)
78 int fd = mkostemp(tmpname, O_CLOEXEC);
84 /* The following utility functions are taken from Weston's
85 * shared/os-compatibility.c. */
87 os_fd_set_cloexec(int fd)
94 flags = fcntl(fd, F_GETFD);
98 if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
105 set_cloexec_or_close(int fd)
107 if (os_fd_set_cloexec(fd) != 0) {
115 create_tmpfile_cloexec(char *tmpname)
117 int fd = mkstemp(tmpname);
119 fd = set_cloexec_or_close(fd);
127 * Create a new, unique, anonymous file of the given size, and
128 * return the file descriptor for it. The file descriptor is set
129 * CLOEXEC. The file is immediately suitable for mmap()'ing
130 * the given size at offset zero.
132 * The file should not have a permanent backing store like a disk,
133 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
135 * The file name is deleted from the file system.
137 * The file is suitable for buffer sharing between processes by
138 * transmitting the file descriptor over Unix sockets using the
139 * SCM_RIGHTS methods.
141 * If the C library implements posix_fallocate(), it is used to
142 * guarantee that disk space is available for the file at the
143 * given size. If disk space is insufficent, errno is set to ENOSPC.
144 * If posix_fallocate() is not supported, program may receive
145 * SIGBUS on accessing mmap()'ed file contents instead.
148 os_create_anonymous_file(off_t size)
150 static const char template[] = "/weston-shared-XXXXXX";
156 path = getenv("XDG_RUNTIME_DIR");
162 name = malloc(strlen(path) + sizeof(template));
167 strcat(name, template);
169 fd = create_tmpfile_cloexec(name);
176 #ifdef HAVE_POSIX_FALLOCATE
177 ret = posix_fallocate(fd, 0, size);
184 ret = ftruncate(fd, size);
195 buffer_release(void *data, struct wl_buffer *buffer)
197 wl_buffer_destroy(buffer);
200 static const struct wl_buffer_listener buffer_listener = {
205 buffer_create(struct interactive_dpy *inter, uint32_t width, uint32_t height)
207 struct wl_shm_pool *pool;
208 struct wl_buffer *buf;
209 struct wl_region *opaque;
215 switch (inter->shm_format) {
216 case WL_SHM_FORMAT_ARGB8888:
217 case WL_SHM_FORMAT_XRGB8888:
218 case WL_SHM_FORMAT_ABGR8888:
219 case WL_SHM_FORMAT_XBGR8888:
222 case WL_SHM_FORMAT_RGB565:
223 case WL_SHM_FORMAT_BGR565:
227 fprintf(stderr, "Unsupported SHM format %d\n", inter->shm_format);
231 size = stride * height;
232 fd = os_create_anonymous_file(size);
234 fprintf(stderr, "Couldn't create surface buffer\n");
238 map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
239 if (map == MAP_FAILED) {
240 fprintf(stderr, "Couldn't mmap surface buffer\n");
243 memset(map, 0xff, size);
246 pool = wl_shm_create_pool(inter->shm, fd, size);
247 buf = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
249 wl_buffer_add_listener(buf, &buffer_listener, inter);
251 wl_surface_attach(inter->wl_surf, buf, 0, 0);
252 wl_surface_damage(inter->wl_surf, 0, 0, width, height);
254 opaque = wl_compositor_create_region(inter->compositor);
255 wl_region_add(opaque, 0, 0, width, height);
256 wl_surface_set_opaque_region(inter->wl_surf, opaque);
257 wl_region_destroy(opaque);
259 wl_shm_pool_destroy(pool);
264 surface_configure(void *data, struct zxdg_surface_v6 *surface,
267 struct interactive_dpy *inter = data;
269 zxdg_surface_v6_ack_configure(inter->xdg_surf, serial);
270 wl_surface_commit(inter->wl_surf);
273 static const struct zxdg_surface_v6_listener surface_listener = {
278 toplevel_configure(void *data, struct zxdg_toplevel_v6 *toplevel,
279 int32_t width, int32_t height, struct wl_array *states)
281 struct interactive_dpy *inter = data;
288 buffer_create(inter, width, height);
292 toplevel_close(void *data, struct zxdg_toplevel_v6 *toplevel)
297 static const struct zxdg_toplevel_v6_listener toplevel_listener = {
302 static void surface_create(struct interactive_dpy *inter)
304 inter->wl_surf = wl_compositor_create_surface(inter->compositor);
305 inter->xdg_surf = zxdg_shell_v6_get_xdg_surface(inter->shell,
307 zxdg_surface_v6_add_listener(inter->xdg_surf, &surface_listener, inter);
308 inter->xdg_top = zxdg_surface_v6_get_toplevel(inter->xdg_surf);
309 zxdg_toplevel_v6_add_listener(inter->xdg_top, &toplevel_listener, inter);
310 zxdg_toplevel_v6_set_title(inter->xdg_top, "xkbcommon event tester");
311 zxdg_toplevel_v6_set_app_id(inter->xdg_top,
312 "org.xkbcommon.test.interactive-wayland");
313 wl_surface_commit(inter->wl_surf);
317 shell_ping(void *data, struct zxdg_shell_v6 *shell, uint32_t serial)
319 zxdg_shell_v6_pong(shell, serial);
322 static const struct zxdg_shell_v6_listener shell_listener = {
327 kbd_keymap(void *data, struct wl_keyboard *wl_kbd, uint32_t format,
328 int fd, uint32_t size)
330 struct interactive_seat *seat = data;
333 buf = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
334 if (buf == MAP_FAILED) {
335 fprintf(stderr, "Failed to mmap keymap: %d\n", errno);
340 seat->keymap = xkb_keymap_new_from_buffer(seat->inter->ctx, buf, size - 1,
341 XKB_KEYMAP_FORMAT_TEXT_V1, 0);
345 fprintf(stderr, "Failed to compile keymap!\n");
349 seat->state = xkb_state_new(seat->keymap);
351 fprintf(stderr, "Failed to create XKB state!\n");
357 kbd_enter(void *data, struct wl_keyboard *wl_kbd, uint32_t serial,
358 struct wl_surface *surf, struct wl_array *keys)
363 kbd_leave(void *data, struct wl_keyboard *wl_kbd, uint32_t serial,
364 struct wl_surface *surf)
369 kbd_key(void *data, struct wl_keyboard *wl_kbd, uint32_t serial, uint32_t time,
370 uint32_t key, uint32_t state)
372 struct interactive_seat *seat = data;
374 if (state != WL_KEYBOARD_KEY_STATE_PRESSED)
377 printf("%s: ", seat->name_str);
378 test_print_keycode_state(seat->state, NULL, key + 8,
379 XKB_CONSUMED_MODE_XKB);
382 if (xkb_state_key_get_one_sym(seat->state, key + 8) == XKB_KEY_Escape)
387 kbd_modifiers(void *data, struct wl_keyboard *wl_kbd, uint32_t serial,
388 uint32_t mods_depressed, uint32_t mods_latched,
389 uint32_t mods_locked, uint32_t group)
391 struct interactive_seat *seat = data;
393 xkb_state_update_mask(seat->state, mods_depressed, mods_latched,
394 mods_locked, 0, 0, group);
398 kbd_repeat_info(void *data, struct wl_keyboard *wl_kbd, int32_t rate,
403 static const struct wl_keyboard_listener kbd_listener = {
413 pointer_enter(void *data, struct wl_pointer *wl_pointer, uint32_t serial,
414 struct wl_surface *surf, wl_fixed_t fsx, wl_fixed_t fsy)
419 pointer_leave(void *data, struct wl_pointer *wl_pointer, uint32_t serial,
420 struct wl_surface *surf)
425 pointer_motion(void *data, struct wl_pointer *wl_pointer, uint32_t time,
426 wl_fixed_t fsx, wl_fixed_t fsy)
431 pointer_button(void *data, struct wl_pointer *wl_pointer, uint32_t serial,
432 uint32_t time, uint32_t button, uint32_t state)
434 struct interactive_seat *seat = data;
436 zxdg_toplevel_v6_move(seat->inter->xdg_top, seat->wl_seat, serial);
440 pointer_axis(void *data, struct wl_pointer *wl_pointer, uint32_t time,
441 uint32_t axis, wl_fixed_t value)
446 pointer_frame(void *data, struct wl_pointer *wl_pointer)
451 pointer_axis_source(void *data, struct wl_pointer *wl_pointer, uint32_t source)
456 pointer_axis_stop(void *data, struct wl_pointer *wl_pointer, uint32_t time,
462 pointer_axis_discrete(void *data, struct wl_pointer *wl_pointer, uint32_t time,
467 static const struct wl_pointer_listener pointer_listener = {
476 pointer_axis_discrete
480 seat_capabilities(void *data, struct wl_seat *wl_seat, uint32_t caps)
482 struct interactive_seat *seat = data;
484 if (!seat->wl_kbd && (caps & WL_SEAT_CAPABILITY_KEYBOARD)) {
485 seat->wl_kbd = wl_seat_get_keyboard(seat->wl_seat);
486 wl_keyboard_add_listener(seat->wl_kbd, &kbd_listener, seat);
488 else if (seat->wl_kbd && !(caps & WL_SEAT_CAPABILITY_KEYBOARD)) {
489 if (seat->version >= WL_SEAT_RELEASE_SINCE_VERSION)
490 wl_keyboard_release(seat->wl_kbd);
492 wl_keyboard_destroy(seat->wl_kbd);
494 xkb_state_unref(seat->state);
495 xkb_keymap_unref(seat->keymap);
502 if (!seat->wl_pointer && (caps & WL_SEAT_CAPABILITY_POINTER)) {
503 seat->wl_pointer = wl_seat_get_pointer(seat->wl_seat);
504 wl_pointer_add_listener(seat->wl_pointer, &pointer_listener,
507 else if (seat->wl_pointer && !(caps & WL_SEAT_CAPABILITY_POINTER)) {
508 if (seat->version >= WL_SEAT_RELEASE_SINCE_VERSION)
509 wl_pointer_release(seat->wl_pointer);
511 wl_pointer_destroy(seat->wl_pointer);
512 seat->wl_pointer = NULL;
517 seat_name(void *data, struct wl_seat *wl_seat, const char *name)
519 struct interactive_seat *seat = data;
521 free(seat->name_str);
522 seat->name_str = strdup(name);
525 static const struct wl_seat_listener seat_listener = {
531 seat_create(struct interactive_dpy *inter, struct wl_registry *registry,
532 uint32_t name, uint32_t version)
535 struct interactive_seat *seat = calloc(1, sizeof(*seat));
537 seat->global_name = name;
539 seat->wl_seat = wl_registry_bind(registry, name, &wl_seat_interface,
541 wl_seat_add_listener(seat->wl_seat, &seat_listener, seat);
542 ret = asprintf(&seat->name_str, "seat:%d",
543 wl_proxy_get_id((struct wl_proxy *) seat->wl_seat));
545 wl_list_insert(&inter->seats, &seat->link);
549 seat_destroy(struct interactive_seat *seat)
552 if (seat->version >= WL_SEAT_RELEASE_SINCE_VERSION)
553 wl_keyboard_release(seat->wl_kbd);
555 wl_keyboard_destroy(seat->wl_kbd);
557 xkb_state_unref(seat->state);
558 xkb_keymap_unref(seat->keymap);
561 if (seat->wl_pointer) {
562 if (seat->version >= WL_SEAT_RELEASE_SINCE_VERSION)
563 wl_pointer_release(seat->wl_pointer);
565 wl_pointer_destroy(seat->wl_pointer);
568 if (seat->version >= WL_SEAT_RELEASE_SINCE_VERSION)
569 wl_seat_release(seat->wl_seat);
571 wl_seat_destroy(seat->wl_seat);
573 free(seat->name_str);
574 wl_list_remove(&seat->link);
579 registry_global(void *data, struct wl_registry *registry, uint32_t name,
580 const char *interface, uint32_t version)
582 struct interactive_dpy *inter = data;
584 if (strcmp(interface, "wl_seat") == 0) {
585 seat_create(inter, registry, name, version);
587 else if (strcmp(interface, "zxdg_shell_v6") == 0) {
588 inter->shell = wl_registry_bind(registry, name,
589 &zxdg_shell_v6_interface,
591 zxdg_shell_v6_add_listener(inter->shell, &shell_listener, inter);
593 else if (strcmp(interface, "wl_compositor") == 0) {
594 inter->compositor = wl_registry_bind(registry, name,
595 &wl_compositor_interface,
598 else if (strcmp(interface, "wl_shm") == 0) {
599 inter->shm = wl_registry_bind(registry, name, &wl_shm_interface,
605 registry_delete(void *data, struct wl_registry *registry, uint32_t name)
607 struct interactive_dpy *inter = data;
608 struct interactive_seat *seat, *tmp;
610 wl_list_for_each_safe(seat, tmp, &inter->seats, link) {
611 if (seat->global_name != name)
618 static const struct wl_registry_listener registry_listener = {
624 dpy_disconnect(struct interactive_dpy *inter)
626 struct interactive_seat *seat, *tmp;
628 wl_list_for_each_safe(seat, tmp, &inter->seats, link)
632 zxdg_surface_v6_destroy(inter->xdg_surf);
634 zxdg_toplevel_v6_destroy(inter->xdg_top);
636 wl_surface_destroy(inter->wl_surf);
638 zxdg_shell_v6_destroy(inter->shell);
639 if (inter->compositor)
640 wl_compositor_destroy(inter->compositor);
642 wl_shm_destroy(inter->shm);
644 /* Do one last roundtrip to try to destroy our wl_buffer. */
645 wl_display_roundtrip(inter->dpy);
647 xkb_context_unref(inter->ctx);
648 wl_display_disconnect(inter->dpy);
652 main(int argc, char *argv[])
655 struct interactive_dpy inter;
656 struct wl_registry *registry;
658 setlocale(LC_ALL, "");
660 memset(&inter, 0, sizeof(inter));
661 wl_list_init(&inter.seats);
663 inter.dpy = wl_display_connect(NULL);
665 fprintf(stderr, "Couldn't connect to Wayland server\n");
670 inter.ctx = test_get_context(0);
673 fprintf(stderr, "Couldn't create xkb context\n");
677 registry = wl_display_get_registry(inter.dpy);
678 wl_registry_add_listener(registry, ®istry_listener, &inter);
680 /* The first roundtrip gets the list of advertised globals. */
681 wl_display_roundtrip(inter.dpy);
683 /* The second roundtrip dispatches the events sent after binding, e.g.
684 * after binding to wl_seat globals in the first roundtrip, we will get
685 * the wl_seat::capabilities event in this roundtrip. */
686 wl_display_roundtrip(inter.dpy);
688 if (!inter.shell || !inter.shm || !inter.compositor) {
689 fprintf(stderr, "Required Wayland interfaces %s%s%s unsupported\n",
690 (inter.shell) ? "" : "xdg_shell ",
691 (inter.shm) ? "" : "wl_shm",
692 (inter.compositor) ? "" : "wl_compositor");
697 surface_create(&inter);
699 test_disable_stdin_echo();
701 ret = wl_display_dispatch(inter.dpy);
702 } while (ret >= 0 && !terminate);
703 test_enable_stdin_echo();
705 wl_registry_destroy(registry);
707 dpy_disconnect(&inter);
709 exit(ret >= 0 ? EXIT_SUCCESS : EXIT_FAILURE);