aewl

view tag.c @ 123:61490330e90a

cleaned up code
author arg@10ksloc.org
date Thu, 20 Jul 2006 12:18:06 +0200
parents 329fd7dae530
children 75576e44c1d8
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 if(c->tags[tsel]) {
57 resize(c, True, TopLeft);
58 }
59 else
60 ban(c);
61 }
62 if(sel && !sel->tags[tsel]) {
63 if((sel = getnext(clients, tsel))) {
64 higher(sel);
65 focus(sel);
66 }
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 arrange = dotile;
79 for(n = 0, c = clients; c; c = c->next)
80 if(c->tags[tsel] && !c->isfloat)
81 n++;
83 if(n > 1)
84 h = (sh - bh) / (n - 1);
85 else
86 h = sh - bh;
88 for(i = 0, c = clients; c; c = c->next) {
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 * c->border;
99 c->h = sh - 2 * c->border - bh;
100 }
101 else if(i == 0) {
102 c->x = sx;
103 c->y = sy + bh;
104 c->w = mw - 2 * c->border;
105 c->h = sh - 2 * c->border - 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 * c->border;
111 c->h = h - 2 * c->border;
112 }
113 else { /* fallback if h < bh */
114 c->x = sx + mw;
115 c->y = sy + bh;
116 c->w = w - 2 * c->border;
117 c->h = sh - 2 * c->border - 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, tsel))) {
127 higher(sel);
128 focus(sel);
129 }
130 }
131 drawall();
132 }
134 Client *
135 getnext(Client *c, unsigned int t)
136 {
137 for(; c && !c->tags[t]; c = c->next);
138 return c;
139 }
141 void
142 heretag(Arg *arg)
143 {
144 int i;
145 Client *c;
147 if(arg->i == tsel)
148 return;
150 if(!(c = getnext(clients, arg->i)))
151 return;
153 for(i = 0; i < TLast; i++)
154 c->tags[i] = NULL;
155 c->tags[tsel] = tags[tsel];
156 pop(c);
157 focus(c);
158 }
160 void
161 replacetag(Arg *arg)
162 {
163 int i;
165 if(!sel)
166 return;
168 for(i = 0; i < TLast; i++)
169 sel->tags[i] = NULL;
170 appendtag(arg);
171 }
173 void
174 settags(Client *c)
175 {
176 char classinst[256];
177 static unsigned int len = rule ? sizeof(rule) / sizeof(rule[0]) : 0;
178 unsigned int i, j;
179 regex_t regex;
180 regmatch_t tmp;
181 Bool matched = False;
182 XClassHint ch;
184 if(!len) {
185 c->tags[tsel] = tags[tsel];
186 return;
187 }
189 if(XGetClassHint(dpy, c->win, &ch)) {
190 snprintf(classinst, sizeof(classinst), "%s:%s",
191 ch.res_class ? ch.res_class : "",
192 ch.res_name ? ch.res_name : "");
193 for(i = 0; !matched && i < len; i++) {
194 if(!regcomp(&regex, rule[i].pattern, 0)) {
195 if(!regexec(&regex, classinst, 1, &tmp, 0)) {
196 for(j = 0; j < TLast; j++) {
197 if(rule[i].tags[j])
198 matched = True;
199 c->tags[j] = rule[i].tags[j];
200 }
201 c->isfloat = rule[i].isfloat;
202 }
203 regfree(&regex);
204 }
205 }
206 if(ch.res_class)
207 XFree(ch.res_class);
208 if(ch.res_name)
209 XFree(ch.res_name);
210 }
211 if(!matched)
212 c->tags[tsel] = tags[tsel];
213 }
215 void
216 view(Arg *arg)
217 {
218 tsel = arg->i;
219 arrange(NULL);
220 drawall();
221 }