#include #include #include #include #include #include #include "xdg-shell-client-protocol.h" static struct wl_compositor *compositor; static struct xdg_wm_base *xdg_wm_base; // Всё ниже - для одного окна static struct xdg_surface *xdg_surface; static struct xdg_toplevel *xdg_toplevel; static struct wl_surface *wl_surface; static struct wl_buffer *buffer; static struct wl_shm *shm; static int16_t width = 200; static int16_t height = 100; static int8_t *data; static int8_t need_close = 0; int32_t alloc_shm(size_t size) { char name[32]; sprintf(name, "/wayland-shm-%d", getpid()); int32_t fd = shm_open(name, O_RDWR | O_CREAT, 0600); if (fd == -1) return fd; shm_unlink(name); ftruncate(fd, size); return fd; } void resize_canvas() { size_t stride = width * 4; size_t size = stride * height; int32_t fd = alloc_shm(size); data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); struct wl_shm_pool *pool = wl_shm_create_pool(shm, fd, size); buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, WL_SHM_FORMAT_ARGB8888); wl_shm_pool_destroy(pool); close(fd); } void resize_new(int16_t w, int16_t h) { if (!w || !h) return; if (width != w || height != h) { if(data) munmap(data, width*height*4); width = w; height = h; resize_canvas(); } } int8_t c; void draw() { size_t stride = width * 4; size_t size = stride * height; memset(data, c++, size); wl_surface_attach(wl_surface, buffer, 0, 0); wl_surface_damage_buffer(wl_surface, 0, 0, width, height); wl_surface_commit(wl_surface); } void xdg_surface_conf(void *data, struct xdg_surface *xdg_surface, uint32_t serial) { xdg_surface_ack_configure(xdg_surface, serial); if (!data) resize_canvas(); draw(); } void xdg_toplevel_conf(void *data, struct xdg_toplevel *xdg_top, int32_t w, int32_t h, struct wl_array *states) { resize_new(w, h); } void xdg_toplevel_cls(void *data, struct xdg_toplevel *xdg_top) { need_close = 1; } void xdg_toplevel_bounds(void *data, struct xdg_toplevel *xdg_top, int32_t w, int32_t h) { } void xdg_toplevel_wm_caps(void *data, struct xdg_toplevel *xdg_top, struct wl_array *caps) { } struct wl_callback_listener callback_listener; void frame_new(void *date, struct wl_callback *cb, uint32_t _) { wl_callback_destroy(cb); cb = wl_surface_frame(wl_surface); wl_callback_add_listener(cb, &callback_listener, 0); draw(); } void wm_base_ping(void *data, struct xdg_wm_base *wm_base, uint32_t serial) { xdg_wm_base_pong(wm_base, serial); } struct xdg_wm_base_listener wm_base_listener; // Функция для прослушивания глобальных объектов void registry_global(void *data, struct wl_registry *reg, uint32_t name, const char *intf, uint32_t version) { printf("%u\t%s\tv:%u\n", name, intf, version); if (!strcmp(intf, wl_compositor_interface.name)) { compositor = wl_registry_bind(reg, name, &wl_compositor_interface, version); } else if (!strcmp(intf, wl_shm_interface.name)) { shm = wl_registry_bind(reg, name, &wl_shm_interface, version); } else if (!strcmp(intf, xdg_wm_base_interface.name)) { xdg_wm_base = wl_registry_bind(reg, name, &xdg_wm_base_interface, version); xdg_wm_base_add_listener(xdg_wm_base, &wm_base_listener, 0); } } void registry_global_remove(void *data, struct wl_registry *reg, uint32_t name) {} struct wl_callback_listener callback_listener = { .done = frame_new}; struct xdg_wm_base_listener wm_base_listener = { .ping = wm_base_ping}; struct xdg_toplevel_listener top_listener = { .configure = xdg_toplevel_conf, .close = xdg_toplevel_cls, .configure_bounds = xdg_toplevel_bounds, .wm_capabilities = xdg_toplevel_wm_caps}; struct xdg_surface_listener surface_listener = { .configure = xdg_surface_conf}; struct wl_registry_listener registry_listener = { .global = registry_global, .global_remove = registry_global_remove}; int32_t run_wayland() { struct wl_display *display = wl_display_connect(0); if (!display) return -1; struct wl_registry *registry = wl_display_get_registry(display); wl_registry_add_listener(registry, ®istry_listener, 0); wl_display_roundtrip(display); wl_surface = wl_compositor_create_surface(compositor); struct wl_callback *wl_callback = wl_surface_frame(wl_surface); wl_callback_add_listener(wl_callback, &callback_listener, 0); xdg_surface = xdg_wm_base_get_xdg_surface(xdg_wm_base, wl_surface); xdg_surface_add_listener(xdg_surface, &surface_listener, 0); xdg_toplevel = xdg_surface_get_toplevel(xdg_surface); xdg_toplevel_add_listener(xdg_toplevel, &top_listener, 0); xdg_toplevel_set_title(xdg_toplevel, "Custom client"); wl_surface_commit(wl_surface); while (wl_display_dispatch(display)) { if (need_close) { printf("Window closing\n"); break; } } if (buffer) wl_buffer_destroy(buffer); xdg_toplevel_destroy(xdg_toplevel); xdg_surface_destroy(xdg_surface); wl_surface_destroy(wl_surface); wl_display_disconnect(display); return 0; }