#include <stdio.h>
#include <stdlib.h>
+#include <stdint.h>
#include <string.h>
+#include <assert.h>
+#include <sys/mman.h>
+#include <errno.h>
+#include <unistd.h>
+#include <inttypes.h>
+#include <fcntl.h>
+#include <xkbcommon/xkbcommon.h>
#include <wayland-client.h>
#include <wayland-client-protocol.h>
struct wl_display *display = NULL;
struct wl_compositor *compositor = NULL;
-struct wl_surface *surface;
+struct wl_seat *seat = NULL;
+struct wl_surface *surface = NULL;
+struct wl_pointer *pointer = NULL;
+struct wl_keyboard *keyboard = NULL;
+struct wl_touch *touch = NULL;
+struct xkb_context *xkb_context = NULL;
+struct xkb_keymap *keymap = NULL;
+
+
+static void pointer_enter(void *data, struct wl_pointer *wl_pointer, uint32_t serial, struct wl_surface *surface, wl_fixed_t surface_x, wl_fixed_t surface_y)
+{
+ (void) data;
+ (void) wl_pointer;
+ (void) serial;
+ (void) surface;
+ (void) surface_x;
+ (void) surface_y;
+
+ printf("... serial=%d, x=%.f, y=%.f\n", serial, wl_fixed_to_double(surface_x), wl_fixed_to_double(surface_y));
+}
+
+static void pointer_leave(void *data, struct wl_pointer *wl_pointer, uint32_t serial, struct wl_surface *surface)
+{
+ (void) data;
+ (void) wl_pointer;
+ (void) serial;
+ (void) surface;
+
+ printf("... serial=%d\n", serial);
+}
+
+static void pointer_motion(void *data, struct wl_pointer *wl_pointer, uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y)
+{
+ (void) data;
+ (void) wl_pointer;
+ (void) time;
+ (void) surface_x;
+ (void) surface_y;
+
+ printf("... time=%d, x=%.f, y=%.f\n", time, wl_fixed_to_double(surface_x), wl_fixed_to_double(surface_y));
+}
+
+static void pointer_button(void *data, struct wl_pointer *wl_pointer, uint32_t serial, uint32_t time, uint32_t button, uint32_t state)
+{
+ (void) data;
+ (void) wl_pointer;
+ (void) serial;
+ (void) time;
+ (void) button;
+ (void) state;
+
+ printf("... serial=%d, time=%d, button=%d, state=%d\n", serial, time, button, state);
+}
+
+static void pointer_axis(void *data, struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis, wl_fixed_t value)
+{
+ (void) data;
+ (void) wl_pointer;
+ (void) time;
+ (void) axis;
+ (void) value;
+
+ printf("... time=%d, axis=%d, value=%.f\n", time, axis, wl_fixed_to_double(value));
+}
+
+static void
+keyboard_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format, int fd, uint32_t size)
+{
+ (void) data;
+ (void) keyboard;
+ (void) format;
+ (void) fd;
+ (void) size;
+
+ char *map = NULL;
+
+ printf("...WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1=%d, WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP=%d\n",
+ WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP);
+ printf("... format=%d, fd=%d, size=%d\n", format, fd, size);
+
+ if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1)
+ {
+ printf("... Invalid format: %d\n", format);
+ close(fd);
+ return;
+ }
+
+ map = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
+
+ if (map == MAP_FAILED)
+ {
+ printf("... Failed to mmap from fd(%d) size(%d)\n", fd, size);
+ close(fd);
+ return;
+ }
+
+ printf("... Succeed to mmap from fd(%d) size(%d)\n", fd, size);
+
+ keymap = xkb_map_new_from_string(xkb_context, map,
+ XKB_KEYMAP_FORMAT_TEXT_V1, 0);
+ if (!keymap)
+ printf("... Failed to get keymap from fd(%d)\n", fd);
+ else
+ printf("... Succeed to get keymap from fd(%d)\n", fd);
+
+ munmap(map, size);
+ close(fd);
+}
+
+static void
+keyboard_enter(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface, struct wl_array *keys)
+{
+ (void) data;
+ (void) keyboard;
+ (void) serial;
+ (void) surface;
+ (void) keys;
+
+ printf("... serial=%d\n", serial);
+}
+
+static void
+keyboard_leave(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface)
+{
+ (void) data;
+ (void) keyboard;
+ (void) serial;
+ (void) surface;
+
+ printf("... serial=%d\n", serial);
+}
+
+static void
+keyboard_key(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
+{
+ (void) data;
+ (void) keyboard;
+ (void) serial;
+ (void) time;
+ (void) key;
+ (void) state;
+
+ printf("... serial=%d, time=%d, key=%d, state=%d\n", serial, time, key, state);
+}
+
+static void
+keyboard_modifiers(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, uint32_t group)
+{
+ (void) data;
+ (void) keyboard;
+ (void) serial;
+ (void) mods_depressed;
+ (void) mods_latched;
+ (void) mods_locked;
+ (void) group;
+
+ printf("... serial=%d, mods_depressed=%d, mods_latched=%d, mods_locked=%d, group=%d\n", serial, mods_depressed, mods_latched, mods_locked, group);
+}
+
+static void
+touch_down(void *data, struct wl_touch *wl_touch, uint32_t serial, uint32_t time, struct wl_surface *surface, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
+{
+ (void) data;
+ (void) wl_touch;
+ (void) serial;
+ (void) time;
+ (void) surface;
+ (void) id;
+ (void) x_w;
+ (void) y_w;
+
+ printf("... serial=%d, time=%d, id=%d, x_w=%.f, y_w=%.f\n", serial, time, id, wl_fixed_to_double(x_w), wl_fixed_to_double(y_w));
+}
+
+static void
+touch_up(void *data, struct wl_touch *wl_touch, uint32_t serial, uint32_t time, int32_t id)
+{
+ (void) data;
+ (void) wl_touch;
+ (void) serial;
+ (void) time;
+ (void) id;
+
+ printf("... serial=%d, time=%d, id=%d\n", serial, time, id);
+}
+
+static void
+touch_motion(void *data, struct wl_touch *wl_touch, uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
+{
+ (void) data;
+ (void) wl_touch;
+ (void) time;
+ (void) id;
+ (void) x_w;
+ (void) y_w;
+
+ printf("... time=%d, id=%d, x_w=%.f, y_w=%.f\n", time, id, wl_fixed_to_double(x_w), wl_fixed_to_double(y_w));
+}
+
+static void
+touch_frame(void *data, struct wl_touch *wl_touch)
+{
+ (void) data;
+ (void) wl_touch;
+
+ printf("...\n");
+}
+
+static void
+touch_cancel(void *data, struct wl_touch *wl_touch)
+{
+ (void) data;
+ (void) wl_touch;
+
+ printf("...\n");
+}
+
+static const struct wl_pointer_listener pointer_listener = {
+ pointer_enter,
+ pointer_leave,
+ pointer_motion,
+ pointer_button,
+ pointer_axis,
+};
+
+
+static const struct wl_touch_listener touch_listener = {
+ touch_down,
+ touch_up,
+ touch_motion,
+ touch_frame,
+ touch_cancel
+};
+
+static const struct wl_keyboard_listener keyboard_listener = {
+ keyboard_keymap,
+ keyboard_enter,
+ keyboard_leave,
+ keyboard_key,
+ keyboard_modifiers
+};
+
+static void
+seat_handle_capabilities(void *data, struct wl_seat *seat,
+ enum wl_seat_capability caps)
+{
+ printf("Capabilities events ! (caps=%d)\n", caps);
+
+ if (caps & WL_SEAT_CAPABILITY_POINTER)
+ {
+ if (!pointer)
+ {
+ printf("Display has a pointer.\n");
+ pointer = wl_seat_get_pointer(seat);
+ wl_pointer_add_listener(pointer, &pointer_listener, NULL);
+ }
+ }
+ else
+ {
+ if (pointer)
+ {
+ printf("Pointer has been removed.\n");
+ wl_pointer_release(pointer);
+ pointer = NULL;
+ }
+ }
+
+ if (caps & WL_SEAT_CAPABILITY_KEYBOARD)
+ {
+ if (!keyboard)
+ {
+ printf("Display has a keyboard.\n");
+ keyboard = wl_seat_get_keyboard(seat);
+ wl_keyboard_add_listener(keyboard, &keyboard_listener, NULL);
+ }
+ }
+ else
+ {
+ if (keyboard)
+ {
+ printf("Keyboard has been removed.\n");
+ wl_keyboard_release(keyboard);
+ keyboard = NULL;
+ }
+ }
+
+ if (caps & WL_SEAT_CAPABILITY_TOUCH)
+ {
+ if (!touch)
+ {
+ printf("Display has a touch screen.\n");
+ touch = wl_seat_get_touch(seat);
+ wl_touch_add_listener(touch, &touch_listener, NULL);
+ }
+ }
+ else
+ {
+ if (touch)
+ {
+ printf("Touch has been removed.\n");
+ wl_touch_release(touch);
+ touch = NULL;
+ }
+ }
+}
+
+static void
+seat_handle_name(void *data, struct wl_seat *seat, const char *name)
+{
+ printf("Seat has a name (%s)\n", name);
+}
+
+static const struct wl_seat_listener seat_listener = {
+ seat_handle_capabilities,
+ seat_handle_name,
+};
static void
global_registry_handler(void *data, struct wl_registry *registry, uint32_t id,
const char *interface, uint32_t version)
{
- printf("Got a registry event for %s id %d\n", interface, id);
+ printf("Registry event : global added (interface:%s, id:%d)\n", interface, id);
if (strcmp(interface, "wl_compositor") == 0)
{
compositor = wl_registry_bind(registry, id, &wl_compositor_interface, 1);
}
+ else if(strcmp(interface, "wl_seat") == 0)
+ {
+ seat = wl_registry_bind(registry, id, &wl_seat_interface, 4);
+ wl_seat_add_listener(seat, &seat_listener, NULL);
+ }
}
static void
global_registry_remover(void *data, struct wl_registry *registry, uint32_t id)
{
- printf("Got a registry losing event for %d\n", id);
+ printf("Registry event : global removed (id:%d)\n", id);
}
static const struct wl_registry_listener registry_listener = {
struct wl_registry *registry = wl_display_get_registry(display);
wl_registry_add_listener(registry, ®istry_listener, NULL);
-
wl_display_dispatch(display);
wl_display_roundtrip(display);
}
else
{
- fprintf(stderr, "Created surface\n");
+ fprintf(stderr, "Created surface and will destroy it.\n");
+ wl_surface_destroy(surface);
+ surface = NULL;
+ }
+
+ while (wl_display_dispatch(display) != -1)
+ {
+ ;
}
wl_display_disconnect(display);