summaryrefslogtreecommitdiffstats
path: root/layout.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--screen.c (renamed from layout.c)218
1 files changed, 191 insertions, 27 deletions
diff --git a/layout.c b/screen.c
index 2763d2c..03387eb 100644
--- a/layout.c
+++ b/screen.c
@@ -1,5 +1,7 @@
1/* See LICENSE file for copyright and license details. */ 1/* See LICENSE file for copyright and license details. */
2#include "dwm.h" 2#include "dwm.h"
3#include <regex.h>
4#include <stdio.h>
3#include <stdlib.h> 5#include <stdlib.h>
4#include <string.h> 6#include <string.h>
5#include <X11/Xatom.h> 7#include <X11/Xatom.h>
@@ -12,9 +14,35 @@ typedef struct {
12 void (*arrange)(void); 14 void (*arrange)(void);
13} Layout; 15} Layout;
14 16
15unsigned int blw = 0; 17typedef struct {
16static char prop[128]; 18 const char *prop;
19 const char *tags;
20 Bool isfloating;
21} Rule;
22
23typedef struct {
24 regex_t *propregex;
25 regex_t *tagregex;
26} Regs;
27
28TAGS
29RULES
30
31static char prop[512];
32static unsigned int nrules = 0;
33static unsigned int nlayouts = 0;
17static unsigned int ltidx = 0; /* default */ 34static unsigned int ltidx = 0; /* default */
35static Regs *regs = NULL;
36
37static unsigned int
38idxoftag(const char *tag) {
39 unsigned int i;
40
41 for(i = 0; i < ntags; i++)
42 if(tags[i] == tag)
43 return i;
44 return 0;
45}
18 46
19static void 47static void
20floating(void) { /* default floating layout */ 48floating(void) { /* default floating layout */
@@ -25,12 +53,56 @@ floating(void) { /* default floating layout */
25 resize(c, c->x, c->y, c->w, c->h, True); 53 resize(c, c->x, c->y, c->w, c->h, True);
26} 54}
27 55
28static unsigned int nlayouts = 0; 56static void
57setdwmprops(void) {
58 unsigned int i;
59
60 for(i = 0; i < ntags && i < sizeof prop - 1; i++)
61 prop[i] = seltags[i] ? '1' : '0';
62 if(i < sizeof prop - 1)
63 prop[i++] = (char)ltidx;
64 prop[i] = '\0';
65 XChangeProperty(dpy, root, dwmprops, XA_STRING, 8,
66 PropModeReplace, (unsigned char *)prop, i);
67}
29 68
30LAYOUTS 69LAYOUTS
31 70
32/* extern */ 71/* extern */
33 72
73unsigned int blw = 0;
74
75void
76applyrules(Client *c) {
77 unsigned int i, j;
78 regmatch_t tmp;
79 Bool matched = False;
80 XClassHint ch = { 0 };
81
82 /* rule matching */
83 XGetClassHint(dpy, c->win, &ch);
84 snprintf(prop, sizeof prop, "%s:%s:%s",
85 ch.res_class ? ch.res_class : "",
86 ch.res_name ? ch.res_name : "", c->name);
87 for(i = 0; i < nrules; i++)
88 if(regs[i].propregex && !regexec(regs[i].propregex, prop, 1, &tmp, 0)) {
89 c->isfloating = rules[i].isfloating;
90 for(j = 0; regs[i].tagregex && j < ntags; j++) {
91 if(!regexec(regs[i].tagregex, tags[j], 1, &tmp, 0)) {
92 matched = True;
93 c->tags[j] = True;
94 }
95 }
96 }
97 if(ch.res_class)
98 XFree(ch.res_class);
99 if(ch.res_name)
100 XFree(ch.res_name);
101 if(!matched)
102 for(i = 0; i < ntags; i++)
103 c->tags[i] = seltags[i];
104}
105
34void 106void
35arrange(void) { 107arrange(void) {
36 Client *c; 108 Client *c;
@@ -46,6 +118,33 @@ arrange(void) {
46} 118}
47 119
48void 120void
121compileregs(void) {
122 unsigned int i;
123 regex_t *reg;
124
125 if(regs)
126 return;
127 nrules = sizeof rules / sizeof rules[0];
128 regs = emallocz(nrules * sizeof(Regs));
129 for(i = 0; i < nrules; i++) {
130 if(rules[i].prop) {
131 reg = emallocz(sizeof(regex_t));
132 if(regcomp(reg, rules[i].prop, REG_EXTENDED))
133 free(reg);
134 else
135 regs[i].propregex = reg;
136 }
137 if(rules[i].tags) {
138 reg = emallocz(sizeof(regex_t));
139 if(regcomp(reg, rules[i].tags, REG_EXTENDED))
140 free(reg);
141 else
142 regs[i].tagregex = reg;
143 }
144 }
145}
146
147void
49focusnext(const char *arg) { 148focusnext(const char *arg) {
50 Client *c; 149 Client *c;
51 150
@@ -83,6 +182,18 @@ getsymbol(void)
83 return layouts[ltidx].symbol; 182 return layouts[ltidx].symbol;
84} 183}
85 184
185void
186initlayouts(void) {
187 unsigned int i, w;
188
189 nlayouts = sizeof layouts / sizeof layouts[0];
190 for(blw = i = 0; i < nlayouts; i++) {
191 w = textw(layouts[i].symbol);
192 if(w > blw)
193 blw = w;
194 }
195}
196
86Bool 197Bool
87isfloating(void) { 198isfloating(void) {
88 return layouts[ltidx].arrange == floating; 199 return layouts[ltidx].arrange == floating;
@@ -94,20 +205,18 @@ isarrange(void (*func)())
94 return func == layouts[ltidx].arrange; 205 return func == layouts[ltidx].arrange;
95} 206}
96 207
97void 208Bool
98initlayouts(void) { 209isvisible(Client *c) {
99 unsigned int i, w; 210 unsigned int i;
100 211
101 nlayouts = sizeof layouts / sizeof layouts[0]; 212 for(i = 0; i < ntags; i++)
102 for(blw = i = 0; i < nlayouts; i++) { 213 if(c->tags[i] && seltags[i])
103 w = textw(layouts[i].symbol); 214 return True;
104 if(w > blw) 215 return False;
105 blw = w;
106 }
107} 216}
108 217
109void 218void
110loaddwmprops(void) { 219getdwmprops(void) {
111 unsigned int i; 220 unsigned int i;
112 221
113 if(gettextprop(root, dwmprops, prop, sizeof prop)) { 222 if(gettextprop(root, dwmprops, prop, sizeof prop)) {
@@ -156,19 +265,6 @@ restack(void) {
156} 265}
157 266
158void 267void
159savedwmprops(void) {
160 unsigned int i;
161
162 for(i = 0; i < ntags && i < sizeof prop - 1; i++)
163 prop[i] = seltags[i] ? '1' : '0';
164 if(i < sizeof prop - 1)
165 prop[i++] = (char)ltidx;
166 prop[i] = '\0';
167 XChangeProperty(dpy, root, dwmprops, XA_STRING, 8,
168 PropModeReplace, (unsigned char *)prop, i);
169}
170
171void
172setlayout(const char *arg) { 268setlayout(const char *arg) {
173 int i; 269 int i;
174 270
@@ -186,7 +282,22 @@ setlayout(const char *arg) {
186 arrange(); 282 arrange();
187 else 283 else
188 drawstatus(); 284 drawstatus();
189 savedwmprops(); 285 setdwmprops();
286}
287
288void
289tag(const char *arg) {
290 unsigned int i;
291
292 if(!sel)
293 return;
294 for(i = 0; i < ntags; i++)
295 sel->tags[i] = arg == NULL;
296 i = idxoftag(arg);
297 if(i >= 0 && i < ntags)
298 sel->tags[i] = True;
299 setprops(sel);
300 arrange();
190} 301}
191 302
192void 303void
@@ -200,6 +311,18 @@ togglebar(const char *arg) {
200} 311}
201 312
202void 313void
314togglefloating(const char *arg) {
315 if(!sel || isfloating())
316 return;
317 sel->isfloating = !sel->isfloating;
318 if(sel->isfloating) {
319 resize(sel, sel->x, sel->y, sel->w, sel->h, True);
320 setprops(sel);
321 }
322 arrange();
323}
324
325void
203togglemax(const char *arg) { 326togglemax(const char *arg) {
204 XEvent ev; 327 XEvent ev;
205 328
@@ -217,3 +340,44 @@ togglemax(const char *arg) {
217 drawstatus(); 340 drawstatus();
218 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev)); 341 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
219} 342}
343
344void
345toggletag(const char *arg) {
346 unsigned int i, j;
347
348 if(!sel)
349 return;
350 i = idxoftag(arg);
351 sel->tags[i] = !sel->tags[i];
352 for(j = 0; j < ntags && !sel->tags[j]; j++);
353 if(j == ntags)
354 sel->tags[i] = True;
355 setprops(sel);
356 arrange();
357}
358
359void
360toggleview(const char *arg) {
361 unsigned int i, j;
362
363 i = idxoftag(arg);
364 seltags[i] = !seltags[i];
365 for(j = 0; j < ntags && !seltags[j]; j++);
366 if(j == ntags)
367 seltags[i] = True; /* cannot toggle last view */
368 setdwmprops();
369 arrange();
370}
371
372void
373view(const char *arg) {
374 unsigned int i;
375
376 for(i = 0; i < ntags; i++)
377 seltags[i] = arg == NULL;
378 i = idxoftag(arg);
379 if(i >= 0 && i < ntags)
380 seltags[i] = True;
381 setdwmprops();
382 arrange();
383}