WIP surface refactor

This commit is contained in:
Rakarake 2026-01-08 16:48:04 +01:00
parent 502f584fbf
commit 5fcd796e74
2 changed files with 98 additions and 61 deletions

130
wayland.c
View file

@ -41,7 +41,11 @@ static void xdg_toplevel_configure (
state->width = width; state->width = width;
state->height = height; state->height = height;
// resize egl window // resize egl window
wl_egl_window_resize(state->egl_window, width, height, 0, 0); struct surface_list *next = state->surface_list;
while (next != NULL) {
wl_egl_window_resize(next->data.egl_window, width, height, 0, 0);
next = next->next;
}
} }
static void xdg_toplevel_close(void *data, struct xdg_toplevel *xdg_toplevel) { static void xdg_toplevel_close(void *data, struct xdg_toplevel *xdg_toplevel) {
@ -63,11 +67,11 @@ static const struct xdg_toplevel_listener xdg_toplevel_listener = {
}; };
static void zwlr_layer_surface_v1_configure(void *data, struct zwlr_layer_surface_v1 *zwlr_layer_surface_v1, uint32_t serial, uint32_t width, uint32_t height) { static void zwlr_layer_surface_v1_configure(void *data, struct zwlr_layer_surface_v1 *zwlr_layer_surface_v1, uint32_t serial, uint32_t width, uint32_t height) {
struct client_state *state = data; // struct client_state *state = data;
state->width = width; // state->width = width;
state->height = height; // state->height = height;
wl_egl_window_resize(state->egl_window, width, height, 0, 0); // wl_egl_window_resize(state->egl_window, width, height, 0, 0);
zwlr_layer_surface_v1_ack_configure(zwlr_layer_surface_v1, serial); // zwlr_layer_surface_v1_ack_configure(zwlr_layer_surface_v1, serial);
} }
static void zwlr_layer_surface_v1_closed(void *data, struct zwlr_layer_surface_v1 * zwlr_layer_surface_v1) { static void zwlr_layer_surface_v1_closed(void *data, struct zwlr_layer_surface_v1 * zwlr_layer_surface_v1) {
@ -96,7 +100,7 @@ static void registry_handle_global(
state->wl_compositor = wl_registry_bind( state->wl_compositor = wl_registry_bind(
registry, name, &wl_compositor_interface, 4); registry, name, &wl_compositor_interface, 4);
// Set up wl_surface // Set up wl_surface
state->wl_surface = wl_compositor_create_surface(state->wl_compositor); //state->wl_surface = wl_compositor_create_surface(state->wl_compositor);
} }
if ((strcmp(interface, xdg_wm_base_interface.name) == 0) && (state->output_type == OUTPUT_WINDOW)) { if ((strcmp(interface, xdg_wm_base_interface.name) == 0) && (state->output_type == OUTPUT_WINDOW)) {
state->xdg_wm_base = wl_registry_bind( state->xdg_wm_base = wl_registry_bind(
@ -104,20 +108,32 @@ static void registry_handle_global(
xdg_wm_base_add_listener(state->xdg_wm_base, xdg_wm_base_add_listener(state->xdg_wm_base,
&xdg_wm_base_listener, state); &xdg_wm_base_listener, state);
state->xdg_surface = xdg_wm_base_get_xdg_surface( // create window
state->xdg_wm_base, state->wl_surface); struct wl_surface *wl_surface = wl_compositor_create_surface(state->wl_compositor);
xdg_surface_add_listener(state->xdg_surface, &xdg_surface_listener, state); struct xdg_surface *xdg_surface = xdg_wm_base_get_xdg_surface(
state->xdg_toplevel = xdg_surface_get_toplevel(state->xdg_surface); state->xdg_wm_base, wl_surface);
xdg_toplevel_set_title(state->xdg_toplevel, "GLONKERS! 🕴️"); xdg_surface_add_listener(xdg_surface, &xdg_surface_listener, state);
xdg_toplevel_add_listener(state->xdg_toplevel, &xdg_toplevel_listener, state); struct xdg_toplevel *xdg_toplevel = xdg_surface_get_toplevel(xdg_surface);
} xdg_toplevel_set_title(xdg_toplevel, "GLONKERS! 🕴️");
if ((strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) && (state->output_type == OUTPUT_LAYER)) { xdg_toplevel_add_listener(xdg_toplevel, &xdg_toplevel_listener, state);
state->zwlr_layer_shell_v1 = wl_registry_bind(registry, name, &zwlr_layer_shell_v1_interface, 4);
} // save all the pointers
if (strcmp(interface, wl_output_interface.name) == 0) { struct surface surface = {
state->wl_output = wl_registry_bind( .wl_surface = wl_surface,
registry, name, &wl_output_interface, 4); .xdg_surface = xdg_surface,
.xdg_toplevel = xdg_toplevel,
};
state->surface_list = malloc(sizeof(struct surface_list));
state->surface_list->data = surface;
state->surface_list->next = NULL;
} }
//if ((strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) && (state->output_type == OUTPUT_LAYER)) {
// state->zwlr_layer_shell_v1 = wl_registry_bind(registry, name, &zwlr_layer_shell_v1_interface, 4);
//}
//if (strcmp(interface, wl_output_interface.name) == 0) {
// state->wl_output = wl_registry_bind(
// registry, name, &wl_output_interface, 4);
//}
} }
static void registry_handle_global_remove( static void registry_handle_global_remove(
@ -197,25 +213,30 @@ static void egl_init(struct client_state *state) {
EGL_NO_CONTEXT, context_attribs); EGL_NO_CONTEXT, context_attribs);
// EGL window // EGL window
state->egl_window = wl_egl_window_create(state->wl_surface, struct surface_list *next = state->surface_list;
state->width, state->height); while (next != NULL) {
if (state->egl_window == EGL_NO_SURFACE) { next->data.egl_window = wl_egl_window_create(next->data.wl_surface,
fprintf(stderr, "Can't create egl window\n"); state->width, state->height);
exit(1); if (next->data.egl_window == EGL_NO_SURFACE) {
} else { fprintf(stderr, "Can't create egl window\n");
fprintf(stderr, "Created egl window\n"); exit(1);
} } else {
fprintf(stderr, "Created egl window\n");
}
state->egl_surface = next->data.egl_surface =
eglCreateWindowSurface(state->egl_display, eglCreateWindowSurface(state->egl_display,
state->egl_config, state->egl_config,
(unsigned long) state->egl_window, NULL); (unsigned long) next->data.egl_window, NULL);
if (eglMakeCurrent(state->egl_display, state->egl_surface, if (eglMakeCurrent(state->egl_display, next->data.egl_surface,
state->egl_surface, state->egl_context)) { next->data.egl_surface, state->egl_context)) {
fprintf(stderr, "Made current\n"); fprintf(stderr, "Made current\n");
} else { } else {
fprintf(stderr, "Made current failed\n"); fprintf(stderr, "Made current failed\n");
}
next = next->next;
} }
} }
@ -228,22 +249,26 @@ void wayland_init(struct client_state *state, int output_type) {
state->output_type = output_type; state->output_type = output_type;
state->wl_display = wl_display_connect(NULL); state->wl_display = wl_display_connect(NULL);
state->wl_registry = wl_display_get_registry(state->wl_display); state->wl_registry = wl_display_get_registry(state->wl_display);
state->wl_output = NULL; state->surface_list = NULL;
wl_registry_add_listener(state->wl_registry, &wl_registry_listener, state); wl_registry_add_listener(state->wl_registry, &wl_registry_listener, state);
wl_display_roundtrip(state->wl_display); wl_display_roundtrip(state->wl_display);
if (output_type == OUTPUT_LAYER) { //if (output_type == OUTPUT_LAYER) {
// wlr_layer_shell // // wlr_layer_shell
int layer = ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND; // int layer = ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND;
state->zwlr_layer_surface_v1 = zwlr_layer_shell_v1_get_layer_surface(state->zwlr_layer_shell_v1, state->wl_surface, state->wl_output, layer, "wallpaper"); // state->zwlr_layer_surface_v1 = zwlr_layer_shell_v1_get_layer_surface(state->zwlr_layer_shell_v1, state->wl_surface, state->wl_output, layer, "wallpaper");
zwlr_layer_surface_v1_set_anchor(state->zwlr_layer_surface_v1, // zwlr_layer_surface_v1_set_anchor(state->zwlr_layer_surface_v1,
ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM | ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP // ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM | ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP
| ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT); // | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT);
zwlr_layer_surface_v1_set_size(state->zwlr_layer_surface_v1, 0, 0); // zwlr_layer_surface_v1_set_size(state->zwlr_layer_surface_v1, 0, 0);
zwlr_layer_surface_v1_add_listener(state->zwlr_layer_surface_v1, &zwlr_layer_surface_v1_listener, state); // zwlr_layer_surface_v1_add_listener(state->zwlr_layer_surface_v1, &zwlr_layer_surface_v1_listener, state);
} //}
wl_surface_commit(state->wl_surface); struct surface_list *next = state->surface_list;
while (next != NULL) {
wl_surface_commit(next->data.wl_surface);
next = next->next;
}
egl_init(state); egl_init(state);
} }
@ -251,6 +276,11 @@ void wayland_init(struct client_state *state, int output_type) {
/// Swaps front/backbuffers and dispatches pending wayland commands. /// Swaps front/backbuffers and dispatches pending wayland commands.
void commit(struct client_state *state) { void commit(struct client_state *state) {
wl_display_dispatch(state->wl_display); wl_display_dispatch(state->wl_display);
eglSwapBuffers(state->egl_display, state->egl_surface); struct surface_list *next = state->surface_list;
while (next != NULL) {
// TODO maybe need to set current egl window or whatever
eglSwapBuffers(state->egl_display, next->data.egl_surface);
next = next->next;
}
} }

View file

@ -8,6 +8,22 @@
/// A desktop layer (wallpaper) /// A desktop layer (wallpaper)
#define OUTPUT_LAYER 1 #define OUTPUT_LAYER 1
struct surface {
struct wl_surface *wl_surface;
struct xdg_surface *xdg_surface;
struct xdg_toplevel *xdg_toplevel;
struct wl_egl_window *egl_window;
struct zwlr_layer_surface_v1 *zwlr_layer_surface_v1;
struct wl_output *wl_output;
EGLSurface egl_surface;
};
struct surface_list {
struct surface data;
struct surface_list *next;
};
struct client_state { struct client_state {
int width, height; int width, height;
int running; int running;
@ -17,21 +33,12 @@ struct client_state {
struct wl_registry *wl_registry; struct wl_registry *wl_registry;
struct wl_compositor *wl_compositor; struct wl_compositor *wl_compositor;
struct xdg_wm_base *xdg_wm_base; struct xdg_wm_base *xdg_wm_base;
/* Objects */
struct wl_surface *wl_surface;
struct xdg_surface *xdg_surface;
struct xdg_toplevel *xdg_toplevel;
struct wl_egl_window *egl_window;
// wlr_layer_shell
struct zwlr_layer_shell_v1 *zwlr_layer_shell_v1; struct zwlr_layer_shell_v1 *zwlr_layer_shell_v1;
struct zwlr_layer_surface_v1 *zwlr_layer_surface_v1;
struct wl_output *wl_output; struct surface_list *surface_list;
EGLDisplay egl_display; EGLDisplay egl_display;
EGLConfig egl_config; EGLConfig egl_config;
EGLSurface egl_surface;
EGLContext egl_context; EGLContext egl_context;
}; };