// @don't really need this I think as it limits some newer things // mainly "c-stacktrace.h" #define _POSIX_C_SOURCE 200112L #include #include #include #include #include #include #include #include #include #include // @todo: load cursor theme #include // @todo: button and input keycode handling #include // @todo: keyboard handling #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; } enum pointer_event_mask { POINTER_EVENT_ENTER = 1 << 0, POINTER_EVENT_LEAVE = 1 << 1, POINTER_EVENT_MOTION = 1 << 2, POINTER_EVENT_BUTTON = 1 << 3, POINTER_EVENT_AXIS = 1 << 4, POINTER_EVENT_AXIS_SOURCE = 1 << 5, POINTER_EVENT_AXIS_STOP = 1 << 6, POINTER_EVENT_AXIS_DISCRETE = 1 << 7, POINTER_EVENT_AXIS_VALUE120 = 1 << 8, POINTER_EVENT_AXIS_RELATIVE_DIRECTION = 1 << 9, }; struct pointer_state { uint32_t event_mask; wl_fixed_t surface_x, surface_y; uint32_t button, state; uint32_t time; uint32_t serial; struct { bool valid; wl_fixed_t value; int32_t discrete; int32_t discrete120; } axes[2]; uint32_t axis_source; }; struct keyboard_state { uint32_t serial; uint32_t time; }; 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 xdg_wm_base *xdg_wm_base; struct wl_seat *wl_seat; /* Objects */ struct wl_surface *wl_surface; struct xdg_surface *xdg_surface; struct xdg_toplevel *xdg_toplevel; struct wl_pointer *wl_pointer; struct wl_keyboard *wl_keyboard; // frame state uint8_t is_fullscreen; uint8_t is_running; uint32_t width; uint32_t height; uint32_t frame_color; uint32_t frame_time; int32_t hidpi_scale; struct { double x; double y; } basic_pointer; struct pointer_state pointer; struct xkb_state *xkb_state; struct xkb_keymap *xkb_keymap; struct xkb_context *xkb_context; struct keyboard_state keyboard; }; // @important: all events handled here, assume a singular surface // or at the very least, multiple surfaces/windows have absolutely // not been tested. // // So, incase that is needed, support must be added. // Leaving as @todo for the future. 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) { int stride = state->width * 4; int size = stride * state->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, state->width, state->height, stride, WL_SHM_FORMAT_XRGB8888); wl_shm_pool_destroy(pool); close(fd); /* Draw checkerboxed background */ for (int y = 0; y < state->height; ++y) { for (int x = 0; x < state->width; ++x) { data[y * state->width + x] = state->frame_color; } } munmap(data, size); wl_buffer_add_listener(buffer, &wl_buffer_listener, NULL); return buffer; } 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 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 xdg_toplevel_configure( void *data, struct xdg_toplevel *xdg_toplevel, int32_t width, int32_t height, struct wl_array *states ) { printf("EVENT::XDG_TOPLEVEL_CONFIGURE\n"); struct client_state *client_state = data; if (width && height) { client_state->width = width; client_state->height = height; } } static void xdg_toplevel_close( void *data, struct xdg_toplevel *xdg_toplevel ) { struct client_state *state = data; state->is_running = 0; } static void xdg_toplevel_configure_bounds( void *data, struct xdg_toplevel *xdg_toplevel, int32_t width, int32_t height ) { printf("EVENT::XDG_TOPLEVEL_CONFIGURE_BOUNDS\n"); } static void xdg_toplevel_wm_capabilities( void *data, struct xdg_toplevel *xdg_toplevel, struct wl_array *capabilities ) { printf("EVENT::XDG_TOPLEVEL_WM_CAPABILITIES\n"); } static const struct xdg_toplevel_listener xdg_toplevel_listener = { .configure = xdg_toplevel_configure, .close = xdg_toplevel_close, .configure_bounds = xdg_toplevel_configure_bounds, .wm_capabilities = xdg_toplevel_wm_capabilities }; static void wl_surface_frame_done(void *data, struct wl_callback *cb, uint32_t time) { wl_callback_destroy(cb); struct client_state *state = data; cb = wl_surface_frame(state->wl_surface); wl_callback_add_listener(cb, &wl_surface_frame_listener, state); 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->frame_time = time; } static const struct wl_callback_listener wl_surface_frame_listener = { .done=wl_surface_frame_done, }; static void wl_surface_preferred_buffer_scale(void *data, struct wl_surface *wl_surface, int32_t factor) { struct client_state *state = data; state->hidpi_scale = factor; printf("EVENT::wl_surface_preferred_buffer_scale.\n\tINFO: display scaling factor is: %d\n", factor); } static void wl_surface_enter(void *data, struct wl_surface *wl_surface, struct wl_output *output) { printf("EVENT::wl_surface_enter\n"); } static void wl_surface_leave(void *data, struct wl_surface *wl_surface, struct wl_output *output) { printf("EVENT::wl_surface_leave\n"); } static const struct wl_surface_listener wl_surface_listener = { // @note: nothing happening with this right now, could not figure it out // can't test this .enter = wl_surface_enter, .leave = wl_surface_leave, .preferred_buffer_scale = wl_surface_preferred_buffer_scale, }; static void wl_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 ) { struct client_state *state = data; state->pointer.serial = serial; state->pointer.event_mask |= POINTER_EVENT_ENTER; state->pointer.surface_x = surface_x; state->pointer.surface_y = surface_y; } static void wl_pointer_leave( void *data, struct wl_pointer *wl_pointer, uint32_t serial, struct wl_surface *surface ) { struct client_state *state = data; state->pointer.serial = serial; state->pointer.event_mask |= POINTER_EVENT_LEAVE; } static void wl_pointer_motion( void *data, struct wl_pointer *wl_pointer, uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y ) { struct client_state *state = data; state->pointer.event_mask |= POINTER_EVENT_MOTION; state->pointer.time = time; state->pointer.surface_x = surface_x; state->pointer.surface_y = surface_y; } static void wl_pointer_button( void *data, struct wl_pointer *wl_pointer, uint32_t serial, uint32_t time, uint32_t button, uint32_t state ) { struct client_state *gstate = data; gstate->pointer.event_mask |= POINTER_EVENT_MOTION; gstate->pointer.serial = serial; gstate->pointer.time = time; gstate->pointer.button = button; gstate->pointer.state = state; } static void wl_pointer_axis( void *data, struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis, wl_fixed_t value ) { struct client_state *state = data; state->pointer.event_mask |= POINTER_EVENT_AXIS; state->pointer.time = time; state->pointer.axes[axis].value = value; state->pointer.axes[axis].valid = true; } static void wl_pointer_frame( void *data, struct wl_pointer *wl_pointer ) { struct client_state *state = data; printf("EVENT::POINTER_EVENT_FRAME::START\n"); if (state->pointer.event_mask & POINTER_EVENT_ENTER) { printf("LOG::POINTER_EVENT_ENTER\n"); } if (state->pointer.event_mask & POINTER_EVENT_LEAVE) { printf("LOG::POINTER_EVENT_LEAVE\n"); } if (state->pointer.event_mask & POINTER_EVENT_MOTION) { // @note: playing around double pointerX = wl_fixed_to_double(state->pointer.surface_x); double pointerY = wl_fixed_to_double(state->pointer.surface_y); printf("EVENT::wl_pointer_motion\n\t X: %f, Y: %f\n", pointerX, pointerY); uint8_t offsetX = pointerX*255/640; uint8_t offsetY = (pointerY*255/480); state->frame_color = 0xFF0000 | offsetY << 8 | offsetX; printf("frame color: %#x\n", state->frame_color); state->basic_pointer.x = pointerX; state->basic_pointer.y = pointerY; } if (state->pointer.event_mask & POINTER_EVENT_BUTTON) { const char *btn_state = ( state->pointer.state == WL_POINTER_BUTTON_STATE_PRESSED ? "pressed": "released" ); printf("LOG::POINTER_EVENT_BUTTON> button %d %s\n", state->pointer.button, btn_state); } // axis events uint32_t axis_events = (POINTER_EVENT_AXIS | POINTER_EVENT_AXIS_SOURCE | POINTER_EVENT_AXIS_STOP | POINTER_EVENT_AXIS_DISCRETE | POINTER_EVENT_AXIS_VALUE120 | POINTER_EVENT_AXIS_RELATIVE_DIRECTION); char *axis_name[2]; { axis_name[WL_POINTER_AXIS_VERTICAL_SCROLL] = "vertical"; axis_name[WL_POINTER_AXIS_HORIZONTAL_SCROLL] = "horizontal"; } char *axis_source[4]; { axis_source[WL_POINTER_AXIS_SOURCE_WHEEL] = "wheel"; axis_source[WL_POINTER_AXIS_SOURCE_FINGER] = "finger"; axis_source[WL_POINTER_AXIS_SOURCE_CONTINUOUS] = "continuous"; axis_source[WL_POINTER_AXIS_SOURCE_WHEEL_TILT] = "wheel tilt"; } if (state->pointer.event_mask & axis_events) { for (int i = 0; i < 2; i++) { if (!state->pointer.axes[i].valid) continue; printf("LOG: Axis `%s` Events\n", axis_name[i]); if (state->pointer.event_mask & POINTER_EVENT_AXIS) { printf("LOG::AXIS: value %f\n", wl_fixed_to_double(state->pointer.axes[i].value)); } if (state->pointer.event_mask & POINTER_EVENT_AXIS_SOURCE) { printf( "LOG::AXIS_SOURCE: %s\n", axis_source[state->pointer.axis_source] ); } if (state->pointer.event_mask & POINTER_EVENT_AXIS_DISCRETE) { printf( "LOG::AXIS_DISCRETE: value %d\n", state->pointer.axes[i].discrete ); } if (state->pointer.event_mask & POINTER_EVENT_AXIS_VALUE120) { printf( "LOG::AXIS_VALUE120: value %d\n", state->pointer.axes[i].discrete120 ); } if (state->pointer.event_mask & POINTER_EVENT_AXIS_STOP) { printf("LOG::AXIS::END\n"); } } } printf("EVENT::POINTER_EVENT_FRAME::END\n"); memset(&state->pointer, 0, sizeof(state->pointer)); } static void wl_pointer_axis_source( void *data, struct wl_pointer *wl_pointer, uint32_t axis_source ) { struct client_state *state = data; state->pointer.event_mask |= POINTER_EVENT_AXIS_SOURCE; state->pointer.axis_source = axis_source; } static void wl_pointer_axis_stop( void *data, struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis ) { struct client_state *state = data; state->pointer.event_mask |= POINTER_EVENT_AXIS_STOP; state->pointer.time = time; state->pointer.axes[axis].valid = false; state->pointer.axes[axis].discrete120 = 0; } static void wl_pointer_axis_discrete( void *data, struct wl_pointer *wl_pointer, uint32_t axis, int32_t discrete ) { struct client_state *state = data; state->pointer.event_mask |= POINTER_EVENT_AXIS_DISCRETE; state->pointer.axes[axis].valid = true; state->pointer.axes[axis].discrete = discrete; } static void wl_pointer_axis_value120( void *data, struct wl_pointer *wl_pointer, uint32_t axis, int32_t value120 ) { struct client_state *state = data; state->pointer.event_mask = POINTER_EVENT_AXIS_VALUE120; state->pointer.axes[axis].valid = true; state->pointer.axes[axis].discrete120 += value120; } static void wl_pointer_axis_relative_direction(void *data, struct wl_pointer *wl_pointer, uint32_t axis, uint32_t direction) { printf("TODO::EVENT::wl_pointer_axis_relative_direction\n"); } static const struct wl_pointer_listener wl_pointer_listener = { .enter=wl_pointer_enter, .leave=wl_pointer_leave, .motion=wl_pointer_motion, .button=wl_pointer_button, .axis=wl_pointer_axis, .frame=wl_pointer_frame, .axis_source=wl_pointer_axis_source, .axis_stop=wl_pointer_axis_stop, .axis_discrete=wl_pointer_axis_discrete, .axis_value120=wl_pointer_axis_value120, .axis_relative_direction=wl_pointer_axis_relative_direction, }; static void wl_keyboard_keymap( void *data, struct wl_keyboard *wl_keyboard, uint32_t format, int32_t fd, uint32_t size ) { struct client_state *state = data; assert(format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1); // @todo: handle for below version 7?? // note that if I am handling that, I need to consider everything else and // also test with lower versions of the wl_keyboard global // for version 7+: will be private char *kb_map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); assert(kb_map != MAP_FAILED); struct xkb_keymap *xkb_keymap = xkb_keymap_new_from_string( state->xkb_context, kb_map, XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS ); munmap(kb_map, size); close(fd); struct xkb_state *xkb_state = xkb_state_new(xkb_keymap); xkb_keymap_unref(state->xkb_keymap); xkb_state_unref(state->xkb_state); state->xkb_keymap = xkb_keymap; 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 *state = data; state->keyboard.serial = serial; printf("LOG::KEYBOARD_ENTER: keys down\n"); uint32_t *key; wl_array_for_each(key, keys) { char buf[128]; xkb_keysym_t sym = xkb_state_key_get_one_sym( state->xkb_state, *key + 8 ); xkb_keysym_get_name(sym, buf, sizeof(buf)); printf("sym: %-12s (%d)\n", buf, sym); xkb_state_key_get_utf8( state->xkb_state, *key + 8, buf, sizeof(buf) ); printf("utf8: '%s'\n", buf); } } static void wl_keyboard_leave(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, struct wl_surface *surface) { printf("LOG::KEYBOARD_LEAVE\n"); } 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 *gstate = data; gstate->keyboard.serial = serial; gstate->keyboard.time = time; char buf[128]; uint32_t keycode = key + 8; xkb_keysym_t sym = xkb_state_key_get_one_sym( gstate->xkb_state, keycode ); xkb_keysym_get_name(sym, buf, sizeof(buf)); const char *action = state == WL_KEYBOARD_KEY_STATE_PRESSED ? "press" : "release"; printf("key: %s, keysym: %-12s (%d), ", action, buf, sym); xkb_state_key_get_utf8(gstate->xkb_state, keycode, buf, sizeof(buf)); printf("utf8: '%s'\n", buf); printf("time_at_key_enter: %d\n", time); if (state == WL_KEYBOARD_KEY_STATE_PRESSED) { if (sym == XKB_KEY_Escape) { gstate->is_running = false; } else if (sym == XKB_KEY_f) { // set fullscreen if (gstate->is_fullscreen) { gstate->is_fullscreen = 0; xdg_toplevel_unset_fullscreen(gstate->xdg_toplevel); } else { gstate->is_fullscreen = 1; xdg_toplevel_set_fullscreen(gstate->xdg_toplevel, NULL); } } } } 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 ) { printf("EVENT::KEYBOARD_MODIFIERS\n"); struct client_state *state = data; xkb_state_update_mask(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) { // @note: PLAY AROUND WITH THIS printf("EVENT::KEYBOARD_REPEAT_INFO\n"); printf(" rate: %d, delay: %d\n", rate, delay); } 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; uint32_t has_pointer = capabilities & WL_SEAT_CAPABILITY_POINTER; if (!state->wl_pointer && has_pointer) { // create capability state->wl_pointer = wl_seat_get_pointer(state->wl_seat); wl_pointer_add_listener(state->wl_pointer, &wl_pointer_listener, state); } else if (state->wl_pointer && !has_pointer) { // destroy capability wl_pointer_release(state->wl_pointer); state->wl_pointer = NULL; } uint32_t has_kb = capabilities & WL_SEAT_CAPABILITY_KEYBOARD; if (has_kb && !state->wl_keyboard) { state->wl_keyboard = wl_seat_get_keyboard(state->wl_seat); wl_keyboard_add_listener(state->wl_keyboard, &wl_keyboard_listener, state); } else if (!has_kb && state->wl_keyboard) { 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) { printf("EVENT::wl_seat_name\n\tINFO: name: %s\n", name); } static const struct wl_seat_listener wl_seat_listener = { .capabilities=wl_seat_capabilities, .name=wl_seat_name }; 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, 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 ); } else if (strcmp(interface, wl_seat_interface.name) == 0) { state->wl_seat = wl_registry_bind( wl_registry, name, &wl_seat_interface, version ); wl_seat_add_listener(state->wl_seat, &wl_seat_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[]) { // init state struct client_state state = { 0 }; state.hidpi_scale = 1; state.frame_time = 0; state.frame_color = 0xFF000000; state.width = 640; state.height = 480; state.is_running = 1; state.is_fullscreen = 0; 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); assert(state.xkb_context); 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); wl_surface_add_listener(state.wl_surface, &wl_surface_listener, &state); 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, "Wayland Base"); wl_surface_commit(state.wl_surface); struct wl_callback *cb = wl_surface_frame(state.wl_surface); wl_callback_add_listener(cb, &wl_surface_frame_listener, &state); while (state.is_running && wl_display_dispatch(state.wl_display)) { /* This space deliberately left blank */ } wl_seat_release(state.wl_seat); return 0; }