nice refactor

This commit is contained in:
Rakarake 2026-02-22 17:31:31 +01:00
parent 795fd34111
commit de525136e7

167
wayland.c
View file

@ -319,6 +319,87 @@ const struct wl_pointer_listener wl_pointer_listener = {
.axis = pointer_axis_handler .axis = pointer_axis_handler
}; };
/// Creates a toplevel surface, and adds it as the only surface in the surface
/// list (TODO this is bad, make it generic - add it to the list instead).
void xdg_tolevel_new(struct client_state *state) {
// create window
struct wl_surface *wl_surface = wl_compositor_create_surface(state->wl_compositor);
struct xdg_surface *xdg_surface = xdg_wm_base_get_xdg_surface(
state->xdg_wm_base, wl_surface);
xdg_surface_add_listener(xdg_surface, &xdg_surface_listener, state);
struct xdg_toplevel *xdg_toplevel = xdg_surface_get_toplevel(xdg_surface);
xdg_toplevel_set_title(xdg_toplevel, "GLONKERS! 🕴️");
xdg_toplevel_add_listener(xdg_toplevel, &xdg_toplevel_listener, state);
// save all the pointers
struct surface surface = {
.wl_surface = wl_surface,
.xdg_surface = xdg_surface,
.xdg_toplevel = xdg_toplevel,
.dirty = false,
.width = DEFAULT_WIDTH,
.height = DEFAULT_HEIGHT,
};
state->surface_list = malloc(sizeof(struct surface_list));
state->surface_list->data = surface;
state->surface_list->next = NULL;
// frame callback
struct wl_callback *cb = wl_surface_frame(wl_surface);
wl_callback_add_listener(cb, &wl_surface_frame_listener, &state->surface_list->data);
egl_init_surface(state, &state->surface_list->data);
wl_surface_commit(wl_surface);
}
void layer_new(struct client_state *state, struct wl_output *wl_output) {
struct wl_surface *wl_surface = wl_compositor_create_surface(state->wl_compositor);
// wlr_layer_shell
int layer = ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND;
struct zwlr_layer_surface_v1 *zwlr_layer_surface_v1 =
zwlr_layer_shell_v1_get_layer_surface(
state->zwlr_layer_shell_v1,
wl_surface,
wl_output,
layer,
"wallpaper"
);
zwlr_layer_surface_v1_set_anchor(zwlr_layer_surface_v1,
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_set_size(zwlr_layer_surface_v1, 0, 0);
zwlr_layer_surface_v1_add_listener(zwlr_layer_surface_v1, &zwlr_layer_surface_v1_listener, state);
struct surface surface = {
.wl_surface = wl_surface,
.zwlr_layer_surface_v1 = zwlr_layer_surface_v1,
.dirty = false,
.width = DEFAULT_WIDTH,
.height = DEFAULT_HEIGHT,
};
// persistent pointer for the callback
struct surface *surface_location;
if (state->surface_list == NULL) {
state->surface_list = malloc(sizeof(struct surface_list));
state->surface_list->data = surface;
state->surface_list->next = NULL;
surface_location = &state->surface_list->data;
} else {
struct surface_list *next = state->surface_list;
while (next->next != NULL) { next = next->next; }
next->next = malloc(sizeof(struct surface_list));
next->next->data = surface;
next->next->next = NULL;
surface_location = &next->next->data;
}
// frame callback
struct wl_callback *cb = wl_surface_frame(wl_surface);
wl_callback_add_listener(cb, &wl_surface_frame_listener, surface_location);
egl_init_surface(state, surface_location);
wl_surface_commit(wl_surface);
}
static void registry_handle_global( static void registry_handle_global(
void *data, void *data,
struct wl_registry *registry, struct wl_registry *registry,
@ -364,33 +445,6 @@ static void registry_handle_global(
registry, name, &xdg_wm_base_interface, 1); registry, name, &xdg_wm_base_interface, 1);
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);
// create window
struct wl_surface *wl_surface = wl_compositor_create_surface(state->wl_compositor);
struct xdg_surface *xdg_surface = xdg_wm_base_get_xdg_surface(
state->xdg_wm_base, wl_surface);
xdg_surface_add_listener(xdg_surface, &xdg_surface_listener, state);
struct xdg_toplevel *xdg_toplevel = xdg_surface_get_toplevel(xdg_surface);
xdg_toplevel_set_title(xdg_toplevel, "GLONKERS! 🕴️");
xdg_toplevel_add_listener(xdg_toplevel, &xdg_toplevel_listener, state);
// save all the pointers
struct surface surface = {
.wl_surface = wl_surface,
.xdg_surface = xdg_surface,
.xdg_toplevel = xdg_toplevel,
.dirty = false,
.width = DEFAULT_WIDTH,
.height = DEFAULT_HEIGHT,
};
state->surface_list = malloc(sizeof(struct surface_list));
state->surface_list->data = surface;
state->surface_list->next = NULL;
// frame callback
struct wl_callback *cb = wl_surface_frame(wl_surface);
wl_callback_add_listener(cb, &wl_surface_frame_listener, &state->surface_list->data);
egl_init_surface(state, &state->surface_list->data);
} }
if ((strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) && (state->output_type == OUTPUT_LAYER)) { 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); state->zwlr_layer_shell_v1 = wl_registry_bind(registry, name, &zwlr_layer_shell_v1_interface, 4);
@ -398,52 +452,7 @@ static void registry_handle_global(
if (strcmp(interface, wl_output_interface.name) == 0 && state->output_type == OUTPUT_LAYER) { if (strcmp(interface, wl_output_interface.name) == 0 && state->output_type == OUTPUT_LAYER) {
struct wl_output *wl_output = wl_registry_bind( struct wl_output *wl_output = wl_registry_bind(
registry, name, &wl_output_interface, 4); registry, name, &wl_output_interface, 4);
layer_new(state, wl_output);
struct wl_surface *wl_surface = wl_compositor_create_surface(state->wl_compositor);
// wlr_layer_shell
int layer = ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND;
struct zwlr_layer_surface_v1 *zwlr_layer_surface_v1 =
zwlr_layer_shell_v1_get_layer_surface(
state->zwlr_layer_shell_v1,
wl_surface,
wl_output,
layer,
"wallpaper"
);
zwlr_layer_surface_v1_set_anchor(zwlr_layer_surface_v1,
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_set_size(zwlr_layer_surface_v1, 0, 0);
zwlr_layer_surface_v1_add_listener(zwlr_layer_surface_v1, &zwlr_layer_surface_v1_listener, state);
struct surface surface = {
.wl_surface = wl_surface,
.zwlr_layer_surface_v1 = zwlr_layer_surface_v1,
.dirty = false,
.width = DEFAULT_WIDTH,
.height = DEFAULT_HEIGHT,
};
// persistent pointer for the callback
struct surface *surface_location;
if (state->surface_list == NULL) {
state->surface_list = malloc(sizeof(struct surface_list));
state->surface_list->data = surface;
state->surface_list->next = NULL;
surface_location = &state->surface_list->data;
} else {
struct surface_list *next = state->surface_list;
while (next->next != NULL) { next = next->next; }
next->next = malloc(sizeof(struct surface_list));
next->next->data = surface;
next->next->next = NULL;
surface_location = &next->next->data;
}
// frame callback
struct wl_callback *cb = wl_surface_frame(wl_surface);
wl_callback_add_listener(cb, &wl_surface_frame_listener, surface_location);
egl_init_surface(state, surface_location);
} }
} }
@ -475,10 +484,14 @@ struct client_state* wayland_init(int output_type) {
wl_seat_get_pointer(state->wl_seat); wl_seat_get_pointer(state->wl_seat);
struct surface_list *next = state->surface_list; //struct surface_list *next = state->surface_list;
while (next != NULL) { //while (next != NULL) {
wl_surface_commit(next->data.wl_surface); // wl_surface_commit(next->data.wl_surface);
next = next->next; // next = next->next;
//}
if (state->output_type == OUTPUT_WINDOW) {
xdg_tolevel_new(state);
} }
return state; return state;