aewl

annotate tag.c @ 307:ec8a66a2b9cc

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