dwm-meillo
view tag.c @ 125:b4b8b4236599
cleaned the CUSTOMIZE flags
author | arg@10ksloc.org |
---|---|
date | Thu, 20 Jul 2006 15:17:52 +0200 |
parents | 75576e44c1d8 |
children | 1480e19f6377 |
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 char *tags[TLast] = {
29 [Tscratch] = "scratch",
30 [Tdev] = "dev",
31 [Twww] = "www",
32 [Twork] = "work",
33 };
35 void (*arrange)(Arg *) = dotile;
37 /* END CUSTOMIZE */
39 /* extern */
41 void
42 appendtag(Arg *arg)
43 {
44 if(!sel)
45 return;
47 sel->tags[arg->i] = tags[arg->i];
48 arrange(NULL);
49 }
51 void
52 dofloat(Arg *arg)
53 {
54 Client *c;
56 for(c = clients; c; c = c->next) {
57 c->ismax = False;
58 if(c->tags[tsel]) {
59 resize(c, True, TopLeft);
60 }
61 else
62 ban(c);
63 }
64 if(sel && !sel->tags[tsel]) {
65 if((sel = getnext(clients, tsel))) {
66 higher(sel);
67 focus(sel);
68 }
69 }
70 drawall();
71 }
73 void
74 dotile(Arg *arg)
75 {
76 int n, i, w, h;
77 Client *c;
79 w = sw - mw;
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 c->ismax = False;
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;
167 if(!sel)
168 return;
170 for(i = 0; i < TLast; i++)
171 sel->tags[i] = NULL;
172 appendtag(arg);
173 }
175 void
176 settags(Client *c)
177 {
178 char classinst[256];
179 static unsigned int len = rule ? sizeof(rule) / sizeof(rule[0]) : 0;
180 unsigned int i, j;
181 regex_t regex;
182 regmatch_t tmp;
183 Bool matched = False;
184 XClassHint ch;
186 if(!len) {
187 c->tags[tsel] = tags[tsel];
188 return;
189 }
191 if(XGetClassHint(dpy, c->win, &ch)) {
192 snprintf(classinst, sizeof(classinst), "%s:%s",
193 ch.res_class ? ch.res_class : "",
194 ch.res_name ? ch.res_name : "");
195 for(i = 0; !matched && i < len; i++) {
196 if(!regcomp(®ex, rule[i].pattern, 0)) {
197 if(!regexec(®ex, classinst, 1, &tmp, 0)) {
198 for(j = 0; j < TLast; j++) {
199 if(rule[i].tags[j])
200 matched = True;
201 c->tags[j] = rule[i].tags[j];
202 }
203 c->isfloat = rule[i].isfloat;
204 }
205 regfree(®ex);
206 }
207 }
208 if(ch.res_class)
209 XFree(ch.res_class);
210 if(ch.res_name)
211 XFree(ch.res_name);
212 }
213 if(!matched)
214 c->tags[tsel] = tags[tsel];
215 }
217 void
218 togglemode(Arg *arg)
219 {
220 arrange = arrange == dofloat ? dotile : dofloat;
221 arrange(NULL);
222 }
224 void
225 view(Arg *arg)
226 {
227 tsel = arg->i;
228 arrange(NULL);
229 drawall();
230 }