aewl

annotate tag.c @ 149:332fcc6d4f88

saved 2LOC
author arg@10ksloc.org
date Tue, 01 Aug 2006 15:16:29 +0200
parents e61447a7f249
children 9bd8a1a50464
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"
garbeam@75 6
arg@114 7 #include <regex.h>
arg@114 8 #include <stdio.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@84 13 /* static */
garbeam@76 14
arg@114 15 typedef struct {
arg@114 16 const char *pattern;
arg@114 17 char *tags[TLast];
arg@114 18 Bool isfloat;
arg@114 19 } Rule;
arg@114 20
arg@146 21 TAGS
arg@146 22 RULES
garbeam@84 23
arg@146 24 void (*arrange)(Arg *) = ARRANGE;
arg@125 25
arg@125 26 /* extern */
arg@125 27
garbeam@76 28 void
garbeam@76 29 appendtag(Arg *arg)
garbeam@75 30 {
garbeam@76 31 if(!sel)
garbeam@76 32 return;
garbeam@75 33
garbeam@76 34 sel->tags[arg->i] = tags[arg->i];
garbeam@75 35 arrange(NULL);
garbeam@75 36 }
garbeam@75 37
garbeam@75 38 void
garbeam@75 39 dofloat(Arg *arg)
garbeam@75 40 {
garbeam@75 41 Client *c;
garbeam@75 42
garbeam@75 43 for(c = clients; c; c = c->next) {
arg@124 44 c->ismax = False;
garbeam@95 45 if(c->tags[tsel]) {
arg@99 46 resize(c, True, TopLeft);
garbeam@95 47 }
garbeam@75 48 else
garbeam@75 49 ban(c);
garbeam@75 50 }
garbeam@75 51 if(sel && !sel->tags[tsel]) {
arg@142 52 if((sel = getnext(clients))) {
garbeam@75 53 higher(sel);
garbeam@75 54 focus(sel);
garbeam@75 55 }
arg@143 56 else
arg@143 57 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
garbeam@75 58 }
garbeam@75 59 drawall();
garbeam@75 60 }
garbeam@75 61
garbeam@75 62 void
garbeam@75 63 dotile(Arg *arg)
garbeam@75 64 {
arg@123 65 int n, i, w, h;
garbeam@75 66 Client *c;
garbeam@75 67
garbeam@75 68 w = sw - mw;
garbeam@75 69 for(n = 0, c = clients; c; c = c->next)
garbeam@80 70 if(c->tags[tsel] && !c->isfloat)
garbeam@75 71 n++;
garbeam@75 72
garbeam@75 73 if(n > 1)
garbeam@75 74 h = (sh - bh) / (n - 1);
garbeam@75 75 else
garbeam@75 76 h = sh - bh;
garbeam@75 77
garbeam@75 78 for(i = 0, c = clients; c; c = c->next) {
arg@124 79 c->ismax = False;
garbeam@75 80 if(c->tags[tsel]) {
garbeam@80 81 if(c->isfloat) {
garbeam@75 82 higher(c);
arg@99 83 resize(c, True, TopLeft);
garbeam@75 84 continue;
garbeam@75 85 }
garbeam@75 86 if(n == 1) {
arg@115 87 c->x = sx;
arg@115 88 c->y = sy + bh;
arg@115 89 c->w = sw - 2 * c->border;
arg@115 90 c->h = sh - 2 * c->border - bh;
garbeam@75 91 }
garbeam@75 92 else if(i == 0) {
arg@115 93 c->x = sx;
arg@115 94 c->y = sy + bh;
arg@115 95 c->w = mw - 2 * c->border;
arg@115 96 c->h = sh - 2 * c->border - bh;
garbeam@75 97 }
arg@104 98 else if(h > bh) {
arg@115 99 c->x = sx + mw;
arg@115 100 c->y = sy + (i - 1) * h + bh;
arg@115 101 c->w = w - 2 * c->border;
arg@115 102 c->h = h - 2 * c->border;
garbeam@75 103 }
arg@104 104 else { /* fallback if h < bh */
arg@115 105 c->x = sx + mw;
arg@115 106 c->y = sy + bh;
arg@115 107 c->w = w - 2 * c->border;
arg@115 108 c->h = sh - 2 * c->border - bh;
arg@104 109 }
arg@99 110 resize(c, False, TopLeft);
garbeam@75 111 i++;
garbeam@75 112 }
garbeam@75 113 else
garbeam@75 114 ban(c);
garbeam@75 115 }
garbeam@75 116 if(!sel || (sel && !sel->tags[tsel])) {
arg@142 117 if((sel = getnext(clients))) {
garbeam@75 118 higher(sel);
garbeam@75 119 focus(sel);
garbeam@75 120 }
arg@143 121 else
arg@143 122 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
garbeam@75 123 }
garbeam@75 124 drawall();
garbeam@75 125 }
garbeam@75 126
garbeam@76 127 Client *
arg@142 128 getnext(Client *c)
garbeam@75 129 {
arg@142 130 for(; c && !c->tags[tsel]; c = c->next);
garbeam@76 131 return c;
garbeam@75 132 }
garbeam@75 133
arg@127 134 Client *
arg@127 135 getprev(Client *c)
arg@127 136 {
arg@127 137 for(; c && !c->tags[tsel]; c = c->prev);
arg@127 138 return c;
arg@127 139 }
arg@127 140
garbeam@75 141 void
garbeam@75 142 replacetag(Arg *arg)
garbeam@75 143 {
garbeam@75 144 int i;
arg@123 145
garbeam@75 146 if(!sel)
garbeam@75 147 return;
garbeam@75 148
garbeam@75 149 for(i = 0; i < TLast; i++)
garbeam@75 150 sel->tags[i] = NULL;
garbeam@75 151 appendtag(arg);
garbeam@75 152 }
garbeam@75 153
garbeam@76 154 void
garbeam@76 155 settags(Client *c)
garbeam@76 156 {
arg@114 157 char classinst[256];
arg@138 158 static unsigned int len = sizeof(rule) / sizeof(rule[0]);
garbeam@76 159 unsigned int i, j;
arg@114 160 regex_t regex;
arg@114 161 regmatch_t tmp;
garbeam@76 162 Bool matched = False;
arg@114 163 XClassHint ch;
garbeam@76 164
garbeam@76 165 if(XGetClassHint(dpy, c->win, &ch)) {
arg@114 166 snprintf(classinst, sizeof(classinst), "%s:%s",
arg@114 167 ch.res_class ? ch.res_class : "",
arg@114 168 ch.res_name ? ch.res_name : "");
arg@114 169 for(i = 0; !matched && i < len; i++) {
arg@114 170 if(!regcomp(&regex, rule[i].pattern, 0)) {
arg@114 171 if(!regexec(&regex, classinst, 1, &tmp, 0)) {
arg@114 172 for(j = 0; j < TLast; j++) {
arg@114 173 if(rule[i].tags[j])
arg@114 174 matched = True;
garbeam@76 175 c->tags[j] = rule[i].tags[j];
arg@114 176 }
garbeam@80 177 c->isfloat = rule[i].isfloat;
garbeam@76 178 }
arg@114 179 regfree(&regex);
arg@114 180 }
garbeam@76 181 }
garbeam@76 182 if(ch.res_class)
garbeam@76 183 XFree(ch.res_class);
garbeam@76 184 if(ch.res_name)
garbeam@76 185 XFree(ch.res_name);
garbeam@76 186 }
garbeam@76 187 if(!matched)
garbeam@76 188 c->tags[tsel] = tags[tsel];
garbeam@76 189 }
garbeam@76 190
garbeam@76 191 void
arg@124 192 togglemode(Arg *arg)
arg@124 193 {
arg@124 194 arrange = arrange == dofloat ? dotile : dofloat;
arg@124 195 arrange(NULL);
arg@124 196 }
arg@124 197
arg@124 198 void
garbeam@76 199 view(Arg *arg)
garbeam@76 200 {
garbeam@76 201 tsel = arg->i;
garbeam@76 202 arrange(NULL);
garbeam@76 203 drawall();
garbeam@76 204 }
arg@144 205
arg@144 206 void
arg@144 207 viewnext(Arg *arg)
arg@144 208 {
arg@144 209 arg->i = (tsel < TLast-1) ? tsel+1 : 0;
arg@144 210 view(arg);
arg@144 211 }
arg@144 212
arg@144 213 void
arg@144 214 viewprev(Arg *arg)
arg@144 215 {
arg@144 216 arg->i = (tsel > 0) ? tsel-1 : TLast-1;
arg@144 217 view(arg);
arg@144 218 }