dwm-meillo

view tag.c @ 114:dfa5cd0969a6

implemented regexp matching for rules
author arg@10ksloc.org
date Wed, 19 Jul 2006 17:42:08 +0200
parents 3a708f113f55
children 329fd7dae530
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"
7 #include <regex.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <sys/types.h>
11 #include <X11/Xutil.h>
13 /* static */
15 typedef struct {
16 const char *pattern;
17 char *tags[TLast];
18 Bool isfloat;
19 } Rule;
21 /* CUSTOMIZE */
22 static Rule rule[] = {
23 /* class instance tags isfloat */
24 { "Firefox.*", { [Twww] = "www" }, False },
25 { "Gimp.*", { 0 }, True},
26 };
28 /* extern */
30 /* CUSTOMIZE */
31 char *tags[TLast] = {
32 [Tscratch] = "scratch",
33 [Tdev] = "dev",
34 [Twww] = "www",
35 [Twork] = "work",
36 };
37 void (*arrange)(Arg *) = dotile;
39 void
40 appendtag(Arg *arg)
41 {
42 if(!sel)
43 return;
45 sel->tags[arg->i] = tags[arg->i];
46 arrange(NULL);
47 }
49 void
50 dofloat(Arg *arg)
51 {
52 Client *c;
54 arrange = dofloat;
55 for(c = clients; c; c = c->next) {
56 setgeom(c);
57 if(c->tags[tsel]) {
58 resize(c, True, TopLeft);
59 }
60 else
61 ban(c);
62 }
63 if(sel && !sel->tags[tsel]) {
64 if((sel = getnext(clients, tsel))) {
65 higher(sel);
66 focus(sel);
67 }
68 }
69 drawall();
70 }
72 void
73 dotile(Arg *arg)
74 {
75 Client *c;
76 int n, i, w, h;
78 w = sw - mw;
79 arrange = dotile;
80 for(n = 0, c = clients; c; c = c->next)
81 if(c->tags[tsel] && !c->isfloat)
82 n++;
84 if(n > 1)
85 h = (sh - bh) / (n - 1);
86 else
87 h = sh - bh;
89 for(i = 0, c = clients; c; c = c->next) {
90 setgeom(c);
91 if(c->tags[tsel]) {
92 if(c->isfloat) {
93 higher(c);
94 resize(c, True, TopLeft);
95 continue;
96 }
97 if(n == 1) {
98 *c->x = sx;
99 *c->y = sy + bh;
100 *c->w = sw - 2 * c->border;
101 *c->h = sh - 2 * c->border - bh;
102 }
103 else if(i == 0) {
104 *c->x = sx;
105 *c->y = sy + bh;
106 *c->w = mw - 2 * c->border;
107 *c->h = sh - 2 * c->border - bh;
108 }
109 else if(h > bh) {
110 *c->x = sx + mw;
111 *c->y = sy + (i - 1) * h + bh;
112 *c->w = w - 2 * c->border;
113 *c->h = h - 2 * c->border;
114 }
115 else { /* fallback if h < bh */
116 *c->x = sx + mw;
117 *c->y = sy + bh;
118 *c->w = w - 2 * c->border;
119 *c->h = sh - 2 * c->border - bh;
120 }
121 resize(c, False, TopLeft);
122 i++;
123 }
124 else
125 ban(c);
126 }
127 if(!sel || (sel && !sel->tags[tsel])) {
128 if((sel = getnext(clients, tsel))) {
129 higher(sel);
130 focus(sel);
131 }
132 }
133 drawall();
134 }
136 Client *
137 getnext(Client *c, unsigned int t)
138 {
139 for(; c && !c->tags[t]; c = c->next);
140 return c;
141 }
143 void
144 heretag(Arg *arg)
145 {
146 int i;
147 Client *c;
149 if(arg->i == tsel)
150 return;
152 if(!(c = getnext(clients, arg->i)))
153 return;
155 for(i = 0; i < TLast; i++)
156 c->tags[i] = NULL;
157 c->tags[tsel] = tags[tsel];
158 pop(c);
159 focus(c);
160 }
162 void
163 replacetag(Arg *arg)
164 {
165 int i;
166 if(!sel)
167 return;
169 for(i = 0; i < TLast; i++)
170 sel->tags[i] = NULL;
171 appendtag(arg);
172 }
174 void
175 settags(Client *c)
176 {
177 char classinst[256];
178 static unsigned int len = rule ? sizeof(rule) / sizeof(rule[0]) : 0;
179 unsigned int i, j;
180 regex_t regex;
181 regmatch_t tmp;
182 Bool matched = False;
183 XClassHint ch;
185 if(!len) {
186 c->tags[tsel] = tags[tsel];
187 return;
188 }
190 if(XGetClassHint(dpy, c->win, &ch)) {
191 snprintf(classinst, sizeof(classinst), "%s:%s",
192 ch.res_class ? ch.res_class : "",
193 ch.res_name ? ch.res_name : "");
194 for(i = 0; !matched && i < len; i++) {
195 if(!regcomp(&regex, rule[i].pattern, 0)) {
196 if(!regexec(&regex, classinst, 1, &tmp, 0)) {
197 for(j = 0; j < TLast; j++) {
198 if(rule[i].tags[j])
199 matched = True;
200 c->tags[j] = rule[i].tags[j];
201 }
202 c->isfloat = rule[i].isfloat;
203 }
204 regfree(&regex);
205 }
206 }
207 if(ch.res_class)
208 XFree(ch.res_class);
209 if(ch.res_name)
210 XFree(ch.res_name);
211 }
212 if(!matched)
213 c->tags[tsel] = tags[tsel];
214 }
216 void
217 view(Arg *arg)
218 {
219 tsel = arg->i;
220 arrange(NULL);
221 drawall();
222 }