aewl

annotate tag.c @ 276:9a0a351dd910

pplied Sanders appendtag patch
author Anselm R.Garbe <arg@10ksloc.org>
date Mon, 14 Aug 2006 15:31:58 +0200
parents be40a56df248
children 1e7fa455e3b4
rev   line source
garbeam@75 1 /*
garbeam@75 2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
garbeam@75 3 * See LICENSE file for license details.
garbeam@75 4 */
garbeam@76 5 #include "dwm.h"
arg@114 6 #include <regex.h>
arg@114 7 #include <stdio.h>
arg@191 8 #include <stdlib.h>
garbeam@75 9 #include <string.h>
arg@114 10 #include <sys/types.h>
garbeam@75 11 #include <X11/Xutil.h>
garbeam@75 12
garbeam@76 13
arg@114 14 typedef struct {
arg@191 15 const char *clpattern;
arg@191 16 const char *tpattern;
arg@114 17 Bool isfloat;
arg@114 18 } Rule;
arg@114 19
arg@191 20 typedef struct {
arg@191 21 regex_t *clregex;
arg@191 22 regex_t *tregex;
arg@191 23 } RReg;
arg@191 24
arg@191 25 /* static */
arg@191 26
arg@146 27 TAGS
arg@146 28 RULES
garbeam@84 29
arg@191 30 static RReg *rreg = NULL;
arg@191 31 static unsigned int len = 0;
arg@191 32
arg@156 33 void (*arrange)(Arg *) = DEFMODE;
arg@125 34
arg@125 35 /* extern */
arg@125 36
garbeam@76 37 void
garbeam@76 38 appendtag(Arg *arg)
garbeam@75 39 {
arg@276 40 Client *c = sel;
arg@276 41
arg@276 42 if(!c)
garbeam@76 43 return;
garbeam@75 44
arg@276 45 c->tags[arg->i] = True;
garbeam@75 46 arrange(NULL);
arg@276 47 focus(c);
arg@276 48 restack();
garbeam@75 49 }
garbeam@75 50
garbeam@75 51 void
garbeam@75 52 dofloat(Arg *arg)
garbeam@75 53 {
garbeam@75 54 Client *c;
garbeam@75 55
garbeam@75 56 for(c = clients; c; c = c->next) {
arg@124 57 c->ismax = False;
arg@261 58 if(isvisible(c)) {
arg@99 59 resize(c, True, TopLeft);
garbeam@95 60 }
garbeam@75 61 else
garbeam@75 62 ban(c);
garbeam@75 63 }
arg@194 64 if((sel = getnext(clients))) {
arg@194 65 focus(sel);
arg@270 66 restack();
garbeam@75 67 }
arg@194 68 else
arg@194 69 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
garbeam@75 70 }
garbeam@75 71
garbeam@75 72 void
garbeam@75 73 dotile(Arg *arg)
garbeam@75 74 {
arg@270 75 int h, i, n, w;
garbeam@75 76 Client *c;
garbeam@75 77
garbeam@75 78 w = sw - mw;
garbeam@75 79 for(n = 0, c = clients; c; c = c->next)
arg@261 80 if(isvisible(c) && !c->isfloat)
garbeam@75 81 n++;
garbeam@75 82
garbeam@75 83 if(n > 1)
garbeam@75 84 h = (sh - bh) / (n - 1);
garbeam@75 85 else
garbeam@75 86 h = sh - bh;
garbeam@75 87
garbeam@75 88 for(i = 0, c = clients; c; c = c->next) {
arg@124 89 c->ismax = False;
arg@261 90 if(isvisible(c)) {
garbeam@80 91 if(c->isfloat) {
arg@99 92 resize(c, True, TopLeft);
garbeam@75 93 continue;
garbeam@75 94 }
garbeam@75 95 if(n == 1) {
arg@115 96 c->x = sx;
arg@115 97 c->y = sy + bh;
arg@164 98 c->w = sw - 2;
arg@164 99 c->h = sh - 2 - bh;
garbeam@75 100 }
garbeam@75 101 else if(i == 0) {
arg@115 102 c->x = sx;
arg@115 103 c->y = sy + bh;
arg@164 104 c->w = mw - 2;
arg@164 105 c->h = sh - 2 - bh;
garbeam@75 106 }
arg@104 107 else if(h > bh) {
arg@115 108 c->x = sx + mw;
arg@115 109 c->y = sy + (i - 1) * h + bh;
arg@164 110 c->w = w - 2;
arg@240 111 if(i + 1 == n)
arg@240 112 c->h = sh - c->y - 2;
arg@240 113 else
arg@240 114 c->h = h - 2;
garbeam@75 115 }
arg@104 116 else { /* fallback if h < bh */
arg@115 117 c->x = sx + mw;
arg@115 118 c->y = sy + bh;
arg@164 119 c->w = w - 2;
arg@164 120 c->h = sh - 2 - bh;
arg@104 121 }
arg@99 122 resize(c, False, TopLeft);
garbeam@75 123 i++;
garbeam@75 124 }
garbeam@75 125 else
garbeam@75 126 ban(c);
garbeam@75 127 }
arg@270 128 if((sel = getnext(clients)))
arg@194 129 focus(sel);
arg@194 130 else
arg@194 131 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
arg@270 132 restack();
garbeam@75 133 }
garbeam@75 134
garbeam@76 135 Client *
arg@142 136 getnext(Client *c)
garbeam@75 137 {
arg@261 138 for(; c && !isvisible(c); c = c->next);
garbeam@76 139 return c;
garbeam@75 140 }
garbeam@75 141
arg@127 142 Client *
arg@127 143 getprev(Client *c)
arg@127 144 {
arg@261 145 for(; c && !isvisible(c); c = c->prev);
arg@127 146 return c;
arg@127 147 }
arg@127 148
garbeam@75 149 void
arg@191 150 initrregs()
arg@191 151 {
arg@191 152 unsigned int i;
arg@191 153 regex_t *reg;
arg@191 154
arg@191 155 if(rreg)
arg@191 156 return;
arg@191 157 len = sizeof(rule) / sizeof(rule[0]);
arg@191 158 rreg = emallocz(len * sizeof(RReg));
arg@191 159
arg@191 160 for(i = 0; i < len; i++) {
arg@191 161 if(rule[i].clpattern) {
arg@191 162 reg = emallocz(sizeof(regex_t));
arg@191 163 if(regcomp(reg, rule[i].clpattern, 0))
arg@191 164 free(reg);
arg@191 165 else
arg@191 166 rreg[i].clregex = reg;
arg@191 167 }
arg@191 168 if(rule[i].tpattern) {
arg@191 169 reg = emallocz(sizeof(regex_t));
arg@191 170 if(regcomp(reg, rule[i].tpattern, 0))
arg@191 171 free(reg);
arg@191 172 else
arg@191 173 rreg[i].tregex = reg;
arg@191 174 }
arg@191 175 }
arg@191 176 }
arg@191 177
arg@261 178 Bool
arg@261 179 isvisible(Client *c)
arg@261 180 {
arg@261 181 unsigned int i;
arg@261 182
arg@261 183 for(i = 0; i < ntags; i++)
arg@262 184 if(c->tags[i] && seltag[i])
arg@261 185 return True;
arg@261 186 return False;
arg@261 187 }
arg@261 188
arg@191 189 void
garbeam@75 190 replacetag(Arg *arg)
garbeam@75 191 {
garbeam@75 192 int i;
arg@123 193
garbeam@75 194 if(!sel)
garbeam@75 195 return;
garbeam@75 196
arg@178 197 for(i = 0; i < ntags; i++)
arg@173 198 sel->tags[i] = False;
garbeam@75 199 appendtag(arg);
garbeam@75 200 }
garbeam@75 201
garbeam@76 202 void
arg@270 203 restack()
arg@270 204 {
arg@270 205 static unsigned int nwins = 0;
arg@270 206 static Window *wins = NULL;
arg@270 207 unsigned int f, fi, m, mi, n;
arg@270 208 Client *c;
arg@270 209 XEvent ev;
arg@270 210
arg@270 211 for(f = 0, m = 0, c = clients; c; c = c->next)
arg@270 212 if(isvisible(c)) {
arg@270 213 if(c->isfloat || arrange == dofloat)
arg@270 214 f++;
arg@270 215 else
arg@270 216 m++;
arg@270 217 }
arg@272 218 if(!(n = 2 * (f + m))) {
arg@272 219 drawstatus();
arg@272 220 return;
arg@272 221 }
arg@270 222 if(nwins < n) {
arg@270 223 nwins = n;
arg@270 224 wins = erealloc(wins, nwins * sizeof(Window));
arg@270 225 }
arg@270 226
arg@270 227 fi = 0;
arg@270 228 mi = 2 * f;
arg@270 229 if(sel->isfloat || arrange == dofloat) {
arg@270 230 wins[fi++] = sel->title;
arg@270 231 wins[fi++] = sel->win;
arg@270 232 }
arg@270 233 else {
arg@270 234 wins[mi++] = sel->title;
arg@270 235 wins[mi++] = sel->win;
arg@270 236 }
arg@270 237 for(c = clients; c; c = c->next)
arg@270 238 if(isvisible(c) && c != sel) {
arg@270 239 if(c->isfloat || arrange == dofloat) {
arg@270 240 wins[fi++] = c->title;
arg@270 241 wins[fi++] = c->win;
arg@270 242 }
arg@270 243 else {
arg@270 244 wins[mi++] = c->title;
arg@270 245 wins[mi++] = c->win;
arg@270 246 }
arg@270 247 }
arg@270 248 XRestackWindows(dpy, wins, n);
arg@270 249 drawall();
arg@270 250 XSync(dpy, False);
arg@270 251 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
arg@270 252 }
arg@270 253
arg@270 254 void
garbeam@76 255 settags(Client *c)
garbeam@76 256 {
arg@114 257 char classinst[256];
arg@191 258 unsigned int i, j;
arg@114 259 regmatch_t tmp;
garbeam@76 260 Bool matched = False;
arg@114 261 XClassHint ch;
garbeam@76 262
garbeam@76 263 if(XGetClassHint(dpy, c->win, &ch)) {
arg@114 264 snprintf(classinst, sizeof(classinst), "%s:%s",
arg@114 265 ch.res_class ? ch.res_class : "",
arg@114 266 ch.res_name ? ch.res_name : "");
arg@191 267 for(i = 0; !matched && i < len; i++)
arg@191 268 if(rreg[i].clregex && !regexec(rreg[i].clregex, classinst, 1, &tmp, 0)) {
arg@191 269 c->isfloat = rule[i].isfloat;
arg@191 270 for(j = 0; rreg[i].tregex && j < ntags; j++) {
arg@191 271 if(!regexec(rreg[i].tregex, tags[j], 1, &tmp, 0)) {
arg@191 272 matched = True;
arg@191 273 c->tags[j] = True;
arg@191 274 }
garbeam@76 275 }
arg@114 276 }
garbeam@76 277 if(ch.res_class)
garbeam@76 278 XFree(ch.res_class);
garbeam@76 279 if(ch.res_name)
garbeam@76 280 XFree(ch.res_name);
garbeam@76 281 }
garbeam@76 282 if(!matched)
arg@261 283 for(i = 0; i < ntags; i++)
arg@262 284 c->tags[i] = seltag[i];
garbeam@76 285 }
garbeam@76 286
garbeam@76 287 void
arg@124 288 togglemode(Arg *arg)
arg@124 289 {
arg@124 290 arrange = arrange == dofloat ? dotile : dofloat;
arg@124 291 arrange(NULL);
arg@124 292 }
arg@124 293
arg@124 294 void
garbeam@76 295 view(Arg *arg)
garbeam@76 296 {
arg@261 297 unsigned int i;
arg@261 298
arg@261 299 for(i = 0; i < ntags; i++)
arg@262 300 seltag[i] = False;
arg@262 301 seltag[arg->i] = True;
arg@262 302 arrange(NULL);
arg@262 303 }
arg@262 304
arg@262 305 void
arg@263 306 toggleview(Arg *arg)
arg@262 307 {
arg@262 308 unsigned int i;
arg@262 309
arg@262 310 seltag[arg->i] = !seltag[arg->i];
arg@262 311 for(i = 0; !seltag[i] && i < ntags; i++);
arg@262 312 if(i == ntags)
arg@262 313 seltag[arg->i] = True; /* cannot toggle last view */
garbeam@76 314 arrange(NULL);
garbeam@76 315 }