1 /* Copyright (c) 2012, Bastien Dejean
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 rule_t *make_rule(void)
37 rule_t *r = malloc(sizeof(rule_t));
38 r->cause[0] = r->effect[0] = '\0';
39 r->next = r->prev = NULL;
44 void add_rule(rule_t *r)
46 if (rule_head == NULL) {
47 rule_head = rule_tail = r;
55 void remove_rule(rule_t *r)
59 rule_t *prev = r->prev;
60 rule_t *next = r->next;
72 void remove_rule_by_cause(char *cause)
74 rule_t *r = rule_head;
76 rule_t *next = r->next;
77 if (streq(r->cause, cause))
83 bool remove_rule_by_index(int idx)
85 for (rule_t *r = rule_head; r != NULL; r = r->next, idx--)
93 rule_consequence_t *make_rule_conquence(void)
95 rule_consequence_t *rc = calloc(1, sizeof(rule_consequence_t));
96 rc->manage = rc->focus = rc->border = true;
100 pending_rule_t *make_pending_rule(int fd, xcb_window_t win, rule_consequence_t *csq)
102 pending_rule_t *pr = malloc(sizeof(pending_rule_t));
103 pr->prev = pr->next = NULL;
110 void add_pending_rule(pending_rule_t *pr)
114 PRINTF("add pending rule %i\n", pr->fd);
115 if (pending_rule_head == NULL) {
116 pending_rule_head = pending_rule_tail = pr;
118 pending_rule_tail->next = pr;
119 pr->prev = pending_rule_tail;
120 pending_rule_tail = pr;
124 void remove_pending_rule(pending_rule_t *pr)
128 PRINTF("remove pending rule %i\n", pr->fd);
129 pending_rule_t *a = pr->prev;
130 pending_rule_t *b = pr->next;
135 if (pr == pending_rule_head)
136 pending_rule_head = b;
137 if (pr == pending_rule_tail)
138 pending_rule_tail = a;
144 void apply_rules(xcb_window_t win, rule_consequence_t *csq)
146 xcb_ewmh_get_atoms_reply_t win_type;
148 if (xcb_ewmh_get_wm_window_type_reply(ewmh, xcb_ewmh_get_wm_window_type(ewmh, win), &win_type, NULL) == 1) {
149 for (unsigned int i = 0; i < win_type.atoms_len; i++) {
150 xcb_atom_t a = win_type.atoms[i];
151 if (a == ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR ||
152 a == ewmh->_NET_WM_WINDOW_TYPE_UTILITY) {
154 } else if (a == ewmh->_NET_WM_WINDOW_TYPE_DIALOG) {
155 csq->floating = true;
157 } else if (a == ewmh->_NET_WM_WINDOW_TYPE_DOCK ||
158 a == ewmh->_NET_WM_WINDOW_TYPE_DESKTOP ||
159 a == ewmh->_NET_WM_WINDOW_TYPE_NOTIFICATION) {
161 if (a == ewmh->_NET_WM_WINDOW_TYPE_DESKTOP)
165 xcb_ewmh_get_atoms_reply_wipe(&win_type);
168 xcb_ewmh_get_atoms_reply_t win_state;
170 if (xcb_ewmh_get_wm_state_reply(ewmh, xcb_ewmh_get_wm_state(ewmh, win), &win_state, NULL) == 1) {
171 for (unsigned int i = 0; i < win_state.atoms_len; i++) {
172 xcb_atom_t a = win_state.atoms[i];
173 if (a == ewmh->_NET_WM_STATE_FULLSCREEN)
174 csq->fullscreen = true;
175 else if (a == ewmh->_NET_WM_STATE_STICKY)
178 xcb_ewmh_get_atoms_reply_wipe(&win_state);
181 xcb_size_hints_t size_hints;
182 if (xcb_icccm_get_wm_normal_hints_reply(dpy, xcb_icccm_get_wm_normal_hints(dpy, win), &size_hints, NULL) == 1) {
183 if (size_hints.min_width > 0 && size_hints.min_height > 0 &&
184 size_hints.min_width == size_hints.max_width &&
185 size_hints.min_height == size_hints.max_height)
186 csq->floating = true;
187 csq->min_width = size_hints.min_width;
188 csq->max_width = size_hints.max_width;
189 csq->min_height = size_hints.min_height;
190 csq->max_height = size_hints.max_height;
193 xcb_window_t transient_for = XCB_NONE;
194 xcb_icccm_get_wm_transient_for_reply(dpy, xcb_icccm_get_wm_transient_for(dpy, win), &transient_for, NULL);
195 if (transient_for != XCB_NONE)
196 csq->floating = true;
198 xcb_icccm_get_wm_class_reply_t reply;
199 if (xcb_icccm_get_wm_class_reply(dpy, xcb_icccm_get_wm_class(dpy, win), &reply, NULL) == 1) {
200 snprintf(csq->class_name, sizeof(csq->class_name), "%s", reply.class_name);
201 snprintf(csq->instance_name, sizeof(csq->instance_name), "%s", reply.instance_name);
202 xcb_icccm_get_wm_class_reply_wipe(&reply);
205 rule_t *rule = rule_head;
206 while (rule != NULL) {
207 rule_t *next = rule->next;
208 if (streq(rule->cause, MATCH_ANY) ||
209 streq(rule->cause, csq->class_name) ||
210 streq(rule->cause, csq->instance_name)) {
212 snprintf(effect, sizeof(effect), "%s", rule->effect);
213 char *key = strtok(effect, CSQ_BLK);
214 char *value = strtok(NULL, CSQ_BLK);
215 while (key != NULL && value != NULL) {
216 parse_key_value(key, value, csq);
217 key = strtok(NULL, CSQ_BLK);
218 value = strtok(NULL, CSQ_BLK);
227 bool schedule_rules(xcb_window_t win, rule_consequence_t *csq)
229 if (external_rules_command[0] == '\0')
237 close(xcb_get_file_descriptor(dpy));
241 snprintf(wid, sizeof(wid), "%i", win);
243 execl(external_rules_command, external_rules_command, wid, csq->class_name, csq->instance_name, NULL);
244 err("Couldn't spawn rule command.\n");
245 } else if (pid > 0) {
247 pending_rule_t *pr = make_pending_rule(fds[0], win, csq);
248 add_pending_rule(pr);
253 void parse_rule_consequence(int fd, rule_consequence_t *csq)
259 while ((nb = read(fd, data, sizeof(data))) > 0) {
260 int end = MIN(nb, (int) sizeof(data) - 1);
262 char *key = strtok(data, CSQ_BLK);
263 char *value = strtok(NULL, CSQ_BLK);
264 while (key != NULL && value != NULL) {
265 parse_key_value(key, value, csq);
266 key = strtok(NULL, CSQ_BLK);
267 value = strtok(NULL, CSQ_BLK);
272 void parse_key_value(char *key, char *value, rule_consequence_t *csq)
275 if (streq("monitor", key)) {
276 snprintf(csq->monitor_desc, sizeof(csq->monitor_desc), "%s", value);
277 } else if (streq("desktop", key)) {
278 snprintf(csq->desktop_desc, sizeof(csq->desktop_desc), "%s", value);
279 } else if (streq("window", key)) {
280 snprintf(csq->node_desc, sizeof(csq->node_desc), "%s", value);
281 } else if (streq("split_dir", key)) {
282 snprintf(csq->split_dir, sizeof(csq->split_dir), "%s", value);
283 } else if (streq("split_ratio", key)) {
285 if (sscanf(value, "%lf", &rat) == 1 && rat > 0 && rat < 1) {
286 csq->split_ratio = rat;
288 } else if (parse_bool(value, &v)) {
289 if (streq("floating", key))
291 #define SETCSQ(name) \
292 else if (streq(#name, key)) \
308 void list_rules(char *pattern, FILE *rsp)
310 for (rule_t *r = rule_head; r != NULL; r = r->next) {
311 if (pattern != NULL && !streq(pattern, r->cause))
313 fprintf(rsp, "%s => %s\n", r->cause, r->effect);