summaryrefslogtreecommitdiffstats
path: root/dwl-patches/patches/singletagset/singletagset-v0.6.patch
blob: 708774b80d1337f3a543116c1f84a538bd99d18e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
From 140fe587bf34cf43e44acaf365b16ec3e385f742 Mon Sep 17 00:00:00 2001
From: Ben Collerson <benc@benc.cc>
Date: Thu, 16 May 2024 09:56:58 +1000
Subject: [PATCH] singletagset

---
 dwl.c | 150 ++++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 140 insertions(+), 10 deletions(-)

diff --git a/dwl.c b/dwl.c
index bf763dfc..5b1d594f 100644
--- a/dwl.c
+++ b/dwl.c
@@ -72,6 +72,7 @@
 #define ROUND(X)                ((int)((X < 0) ? (X - 0.5) : (X + 0.5)))
 #define CLEANMASK(mask)         (mask & ~WLR_MODIFIER_CAPS)
 #define VISIBLEON(C, M)         ((M) && (C)->mon == (M) && ((C)->tags & (M)->tagset[(M)->seltags]))
+#define SVISIBLEON(C, M)        ((M) && (C)->mon && ((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)
@@ -246,6 +247,7 @@ static void arrange(Monitor *m);
 static void arrangelayer(Monitor *m, struct wl_list *list,
 		struct wlr_box *usable_area, int exclusive);
 static void arrangelayers(Monitor *m);
+static void attachclients(Monitor *m);
 static void axisnotify(struct wl_listener *listener, void *data);
 static void buttonpress(struct wl_listener *listener, void *data);
 static void chvt(const Arg *arg);
@@ -283,6 +285,7 @@ static void focusmon(const Arg *arg);
 static void focusstack(const Arg *arg);
 static Client *focustop(Monitor *m);
 static void fullscreennotify(struct wl_listener *listener, void *data);
+static size_t getunusedtag(void);
 static void handlesig(int signo);
 static void incnmaster(const Arg *arg);
 static void inputdevice(struct wl_listener *listener, void *data);
@@ -470,7 +473,18 @@ applyrules(Client *c)
 			}
 		}
 	}
+	wlr_scene_node_reparent(&c->scene->node, layers[c->isfloating ? LyrFloat : LyrTile]);
+
+	wl_list_for_each(m, &mons, link) {
+		// tag with different monitor selected by rules
+		if (m->tagset[m->seltags] & newtags) {
+			mon = m;
+			break;
+		}
+	}
+
 	setmon(c, mon, newtags);
+	attachclients(mon);
 }
 
 void
@@ -558,6 +572,45 @@ arrangelayers(Monitor *m)
 	}
 }
 
