throw in the towel and switch to cairo lmao

This commit is contained in:
IoIxD 2025-12-01 20:05:03 -07:00
commit be2eb6483c
3 changed files with 38 additions and 61 deletions

View file

@ -15,6 +15,8 @@
#include <wayland-client.h>
#include <xkbcommon/xkbcommon.h>
#include <cairo/cairo.h>
MWDECL int MwLLWaylandCallInit(void);
#ifndef WL_PROTOCOLS_DEFINED
@ -64,8 +66,9 @@ struct _MwLLWayland {
struct wl_shm* shm;
struct wl_registry_listener registry_listener;
cairo_surface_t* cairo_surface;
MwU8* mapped_buffer;
int mapped_buffer_size;
struct {
const char* key;

View file

@ -30,7 +30,7 @@ if (grep(/^gdi$/, @backends)) {
if (grep(/^wayland$/, @backends)) {
add_cflags("-DUSE_WAYLAND");
new_object("src/backend/wayland.c");
add_libs("-lwayland-client -lxkbcommon");
add_libs("-lcairo -lwayland-client -lxkbcommon");
scan_wayland_protocol("stable", "xdg-shell","");
scan_wayland_protocol("stable", "tablet","-v2");

View file

@ -60,6 +60,7 @@ static void buffer_setup(struct _MwLLWayland* wayland) {
struct wl_shm_pool* pool;
struct wl_buffer* buffer;
int x, y;
int stride = wayland->ww * 4;
int fd;
MwU32 size = wayland->ww * wayland->wh * 4;
@ -80,10 +81,15 @@ static void buffer_setup(struct _MwLLWayland* wayland) {
return;
}
lseek(fd, 0, SEEK_SET);
wayland->mapped_buffer = mmap(NULL, size, PROT_WRITE, MAP_SHARED, fd, 0);
memset(wayland->mapped_buffer, 0xFF, size);
wayland->mapped_buffer_size = size;
// wayland->mapped_buffer_size = size;
wayland->cairo_surface = cairo_image_surface_create_for_data(wayland->mapped_buffer,
CAIRO_FORMAT_ARGB32,
wayland->ww,
wayland->wh,
stride);
fsync(fd);
@ -91,7 +97,7 @@ static void buffer_setup(struct _MwLLWayland* wayland) {
printf("failure setting up wl_shm: could not create pool.\n");
}
buffer = wl_shm_pool_create_buffer(pool, 0, wayland->ww, wayland->wh, wayland->ww * 4, WL_SHM_FORMAT_ARGB8888);
buffer = wl_shm_pool_create_buffer(pool, 0, wayland->ww, wayland->wh, stride, WL_SHM_FORMAT_ARGB8888);
if(wayland->configured) {
wl_surface_attach(wayland->surface, buffer, 0, 0);
@ -428,76 +434,41 @@ 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);
// }
// }
cairo_t* cairo = cairo_create(handle->wayland.cairo_surface);
cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE);
cairo_set_line_width(cairo, 1);
cairo_set_source_rgb(cairo, color->common.red / 255., color->common.green / 255., color->common.blue / 255.);
cairo_paint(cairo);
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);
cairo_move_to(cairo, p1.x, p1.y);
cairo_line_to(cairo, p2.x, p2.y);
}
cairo_fill(cairo);
cairo_destroy(cairo);
}
static void MwLLLineImpl(MwLL handle, MwPoint* points, MwLLColor color) {
MwPoint p1 = points[0];
MwPoint p2 = points[1];
int i;
line_draw(handle, p1.x, p1.y, p2.x, p2.y, color);
cairo_t* cairo = cairo_create(handle->wayland.cairo_surface);
cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE);
cairo_set_line_width(cairo, 1);
cairo_set_source_rgb(cairo, color->common.red / 255., color->common.green / 255., color->common.blue / 255.);
cairo_paint(cairo);
cairo_move_to(cairo, p1.x, p1.y);
cairo_line_to(cairo, p2.x, p2.y);
cairo_fill(cairo);
cairo_destroy(cairo);
}
static MwLLColor MwLLAllocColorImpl(MwLL handle, int r, int g, int b) {
@ -520,6 +491,9 @@ static void MwLLFreeColorImpl(MwLLColor color) {
free(color);
}
static void MwLLSetBackgroundImpl(MwLL handle, MwLLColor color) {
}
static int MwLLPendingImpl(MwLL handle) {
enum {
DISPLAY_FD,