Compare commits
6 Commits
9cdc529918
...
e0ac79bae1
| Author | SHA1 | Date | |
|---|---|---|---|
| e0ac79bae1 | |||
| 0d3efeda49 | |||
| 6880efafab | |||
| eeb632ed0e | |||
| 1f700295ff | |||
| b11a56ad06 |
@@ -24,27 +24,55 @@ set(PROTOCOL_XML ${WAYLAND_PROTOCOLS_DIR}/stable/xdg-shell/xdg-shell.xml)
|
|||||||
|
|
||||||
# Генерируем заголовочный файл протокола
|
# Генерируем заголовочный файл протокола
|
||||||
add_custom_command(
|
add_custom_command(
|
||||||
OUTPUT xdg-shell-client-protocol.h
|
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/xdg-shell-client-protocol.h
|
||||||
COMMAND ${WAYLAND_SCANNER} client-header ${PROTOCOL_XML} xdg-shell-client-protocol.h
|
COMMAND ${WAYLAND_SCANNER} client-header ${PROTOCOL_XML} ${CMAKE_CURRENT_BINARY_DIR}/xdg-shell-client-protocol.h
|
||||||
DEPENDS ${PROTOCOL_XML}
|
DEPENDS ${PROTOCOL_XML}
|
||||||
)
|
)
|
||||||
|
|
||||||
# Генерируем исходный файл протокола
|
# Генерируем исходный файл протокола
|
||||||
add_custom_command(
|
add_custom_command(
|
||||||
OUTPUT xdg-shell-client-protocol.c
|
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/xdg-shell-client-protocol.c
|
||||||
COMMAND ${WAYLAND_SCANNER} private-code ${PROTOCOL_XML} xdg-shell-client-protocol.c
|
COMMAND ${WAYLAND_SCANNER} private-code ${PROTOCOL_XML} ${CMAKE_CURRENT_BINARY_DIR}/xdg-shell-client-protocol.c
|
||||||
DEPENDS ${PROTOCOL_XML}
|
DEPENDS ${PROTOCOL_XML}
|
||||||
)
|
)
|
||||||
|
|
||||||
# Цель для генерации протокола
|
# Цель для генерации протокола
|
||||||
add_custom_target(generate-xdg-shell DEPENDS xdg-shell-client-protocol.h xdg-shell-client-protocol.c)
|
add_custom_target(generate-xdg-shell DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/xdg-shell-client-protocol.h ${CMAKE_CURRENT_BINARY_DIR}/xdg-shell-client-protocol.c)
|
||||||
|
|
||||||
# Создаем исполняемый файл из ассемблерного, C и сгенерированного кода
|
# Создаем исполняемый файл из ассемблерного, C и сгенерированного кода
|
||||||
add_executable(wayland asm.asm c.c xdg-shell-client-protocol.c)
|
file(GLOB_RECURSE WAYLAND_SOURCES CONFIGURE_DEPENDS
|
||||||
|
"${CMAKE_CURRENT_SOURCE_DIR}/src/*.c"
|
||||||
|
"${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp"
|
||||||
|
"${CMAKE_CURRENT_SOURCE_DIR}/src/*.asm"
|
||||||
|
)
|
||||||
|
|
||||||
# Зависимость от генерации протокола
|
# Append the generated XDG C source (will be created in the binary dir)
|
||||||
|
list(APPEND WAYLAND_SOURCES ${CMAKE_CURRENT_BINARY_DIR}/xdg-shell-client-protocol.c)
|
||||||
|
|
||||||
|
# Create executable from collected sources
|
||||||
|
add_executable(wayland ${WAYLAND_SOURCES})
|
||||||
|
|
||||||
|
# Ensure generated files are produced before building the target
|
||||||
add_dependencies(wayland generate-xdg-shell)
|
add_dependencies(wayland generate-xdg-shell)
|
||||||
|
|
||||||
|
# Include headers and binary dir where generated headers are written
|
||||||
|
target_include_directories(wayland PRIVATE
|
||||||
|
${CMAKE_CURRENT_SOURCE_DIR}/include
|
||||||
|
${CMAKE_CURRENT_BINARY_DIR}
|
||||||
|
${WAYLAND_CLIENT_INCLUDE_DIRS}
|
||||||
|
${XKBCOMMON_INCLUDE_DIRS}
|
||||||
|
)
|
||||||
|
|
||||||
pkg_check_modules(WAYLAND_CLIENT REQUIRED wayland-client)
|
pkg_check_modules(WAYLAND_CLIENT REQUIRED wayland-client)
|
||||||
target_link_libraries(wayland ${WAYLAND_CLIENT_LIBRARIES})
|
set(WAYLAND_CLIENT_LIBRARIES ${WAYLAND_CLIENT_LIBRARIES})
|
||||||
target_include_directories(wayland PRIVATE ${WAYLAND_CLIENT_INCLUDE_DIRS} ${CMAKE_CURRENT_BINARY_DIR})
|
set(WAYLAND_CLIENT_INCLUDE_DIRS ${WAYLAND_CLIENT_INCLUDE_DIRS})
|
||||||
|
|
||||||
|
# xkbcommon for keyboard layout handling
|
||||||
|
pkg_check_modules(XKBCOMMON REQUIRED xkbcommon)
|
||||||
|
set(XKBCOMMON_LIBRARIES ${XKBCOMMON_LIBRARIES})
|
||||||
|
set(XKBCOMMON_INCLUDE_DIRS ${XKBCOMMON_INCLUDE_DIRS})
|
||||||
|
|
||||||
|
find_package(Threads REQUIRED)
|
||||||
|
|
||||||
|
# Link to system libraries
|
||||||
|
target_link_libraries(wayland PRIVATE ${WAYLAND_CLIENT_LIBRARIES} ${XKBCOMMON_LIBRARIES} Threads::Threads)
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
global main
|
|
||||||
extern run_wayland
|
|
||||||
|
|
||||||
section .text
|
|
||||||
main:
|
|
||||||
enter 0, 0
|
|
||||||
call run_wayland
|
|
||||||
leave
|
|
||||||
ret
|
|
||||||
194
wayland/c.c
194
wayland/c.c
@@ -1,194 +0,0 @@
|
|||||||
#include <wayland-client.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <sys/mman.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#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;
|
|
||||||
}
|
|
||||||
14
wayland/include/input.h
Normal file
14
wayland/include/input.h
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
#ifndef WAYLAND_INPUT_H
|
||||||
|
#define WAYLAND_INPUT_H
|
||||||
|
|
||||||
|
#include <wayland-client.h>
|
||||||
|
|
||||||
|
/* Вызывается при привязке wl_seat регистром */
|
||||||
|
void input_register_seat(struct wl_seat *seat);
|
||||||
|
/* Очистка input (разрушить keyboard и xkb state) */
|
||||||
|
void input_cleanup(void);
|
||||||
|
|
||||||
|
/* Возвращает wl_surface, имеющую фокус клавиатуры или NULL */
|
||||||
|
struct wl_surface *input_get_keyboard_focus(void);
|
||||||
|
|
||||||
|
#endif
|
||||||
14
wayland/include/registry.h
Normal file
14
wayland/include/registry.h
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
#ifndef WAYLAND_REGISTRY_H
|
||||||
|
#define WAYLAND_REGISTRY_H
|
||||||
|
|
||||||
|
#include <wayland-client.h>
|
||||||
|
|
||||||
|
int registry_global_bind(struct wl_display *display);
|
||||||
|
void registry_global_unbind(void);
|
||||||
|
|
||||||
|
/* Доступ к привязанным глобальным объектам */
|
||||||
|
struct wl_compositor *registry_get_compositor(void);
|
||||||
|
struct wl_shm *registry_get_shm(void);
|
||||||
|
struct xdg_wm_base *registry_get_xdg_wm_base(void);
|
||||||
|
|
||||||
|
#endif
|
||||||
18
wayland/include/wayland_runtime.h
Normal file
18
wayland/include/wayland_runtime.h
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#ifndef WAYLAND_RUNTIME_H
|
||||||
|
#define WAYLAND_RUNTIME_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
/* Инициализация общего соединения Wayland и подготовка глобальных данных */
|
||||||
|
int32_t init_wayland(void);
|
||||||
|
|
||||||
|
/* Создать поток для окна; вернуть индекс слота или отрицательное при ошибке */
|
||||||
|
int32_t run_window(void);
|
||||||
|
|
||||||
|
/* Блокировать до завершения всех оконных потоков */
|
||||||
|
void wait_for_windows(void);
|
||||||
|
|
||||||
|
/* Остановить оконные потоки, очистить input и закрыть соединение Wayland */
|
||||||
|
void destroy_wayland(void);
|
||||||
|
|
||||||
|
#endif
|
||||||
31
wayland/include/window.h
Normal file
31
wayland/include/window.h
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
#ifndef WAYLAND_WINDOW_H
|
||||||
|
#define WAYLAND_WINDOW_H
|
||||||
|
|
||||||
|
#include <wayland-client.h>
|
||||||
|
|
||||||
|
/* Данные одного Wayland-окна (одна поверхность) */
|
||||||
|
struct wayland_window {
|
||||||
|
int id;
|
||||||
|
struct wl_surface *wl_surface;
|
||||||
|
struct wl_buffer *buffer;
|
||||||
|
struct wl_callback *frame_callback;
|
||||||
|
struct xdg_surface *xdg_surface;
|
||||||
|
struct xdg_toplevel *xdg_toplevel;
|
||||||
|
struct wl_event_queue *queue; /* очередь событий для окна */
|
||||||
|
uint8_t *data;
|
||||||
|
int32_t width;
|
||||||
|
int32_t height;
|
||||||
|
int need_close;
|
||||||
|
uint8_t color;
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Инициализация окна; структура предоставляется вызывающим */
|
||||||
|
int window_init(struct wl_display *display, struct wl_event_queue *queue, struct wayland_window *win);
|
||||||
|
|
||||||
|
/* Нечётное значение — окно запросило закрытие */
|
||||||
|
int window_should_close(struct wayland_window *win);
|
||||||
|
|
||||||
|
/* Уничтожить окно и связанные ресурсы */
|
||||||
|
void window_destroy(struct wayland_window *win);
|
||||||
|
|
||||||
|
#endif
|
||||||
25
wayland/src/asm.asm
Normal file
25
wayland/src/asm.asm
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
global main
|
||||||
|
extern init_wayland
|
||||||
|
extern run_window
|
||||||
|
extern wait_for_windows
|
||||||
|
extern destroy_wayland
|
||||||
|
|
||||||
|
section .text
|
||||||
|
main:
|
||||||
|
enter 0, 0
|
||||||
|
|
||||||
|
call init_wayland
|
||||||
|
cmp eax, 0
|
||||||
|
jl .shutdown
|
||||||
|
|
||||||
|
; Launch the first window thread (duplicate calls for more windows)
|
||||||
|
call run_window
|
||||||
|
call run_window
|
||||||
|
call run_window
|
||||||
|
|
||||||
|
call wait_for_windows
|
||||||
|
|
||||||
|
.shutdown:
|
||||||
|
call destroy_wayland
|
||||||
|
leave
|
||||||
|
ret
|
||||||
179
wayland/src/c.c
Normal file
179
wayland/src/c.c
Normal file
@@ -0,0 +1,179 @@
|
|||||||
|
#include <errno.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <stdatomic.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <wayland-client.h>
|
||||||
|
|
||||||
|
#include "input.h"
|
||||||
|
#include "registry.h"
|
||||||
|
#include "wayland_runtime.h"
|
||||||
|
#include "window.h"
|
||||||
|
|
||||||
|
#define MAX_WINDOW_THREADS 128
|
||||||
|
|
||||||
|
struct window_thread_slot
|
||||||
|
{
|
||||||
|
pthread_t thread;
|
||||||
|
int active;
|
||||||
|
struct wl_event_queue *queue;
|
||||||
|
struct wayland_window window;
|
||||||
|
};
|
||||||
|
|
||||||
|
static struct wl_display *g_display = NULL;
|
||||||
|
static struct window_thread_slot g_slots[MAX_WINDOW_THREADS];
|
||||||
|
static pthread_mutex_t g_thread_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||||
|
static pthread_cond_t g_thread_cond = PTHREAD_COND_INITIALIZER;
|
||||||
|
static atomic_int g_active_threads = 0;
|
||||||
|
static atomic_bool g_shutdown = 0;
|
||||||
|
static int g_initialized = 0;
|
||||||
|
|
||||||
|
static void signal_thread_exit(struct window_thread_slot *slot)
|
||||||
|
{
|
||||||
|
pthread_mutex_lock(&g_thread_lock);
|
||||||
|
slot->active = 0;
|
||||||
|
atomic_fetch_sub(&g_active_threads, 1);
|
||||||
|
pthread_cond_broadcast(&g_thread_cond);
|
||||||
|
pthread_mutex_unlock(&g_thread_lock);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void *window_thread_main(void *arg)
|
||||||
|
{
|
||||||
|
struct window_thread_slot *slot = arg;
|
||||||
|
memset(&slot->window, 0, sizeof(slot->window));
|
||||||
|
|
||||||
|
slot->queue = wl_display_create_queue(g_display);
|
||||||
|
if (!slot->queue)
|
||||||
|
{
|
||||||
|
signal_thread_exit(slot);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (window_init(g_display, slot->queue, &slot->window) < 0)
|
||||||
|
{
|
||||||
|
input_cleanup();
|
||||||
|
wl_event_queue_destroy(slot->queue);
|
||||||
|
slot->queue = NULL;
|
||||||
|
signal_thread_exit(slot);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (!atomic_load(&g_shutdown) && !window_should_close(&slot->window))
|
||||||
|
{
|
||||||
|
int dispatch = wl_display_dispatch_queue(g_display, slot->queue);
|
||||||
|
if (dispatch < 0)
|
||||||
|
{
|
||||||
|
if (errno == EINTR)
|
||||||
|
continue;
|
||||||
|
atomic_store(&g_shutdown, 1);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("Window #%d closed\n", slot->window.id);
|
||||||
|
|
||||||
|
input_cleanup();
|
||||||
|
window_destroy(&slot->window);
|
||||||
|
|
||||||
|
if (slot->queue)
|
||||||
|
{
|
||||||
|
wl_event_queue_destroy(slot->queue);
|
||||||
|
slot->queue = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
signal_thread_exit(slot);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t init_wayland(void)
|
||||||
|
{
|
||||||
|
if (g_initialized)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
g_display = wl_display_connect(NULL);
|
||||||
|
if (!g_display)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
/* Привязать глобальные объекты и запустить поток обработки (дефолтная очередь).
|
||||||
|
* `registry_global_bind` добавляет listener и делает roundtrip. */
|
||||||
|
if (registry_global_bind(g_display) < 0)
|
||||||
|
{
|
||||||
|
wl_display_disconnect(g_display);
|
||||||
|
g_display = NULL;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
atomic_store(&g_shutdown, 0);
|
||||||
|
atomic_store(&g_active_threads, 0);
|
||||||
|
g_initialized = 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t run_window(void)
|
||||||
|
{
|
||||||
|
if (!g_initialized || !g_display)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
int slot_index = -1;
|
||||||
|
|
||||||
|
pthread_mutex_lock(&g_thread_lock);
|
||||||
|
for (int i = 0; i < MAX_WINDOW_THREADS; ++i)
|
||||||
|
{
|
||||||
|
if (!g_slots[i].active)
|
||||||
|
{
|
||||||
|
slot_index = i;
|
||||||
|
g_slots[i].active = 1;
|
||||||
|
atomic_fetch_add(&g_active_threads, 1);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pthread_mutex_unlock(&g_thread_lock);
|
||||||
|
|
||||||
|
if (slot_index < 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
int create_res = pthread_create(&g_slots[slot_index].thread, NULL, window_thread_main, &g_slots[slot_index]);
|
||||||
|
if (create_res != 0)
|
||||||
|
{
|
||||||
|
pthread_mutex_lock(&g_thread_lock);
|
||||||
|
g_slots[slot_index].active = 0;
|
||||||
|
atomic_fetch_sub(&g_active_threads, 1);
|
||||||
|
pthread_mutex_unlock(&g_thread_lock);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
pthread_detach(g_slots[slot_index].thread);
|
||||||
|
return slot_index;
|
||||||
|
}
|
||||||
|
|
||||||
|
void wait_for_windows(void)
|
||||||
|
{
|
||||||
|
pthread_mutex_lock(&g_thread_lock);
|
||||||
|
while (atomic_load(&g_active_threads) > 0)
|
||||||
|
pthread_cond_wait(&g_thread_cond, &g_thread_lock);
|
||||||
|
pthread_mutex_unlock(&g_thread_lock);
|
||||||
|
}
|
||||||
|
|
||||||
|
void destroy_wayland(void)
|
||||||
|
{
|
||||||
|
if (!g_initialized)
|
||||||
|
return;
|
||||||
|
|
||||||
|
wait_for_windows();
|
||||||
|
|
||||||
|
input_cleanup();
|
||||||
|
/* Остановить поток глобального реестра и сбросить глобальные данные */
|
||||||
|
registry_global_unbind();
|
||||||
|
|
||||||
|
if (g_display)
|
||||||
|
{
|
||||||
|
wl_display_disconnect(g_display);
|
||||||
|
g_display = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(g_slots, 0, sizeof(g_slots));
|
||||||
|
|
||||||
|
g_initialized = 0;
|
||||||
|
atomic_store(&g_shutdown, 0);
|
||||||
|
}
|
||||||
217
wayland/src/input.c
Normal file
217
wayland/src/input.c
Normal file
@@ -0,0 +1,217 @@
|
|||||||
|
#include "input.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <xkbcommon/xkbcommon.h>
|
||||||
|
#include <wayland-client.h>
|
||||||
|
#include <sys/mman.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
|
||||||
|
static __thread struct wl_seat *seat = NULL;
|
||||||
|
static __thread struct wl_keyboard *keyboard = NULL;
|
||||||
|
static __thread struct xkb_context *xkb_ctx = NULL;
|
||||||
|
static __thread struct xkb_keymap *xkb_keymap = NULL;
|
||||||
|
static __thread struct xkb_state *xkb_state = NULL;
|
||||||
|
static struct wl_surface *keyboard_focus = NULL;
|
||||||
|
|
||||||
|
/* Обработчики клавиатуры */
|
||||||
|
static void keyboard_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format, int fd, uint32_t size)
|
||||||
|
{
|
||||||
|
printf("keyboard: keymap format=%u size=%u\n", format, size);
|
||||||
|
if (fd < 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1)
|
||||||
|
{
|
||||||
|
close(fd);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *map = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
|
||||||
|
if (map == MAP_FAILED)
|
||||||
|
{
|
||||||
|
perror("mmap");
|
||||||
|
close(fd);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!xkb_ctx)
|
||||||
|
xkb_ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
|
||||||
|
|
||||||
|
if (xkb_state)
|
||||||
|
{
|
||||||
|
xkb_state_unref(xkb_state);
|
||||||
|
xkb_state = NULL;
|
||||||
|
}
|
||||||
|
if (xkb_keymap)
|
||||||
|
{
|
||||||
|
xkb_keymap_unref(xkb_keymap);
|
||||||
|
xkb_keymap = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
xkb_keymap = xkb_keymap_new_from_string(xkb_ctx, map, XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS);
|
||||||
|
if (!xkb_keymap)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Unable to compile xkb keymap\n");
|
||||||
|
munmap(map, size);
|
||||||
|
close(fd);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
xkb_state = xkb_state_new(xkb_keymap);
|
||||||
|
|
||||||
|
munmap(map, size);
|
||||||
|
close(fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void keyboard_enter(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface, struct wl_array *keys)
|
||||||
|
{
|
||||||
|
printf("keyboard: enter serial=%u surface=%p\n", serial, surface);
|
||||||
|
/* Сохраняем поверхность, которая получила фокус клавиатуры */
|
||||||
|
keyboard_focus = surface;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void keyboard_leave(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface)
|
||||||
|
{
|
||||||
|
printf("keyboard: leave serial=%u surface=%p\n", serial, surface);
|
||||||
|
/* Если уходим с фокусной поверхности — сбросить фокус */
|
||||||
|
if (keyboard_focus == surface)
|
||||||
|
keyboard_focus = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void keyboard_key(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
|
||||||
|
{
|
||||||
|
const char *s = (state == WL_KEYBOARD_KEY_STATE_PRESSED) ? "PRESSED"
|
||||||
|
: (state == WL_KEYBOARD_KEY_STATE_RELEASED) ? "RELEASED"
|
||||||
|
: (state == WL_KEYBOARD_KEY_STATE_REPEATED) ? "REPEATED"
|
||||||
|
: "Unknown";
|
||||||
|
/* Отладочный вывод удалён */
|
||||||
|
|
||||||
|
if (xkb_state && keyboard_focus)
|
||||||
|
{
|
||||||
|
xkb_keycode_t kc = (xkb_keycode_t)key + 8;
|
||||||
|
xkb_keysym_t ks = xkb_state_key_get_one_sym(xkb_state, kc);
|
||||||
|
if (ks != XKB_KEY_NoSymbol)
|
||||||
|
{
|
||||||
|
char buf[64];
|
||||||
|
int n = xkb_keysym_to_utf8(ks, buf, sizeof(buf));
|
||||||
|
if (ks == XKB_KEY_Return)
|
||||||
|
sprintf(buf, "Return");
|
||||||
|
if (n > 0)
|
||||||
|
printf("keyboard: symbol '%s' (keysym 0x%x) on surface:%p\n", buf, ks, keyboard_focus);
|
||||||
|
else
|
||||||
|
printf("keyboard: keysym 0x%x (no UTF-8 representation)\n", ks);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void keyboard_modifiers(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, uint32_t group)
|
||||||
|
{
|
||||||
|
printf("keyboard: modifiers serial=%u depressed=%u latched=%u locked=%u group=%u\n", serial, mods_depressed, mods_latched, mods_locked, group);
|
||||||
|
if (xkb_state)
|
||||||
|
{
|
||||||
|
xkb_state_update_mask(xkb_state, mods_depressed, mods_latched, mods_locked, group, 0, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void keyboard_repeat_info(void *data, struct wl_keyboard *keyboard, int32_t rate, int32_t delay)
|
||||||
|
{
|
||||||
|
printf("keyboard: repeat rate=%d delay=%d\n", rate, delay);
|
||||||
|
}
|
||||||
|
|
||||||
|
static const struct wl_keyboard_listener keyboard_listener = {
|
||||||
|
.keymap = keyboard_keymap,
|
||||||
|
.enter = keyboard_enter,
|
||||||
|
.leave = keyboard_leave,
|
||||||
|
.key = keyboard_key,
|
||||||
|
.modifiers = keyboard_modifiers,
|
||||||
|
.repeat_info = keyboard_repeat_info};
|
||||||
|
|
||||||
|
/* Обработчики wl_seat */
|
||||||
|
static void seat_capabilities(void *data, struct wl_seat *seat_local, uint32_t caps)
|
||||||
|
{
|
||||||
|
if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !keyboard)
|
||||||
|
{
|
||||||
|
keyboard = wl_seat_get_keyboard(seat_local);
|
||||||
|
wl_keyboard_add_listener(keyboard, &keyboard_listener, NULL);
|
||||||
|
printf("Seat reports keyboard capability - keyboard bound\n");
|
||||||
|
}
|
||||||
|
else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && keyboard)
|
||||||
|
{
|
||||||
|
wl_keyboard_destroy(keyboard);
|
||||||
|
keyboard = NULL;
|
||||||
|
if (xkb_state)
|
||||||
|
{
|
||||||
|
xkb_state_unref(xkb_state);
|
||||||
|
xkb_state = NULL;
|
||||||
|
}
|
||||||
|
if (xkb_keymap)
|
||||||
|
{
|
||||||
|
xkb_keymap_unref(xkb_keymap);
|
||||||
|
xkb_keymap = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void seat_name(void *data, struct wl_seat *seat, const char *name)
|
||||||
|
{
|
||||||
|
printf("Seat name: %s\n", name);
|
||||||
|
}
|
||||||
|
|
||||||
|
static const struct wl_seat_listener seat_listener = {
|
||||||
|
.capabilities = seat_capabilities,
|
||||||
|
.name = seat_name};
|
||||||
|
|
||||||
|
void input_register_seat(struct wl_seat *s)
|
||||||
|
{
|
||||||
|
if (!s)
|
||||||
|
return;
|
||||||
|
if (seat)
|
||||||
|
{
|
||||||
|
if (keyboard)
|
||||||
|
{
|
||||||
|
wl_keyboard_destroy(keyboard);
|
||||||
|
keyboard = NULL;
|
||||||
|
}
|
||||||
|
wl_seat_destroy(seat);
|
||||||
|
seat = NULL;
|
||||||
|
}
|
||||||
|
seat = s;
|
||||||
|
wl_seat_add_listener(seat, &seat_listener, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
void input_cleanup(void)
|
||||||
|
{
|
||||||
|
if (keyboard)
|
||||||
|
{
|
||||||
|
wl_keyboard_destroy(keyboard);
|
||||||
|
keyboard = NULL;
|
||||||
|
}
|
||||||
|
if (seat)
|
||||||
|
{
|
||||||
|
wl_seat_destroy(seat);
|
||||||
|
seat = NULL;
|
||||||
|
}
|
||||||
|
if (xkb_state)
|
||||||
|
{
|
||||||
|
xkb_state_unref(xkb_state);
|
||||||
|
xkb_state = NULL;
|
||||||
|
}
|
||||||
|
if (xkb_keymap)
|
||||||
|
{
|
||||||
|
xkb_keymap_unref(xkb_keymap);
|
||||||
|
xkb_keymap = NULL;
|
||||||
|
}
|
||||||
|
if (xkb_ctx)
|
||||||
|
{
|
||||||
|
xkb_context_unref(xkb_ctx);
|
||||||
|
xkb_ctx = NULL;
|
||||||
|
}
|
||||||
|
keyboard_focus = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct wl_surface *input_get_keyboard_focus(void)
|
||||||
|
{
|
||||||
|
return keyboard_focus;
|
||||||
|
}
|
||||||
222
wayland/src/registry.c
Normal file
222
wayland/src/registry.c
Normal file
@@ -0,0 +1,222 @@
|
|||||||
|
/* Copyright (c) 2025 */
|
||||||
|
|
||||||
|
#include "registry.h"
|
||||||
|
#include "input.h"
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <stdatomic.h>
|
||||||
|
#include <poll.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <wayland-client.h>
|
||||||
|
#include "xdg-shell-client-protocol.h"
|
||||||
|
|
||||||
|
static void wm_base_ping(void *data, struct xdg_wm_base *wm_base, uint32_t serial)
|
||||||
|
{
|
||||||
|
(void)data;
|
||||||
|
xdg_wm_base_pong(wm_base, serial);
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct xdg_wm_base_listener wm_base_listener = {
|
||||||
|
.ping = wm_base_ping
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/* Глобальные объекты, привязанные к единому registry-потоку (общие для процесса) */
|
||||||
|
static struct wl_compositor *global_compositor;
|
||||||
|
static struct wl_shm *global_shm;
|
||||||
|
static struct xdg_wm_base *global_wm_base;
|
||||||
|
/* Глобальный реестр живёт на весь срок программы (удаляется при сбросе) */
|
||||||
|
static struct wl_registry *global_registry = NULL;
|
||||||
|
|
||||||
|
/* Поток обработки глобальных событий: использует дефолтную очередь */
|
||||||
|
static pthread_t global_thread;
|
||||||
|
static _Atomic int global_thread_running = 0;
|
||||||
|
static struct wl_display *global_display = NULL;
|
||||||
|
|
||||||
|
static uint32_t clamp_version(uint32_t announced, uint32_t supported)
|
||||||
|
{
|
||||||
|
return (announced < supported) ? announced : supported;
|
||||||
|
}
|
||||||
|
|
||||||
|
static 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))
|
||||||
|
{
|
||||||
|
uint32_t ver = clamp_version(version, wl_compositor_interface.version);
|
||||||
|
/* Привязать compositor на дефолтной очереди сразу */
|
||||||
|
if (!global_compositor)
|
||||||
|
global_compositor = wl_registry_bind(reg, name, &wl_compositor_interface, ver);
|
||||||
|
}
|
||||||
|
else if (!strcmp(intf, wl_shm_interface.name))
|
||||||
|
{
|
||||||
|
uint32_t ver = clamp_version(version, wl_shm_interface.version);
|
||||||
|
if (!global_shm)
|
||||||
|
global_shm = wl_registry_bind(reg, name, &wl_shm_interface, ver);
|
||||||
|
}
|
||||||
|
else if (!strcmp(intf, xdg_wm_base_interface.name))
|
||||||
|
{
|
||||||
|
uint32_t ver = clamp_version(version, xdg_wm_base_interface.version);
|
||||||
|
if (!global_wm_base)
|
||||||
|
global_wm_base = wl_registry_bind(reg, name, &xdg_wm_base_interface, ver);
|
||||||
|
if (global_wm_base)
|
||||||
|
xdg_wm_base_add_listener(global_wm_base, &wm_base_listener, NULL);
|
||||||
|
}
|
||||||
|
else if (!strcmp(intf, wl_seat_interface.name))
|
||||||
|
{
|
||||||
|
uint32_t ver = clamp_version(version, wl_seat_interface.version);
|
||||||
|
/* Привязать seat на дефолтной очереди */
|
||||||
|
struct wl_seat *seat = wl_registry_bind(reg, name, &wl_seat_interface, ver);
|
||||||
|
if (seat)
|
||||||
|
input_register_seat(seat);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void registry_global_remove(void *data, struct wl_registry *reg, uint32_t name)
|
||||||
|
{
|
||||||
|
(void)data;
|
||||||
|
(void)reg;
|
||||||
|
(void)name;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const struct wl_registry_listener registry_listener = {
|
||||||
|
.global = registry_global,
|
||||||
|
.global_remove = registry_global_remove
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
static void destroy_global_objects(void)
|
||||||
|
{
|
||||||
|
if (global_compositor)
|
||||||
|
{
|
||||||
|
wl_compositor_destroy(global_compositor);
|
||||||
|
global_compositor = NULL;
|
||||||
|
}
|
||||||
|
if (global_shm)
|
||||||
|
{
|
||||||
|
wl_shm_destroy(global_shm);
|
||||||
|
global_shm = NULL;
|
||||||
|
}
|
||||||
|
if (global_wm_base)
|
||||||
|
{
|
||||||
|
xdg_wm_base_destroy(global_wm_base);
|
||||||
|
global_wm_base = NULL;
|
||||||
|
}
|
||||||
|
if (global_registry)
|
||||||
|
{
|
||||||
|
wl_registry_destroy(global_registry);
|
||||||
|
global_registry = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Привязка глобального реестра — привязать compositor/shm/xdg_wm_base и
|
||||||
|
* запустить поток обработки глобальной очереди. Возвращает 0 при успехе,
|
||||||
|
* -1 при ошибке.
|
||||||
|
*/
|
||||||
|
static void *registry_global_thread(void *arg)
|
||||||
|
{
|
||||||
|
struct wl_display *display = arg;
|
||||||
|
int fd = wl_display_get_fd(display);
|
||||||
|
while (global_thread_running)
|
||||||
|
{
|
||||||
|
struct pollfd pfd;
|
||||||
|
pfd.fd = fd;
|
||||||
|
pfd.events = POLLIN;
|
||||||
|
pfd.revents = 0;
|
||||||
|
int ret = poll(&pfd, 1, 100); /* таймаут 100 ms для проверки флага запуска */
|
||||||
|
if (ret == -1)
|
||||||
|
{
|
||||||
|
if (errno == EINTR)
|
||||||
|
continue;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (ret == 0)
|
||||||
|
continue;
|
||||||
|
if (pfd.revents & POLLIN)
|
||||||
|
{
|
||||||
|
int r = wl_display_dispatch(display);
|
||||||
|
if (r < 0)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
int registry_global_bind(struct wl_display *display)
|
||||||
|
{
|
||||||
|
if (!display)
|
||||||
|
return -1;
|
||||||
|
if (global_thread_running)
|
||||||
|
return 0; /* уже привязан */
|
||||||
|
/* Используем дефолтную очередь для глобальных объектов.
|
||||||
|
* Добавим listener и сделаем roundtrip, чтобы вызывающий код
|
||||||
|
* мог не делать этого заранее. Реестр привяжет глобалы сразу
|
||||||
|
* при получении событий. */
|
||||||
|
|
||||||
|
struct wl_registry *registry = wl_display_get_registry(display);
|
||||||
|
if (!registry)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
wl_registry_add_listener(registry, ®istry_listener, NULL);
|
||||||
|
if (wl_display_roundtrip(display) < 0)
|
||||||
|
{
|
||||||
|
/* произошла ошибка — уничтожить registry и вернуть ошибку */
|
||||||
|
wl_registry_destroy(registry);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
global_registry = registry;
|
||||||
|
|
||||||
|
if (!global_compositor || !global_shm || !global_wm_base)
|
||||||
|
{
|
||||||
|
destroy_global_objects();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Запустить поток обработки событий дефолтной очереди */
|
||||||
|
global_thread_running = 1;
|
||||||
|
global_display = display;
|
||||||
|
int thr_res = pthread_create(&global_thread, NULL, registry_global_thread, display);
|
||||||
|
if (thr_res != 0)
|
||||||
|
{
|
||||||
|
global_thread_running = 0;
|
||||||
|
destroy_global_objects();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void registry_global_unbind(void)
|
||||||
|
{
|
||||||
|
if (!global_thread_running)
|
||||||
|
return;
|
||||||
|
|
||||||
|
/* Корректно остановить поток */
|
||||||
|
global_thread_running = 0;
|
||||||
|
pthread_join(global_thread, NULL);
|
||||||
|
global_display = NULL;
|
||||||
|
|
||||||
|
destroy_global_objects();
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct wl_compositor *registry_get_compositor(void)
|
||||||
|
{
|
||||||
|
return global_compositor;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct wl_shm *registry_get_shm(void)
|
||||||
|
{
|
||||||
|
return global_shm;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct xdg_wm_base *registry_get_xdg_wm_base(void)
|
||||||
|
{
|
||||||
|
return global_wm_base;
|
||||||
|
}
|
||||||
243
wayland/src/window.c
Normal file
243
wayland/src/window.c
Normal file
@@ -0,0 +1,243 @@
|
|||||||
|
#include "window.h"
|
||||||
|
#include "registry.h"
|
||||||
|
#include "xdg-shell-client-protocol.h"
|
||||||
|
#include <wayland-client.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <sys/mman.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdatomic.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static atomic_uint_fast64_t shm_counter = 0;
|
||||||
|
|
||||||
|
static int32_t alloc_shm(size_t size)
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
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;
|
||||||
|
|
||||||
|
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, int32_t w, int32_t h)
|
||||||
|
{
|
||||||
|
if (!w || !h)
|
||||||
|
return;
|
||||||
|
if (win->width != w || win->height != h)
|
||||||
|
{
|
||||||
|
if (win->data)
|
||||||
|
{
|
||||||
|
munmap(win->data, win->width * win->height * 4);
|
||||||
|
win->data = NULL;
|
||||||
|
}
|
||||||
|
win->width = w;
|
||||||
|
win->height = h;
|
||||||
|
resize_canvas(win);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
wl_surface_damage_buffer(win->wl_surface, 0, 0, win->width, win->height);
|
||||||
|
wl_surface_commit(win->wl_surface);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void xdg_surface_conf(void *data, struct xdg_surface *xdg_surface_local, uint32_t serial)
|
||||||
|
{
|
||||||
|
xdg_surface_ack_configure(xdg_surface_local, serial);
|
||||||
|
struct wayland_window *win = data;
|
||||||
|
if (!win->data)
|
||||||
|
resize_canvas(win);
|
||||||
|
|
||||||
|
draw(win);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void xdg_toplevel_conf(void *data, struct xdg_toplevel *xdg_top, int32_t w, int32_t h, struct wl_array *states)
|
||||||
|
{
|
||||||
|
(void)states;
|
||||||
|
struct wayland_window *win = data;
|
||||||
|
resize_new(win, w, h);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void xdg_toplevel_cls(void *data, struct xdg_toplevel *xdg_top)
|
||||||
|
{
|
||||||
|
struct wayland_window *win = data;
|
||||||
|
(void)xdg_top;
|
||||||
|
win->need_close = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void xdg_toplevel_bounds(void *data, struct xdg_toplevel *xdg_top, int32_t w, int32_t h)
|
||||||
|
{
|
||||||
|
(void)data; (void)xdg_top; (void)w; (void)h;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void xdg_toplevel_wm_caps(void *data, struct xdg_toplevel *xdg_top, struct wl_array *caps)
|
||||||
|
{
|
||||||
|
(void)data; (void)xdg_top; (void)caps;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct wl_callback_listener callback_listener;
|
||||||
|
static void setup_frame_callback(struct wayland_window *win, struct wl_event_queue *queue)
|
||||||
|
{
|
||||||
|
struct wl_callback *cb = wl_surface_frame(win->wl_surface);
|
||||||
|
win->frame_callback = cb;
|
||||||
|
if (cb && queue)
|
||||||
|
wl_proxy_set_queue((struct wl_proxy *)cb, queue);
|
||||||
|
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, win->queue);
|
||||||
|
draw(win);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static struct wl_callback_listener callback_listener = {
|
||||||
|
.done = frame_new
|
||||||
|
};
|
||||||
|
static struct xdg_surface_listener surface_listener = {
|
||||||
|
.configure = xdg_surface_conf
|
||||||
|
};
|
||||||
|
static 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
|
||||||
|
};
|
||||||
|
|
||||||
|
int window_init(struct wl_display *display, struct wl_event_queue *queue, struct wayland_window *win)
|
||||||
|
{
|
||||||
|
static int id = 0;
|
||||||
|
(void)display;
|
||||||
|
struct wl_compositor *compositor = registry_get_compositor();
|
||||||
|
struct wl_shm *shm = registry_get_shm();
|
||||||
|
struct xdg_wm_base *wm = registry_get_xdg_wm_base();
|
||||||
|
if (!compositor || !shm || !wm)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
win->wl_surface = wl_compositor_create_surface(compositor);
|
||||||
|
win->queue = queue;
|
||||||
|
if (win->wl_surface && queue)
|
||||||
|
wl_proxy_set_queue((struct wl_proxy *)win->wl_surface, queue);
|
||||||
|
|
||||||
|
setup_frame_callback(win, queue);
|
||||||
|
|
||||||
|
win->xdg_surface = xdg_wm_base_get_xdg_surface(wm, win->wl_surface);
|
||||||
|
if (win->xdg_surface && queue)
|
||||||
|
wl_proxy_set_queue((struct wl_proxy *)win->xdg_surface, queue);
|
||||||
|
xdg_surface_add_listener(win->xdg_surface, &surface_listener, win);
|
||||||
|
|
||||||
|
win->xdg_toplevel = xdg_surface_get_toplevel(win->xdg_surface);
|
||||||
|
if (win->xdg_toplevel && queue)
|
||||||
|
wl_proxy_set_queue((struct wl_proxy *)win->xdg_toplevel, queue);
|
||||||
|
|
||||||
|
|
||||||
|
xdg_toplevel_add_listener(win->xdg_toplevel, &top_listener, win);
|
||||||
|
|
||||||
|
wl_surface_commit(win->wl_surface);
|
||||||
|
|
||||||
|
/* wm_base — глобальный объект, listener в registry */
|
||||||
|
|
||||||
|
/* Инициализация состояния */
|
||||||
|
win->id = id++;
|
||||||
|
win->width = 400;
|
||||||
|
win->height = 250;
|
||||||
|
win->data = NULL;
|
||||||
|
win->buffer = NULL;
|
||||||
|
win->frame_callback = NULL;
|
||||||
|
win->need_close = 0;
|
||||||
|
win->color = 0;
|
||||||
|
|
||||||
|
xdg_toplevel_set_app_id(win->xdg_toplevel, "my-wayland-window");
|
||||||
|
xdg_toplevel_set_title(win->xdg_toplevel, "Custom Title");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int window_should_close(struct wayland_window *win)
|
||||||
|
{
|
||||||
|
return win->need_close;
|
||||||
|
}
|
||||||
|
|
||||||
|
void window_destroy(struct wayland_window *win)
|
||||||
|
{
|
||||||
|
destroy_frame_callback(win);
|
||||||
|
if (win->buffer)
|
||||||
|
wl_buffer_destroy(win->buffer);
|
||||||
|
if (win->xdg_toplevel)
|
||||||
|
xdg_toplevel_destroy(win->xdg_toplevel);
|
||||||
|
if (win->xdg_surface)
|
||||||
|
xdg_surface_destroy(win->xdg_surface);
|
||||||
|
if (win->wl_surface)
|
||||||
|
wl_surface_destroy(win->wl_surface);
|
||||||
|
if (win->data)
|
||||||
|
munmap(win->data, win->width * win->height * 4);
|
||||||
|
}
|
||||||
|
/* конец файла */
|
||||||
|
#include "registry.h"
|
||||||
Reference in New Issue
Block a user