aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Jamison <jackqjamison@gmail.com>2025-10-11 02:30:18 -0400
committerJack Jamison <jackqjamison@gmail.com>2025-10-11 02:30:18 -0400
commitf43ec9b27ebe37b799709985f5587e29ec8ef88b (patch)
treecfae61b2e1ffc3658bdf67450666f5f0c89b2b92
parentba3a68a4f73f6653b1e1b095abc08234dedc504d (diff)
refactor to .h files
-rw-r--r--Makefile7
-rw-r--r--keyboard.c10
-rw-r--r--layer_surface.c20
-rw-r--r--main.c28
-rw-r--r--pointer.c12
-rw-r--r--registry.c11
-rw-r--r--seat.c20
-rw-r--r--wayland.h12
8 files changed, 74 insertions, 46 deletions
diff --git a/Makefile b/Makefile
index 98e3784..24e62bb 100644
--- a/Makefile
+++ b/Makefile
@@ -10,6 +10,7 @@ WLC=$(patsubst %.xml,$(OBJDIR)/%.c, $(WLPROT))
WLH=$(patsubst %.xml,$(OBJDIR)/%.h, $(WLPROT))
SRC=$(WLC) $(wildcard *.c) $(wildcard glad/*.c)
OBJ=$(patsubst %.c,$(OBJDIR)/%.o, $(notdir $(SRC)))
+HDEPS=state.h wayland.h
.SILENT: $(OBJ) $(WLC) $(WLH) $(BIN) $(OBJDIR) compile_flags
@@ -22,15 +23,15 @@ $(BIN): $(OBJ)
printf " LD %s\n" $@
$(CC) $(LDFLAGS) $^ -o $@
-$(OBJDIR)/%.o: %.c %.h state.h
+$(OBJDIR)/%.o: %.c %.h $(HDEPS)
printf " CC %s\n" $@
$(CC) $(CFLAGS) -c $< -o $@
-$(OBJDIR)/%.o: */%.c */%.h state.h
+$(OBJDIR)/%.o: */%.c */%.h $(HDEPS)
printf " CC %s\n" $@
$(CC) $(CFLAGS) -c $< -o $@
-$(OBJDIR)/%.o: %.c state.h
+$(OBJDIR)/%.o: %.c $(HDEPS)
printf " CC %s\n" $@
$(CC) $(CFLAGS) -c $< -o $@
diff --git a/keyboard.c b/keyboard.c
index ab208c2..94bd29f 100644
--- a/keyboard.c
+++ b/keyboard.c
@@ -1,10 +1,16 @@
-#include <assert.h>
#include <sys/mman.h>
#include <unistd.h>
-#include <wayland-client-protocol.h>
#include <locale.h>
+
#include "state.h"
+struct wl_keyboard_listener keyboard_listener;
+
+void setup_keyboard(client_state* state) {
+ state->keyboard = wl_seat_get_keyboard(state->seat);
+ wl_keyboard_add_listener(state->keyboard, &keyboard_listener, state);
+}
+
void wl_keyboard_keymap(void *data, struct wl_keyboard *wl_keyboard,
uint32_t format, int32_t fd, uint32_t size) {
client_state* state = data;
diff --git a/layer_surface.c b/layer_surface.c
index f2779fd..0463677 100644
--- a/layer_surface.c
+++ b/layer_surface.c
@@ -1,5 +1,25 @@
#include "./state.h"
+struct zwlr_layer_surface_v1_listener layer_surface_listener;
+
+void setup_surface(client_state* state) {
+ state->surface = wl_compositor_create_surface(state->compositor);
+ state->layer_surface = zwlr_layer_shell_v1_get_layer_surface(
+ state->layer_shell, state->surface, NULL,
+ ZWLR_LAYER_SHELL_V1_LAYER_TOP, "swenu");
+ zwlr_layer_surface_v1_add_listener(state->layer_surface, &layer_surface_listener, state);
+ zwlr_layer_surface_v1_set_size(state->layer_surface, 600, 400);
+ zwlr_layer_surface_v1_set_anchor(state->layer_surface, ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP |
+ ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM |
+ ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT |
+ ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT);
+ zwlr_layer_surface_v1_set_exclusive_zone(state->layer_surface, -1);
+ zwlr_layer_surface_v1_set_margin(state->layer_surface, 0, 0, 0, 0);
+ zwlr_layer_surface_v1_set_keyboard_interactivity(state->layer_surface, ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_EXCLUSIVE);
+ wl_surface_commit(state->surface);
+ wl_display_roundtrip(state->display);
+}
+
void configure(void *data, struct zwlr_layer_surface_v1 *zwlr_layer_surface_v1, uint32_t serial, uint32_t width, uint32_t height) {
client_state* state = data;
state->width = width;
diff --git a/main.c b/main.c
index 9baf779..025bf7a 100644
--- a/main.c
+++ b/main.c
@@ -2,16 +2,11 @@
#include <stdio.h>
#include "state.h"
+#include "wayland.h"
#include "graphics.h"
-#include "wlr-layer-shell-unstable-v1.h"
-
-extern struct wl_registry_listener registry_listener;
-extern struct zwlr_layer_surface_v1_listener layer_surface_listener;
-
int main() {
client_state state = {0};
-
state.running = true;
state.display = wl_display_connect(NULL);
@@ -20,25 +15,8 @@ int main() {
return 0;
}
- state.registry = wl_display_get_registry(state.display);
- wl_registry_add_listener(state.registry, &registry_listener, &state);
- wl_display_roundtrip(state.display);
-
- state.surface = wl_compositor_create_surface(state.compositor);
- state.layer_surface = zwlr_layer_shell_v1_get_layer_surface(
- state.layer_shell, state.surface, NULL,
- ZWLR_LAYER_SHELL_V1_LAYER_TOP, "swenu");
- zwlr_layer_surface_v1_add_listener(state.layer_surface, &layer_surface_listener, &state);
- zwlr_layer_surface_v1_set_size(state.layer_surface, 600, 400);
- zwlr_layer_surface_v1_set_anchor(state.layer_surface, ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP |
- ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM |
- ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT |
- ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT);
- zwlr_layer_surface_v1_set_exclusive_zone(state.layer_surface, -1);
- zwlr_layer_surface_v1_set_margin(state.layer_surface, 0, 0, 0, 0);
- zwlr_layer_surface_v1_set_keyboard_interactivity(state.layer_surface, ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_EXCLUSIVE);
- wl_surface_commit(state.surface);
- wl_display_roundtrip(state.display);
+ setup_registry(&state);
+ setup_surface(&state);
if (!init_gl(&state)) {
fprintf(stderr, "Failed to Initalize EGL/OpenGL\n");
diff --git a/pointer.c b/pointer.c
index 8acab04..9d36df8 100644
--- a/pointer.c
+++ b/pointer.c
@@ -1,7 +1,13 @@
-#include "./state.h"
-#include "cursor-shape-v1.h"
-#define CLAMP(x, min, max) (x > max ? max : (x < min ? min : x))
+#include "state.h"
+struct wl_pointer_listener pointer_listener;
+
+void setup_pointer(client_state* state) {
+ state->pointer = wl_seat_get_pointer(state->seat);
+ state->cursor_shape_device = wp_cursor_shape_manager_v1_get_pointer(
+ state->cursor_shape_manager, state->pointer);
+ wl_pointer_add_listener(state->pointer, &pointer_listener, state);
+}
void enter(void *data, struct wl_pointer *pointer, uint32_t serial,
struct wl_surface *surface, wl_fixed_t fixed_surface_x, wl_fixed_t fixed_surface_y) {
diff --git a/registry.c b/registry.c
index 2f98d12..bee7d1b 100644
--- a/registry.c
+++ b/registry.c
@@ -2,9 +2,15 @@
#include <string.h>
#include "./state.h"
+#include "wayland.h"
-extern struct wl_seat_listener seat_listener;
+struct wl_registry_listener registry_listener;
+void setup_registry(client_state* state) {
+ state->registry = wl_display_get_registry(state->display);
+ wl_registry_add_listener(state->registry, &registry_listener, state);
+ wl_display_roundtrip(state->display);
+}
void global(void *data, struct wl_registry *wl_registry, uint32_t name, const char *interface, uint32_t version) {
client_state* state = data;
@@ -14,8 +20,7 @@ void global(void *data, struct wl_registry *wl_registry, uint32_t name, const ch
state->compositor = wl_registry_bind(wl_registry, name, &wl_compositor_interface, 1);
}
else if (!strcmp(interface, wl_seat_interface.name)) {
- state->seat = wl_registry_bind(wl_registry, name, &wl_seat_interface, 1);
- wl_seat_add_listener(state->seat, &seat_listener, state);
+ setup_seat(state, name);
}
else if (!strcmp(interface, zwlr_layer_shell_v1_interface.name)) {
state->layer_shell = wl_registry_bind(wl_registry, name, &zwlr_layer_shell_v1_interface, version);
diff --git a/seat.c b/seat.c
index 2dd84a3..6199f13 100644
--- a/seat.c
+++ b/seat.c
@@ -1,23 +1,23 @@
#include "state.h"
-#include <cursor-shape-v1.h>
-extern struct wl_pointer_listener pointer_listener;
-extern struct wl_keyboard_listener keyboard_listener;
+#include "wayland.h"
+
+struct wl_seat_listener seat_listener;
+
+void setup_seat(client_state* state, uint32_t name) {
+ state->seat = wl_registry_bind(state->registry, name, &wl_seat_interface, 1);
+ wl_seat_add_listener(state->seat, &seat_listener, state);
+}
void capabilities(void *data, struct wl_seat *wl_seat, uint32_t capabilities) {
client_state* state = data;
if (capabilities & WL_SEAT_CAPABILITY_KEYBOARD && !state->keyboard) {
- state->keyboard = wl_seat_get_keyboard(wl_seat);
- wl_keyboard_add_listener(state->keyboard, &keyboard_listener, state);
+ setup_keyboard(state);
}
if (capabilities & WL_SEAT_CAPABILITY_POINTER && !state->pointer) {
- state->pointer = wl_seat_get_pointer(wl_seat);
- state->cursor_shape_device = wp_cursor_shape_manager_v1_get_pointer(
- state->cursor_shape_manager, state->pointer);
- wl_pointer_add_listener(state->pointer, &pointer_listener, state);
+ setup_pointer(state);
}
-
}
void name(void *data, struct wl_seat *wl_seat, const char *name) {
diff --git a/wayland.h b/wayland.h
new file mode 100644
index 0000000..c5f4daa
--- /dev/null
+++ b/wayland.h
@@ -0,0 +1,12 @@
+#ifndef WAYLAND_H_
+#define WAYLAND_H_
+
+#include "state.h"
+
+void setup_registry(client_state* state);
+void setup_surface(client_state* state);
+void setup_seat(client_state* state, uint32_t name);
+void setup_pointer(client_state* state);
+void setup_keyboard(client_state* state);
+
+#endif