dwm-meillo

annotate tag.c @ 250:a9392d33865a

removed the if(clients) check from popping code
author Anselm R.Garbe <arg@10ksloc.org>
date Fri, 11 Aug 2006 08:34:42 +0200
parents 655e38416bb8
children d6fd632d861c
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 {
garbeam@76 40 if(!sel)
garbeam@76 41 return;
garbeam@75 42
arg@173 43 sel->tags[arg->i] = True;
garbeam@75 44 arrange(NULL);
garbeam@75 45 }
garbeam@75 46
garbeam@75 47 void
garbeam@75 48 dofloat(Arg *arg)
garbeam@75 49 {
garbeam@75 50 Client *c;
garbeam@75 51
garbeam@75 52 for(c = clients; c; c = c->next) {
arg@124 53 c->ismax = False;
garbeam@95 54 if(c->tags[tsel]) {
arg@99 55 resize(c, True, TopLeft);
garbeam@95 56 }
garbeam@75 57 else
garbeam@75 58 ban(c);
garbeam@75 59 }
arg@194 60 if((sel = getnext(clients))) {
arg@194 61 higher(sel);
arg@194 62 focus(sel);
garbeam@75 63 }
arg@194 64 else
arg@194 65 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
garbeam@75 66 drawall();
garbeam@75 67 }
garbeam@75 68
garbeam@75 69 void
garbeam@75 70 dotile(Arg *arg)
garbeam@75 71 {
arg@123 72 int n, i, w, h;
garbeam@75 73 Client *c;
garbeam@75 74
garbeam@75 75 w = sw - mw;
garbeam@75 76 for(n = 0, c = clients; c; c = c->next)
garbeam@80 77 if(c->tags[tsel] && !c->isfloat)
garbeam@75 78 n++;
garbeam@75 79
garbeam@75 80 if(n > 1)
garbeam@75 81 h = (sh - bh) / (n - 1);
garbeam@75 82 else
garbeam@75 83 h = sh - bh;
garbeam@75 84
garbeam@75 85 for(i = 0, c = clients; c; c = c->next) {
arg@124 86 c->ismax = False;
garbeam@75 87 if(c->tags[tsel]) {
garbeam@80 88 if(c->isfloat) {
garbeam@75 89 higher(c);
arg@99 90 resize(c, True, TopLeft);
garbeam@75 91 continue;
garbeam@75 92 }
garbeam@75 93 if(n == 1) {
arg@115 94 c->x = sx;
arg@115 95 c->y = sy + bh;
arg@164 96 c->w = sw - 2;
arg@164 97 c->h = sh - 2 - bh;
garbeam@75 98 }
garbeam@75 99 else if(i == 0) {
arg@115 100 c->x = sx;
arg@115 101 c->y = sy + bh;
arg@164 102 c->w = mw - 2;
arg@164 103 c->h = sh - 2 - bh;
garbeam@75 104 }
arg@104 105 else if(h > bh) {
arg@115 106 c->x = sx + mw;
arg@115 107 c->y = sy + (i - 1) * h + bh;
arg@164 108 c->w = w - 2;
arg@240 109 if(i + 1 == n)
arg@240 110 c->h = sh - c->y - 2;
arg@240 111 else
arg@240 112 c->h = h - 2;
garbeam@75 113 }
arg@104 114 else { /* fallback if h < bh */
arg@115 115 c->x = sx + mw;
arg@115 116 c->y = sy + bh;
arg@164 117 c->w = w - 2;
arg@164 118 c->h = sh - 2 - bh;
arg@104 119 }
arg@99 120 resize(c, False, TopLeft);
garbeam@75 121 i++;
garbeam@75 122 }
garbeam@75 123 else
garbeam@75 124 ban(c);
garbeam@75 125 }
arg@194 126 if((sel = getnext(clients))) {
arg@194 127 higher(sel);
arg@194 128 focus(sel);
garbeam@75 129 }
arg@194 130 else
arg@194 131 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
garbeam@75 132 drawall();
garbeam@75 133 }
garbeam@75 134
garbeam@76 135 Client *
arg@142 136 getnext(Client *c)
garbeam@75 137 {
arg@142 138 for(; c && !c->tags[tsel]; 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@127 145 for(; c && !c->tags[tsel]; 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@191 178 void
garbeam@75 179 replacetag(Arg *arg)
garbeam@75 180 {
garbeam@75 181 int i;
arg@123 182
garbeam@75 183 if(!sel)
garbeam@75 184 return;
garbeam@75 185
arg@178 186 for(i = 0; i < ntags; i++)
arg@173 187 sel->tags[i] = False;
garbeam@75 188 appendtag(arg);
garbeam@75 189 }
garbeam@75 190
garbeam@76 191 void
garbeam@76 192 settags(Client *c)
garbeam@76 193 {
arg@114 194 char classinst[256];
arg@191 195 unsigned int i, j;
arg@114 196 regmatch_t tmp;
garbeam@76 197 Bool matched = False;
arg@114 198 XClassHint ch;
garbeam@76 199
garbeam@76 200 if(XGetClassHint(dpy, c->win, &ch)) {
arg@114 201 snprintf(classinst, sizeof(classinst), "%s:%s",
arg@114 202 ch.res_class ? ch.res_class : "",
arg@114 203 ch.res_name ? ch.res_name : "");
arg@191 204 for(i = 0; !matched && i < len; i++)
arg@191 205 if(rreg[i].clregex && !regexec(rreg[i].clregex, classinst, 1, &tmp, 0)) {
arg@191 206 c->isfloat = rule[i].isfloat;
arg@191 207 for(j = 0; rreg[i].tregex && j < ntags; j++) {
arg@191 208 if(!regexec(rreg[i].tregex, tags[j], 1, &tmp, 0)) {
arg@191 209 matched = True;
arg@191 210 c->tags[j] = True;
arg@191 211 }
garbeam@76 212 }
arg@114 213 }
garbeam@76 214 if(ch.res_class)
garbeam@76 215 XFree(ch.res_class);
garbeam@76 216 if(ch.res_name)
garbeam@76 217 XFree(ch.res_name);
garbeam@76 218 }
garbeam@76 219 if(!matched)
arg@173 220 c->tags[tsel] = True;
garbeam@76 221 }
garbeam@76 222
garbeam@76 223 void
arg@124 224 togglemode(Arg *arg)
arg@124 225 {
arg@124 226 arrange = arrange == dofloat ? dotile : dofloat;
arg@124 227 arrange(NULL);
arg@124 228 }
arg@124 229
arg@124 230 void
garbeam@76 231 view(Arg *arg)
garbeam@76 232 {
garbeam@76 233 tsel = arg->i;
garbeam@76 234 arrange(NULL);
garbeam@76 235 drawall();
garbeam@76 236 }
arg@144 237
arg@144 238 void
arg@144 239 viewnext(Arg *arg)
arg@144 240 {
arg@178 241 arg->i = (tsel < ntags-1) ? tsel+1 : 0;
arg@144 242 view(arg);
arg@144 243 }
arg@144 244
arg@144 245 void
arg@144 246 viewprev(Arg *arg)
arg@144 247 {
arg@178 248 arg->i = (tsel > 0) ? tsel-1 : ntags-1;
arg@144 249 view(arg);
arg@144 250 }