annotate tag.c @ 3:faa4cb9d7bd6

add TASKBAR and CLICK_TO_FOCUS
author paulo@localhost
date Thu, 23 Apr 2009 02:43:13 -0700
parents 7024076fa948
children e749c30135e9
rev   line source
paulo@0 1 /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
paulo@0 2 * See LICENSE file for license details.
paulo@0 3 */
paulo@0 4 #include "dwm.h"
paulo@0 5 #include <regex.h>
paulo@0 6 #include <stdio.h>
paulo@0 7 #include <stdlib.h>
paulo@0 8 #include <X11/Xutil.h>
paulo@0 9
paulo@0 10 /* static */
paulo@0 11
paulo@0 12 typedef struct {
paulo@0 13 const char *prop;
paulo@0 14 const char *tags;
paulo@0 15 Bool isversatile;
paulo@0 16 } Rule;
paulo@0 17
paulo@0 18 typedef struct {
paulo@0 19 regex_t *propregex;
paulo@0 20 regex_t *tagregex;
paulo@0 21 } Regs;
paulo@0 22
paulo@0 23 TAGS
paulo@0 24 RULES
paulo@0 25
paulo@0 26 static Regs *regs = NULL;
paulo@0 27 static unsigned int nrules = 0;
paulo@0 28 static unsigned int lastview = 0;
paulo@0 29
paulo@0 30 /* extern */
paulo@0 31
paulo@0 32 void
paulo@0 33 compileregs(void) {
paulo@0 34 unsigned int i;
paulo@0 35 regex_t *reg;
paulo@0 36
paulo@0 37 if(regs)
paulo@0 38 return;
paulo@0 39 nrules = sizeof rule / sizeof rule[0];
paulo@0 40 regs = emallocz(nrules * sizeof(Regs));
paulo@0 41 for(i = 0; i < nrules; i++) {
paulo@0 42 if(rule[i].prop) {
paulo@0 43 reg = emallocz(sizeof(regex_t));
paulo@0 44 if(regcomp(reg, rule[i].prop, REG_EXTENDED))
paulo@0 45 free(reg);
paulo@0 46 else
paulo@0 47 regs[i].propregex = reg;
paulo@0 48 }
paulo@0 49 if(rule[i].tags) {
paulo@0 50 reg = emallocz(sizeof(regex_t));
paulo@0 51 if(regcomp(reg, rule[i].tags, REG_EXTENDED))
paulo@0 52 free(reg);
paulo@0 53 else
paulo@0 54 regs[i].tagregex = reg;
paulo@0 55 }
paulo@0 56 }
paulo@0 57 }
paulo@0 58
paulo@0 59 Bool
paulo@0 60 isvisible(Client *c) {
paulo@0 61 unsigned int i;
paulo@0 62
paulo@0 63 for(i = 0; i < ntags; i++)
paulo@0 64 if(c->tags[i] && seltag[i])
paulo@0 65 return True;
paulo@0 66 return False;
paulo@0 67 }
paulo@0 68
paulo@0 69 void
paulo@0 70 settags(Client *c, Client *trans) {
paulo@0 71 char prop[512];
paulo@0 72 unsigned int i, j;
paulo@0 73 regmatch_t tmp;
paulo@0 74 Bool matched = trans != NULL;
paulo@0 75 XClassHint ch = { 0 };
paulo@0 76
paulo@0 77 if(matched)
paulo@0 78 for(i = 0; i < ntags; i++)
paulo@0 79 c->tags[i] = trans->tags[i];
paulo@0 80 else {
paulo@0 81 XGetClassHint(dpy, c->win, &ch);
paulo@0 82 snprintf(prop, sizeof prop, "%s:%s:%s",
paulo@0 83 ch.res_class ? ch.res_class : "",
paulo@0 84 ch.res_name ? ch.res_name : "", c->name);
paulo@0 85 for(i = 0; i < nrules; i++)
paulo@0 86 if(regs[i].propregex && !regexec(regs[i].propregex, prop, 1, &tmp, 0)) {
paulo@0 87 c->isversatile = rule[i].isversatile;
paulo@0 88 for(j = 0; regs[i].tagregex && j < ntags; j++) {
paulo@0 89 if(!regexec(regs[i].tagregex, tags[j], 1, &tmp, 0)) {
paulo@0 90 matched = True;
paulo@0 91 c->tags[j] = True;
paulo@0 92 }
paulo@0 93 }
paulo@0 94 }
paulo@0 95 if(ch.res_class)
paulo@0 96 XFree(ch.res_class);
paulo@0 97 if(ch.res_name)
paulo@0 98 XFree(ch.res_name);
paulo@0 99 }
paulo@0 100 if(!matched)
paulo@0 101 for(i = 0; i < ntags; i++)
paulo@0 102 c->tags[i] = seltag[i];
paulo@0 103 }
paulo@0 104
paulo@0 105 void
paulo@0 106 tag(Arg *arg) {
paulo@0 107 unsigned int i;
paulo@0 108
paulo@0 109 if(!sel)
paulo@0 110 return;
paulo@0 111 for(i = 0; i < ntags; i++)
paulo@0 112 sel->tags[i] = (arg->i == -1) ? True : False;
paulo@0 113 if(arg->i >= 0 && arg->i < ntags)
paulo@0 114 sel->tags[arg->i] = True;
paulo@0 115 lt->arrange();
paulo@0 116 }
paulo@0 117
paulo@0 118 void
paulo@0 119 toggletag(Arg *arg) {
paulo@0 120 unsigned int i;
paulo@0 121
paulo@0 122 if(!sel)
paulo@0 123 return;
paulo@0 124 sel->tags[arg->i] = !sel->tags[arg->i];
paulo@0 125 for(i = 0; i < ntags && !sel->tags[i]; i++);
paulo@0 126 if(i == ntags)
paulo@0 127 sel->tags[arg->i] = True;
paulo@0 128 lt->arrange();
paulo@0 129 }
paulo@0 130
paulo@0 131 void
paulo@0 132 toggleview(Arg *arg) {
paulo@0 133 unsigned int i;
paulo@0 134
paulo@0 135 seltag[arg->i] = !seltag[arg->i];
paulo@0 136 for(i = 0; i < ntags && !seltag[i]; i++);
paulo@0 137 if(i == ntags)
paulo@0 138 seltag[arg->i] = True; /* cannot toggle last view */
paulo@0 139 lt->arrange();
paulo@0 140 }
paulo@0 141
paulo@0 142 void
paulo@0 143 view(Arg *arg) {
paulo@0 144 unsigned int i;
paulo@0 145 Arg a;
paulo@0 146
paulo@0 147 a.i = 0;
paulo@0 148 setlayout(&a); /* back to default layout */
paulo@0 149
paulo@0 150 for(i = 0; i < ntags; i++) {
paulo@0 151 if (seltag[i] == True)
paulo@0 152 lastview = i;
paulo@0 153 seltag[i] = (arg->i == -1) ? True : False;
paulo@0 154 }
paulo@0 155 if(arg->i >= 0 && arg->i < ntags)
paulo@0 156 seltag[arg->i] = True;
paulo@0 157 lt->arrange();
paulo@0 158 }
paulo@0 159
paulo@0 160 void
paulo@0 161 last_view(Arg *arg) {
paulo@0 162 Arg a;
paulo@0 163
paulo@0 164 a.i = lastview;
paulo@0 165 view(&a);
paulo@0 166 }
paulo@1 167
paulo@1 168 void
paulo@1 169 next_view(Arg *arg) {
paulo@1 170 int i;
paulo@1 171 Arg a;
paulo@1 172
paulo@1 173 a.i = 0;
paulo@1 174 setlayout(&a); /* back to default layout */
paulo@1 175
paulo@1 176 for(i = 0; i < ntags; i++) {
paulo@1 177 if (seltag[i] == True) {
paulo@1 178 seltag[i] = False;
paulo@1 179 seltag[(i + arg->i) >= 0 ? (i + arg->i) % ntags : ntags - 1] = True;
paulo@1 180 break;
paulo@1 181 }
paulo@1 182 }
paulo@1 183 lt->arrange();
paulo@1 184 }