b1a2eb98ef2e3d2b673d51fdf67661c93829b78a
[dwm.git] / tag.c
1 /* (C)opyright MMVI 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 <string.h>
9 #include <sys/types.h>
10 #include <X11/Xutil.h>
11
12
13 typedef struct {
14 const char *clpattern;
15 const char *tpattern;
16 Bool isfloat;
17 } Rule;
18
19 typedef struct {
20 regex_t *clregex;
21 regex_t *tregex;
22 } RReg;
23
24 /* static */
25
26 TAGS
27 RULES
28
29 static RReg *rreg = NULL;
30 static unsigned int len = 0;
31
32 /* extern */
33
34 Client *
35 getnext(Client *c) {
36 for(; c && !isvisible(c); c = c->next);
37 return c;
38 }
39
40 Client *
41 getprev(Client *c) {
42 for(; c && !isvisible(c); c = c->prev);
43 return c;
44 }
45
46 void
47 initrregs(void) {
48 unsigned int i;
49 regex_t *reg;
50
51 if(rreg)
52 return;
53 len = sizeof rule / sizeof rule[0];
54 rreg = emallocz(len * sizeof(RReg));
55 for(i = 0; i < len; i++) {
56 if(rule[i].clpattern) {
57 reg = emallocz(sizeof(regex_t));
58 if(regcomp(reg, rule[i].clpattern, 0))
59 free(reg);
60 else
61 rreg[i].clregex = reg;
62 }
63 if(rule[i].tpattern) {
64 reg = emallocz(sizeof(regex_t));
65 if(regcomp(reg, rule[i].tpattern, 0))
66 free(reg);
67 else
68 rreg[i].tregex = reg;
69 }
70 }
71 }
72
73 void
74 settags(Client *c, Client *trans) {
75 char prop[512];
76 unsigned int i, j;
77 regmatch_t tmp;
78 Bool matched = trans != NULL;
79 XClassHint ch;
80
81 if(matched) {
82 for(i = 0; i < ntags; i++)
83 c->tags[i] = trans->tags[i];
84 }
85 else if(XGetClassHint(dpy, c->win, &ch)) {
86 snprintf(prop, sizeof prop, "%s:%s:%s",
87 ch.res_class ? ch.res_class : "",
88 ch.res_name ? ch.res_name : "", c->name);
89 for(i = 0; !matched && i < len; i++)
90 if(rreg[i].clregex && !regexec(rreg[i].clregex, prop, 1, &tmp, 0)) {
91 c->isfloat = rule[i].isfloat;
92 for(j = 0; rreg[i].tregex && j < ntags; j++) {
93 if(!regexec(rreg[i].tregex, tags[j], 1, &tmp, 0)) {
94 matched = True;
95 c->tags[j] = True;
96 }
97 }
98 }
99 if(ch.res_class)
100 XFree(ch.res_class);
101 if(ch.res_name)
102 XFree(ch.res_name);
103 }
104 if(!matched)
105 for(i = 0; i < ntags; i++)
106 c->tags[i] = seltag[i];
107 }
108
109 void
110 tag(Arg *arg) {
111 unsigned int i;
112
113 if(!sel)
114 return;
115 for(i = 0; i < ntags; i++)
116 sel->tags[i] = False;
117 sel->tags[arg->i] = True;
118 arrange();
119 }
120
121 void
122 toggletag(Arg *arg) {
123 unsigned int i;
124
125 if(!sel)
126 return;
127 sel->tags[arg->i] = !sel->tags[arg->i];
128 for(i = 0; i < ntags && !sel->tags[i]; i++);
129 if(i == ntags)
130 sel->tags[arg->i] = True;
131 arrange();
132 }