refactor: create include/common.h with shared types/macros, deduplicate across 7 files

This commit is contained in:
2026-07-02 01:58:57 -04:00
parent 9581031dac
commit 1e09b182ee
8 changed files with 48 additions and 71 deletions
+42
View File
@@ -0,0 +1,42 @@
#pragma once
#include <stdint.h>
#include <stddef.h>
/* 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;
}
+1 -7
View File
@@ -1,13 +1,7 @@
#pragma once #pragma once
#include "peachwm-ipc-unstable-v2-protocol.h" #include "peachwm-ipc-unstable-v2-protocol.h"
#include "monitor.h" #include "monitor.h"
#include "common.h"
/* macros needed by IPC */
#ifndef TAGCOUNT
#define TAGCOUNT 9
#endif
#define LENGTH(X) (sizeof X / sizeof X[0])
#define TAGMASK ((1u << TAGCOUNT) - 1)
/* Forward declarations from peachwm.c */ /* Forward declarations from peachwm.c */
+1 -11
View File
@@ -14,17 +14,7 @@
#include "ipc.h" #include "ipc.h"
#include "monitor.h" #include "monitor.h"
#include "util.h" #include "util.h"
#include "common.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;
void view(const Arg *arg); void view(const Arg *arg);
void toggleview(const Arg *arg); void toggleview(const Arg *arg);
+1 -11
View File
@@ -29,17 +29,7 @@
#include "ipc.h" #include "ipc.h"
#include "ipc_socket.h" #include "ipc_socket.h"
#include "util.h" #include "util.h"
#include "common.h"
/* ------------------------------------------------------------------ */
/* Types from peachwm.c needed by command handlers */
/* ------------------------------------------------------------------ */
typedef union {
int i;
uint32_t ui;
float f;
const void *v;
} Arg;
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
/* Functions from peachwm.c called by IPC command handler */ /* Functions from peachwm.c called by IPC command handler */
+1 -13
View File
@@ -8,19 +8,7 @@
#include "util.h" #include "util.h"
#include "layout.h" #include "layout.h"
#include "parser/parser.h" #include "parser/parser.h"
#include "common.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))
/* globals from peachwm.c */ /* globals from peachwm.c */
extern struct wl_list clients; extern struct wl_list clients;
+1 -4
View File
@@ -82,14 +82,11 @@
#include "ext_workspace.h" #include "ext_workspace.h"
#include "ipc.h" #include "ipc.h"
#include "ipc_socket.h" #include "ipc_socket.h"
#include "common.h"
/* macros */ /* 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 CLEANMASK(mask) (mask & ~WLR_MODIFIER_CAPS)
#define LENGTH(X) (sizeof X / sizeof X[0])
#define END(A) ((A) + LENGTH(A)) #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(E, L, H) wl_signal_add((E), ((L)->notify = (H), (L)))
#define LISTEN_STATIC(E, H) \ #define LISTEN_STATIC(E, H) \
do { \ do { \
-11
View File
@@ -14,17 +14,6 @@
#include "monitor.h" #include "monitor.h"
#include "scratchpad.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 */ /* Extern functions from peachwm.c / layout.c / ipc client */
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
+1 -14
View File
@@ -3,6 +3,7 @@
#include <wlr/types/wlr_scene.h> #include <wlr/types/wlr_scene.h>
#include "client.h" #include "client.h"
#include "monitor.h" #include "monitor.h"
#include "common.h"
/* Forward declarations from peachwm.c */ /* Forward declarations from peachwm.c */
extern struct wl_list clients; extern struct wl_list clients;
@@ -29,20 +30,6 @@ enum {
/* NUM_LAYERS = 9 */ /* 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 ----------- */ /* ----------- Extracted scratchpad functions ----------- */
/* Reparent + resize every scratchpad client of @m to a centred 80% box */ /* Reparent + resize every scratchpad client of @m to a centred 80% box */