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