renamed floating into swimming (this does not clash with C naming conventions and...
[dwm.git] / screen.c
1 /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
2  * See LICENSE file for license details.
3  */
4 #include "dwm.h"
5 #include <regex.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/types.h>
10 #include <X11/Xutil.h>
11
12 unsigned int master = MASTER;
13 unsigned int nmaster = NMASTER;
14 unsigned int blw = 0;
15 Layout *lt = NULL;
16
17 /* static */
18
19 typedef struct {
20         const char *prop;
21         const char *tags;
22         Bool swimming;
23 } Rule;
24
25 typedef struct {
26         regex_t *propregex;
27         regex_t *tagregex;
28 } Regs;
29
30 TAGS
31 RULES
32
33 static Regs *regs = NULL;
34 static unsigned int nrules = 0;
35 static unsigned int nlayouts = 0;
36
37 static void
38 tile(void) {
39         unsigned int i, n, nx, ny, nw, nh, mw, mh, tw, th;
40         Client *c;
41
42         for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
43                 n++;
44         /* window geoms */
45         mh = (n > nmaster) ? wah / nmaster : wah / (n > 0 ? n : 1);
46         mw = (n > nmaster) ? (waw * master) / 1000 : waw;
47         th = (n > nmaster) ? wah / (n - nmaster) : 0;
48         tw = waw - mw;
49
50         for(i = 0, c = clients; c; c = c->next)
51                 if(isvisible(c)) {
52                         if(c->isbanned)
53                                 XMoveWindow(dpy, c->win, c->x, c->y);
54                         c->isbanned = False;
55                         if(c->swimming)
56                                 continue;
57                         c->ismax = False;
58                         nx = wax;
59                         ny = way;
60                         if(i < nmaster) {
61                                 ny += i * mh;
62                                 nw = mw - 2 * BORDERPX;
63                                 nh = mh - 2 * BORDERPX;
64                         }
65                         else {  /* tile window */
66                                 nx += mw;
67                                 nw = tw - 2 * BORDERPX;
68                                 if(th > 2 * BORDERPX) {
69                                         ny += (i - nmaster) * th;
70                                         nh = th - 2 * BORDERPX;
71                                 }
72                                 else /* fallback if th <= 2 * BORDERPX */
73                                         nh = wah - 2 * BORDERPX;
74                         }
75                         resize(c, nx, ny, nw, nh, False);
76                         i++;
77                 }
78                 else {
79                         c->isbanned = True;
80                         XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
81                 }
82         if(!sel || !isvisible(sel)) {
83                 for(c = stack; c && !isvisible(c); c = c->snext);
84                 focus(c);
85         }
86         restack();
87 }
88
89 LAYOUTS
90
91 /* extern */
92
93 void
94 compileregs(void) {
95         unsigned int i;
96         regex_t *reg;
97
98         if(regs)
99                 return;
100         nrules = sizeof rule / sizeof rule[0];
101         regs = emallocz(nrules * sizeof(Regs));
102         for(i = 0; i < nrules; i++) {
103                 if(rule[i].prop) {
104                         reg = emallocz(sizeof(regex_t));
105                         if(regcomp(reg, rule[i].prop, REG_EXTENDED))
106                                 free(reg);
107                         else
108                                 regs[i].propregex = reg;
109                 }
110                 if(rule[i].tags) {
111                         reg = emallocz(sizeof(regex_t));
112                         if(regcomp(reg, rule[i].tags, REG_EXTENDED))
113                                 free(reg);
114                         else
115                                 regs[i].tagregex = reg;
116                 }
117         }
118 }
119
120 void
121 incnmaster(Arg *arg) {
122         if((lt->arrange != tile) || (nmaster + arg->i < 1)
123         || (wah / (nmaster + arg->i) <= 2 * BORDERPX))
124                 return;
125         nmaster += arg->i;
126         if(sel)
127                 lt->arrange();
128         else
129                 drawstatus();
130 }
131
132 void
133 initlayouts(void) {
134         unsigned int i, w;
135
136         lt = &layout[0];
137         nlayouts = sizeof layout / sizeof layout[0];
138         for(blw = i = 0; i < nlayouts; i++) {
139                 w = textw(layout[i].symbol);
140                 if(w > blw)
141                         blw = w;
142         }
143 }
144
145 Bool
146 isvisible(Client *c) {
147         unsigned int i;
148
149         for(i = 0; i < ntags; i++)
150                 if(c->tags[i] && seltag[i])
151                         return True;
152         return False;
153 }
154
155 void
156 resizemaster(Arg *arg) {
157         if(lt->arrange != tile)
158                 return;
159         if(arg->i == 0)
160                 master = MASTER;
161         else {
162                 if(waw * (master + arg->i) / 1000 >= waw - 2 * BORDERPX
163                 || waw * (master + arg->i) / 1000 <= 2 * BORDERPX)
164                         return;
165                 master += arg->i;
166         }
167         lt->arrange();
168 }
169
170 void
171 restack(void) {
172         Client *c;
173         XEvent ev;
174
175         drawstatus();
176         if(!sel)
177                 return;
178         if(sel->swimming || lt->arrange == swim)
179                 XRaiseWindow(dpy, sel->win);
180         if(lt->arrange != swim) {
181                 if(!sel->swimming)
182                         XLowerWindow(dpy, sel->win);
183                 for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
184                         if(c == sel)
185                                 continue;
186                         XLowerWindow(dpy, c->win);
187                 }
188         }
189         XSync(dpy, False);
190         while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
191 }
192
193 void
194 settags(Client *c, Client *trans) {
195         char prop[512];
196         unsigned int i, j;
197         regmatch_t tmp;
198         Bool matched = trans != NULL;
199         XClassHint ch = { 0 };
200
201         if(matched)
202                 for(i = 0; i < ntags; i++)
203                         c->tags[i] = trans->tags[i];
204         else {
205                 XGetClassHint(dpy, c->win, &ch);
206                 snprintf(prop, sizeof prop, "%s:%s:%s",
207                                 ch.res_class ? ch.res_class : "",
208                                 ch.res_name ? ch.res_name : "", c->name);
209                 for(i = 0; i < nrules; i++)
210                         if(regs[i].propregex && !regexec(regs[i].propregex, prop, 1, &tmp, 0)) {
211                                 c->swimming = rule[i].swimming;
212                                 for(j = 0; regs[i].tagregex && j < ntags; j++) {
213                                         if(!regexec(regs[i].tagregex, tags[j], 1, &tmp, 0)) {
214                                                 matched = True;
215                                                 c->tags[j] = True;
216                                         }
217                                 }
218                         }
219                 if(ch.res_class)
220                         XFree(ch.res_class);
221                 if(ch.res_name)
222                         XFree(ch.res_name);
223         }
224         if(!matched)
225                 for(i = 0; i < ntags; i++)
226                         c->tags[i] = seltag[i];
227 }
228
229 void
230 swim(void) {
231         Client *c;
232
233         for(c = clients; c; c = c->next) {
234                 if(isvisible(c)) {
235                         if(c->isbanned)
236                                 XMoveWindow(dpy, c->win, c->x, c->y);
237                         c->isbanned = False;
238                         resize(c, c->x, c->y, c->w, c->h, True);
239                 }
240                 else {
241                         c->isbanned = True;
242                         XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
243                 }
244         }
245         if(!sel || !isvisible(sel)) {
246                 for(c = stack; c && !isvisible(c); c = c->snext);
247                 focus(c);
248         }
249         restack();
250 }
251
252 void
253 tag(Arg *arg) {
254         unsigned int i;
255
256         if(!sel)
257                 return;
258         for(i = 0; i < ntags; i++)
259                 sel->tags[i] = (arg->i == -1) ? True : False;
260         if(arg->i >= 0 && arg->i < ntags)
261                 sel->tags[arg->i] = True;
262         lt->arrange();
263 }
264
265 void
266 toggleswimming(Arg *arg) {
267         if(!sel || lt->arrange == swim)
268                 return;
269         sel->swimming = !sel->swimming;
270         lt->arrange();
271 }
272
273 void
274 toggletag(Arg *arg) {
275         unsigned int i;
276
277         if(!sel)
278                 return;
279         sel->tags[arg->i] = !sel->tags[arg->i];
280         for(i = 0; i < ntags && !sel->tags[i]; i++);
281         if(i == ntags)
282                 sel->tags[arg->i] = True;
283         lt->arrange();
284 }
285
286 void
287 togglelayout(Arg *arg) {
288         unsigned int i;
289
290         for(i = 0; i < nlayouts && lt != &layout[i]; i++);
291         if(i == nlayouts - 1)
292                 lt = &layout[0];
293         else
294                 lt = &layout[++i];
295         if(sel)
296                 lt->arrange();
297         else
298                 drawstatus();
299 }
300
301 void
302 toggleview(Arg *arg) {
303         unsigned int i;
304
305         seltag[arg->i] = !seltag[arg->i];
306         for(i = 0; i < ntags && !seltag[i]; i++);
307         if(i == ntags)
308                 seltag[arg->i] = True; /* cannot toggle last view */
309         lt->arrange();
310 }
311
312 void
313 view(Arg *arg) {
314         unsigned int i;
315
316         for(i = 0; i < ntags; i++)
317                 seltag[i] = (arg->i == -1) ? True : False;
318         if(arg->i >= 0 && arg->i < ntags)
319                 seltag[arg->i] = True;
320         lt->arrange();
321 }