#include "common/shm.h" #include "common/state.h" #include "keyboard.h" #include "pointer.h" #include "protocols/xdg-shell-client-protocol.h" #include "protocols/xdg-decoration-client-protocol.h" #include #include #include #include #include #include static const struct wl_callback_listener wl_surface_frame_listener; static struct wl_buffer *draw_frame(struct client_state *state); static void registry_handle_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_compositor_interface.name) == 0) { state->wl_compositor = wl_registry_bind(wl_registry, name, &wl_compositor_interface, 4); } else 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, xdg_wm_base_interface.name) == 0) { state->xdg_wm_base = wl_registry_bind(wl_registry, name, &xdg_wm_base_interface, 3); } else if (strcmp(interface, wl_seat_interface.name) == 0) { state->wl_seat = wl_registry_bind(wl_registry, name, &wl_seat_interface, 7); } else if (strcmp(interface, zxdg_decoration_manager_v1_interface.name) == 0) { state->xdg_decoration_manager = wl_registry_bind(wl_registry, name, &zxdg_decoration_manager_v1_interface, 1); } } static void registry_handle_global_remove(void *data, struct wl_registry *registry, uint32_t name) { // This space deliberately left blank } static void shm_handle_format(void *data, struct wl_shm *wl_shm, uint32_t format) { struct client_state *state = data; } static void wm_base_handle_ping(void *data, struct xdg_wm_base *xdg_wm_base, uint32_t serial) { struct client_state *state = data; xdg_wm_base_pong(xdg_wm_base, serial); } static void surface_handle_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 void seat_handle_name(void *data, struct wl_seat *wl_seat, const char *name) { fprintf(stderr, "seat name: %s\n", name); } static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat, uint32_t capabilities) { struct client_state *state = data; bool have_pointer = capabilities & WL_SEAT_CAPABILITY_POINTER; if (have_pointer && state->wl_pointer == NULL) { state->wl_pointer = wl_seat_get_pointer(state->wl_seat); wl_pointer_add_listener(state->wl_pointer, &wl_pointer_listener, state); } else if (!have_pointer && state->wl_pointer != NULL) { wl_pointer_release(state->wl_pointer); state->wl_pointer = NULL; } 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_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 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 *xdg_toplevel) { struct client_state *state = data; state->closed = true; } static const struct wl_registry_listener wl_registry_listener = { .global = registry_handle_global, .global_remove = registry_handle_global_remove, }; static const struct wl_shm_listener wl_shm_listener = { .format = shm_handle_format, }; static const struct xdg_wm_base_listener xdg_wm_base_listener = { .ping = wm_base_handle_ping, }; static const struct xdg_surface_listener xdg_surface_listener = { .configure = surface_handle_configure, }; static const struct wl_seat_listener wl_seat_listener = { .name = seat_handle_name, .capabilities = seat_handle_capabilities, }; static const struct wl_callback_listener wl_surface_frame_listener = { .done = wl_surface_frame_done, }; static const struct xdg_toplevel_listener xdg_toplevel_listener = { .configure = xdg_toplevel_configure, .close = xdg_toplevel_close, }; 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 width = state->width; int 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); // Solid white // uint32_t *pixels = (uint32_t *)&data[offset]; // memset(pixels, 255, width * height * 4); // Checkerboard pattern int offset = (int)state->offset % 8; uint32_t *pixels = (uint32_t *)data; for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { if (((x + offset) + (y + offset) / 8 * 8) % 16 < 8) { pixels[y * width + x] = 0xFF666666; } else { pixels[y * width + x] = 0xFFEEEEEE; } } } munmap(data, size); wl_buffer_add_listener(buffer, &wl_buffer_listener, NULL); return buffer; } 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.xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); state.wl_registry = wl_display_get_registry(state.wl_display); wl_registry_add_listener(state.wl_registry, &wl_registry_listener, &state); wl_display_roundtrip(state.wl_display); wl_seat_add_listener(state.wl_seat, &wl_seat_listener, &state); state.wl_surface = wl_compositor_create_surface(state.wl_compositor); wl_shm_add_listener(state.wl_shm, &wl_shm_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, "Example client"); wl_surface_commit(state.wl_surface); xdg_wm_base_add_listener(state.xdg_wm_base, &xdg_wm_base_listener, &state); struct wl_callback *cb = wl_surface_frame(state.wl_surface); wl_callback_add_listener(cb, &wl_surface_frame_listener, &state); state.xdg_toplevel_decoration = zxdg_decoration_manager_v1_get_toplevel_decoration(state.xdg_decoration_manager, state.xdg_toplevel); zxdg_toplevel_decoration_v1_set_mode(state.xdg_toplevel_decoration, ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE); while (wl_display_dispatch(state.wl_display)) { /* This space deliberately left blank */ } return 0; }