commit fe4502f

wf  ·  2026-05-02 12:57:41 +0000 UTC
parent 4ec0b1e
Add window title formatting
4 files changed,  +59, -10
+1, -0
1@@ -34,6 +34,7 @@ enum cmd {
2 	cmd_outer_unfocus_color,
3 	cmd_inner_border_width,
4 	cmd_outer_border_width,
5+	cmd_title_format,
6 	cmd_set_decor,
7 	cmd_quit,
8 	cmd_config,
+7, -6
 1@@ -49,6 +49,7 @@ static const struct command commands[] = {
 2 	{ "outer_unfocus_color", cmd_outer_unfocus_color, 1, true  },
 3 	{ "inner_border_width",  cmd_inner_border_width,  1, true  },
 4 	{ "outer_border_width",  cmd_outer_border_width,  1, true  },
 5+	{ "title_format",        cmd_title_format,        1, true  },
 6 	{ "set_decor",           cmd_set_decor,           1, true  },
 7 	{ "quit",                cmd_quit,                0, false }
 8 };
 9@@ -119,16 +120,16 @@ ipc_msg(const struct command *cmd, int argc, char **argv) {
10 	}
11 
12 	if (n > 0) {
13-		char *msg;
14-		long ret = strtol(resp, &msg, 10);
15-		if (msg == resp) {
16+		char *msg2;
17+		long ret = strtol(resp, &msg2, 10);
18+		if (msg2 == resp) {
19 			ret = 0;
20 		}
21 
22-		while (*msg == ' ') msg++;
23+		while (*msg2 == ' ') msg2++;
24 
25-		if (strcmp(msg, "") != 0)
26-			printf("%s", msg);
27+		if (strcmp(msg2, "") != 0)
28+			printf("%s", msg2);
29 		return (int)ret;
30 	}
31 
+48, -4
 1@@ -254,7 +254,7 @@ setup(void) {
 2 	decor->padding    = 0;
 3 	decor->offset_x   = 0;
 4 	decor->offset_y   = 0;
 5-	decor->fontname   = "sans-serif:size=10";
 6+	decor->fontname   = "sans-serif:size=11";
 7 
 8 	wm.loop = wl_display_get_event_loop(wm.dpy);
 9 	if (!swc_initialize(wm.dpy, wm.loop, &mgr))
10@@ -325,6 +325,52 @@ bind_handler(void *data, uint32_t time, uint32_t value, uint32_t state) {
11 	}
12 }
13 
14+static const char *
15+title_format(struct client *target, char *fmt) {
16+	static char buf[MAXSIZE];
17+	size_t oi = 0;
18+
19+	if (!fmt || !target || !target->win) {
20+		buf[0] = '\0';
21+		return buf;
22+	}
23+
24+	for (size_t i = 0; fmt[i] != '\0' && oi + 1 < MAXSIZE; ++i) {
25+		if (fmt[i] != '%') {
26+			buf[oi++] = fmt[i];
27+			continue;
28+		}
29+
30+		++i;
31+		if (fmt[i] == '\0')
32+			break;
33+		char c = fmt[i];
34+
35+		if (c == '%') {
36+			if (oi + 1 < MAXSIZE) buf[oi++] = '%';
37+		} else if (c == 't' || c == 'a') {
38+			const char *t = (c == 't') ? target->win->title : target->win->app_id;
39+			if (!t) continue;
40+			while (*t && oi + 1 < MAXSIZE)
41+				buf[oi++] = *t++;
42+		} else if (c == 'p') {
43+			int pid = swc_window_get_pid(target->win);
44+			int n = snprintf(buf + oi, MAXSIZE - oi, "%d", pid);
45+			if (n > 0)
46+				oi += (size_t)(n < (int)(MAXSIZE - oi) ? n : (int)(MAXSIZE - oi - 1));
47+		} else {
48+			if (oi + 2 < MAXSIZE) {
49+				buf[oi++] = '%';
50+				buf[oi++] = c;
51+			} else
52+				break;
53+		}
54+	}
55+
56+	buf[oi] = '\0';
57+	return buf;
58+}
59+
60 void
61 decorate(struct client *c, bool focus) {
62 	if (!c)
63@@ -346,8 +392,7 @@ decorate(struct client *c, bool focus) {
64 		.bottom_right = focus ? decor->active.bottom_right : decor->inactive.bottom_right
65 	};
66 
67-	// const char *title = title_format(config.tf);
68-	const char *title = c->win->title;
69+	const char *title = title_format(c, config.tf);
70 	struct swc_decor new = {
71 		.color = decor->background,
72 		.left = decor->edge_left,
73@@ -369,7 +414,6 @@ decorate(struct client *c, bool focus) {
74 	};
75 
76 	swc_window_set_decor(c->win, &new);
77-	_inf("decorated c=%p focus=%d", (void*)c, focus);
78 }
79 
80 void
+3, -0
 1@@ -483,6 +483,9 @@ ipc_config(char **arg) {
 2 		case cmd_outer_border_width:
 3 			config.ob_width = fn_int(arg[2]);
 4 			break;
 5+		case cmd_title_format:
 6+			config.tf = arg[2];
 7+			break;
 8 		case cmd_set_decor:
 9 			load_decor(arg[2]);
10 			break;