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 MAX(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 * Create a new, unique, anonymous file of the given size, and
135 * return the file descriptor for it. The file descriptor is set
136 * CLOEXEC. The file is immediately suitable for mmap()'ing
137 * the given size at offset zero.
139 * The file should not have a permanent backing store like a disk,
140 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
142 * The file name is deleted from the file system.
144 * The file is suitable for buffer sharing between processes by
145 * transmitting the file descriptor over Unix sockets using the
146 * SCM_RIGHTS methods.
148 * If the C library implements posix_fallocate(), it is used to
149 * guarantee that disk space is available for the file at the
150 * given size. If disk space is insufficent, errno is set to ENOSPC.
151 * If posix_fallocate() is not supported, program may receive
152 * SIGBUS on accessing mmap()'ed file contents instead.
155 os_create_anonymous_file(off_t size)
157 static const char template[] = "/weston-shared-XXXXXX";
163 path = getenv("XDG_RUNTIME_DIR");
169 name = malloc(strlen(path) + sizeof(template));
174 strcat(name, template);
176 fd = create_tmpfile_cloexec(name);
183 #ifdef HAVE_POSIX_FALLOCATE
184 ret = posix_fallocate(fd, 0, size);
191 ret = ftruncate(fd, size);
202 buffer_release(void *data, struct wl_buffer *buffer)
204 wl_buffer_destroy(buffer);
207 static const struct wl_buffer_listener buffer_listener = {
212 buffer_create(struct interactive_dpy *inter, uint32_t width, uint32_t height)
214 struct wl_shm_pool *pool;
215 struct wl_buffer *buf;
216 struct wl_region *opaque;
222 switch (inter->shm_format) {
223 case WL_SHM_FORMAT_ARGB8888:
224 case WL_SHM_FORMAT_XRGB8888:
225 case WL_SHM_FORMAT_ABGR8888:
226 case WL_SHM_FORMAT_XBGR8888:
229 case WL_SHM_FORMAT_RGB565:
230 case WL_SHM_FORMAT_BGR565:
234 fprintf(stderr, "Unsupported SHM format %d\n", inter->shm_format);
238 size = stride * height;
239 fd = os_create_anonymous_file(size);
241 fprintf(stderr, "Couldn't create surface buffer\n");
245 map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
246 if (map == MAP_FAILED) {
247 fprintf(stderr, "Couldn't mmap surface buffer\n");
250 memset(map, 0xff, size);
253 pool = wl_shm_create_pool(inter->shm, fd, size);
254 buf = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
256 wl_buffer_add_listener(buf, &buffer_listener, inter);
258 wl_surface_attach(inter->wl_surf, buf, 0, 0);
259 wl_surface_damage(inter->wl_surf, 0, 0, width, height);
261 opaque = wl_compositor_create_region(inter->compositor);
262 wl_region_add(opaque, 0, 0, width, height);
263 wl_surface_set_opaque_region(inter->wl_surf, opaque);
264 wl_region_destroy(opaque);
266 wl_shm_pool_destroy(pool);
271 surface_configure(void *data, struct xdg_surface *surface,
274 struct interactive_dpy *inter = data;
276 xdg_surface_ack_configure(inter->xdg_surf, serial);
277 wl_surface_commit(inter->wl_surf);
280 static const struct xdg_surface_listener surface_listener = {
285 toplevel_configure(void *data, struct xdg_toplevel *toplevel,
286 int32_t width, int32_t height, struct wl_array *states)
288 struct interactive_dpy *inter = data;
295 buffer_create(inter, width, height);
299 toplevel_close(void *data, struct xdg_toplevel *toplevel)
304 static const struct xdg_toplevel_listener toplevel_listener = {
309 static void surface_create(struct interactive_dpy *inter)
311 inter->wl_surf = wl_compositor_create_surface(inter->compositor);
312 inter->xdg_surf = xdg_wm_base_get_xdg_surface(inter->shell, inter->wl_surf);
313 xdg_surface_add_listener(inter->xdg_surf, &surface_listener, inter);
314 inter->xdg_top = xdg_surface_get_toplevel(inter->xdg_surf);
315 xdg_toplevel_add_listener(inter->xdg_top, &toplevel_listener, inter);
316 xdg_toplevel_set_title(inter->xdg_top, "xkbcommon event tester");
317 xdg_toplevel_set_app_id(inter->xdg_top,
318 "org.xkbcommon.test.interactive-wayland");
319 wl_surface_commit(inter->wl_surf);
323 shell_ping(void *data, struct xdg_wm_base *shell, uint32_t serial)
325 xdg_wm_base_pong(shell, serial);
328 static const struct xdg_wm_base_listener shell_listener = {
333 kbd_keymap(void *data, struct wl_keyboard *wl_kbd, uint32_t format,
334 int fd, uint32_t size)
336 struct interactive_seat *seat = data;
339 buf = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
340 if (buf == MAP_FAILED) {
341 fprintf(stderr, "Failed to mmap keymap: %d\n", errno);
346 seat->keymap = xkb_keymap_new_from_buffer(seat->inter->ctx, buf, size - 1,
347 XKB_KEYMAP_FORMAT_TEXT_V1,
348 XKB_KEYMAP_COMPILE_NO_FLAGS);
352 fprintf(stderr, "Failed to compile keymap!\n");
356 seat->state = xkb_state_new(seat->keymap);
358 fprintf(stderr, "Failed to create XKB state!\n");
364 kbd_enter(void *data, struct wl_keyboard *wl_kbd, uint32_t serial,
365 struct wl_surface *surf, struct wl_array *keys)
370 kbd_leave(void *data, struct wl_keyboard *wl_kbd, uint32_t serial,
371 struct wl_surface *surf)
376 kbd_key(void *data, struct wl_keyboard *wl_kbd, uint32_t serial, uint32_t time,
377 uint32_t key, uint32_t state)
379 struct interactive_seat *seat = data;
381 if (state != WL_KEYBOARD_KEY_STATE_PRESSED)
384 printf("%s: ", seat->name_str);
385 tools_print_keycode_state(seat->state, NULL, key + 8,
386 XKB_CONSUMED_MODE_XKB);
389 if (xkb_state_key_get_one_sym(seat->state, key + 8) == XKB_KEY_Escape)
394 kbd_modifiers(void *data, struct wl_keyboard *wl_kbd, uint32_t serial,
395 uint32_t mods_depressed, uint32_t mods_latched,
396 uint32_t mods_locked, uint32_t group)
398 struct interactive_seat *seat = data;
400 xkb_state_update_mask(seat->state, mods_depressed, mods_latched,
401 mods_locked, 0, 0, group);
405 kbd_repeat_info(void *data, struct wl_keyboard *wl_kbd, int32_t rate,
410 static const struct wl_keyboard_listener kbd_listener = {
420 pointer_enter(void *data, struct wl_pointer *wl_pointer, uint32_t serial,
421 struct wl_surface *surf, wl_fixed_t fsx, wl_fixed_t fsy)
426 pointer_leave(void *data, struct wl_pointer *wl_pointer, uint32_t serial,
427 struct wl_surface *surf)
432 pointer_motion(void *data, struct wl_pointer *wl_pointer, uint32_t time,
433 wl_fixed_t fsx, wl_fixed_t fsy)
438 pointer_button(void *data, struct wl_pointer *wl_pointer, uint32_t serial,
439 uint32_t time, uint32_t button, uint32_t state)
441 struct interactive_seat *seat = data;
443 xdg_toplevel_move(seat->inter->xdg_top, seat->wl_seat, serial);
447 pointer_axis(void *data, struct wl_pointer *wl_pointer, uint32_t time,
448 uint32_t axis, wl_fixed_t value)
453 pointer_frame(void *data, struct wl_pointer *wl_pointer)
458 pointer_axis_source(void *data, struct wl_pointer *wl_pointer, uint32_t source)
463 pointer_axis_stop(void *data, struct wl_pointer *wl_pointer, uint32_t time,
469 pointer_axis_discrete(void *data, struct wl_pointer *wl_pointer, uint32_t time,
474 static const struct wl_pointer_listener pointer_listener = {
483 pointer_axis_discrete
487 seat_capabilities(void *data, struct wl_seat *wl_seat, uint32_t caps)
489 struct interactive_seat *seat = data;
491 if (!seat->wl_kbd && (caps & WL_SEAT_CAPABILITY_KEYBOARD)) {
492 seat->wl_kbd = wl_seat_get_keyboard(seat->wl_seat);
493 wl_keyboard_add_listener(seat->wl_kbd, &kbd_listener, seat);
495 else if (seat->wl_kbd && !(caps & WL_SEAT_CAPABILITY_KEYBOARD)) {
496 if (seat->version >= WL_SEAT_RELEASE_SINCE_VERSION)
497 wl_keyboard_release(seat->wl_kbd);
499 wl_keyboard_destroy(seat->wl_kbd);
501 xkb_state_unref(seat->state);
502 xkb_keymap_unref(seat->keymap);
509 if (!seat->wl_pointer && (caps & WL_SEAT_CAPABILITY_POINTER)) {
510 seat->wl_pointer = wl_seat_get_pointer(seat->wl_seat);
511 wl_pointer_add_listener(seat->wl_pointer, &pointer_listener,
514 else if (seat->wl_pointer && !(caps & WL_SEAT_CAPABILITY_POINTER)) {
515 if (seat->version >= WL_SEAT_RELEASE_SINCE_VERSION)
516 wl_pointer_release(seat->wl_pointer);
518 wl_pointer_destroy(seat->wl_pointer);
519 seat->wl_pointer = NULL;
524 seat_name(void *data, struct wl_seat *wl_seat, const char *name)
526 struct interactive_seat *seat = data;
528 free(seat->name_str);
529 seat->name_str = strdup(name);
532 static const struct wl_seat_listener seat_listener = {
538 seat_create(struct interactive_dpy *inter, struct wl_registry *registry,
539 uint32_t name, uint32_t version)
542 struct interactive_seat *seat = calloc(1, sizeof(*seat));
544 seat->global_name = name;
546 seat->wl_seat = wl_registry_bind(registry, name, &wl_seat_interface,
548 wl_seat_add_listener(seat->wl_seat, &seat_listener, seat);
549 ret = asprintf(&seat->name_str, "seat:%d",
550 wl_proxy_get_id((struct wl_proxy *) seat->wl_seat));
552 wl_list_insert(&inter->seats, &seat->link);
556 seat_destroy(struct interactive_seat *seat)
559 if (seat->version >= WL_SEAT_RELEASE_SINCE_VERSION)
560 wl_keyboard_release(seat->wl_kbd);
562 wl_keyboard_destroy(seat->wl_kbd);
564 xkb_state_unref(seat->state);
565 xkb_keymap_unref(seat->keymap);
568 if (seat->wl_pointer) {
569 if (seat->version >= WL_SEAT_RELEASE_SINCE_VERSION)
570 wl_pointer_release(seat->wl_pointer);
572 wl_pointer_destroy(seat->wl_pointer);
575 if (seat->version >= WL_SEAT_RELEASE_SINCE_VERSION)
576 wl_seat_release(seat->wl_seat);
578 wl_seat_destroy(seat->wl_seat);
580 free(seat->name_str);
581 wl_list_remove(&seat->link);
586 registry_global(void *data, struct wl_registry *registry, uint32_t name,
587 const char *interface, uint32_t version)
589 struct interactive_dpy *inter = data;
591 if (strcmp(interface, "wl_seat") == 0) {
592 seat_create(inter, registry, name, version);
594 else if (strcmp(interface, "xdg_wm_base") == 0) {
595 inter->shell = wl_registry_bind(registry, name,
596 &xdg_wm_base_interface,
598 xdg_wm_base_add_listener(inter->shell, &shell_listener, inter);
600 else if (strcmp(interface, "wl_compositor") == 0) {
601 inter->compositor = wl_registry_bind(registry, name,
602 &wl_compositor_interface,
605 else if (strcmp(interface, "wl_shm") == 0) {
606 inter->shm = wl_registry_bind(registry, name, &wl_shm_interface,
612 registry_delete(void *data, struct wl_registry *registry, uint32_t name)
614 struct interactive_dpy *inter = data;
615 struct interactive_seat *seat, *tmp;
617 wl_list_for_each_safe(seat, tmp, &inter->seats, link) {
618 if (seat->global_name != name)
625 static const struct wl_registry_listener registry_listener = {
631 dpy_disconnect(struct interactive_dpy *inter)
633 struct interactive_seat *seat, *tmp;
635 wl_list_for_each_safe(seat, tmp, &inter->seats, link)
639 xdg_surface_destroy(inter->xdg_surf);
641 xdg_toplevel_destroy(inter->xdg_top);
643 wl_surface_destroy(inter->wl_surf);
645 xdg_wm_base_destroy(inter->shell);
646 if (inter->compositor)
647 wl_compositor_destroy(inter->compositor);
649 wl_shm_destroy(inter->shm);
651 /* Do one last roundtrip to try to destroy our wl_buffer. */
652 wl_display_roundtrip(inter->dpy);
654 xkb_context_unref(inter->ctx);
655 wl_display_disconnect(inter->dpy);
659 main(int argc, char *argv[])
662 struct interactive_dpy inter;
663 struct wl_registry *registry;
666 ret = strcmp(argv[1], "--help");
667 fprintf(ret ? stderr : stdout, "Usage: %s [--help]\n", argv[0]);
669 fprintf(stderr, "unrecognized option: %s\n", argv[1]);
670 return ret ? EXIT_INVALID_USAGE : EXIT_SUCCESS;
673 setlocale(LC_ALL, "");
675 memset(&inter, 0, sizeof(inter));
676 wl_list_init(&inter.seats);
678 inter.dpy = wl_display_connect(NULL);
680 fprintf(stderr, "Couldn't connect to Wayland server\n");
685 inter.ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
688 fprintf(stderr, "Couldn't create xkb context\n");
692 registry = wl_display_get_registry(inter.dpy);
693 wl_registry_add_listener(registry, ®istry_listener, &inter);
695 /* The first roundtrip gets the list of advertised globals. */
696 wl_display_roundtrip(inter.dpy);
698 /* The second roundtrip dispatches the events sent after binding, e.g.
699 * after binding to wl_seat globals in the first roundtrip, we will get
700 * the wl_seat::capabilities event in this roundtrip. */
701 wl_display_roundtrip(inter.dpy);
703 if (!inter.shell || !inter.shm || !inter.compositor) {
704 fprintf(stderr, "Required Wayland interfaces %s%s%s unsupported\n",
705 (inter.shell) ? "" : "xdg_shell ",
706 (inter.shm) ? "" : "wl_shm",
707 (inter.compositor) ? "" : "wl_compositor");
712 surface_create(&inter);
714 tools_disable_stdin_echo();
716 ret = wl_display_dispatch(inter.dpy);
717 } while (ret >= 0 && !terminate);
718 tools_enable_stdin_echo();
720 wl_registry_destroy(registry);
722 dpy_disconnect(&inter);
724 exit(ret >= 0 ? EXIT_SUCCESS : EXIT_FAILURE);