view tag.c @ 3:faa4cb9d7bd6

add TASKBAR and CLICK_TO_FOCUS
author paulo@localhost
date Thu, 23 Apr 2009 02:43:13 -0700
parents 7024076fa948
children e749c30135e9
line source
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>
10 /* static */
12 typedef struct {
13 const char *prop;
14 const char *tags;
15 Bool isversatile;
16 } Rule;
18 typedef struct {
19 regex_t *propregex;
20 regex_t *tagregex;
21 } Regs;
23 TAGS
24 RULES
26 static Regs *regs = NULL;
27 static unsigned int nrules = 0;
28 static unsigned int lastview = 0;
30 /* extern */
32 void
33 compileregs(void) {
34 unsigned int i;
35 regex_t *reg;
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 }
59 Bool
60 isvisible(Client *c) {
61 unsigned int i;
63 for(i = 0; i < ntags; i++)
64 if(c->tags[i] && seltag[i])
65 return True;
66 return False;
67 }
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 };
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 }
105 void
106 tag(Arg *arg) {
107 unsigned int i;
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 }
118 void
119 toggletag(Arg *arg) {
120 unsigned int i;
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 }
131 void
132 toggleview(Arg *arg) {
133 unsigned int i;
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 }
142 void
143 view(Arg *arg) {
144 unsigned int i;
145 Arg a;
147 a.i = 0;
148 setlayout(&a); /* back to default layout */
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 }
160 void
161 last_view(Arg *arg) {
162 Arg a;
164 a.i = lastview;
165 view(&a);
166 }
168 void
169 next_view(Arg *arg) {
170 int i;
171 Arg a;
173 a.i = 0;
174 setlayout(&a); /* back to default layout */
176 for(i = 0; i < ntags; i++) {
177 if (seltag[i] == True) {
178 seltag[i] = False;
179 seltag[(i + arg->i) >= 0 ? (i + arg->i) % ntags : ntags - 1] = True;
180 break;
181 }
182 }
183 lt->arrange();
184 }