Compare commits

...

7 Commits

Author SHA1 Message Date
IoIxD
a384add9e9 wayland: properly do MwLLPending and MwLLNextEvent 2025-12-18 23:50:03 -07:00
IoIxD
6123fd1304 wayland: implement tool windows. add destructors for protocols 2025-12-18 23:32:51 -07:00
IoIxD
b7488b9440 wayland: seperate MwLLPending and MwLLNextEvent 2025-12-18 19:59:40 -07:00
IoIxD
18511dec2f wayland: remove a few unused fields 2025-12-18 18:27:44 -07:00
IoIxD
59ff6f1008 wayland: add icon support 2025-12-18 18:21:57 -07:00
IoIxD
a27a01ce41 wayland: MwLLGetClipboardImpl. 2025-12-18 17:46:23 -07:00
IoIxD
b0f4713ff1 wayland: remove unused cursor_shape 2025-12-18 13:46:22 -07:00
4 changed files with 602 additions and 62 deletions

View File

@@ -0,0 +1,65 @@
#define _MILSKO
#include <Mw/Milsko.h>
MwWidget window, button, text;
void resize(MwWidget handle, void* user_data, void* call_data) {
unsigned int w, h, mh;
(void)user_data;
(void)call_data;
w = MwGetInteger(handle, MwNwidth);
h = MwGetInteger(handle, MwNheight);
MwVaApply(button,
MwNy, 50 + mh,
MwNwidth, w - 50 * 2,
MwNheight, h - 125 - 50 * 3,
NULL);
MwVaApply(text,
MwNy, 200 + mh,
MwNwidth, w - 50 * 2,
MwNheight, h - 125 - 50 * 3,
NULL);
}
void handler(MwWidget handle, void* user_data, void* call_data) {
(void)handle;
(void)user_data;
(void)call_data;
char* clipboard;
clipboard = MwLLGetClipboard(handle->lowlevel);
if(clipboard != NULL) {
MwVaApply(text, MwNtext, clipboard);
MwForceRender(text);
}
resize(window, NULL, NULL);
}
int main() {
MwMenu m, m2;
MwLibraryInit();
window = MwVaCreateWidget(MwWindowClass, "main", NULL, MwDEFAULT, MwDEFAULT, 400, 400,
MwNtitle, "clipboard",
NULL);
button = MwVaCreateWidget(MwButtonClass, "button", window, 50, 50, 300, 125,
MwNtext, "get clipboard contents",
NULL);
text = MwVaCreateWidget(MwLabelClass, "label", window, 50, 200, 300, 125,
MwNtext, "",
NULL);
MwAddUserHandler(window, MwNresizeHandler, resize, NULL);
MwAddUserHandler(button, MwNactivateHandler, handler, NULL);
resize(window, NULL, NULL);
MwLoop(window);
}

View File

