Unfinished Wayland PR #1

Merged
IoI_xD merged 55 commits from IoI_xD/milsko:wayland into master 2025-12-10 11:11:02 +09:00
12 changed files with 581 additions and 3 deletions
Showing only changes of commit 843cc74c4c - Show all commits

wayland backend init commit

IoIxD 2025-11-29 17:56:33 -07:00

3
.gitignore vendored
View file

@ -3,8 +3,11 @@
examples/*
examples/*.exe
examples/*/*
examples/*/*.o
examples/*/*.exe
!examples/*/
!examples/*.*
!examples/*/*.*
compile_flags.txt
*.a
Makefile

View file

@ -26,13 +26,15 @@ typedef void* MwLLPixmap;
enum MwLLBackends {
MwLLBackendX11 = 0,
MwLLBackendGDI
MwLLBackendGDI,
MwLLBackendWayland,
};
struct _MwLLCommon {
void* user;
int copy_buffer;
int type;
int success;
MwLLHandler handler;
};
@ -56,6 +58,9 @@ struct _MwLLCommonPixmap {
#ifdef USE_GDI
#include <Mw/LowLevel/GDI.h>
#endif
#ifdef USE_WAYLAND
#include <Mw/LowLevel/Wayland.h>
#endif
union _MwLL {
struct _MwLLCommon common;
@ -65,6 +70,9 @@ union _MwLL {
#ifdef USE_GDI
struct _MwLLGDI gdi;
#endif
#ifdef USE_WAYLAND
struct _MwLLWayland wayland;
#endif
};
union _MwLLColor {
@ -75,6 +83,9 @@ union _MwLLColor {
#ifdef USE_GDI
struct _MwLLGDIColor gdi;
#endif
#ifdef USE_WAYLAND
struct _MwLLWaylandColor wayland;
#endif
};
union _MwLLPixmap {
@ -85,6 +96,9 @@ union _MwLLPixmap {
#ifdef USE_GDI
struct _MwLLGDIPixmap gdi;
#endif
#ifdef USE_WAYLAND
struct _MwLLWaylandPixmap wayland;
#endif
};
#endif
#include <Mw/TypeDefs.h>

View file

@ -0,0 +1,84 @@
/* $Id$ */
/*!
* @file Mw/LowLevel/Wayland.h
* @brief Wayland Backend
* @warning This is used internally
*/
#ifndef __MW_LOWLEVEL_WAYLAND_H__
#define __MW_LOWLEVEL_WAYLAND_H__
#include <Mw/MachDep.h>
#include <Mw/TypeDefs.h>
#include <Mw/LowLevel.h>
#include <wayland-client.h>
#include <xkbcommon/xkbcommon.h>
MWDECL int MwLLWaylandCallInit(void);
#ifndef WL_PROTOCOLS_DEFINED
#define WL_PROTOCOLS_DEFINED
#include "Wayland/xdg-shell-client-protocol.h"
#include "Wayland/xdg-decoration-client-protocol.h"
#include "Wayland/cursor-shape-client-protocol.h"
#endif
typedef struct wayland_protocol {
void* listener;
void* context;
} wayland_protocol_t;
typedef wayland_protocol_t*(wl_setup_func)(MwU32, struct _MwLLWayland*);
struct _MwLLWayland {
struct _MwLLCommon common;
// struct wl_shm* shm;
struct wl_display* display;
struct wl_registry* registry;
// struct wl_compositor* compositor;
// struct wl_seat* seat;
struct wl_surface* surface;
struct xdg_surface* xdg_surface;
// struct xdg_wm_base* xdg_wm_base;
struct xdg_toplevel* xdg_top_level;
struct wl_registry_listener registry_listener;
// struct zxdg_decoration_manager_v1* decoration_manager;
// struct zxdg_toplevel_decoration_v1* toplevel_decoration;
struct xdg_toplevel_listener xdg_toplevel_listener;
// struct xdg_wm_base_listener xdg_wm_base_listener;
struct xdg_surface_listener xdg_surface_listener;
// struct wl_pointer_listener pointer_listener;
// struct wl_seat_listener seat_listener;
// struct wl_keyboard_listener keyboard_listener;
// struct wl_shell_surface_listener shell_surface_listener;
// struct zxdg_toplevel_decoration_v1_listener decoration_listener;
struct {
const char* key;
wl_setup_func* value;
}* wl_protocol_setup_map;
struct {
const char* key;
wayland_protocol_t* value;
}* wl_protocol_map;
struct xkb_context* xkb_context;
MwBool configured;
MwBool running;
};
struct _MwLLWaylandColor {
struct _MwLLCommonColor common;
};
struct _MwLLWaylandPixmap {
struct _MwLLCommonPixmap common;
};
#endif

View file

@ -0,0 +1 @@
*protocol.h

View file

@ -0,0 +1 @@
Folder for wayland-scanner to place any of its files.

View file

@ -1,4 +1,9 @@
# $Id$
use_backend("x11");
if(system("wayland-scanner -v >/dev/null 2>&1") == 0){
use_backend("wayland", "x11");
} else {
use_backend("x11");
}
1;

View file

@ -27,6 +27,19 @@ if (grep(/^gdi$/, @backends)) {
$gl_libs = "-lopengl32 -lglu32";
}
if (grep(/^wayland$/, @backends)) {
add_cflags("-DUSE_WAYLAND");
new_object("src/backend/wayland.c");
add_libs("-lwayland-client -lxkbcommon");
scan_wayland_protocol("stable", "xdg-shell","");
scan_wayland_protocol("stable", "tablet","-v2");
scan_wayland_protocol("staging", "cursor-shape","-v1");
scan_wayland_protocol("unstable", "xdg-decoration","-unstable-v1");
$gl_libs = "-lGL -lGLU";
}
if (param_get("stb-image")) {
add_cflags("-DUSE_STB_IMAGE");
}

View file

@ -55,6 +55,23 @@ sub use_backend {
}
}
sub scan_wayland_protocol {
my $tier = $_[0];
my $proto = $_[1];
my $ver = $_[2];
my $proto_c = "src/backend/wayland-${proto}-protocol.c";
if(system("wayland-scanner private-code /usr/share/wayland-protocols/${tier}/${proto}/${proto}${ver}.xml ${proto_c}") != 0) {
print("^ Error on getting private code for /usr/share/wayland-protocols/${tier}/${proto}/${proto}${ver}.xml\n");
} else {
new_object($proto_c);
};
if(system("wayland-scanner client-header /usr/share/wayland-protocols/${tier}/${proto}/${proto}${ver}.xml include/Mw/LowLevel/Wayland/${proto}-client-protocol.h")) {
print("^ Error on getting client header for /usr/share/wayland-protocols/${tier}/${proto}/${proto}${ver}.xml\n");
};
}
our %params = ();
sub param_set {

1
src/backend/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
wayland*protocol.c

437
src/backend/wayland.c Normal file
View file

@ -0,0 +1,437 @@
/* $Id$ */
/**
TODO:
- would fit with Milsko's goal to support older compositors using wl_shell. would also suck to test because most compositors outright removed that around 2021...
*/
#include <Mw/Milsko.h>
#include "../../external/stb_ds.h"
#define WAYLAND_GET_INTERFACE(r, inter) shget(r->wayland.wl_protocol_map, inter##_interface.name)
// REGISTRY
static void new_protocol(void* data, struct wl_registry* registry,
MwU32 name, const char* interface,
MwU32 version) {
struct _MwLLWayland* wayland = data;
wl_setup_func* func = shget(wayland->wl_protocol_setup_map, interface);
if(func != NULL) {
shput(wayland->wl_protocol_map, interface, func(name, data));
}
};
static void protocol_removed(void* data,
struct wl_registry* registry,
MwU32 name) {
};
// SEAT
static void wl_seat_name(void* data, struct wl_seat* wl_seat, const char* name) {};
static void wl_seat_capabilities(void* data, struct wl_seat* wl_seat,
uint32_t capabilities) {};
// XDG SHELL
void xdg_wm_base_ping(void* data,
struct xdg_wm_base* xdg_wm_base,
uint32_t serial) {
// The compositor will send us a ping event to check that we're responsive.
// We need to send back a pong request immediately.
xdg_wm_base_pong(xdg_wm_base, serial);
};
// XDG SHELL SURFACE
static void wl_shell_surface_ping(void* data,
struct wl_shell_surface* wl_shell_surface,
uint32_t serial) {};
static void wl_shell_surface_configure(void* data,
struct wl_shell_surface* wl_shell_surface,
uint32_t edges,
int32_t width,
int32_t height) {};
static void wl_shell_surface_popup_done(void* data,
struct wl_shell_surface* wl_shell_surface) {};
static wayland_protocol_t* wl_shm_setup(MwU32 name, struct _MwLLWayland* wayland) {
wayland_protocol_t* proto = malloc(sizeof(wayland_protocol_t));
proto->listener = NULL;
proto->context = wl_registry_bind(wayland->registry, name, &wl_shm_interface, 1);
return proto;
}
static wayland_protocol_t* wl_seat_setup(MwU32 name, struct _MwLLWayland* wayland) {
wayland_protocol_t* proto = malloc(sizeof(wayland_protocol_t));
proto->listener = malloc(sizeof(struct wl_seat_listener));
((struct wl_seat_listener*)proto->listener)->name = wl_seat_name;
((struct wl_seat_listener*)proto->listener)->capabilities = wl_seat_capabilities;
proto->context = wl_registry_bind(wayland->registry, name, &wl_seat_interface, 1);
wl_seat_add_listener(proto->context, proto->listener, wayland);
return proto;
}
static wayland_protocol_t* wl_compositor_setup(MwU32 name, struct _MwLLWayland* wayland) {
wayland_protocol_t* proto = malloc(sizeof(wayland_protocol_t));
proto->listener = NULL;
proto->context = wl_registry_bind(wayland->registry, name, &wl_compositor_interface, 1);
return proto;
}
static wayland_protocol_t* xdg_wm_base_setup(MwU32 name, struct _MwLLWayland* wayland) {
wayland_protocol_t* proto = malloc(sizeof(wayland_protocol_t));
proto->listener = malloc(sizeof(struct xdg_wm_base_listener));
((struct xdg_wm_base_listener*)proto->listener)->ping = xdg_wm_base_ping;
proto->context = wl_registry_bind(wayland->registry, name, &xdg_wm_base_interface, 1);
xdg_wm_base_add_listener(proto->context, proto->listener, wayland);
return proto;
}
static wayland_protocol_t* wl_shell_surface_setup(MwU32 name, struct _MwLLWayland* wayland) {
wayland_protocol_t* proto = malloc(sizeof(wayland_protocol_t));
proto->listener = malloc(sizeof(struct wl_shell_surface_listener));
((struct wl_shell_surface_listener*)proto->listener)->ping = wl_shell_surface_ping;
((struct wl_shell_surface_listener*)proto->listener)->configure = wl_shell_surface_configure;
((struct wl_shell_surface_listener*)proto->listener)->popup_done = wl_shell_surface_popup_done;
proto->context = wl_registry_bind(wayland->registry, name, &wl_shell_surface_interface, 1);
wl_shell_surface_add_listener(proto->context, proto->listener, wayland);
return proto;
}
static wayland_protocol_t* wp_cursor_shape_manager_v1_setup(MwU32 name, struct _MwLLWayland* wayland) {
wayland_protocol_t* proto = malloc(sizeof(wayland_protocol_t));
proto->listener = NULL;
proto->context = wl_registry_bind(wayland->registry, name, &wp_cursor_shape_manager_v1_interface, 1);
return proto;
}
typedef struct zxdg_decoration_manager_v1_context {
struct zxdg_decoration_manager_v1* manager;
// struct zxdg_decoration_toplevel_v1* toplevel;
struct zxdg_toplevel_decoration_v1* decoration;
} zxdg_decoration_manager_v1_context_t;
static wayland_protocol_t* zxdg_decoration_manager_v1_setup(MwU32 name, struct _MwLLWayland* wayland) {
wayland_protocol_t* proto = malloc(sizeof(wayland_protocol_t));
zxdg_decoration_manager_v1_context_t* ctx = malloc(sizeof(zxdg_decoration_manager_v1_context_t));
proto->listener = NULL;
ctx->manager = wl_registry_bind(wayland->registry, name, &zxdg_decoration_manager_v1_interface, 1);
;
proto->context = ctx;
return proto;
}
static void xdg_toplevel_configure(void* data,
struct xdg_toplevel* xdg_toplevel,
int32_t width, int32_t height,
struct wl_array* states) {
struct _MwLLWayland* self = (struct _MwLLWayland*)data;
wl_surface_commit(self->surface);
return;
};
static void decoration_configure(
void* data, struct zxdg_toplevel_decoration_v1* zxdg_toplevel_decoration_v1,
uint32_t mode) {
}
static void xdg_toplevel_close(
void* data, struct xdg_toplevel* xdg_toplevel) {
struct _MwLLWayland* self = (struct _MwLLWayland*)data;
// Stop running if the user requests to close the toplevel
self->running = MwFALSE;
};
static void xdg_surface_configure(
void* data, struct xdg_surface* xdg_surface, uint32_t serial) {
struct _MwLLWayland* self = (struct _MwLLWayland*)data;
// The compositor configures our surface, acknowledge the configure event
xdg_surface_ack_configure(xdg_surface, serial);
if(self->configured) {
// If this isn't the first configure event we've received, we already
// have a buffer attached, so no need to do anything. Commit the
// surface to apply the configure acknowledgement.
wl_surface_commit(self->surface);
}
self->configured = MwTRUE;
}
static void shell_surface_ping(void* data,
struct wl_shell_surface* shell_surface,
uint32_t serial) {};
static void shell_surface_configure(
void* data, struct wl_shell_surface* shell_surface, uint32_t edges,
int32_t width, int32_t height) {};
static void shell_surface_popup_done(
void* data, struct wl_shell_surface* shell_surface) {};
static void setup_callbacks(struct _MwLLWayland* wayland) {
wayland->registry_listener.global = new_protocol;
wayland->registry_listener.global_remove = protocol_removed;
#define WL_INTERFACE(interface) \
shput(wayland->wl_protocol_setup_map, interface##_interface.name, (wl_setup_func*)interface##_setup);
WL_INTERFACE(wl_shm);
WL_INTERFACE(wl_seat);
WL_INTERFACE(wl_compositor);
WL_INTERFACE(xdg_wm_base);
WL_INTERFACE(wl_shell_surface);
WL_INTERFACE(zxdg_decoration_manager_v1);
WL_INTERFACE(wp_cursor_shape_manager_v1);
}
static MwLL MwLLCreateImpl(MwLL parent, int x, int y, int width, int height) {
MwLL r;
r = malloc(sizeof(*r));
MwLLCreateCommon(r);
r->common.copy_buffer = 1;
r->common.type = MwLLBackendWayland;
setup_callbacks(&r->wayland);
// Connect to the Wayland compositor
r->wayland.display = wl_display_connect(NULL);
if(r->wayland.display == NULL) {
printf("wayland: failed to create display\n");
r->common.success = MwFALSE;
return r;
}
// Obtain the wl_registry and fetch the list of globals
r->wayland.registry = wl_display_get_registry(r->wayland.display);
wl_registry_add_listener(r->wayland.registry, &r->wayland.registry_listener, r);
if(wl_display_roundtrip(r->wayland.display) == -1) {
printf("roundtrip failed\n");
r->common.success = MwFALSE;
return r;
}
// Check that all globals we require are available
if(
WAYLAND_GET_INTERFACE(r, wl_compositor)->context == NULL ||
WAYLAND_GET_INTERFACE(r, xdg_wm_base)->context == NULL ||
WAYLAND_GET_INTERFACE(r, wl_seat)->context == NULL) {
printf("no wl_shm, wl_compositor, wl_seat, or xdg_wm_base support\n");
r->common.success = MwFALSE;
return r;
}
// wl_log_set_handler_client(_MwLLWayland::logger);
r->wayland.xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
// Create a wl_surface, a xdg_surface and a xdg_toplevel
r->wayland.surface = wl_compositor_create_surface(WAYLAND_GET_INTERFACE(r, wl_compositor)->context);
r->wayland.xdg_surface =
xdg_wm_base_get_xdg_surface(WAYLAND_GET_INTERFACE(r, xdg_wm_base)->context, r->wayland.surface);
r->wayland.xdg_top_level = xdg_surface_get_toplevel(r->wayland.xdg_surface);
// setup mandatory listeners
r->wayland.xdg_surface_listener.configure = xdg_surface_configure;
r->wayland.xdg_toplevel_listener.configure = xdg_toplevel_configure;
r->wayland.xdg_toplevel_listener.close = xdg_toplevel_close;
xdg_surface_add_listener(r->wayland.xdg_surface, &r->wayland.xdg_surface_listener, r);
xdg_toplevel_add_listener(r->wayland.xdg_top_level, &r->wayland.xdg_toplevel_listener,
r);
xdg_toplevel_set_title(r->wayland.xdg_top_level, "Milsko Wayland App");
xdg_toplevel_set_app_id(r->wayland.xdg_top_level, "Milsko Wayland App");
// if (!mFlags.windowResizable) {
// xdg_toplevel_set_min_size(mXDGTopLevel, mWidth, mHeight);
// xdg_toplevel_set_max_size(mXDGTopLevel, mWidth, mHeight);
// }
// Perform the initial commit and wait for the first configure event
wl_surface_commit(r->wayland.surface);
while(wl_display_dispatch(r->wayland.display) != -1 && !r->wayland.configured) {
}
// setup decorations if we can
if(WAYLAND_GET_INTERFACE(r, zxdg_decoration_manager_v1)->context != NULL) {
zxdg_decoration_manager_v1_context_t* dec = WAYLAND_GET_INTERFACE(r, zxdg_decoration_manager_v1)->context;
dec->decoration = zxdg_decoration_manager_v1_get_toplevel_decoration(
dec->manager, r->wayland.xdg_top_level);
// zxdg_toplevel_decoration_v1_add_listener(dec->decoration,
// dec->listener, r);
zxdg_toplevel_decoration_v1_set_mode(
dec->decoration, ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE);
}
wl_surface_commit(r->wayland.surface);
r->common.success = MwTRUE;
return r;
}
static void MwLLDestroyImpl(MwLL handle) {
MwLLDestroyCommon(handle);
free(handle);
}
static void MwLLPolygonImpl(MwLL handle, MwPoint* points, int points_count, MwLLColor color) {
}
static void MwLLLineImpl(MwLL handle, MwPoint* points, MwLLColor color) {
}
static MwLLColor MwLLAllocColorImpl(MwLL handle, int r, int g, int b) {
MwLLColor c = malloc(sizeof(*c));
c->common.red = r;
c->common.blue = b;
c->common.green = g;
return c;
}
static void MwLLColorUpdateImpl(MwLL handle, MwLLColor c, int r, int g, int b) {
c->common.red = r;
c->common.blue = b;
c->common.green = g;
}
static void MwLLGetXYWHImpl(MwLL handle, int* x, int* y, unsigned int* w, unsigned int* h) {
*x = 0;
*y = 0;
*w = 0;
*h = 0;
}
static void MwLLSetXYImpl(MwLL handle, int x, int y) {
}
static void MwLLSetWHImpl(MwLL handle, int w, int h) {
}
static void MwLLFreeColorImpl(MwLLColor color) {
}
static void MwLLSetBackgroundImpl(MwLL handle, MwLLColor color) {
}
static int MwLLPendingImpl(MwLL handle) {
return 0;
}
static void MwLLNextEventImpl(MwLL handle) {
}
static void MwLLSetTitleImpl(MwLL handle, const char* title) {
}
static MwLLPixmap MwLLCreatePixmapImpl(MwLL handle, unsigned char* data, int width, int height) {
MwLLPixmap r = malloc(sizeof(*r));
r->common.width = width;
r->common.height = height;
r->common.raw = data;
return r;
}
static void MwLLPixmapUpdateImpl(MwLLPixmap r) {
}
static void MwLLDestroyPixmapImpl(MwLLPixmap pixmap) {
free(pixmap);
}
static void MwLLDrawPixmapImpl(MwLL handle, MwRect* rect, MwLLPixmap pixmap) {
}
static void MwLLSetIconImpl(MwLL handle, MwLLPixmap pixmap) {
}
static void MwLLForceRenderImpl(MwLL handle) {
}
static void MwLLSetCursorImpl(MwLL handle, MwCursor* image, MwCursor* mask) {
}
static void MwLLDetachImpl(MwLL handle, MwPoint* point) {
}
static void MwLLShowImpl(MwLL handle, int show) {
}
static void MwLLMakePopupImpl(MwLL handle, MwLL parent) {
}
static void MwLLSetSizeHintsImpl(MwLL handle, int minx, int miny, int maxx, int maxy) {
}
static void MwLLMakeBorderlessImpl(MwLL handle, int toggle) {
}
static void MwLLFocusImpl(MwLL handle) {
}
static void MwLLGrabPointerImpl(MwLL handle, int toggle) {
}
static void MwLLSetClipboardImpl(MwLL handle, const char* text) {
}
static char* MwLLGetClipboardImpl(MwLL handle) {
char* r = NULL;
return r;
}
static void MwLLMakeToolWindowImpl(MwLL handle) {
}
static void MwLLGetCursorCoordImpl(MwLL handle, MwPoint* point) {
}
static void MwLLGetScreenSizeImpl(MwLL handle, MwRect* rect) {
}
static void MwLLBeginStateChangeImpl(MwLL handle) {
MwLLShow(handle, 0);
}
static void MwLLEndStateChangeImpl(MwLL handle) {
MwLLShow(handle, 1);
}
static int MwLLWaylandCallInitImpl(void) {
#ifdef __linux__
if(strcmp(getenv("XDG_SESSION_TYPE"), "wayland")) {
return 1;
}
#endif
return 0;
}
#include "call.c"
CALL(Wayland);

View file

@ -1019,7 +1019,6 @@ static void MwLLEndStateChangeImpl(MwLL handle) {
}
static int MwLLX11CallInitImpl(void) {
/* TODO: check properly */
return 0;
}

View file

@ -687,6 +687,9 @@ MwWidget MwGetParent(MwWidget handle) {
typedef int (*call_t)(void);
int MwLibraryInit(void) {
call_t calls[] = {
#ifdef USE_WAYLAND
MwLLWaylandCallInit,
#endif
#ifdef USE_X11
MwLLX11CallInit,
#endif