From 7a640bd0622caf8a3354934d0400e8c361cdeee3 Mon Sep 17 00:00:00 2001 From: Phil Jones Date: Tue, 26 Oct 2021 17:00:14 +0100 Subject: Add EGL setup (unused for now). --- meson.build | 7 +- src/client.c | 464 ----------------------------------------------------------- src/client.h | 34 +++++ src/egl.c | 122 ++++++++++++++++ src/egl.h | 17 +++ src/log.c | 40 ++++++ src/log.h | 9 ++ src/main.c | 398 ++++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 626 insertions(+), 465 deletions(-) delete mode 100644 src/client.c create mode 100644 src/client.h create mode 100644 src/egl.c create mode 100644 src/egl.h create mode 100644 src/log.c create mode 100644 src/log.h create mode 100644 src/main.c diff --git a/meson.build b/meson.build index 7474a4f..cdffbdd 100644 --- a/meson.build +++ b/meson.build @@ -15,6 +15,9 @@ project( ) debug = get_option('buildtype').startswith('debug') +if debug + add_project_arguments('-DDEBUG', language : 'c') +endif data_location = join_paths( get_option('prefix'), @@ -34,7 +37,9 @@ add_project_arguments( ) sources = files( - 'src/client.c', + 'src/main.c', + 'src/log.c', + 'src/egl.c', 'src/xdg-shell-protocol.c', ) diff --git a/src/client.c b/src/client.c deleted file mode 100644 index 0b2bdec..0000000 --- a/src/client.c +++ /dev/null @@ -1,464 +0,0 @@ -#define _POSIX_C_SOURCE 200112L -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "xdg-shell-client-protocol.h" - -/* Shared memory support code */ -static void -randname(char *buf) -{ - struct timespec ts; - clock_gettime(CLOCK_REALTIME, &ts); - long r = ts.tv_nsec; - for (int i = 0; i < 6; ++i) { - buf[i] = 'A'+(r&15)+(r&16)*2; - r >>= 5; - } -} - -static int -create_shm_file(void) -{ - int retries = 100; - do { - char name[] = "/wl_shm-XXXXXX"; - randname(name + sizeof(name) - 7); - --retries; - int fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, 0600); - if (fd >= 0) { - shm_unlink(name); - return fd; - } - } while (retries > 0 && errno == EEXIST); - return -1; -} - -static int -allocate_shm_file(size_t size) -{ - int fd = create_shm_file(); - if (fd < 0) - return -1; - int ret; - do { - ret = ftruncate(fd, size); - } while (ret < 0 && errno == EINTR); - if (ret < 0) { - close(fd); - return -1; - } - return fd; -} - -/* Wayland code */ -struct client_state { - /* Globals */ - struct wl_display *wl_display; - struct wl_registry *wl_registry; - struct wl_shm *wl_shm; - struct wl_compositor *wl_compositor; - struct wl_seat *wl_seat; - struct xdg_wm_base *xdg_wm_base; - /* Objects */ - struct wl_surface *wl_surface; - struct wl_keyboard *wl_keyboard; - struct xdg_surface *xdg_surface; - struct xdg_toplevel *xdg_toplevel; - /* State */ - float offset; - uint32_t last_frame; - int width; - int height; - bool closed; - /* Keyboard state */ - struct xkb_state *xkb_state; - struct xkb_context *xkb_context; - struct xkb_keymap *xkb_keymap; -}; - -static void -wl_buffer_release(void *data, struct wl_buffer *wl_buffer) -{ - /* Sent by the compositor when it's no longer using this buffer */ - wl_buffer_destroy(wl_buffer); -} - -static const struct wl_buffer_listener wl_buffer_listener = { - .release = wl_buffer_release, -}; - -static struct wl_buffer * -draw_frame(struct client_state *state) -{ - const int width = state->width, height = state->height; - int stride = width * 4; - int size = stride * height; - - int fd = allocate_shm_file(size); - if (fd == -1) { - return NULL; - } - - uint32_t *data = mmap(NULL, size, - PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); - if (data == MAP_FAILED) { - close(fd); - return NULL; - } - - struct wl_shm_pool *pool = wl_shm_create_pool(state->wl_shm, fd, size); - struct wl_buffer *buffer = wl_shm_pool_create_buffer(pool, 0, - width, height, stride, WL_SHM_FORMAT_XRGB8888); - wl_shm_pool_destroy(pool); - close(fd); - - /* Draw checkerboxed background */ - int offset = (int)state->offset % 8; - for (int y = 0; y < height; ++y) { - for (int x = 0; x < width; ++x) { - if (((x + offset) + (y + offset) / 8 * 8) % 16 < 8) - data[y * width + x] = 0xFF666666; - else - data[y * width + x] = 0xFFEEEEEE; - } - } - - munmap(data, size); - wl_buffer_add_listener(buffer, &wl_buffer_listener, NULL); - return buffer; -} - -static void -xdg_toplevel_configure(void *data, - struct xdg_toplevel *xdg_toplevel, int32_t width, int32_t height, - struct wl_array *states) -{ - struct client_state *state = data; - if (width == 0 || height == 0) { - /* Compositor is deferring to us */ - return; - } - state->width = width; - state->height = height; -} - -static void -xdg_toplevel_close(void *data, struct xdg_toplevel *toplevel) -{ - struct client_state *state = data; - state->closed = true; -} - -static const struct xdg_toplevel_listener xdg_toplevel_listener = { - .configure = xdg_toplevel_configure, - .close = xdg_toplevel_close -}; - -static void -xdg_surface_configure(void *data, - struct xdg_surface *xdg_surface, uint32_t serial) -{ - struct client_state *state = data; - xdg_surface_ack_configure(xdg_surface, serial); - - struct wl_buffer *buffer = draw_frame(state); - wl_surface_attach(state->wl_surface, buffer, 0, 0); - wl_surface_commit(state->wl_surface); -} - -static const struct xdg_surface_listener xdg_surface_listener = { - .configure = xdg_surface_configure, -}; - -static void -wl_keyboard_keymap(void *data, struct wl_keyboard *wl_keyboard, - uint32_t format, int32_t fd, uint32_t size) -{ - struct client_state *client_state = data; - assert(format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1); - - char *map_shm = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); - assert(map_shm != MAP_FAILED); - - struct xkb_keymap *xkb_keymap = xkb_keymap_new_from_string( - client_state->xkb_context, map_shm, - XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS); - munmap(map_shm, size); - close(fd); - - struct xkb_state *xkb_state = xkb_state_new(xkb_keymap); - xkb_keymap_unref(client_state->xkb_keymap); - xkb_state_unref(client_state->xkb_state); - client_state->xkb_keymap = xkb_keymap; - client_state->xkb_state = xkb_state; -} - -static void -wl_keyboard_enter(void *data, struct wl_keyboard *wl_keyboard, - uint32_t serial, struct wl_surface *surface, - struct wl_array *keys) -{ - struct client_state *client_state = data; - fprintf(stderr, "keyboard enter; keys pressed are:\n"); - uint32_t *key; - wl_array_for_each(key, keys) { - char buf[128]; - xkb_keysym_t sym = xkb_state_key_get_one_sym( - client_state->xkb_state, *key + 8); - xkb_keysym_get_name(sym, buf, sizeof(buf)); - fprintf(stderr, "sym: %-12s (%d), ", buf, sym); - xkb_state_key_get_utf8(client_state->xkb_state, - *key + 8, buf, sizeof(buf)); - fprintf(stderr, "utf8: '%s'\n", buf); - } -} - -static void -wl_keyboard_key(void *data, struct wl_keyboard *wl_keyboard, - uint32_t serial, uint32_t time, uint32_t key, uint32_t state) -{ - struct client_state *client_state = data; - char buf[128]; - uint32_t keycode = key + 8; - xkb_keysym_t sym = xkb_state_key_get_one_sym( - client_state->xkb_state, keycode); - xkb_keysym_get_name(sym, buf, sizeof(buf)); - const char *action = - state == WL_KEYBOARD_KEY_STATE_PRESSED ? "press" : "release"; - fprintf(stderr, "key %s: sym: %-12s (%d), ", action, buf, sym); - xkb_state_key_get_utf8(client_state->xkb_state, keycode, - buf, sizeof(buf)); - fprintf(stderr, "utf8: '%s'\n", buf); -} - -static void -wl_keyboard_leave(void *data, struct wl_keyboard *wl_keyboard, - uint32_t serial, struct wl_surface *surface) -{ - fprintf(stderr, "keyboard leave\n"); -} - -static void -wl_keyboard_modifiers(void *data, struct wl_keyboard *wl_keyboard, - uint32_t serial, uint32_t mods_depressed, - uint32_t mods_latched, uint32_t mods_locked, - uint32_t group) -{ - struct client_state *client_state = data; - xkb_state_update_mask(client_state->xkb_state, - mods_depressed, mods_latched, mods_locked, 0, 0, group); -} - -static void -wl_keyboard_repeat_info(void *data, struct wl_keyboard *wl_keyboard, - int32_t rate, int32_t delay) -{ - /* Left as an exercise for the reader */ -} - -static const struct wl_keyboard_listener wl_keyboard_listener = { - .keymap = wl_keyboard_keymap, - .enter = wl_keyboard_enter, - .leave = wl_keyboard_leave, - .key = wl_keyboard_key, - .modifiers = wl_keyboard_modifiers, - .repeat_info = wl_keyboard_repeat_info, -}; - -static void -wl_seat_capabilities(void *data, struct wl_seat *wl_seat, uint32_t capabilities) -{ - struct client_state *state = data; - - bool have_keyboard = capabilities & WL_SEAT_CAPABILITY_KEYBOARD; - - if (have_keyboard && state->wl_keyboard == NULL) { - state->wl_keyboard = wl_seat_get_keyboard(state->wl_seat); - wl_keyboard_add_listener(state->wl_keyboard, - &wl_keyboard_listener, state); - } else if (!have_keyboard && state->wl_keyboard != NULL) { - wl_keyboard_release(state->wl_keyboard); - state->wl_keyboard = NULL; - } -} - -static void -wl_seat_name(void *data, struct wl_seat *wl_seat, const char *name) -{ - fprintf(stderr, "seat name: %s\n", name); -} - -static const struct wl_seat_listener wl_seat_listener = { - .capabilities = wl_seat_capabilities, - .name = wl_seat_name, -}; - -static void -xdg_wm_base_ping(void *data, struct xdg_wm_base *xdg_wm_base, uint32_t serial) -{ - xdg_wm_base_pong(xdg_wm_base, serial); -} - -static const struct xdg_wm_base_listener xdg_wm_base_listener = { - .ping = xdg_wm_base_ping, -}; - -static const struct wl_callback_listener wl_surface_frame_listener; - -static void -wl_surface_frame_done(void *data, struct wl_callback *cb, uint32_t time) -{ - /* Destroy this callback */ - wl_callback_destroy(cb); - - /* Request another frame */ - struct client_state *state = data; - cb = wl_surface_frame(state->wl_surface); - wl_callback_add_listener(cb, &wl_surface_frame_listener, state); - - /* Update scroll amount at 24 pixels per second */ - if (state->last_frame != 0) { - int elapsed = time - state->last_frame; - state->offset += elapsed / 1000.0 * 24; - } - - /* Submit a frame for this event */ - struct wl_buffer *buffer = draw_frame(state); - wl_surface_attach(state->wl_surface, buffer, 0, 0); - wl_surface_damage_buffer(state->wl_surface, 0, 0, INT32_MAX, INT32_MAX); - wl_surface_commit(state->wl_surface); - - state->last_frame = time; -} - -static const struct wl_callback_listener wl_surface_frame_listener = { - .done = wl_surface_frame_done, -}; - -static void -registry_global(void *data, struct wl_registry *wl_registry, - uint32_t name, const char *interface, uint32_t version) -{ - struct client_state *state = data; - if (strcmp(interface, wl_shm_interface.name) == 0) { - state->wl_shm = wl_registry_bind( - wl_registry, name, &wl_shm_interface, 1); - } else if (strcmp(interface, wl_compositor_interface.name) == 0) { - state->wl_compositor = wl_registry_bind( - wl_registry, name, &wl_compositor_interface, 4); - } else if (strcmp(interface, wl_seat_interface.name) == 0) { - state->wl_seat = wl_registry_bind( - wl_registry, name, &wl_seat_interface, 7); - wl_seat_add_listener(state->wl_seat, - &wl_seat_listener, state); - } else if (strcmp(interface, xdg_wm_base_interface.name) == 0) { - state->xdg_wm_base = wl_registry_bind( - wl_registry, name, &xdg_wm_base_interface, 1); - xdg_wm_base_add_listener(state->xdg_wm_base, - &xdg_wm_base_listener, state); - } -} - -static void -registry_global_remove(void *data, - struct wl_registry *wl_registry, uint32_t name) -{ - /* This space deliberately left blank */ -} - -static const struct wl_registry_listener wl_registry_listener = { - .global = registry_global, - .global_remove = registry_global_remove, -}; - -int -main(int argc, char *argv[]) -{ - struct client_state state = { 0 }; - state.width = 640; - state.height = 480; - state.wl_display = wl_display_connect(NULL); - state.wl_registry = wl_display_get_registry(state.wl_display); - state.xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); - wl_registry_add_listener(state.wl_registry, &wl_registry_listener, &state); - wl_display_roundtrip(state.wl_display); - - state.wl_surface = wl_compositor_create_surface(state.wl_compositor); - state.xdg_surface = xdg_wm_base_get_xdg_surface( - state.xdg_wm_base, state.wl_surface); - xdg_surface_add_listener(state.xdg_surface, &xdg_surface_listener, &state); - state.xdg_toplevel = xdg_surface_get_toplevel(state.xdg_surface); - xdg_toplevel_add_listener(state.xdg_toplevel, - &xdg_toplevel_listener, &state); - xdg_toplevel_set_title(state.xdg_toplevel, "Example client"); - - wl_surface_commit(state.wl_surface); - - - /* Setup EGL */ - EGLDisplay egl_dpy = eglGetPlatformDisplayEXT(EGL_PLATFORM_WAYLAND_KHR, - state.wl_display, NULL); - assert(egl_dpy != NULL); - bool res = eglInitialize(egl_dpy, NULL, NULL); - assert(res); - - const char *egl_extension_st = eglQueryString (egl_dpy, EGL_EXTENSIONS); - assert (strstr (egl_extension_st, "EGL_KHR_create_context") != NULL); - - static const EGLint config_attribs[] = { - EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, - EGL_SURFACE_TYPE, EGL_WINDOW_BIT, - EGL_NONE - }; - EGLConfig cfg; - EGLint count; - - res = eglChooseConfig (egl_dpy, config_attribs, &cfg, 1, &count); - assert (res); - - res = eglBindAPI (EGL_OPENGL_ES_API); - assert (res); - - static const EGLint attribs[] = { - EGL_CONTEXT_MAJOR_VERSION, 2, - EGL_NONE - }; - EGLContext core_ctx = eglCreateContext (egl_dpy, - cfg, - EGL_NO_CONTEXT, - attribs); - assert (core_ctx != EGL_NO_CONTEXT); - - struct wl_egl_window *egl_window = wl_egl_window_create(state.wl_surface, state.width, state.height); - EGLSurface surface = eglCreatePlatformWindowSurfaceEXT(egl_dpy, cfg, egl_window, NULL); - assert (surface != EGL_NO_SURFACE); - - res = eglMakeCurrent (egl_dpy, surface, surface, core_ctx); - assert (res); - - - - /* */ - - struct wl_callback *cb = wl_surface_frame(state.wl_surface); - wl_callback_add_listener(cb, &wl_surface_frame_listener, &state); - - while (!state.closed) { - wl_display_dispatch(state.wl_display); - } - - return 0; -} diff --git a/src/client.h b/src/client.h new file mode 100644 index 0000000..04d78ac --- /dev/null +++ b/src/client.h @@ -0,0 +1,34 @@ +#ifndef CLIENT_H +#define CLIENT_H + +#include +#include +#include "egl.h" + +struct client_state { + /* Globals */ + struct wl_display *wl_display; + struct wl_registry *wl_registry; + struct wl_shm *wl_shm; + struct wl_compositor *wl_compositor; + struct wl_seat *wl_seat; + struct xdg_wm_base *xdg_wm_base; + /* Objects */ + struct wl_surface *wl_surface; + struct wl_keyboard *wl_keyboard; + struct xdg_surface *xdg_surface; + struct xdg_toplevel *xdg_toplevel; + /* State */ + float offset; + uint32_t last_frame; + int width; + int height; + bool closed; + struct egl egl; + /* Keyboard state */ + struct xkb_state *xkb_state; + struct xkb_context *xkb_context; + struct xkb_keymap *xkb_keymap; +}; + +#endif /* CLIENT_H */ diff --git a/src/egl.c b/src/egl.c new file mode 100644 index 0000000..bd45413 --- /dev/null +++ b/src/egl.c @@ -0,0 +1,122 @@ +#include +#include +#include +#include "client.h" +#include "log.h" + +static void egl_log_error(); +static const char *egl_error_string(); + +void egl_create_window(struct client_state *state) +{ + state->egl.window = wl_egl_window_create( + state->wl_surface, + state->width, + state->height); + + if (state->egl.window == EGL_NO_SURFACE) { + egl_log_error("Couldn't create EGL window"); + } +} + +void egl_create_context(struct client_state *state) +{ + struct egl *egl = &state->egl; + egl->display = eglGetDisplay(state->wl_display); + if (egl->display == EGL_NO_DISPLAY) { + egl_log_error("Couldn't get EGL display"); + return; + } + + if (!eglInitialize(egl->display, NULL, NULL)) { + egl_log_error("Couldn't initialise EGL"); + return; + } + + EGLBoolean result; + EGLConfig config; + EGLint num_configs; + static const EGLint config_attribs[] = { + EGL_SURFACE_TYPE, EGL_WINDOW_BIT, + EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, + EGL_RED_SIZE, 8, + EGL_GREEN_SIZE, 8, + EGL_BLUE_SIZE, 8, + EGL_NONE + }; + + result = eglGetConfigs(egl->display, NULL, 0, &num_configs); + if ((result != EGL_TRUE) || (num_configs == 0)) { + egl_log_error("No EGL configs available"); + return; + } + + result = eglChooseConfig(egl->display, config_attribs, &config, 1, &num_configs); + if ((result != EGL_TRUE) || (num_configs != 1)) { + egl_log_error("Failed to choose EGL config"); + return; + } + + egl->surface = eglCreateWindowSurface(egl->display, config, egl->window, NULL); + if (egl->surface == EGL_NO_SURFACE) { + egl_log_error("Couldn't create EGL window surface"); + return; + } + + static const EGLint context_attribs[] = { + EGL_CONTEXT_MAJOR_VERSION, 2, + EGL_NONE + }; + + egl->context = eglCreateContext(egl->display, config, EGL_NO_CONTEXT, context_attribs); + if (egl->context == EGL_NO_CONTEXT) { + egl_log_error("Couldn't create EGL context"); + return; + } + + result = eglMakeCurrent(egl->display, egl->surface, egl->surface, egl->context); + if (!result) { + egl_log_error("Couldn't make EGL context current"); + return; + } +} + +void egl_log_error(const char *msg) { + log_error("%s: %s\n", msg, egl_error_string()); +} + +const char *egl_error_string() { + switch(eglGetError()) { + case EGL_SUCCESS: + return "EGL_SUCCESS"; + case EGL_NOT_INITIALIZED: + return "EGL_NOT_INITIALIZED"; + case EGL_BAD_ACCESS: + return "EGL_BAD_ACCESS"; + case EGL_BAD_ALLOC: + return "EGL_BAD_ALLOC"; + case EGL_BAD_ATTRIBUTE: + return "EGL_BAD_ATTRIBUTE"; + case EGL_BAD_CONTEXT: + return "EGL_BAD_CONTEXT"; + case EGL_BAD_CONFIG: + return "EGL_BAD_CONFIG"; + case EGL_BAD_CURRENT_SURFACE: + return "EGL_BAD_CURRENT_SURFACE"; + case EGL_BAD_DISPLAY: + return "EGL_BAD_DISPLAY"; + case EGL_BAD_SURFACE: + return "EGL_BAD_SURFACE"; + case EGL_BAD_MATCH: + return "EGL_BAD_MATCH"; + case EGL_BAD_PARAMETER: + return "EGL_BAD_PARAMETER"; + case EGL_BAD_NATIVE_PIXMAP: + return "EGL_BAD_NATIVE_PIXMAP"; + case EGL_BAD_NATIVE_WINDOW: + return "EGL_BAD_NATIVE_WINDOW"; + case EGL_CONTEXT_LOST: + return "EGL_CONTEXT_LOST"; + } + return "Unknown EGL error"; +} diff --git a/src/egl.h b/src/egl.h new file mode 100644 index 0000000..f97bd22 --- /dev/null +++ b/src/egl.h @@ -0,0 +1,17 @@ +#ifndef EGL_H +#define EGL_H + +#include + +struct client_state; +struct egl { + EGLNativeWindowType window; + EGLDisplay display; + EGLContext context; + EGLSurface surface; +}; + +void egl_create_window(struct client_state *state); +void egl_create_context(struct client_state *state); + +#endif /* EGL_H */ diff --git a/src/log.c b/src/log.c new file mode 100644 index 0000000..eb4d165 --- /dev/null +++ b/src/log.c @@ -0,0 +1,40 @@ +#include + +void log_error(const char *const fmt, ...) +{ + va_list args; + va_start(args, fmt); + fprintf(stderr, "[ERROR]: "); + vfprintf(stderr, fmt, args); + va_end(args); +} + +void log_warning(const char *const fmt, ...) +{ + va_list args; + va_start(args, fmt); + fprintf(stderr, "[WARNING]: "); + vfprintf(stderr, fmt, args); + va_end(args); +} + +void log_debug(const char *const fmt, ...) +{ +#ifndef DEBUG + return; +#endif + va_list args; + va_start(args, fmt); + printf("[DEBUG]: "); + vprintf(fmt, args); + va_end(args); +} + +void log_info(const char *const fmt, ...) +{ + va_list args; + va_start(args, fmt); + printf("[INFO]: "); + vprintf(fmt, args); + va_end(args); +} diff --git a/src/log.h b/src/log.h new file mode 100644 index 0000000..e030bc9 --- /dev/null +++ b/src/log.h @@ -0,0 +1,9 @@ +#ifndef LOG_H +#define LOG_H + +void log_error(const char *const fmt, ...); +void log_warning(const char *const fmt, ...); +void log_debug(const char *const fmt, ...); +void log_info(const char *const fmt, ...); + +#endif /* LOG_H */ diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..5991424 --- /dev/null +++ b/src/main.c @@ -0,0 +1,398 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "client.h" +#include "egl.h" +#include "xdg-shell-client-protocol.h" + +/* Shared memory support code */ +static void +randname(char *buf) +{ + struct timespec ts; + clock_gettime(CLOCK_REALTIME, &ts); + long r = ts.tv_nsec; + for (int i = 0; i < 6; ++i) { + buf[i] = 'A'+(r&15)+(r&16)*2; + r >>= 5; + } +} + +static int +create_shm_file(void) +{ + int retries = 100; + do { + char name[] = "/wl_shm-XXXXXX"; + randname(name + sizeof(name) - 7); + --retries; + int fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, 0600); + if (fd >= 0) { + shm_unlink(name); + return fd; + } + } while (retries > 0 && errno == EEXIST); + return -1; +} + +static int +allocate_shm_file(size_t size) +{ + int fd = create_shm_file(); + if (fd < 0) + return -1; + int ret; + do { + ret = ftruncate(fd, size); + } while (ret < 0 && errno == EINTR); + if (ret < 0) { + close(fd); + return -1; + } + return fd; +} + +/* Wayland code */ + +static void +wl_buffer_release(void *data, struct wl_buffer *wl_buffer) +{ + /* Sent by the compositor when it's no longer using this buffer */ + wl_buffer_destroy(wl_buffer); +} + +static const struct wl_buffer_listener wl_buffer_listener = { + .release = wl_buffer_release, +}; + +static struct wl_buffer * +draw_frame(struct client_state *state) +{ + const int width = state->width, height = state->height; + int stride = width * 4; + int size = stride * height; + + int fd = allocate_shm_file(size); + if (fd == -1) { + return NULL; + } + + uint32_t *data = mmap(NULL, size, + PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (data == MAP_FAILED) { + close(fd); + return NULL; + } + + struct wl_shm_pool *pool = wl_shm_create_pool(state->wl_shm, fd, size); + struct wl_buffer *buffer = wl_shm_pool_create_buffer(pool, 0, + width, height, stride, WL_SHM_FORMAT_XRGB8888); + wl_shm_pool_destroy(pool); + close(fd); + + /* Draw checkerboxed background */ + int offset = (int)state->offset % 8; + for (int y = 0; y < height; ++y) { + for (int x = 0; x < width; ++x) { + if (((x + offset) + (y + offset) / 8 * 8) % 16 < 8) + data[y * width + x] = 0xFF666666; + else + data[y * width + x] = 0xFFEEEEEE; + } + } + + munmap(data, size); + wl_buffer_add_listener(buffer, &wl_buffer_listener, NULL); + return buffer; +} + +static void +xdg_toplevel_configure(void *data, + struct xdg_toplevel *xdg_toplevel, int32_t width, int32_t height, + struct wl_array *states) +{ + struct client_state *state = data; + if (width == 0 || height == 0) { + /* Compositor is deferring to us */ + return; + } + state->width = width; + state->height = height; +} + +static void +xdg_toplevel_close(void *data, struct xdg_toplevel *toplevel) +{ + struct client_state *state = data; + state->closed = true; +} + +static const struct xdg_toplevel_listener xdg_toplevel_listener = { + .configure = xdg_toplevel_configure, + .close = xdg_toplevel_close +}; + +static void +xdg_surface_configure(void *data, + struct xdg_surface *xdg_surface, uint32_t serial) +{ + struct client_state *state = data; + xdg_surface_ack_configure(xdg_surface, serial); + + struct wl_buffer *buffer = draw_frame(state); + wl_surface_attach(state->wl_surface, buffer, 0, 0); + wl_surface_commit(state->wl_surface); +} + +static const struct xdg_surface_listener xdg_surface_listener = { + .configure = xdg_surface_configure, +}; + +static void +wl_keyboard_keymap(void *data, struct wl_keyboard *wl_keyboard, + uint32_t format, int32_t fd, uint32_t size) +{ + struct client_state *client_state = data; + assert(format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1); + + char *map_shm = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); + assert(map_shm != MAP_FAILED); + + struct xkb_keymap *xkb_keymap = xkb_keymap_new_from_string( + client_state->xkb_context, map_shm, + XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS); + munmap(map_shm, size); + close(fd); + + struct xkb_state *xkb_state = xkb_state_new(xkb_keymap); + xkb_keymap_unref(client_state->xkb_keymap); + xkb_state_unref(client_state->xkb_state); + client_state->xkb_keymap = xkb_keymap; + client_state->xkb_state = xkb_state; +} + +static void +wl_keyboard_enter(void *data, struct wl_keyboard *wl_keyboard, + uint32_t serial, struct wl_surface *surface, + struct wl_array *keys) +{ + struct client_state *client_state = data; + fprintf(stderr, "keyboard enter; keys pressed are:\n"); + uint32_t *key; + wl_array_for_each(key, keys) { + char buf[128]; + xkb_keysym_t sym = xkb_state_key_get_one_sym( + client_state->xkb_state, *key + 8); + xkb_keysym_get_name(sym, buf, sizeof(buf)); + fprintf(stderr, "sym: %-12s (%d), ", buf, sym); + xkb_state_key_get_utf8(client_state->xkb_state, + *key + 8, buf, sizeof(buf)); + fprintf(stderr, "utf8: '%s'\n", buf); + } +} + +static void +wl_keyboard_key(void *data, struct wl_keyboard *wl_keyboard, + uint32_t serial, uint32_t time, uint32_t key, uint32_t state) +{ + struct client_state *client_state = data; + char buf[128]; + uint32_t keycode = key + 8; + xkb_keysym_t sym = xkb_state_key_get_one_sym( + client_state->xkb_state, keycode); + xkb_keysym_get_name(sym, buf, sizeof(buf)); + const char *action = + state == WL_KEYBOARD_KEY_STATE_PRESSED ? "press" : "release"; + fprintf(stderr, "key %s: sym: %-12s (%d), ", action, buf, sym); + xkb_state_key_get_utf8(client_state->xkb_state, keycode, + buf, sizeof(buf)); + fprintf(stderr, "utf8: '%s'\n", buf); +} + +static void +wl_keyboard_leave(void *data, struct wl_keyboard *wl_keyboard, + uint32_t serial, struct wl_surface *surface) +{ + fprintf(stderr, "keyboard leave\n"); +} + +static void +wl_keyboard_modifiers(void *data, struct wl_keyboard *wl_keyboard, + uint32_t serial, uint32_t mods_depressed, + uint32_t mods_latched, uint32_t mods_locked, + uint32_t group) +{ + struct client_state *client_state = data; + xkb_state_update_mask(client_state->xkb_state, + mods_depressed, mods_latched, mods_locked, 0, 0, group); +} + +static void +wl_keyboard_repeat_info(void *data, struct wl_keyboard *wl_keyboard, + int32_t rate, int32_t delay) +{ + /* Left as an exercise for the reader */ +} + +static const struct wl_keyboard_listener wl_keyboard_listener = { + .keymap = wl_keyboard_keymap, + .enter = wl_keyboard_enter, + .leave = wl_keyboard_leave, + .key = wl_keyboard_key, + .modifiers = wl_keyboard_modifiers, + .repeat_info = wl_keyboard_repeat_info, +}; + +static void +wl_seat_capabilities(void *data, struct wl_seat *wl_seat, uint32_t capabilities) +{ + struct client_state *state = data; + + bool have_keyboard = capabilities & WL_SEAT_CAPABILITY_KEYBOARD; + + if (have_keyboard && state->wl_keyboard == NULL) { + state->wl_keyboard = wl_seat_get_keyboard(state->wl_seat); + wl_keyboard_add_listener(state->wl_keyboard, + &wl_keyboard_listener, state); + } else if (!have_keyboard && state->wl_keyboard != NULL) { + wl_keyboard_release(state->wl_keyboard); + state->wl_keyboard = NULL; + } +} + +static void +wl_seat_name(void *data, struct wl_seat *wl_seat, const char *name) +{ + fprintf(stderr, "seat name: %s\n", name); +} + +static const struct wl_seat_listener wl_seat_listener = { + .capabilities = wl_seat_capabilities, + .name = wl_seat_name, +}; + +static void +xdg_wm_base_ping(void *data, struct xdg_wm_base *xdg_wm_base, uint32_t serial) +{ + xdg_wm_base_pong(xdg_wm_base, serial); +} + +static const struct xdg_wm_base_listener xdg_wm_base_listener = { + .ping = xdg_wm_base_ping, +}; + +static const struct wl_callback_listener wl_surface_frame_listener; + +static void +wl_surface_frame_done(void *data, struct wl_callback *cb, uint32_t time) +{ + /* Destroy this callback */ + wl_callback_destroy(cb); + + /* Request another frame */ + struct client_state *state = data; + cb = wl_surface_frame(state->wl_surface); + wl_callback_add_listener(cb, &wl_surface_frame_listener, state); + + /* Update scroll amount at 24 pixels per second */ + if (state->last_frame != 0) { + int elapsed = time - state->last_frame; + state->offset += elapsed / 1000.0 * 24; + } + + /* Submit a frame for this event */ + struct wl_buffer *buffer = draw_frame(state); + wl_surface_attach(state->wl_surface, buffer, 0, 0); + wl_surface_damage_buffer(state->wl_surface, 0, 0, INT32_MAX, INT32_MAX); + wl_surface_commit(state->wl_surface); + + state->last_frame = time; +} + +static const struct wl_callback_listener wl_surface_frame_listener = { + .done = wl_surface_frame_done, +}; + +static void +registry_global(void *data, struct wl_registry *wl_registry, + uint32_t name, const char *interface, uint32_t version) +{ + struct client_state *state = data; + if (strcmp(interface, wl_shm_interface.name) == 0) { + state->wl_shm = wl_registry_bind( + wl_registry, name, &wl_shm_interface, 1); + } else if (strcmp(interface, wl_compositor_interface.name) == 0) { + state->wl_compositor = wl_registry_bind( + wl_registry, name, &wl_compositor_interface, 4); + } else if (strcmp(interface, wl_seat_interface.name) == 0) { + state->wl_seat = wl_registry_bind( + wl_registry, name, &wl_seat_interface, 7); + wl_seat_add_listener(state->wl_seat, + &wl_seat_listener, state); + } else if (strcmp(interface, xdg_wm_base_interface.name) == 0) { + state->xdg_wm_base = wl_registry_bind( + wl_registry, name, &xdg_wm_base_interface, 1); + xdg_wm_base_add_listener(state->xdg_wm_base, + &xdg_wm_base_listener, state); + } +} + +static void +registry_global_remove(void *data, + struct wl_registry *wl_registry, uint32_t name) +{ + /* This space deliberately left blank */ +} + +static const struct wl_registry_listener wl_registry_listener = { + .global = registry_global, + .global_remove = registry_global_remove, +}; + +int +main(int argc, char *argv[]) +{ + struct client_state state = { 0 }; + state.width = 640; + state.height = 480; + state.wl_display = wl_display_connect(NULL); + state.wl_registry = wl_display_get_registry(state.wl_display); + state.xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + wl_registry_add_listener(state.wl_registry, &wl_registry_listener, &state); + wl_display_roundtrip(state.wl_display); + + state.wl_surface = wl_compositor_create_surface(state.wl_compositor); + state.xdg_surface = xdg_wm_base_get_xdg_surface( + state.xdg_wm_base, state.wl_surface); + xdg_surface_add_listener(state.xdg_surface, &xdg_surface_listener, &state); + state.xdg_toplevel = xdg_surface_get_toplevel(state.xdg_surface); + xdg_toplevel_add_listener(state.xdg_toplevel, + &xdg_toplevel_listener, &state); + xdg_toplevel_set_title(state.xdg_toplevel, "Example client"); + + wl_surface_commit(state.wl_surface); + + egl_create_window(&state); + egl_create_context(&state); + + struct wl_callback *cb = wl_surface_frame(state.wl_surface); + wl_callback_add_listener(cb, &wl_surface_frame_listener, &state); + + while (!state.closed) { + wl_display_dispatch(state.wl_display); + } + + return 0; +} -- cgit v1.2.3