nice refactor

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

125
wayland.c
View file

@ -319,52 +319,9 @@ const struct wl_pointer_listener wl_pointer_listener = {
.axis = pointer_axis_handler .axis = pointer_axis_handler
}; };
static void registry_handle_global( /// Creates a toplevel surface, and adds it as the only surface in the surface
void *data, /// list (TODO this is bad, make it generic - add it to the list instead).
struct wl_registry *registry, void xdg_tolevel_new(struct client_state *state) {
uint32_t name,
const char *interface,
uint32_t version
) {
printf("interface: '%s', version: %d, name: %d\n",
interface, version, name);
struct client_state *state = data;
if (strcmp(interface, wl_compositor_interface.name) == 0) {
state->wl_compositor = wl_registry_bind(
registry, name, &wl_compositor_interface, 4);
// Set up wl_surface
//state->wl_surface = wl_compositor_create_surface(state->wl_compositor);
}
if (strcmp(interface, wl_shm_interface.name) == 0) {
state->wl_shm = wl_registry_bind(registry, name,
&wl_shm_interface, 1);
}
if (strcmp(interface, wl_seat_interface.name) == 0) {
state->wl_seat = wl_registry_bind(registry, name,
&wl_seat_interface, 1);
state->wl_pointer = wl_seat_get_pointer(state->wl_seat);
wl_pointer_add_listener(state->wl_pointer, &wl_pointer_listener, state);
struct wl_cursor_theme *cursor_theme =
wl_cursor_theme_load(NULL, 24, state->wl_shm);
struct wl_cursor *cursor =
wl_cursor_theme_get_cursor(cursor_theme, "left_ptr");
state->wl_cursor_image = cursor->images[0];
struct wl_buffer *cursor_buffer =
wl_cursor_image_get_buffer(state->wl_cursor_image);
state->cursor_surface = wl_compositor_create_surface(state->wl_compositor);
wl_surface_attach(state->cursor_surface, cursor_buffer, 0, 0);
wl_surface_commit(state->cursor_surface);
}
if ((strcmp(interface, xdg_wm_base_interface.name) == 0) && (state->output_type == OUTPUT_WINDOW)) {
state->xdg_wm_base = wl_registry_bind(
registry, name, &xdg_wm_base_interface, 1);
xdg_wm_base_add_listener(state->xdg_wm_base,
&xdg_wm_base_listener, state);
// create window // create window
struct wl_surface *wl_surface = wl_compositor_create_surface(state->wl_compositor); struct wl_surface *wl_surface = wl_compositor_create_surface(state->wl_compositor);
struct xdg_surface *xdg_surface = xdg_wm_base_get_xdg_surface( struct xdg_surface *xdg_surface = xdg_wm_base_get_xdg_surface(
@ -391,14 +348,10 @@ static void registry_handle_global(
struct wl_callback *cb = wl_surface_frame(wl_surface); struct wl_callback *cb = wl_surface_frame(wl_surface);
wl_callback_add_listener(cb, &wl_surface_frame_listener, &state->surface_list->data); wl_callback_add_listener(cb, &wl_surface_frame_listener, &state->surface_list->data);
egl_init_surface(state, &state->surface_list->data); egl_init_surface(state, &state->surface_list->data);
wl_surface_commit(wl_surface);
} }
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->output_type == OUTPUT_LAYER) {
struct wl_output *wl_output = wl_registry_bind(
registry, name, &wl_output_interface, 4);
void layer_new(struct client_state *state, struct wl_output *wl_output) {
struct wl_surface *wl_surface = wl_compositor_create_surface(state->wl_compositor); struct wl_surface *wl_surface = wl_compositor_create_surface(state->wl_compositor);
// wlr_layer_shell // wlr_layer_shell
@ -444,6 +397,62 @@ static void registry_handle_global(
struct wl_callback *cb = wl_surface_frame(wl_surface); struct wl_callback *cb = wl_surface_frame(wl_surface);
wl_callback_add_listener(cb, &wl_surface_frame_listener, surface_location); wl_callback_add_listener(cb, &wl_surface_frame_listener, surface_location);
egl_init_surface(state, surface_location); egl_init_surface(state, surface_location);
wl_surface_commit(wl_surface);
}
static void registry_handle_global(
void *data,
struct wl_registry *registry,
uint32_t name,
const char *interface,
uint32_t version
) {
printf("interface: '%s', version: %d, name: %d\n",
interface, version, name);
struct client_state *state = data;
if (strcmp(interface, wl_compositor_interface.name) == 0) {
state->wl_compositor = wl_registry_bind(
registry, name, &wl_compositor_interface, 4);
// Set up wl_surface
//state->wl_surface = wl_compositor_create_surface(state->wl_compositor);
}
if (strcmp(interface, wl_shm_interface.name) == 0) {
state->wl_shm = wl_registry_bind(registry, name,
&wl_shm_interface, 1);
}
if (strcmp(interface, wl_seat_interface.name) == 0) {
state->wl_seat = wl_registry_bind(registry, name,
&wl_seat_interface, 1);
state->wl_pointer = wl_seat_get_pointer(state->wl_seat);
wl_pointer_add_listener(state->wl_pointer, &wl_pointer_listener, state);
struct wl_cursor_theme *cursor_theme =
wl_cursor_theme_load(NULL, 24, state->wl_shm);
struct wl_cursor *cursor =
wl_cursor_theme_get_cursor(cursor_theme, "left_ptr");
state->wl_cursor_image = cursor->images[0];
struct wl_buffer *cursor_buffer =
wl_cursor_image_get_buffer(state->wl_cursor_image);
state->cursor_surface = wl_compositor_create_surface(state->wl_compositor);
wl_surface_attach(state->cursor_surface, cursor_buffer, 0, 0);
wl_surface_commit(state->cursor_surface);
}
if ((strcmp(interface, xdg_wm_base_interface.name) == 0) && (state->output_type == OUTPUT_WINDOW)) {
state->xdg_wm_base = wl_registry_bind(
registry, name, &xdg_wm_base_interface, 1);
xdg_wm_base_add_listener(state->xdg_wm_base,
&xdg_wm_base_listener, state);
}
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->output_type == OUTPUT_LAYER) {
struct wl_output *wl_output = wl_registry_bind(
registry, name, &wl_output_interface, 4);
layer_new(state, wl_output);
} }
} }
@ -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;