aewl

view tag.c @ 130:30d1302dbe3b

sanitization of several clunky stuff, removed heretag (rarely of use), simplified pop(), changed shortcuts to always contain MODKEY
author arg@10ksloc.org
date Fri, 21 Jul 2006 07:37:52 +0200
parents 1480e19f6377
children c1185dc7a36e
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.*", { [Tnet] = "net" }, False },
25 { "Gimp.*", { 0 }, True},
26 };
28 char *tags[TLast] = {
29 [Tfnord] = "fnord",
30 [Tdev] = "dev",
31 [Tnet] = "net",
32 [Twork] = "work",
33 [Tmisc] = "misc",
34 };
36 void (*arrange)(Arg *) = dotile;
38 /* END CUSTOMIZE */
40 /* extern */
42 void
43 appendtag(Arg *arg)
44 {
45 if(!sel)
46 return;
48 sel->tags[arg->i] = tags[arg->i];
49 arrange(NULL);
50 }
52 void
53 dofloat(Arg *arg)
54 {
55 Client *c;
57 for(c = clients; c; c = c->next) {
58 c->ismax = False;
59 if(c->tags[tsel]) {
60 resize(c, True, TopLeft);
61 }
62 else
63 ban(c);
64 }
65 if(sel && !sel->tags[tsel]) {
66 if((sel = getnext(clients, tsel))) {
67 higher(sel);
68 focus(sel);
69 }
70 }
71 drawall();
72 }
74 void
75 dotile(Arg *arg)
76 {
77 int n, i, w, h;
78 Client *c;
80 w = sw - mw;
81 for(n = 0, c = clients; c; c = c->next)
82 if(c->tags[tsel] && !c->isfloat)
83 n++;
85 if(n > 1)
86 h = (sh - bh) / (n - 1);
87 else
88 h = sh - bh;
90 for(i = 0, c = clients; c; c = c->next) {
91 c->ismax = False;
92 if(c->tags[tsel]) {
93 if(c->isfloat) {
94 higher(c);
95 resize(c, True, TopLeft);
96 continue;
97 }
98 if(n == 1) {
99 c->x = sx;
100 c->y = sy + bh;
101 c->w = sw - 2 * c->border;
102 c->h = sh - 2 * c->border - bh;
103 }
104 else if(i == 0) {
105 c->x = sx;
106 c->y = sy + bh;
107 c->w = mw - 2 * c->border;
108 c->h = sh - 2 * c->border - bh;
109 }
110 else if(h > bh) {
111 c->x = sx + mw;
112 c->y = sy + (i - 1) * h + bh;
113 c->w = w - 2 * c->border;
114 c->h = h - 2 * c->border;
115 }
116 else { /* fallback if h < bh */
117 c->x = sx + mw;
118 c->y = sy + bh;
119 c->w = w - 2 * c->border;
120 c->h = sh - 2 * c->border - bh;
121 }
122 resize(c, False, TopLeft);
123 i++;
124 }
125 else
126 ban(c);
127 }
128 if(!sel || (sel && !sel->tags[tsel])) {
129 if((sel = getnext(clients, tsel))) {
130 higher(sel);
131 focus(sel);
132 }
133 }
134 drawall();
135 }
137 Client *
138 getnext(Client *c, unsigned int t)
139 {
140 for(; c && !c->tags[t]; c = c->next);
141 return c;
142 }
144 Client *
145 getprev(Client *c)
146 {
147 for(; c && !c->tags[tsel]; c = c->prev);
148 return c;
149 }
151 void
152 replacetag(Arg *arg)
153 {
154 int i;
156 if(!sel)
157 return;
159 for(i = 0; i < TLast; i++)
160 sel->tags[i] = NULL;
161 appendtag(arg);
162 }
164 void
165 settags(Client *c)
166 {
167 char classinst[256];
168 static unsigned int len = rule ? sizeof(rule) / sizeof(rule[0]) : 0;
169 unsigned int i, j;
170 regex_t regex;
171 regmatch_t tmp;
172 Bool matched = False;
173 XClassHint ch;
175 if(!len) {
176 c->tags[tsel] = tags[tsel];
177 return;
178 }
180 if(XGetClassHint(dpy, c->win, &ch)) {
181 snprintf(classinst, sizeof(classinst), "%s:%s",
182 ch.res_class ? ch.res_class : "",
183 ch.res_name ? ch.res_name : "");
184 for(i = 0; !matched && i < len; i++) {
185 if(!regcomp(&regex, rule[i].pattern, 0)) {
186 if(!regexec(&regex, classinst, 1, &tmp, 0)) {
187 for(j = 0; j < TLast; j++) {
188 if(rule[i].tags[j])
189 matched = True;
190 c->tags[j] = rule[i].tags[j];
191 }
192 c->isfloat = rule[i].isfloat;
193 }
194 regfree(&regex);
195 }
196 }
197 if(ch.res_class)
198 XFree(ch.res_class);
199 if(ch.res_name)
200 XFree(ch.res_name);
201 }
202 if(!matched)
203 c->tags[tsel] = tags[tsel];
204 }
206 void
207 togglemode(Arg *arg)
208 {
209 arrange = arrange == dofloat ? dotile : dofloat;
210 arrange(NULL);
211 }
213 void
214 view(Arg *arg)
215 {
216 tsel = arg->i;
217 arrange(NULL);
218 drawall();
219 }