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