From 1e09b182ee510e27d543a51f018dcc04cc39d5b9 Mon Sep 17 00:00:00 2001 From: HuntedByTheIRS Date: Thu, 2 Jul 2026 01:58:57 -0400 Subject: [PATCH] refactor: create include/common.h with shared types/macros, deduplicate across 7 files --- include/common.h | 42 ++++++++++++++++++++++++++++++++++++++++++ include/ipc.h | 8 +------- src/ext_workspace.c | 12 +----------- src/ipc_socket.c | 12 +----------- src/layout.c | 14 +------------- src/peachwm.c | 5 +---- src/scratchpad.c | 11 ----------- src/scratchpad.h | 15 +-------------- 8 files changed, 48 insertions(+), 71 deletions(-) create mode 100644 include/common.h diff --git a/include/common.h b/include/common.h new file mode 100644 index 0000000..8846488 --- /dev/null +++ b/include/common.h @@ -0,0 +1,42 @@ +#pragma once + +#include +#include + +/* Default tag count — define before including to override */ +#ifndef TAGCOUNT +#define TAGCOUNT 9 +#endif + +/* Forward declarations for types used below */ +struct Client; +struct Monitor; + +/* Argument union used by keybind functions */ +typedef union { + int i; + uint32_t ui; + float f; + const void *v; +} Arg; + +/* Compute array length */ +#define LENGTH(X) (sizeof(X) / sizeof((X)[0])) + +/* Bitmask covering all tags */ +#define TAGMASK ((1u << TAGCOUNT) - 1) + +/* Convenience min/max */ +#define MAX(A, B) ((A) > (B) ? (A) : (B)) +#define MIN(A, B) ((A) < (B) ? (A) : (B)) + +/* Convenience wrapper for the function below */ +#define VISIBLEON(C, M) visibleon((C), (M)) + +/* Determine whether client @c is visible on monitor @m */ +static inline int visibleon(struct Client *c, struct Monitor *m) { + return m && c->mon == m + ? (c->isscratchpad ? m->scratchpad_visible + : (int)(c->tags & m->tagset[m->seltags])) + : 0; +} diff --git a/include/ipc.h b/include/ipc.h index f6df26f..0b649b2 100644 --- a/include/ipc.h +++ b/include/ipc.h @@ -1,13 +1,7 @@ #pragma once #include "peachwm-ipc-unstable-v2-protocol.h" #include "monitor.h" - -/* macros needed by IPC */ -#ifndef TAGCOUNT -#define TAGCOUNT 9 -#endif -#define LENGTH(X) (sizeof X / sizeof X[0]) -#define TAGMASK ((1u << TAGCOUNT) - 1) +#include "common.h" /* Forward declarations from peachwm.c */ diff --git a/src/ext_workspace.c b/src/ext_workspace.c index b5bf777..c5c54f5 100644 --- a/src/ext_workspace.c +++ b/src/ext_workspace.c @@ -14,17 +14,7 @@ #include "ipc.h" #include "monitor.h" #include "util.h" - -#define LENGTH(X) (sizeof X / sizeof X[0]) -#define TAGMASK ((1u << TAGCOUNT) - 1) - -/* Forward declarations from peachwm.c */ -typedef union { - int i; - uint32_t ui; - float f; - const void *v; -} Arg; +#include "common.h" void view(const Arg *arg); void toggleview(const Arg *arg); diff --git a/src/ipc_socket.c b/src/ipc_socket.c index f143362..46ade7b 100644 --- a/src/ipc_socket.c +++ b/src/ipc_socket.c @@ -29,17 +29,7 @@ #include "ipc.h" #include "ipc_socket.h" #include "util.h" - -/* ------------------------------------------------------------------ */ -/* Types from peachwm.c needed by command handlers */ -/* ------------------------------------------------------------------ */ - -typedef union { - int i; - uint32_t ui; - float f; - const void *v; -} Arg; +#include "common.h" /* ------------------------------------------------------------------ */ /* Functions from peachwm.c called by IPC command handler */ diff --git a/src/layout.c b/src/layout.c index 72410bf..0a73dc4 100644 --- a/src/layout.c +++ b/src/layout.c @@ -8,19 +8,7 @@ #include "util.h" #include "layout.h" #include "parser/parser.h" - -/* macros (duplicated from peachwm.c) */ -#define MAX(A, B) ((A) > (B) ? (A) : (B)) -#define LENGTH(X) (sizeof X / sizeof X[0]) -#define TAGMASK ((1u << TAGCOUNT) - 1) - -static inline int visibleon(Client *c, Monitor *m) { - return m && c->mon == m - ? (c->isscratchpad ? m->scratchpad_visible - : (int)(c->tags & m->tagset[m->seltags])) - : 0; -} -#define VISIBLEON(C, M) visibleon((C), (M)) +#include "common.h" /* globals from peachwm.c */ extern struct wl_list clients; diff --git a/src/peachwm.c b/src/peachwm.c index f6c698c..d76d513 100644 --- a/src/peachwm.c +++ b/src/peachwm.c @@ -82,14 +82,11 @@ #include "ext_workspace.h" #include "ipc.h" #include "ipc_socket.h" +#include "common.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 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 { \ diff --git a/src/scratchpad.c b/src/scratchpad.c index 00309b7..401e254 100644 --- a/src/scratchpad.c +++ b/src/scratchpad.c @@ -14,17 +14,6 @@ #include "monitor.h" #include "scratchpad.h" -/* ------------------------------------------------------------------ */ -/* Visibility helper — duplicated from layout.c / peachwm.c */ -/* ------------------------------------------------------------------ */ - -int visibleon(Client *c, Monitor *m) { - return m && c->mon == m - ? (c->isscratchpad ? m->scratchpad_visible - : (int)(c->tags & m->tagset[m->seltags])) - : 0; -} - /* ------------------------------------------------------------------ */ /* Extern functions from peachwm.c / layout.c / ipc client */ /* ------------------------------------------------------------------ */ diff --git a/src/scratchpad.h b/src/scratchpad.h index 54292e7..343b2c6 100644 --- a/src/scratchpad.h +++ b/src/scratchpad.h @@ -3,6 +3,7 @@ #include #include "client.h" #include "monitor.h" +#include "common.h" /* Forward declarations from peachwm.c */ extern struct wl_list clients; @@ -29,20 +30,6 @@ enum { /* NUM_LAYERS = 9 */ }; -/* The argument union used by keybind functions. - * Both peachwm.c and the scratchpad module need to agree on this, - * so it sits here rather than privately in each .c file. */ -typedef union { - int i; - uint32_t ui; - float f; - const void *v; -} Arg; - -/* Scoped visibility helper — also defined (identically) in layout.c */ -int visibleon(Client *c, Monitor *m); -#define VISIBLEON(C, M) visibleon((C), (M)) - /* ----------- Extracted scratchpad functions ----------- */ /* Reparent + resize every scratchpad client of @m to a centred 80% box */