JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
implemented fallback for too many clients in stacked mode
[dwm.git] / tag.c
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"
6
7 #include <string.h>
8 #include <X11/Xutil.h>
9
10 /* static */
11
12 /* CUSTOMIZE */ 
13 static Rule rule[] = {
14         /* class                        instance        tags                                            isfloat */
15         { "Firefox-bin",        "firefox-bin",  { [Twww] = "www" },                     False },
16 };
17
18 /* extern */
19
20 /* CUSTOMIZE */
21 char *tags[TLast] = {
22         [Tscratch] = "scratch",
23         [Tdev] = "dev",
24         [Twww] = "www",
25         [Twork] = "work",
26 };
27 void (*arrange)(Arg *) = dotile;
28
29 void
30 appendtag(Arg *arg)
31 {
32         if(!sel)
33                 return;
34
35         sel->tags[arg->i] = tags[arg->i];
36         arrange(NULL);
37 }
38
39 void
40 dofloat(Arg *arg)
41 {
42         Client *c;
43
44         arrange = dofloat;
45         for(c = clients; c; c = c->next) {
46                 setgeom(c);
47                 if(c->tags[tsel]) {
48                         resize(c, True, TopLeft);
49                 }
50                 else
51                         ban(c);
52         }
53         if(sel && !sel->tags[tsel]) {
54                 if((sel = getnext(clients, tsel))) {
55                         higher(sel);
56                         focus(sel);
57                 }
58         }
59         drawall();
60 }
61
62 void
63 dotile(Arg *arg)
64 {
65         Client *c;
66         int n, i, w, h;
67
68         w = sw - mw;
69         arrange = dotile;
70         for(n = 0, c = clients; c; c = c->next)
71                 if(c->tags[tsel] && !c->isfloat)
72                         n++;
73
74         if(n > 1)
75                 h = (sh - bh) / (n - 1);
76         else
77                 h = sh - bh;
78
79         for(i = 0, c = clients; c; c = c->next) {
80                 setgeom(c);
81                 if(c->tags[tsel]) {
82                         if(c->isfloat) {
83                                 higher(c);
84                                 resize(c, True, TopLeft);
85                                 continue;
86                         }
87                         if(n == 1) {
88                                 *c->x = sx;
89                                 *c->y = sy + bh;
90                                 *c->w = sw - 2 * c->border;
91                                 *c->h = sh - 2 * c->border - bh;
92                         }
93                         else if(i == 0) {
94                                 *c->x = sx;
95                                 *c->y = sy + bh;
96                                 *c->w = mw - 2 * c->border;
97                                 *c->h = sh - 2 * c->border - bh;
98                         }
99                         else if(h > bh) {
100                                 *c->x = sx + mw;
101                                 *c->y = sy + (i - 1) * h + bh;
102                                 *c->w = w - 2 * c->border;
103                                 *c->h = h - 2 * c->border;
104                         }
105                         else { /* fallback if h < bh */
106                                 *c->x = sx + mw;
107                                 *c->y = sy + bh;
108                                 *c->w = w - 2 * c->border;
109                                 *c->h = sh - 2 * c->border - bh;
110                         }
111                         resize(c, False, TopLeft);
112                         i++;
113                 }
114                 else
115                         ban(c);
116         }
117         if(!sel || (sel && !sel->tags[tsel])) {
118                 if((sel = getnext(clients, tsel))) {
119                         higher(sel);
120                         focus(sel);
121                 }
122         }
123         drawall();
124 }
125
126 Client *
127 getnext(Client *c, unsigned int t)
128 {
129         for(; c && !c->tags[t]; c = c->next);
130         return c;
131 }
132
133 void
134 heretag(Arg *arg)
135 {
136         int i;
137         Client *c;
138
139         if(arg->i == tsel)
140                 return;
141
142         if(!(c = getnext(clients, arg->i)))
143                 return;
144
145         for(i = 0; i < TLast; i++)
146                 c->tags[i] = NULL;
147         c->tags[tsel] = tags[tsel];
148         pop(c);
149         focus(c);
150 }
151
152 void
153 replacetag(Arg *arg)
154 {
155         int i;
156         if(!sel)
157                 return;
158
159         for(i = 0; i < TLast; i++)
160                 sel->tags[i] = NULL;
161         appendtag(arg);
162 }
163
164 void
165 settags(Client *c)
166 {
167         XClassHint ch;
168         static unsigned int len = rule ? sizeof(rule) / sizeof(rule[0]) : 0;
169         unsigned int i, j;
170         Bool matched = False;
171
172         if(!len) {
173                 c->tags[tsel] = tags[tsel];
174                 return;
175         }
176
177         if(XGetClassHint(dpy, c->win, &ch)) {
178                 if(ch.res_class && ch.res_name) {
179                         for(i = 0; i < len; i++)
180                                 if(!strncmp(rule[i].class, ch.res_class, sizeof(rule[i].class))
181                                         && !strncmp(rule[i].instance, ch.res_name, sizeof(rule[i].instance)))
182                                 {
183                                         for(j = 0; j < TLast; j++)
184                                                 c->tags[j] = rule[i].tags[j];
185                                         c->isfloat = rule[i].isfloat;
186                                         matched = True;
187                                         break;
188                                 }
189                 }
190                 if(ch.res_class)
191                         XFree(ch.res_class);
192                 if(ch.res_name)
193                         XFree(ch.res_name);
194         }
195
196         if(!matched)
197                 c->tags[tsel] = tags[tsel];
198 }
199
200 void
201 view(Arg *arg)
202 {
203         tsel = arg->i;
204         arrange(NULL);
205         drawall();
206 }