tag.c (view raw)
1/* See LICENSE file for copyright and license details. */
2#include "dwm.h"
3#include <regex.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7#include <X11/Xatom.h>
8#include <X11/Xutil.h>
9
10/* static */
11
12typedef struct {
13 const char *prop;
14 const char *tags;
15 Bool isfloating;
16} Rule;
17
18typedef struct {
19 regex_t *propregex;
20 regex_t *tagregex;
21} Regs;
22
23TAGS
24RULES
25
26static Regs *regs = NULL;
27static unsigned int nrules = 0;
28static char prop[512];
29
30static void
31persistconfig(Client *c) {
32 unsigned int i;
33
34 for(i = 0; i < ntags && i < sizeof prop - 1; i++)
35 prop[i] = c->tags[i] ? '1' : '0';
36 if(i < sizeof prop - 1)
37 prop[i++] = c->isfloating ? '1' : '0';
38 prop[i] = '\0';
39 XChangeProperty(dpy, c->win, dwmconfig, XA_STRING, 8,
40 PropModeReplace, (unsigned char *)prop, i);
41}
42
43/* extern */
44
45void
46compileregs(void) {
47 unsigned int i;
48 regex_t *reg;
49
50 if(regs)
51 return;
52 nrules = sizeof rule / sizeof rule[0];
53 regs = emallocz(nrules * sizeof(Regs));
54 for(i = 0; i < nrules; i++) {
55 if(rule[i].prop) {
56 reg = emallocz(sizeof(regex_t));
57 if(regcomp(reg, rule[i].prop, REG_EXTENDED))
58 free(reg);
59 else
60 regs[i].propregex = reg;
61 }
62 if(rule[i].tags) {
63 reg = emallocz(sizeof(regex_t));
64 if(regcomp(reg, rule[i].tags, REG_EXTENDED))
65 free(reg);
66 else
67 regs[i].tagregex = reg;
68 }
69 }
70}
71
72Bool
73isvisible(Client *c) {
74 unsigned int i;
75
76 for(i = 0; i < ntags; i++)
77 if(c->tags[i] && seltag[i])
78 return True;
79 return False;
80}
81
82void
83settags(Client *c, Client *trans) {
84 unsigned int i, j;
85 regmatch_t tmp;
86 Bool matched = trans != NULL;
87 XClassHint ch = { 0 };
88 XTextProperty name;
89
90 if(matched) {
91 for(i = 0; i < ntags; i++)
92 c->tags[i] = trans->tags[i];
93 }
94 else {
95 /* check if window has set a property */
96 name.nitems = 0;
97 XGetTextProperty(dpy, c->win, &name, dwmconfig);
98 if(name.nitems && name.encoding == XA_STRING) {
99 strncpy(prop, (char *)name.value, sizeof prop - 1);
100 prop[sizeof prop - 1] = '\0';
101 XFree(name.value);
102 for(i = 0; i < ntags && i < sizeof prop - 1 && prop[i] != '\0'; i++)
103 if((c->tags[i] = prop[i] == '1'))
104 matched = True;
105 if(i < sizeof prop - 1 && prop[i] != '\0')
106 c->isfloating = prop[i] == '1';
107 }
108 }
109 if(!matched) {
110 /* rule matching */
111 XGetClassHint(dpy, c->win, &ch);
112 snprintf(prop, sizeof prop, "%s:%s:%s",
113 ch.res_class ? ch.res_class : "",
114 ch.res_name ? ch.res_name : "", c->name);
115 for(i = 0; i < nrules; i++)
116 if(regs[i].propregex && !regexec(regs[i].propregex, prop, 1, &tmp, 0)) {
117 c->isfloating = rule[i].isfloating;
118 for(j = 0; regs[i].tagregex && j < ntags; j++) {
119 if(!regexec(regs[i].tagregex, tags[j], 1, &tmp, 0)) {
120 matched = True;
121 c->tags[j] = True;
122 }
123 }
124 }
125 if(ch.res_class)
126 XFree(ch.res_class);
127 if(ch.res_name)
128 XFree(ch.res_name);
129 }
130 if(!matched)
131 for(i = 0; i < ntags; i++)
132 c->tags[i] = seltag[i];
133 persistconfig(c);
134}
135
136void
137tag(const char *arg) {
138 int i;
139
140 if(!sel)
141 return;
142 for(i = 0; i < ntags; i++)
143 sel->tags[i] = arg == NULL;
144 i = arg ? atoi(arg) : 0;
145 if(i >= 0 && i < ntags)
146 sel->tags[i] = True;
147 if(sel)
148 persistconfig(sel);
149 arrange();
150}
151
152void
153togglefloating(const char *arg) {
154 if(!sel || isfloating())
155 return;
156 sel->isfloating = !sel->isfloating;
157 if(sel->isfloating)
158 resize(sel, sel->x, sel->y, sel->w, sel->h, True);
159 arrange();
160}
161
162void
163toggletag(const char *arg) {
164 int i, j;
165
166 if(!sel)
167 return;
168 i = arg ? atoi(arg) : 0;
169 sel->tags[i] = !sel->tags[i];
170 for(j = 0; j < ntags && !sel->tags[j]; j++);
171 if(j == ntags)
172 sel->tags[i] = True;
173 if(sel)
174 persistconfig(sel);
175 arrange();
176}
177
178void
179toggleview(const char *arg) {
180 int i, j;
181
182 i = arg ? atoi(arg) : 0;
183 seltag[i] = !seltag[i];
184 for(j = 0; j < ntags && !seltag[j]; j++);
185 if(j == ntags)
186 seltag[i] = True; /* cannot toggle last view */
187 arrange();
188}
189
190void
191view(const char *arg) {
192 int i;
193
194 for(i = 0; i < ntags; i++)
195 seltag[i] = arg == NULL;
196 i = arg ? atoi(arg) : 0;
197 if(i >= 0 && i < ntags)
198 seltag[i] = True;
199 arrange();
200}