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@125
|
33 /* extern */
|
arg@125
|
34
|
garbeam@76
|
35 Client *
|
arg@142
|
36 getnext(Client *c)
|
garbeam@75
|
37 {
|
arg@261
|
38 for(; c && !isvisible(c); c = c->next);
|
garbeam@76
|
39 return c;
|
garbeam@75
|
40 }
|
garbeam@75
|
41
|
arg@127
|
42 Client *
|
arg@127
|
43 getprev(Client *c)
|
arg@127
|
44 {
|
arg@261
|
45 for(; c && !isvisible(c); c = c->prev);
|
arg@127
|
46 return c;
|
arg@127
|
47 }
|
arg@127
|
48
|
garbeam@75
|
49 void
|
arg@191
|
50 initrregs()
|
arg@191
|
51 {
|
arg@191
|
52 unsigned int i;
|
arg@191
|
53 regex_t *reg;
|
arg@191
|
54
|
arg@191
|
55 if(rreg)
|
arg@191
|
56 return;
|
arg@191
|
57 len = sizeof(rule) / sizeof(rule[0]);
|
arg@191
|
58 rreg = emallocz(len * sizeof(RReg));
|
arg@191
|
59
|
arg@191
|
60 for(i = 0; i < len; i++) {
|
arg@191
|
61 if(rule[i].clpattern) {
|
arg@191
|
62 reg = emallocz(sizeof(regex_t));
|
arg@191
|
63 if(regcomp(reg, rule[i].clpattern, 0))
|
arg@191
|
64 free(reg);
|
arg@191
|
65 else
|
arg@191
|
66 rreg[i].clregex = reg;
|
arg@191
|
67 }
|
arg@191
|
68 if(rule[i].tpattern) {
|
arg@191
|
69 reg = emallocz(sizeof(regex_t));
|
arg@191
|
70 if(regcomp(reg, rule[i].tpattern, 0))
|
arg@191
|
71 free(reg);
|
arg@191
|
72 else
|
arg@191
|
73 rreg[i].tregex = reg;
|
arg@191
|
74 }
|
arg@191
|
75 }
|
arg@191
|
76 }
|
arg@191
|
77
|
arg@270
|
78 void
|
arg@431
|
79 settags(Client *c, Client *trans)
|
garbeam@76
|
80 {
|
arg@336
|
81 char prop[512];
|
arg@191
|
82 unsigned int i, j;
|
arg@114
|
83 regmatch_t tmp;
|
arg@431
|
84 Bool matched = trans != NULL;
|
arg@114
|
85 XClassHint ch;
|
garbeam@76
|
86
|
arg@431
|
87 if(matched) {
|
arg@431
|
88 for(i = 0; i < ntags; i++)
|
arg@431
|
89 c->tags[i] = trans->tags[i];
|
arg@431
|
90 }
|
arg@431
|
91 else if(XGetClassHint(dpy, c->win, &ch)) {
|
arg@336
|
92 snprintf(prop, sizeof(prop), "%s:%s:%s",
|
arg@114
|
93 ch.res_class ? ch.res_class : "",
|
arg@336
|
94 ch.res_name ? ch.res_name : "", c->name);
|
arg@191
|
95 for(i = 0; !matched && i < len; i++)
|
arg@336
|
96 if(rreg[i].clregex && !regexec(rreg[i].clregex, prop, 1, &tmp, 0)) {
|
arg@191
|
97 c->isfloat = rule[i].isfloat;
|
arg@191
|
98 for(j = 0; rreg[i].tregex && j < ntags; j++) {
|
arg@191
|
99 if(!regexec(rreg[i].tregex, tags[j], 1, &tmp, 0)) {
|
arg@191
|
100 matched = True;
|
arg@191
|
101 c->tags[j] = True;
|
arg@191
|
102 }
|
garbeam@76
|
103 }
|
arg@114
|
104 }
|
garbeam@76
|
105 if(ch.res_class)
|
garbeam@76
|
106 XFree(ch.res_class);
|
garbeam@76
|
107 if(ch.res_name)
|
garbeam@76
|
108 XFree(ch.res_name);
|
garbeam@76
|
109 }
|
garbeam@76
|
110 if(!matched)
|
arg@261
|
111 for(i = 0; i < ntags; i++)
|
arg@262
|
112 c->tags[i] = seltag[i];
|
arg@440
|
113 if(!c->isfloat)
|
arg@440
|
114 for(c->weight = 0; c->weight < ntags && !c->tags[c->weight]; c->weight++);
|
garbeam@76
|
115 }
|
garbeam@76
|
116
|
garbeam@76
|
117 void
|
arg@284
|
118 tag(Arg *arg)
|
arg@284
|
119 {
|
arg@284
|
120 unsigned int i;
|
arg@284
|
121
|
arg@284
|
122 if(!sel)
|
arg@284
|
123 return;
|
arg@284
|
124
|
arg@284
|
125 for(i = 0; i < ntags; i++)
|
arg@284
|
126 sel->tags[i] = False;
|
arg@284
|
127 sel->tags[arg->i] = True;
|
arg@440
|
128 if(!sel->isfloat)
|
arg@440
|
129 sel->weight = arg->i;
|
arg@393
|
130 arrange(NULL);
|
arg@284
|
131 }
|
arg@284
|
132
|
arg@284
|
133 void
|
arg@284
|
134 toggletag(Arg *arg)
|
arg@284
|
135 {
|
arg@284
|
136 unsigned int i;
|
arg@284
|
137
|
arg@284
|
138 if(!sel)
|
arg@284
|
139 return;
|
arg@284
|
140
|
arg@284
|
141 sel->tags[arg->i] = !sel->tags[arg->i];
|
arg@284
|
142 for(i = 0; i < ntags && !sel->tags[i]; i++);
|
arg@284
|
143 if(i == ntags)
|
arg@284
|
144 sel->tags[arg->i] = True;
|
arg@440
|
145 if(!sel->isfloat)
|
arg@440
|
146 sel->weight = (i == ntags) ? arg->i : i;
|
arg@393
|
147 arrange(NULL);
|
arg@284
|
148 }
|