rev |
line source |
garbeam@75
|
1 /*
|
garbeam@75
|
2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
|
garbeam@75
|
3 * See LICENSE file for license details.
|
garbeam@75
|
4 */
|
garbeam@76
|
5 #include "dwm.h"
|
arg@114
|
6 #include <regex.h>
|
arg@114
|
7 #include <stdio.h>
|
arg@191
|
8 #include <stdlib.h>
|
garbeam@75
|
9 #include <string.h>
|
arg@114
|
10 #include <sys/types.h>
|
garbeam@75
|
11 #include <X11/Xutil.h>
|
garbeam@75
|
12
|
garbeam@76
|
13
|
arg@114
|
14 typedef struct {
|
arg@191
|
15 const char *clpattern;
|
arg@191
|
16 const char *tpattern;
|
arg@114
|
17 Bool isfloat;
|
arg@114
|
18 } Rule;
|
arg@114
|
19
|
arg@191
|
20 typedef struct {
|
arg@191
|
21 regex_t *clregex;
|
arg@191
|
22 regex_t *tregex;
|
arg@191
|
23 } RReg;
|
arg@191
|
24
|
arg@191
|
25 /* static */
|
arg@191
|
26
|
arg@146
|
27 TAGS
|
arg@146
|
28 RULES
|
garbeam@84
|
29
|
arg@191
|
30 static RReg *rreg = NULL;
|
arg@191
|
31 static unsigned int len = 0;
|
arg@191
|
32
|
arg@156
|
33 void (*arrange)(Arg *) = DEFMODE;
|
arg@125
|
34
|
arg@125
|
35 /* extern */
|
arg@125
|
36
|
garbeam@76
|
37 void
|
garbeam@76
|
38 appendtag(Arg *arg)
|
garbeam@75
|
39 {
|
garbeam@76
|
40 if(!sel)
|
garbeam@76
|
41 return;
|
garbeam@75
|
42
|
arg@173
|
43 sel->tags[arg->i] = True;
|
garbeam@75
|
44 arrange(NULL);
|
garbeam@75
|
45 }
|
garbeam@75
|
46
|
garbeam@75
|
47 void
|
garbeam@75
|
48 dofloat(Arg *arg)
|
garbeam@75
|
49 {
|
garbeam@75
|
50 Client *c;
|
garbeam@75
|
51
|
garbeam@75
|
52 for(c = clients; c; c = c->next) {
|
arg@124
|
53 c->ismax = False;
|
garbeam@95
|
54 if(c->tags[tsel]) {
|
arg@99
|
55 resize(c, True, TopLeft);
|
garbeam@95
|
56 }
|
garbeam@75
|
57 else
|
garbeam@75
|
58 ban(c);
|
garbeam@75
|
59 }
|
arg@194
|
60 if((sel = getnext(clients))) {
|
arg@194
|
61 higher(sel);
|
arg@194
|
62 focus(sel);
|
garbeam@75
|
63 }
|
arg@194
|
64 else
|
arg@194
|
65 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
|
garbeam@75
|
66 drawall();
|
garbeam@75
|
67 }
|
garbeam@75
|
68
|
garbeam@75
|
69 void
|
garbeam@75
|
70 dotile(Arg *arg)
|
garbeam@75
|
71 {
|
arg@123
|
72 int n, i, w, h;
|
garbeam@75
|
73 Client *c;
|
garbeam@75
|
74
|
garbeam@75
|
75 w = sw - mw;
|
garbeam@75
|
76 for(n = 0, c = clients; c; c = c->next)
|
garbeam@80
|
77 if(c->tags[tsel] && !c->isfloat)
|
garbeam@75
|
78 n++;
|
garbeam@75
|
79
|
garbeam@75
|
80 if(n > 1)
|
garbeam@75
|
81 h = (sh - bh) / (n - 1);
|
garbeam@75
|
82 else
|
garbeam@75
|
83 h = sh - bh;
|
garbeam@75
|
84
|
garbeam@75
|
85 for(i = 0, c = clients; c; c = c->next) {
|
arg@124
|
86 c->ismax = False;
|
garbeam@75
|
87 if(c->tags[tsel]) {
|
garbeam@80
|
88 if(c->isfloat) {
|
garbeam@75
|
89 higher(c);
|
arg@99
|
90 resize(c, True, TopLeft);
|
garbeam@75
|
91 continue;
|
garbeam@75
|
92 }
|
garbeam@75
|
93 if(n == 1) {
|
arg@115
|
94 c->x = sx;
|
arg@115
|
95 c->y = sy + bh;
|
arg@164
|
96 c->w = sw - 2;
|
arg@164
|
97 c->h = sh - 2 - bh;
|
garbeam@75
|
98 }
|
garbeam@75
|
99 else if(i == 0) {
|
arg@115
|
100 c->x = sx;
|
arg@115
|
101 c->y = sy + bh;
|
arg@164
|
102 c->w = mw - 2;
|
arg@164
|
103 c->h = sh - 2 - bh;
|
garbeam@75
|
104 }
|
arg@104
|
105 else if(h > bh) {
|
arg@115
|
106 c->x = sx + mw;
|
arg@115
|
107 c->y = sy + (i - 1) * h + bh;
|
arg@164
|
108 c->w = w - 2;
|
arg@164
|
109 c->h = h - 2;
|
garbeam@75
|
110 }
|
arg@104
|
111 else { /* fallback if h < bh */
|
arg@115
|
112 c->x = sx + mw;
|
arg@115
|
113 c->y = sy + bh;
|
arg@164
|
114 c->w = w - 2;
|
arg@164
|
115 c->h = sh - 2 - bh;
|
arg@104
|
116 }
|
arg@99
|
117 resize(c, False, TopLeft);
|
garbeam@75
|
118 i++;
|
garbeam@75
|
119 }
|
garbeam@75
|
120 else
|
garbeam@75
|
121 ban(c);
|
garbeam@75
|
122 }
|
arg@194
|
123 if((sel = getnext(clients))) {
|
arg@194
|
124 higher(sel);
|
arg@194
|
125 focus(sel);
|
garbeam@75
|
126 }
|
arg@194
|
127 else
|
arg@194
|
128 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
|
garbeam@75
|
129 drawall();
|
garbeam@75
|
130 }
|
garbeam@75
|
131
|
garbeam@76
|
132 Client *
|
arg@142
|
133 getnext(Client *c)
|
garbeam@75
|
134 {
|
arg@142
|
135 for(; c && !c->tags[tsel]; c = c->next);
|
garbeam@76
|
136 return c;
|
garbeam@75
|
137 }
|
garbeam@75
|
138
|
arg@127
|
139 Client *
|
arg@127
|
140 getprev(Client *c)
|
arg@127
|
141 {
|
arg@127
|
142 for(; c && !c->tags[tsel]; c = c->prev);
|
arg@127
|
143 return c;
|
arg@127
|
144 }
|
arg@127
|
145
|
garbeam@75
|
146 void
|
arg@191
|
147 initrregs()
|
arg@191
|
148 {
|
arg@191
|
149 unsigned int i;
|
arg@191
|
150 regex_t *reg;
|
arg@191
|
151
|
arg@191
|
152 if(rreg)
|
arg@191
|
153 return;
|
arg@191
|
154 len = sizeof(rule) / sizeof(rule[0]);
|
arg@191
|
155 rreg = emallocz(len * sizeof(RReg));
|
arg@191
|
156
|
arg@191
|
157 for(i = 0; i < len; i++) {
|
arg@191
|
158 if(rule[i].clpattern) {
|
arg@191
|
159 reg = emallocz(sizeof(regex_t));
|
arg@191
|
160 if(regcomp(reg, rule[i].clpattern, 0))
|
arg@191
|
161 free(reg);
|
arg@191
|
162 else
|
arg@191
|
163 rreg[i].clregex = reg;
|
arg@191
|
164 }
|
arg@191
|
165 if(rule[i].tpattern) {
|
arg@191
|
166 reg = emallocz(sizeof(regex_t));
|
arg@191
|
167 if(regcomp(reg, rule[i].tpattern, 0))
|
arg@191
|
168 free(reg);
|
arg@191
|
169 else
|
arg@191
|
170 rreg[i].tregex = reg;
|
arg@191
|
171 }
|
arg@191
|
172 }
|
arg@191
|
173 }
|
arg@191
|
174
|
arg@191
|
175 void
|
garbeam@75
|
176 replacetag(Arg *arg)
|
garbeam@75
|
177 {
|
garbeam@75
|
178 int i;
|
arg@123
|
179
|
garbeam@75
|
180 if(!sel)
|
garbeam@75
|
181 return;
|
garbeam@75
|
182
|
arg@178
|
183 for(i = 0; i < ntags; i++)
|
arg@173
|
184 sel->tags[i] = False;
|
garbeam@75
|
185 appendtag(arg);
|
garbeam@75
|
186 }
|
garbeam@75
|
187
|
garbeam@76
|
188 void
|
garbeam@76
|
189 settags(Client *c)
|
garbeam@76
|
190 {
|
arg@114
|
191 char classinst[256];
|
arg@191
|
192 unsigned int i, j;
|
arg@114
|
193 regmatch_t tmp;
|
garbeam@76
|
194 Bool matched = False;
|
arg@114
|
195 XClassHint ch;
|
garbeam@76
|
196
|
garbeam@76
|
197 if(XGetClassHint(dpy, c->win, &ch)) {
|
arg@114
|
198 snprintf(classinst, sizeof(classinst), "%s:%s",
|
arg@114
|
199 ch.res_class ? ch.res_class : "",
|
arg@114
|
200 ch.res_name ? ch.res_name : "");
|
arg@191
|
201 for(i = 0; !matched && i < len; i++)
|
arg@191
|
202 if(rreg[i].clregex && !regexec(rreg[i].clregex, classinst, 1, &tmp, 0)) {
|
arg@191
|
203 c->isfloat = rule[i].isfloat;
|
arg@191
|
204 for(j = 0; rreg[i].tregex && j < ntags; j++) {
|
arg@191
|
205 if(!regexec(rreg[i].tregex, tags[j], 1, &tmp, 0)) {
|
arg@191
|
206 matched = True;
|
arg@191
|
207 c->tags[j] = True;
|
arg@191
|
208 }
|
garbeam@76
|
209 }
|
arg@114
|
210 }
|
garbeam@76
|
211 if(ch.res_class)
|
garbeam@76
|
212 XFree(ch.res_class);
|
garbeam@76
|
213 if(ch.res_name)
|
garbeam@76
|
214 XFree(ch.res_name);
|
garbeam@76
|
215 }
|
garbeam@76
|
216 if(!matched)
|
arg@173
|
217 c->tags[tsel] = True;
|
garbeam@76
|
218 }
|
garbeam@76
|
219
|
garbeam@76
|
220 void
|
arg@124
|
221 togglemode(Arg *arg)
|
arg@124
|
222 {
|
arg@124
|
223 arrange = arrange == dofloat ? dotile : dofloat;
|
arg@124
|
224 arrange(NULL);
|
arg@124
|
225 }
|
arg@124
|
226
|
arg@124
|
227 void
|
garbeam@76
|
228 view(Arg *arg)
|
garbeam@76
|
229 {
|
garbeam@76
|
230 tsel = arg->i;
|
garbeam@76
|
231 arrange(NULL);
|
garbeam@76
|
232 drawall();
|
garbeam@76
|
233 }
|
arg@144
|
234
|
arg@144
|
235 void
|
arg@144
|
236 viewnext(Arg *arg)
|
arg@144
|
237 {
|
arg@178
|
238 arg->i = (tsel < ntags-1) ? tsel+1 : 0;
|
arg@144
|
239 view(arg);
|
arg@144
|
240 }
|
arg@144
|
241
|
arg@144
|
242 void
|
arg@144
|
243 viewprev(Arg *arg)
|
arg@144
|
244 {
|
arg@178
|
245 arg->i = (tsel > 0) ? tsel-1 : ntags-1;
|
arg@144
|
246 view(arg);
|
arg@144
|
247 }
|