dwm-meillo

annotate tag.c @ 127:1480e19f6377

using double-linked list in order to get correct prev focus handling
author arg@10ksloc.org
date Thu, 20 Jul 2006 16:54:20 +0200
parents b4b8b4236599
children 30d1302dbe3b
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
garbeam@84 21 /* CUSTOMIZE */
garbeam@84 22 static Rule rule[] = {
arg@123 23 /* class:instance tags isfloat */
arg@123 24 { "Firefox.*", { [Twww] = "www" }, False },
arg@123 25 { "Gimp.*", { 0 }, True},
garbeam@84 26 };
garbeam@84 27
garbeam@76 28 char *tags[TLast] = {
garbeam@76 29 [Tscratch] = "scratch",
garbeam@76 30 [Tdev] = "dev",
garbeam@76 31 [Twww] = "www",
garbeam@76 32 [Twork] = "work",
garbeam@76 33 };
arg@125 34
garbeam@75 35 void (*arrange)(Arg *) = dotile;
garbeam@75 36
arg@125 37 /* END CUSTOMIZE */
arg@125 38
arg@125 39 /* extern */
arg@125 40
garbeam@76 41 void
garbeam@76 42 appendtag(Arg *arg)
garbeam@75 43 {
garbeam@76 44 if(!sel)
garbeam@76 45 return;
garbeam@75 46
garbeam@76 47 sel->tags[arg->i] = tags[arg->i];
garbeam@75 48 arrange(NULL);
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;
garbeam@95 58 if(c->tags[tsel]) {
arg@99 59 resize(c, True, TopLeft);
garbeam@95 60 }
garbeam@75 61 else
garbeam@75 62 ban(c);
garbeam@75 63 }
garbeam@75 64 if(sel && !sel->tags[tsel]) {
garbeam@93 65 if((sel = getnext(clients, tsel))) {
garbeam@75 66 higher(sel);
garbeam@75 67 focus(sel);
garbeam@75 68 }
garbeam@75 69 }
garbeam@75 70 drawall();
garbeam@75 71 }
garbeam@75 72
garbeam@75 73 void
garbeam@75 74 dotile(Arg *arg)
garbeam@75 75 {
arg@123 76 int n, i, w, h;
garbeam@75 77 Client *c;
garbeam@75 78
garbeam@75 79 w = sw - mw;
garbeam@75 80 for(n = 0, c = clients; c; c = c->next)
garbeam@80 81 if(c->tags[tsel] && !c->isfloat)
garbeam@75 82 n++;
garbeam@75 83
garbeam@75 84 if(n > 1)
garbeam@75 85 h = (sh - bh) / (n - 1);
garbeam@75 86 else
garbeam@75 87 h = sh - bh;
garbeam@75 88
garbeam@75 89 for(i = 0, c = clients; c; c = c->next) {
arg@124 90 c->ismax = False;
garbeam@75 91 if(c->tags[tsel]) {
garbeam@80 92 if(c->isfloat) {
garbeam@75 93 higher(c);
arg@99 94 resize(c, True, TopLeft);
garbeam@75 95 continue;
garbeam@75 96 }
garbeam@75 97 if(n == 1) {
arg@115 98 c->x = sx;
arg@115 99 c->y = sy + bh;
arg@115 100 c->w = sw - 2 * c->border;
arg@115 101 c->h = sh - 2 * c->border - bh;
garbeam@75 102 }
garbeam@75 103 else if(i == 0) {
arg@115 104 c->x = sx;
arg@115 105 c->y = sy + bh;
arg@115 106 c->w = mw - 2 * c->border;
arg@115 107 c->h = sh - 2 * c->border - bh;
garbeam@75 108 }
arg@104 109 else if(h > bh) {
arg@115 110 c->x = sx + mw;
arg@115 111 c->y = sy + (i - 1) * h + bh;
arg@115 112 c->w = w - 2 * c->border;
arg@115 113 c->h = h - 2 * c->border;
garbeam@75 114 }
arg@104 115 else { /* fallback if h < bh */
arg@115 116 c->x = sx + mw;
arg@115 117 c->y = sy + bh;
arg@115 118 c->w = w - 2 * c->border;
arg@115 119 c->h = sh - 2 * c->border - bh;
arg@104 120 }
arg@99 121 resize(c, False, TopLeft);
garbeam@75 122 i++;
garbeam@75 123 }
garbeam@75 124 else
garbeam@75 125 ban(c);
garbeam@75 126 }
garbeam@75 127 if(!sel || (sel && !sel->tags[tsel])) {
garbeam@93 128 if((sel = getnext(clients, tsel))) {
garbeam@75 129 higher(sel);
garbeam@75 130 focus(sel);
garbeam@75 131 }
garbeam@75 132 }
garbeam@75 133 drawall();
garbeam@75 134 }
garbeam@75 135
garbeam@76 136 Client *
garbeam@93 137 getnext(Client *c, unsigned int t)
garbeam@75 138 {
garbeam@93 139 for(; c && !c->tags[t]; c = c->next);
garbeam@76 140 return c;
garbeam@75 141 }
garbeam@75 142
arg@127 143 Client *
arg@127 144 getprev(Client *c)
arg@127 145 {
arg@127 146 for(; c && !c->tags[tsel]; c = c->prev);
arg@127 147 return c;
arg@127 148 }
arg@127 149
garbeam@75 150 void
garbeam@93 151 heretag(Arg *arg)
garbeam@93 152 {
garbeam@93 153 int i;
garbeam@93 154 Client *c;
garbeam@93 155
garbeam@93 156 if(arg->i == tsel)
garbeam@93 157 return;
garbeam@93 158
garbeam@93 159 if(!(c = getnext(clients, arg->i)))
garbeam@93 160 return;
garbeam@93 161
garbeam@93 162 for(i = 0; i < TLast; i++)
garbeam@93 163 c->tags[i] = NULL;
garbeam@93 164 c->tags[tsel] = tags[tsel];
garbeam@94 165 pop(c);
garbeam@93 166 focus(c);
garbeam@93 167 }
garbeam@93 168
garbeam@93 169 void
garbeam@75 170 replacetag(Arg *arg)
garbeam@75 171 {
garbeam@75 172 int i;
arg@123 173
garbeam@75 174 if(!sel)
garbeam@75 175 return;
garbeam@75 176
garbeam@75 177 for(i = 0; i < TLast; i++)
garbeam@75 178 sel->tags[i] = NULL;
garbeam@75 179 appendtag(arg);
garbeam@75 180 }
garbeam@75 181
garbeam@76 182 void
garbeam@76 183 settags(Client *c)
garbeam@76 184 {
arg@114 185 char classinst[256];
garbeam@76 186 static unsigned int len = rule ? sizeof(rule) / sizeof(rule[0]) : 0;
garbeam@76 187 unsigned int i, j;
arg@114 188 regex_t regex;
arg@114 189 regmatch_t tmp;
garbeam@76 190 Bool matched = False;
arg@114 191 XClassHint ch;
garbeam@76 192
garbeam@76 193 if(!len) {
garbeam@76 194 c->tags[tsel] = tags[tsel];
garbeam@76 195 return;
garbeam@76 196 }
garbeam@76 197
garbeam@76 198 if(XGetClassHint(dpy, c->win, &ch)) {
arg@114 199 snprintf(classinst, sizeof(classinst), "%s:%s",
arg@114 200 ch.res_class ? ch.res_class : "",
arg@114 201 ch.res_name ? ch.res_name : "");
arg@114 202 for(i = 0; !matched && i < len; i++) {
arg@114 203 if(!regcomp(&regex, rule[i].pattern, 0)) {
arg@114 204 if(!regexec(&regex, classinst, 1, &tmp, 0)) {
arg@114 205 for(j = 0; j < TLast; j++) {
arg@114 206 if(rule[i].tags[j])
arg@114 207 matched = True;
garbeam@76 208 c->tags[j] = rule[i].tags[j];
arg@114 209 }
garbeam@80 210 c->isfloat = rule[i].isfloat;
garbeam@76 211 }
arg@114 212 regfree(&regex);
arg@114 213 }
garbeam@76 214 }
garbeam@76 215 if(ch.res_class)
garbeam@76 216 XFree(ch.res_class);
garbeam@76 217 if(ch.res_name)
garbeam@76 218 XFree(ch.res_name);
garbeam@76 219 }
garbeam@76 220 if(!matched)
garbeam@76 221 c->tags[tsel] = tags[tsel];
garbeam@76 222 }
garbeam@76 223
garbeam@76 224 void
arg@124 225 togglemode(Arg *arg)
arg@124 226 {
arg@124 227 arrange = arrange == dofloat ? dotile : dofloat;
arg@124 228 arrange(NULL);
arg@124 229 }
arg@124 230
arg@124 231 void
garbeam@76 232 view(Arg *arg)
garbeam@76 233 {
garbeam@76 234 tsel = arg->i;
garbeam@76 235 arrange(NULL);
garbeam@76 236 drawall();
garbeam@76 237 }