dwm-meillo

view tag.c @ 191:56fee1dc9d53

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