-
Notifications
You must be signed in to change notification settings - Fork 0
/
viewstack.c
260 lines (229 loc) · 4.84 KB
/
viewstack.c
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
Bool
hasclientson(Monitor *m, unsigned int tagset) {
Client *c;
unsigned int nc;
for(c = m->clients, nc = 0; c; c = c->next)
if(!c->nofocus && (c->tags & tagset) && c->tags != TAGMASK)
++nc;
return nc > 0;
}
Bool
movetostacktop(Monitor *m, unsigned int ui) {
ViewStack *v = m->vs;
ViewStack *vprev = NULL;
while (v) {
if (v->tagset == ui) {
if (v != m->vs) {
if (vprev)
vprev->next = v->next;
v->next = m->vs;
m->vs = v;
}
return True;
}
else {
vprev = v;
v = v->next;
}
}
return False;
}
void
copyviewstack(ViewStack *vdst, const ViewStack *vsrc) {
int i;
vdst->tagset = vsrc->tagset;
vdst->curlt = vsrc->curlt;
vdst->lt[0] = vsrc->lt[0];
vdst->lt[1] = vsrc->lt[1];
vdst->mfact = vsrc->mfact;
vdst->msplit = vsrc->msplit;
for (i = 0; i < 3; ++i)
vdst->ltaxis[i] = vsrc->ltaxis[i];
vdst->showbar = vsrc->showbar;
vdst->showdock = vsrc->showdock;
}
int
tagcount(unsigned int ui) {
int n, i;
for(n = 0, i = 0; i < numtags; ++i)
if(ui & (1 << i))
++n;
return n;
}
ViewStack*
createviewstack (const ViewStack *vref, unsigned int ui) {
ViewStack *v = (ViewStack*)calloc(1, sizeof(ViewStack));
int i;
if (vref != NULL)
copyviewstack(v, vref);
else {
v->lt[0] = &layouts[initlayout];
v->lt[1] = &layouts[FLOAT];
v->showbar = ui == vtag ? False : showbar;
v->showdock = showdock;
v->mfact = mfact;
for (i = 0; i < 3; ++i)
v->ltaxis[i] = layoutaxis[i];
}
v->tagset = ui;
if(v->lt[0] == &layouts[VARIMONO]) {
v->msplit = tagcount(ui);
if(v->msplit == 0)
v->msplit = 1;
else if(v->msplit > 2)
v->msplit = 2;
}
else
v->msplit = 1;
return v;
}
ViewStack*
getviewstackof(Monitor* m, const unsigned int tagset) {
ViewStack **pvs;
for(pvs = &m->vs; *pvs && (*pvs)->tagset != tagset; pvs = &(*pvs)->next) ;
if(!*pvs)
*pvs = createviewstack(m->vs, tagset);
return *pvs;
}
void
moveviewstacksecond(Monitor* m, unsigned int ui) {
movetostacktop(m, ui);
if(m->vs->next)
movetostacktop(m, m->vs->next->tagset);
}
void
cleanupviewstack(ViewStack *v) {
ViewStack *vnext;
while(v) {
vnext = v->next;
free(v);
v = vnext;
}
}
void
movetoptoend(Monitor *m) {
ViewStack** pv;
for(pv = &m->vs; *pv; pv = &(*pv)->next) ;
*pv = m->vs;
m->vs = m->vs->next;
(*pv)->next = NULL;
}
void
viewstackadd(Monitor *m, unsigned int ui, Bool newview) {
ViewStack* v;
ViewStack* vref = m->vs;
if (!newview) {
movetoptoend(m);
}
if (!movetostacktop(m, ui)) {
v = createviewstack(newview ? NULL : vref, ui);
v->next = m->vs;
m->vs = v;
}
}
void
storestackviewlayout (Monitor *m, unsigned int ui, const Layout* lt, Bool showdock) {
ViewStack *v;
ViewStack **pv = &m->vs;
for(v = m->vs; v && v->tagset != ui; pv = &v->next, v = v->next) ;
if (!v) {
*pv = createviewstack(m->vs, ui);
v = *pv;
}
if (v->lt[v->curlt] != lt)
v->curlt ^= 1;
v->lt[v->curlt] = lt;
v->showdock = showdock;
}
void
rewindstack (const Arg *arg) {
ViewStack *v;
if (selmon->vs && selmon->vs->next) {
v = selmon->vs;
free(selmon->vs);
selmon->vs = v;
arrange(selmon);
}
}
void
settagsetlayout(Monitor* m, unsigned int tagset, const Layout *lt)
{
ViewStack *vs = getviewstackof(m, tagset);
if (lt != vs->lt[vs->curlt])
vs->curlt ^= 1;
vs->lt[vs->curlt] = lt;
}
#if DEBUG_TAGSETS
void
printtagset (unsigned int ui) {
int i;
for(i = 0; i < numtags; ++i) {
if (ui & (1 << i))
fprintf(stderr, "%d ", i);
}
fprintf(stderr, "\n");
}
#endif
unsigned int
findtoggletagset (Monitor *m) {
unsigned int toggletags = m->vs->tagset;
unsigned int curseltags = m->vs->tagset;
unsigned int tag = 0;
ViewStack* v = m->vs;
#if DEBUG_TAGSETS
fprintf(stderr, "stack:\n");
while (v) {
printtagset(v->tagset);
v = v->next;
}
#endif
/* rewind stack for different non-empty tagset that share some tags */
v = m->vs;
while (toggletags == curseltags && v) {
if ((v->tagset & curseltags) == 0 && hasclientson(m, v->tagset))
{
toggletags = v->tagset;
}
v = v->next;
}
/* rewind stack for different non-empty tagset that share some tags */
v = m->vs;
while (toggletags == curseltags && v) {
if (v->tagset != curseltags && hasclientson(m, v->tagset))
{
toggletags = v->tagset;
}
v = v->next;
}
/* try any tag different than current */
while (toggletags == curseltags && tag < numtags) {
if (((1 << tag) & curseltags) == 0 && hasclientson(m, 1 << tag))
{
toggletags = (1 << tag);
}
++tag;
}
/* rewind stack for tagset (empty) with no tags in common */
v = m->vs;
while (toggletags == curseltags && v) {
if ((v->tagset & curseltags) == 0)
{
toggletags = v->tagset;
}
v = v->next;
}
/* rewind stack for different tagset (empty) that share some tags */
v = m->vs;
while (toggletags == curseltags && v) {
if (v->tagset != curseltags)
{
toggletags = v->tagset;
}
v = v->next;
}
return toggletags;
}
Bool
hasvclients (Monitor *m) {
return hasclientson(m, vtag);
}