From 0928b2115e1f11144e9f03ab2db37308742948fc Mon Sep 17 00:00:00 2001 From: Siva Mahadevan Date: Sat, 28 Feb 2026 18:49:59 -0500 Subject: [PATCH] move util.c to util.h This allows single-source compilation and whole-program optimization using something like: make CFLAGS='-flto -ffunction-sections -fdata-sections -Wl,--gc-sections' Furthermore, move more compositor-agnostic utility functions/macros to util.h, similar to the original dwm sources. --- Makefile | 14 ++++--------- README.md | 2 +- dwl.c | 4 ---- util.c | 51 ----------------------------------------------- util.h | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 5 files changed, 61 insertions(+), 69 deletions(-) delete mode 100644 util.c diff --git a/Makefile b/Makefile index 578194f..ad174d2 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ include config.mk # flags for compiling DWLCPPFLAGS = -I. -DWLR_USE_UNSTABLE -D_POSIX_C_SOURCE=200809L \ - -DVERSION=\"$(VERSION)\" $(XWAYLAND) + -DVERSION=\"$(VERSION)\" $(XWAYLAND) $(CPPFLAGS) DWLDEVCFLAGS = -g -Wpedantic -Wall -Wextra -Wdeclaration-after-statement \ -Wno-unused-parameter -Wshadow -Wunused-macros -Werror=strict-prototypes \ -Werror=implicit -Werror=return-type -Werror=incompatible-pointer-types \ @@ -17,12 +17,10 @@ DWLCFLAGS = `$(PKG_CONFIG) --cflags $(PKGS)` $(WLR_INCS) $(DWLCPPFLAGS) $(DWLDEV LDLIBS = `$(PKG_CONFIG) --libs $(PKGS)` $(WLR_LIBS) -lm $(LIBS) all: dwl -dwl: dwl.o util.o - $(CC) dwl.o util.o $(DWLCFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ -dwl.o: dwl.c client.h config.h config.mk cursor-shape-v1-protocol.h \ +dwl: dwl.c client.h config.h util.h config.mk cursor-shape-v1-protocol.h \ pointer-constraints-unstable-v1-protocol.h wlr-layer-shell-unstable-v1-protocol.h \ wlr-output-power-management-unstable-v1-protocol.h xdg-shell-protocol.h -util.o: util.c util.h + $(CC) dwl.c $(DWLCFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ # wayland-scanner is a tool which generates C headers and rigging for Wayland # protocols, which are specified in XML. wlroots requires you to rig these up @@ -54,7 +52,7 @@ clean: dist: clean mkdir -p dwl-$(VERSION) cp -R LICENSE* Makefile CHANGELOG.md README.md client.h config.def.h \ - config.mk protocols dwl.1 dwl.c util.c util.h dwl.desktop \ + config.mk protocols dwl.1 dwl.c util.h dwl.desktop \ dwl-$(VERSION) tar -caf dwl-$(VERSION).tar.gz dwl-$(VERSION) rm -rf dwl-$(VERSION) @@ -73,7 +71,3 @@ install: dwl uninstall: rm -f $(DESTDIR)$(PREFIX)/bin/dwl $(DESTDIR)$(MANDIR)/man1/dwl.1 \ $(DESTDIR)$(DATADIR)/wayland-sessions/dwl.desktop - -.SUFFIXES: .c .o -.c.o: - $(CC) $(CPPFLAGS) $(DWLCFLAGS) -o $@ -c $< diff --git a/README.md b/README.md index 3a3b9bd..b6c5300 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ primarily in terms of functionality, and secondarily in terms of philosophy. Like [dwm], dwl is: - Easy to understand, hack on, and extend with patches -- One C source file (or a very small number) configurable via `config.h` +- One C source file configurable via `config.h` - Tied to as few external dependencies as possible ## Getting Started: diff --git a/dwl.c b/dwl.c index 101a45f..3893ec7 100644 --- a/dwl.c +++ b/dwl.c @@ -72,12 +72,8 @@ #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) diff --git a/util.c b/util.c deleted file mode 100644 index b925987..0000000 --- a/util.c +++ /dev/null @@ -1,51 +0,0 @@ -/* See LICENSE.dwm file for copyright and license details. */ -#include -#include -#include -#include -#include - -#include "util.h" - -void -die(const char *fmt, ...) { - va_list ap; - - va_start(ap, fmt); - vfprintf(stderr, fmt, ap); - va_end(ap); - - if (fmt[0] && fmt[strlen(fmt)-1] == ':') { - fputc(' ', stderr); - perror(NULL); - } else { - fputc('\n', stderr); - } - - exit(1); -} - -void * -ecalloc(size_t nmemb, size_t size) -{ - void *p; - - if (!(p = calloc(nmemb, size))) - die("calloc:"); - return p; -} - -int -fd_set_nonblock(int fd) { - int flags = fcntl(fd, F_GETFL); - if (flags < 0) { - perror("fcntl(F_GETFL):"); - return -1; - } - if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) { - perror("fcntl(F_SETFL):"); - return -1; - } - - return 0; -} diff --git a/util.h b/util.h index 226980d..40cfbe4 100644 --- a/util.h +++ b/util.h @@ -1,5 +1,58 @@ /* See LICENSE.dwm file for copyright and license details. */ +#include +#include +#include +#include +#include -void die(const char *fmt, ...); -void *ecalloc(size_t nmemb, size_t size); -int fd_set_nonblock(int fd); +#define MAX(A, B) ((A) > (B) ? (A) : (B)) +#define MIN(A, B) ((A) < (B) ? (A) : (B)) +#define LENGTH(X) (sizeof X / sizeof X[0]) +#define END(A) ((A) + LENGTH(A)) + +static void die(const char *fmt, ...); +static void *ecalloc(size_t nmemb, size_t size); +static int fd_set_nonblock(int fd); + +void +die(const char *fmt, ...) { + va_list ap; + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + + if (fmt[0] && fmt[strlen(fmt)-1] == ':') { + fputc(' ', stderr); + perror(NULL); + } else { + fputc('\n', stderr); + } + + exit(1); +} + +void * +ecalloc(size_t nmemb, size_t size) +{ + void *p; + + if (!(p = calloc(nmemb, size))) + die("calloc:"); + return p; +} + +int +fd_set_nonblock(int fd) { + int flags = fcntl(fd, F_GETFL); + if (flags < 0) { + perror("fcntl(F_GETFL):"); + return -1; + } + if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) { + perror("fcntl(F_SETFL):"); + return -1; + } + + return 0; +}