+void
+attachclients(Monitor *m)
+{
+	Monitor *tm;
+	unsigned int utags = 0;
+	Client *c;
+	int rmons = 0;
+
+	if (m == NULL) {
+		return;
+	}
+
+	wl_list_for_each(tm, &mons, link) {
+		if (tm != m) {
+			utags |= tm->tagset[tm->seltags];
+		}
+	}
+
+	wl_list_for_each(c, &clients, link) {
+		if (SVISIBLEON(c, m)) {
+			/* if client is also visible on other tags that are displayed on
+			 * other monitors, remove these tags */
+			if (c->tags & utags) {
+				c->tags = c->tags & m->tagset[m->seltags];
+				rmons = 1;
+			}
+			setmon(c, m, c->tags);
+		}
+	}
+
+	if (rmons) {
+		wl_list_for_each(tm, &mons, link) {
+			if (tm != m) {
+				arrange(tm);
+			}
+		}
+	}
+}
+
 void
 axisnotify(struct wl_listener *listener, void *data)
 {
@@ -881,7 +934,7 @@ createmon(struct wl_listener *listener, void *data)
 
 	wlr_output_state_init(&state);
 	/* Initialize monitor state using configured rules */
-	m->tagset[0] = m->tagset[1] = 1;
+	m->tagset[0] = m->tagset[1] = (1<<getunusedtag()) & TAGMASK;
 	for (r = monrules; r < END(monrules); r++) {
 		if (!r->name || strstr(wlr_output->name, r->name)) {
 			m->m.x = r->x;
@@ -1354,7 +1407,7 @@ focustop(Monitor *m)
 {
 	Client *c;
 	wl_list_for_each(c, &fstack, flink) {
-		if (VISIBLEON(c, m))
+		if (SVISIBLEON(c, m))
 			return c;
 	}
 	return NULL;
@@ -1367,6 +1420,29 @@ fullscreennotify(struct wl_listener *listener, void *data)
 	setfullscreen(c, client_wants_fullscreen(c));
 }
 
+size_t
+getunusedtag(void)
+{
+	size_t i = 0;
+	Monitor *m;
+	if (wl_list_empty(&mons)) {
+		return i;
+	}
+	for (i=0; i < TAGCOUNT; i++) {
+		int is_used = 0;
+		wl_list_for_each(m, &mons, link) {
+			if ((m->tagset[m->seltags] & (1<<i))) {
+				is_used = 1;
+			}
+		}
+
+		if (!is_used) {
+			return i;
+		}
+	}
+	return i;
+}
+
 void
 handlesig(int signo)
 {
@@ -1915,8 +1991,6 @@ printstatus(void)
 	wl_list_for_each(m, &mons, link) {
 		occ = urg = 0;
 		wl_list_for_each(c, &clients, link) {
-			if (c->mon != m)
-				continue;
 			occ |= c->tags;
 			if (c->isurgent)
 				urg |= c->tags;
@@ -2551,6 +2625,7 @@ startdrag(struct wl_listener *listener, void *data)
 void
 tag(const Arg *arg)
 {
+	Monitor *m;
 	Client *sel = focustop(selmon);
 	if (!sel || (arg->ui & TAGMASK) == 0)
 		return;
@@ -2558,15 +2633,25 @@ tag(const Arg *arg)
 	sel->tags = arg->ui & TAGMASK;
 	focusclient(focustop(selmon), 1);
 	arrange(selmon);
+	wl_list_for_each(m, &mons, link) {
+		attachclients(m);
+		arrange(m);
+	}
 	printstatus();
 }
 
 void
 tagmon(const Arg *arg)
 {
+	Monitor *m;
 	Client *sel = focustop(selmon);
-	if (sel)
+	if (sel) {
 		setmon(sel, dirtomon(arg->i), 0);
+		wl_list_for_each(m, &mons, link) {
+			arrange(m);
+		}
+		focusclient(focustop(sel->mon), 1);
+	}
 }
 
 void
@@ -2623,12 +2708,18 @@ togglefullscreen(const Arg *arg)
 void
 toggletag(const Arg *arg)
 {
+	Monitor *m;
 	uint32_t newtags;
 	Client *sel = focustop(selmon);
 	if (!sel || !(newtags = sel->tags ^ (arg->ui & TAGMASK)))
 		return;
 
+	wl_list_for_each(m, &mons, link)
+		if (m != selmon && newtags & m->tagset[m->seltags])
+			return;
+
 	sel->tags = newtags;
+	attachclients(selmon);
 	focusclient(focustop(selmon), 1);
 	arrange(selmon);
 	printstatus();
@@ -2637,11 +2728,17 @@ toggletag(const Arg *arg)
 void
 toggleview(const Arg *arg)
 {
+	Monitor *m;
 	uint32_t newtagset;
 	if (!(newtagset = selmon ? selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK) : 0))
 		return;
 
+	wl_list_for_each(m, &mons, link)
+		if (m !=selmon && newtagset & m->tagset[m->seltags])
+			return;
+
 	selmon->tagset[selmon->seltags] = newtagset;
+	attachclients(selmon);
 	focusclient(focustop(selmon), 1);
 	arrange(selmon);
 	printstatus();
@@ -2759,6 +2856,7 @@ updatemons(struct wl_listener *listener, void *data)
 			wlr_session_lock_surface_v1_configure(m->lock_surface, m->m.width, m->m.height);
 		}
 
+		attachclients(m);
 		/* Calculate the effective monitor geometry to use for clients */
 		arrangelayers(m);
 		/* Don't move clients to the left output when plugging monitors */
@@ -2825,13 +2923,45 @@ urgent(struct wl_listener *listener, void *data)
 void
 view(const Arg *arg)
 {
-	if (!selmon || (arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
+	Monitor *m, *origm = selmon;
+	unsigned int newtags;
+
+	if (!selmon || (arg->ui & TAGMASK) == selmon->tagset[selmon->seltags]) {
 		return;
-	selmon->seltags ^= 1; /* toggle sel tagset */
+	}
+
+	newtags = origm->tagset[origm->seltags ^ 1];
+
+	/* swap tags when trying to display a tag from another monitor */
+	if (arg->ui & TAGMASK) {
+		newtags = arg->ui & TAGMASK;
+	}
+	wl_list_for_each(m, &mons, link) {
+		if (m != origm && newtags & m->tagset[m->seltags]) {
+			/* prevent displaying all tags (MODKEY-0) when multiple monitors
+			 * are connected */
+			if (newtags & origm->tagset[origm->seltags]) {
+				return;
+			}
+			m->seltags ^= 1;
+			m->tagset[m->seltags] = origm->tagset[origm->seltags];
+			attachclients(m);
+			/* Beware: this changes selmon */
+			focusclient(focustop(m), 1);
+			arrange(m);
+			break;
+		}
+	}
+
+	origm->seltags ^= 1; /* toggle sel tagset */
 	if (arg->ui & TAGMASK)
-		selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
-	focusclient(focustop(selmon), 1);
-	arrange(selmon);
+		origm->tagset[origm->seltags] = arg->ui & TAGMASK;
+
+	/* Change selmon back to orig mon */
+	selmon = origm;
+	attachclients(origm);
+	focusclient(focustop(origm), 1);
+	arrange(origm);
 	printstatus();
 }
 
-- 
2.43.0