dwm-meillo

view tag.c @ 127:1480e19f6377

using double-linked list in order to get correct prev focus handling
author arg@10ksloc.org
date Thu, 20 Jul 2006 16:54:20 +0200
parents b4b8b4236599
children 30d1302dbe3b
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 Client *
144 getprev(Client *c)
145 {
146 for(; c && !c->tags[tsel]; c = c->prev);
147 return c;
148 }
150 void
151 heretag(Arg *arg)
152 {
153 int i;
154 Client *c;
156 if(arg->i == tsel)
157 return;
159 if(!(c = getnext(clients, arg->i)))
160 return;
162 for(i = 0; i < TLast; i++)
163 c->tags[i] = NULL;
164 c->tags[tsel] = tags[tsel];
165 pop(c);
166 focus(c);
167 }
169 void
170 replacetag(Arg *arg)
171 {
172 int i;
174 if(!sel)
175 return;
177 for(i = 0; i < TLast; i++)
178 sel->tags[i] = NULL;
179 appendtag(arg);
180 }
182 void
183 settags(Client *c)
184 {
185 char classinst[256];
186 static unsigned int len = rule ? sizeof(rule) / sizeof(rule[0]) : 0;
187 unsigned int i, j;
188 regex_t regex;
189 regmatch_t tmp;
190 Bool matched = False;
191 XClassHint ch;
193 if(!len) {
194 c->tags[tsel] = tags[tsel];
195 return;
196 }
198 if(XGetClassHint(dpy, c->win, &ch)) {
199 snprintf(classinst, sizeof(classinst), "%s:%s",
200 ch.res_class ? ch.res_class : "",
201 ch.res_name ? ch.res_name : "");
202 for(i = 0; !matched && i < len; i++) {
203 if(!regcomp(&regex, rule[i].pattern, 0)) {
204 if(!regexec(&regex, classinst, 1, &tmp, 0)) {
205 for(j = 0; j < TLast; j++) {
206 if(rule[i].tags[j])
207 matched = True;
208 c->tags[j] = rule[i].tags[j];
209 }
210 c->isfloat = rule[i].isfloat;
211 }
212 regfree(&regex);
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] = tags[tsel];
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 }