#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <wayland-client.h>

struct wl_display* display = NULL;
struct wl_seat* seat = NULL;

static void
global_registry_handler(void *data, struct wl_registry *registry, uint32_t id,
	const char *interface, uint32_t version)
{
	if (strcmp(interface, "wl_seat") == 0)
		seat = wl_registry_bind(registry, id, &wl_seat_interface, 1);
}

static const struct wl_registry_listener registry_listener = {
	global_registry_handler,
	NULL
};

static void
keymap_handler(void *data, struct wl_keyboard *keyboard, 
	uint32_t format, int32_t fd, uint32_t size)
{
	char *keymap = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
	assert(keymap != MAP_FAILED);
	printf("Trashing keymap\n");
	keymap[0] = 'a';
}

static const struct wl_keyboard_listener keyboard_listener = {
	keymap_handler,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL
};


int main()
{
	display = wl_display_connect(NULL);
	assert(display);
	struct wl_registry* registry = wl_display_get_registry(display);
	wl_registry_add_listener(registry, &registry_listener, NULL);
	assert(registry);
	wl_display_roundtrip(display);

	assert(seat);
	struct wl_keyboard* keyboard = wl_seat_get_keyboard(seat);
	wl_keyboard_add_listener(keyboard, &keyboard_listener, NULL);
	wl_display_roundtrip(display);

	return 0;
}
