From f4fa50d4bf8463244d4d930651e759e568960e64 Mon Sep 17 00:00:00 2001 From: Emile Clark-Boman Date: Wed, 27 Aug 2025 17:59:12 +1000 Subject: [PATCH] MAJOR REFACTOR (2) --- src/lib/client.c | 2 ++ src/lib/client.h | 2 ++ src/lib/crywl.c | 48 ++++++++++++++++++++++++++++- src/lib/crywl.h | 1 + src/lib/{layer.c => layers.c} | 2 ++ src/lib/{layer.h => layers.h} | 8 +++-- src/lib/macros.h | 14 +++++++++ src/lib/monitor.c | 25 +++++++++++++++ src/lib/monitor.h | 14 +++++---- src/lib/other.c | 58 ----------------------------------- src/lib/other.h | 9 ------ src/main.c | 21 +------------ src/util.h | 27 +++++++++++++++- 13 files changed, 133 insertions(+), 98 deletions(-) rename src/lib/{layer.c => layers.c} (98%) rename src/lib/{layer.h => layers.h} (86%) create mode 100644 src/lib/macros.h delete mode 100644 src/lib/other.c delete mode 100644 src/lib/other.h diff --git a/src/lib/client.c b/src/lib/client.c index 5508198..a3879fb 100644 --- a/src/lib/client.c +++ b/src/lib/client.c @@ -1,3 +1,5 @@ +#include "lib/client.h" + void applybounds(Client *c, struct wlr_box *bbox) { /* set minimum possible */ c->geom.width = MAX(1 + 2 * (int)c->bw, c->geom.width); diff --git a/src/lib/client.h b/src/lib/client.h index 4bbb8bc..64f55dc 100644 --- a/src/lib/client.h +++ b/src/lib/client.h @@ -9,6 +9,8 @@ enum { XDGShell, LayerShell, X11 }; /* client types */ +typedef struct Monitor Monitor; // forward declare + typedef struct { /* Must keep this field first */ unsigned int type; /* XDGShell or X11* */ diff --git a/src/lib/crywl.c b/src/lib/crywl.c index 2efef93..0b9a145 100644 --- a/src/lib/crywl.c +++ b/src/lib/crywl.c @@ -1,3 +1,12 @@ +#include "sys/types.h" + +#include "crywl.h" + +/* Static Variables */ +static pid_t child_pid = -1; + + + void chvt(const Arg *arg) { wlr_session_change_vt(session, arg->ui); } void cleanup(void) { @@ -295,6 +304,42 @@ void handlesig(int signo) { quit(NULL); } +void printstatus(void) { + Monitor *m = NULL; + Client *c; + uint32_t occ, urg, sel; + + wl_list_for_each(m, &mons, link) { + occ = urg = 0; + wl_list_for_each(c, &clients, link) { + if (c->mon != m) + continue; + occ |= c->tags; + if (c->isurgent) + urg |= c->tags; + } + if ((c = focustop(m))) { + printf("%s title %s\n", m->wlr_output->name, client_get_title(c)); + printf("%s appid %s\n", m->wlr_output->name, client_get_appid(c)); + printf("%s fullscreen %d\n", m->wlr_output->name, c->isfullscreen); + printf("%s floating %d\n", m->wlr_output->name, c->isfloating); + sel = c->tags; + } else { + printf("%s title \n", m->wlr_output->name); + printf("%s appid \n", m->wlr_output->name); + printf("%s fullscreen \n", m->wlr_output->name); + printf("%s floating \n", m->wlr_output->name); + sel = 0; + } + + printf("%s selmon %u\n", m->wlr_output->name, m == selmon); + printf("%s tags %" PRIu32 " %" PRIu32 " %" PRIu32 " %" PRIu32 "\n", + m->wlr_output->name, occ, m->tagset[m->seltags], sel, urg); + printf("%s layout %s\n", m->wlr_output->name, m->ltsymbol); + } + fflush(stdout); +} + void run(char *startup_cmd) { /* Add a Unix socket to the Wayland display. */ const char *socket = wl_display_add_socket_auto(dpy); @@ -315,7 +360,7 @@ void run(char *startup_cmd) { die("startup: pipe:"); if ((child_pid = fork()) < 0) die("startup: fork:"); - if (child_pid == 0) { + if (child_pid == 0) { setsid(); dup2(piperw[0], STDIN_FILENO); close(piperw[0]); @@ -365,3 +410,4 @@ void spawn(const Arg *arg) { } void quit(const Arg *arg) { wl_display_terminate(dpy); } + diff --git a/src/lib/crywl.h b/src/lib/crywl.h index 6ec749c..0b3620e 100644 --- a/src/lib/crywl.h +++ b/src/lib/crywl.h @@ -7,6 +7,7 @@ static void chvt(const Arg *arg); static void cleanup(void); static void cleanuplisteners(void); static void handlesig(int signo); +static void printstatus(void); static void quit(const Arg *arg); static void run(char *startup_cmd); static void setup(void); diff --git a/src/lib/layer.c b/src/lib/layers.c similarity index 98% rename from src/lib/layer.c rename to src/lib/layers.c index bbd0159..ddeb7f0 100644 --- a/src/lib/layer.c +++ b/src/lib/layers.c @@ -1,3 +1,5 @@ +#include "lib/layers.h" + void createlayersurface(struct wl_listener *listener, void *data) { struct wlr_layer_surface_v1 *layer_surface = data; LayerSurface *l; diff --git a/src/lib/layer.h b/src/lib/layers.h similarity index 86% rename from src/lib/layer.h rename to src/lib/layers.h index 68ebb18..f789804 100644 --- a/src/lib/layer.h +++ b/src/lib/layers.h @@ -1,5 +1,7 @@ -#ifndef CRYWL_LAYER_H -#define CRYWL_LAYER_H +#ifndef CRYWL_LAYERS_H +#define CRYWL_LAYERS_H + +#include "lib/monitor.h" enum { LyrBg, @@ -33,4 +35,4 @@ typedef struct { /* ===== Function Declarations ===== */ static void createlayersurface(struct wl_listener *listener, void *data); -#endif /* CRYWL_LAYER_H */ +#endif /* CRYWL_LAYERS_H */ diff --git a/src/lib/macros.h b/src/lib/macros.h new file mode 100644 index 0000000..a2d7e88 --- /dev/null +++ b/src/lib/macros.h @@ -0,0 +1,14 @@ +#ifndef CRYWL_MACROS_H +#define CRYWL_MACROS_H + +#define MAX(A, B) ((A) > (B) ? (A) : (B)) +#define MIN(A, B) ((A) < (B) ? (A) : (B)) +#define CLEANMASK(mask) (mask & ~WLR_MODIFIER_CAPS) +#define VISIBLEON(C, M) ((M) && (C)->mon == (M) && ((C)->tags & (M)->tagset[(M)->seltags])) +#define LENGTH(X) (sizeof X / sizeof X[0]) +#define END(A) ((A) + LENGTH(A)) +#define TAGMASK ((1u << TAGCOUNT) - 1) +#define LISTEN(E, L, H) wl_signal_add((E), ((L)->notify = (H), (L))) +#define LISTEN_STATIC(E, H) do { struct wl_listener *_l = ecalloc(1, sizeof(*_l)); _l->notify = (H); wl_signal_add((E), _l); } while (0) + +#endif /* CRYWL_MACROS_H */ diff --git a/src/lib/monitor.c b/src/lib/monitor.c index 6ff1d9d..18537d5 100644 --- a/src/lib/monitor.c +++ b/src/lib/monitor.c @@ -1,3 +1,5 @@ +#include "lib/monitor.h" + void arrange(Monitor *m) { Client *c; @@ -292,6 +294,29 @@ Client *focustop(Monitor *m) { return NULL; } +void gpureset(struct wl_listener *listener, void *data) { + struct wlr_renderer *old_drw = drw; + struct wlr_allocator *old_alloc = alloc; + struct Monitor *m; + if (!(drw = wlr_renderer_autocreate(backend))) + die("couldn't recreate renderer"); + + if (!(alloc = wlr_allocator_autocreate(backend, drw))) + die("couldn't recreate allocator"); + + wl_list_remove(&gpu_reset.link); + wl_signal_add(&drw->events.lost, &gpu_reset); + + wlr_compositor_set_renderer(compositor, drw); + + wl_list_for_each(m, &mons, link) { + wlr_output_init_render(m->wlr_output, alloc, drw); + } + + wlr_allocator_destroy(old_alloc); + wlr_renderer_destroy(old_drw); +} + void incnmaster(const Arg *arg) { if (!arg || !selmon) return; diff --git a/src/lib/monitor.h b/src/lib/monitor.h index c008425..41327a3 100644 --- a/src/lib/monitor.h +++ b/src/lib/monitor.h @@ -1,7 +1,13 @@ #ifndef CRYWL_MONITOR_H #define CRYWL_MONITOR_H -typedef struct Monitor Monitor; +#include "lib/client.h" + +// client.h contains a forward declaration of Monitor +typedef struct { + const char *symbol; + void (*arrange)(Monitor *); +} Layout; struct Monitor { struct wl_list link; @@ -27,11 +33,6 @@ struct Monitor { int asleep; }; -typedef struct { - const char *symbol; - void (*arrange)(Monitor *); -} Layout; - typedef struct { const char *name; float mfact; @@ -63,6 +64,7 @@ static Monitor *dirtomon(enum wlr_direction dir); static void focusmon(const Arg *arg); static void focusstack(const Arg *arg); static Client *focustop(Monitor *m); +static void gpureset(struct wl_listener *listener, void *data); static void incnmaster(const Arg *arg); static void monocle(Monitor *m); static void rendermon(struct wl_listener *listener, void *data); diff --git a/src/lib/other.c b/src/lib/other.c deleted file mode 100644 index 0ea8454..0000000 --- a/src/lib/other.c +++ /dev/null @@ -1,58 +0,0 @@ -void gpureset(struct wl_listener *listener, void *data) { - struct wlr_renderer *old_drw = drw; - struct wlr_allocator *old_alloc = alloc; - struct Monitor *m; - if (!(drw = wlr_renderer_autocreate(backend))) - die("couldn't recreate renderer"); - - if (!(alloc = wlr_allocator_autocreate(backend, drw))) - die("couldn't recreate allocator"); - - wl_list_remove(&gpu_reset.link); - wl_signal_add(&drw->events.lost, &gpu_reset); - - wlr_compositor_set_renderer(compositor, drw); - - wl_list_for_each(m, &mons, link) { - wlr_output_init_render(m->wlr_output, alloc, drw); - } - - wlr_allocator_destroy(old_alloc); - wlr_renderer_destroy(old_drw); -} - -void printstatus(void) { - Monitor *m = NULL; - Client *c; - uint32_t occ, urg, sel; - - wl_list_for_each(m, &mons, link) { - occ = urg = 0; - wl_list_for_each(c, &clients, link) { - if (c->mon != m) - continue; - occ |= c->tags; - if (c->isurgent) - urg |= c->tags; - } - if ((c = focustop(m))) { - printf("%s title %s\n", m->wlr_output->name, client_get_title(c)); - printf("%s appid %s\n", m->wlr_output->name, client_get_appid(c)); - printf("%s fullscreen %d\n", m->wlr_output->name, c->isfullscreen); - printf("%s floating %d\n", m->wlr_output->name, c->isfloating); - sel = c->tags; - } else { - printf("%s title \n", m->wlr_output->name); - printf("%s appid \n", m->wlr_output->name); - printf("%s fullscreen \n", m->wlr_output->name); - printf("%s floating \n", m->wlr_output->name); - sel = 0; - } - - printf("%s selmon %u\n", m->wlr_output->name, m == selmon); - printf("%s tags %" PRIu32 " %" PRIu32 " %" PRIu32 " %" PRIu32 "\n", - m->wlr_output->name, occ, m->tagset[m->seltags], sel, urg); - printf("%s layout %s\n", m->wlr_output->name, m->ltsymbol); - } - fflush(stdout); -} diff --git a/src/lib/other.h b/src/lib/other.h deleted file mode 100644 index 68c8737..0000000 --- a/src/lib/other.h +++ /dev/null @@ -1,9 +0,0 @@ -/* TODO: rename other.(c|h) to some better word */ -#ifndef CRYWL_OTHER_H -#define CRYWL_OTHER_H - -/* ===== Function Declarations ===== */ -static void gpureset(struct wl_listener *listener, void *data); -static void printstatus(void); - -#endif /* CRYWL_OTHER_H */ diff --git a/src/main.c b/src/main.c index 1133c8b..2c997b1 100644 --- a/src/main.c +++ b/src/main.c @@ -69,28 +69,9 @@ #endif #include "util.h" - -/* macros */ -#define MAX(A, B) ((A) > (B) ? (A) : (B)) -#define MIN(A, B) ((A) < (B) ? (A) : (B)) -#define CLEANMASK(mask) (mask & ~WLR_MODIFIER_CAPS) -#define VISIBLEON(C, M) ((M) && (C)->mon == (M) && ((C)->tags & (M)->tagset[(M)->seltags])) -#define LENGTH(X) (sizeof X / sizeof X[0]) -#define END(A) ((A) + LENGTH(A)) -#define TAGMASK ((1u << TAGCOUNT) - 1) -#define LISTEN(E, L, H) wl_signal_add((E), ((L)->notify = (H), (L))) -#define LISTEN_STATIC(E, H) do { struct wl_listener *_l = ecalloc(1, sizeof(*_l)); _l->notify = (H); wl_signal_add((E), _l); } while (0) - -/* enums */ -typedef union { - int i; - uint32_t ui; - float f; - const void *v; -} Arg; +#include "lib/layers.h" /* variables */ -static pid_t child_pid = -1; static int locked; static void *exclusive_focus; static struct wl_display *dpy; diff --git a/src/util.h b/src/util.h index 226980d..f122710 100644 --- a/src/util.h +++ b/src/util.h @@ -1,5 +1,30 @@ -/* See LICENSE.dwm file for copyright and license details. */ +#ifndef CRYWL_UTIL_H +#define CRYWL_UTIL_H +#include +#include + +/* MACROS */ +#define MAX(A, B) ((A) > (B) ? (A) : (B)) +#define MIN(A, B) ((A) < (B) ? (A) : (B)) +#define CLEANMASK(mask) (mask & ~WLR_MODIFIER_CAPS) +#define VISIBLEON(C, M) ((M) && (C)->mon == (M) && ((C)->tags & (M)->tagset[(M)->seltags])) +#define LENGTH(X) (sizeof X / sizeof X[0]) +#define END(A) ((A) + LENGTH(A)) +#define TAGMASK ((1u << TAGCOUNT) - 1) +#define LISTEN(E, L, H) wl_signal_add((E), ((L)->notify = (H), (L))) +#define LISTEN_STATIC(E, H) do { struct wl_listener *_l = ecalloc(1, sizeof(*_l)); _l->notify = (H); wl_signal_add((E), _l); } while (0) + +typedef union { + int i; + uint32_t ui; + float f; + const void *v; +} Arg; + +/* ===== Function Declaration ===== */ void die(const char *fmt, ...); void *ecalloc(size_t nmemb, size_t size); int fd_set_nonblock(int fd); + +#endif /* CRYWL_UTIL_H */