1 /* Copyright ©2004-2006 Anselm R. Garbe <garbeam at gmail dot com>
2 * Copyright ©2006-2007 Kris Maglione <fbsdaemon@gmail.com>
3 * See LICENSE file for license details.
4 */
5 #include "dat.h"
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include "fns.h"
10
11 static Bool
is_empty(View * v)12 is_empty(View *v) {
13 Area *a;
14 for(a=v->area; a; a=a->next)
15 if(a->frame)
16 return False;
17 return True;
18 }
19
20 Frame *
view_clientframe(View * v,Client * c)21 view_clientframe(View *v, Client *c) {
22 Frame *f;
23
24 for(f=c->frame; f; f=f->cnext)
25 if(f->area->view == v)
26 break;
27 return f;
28 }
29
30 static void
assign_sel_view(View * v)31 assign_sel_view(View *v) {
32 if(screen->sel != v) {
33 if(screen->sel)
34 write_event("UnfocusTag %s\n",screen->sel->name);
35 screen->sel = v;
36 write_event("FocusTag %s\n", screen->sel->name);
37 }
38 }
39
40 Client *
view_selclient(View * v)41 view_selclient(View *v) {
42 return v->sel && v->sel->sel ? v->sel->sel->client : nil;
43 }
44
45 View *
get_view(const char * name)46 get_view(const char *name) {
47 View *v;
48 int cmp;
49
50 SET(cmp);
51 for(v = view; v; v=v->next)
52 if((cmp=strcmp(name, v->name)) >= 0)
53 break;
54 if(!v || cmp != 0)
55 v = create_view(name);
56 return v;
57 }
58
59 View *
create_view(const char * name)60 create_view(const char *name) {
61 static ushort id = 1;
62 View **i, *v;
63
64 v = emallocz(sizeof(View));
65 v->id = id++;
66
67 utflcpy(v->name, name, sizeof(v->name));
68
69 write_event("CreateTag %s\n", v->name);
70 create_area(v, nil, 0);
71 new_column(v, v->area, 0);
72
73 focus_area(v->area->next);
74
75 for(i=&view; *i; i=&(*i)->next)
76 if(strcmp((*i)->name, name) < 0)
77 break;
78 v->next = *i;
79 *i = v;
80
81 if(!screen->sel)
82 assign_sel_view(v);
83 return v;
84 }
85
86 void
destroy_view(View * v)87 destroy_view(View *v) {
88 Area *a;
89 View **i, *tv;
90
91 while((a = v->area->next))
92 destroy_area(a);
93 destroy_area(v->area);
94
95 for(i=&view; *i; i=&(*i)->next)
96 if(*i == v) break;
97 *i = v->next;
98
99 write_event("DestroyTag %s\n", v->name);
100
101 if(v == screen->sel) {
102 for(tv=view; tv; tv=tv->next)
103 if(tv->next == *i) break;
104 if(tv == nil)
105 tv = view;
106 if(tv)
107 focus_view(screen, tv);
108 }
109 free(v);
110 }
111
112 static void
update_frame_selectors(View * v)113 update_frame_selectors(View *v) {
114 Area *a;
115 Frame *f;
116
117 for(a=v->area; a; a=a->next)
118 for(f=a->frame; f; f=f->anext)
119 f->client->sel = f;
120 }
121
122 void
focus_view(WMScreen * s,View * v)123 focus_view(WMScreen *s, View *v) {
124 Frame *f;
125 Client *c;
126
127 USED(s);
128
129 XGrabServer(display);
130
131 assign_sel_view(v);
132 update_frame_selectors(v);
133 update_divs();
134 for(c=client; c; c=c->next)
135 if((f = c->sel)) {
136 if(f->view == v)
137 resize_client(c, &f->r);
138 else {
139 unmap_frame(c);
140 unmap_client(c, IconicState);
141 }
142 }
143
144 restack_view(v);
145 focus_area(v->sel);
146 draw_frames();
147
148 XSync(display, False);
149 XUngrabServer(display);
150 flushevents(EnterWindowMask, False);
151 }
152
153 void
select_view(const char * arg)154 select_view(const char *arg) {
155 char buf[256];
156
157 utflcpy(buf, arg, sizeof(buf));
158 trim(buf, " \t+/");
159
160 if(strlen(buf) == 0)
161 return;
162 if(!strcmp(buf, ".") || !strcmp(buf, ".."))
163 return;
164
165 assign_sel_view(get_view(buf));
166 update_views(); /* performs focus_view */
167 }
168
169 void
attach_to_view(View * v,Frame * f)170 attach_to_view(View *v, Frame *f) {
171 Client *c;
172
173 c = f->client;
174 c->revert = nil;
175 if(c->trans || c->floating || c->fixedsize
176 || c->titleless || c->borderless || c->fullscreen)
177 focus_area(v->area);
178 else if(starting && v->sel->floating)
179 focus_area(v->area->next);
180 attach_to_area(v->sel, f);
181 }
182
183 void
restack_view(View * v)184 restack_view(View *v) {
185 static XWindow *wins;
186 static uint winssz;
187 Divide *d;
188 Frame *f;
189 Client *c;
190 Area *a;
191 uint n, i;
192
193 if(v != screen->sel)
194 return;
195
196 i = 0;
197 for(c = client; c; c = c->next)
198 i++;
199 if(i == 0)
200 return;
201
202 for(a = v->area; a; a = a->next)
203 i++;
204
205 if(i >= winssz) {
206 winssz = 2 * i;
207 wins = erealloc(wins, sizeof(Window) * winssz);
208 }
209
210 n = 0;
211 wins[n++] = screen->barwin->w;
212 for(f = v->area->frame; f; f = f->anext)
213 if(f->client->fullscreen) {
214 n--;
215 break;
216 }
217
218 for(f=v->area->stack; f; f=f->snext)
219 wins[n++] = f->client->framewin->w;
220
221 for(d = divs; d && d->w->mapped; d = d->next)
222 wins[n++] = d->w->w;
223
224 for(a=v->area->next; a; a=a->next)
225 if(a->frame) {
226 wins[n++] = a->sel->client->framewin->w;
227 for(f=a->frame; f; f=f->anext)
228 if(f != a->sel)
229 wins[n++] = f->client->framewin->w;
230 }
231 if(n) {
232 XRaiseWindow(display, wins[0]);
233 XRestackWindows(display, wins, n);
234 }
235 }
236
237 void
scale_view(View * v,int w)238 scale_view(View *v, int w) {
239 uint xoff, numcol;
240 uint minwidth;
241 Area *a;
242 float scale;
243 int wdiff, dx;
244
245 minwidth = Dx(screen->r)/NCOL;
246
247 if(!v->area->next)
248 return;
249
250 numcol = 0;
251 dx = 0;
252 for(a=v->area->next; a; a=a->next) {
253 numcol++;
254 dx += Dx(a->r);
255 }
256
257 scale = (float)w / dx;
258 xoff = 0;
259 for(a=v->area->next; a; a=a->next) {
260 a->r.max.x = xoff + Dx(a->r) * scale;
261 a->r.min.x = xoff;
262 if(!a->next)
263 a->r.max.x = w;
264 xoff = a->r.max.x;
265 }
266
267 /* minwidth can only be respected when there is enough space;
268 * the caller should guarantee this */
269 if(numcol * minwidth > w)
270 return;
271
272 xoff = 0;
273 for(a=v->area->next, numcol--; a; a=a->next, numcol--) {
274 a->r.min.x = xoff;
275 dx = numcol * minwidth;
276
277 if(Dx(a->r) < minwidth)
278 a->r.max.x = xoff + minwidth;
279 else if((wdiff = xoff + Dx(a->r) - w + dx) > 0)
280 a->r.max.x -= wdiff;
281 if(!a->next)
282 a->r.max.x = w;
283 xoff = a->r.max.x;
284 }
285 }
286
287 void
arrange_view(View * v)288 arrange_view(View *v) {
289 uint xoff;
290 Area *a;
291
292 if(!v->area->next)
293 return;
294
295 scale_view(v, Dx(screen->r));
296 xoff = 0;
297 for(a=v->area->next; a; a=a->next) {
298 a->r.min.x = xoff;
299 a->r.min.y = 0;
300 a->r.max.y = screen->brect.min.y;
301 xoff = a->r.max.x;
302 arrange_column(a, False);
303 }
304 if(v == screen->sel)
305 update_divs();
306 }
307
308 Rectangle *
rects_of_view(View * v,uint * num,Frame * ignore)309 rects_of_view(View *v, uint *num, Frame *ignore) {
310 Rectangle *result;
311 Frame *f;
312 int i;
313
314 i = 2;
315 for(f=v->area->frame; f; f=f->anext)
316 i++;
317
318 result = emallocz(i * sizeof(Rectangle));
319
320 i = 0;
321 for(f=v->area->frame; f; f=f->anext)
322 if(f != ignore)
323 result[i++] = f->r;
324 result[i++] = screen->r;
325 result[i++] = screen->brect;
326
327 *num = i;
328 return result;
329 }
330
331 /* XXX: This will need cleanup */
332 uchar *
view_index(View * v)333 view_index(View *v) {
334 Rectangle *r;
335 Frame *f;
336 Area *a;
337 char *buf, *end;
338 uint i;
339
340 buf = buffer;
341 end = buffer+sizeof(buffer)-1;
342 for((a=v->area), (i=0); a && buf < end-1; (a=a->next), i++) {
343 if(a->floating)
344 buf = seprint(buf, end, "# ~ %d %d\n",
345 Dx(a->r), Dy(a->r));
346 else
347 buf = seprint(buf, end, "# %d %d %d\n",
348 i, a->r.min.x, Dx(a->r));
349
350 for(f=a->frame; f && buf < end-1; f=f->anext) {
351 r = &f->r;
352 if(a->floating)
353 buf = seprint(buf, end, "~ %C %d %d %d %d %s\n",
354 f->client,
355 r->min.x, r->min.y,
356 Dx(*r), Dy(*r),
357 f->client->props);
358 else
359 buf = seprint(buf, end, "%d %C %d %d %s\n",
360 i, f->client,
361 r->min.y, Dy(*r),
362 f->client->props);
363 }
364 }
365 return (uchar*)buffer;
366 }
367
368 uchar *
view_ctl(View * v)369 view_ctl(View *v) {
370 Area *a;
371 char *buf, *end;
372 uint i;
373
374 buf = buffer;
375 end = buffer+sizeof(buffer)-1;
376
377 buf = seprint(buf, end, "%s\n", v->name);
378
379 /* select <area>[ <frame>] */
380 buf = seprint(buf, end, "select %s", area_name(v->sel));
381 if(v->sel->sel)
382 buf = seprint(buf, end, " %d", frame_idx(v->sel->sel));
383 buf = seprint(buf, end, "\n");
384
385 /* select client <client> */
386 if(v->sel->sel)
387 buf = seprint(buf, end, "select client %C\n", v->sel->sel->client);
388
389 for(a = v->area->next, i = 1; a && buf < end-1; a = a->next, i++) {
390 buf = seprint(buf, end, "colmode %d %s\n",
391 i, colmode2str(a->mode));
392 }
393 return (uchar*)buffer;
394 }
395
396 void
update_views(void)397 update_views(void) {
398 View *n, *v, *old;
399 Bool found;
400
401 old = screen->sel;
402 for(v=view; v; v=v->next)
403 update_frame_selectors(v);
404
405 found = False;
406 for(v=view; v; v=n) {
407 n=v->next;
408 if(v != old) {
409 if(is_empty(v))
410 destroy_view(v);
411 else
412 found = True;
413 }
414 }
415
416 if(found && !strcmp(old->name, "nil") && is_empty(old))
417 destroy_view(old);
418 focus_view(screen, screen->sel);
419 }
420
421 uint
newcolw(View * v,int num)422 newcolw(View *v, int num) {
423 Rule *r;
424 ulong n;
425
426 for(r=def.colrules.rule; r; r=r->next)
427 if(regexec(r->regex, v->name, nil, 0)) {
428 char buf[sizeof r->value];
429 char *toks[16];
430
431 strcpy(buf, r->value);
432
433 n = tokenize(toks, 16, buf, '+');
434 if(num < n)
435 if(getulong(toks[num], &n))
436 return Dx(screen->r) * (n / 100.0);
437 break;
438 }
439 return 0;
440 }
441