commit 13ef4c7

wf  ·  2026-05-09 13:38:16 +0000 UTC
parent 597db23
Clean up code
4 files changed,  +90, -105
+1, -1
1@@ -1,6 +1,6 @@
2 CC         ?= cc
3 PKG_CONFIG ?= pkg-config
4-CFLAGS     += -std=c99 -Wall -Wextra -Wshadow -Oz -pedantic `pkg-config --cflags swc spng`
5+CFLAGS     += -std=c99 -Wall -Wextra -Wshadow -Wno-unused-parameter -Oz -pedantic `pkg-config --cflags swc spng`
6 LDFLAGS    += -Iinclude
7 LDLIBS     := `${PKG_CONFIG} --libs swc spng`
8 PREFIX     ?= /usr/local
+0, -1
1@@ -3,7 +3,6 @@
2 
3 #include <stdint.h>
4 
5-#define UNUSED(x)  (void)x
6 #define SOCK_PATH  "/tmp/.howl.sock"
7 #define MAXSIZE    256
8 
+87, -85
  1@@ -108,8 +108,6 @@ static const cmd_handler_t cmd_handler [cmd_last] = {
  2 
  3 static int
  4 ipc_handler(int fd, uint32_t mask, void *data) {
  5-	UNUSED(data);
  6-
  7 	if (mask & WL_EVENT_HANGUP) {
  8 		/* conn. closed */
  9 		_wrn("IPC socket connection closed prematurely: %s", strerror(errno));
 10@@ -123,69 +121,70 @@ ipc_handler(int fd, uint32_t mask, void *data) {
 11 	}
 12 
 13 	if (mask & WL_EVENT_READABLE) {
 14-		if (fd == sfd) {
 15-			int afd = accept(sfd, NULL, NULL);
 16-			if (afd == -1) {
 17-				_wrn("couldn't accept incoming connection on IPC socket: %s", strerror(errno));
 18-				goto end;
 19-			}
 20-
 21-			char buf[MAXSIZE];
 22-			ssize_t n;
 23+		if (fd != sfd)
 24+			return 0;
 25 
 26-			if ((n = read(afd, buf, sizeof(buf) - 1)) < 0) {
 27-				/* read error */
 28-				_wrn("couldn't read from IPC socket: %s", strerror(errno));
 29-				goto end;
 30-			}
 31+		int afd = accept(sfd, NULL, NULL);
 32+		if (afd == -1) {
 33+			_wrn("couldn't accept incoming connection on IPC socket: %s", strerror(errno));
 34+			goto end;
 35+		}
 36 
 37-			if (n == 0) {
 38-				/* EOF */
 39-				_wrn("unexpected EOF on IPC socket: %s", strerror(errno));
 40-				goto end;
 41-			}
 42+		char buf[MAXSIZE];
 43+		ssize_t n;
 44 
 45-			if (n > 0) {
 46-				int argc = 0;
 47-				char *argv[64], *tok = NULL;
 48+		if ((n = read(afd, buf, sizeof(buf) - 1)) < 0) {
 49+			/* read error */
 50+			_wrn("couldn't read from IPC socket: %s", strerror(errno));
 51+			goto end;
 52+		}
 53 
 54-				buf[n] = '\0';
 55-				while (buf[n-1] == '\r' || buf[n-1] == '\n' || buf[n-1] == ' ')
 56-					buf[n-1] = '\0';
 57+		if (n == 0) {
 58+			/* EOF */
 59+			_wrn("unexpected EOF on IPC socket: %s", strerror(errno));
 60+			goto end;
 61+		}
 62 
 63-				tok = strtok(buf, " ");
 64-				while (tok != NULL && argc < 63) {
 65-					argv[argc++] = tok;
 66-					tok = strtok(NULL, " ");
 67-				}
 68-				argv[argc] = NULL;
 69+		if (n > 0) {
 70+			int argc = 0;
 71+			char *argv[64], *tok = NULL;
 72 
 73-				if (argc == 0)
 74-					goto end;
 75+			buf[n] = '\0';
 76+			while (buf[n-1] == '\r' || buf[n-1] == '\n' || buf[n-1] == ' ')
 77+				buf[n-1] = '\0';
 78 
 79-				int cmd = atoi(argv[0]);
 80-				if (cmd < 0 || cmd > cmd_last) {
 81-					_wrn("IPC command index out of bounds");
 82-					goto end;
 83-				}
 84+			tok = strtok(buf, " ");
 85+			while (tok != NULL && argc < 63) {
 86+				argv[argc++] = tok;
 87+				tok = strtok(NULL, " ");
 88+			}
 89+			argv[argc] = NULL;
 90 
 91-				/* TODO: this is hard to debug */
 92-				if (!cmd_handler[cmd]) {
 93-					_wrn("no such command #%d", cmd);
 94-					goto end;
 95-				}
 96+			if (argc == 0)
 97+				goto end;
 98 
 99-				status s = cmd_handler[cmd](argv);
100-				char answer[MAXSIZE];
101-				snprintf(answer, MAXSIZE * sizeof(char), "%d %s", s.ok, s.msg);
102+			int cmd = atoi(argv[0]);
103+			if (cmd < 0 || cmd > cmd_last) {
104+				_wrn("IPC command index out of bounds");
105+				goto end;
106+			}
107 
108-				if (send(afd, answer, strlen(answer), 0) == -1)
109-					_wrn("couldn't send answer to client");
110+			/* TODO: this is hard to debug */
111+			if (!cmd_handler[cmd]) {
112+				_wrn("no such command #%d", cmd);
113+				goto end;
114 			}
115-end:
116-			close(afd);
117-			return 0;
118+
119+			status s = cmd_handler[cmd](argv);
120+			char answer[MAXSIZE];
121+			snprintf(answer, MAXSIZE * sizeof(char), "%d %s", s.ok, s.msg);
122+
123+			if (send(afd, answer, strlen(answer), 0) == -1)
124+				_wrn("couldn't send answer to client");
125 		}
126+end:
127+		close(afd);
128+		return 0;
129 	}
130 
131 	return 0; /* NOTREACHED */
132@@ -217,8 +216,6 @@ setup_ipc(void) {
133 
134 static void
135 sig_handler(int s) {
136-	UNUSED(s);
137-
138 	_wrn("caught deadly signal, exiting!");
139 	cleanup();
140 	if (wm.dpy) {
141@@ -323,9 +320,6 @@ load_config(char *conf_path) {
142 
143 void
144 bind_handler(void *data, uint32_t time, uint32_t value, uint32_t state) {
145-	UNUSED(time);
146-	UNUSED(value);
147-
148 	if (state != WL_KEYBOARD_KEY_STATE_PRESSED)
149 		return;
150 
151@@ -338,50 +332,58 @@ bind_handler(void *data, uint32_t time, uint32_t value, uint32_t state) {
152 }
153 
154 static const char *
155-title_format(struct client *target, char *fmt) {
156+title_format(struct client *c, char *fmt) {
157 	static char buf[MAXSIZE];
158-	size_t oi = 0;
159+	size_t o = 0;
160 
161-	if (!fmt || !target || !target->win) {
162+	if (!fmt || !c || !c->win) {
163 		buf[0] = '\0';
164 		return buf;
165 	}
166 
167-	for (size_t i = 0; fmt[i] != '\0' && oi + 1 < MAXSIZE; ++i) {
168+	for (size_t i = 0; fmt[i] != '\0' && o + 1 < MAXSIZE; ++i) {
169 		if (fmt[i] != '%') {
170-			buf[oi++] = fmt[i];
171+			buf[o++] = fmt[i];
172 			continue;
173 		}
174 
175 		++i;
176 		if (fmt[i] == '\0')
177 			break;
178-		char c = fmt[i];
179-
180-		if (c == '%') {
181-			if (oi + 1 < MAXSIZE)
182-				buf[oi++] = '%';
183-		} else if (c == 't' || c == 'a') {
184-			const char *t = (c == 't') ? target->win->title : target->win->app_id;
185-			if (!t)
186-				continue;
187-			while (*t && oi + 1 < MAXSIZE)
188-				buf[oi++] = *t++;
189-		} else if (c == 'p') {
190-			int pid = swc_window_get_pid(target->win);
191-			int n = snprintf(buf + oi, MAXSIZE - oi, "%d", pid);
192-			if (n > 0)
193-				oi += (size_t)(n < (int)(MAXSIZE - oi) ? n : (int)(MAXSIZE - oi - 1));
194-		} else {
195-			if (oi + 2 < MAXSIZE) {
196-				buf[oi++] = '%';
197-				buf[oi++] = c;
198-			} else
199+		char ch = fmt[i];
200+
201+		switch (ch) {
202+			case '%':
203+				if (o + 1 < MAXSIZE)
204+					buf[o++] = '%';
205+				break;
206+			case 't':
207+				/* FALLTHROUGH */
208+			case 'a': {
209+				const char *s = (ch == 't') ? c->win->title : c->win->app_id;
210+				if (!s)
211+					continue;
212+				while (*s && o + 1 < MAXSIZE)
213+					buf[o++] = *s++;
214+				break;
215+			}
216+			case 'p': {
217+				pid_t pid = swc_window_get_pid(c->win);
218+				int n = snprintf(buf + o, MAXSIZE - o, "%d", pid);
219+				if (n > 0)
220+					o += (size_t)(n < (int)(MAXSIZE - o) ? n : (int)(MAXSIZE - o - 1));
221+				break;
222+			}
223+			default:
224+				if (o + 2 < MAXSIZE) {
225+					buf[o++] = '%';
226+					buf[o++] = ch;
227+				}
228 				break;
229 		}
230 	}
231 
232-	buf[oi] = '\0';
233+	buf[o] = '\0';
234 	return buf;
235 }
236 
+2, -18
 1@@ -271,8 +271,6 @@ ipc_raise(char **arg) {
 2 
 3 status
 4 ipc_focus_prev(char **arg) {
 5-	UNUSED(arg);
 6-
 7 	focus_prev();
 8 
 9 	return (status){ true, "" };
10@@ -280,8 +278,6 @@ ipc_focus_prev(char **arg) {
11 
12 status
13 ipc_focus_next(char **arg) {
14-	UNUSED(arg);
15-
16 	focus_next();
17 
18 	return (status){ true, "" };
19@@ -374,8 +370,6 @@ ipc_get_app_id(char **arg) {
20 
21 status
22 ipc_get_focus(char **arg) {
23-	UNUSED(arg);
24-
25 	status s = {0};
26 
27 	snprintf(s.msg, MAXSIZE * sizeof(char), "%" PRIu32 "\n", wm.cur->id);
28@@ -386,8 +380,6 @@ ipc_get_focus(char **arg) {
29 
30 status
31 ipc_get_workspace(char **arg) {
32-	UNUSED(arg);
33-
34 	status s = {0};
35 
36 	snprintf(s.msg, MAXSIZE * sizeof(char), "%d\n", wm.ws);
37@@ -398,8 +390,6 @@ ipc_get_workspace(char **arg) {
38 
39 status
40 ipc_list_windows(char **arg) {
41-	UNUSED(arg);
42-
43 	status s = {0};
44 
45 	if (!wl_list_empty(&wm.clients)) {
46@@ -415,8 +405,6 @@ ipc_list_windows(char **arg) {
47 
48 status
49 ipc_get_screen_geometry(char **arg) {
50-	UNUSED(arg);
51-
52 	status s = {0};
53 
54 	if (!wm.scr)
55@@ -430,8 +418,6 @@ ipc_get_screen_geometry(char **arg) {
56 
57 status
58 ipc_get_cursor_position(char **arg) {
59-	UNUSED(arg);
60-
61 	status s = {0};
62 
63 	int32_t cx = 0, cy = 0;
64@@ -481,11 +467,11 @@ ipc_config(char **arg) {
65 
66 	enum cmd cmd = fn_int(arg[1]);
67 	switch (cmd) {
68-		case cmd_modkey:
69-			; /* smh */
70+		case cmd_modkey: {
71 			struct bind tmp = fn_bind(arg[2]);
72 			config.mod = tmp.mod;
73 			break;
74+		}
75 		case cmd_inner_focus_color:
76 			config.if_color = fn_hex(arg[2]);
77 			break;
78@@ -538,8 +524,6 @@ ipc_config(char **arg) {
79 
80 status
81 ipc_quit(char **arg) {
82-	UNUSED(arg);
83-
84 	wm.running = false;
85 	swc_finalize();
86 	wl_display_terminate(wm.dpy);