more wayland work

This commit is contained in:
IoIxD 2025-12-01 19:12:49 -07:00
commit 37fe636c75
10 changed files with 152 additions and 78 deletions

View file

@ -56,13 +56,13 @@ void resize(MwWidget w, void* user, void* client) {
int main() {
MwWidget window;
int i;
int i;
MwLibraryInit();
window = MwVaCreateWidget(MwWindowClass, "window", NULL, MwDEFAULT, MwDEFAULT, (ww = 500), (wh = 500),
MwNtitle, "rotate",
NULL);
MwNtitle, "rotate",
NULL);
for(i = 0; i < (int)(sizeof(buttons) / sizeof(buttons[0])); i++) {
const char* color = "";

View file

@ -198,8 +198,6 @@ MWDECL void (*MwLLMakePopup)(MwLL handle, MwLL parent);
MWDECL void (*MwLLBeginStateChange)(MwLL handle);
MWDECL void (*MwLLEndStateChange)(MwLL handle);
MWDECL void (*MwLLSetBackground)(MwLL handle, MwLLColor color);
MWDECL void (*MwLLFocus)(MwLL handle);
MWDECL void (*MwLLGrabPointer)(MwLL handle, int toggle);

View file

@ -7,6 +7,7 @@
#ifndef __MW_LOWLEVEL_WAYLAND_H__
#define __MW_LOWLEVEL_WAYLAND_H__
#include "Mw/BaseTypes.h"
#include <Mw/MachDep.h>
#include <Mw/TypeDefs.h>
#include <Mw/LowLevel.h>
@ -63,6 +64,9 @@ struct _MwLLWayland {
struct wl_shm* shm;
struct wl_registry_listener registry_listener;
MwU8* mapped_buffer;
int mapped_buffer_size;
struct {
const char* key;
wl_setup_func* value;
@ -74,6 +78,7 @@ struct _MwLLWayland {
}* wl_protocol_map;
MwBool configured;
MwBool force_redraw;
MwU32 x, y, ww, wh;
};

View file

@ -44,8 +44,6 @@
\
MwLLBeginStateChange = MwLLBeginStateChangeImpl; \
MwLLEndStateChange = MwLLEndStateChangeImpl; \
\
MwLLSetBackground = MwLLSetBackgroundImpl; \
\
MwLLFocus = MwLLFocusImpl; \
MwLLGrabPointer = MwLLGrabPointerImpl; \

View file

@ -342,11 +342,6 @@ static void MwLLFreeColorImpl(MwLLColor color) {
free(color);
}
static void MwLLSetBackgroundImpl(MwLL handle, MwLLColor color) {
(void)handle;
(void)color;
}
static void MwLLGetXYWHImpl(MwLL handle, int* x, int* y, unsigned int* w, unsigned int* h) {
RECT rc;

View file

@ -13,6 +13,8 @@
#include <wayland-client-protocol.h>
#include "../../external/stb_ds.h"
#include "Mw/BaseTypes.h"
#include "Mw/LowLevel.h"
#include <sys/mman.h>
@ -62,7 +64,6 @@ static void buffer_setup(struct _MwLLWayland* wayland) {
int fd;
MwU32 size = wayland->ww * wayland->wh * 4;
char temp_name[] = "/tmp/milsko-wl-shm-XXXXXX";
void* map;
fd = mkstemp(temp_name);
@ -80,8 +81,9 @@ static void buffer_setup(struct _MwLLWayland* wayland) {
}
lseek(fd, 0, SEEK_SET);
map = mmap(NULL, size, PROT_WRITE, MAP_SHARED, fd, 0);
memset(map, 0xFF, size);
wayland->mapped_buffer = mmap(NULL, size, PROT_WRITE, MAP_SHARED, fd, 0);
memset(wayland->mapped_buffer, 0xFF, size);
wayland->mapped_buffer_size = size;
fsync(fd);
@ -176,10 +178,11 @@ static void xdg_toplevel_configure(void* data,
self->wayland.wh = height;
xdg_surface_set_window_geometry(self->wayland.toplevel->xdg_surface, self->wayland.x, self->wayland.y, self->wayland.ww, self->wayland.wh);
MwLLSetWH(self, width, height);
MwLLDispatch(self, resize, NULL);
};
wl_surface_commit(self->wayland.surface);
MwLLForceRender(self);
return;
};
@ -253,48 +256,6 @@ static MwBool flush_display(struct wl_display* display) {
return MwTRUE;
}
static void event_loop(MwLL handle) {
enum {
DISPLAY_FD,
KEYREPEAT_FD,
CURSOR_FD,
LIBDECOR_FD
};
if(handle->wayland.display == NULL) {
return;
}
struct pollfd fd = {wl_display_get_fd(handle->wayland.display), POLLIN};
int timeout = 500;
while(wl_display_prepare_read(handle->wayland.display) != 0) {
if(wl_display_dispatch_pending(handle->wayland.display) > 0) {
return;
}
}
// If an error other than EAGAIN happens, we have likely been disconnected
// from the Wayland session
if(!flush_display(handle->wayland.display)) {
wl_display_cancel_read(handle->wayland.display);
handle->wayland.toplevel->running = MwFALSE;
return;
}
if(!poll(&fd, 1, timeout)) {
wl_display_cancel_read(handle->wayland.display);
return;
}
if(fd.revents & POLLIN) {
wl_display_read_events(handle->wayland.display);
if(wl_display_dispatch_pending(handle->wayland.display) > 0) {
}
} else {
wl_display_cancel_read(handle->wayland.display);
}
}
static void setup_toplevel(MwLL r, int x, int y, int width, int height) {
r->wayland.type = MWLL_WAYLAND_TOPLEVEL;
r->wayland.toplevel = malloc(sizeof(struct _MwLLWaylandTopLevel));
@ -412,8 +373,8 @@ static MwLL MwLLCreateImpl(MwLL parent, int x, int y, int width, int height) {
r = malloc(sizeof(*r));
MwLLCreateCommon(r);
r->common.copy_buffer = 1;
r->common.type = MwLLBackendWayland;
// r->common.copy_buffer = 1;
r->common.type = MwLLBackendWayland;
if(width == 0) width = parent->wayland.ww;
if(height == 0) height = parent->wayland.wh;
@ -467,10 +428,76 @@ static void MwLLSetWHImpl(MwLL handle, int w, int h) {
}
}
// fractional part of x
static float fpart(float x) {
return x - floor(x);
}
static float rfpart(float x) {
return x - fpart(x);
}
#define IMAGE_POS_GET(x, y, o) handle->wayland.mapped_buffer[(((y * handle->wayland.ww) + x) * 4) + o]
#define IMAGE_POS_SET(x, y, o, v) IMAGE_POS_GET(x, y, o) = v;
#define IMAGE_PLOT(x, y, color) \
IMAGE_POS_SET(x, y, 0, color->common.blue); \
IMAGE_POS_SET(x, y, 1, color->common.green); \
IMAGE_POS_SET(x, y, 2, color->common.red); \
IMAGE_POS_SET(x, y, 3, 255);
static void
line_draw(MwLL handle, int x0, int y0, int x1, int y1, MwLLColor color) {
int dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
int dy = -abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
int err = dx + dy, e2;
for(;;) {
IMAGE_PLOT(x0, y0, color);
if(x0 == x1 && y0 == y1) break;
e2 = 2 * err;
if(e2 >= dy) {
err += dy;
x0 += sx;
}
if(e2 <= dx) {
err += dx;
y0 += sy;
}
}
}
static void MwLLPolygonImpl(MwLL handle, MwPoint* points, int points_count, MwLLColor color) {
int i;
int lx = 65536, ly = 65536;
int hx = 0, hy = 0;
int x, y;
for(i = 0; i < points_count - 1; i++) {
MwPoint p1 = points[i];
MwPoint p2 = points[i + 1];
if(p1.x < lx) lx = p1.x;
if(p1.y < ly) ly = p1.y;
if(p1.x > hx) hx = p1.x;
if(p1.y > hy) hy = p1.y;
}
// for(y = ly; y < hy; y++) {
// for(x = lx; x < hx; x++) {
// IMAGE_PLOT(x, y, color);
// }
// }
for(i = 0; i < points_count - 1; i++) {
MwPoint p1 = points[i];
MwPoint p2 = points[i + 1];
line_draw(handle, p1.x, p1.y, p2.x, p2.y, color);
}
}
static void MwLLLineImpl(MwLL handle, MwPoint* points, MwLLColor color) {
MwPoint p1 = points[0];
MwPoint p2 = points[1];
line_draw(handle, p1.x, p1.y, p2.x, p2.y, color);
}
static MwLLColor MwLLAllocColorImpl(MwLL handle, int r, int g, int b) {
@ -493,16 +520,73 @@ static void MwLLFreeColorImpl(MwLLColor color) {
free(color);
}
static void MwLLSetBackgroundImpl(MwLL handle, MwLLColor color) {
}
static int MwLLPendingImpl(MwLL handle) {
event_loop(handle);
return 0;
enum {
DISPLAY_FD,
KEYREPEAT_FD,
CURSOR_FD
};
struct pollfd fd;
int timeout = 1;
if(handle->wayland.force_redraw) {
return 1;
}
if(handle->wayland.display == NULL) {
return 0;
}
fd.fd = wl_display_get_fd(handle->wayland.display);
fd.events = POLLIN;
while(wl_display_prepare_read(handle->wayland.display) != 0) {
if(wl_display_dispatch_pending(handle->wayland.display) > 0) {
return 0;
}
}
// If an error other than EAGAIN happens, we have likely been disconnected
// from the Wayland session
if(!flush_display(handle->wayland.display)) {
wl_display_cancel_read(handle->wayland.display);
handle->wayland.toplevel->running = MwFALSE;
return 0;
}
// Condition where no events are being sent.
if(!poll(&fd, 1, timeout)) {
wl_display_cancel_read(handle->wayland.display);
// In this case, we need to commit the surface for any animations, etc.
wl_surface_commit(handle->wayland.surface);
return 0;
}
if(fd.revents & POLLIN) {
wl_display_read_events(handle->wayland.display);
if(wl_display_dispatch_pending(handle->wayland.display) > 0) {
}
} else {
wl_display_cancel_read(handle->wayland.display);
}
return 1;
}
static void MwLLNextEventImpl(MwLL handle) {
event_loop(handle);
MwBool render;
render = handle->wayland.force_redraw;
if(render) {
int x, y;
unsigned int w, h;
MwLLGetXYWH(handle, &x, &y, &w, &h);
MwLLDispatch(handle, draw, NULL);
handle->wayland.force_redraw = MwFALSE;
}
}
static void MwLLSetTitleImpl(MwLL handle, const char* title) {
@ -537,6 +621,7 @@ static void MwLLForceRenderImpl(MwLL handle) {
// event_loop(handle);
wl_surface_damage(handle->wayland.surface, 0, 0, handle->wayland.ww, handle->wayland.wh);
wl_surface_commit(handle->wayland.surface);
handle->wayland.force_redraw = MwTRUE;
}
static void MwLLSetCursorImpl(MwLL handle, MwCursor* image, MwCursor* mask) {

View file

@ -395,10 +395,6 @@ static void MwLLFreeColorImpl(MwLLColor color) {
free(color);
}
static void MwLLSetBackgroundImpl(MwLL handle, MwLLColor color) {
XSetWindowBackground(handle->x11.display, handle->x11.window, color->x11.pixel);
}
static int MwLLPendingImpl(MwLL handle) {
XEvent ev;
if(XCheckTypedWindowEvent(handle->x11.display, handle->x11.window, ClientMessage, &ev) || XCheckWindowEvent(handle->x11.display, handle->x11.window, mask, &ev)) {

View file

@ -295,7 +295,9 @@ int MwStep(MwWidget handle) {
arrfree(widgets);
handle->prop_event = 0;
if(handle->lowlevel != NULL && MwLLPending(handle->lowlevel)) MwLLNextEvent(handle->lowlevel);
if(handle->lowlevel != NULL && MwLLPending(handle->lowlevel))
MwLLNextEvent(handle->lowlevel);
handle->prop_event = 1;
clean_destroy_queue(handle);
@ -378,11 +380,7 @@ void MwSetText(MwWidget handle, const char* key, const char* value) {
shput(handle->text, key, v);
}
if(handle->prop_event) MwDispatch3(handle, prop_change, key);
if(strcmp(key, MwNbackground) == 0) {
MwLLColor c = MwParseColor(handle, value);
MwLLSetBackground(handle->lowlevel, c);
MwLLFreeColor(c);
}
if(strcmp(key, MwNbackground) == 0 || strcmp(key, MwNforeground) == 0 || strcmp(key, MwNsubBackground) == 0 || strcmp(key, MwNsubForeground) == 0) {
MwForceRender(handle);
}

View file

@ -1,5 +1,6 @@
/* $Id$ */
#include <Mw/Milsko.h>
#include <stdio.h>
#ifdef USE_STB_IMAGE
#include "../external/stb_image.h"

View file

@ -40,8 +40,6 @@ void (*MwLLMakePopup)(MwLL handle, MwLL parent);
void (*MwLLBeginStateChange)(MwLL handle);
void (*MwLLEndStateChange)(MwLL handle);
void (*MwLLSetBackground)(MwLL handle, MwLLColor color);
void (*MwLLFocus)(MwLL handle);
void (*MwLLGrabPointer)(MwLL handle, int toggle);