diff options
| author | Abhinav Prasai <abhinav.prsai@gmail.com> | 2025-08-27 19:52:58 +0100 |
|---|---|---|
| committer | Abhinav Prasai <abhinav.prsai@gmail.com> | 2025-08-28 09:54:09 +0100 |
| commit | 6962146b747e449e7281b6c6acda0a32fb1ff453 (patch) | |
| tree | 3875d5852852015aa3e8d3360358c17bff8e1413 | |
| parent | ce3f2d4fa36c0c4e13a30e958ec8aa5f5e84b883 (diff) | |
update Makefile + fix warnings
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | Makefile | 93 | ||||
| -rw-r--r-- | compile_flags.txt | 24 | ||||
| -rw-r--r-- | src/defs.h | 2 | ||||
| -rw-r--r-- | src/sxwm.c | 80 |
5 files changed, 124 insertions, 76 deletions
@@ -1,2 +1,3 @@ build/ sxwm +compile_flags.txt @@ -1,28 +1,56 @@ -CC ?= gcc -CFLAGS ?= -std=c99 -Wall -Wextra -Os -LDFLAGS ?= -lX11 -lXinerama -lXcursor -Isrc - -PREFIX ?= /usr/local -BIN := sxwm -SRC_DIR := src -OBJ_DIR := build -SRC := $(wildcard $(SRC_DIR)/*.c) -OBJ := $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(SRC)) -DEP := $(OBJ:.o=.d) - -MAN := sxwm.1 -MAN_DIR := $(PREFIX)/share/man/man1 +# tools +CC ?= cc +PKG_CONFIG ?= pkg-config +# install dirs +PREFIX ?= /usr/local +DESTDIR ?= +BIN := sxwm +MAN := sxwm.1 +MAN_DIR := $(PREFIX)/share/man/man1 XSESSIONS := $(DESTDIR)$(PREFIX)/share/xsessions +# layout +SRC_DIR := src/ +OBJ_DIR := build/ +SRC := $(wildcard $(SRC_DIR)/*.c) +OBJ := $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(SRC)) +DEP := $(OBJ:.o=.d) + +# flags +CPPFLAGS ?= -Isrc -D_FORTIFY_SOURCE=2 + +# compile flags + warnings, hardening +CFLAGS ?= -std=c99 -Os -pipe \ + -Wall -Wextra -Wformat=2 -Werror=format-security \ + -Wshadow -Wpointer-arith -Wcast-qual -Wwrite-strings \ + -Wmissing-prototypes -Wstrict-prototypes -Wswitch-enum \ + -Wundef -Wvla -fno-common -fno-strict-aliasing \ + -fstack-protector-strong -fPIE + +# linker +LDFLAGS ?= -Wl,-O1 -pie + +# libraries +LDLIBS ?= -lX11 -lXinerama -lXcursor + +# prefer pkg-confgi +ifneq ($(shell $(PKG_CONFIG) --exists x11 xinerama xcursor && echo yes),) +CPPFLAGS += $(shell $(PKG_CONFIG) --cflags x11 xinerama xcursor) +LDLIBS := $(shell $(PKG_CONFIG) --libs x11 xinerama xcursor) +endif + +.PHONY: all clean install uninstall clangd +.SUFFIXES: + all: $(BIN) $(BIN): $(OBJ) - $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) + $(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS) $(OBJ_DIR)/%.o: $(SRC_DIR)/%.c | $(OBJ_DIR) @mkdir -p $(dir $@) - $(CC) $(CFLAGS) -MMD -MP -c -o $@ $< + $(CC) $(CPPFLAGS) $(CFLAGS) -MMD -MP -c -o $@ $< -include $(DEP) @@ -33,34 +61,31 @@ clean: @rm -rf $(OBJ_DIR) $(BIN) install: all - @echo "Installing $(BIN) to $(DESTDIR)$(PREFIX)/bin..." + @echo "installing $(BIN) to $(DESTDIR)$(PREFIX)/bin..." @mkdir -p "$(DESTDIR)$(PREFIX)/bin" @install -m 755 $(BIN) "$(DESTDIR)$(PREFIX)/bin/$(BIN)" - @echo "Installing sxwm.desktop to $(XSESSIONS)..." + @echo "installing sxwm.desktop to $(XSESSIONS)..." @mkdir -p "$(XSESSIONS)" @install -m 644 sxwm.desktop "$(XSESSIONS)/sxwm.desktop" - @echo "Installing man page to $(DESTDIR)$(MAN_DIR)..." - @mkdir -p $(DESTDIR)$(MAN_DIR) - @install -m 644 $(MAN) $(DESTDIR)$(MAN_DIR)/ - @echo "Copying default configuration to $(DESTDIR)$(PREFIX)/share/sxwmrc..." + @echo "installing man page to $(DESTDIR)$(MAN_DIR)..." + @mkdir -p "$(DESTDIR)$(MAN_DIR)" + @install -m 644 $(MAN) "$(DESTDIR)$(MAN_DIR)/" + @echo "copying default config to $(DESTDIR)$(PREFIX)/share/sxwmrc..." @mkdir -p "$(DESTDIR)$(PREFIX)/share" @install -m 644 default_sxwmrc "$(DESTDIR)$(PREFIX)/share/sxwmrc" - @echo "Installation complete." + @echo "installation complete :)" uninstall: - @echo "Uninstalling $(BIN) from $(DESTDIR)$(PREFIX)/bin..." + @echo "uninstalling $(BIN) from $(DESTDIR)$(PREFIX)/bin..." @rm -f "$(DESTDIR)$(PREFIX)/bin/$(BIN)" - @echo "Uninstalling sxwm.desktop from $(XSESSIONS)..." + @echo "uninstalling sxwm.desktop from $(XSESSIONS)..." @rm -f "$(XSESSIONS)/sxwm.desktop" - @echo "Uninstalling man page from $(DESTDIR)$(MAN_DIR)..." - @rm -f $(DESTDIR)$(MAN_DIR)/$(MAN) - @echo "Uninstallation complete." + @echo "uninstalling man page from $(DESTDIR)$(MAN_DIR)..." + @rm -f "$(DESTDIR)$(MAN_DIR)/$(MAN)" + @echo "uninstallation complete :)" +# dev tools clangd: - @echo "Generating compile_flags.txt" + @echo "generating compile_flags.txt" @rm -f compile_flags.txt - @for flag in $(CFLAGS) $(LDFLAGS); do \ - echo $$flag >> compile_flags.txt; \ - done - -.PHONY: all clean install uninstall clangd + @for flag in $(CPPFLAGS) $(CFLAGS); do echo $$flag >> compile_flags.txt; done diff --git a/compile_flags.txt b/compile_flags.txt index e568f62..fa0c2c1 100644 --- a/compile_flags.txt +++ b/compile_flags.txt @@ -1,8 +1,22 @@ +-Isrc +-D_FORTIFY_SOURCE=2 -std=c99 +-Os +-pipe -Wall -Wextra --Os --lX11 --lXinerama --lXcursor --Isrc +-Wformat=2 +-Werror=format-security +-Wshadow +-Wpointer-arith +-Wcast-qual +-Wwrite-strings +-Wmissing-prototypes +-Wstrict-prototypes +-Wswitch-enum +-Wundef +-Wvla +-fno-common +-fno-strict-aliasing +-fstack-protector-strong +-fPIE @@ -130,7 +130,7 @@ typedef struct { void (*fn)(void); } CommandEntry; -extern void centre_window(); +extern void centre_window(void); extern void close_focused(void); extern void dec_gaps(void); extern void focus_next(void); @@ -41,7 +41,7 @@ #include "parser.h" Client *add_client(Window w, int ws); -/* void centre_window(); */ +/* void centre_window(void); */ void change_workspace(int ws); int clean_mask(int mask); /* void close_focused(void); */ @@ -78,7 +78,7 @@ Bool is_child_proc(pid_t pid1, pid_t pid2); /* void move_prev_mon(void); */ void move_to_workspace(int ws); void other_wm(void); -int other_wm_err(Display *dpy, XErrorEvent *ee); +int other_wm_err(Display *d, XErrorEvent *ee); /* long parse_col(const char *hex); */ /* void quit(void); */ /* void reload_config(void); */ @@ -97,7 +97,7 @@ void set_frame_extents(Window w); void set_input_focus(Client *c, Bool raise_win, Bool warp); void set_win_scratchpad(int n); int snap_coordinate(int pos, int size, int screen_size, int snap_dist); -void spawn(const char **argv); +void spawn(const char * const *argv); void startup_exec(void); void swallow_window(Client *swallower, Client *swallowed); void swap_clients(Client *a, Client *b); @@ -116,7 +116,7 @@ void update_workarea(void); void warp_cursor(Client *c); Bool window_should_float(Window w); Bool window_should_start_fullscreen(Window w); -int xerr(Display *dpy, XErrorEvent *ee); +int xerr(Display *d, XErrorEvent *ee); void xev_case(XEvent *xev); #include "config.h" @@ -286,7 +286,7 @@ Client *add_client(Window w, int ws) return c; } -void centre_window() +void centre_window(void) { if (!focused || !focused->mapped || !focused->floating) { return; @@ -485,20 +485,20 @@ Client *find_client(Window w) Window find_toplevel(Window w) { - Window root = None; + Window root_win = None; Window parent; Window *kids; unsigned n_kids; while (True) { - if (w == root) { + if (w == root_win) { break; } - if (XQueryTree(dpy, w, &root, &parent, &kids, &n_kids) == 0) { + if (XQueryTree(dpy, w, &root_win, &parent, &kids, &n_kids) == 0) { break; } XFree(kids); - if (parent == root || parent == None) { + if (parent == root_win || parent == None) { break; } w = parent; @@ -1102,10 +1102,10 @@ void hdl_map_req(XEvent *xev) Atom state_type; Atom *state_atoms = NULL; int state_format; - unsigned long n_items; unsigned long bytes_after; + n_items = 0; - if (XGetWindowProperty(dpy, w, _NET_WM_STATE, 0, 8, False, XA_ATOM, &state_type, &state_format,&n_items, + if (XGetWindowProperty(dpy, w, _NET_WM_STATE, 0, 8, False, XA_ATOM, &state_type, &state_format, &n_items, &bytes_after, (unsigned char**)&state_atoms) == Success && state_atoms) { for (unsigned long i = 0; i < n_items; i++) { if (state_atoms[i] == _NET_WM_STATE_MODAL) { @@ -1284,7 +1284,7 @@ void hdl_motion(XEvent *xev) break; } } - Monitor *current_mon = &mons[mon]; + Monitor *current_mon_motion = &mons[mon]; if (drag_mode == DRAG_SWAP) { Window root_ret, child; @@ -1331,14 +1331,14 @@ void hdl_motion(XEvent *xev) int outer_h = drag_client->h + 2 * user_config.border_width; /* snap relative to this mons bounds: */ - int rel_x = nx - current_mon->x; - int rel_y = ny - current_mon->y; + int rel_x = nx - current_mon_motion->x; + int rel_y = ny - current_mon_motion->y; - rel_x = snap_coordinate(rel_x, outer_w, current_mon->w, user_config.snap_distance); - rel_y = snap_coordinate(rel_y, outer_h, current_mon->h, user_config.snap_distance); + rel_x = snap_coordinate(rel_x, outer_w, current_mon_motion->w, user_config.snap_distance); + rel_y = snap_coordinate(rel_y, outer_h, current_mon_motion->h, user_config.snap_distance); - nx = current_mon->x + rel_x; - ny = current_mon->y + rel_y; + nx = current_mon_motion->x + rel_x; + ny = current_mon_motion->y + rel_y; if (!drag_client->floating && (UDIST(nx, drag_client->x) > user_config.snap_distance || UDIST(ny, drag_client->y) > user_config.snap_distance)) { @@ -1356,8 +1356,8 @@ void hdl_motion(XEvent *xev) int nh = drag_orig_h + dy; /* clamp relative to this mon */ - int max_w = (current_mon->w - (drag_client->x - current_mon->x)); - int max_h = (current_mon->h - (drag_client->y - current_mon->y)); + int max_w = (current_mon_motion->w - (drag_client->x - current_mon_motion->x)); + int max_h = (current_mon_motion->h - (drag_client->y - current_mon_motion->y)); drag_client->w = CLAMP(nw, MIN_WINDOW_SIZE, max_w); drag_client->h = CLAMP(nh, MIN_WINDOW_SIZE, max_h); @@ -1686,11 +1686,11 @@ void other_wm(void) XSync(dpy, False); } -int other_wm_err(Display *dpy, XErrorEvent *ee) +int other_wm_err(Display *d, XErrorEvent *ee) { errx(0, "can't start because another window manager is already running"); return 0; - (void)dpy; + (void)d; (void)ee; } @@ -2100,20 +2100,20 @@ void setup_atoms(void) /* workspace setup */ long num_workspaces = NUM_WORKSPACES; XChangeProperty(dpy, root, _NET_NUMBER_OF_DESKTOPS, XA_CARDINAL, 32, - PropModeReplace, (unsigned char *)&num_workspaces, 1); + PropModeReplace, (const unsigned char *)&num_workspaces, 1); const char workspace_names[] = WORKSPACE_NAMES; int names_len = sizeof(workspace_names); XChangeProperty(dpy, root, _NET_DESKTOP_NAMES, UTF8_STRING, 8, - PropModeReplace, (unsigned char *)workspace_names, names_len); + PropModeReplace, (const unsigned char *)workspace_names, names_len); XChangeProperty(dpy, root, _NET_CURRENT_DESKTOP, XA_CARDINAL, 32, - PropModeReplace, (unsigned char *)¤t_ws, 1); + PropModeReplace, (const unsigned char *)¤t_ws, 1); /* load supported list */ int support_list_len = sizeof(support_list) / sizeof(Atom); XChangeProperty(dpy, root, _NET_SUPPORTED, XA_ATOM, 32, - PropModeReplace, (unsigned char *)support_list, support_list_len); + PropModeReplace, (const unsigned char *)support_list, support_list_len); update_workarea(); } @@ -2193,11 +2193,12 @@ int snap_coordinate(int pos, int size, int screen_size, int snap_dist) return pos; } -void spawn(const char **argv) +void spawn(const char * const *argv) { int argc = 0; - while (argv[argc]) + while (argv[argc]) { argc++; + } int cmd_count = 1; for (int i = 0; i < argc; i++) { @@ -2206,7 +2207,7 @@ void spawn(const char **argv) } } - char ***commands = malloc(cmd_count * sizeof(char **)); + const char ***commands = malloc(cmd_count * sizeof(char **)); /* *** bruh */ if (!commands) { perror("malloc commands"); return; @@ -2217,7 +2218,7 @@ void spawn(const char **argv) for (int i = 0; i <= argc; i++) { if (!argv[i] || strcmp(argv[i], "|") == 0) { int len = i - arg_start; - char **cmd_args = malloc((len + 1) * sizeof(char *)); + const char **cmd_args = malloc((len + 1) * sizeof(char *)); if (!cmd_args) { perror("malloc cmd_args"); for (int j = 0; j < cmd_idx; j++) { @@ -2227,7 +2228,7 @@ void spawn(const char **argv) return; } for (int j = 0; j < len; j++) { - cmd_args[j] = (char *)argv[arg_start + j]; + cmd_args[j] = argv[arg_start + j]; } cmd_args[len] = NULL; commands[cmd_idx++] = cmd_args; @@ -2235,7 +2236,11 @@ void spawn(const char **argv) } } - int pipes[cmd_count - 1][2]; + int (*pipes)[2] = malloc(sizeof(int[2]) * (cmd_count - 1)); + if (!pipes) { + perror("malloc pipes"); + return; + } for (int i = 0; i < cmd_count - 1; i++) { if (pipe(pipes[i]) == -1) { perror("pipe"); @@ -2243,6 +2248,7 @@ void spawn(const char **argv) free(commands[j]); } free(commands); + free(pipes); return; } } @@ -2259,6 +2265,7 @@ void spawn(const char **argv) free(commands[j]); } free(commands); + free(pipes); return; } if (pid == 0) { @@ -2276,7 +2283,7 @@ void spawn(const char **argv) close(pipes[k][1]); } - execvp(commands[i][0], commands[i]); + execvp(commands[i][0], (char* const*)(void*)commands[i]); fprintf(stderr, "sxwm: execvp '%s' failed\n", commands[i][0]); exit(EXIT_FAILURE); } @@ -2291,6 +2298,7 @@ void spawn(const char **argv) free(commands[i]); } free(commands); + free(pipes); } void startup_exec(void) @@ -2301,7 +2309,7 @@ void startup_exec(void) if (argv) { spawn(argv); for (int j = 0; argv[j]; j++) { - free((char*)argv[j]); + free((void*)(uintptr_t)argv[j]); } free(argv); } @@ -2961,7 +2969,7 @@ Bool window_should_start_fullscreen(Window w) return False; } -int xerr(Display *dpy, XErrorEvent *ee) +int xerr(Display *d, XErrorEvent *ee) { /* ignore noise & non fatal errors */ const struct { @@ -2980,7 +2988,7 @@ int xerr(Display *dpy, XErrorEvent *ee) } return 0; - (void)dpy; + (void)d; (void)ee; } |