@@ -24,6 +24,8 @@ MWDECL int MwLLWaylandCallInit(void);
#include "Wayland/xdg-shell-client-protocol.h"
#include "Wayland/xdg-decoration-client-protocol.h"
#include "Wayland/cursor-shape-client-protocol.h"
#include "Wayland/primary-selection-client-protocol.h"
#include "Wayland/xdg-toplevel-icon-client-protocol.h"
#endif
struct _MwLLWayland;
@@ -34,6 +36,12 @@ typedef struct wayland_protocol {
} wayland_protocol_t;
typedef wayland_protocol_t*(wl_setup_func)(MwU32, struct _MwLLWayland*);
typedef void(wl_destroy_func)(struct _MwLLWayland* wayland, wayland_protocol_t* data);
typedef struct wayland_protocol_callback_table {
wl_setup_func* setup;
wl_destroy_func* destroy;
} wayland_protocol_callback_table_t;
struct _MwLLWaylandTopLevel {
struct xdg_surface* xdg_surface;
@@ -55,10 +63,20 @@ struct _MwLLWaylandSublevel {
struct wl_subcompositor* subcompositor;
MwLL parent;
MwLL topmost_parent; /* The parent at the top of all the other parents. Usually a toplevel. */
struct xdg_surface* parent_xdg_surface;
};
/* Shared set of anything needed for a shm buffer. Used both for surface framebuffers, and cursors. */
struct _MwLLWaylandPopup {
struct xdg_surface* xdg_surface;
struct xdg_popup* xdg_popup;
struct xdg_positioner* xdg_positioner;
struct xdg_surface_listener xdg_surface_listener;
struct xdg_wm_base* xdg_wm_base;
MwLL parent;
};
/* Shared set of anything needed for a shm buffer. */
struct _MwLLWaylandShmBuffer {
struct wl_shm* shm;
struct wl_shm_pool* shm_pool;
@@ -72,6 +90,13 @@ struct _MwLLWaylandShmBuffer {
MwBool setup;
};
enum _MwLLWaylandType {
MWLL_WAYLAND_UNKNOWN = 0,
MWLL_WAYLAND_TOPLEVEL,
MWLL_WAYLAND_SUBLEVEL,
MWLL_WAYLAND_POPUP,
};
struct _MwLLWayland {
struct _MwLLCommon common;
@@ -80,17 +105,17 @@ struct _MwLLWayland {
struct _MwLLWaylandTopLevel* toplevel;
/* Pointer for data that's only loaded if the widget is a sublevel */
struct _MwLLWaylandSublevel* sublevel;
/* Pointer for data that's only loaded if the widget is a popup */
struct _MwLLWaylandPopup* popup;
};
enum {
MWLL_WAYLAND_TOPLEVEL = 0,
MWLL_WAYLAND_SUBLEVEL, /* Sublevels are surfaces that have the toplevel as a parent. Some parts of the code also call them subwidgets. */
} type;
enum _MwLLWaylandType type;
enum _MwLLWaylandType type_to_be;
/* Map of Wayland interfaces to their relevant setup functions. */
struct {
const char* key;
wl_setup_func* value;
const char* key;
wayland_protocol_callback_table_t* value;
}* wl_protocol_setup_map;
/* Map of Wayland interfaces to any information we keep about them once we've registered them. */
@@ -106,14 +131,15 @@ struct _MwLLWayland {
struct wl_region* region;
struct wl_output* output;
struct wl_pointer* pointer;
MwU32 pointer_serial;
struct wl_pointer* pointer;
struct wl_keyboard* keyboard;
MwU32 pointer_serial;
MwBool active; /* Whether or not the surface is the one being hovered over. */
MwBool events_pending;
MwBool test;
MwU32 mod_state;
MwLL* sublevels; /* stb_ds managed array of any sublevels */
MwBool configured; /* Whether or not xdg_toplevel_configure has run once */
MwU32 x, y, ww, wh; /* Window position */
@@ -121,8 +147,11 @@ struct _MwLLWayland {
MwU32 mw, mh; /* Monitor width and height as advertised by wl_output.mode */
struct _MwLLWaylandShmBuffer framebuffer;
struct _MwLLWaylandShmBuffer cursor;
MwLL parent;
struct _MwLLWaylandShmBuffer framebuffer;
struct _MwLLWaylandShmBuffer cursor;
struct _MwLLWaylandShmBuffer* icon;
cairo_surface_t* cs;
cairo_t* cairo;

View File

@@ -34,8 +34,10 @@ if (grep(/^wayland$/, @backends)) {
scan_wayland_protocol("stable", "xdg-shell", "");
scan_wayland_protocol("stable", "tablet", "-v2");
scan_wayland_protocol("staging", "xdg-toplevel-icon", "-v1");
scan_wayland_protocol("staging", "cursor-shape", "-v1");
scan_wayland_protocol("unstable", "xdg-decoration", "-unstable-v1");
scan_wayland_protocol("unstable", "primary-selection", "-unstable-v1");
$gl_libs = "-lEGL -lwayland-egl lGL -lGLU";
}
@@ -112,6 +114,7 @@ new_example("examples/basic/colorpicker");
new_example("examples/basic/combobox");
new_example("examples/basic/treeview");
new_example("examples/basic/box");
new_example("examples/basic/clipboard");
if (param_get("opengl")) {
new_example("examples/gldemos/boing", $gl_libs);

View File

@@ -7,6 +7,15 @@
#include <sys/mman.h>
#include <wayland-util.h>
#include <unistd.h>
/* TODO:
* - MwLLSetClipboardImpl
* - MwLLGrabPointerImpl
* - MwLLMakePopupImpl
* - MwLLShowImpl
* - MwLLDetachImpl
*/
/* TODO: find out what FreeBSD and such wants us to include */
#ifdef __FreeBSD__
@@ -22,6 +31,8 @@ static void framebuffer_setup(struct _MwLLWayland* wayland);
/* Destroy the framebuffer */
static void framebuffer_destroy(struct _MwLLWayland* handle);
static void buffer_destroy(struct _MwLLWaylandShmBuffer* buffer);
static void region_setup(MwLL handle);
static void region_invalidate(MwLL handle);
@@ -34,10 +45,11 @@ static void new_protocol(void* data, struct wl_registry* registry,
MwU32 version) {
struct _MwLLWayland* wayland = data;
wl_setup_func* func = shget(wayland->wl_protocol_setup_map, interface);
if(func != NULL) {
/* printf("registering interface %s\n", interface); */
shput(wayland->wl_protocol_map, interface, func(name, data));
wayland_protocol_callback_table_t* cb = shget(wayland->wl_protocol_setup_map, interface);
if(cb != NULL) {
char* inter = malloc(strlen(interface));
strcpy(inter, interface);
shput(wayland->wl_protocol_map, inter, cb->setup(name, data));
} else {
/* printf("unknown interface %s\n", interface); */
}
@@ -47,9 +59,85 @@ static void new_protocol(void* data, struct wl_registry* registry,
static void protocol_removed(void* data,
struct wl_registry* registry,
MwU32 name) {
printf("%d destroyed\n", name);
};
static void offer_offer(void* data,
struct zwp_primary_selection_offer_v1* zwp_primary_selection_offer_v1,
const char* mime_type) {
};
struct zwp_primary_selection_offer_v1_listener offer_listener = {
.offer = offer_offer,
};
typedef struct primary_selection_device_context {
struct zwp_primary_selection_device_v1* device;
struct zwp_primary_selection_offer_v1* offer;
} primary_selection_device_context_t;
typedef struct primary_selection_context {
struct zwp_primary_selection_device_manager_v1* manager;
/* stored seperately of the listeners because it's not a pointer and it gets assigned to data devices dynamically. */
struct zwp_primary_selection_device_v1_listener listener;
struct zwp_primary_selection_source_v1* source;
primary_selection_device_context_t** devices;
} primary_selection_context_t;
static void primary_selection_data_offer(void* data,
struct zwp_primary_selection_device_v1* zwp_primary_selection_device_v1,
struct zwp_primary_selection_offer_v1* offer) {
struct primary_selection_device_context* self = data;
zwp_primary_selection_offer_v1_add_listener(offer, &offer_listener, data);
self->offer = offer;
};
static void primary_selection_selection(void* data,
struct zwp_primary_selection_device_v1* zwp_primary_selection_device_v1,
struct zwp_primary_selection_offer_v1* id) {
};
/* zwp_primary_selection_device_manager_v1 setup function */
static wayland_protocol_t* zwp_primary_selection_device_manager_v1_setup(MwU32 name, struct _MwLLWayland* wayland) {
wayland_protocol_t* proto = malloc(sizeof(wayland_protocol_t));
primary_selection_context_t* context = malloc(sizeof(primary_selection_context_t));
context->manager = wl_registry_bind(wayland->registry, name, &zwp_primary_selection_device_manager_v1_interface, 1);
struct zwp_primary_selection_device_manager_v1* device;
context->listener.data_offer = primary_selection_data_offer;
context->listener.selection = primary_selection_selection;
context->source = zwp_primary_selection_device_manager_v1_create_source(context->manager);
context->devices = NULL;
proto->context = context;
proto->listener = NULL;
return proto;
}
static void zwp_primary_selection_device_manager_v1_interface_destroy(struct _MwLLWayland* wayland, wayland_protocol_t* data) {
primary_selection_context_t* context = data->context;
int i;
zwp_primary_selection_device_manager_v1_destroy(context->manager);
for(i = 0; i < arrlen(context->devices); i++) {
primary_selection_device_context_t* device = context->devices[i];
// zwp_primary_selection_device_v1_destroy(device->device);
// zwp_primary_selection_offer_v1_destroy(device->offer);
free(device);
}
arrfree(context->devices);
zwp_primary_selection_source_v1_destroy(context->source);
free(data->listener);
}
/* `wl_pointer.enter` callback */
static void pointer_enter(void* data, struct wl_pointer* wl_pointer, MwU32 serial,
struct wl_surface* surface, wl_fixed_t surface_x,
@@ -76,6 +164,7 @@ static void pointer_motion(void* data, struct wl_pointer* wl_pointer, MwU32 time
p.point = self->wayland.cur_mouse_pos;
MwLLDispatch(self, move, &p);
self->wayland.events_pending = MwTRUE;
/*timed_redraw(self, time, 50, &self->wayland.cooldown_timer);*/
};
@@ -108,6 +197,7 @@ static void pointer_button(void* data, struct wl_pointer* wl_pointer, MwU32 seri
}
MwLLDispatch(self, draw, NULL);
self->wayland.events_pending = MwTRUE;
};
/* `wl_pointer.axis` callback */
@@ -159,6 +249,7 @@ static void keyboard_enter(void* data,
struct wl_array* keys) {
MwLL self = data;
MwLLDispatch(self, focus_in, NULL);
self->wayland.events_pending = MwTRUE;
};
/* `wl_keyboard.leave` callback */
@@ -168,6 +259,7 @@ static void keyboard_leave(void* data,
struct wl_surface* surface) {
MwLL self = data;
MwLLDispatch(self, focus_out, NULL);
self->wayland.events_pending = MwTRUE;
};
/* `wl_keyboard.key` callback */
@@ -254,6 +346,7 @@ static void keyboard_key(void* data,
}
}
}
self->wayland.events_pending = MwTRUE;
};
/* `wl_keyboard.modifiers` callback */
@@ -288,13 +381,28 @@ static void wl_seat_capabilities(void* data, struct wl_seat* wl_seat,
MwU32 capabilities) {
MwLL self = data;
if(capabilities & WL_SEAT_CAPABILITY_KEYBOARD) {
struct wl_keyboard* keyboard = wl_seat_get_keyboard(wl_seat);
wl_keyboard_add_listener(keyboard, &keyboard_listener, data);
self->wayland.keyboard = wl_seat_get_keyboard(wl_seat);
wl_keyboard_add_listener(self->wayland.keyboard, &keyboard_listener, data);
}
if(capabilities & WL_SEAT_CAPABILITY_POINTER) {
self->wayland.pointer = wl_seat_get_pointer(wl_seat);
wl_pointer_add_listener(self->wayland.pointer, &pointer_listener, data);
}
/* primary selection support */
if(WAYLAND_GET_INTERFACE(self->wayland, zwp_primary_selection_device_manager_v1) != NULL) {
primary_selection_context_t* primary_selection = WAYLAND_GET_INTERFACE(self->wayland, zwp_primary_selection_device_manager_v1)->context;
primary_selection_device_context_t* device_ctx = malloc(sizeof(primary_selection_device_context_t));
device_ctx->device =
zwp_primary_selection_device_manager_v1_get_device(primary_selection->manager, wl_seat);
zwp_primary_selection_device_v1_add_listener(device_ctx->device, &primary_selection->listener, device_ctx);
device_ctx->offer = NULL;
arrpush(primary_selection->devices, device_ctx);
}
};
static void output_geometry(void* data,
@@ -344,6 +452,11 @@ static wayland_protocol_t* wl_seat_setup(MwU32 name, MwLL ll) {
return proto;
}
static void wl_seat_interface_destroy(struct _MwLLWayland* wayland, wayland_protocol_t* data) {
free(data->listener);
wl_seat_destroy(data->context);
}
/* wl_output setup function */
static wayland_protocol_t* wl_output_setup(MwU32 name, MwLL ll) {
ll->wayland.output = wl_registry_bind(ll->wayland.registry, name, &wl_output_interface, 1);
@@ -352,6 +465,10 @@ static wayland_protocol_t* wl_output_setup(MwU32 name, MwLL ll) {
return NULL;
}
static void wl_output_interface_destroy(struct _MwLLWayland* wayland, wayland_protocol_t* data) {
// wl_output_destroy(wayland->output);
}
/* wl_compositor setup function */
static wayland_protocol_t* wl_compositor_setup(MwU32 name, struct _MwLLWayland* wayland) {
wayland->compositor = wl_registry_bind(wayland->registry, name, &wl_compositor_interface, 1);
@@ -359,6 +476,10 @@ static wayland_protocol_t* wl_compositor_setup(MwU32 name, struct _MwLLWayland*
return NULL;
}
static void wl_compositor_interface_destroy(struct _MwLLWayland* wayland, wayland_protocol_t* data) {
// wl_compositor_destroy(wayland->compositor);
}
/* wl_subcompositor setup function */
static wayland_protocol_t* wl_subcompositor_setup(MwU32 name, struct _MwLLWayland* wayland) {
wayland->sublevel->subcompositor = wl_registry_bind(wayland->registry, name, &wl_subcompositor_interface, 1);
@@ -366,6 +487,10 @@ static wayland_protocol_t* wl_subcompositor_setup(MwU32 name, struct _MwLLWaylan
return NULL;
}
static void wl_subcompositor_interface_destroy(struct _MwLLWayland* wayland, wayland_protocol_t* data) {
wl_subcompositor_destroy(wayland->sublevel->subcompositor);
}
/* xdg_wm_base setup function */
static wayland_protocol_t* xdg_wm_base_setup(MwU32 name, struct _MwLLWayland* wayland) {
wayland_protocol_t* proto = malloc(sizeof(wayland_protocol_t));
@@ -379,14 +504,9 @@ static wayland_protocol_t* xdg_wm_base_setup(MwU32 name, struct _MwLLWayland* wa
return proto;
}
/* wp_cursor_shape_manager setup function */
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;
static void xdg_wm_base_interface_destroy(struct _MwLLWayland* wayland, wayland_protocol_t* data) {
xdg_wm_base_destroy(data->context);
free(data->listener);
}
/* the two decoration manager constructs */
@@ -409,6 +529,13 @@ static wayland_protocol_t* zxdg_decoration_manager_v1_setup(MwU32 name, struct _
return proto;
}
static void zxdg_decoration_manager_v1_interface_destroy(struct _MwLLWayland* wayland, wayland_protocol_t* data) {
zxdg_decoration_manager_v1_context_t* context = data->context;
zxdg_decoration_manager_v1_destroy(context->manager);
zxdg_toplevel_decoration_v1_destroy(context->decoration);
}
/* `xdg_toplevel.close` callback */
static void xdg_toplevel_configure(void* data,
struct xdg_toplevel* xdg_toplevel,
@@ -438,11 +565,6 @@ static void xdg_toplevel_configure(void* data,
MwLLDispatch(self, resize, NULL);
MwLLDispatch(self, draw, NULL);
for(i = 0; i < arrlen(self->wayland.sublevels); i++) {
MwLL handle = self->wayland.sublevels[i];
region_setup(handle);
}
/*if(!self->wayland.egl_setup) {
self->wayland.egl_setup = egl_setup(self, self->wayland.x, self->wayland.y, width, height);
} else {
@@ -489,14 +611,15 @@ static wayland_protocol_t* wl_shm_setup(MwU32 name, struct _MwLLWayland* wayland
return NULL;
}
static void update_framebuffer(struct _MwLLWayland* wayland) {
struct _MwLLWaylandShmBuffer* buffer = &wayland->framebuffer;
static void wl_shm_interface_destroy(struct _MwLLWayland* wayland, wayland_protocol_t* data) {
// buffer_destroy(&wayland->framebuffer);
}
static void update_buffer(struct _MwLLWaylandShmBuffer* buffer) {
fsync(buffer->fd);
if(wayland->configured && buffer->setup) {
wl_surface_attach(wayland->framebuffer.surface, buffer->shm_buffer, wayland->x, wayland->y);
wl_surface_commit(wayland->framebuffer.surface);
}
wl_surface_attach(buffer->surface, buffer->shm_buffer, 0, 0);
wl_surface_commit(buffer->surface);
}
static void buffer_setup(struct _MwLLWaylandShmBuffer* buffer, MwU32 width, MwU32 height) {
@@ -550,7 +673,8 @@ static void framebuffer_setup(struct _MwLLWayland* wayland) {
wayland->cairo = cairo_create(wayland->cs);
memset(wayland->framebuffer.buf, 255, wayland->framebuffer.buf_size);
update_framebuffer(wayland);
update_buffer(&wayland->framebuffer);
wayland->events_pending = MwTRUE;
};
static void framebuffer_destroy(struct _MwLLWayland* wayland) {
buffer_destroy(&wayland->framebuffer);
@@ -573,13 +697,41 @@ static void region_setup(MwLL handle) {
wl_surface_set_opaque_region(handle->wayland.framebuffer.surface, handle->wayland.region);
}
static wayland_protocol_t* xdg_toplevel_icon_manager_v1_setup(MwU32 name, struct _MwLLWayland* wayland) {
wayland_protocol_t* proto = malloc(sizeof(wayland_protocol_t));
proto->context = wl_registry_bind(wayland->registry, name, &xdg_toplevel_icon_manager_v1_interface, 1);
proto->listener = NULL;
return proto;
}
static void xdg_toplevel_icon_manager_v1_interface_destroy(struct _MwLLWayland* wayland, wayland_protocol_t* data) {
free(data->listener);
xdg_toplevel_icon_manager_v1_destroy(data->context);
}
/* Flush Wayland events */
static void wl_flush(MwLL handle) {
struct pollfd pfd;
int rc;
pfd.fd = wl_display_get_fd(handle->wayland.display);
pfd.events = POLLIN;
while(MwTRUE) {
rc = wl_display_flush(handle->wayland.display);
if(errno != EAGAIN || rc != -1) {
break;
}
if(poll(&pfd, 1, -1) == -1) {
break;
}
}
}
/* Standard Wayland event loop. */
static int event_loop(MwLL handle) {
enum {
DISPLAY_FD,
KEYREPEAT_FD,
CURSOR_FD
};
struct pollfd fd;
int timeout = 1;
struct _MwLLWayland* wayland = &handle->wayland;
@@ -635,7 +787,12 @@ static int event_loop(MwLL handle) {
static void setup_callbacks(struct _MwLLWayland* wayland) {
/* Convience macro for adding the interface functions to the setup map */
#define WL_INTERFACE(interface) \
shput(wayland->wl_protocol_setup_map, interface##_interface.name, (wl_setup_func*)interface##_setup);
do { \
wayland_protocol_callback_table_t* cb = malloc(sizeof(wayland_protocol_callback_table_t)); \
cb->setup = (wl_setup_func*)interface##_setup; \
cb->destroy = (wl_destroy_func*)interface##_interface_destroy; \
shput(wayland->wl_protocol_setup_map, interface##_interface.name, cb); \
} while(0);
wayland->registry_listener.global = new_protocol;
wayland->registry_listener.global_remove = protocol_removed;
@@ -646,10 +803,13 @@ static void setup_callbacks(struct _MwLLWayland* wayland) {
WL_INTERFACE(wl_compositor);
WL_INTERFACE(wl_seat);
WL_INTERFACE(wl_output);
WL_INTERFACE(zwp_primary_selection_device_manager_v1);
if(wayland->type == MWLL_WAYLAND_TOPLEVEL) {
WL_INTERFACE(xdg_wm_base);
WL_INTERFACE(zxdg_decoration_manager_v1);
WL_INTERFACE(wp_cursor_shape_manager_v1);
WL_INTERFACE(xdg_toplevel_icon_manager_v1);
} else if(wayland->type == MWLL_WAYLAND_POPUP) {
WL_INTERFACE(xdg_wm_base);
} else {
WL_INTERFACE(wl_subcompositor);
}
@@ -722,10 +882,28 @@ static void setup_toplevel(MwLL r, int x, int y) {
} else {
printf("zxdg null\n");
}
}
/*egl_resetup(r);
MwLLDispatch(r, draw, NULL);
recursive_draw(r);*/
/* Toplevel destroy function */
static void destroy_toplevel(MwLL r) {
int i;
if(shget(r->wayland.wl_protocol_map, zxdg_decoration_manager_v1_interface.name) != NULL) {
zxdg_decoration_manager_v1_context_t* dec = WAYLAND_GET_INTERFACE(r->wayland, zxdg_decoration_manager_v1)->context;
zxdg_decoration_manager_v1_destroy(dec->manager);
}
wl_compositor_destroy(r->wayland.compositor);
xdg_surface_destroy(r->wayland.toplevel->xdg_surface);
xdg_toplevel_destroy(r->wayland.toplevel->xdg_top_level);
xkb_context_unref(r->wayland.toplevel->xkb_context);
wl_registry_destroy(r->wayland.registry);
wl_display_disconnect(r->wayland.display);
}
/* Sublevel setup function */
@@ -733,7 +911,14 @@ static void setup_sublevel(MwLL parent, MwLL r, int x, int y) {
struct wl_compositor* compositor = parent->wayland.compositor;
struct wl_surface* parent_surface = parent->wayland.framebuffer.surface;
r->wayland.sublevel = malloc(sizeof(struct _MwLLWaylandSublevel));
r->wayland.sublevel = malloc(sizeof(struct _MwLLWaylandSublevel));
r->wayland.sublevel->parent = parent;
if(parent->wayland.type == MWLL_WAYLAND_TOPLEVEL) {
r->wayland.sublevel->parent_xdg_surface = parent->wayland.toplevel->xdg_surface;
} else {
r->wayland.sublevel->parent_xdg_surface = parent->wayland.sublevel->parent_xdg_surface;
}
r->wayland.type = MWLL_WAYLAND_SUBLEVEL;
@@ -762,6 +947,112 @@ static void setup_sublevel(MwLL parent, MwLL r, int x, int y) {
r->wayland.configured = MwTRUE;
}
/* Sublevel setup function */
static void destroy_sublevel(MwLL r) {
wl_subsurface_destroy(r->wayland.sublevel->subsurface);
wl_surface_destroy(r->wayland.framebuffer.surface);
free(r->wayland.sublevel);
}
static void popup_configure(void* data,
struct xdg_popup* xdg_popup,
int32_t x,
int32_t y,
int32_t width,
int32_t height) {
MwLL self = data;
self->wayland.x = x;
self->wayland.y = y;
self->wayland.ww = width;
self->wayland.wh = height;
};
static void popup_done(void* data,
struct xdg_popup* xdg_popup) {
};
struct xdg_popup_listener popup_listener = {
.configure = popup_configure,
.popup_done = popup_done,
};
/* Popup setup function */
static void setup_popup(MwLL r) {
int i;
struct xdg_surface* xdg_surface;
MwLL topmost_parent = r->wayland.parent;
while(topmost_parent->wayland.type != MWLL_WAYLAND_TOPLEVEL) {
topmost_parent = topmost_parent->wayland.parent;
}
r->wayland.type = MWLL_WAYLAND_POPUP;
setup_callbacks(&r->wayland);
/* Do a roundtrip to ensure all interfaces are setup. */
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");
raise(SIGTRAP);
return;
}
r->wayland.popup = calloc(sizeof(struct _MwLLWaylandPopup), 0);
// r->wayland.popup->parent = parent;
r->wayland.framebuffer.surface = wl_compositor_create_surface(r->wayland.compositor);
r->wayland.popup->xdg_wm_base = WAYLAND_GET_INTERFACE(r->wayland, xdg_wm_base)->context;
r->wayland.popup->xdg_positioner = xdg_wm_base_create_positioner(r->wayland.popup->xdg_wm_base);
xdg_positioner_set_size(r->wayland.popup->xdg_positioner, r->wayland.ww, r->wayland.wh);
xdg_positioner_set_anchor_rect(
r->wayland.popup->xdg_positioner,
topmost_parent->wayland.cur_mouse_pos.x,
topmost_parent->wayland.cur_mouse_pos.y,
1, 1);
xdg_surface = topmost_parent->wayland.toplevel->xdg_surface;
r->wayland.popup->xdg_surface =
xdg_wm_base_get_xdg_surface(r->wayland.popup->xdg_wm_base, r->wayland.framebuffer.surface);
r->wayland.popup->xdg_popup = xdg_surface_get_popup(
r->wayland.popup->xdg_surface,
xdg_surface,
r->wayland.popup->xdg_positioner);
xdg_popup_add_listener(r->wayland.popup->xdg_popup, &popup_listener, r);
r->wayland.popup->xdg_surface_listener.configure = xdg_surface_configure;
xdg_surface_add_listener(r->wayland.popup->xdg_surface, &r->wayland.popup->xdg_surface_listener, r);
/* Perform the initial commit and wait for the first configure event */
wl_surface_commit(r->wayland.framebuffer.surface);
event_loop(r);
}
/* Popup destroy function */
static void destroy_popup(MwLL r) {
xdg_popup_destroy(r->wayland.popup->xdg_popup);
xdg_surface_destroy(r->wayland.popup->xdg_surface);
xdg_positioner_destroy(r->wayland.popup->xdg_positioner);
wl_surface_destroy(r->wayland.framebuffer.surface);
// free(r->wayland.popup);
wl_registry_destroy(r->wayland.registry);
}
static MwLL MwLLCreateImpl(MwLL parent, int x, int y, int width, int height) {
MwLL r;
r = malloc(sizeof(*r));
@@ -780,12 +1071,11 @@ static MwLL MwLLCreateImpl(MwLL parent, int x, int y, int width, int height) {
y = 0;
}
r->wayland.ww = width;
r->wayland.wh = height;
r->wayland.x = x;
r->wayland.y = y;
r->wayland.sublevels = NULL;
r->wayland.ww = width;
r->wayland.wh = height;
r->wayland.x = x;
r->wayland.y = y;
r->wayland.parent = parent;
if(parent == NULL) {
setup_toplevel(r, x, y);
@@ -804,12 +1094,52 @@ static MwLL MwLLCreateImpl(MwLL parent, int x, int y, int width, int height) {
}
static void MwLLDestroyImpl(MwLL handle) {
int i;
event_loop(handle);
MwLLDestroyCommon(handle);
buffer_destroy(&handle->wayland.framebuffer);
buffer_destroy(&handle->wayland.cursor);
wl_region_destroy(handle->wayland.region);
if(WAYLAND_GET_INTERFACE(handle->wayland, zwp_primary_selection_device_manager_v1) != NULL) {
primary_selection_context_t* primary_selection = WAYLAND_GET_INTERFACE(handle->wayland, zwp_primary_selection_device_manager_v1)->context;
for(i = 0; i < arrlen(primary_selection->devices); i++) {
zwp_primary_selection_offer_v1_destroy(primary_selection->devices[i]->offer);
}
}
if(handle->wayland.icon != NULL) {
buffer_destroy(handle->wayland.icon);
wl_surface_destroy(handle->wayland.icon->surface);
}
if(handle->wayland.type == MWLL_WAYLAND_TOPLEVEL) {
destroy_toplevel(handle);
} else if(handle->wayland.type == MWLL_WAYLAND_SUBLEVEL) {
destroy_sublevel(handle);
} else if(handle->wayland.type == MWLL_WAYLAND_POPUP) {
destroy_popup(handle);
}
for(i = 0; i < shlen(handle->wayland.wl_protocol_setup_map); i++) {
void* ctx = shget(handle->wayland.wl_protocol_map, handle->wayland.wl_protocol_setup_map[i].key);
handle->wayland.wl_protocol_setup_map[i].value->destroy(&handle->wayland, ctx);
}
shfree(handle->wayland.wl_protocol_map);
shfree(handle->wayland.wl_protocol_setup_map);
if(handle->wayland.pointer != NULL) {
wl_pointer_destroy(handle->wayland.pointer);
}
if(handle->wayland.keyboard != NULL) {
wl_keyboard_destroy(handle->wayland.keyboard);
}
free(handle);
}
@@ -848,6 +1178,7 @@ static void MwLLSetWHImpl(MwLL handle, int w, int h) {
}
refresh:
MwLLDispatch(handle, draw, NULL);
region_setup(handle);
framebuffer_destroy(&handle->wayland);
@@ -869,6 +1200,7 @@ static void MwLLPolygonImpl(MwLL handle, MwPoint* points, int points_count, MwLL
}
cairo_close_path(handle->wayland.cairo);
cairo_fill(handle->wayland.cairo);
handle->wayland.events_pending = MwTRUE;
}
static void MwLLLineImpl(MwLL handle, MwPoint* points, MwLLColor color) {
@@ -886,13 +1218,15 @@ static void MwLLLineImpl(MwLL handle, MwPoint* points, MwLLColor color) {
}
cairo_close_path(handle->wayland.cairo);
cairo_stroke(handle->wayland.cairo);
handle->wayland.events_pending = MwTRUE;
}
static void MwLLBeginDrawImpl(MwLL handle) {
}
static void MwLLEndDrawImpl(MwLL handle) {
update_framebuffer(&handle->wayland);
update_buffer(&handle->wayland.framebuffer);
handle->wayland.events_pending = MwTRUE;
}
static MwLLColor MwLLAllocColorImpl(MwLL handle, int r, int g, int b) {
@@ -916,10 +1250,14 @@ static void MwLLFreeColorImpl(MwLLColor color) {
}
static int MwLLPendingImpl(MwLL handle) {
return event_loop(handle);
return handle->wayland.events_pending || wl_display_dispatch(handle->wayland.display);
}
static void MwLLNextEventImpl(MwLL handle) {
event_loop(handle);
if(handle->wayland.events_pending) {
handle->wayland.events_pending = MwFALSE;
}
}
static void MwLLSetTitleImpl(MwLL handle, const char* title) {
@@ -989,6 +1327,41 @@ static void MwLLDrawPixmapImpl(MwLL handle, MwRect* rect, MwLLPixmap pixmap) {
cairo_surface_destroy(cs);
}
static void MwLLSetIconImpl(MwLL handle, MwLLPixmap pixmap) {
if(handle->wayland.type == MWLL_WAYLAND_TOPLEVEL) {
if(WAYLAND_GET_INTERFACE(handle->wayland, xdg_toplevel_icon_manager_v1) != NULL) {
void* map;
struct xdg_toplevel_icon_manager_v1* icon_manager = WAYLAND_GET_INTERFACE(handle->wayland, xdg_toplevel_icon_manager_v1)->context;
struct xdg_toplevel_icon_v1* icon = xdg_toplevel_icon_manager_v1_create_icon(icon_manager);
MwU64 size = pixmap->common.width * pixmap->common.height * 4;
int i = 0;
if(handle->wayland.icon == NULL) {
handle->wayland.icon = calloc(sizeof(struct _MwLLWaylandShmBuffer), 0);
handle->wayland.icon->shm = handle->wayland.framebuffer.shm;
}
if(handle->wayland.icon->setup) {
buffer_destroy(handle->wayland.icon);
wl_surface_destroy(handle->wayland.icon->surface);
}
handle->wayland.icon->surface = wl_compositor_create_surface(handle->wayland.compositor);
buffer_setup(handle->wayland.icon, pixmap->common.width, pixmap->common.height);
for(; i < size; i += 2) {
handle->wayland.icon->buf[i] = pixmap->common.raw[i + 2];
handle->wayland.icon->buf[i + 1] = pixmap->common.raw[i + 1];
handle->wayland.icon->buf[i + 2] = pixmap->common.raw[i + 0];
handle->wayland.icon->buf[i + 3] = 255;
}
update_buffer(handle->wayland.icon);
xdg_toplevel_icon_v1_add_buffer(icon, handle->wayland.icon->shm_buffer, 1);
xdg_toplevel_icon_manager_v1_set_icon(icon_manager, handle->wayland.toplevel->xdg_top_level, icon);
}
}
}
static void MwLLForceRenderImpl(MwLL handle) {
@@ -1052,6 +1425,26 @@ static void MwLLSetCursorImpl(MwLL handle, MwCursor* image, MwCursor* mask) {
}
static void MwLLDetachImpl(MwLL handle, MwPoint* point) {
switch(handle->wayland.type) {
case MWLL_WAYLAND_UNKNOWN:
case MWLL_WAYLAND_TOPLEVEL:
destroy_toplevel(handle);
break;
case MWLL_WAYLAND_SUBLEVEL:
destroy_sublevel(handle);
break;
case MWLL_WAYLAND_POPUP:
return;
}
switch(handle->wayland.type_to_be) {
case MWLL_WAYLAND_POPUP:
setup_popup(handle);
return;
default:
setup_toplevel(handle, point->x, point->y);
break;
}
}
static void MwLLShowImpl(MwLL handle, int show) {
@@ -1085,6 +1478,7 @@ static void MwLLMakeBorderlessImpl(MwLL handle, int toggle) {
}
static void MwLLFocusImpl(MwLL handle) {
printf("[WARNING] MwLLFocus not supported on Wayland\n");
}
static void MwLLGrabPointerImpl(MwLL handle, int toggle) {
@@ -1094,11 +1488,60 @@ static void MwLLSetClipboardImpl(MwLL handle, const char* text) {
}
static char* MwLLGetClipboardImpl(MwLL handle) {
char* r = NULL;
return r;
if(WAYLAND_GET_INTERFACE(handle->wayland, zwp_primary_selection_device_manager_v1) != NULL) {
primary_selection_context_t* primary_selection = WAYLAND_GET_INTERFACE(handle->wayland, zwp_primary_selection_device_manager_v1)->context;
int i, n = 0;
int fds[2];
struct timeval timeout;
fd_set set;
char* clip_buffer = NULL;
if(pipe(fds) != 0) {
return NULL;
}
FD_ZERO(&set);
FD_SET(fds[0], &set);
timeout.tv_sec = 0;
timeout.tv_usec = 100;
for(i = 0; i < arrlen(primary_selection->devices); i++) {
primary_selection_device_context_t* device = primary_selection->devices[i];
size_t size = 0;
char ch;
int rc = 0;
zwp_primary_selection_offer_v1_receive(device->offer, "text/plain", fds[1]);
wl_flush(handle);
fcntl(fds[0], F_SETFL, O_NONBLOCK);
while(MwTRUE) {
rc = select(fds[0] + 1, &set, NULL, NULL, &timeout);
if(rc <= 0) {
break;
} else {
read(fds[0], &ch, 1);
arrpush(clip_buffer, ch);
if(n++ >= 10240) {
break;
}
}
}
return clip_buffer;
}
// zwp_primary_selection_source_v1_send();
// arrpush(primary_selection->devices, device);
}
return NULL;
}
static void MwLLMakeToolWindowImpl(MwLL handle) {
handle->wayland.type_to_be = MWLL_WAYLAND_POPUP;
}
static void MwLLGetCursorCoordImpl(MwLL handle, MwPoint* point) {