feat: separate wayland implementation

This commit is contained in:
2026-02-13 13:27:06 +01:00
parent 696d55db02
commit ae9de0a22e
11 changed files with 635 additions and 470 deletions

96
src/state/wayland.cpp Normal file
View File

@@ -0,0 +1,96 @@
#include "state/wayland.h"
#include <stdio.h>
#include <string>
#include <wayland-client-protocol.h>
#include "xdg-shell-client-protocol.h"
#define UNUSED(x) (void)(x)
WaylandState *WaylandState::GetInstance() {
if (!WaylandState::s_instance)
WaylandState::s_instance = new WaylandState();
return WaylandState::s_instance;
}
void WaylandState::wm_base_handle_ping(void *data, struct xdg_wm_base *wm_base,
uint32_t serial) {
UNUSED(data);
printf("INFO: ping method fired! sending pong...\n");
xdg_wm_base_pong(wm_base, serial);
}
void WaylandState::registry_handle_global(void *data, struct wl_registry *registry,
uint32_t name, const char *interface,
uint32_t version) {
UNUSED(data);
auto state = reinterpret_cast<WaylandState *>(data);
// printf("HANDLE_GLOBAL: new interface '%s', name = %d, version = %d\n",
// interface, name, version);
std::string interface_name(interface);
if (interface_name == "wl_compositor") {
state->m_compositor = reinterpret_cast<struct wl_compositor *>(
wl_registry_bind(registry, name, &wl_compositor_interface, version));
printf("[DEBUG] global_binding(%s): compositor %p\n", interface,
state->m_compositor);
}
if (interface_name == "xdg_wm_base") {
state->m_wm_base = reinterpret_cast<struct xdg_wm_base *>(
wl_registry_bind(registry, name, &xdg_wm_base_interface, version));
printf("[DEBUG] global_binding(%s): xdg_wm_base %p\n", interface,
state->m_wm_base);
if (state->m_wm_base == NULL) {
fprintf(stderr, "ERROR: could not bind to xdg_wm_base\n");
}
state->m_wm_base_listener.ping = wm_base_handle_ping;
xdg_wm_base_add_listener(state->m_wm_base, &state->m_wm_base_listener,
NULL);
}
}
void WaylandState::registry_handle_global_remove(void *data,
struct wl_registry *registry,
uint32_t name) {
UNUSED(data);
UNUSED(registry);
UNUSED(name);
}
WaylandState::WaylandState() {
m_display = wl_display_connect(NULL);
if (m_display == NULL) {
fprintf(stderr, "ERROR: could not connect to wl display\n");
exit(1);
}
struct wl_registry *registry = wl_display_get_registry(m_display);
if (registry == NULL) {
fprintf(stderr, "ERROR: could not connect to wl registry\n");
exit(1);
}
m_reg_listener.global = registry_handle_global;
m_reg_listener.global_remove = registry_handle_global_remove;
auto handle = wl_registry_add_listener(registry, &m_reg_listener, this);
printf("[DEBUG] wl_registry listener bound: %d\n", handle);
wl_display_roundtrip(m_display);
}
WaylandState::~WaylandState() {
wl_display_flush(m_display);
wl_display_roundtrip(m_display);
if (m_wm_base)
xdg_wm_base_destroy(m_wm_base);
if (m_compositor)
wl_compositor_destroy(m_compositor);
wl_display_disconnect(m_display);
}
WaylandState *WaylandState::s_instance = nullptr;