dwm-meillo

annotate tag.c @ 286:ff501d94de62

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