Поддержка многопоточности

This commit is contained in:
2025-11-16 20:50:02 +03:00
parent eeb632ed0e
commit 6880efafab
9 changed files with 420 additions and 57 deletions

View File

@@ -9,6 +9,7 @@
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdatomic.h>
// Moving all window specific data into `struct wayland_window`.
@@ -17,31 +18,62 @@ static struct xdg_surface_listener surface_listener;
static struct xdg_toplevel_listener top_listener;
static struct xdg_wm_base_listener wm_base_listener;
static atomic_uint_fast64_t shm_counter = 0;
static int32_t alloc_shm(size_t size)
{
char name[32];
sprintf(name, "/wayland-shm-%d", getpid());
char name[64];
uint64_t id = atomic_fetch_add(&shm_counter, 1);
snprintf(name, sizeof(name), "/wayland-shm-%d-%llu", getpid(), (unsigned long long)id);
int32_t fd = shm_open(name, O_RDWR | O_CREAT, 0600);
if (fd == -1)
return fd;
shm_unlink(name);
ftruncate(fd, size);
if (ftruncate(fd, size) == -1)
{
close(fd);
return -1;
}
return fd;
}
static void destroy_frame_callback(struct wayland_window *win)
{
if (win->frame_callback)
{
wl_callback_destroy(win->frame_callback);
win->frame_callback = NULL;
}
}
static void resize_canvas(struct wayland_window *win)
{
size_t stride = win->width * 4;
size_t size = stride * win->height;
int32_t fd = alloc_shm(size);
if (fd == -1)
return;
win->data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (win->buffer)
{
wl_buffer_destroy(win->buffer);
win->buffer = NULL;
}
void *map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (map == MAP_FAILED)
{
close(fd);
return;
}
struct wl_shm *shm = registry_get_shm();
struct wl_shm_pool *pool = wl_shm_create_pool(shm, fd, size);
win->buffer = wl_shm_pool_create_buffer(pool, 0, win->width, win->height, stride, WL_SHM_FORMAT_ARGB8888);
wl_shm_pool_destroy(pool);
close(fd);
win->data = map;
}
static void resize_new(struct wayland_window *win, int16_t w, int16_t h)
@@ -50,8 +82,11 @@ static void resize_new(struct wayland_window *win, int16_t w, int16_t h)
return;
if (win->width != w || win->height != h)
{
if(win->data)
munmap(win->data, win->width*win->height*4);
if (win->data)
{
munmap(win->data, win->width * win->height * 4);
win->data = NULL;
}
win->width = w;
win->height = h;
resize_canvas(win);
@@ -63,6 +98,9 @@ static void draw(struct wayland_window *win)
size_t stride = win->width * 4;
size_t size = stride * win->height;
if (!win->data || !win->buffer)
return;
memset(win->data, win->color++, size);
wl_surface_attach(win->wl_surface, win->buffer, 0, 0);
@@ -104,12 +142,20 @@ static void xdg_toplevel_wm_caps(void *data, struct xdg_toplevel *xdg_top, struc
(void)data; (void)xdg_top; (void)caps;
}
static void frame_new(void *date, struct wl_callback *cb, uint32_t _)
static void setup_frame_callback(struct wayland_window *win)
{
struct wayland_window *win = date;
wl_callback_destroy(cb);
cb = wl_surface_frame(win->wl_surface);
struct wl_callback *cb = wl_surface_frame(win->wl_surface);
win->frame_callback = cb;
wl_callback_add_listener(cb, &callback_listener, win);
}
static void frame_new(void *data, struct wl_callback *cb, uint32_t _)
{
struct wayland_window *win = data;
if (win->frame_callback == cb)
win->frame_callback = NULL;
wl_callback_destroy(cb);
setup_frame_callback(win);
draw(win);
}
@@ -128,10 +174,10 @@ int window_init(struct wl_display *display, struct wayland_window *win)
return -1;
win->wl_surface = wl_compositor_create_surface(compositor);
struct wl_callback *wl_callback = wl_surface_frame(win->wl_surface);
callback_listener.done = frame_new;
wl_callback_add_listener(wl_callback, &callback_listener, win);
win->frame_callback = NULL;
setup_frame_callback(win);
win->xdg_surface = xdg_wm_base_get_xdg_surface(wm, win->wl_surface);
surface_listener.configure = xdg_surface_conf;
@@ -156,6 +202,7 @@ int window_init(struct wl_display *display, struct wayland_window *win)
win->height = 100;
win->data = NULL;
win->buffer = NULL;
win->frame_callback = NULL;
win->need_close = 0;
win->color = 0;
@@ -169,6 +216,7 @@ int window_should_close(struct wayland_window *win)
void window_destroy(struct wayland_window *win)
{
destroy_frame_callback(win);
if (win->buffer)
wl_buffer_destroy(win->buffer);
if (win->xdg_toplevel)