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-v5-client-protocol.h"
38 #include <wayland-util.h>
40 struct interactive_dpy {
41 struct wl_display *dpy;
42 struct wl_compositor *compositor;
43 struct xdg_shell *shell;
47 struct xkb_context *ctx;
49 struct wl_surface *wl_surf;
50 struct xdg_surface *xdg_surf;
55 struct interactive_seat {
56 struct interactive_dpy *inter;
58 struct wl_seat *wl_seat;
59 struct wl_keyboard *wl_kbd;
60 struct wl_pointer *wl_pointer;
61 uint32_t version; /* ... of wl_seat */
62 uint32_t global_name; /* an ID of sorts */
63 char *name_str; /* a descriptor */
65 struct xkb_keymap *keymap;
66 struct xkb_state *state;
71 static bool terminate;
73 /* The following utility functions are taken from Weston's
74 * shared/os-compatibility.c. */
76 os_fd_set_cloexec(int fd)
83 flags = fcntl(fd, F_GETFD);
87 if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
94 set_cloexec_or_close(int fd)
96 if (os_fd_set_cloexec(fd) != 0) {
104 create_tmpfile_cloexec(char *tmpname)
109 fd = mkostemp(tmpname, O_CLOEXEC);
113 fd = mkstemp(tmpname);
115 fd = set_cloexec_or_close(fd);
124 * Create a new, unique, anonymous file of the given size, and
125 * return the file descriptor for it. The file descriptor is set
126 * CLOEXEC. The file is immediately suitable for mmap()'ing
127 * the given size at offset zero.
129 * The file should not have a permanent backing store like a disk,
130 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
132 * The file name is deleted from the file system.
134 * The file is suitable for buffer sharing between processes by
135 * transmitting the file descriptor over Unix sockets using the
136 * SCM_RIGHTS methods.
138 * If the C library implements posix_fallocate(), it is used to
139 * guarantee that disk space is available for the file at the
140 * given size. If disk space is insufficent, errno is set to ENOSPC.
141 * If posix_fallocate() is not supported, program may receive
142 * SIGBUS on accessing mmap()'ed file contents instead.
145 os_create_anonymous_file(off_t size)
147 static const char template[] = "/weston-shared-XXXXXX";
153 path = getenv("XDG_RUNTIME_DIR");
159 name = malloc(strlen(path) + sizeof(template));
164 strcat(name, template);
166 fd = create_tmpfile_cloexec(name);
173 #ifdef HAVE_POSIX_FALLOCATE
174 ret = posix_fallocate(fd, 0, size);
181 ret = ftruncate(fd, size);
192 buffer_release(void *data, struct wl_buffer *buffer)
194 wl_buffer_destroy(buffer);
197 static const struct wl_buffer_listener buffer_listener = {
202 buffer_create(struct interactive_dpy *inter, uint32_t width, uint32_t height)
204 struct wl_shm_pool *pool;
205 struct wl_buffer *buf;
206 struct wl_region *opaque;
212 switch (inter->shm_format) {
213 case WL_SHM_FORMAT_ARGB8888:
214 case WL_SHM_FORMAT_XRGB8888:
215 case WL_SHM_FORMAT_ABGR8888:
216 case WL_SHM_FORMAT_XBGR8888:
219 case WL_SHM_FORMAT_RGB565:
220 case WL_SHM_FORMAT_BGR565:
225 size = stride * height;
226 fd = os_create_anonymous_file(size);
228 fprintf(stderr, "Couldn't create surface buffer\n");
232 map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
233 if (map == MAP_FAILED) {
234 fprintf(stderr, "Couldn't mmap surface buffer\n");
237 memset(map, 0xff, size);
240 pool = wl_shm_create_pool(inter->shm, fd, size);
241 buf = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
243 wl_buffer_add_listener(buf, &buffer_listener, inter);
245 wl_surface_attach(inter->wl_surf, buf, 0, 0);
246 wl_surface_damage(inter->wl_surf, 0, 0, width, height);
248 opaque = wl_compositor_create_region(inter->compositor);
249 wl_region_add(opaque, 0, 0, width, height);
250 wl_surface_set_opaque_region(inter->wl_surf, opaque);
251 wl_region_destroy(opaque);
253 wl_shm_pool_destroy(pool);
258 surface_configure(void *data, struct xdg_surface *surface,
259 int32_t width, int32_t height, struct wl_array *states,
262 struct interactive_dpy *inter = data;
264 if (width == 0 || height == 0) {
265 xdg_surface_ack_configure(inter->xdg_surf, serial);
269 buffer_create(inter, width, height);
270 xdg_surface_ack_configure(inter->xdg_surf, serial);
271 wl_surface_commit(inter->wl_surf);
275 surface_close(void *data, struct xdg_surface *surface)
280 static const struct xdg_surface_listener surface_listener = {
285 static void surface_create(struct interactive_dpy *inter)
287 int width = 200, height = 200;
289 inter->wl_surf = wl_compositor_create_surface(inter->compositor);
290 inter->xdg_surf = xdg_shell_get_xdg_surface(inter->shell,
292 xdg_surface_add_listener(inter->xdg_surf, &surface_listener, inter);
293 xdg_surface_set_title(inter->xdg_surf, "xkbcommon event tester");
294 xdg_surface_set_app_id(inter->xdg_surf,
295 "org.xkbcommon.test.interactive-wayland");
297 buffer_create(inter, width, height);
298 wl_surface_commit(inter->wl_surf);
302 shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
304 xdg_shell_pong(shell, serial);
307 static const struct xdg_shell_listener shell_listener = {
312 kbd_keymap(void *data, struct wl_keyboard *wl_kbd, uint32_t format,
313 int fd, uint32_t size)
315 struct interactive_seat *seat = data;
318 buf = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
319 if (buf == MAP_FAILED) {
320 fprintf(stderr, "Failed to mmap keymap: %d\n", errno);
325 seat->keymap = xkb_keymap_new_from_buffer(seat->inter->ctx, buf, size - 1,
326 XKB_KEYMAP_FORMAT_TEXT_V1, 0);
330 fprintf(stderr, "Failed to compile keymap!\n");
334 seat->state = xkb_state_new(seat->keymap);
336 fprintf(stderr, "Failed to create XKB state!\n");
342 kbd_enter(void *data, struct wl_keyboard *wl_kbd, uint32_t serial,
343 struct wl_surface *surf, struct wl_array *keys)
348 kbd_leave(void *data, struct wl_keyboard *wl_kbd, uint32_t serial,
349 struct wl_surface *surf)
354 kbd_key(void *data, struct wl_keyboard *wl_kbd, uint32_t serial, uint32_t time,
355 uint32_t key, uint32_t state)
357 struct interactive_seat *seat = data;
359 if (state != WL_KEYBOARD_KEY_STATE_PRESSED)
362 printf("%s: ", seat->name_str);
363 test_print_keycode_state(seat->state, NULL, key + 8,
364 XKB_CONSUMED_MODE_XKB);
367 if (xkb_state_key_get_one_sym(seat->state, key + 8) == XKB_KEY_Escape)
372 kbd_modifiers(void *data, struct wl_keyboard *wl_kbd, uint32_t serial,
373 uint32_t mods_depressed, uint32_t mods_latched,
374 uint32_t mods_locked, uint32_t group)
376 struct interactive_seat *seat = data;
378 xkb_state_update_mask(seat->state, mods_depressed, mods_latched,
379 mods_locked, 0, 0, group);
383 kbd_repeat_info(void *data, struct wl_keyboard *wl_kbd, int32_t rate,
388 static const struct wl_keyboard_listener kbd_listener = {
398 pointer_enter(void *data, struct wl_pointer *wl_pointer, uint32_t serial,
399 struct wl_surface *surf, wl_fixed_t fsx, wl_fixed_t fsy)
404 pointer_leave(void *data, struct wl_pointer *wl_pointer, uint32_t serial,
405 struct wl_surface *surf)
410 pointer_motion(void *data, struct wl_pointer *wl_pointer, uint32_t time,
411 wl_fixed_t fsx, wl_fixed_t fsy)
416 pointer_button(void *data, struct wl_pointer *wl_pointer, uint32_t serial,
417 uint32_t time, uint32_t button, uint32_t state)
419 struct interactive_seat *seat = data;
421 xdg_surface_move(seat->inter->xdg_surf, seat->wl_seat, serial);
425 pointer_axis(void *data, struct wl_pointer *wl_pointer, uint32_t time,
426 uint32_t axis, wl_fixed_t value)
431 pointer_frame(void *data, struct wl_pointer *wl_pointer)
436 pointer_axis_source(void *data, struct wl_pointer *wl_pointer, uint32_t source)
441 pointer_axis_stop(void *data, struct wl_pointer *wl_pointer, uint32_t time,
447 pointer_axis_discrete(void *data, struct wl_pointer *wl_pointer, uint32_t time,
452 static const struct wl_pointer_listener pointer_listener = {
461 pointer_axis_discrete
465 seat_capabilities(void *data, struct wl_seat *wl_seat, uint32_t caps)
467 struct interactive_seat *seat = data;
469 if (!seat->wl_kbd && (caps & WL_SEAT_CAPABILITY_KEYBOARD)) {
470 seat->wl_kbd = wl_seat_get_keyboard(seat->wl_seat);
471 wl_keyboard_add_listener(seat->wl_kbd, &kbd_listener, seat);
473 else if (seat->wl_kbd && !(caps & WL_SEAT_CAPABILITY_KEYBOARD)) {
474 if (seat->version >= WL_SEAT_RELEASE_SINCE_VERSION)
475 wl_keyboard_release(seat->wl_kbd);
477 wl_keyboard_destroy(seat->wl_kbd);
479 xkb_state_unref(seat->state);
480 xkb_keymap_unref(seat->keymap);
487 if (!seat->wl_pointer && (caps & WL_SEAT_CAPABILITY_POINTER)) {
488 seat->wl_pointer = wl_seat_get_pointer(seat->wl_seat);
489 wl_pointer_add_listener(seat->wl_pointer, &pointer_listener,
492 else if (seat->wl_pointer && !(caps & WL_SEAT_CAPABILITY_POINTER)) {
493 if (seat->version >= WL_SEAT_RELEASE_SINCE_VERSION)
494 wl_pointer_release(seat->wl_pointer);
496 wl_pointer_destroy(seat->wl_pointer);
497 seat->wl_pointer = NULL;
502 seat_name(void *data, struct wl_seat *wl_seat, const char *name)
504 struct interactive_seat *seat = data;
506 free(seat->name_str);
507 seat->name_str = strdup(name);
510 static const struct wl_seat_listener seat_listener = {
516 seat_create(struct interactive_dpy *inter, struct wl_registry *registry,
517 uint32_t name, uint32_t version)
520 struct interactive_seat *seat = calloc(1, sizeof(*seat));
522 seat->global_name = name;
524 seat->wl_seat = wl_registry_bind(registry, name, &wl_seat_interface,
526 wl_seat_add_listener(seat->wl_seat, &seat_listener, seat);
527 ret = asprintf(&seat->name_str, "seat:%d",
528 wl_proxy_get_id((struct wl_proxy *) seat->wl_seat));
530 wl_list_insert(&inter->seats, &seat->link);
534 seat_destroy(struct interactive_seat *seat)
537 if (seat->version >= WL_SEAT_RELEASE_SINCE_VERSION)
538 wl_keyboard_release(seat->wl_kbd);
540 wl_keyboard_destroy(seat->wl_kbd);
542 xkb_state_unref(seat->state);
543 xkb_keymap_unref(seat->keymap);
546 if (seat->wl_pointer) {
547 if (seat->version >= WL_SEAT_RELEASE_SINCE_VERSION)
548 wl_pointer_release(seat->wl_pointer);
550 wl_pointer_destroy(seat->wl_pointer);
553 if (seat->version >= WL_SEAT_RELEASE_SINCE_VERSION)
554 wl_seat_release(seat->wl_seat);
556 wl_seat_destroy(seat->wl_seat);
558 free(seat->name_str);
559 wl_list_remove(&seat->link);
564 registry_global(void *data, struct wl_registry *registry, uint32_t name,
565 const char *interface, uint32_t version)
567 struct interactive_dpy *inter = data;
569 if (strcmp(interface, "wl_seat") == 0) {
570 seat_create(inter, registry, name, version);
572 else if (strcmp(interface, "xdg_shell") == 0) {
573 inter->shell = wl_registry_bind(registry, name,
574 &xdg_shell_interface,
576 xdg_shell_add_listener(inter->shell, &shell_listener, inter);
577 xdg_shell_use_unstable_version(inter->shell, 5);
579 else if (strcmp(interface, "wl_compositor") == 0) {
580 inter->compositor = wl_registry_bind(registry, name,
581 &wl_compositor_interface,
584 else if (strcmp(interface, "wl_shm") == 0) {
585 inter->shm = wl_registry_bind(registry, name, &wl_shm_interface,
591 registry_delete(void *data, struct wl_registry *registry, uint32_t name)
593 struct interactive_dpy *inter = data;
594 struct interactive_seat *seat, *tmp;
596 wl_list_for_each_safe(seat, tmp, &inter->seats, link) {
597 if (seat->global_name != name)
604 static const struct wl_registry_listener registry_listener = {
610 dpy_disconnect(struct interactive_dpy *inter)
612 struct interactive_seat *seat, *tmp;
614 wl_list_for_each_safe(seat, tmp, &inter->seats, link)
618 xdg_surface_destroy(inter->xdg_surf);
620 wl_surface_destroy(inter->wl_surf);
622 xdg_shell_destroy(inter->shell);
623 if (inter->compositor)
624 wl_compositor_destroy(inter->compositor);
626 wl_shm_destroy(inter->shm);
628 /* Do one last roundtrip to try to destroy our wl_buffer. */
629 wl_display_roundtrip(inter->dpy);
631 xkb_context_unref(inter->ctx);
632 wl_display_disconnect(inter->dpy);
636 main(int argc, char *argv[])
639 struct interactive_dpy inter;
640 struct wl_registry *registry;
642 setlocale(LC_ALL, "");
644 memset(&inter, 0, sizeof(inter));
645 wl_list_init(&inter.seats);
647 inter.dpy = wl_display_connect(NULL);
649 fprintf(stderr, "Couldn't connect to Wayland server\n");
654 inter.ctx = test_get_context(0);
657 fprintf(stderr, "Couldn't create xkb context\n");
661 registry = wl_display_get_registry(inter.dpy);
662 wl_registry_add_listener(registry, ®istry_listener, &inter);
664 /* The first roundtrip gets the list of advertised globals. */
665 wl_display_roundtrip(inter.dpy);
667 /* The second roundtrip dispatches the events sent after binding, e.g.
668 * after binding to wl_seat globals in the first roundtrip, we will get
669 * the wl_seat::capabilities event in this roundtrip. */
670 wl_display_roundtrip(inter.dpy);
672 if (!inter.shell || !inter.shm || !inter.compositor) {
673 fprintf(stderr, "Required Wayland interfaces %s%s%s unsupported\n",
674 (inter.shell) ? "" : "xdg_shell ",
675 (inter.shm) ? "" : "wl_shm",
676 (inter.compositor) ? "" : "wl_compositor");
680 surface_create(&inter);
682 test_disable_stdin_echo();
684 ret = wl_display_dispatch(inter.dpy);
685 } while (ret >= 0 && !terminate);
686 test_enable_stdin_echo();
688 wl_registry_destroy(registry);
690 dpy_disconnect(&inter);
692 exit(ret >= 0 ? EXIT_SUCCESS : EXIT_FAILURE);