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.
28 #include <sys/types.h>
39 rule_t *make_rule(void)
41 rule_t *r = calloc(1, sizeof(rule_t));
42 r->class_name[0] = r->instance_name[0] = r->name[0] = r->effect[0] = '\0';
43 r->next = r->prev = NULL;
48 void add_rule(rule_t *r)
50 if (rule_head == NULL) {
51 rule_head = rule_tail = r;
59 void remove_rule(rule_t *r)
64 rule_t *prev = r->prev;
65 rule_t *next = r->next;
81 void remove_rule_by_cause(char *cause)
83 rule_t *r = rule_head;
84 char *class_name = strtok(cause, COL_TOK);
85 char *instance_name = strtok(NULL, COL_TOK);
86 char *name = strtok(NULL, COL_TOK);
88 rule_t *next = r->next;
89 if ((class_name != NULL && (streq(class_name, MATCH_ANY) || streq(r->class_name, class_name))) &&
90 (instance_name == NULL || streq(instance_name, MATCH_ANY) || streq(r->instance_name, instance_name)) &&
91 (name == NULL || streq(name, MATCH_ANY) || streq(r->name, name))) {
98 bool remove_rule_by_index(int idx)
100 for (rule_t *r = rule_head; r != NULL; r = r->next, idx--) {
109 rule_consequence_t *make_rule_consequence(void)
111 rule_consequence_t *rc = calloc(1, sizeof(rule_consequence_t));
112 rc->manage = rc->focus = rc->border = true;
119 pending_rule_t *make_pending_rule(int fd, xcb_window_t win, rule_consequence_t *csq)
121 pending_rule_t *pr = calloc(1, sizeof(pending_rule_t));
122 pr->prev = pr->next = NULL;
123 pr->event_head = pr->event_tail = NULL;
130 void add_pending_rule(pending_rule_t *pr)
135 if (pending_rule_head == NULL) {
136 pending_rule_head = pending_rule_tail = pr;
138 pending_rule_tail->next = pr;
139 pr->prev = pending_rule_tail;
140 pending_rule_tail = pr;
144 void remove_pending_rule(pending_rule_t *pr)
149 pending_rule_t *a = pr->prev;
150 pending_rule_t *b = pr->next;
157 if (pr == pending_rule_head) {
158 pending_rule_head = b;
160 if (pr == pending_rule_tail) {
161 pending_rule_tail = a;
165 event_queue_t *eq = pr->event_head;
167 event_queue_t *next = eq->next;
174 void postpone_event(pending_rule_t *pr, xcb_generic_event_t *evt)
176 event_queue_t *eq = make_event_queue(evt);
177 if (pr->event_tail == NULL) {
178 pr->event_head = pr->event_tail = eq;
180 pr->event_tail->next = eq;
181 eq->prev = pr->event_tail;
186 event_queue_t *make_event_queue(xcb_generic_event_t *evt)
188 event_queue_t *eq = calloc(1, sizeof(event_queue_t));
189 eq->prev = eq->next = NULL;
195 #define SET_CSQ_SPLIT_DIR(val) \
197 if (csq->split_dir == NULL) { \
198 csq->split_dir = calloc(1, sizeof(direction_t)); \
200 *(csq->split_dir) = (val); \
203 #define SET_CSQ_STATE(val) \
205 if (csq->state == NULL) { \
206 csq->state = calloc(1, sizeof(client_state_t)); \
208 *(csq->state) = (val); \
211 #define SET_CSQ_LAYER(val) \
213 if (csq->layer == NULL) { \
214 csq->layer = calloc(1, sizeof(stack_layer_t)); \
216 *(csq->layer) = (val); \
219 void _apply_window_type(xcb_window_t win, rule_consequence_t *csq)
221 xcb_ewmh_get_atoms_reply_t win_type;
222 if (xcb_ewmh_get_wm_window_type_reply(ewmh, xcb_ewmh_get_wm_window_type(ewmh, win), &win_type, NULL) == 1) {
223 for (unsigned int i = 0; i < win_type.atoms_len; i++) {
224 xcb_atom_t a = win_type.atoms[i];
225 if (a == ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR ||
226 a == ewmh->_NET_WM_WINDOW_TYPE_UTILITY) {
228 } else if (a == ewmh->_NET_WM_WINDOW_TYPE_DIALOG) {
229 SET_CSQ_STATE(STATE_FLOATING);
231 } else if (a == ewmh->_NET_WM_WINDOW_TYPE_DOCK ||
232 a == ewmh->_NET_WM_WINDOW_TYPE_DESKTOP ||
233 a == ewmh->_NET_WM_WINDOW_TYPE_NOTIFICATION) {
235 if (a == ewmh->_NET_WM_WINDOW_TYPE_DESKTOP) {
240 xcb_ewmh_get_atoms_reply_wipe(&win_type);
244 void _apply_window_state(xcb_window_t win, rule_consequence_t *csq)
246 xcb_ewmh_get_atoms_reply_t win_state;
247 if (xcb_ewmh_get_wm_state_reply(ewmh, xcb_ewmh_get_wm_state(ewmh, win), &win_state, NULL) == 1) {
248 for (unsigned int i = 0; i < win_state.atoms_len; i++) {
249 xcb_atom_t a = win_state.atoms[i];
250 if (a == ewmh->_NET_WM_STATE_FULLSCREEN) {
251 SET_CSQ_STATE(STATE_FULLSCREEN);
252 } else if (a == ewmh->_NET_WM_STATE_BELOW) {
253 SET_CSQ_LAYER(LAYER_BELOW);
254 } else if (a == ewmh->_NET_WM_STATE_ABOVE) {
255 SET_CSQ_LAYER(LAYER_ABOVE);
256 } else if (a == ewmh->_NET_WM_STATE_STICKY) {
260 xcb_ewmh_get_atoms_reply_wipe(&win_state);
264 void _apply_transient(xcb_window_t win, rule_consequence_t *csq)
266 xcb_window_t transient_for = XCB_NONE;
267 xcb_icccm_get_wm_transient_for_reply(dpy, xcb_icccm_get_wm_transient_for(dpy, win), &transient_for, NULL);
268 if (transient_for != XCB_NONE) {
269 SET_CSQ_STATE(STATE_FLOATING);
273 void _apply_hints(xcb_window_t win, rule_consequence_t *csq)
275 xcb_size_hints_t size_hints;
276 if (xcb_icccm_get_wm_normal_hints_reply(dpy, xcb_icccm_get_wm_normal_hints(dpy, win), &size_hints, NULL) == 1) {
277 if ((size_hints.flags & (XCB_ICCCM_SIZE_HINT_P_MIN_SIZE | XCB_ICCCM_SIZE_HINT_P_MAX_SIZE)) &&
278 size_hints.min_width == size_hints.max_width && size_hints.min_height == size_hints.max_height) {
279 SET_CSQ_STATE(STATE_FLOATING);
284 void _apply_class(xcb_window_t win, rule_consequence_t *csq)
286 xcb_icccm_get_wm_class_reply_t reply;
287 if (xcb_icccm_get_wm_class_reply(dpy, xcb_icccm_get_wm_class(dpy, win), &reply, NULL) == 1) {
288 snprintf(csq->class_name, sizeof(csq->class_name), "%s", reply.class_name);
289 snprintf(csq->instance_name, sizeof(csq->instance_name), "%s", reply.instance_name);
290 xcb_icccm_get_wm_class_reply_wipe(&reply);
294 void _apply_name(xcb_window_t win, rule_consequence_t *csq)
296 xcb_icccm_get_text_property_reply_t reply;
297 if (xcb_icccm_get_wm_name_reply(dpy, xcb_icccm_get_wm_name(dpy, win), &reply, NULL) == 1) {
298 snprintf(csq->name, sizeof(csq->name), "%s", reply.name);
299 xcb_icccm_get_text_property_reply_wipe(&reply);
303 void parse_keys_values(char *buf, rule_consequence_t *csq)
305 char *key = strtok(buf, CSQ_BLK);
306 char *value = strtok(NULL, CSQ_BLK);
307 while (key != NULL && value != NULL) {
308 parse_key_value(key, value, csq);
309 key = strtok(NULL, CSQ_BLK);
310 value = strtok(NULL, CSQ_BLK);
314 void apply_rules(xcb_window_t win, rule_consequence_t *csq)
316 _apply_window_type(win, csq);
317 _apply_window_state(win, csq);
318 _apply_transient(win, csq);
319 _apply_hints(win, csq);
320 _apply_class(win, csq);
321 _apply_name(win, csq);
323 rule_t *rule = rule_head;
324 while (rule != NULL) {
325 rule_t *next = rule->next;
326 if ((streq(rule->class_name, MATCH_ANY) || streq(rule->class_name, csq->class_name)) &&
327 (streq(rule->instance_name, MATCH_ANY) || streq(rule->instance_name, csq->instance_name)) &&
328 (streq(rule->name, MATCH_ANY) || streq(rule->name, csq->name))) {
330 snprintf(effect, sizeof(effect), "%s", rule->effect);
331 parse_keys_values(effect, csq);
332 if (rule->one_shot) {
341 bool schedule_rules(xcb_window_t win, rule_consequence_t *csq)
343 if (external_rules_command[0] == '\0') {
346 resolve_rule_consequence(csq);
348 if (pipe(fds) == -1) {
354 close(xcb_get_file_descriptor(dpy));
360 print_rule_consequence(&csq_buf, csq);
361 snprintf(wid, sizeof(wid), "%i", win);
363 execl(external_rules_command, external_rules_command, wid, csq->class_name, csq->instance_name, csq_buf, NULL);
365 err("Couldn't spawn rule command.\n");
366 } else if (pid > 0) {
368 pending_rule_t *pr = make_pending_rule(fds[0], win, csq);
369 add_pending_rule(pr);
374 void parse_rule_consequence(int fd, rule_consequence_t *csq)
381 while ((nb = read(fd, data, sizeof(data))) > 0) {
382 int end = MIN(nb, (int) sizeof(data) - 1);
384 parse_keys_values(data, csq);
388 void parse_key_value(char *key, char *value, rule_consequence_t *csq)
391 if (streq("monitor", key)) {
392 snprintf(csq->monitor_desc, sizeof(csq->monitor_desc), "%s", value);
393 } else if (streq("desktop", key)) {
394 snprintf(csq->desktop_desc, sizeof(csq->desktop_desc), "%s", value);
395 } else if (streq("node", key)) {
396 snprintf(csq->node_desc, sizeof(csq->node_desc), "%s", value);
397 } else if (streq("split_dir", key)) {
399 if (parse_direction(value, &dir)) {
400 SET_CSQ_SPLIT_DIR(dir);
402 } else if (streq("state", key)) {
404 if (parse_client_state(value, &cst)) {
407 } else if (streq("layer", key)) {
409 if (parse_stack_layer(value, &lyr)) {
412 } else if (streq("split_ratio", key)) {
414 if (sscanf(value, "%lf", &rat) == 1 && rat > 0 && rat < 1) {
415 csq->split_ratio = rat;
417 } else if (streq("rectangle", key)) {
418 if (csq->rect == NULL) {
419 csq->rect = calloc(1, sizeof(xcb_rectangle_t));
421 if (!parse_rectangle(value, csq->rect)) {
425 } else if (parse_bool(value, &v)) {
426 if (streq("hidden", key)) {
429 #define SETCSQ(name) \
430 else if (streq(#name, key)) { \
449 void list_rules(FILE *rsp)
451 for (rule_t *r = rule_head; r != NULL; r = r->next) {
452 fprintf(rsp, "%s:%s:%s %c> %s\n", r->class_name, r->instance_name, r->name, r->one_shot?'-':'=', r->effect);